Form.cs 13 KB

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