Form.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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.18 $
  27. // $Modtime: $
  28. // $Log: Form.cs,v $
  29. // Revision 1.18 2004/10/18 04:47:09 pbartok
  30. // - Fixed Form.ControlCollection to handle owner relations
  31. // - Added Owner/OwnedForms handling
  32. // - Implemented Z-Ordering for owned forms
  33. // - Removed unneeded private overload of ShowDialog
  34. // - Fixed ShowDialog, added the X11 incarnation of modal handling (or so I
  35. // hope)
  36. // - Fixed Close(), had wrong default
  37. // - Added firing of OnLoad event
  38. // - Added some commented out debug code for Ownership handling
  39. //
  40. // Revision 1.17 2004/10/15 12:43:19 jordi
  41. // menu work, mainmenu, subitems, etc
  42. //
  43. // Revision 1.16 2004/10/14 06:17:58 ravindra
  44. // Fixed class signature. ShowDialog (Control) is not a public method.
  45. //
  46. // Revision 1.15 2004/10/08 08:50:29 jordi
  47. // more menu work
  48. //
  49. // Revision 1.14 2004/10/02 19:05:52 pbartok
  50. // - Added KeyPreview property
  51. // - Added Menu property (still incomplete, pending Jordi's menu work)
  52. // - Implemented ProcessCmdKey
  53. // - Implemented ProcessDialogKey
  54. // - Implemented ProcessKeyPreview
  55. //
  56. // Revision 1.13 2004/10/01 17:53:26 jackson
  57. // Implement the Close method so work on MessageBox can continue.
  58. //
  59. // Revision 1.12 2004/09/23 19:08:59 jackson
  60. // Temp build fixage
  61. //
  62. // Revision 1.11 2004/09/22 20:09:44 pbartok
  63. // - Added Form.ControllCollection class
  64. // - Added handling for Form owners: Owner, OwnedForms, AddOwnedForm,
  65. // RemoveOwnedForm (still incomplete, missing on-top and common
  66. // minimize/maximize behaviour)
  67. // - Added StartPosition property (still incomplete, does not use when
  68. // creating the form)
  69. // - Added ShowDialog() methods (still incomplete, missing forcing the dialog
  70. // modal)
  71. //
  72. // Revision 1.10 2004/09/13 16:56:04 pbartok
  73. // - Fixed #region names
  74. // - Moved properties and methods into their proper #regions
  75. //
  76. // Revision 1.9 2004/09/13 16:51:29 pbartok
  77. // - Added Accept and CancelButton properties
  78. // - Added ProcessDialogKey() method
  79. //
  80. // Revision 1.8 2004/09/01 02:05:18 pbartok
  81. // - Added (partial) implementation of DialogResult; rest needs to be
  82. // implemented when the modal loop code is done
  83. //
  84. // Revision 1.7 2004/08/23 22:10:02 pbartok
  85. // - Fixed handling of WM_CLOSE message
  86. // - Removed debug output
  87. //
  88. // Revision 1.6 2004/08/22 21:10:30 pbartok
  89. // - Removed OverlappedWindow style from Control, instead it's default
  90. // now is child
  91. // - Made form windows OverlappedWindow by default
  92. //
  93. // Revision 1.5 2004/08/19 21:30:37 pbartok
  94. // - Added handling of WM_CLOSE
  95. //
  96. // Revision 1.4 2004/08/11 22:20:59 pbartok
  97. // - Signature fixes
  98. //
  99. // Revision 1.3 2004/08/04 21:13:47 pbartok
  100. // - Added AutoScale properties
  101. //
  102. // Revision 1.2 2004/07/13 15:31:45 jordi
  103. // commit: new properties and fixes form size problems
  104. //
  105. // Revision 1.1 2004/07/09 05:21:25 pbartok
  106. // - Initial check-in
  107. //
  108. //
  109. // NOT COMPLETE
  110. using System;
  111. using System.Drawing;
  112. using System.ComponentModel;
  113. using System.Collections;
  114. using System.Runtime.InteropServices;
  115. using System.Threading;
  116. namespace System.Windows.Forms {
  117. public class Form : ContainerControl {
  118. #region Local Variables
  119. internal bool closing;
  120. private static bool autoscale;
  121. private static Size autoscale_base_size;
  122. private bool is_modal;
  123. internal bool end_modal; // This var is being monitored by the application modal loop
  124. private IButtonControl accept_button;
  125. private IButtonControl cancel_button;
  126. private DialogResult dialog_result;
  127. private FormStartPosition start_position;
  128. private Form owner;
  129. private Form.ControlCollection owned_forms;
  130. private bool key_preview;
  131. private MainMenu menu;
  132. #endregion // Local Variables
  133. #region Public Classes
  134. public class ControlCollection : Control.ControlCollection {
  135. Form form_owner;
  136. public ControlCollection(Form owner) : base(owner) {
  137. this.form_owner = owner;
  138. }
  139. public override void Add(Control value) {
  140. for (int i=0; i<list.Count; i++) {
  141. if (list[i]==value) {
  142. // Do we need to do anything here?
  143. return;
  144. }
  145. }
  146. list.Add(value);
  147. ((Form)value).owner=(Form)owner;
  148. }
  149. public override void Remove(Control value) {
  150. ((Form)value).owner = null;
  151. base.Remove (value);
  152. }
  153. }
  154. #endregion // Public Classes
  155. #region Public Constructor & Destructor
  156. public Form() {
  157. closing = false;
  158. is_modal = false;
  159. end_modal = false;
  160. dialog_result = DialogResult.None;
  161. start_position = FormStartPosition.WindowsDefaultLocation;
  162. key_preview = false;
  163. menu = null;
  164. owned_forms = new Form.ControlCollection(this);
  165. MouseDown += new MouseEventHandler (OnMouseDownForm);
  166. MouseMove += new MouseEventHandler (OnMouseMoveForm);
  167. }
  168. #endregion // Public Constructor & Destructor
  169. #region Private and Internal Methods
  170. private void OnMouseDownForm (object sender, MouseEventArgs e)
  171. {
  172. if (menu != null)
  173. menu.OnMouseDown (this, e);
  174. }
  175. private void OnMouseMoveForm (object sender, MouseEventArgs e)
  176. {
  177. if (menu != null)
  178. menu.OnMouseMove (this, e);
  179. }
  180. private void OnDrawMenu (Graphics dc)
  181. {
  182. if (menu != null) {
  183. Rectangle rect = new Rectangle (0,0, Width, 0);
  184. MenuAPI.DrawMenuBar (dc, menu.Handle, rect);
  185. }
  186. }
  187. #endregion // Private and Internal Methods
  188. #region Public Static Properties
  189. #endregion // Public Static Properties
  190. #region Public Instance Properties
  191. public IButtonControl AcceptButton {
  192. get {
  193. return accept_button;
  194. }
  195. set {
  196. accept_button = value;
  197. }
  198. }
  199. public bool AutoScale {
  200. get {
  201. return autoscale;
  202. }
  203. set {
  204. autoscale=value;
  205. }
  206. }
  207. public virtual Size AutoScaleBaseSize {
  208. get {
  209. return autoscale_base_size;
  210. }
  211. set {
  212. autoscale_base_size=value;
  213. }
  214. }
  215. public IButtonControl CancelButton {
  216. get {
  217. return cancel_button;
  218. }
  219. set {
  220. cancel_button = value;
  221. }
  222. }
  223. public DialogResult DialogResult {
  224. get {
  225. return dialog_result;
  226. }
  227. set {
  228. dialog_result = value;
  229. if (is_modal && (dialog_result != DialogResult.None)) {
  230. end_modal = true;
  231. }
  232. }
  233. }
  234. public bool KeyPreview {
  235. get {
  236. return key_preview;
  237. }
  238. set {
  239. key_preview = value;
  240. }
  241. }
  242. public MainMenu Menu {
  243. get {
  244. return menu;
  245. }
  246. set {
  247. if (menu != value) {
  248. // FIXME - I have to wait for jordi to finish menus before I can do some of this
  249. // We'll need a way to store what form owns the menu inside the menu; I'd
  250. // have to set this here.
  251. menu = value;
  252. menu.SetForm (this);
  253. }
  254. }
  255. }
  256. public bool Modal {
  257. get {
  258. return is_modal;
  259. }
  260. }
  261. public Form[] OwnedForms {
  262. get {
  263. Form[] form_list;
  264. form_list = new Form[owned_forms.Count];
  265. for (int i=0; i<owned_forms.Count; i++) {
  266. form_list[i] = (Form)owned_forms[i];
  267. }
  268. return form_list;
  269. }
  270. }
  271. public Form Owner {
  272. get {
  273. return owner;
  274. }
  275. set {
  276. if (owner != value) {
  277. if (owner != null) {
  278. owner.RemoveOwnedForm(this);
  279. }
  280. owner = value;
  281. owner.AddOwnedForm(this);
  282. if (owner != null) {
  283. XplatUI.SetTopmost(this.window.Handle, true);
  284. } else {
  285. XplatUI.SetTopmost(this.window.Handle, false);
  286. }
  287. }
  288. }
  289. }
  290. public FormStartPosition StartPosition {
  291. get {
  292. return start_position;
  293. }
  294. set {
  295. start_position = value;
  296. }
  297. }
  298. #endregion // Public Instance Properties
  299. #region Protected Instance Properties
  300. [MonoTODO("Need to add MDI support")]
  301. protected override CreateParams CreateParams {
  302. get {
  303. CreateParams create_params = new CreateParams();
  304. create_params.Caption = "";
  305. create_params.ClassName=XplatUI.DefaultClassName;
  306. create_params.ClassStyle = 0;
  307. create_params.ExStyle=0;
  308. create_params.Parent=IntPtr.Zero;
  309. create_params.Param=0;
  310. create_params.X = Left;
  311. create_params.Y = Top;
  312. create_params.Width = Width;
  313. create_params.Height = Height;
  314. create_params.Style = (int)WindowStyles.WS_OVERLAPPEDWINDOW;
  315. create_params.Style |= (int)WindowStyles.WS_OVERLAPPED;
  316. //create_params.Style |= (int)WindowStyles.WS_VISIBLE;
  317. create_params.Style |= (int)WindowStyles.WS_CLIPSIBLINGS;
  318. create_params.Style |= (int)WindowStyles.WS_CLIPCHILDREN;
  319. switch(start_position) {
  320. case FormStartPosition.CenterParent: {
  321. if (Parent!=null && Width>0 && Height>0) {
  322. Size ParentSize;
  323. ParentSize = Parent.Size;
  324. create_params.X = Parent.Size.Width/2-Width/2;
  325. create_params.Y = Parent.Size.Height/2-Height/2;
  326. }
  327. break;
  328. }
  329. case FormStartPosition.CenterScreen: {
  330. if (Width>0 && Height>0) {
  331. Size DisplaySize;
  332. XplatUI.GetDisplaySize(out DisplaySize);
  333. create_params.X = DisplaySize.Width/2-Width/2;
  334. create_params.Y = DisplaySize.Height/2-Height/2;
  335. }
  336. break;
  337. }
  338. case FormStartPosition.Manual: {
  339. break;
  340. }
  341. case FormStartPosition.WindowsDefaultBounds: {
  342. create_params.X = -1;
  343. create_params.Y = -1;
  344. create_params.Width = -1;
  345. create_params.Height = -1;
  346. break;
  347. }
  348. case FormStartPosition.WindowsDefaultLocation: {
  349. create_params.X = -1;
  350. create_params.Y = -1;
  351. break;
  352. }
  353. }
  354. return create_params;
  355. }
  356. }
  357. protected override Size DefaultSize {
  358. get {
  359. return new Size (250, 250);
  360. }
  361. }
  362. protected override void OnPaint (PaintEventArgs pevent)
  363. {
  364. base.OnPaint (pevent);
  365. OnDrawMenu (pevent.Graphics);
  366. }
  367. #endregion // Protected Instance Properties
  368. #region Public Static Methods
  369. #endregion // Public Static Methods
  370. #region Public Instance Methods
  371. public void AddOwnedForm(Form ownedForm) {
  372. owned_forms.Add(ownedForm);
  373. }
  374. public void RemoveOwnedForm(Form ownedForm) {
  375. owned_forms.Remove(ownedForm);
  376. }
  377. public DialogResult ShowDialog() {
  378. return ShowDialog(null);
  379. }
  380. public DialogResult ShowDialog(IWin32Window ownerWin32) {
  381. Control owner = null;
  382. if (ownerWin32 != null) {
  383. owner = Control.FromHandle(ownerWin32.Handle);
  384. }
  385. if (is_modal) {
  386. return DialogResult.None;
  387. }
  388. if (Visible) {
  389. throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
  390. }
  391. if (!IsHandleCreated) {
  392. CreateControl();
  393. }
  394. XplatUI.SetModal(window.Handle, true);
  395. Show();
  396. is_modal = true;
  397. Application.ModalRun(this);
  398. is_modal = false;
  399. Hide();
  400. XplatUI.SetModal(window.Handle, false);
  401. return DialogResult;
  402. }
  403. public void Close ()
  404. {
  405. CancelEventArgs args = new CancelEventArgs ();
  406. OnClosing (args);
  407. if (!args.Cancel) {
  408. OnClosed (EventArgs.Empty);
  409. closing = true;
  410. return;
  411. }
  412. }
  413. #endregion // Public Instance Methods
  414. #region Protected Instance Methods
  415. protected override void OnCreateControl() {
  416. base.OnCreateControl ();
  417. OnLoad(EventArgs.Empty);
  418. }
  419. protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
  420. if (base.ProcessCmdKey (ref msg, keyData)) {
  421. return true;
  422. }
  423. // Give our menu a shot
  424. if (menu != null) {
  425. return menu.ProcessCmdKey(ref msg, keyData);
  426. }
  427. return false;
  428. }
  429. protected override bool ProcessDialogKey(Keys keyData) {
  430. if (keyData == Keys.Enter && accept_button != null) {
  431. accept_button.PerformClick();
  432. return true;
  433. } else if (keyData == Keys.Enter && cancel_button != null) {
  434. cancel_button.PerformClick();
  435. return true;
  436. }
  437. return base.ProcessDialogKey(keyData);
  438. }
  439. protected override bool ProcessKeyPreview(ref Message msg) {
  440. if (key_preview) {
  441. if (ProcessKeyEventArgs(ref msg)) {
  442. return true;
  443. }
  444. }
  445. return base.ProcessKeyPreview (ref msg);
  446. }
  447. protected override void WndProc(ref Message m) {
  448. switch((Msg)m.Msg) {
  449. case Msg.WM_CLOSE: {
  450. CancelEventArgs args = new CancelEventArgs();
  451. OnClosing(args);
  452. if (!args.Cancel) {
  453. OnClosed(EventArgs.Empty);
  454. closing = true;
  455. base.WndProc(ref m);
  456. break;
  457. }
  458. break;
  459. }
  460. #if topmost_workaround
  461. case Msg.WM_ACTIVATE: {
  462. if (this.OwnedForms.Length>0) {
  463. XplatUI.SetZOrder(this.OwnedForms[0].window.Handle, this.window.Handle, false, false);
  464. }
  465. break;
  466. }
  467. #endif
  468. default: {
  469. base.WndProc (ref m);
  470. break;
  471. }
  472. }
  473. }
  474. #endregion // Protected Instance Methods
  475. #region Events
  476. protected virtual void OnActivated(EventArgs e) {
  477. if (Activated != null) {
  478. Activated(this, e);
  479. }
  480. }
  481. protected virtual void OnClosed(EventArgs e) {
  482. if (Closed != null) {
  483. Closed(this, e);
  484. }
  485. }
  486. protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
  487. if (Closing != null) {
  488. Closing(this, e);
  489. }
  490. }
  491. protected virtual void OnDeactivate(EventArgs e) {
  492. if (Deactivate != null) {
  493. Deactivate(this, e);
  494. }
  495. }
  496. protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
  497. if (InputLanguageChanged!=null) {
  498. InputLanguageChanged(this, e);
  499. }
  500. }
  501. protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
  502. if (InputLanguageChanging!=null) {
  503. InputLanguageChanging(this, e);
  504. }
  505. }
  506. protected virtual void OnLoad(EventArgs e) {
  507. if (Load != null) {
  508. Load(this, e);
  509. }
  510. }
  511. protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
  512. if (MaximizedBoundsChanged != null) {
  513. MaximizedBoundsChanged(this, e);
  514. }
  515. }
  516. protected virtual void OnMaximumSizeChanged(EventArgs e) {
  517. if (MaximumSizeChanged != null) {
  518. MaximumSizeChanged(this, e);
  519. }
  520. }
  521. protected virtual void OnMdiChildActivate(EventArgs e) {
  522. if (MdiChildActivate != null) {
  523. MdiChildActivate(this, e);
  524. }
  525. }
  526. protected virtual void OnMenuComplete(EventArgs e) {
  527. if (MenuComplete != null) {
  528. MenuComplete(this, e);
  529. }
  530. }
  531. protected virtual void OnMenuStart(EventArgs e) {
  532. if (MenuStart != null) {
  533. MenuStart(this, e);
  534. }
  535. }
  536. protected virtual void OnMinimumSizeChanged(EventArgs e) {
  537. if (MinimumSizeChanged != null) {
  538. MinimumSizeChanged(this, e);
  539. }
  540. }
  541. public event EventHandler Activated;
  542. public event EventHandler Closed;
  543. public event CancelEventHandler Closing;
  544. public event EventHandler Deactivate;
  545. public event InputLanguageChangedEventHandler InputLanguageChanged;
  546. public event InputLanguageChangingEventHandler InputLanguageChanging;
  547. public event EventHandler Load;
  548. public event EventHandler MaximizedBoundsChanged;
  549. public event EventHandler MaximumSizeChanged;
  550. public event EventHandler MdiChildActivate;
  551. public event EventHandler MenuComplete;
  552. public event EventHandler MenuStart;
  553. public event EventHandler MinimumSizeChanged;
  554. #endregion // Events
  555. }
  556. }