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