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