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