2006-12-04 Rolf Bjarne Kvinge <RKvinge@novell.com>
[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-2006 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.ComponentModel.Design;
32 using System.ComponentModel.Design.Serialization;
33 using System.Collections;
34 using System.Runtime.InteropServices;
35 using System.Threading;
36
37 namespace System.Windows.Forms {
38         [DesignerCategory("Form")]
39         [DesignTimeVisible(false)]
40         [Designer("System.Windows.Forms.Design.FormDocumentDesigner, " + Consts.AssemblySystem_Design, typeof(IRootDesigner))]
41         [DefaultEvent("Load")]
42         [ToolboxItem(false)]
43         public class Form : ContainerControl {
44                 #region Local Variables
45                 internal bool                   closing;
46                 FormBorderStyle                 form_border_style;
47                 private bool                    autoscale;
48                 private Size                    clientsize_set;
49                 private Size                    autoscale_base_size;
50                 private bool                    allow_transparency;
51                 private static Icon             default_icon;
52                 internal bool                   is_modal;
53                 internal FormWindowState        window_state;
54                 private bool                    control_box;
55                 private bool                    minimize_box;
56                 private bool                    maximize_box;
57                 private bool                    help_button;
58                 private bool                    show_in_taskbar;
59                 private bool                    topmost;
60                 private IButtonControl          accept_button;
61                 private IButtonControl          cancel_button;
62                 private DialogResult            dialog_result;
63                 private FormStartPosition       start_position;
64                 private Form                    owner;
65                 private Form.ControlCollection  owned_forms;
66                 private MdiClient               mdi_container;
67                 internal InternalWindowManager  window_manager;
68                 private Form                    mdi_parent;
69                 private bool                    key_preview;
70                 private MainMenu                menu;
71                 private Icon                    icon;
72                 private Size                    maximum_size;
73                 private Size                    minimum_size;
74                 private SizeGripStyle           size_grip_style;
75                 private Rectangle               maximized_bounds;
76                 private Rectangle               default_maximized_bounds;
77                 private double                  opacity;
78                 internal ApplicationContext     context;
79                 Color                           transparency_key;
80                 internal MenuTracker            active_tracker;
81                 private bool                    is_loaded;
82                 internal bool                   is_changing_visible_state;
83                 internal bool                   has_been_visible;
84
85                 #endregion      // Local Variables
86
87                 #region Private & Internal Methods
88                 static Form ()
89                 {
90                         default_icon = Locale.GetResource("mono.ico") as Icon;
91                 }
92
93                 // warning: this is only hooked up when an mdi container is created.
94                 private void ControlAddedHandler (object sender, ControlEventArgs e)
95                 {
96                         if (mdi_container != null) {
97                                 mdi_container.SendToBack ();
98                         }
99                 }
100
101                 private void SelectActiveControl ()
102                 {
103                         if (this.IsMdiContainer)
104                                 return;
105                                 
106                         if (this.ActiveControl == null) {
107                                 bool visible;
108
109                                 // This visible hack is to work around CanSelect always being false if one of the parents
110                                 // is not visible; and we by default create Form invisible...
111                                 visible = this.is_visible;
112                                 this.is_visible = true;
113
114                                 if (SelectNextControl (this, true, true, true, true) == false) {
115                                         Select (this);
116                                 }
117
118                                 this.is_visible = visible;
119                         } else {
120                                 Select (ActiveControl);
121                         }
122                 }
123                 #endregion      // Private & Internal Methods
124
125                 #region Public Classes
126                 public new class ControlCollection : Control.ControlCollection {
127                         Form    form_owner;
128
129                         public ControlCollection(Form owner) : base(owner) {
130                                 this.form_owner = owner;
131                         }
132
133                         public override void Add(Control value) {
134                                 if (Contains (value))
135                                         return;
136                                 AddToList (value);
137                                 ((Form)value).owner=form_owner;
138                         }
139
140                         public override void Remove(Control value) {
141                                 ((Form)value).owner = null;
142                                 base.Remove (value);
143                         }
144                 }
145                 #endregion      // Public Classes
146
147                 #region Public Constructor & Destructor
148                 public Form ()
149                 {
150                         SizeF current_scale = GetAutoScaleSize (DeviceContext, Font);
151
152                         autoscale = true;
153                         autoscale_base_size = new Size ((int)current_scale.Width, (int) current_scale.Height);
154                         allow_transparency = false;
155                         closing = false;
156                         is_modal = false;
157                         dialog_result = DialogResult.None;
158                         start_position = FormStartPosition.WindowsDefaultLocation;
159                         form_border_style = FormBorderStyle.Sizable;
160                         window_state = FormWindowState.Normal;
161                         key_preview = false;
162                         opacity = 1D;
163                         menu = null;
164                         icon = default_icon;
165                         minimum_size = Size.Empty;
166                         maximum_size = Size.Empty;
167                         clientsize_set = Size.Empty;
168                         control_box = true;
169                         minimize_box = true;
170                         maximize_box = true;
171                         help_button = false;
172                         show_in_taskbar = true;
173                         ime_mode = ImeMode.NoControl;
174                         is_visible = false;
175                         is_toplevel = true;
176                         size_grip_style = SizeGripStyle.Auto;
177                         maximized_bounds = Rectangle.Empty;
178                         default_maximized_bounds = Rectangle.Empty;
179                         owned_forms = new Form.ControlCollection(this);
180                         transparency_key = Color.Empty;
181
182                         // FIXME: this should disappear just as soon as the handle creation is done in the right place (here is too soon()
183                         UpdateBounds();
184
185                 }
186                 #endregion      // Public Constructor & Destructor
187
188                 #region Public Static Properties
189
190                 public static Form ActiveForm {
191                         get {
192                                 Control active;
193
194                                 active = FromHandle(XplatUI.GetActive());
195
196                                 if (active != null) {
197                                         if ( !(active is Form)) {
198                                                 Control parent;
199
200                                                 parent = active.Parent;
201                                                 while (parent != null) {
202                                                         if (parent is Form) {
203                                                                 return (Form)parent;
204                                                         }
205                                                         parent = parent.Parent;
206                                                 }
207                                         } else {
208                                                 return (Form)active;
209                                         }
210                                 }
211                                 return null;
212                         }
213                 }
214
215                 #endregion      // Public Static Properties
216
217                 #region Public Instance Properties
218                 [DefaultValue(null)]
219                 public IButtonControl AcceptButton {
220                         get {
221                                 return accept_button;
222                         }
223
224                         set {
225                                 accept_button = value;
226                                 CheckAcceptButton();
227                         }
228                 }
229
230                 [Browsable(false)]
231                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
232                 public bool AllowTransparency {
233                         get {
234                                 return allow_transparency;
235                         }
236
237                         set {
238                                 if (value == allow_transparency) {
239                                         return;
240                                 }
241
242                                 allow_transparency = value;
243
244                                 if (value) {
245                                         if (IsHandleCreated) {
246                                                 if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
247                                                         XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
248                                                 }
249                                         } else {
250                                                 UpdateStyles(); // Remove the WS_EX_LAYERED style
251                                         }
252                                 }
253                         }
254                 }
255
256                 [DefaultValue(true)]
257                 [MWFCategory("Layout")]
258                 public bool AutoScale {
259                         get {
260                                 return autoscale;
261                         }
262
263                         set {
264                                 autoscale = value;
265                         }
266                 }
267
268                 [Localizable(true)]
269                 [Browsable(false)]
270                 [EditorBrowsable(EditorBrowsableState.Advanced)]
271                 public virtual Size AutoScaleBaseSize {
272                         get {
273                                 return autoscale_base_size;
274                         }
275
276                         set {
277                                 autoscale_base_size = value;
278                         }
279                 }
280
281                 [Localizable(true)]
282                 public override bool AutoScroll {
283                         get {
284                                 return base.AutoScroll;
285                         }
286                         set {
287                                 base.AutoScroll = value;
288                         }
289                 }
290
291                 public override Color BackColor {
292                         get {
293                                 /* we don't let parents override our
294                                  default background color for forms.
295                                  this fixes the default color for mdi
296                                  children. */
297                                 if (background_color.IsEmpty)
298                                         return DefaultBackColor;
299                                 else
300                                         return background_color;
301                         }
302                         set {
303                                 base.BackColor = value;
304                         }
305                 }
306
307                 [DefaultValue(null)]
308                 public IButtonControl CancelButton {
309                         get {
310                                 return cancel_button;
311                         }
312
313                         set {
314                                 cancel_button = value;
315                         }
316                 }
317
318                 // new property so we can change the DesignerSerializationVisibility
319                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
320                 [Localizable(true)]
321                 public new Size ClientSize {
322                         get { return base.ClientSize; }
323                         set { base.ClientSize = value; }
324                 }
325
326                 [DefaultValue(true)]
327                 [MWFCategory("Window Style")]
328                 public bool ControlBox {
329                         get {
330                                 return control_box;
331                         }
332
333                         set {
334                                 if (control_box != value) {
335                                         control_box = value;
336                                         UpdateStyles();
337                                 }
338                         }
339                 }
340
341                 [Browsable(false)]
342                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
343                 public Rectangle DesktopBounds {
344                         get {
345                                 return new Rectangle(Location, Size);
346                         }
347
348                         set {
349                                 Bounds = value;
350                         }
351                 }
352
353                 [Browsable(false)]
354                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
355                 public Point DesktopLocation {
356                         get {
357                                 return Location;
358                         }
359
360                         set {
361                                 Location = value;
362                         }
363                 }
364
365                 [Browsable(false)]
366                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
367                 public DialogResult DialogResult {
368                         get {
369                                 return dialog_result;
370                         }
371
372                         set {
373                                 if (value < DialogResult.None || value > DialogResult.No)
374                                         throw new InvalidEnumArgumentException ("value", (int) value, 
375                                                         typeof (DialogResult));
376
377                                 dialog_result = value;
378                                 closing = (dialog_result != DialogResult.None && is_modal);
379                         }
380                 }
381
382                 [DefaultValue(FormBorderStyle.Sizable)]
383                 [DispId(-504)]
384                 [MWFCategory("Appearance")]
385                 public FormBorderStyle FormBorderStyle {
386                         get {
387                                 return form_border_style;
388                         }
389                         set {
390                                 form_border_style = value;
391
392                                 if (window_manager == null) {
393                                         if (IsHandleCreated) {
394                                                 XplatUI.SetBorderStyle(window.Handle, form_border_style);
395                                         }
396                                 } else {
397                                         window_manager.UpdateBorderStyle (value);
398                                 }
399
400                                 UpdateStyles();
401                         }
402                 }
403
404                 [DefaultValue(false)]
405                 [MWFCategory("Window Style")]
406                 public bool HelpButton {
407                         get {
408                                 return help_button;
409                         }
410
411                         set {
412                                 if (help_button != value) {
413                                         help_button = value;
414                                         UpdateStyles();
415                                 }
416                         }
417                 }
418
419                 [Localizable(true)]
420                 [AmbientValue(null)]
421                 [MWFCategory("Window Style")]
422                 public Icon Icon {
423                         get {
424                                 return icon;
425                         }
426
427                         set {
428                                 if (icon != value) {
429                                         icon = value;
430
431                                         if (IsHandleCreated) {
432                                                 XplatUI.SetIcon(Handle, icon == null ? default_icon : icon);
433                                         }
434                                 }
435                         }
436                 }
437
438                 [Browsable(false)]
439                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
440                 public bool IsMdiChild {
441                         get {
442                                 return mdi_parent != null;
443                         }
444                 }
445
446                 [DefaultValue(false)]
447                 [MWFCategory("Window Style")]
448                 public bool IsMdiContainer {
449                         get {
450                                 return mdi_container != null;
451                         }
452
453                         set {
454                                 if (value && mdi_container == null) {
455                                         mdi_container = new MdiClient ();
456                                         Controls.Add(mdi_container);
457                                         ControlAdded += new ControlEventHandler (ControlAddedHandler);
458                                         mdi_container.SendToBack ();
459                                         mdi_container.SetParentText (true);
460                                 } else if (!value && mdi_container != null) {
461                                         Controls.Remove(mdi_container);
462                                         mdi_container = null;
463                                 }
464                         }
465                 }
466
467                 [Browsable(false)]
468                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
469                 public Form ActiveMdiChild {
470                         get {
471                                 if (!IsMdiContainer)
472                                         return null;
473                                 return (Form) mdi_container.ActiveMdiChild;
474                         }
475                 }
476
477                 [Browsable(false)]
478                 [EditorBrowsable(EditorBrowsableState.Advanced)]
479                 public bool IsRestrictedWindow {
480                         get {
481                                 return false;
482                         }
483                 }
484
485                 [DefaultValue(false)]
486                 public bool KeyPreview {
487                         get {
488                                 return key_preview;
489                         }
490
491                         set {
492                                 key_preview = value;
493                         }
494                 }
495
496                 [DefaultValue(true)]
497                 [MWFCategory("Window Style")]
498                 public bool MaximizeBox {
499                         get {
500                                 return maximize_box;
501                         }
502                         set {
503                                 if (maximize_box != value) {
504                                         maximize_box = value;
505                                         if (IsHandleCreated) {
506                                                 RecreateHandle();
507                                         }
508                                         UpdateStyles();
509                                 }
510                         }
511                 }
512
513                 [DefaultValue("{Width=0, Height=0}")]
514                 [Localizable(true)]
515                 [RefreshProperties(RefreshProperties.Repaint)]
516                 [MWFCategory("Layout")]
517                 public Size MaximumSize {
518                         get {
519                                 return maximum_size;
520                         }
521
522                         set {
523                                 if (maximum_size != value) {
524                                         maximum_size = value;
525                                         OnMaximumSizeChanged(EventArgs.Empty);
526                                         if (IsHandleCreated) {
527                                                 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
528                                         }
529                                 }
530                         }
531                 }
532
533                 [Browsable(false)]
534                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
535                 public Form[] MdiChildren {
536                         get {
537                                 if (mdi_container != null)
538                                         return mdi_container.MdiChildren;
539                                 else
540                                         return new Form[0];
541                         }
542                 }
543
544                 [Browsable(false)]
545                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
546                 public Form MdiParent {
547                         get {
548                                 return mdi_parent;
549                         }
550
551                         set {
552                                 if (value != null && !value.IsMdiContainer)
553                                         throw new ArgumentException ();
554
555                                 if (mdi_parent != null) {
556                                         mdi_parent.MdiContainer.Controls.Remove (this);
557                                 }
558
559                                 if (value != null) {
560                                         mdi_parent = value;
561                                         window_manager = new MdiWindowManager (this,
562                                                         mdi_parent.MdiContainer);
563                                         mdi_parent.MdiContainer.Controls.Add (this);
564                                         mdi_parent.MdiContainer.Controls.SetChildIndex (this, 0);
565
566                                         RecreateHandle ();
567
568                                 } else if (mdi_parent != null) {
569                                         mdi_parent = null;
570
571                                         // Create a new window manager
572                                         window_manager = null;
573                                         FormBorderStyle = form_border_style;
574
575                                         RecreateHandle ();
576                                 }
577                         }
578                 }
579
580                 internal MenuTracker ActiveTracker {
581                         get { return active_tracker; }
582                         set {
583                                 if (value == active_tracker)
584                                         return;
585
586                                 Capture = value != null;
587                                 active_tracker = value;
588                         }
589                 }
590
591                 internal MdiClient MdiContainer {
592                         get { return mdi_container; }
593                 }
594
595                 internal InternalWindowManager WindowManager {
596                         get { return window_manager; }
597                 }
598
599                 [DefaultValue(null)]
600                 [MWFCategory("Window Style")]
601                 public MainMenu Menu {
602                         get {
603                                 return menu;
604                         }
605
606                         set {
607                                 if (menu != value) {
608                                         menu = value;
609
610                                         if (menu != null && !IsMdiChild) {
611                                                 menu.SetForm (this);
612
613                                                 if (IsHandleCreated) {
614                                                         XplatUI.SetMenu (window.Handle, menu);
615                                                 }
616
617                                                 if (clientsize_set != Size.Empty) {
618                                                         SetClientSizeCore(clientsize_set.Width, clientsize_set.Height);
619                                                 } else {
620                                                         UpdateBounds (bounds.X, bounds.Y, bounds.Width, bounds.Height, ClientSize.Width, ClientSize.Height - 
621                                                                 ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu, ClientSize.Width));
622                                                 }
623                                         } else
624                                                 UpdateBounds ();
625                                 }
626                         }
627                 }
628
629                 [Browsable(false)]
630                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
631                 [EditorBrowsable(EditorBrowsableState.Advanced)]
632                 public MainMenu MergedMenu {
633                         get {
634                                 if (!IsMdiChild || window_manager == null)
635                                         return null;
636                                 return ((MdiWindowManager) window_manager).MergedMenu;
637                         }
638                 }
639
640                 // This is the menu in display and being used because of merging this can
641                 // be different then the menu that is actually assosciated with the form
642                 internal MainMenu ActiveMenu {
643                         get {
644                                 if (IsMdiChild)
645                                         return null;
646
647                                 if (IsMdiContainer && mdi_container.Controls.Count > 0 &&
648                                                 ((Form) mdi_container.Controls [0]).WindowState == FormWindowState.Maximized) {
649                                         MdiWindowManager wm = (MdiWindowManager) ((Form) mdi_container.Controls [0]).WindowManager;
650                                         return wm.MaximizedMenu;
651                                 }
652
653                                 Form amc = ActiveMdiChild;
654                                 if (amc == null || amc.Menu == null)
655                                         return menu;
656                                 return amc.MergedMenu;
657                         }
658                 }
659
660                 internal MdiWindowManager ActiveMaximizedMdiChild {
661                         get {
662                                 Form child = ActiveMdiChild;
663                                 if (child == null)
664                                         return null;
665                                 if (child.WindowManager == null || child.window_state != FormWindowState.Maximized)
666                                         return null;
667                                 return (MdiWindowManager) child.WindowManager;
668                         }
669                 }
670
671                 [DefaultValue(true)]
672                 [MWFCategory("Window Style")]
673                 public bool MinimizeBox {
674                         get {
675                                 return minimize_box;
676                         }
677                         set {
678                                 if (minimize_box != value) {
679                                         minimize_box = value;
680                                         if (IsHandleCreated) {
681                                                 RecreateHandle();
682                                         }
683                                         UpdateStyles();
684                                 }
685                         }
686                 }
687
688                 [DefaultValue("{Width=0, Height=0}")]
689                 [Localizable(true)]
690                 [RefreshProperties(RefreshProperties.Repaint)]
691                 [MWFCategory("Layout")]
692                 public Size MinimumSize {
693                         get {
694                                 return minimum_size;
695                         }
696
697                         set {
698                                 if (minimum_size != value) {
699                                         minimum_size = value;
700
701                                         if ((Size.Width < value.Width) || (Size.Height < value.Height)) {
702                                                 Size = new Size(Math.Max(Size.Width, value.Width), Math.Max(Size.Height, value.Height));
703                                         }
704   
705
706                                         OnMinimumSizeChanged(EventArgs.Empty);
707                                         if (IsHandleCreated) {
708                                                 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
709                                         }
710                                 }
711                         }
712                 }
713
714                 [Browsable(false)]
715                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
716                 public bool Modal  {
717                         get {
718                                 return is_modal;
719                         }
720                 }
721
722                 [DefaultValue(1D)]
723                 [TypeConverter(typeof(OpacityConverter))]
724                 [MWFCategory("Window Style")]
725                 public double Opacity {
726                         get {
727                                 if (IsHandleCreated) {
728                                         if ((XplatUI.SupportsTransparency () & TransparencySupport.Get) != 0)
729                                                 return XplatUI.GetWindowTransparency (Handle);
730                                 }
731
732                                 return opacity;
733                         }
734
735                         set {
736                                 opacity = value;
737
738                                 AllowTransparency = true;
739
740                                 if (IsHandleCreated) {
741                                         UpdateStyles();
742                                         if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
743                                                 XplatUI.SetWindowTransparency(Handle, opacity, TransparencyKey);
744                                 }
745                         }
746                 }
747                         
748
749                 [Browsable(false)]
750                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
751                 public Form[] OwnedForms {
752                         get {
753                                 Form[] form_list;
754
755                                 form_list = new Form[owned_forms.Count];
756
757                                 for (int i=0; i<owned_forms.Count; i++) {
758                                         form_list[i] = (Form)owned_forms[i];
759                                 }
760
761                                 return form_list;
762                         }
763                 }
764
765                 [Browsable(false)]
766                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
767                 public Form Owner {
768                         get {
769                                 return owner;
770                         }
771
772                         set {
773                                 if (owner != value) {
774                                         if (owner != null) {
775                                                 owner.RemoveOwnedForm(this);
776                                         }
777                                         owner = value;
778                                         if (owner != null)
779                                                 owner.AddOwnedForm(this);
780                                         if (IsHandleCreated) {
781                                                 if (owner != null && owner.IsHandleCreated) {
782                                                         XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
783                                                 } else {
784                                                         XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
785                                                 }
786                                         }
787                                 }
788                         }
789                 }
790
791                 [DefaultValue(true)]
792                 [MWFCategory("Window Style")]
793                 public bool ShowInTaskbar {
794                         get {
795                                 return show_in_taskbar;
796                         }
797                         set {
798                                 if (show_in_taskbar != value) {
799                                         show_in_taskbar = value;
800                                         if (IsHandleCreated) {
801                                                 RecreateHandle();
802                                         }
803                                         UpdateStyles();
804                                 }
805                         }
806                 }
807
808                 // new property so we can set the DesignerSerializationVisibility
809                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
810                 [Localizable(false)]
811                 public new Size Size {
812                         get { return base.Size; }
813                         set { base.Size = value; }
814                 }
815
816                 [MonoTODO("Trigger something when GripStyle is set")]
817                 [DefaultValue(SizeGripStyle.Auto)]
818                 [MWFCategory("Window Style")]
819                 public SizeGripStyle SizeGripStyle {
820                         get {
821                                 return size_grip_style;
822                         }
823
824                         set {
825                                 size_grip_style = value;
826                         }
827                 }
828
829                 [DefaultValue(FormStartPosition.WindowsDefaultLocation)]
830                 [Localizable(true)]
831                 [MWFCategory("Layout")]
832                 public FormStartPosition StartPosition {
833                         get {
834                                 return start_position;
835                         }
836
837                         set {
838                                 if (start_position == FormStartPosition.WindowsDefaultLocation) {               // Only do this if it's not set yet
839                                         start_position = value;
840                                         if (IsHandleCreated) {
841                                                 switch(start_position) {
842                                                         case FormStartPosition.CenterParent: {
843                                                                 CenterToParent();
844                                                                 break;
845                                                         }
846
847                                                         case FormStartPosition.CenterScreen: {
848                                                                 CenterToScreen();
849                                                                 break;
850                                                         }
851
852                                                         case FormStartPosition.Manual: {
853                                                                 Left = CreateParams.X;
854                                                                 Top = CreateParams.Y;
855                                                                 break;
856                                                         }
857
858                                                         default: {
859                                                                 break;
860                                                         }
861                                                 }
862                                         }
863                                 }
864                         }
865                 }
866
867                 // new property so we can set EditorBrowsable to never
868                 [Browsable(false)]
869                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
870                 [EditorBrowsable(EditorBrowsableState.Never)]
871                 public new int TabIndex {
872                         get { return base.TabIndex; }
873                         set { base.TabIndex = value; }
874                 }
875
876                 [Browsable(false)]
877                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
878                 [EditorBrowsable(EditorBrowsableState.Advanced)]
879                 public bool TopLevel {
880                         get {
881                                 return GetTopLevel();
882                         }
883
884                         set {
885                                 if (!value && IsMdiContainer)
886                                         throw new ArgumentException ("MDI Container forms must be top level.");
887                                 SetTopLevel(value);
888                         }
889                 }
890
891                 [DefaultValue(false)]
892                 [MWFCategory("Window Style")]
893                 public bool TopMost {
894                         get {
895                                 return topmost;
896                         }
897
898                         set {
899                                 if (topmost != value) {
900                                         topmost = value;
901                                         if (IsHandleCreated)
902                                                 XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, value);
903                                 }
904                         }
905                 }
906
907                 [MWFCategory("Window Style")]
908                 public Color TransparencyKey {
909                         get {
910                                 return transparency_key;
911                         }
912
913                         set {
914                                 transparency_key = value;
915
916                                 AllowTransparency = true;
917                                 UpdateStyles();
918                                 if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
919                                         XplatUI.SetWindowTransparency(Handle, Opacity, transparency_key);
920                         }
921                 }
922
923                 [DefaultValue(FormWindowState.Normal)]
924                 [MWFCategory("Layout")]
925                 public FormWindowState WindowState {
926                         get {
927                                 if (IsHandleCreated) {
928
929                                         if (window_manager != null)
930                                                 return window_manager.GetWindowState ();
931
932                                         FormWindowState new_state = XplatUI.GetWindowState(Handle);
933                                         if (new_state != (FormWindowState)(-1))
934                                                 window_state = new_state;
935                                 }
936
937                                 return window_state;
938                         }
939
940                         set {
941                                 FormWindowState old_state = window_state;
942                                 window_state = value;
943                                 if (IsHandleCreated) {
944
945                                         if (window_manager != null) {
946                                                 window_manager.SetWindowState (old_state, value);
947                                                 return;
948                                         }
949
950                                         XplatUI.SetWindowState(Handle, value);
951                                 }
952                         }
953                 }
954
955                 #endregion      // Public Instance Properties
956
957                 #region Protected Instance Properties
958                 protected override CreateParams CreateParams {
959                         get {
960                                 CreateParams cp = new CreateParams ();
961
962                                 cp.Caption = Text;
963                                 cp.ClassName = XplatUI.DefaultClassName;
964                                 cp.ClassStyle = 0;
965                                 cp.Style = 0;
966                                 cp.ExStyle = 0;
967                                 cp.Param = 0;
968                                 cp.Parent = IntPtr.Zero;
969                                 cp.menu = ActiveMenu;
970
971                                 if (start_position == FormStartPosition.WindowsDefaultLocation && !IsMdiChild) {
972                                         cp.X = unchecked((int)0x80000000);
973                                         cp.Y = unchecked((int)0x80000000);
974                                 } else {
975                                         cp.X = Left;
976                                         cp.Y = Top;
977                                 }
978                                 cp.Width = Width;
979                                 cp.Height = Height;
980
981                                 cp.Style = (int)(WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS);
982
983                                 if (IsMdiChild) {
984                                         cp.Style |= (int)(WindowStyles.WS_CHILD | WindowStyles.WS_CAPTION);
985                                         if (Parent != null) {
986                                                 cp.Parent = Parent.Handle;
987                                         }
988
989                                         cp.ExStyle |= (int) (WindowExStyles.WS_EX_WINDOWEDGE | WindowExStyles.WS_EX_MDICHILD);
990
991                                         switch (FormBorderStyle) {
992                                         case FormBorderStyle.None:
993                                                 break;
994                                         case FormBorderStyle.FixedToolWindow:
995                                         case FormBorderStyle.SizableToolWindow:
996                                                 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW;
997                                                 goto default;
998                                         default:
999                                                 cp.Style |= (int) WindowStyles.WS_OVERLAPPEDWINDOW;
1000                                                 break;
1001                                         }
1002                                         
1003                                 } else {
1004                                         switch (FormBorderStyle) {
1005                                                 case FormBorderStyle.Fixed3D: {
1006                                                         cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1007                                                         cp.ExStyle |= (int)WindowExStyles.WS_EX_CLIENTEDGE; 
1008                                                         break;
1009                                                 }
1010
1011                                                 case FormBorderStyle.FixedDialog: {
1012                                                         cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1013                                                         cp.ExStyle |= (int)(WindowExStyles.WS_EX_DLGMODALFRAME | WindowExStyles.WS_EX_CONTROLPARENT);
1014                                                         break;
1015                                                 }
1016
1017                                                 case FormBorderStyle.FixedSingle: {
1018                                                         cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1019                                                         break;
1020                                                 }
1021
1022                                                 case FormBorderStyle.FixedToolWindow: { 
1023                                                         cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1024                                                         cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1025                                                         break;
1026                                                 }
1027
1028                                                 case FormBorderStyle.Sizable: {
1029                                                         cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION); 
1030                                                         break;
1031                                                 }
1032
1033                                                 case FormBorderStyle.SizableToolWindow: {
1034                                                         cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
1035                                                         cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1036                                                         break;
1037                                                 }
1038
1039                                                 case FormBorderStyle.None: {
1040                                                         break;
1041                                                 }
1042                                         }
1043                                 }
1044
1045                                 switch(window_state) {
1046                                         case FormWindowState.Maximized: {
1047                                                 cp.Style |= (int)WindowStyles.WS_MAXIMIZE;
1048                                                 break;
1049                                         }
1050
1051                                         case FormWindowState.Minimized: {
1052                                                 cp.Style |= (int)WindowStyles.WS_MINIMIZE;
1053                                                 break;
1054                                         }
1055                                 }
1056
1057                                 if (TopMost) {
1058                                         cp.ExStyle |= (int) WindowExStyles.WS_EX_TOPMOST;
1059                                 }
1060
1061                                 if (ShowInTaskbar) {
1062                                         cp.ExStyle |= (int)WindowExStyles.WS_EX_APPWINDOW;
1063                                 }
1064
1065                                 if (MaximizeBox) {
1066                                         cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
1067                                 }
1068
1069                                 if (MinimizeBox) {
1070                                         cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
1071                                 }
1072
1073                                 if (ControlBox) {
1074                                         cp.Style |= (int)WindowStyles.WS_SYSMENU;
1075                                 }
1076
1077                                 if (HelpButton && !MaximizeBox && !MinimizeBox) {
1078                                         cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTEXTHELP;
1079                                 }
1080                                 
1081                                 if (Visible)
1082                                         cp.Style |= (int)WindowStyles.WS_VISIBLE;
1083
1084                                 if (Opacity < 1.0 || TransparencyKey != Color.Empty) {
1085                                         cp.ExStyle |= (int)WindowExStyles.WS_EX_LAYERED;
1086                                 }
1087
1088                                 if (!is_enabled && context == null) {
1089                                         cp.Style |= (int)(WindowStyles.WS_DISABLED);
1090                                 }
1091
1092                                 return cp;
1093                         }
1094                 }
1095
1096                 protected override ImeMode DefaultImeMode {
1097                         get {
1098                                 return ImeMode.NoControl;
1099                         }
1100                 }
1101
1102                 protected override Size DefaultSize {
1103                         get {
1104                                 return new Size (300, 300);
1105                         }
1106                 }
1107
1108                 protected Rectangle MaximizedBounds {
1109                         get {
1110                                 if (maximized_bounds != Rectangle.Empty) {
1111                                         return maximized_bounds;
1112                                 }
1113                                 return default_maximized_bounds;
1114                         }
1115
1116                         set {
1117                                 maximized_bounds = value;
1118                                 OnMaximizedBoundsChanged(EventArgs.Empty);
1119                                 if (IsHandleCreated) {
1120                                         XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
1121                                 }
1122                         }
1123                 }
1124                 #endregion      // Protected Instance Properties
1125
1126                 #region Public Static Methods
1127                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1128                 public static SizeF GetAutoScaleSize (Font font)
1129                 {
1130                         return XplatUI.GetAutoScaleSize(font);
1131                 }
1132
1133                 #endregion      // Public Static Methods
1134
1135                 #region Public Instance Methods
1136                 internal SizeF GetAutoScaleSize (Graphics g, Font font)
1137                 {
1138                         //
1139                         // The following constants come from the dotnet mailing list
1140                         // discussion: http://discuss.develop.com/archives/wa.exe?A2=ind0203A&L=DOTNET&P=R3655
1141                         //
1142                         // The magic number is "Its almost the length
1143                         // of the string with a smattering added in
1144                         // for compat with earlier code".
1145                         //
1146         
1147                         string magic_string = "The quick brown fox jumped over the lazy dog.";
1148                         double magic_number = 44.549996948242189;
1149                         float width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
1150                         
1151                         return new SizeF (width, font.Height);
1152                 }
1153                                                  
1154                 public void Activate() {
1155                         Form    active;
1156
1157                         // The docs say activate only activates if our app is already active
1158                         if (IsHandleCreated) {
1159                                 if (IsMdiChild) {
1160                                         MdiParent.ActivateMdiChild (this);
1161                                 } else {
1162                                         active = ActiveForm;
1163                                         if ((active != null) && (this != active)) {
1164                                                 XplatUI.Activate(window.Handle);
1165                                         }
1166                                 }
1167                         }
1168                 }
1169
1170                 public void AddOwnedForm(Form ownedForm) {
1171                         if (!owned_forms.Contains(ownedForm)) {
1172                                 owned_forms.Add(ownedForm);
1173                         }
1174                         ownedForm.Owner = this;
1175                 }
1176
1177                 public void Close () {
1178                         if (IsDisposed)
1179                                 return;
1180
1181                         if (!is_visible)
1182                                 return;
1183
1184 #if NET_2_0
1185                         FormClosingEventArgs ce = new FormClosingEventArgs (CloseReason.FormOwnerClosing, false);
1186                         OnFormClosing (ce);
1187                         if (ce.Cancel)
1188                                 return;
1189 #endif
1190                         XplatUI.SendMessage(this.Handle, Msg.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
1191                 }
1192
1193                 public void LayoutMdi(MdiLayout value) {
1194                         if (mdi_container != null) {
1195                                 mdi_container.LayoutMdi(value);
1196                         }
1197                 }
1198
1199                 public void RemoveOwnedForm(Form ownedForm) {
1200                         owned_forms.Remove(ownedForm);
1201                 }
1202
1203                 public void SetDesktopBounds(int x, int y, int width, int height) {
1204                         DesktopBounds = new Rectangle(x, y, width, height);
1205                 }
1206
1207                 public void SetDesktopLocation(int x, int y) {
1208                         DesktopLocation = new Point(x, y);
1209                 }
1210
1211                 public DialogResult ShowDialog() {
1212                         return ShowDialog(this.owner);
1213                 }
1214
1215                 public DialogResult ShowDialog(IWin32Window ownerWin32) {
1216                         Rectangle       area;
1217                         bool            confined;
1218                         IntPtr          capture_window;
1219
1220                         owner = null;
1221
1222                         if (ownerWin32 != null) {
1223                                 Control c = Control.FromHandle (ownerWin32.Handle);
1224                                 if (c != null)
1225                                         owner = c.TopLevelControl as Form;
1226                         }
1227
1228                         if (owner == this) {
1229                                 throw new InvalidOperationException("The 'ownerWin32' cannot be the form being shown.");
1230                         }
1231
1232                         if (is_modal) {
1233                                 throw new InvalidOperationException("The form is already displayed as a modal dialog.");
1234                         }
1235
1236                         if (Visible) {
1237                                 throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
1238                         }
1239
1240                         if (!Enabled) {
1241                                 throw new InvalidOperationException("Cannot display a disabled form as modal dialog.");
1242                         }
1243
1244                         if (TopLevelControl != this) {
1245                                 throw new InvalidOperationException("Can only display TopLevel forms as modal dialog.");
1246                         }
1247
1248                         #if broken
1249                         // Can't do this, will screw us in the modal loop
1250                         form_parent_window.Parent = this.owner;
1251                         #endif
1252
1253                         // Release any captures
1254                         XplatUI.GrabInfo(out capture_window, out confined, out area);
1255                         if (capture_window != IntPtr.Zero) {
1256                                 XplatUI.UngrabWindow(capture_window);
1257                         }
1258
1259 #if not
1260                         // Commented out; we instead let the Visible=true inside the runloop create the control
1261                         // otherwise setting DialogResult inside any of the events that are triggered by the
1262                         // create will not actually cause the form to not be displayed.
1263                         // Leaving this comment here in case there was an actual purpose to creating the control
1264                         // in here.
1265                         if (!IsHandleCreated) {
1266                                 CreateControl();
1267                         }
1268 #endif
1269
1270                         Application.RunLoop(true, new ApplicationContext(this));
1271
1272                         if (owner != null) {
1273                                 // Cannot use Activate(), it has a check for the current active window...
1274                                 XplatUI.Activate(owner.window.Handle);
1275                         }
1276
1277                         if (DialogResult != DialogResult.None) {
1278                                 return DialogResult;
1279                         }
1280                         DialogResult = DialogResult.Cancel;
1281                         return DialogResult.Cancel;
1282                 }
1283
1284                 public override string ToString() {
1285                         return GetType().FullName.ToString() + ", Text: " + Text;
1286                 }
1287                 #endregion      // Public Instance Methods
1288
1289                 #region Protected Instance Methods
1290                 protected void ActivateMdiChild(Form form) {
1291                         if (!IsMdiContainer)
1292                                 return;
1293                         mdi_container.ActivateChild (form);
1294                         OnMdiChildActivate(EventArgs.Empty);
1295                 }
1296
1297                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1298                 protected override void AdjustFormScrollbars(bool displayScrollbars) {
1299                         base.AdjustFormScrollbars (displayScrollbars);
1300                 }
1301
1302                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1303                 protected void ApplyAutoScaling()
1304                 {
1305                         SizeF current_size_f = GetAutoScaleSize (DeviceContext, Font);
1306                         Size current_size = new Size ((int) current_size_f.Width, (int) current_size_f.Height);
1307                         float   dx;
1308                         float   dy;
1309
1310                         if (current_size == autoscale_base_size)
1311                                 return;
1312
1313                         if (Environment.GetEnvironmentVariable ("MONO_MWF_SCALING") == "disable"){
1314                                 return;
1315                         }
1316                         
1317                         //
1318                         // I tried applying the Fudge height factor from:
1319                         // http://blogs.msdn.com/mharsh/archive/2004/01/25/62621.aspx
1320                         // but it makes things larger without looking better.
1321                         //
1322                         if (current_size_f.Width != AutoScaleBaseSize.Width) {
1323                                 dx = current_size_f.Width / AutoScaleBaseSize.Width + 0.08f;
1324                         } else {
1325                                 dx = 1;
1326                         }
1327
1328                         if (current_size_f.Height != AutoScaleBaseSize.Height) {
1329                                 dy = current_size_f.Height / AutoScaleBaseSize.Height + 0.08f;
1330                         } else {
1331                                 dy = 1;
1332                         }
1333
1334                         Scale (dx, dy);
1335                         
1336                         AutoScaleBaseSize = current_size;
1337                 }
1338
1339                 protected void CenterToParent() {
1340                         Control ctl;
1341                         int     w;
1342                         int     h;
1343
1344                         if (Width > 0) {
1345                                 w = Width;
1346                         } else {
1347                                 w = DefaultSize.Width;
1348                         }
1349
1350                         if (Height > 0) {
1351                                 h = Height;
1352                         } else {
1353                                 h = DefaultSize.Height;
1354                         }
1355
1356                         ctl = null;
1357                         if (parent != null) {
1358                                 ctl = parent;
1359                         } else if (owner != null) {
1360                                 ctl = owner;
1361                         }
1362
1363                         if (owner != null) {
1364                                 this.Location = new Point(ctl.Left + ctl.Width / 2 - w /2, ctl.Top + ctl.Height / 2 - h / 2);
1365                         }
1366                 }
1367
1368                 protected void CenterToScreen() {
1369                         Size    DisplaySize;
1370                         int     w;
1371                         int     h;
1372
1373                         if (Width > 0) {
1374                                 w = Width;
1375                         } else {
1376                                 w = DefaultSize.Width;
1377                         }
1378
1379                         if (Height > 0) {
1380                                 h = Height;
1381                         } else {
1382                                 h = DefaultSize.Height;
1383                         }
1384
1385                         XplatUI.GetDisplaySize(out DisplaySize);
1386                         this.Location = new Point(DisplaySize.Width / 2 - w / 2, DisplaySize.Height / 2 - h / 2);
1387                 }
1388
1389                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1390                 protected override Control.ControlCollection CreateControlsInstance() {
1391                         return base.CreateControlsInstance ();
1392                 }
1393
1394                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1395                 protected override void CreateHandle() {
1396                         base.CreateHandle ();
1397
1398                         UpdateBounds();
1399
1400                         if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
1401                                 if (allow_transparency) {
1402                                         XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
1403                                 }
1404                         }
1405
1406                         XplatUI.SetWindowMinMax(window.Handle, maximized_bounds, minimum_size, maximum_size);
1407                         if ((FormBorderStyle != FormBorderStyle.FixedDialog) && (icon != null)) {
1408                                 XplatUI.SetIcon(window.Handle, icon);
1409                         }
1410
1411                         if ((owner != null) && (owner.IsHandleCreated)) {
1412                                 XplatUI.SetTopmost(window.Handle, owner.window.Handle, true);
1413                         }
1414
1415                         for (int i = 0; i < owned_forms.Count; i++) {
1416                                 if (owned_forms[i].IsHandleCreated)
1417                                         XplatUI.SetTopmost(owned_forms[i].window.Handle, window.Handle, true);
1418                         }
1419                         
1420                         if (window_manager != null && window_state != FormWindowState.Normal) {
1421                                 window_manager.SetWindowState (FormWindowState.Normal, window_state);
1422                         }
1423
1424                 }
1425
1426                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1427                 protected override void DefWndProc(ref Message m) {
1428                         base.DefWndProc (ref m);
1429                 }
1430
1431                 protected override void Dispose(bool disposing)
1432                 {
1433                         for (int i = 0; i < owned_forms.Count; i++)
1434                                 ((Form)owned_forms[i]).Owner = null;
1435
1436                         owned_forms.Clear ();
1437                         
1438                         base.Dispose (disposing);
1439                 }
1440
1441                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1442                 protected virtual void OnActivated(EventArgs e)
1443                 {
1444                         if (is_loaded)
1445                                 SelectActiveControl ();
1446
1447                         EventHandler eh = (EventHandler)(Events [ActivatedEvent]);
1448                         if (eh != null)
1449                                 eh (this, e);
1450                 }
1451
1452                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1453                 protected virtual void OnClosed(EventArgs e) {
1454                         EventHandler eh = (EventHandler)(Events [ClosedEvent]);
1455                         if (eh != null)
1456                                 eh (this, e);
1457                 }
1458
1459                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1460                 protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
1461                         CancelEventHandler eh = (CancelEventHandler)(Events [ClosingEvent]);
1462                         if (eh != null)
1463                                 eh (this, e);
1464                 }
1465
1466                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1467                 protected override void OnCreateControl() {
1468                         base.OnCreateControl ();
1469
1470                         if (menu != null) {
1471                                 XplatUI.SetMenu(window.Handle, menu);
1472                         }
1473
1474                         OnLoad(EventArgs.Empty);
1475                         
1476                         SelectActiveControl ();
1477
1478                         // Send initial location
1479                         OnLocationChanged(EventArgs.Empty);
1480
1481                         if (IsMdiContainer) {
1482                                 mdi_container.LayoutMdi (MdiLayout.Cascade);
1483                         }
1484                 }
1485
1486                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1487                 protected virtual void OnDeactivate(EventArgs e) {
1488                         EventHandler eh = (EventHandler)(Events [DeactivateEvent]);
1489                         if (eh != null)
1490                                 eh (this, e);
1491                 }
1492
1493                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1494                 protected override void OnFontChanged(EventArgs e) {
1495                         base.OnFontChanged (e);
1496                 }
1497
1498                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1499                 protected override void OnHandleCreated(EventArgs e) {
1500                         XplatUI.SetBorderStyle(window.Handle, form_border_style);
1501                         base.OnHandleCreated (e);
1502                 }
1503
1504                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1505                 protected override void OnHandleDestroyed(EventArgs e) {
1506                         base.OnHandleDestroyed (e);
1507                 }
1508
1509                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1510                 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
1511                         InputLanguageChangedEventHandler eh = (InputLanguageChangedEventHandler)(Events [InputLanguageChangedEvent]);
1512                         if (eh != null)
1513                                 eh (this, e);
1514                 }
1515
1516                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1517                 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
1518                         InputLanguageChangingEventHandler eh = (InputLanguageChangingEventHandler)(Events [InputLanguageChangingEvent]);
1519                         if (eh != null)
1520                                 eh (this, e);
1521                 }
1522
1523                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1524                 protected virtual void OnLoad(EventArgs e) {
1525                         if (AutoScale){
1526                                 ApplyAutoScaling ();
1527                                 AutoScale = false;
1528                         }
1529
1530                         EventHandler eh = (EventHandler)(Events [LoadEvent]);
1531                         if (eh != null)
1532                                 eh (this, e);
1533
1534                         if (!IsMdiChild) {
1535                                 switch (StartPosition) {
1536                                         case FormStartPosition.CenterScreen:
1537                                                 this.CenterToScreen();
1538                                                 break;
1539                                         case FormStartPosition.CenterParent:
1540                                                 this.CenterToParent ();
1541                                                 break;
1542                                         case FormStartPosition.Manual: 
1543                                                 Left = CreateParams.X;
1544                                                 Top = CreateParams.Y;
1545                                                 break;
1546                                 }
1547                         }
1548                         is_loaded = true;
1549                 }
1550
1551                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1552                 protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
1553                         EventHandler eh = (EventHandler)(Events [MaximizedBoundsChangedEvent]);
1554                         if (eh != null)
1555                                 eh (this, e);
1556                 }
1557
1558                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1559                 protected virtual void OnMaximumSizeChanged(EventArgs e) {
1560                         EventHandler eh = (EventHandler)(Events [MaximumSizeChangedEvent]);
1561                         if (eh != null)
1562                                 eh (this, e);
1563                 }
1564
1565                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1566                 protected virtual void OnMdiChildActivate(EventArgs e) {
1567                         EventHandler eh = (EventHandler)(Events [MdiChildActivateEvent]);
1568                         if (eh != null)
1569                                 eh (this, e);
1570                 }
1571
1572                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1573                 protected virtual void OnMenuComplete(EventArgs e) {
1574                         EventHandler eh = (EventHandler)(Events [MenuCompleteEvent]);
1575                         if (eh != null)
1576                                 eh (this, e);
1577                 }
1578
1579                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1580                 protected virtual void OnMenuStart(EventArgs e) {
1581                         EventHandler eh = (EventHandler)(Events [MenuStartEvent]);
1582                         if (eh != null)
1583                                 eh (this, e);
1584                 }
1585
1586                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1587                 protected virtual void OnMinimumSizeChanged(EventArgs e) {
1588                         EventHandler eh = (EventHandler)(Events [MinimumSizeChangedEvent]);
1589                         if (eh != null)
1590                                 eh (this, e);
1591                 }
1592
1593                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1594                 protected override void OnPaint (PaintEventArgs pevent) {
1595                         base.OnPaint (pevent);
1596                 }
1597
1598                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1599                 protected override void OnResize(EventArgs e) {
1600                         base.OnResize(e);
1601
1602                         if (this.IsMdiChild && ParentForm != null) {
1603                                 ParentForm.PerformLayout();
1604                                 ParentForm.Size = ParentForm.Size;
1605                         }
1606                 }
1607
1608                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1609                 protected override void OnStyleChanged(EventArgs e) {
1610                         base.OnStyleChanged (e);
1611                 }
1612
1613                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1614                 protected override void OnTextChanged(EventArgs e) {
1615                         base.OnTextChanged (e);
1616
1617                         if (mdi_container != null)
1618                                 mdi_container.SetParentText(true);
1619                 }
1620
1621                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1622                 protected override void OnVisibleChanged(EventArgs e) {
1623                         base.OnVisibleChanged (e);
1624                         
1625                         if (Visible) {
1626                                 if (window_manager != null)
1627                                         window_manager.SetWindowState (WindowState, WindowState);
1628                         }
1629                 }
1630
1631                 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
1632                         if (base.ProcessCmdKey (ref msg, keyData)) {
1633                                 return true;
1634                         }
1635
1636                         // Give our menu a shot
1637                         if (ActiveMenu != null) {
1638                                 return ActiveMenu.ProcessCmdKey(ref msg, keyData);
1639                         }
1640
1641                         return false;
1642                 }
1643
1644                 // LAMESPEC - Not documented that Form overrides ProcessDialogChar; class-status showed
1645                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1646                 protected override bool ProcessDialogChar(char charCode) {
1647                         return base.ProcessDialogChar (charCode);
1648                 }
1649
1650                 protected override bool ProcessDialogKey(Keys keyData) {
1651                         if ((keyData & Keys.Modifiers) == 0) {
1652                                 if (keyData == Keys.Enter) {
1653                                         IntPtr window = XplatUI.GetFocus ();
1654                                         Control c = Control.FromHandle (window);
1655                                         if (c is Button && c.FindForm () == this) {
1656                                                 ((Button)c).PerformClick ();
1657                                                 return true;
1658                                         }
1659                                         else if (accept_button != null) {
1660                                                 accept_button.PerformClick();
1661                                                 return true;
1662                                         }
1663                                 } else if (keyData == Keys.Escape && cancel_button != null) {
1664                                         cancel_button.PerformClick();
1665                                         return true;
1666                                 }
1667                         }
1668                         return base.ProcessDialogKey(keyData);
1669                 }
1670
1671                 protected override bool ProcessKeyPreview(ref Message msg) {
1672                         if (key_preview) {
1673                                 if (ProcessKeyEventArgs(ref msg)) {
1674                                         return true;
1675                                 }
1676                         }
1677                         return base.ProcessKeyPreview (ref msg);
1678                 }
1679
1680                 protected override bool ProcessTabKey(bool forward) {
1681                         return SelectNextControl(ActiveControl, forward, true, true, true);
1682                 }
1683
1684                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1685                 protected override void ScaleCore(float dx, float dy) {
1686                         try {
1687                                 SuspendLayout();
1688
1689                                 // We can't scale max or min windows
1690                                 if (WindowState == FormWindowState.Normal) {
1691                                         // We cannot call base since base also adjusts X/Y, but
1692                                         // a form is toplevel and doesn't move
1693                                         Size    size;
1694
1695                                         size = ClientSize;
1696                                         if (!GetStyle(ControlStyles.FixedWidth)) {
1697                                                 size.Width = (int)(size.Width * dx);
1698                                         }
1699
1700                                         if (!GetStyle(ControlStyles.FixedHeight)) {
1701                                                 size.Height = (int)(size.Height * dy);
1702                                         }
1703
1704                                         ClientSize = size;
1705                                 }
1706
1707                                 /* Now scale our children */
1708                                 Control [] controls = child_controls.GetAllControls ();
1709                                 for (int i=0; i < controls.Length; i++) {
1710                                         controls[i].Scale(dx, dy);
1711                                 }
1712                         }
1713
1714                         finally {
1715                                 ResumeLayout();
1716                         }
1717                 }
1718
1719                 protected override void Select(bool directed, bool forward) {
1720                         Form    parent;
1721
1722                         if (directed) {
1723                                 base.SelectNextControl(null, forward, true, true, true);
1724                         }
1725
1726                         parent = this.ParentForm;
1727                         if (parent != null) {
1728                                 parent.ActiveControl = this;
1729                         }
1730
1731                         Activate();
1732                 }
1733
1734                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1735                 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
1736                         base.SetBoundsCore (x, y, width, height, specified);
1737                 }
1738
1739                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1740                 protected override void SetClientSizeCore(int x, int y) {
1741                         if ((minimum_size.Width != 0) && (x < minimum_size.Width)) {
1742                                 x = minimum_size.Width;
1743                         } else if ((maximum_size.Width != 0) && (x > maximum_size.Width)) {
1744                                 x = maximum_size.Width;
1745                         }
1746
1747                         if ((minimum_size.Height != 0) && (y < minimum_size.Height)) {
1748                                 y = minimum_size.Height;
1749                         } else if ((maximum_size.Height != 0) && (y > maximum_size.Height)) {
1750                                 y = maximum_size.Height;
1751                         }
1752
1753                         Rectangle ClientRect = new Rectangle(0, 0, x, y);
1754                         Rectangle WindowRect;
1755                         CreateParams cp = this.CreateParams;
1756
1757                         clientsize_set = new Size(x, y);
1758
1759                         if (XplatUI.CalculateWindowRect(ref ClientRect, cp.Style, cp.ExStyle, cp.menu, out WindowRect)) {
1760                                 SetBounds(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
1761                         }
1762                 }
1763
1764                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1765                 protected override void SetVisibleCore(bool value) {
1766                         is_changing_visible_state = true;
1767                         has_been_visible = value || has_been_visible;
1768                         base.SetVisibleCore (value);
1769                         is_changing_visible_state = false;
1770                 }
1771
1772                 protected override void UpdateDefaultButton() {
1773                         base.UpdateDefaultButton ();
1774                 }
1775
1776                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1777                 protected override void WndProc(ref Message m) {
1778
1779                         if (window_manager != null && window_manager.HandleMessage (ref m)) {
1780                                 return;
1781                         }
1782
1783                         switch((Msg)m.Msg) {
1784                                 case Msg.WM_DESTROY: {
1785                                         base.WndProc(ref m);
1786                                         if (!is_recreating) {
1787                                                 this.closing = true;
1788                                         }
1789                                         return;
1790                                 }
1791
1792                                 case Msg.WM_CLOSE_INTERNAL: {
1793                                         DestroyHandle();
1794                                         break;
1795                                 }
1796
1797                                 case Msg.WM_CLOSE: {
1798                                         Form act = Form.ActiveForm;
1799                                         if (act != null && act != this && act.Modal == true) {
1800                                                 return;
1801                                         }
1802
1803                                         CancelEventArgs args = new CancelEventArgs ();
1804
1805                                         if (mdi_container != null) {
1806                                                 foreach (Form mdi_child in mdi_container.MdiChildren) {
1807                                                         mdi_child.OnClosing (args);
1808                                                 }
1809                                         }
1810
1811                                         if (!is_modal) {
1812                                                 OnClosing (args);
1813                                                 if (!args.Cancel) {
1814                                                         OnClosed (EventArgs.Empty);
1815                                                         closing = true;
1816                                                 }
1817                                                 Dispose ();
1818                                         } else {
1819                                                 OnClosing (args);
1820                                                 if (args.Cancel) {
1821                                                         DialogResult = DialogResult.None;
1822                                                         closing = false;
1823                                                 } else {
1824                                                         OnClosed (EventArgs.Empty);
1825                                                         closing = true;
1826                                                         Hide ();
1827                                                 }
1828                                         }
1829
1830                                         return;
1831                                 }
1832
1833                                 case Msg.WM_WINDOWPOSCHANGED: {
1834                                         if (WindowState != FormWindowState.Minimized) {
1835                                                 base.WndProc(ref m);
1836                                         }
1837                                         return;
1838                                 }
1839
1840 #if NET_2_0
1841                                 case Msg.WM_SYSCOMMAND: {
1842                                         // Let *Strips know the app's title bar was clicked
1843                                         if (XplatUI.IsEnabled (Handle))
1844                                                 ToolStripManager.FireAppClicked ();
1845                                                 
1846                                         base.WndProc(ref m);
1847                                         break;
1848                                 }
1849 #endif
1850         
1851                                 case Msg.WM_ACTIVATE: {
1852                                         if (m.WParam != (IntPtr)WindowActiveFlags.WA_INACTIVE) {
1853                                                 OnActivated(EventArgs.Empty);
1854                                         } else {
1855                                                 OnDeactivate(EventArgs.Empty);
1856                                         }
1857                                         return;
1858                                 }
1859
1860                                 case Msg.WM_KILLFOCUS: {
1861                                         base.WndProc(ref m);
1862                                         return;
1863                                 }
1864
1865                                 case Msg.WM_SETFOCUS: {
1866                                         if (ActiveControl != null && ActiveControl != this) {
1867                                                 ActiveControl.Focus();
1868                                                 return; // FIXME - do we need to run base.WndProc, even though we just changed focus?
1869                                         }
1870                                         base.WndProc(ref m);
1871                                         return;
1872                                 }
1873
1874                                 // Menu drawing
1875                                 case Msg.WM_NCLBUTTONDOWN: {
1876                                         if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
1877                                                 ActiveMenu.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, Control.MousePosition.X, Control.MousePosition.Y, 0));
1878                                         }
1879
1880                                         if (ActiveMaximizedMdiChild != null) {
1881                                                 if (ActiveMaximizedMdiChild.HandleMenuMouseDown (ActiveMenu,
1882                                                                 LowOrder ((int) m.LParam.ToInt32 ()),
1883                                                                 HighOrder ((int) m.LParam.ToInt32 ()))) {
1884                                                         // Don't let base process this message, otherwise we won't
1885                                                         // get a WM_NCLBUTTONUP.
1886                                                         return;
1887                                                 }
1888                                         }
1889                                         base.WndProc(ref m);
1890                                         return;
1891                                 }
1892                                 case Msg.WM_NCLBUTTONUP: {
1893                                         if (ActiveMaximizedMdiChild != null) {
1894                                                 ActiveMaximizedMdiChild.HandleMenuMouseUp (ActiveMenu,
1895                                                                 LowOrder ((int)m.LParam.ToInt32 ()),
1896                                                                 HighOrder ((int)m.LParam.ToInt32 ()));
1897                                         }
1898                                         base.WndProc (ref m);
1899                                         return;
1900                                 }
1901
1902                                 case Msg.WM_NCMOUSELEAVE: {
1903                                         if (ActiveMaximizedMdiChild != null) {
1904                                                 ActiveMaximizedMdiChild.HandleMenuMouseLeave(ActiveMenu,
1905                                                                 LowOrder((int)m.LParam.ToInt32()),
1906                                                                 HighOrder((int)m.LParam.ToInt32()));
1907                                         }
1908                                         base.WndProc(ref m);
1909                                         return;
1910                                 }
1911                                 
1912                                 case Msg.WM_NCMOUSEMOVE: {
1913                                         if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
1914                                                 ActiveMenu.OnMouseMove(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0));
1915                                         }
1916                                         
1917                                         if (ActiveMaximizedMdiChild != null) {
1918                                                 ActiveMaximizedMdiChild.HandleMenuMouseMove (ActiveMenu,
1919                                                                 LowOrder ((int)m.LParam.ToInt32 ()),
1920                                                                 HighOrder ((int)m.LParam.ToInt32 ()));
1921                                         }
1922                                         base.WndProc(ref m);
1923                                         return;
1924                                 }
1925
1926                                 case Msg.WM_NCPAINT: {
1927                                         if (ActiveMenu != null) {
1928                                                 PaintEventArgs  pe;
1929                                                 Point           pnt;
1930
1931                                                 pe = XplatUI.PaintEventStart(Handle, false);
1932                                                 pnt = XplatUI.GetMenuOrigin(window.Handle);
1933
1934                                                 // The entire menu has to be in the clip rectangle because the 
1935                                                 // control buttons are right-aligned and otherwise they would
1936                                                 // stay painted when the window gets resized.
1937                                                 Rectangle clip = new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0);
1938                                                 clip = Rectangle.Union(clip, pe.ClipRectangle);
1939                                                 pe.SetClip(clip);
1940                                                 pe.Graphics.SetClip(clip);
1941                                                 
1942                                                 ActiveMenu.Draw (pe, new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0));
1943
1944                                                 if (ActiveMaximizedMdiChild != null) {
1945                                                         ActiveMaximizedMdiChild.DrawMaximizedButtons (ActiveMenu, pe);
1946                                                 }
1947
1948                                                 XplatUI.PaintEventEnd(Handle, false);
1949                                         }
1950
1951                                         base.WndProc(ref m);
1952                                         return;
1953                                 }
1954
1955                                 case Msg.WM_NCCALCSIZE: {
1956                                         XplatUIWin32.NCCALCSIZE_PARAMS  ncp;
1957
1958                                         if ((ActiveMenu != null) && (m.WParam == (IntPtr)1)) {
1959                                                 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
1960
1961                                                 // Adjust for menu
1962                                                 ncp.rgrc1.top += ThemeEngine.Current.CalcMenuBarSize (DeviceContext, ActiveMenu, ncp.rgrc1.right - ncp.rgrc1.left);
1963                                                 Marshal.StructureToPtr(ncp, m.LParam, true);
1964                                         }
1965                                         DefWndProc(ref m);
1966                                         break;
1967                                 }
1968
1969                                 case Msg.WM_MOUSEMOVE: {
1970                                         if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
1971                                                 MouseEventArgs args;
1972
1973                                                 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
1974                                                         mouse_clicks,  LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),  0);
1975                                                 active_tracker.OnMotion(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
1976                                                 break;
1977                                         }
1978                                         base.WndProc(ref m);
1979                                         break;
1980                                 }
1981
1982                                 case Msg.WM_LBUTTONDOWN:
1983                                 case Msg.WM_MBUTTONDOWN:
1984                                 case Msg.WM_RBUTTONDOWN: {                                      
1985                                         if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
1986                                                 MouseEventArgs args;
1987
1988                                                 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
1989                                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
1990                                                 active_tracker.OnMouseDown(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
1991                                                 return;
1992                                         }
1993                                         base.WndProc(ref m);
1994                                         return;
1995                                 }
1996
1997                                 case Msg.WM_LBUTTONUP:
1998                                 case Msg.WM_MBUTTONUP:
1999                                 case Msg.WM_RBUTTONUP: {
2000                                         if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2001                                                 MouseEventArgs args;
2002                                                 MouseButtons mb = FromParamToMouseButtons ((int) m.WParam.ToInt32());
2003                                                 
2004                                                 // We add in the button that was released (not sent in WParam)
2005                                                 switch((Msg)m.Msg) {
2006                                                         case Msg.WM_LBUTTONUP:
2007                                                                 mb |= MouseButtons.Left;
2008                                                                 break;
2009                                                         case Msg.WM_MBUTTONUP:
2010                                                                 mb |= MouseButtons.Middle;
2011                                                                 break;
2012                                                         case Msg.WM_RBUTTONUP:
2013                                                                 mb |= MouseButtons.Right;
2014                                                                 break;
2015                                                 }
2016                                                 
2017                                                 args = new MouseEventArgs (mb, mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2018                                                 active_tracker.OnMouseUp(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2019                                                 mouse_clicks = 1;
2020                                                 return;
2021                                         }
2022                                         base.WndProc(ref m);
2023                                         return;
2024                                 }
2025
2026                                 case Msg.WM_GETMINMAXINFO: {
2027                                         MINMAXINFO      mmi;
2028
2029                                         if (m.LParam != IntPtr.Zero) {
2030                                                 mmi = (MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO));
2031
2032                                                 default_maximized_bounds = new Rectangle(mmi.ptMaxPosition.x, mmi.ptMaxPosition.y, mmi.ptMaxSize.x, mmi.ptMaxSize.y);
2033                                                 if (maximized_bounds != Rectangle.Empty) {
2034                                                         mmi.ptMaxPosition.x = maximized_bounds.Left;
2035                                                         mmi.ptMaxPosition.y = maximized_bounds.Top;
2036                                                         mmi.ptMaxSize.x = maximized_bounds.Width;
2037                                                         mmi.ptMaxSize.y = maximized_bounds.Height;
2038                                                 }
2039
2040                                                 if (minimum_size != Size.Empty) {
2041                                                         mmi.ptMinTrackSize.x = minimum_size.Width;
2042                                                         mmi.ptMinTrackSize.y = minimum_size.Height;
2043                                                 }
2044
2045                                                 if (maximum_size != Size.Empty) {
2046                                                         mmi.ptMaxTrackSize.x = maximum_size.Width;
2047                                                         mmi.ptMaxTrackSize.y = maximum_size.Height;
2048                                                 }
2049                                                 Marshal.StructureToPtr(mmi, m.LParam, false);
2050                                         }
2051                                         break;
2052                                 }
2053                                 
2054 #if NET_2_0
2055                                 case Msg.WM_MOUSEACTIVATE: {
2056                                         // Let *Strips know the form or another control has been clicked
2057                                         if (XplatUI.IsEnabled (Handle))
2058                                                 ToolStripManager.FireAppClicked ();
2059                                                 
2060                                         base.WndProc (ref m);
2061                                         break;                          
2062                                 }
2063                                 
2064                                 case Msg.WM_ACTIVATEAPP: {
2065                                         // Let *Strips know the app lost focus
2066                                         if (m.WParam == (IntPtr)0) 
2067                                                 if (XplatUI.IsEnabled (Handle))
2068                                                         ToolStripManager.FireAppFocusChanged (this);
2069                                                         
2070                                         base.WndProc (ref m);
2071                                         break;                          
2072                                 }
2073 #endif
2074
2075                                 default: {
2076                                         base.WndProc (ref m);
2077                                         break;
2078                                 }
2079                         }
2080                 }
2081                 #endregion      // Protected Instance Methods
2082
2083                 internal void RemoveWindowManager ()
2084                 {
2085                         window_manager = null;
2086                 }
2087                 
2088                 internal override void CheckAcceptButton()
2089                 {
2090                         if (accept_button != null) {
2091                                 Button a_button = accept_button as Button;
2092                                 
2093                                 if (ActiveControl == a_button)
2094                                         return;
2095                                 
2096                                 if (ActiveControl is Button) {
2097                                         a_button.paint_as_acceptbutton = false;
2098                                         a_button.Redraw();
2099                                         return;
2100                                 } else {
2101                                         a_button.paint_as_acceptbutton = true;
2102                                         a_button.Redraw();
2103                                 }
2104                         }
2105                 }
2106                 
2107                 #region Events
2108                 static object ActivatedEvent = new object ();
2109                 static object ClosedEvent = new object ();
2110                 static object ClosingEvent = new object ();
2111                 static object DeactivateEvent = new object ();
2112                 static object InputLanguageChangedEvent = new object ();
2113                 static object InputLanguageChangingEvent = new object ();
2114                 static object LoadEvent = new object ();
2115                 static object MaximizedBoundsChangedEvent = new object ();
2116                 static object MaximumSizeChangedEvent = new object ();
2117                 static object MdiChildActivateEvent = new object ();
2118                 static object MenuCompleteEvent = new object ();
2119                 static object MenuStartEvent = new object ();
2120                 static object MinimumSizeChangedEvent = new object ();
2121
2122                 public event EventHandler Activated {
2123                         add { Events.AddHandler (ActivatedEvent, value); }
2124                         remove { Events.RemoveHandler (ActivatedEvent, value); }
2125                 }
2126
2127                 public event EventHandler Closed {
2128                         add { Events.AddHandler (ClosedEvent, value); }
2129                         remove { Events.RemoveHandler (ClosedEvent, value); }
2130                 }
2131
2132                 public event CancelEventHandler Closing {
2133                         add { Events.AddHandler (ClosingEvent, value); }
2134                         remove { Events.RemoveHandler (ClosingEvent, value); }
2135                 }
2136
2137                 public event EventHandler Deactivate {
2138                         add { Events.AddHandler (DeactivateEvent, value); }
2139                         remove { Events.RemoveHandler (DeactivateEvent, value); }
2140                 }
2141
2142                 public event InputLanguageChangedEventHandler InputLanguageChanged {
2143                         add { Events.AddHandler (InputLanguageChangedEvent, value); }
2144                         remove { Events.RemoveHandler (InputLanguageChangedEvent, value); }
2145                 }
2146
2147                 public event InputLanguageChangingEventHandler InputLanguageChanging {
2148                         add { Events.AddHandler (InputLanguageChangingEvent, value); }
2149                         remove { Events.RemoveHandler (InputLanguageChangingEvent, value); }
2150                 }
2151
2152                 public event EventHandler Load {
2153                         add { Events.AddHandler (LoadEvent, value); }
2154                         remove { Events.RemoveHandler (LoadEvent, value); }
2155                 }
2156
2157                 public event EventHandler MaximizedBoundsChanged {
2158                         add { Events.AddHandler (MaximizedBoundsChangedEvent, value); }
2159                         remove { Events.RemoveHandler (MaximizedBoundsChangedEvent, value); }
2160                 }
2161
2162                 public event EventHandler MaximumSizeChanged {
2163                         add { Events.AddHandler (MaximumSizeChangedEvent, value); }
2164                         remove { Events.RemoveHandler (MaximumSizeChangedEvent, value); }
2165                 }
2166
2167                 public event EventHandler MdiChildActivate {
2168                         add { Events.AddHandler (MdiChildActivateEvent, value); }
2169                         remove { Events.RemoveHandler (MdiChildActivateEvent, value); }
2170                 }
2171
2172                 public event EventHandler MenuComplete {
2173                         add { Events.AddHandler (MenuCompleteEvent, value); }
2174                         remove { Events.RemoveHandler (MenuCompleteEvent, value); }
2175                 }
2176
2177                 public event EventHandler MenuStart {
2178                         add { Events.AddHandler (MenuStartEvent, value); }
2179                         remove { Events.RemoveHandler (MenuStartEvent, value); }
2180                 }
2181
2182                 public event EventHandler MinimumSizeChanged {
2183                         add { Events.AddHandler (MinimumSizeChangedEvent, value); }
2184                         remove { Events.RemoveHandler (MinimumSizeChangedEvent, value); }
2185                 }
2186
2187
2188                 [Browsable(false)]
2189                 [EditorBrowsable(EditorBrowsableState.Never)]
2190                 public new event EventHandler TabIndexChanged {
2191                         add { base.TabIndexChanged += value; }
2192                         remove { base.TabIndexChanged -= value; }
2193                 }
2194                 #endregion      // Events
2195
2196 #if NET_2_0
2197                 public override string Text {
2198                         get {
2199                                 return base.Text;
2200                         }
2201
2202                         set {
2203                                 base.Text = value;
2204                         }
2205                 }
2206
2207                 public new Point Location {
2208                         get {
2209                                 return base.Location;
2210                         }
2211
2212                         set {
2213                                 base.Location = value;
2214                         }
2215                 }
2216
2217                 static object FormClosingEvent = new object ();
2218                 static object FormClosedEvent = new object ();
2219
2220                 public event FormClosingEventHandler FormClosing {
2221                         add { Events.AddHandler (FormClosingEvent, value); }
2222                         remove { Events.RemoveHandler (FormClosingEvent, value); }
2223                 }
2224
2225                 public event FormClosedEventHandler FormClosed {
2226                         add { Events.AddHandler (FormClosedEvent, value); }
2227                         remove { Events.RemoveHandler (FormClosedEvent, value); }
2228                 }
2229                 
2230                 protected virtual void OnFormClosing (FormClosingEventArgs e)
2231                 {
2232                         FormClosingEventHandler eh = (FormClosingEventHandler)(Events [FormClosingEvent]);
2233                         if (eh != null)
2234                                 eh (this, e);
2235                 }
2236 #endif
2237         }
2238 }