Form.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // $Revision: 1.12 $
  27. // $Modtime: $
  28. // $Log: Form.cs,v $
  29. // Revision 1.12 2004/09/23 19:08:59 jackson
  30. // Temp build fixage
  31. //
  32. // Revision 1.11 2004/09/22 20:09:44 pbartok
  33. // - Added Form.ControllCollection class
  34. // - Added handling for Form owners: Owner, OwnedForms, AddOwnedForm,
  35. // RemoveOwnedForm (still incomplete, missing on-top and common
  36. // minimize/maximize behaviour)
  37. // - Added StartPosition property (still incomplete, does not use when
  38. // creating the form)
  39. // - Added ShowDialog() methods (still incomplete, missing forcing the dialog
  40. // modal)
  41. //
  42. // Revision 1.10 2004/09/13 16:56:04 pbartok
  43. // - Fixed #region names
  44. // - Moved properties and methods into their proper #regions
  45. //
  46. // Revision 1.9 2004/09/13 16:51:29 pbartok
  47. // - Added Accept and CancelButton properties
  48. // - Added ProcessDialogKey() method
  49. //
  50. // Revision 1.8 2004/09/01 02:05:18 pbartok
  51. // - Added (partial) implementation of DialogResult; rest needs to be
  52. // implemented when the modal loop code is done
  53. //
  54. // Revision 1.7 2004/08/23 22:10:02 pbartok
  55. // - Fixed handling of WM_CLOSE message
  56. // - Removed debug output
  57. //
  58. // Revision 1.6 2004/08/22 21:10:30 pbartok
  59. // - Removed OverlappedWindow style from Control, instead it's default
  60. // now is child
  61. // - Made form windows OverlappedWindow by default
  62. //
  63. // Revision 1.5 2004/08/19 21:30:37 pbartok
  64. // - Added handling of WM_CLOSE
  65. //
  66. // Revision 1.4 2004/08/11 22:20:59 pbartok
  67. // - Signature fixes
  68. //
  69. // Revision 1.3 2004/08/04 21:13:47 pbartok
  70. // - Added AutoScale properties
  71. //
  72. // Revision 1.2 2004/07/13 15:31:45 jordi
  73. // commit: new properties and fixes form size problems
  74. //
  75. // Revision 1.1 2004/07/09 05:21:25 pbartok
  76. // - Initial check-in
  77. //
  78. //
  79. // NOT COMPLETE
  80. using System;
  81. using System.Drawing;
  82. using System.ComponentModel;
  83. using System.Collections;
  84. using System.Runtime.InteropServices;
  85. using System.Threading;
  86. namespace System.Windows.Forms {
  87. public class Form : ContainerControl {
  88. #region Local Variables
  89. internal bool closing;
  90. private static bool autoscale;
  91. private static Size autoscale_base_size;
  92. private bool is_modal;
  93. internal bool end_modal; // This var is being monitored by the application modal loop
  94. private IButtonControl accept_button;
  95. private IButtonControl cancel_button;
  96. private DialogResult dialog_result;
  97. private FormStartPosition start_position;
  98. private Form owner;
  99. private Form.ControlCollection owned_forms;
  100. #endregion // Local Variables
  101. #region Public Classes
  102. public class ControlCollection : Control.ControlCollection {
  103. Form form_owner;
  104. public ControlCollection(Form owner) : base(owner) {
  105. this.form_owner = owner;
  106. }
  107. public override void Add(Control value) {
  108. base.Add(value);
  109. ((Form)value).owner = form_owner;
  110. }
  111. public override void Remove(Control value) {
  112. ((Form)value).owner = null;
  113. base.Remove (value);
  114. }
  115. }
  116. #endregion // Public Classes
  117. #region Public Constructor & Destructor
  118. public Form() {
  119. closing = false;
  120. is_modal = false;
  121. end_modal = false;
  122. dialog_result = DialogResult.None;
  123. start_position = FormStartPosition.WindowsDefaultLocation;
  124. }
  125. #endregion // Public Constructor & Destructor
  126. #region Private and Internal Methods
  127. public DialogResult ShowDialog(Control owner) {
  128. if (is_modal) {
  129. return DialogResult.None;
  130. }
  131. if (owner != null) {
  132. //this.Parent = owner;
  133. } else {
  134. ;; // get an owner
  135. }
  136. if (IsHandleCreated) {
  137. // if (owner != this.owner) {
  138. // this.RecreateHandle();
  139. // }
  140. } else {
  141. CreateControl();
  142. }
  143. Show();
  144. is_modal = true;
  145. Application.ModalRun(this);
  146. is_modal = false;
  147. Hide();
  148. return DialogResult;
  149. }
  150. #endregion // Private and Internal Methods
  151. #region Public Static Properties
  152. #endregion // Public Static Properties
  153. #region Public Instance Properties
  154. public IButtonControl AcceptButton {
  155. get {
  156. return accept_button;
  157. }
  158. set {
  159. accept_button = value;
  160. }
  161. }
  162. public bool AutoScale {
  163. get {
  164. return autoscale;
  165. }
  166. set {
  167. autoscale=value;
  168. }
  169. }
  170. public virtual Size AutoScaleBaseSize {
  171. get {
  172. return autoscale_base_size;
  173. }
  174. set {
  175. autoscale_base_size=value;
  176. }
  177. }
  178. public IButtonControl CancelButton {
  179. get {
  180. return cancel_button;
  181. }
  182. set {
  183. cancel_button = value;
  184. }
  185. }
  186. public DialogResult DialogResult {
  187. get {
  188. return dialog_result;
  189. }
  190. set {
  191. dialog_result = value;
  192. if (is_modal && (dialog_result != DialogResult.None)) {
  193. end_modal = true;
  194. }
  195. }
  196. }
  197. public bool Modal {
  198. get {
  199. return is_modal;
  200. }
  201. }
  202. public Form[] OwnedForms {
  203. get {
  204. Form[] form_list;
  205. form_list = new Form[owned_forms.Count];
  206. for (int i=0; i<owned_forms.Count; i++) {
  207. form_list[i] = (Form)owned_forms[i];
  208. }
  209. return form_list;
  210. }
  211. }
  212. public Form Owner {
  213. get {
  214. return owner;
  215. }
  216. set {
  217. if (owner != value) {
  218. owner.RemoveOwnedForm(this);
  219. owner = value;
  220. owner.AddOwnedForm(this);
  221. }
  222. }
  223. }
  224. public FormStartPosition StartPosition {
  225. get {
  226. return start_position;
  227. }
  228. set {
  229. start_position = value;
  230. }
  231. }
  232. #endregion // Public Instance Properties
  233. #region Protected Instance Properties
  234. [MonoTODO("Need to add MDI support")]
  235. protected override CreateParams CreateParams {
  236. get {
  237. CreateParams create_params = new CreateParams();
  238. create_params.Caption = "";
  239. create_params.ClassName=XplatUI.DefaultClassName;
  240. create_params.ClassStyle = 0;
  241. create_params.ExStyle=0;
  242. create_params.Parent=IntPtr.Zero;
  243. create_params.Param=0;
  244. create_params.X = Left;
  245. create_params.Y = Top;
  246. create_params.Width = Width;
  247. create_params.Height = Height;
  248. create_params.Style = (int)WindowStyles.WS_OVERLAPPEDWINDOW;
  249. create_params.Style |= (int)WindowStyles.WS_VISIBLE;
  250. switch(start_position) {
  251. case FormStartPosition.CenterParent: {
  252. if (Parent!=null && Width>0 && Height>0) {
  253. Size ParentSize;
  254. ParentSize = Parent.Size;
  255. create_params.X = Parent.Size.Width/2-Width/2;
  256. create_params.Y = Parent.Size.Height/2-Height/2;
  257. }
  258. break;
  259. }
  260. case FormStartPosition.CenterScreen: {
  261. if (Width>0 && Height>0) {
  262. Size DisplaySize;
  263. XplatUI.GetDisplaySize(out DisplaySize);
  264. create_params.X = DisplaySize.Width/2-Width/2;
  265. create_params.Y = DisplaySize.Height/2-Height/2;
  266. }
  267. break;
  268. }
  269. case FormStartPosition.Manual: {
  270. break;
  271. }
  272. case FormStartPosition.WindowsDefaultBounds: {
  273. create_params.X = -1;
  274. create_params.Y = -1;
  275. create_params.Width = -1;
  276. create_params.Height = -1;
  277. break;
  278. }
  279. case FormStartPosition.WindowsDefaultLocation: {
  280. create_params.X = -1;
  281. create_params.Y = -1;
  282. break;
  283. }
  284. }
  285. return create_params;
  286. }
  287. }
  288. protected override Size DefaultSize {
  289. get {
  290. return new Size (250, 250);
  291. }
  292. }
  293. #endregion // Protected Instance Properties
  294. #region Public Static Methods
  295. #endregion // Public Static Methods
  296. #region Public Instance Methods
  297. public void AddOwnedForm(Form ownedForm) {
  298. owned_forms.Add(ownedForm);
  299. }
  300. public void RemoveOwnedForm(Form ownedForm) {
  301. owned_forms.Remove(ownedForm);
  302. }
  303. public DialogResult ShowDialog() {
  304. return ShowDialog(null);
  305. }
  306. public DialogResult ShowDialog(IWin32Window owner) {
  307. return ShowDialog(Control.FromHandle(owner.Handle));
  308. }
  309. #endregion // Public Instance Methods
  310. #region Protected Instance Methods
  311. protected override bool ProcessDialogKey(Keys keyData) {
  312. if (keyData == Keys.Enter && accept_button != null) {
  313. accept_button.PerformClick();
  314. return true;
  315. } else if (keyData == Keys.Enter && cancel_button != null) {
  316. cancel_button.PerformClick();
  317. return true;
  318. }
  319. return base.ProcessDialogKey(keyData);
  320. }
  321. protected override void WndProc(ref Message m) {
  322. switch((Msg)m.Msg) {
  323. case Msg.WM_CLOSE: {
  324. CancelEventArgs args = new CancelEventArgs(true);
  325. OnClosing(args);
  326. if (!args.Cancel) {
  327. OnClosed(EventArgs.Empty);
  328. base.WndProc(ref m);
  329. break;
  330. }
  331. closing = true;
  332. break;
  333. }
  334. default: {
  335. base.WndProc (ref m);
  336. break;
  337. }
  338. }
  339. }
  340. #endregion // Protected Instance Methods
  341. #region Events
  342. protected virtual void OnActivated(EventArgs e) {
  343. if (Activated != null) {
  344. Activated(this, e);
  345. }
  346. }
  347. protected virtual void OnClosed(EventArgs e) {
  348. if (Closed != null) {
  349. Closed(this, e);
  350. }
  351. }
  352. protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
  353. if (Closing != null) {
  354. Closing(this, e);
  355. }
  356. }
  357. protected virtual void OnDeactivate(EventArgs e) {
  358. if (Deactivate != null) {
  359. Deactivate(this, e);
  360. }
  361. }
  362. protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
  363. if (InputLanguageChanged!=null) {
  364. InputLanguageChanged(this, e);
  365. }
  366. }
  367. protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
  368. if (InputLanguageChanging!=null) {
  369. InputLanguageChanging(this, e);
  370. }
  371. }
  372. protected virtual void OnLoad(EventArgs e) {
  373. if (Load != null) {
  374. Load(this, e);
  375. }
  376. }
  377. protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
  378. if (MaximizedBoundsChanged != null) {
  379. MaximizedBoundsChanged(this, e);
  380. }
  381. }
  382. protected virtual void OnMaximumSizeChanged(EventArgs e) {
  383. if (MaximumSizeChanged != null) {
  384. MaximumSizeChanged(this, e);
  385. }
  386. }
  387. protected virtual void OnMdiChildActivate(EventArgs e) {
  388. if (MdiChildActivate != null) {
  389. MdiChildActivate(this, e);
  390. }
  391. }
  392. protected virtual void OnMenuComplete(EventArgs e) {
  393. if (MenuComplete != null) {
  394. MenuComplete(this, e);
  395. }
  396. }
  397. protected virtual void OnMenuStart(EventArgs e) {
  398. if (MenuStart != null) {
  399. MenuStart(this, e);
  400. }
  401. }
  402. protected virtual void OnMinimumSizeChanged(EventArgs e) {
  403. if (MinimumSizeChanged != null) {
  404. MinimumSizeChanged(this, e);
  405. }
  406. }
  407. public event EventHandler Activated;
  408. public event EventHandler Closed;
  409. public event CancelEventHandler Closing;
  410. public event EventHandler Deactivate;
  411. public event InputLanguageChangedEventHandler InputLanguageChanged;
  412. public event InputLanguageChangingEventHandler InputLanguageChanging;
  413. public event EventHandler Load;
  414. public event EventHandler MaximizedBoundsChanged;
  415. public event EventHandler MaximumSizeChanged;
  416. public event EventHandler MdiChildActivate;
  417. public event EventHandler MenuComplete;
  418. public event EventHandler MenuStart;
  419. public event EventHandler MinimumSizeChanged;
  420. #endregion // Events
  421. }
  422. }