2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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 // NOT COMPLETE
27
28 using System;
29 using System.Drawing;
30 using System.ComponentModel;
31 using System.Collections;
32 using System.Runtime.InteropServices;
33 using System.Threading;
34
35 namespace System.Windows.Forms {
36         public class Form : ContainerControl {
37                 #region Local Variables
38                 internal static Form            active_form;
39                 internal bool                   closing;
40                 FormBorderStyle                 formBorderStyle;
41                 private static bool             autoscale;
42                 private static Size             autoscale_base_size;
43                 internal bool                   is_modal;
44                 internal bool                   end_modal;                      // This var is being monitored by the application modal loop
45                 private bool                    control_box;
46                 private bool                    minimize_box;
47                 private bool                    maximize_box;
48                 private bool                    help_button;
49                 private bool                    show_in_taskbar;
50                 private bool                    topmost;
51                 private IButtonControl          accept_button;
52                 private IButtonControl          cancel_button;
53                 private DialogResult            dialog_result;
54                 private FormStartPosition       start_position;
55                 private Form                    owner;
56                 private Form.ControlCollection  owned_forms;
57                 private bool                    key_preview;
58                 private MainMenu                menu;
59                 internal FormParentWindow       form_parent_window;
60                 private bool                    created_form_parent;
61                 private Icon                    icon;
62                 private Size                    maximum_size;
63                 private Size                    minimum_size;
64                 #endregion      // Local Variables
65
66                 #region Private Classes
67
68                 // This class will take over for the client area
69                 internal class FormParentWindow : Control {
70                         #region FormParentWindow Class Local Variables
71                         internal Form   owner;
72                         #endregion      // FormParentWindow Class Local Variables
73
74                         #region FormParentWindow Class Constructor
75                         internal FormParentWindow(Form owner) : base() {
76                                 this.owner = owner;
77
78                                 this.Width = 250;
79                                 this.Height = 250;
80
81                                 BackColor = owner.BackColor;
82                                 Text = "FormParent";
83                                 this.Dock = DockStyle.Fill;
84                                 this.is_visible = false;
85
86                                 // We must set this via the internal var, the SetTopLevel method will too much stuff
87                                 is_toplevel = true;
88
89                                 MouseDown += new MouseEventHandler (OnMouseDownForm); 
90                                 MouseMove += new MouseEventHandler (OnMouseMoveForm); 
91                                 owner.TextChanged += new EventHandler(OnFormTextChanged);
92                                 CreateControl();                // Create us right away, we have code referencing this.window
93                         }
94                         #endregion      // FormParentWindow Class Constructor
95
96                         #region FormParentWindow Class Protected Instance Methods
97                         protected override void OnResize(EventArgs e) {
98                                 base.OnResize(e);
99
100                                 if (owner.menu == null) {
101                                         owner.SetBoundsCore(0, 0, ClientSize.Width, ClientSize.Height, BoundsSpecified.All);
102                                 } else {
103                                         int menu_height;
104
105                                         menu_height = MenuAPI.MenuBarCalcSize(DeviceContext, owner.Menu.menu_handle, ClientSize.Width);
106                                         Invalidate (new Rectangle (0, 0, ClientSize.Width, menu_height));                                       
107                                         owner.SetBoundsCore(0, menu_height, ClientSize.Width, ClientSize.Height-menu_height, BoundsSpecified.All);
108                                 }
109                         }
110
111                         protected override void OnPaint(PaintEventArgs pevent) {
112                                 OnDrawMenu (pevent.Graphics);
113                         }
114
115                         protected override void Select(bool directed, bool forward) {\r
116                                 base.Select (directed, forward);\r
117                         }\r
118
119                         protected override void WndProc(ref Message m) {
120                                 switch((Msg)m.Msg) {
121                                         case Msg.WM_CLOSE: {
122                                                 CancelEventArgs args = new CancelEventArgs();
123
124                                                 owner.OnClosing(args);
125
126                                                 if (!args.Cancel) {
127                                                         owner.OnClosed(EventArgs.Empty);
128                                                         owner.closing = true;
129                                                         base.WndProc(ref m);
130                                                         break;
131                                                 }
132                                                 return;
133                                         }
134
135                                         case Msg.WM_ACTIVATE: {
136                                                 if (m.WParam != (IntPtr)WindowActiveFlags.WA_INACTIVE) {
137                                                         owner.OnActivated(EventArgs.Empty);
138                                                 } else {
139                                                         owner.OnDeactivate(EventArgs.Empty);
140                                                 }
141                                                 return;
142                                         }
143
144 #if topmost_workaround
145                                         case Msg.WM_ACTIVATE: {
146                                                         if (this.OwnedForms.Length>0) {
147                                                                 XplatUI.SetZOrder(this.OwnedForms[0].window.Handle, this.window.Handle, false, false);
148                                                         }
149                                                 break;
150                                         }
151 #endif
152
153                                         case Msg.WM_SETFOCUS: {
154                                                 owner.WndProc(ref m);
155                                                 return;
156                                         }
157
158                                         case Msg.WM_KILLFOCUS: {
159                                                 owner.WndProc(ref m);
160                                                 return;
161                                         }
162
163                                         default: {
164                                                 base.WndProc (ref m);
165                                                 return;
166                                         }
167                                 }
168                         }
169                         #endregion      // FormParentWindow Class Protected Instance Methods
170
171                         #region FormParentWindow Class Private & Internal Methods
172                         internal void MenuChanged() {
173                                 OnResize(EventArgs.Empty);
174                         }
175
176                         private void OnMouseDownForm (object sender, MouseEventArgs e) {                        
177                                 if (owner.menu != null)
178                                         owner.menu.OnMouseDown (owner, e);
179                         }
180
181                         private void OnMouseMoveForm (object sender, MouseEventArgs e) {                        
182                                 if (owner.menu != null)
183                                         owner.menu.OnMouseMove (owner, e);
184                         }
185                 
186                 
187                         private void OnDrawMenu (Graphics dc) {
188                                 if (owner.menu != null) {                                                                                                       
189                                         Rectangle rect = new Rectangle (0,0, Width, 0);                 
190                                         MenuAPI.DrawMenuBar (dc, owner.menu.Handle, rect);
191                                 }                       
192                         }
193                         private void OnFormTextChanged(object sender, EventArgs e) {
194                                 this.Text = ((Control)sender).Text;
195                         }
196                         #endregion      // FormParentWindow Class Private & Internal Methods
197                 }
198                 #endregion      // Private Classes
199
200                 #region Public Classes
201                 public new class ControlCollection : Control.ControlCollection {
202                         Form    form_owner;
203
204                         public ControlCollection(Form owner) : base(owner) {
205                                 this.form_owner = owner;
206                         }
207
208                         public override void Add(Control value) {
209                                 for (int i=0; i<list.Count; i++) {
210                                         if (list[i]==value) {
211                                                 // Do we need to do anything here?
212                                                 return;
213                                         }
214                                 }
215                                 list.Add(value);
216                                 ((Form)value).owner=(Form)owner;
217                         }
218
219                         public override void Remove(Control value) {
220                                 ((Form)value).owner = null;
221                                 base.Remove (value);
222                         }
223                 }
224                 #endregion      // Public Classes
225                         
226                 #region Public Constructor & Destructor
227                 public Form() {
228                         closing = false;
229                         is_modal = false;
230                         end_modal = false;
231                         dialog_result = DialogResult.None;
232                         start_position = FormStartPosition.WindowsDefaultLocation;
233                         formBorderStyle = FormBorderStyle.Sizable;
234                         key_preview = false;
235                         menu = null;
236                         icon = null;
237                         minimum_size = new Size(0, 0);
238                         maximum_size = new Size(0, 0);
239                         control_box = true;
240                         minimize_box = true;
241                         maximize_box = true;
242                         help_button = false;
243                         show_in_taskbar = true;
244                         ime_mode = ImeMode.NoControl;
245
246                         owned_forms = new Form.ControlCollection(this);
247                 }
248                 #endregion      // Public Constructor & Destructor
249
250
251                 #region Public Static Properties
252
253                 public static Form ActiveForm {
254                         get {
255                                 Control active;
256
257                                 active = FromHandle(XplatUI.GetActive());
258
259                                 if (active != null) {
260                                         if ( !(active is Form)) {
261                                                 if (active is FormParentWindow) {
262                                                         return ((FormParentWindow)active).owner;
263                                                 } else {
264                                                         Control parent;
265
266                                                         parent = active.Parent;
267                                                         while (parent != null) {
268                                                                 if (parent is Form) {
269                                                                         return (Form)parent;
270                                                                 }
271                                                                 parent = parent.Parent;
272                                                         }
273                                                 }
274                                         } else {
275                                                 return (Form)active;
276                                         }
277                                 }
278                                 return null;
279                         }
280                 }
281
282                 #endregion      // Public Static Properties
283
284                 #region Public Instance Properties
285                 public IButtonControl AcceptButton {
286                         get {
287                                 return accept_button;
288                         }
289
290                         set {
291                                 accept_button = value;
292                         }
293                 }
294                         
295                 public bool AutoScale {
296                         get {
297                                 return autoscale;
298                         }
299
300                         set {
301                                 autoscale=value;
302                         }
303                 }
304
305                 public virtual Size AutoScaleBaseSize {
306                         get {
307                                 return autoscale_base_size;
308                         }
309
310                         set {
311                                 autoscale_base_size=value;
312                         }
313                 }
314
315                 public IButtonControl CancelButton {
316                         get {
317                                 return cancel_button;
318                         }
319
320                         set {
321                                 cancel_button = value;
322                         }
323                 }
324
325                 public Size ClientSize {
326                         get {
327                                 return base.ClientSize;
328                         }
329
330                         set {
331                                 form_parent_window.ClientSize = value;
332                         }
333                 }
334
335                 public bool ControlBox {
336                         get {
337                                 return control_box;
338                         }
339
340                         set {
341                                 if (control_box != value) {
342                                         control_box = value;
343                                         UpdateStyles();
344                                 }
345                         }
346                 }
347
348                 public Rectangle DesktopBounds {
349                         get {
350                                 return new Rectangle(form_parent_window.Location, form_parent_window.Size);
351                         }
352
353                         set {
354                                 this.form_parent_window.Bounds = value;
355                         }
356                 }
357
358                 public Point DesktopLocation {
359                         get {
360                                 return form_parent_window.Location;
361                         }
362
363                         set {
364                                 form_parent_window.Location = value;
365                         }
366                 }
367
368                 public DialogResult DialogResult {
369                         get {
370                                 return dialog_result;
371                         }
372
373                         set {
374                                 dialog_result = value;
375
376                                 if (is_modal && (dialog_result != DialogResult.None)) {
377                                         end_modal = true;
378                                 }
379                         }
380                 }
381
382                 public FormBorderStyle FormBorderStyle {
383                         get {
384                                 return formBorderStyle;
385                         }
386                         set {
387                                 formBorderStyle = value;
388                                 Invalidate ();
389                         }
390                 }
391
392                 public bool HelpButton {
393                         get {
394                                 return help_button;
395                         }
396
397                         set {
398                                 if (help_button != value) {
399                                         help_button = value;
400                                         UpdateStyles();
401                                 }
402                         }
403                 }
404
405                 public Icon Icon {
406                         get {
407                                 return icon;
408                         }
409
410                         set {
411                                 if (icon != value) {
412                                         icon = value;
413                                 }
414                         }
415                 }
416
417                 public bool IsRestrictedWindow {
418                         get {
419                                 return false;
420                         }
421                 }
422
423                 public bool KeyPreview {
424                         get {
425                                 return key_preview;
426                         }
427
428                         set {
429                                 key_preview = value;
430                         }
431                 }
432
433                 public bool MaximizeBox {
434                         get {
435                                 return maximize_box;
436                         }
437                         set {
438                                 if (maximize_box != value) {
439                                         maximize_box = value;
440                                         UpdateStyles();
441                                 }
442                         }
443                 }
444
445                 public Size MaximumSize {
446                         get {
447                                 return maximum_size;
448                         }
449
450                         set {
451                                 if (maximum_size != value) {
452                                         maximum_size = value;
453                                 }
454                         }
455                 }
456
457                 public MainMenu Menu {
458                         get {
459                                 return menu;
460                         }
461
462                         set {
463                                 if (menu != value) {
464                                         // To simulate the non-client are for menus we create a 
465                                         // new control as the 'client area' of our form.  This
466                                         // way, the origin stays 0,0 and we don't have to fiddle with
467                                         // coordinates. The menu area is part of the original container
468
469                                         menu = value;
470
471                                         menu.SetForm (this);
472                                         MenuAPI.SetMenuBarWindow (menu.Handle, form_parent_window);
473
474                                         form_parent_window.MenuChanged();
475                                 }
476                         }
477                 }
478
479                 public bool MinimizeBox {
480                         get {
481                                 return minimize_box;
482                         }
483                         set {
484                                 if (minimize_box != value) {
485                                         minimize_box = value;
486                                         UpdateStyles();
487                                 }
488                         }
489                 }
490
491                 public Size MinimumSize {
492                         get {
493                                 return minimum_size;
494                         }
495
496                         set {
497                                 if (minimum_size != value) {
498                                         minimum_size = value;
499                                 }
500                         }
501                 }
502
503                 public bool Modal  {
504                         get {
505                                 return is_modal;
506                         }
507                 }
508
509                 public Form[] OwnedForms {
510                         get {
511                                 Form[] form_list;
512
513                                 form_list = new Form[owned_forms.Count];
514
515                                 for (int i=0; i<owned_forms.Count; i++) {
516                                         form_list[i] = (Form)owned_forms[i];
517                                 }
518
519                                 return form_list;
520                         }
521                 }
522
523                 public Form Owner {
524                         get {
525                                 return owner;
526                         }
527
528                         set {
529                                 if (owner != value) {
530                                         if (owner != null) {
531                                                 owner.RemoveOwnedForm(this);
532                                         }
533                                         owner = value;
534                                         owner.AddOwnedForm(this);
535                                         if (owner != null) {
536                                                 XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
537                                         } else {
538                                                 XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
539                                         }
540                                 }
541                         }
542                 }
543
544                 public bool ShowInTaskbar {
545                         get {
546                                 return show_in_taskbar;
547                         }
548                         set {
549                                 if (show_in_taskbar != value) {
550                                         show_in_taskbar = value;
551                                         UpdateStyles();
552                                 }
553                         }
554                 }
555
556                 public Size Size {
557                         get {
558                                 return form_parent_window.Size;
559                         }
560
561                         set {
562                                 form_parent_window.Size = value;
563                         }
564                 }
565
566                 public FormStartPosition StartPosition {
567                         get {
568                                 return start_position;
569                         }
570
571                         set {
572                                 if (start_position == FormStartPosition.WindowsDefaultLocation) {               // Only do this if it's not set yet
573                                         start_position = value;
574                                         if (form_parent_window.IsHandleCreated) {
575                                                 switch(start_position) {
576                                                         case FormStartPosition.CenterParent: {
577                                                                 if (Parent!=null && Width>0 && Height>0) {
578                                                                         this.Location = new Point(Parent.Size.Width/2-Width/2, Parent.Size.Height/2-Height/2);
579                                                                 }
580                                                                 break;
581                                                         }
582
583                                                         case FormStartPosition.CenterScreen: {
584                                                                 if (Width>0 && Height>0) {
585                                                                         Size    DisplaySize;
586
587                                                                         XplatUI.GetDisplaySize(out DisplaySize);
588                                                                         this.Location = new Point(DisplaySize.Width/2-Width/2, DisplaySize.Height/2-Height/2);
589                                                                 }
590                                                                 break;
591                                                         }
592
593                                                         default: {
594                                                                 break;
595                                                         }
596                                                 }
597                                         }
598                                 }
599                         }
600                 }
601
602                 public bool TopLevel {
603                         get {
604                                 return GetTopLevel();
605                         }
606
607                         set {
608                                 SetTopLevel(value);
609                         }
610                 }
611
612                 public bool TopMost {
613                         get {
614                                 return topmost;
615                         }
616
617                         set {
618                                 if (topmost != value) {
619                                         topmost = value;
620                                         XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, value);
621                                 }
622                         }
623                 }
624
625                 public FormWindowState WindowState {
626                         get {
627                                 return XplatUI.GetWindowState(form_parent_window.window.Handle);
628                         }
629
630                         set {
631                                 XplatUI.SetWindowState(form_parent_window.window.Handle, value);
632                         }
633                 }
634
635                 #endregion      // Public Instance Properties
636
637
638                 internal CreateParams CreateClientAreaParams {
639                         get {
640                                 CreateParams cp = new CreateParams();
641
642                                 if (this.form_parent_window == null) {
643                                         form_parent_window = new FormParentWindow(this);
644                                         form_parent_window.MenuChanged();
645                                 }
646
647                                 cp.Caption = "ClientArea";
648                                 cp.ClassName=XplatUI.DefaultClassName;
649                                 cp.ClassStyle = 0;
650                                 cp.ExStyle=0;
651                                 cp.Param=0;
652                                 cp.Parent = form_parent_window.window.Handle;
653                                 cp.X = 0;
654                                 cp.Y = 0;
655                                 cp.Width = form_parent_window.ClientSize.Width;
656                                 cp.Height = form_parent_window.ClientSize.Width;
657                                 
658                                 cp.Style = (int)WindowStyles.WS_CHILD;
659                                 cp.Style |= (int)WindowStyles.WS_VISIBLE;
660                                 cp.Style |= (int)WindowStyles.WS_CLIPSIBLINGS;
661                                 cp.Style |= (int)WindowStyles.WS_CLIPCHILDREN;
662
663                                 return cp;
664                         }
665                 }
666
667                 internal CreateParams CreateFormParams {
668                         get {
669                                 return CreateParams;
670                         }
671                 }
672
673                 #region Protected Instance Properties
674                 [MonoTODO("Need to add MDI support")]
675                 protected override CreateParams CreateParams {
676                         get {
677                                 CreateParams cp;
678
679                                 if (!created_form_parent) {
680                                         created_form_parent = true;
681                                         form_parent_window = new FormParentWindow(this);
682                                 }
683
684                                 cp = new CreateParams();
685
686                                 cp.Caption = "FormWindow";
687                                 cp.ClassName=XplatUI.DefaultClassName;
688                                 cp.ClassStyle = 0;
689                                 cp.ExStyle=0;
690                                 cp.Param=0;
691                                 cp.Parent = IntPtr.Zero;
692 //                              if (start_position == FormStartPosition.WindowsDefaultLocation) {
693                                         cp.X = unchecked((int)0x80000000);
694                                         cp.Y = unchecked((int)0x80000000);
695 //                              } else {
696 //                                      cp.X = Left;
697 //                                      cp.Y = Top;
698 //                              }
699                                 cp.Width = Width;
700                                 cp.Height = Height;
701                                 
702                                 cp.Style = (int)(WindowStyles.WS_OVERLAPPEDWINDOW | 
703                                         WindowStyles.WS_CLIPSIBLINGS | 
704                                         WindowStyles.WS_CLIPCHILDREN);
705
706                                 if (ShowInTaskbar) {
707                                         cp.ExStyle |= (int)WindowStyles.WS_EX_APPWINDOW;
708                                 }
709
710                                 if (MaximizeBox) {
711                                         cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
712                                 }
713
714                                 if (MinimizeBox) {
715                                         cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
716                                 }
717
718                                 if (ControlBox) {
719                                         cp.Style |= (int)WindowStyles.WS_SYSMENU;
720                                 }
721
722                                 if (HelpButton) {
723                                         cp.ExStyle |= (int)WindowStyles.WS_EX_CONTEXTHELP;
724                                 }
725                                 return cp;
726                         }
727                 }
728
729                 protected override Size DefaultSize {
730                         get {
731                                 return new Size (250, 250);
732                         }
733                 }               
734
735                 protected override void OnPaint (PaintEventArgs pevent)
736                 {
737                         base.OnPaint (pevent);
738                 }               
739                 
740                 #endregion      // Protected Instance Properties
741
742
743                 #region Public Static Methods
744                 #endregion      // Public Static Methods
745
746                 #region Public Instance Methods
747                 public void Activate() {
748                         Form    active;
749
750                         // The docs say activate only activates if our app is already active
751                         active = ActiveForm;
752                         if ((active != null) && (this != active)) {
753                                 XplatUI.Activate(form_parent_window.window.Handle);
754                         }
755                 }
756
757                 public void AddOwnedForm(Form ownedForm) {
758                         owned_forms.Add(ownedForm);
759                 }
760
761                 public void RemoveOwnedForm(Form ownedForm) {
762                         owned_forms.Remove(ownedForm);
763                 }
764
765                 public void SetDesktopBounds(int x, int y, int width, int height) {
766                         DesktopBounds = new Rectangle(x, y, width, height);
767                 }
768
769                 public void SetDesktopLocation(int x, int y) {
770                         DesktopLocation = new Point(x, y);
771                 }
772
773                 public DialogResult ShowDialog() {
774                         return ShowDialog(null);
775                 }
776
777                 public DialogResult ShowDialog(IWin32Window ownerWin32) {
778                         Form            previous;
779
780                         #if broken
781                         Control         owner = null;
782
783                         if (ownerWin32 != null) {
784                                 owner = Control.FromHandle(ownerWin32.Handle);
785                         }
786                         #endif
787
788                         if (is_modal) {
789                                 return DialogResult.None;
790                         }
791
792                         if (Visible) {
793                                 throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
794                         }
795
796                         #if broken
797                         // Can't do this, will screw us in the modal loop
798                         form_parent_window.Parent = owner;
799                         #endif
800
801                         previous = Form.ActiveForm;
802
803                         if (!IsHandleCreated) {
804                                 CreateControl();
805                         }
806
807                         XplatUI.SetModal(form_parent_window.window.Handle, true);
808
809                         Show();
810                         PerformLayout();
811
812                         is_modal = true;
813                         Application.ModalRun(this);
814                         is_modal = false;
815                         Hide();
816
817                         XplatUI.SetModal(form_parent_window.window.Handle, false);
818
819                         if (previous != null) {
820                                 // Cannot use Activate(), it has a check for the current active window...
821                                 XplatUI.Activate(previous.form_parent_window.window.Handle);
822                         }
823
824                         return DialogResult;
825                 }
826
827                 public void Close ()
828                 {
829                         CancelEventArgs args = new CancelEventArgs ();
830                         OnClosing (args);
831                         if (!args.Cancel) {
832                                 OnClosed (EventArgs.Empty);
833                                 closing = true;
834                                 return;
835                         }
836                 }
837
838                 #endregion      // Public Instance Methods
839
840                 #region Protected Instance Methods
841                 protected override void CreateHandle() {
842                         base.CreateHandle ();
843                 }
844
845                 protected override void OnCreateControl() {
846                         base.OnCreateControl ();
847                         if (this.ActiveControl == null) {
848                                 if (SelectNextControl(this, true, true, true, true) == false) {
849                                         Select(this);
850                                 }
851                         }
852                         OnLoad(EventArgs.Empty);
853
854                         // Send initial location
855                         OnLocationChanged(EventArgs.Empty);
856                 }
857
858                 protected override void OnHandleCreated(EventArgs e) {
859                         base.OnHandleCreated (e);
860                 }
861
862                 protected override void OnHandleDestroyed(EventArgs e) {
863                         base.OnHandleDestroyed (e);
864                 }
865
866
867                 protected override void OnResize(EventArgs e) {
868                         base.OnResize(e);
869                 }
870
871                 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
872                         if (base.ProcessCmdKey (ref msg, keyData)) {
873                                 return true;
874                         }
875
876                         // Give our menu a shot
877                         if (menu != null) {
878                                 return menu.ProcessCmdKey(ref msg, keyData);
879                         }
880
881                         return false;
882                 }
883
884                 protected override bool ProcessDialogKey(Keys keyData) {
885                         if ((keyData & Keys.Modifiers) == 0) {
886                                 if (keyData == Keys.Enter && accept_button != null) {
887                                         accept_button.PerformClick();
888                                         return true;
889                                 } else if (keyData == Keys.Escape && cancel_button != null) {
890                                         cancel_button.PerformClick();
891                                         return true;
892                                 }
893                         }
894                         return base.ProcessDialogKey(keyData);
895                 }
896
897                 protected override bool ProcessKeyPreview(ref Message msg) {
898                         if (key_preview) {
899                                 if (ProcessKeyEventArgs(ref msg)) {
900                                         return true;
901                                 }
902                         }
903                         return base.ProcessKeyPreview (ref msg);
904                 }
905
906                 protected override void SetClientSizeCore(int x, int y) {
907                         if ((minimum_size.Width != 0) && (x < minimum_size.Width)) {
908                                 x = minimum_size.Width;
909                         } else if ((maximum_size.Width != 0) && (x > maximum_size.Width)) {
910                                 x = maximum_size.Width;
911                         }
912
913                         if ((minimum_size.Height != 0) && (y < minimum_size.Height)) {
914                                 y = minimum_size.Height;
915                         } else if ((maximum_size.Height != 0) && (y > maximum_size.Height)) {
916                                 y = maximum_size.Height;
917                         }
918
919                         base.SetClientSizeCore (x, y);
920                 }
921
922
923                 protected override void WndProc(ref Message m) {
924                         switch((Msg)m.Msg) {
925                                 case Msg.WM_CLOSE: {
926                                         CancelEventArgs args = new CancelEventArgs();
927
928                                         OnClosing(args);
929
930                                         if (!args.Cancel) {
931                                                 OnClosed(EventArgs.Empty);
932                                                 closing = true;
933                                                 base.WndProc(ref m);
934                                                 break;
935                                         }
936                                         break;
937                                 }
938
939                                 case Msg.WM_KILLFOCUS: {
940                                         return;
941                                 }
942
943                                 case Msg.WM_SETFOCUS: {
944                                         if (this.ActiveControl != null) {
945                                                 ActiveControl.Focus();
946                                         }
947                                         return;
948                                 }
949
950                                 default: {
951                                         base.WndProc (ref m);
952                                         break;
953                                 }
954                         }
955                 }
956                 #endregion      // Protected Instance Methods
957
958                 #region Events
959                 protected virtual void OnActivated(EventArgs e) {
960                         if (Activated != null) {
961                                 Activated(this, e);
962                         }
963                 }
964
965                 protected virtual void OnClosed(EventArgs e) {
966                         if (Closed != null) {
967                                 Closed(this, e);
968                         }
969                 }
970
971                 protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
972                         if (Closing != null) {
973                                 Closing(this, e);
974                         }
975                 }
976
977                 protected virtual void OnDeactivate(EventArgs e) {
978                         if (Deactivate != null) {
979                                 Deactivate(this, e);
980                         }
981                 }
982
983                 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
984                         if (InputLanguageChanged!=null) {
985                                 InputLanguageChanged(this, e);
986                         }
987                 }
988
989                 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
990                         if (InputLanguageChanging!=null) {
991                                 InputLanguageChanging(this, e);
992                         }
993                 }
994
995                 protected virtual void OnLoad(EventArgs e) {
996                         if (Load != null) {
997                                 Load(this, e);
998                         }
999                 }
1000
1001                 protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
1002                         if (MaximizedBoundsChanged != null) {
1003                                 MaximizedBoundsChanged(this, e);
1004                         }
1005                 }
1006
1007                 protected virtual void OnMaximumSizeChanged(EventArgs e) {
1008                         if (MaximumSizeChanged != null) {
1009                                 MaximumSizeChanged(this, e);
1010                         }
1011                 }
1012
1013                 protected virtual void OnMdiChildActivate(EventArgs e) {
1014                         if (MdiChildActivate != null) {
1015                                 MdiChildActivate(this, e);
1016                         }
1017                 }
1018
1019                 protected virtual void OnMenuComplete(EventArgs e) {
1020                         if (MenuComplete != null) {
1021                                 MenuComplete(this, e);
1022                         }
1023                 }
1024
1025                 protected virtual void OnMenuStart(EventArgs e) {
1026                         if (MenuStart != null) {
1027                                 MenuStart(this, e);
1028                         }
1029                 }
1030
1031                 protected virtual void OnMinimumSizeChanged(EventArgs e) {
1032                         if (MinimumSizeChanged != null) {
1033                                 MinimumSizeChanged(this, e);
1034                         }
1035                 }
1036
1037                 public event EventHandler Activated;
1038                 public event EventHandler Closed;
1039                 public event CancelEventHandler Closing;
1040                 public event EventHandler Deactivate;
1041                 public event InputLanguageChangedEventHandler InputLanguageChanged;
1042                 public event InputLanguageChangingEventHandler InputLanguageChanging;
1043                 public event EventHandler Load;
1044                 public event EventHandler MaximizedBoundsChanged;
1045                 public event EventHandler MaximumSizeChanged;
1046                 public event EventHandler MdiChildActivate;
1047                 public event EventHandler MenuComplete;
1048                 public event EventHandler MenuStart;
1049                 public event EventHandler MinimumSizeChanged;
1050                 #endregion      // Events
1051         }
1052 }