Form.cs 13 KB

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