Form.cs 14 KB

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