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