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