Form.cs 14 KB

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