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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2006 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
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
;
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")]
43 [ClassInterface (ClassInterfaceType
.AutoDispatch
)]
44 [InitializationEvent ("Load")]
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
;
65 private IButtonControl accept_button
;
66 private IButtonControl cancel_button
;
67 private DialogResult dialog_result
;
68 private FormStartPosition start_position
;
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
;
77 private Size maximum_size
;
78 private Size minimum_size
;
79 private SizeGripStyle size_grip_style
;
80 private Rectangle maximized_bounds
;
81 private Rectangle default_maximized_bounds
;
82 private double opacity
;
83 internal ApplicationContext context
;
84 Color transparency_key
;
85 internal MenuTracker active_tracker
;
86 private bool is_loaded
;
87 internal bool is_changing_visible_state
;
88 internal bool has_been_visible
;
91 private MenuStrip main_menu_strip
;
92 private bool show_icon
;
93 private bool shown_raised
; // The shown event is only raised once
95 #endregion // Local Variables
97 #region Private & Internal Methods
100 default_icon
= Locale
.GetResource("mono.ico") as Icon
;
103 // warning: this is only hooked up when an mdi container is created.
104 private void ControlAddedHandler (object sender
, ControlEventArgs e
)
106 if (mdi_container
!= null) {
107 mdi_container
.SendToBack ();
111 // Convenience method for fire BOTH OnClosing and OnFormClosing events
112 // Returns the value of Cancel, so true means the Close was cancelled,
113 // and you shouldn't close the form.
114 internal bool FireClosingEvents (CloseReason reason
)
116 CancelEventArgs cea
= new CancelEventArgs ();
117 this.OnClosing (cea
);
120 FormClosingEventArgs fcea
= new FormClosingEventArgs (reason
, cea
.Cancel
);
121 this.OnFormClosing (fcea
);
128 private void SelectActiveControl ()
130 if (this.IsMdiContainer
) {
131 mdi_container
.SendFocusToActiveChild ();
135 if (this.ActiveControl
== null) {
138 // This visible hack is to work around CanSelect always being false if one of the parents
139 // is not visible; and we by default create Form invisible...
140 visible
= this.is_visible
;
141 this.is_visible
= true;
143 if (SelectNextControl (this, true, true, true, true) == false) {
147 this.is_visible
= visible
;
149 Select (ActiveControl
);
152 #endregion // Private & Internal Methods
154 #region Public Classes
155 public new class ControlCollection
: Control
.ControlCollection
{
158 public ControlCollection(Form owner
) : base(owner
) {
159 this.form_owner
= owner
;
162 public override void Add(Control
value) {
163 if (Contains (value))
166 ((Form
)value).owner
=form_owner
;
169 public override void Remove(Control
value) {
170 ((Form
)value).owner
= null;
174 #endregion // Public Classes
176 #region Public Constructor & Destructor
179 SizeF current_scale
= GetAutoScaleSize (DeviceContext
, Font
);
182 autoscale_base_size
= new Size ((int)current_scale
.Width
, (int) current_scale
.Height
);
183 allow_transparency
= false;
186 dialog_result
= DialogResult
.None
;
187 start_position
= FormStartPosition
.WindowsDefaultLocation
;
188 form_border_style
= FormBorderStyle
.Sizable
;
189 window_state
= FormWindowState
.Normal
;
194 minimum_size
= Size
.Empty
;
195 maximum_size
= Size
.Empty
;
196 clientsize_set
= Size
.Empty
;
201 show_in_taskbar
= true;
204 size_grip_style
= SizeGripStyle
.Auto
;
205 maximized_bounds
= Rectangle
.Empty
;
206 default_maximized_bounds
= Rectangle
.Empty
;
207 owned_forms
= new Form
.ControlCollection(this);
208 transparency_key
= Color
.Empty
;
214 // FIXME: this should disappear just as soon as the handle creation is done in the right place (here is too soon()
218 #endregion // Public Constructor & Destructor
220 #region Public Static Properties
222 public static Form ActiveForm
{
226 active
= FromHandle(XplatUI
.GetActive());
228 if (active
!= null) {
229 if ( !(active
is Form
)) {
232 parent
= active
.Parent
;
233 while (parent
!= null) {
234 if (parent
is Form
) {
237 parent
= parent
.Parent
;
247 #endregion // Public Static Properties
249 #region Public Instance Properties
251 public IButtonControl AcceptButton
{
253 return accept_button
;
257 accept_button
= value;
263 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
264 public bool AllowTransparency
{
266 return allow_transparency
;
270 if (value == allow_transparency
) {
274 allow_transparency
= value;
277 if (IsHandleCreated
) {
278 if ((XplatUI
.SupportsTransparency() & TransparencySupport
.Set
) != 0) {
279 XplatUI
.SetWindowTransparency(Handle
, Opacity
, TransparencyKey
);
282 UpdateStyles(); // Remove the WS_EX_LAYERED style
290 [EditorBrowsable (EditorBrowsableState
.Never
)]
291 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
292 [Obsolete ("This property has been deprecated in favor of AutoScaleMode.")]
296 [MWFCategory("Layout")]
297 public bool AutoScale
{
308 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
309 [EditorBrowsable(EditorBrowsableState
.Never
)]
311 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
315 public virtual Size AutoScaleBaseSize
{
317 return autoscale_base_size
;
321 autoscale_base_size
= value;
326 public override bool AutoScroll
{
328 return base.AutoScroll
;
331 base.AutoScroll
= value;
335 public override Color BackColor
{
337 /* we don't let parents override our
338 default background color for forms.
339 this fixes the default color for mdi
341 if (background_color
.IsEmpty
)
342 return DefaultBackColor
;
344 return background_color
;
347 base.BackColor
= value;
352 public IButtonControl CancelButton
{
354 return cancel_button
;
358 cancel_button
= value;
359 if (cancel_button
!= null && cancel_button
.DialogResult
== DialogResult
.None
)
360 cancel_button
.DialogResult
= DialogResult
.Cancel
;
364 // new property so we can change the DesignerSerializationVisibility
365 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Visible
)]
367 public new Size ClientSize
{
368 get { return base.ClientSize; }
369 set { base.ClientSize = value; }
373 [MWFCategory("Window Style")]
374 public bool ControlBox
{
380 if (control_box
!= value) {
388 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
389 public Rectangle DesktopBounds
{
391 return new Rectangle(Location
, Size
);
400 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
401 public Point DesktopLocation
{
412 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
413 public DialogResult DialogResult
{
415 return dialog_result
;
419 if (value < DialogResult
.None
|| value > DialogResult
.No
)
420 throw new InvalidEnumArgumentException ("value", (int) value,
421 typeof (DialogResult
));
423 dialog_result
= value;
424 closing
= (dialog_result
!= DialogResult
.None
&& is_modal
);
428 [DefaultValue(FormBorderStyle
.Sizable
)]
430 [MWFCategory("Appearance")]
431 public FormBorderStyle FormBorderStyle
{
433 return form_border_style
;
436 form_border_style
= value;
438 if (window_manager
== null) {
439 if (IsHandleCreated
) {
440 XplatUI
.SetBorderStyle(window
.Handle
, form_border_style
);
443 window_manager
.UpdateBorderStyle (value);
450 [DefaultValue(false)]
451 [MWFCategory("Window Style")]
452 public bool HelpButton
{
458 if (help_button
!= value) {
467 [MWFCategory("Window Style")]
477 if (IsHandleCreated
) {
478 XplatUI
.SetIcon(Handle
, icon
== null ? default_icon
: icon
);
485 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
486 public bool IsMdiChild
{
488 return mdi_parent
!= null;
492 [DefaultValue(false)]
493 [MWFCategory("Window Style")]
494 public bool IsMdiContainer
{
496 return mdi_container
!= null;
500 if (value && mdi_container
== null) {
501 mdi_container
= new MdiClient ();
502 Controls
.Add(mdi_container
);
503 ControlAdded
+= new ControlEventHandler (ControlAddedHandler
);
504 mdi_container
.SendToBack ();
505 mdi_container
.SetParentText (true);
506 } else if (!value && mdi_container
!= null) {
507 Controls
.Remove(mdi_container
);
508 mdi_container
= null;
514 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
515 public Form ActiveMdiChild
{
519 return (Form
) mdi_container
.ActiveMdiChild
;
524 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
525 public bool IsRestrictedWindow
{
531 [DefaultValue(false)]
532 public bool KeyPreview
{
543 [DefaultValue (null)]
544 [TypeConverter (typeof (ReferenceConverter
))]
545 public MenuStrip MainMenuStrip
{
546 get { return this.main_menu_strip; }
548 if (this.main_menu_strip
!= value) {
549 this.main_menu_strip
= value;
550 this.main_menu_strip
.RefreshMdiItems ();
556 public new Padding Margin
{
557 get { return base.Margin; }
558 set { base.Margin = value; }
563 [MWFCategory("Window Style")]
564 public bool MaximizeBox
{
569 if (maximize_box
!= value) {
570 maximize_box
= value;
571 if (IsHandleCreated
) {
579 [DefaultValue("{Width=0, Height=0}")]
581 [RefreshProperties(RefreshProperties
.Repaint
)]
582 [MWFCategory("Layout")]
593 if (maximum_size
!= value) {
594 maximum_size
= value;
595 OnMaximumSizeChanged(EventArgs
.Empty
);
596 if (IsHandleCreated
) {
597 XplatUI
.SetWindowMinMax(Handle
, maximized_bounds
, minimum_size
, maximum_size
);
604 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
605 public Form
[] MdiChildren
{
607 if (mdi_container
!= null)
608 return mdi_container
.MdiChildren
;
615 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
616 public Form MdiParent
{
622 if (value != null && !value.IsMdiContainer
)
623 throw new ArgumentException ("Form that was specified to be "
624 + "the MdiParent for this form is not an MdiContainer.");
626 if (mdi_parent
!= null) {
627 mdi_parent
.MdiContainer
.Controls
.Remove (this);
632 window_manager
= new MdiWindowManager (this,
633 mdi_parent
.MdiContainer
);
634 mdi_parent
.MdiContainer
.Controls
.Add (this);
635 mdi_parent
.MdiContainer
.Controls
.SetChildIndex (this, 0);
639 } else if (mdi_parent
!= null) {
642 // Create a new window manager
643 window_manager
= null;
644 FormBorderStyle
= form_border_style
;
651 internal MenuTracker ActiveTracker
{
652 get { return active_tracker; }
654 if (value == active_tracker
)
657 Capture
= value != null;
658 active_tracker
= value;
662 internal MdiClient MdiContainer
{
663 get { return mdi_container; }
666 internal InternalWindowManager WindowManager
{
667 get { return window_manager; }
672 [TypeConverter (typeof (ReferenceConverter
))]
675 [MWFCategory("Window Style")]
676 public MainMenu Menu
{
685 if (menu
!= null && !IsMdiChild
) {
688 if (IsHandleCreated
) {
689 XplatUI
.SetMenu (window
.Handle
, menu
);
692 if (clientsize_set
!= Size
.Empty
) {
693 SetClientSizeCore(clientsize_set
.Width
, clientsize_set
.Height
);
695 UpdateBounds (bounds
.X
, bounds
.Y
, bounds
.Width
, bounds
.Height
, ClientSize
.Width
, ClientSize
.Height
-
696 ThemeEngine
.Current
.CalcMenuBarSize (DeviceContext
, menu
, ClientSize
.Width
));
705 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
706 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
707 public MainMenu MergedMenu
{
709 if (!IsMdiChild
|| window_manager
== null)
711 return ((MdiWindowManager
) window_manager
).MergedMenu
;
715 // This is the menu in display and being used because of merging this can
716 // be different then the menu that is actually assosciated with the form
717 internal MainMenu ActiveMenu
{
722 if (IsMdiContainer
&& mdi_container
.Controls
.Count
> 0 &&
723 ((Form
) mdi_container
.Controls
[0]).WindowState
== FormWindowState
.Maximized
) {
724 MdiWindowManager wm
= (MdiWindowManager
) ((Form
) mdi_container
.Controls
[0]).WindowManager
;
725 return wm
.MaximizedMenu
;
728 Form amc
= ActiveMdiChild
;
729 if (amc
== null || amc
.Menu
== null)
731 return amc
.MergedMenu
;
735 internal MdiWindowManager ActiveMaximizedMdiChild
{
737 Form child
= ActiveMdiChild
;
740 if (child
.WindowManager
== null || child
.window_state
!= FormWindowState
.Maximized
)
742 return (MdiWindowManager
) child
.WindowManager
;
747 [MWFCategory("Window Style")]
748 public bool MinimizeBox
{
753 if (minimize_box
!= value) {
754 minimize_box
= value;
755 if (IsHandleCreated
) {
764 [DefaultValue("{Width=0, Height=0}")]
767 [RefreshProperties(RefreshProperties
.Repaint
)]
768 [MWFCategory("Layout")]
779 if (minimum_size
!= value) {
780 minimum_size
= value;
782 if ((Size
.Width
< value.Width
) || (Size
.Height
< value.Height
)) {
783 Size
= new Size(Math
.Max(Size
.Width
, value.Width
), Math
.Max(Size
.Height
, value.Height
));
787 OnMinimumSizeChanged(EventArgs
.Empty
);
788 if (IsHandleCreated
) {
789 XplatUI
.SetWindowMinMax(Handle
, maximized_bounds
, minimum_size
, maximum_size
);
796 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
804 [TypeConverter(typeof(OpacityConverter
))]
805 [MWFCategory("Window Style")]
806 public double Opacity
{
808 if (IsHandleCreated
) {
809 if ((XplatUI
.SupportsTransparency () & TransparencySupport
.Get
) != 0)
810 return XplatUI
.GetWindowTransparency (Handle
);
819 AllowTransparency
= true;
821 if (IsHandleCreated
) {
823 if ((XplatUI
.SupportsTransparency () & TransparencySupport
.Set
) != 0)
824 XplatUI
.SetWindowTransparency(Handle
, opacity
, TransparencyKey
);
831 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
832 public Form
[] OwnedForms
{
836 form_list
= new Form
[owned_forms
.Count
];
838 for (int i
=0; i
<owned_forms
.Count
; i
++) {
839 form_list
[i
] = (Form
)owned_forms
[i
];
847 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
854 if (owner
!= value) {
856 owner
.RemoveOwnedForm(this);
860 owner
.AddOwnedForm(this);
861 if (IsHandleCreated
) {
862 if (owner
!= null && owner
.IsHandleCreated
) {
863 XplatUI
.SetTopmost(this.window
.Handle
, owner
.window
.Handle
, true);
865 XplatUI
.SetTopmost(this.window
.Handle
, IntPtr
.Zero
, false);
873 [DefaultValue (true)]
874 public bool ShowIcon
{
875 get { return this.show_icon; }
877 if (this.show_icon
!= value ) {
878 this.show_icon
= value;
881 XplatUI
.SetIcon (this.Handle
, value == true ? this.Icon
: null);
882 XplatUI
.InvalidateNC (this.Handle
);
889 [MWFCategory("Window Style")]
890 public bool ShowInTaskbar
{
892 return show_in_taskbar
;
895 if (show_in_taskbar
!= value) {
896 show_in_taskbar
= value;
897 if (IsHandleCreated
) {
905 // new property so we can set the DesignerSerializationVisibility
906 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
908 public new Size Size
{
909 get { return base.Size; }
910 set { base.Size = value; }
913 [MonoTODO("Trigger something when GripStyle is set")]
914 [DefaultValue(SizeGripStyle
.Auto
)]
915 [MWFCategory("Window Style")]
916 public SizeGripStyle SizeGripStyle
{
918 return size_grip_style
;
922 size_grip_style
= value;
926 [DefaultValue(FormStartPosition
.WindowsDefaultLocation
)]
928 [MWFCategory("Layout")]
929 public FormStartPosition StartPosition
{
931 return start_position
;
935 if (start_position
== FormStartPosition
.WindowsDefaultLocation
) { // Only do this if it's not set yet
936 start_position
= value;
937 if (IsHandleCreated
) {
938 switch(start_position
) {
939 case FormStartPosition
.CenterParent
: {
944 case FormStartPosition
.CenterScreen
: {
949 case FormStartPosition
.Manual
: {
950 Left
= CreateParams
.X
;
951 Top
= CreateParams
.Y
;
964 // new property so we can set EditorBrowsable to never
966 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
967 [EditorBrowsable(EditorBrowsableState
.Never
)]
968 public new int TabIndex
{
969 get { return base.TabIndex; }
970 set { base.TabIndex = value; }
975 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
976 [EditorBrowsable(EditorBrowsableState
.Never
)]
977 public new bool TabStop
{
978 get { return base.TabStop; }
979 set { base.TabStop = value; }
984 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
985 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
986 public bool TopLevel
{
988 return GetTopLevel();
992 if (!value && IsMdiContainer
)
993 throw new ArgumentException ("MDI Container forms must be top level.");
998 [DefaultValue(false)]
999 [MWFCategory("Window Style")]
1000 public bool TopMost
{
1006 if (topmost
!= value) {
1008 if (IsHandleCreated
)
1009 XplatUI
.SetTopmost(window
.Handle
, owner
!= null ? owner
.window
.Handle
: IntPtr
.Zero
, value);
1014 [MWFCategory("Window Style")]
1015 public Color TransparencyKey
{
1017 return transparency_key
;
1021 transparency_key
= value;
1023 AllowTransparency
= true;
1025 if ((XplatUI
.SupportsTransparency () & TransparencySupport
.Set
) != 0)
1026 XplatUI
.SetWindowTransparency(Handle
, Opacity
, transparency_key
);
1030 [DefaultValue(FormWindowState
.Normal
)]
1031 [MWFCategory("Layout")]
1032 public FormWindowState WindowState
{
1034 if (IsHandleCreated
) {
1036 if (window_manager
!= null)
1037 return window_manager
.GetWindowState ();
1039 FormWindowState new_state
= XplatUI
.GetWindowState(Handle
);
1040 if (new_state
!= (FormWindowState
)(-1))
1041 window_state
= new_state
;
1044 return window_state
;
1048 FormWindowState old_state
= window_state
;
1049 window_state
= value;
1050 if (IsHandleCreated
) {
1052 if (window_manager
!= null) {
1053 window_manager
.SetWindowState (old_state
, value);
1057 XplatUI
.SetWindowState(Handle
, value);
1062 #endregion // Public Instance Properties
1064 #region Protected Instance Properties
1065 protected override CreateParams CreateParams
{
1067 CreateParams cp
= new CreateParams ();
1070 cp
.ClassName
= XplatUI
.DefaultClassName
;
1075 cp
.Parent
= IntPtr
.Zero
;
1076 cp
.menu
= ActiveMenu
;
1078 if (start_position
== FormStartPosition
.WindowsDefaultLocation
&& !IsMdiChild
) {
1079 cp
.X
= unchecked((int)0x80000000);
1080 cp
.Y
= unchecked((int)0x80000000);
1088 cp
.Style
= (int)(WindowStyles
.WS_CLIPCHILDREN
| WindowStyles
.WS_CLIPSIBLINGS
);
1091 cp
.Style
|= (int)(WindowStyles
.WS_CHILD
| WindowStyles
.WS_CAPTION
);
1092 if (Parent
!= null) {
1093 cp
.Parent
= Parent
.Handle
;
1096 cp
.ExStyle
|= (int) (WindowExStyles
.WS_EX_WINDOWEDGE
| WindowExStyles
.WS_EX_MDICHILD
);
1098 switch (FormBorderStyle
) {
1099 case FormBorderStyle
.None
:
1101 case FormBorderStyle
.FixedToolWindow
:
1102 case FormBorderStyle
.SizableToolWindow
:
1103 cp
.ExStyle
|= (int) WindowExStyles
.WS_EX_TOOLWINDOW
;
1106 cp
.Style
|= (int) WindowStyles
.WS_OVERLAPPEDWINDOW
;
1111 switch (FormBorderStyle
) {
1112 case FormBorderStyle
.Fixed3D
: {
1113 cp
.Style
|= (int)(WindowStyles
.WS_CAPTION
| WindowStyles
.WS_BORDER
);
1114 cp
.ExStyle
|= (int)WindowExStyles
.WS_EX_CLIENTEDGE
;
1118 case FormBorderStyle
.FixedDialog
: {
1119 cp
.Style
|= (int)(WindowStyles
.WS_CAPTION
| WindowStyles
.WS_BORDER
);
1120 cp
.ExStyle
|= (int)(WindowExStyles
.WS_EX_DLGMODALFRAME
| WindowExStyles
.WS_EX_CONTROLPARENT
);
1124 case FormBorderStyle
.FixedSingle
: {
1125 cp
.Style
|= (int)(WindowStyles
.WS_CAPTION
| WindowStyles
.WS_BORDER
);
1129 case FormBorderStyle
.FixedToolWindow
: {
1130 cp
.Style
|= (int)(WindowStyles
.WS_CAPTION
| WindowStyles
.WS_BORDER
);
1131 cp
.ExStyle
|= (int)(WindowExStyles
.WS_EX_TOOLWINDOW
);
1135 case FormBorderStyle
.Sizable
: {
1136 cp
.Style
|= (int)(WindowStyles
.WS_BORDER
| WindowStyles
.WS_THICKFRAME
| WindowStyles
.WS_CAPTION
);
1140 case FormBorderStyle
.SizableToolWindow
: {
1141 cp
.Style
|= (int)(WindowStyles
.WS_BORDER
| WindowStyles
.WS_THICKFRAME
| WindowStyles
.WS_CAPTION
);
1142 cp
.ExStyle
|= (int)(WindowExStyles
.WS_EX_TOOLWINDOW
);
1146 case FormBorderStyle
.None
: {
1152 switch(window_state
) {
1153 case FormWindowState
.Maximized
: {
1154 cp
.Style
|= (int)WindowStyles
.WS_MAXIMIZE
;
1158 case FormWindowState
.Minimized
: {
1159 cp
.Style
|= (int)WindowStyles
.WS_MINIMIZE
;
1165 cp
.ExStyle
|= (int) WindowExStyles
.WS_EX_TOPMOST
;
1168 if (ShowInTaskbar
) {
1169 cp
.ExStyle
|= (int)WindowExStyles
.WS_EX_APPWINDOW
;
1173 cp
.Style
|= (int)WindowStyles
.WS_MAXIMIZEBOX
;
1177 cp
.Style
|= (int)WindowStyles
.WS_MINIMIZEBOX
;
1181 cp
.Style
|= (int)WindowStyles
.WS_SYSMENU
;
1185 if (!this.show_icon
) {
1186 cp
.ExStyle
|= (int)WindowExStyles
.WS_EX_DLGMODALFRAME
;
1190 if (HelpButton
&& !MaximizeBox
&& !MinimizeBox
) {
1191 cp
.ExStyle
|= (int)WindowExStyles
.WS_EX_CONTEXTHELP
;
1195 cp
.Style
|= (int)WindowStyles
.WS_VISIBLE
;
1197 if (opacity
< 1.0 || TransparencyKey
!= Color
.Empty
) {
1198 cp
.ExStyle
|= (int)WindowExStyles
.WS_EX_LAYERED
;
1201 if (!is_enabled
&& context
== null) {
1202 cp
.Style
|= (int)(WindowStyles
.WS_DISABLED
);
1209 protected override ImeMode DefaultImeMode
{
1211 return ImeMode
.NoControl
;
1215 protected override Size DefaultSize
{
1217 return new Size (300, 300);
1221 protected Rectangle MaximizedBounds
{
1223 if (maximized_bounds
!= Rectangle
.Empty
) {
1224 return maximized_bounds
;
1226 return default_maximized_bounds
;
1230 maximized_bounds
= value;
1231 OnMaximizedBoundsChanged(EventArgs
.Empty
);
1232 if (IsHandleCreated
) {
1233 XplatUI
.SetWindowMinMax(Handle
, maximized_bounds
, minimum_size
, maximum_size
);
1237 #endregion // Protected Instance Properties
1239 #region Public Static Methods
1241 [EditorBrowsable(EditorBrowsableState
.Never
)]
1242 [Obsolete ("This method has been deprecated. Use AutoScaleDimensions instead")]
1244 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1246 public static SizeF
GetAutoScaleSize (Font font
)
1248 return XplatUI
.GetAutoScaleSize(font
);
1251 #endregion // Public Static Methods
1253 #region Public Instance Methods
1254 internal SizeF
GetAutoScaleSize (Graphics g
, Font font
)
1257 // The following constants come from the dotnet mailing list
1258 // discussion: http://discuss.develop.com/archives/wa.exe?A2=ind0203A&L=DOTNET&P=R3655
1260 // The magic number is "Its almost the length
1261 // of the string with a smattering added in
1262 // for compat with earlier code".
1265 string magic_string
= "The quick brown fox jumped over the lazy dog.";
1266 double magic_number
= 44.549996948242189;
1267 float width
= (float) (g
.MeasureString (magic_string
, font
).Width
/ magic_number
);
1269 return new SizeF (width
, font
.Height
);
1272 public void Activate ()
1276 // The docs say activate only activates if our app is already active
1277 if (IsHandleCreated
) {
1279 MdiParent
.ActivateMdiChild (this);
1280 } else if (IsMdiContainer
) {
1281 mdi_container
.SendFocusToActiveChild ();
1283 active
= ActiveForm
;
1284 if ((active
!= null) && (this != active
)) {
1285 XplatUI
.Activate(window
.Handle
);
1291 public void AddOwnedForm(Form ownedForm
) {
1292 if (!owned_forms
.Contains(ownedForm
)) {
1293 owned_forms
.Add(ownedForm
);
1295 ownedForm
.Owner
= this;
1298 public void Close () {
1305 XplatUI
.SendMessage(this.Handle
, Msg
.WM_CLOSE
, IntPtr
.Zero
, IntPtr
.Zero
);
1308 public void LayoutMdi(MdiLayout
value) {
1309 if (mdi_container
!= null) {
1310 mdi_container
.LayoutMdi(value);
1314 public void RemoveOwnedForm(Form ownedForm
) {
1315 owned_forms
.Remove(ownedForm
);
1318 public void SetDesktopBounds(int x
, int y
, int width
, int height
) {
1319 DesktopBounds
= new Rectangle(x
, y
, width
, height
);
1322 public void SetDesktopLocation(int x
, int y
) {
1323 DesktopLocation
= new Point(x
, y
);
1327 public void Show (IWin32Window owner
)
1332 this.Owner
= Control
.FromHandle (owner
.Handle
).TopLevelControl
as Form
;
1335 throw new InvalidOperationException ("The 'owner' cannot be the form being shown.");
1341 public DialogResult
ShowDialog() {
1342 return ShowDialog(this.owner
);
1345 public DialogResult
ShowDialog(IWin32Window ownerWin32
) {
1348 IntPtr capture_window
;
1352 if (ownerWin32
!= null) {
1353 Control c
= Control
.FromHandle (ownerWin32
.Handle
);
1355 owner
= c
.TopLevelControl
as Form
;
1358 if (owner
== this) {
1359 throw new InvalidOperationException("The 'ownerWin32' cannot be the form being shown.");
1363 throw new InvalidOperationException("The form is already displayed as a modal dialog.");
1367 throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
1371 throw new InvalidOperationException("Cannot display a disabled form as modal dialog.");
1374 if (TopLevelControl
!= this) {
1375 throw new InvalidOperationException("Can only display TopLevel forms as modal dialog.");
1379 // Can't do this, will screw us in the modal loop
1380 form_parent_window
.Parent
= this.owner
;
1383 // Release any captures
1384 XplatUI
.GrabInfo(out capture_window
, out confined
, out area
);
1385 if (capture_window
!= IntPtr
.Zero
) {
1386 XplatUI
.UngrabWindow(capture_window
);
1390 // Commented out; we instead let the Visible=true inside the runloop create the control
1391 // otherwise setting DialogResult inside any of the events that are triggered by the
1392 // create will not actually cause the form to not be displayed.
1393 // Leaving this comment here in case there was an actual purpose to creating the control
1395 if (!IsHandleCreated
) {
1400 Application
.RunLoop(true, new ApplicationContext(this));
1402 if (owner
!= null) {
1403 // Cannot use Activate(), it has a check for the current active window...
1404 XplatUI
.Activate(owner
.window
.Handle
);
1407 if (DialogResult
!= DialogResult
.None
) {
1408 return DialogResult
;
1410 DialogResult
= DialogResult
.Cancel
;
1411 return DialogResult
.Cancel
;
1414 public override string ToString() {
1415 return GetType().FullName
.ToString() + ", Text: " + Text
;
1417 #endregion // Public Instance Methods
1419 #region Protected Instance Methods
1420 protected void ActivateMdiChild(Form form
) {
1421 if (!IsMdiContainer
)
1423 mdi_container
.ActivateChild (form
);
1424 OnMdiChildActivate(EventArgs
.Empty
);
1427 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1428 protected override void AdjustFormScrollbars(bool displayScrollbars
) {
1429 base.AdjustFormScrollbars (displayScrollbars
);
1433 [EditorBrowsable(EditorBrowsableState
.Never
)]
1434 [Obsolete ("This method has been deprecated")] // XXX what to use instead?
1436 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1438 protected void ApplyAutoScaling()
1440 SizeF current_size_f
= GetAutoScaleSize (DeviceContext
, Font
);
1441 Size current_size
= new Size ((int) current_size_f
.Width
, (int) current_size_f
.Height
);
1445 if (current_size
== autoscale_base_size
)
1448 if (Environment
.GetEnvironmentVariable ("MONO_MWF_SCALING") == "disable"){
1453 // I tried applying the Fudge height factor from:
1454 // http://blogs.msdn.com/mharsh/archive/2004/01/25/62621.aspx
1455 // but it makes things larger without looking better.
1457 if (current_size_f
.Width
!= AutoScaleBaseSize
.Width
) {
1458 dx
= current_size_f
.Width
/ AutoScaleBaseSize
.Width
+ 0.08f
;
1463 if (current_size_f
.Height
!= AutoScaleBaseSize
.Height
) {
1464 dy
= current_size_f
.Height
/ AutoScaleBaseSize
.Height
+ 0.08f
;
1471 AutoScaleBaseSize
= current_size
;
1474 protected void CenterToParent() {
1482 w
= DefaultSize
.Width
;
1488 h
= DefaultSize
.Height
;
1492 if (Parent
!= null) {
1494 } else if (owner
!= null) {
1498 if (owner
!= null) {
1499 this.Location
= new Point(ctl
.Left
+ ctl
.Width
/ 2 - w
/2, ctl
.Top
+ ctl
.Height
/ 2 - h
/ 2);
1503 protected void CenterToScreen() {
1511 w
= DefaultSize
.Width
;
1517 h
= DefaultSize
.Height
;
1520 XplatUI
.GetDisplaySize(out DisplaySize
);
1521 this.Location
= new Point(DisplaySize
.Width
/ 2 - w
/ 2, DisplaySize
.Height
/ 2 - h
/ 2);
1524 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1525 protected override Control
.ControlCollection
CreateControlsInstance() {
1526 return base.CreateControlsInstance ();
1529 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1530 protected override void CreateHandle() {
1531 base.CreateHandle ();
1533 Application
.AddForm (this);
1537 if ((XplatUI
.SupportsTransparency() & TransparencySupport
.Set
) != 0) {
1538 if (allow_transparency
) {
1539 XplatUI
.SetWindowTransparency(Handle
, Opacity
, TransparencyKey
);
1543 XplatUI
.SetWindowMinMax(window
.Handle
, maximized_bounds
, minimum_size
, maximum_size
);
1544 if ((FormBorderStyle
!= FormBorderStyle
.FixedDialog
) && (icon
!= null)) {
1545 XplatUI
.SetIcon(window
.Handle
, icon
);
1548 if ((owner
!= null) && (owner
.IsHandleCreated
)) {
1549 XplatUI
.SetTopmost(window
.Handle
, owner
.window
.Handle
, true);
1552 for (int i
= 0; i
< owned_forms
.Count
; i
++) {
1553 if (owned_forms
[i
].IsHandleCreated
)
1554 XplatUI
.SetTopmost(owned_forms
[i
].window
.Handle
, window
.Handle
, true);
1557 if (window_manager
!= null) {
1558 if (window_state
!= FormWindowState
.Normal
) {
1559 window_manager
.SetWindowState (FormWindowState
.Normal
, window_state
);
1561 XplatUI
.RequestNCRecalc (window
.Handle
);
1566 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1567 protected override void DefWndProc(ref Message m
) {
1568 base.DefWndProc (ref m
);
1571 protected override void Dispose(bool disposing
)
1573 for (int i
= 0; i
< owned_forms
.Count
; i
++)
1574 ((Form
)owned_forms
[i
]).Owner
= null;
1576 owned_forms
.Clear ();
1578 base.Dispose (disposing
);
1580 Application
.RemoveForm (this);
1583 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1584 protected virtual void OnActivated(EventArgs e
)
1586 EventHandler eh
= (EventHandler
)(Events
[ActivatedEvent
]);
1591 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1592 protected virtual void OnClosed(EventArgs e
) {
1593 EventHandler eh
= (EventHandler
)(Events
[ClosedEvent
]);
1598 // Consider calling FireClosingEvents instead of calling this directly.
1599 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
1600 protected virtual void OnClosing(System
.ComponentModel
.CancelEventArgs e
) {
1601 CancelEventHandler eh
= (CancelEventHandler
)(Events
[ClosingEvent
]);
1606 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1607 protected override void OnCreateControl() {
1608 base.OnCreateControl ();
1611 XplatUI
.SetMenu(window
.Handle
, menu
);
1614 OnLoad(EventArgs
.Empty
);
1616 // Send initial location
1617 OnLocationChanged(EventArgs
.Empty
);
1619 if (IsMdiContainer
) {
1620 mdi_container
.LayoutMdi (MdiLayout
.Cascade
);
1624 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1625 protected virtual void OnDeactivate(EventArgs e
) {
1626 EventHandler eh
= (EventHandler
)(Events
[DeactivateEvent
]);
1631 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1632 protected override void OnFontChanged(EventArgs e
) {
1633 base.OnFontChanged (e
);
1636 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1637 protected override void OnHandleCreated(EventArgs e
) {
1638 XplatUI
.SetBorderStyle(window
.Handle
, form_border_style
);
1639 base.OnHandleCreated (e
);
1642 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1643 protected override void OnHandleDestroyed(EventArgs e
) {
1644 base.OnHandleDestroyed (e
);
1647 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1648 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e
) {
1649 InputLanguageChangedEventHandler eh
= (InputLanguageChangedEventHandler
)(Events
[InputLanguageChangedEvent
]);
1654 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1655 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e
) {
1656 InputLanguageChangingEventHandler eh
= (InputLanguageChangingEventHandler
)(Events
[InputLanguageChangingEvent
]);
1661 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1662 protected virtual void OnLoad(EventArgs e
) {
1664 ApplyAutoScaling ();
1668 EventHandler eh
= (EventHandler
)(Events
[LoadEvent
]);
1673 switch (StartPosition
) {
1674 case FormStartPosition
.CenterScreen
:
1675 this.CenterToScreen();
1677 case FormStartPosition
.CenterParent
:
1678 this.CenterToParent ();
1680 case FormStartPosition
.Manual
:
1681 Left
= CreateParams
.X
;
1682 Top
= CreateParams
.Y
;
1689 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1690 protected virtual void OnMaximizedBoundsChanged(EventArgs e
) {
1691 EventHandler eh
= (EventHandler
)(Events
[MaximizedBoundsChangedEvent
]);
1696 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1697 protected virtual void OnMaximumSizeChanged(EventArgs e
) {
1698 EventHandler eh
= (EventHandler
)(Events
[MaximumSizeChangedEvent
]);
1703 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1704 protected virtual void OnMdiChildActivate(EventArgs e
) {
1705 EventHandler eh
= (EventHandler
)(Events
[MdiChildActivateEvent
]);
1710 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1711 protected virtual void OnMenuComplete(EventArgs e
) {
1712 EventHandler eh
= (EventHandler
)(Events
[MenuCompleteEvent
]);
1717 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1718 protected virtual void OnMenuStart(EventArgs e
) {
1719 EventHandler eh
= (EventHandler
)(Events
[MenuStartEvent
]);
1724 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1725 protected virtual void OnMinimumSizeChanged(EventArgs e
) {
1726 EventHandler eh
= (EventHandler
)(Events
[MinimumSizeChangedEvent
]);
1731 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1732 protected override void OnPaint (PaintEventArgs pevent
) {
1733 base.OnPaint (pevent
);
1736 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1737 protected override void OnResize(EventArgs e
) {
1740 if (this.IsMdiChild
&& ParentForm
!= null) {
1741 ParentForm
.PerformLayout();
1742 ParentForm
.Size
= ParentForm
.Size
;
1746 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1747 protected override void OnStyleChanged(EventArgs e
) {
1748 base.OnStyleChanged (e
);
1751 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1752 protected override void OnTextChanged(EventArgs e
) {
1753 base.OnTextChanged (e
);
1755 if (mdi_container
!= null)
1756 mdi_container
.SetParentText(true);
1759 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1760 protected override void OnVisibleChanged(EventArgs e
) {
1761 base.OnVisibleChanged (e
);
1764 if (window_manager
!= null)
1765 window_manager
.SetWindowState (WindowState
, WindowState
);
1769 protected override bool ProcessCmdKey(ref Message msg
, Keys keyData
) {
1770 if (base.ProcessCmdKey (ref msg
, keyData
)) {
1774 // Give our menu a shot
1775 if (ActiveMenu
!= null) {
1776 return ActiveMenu
.ProcessCmdKey(ref msg
, keyData
);
1782 case Keys
.Control
| Keys
.F4
:
1783 case Keys
.Control
| Keys
.Shift
| Keys
.F4
:
1786 case Keys
.Control
| Keys
.Tab
:
1787 case Keys
.Control
| Keys
.F6
:
1788 MdiParent
.MdiContainer
.ActivateNextChild ();
1790 case Keys
.Control
| Keys
.Shift
| Keys
.Tab
:
1791 case Keys
.Control
| Keys
.Shift
| Keys
.F6
:
1792 MdiParent
.MdiContainer
.ActivatePreviousChild ();
1800 // LAMESPEC - Not documented that Form overrides ProcessDialogChar; class-status showed
1801 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
1802 protected override bool ProcessDialogChar(char charCode
) {
1803 return base.ProcessDialogChar (charCode
);
1806 protected override bool ProcessDialogKey(Keys keyData
) {
1807 if ((keyData
& Keys
.Modifiers
) == 0) {
1808 if (keyData
== Keys
.Enter
) {
1809 IntPtr window
= XplatUI
.GetFocus ();
1810 Control c
= Control
.FromHandle (window
);
1811 if (c
is Button
&& c
.FindForm () == this) {
1812 ((Button
)c
).PerformClick ();
1815 else if (accept_button
!= null) {
1816 accept_button
.PerformClick();
1819 } else if (keyData
== Keys
.Escape
&& cancel_button
!= null) {
1820 cancel_button
.PerformClick();
1824 return base.ProcessDialogKey(keyData
);
1827 protected override bool ProcessKeyPreview(ref Message msg
) {
1829 if (ProcessKeyEventArgs(ref msg
)) {
1833 return base.ProcessKeyPreview (ref msg
);
1836 protected override bool ProcessTabKey(bool forward
) {
1837 return SelectNextControl(ActiveControl
, forward
, true, true, true);
1841 [EditorBrowsable(EditorBrowsableState
.Never
)]
1843 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1845 protected override void ScaleCore(float dx
, float dy
) {
1849 // We can't scale max or min windows
1850 if (WindowState
== FormWindowState
.Normal
) {
1851 // We cannot call base since base also adjusts X/Y, but
1852 // a form is toplevel and doesn't move
1856 if (!GetStyle(ControlStyles
.FixedWidth
)) {
1857 size
.Width
= (int)(size
.Width
* dx
);
1860 if (!GetStyle(ControlStyles
.FixedHeight
)) {
1861 size
.Height
= (int)(size
.Height
* dy
);
1867 /* Now scale our children */
1868 Control
[] controls
= Controls
.GetAllControls ();
1869 for (int i
=0; i
< controls
.Length
; i
++) {
1870 controls
[i
].Scale(dx
, dy
);
1879 protected override void Select(bool directed
, bool forward
) {
1883 base.SelectNextControl(null, forward
, true, true, true);
1886 parent
= this.ParentForm
;
1887 if (parent
!= null) {
1888 parent
.ActiveControl
= this;
1894 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1895 protected override void SetBoundsCore(int x
, int y
, int width
, int height
, BoundsSpecified specified
) {
1896 base.SetBoundsCore (x
, y
, width
, height
, specified
);
1899 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1900 protected override void SetClientSizeCore(int x
, int y
) {
1901 if ((minimum_size
.Width
!= 0) && (x
< minimum_size
.Width
)) {
1902 x
= minimum_size
.Width
;
1903 } else if ((maximum_size
.Width
!= 0) && (x
> maximum_size
.Width
)) {
1904 x
= maximum_size
.Width
;
1907 if ((minimum_size
.Height
!= 0) && (y
< minimum_size
.Height
)) {
1908 y
= minimum_size
.Height
;
1909 } else if ((maximum_size
.Height
!= 0) && (y
> maximum_size
.Height
)) {
1910 y
= maximum_size
.Height
;
1913 Rectangle ClientRect
= new Rectangle(0, 0, x
, y
);
1914 Rectangle WindowRect
;
1915 CreateParams cp
= this.CreateParams
;
1917 clientsize_set
= new Size(x
, y
);
1919 if (XplatUI
.CalculateWindowRect(ref ClientRect
, cp
.Style
, cp
.ExStyle
, cp
.menu
, out WindowRect
)) {
1920 SetBounds(bounds
.X
, bounds
.Y
, WindowRect
.Width
, WindowRect
.Height
, BoundsSpecified
.Size
);
1924 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1925 protected override void SetVisibleCore(bool value) {
1926 is_changing_visible_state
= true;
1927 has_been_visible
= value || has_been_visible
;
1928 base.SetVisibleCore (value);
1929 is_changing_visible_state
= false;
1932 // Shown event is only called once, the first time the form is made visible
1933 if (value && !shown_raised
) {
1934 this.OnShown (EventArgs
.Empty
);
1935 shown_raised
= true;
1940 protected override void UpdateDefaultButton() {
1941 base.UpdateDefaultButton ();
1944 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1945 protected override void WndProc(ref Message m
) {
1947 if (window_manager
!= null && window_manager
.HandleMessage (ref m
)) {
1951 switch((Msg
)m
.Msg
) {
1952 case Msg
.WM_DESTROY
: {
1953 base.WndProc(ref m
);
1954 if (!RecreatingHandle
) {
1955 this.closing
= true;
1960 case Msg
.WM_CLOSE_INTERNAL
: {
1965 case Msg
.WM_CLOSE
: {
1966 Form act
= Form
.ActiveForm
;
1967 if (act
!= null && act
!= this && act
.Modal
== true) {
1971 if (mdi_container
!= null) {
1972 foreach (Form mdi_child
in mdi_container
.MdiChildren
) {
1973 mdi_child
.FireClosingEvents (CloseReason
.MdiFormClosing
);
1978 if (!FireClosingEvents (CloseReason
.UserClosing
)) {
1979 OnClosed (EventArgs
.Empty
);
1981 OnFormClosed (new FormClosedEventArgs (CloseReason
.UserClosing
));
1990 if (FireClosingEvents (CloseReason
.UserClosing
)) {
1991 DialogResult
= DialogResult
.None
;
1995 OnClosed (EventArgs
.Empty
);
1997 OnFormClosed (new FormClosedEventArgs (CloseReason
.UserClosing
));
2007 case Msg
.WM_WINDOWPOSCHANGED
: {
2008 if (WindowState
!= FormWindowState
.Minimized
) {
2009 base.WndProc(ref m
);
2015 case Msg
.WM_SYSCOMMAND
: {
2016 // Let *Strips know the app's title bar was clicked
2017 if (XplatUI
.IsEnabled (Handle
))
2018 ToolStripManager
.FireAppClicked ();
2020 base.WndProc(ref m
);
2025 case Msg
.WM_ACTIVATE
: {
2026 if (m
.WParam
!= (IntPtr
)WindowActiveFlags
.WA_INACTIVE
) {
2028 SelectActiveControl ();
2030 if (ActiveControl
!= null && !ActiveControl
.Focused
)
2031 SendControlFocus (ActiveControl
);
2034 OnActivated(EventArgs
.Empty
);
2036 OnDeactivate(EventArgs
.Empty
);
2041 case Msg
.WM_KILLFOCUS
: {
2042 base.WndProc(ref m
);
2046 case Msg
.WM_SETFOCUS
: {
2047 if (ActiveControl
!= null && ActiveControl
!= this) {
2048 ActiveControl
.Focus();
2049 return; // FIXME - do we need to run base.WndProc, even though we just changed focus?
2051 if (IsMdiContainer
) {
2052 mdi_container
.SendFocusToActiveChild ();
2055 base.WndProc(ref m
);
2060 case Msg
.WM_NCLBUTTONDOWN
: {
2061 if (XplatUI
.IsEnabled (Handle
) && ActiveMenu
!= null) {
2062 ActiveMenu
.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m
.WParam
.ToInt32()), mouse_clicks
, Control
.MousePosition
.X
, Control
.MousePosition
.Y
, 0));
2065 if (ActiveMaximizedMdiChild
!= null) {
2066 if (ActiveMaximizedMdiChild
.HandleMenuMouseDown (ActiveMenu
,
2067 LowOrder ((int) m
.LParam
.ToInt32 ()),
2068 HighOrder ((int) m
.LParam
.ToInt32 ()))) {
2069 // Don't let base process this message, otherwise we won't
2070 // get a WM_NCLBUTTONUP.
2074 base.WndProc(ref m
);
2077 case Msg
.WM_NCLBUTTONUP
: {
2078 if (ActiveMaximizedMdiChild
!= null) {
2079 ActiveMaximizedMdiChild
.HandleMenuMouseUp (ActiveMenu
,
2080 LowOrder ((int)m
.LParam
.ToInt32 ()),
2081 HighOrder ((int)m
.LParam
.ToInt32 ()));
2083 base.WndProc (ref m
);
2087 case Msg
.WM_NCMOUSELEAVE
: {
2088 if (ActiveMaximizedMdiChild
!= null) {
2089 ActiveMaximizedMdiChild
.HandleMenuMouseLeave(ActiveMenu
,
2090 LowOrder((int)m
.LParam
.ToInt32()),
2091 HighOrder((int)m
.LParam
.ToInt32()));
2093 base.WndProc(ref m
);
2097 case Msg
.WM_NCMOUSEMOVE
: {
2098 if (XplatUI
.IsEnabled (Handle
) && ActiveMenu
!= null) {
2099 ActiveMenu
.OnMouseMove(this, new MouseEventArgs (FromParamToMouseButtons ((int) m
.WParam
.ToInt32()), mouse_clicks
, LowOrder ((int) m
.LParam
.ToInt32 ()), HighOrder ((int) m
.LParam
.ToInt32 ()), 0));
2102 if (ActiveMaximizedMdiChild
!= null) {
2103 XplatUI
.RequestAdditionalWM_NCMessages (Handle
, false, true);
2104 ActiveMaximizedMdiChild
.HandleMenuMouseMove (ActiveMenu
,
2105 LowOrder ((int)m
.LParam
.ToInt32 ()),
2106 HighOrder ((int)m
.LParam
.ToInt32 ()));
2108 base.WndProc(ref m
);
2112 case Msg
.WM_NCPAINT
: {
2113 if (ActiveMenu
!= null) {
2117 pe
= XplatUI
.PaintEventStart(Handle
, false);
2118 pnt
= XplatUI
.GetMenuOrigin(window
.Handle
);
2120 // The entire menu has to be in the clip rectangle because the
2121 // control buttons are right-aligned and otherwise they would
2122 // stay painted when the window gets resized.
2123 Rectangle clip
= new Rectangle (pnt
.X
, pnt
.Y
, ClientSize
.Width
, 0);
2124 clip
= Rectangle
.Union(clip
, pe
.ClipRectangle
);
2126 pe
.Graphics
.SetClip(clip
);
2128 ActiveMenu
.Draw (pe
, new Rectangle (pnt
.X
, pnt
.Y
, ClientSize
.Width
, 0));
2130 if (ActiveMaximizedMdiChild
!= null) {
2131 ActiveMaximizedMdiChild
.DrawMaximizedButtons (ActiveMenu
, pe
);
2134 XplatUI
.PaintEventEnd(Handle
, false);
2137 base.WndProc(ref m
);
2141 case Msg
.WM_NCCALCSIZE
: {
2142 XplatUIWin32
.NCCALCSIZE_PARAMS ncp
;
2144 if ((ActiveMenu
!= null) && (m
.WParam
== (IntPtr
)1)) {
2145 ncp
= (XplatUIWin32
.NCCALCSIZE_PARAMS
)Marshal
.PtrToStructure(m
.LParam
, typeof(XplatUIWin32
.NCCALCSIZE_PARAMS
));
2148 ncp
.rgrc1
.top
+= ThemeEngine
.Current
.CalcMenuBarSize (DeviceContext
, ActiveMenu
, ClientSize
.Width
);
2149 Marshal
.StructureToPtr(ncp
, m
.LParam
, true);
2155 case Msg
.WM_MOUSEMOVE
: {
2156 if (XplatUI
.IsEnabled (Handle
) && active_tracker
!= null) {
2157 MouseEventArgs args
;
2159 args
= new MouseEventArgs (FromParamToMouseButtons ((int) m
.WParam
.ToInt32()),
2160 mouse_clicks
, LowOrder ((int) m
.LParam
.ToInt32 ()), HighOrder ((int) m
.LParam
.ToInt32 ()), 0);
2161 active_tracker
.OnMotion(new MouseEventArgs (args
.Button
, args
.Clicks
, Control
.MousePosition
.X
, Control
.MousePosition
.Y
, args
.Delta
));
2164 base.WndProc(ref m
);
2168 case Msg
.WM_LBUTTONDOWN
:
2169 case Msg
.WM_MBUTTONDOWN
:
2170 case Msg
.WM_RBUTTONDOWN
: {
2171 if (XplatUI
.IsEnabled (Handle
) && active_tracker
!= null) {
2172 MouseEventArgs args
;
2174 args
= new MouseEventArgs (FromParamToMouseButtons ((int) m
.WParam
.ToInt32()),
2175 mouse_clicks
, LowOrder ((int) m
.LParam
.ToInt32 ()), HighOrder ((int) m
.LParam
.ToInt32 ()), 0);
2176 active_tracker
.OnMouseDown(new MouseEventArgs (args
.Button
, args
.Clicks
, Control
.MousePosition
.X
, Control
.MousePosition
.Y
, args
.Delta
));
2179 base.WndProc(ref m
);
2183 case Msg
.WM_LBUTTONUP
:
2184 case Msg
.WM_MBUTTONUP
:
2185 case Msg
.WM_RBUTTONUP
: {
2186 if (XplatUI
.IsEnabled (Handle
) && active_tracker
!= null) {
2187 MouseEventArgs args
;
2188 MouseButtons mb
= FromParamToMouseButtons ((int) m
.WParam
.ToInt32());
2190 // We add in the button that was released (not sent in WParam)
2191 switch((Msg
)m
.Msg
) {
2192 case Msg
.WM_LBUTTONUP
:
2193 mb
|= MouseButtons
.Left
;
2195 case Msg
.WM_MBUTTONUP
:
2196 mb
|= MouseButtons
.Middle
;
2198 case Msg
.WM_RBUTTONUP
:
2199 mb
|= MouseButtons
.Right
;
2203 args
= new MouseEventArgs (mb
, mouse_clicks
, LowOrder ((int) m
.LParam
.ToInt32 ()), HighOrder ((int) m
.LParam
.ToInt32 ()), 0);
2204 active_tracker
.OnMouseUp(new MouseEventArgs (args
.Button
, args
.Clicks
, Control
.MousePosition
.X
, Control
.MousePosition
.Y
, args
.Delta
));
2208 base.WndProc(ref m
);
2212 case Msg
.WM_GETMINMAXINFO
: {
2215 if (m
.LParam
!= IntPtr
.Zero
) {
2216 mmi
= (MINMAXINFO
)Marshal
.PtrToStructure(m
.LParam
, typeof(MINMAXINFO
));
2218 default_maximized_bounds
= new Rectangle(mmi
.ptMaxPosition
.x
, mmi
.ptMaxPosition
.y
, mmi
.ptMaxSize
.x
, mmi
.ptMaxSize
.y
);
2219 if (maximized_bounds
!= Rectangle
.Empty
) {
2220 mmi
.ptMaxPosition
.x
= maximized_bounds
.Left
;
2221 mmi
.ptMaxPosition
.y
= maximized_bounds
.Top
;
2222 mmi
.ptMaxSize
.x
= maximized_bounds
.Width
;
2223 mmi
.ptMaxSize
.y
= maximized_bounds
.Height
;
2226 if (minimum_size
!= Size
.Empty
) {
2227 mmi
.ptMinTrackSize
.x
= minimum_size
.Width
;
2228 mmi
.ptMinTrackSize
.y
= minimum_size
.Height
;
2231 if (maximum_size
!= Size
.Empty
) {
2232 mmi
.ptMaxTrackSize
.x
= maximum_size
.Width
;
2233 mmi
.ptMaxTrackSize
.y
= maximum_size
.Height
;
2235 Marshal
.StructureToPtr(mmi
, m
.LParam
, false);
2241 case Msg
.WM_MOUSEACTIVATE
: {
2242 // Let *Strips know the form or another control has been clicked
2243 if (XplatUI
.IsEnabled (Handle
))
2244 ToolStripManager
.FireAppClicked ();
2246 base.WndProc (ref m
);
2250 case Msg
.WM_ACTIVATEAPP
: {
2251 // Let *Strips know the app lost focus
2252 if (m
.WParam
== (IntPtr
)0)
2253 if (XplatUI
.IsEnabled (Handle
))
2254 ToolStripManager
.FireAppFocusChanged (this);
2256 base.WndProc (ref m
);
2262 base.WndProc (ref m
);
2267 #endregion // Protected Instance Methods
2269 internal override void FireEnter ()
2271 // do nothing - forms don't generate OnEnter
2274 internal override void FireLeave ()
2276 // do nothing - forms don't generate OnLeave
2279 internal void RemoveWindowManager ()
2281 window_manager
= null;
2284 internal override void CheckAcceptButton()
2286 if (accept_button
!= null) {
2287 Button a_button
= accept_button
as Button
;
2289 if (ActiveControl
== a_button
)
2292 if (ActiveControl
is Button
) {
2293 a_button
.paint_as_acceptbutton
= false;
2297 a_button
.paint_as_acceptbutton
= true;
2304 static object ActivatedEvent
= new object ();
2305 static object ClosedEvent
= new object ();
2306 static object ClosingEvent
= new object ();
2307 static object DeactivateEvent
= new object ();
2308 static object InputLanguageChangedEvent
= new object ();
2309 static object InputLanguageChangingEvent
= new object ();
2310 static object LoadEvent
= new object ();
2311 static object MaximizedBoundsChangedEvent
= new object ();
2312 static object MaximumSizeChangedEvent
= new object ();
2313 static object MdiChildActivateEvent
= new object ();
2314 static object MenuCompleteEvent
= new object ();
2315 static object MenuStartEvent
= new object ();
2316 static object MinimumSizeChangedEvent
= new object ();
2318 public event EventHandler Activated
{
2319 add { Events.AddHandler (ActivatedEvent, value); }
2320 remove { Events.RemoveHandler (ActivatedEvent, value); }
2325 [EditorBrowsable (EditorBrowsableState
.Never
)]
2327 public event EventHandler Closed
{
2328 add { Events.AddHandler (ClosedEvent, value); }
2329 remove { Events.RemoveHandler (ClosedEvent, value); }
2334 [EditorBrowsable (EditorBrowsableState
.Never
)]
2336 public event CancelEventHandler Closing
{
2337 add { Events.AddHandler (ClosingEvent, value); }
2338 remove { Events.RemoveHandler (ClosingEvent, value); }
2341 public event EventHandler Deactivate
{
2342 add { Events.AddHandler (DeactivateEvent, value); }
2343 remove { Events.RemoveHandler (DeactivateEvent, value); }
2346 public event InputLanguageChangedEventHandler InputLanguageChanged
{
2347 add { Events.AddHandler (InputLanguageChangedEvent, value); }
2348 remove { Events.RemoveHandler (InputLanguageChangedEvent, value); }
2351 public event InputLanguageChangingEventHandler InputLanguageChanging
{
2352 add { Events.AddHandler (InputLanguageChangingEvent, value); }
2353 remove { Events.RemoveHandler (InputLanguageChangingEvent, value); }
2356 public event EventHandler Load
{
2357 add { Events.AddHandler (LoadEvent, value); }
2358 remove { Events.RemoveHandler (LoadEvent, value); }
2361 public event EventHandler MaximizedBoundsChanged
{
2362 add { Events.AddHandler (MaximizedBoundsChangedEvent, value); }
2363 remove { Events.RemoveHandler (MaximizedBoundsChangedEvent, value); }
2366 public event EventHandler MaximumSizeChanged
{
2367 add { Events.AddHandler (MaximumSizeChangedEvent, value); }
2368 remove { Events.RemoveHandler (MaximumSizeChangedEvent, value); }
2371 public event EventHandler MdiChildActivate
{
2372 add { Events.AddHandler (MdiChildActivateEvent, value); }
2373 remove { Events.RemoveHandler (MdiChildActivateEvent, value); }
2379 public event EventHandler MenuComplete
{
2380 add { Events.AddHandler (MenuCompleteEvent, value); }
2381 remove { Events.RemoveHandler (MenuCompleteEvent, value); }
2387 public event EventHandler MenuStart
{
2388 add { Events.AddHandler (MenuStartEvent, value); }
2389 remove { Events.RemoveHandler (MenuStartEvent, value); }
2392 public event EventHandler MinimumSizeChanged
{
2393 add { Events.AddHandler (MinimumSizeChangedEvent, value); }
2394 remove { Events.RemoveHandler (MinimumSizeChangedEvent, value); }
2399 [EditorBrowsable(EditorBrowsableState
.Never
)]
2400 public new event EventHandler TabIndexChanged
{
2401 add { base.TabIndexChanged += value; }
2402 remove { base.TabIndexChanged -= value; }
2404 #endregion // Events
2407 [SettingsBindable (true)]
2408 public override string Text
{
2418 [SettingsBindable (true)]
2419 public new Point Location
{
2421 return base.Location
;
2425 base.Location
= value;
2429 static object AutoValidateChangedEvent
= new object ();
2430 static object FormClosingEvent
= new object ();
2431 static object FormClosedEvent
= new object ();
2432 static object HelpButtonClickedEvent
= new object ();
2433 static object ResizeEndEvent
= new object ();
2434 static object ResizeBeginEvent
= new object ();
2435 static object RightToLeftLayoutChangedEvent
= new object ();
2436 static object ShownEvent
= new object ();
2439 [EditorBrowsable (EditorBrowsableState
.Always
)]
2440 public new event EventHandler AutoSizeChanged
{
2441 add { base.AutoSizeChanged += value; }
2442 remove { base.AutoSizeChanged -= value; }
2446 [EditorBrowsable (EditorBrowsableState
.Always
)]
2447 public event EventHandler AutoValidateChanged
{
2448 add { Events.AddHandler (AutoValidateChangedEvent, value); }
2449 remove { Events.RemoveHandler (AutoValidateChangedEvent, value); }
2452 public event FormClosingEventHandler FormClosing
{
2453 add { Events.AddHandler (FormClosingEvent, value); }
2454 remove { Events.RemoveHandler (FormClosingEvent, value); }
2457 public event FormClosedEventHandler FormClosed
{
2458 add { Events.AddHandler (FormClosedEvent, value); }
2459 remove { Events.RemoveHandler (FormClosedEvent, value); }
2463 [EditorBrowsable (EditorBrowsableState
.Always
)]
2464 public event CancelEventHandler HelpButtonClicked
{
2465 add { Events.AddHandler (HelpButtonClickedEvent, value); }
2466 remove { Events.RemoveHandler (HelpButtonClickedEvent, value); }
2470 [EditorBrowsable (EditorBrowsableState
.Never
)]
2471 public new event EventHandler MarginChanged
{
2472 add { base.MarginChanged += value; }
2473 remove { base.MarginChanged -= value; }
2476 public event EventHandler RightToLeftLayoutChanged
{
2477 add { Events.AddHandler (RightToLeftLayoutChangedEvent, value); }
2478 remove { Events.RemoveHandler (RightToLeftLayoutChangedEvent, value); }
2481 public event EventHandler ResizeBegin
{
2482 add { Events.AddHandler (ResizeBeginEvent, value); }
2483 remove { Events.RemoveHandler (ResizeBeginEvent, value); }
2486 public event EventHandler ResizeEnd
{
2487 add { Events.AddHandler (ResizeEndEvent, value); }
2488 remove { Events.RemoveHandler (ResizeEndEvent, value); }
2491 public event EventHandler Shown
{
2492 add { Events.AddHandler (ShownEvent, value); }
2493 remove { Events.RemoveHandler (ShownEvent, value); }
2497 [EditorBrowsable (EditorBrowsableState
.Never
)]
2498 public new event EventHandler TabStopChanged
{
2499 add { base.TabStopChanged += value; }
2500 remove { base.TabStopChanged -= value; }
2504 [EditorBrowsable (EditorBrowsableState
.Always
)]
2505 protected override void OnBackgroundImageChanged (EventArgs e
)
2507 base.OnBackgroundImageChanged (e
);
2510 [EditorBrowsable (EditorBrowsableState
.Always
)]
2511 protected override void OnBackgroundImageLayoutChanged (EventArgs e
)
2513 base.OnBackgroundImageLayoutChanged (e
);
2516 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2517 protected override void OnEnabledChanged (EventArgs e
)
2519 base.OnEnabledChanged (e
);
2522 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2523 protected override void OnEnter (EventArgs e
)
2528 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2529 protected virtual void OnFormClosed (FormClosedEventArgs e
) {
2530 FormClosedEventHandler eh
= (FormClosedEventHandler
)(Events
[FormClosedEvent
]);
2535 // Consider calling FireClosingEvents instead of calling this directly.
2536 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2537 protected virtual void OnFormClosing (FormClosingEventArgs e
)
2539 FormClosingEventHandler eh
= (FormClosingEventHandler
)(Events
[FormClosingEvent
]);
2544 [MonoTODO ("Not hooked up to event")]
2545 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2546 protected virtual void OnHelpButtonClicked (CancelEventArgs e
)
2548 CancelEventHandler eh
= (CancelEventHandler
)(Events
[HelpButtonClickedEvent
]);
2553 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2554 protected override void OnLayout (LayoutEventArgs levent
)
2556 base.OnLayout (levent
);
2559 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2560 protected virtual void OnResizeBegin (EventArgs e
)
2562 EventHandler eh
= (EventHandler
) (Events
[ResizeBeginEvent
]);
2567 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2568 protected virtual void OnResizeEnd (EventArgs e
)
2570 EventHandler eh
= (EventHandler
) (Events
[ResizeEndEvent
]);
2575 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2576 protected virtual void OnShown (EventArgs e
)
2578 EventHandler eh
= (EventHandler
) (Events
[ShownEvent
]);