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