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