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