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