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