implemented clipping, selection single and multiple, and bug fixing
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Form.cs
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    pbartok@novell.com
24 //
25 //
26 // $Revision: 1.22 $
27 // $Modtime: $
28 // $Log: Form.cs,v $
29 // Revision 1.22  2004/11/08 20:50:29  pbartok
30 // - Fixed arguments for updated SetTopmost method
31 //
32 // Revision 1.21  2004/11/04 14:47:58  jordi
33 // collection completion, drawing issues, missing features
34 //
35 // Revision 1.20  2004/10/29 15:55:26  jordi
36 // Menu key navigation, itemcollection completion, menu fixes
37 //
38 // Revision 1.19  2004/10/20 03:56:23  pbartok
39 // - Added private FormParentWindow class which acts as the container for
40 //   our form and as the non-client area where menus are drawn
41 // - Added/Moved required tie-ins to Jordi's menus
42 // - Fixed/Implemented the FormStartPosition functionality
43 //
44 // Revision 1.18  2004/10/18 04:47:09  pbartok
45 // - Fixed Form.ControlCollection to handle owner relations
46 // - Added Owner/OwnedForms handling
47 // - Implemented Z-Ordering for owned forms
48 // - Removed unneeded private overload of ShowDialog
49 // - Fixed ShowDialog, added the X11 incarnation of modal handling (or so I
50 //   hope)
51 // - Fixed Close(), had wrong default
52 // - Added firing of OnLoad event
53 // - Added some commented out debug code for Ownership handling
54 //
55 // Revision 1.17  2004/10/15 12:43:19  jordi
56 // menu work, mainmenu, subitems, etc
57 //
58 // Revision 1.16  2004/10/14 06:17:58  ravindra
59 // Fixed class signature. ShowDialog (Control) is not a public method.
60 //
61 // Revision 1.15  2004/10/08 08:50:29  jordi
62 // more menu work
63 //
64 // Revision 1.14  2004/10/02 19:05:52  pbartok
65 // - Added KeyPreview property
66 // - Added Menu property (still incomplete, pending Jordi's menu work)
67 // - Implemented ProcessCmdKey
68 // - Implemented ProcessDialogKey
69 // - Implemented ProcessKeyPreview
70 //
71 // Revision 1.13  2004/10/01 17:53:26  jackson
72 // Implement the Close method so work on MessageBox can continue.
73 //
74 // Revision 1.12  2004/09/23 19:08:59  jackson
75 // Temp build fixage
76 //
77 // Revision 1.11  2004/09/22 20:09:44  pbartok
78 // - Added Form.ControllCollection class
79 // - Added handling for Form owners: Owner, OwnedForms, AddOwnedForm,
80 //   RemoveOwnedForm (still incomplete, missing on-top and common
81 //   minimize/maximize behaviour)
82 // - Added StartPosition property (still incomplete, does not use when
83 //   creating the form)
84 // - Added ShowDialog() methods (still incomplete, missing forcing the dialog
85 //   modal)
86 //
87 // Revision 1.10  2004/09/13 16:56:04  pbartok
88 // - Fixed #region names
89 // - Moved properties and methods into their proper #regions
90 //
91 // Revision 1.9  2004/09/13 16:51:29  pbartok
92 // - Added Accept and CancelButton properties
93 // - Added ProcessDialogKey() method
94 //
95 // Revision 1.8  2004/09/01 02:05:18  pbartok
96 // - Added (partial) implementation of DialogResult; rest needs to be
97 //   implemented when the modal loop code is done
98 //
99 // Revision 1.7  2004/08/23 22:10:02  pbartok
100 // - Fixed handling of WM_CLOSE message
101 // - Removed debug output
102 //
103 // Revision 1.6  2004/08/22 21:10:30  pbartok
104 // - Removed OverlappedWindow style from Control, instead it's default
105 //   now is child
106 // - Made form windows OverlappedWindow by default
107 //
108 // Revision 1.5  2004/08/19 21:30:37  pbartok
109 // - Added handling of WM_CLOSE
110 //
111 // Revision 1.4  2004/08/11 22:20:59  pbartok
112 // - Signature fixes
113 //
114 // Revision 1.3  2004/08/04 21:13:47  pbartok
115 // - Added AutoScale properties
116 //
117 // Revision 1.2  2004/07/13 15:31:45  jordi
118 // commit: new properties and fixes form size problems
119 //
120 // Revision 1.1  2004/07/09 05:21:25  pbartok
121 // - Initial check-in
122 //
123 //
124
125 // NOT COMPLETE
126
127 using System;
128 using System.Drawing;
129 using System.ComponentModel;
130 using System.Collections;
131 using System.Runtime.InteropServices;
132 using System.Threading;
133
134 namespace System.Windows.Forms {
135         public class Form : ContainerControl {
136                 #region Local Variables
137                 internal bool                   closing;
138
139                 [Flags]
140                 enum Flags {
141                         ShowInTaskbar   = 0x0001,
142                         MaximizeBox             = 0x0002,
143                         MinimizeBox             = 0x0004,
144                         TopMost                 = 0x0008
145                 }
146
147                 Flags flags = Flags.ShowInTaskbar | Flags.MaximizeBox | Flags.MinimizeBox;
148                 FormBorderStyle formBorderStyle = FormBorderStyle.Sizable;
149                 private static bool             autoscale;
150                 private static Size             autoscale_base_size;
151                 private bool                    is_modal;
152                 internal bool                   end_modal;                      // This var is being monitored by the application modal loop
153                 private IButtonControl          accept_button;
154                 private IButtonControl          cancel_button;
155                 private DialogResult            dialog_result;
156                 private FormStartPosition       start_position;
157                 private Form                    owner;
158                 private Form.ControlCollection  owned_forms;
159                 private bool                    key_preview;
160                 private MainMenu                menu;
161                 internal FormParentWindow       form_parent_window;
162                 #endregion      // Local Variables
163
164                 #region Private Classes
165
166                 // This class will take over for the client area
167                 internal class FormParentWindow : Control {
168                         #region FormParentWindow Class Local Variables
169                         internal Form   owner;
170                         #endregion      // FormParentWindow Class Local Variables
171
172                         #region FormParentWindow Class Constructor
173                         internal FormParentWindow(Form owner) : base() {
174                                 this.owner = owner;
175
176                                 this.Width = 250;
177                                 this.Height = 250;
178
179                                 BackColor = owner.BackColor;
180                                 Text = "FormParent";
181                                 this.Location = new Point(0, 0);
182                                 this.Dock = DockStyle.Fill;
183
184                                 MouseDown += new MouseEventHandler (OnMouseDownForm); 
185                                 MouseMove += new MouseEventHandler (OnMouseMoveForm); 
186                                 owner.TextChanged += new EventHandler(OnFormTextChanged);
187                         }
188                         #endregion      // FormParentWindow Class Constructor
189 \r
190                         #region FormParentWindow Class Protected Instance Methods\r
191                         protected override CreateParams CreateParams {\r
192                                 get {\r
193                                         CreateParams cp;\r
194 \r
195                                         cp = base.CreateParams;\r
196 \r
197                                         cp.Style = (int)(WindowStyles.WS_OVERLAPPEDWINDOW | 
198                                                 WindowStyles.WS_VISIBLE | 
199                                                 WindowStyles.WS_CLIPSIBLINGS | 
200                                                 WindowStyles.WS_CLIPCHILDREN);
201
202                                         cp.Width = 250;
203                                         cp.Height = 250;
204 \r
205 #if later\r
206                                         if (this.IsHandleCreated) {\r
207                                                 int     x;\r
208                                                 int     y;\r
209                                                 int     width;\r
210                                                 int     height;\r
211                                                 int     cwidth;\r
212                                                 int     cheight;\r
213 \r
214                                                 XplatUI.GetWindowPos(this.window.Handle, out x, out y, out width, out height, out cwidth, out cheight);\r
215                                                 UpdateBounds(x, y, width, height);\r
216                                                 owner.UpdateBounds(x, y, width, height);\r
217                                         }\r
218 \r
219 #endif\r
220                                         return cp;\r
221                                 }\r
222                         }\r
223 \r
224                         protected override void OnResize(EventArgs e) {\r
225                                 base.OnResize(e);
226                                 //owner.SetBoundsCore(owner.Bounds.X, owner.Bounds.Y, ClientSize.Width, ClientSize.Height, BoundsSpecified.All);
227                                 if (owner.menu == null) {
228                                         owner.SetBoundsCore(0, 0, ClientSize.Width, ClientSize.Height, BoundsSpecified.All);
229                                 } else {
230                                         int menu_height;
231
232                                         menu_height = MenuAPI.MenuBarCalcSize(DeviceContext, owner.Menu.menu_handle, ClientSize.Width);
233                                         Invalidate (new Rectangle (0, 0, ClientSize.Width, menu_height));                                       
234                                         owner.SetBoundsCore(0, menu_height, ClientSize.Width, ClientSize.Height-menu_height, BoundsSpecified.All);
235                                 }
236                         }\r
237 \r
238                         protected override void OnPaint(PaintEventArgs pevent) {\r
239                                 OnDrawMenu (pevent.Graphics);
240                         }\r
241 \r
242                         protected override void WndProc(ref Message m) {\r
243                                 switch((Msg)m.Msg) {\r
244                                         case Msg.WM_CLOSE: {\r
245                                                 CancelEventArgs args = new CancelEventArgs();\r
246 \r
247                                                 owner.OnClosing(args);\r
248 \r
249                                                 if (!args.Cancel) {\r
250                                                         owner.OnClosed(EventArgs.Empty);\r
251                                                         owner.closing = true;\r
252                                                         base.WndProc(ref m);\r
253                                                         break;\r
254                                                 }\r
255                                                 break;\r
256                                         }\r
257 \r
258                                         default: {\r
259                                                 base.WndProc (ref m);\r
260                                                 break;\r
261                                         }\r
262                                 }\r
263                         }\r
264                         #endregion      // FormParentWindow Class Protected Instance Methods\r
265 \r
266                         #region FormParentWindow Class Private Methods\r
267                         private void OnMouseDownForm (object sender, MouseEventArgs e) {                        
268                                 if (owner.menu != null)
269                                         owner.menu.OnMouseDown (owner, e);
270                         }
271
272                         private void OnMouseMoveForm (object sender, MouseEventArgs e) {                        
273                                 if (owner.menu != null)
274                                         owner.menu.OnMouseMove (owner, e);
275                         }
276                 
277                 
278                         private void OnDrawMenu (Graphics dc) {
279                                 if (owner.menu != null) {                                                                                                       
280                                         Rectangle rect = new Rectangle (0,0, Width, 0);                 
281                                         MenuAPI.DrawMenuBar (dc, owner.menu.Handle, rect);
282                                 }                       
283                         }
284                         private void OnFormTextChanged(object sender, EventArgs e) {\r
285                                 this.Text = ((Control)sender).Text;\r
286                         }\r
287                         #endregion      // FormParentWindow Class Private Methods
288                 }
289                 #endregion      // Private Classes
290
291                 #region Public Classes
292                 public class ControlCollection : Control.ControlCollection {
293                         Form    form_owner;
294
295                         public ControlCollection(Form owner) : base(owner) {
296                                 this.form_owner = owner;
297                         }
298
299                         public override void Add(Control value) {
300                                 for (int i=0; i<list.Count; i++) {
301                                         if (list[i]==value) {
302                                                 // Do we need to do anything here?
303                                                 return;
304                                         }
305                                 }
306                                 list.Add(value);
307                                 ((Form)value).owner=(Form)owner;
308                         }
309
310                         public override void Remove(Control value) {\r
311                                 ((Form)value).owner = null;\r
312                                 base.Remove (value);\r
313                         }\r
314                 }
315                 #endregion      // Public Classes
316                         
317                 #region Public Constructor & Destructor
318                 public Form() {
319                         closing = false;
320                         is_modal = false;
321                         end_modal = false;
322                         dialog_result = DialogResult.None;
323                         start_position = FormStartPosition.WindowsDefaultLocation;
324                         key_preview = false;
325                         menu = null;
326
327                         owned_forms = new Form.ControlCollection(this);
328                 }
329                 #endregion      // Public Constructor & Destructor
330
331
332                 #region Public Static Properties
333
334                 public static Form ActiveForm {\r
335                         get {\r
336                                 //FIXME: create next driver call\r
337                                 return null;\r
338                         }\r
339                 }\r
340
341                 #endregion      // Public Static Properties
342
343                 #region Public Instance Properties
344                 public IButtonControl AcceptButton {
345                         get {
346                                 return accept_button;
347                         }
348
349                         set {
350                                 accept_button = value;
351                         }
352                 }
353                         
354                 public bool AutoScale {
355                         get {
356                                 return autoscale;
357                         }
358
359                         set {
360                                 autoscale=value;
361                         }
362                 }
363
364                 public virtual Size AutoScaleBaseSize {
365                         get {
366                                 return autoscale_base_size;
367                         }
368
369                         set {
370                                 autoscale_base_size=value;
371                         }
372                 }
373
374                 public IButtonControl CancelButton {
375                         get {
376                                 return cancel_button;
377                         }
378
379                         set {
380                                 cancel_button = value;
381                         }
382                 }
383
384                 public DialogResult DialogResult {
385                         get {
386                                 return dialog_result;
387                         }
388
389                         set {
390                                 dialog_result = value;
391
392                                 if (is_modal && (dialog_result != DialogResult.None)) {
393                                         end_modal = true;
394                                 }
395                         }
396                 }
397
398                 public FormBorderStyle FormBorderStyle {\r
399                         get {\r
400                                 return formBorderStyle;\r
401                         }\r
402                         set {\r
403                                 formBorderStyle = value;\r
404                                 Invalidate ();\r
405                         }\r
406                 }\r
407
408
409                 public bool KeyPreview {
410                         get {
411                                 return key_preview;
412                         }
413
414                         set {
415                                 key_preview = value;
416                         }
417                 }
418
419                 public bool MaximizeBox {\r
420                         get {\r
421                                 return (flags & Flags.MaximizeBox) != 0;\r
422                         }\r
423                         set {\r
424                                 if (value)\r
425                                         flags &= ~Flags.MaximizeBox;\r
426                                 else\r
427                                         flags |= Flags.MaximizeBox;\r
428                         }\r
429                 }
430
431                 public MainMenu Menu {
432                         get {
433                                 return menu;
434                         }
435
436                         set {
437                                 if (menu != value) {
438                                         if (value == null) {
439                                                 form_parent_window.Width = form_parent_window.Width;    // Trigger a resize
440                                         }
441
442                                         menu = value;
443
444                                         // To simulate the non-client are for menus we create a 
445                                         // new control as the 'client area' of our form.  This
446                                         // way, the origin stays 0,0 and we don't have to fiddle with
447                                         // coordinates. The menu area is part of the original container
448                                         if (menu != null) {
449                                                 form_parent_window.Width = form_parent_window.Width;    // Trigger a resize
450                                         }
451
452                                         menu.SetForm (this);
453                                         MenuAPI.SetMenuBarWindow (menu.Handle, form_parent_window);
454                                 }
455                         }
456                 }
457
458                 public bool MinimizeBox {\r
459                         get {\r
460                                 return (flags & Flags.MinimizeBox) != 0;\r
461                         }\r
462                         set {\r
463                                 if (value)\r
464                                         flags &= ~Flags.MinimizeBox;\r
465                                 else\r
466                                         flags |= Flags.MinimizeBox;\r
467                         }\r
468                 }
469
470                 public bool Modal  {
471                         get {
472                                 return is_modal;
473                         }
474                 }
475
476                 public Form[] OwnedForms {
477                         get {
478                                 Form[] form_list;
479
480                                 form_list = new Form[owned_forms.Count];
481
482                                 for (int i=0; i<owned_forms.Count; i++) {
483                                         form_list[i] = (Form)owned_forms[i];
484                                 }
485
486                                 return form_list;
487                         }
488                 }
489
490                 public Form Owner {
491                         get {
492                                 return owner;
493                         }
494
495                         set {
496                                 if (owner != value) {
497                                         if (owner != null) {
498                                                 owner.RemoveOwnedForm(this);
499                                         }
500                                         owner = value;
501                                         owner.AddOwnedForm(this);
502                                         if (owner != null) {
503                                                 XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
504                                         } else {
505                                                 XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
506                                         }
507                                 }
508                         }
509                 }
510
511                 public bool ShowInTaskbar {\r
512                         get {\r
513                                 return (flags & Flags.ShowInTaskbar) != 0;\r
514                         }\r
515                         set {\r
516                                 if (value)\r
517                                         flags &= ~Flags.ShowInTaskbar;\r
518                                 else\r
519                                         flags |= Flags.ShowInTaskbar;\r
520                         }\r
521                 }\r
522
523                 public FormStartPosition StartPosition {
524                         get {
525                                 return start_position;
526                         }
527
528                         set {
529                                 if (start_position == FormStartPosition.WindowsDefaultLocation) {               // Only do this if it's not set yet
530                                         start_position = value;
531                                         if (form_parent_window.IsHandleCreated) {
532                                                 switch(start_position) {
533                                                         case FormStartPosition.CenterParent: {
534                                                                 if (Parent!=null && Width>0 && Height>0) {
535                                                                         this.Location = new Point(Parent.Size.Width/2-Width/2, Parent.Size.Height/2-Height/2);
536                                                                 }
537                                                                 break;
538                                                         }
539
540                                                         case FormStartPosition.CenterScreen: {
541                                                                 if (Width>0 && Height>0) {
542                                                                         Size    DisplaySize;
543
544                                                                         XplatUI.GetDisplaySize(out DisplaySize);
545                                                                         this.Location = new Point(DisplaySize.Width/2-Width/2, DisplaySize.Height/2-Height/2);
546                                                                 }
547                                                                 break;
548                                                         }
549
550                                                         default: {
551                                                                 break;
552                                                         }
553                                                 }
554                                         }
555                                 }
556                         }
557                 }
558
559                 public bool TopMost {\r
560                         get {\r
561                                 return (flags & Flags.TopMost) != 0;\r
562                         }\r
563                         set {\r
564                                 if (TopMost == value)\r
565                                         return;\r
566 \r
567                                 if (value)\r
568                                         flags &= ~Flags.TopMost;\r
569                                 else\r
570                                         flags |= Flags.TopMost;\r
571 \r
572                                 XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, TopMost);
573                         }\r
574                 }\r
575
576                 #endregion      // Public Instance Properties
577
578                 #region Protected Instance Properties
579                 [MonoTODO("Need to add MDI support")]
580                 protected override CreateParams CreateParams {
581                         get {
582                                 CreateParams cp = new CreateParams();
583
584                                 if (this.form_parent_window == null) {
585                                         form_parent_window = new FormParentWindow(this);
586                                 }
587
588                                 cp.Caption = "ClientArea";
589                                 cp.ClassName=XplatUI.DefaultClassName;
590                                 cp.ClassStyle = 0;
591                                 cp.ExStyle=0;
592                                 cp.Param=0;
593                                 cp.Parent = this.form_parent_window.window.Handle;
594                                 cp.X = Left;
595                                 cp.Y = Top;
596                                 cp.Width = Width;
597                                 cp.Height = Height;
598                                 
599                                 cp.Style = (int)WindowStyles.WS_CHILD;
600                                 cp.Style |= (int)WindowStyles.WS_VISIBLE;
601                                 cp.Style |= (int)WindowStyles.WS_CLIPSIBLINGS;
602                                 cp.Style |= (int)WindowStyles.WS_CLIPCHILDREN;
603
604                                 if (ShowInTaskbar)
605                                         cp.ExStyle |= (int)WindowStyles.WS_EX_APPWINDOW;
606                                 if (MaximizeBox)
607                                         cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
608                                 if (MinimizeBox)
609                                         cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
610
611                                 return cp;
612                         }
613                 }
614
615                 protected override Size DefaultSize {
616                         get {
617                                 return new Size (250, 250);
618                         }
619                 }               
620
621                 protected override void OnPaint (PaintEventArgs pevent)
622                 {
623                         base.OnPaint (pevent);
624                 }               
625                 
626                 #endregion      // Protected Instance Properties
627
628
629                 #region Public Static Methods
630                 #endregion      // Public Static Methods
631
632                 #region Public Instance Methods
633                 public void AddOwnedForm(Form ownedForm) {
634                         owned_forms.Add(ownedForm);
635                 }
636
637                 public void RemoveOwnedForm(Form ownedForm) {
638                         owned_forms.Remove(ownedForm);
639                 }
640
641                 public DialogResult ShowDialog() {
642                         return ShowDialog(null);
643                 }
644
645                 public DialogResult ShowDialog(IWin32Window ownerWin32) {
646                         Control         owner = null;
647
648                         if (ownerWin32 != null) {
649                                 owner = Control.FromHandle(ownerWin32.Handle);
650                         }
651
652                         if (is_modal) {
653                                 return DialogResult.None;
654                         }
655
656                         if (Visible) {
657                                 throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
658                         }
659
660                         if (!IsHandleCreated) {
661                                 CreateControl();
662                         }
663
664                         XplatUI.SetModal(window.Handle, true);
665
666                         Show();
667
668                         is_modal = true;
669                         Application.ModalRun(this);
670                         is_modal = false;
671                         Hide();
672
673                         XplatUI.SetModal(window.Handle, false);
674
675                         return DialogResult;
676                 }
677
678                 public void Close ()
679                 {
680                         CancelEventArgs args = new CancelEventArgs ();
681                         OnClosing (args);
682                         if (!args.Cancel) {
683                                 OnClosed (EventArgs.Empty);
684                                 closing = true;
685                                 return;
686                         }
687                 }
688
689                 #endregion      // Public Instance Methods
690
691                 #region Protected Instance Methods
692                 protected override void CreateHandle() {\r
693                         base.CreateHandle ();\r
694                 }\r
695
696                 protected override void OnCreateControl() {\r
697                         base.OnCreateControl ();\r
698                         OnLoad(EventArgs.Empty);\r
699                 }\r
700
701                 protected override void OnHandleCreated(EventArgs e) {\r
702                         base.OnHandleCreated (e);\r
703                 }\r
704
705                 protected override void OnHandleDestroyed(EventArgs e) {\r
706                         base.OnHandleDestroyed (e);\r
707                 }\r
708
709
710                 protected override void OnResize(EventArgs e) {
711                         base.OnResize(e);
712                 }
713 \r
714                 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {\r
715                         if (base.ProcessCmdKey (ref msg, keyData)) {\r
716                                 return true;\r
717                         }\r
718 \r
719                         // Give our menu a shot\r
720                         if (menu != null) {\r
721                                 return menu.ProcessCmdKey(ref msg, keyData);\r
722                         }\r
723 \r
724                         return false;\r
725                 }\r
726
727                 protected override bool ProcessDialogKey(Keys keyData) {
728                         if (keyData == Keys.Enter && accept_button != null) {
729                                 accept_button.PerformClick();
730                                 return true;
731                         } else if (keyData == Keys.Enter && cancel_button != null) {
732                                 cancel_button.PerformClick();
733                                 return true;
734                         }
735                         return base.ProcessDialogKey(keyData);
736                 }
737
738                 protected override bool ProcessKeyPreview(ref Message msg) {\r
739                         if (key_preview) {\r
740                                 if (ProcessKeyEventArgs(ref msg)) {\r
741                                         return true;\r
742                                 }\r
743                         }\r
744                         return base.ProcessKeyPreview (ref msg);\r
745                 }\r
746
747                 protected override void WndProc(ref Message m) {\r
748                         switch((Msg)m.Msg) {\r
749                                 case Msg.WM_CLOSE: {\r
750                                         CancelEventArgs args = new CancelEventArgs();\r
751 \r
752                                         OnClosing(args);\r
753 \r
754                                         if (!args.Cancel) {\r
755                                                 OnClosed(EventArgs.Empty);\r
756                                                 closing = true;\r
757                                                 base.WndProc(ref m);\r
758                                                 break;\r
759                                         }\r
760                                         break;\r
761                                 }\r
762 \r
763 #if topmost_workaround
764                                 case Msg.WM_ACTIVATE: {\r
765                                         if (this.OwnedForms.Length>0) {
766                                                 XplatUI.SetZOrder(this.OwnedForms[0].window.Handle, this.window.Handle, false, false);
767                                         }
768                                         break;\r
769                                 }\r
770 #endif\r
771                                 default: {\r
772                                         base.WndProc (ref m);\r
773                                         break;\r
774                                 }\r
775                         }\r
776                 }\r
777                 #endregion      // Protected Instance Methods
778
779                 #region Events
780                 protected virtual void OnActivated(EventArgs e) {
781                         if (Activated != null) {
782                                 Activated(this, e);
783                         }
784                 }
785
786                 protected virtual void OnClosed(EventArgs e) {
787                         if (Closed != null) {
788                                 Closed(this, e);
789                         }
790                 }
791
792                 protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
793                         if (Closing != null) {
794                                 Closing(this, e);
795                         }
796                 }
797
798                 protected virtual void OnDeactivate(EventArgs e) {
799                         if (Deactivate != null) {
800                                 Deactivate(this, e);
801                         }
802                 }
803
804                 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
805                         if (InputLanguageChanged!=null) {
806                                 InputLanguageChanged(this, e);
807                         }
808                 }
809
810                 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
811                         if (InputLanguageChanging!=null) {
812                                 InputLanguageChanging(this, e);
813                         }
814                 }
815
816                 protected virtual void OnLoad(EventArgs e) {
817                         if (Load != null) {
818                                 Load(this, e);
819                         }
820                 }
821
822                 protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
823                         if (MaximizedBoundsChanged != null) {
824                                 MaximizedBoundsChanged(this, e);
825                         }
826                 }
827
828                 protected virtual void OnMaximumSizeChanged(EventArgs e) {
829                         if (MaximumSizeChanged != null) {
830                                 MaximumSizeChanged(this, e);
831                         }
832                 }
833
834                 protected virtual void OnMdiChildActivate(EventArgs e) {
835                         if (MdiChildActivate != null) {
836                                 MdiChildActivate(this, e);
837                         }
838                 }
839
840                 protected virtual void OnMenuComplete(EventArgs e) {
841                         if (MenuComplete != null) {
842                                 MenuComplete(this, e);
843                         }
844                 }
845
846                 protected virtual void OnMenuStart(EventArgs e) {
847                         if (MenuStart != null) {
848                                 MenuStart(this, e);
849                         }
850                 }
851
852                 protected virtual void OnMinimumSizeChanged(EventArgs e) {
853                         if (MinimumSizeChanged != null) {
854                                 MinimumSizeChanged(this, e);
855                         }
856                 }
857
858                 public event EventHandler Activated;
859                 public event EventHandler Closed;
860                 public event CancelEventHandler Closing;
861                 public event EventHandler Deactivate;
862                 public event InputLanguageChangedEventHandler InputLanguageChanged;
863                 public event InputLanguageChangingEventHandler InputLanguageChanging;
864                 public event EventHandler Load;
865                 public event EventHandler MaximizedBoundsChanged;
866                 public event EventHandler MaximumSizeChanged;
867                 public event EventHandler MdiChildActivate;
868                 public event EventHandler MenuComplete;
869                 public event EventHandler MenuStart;
870                 public event EventHandler MinimumSizeChanged;
871                 #endregion      // Events
872         }
873 }