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