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