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