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
;
93 #endregion // Local Variables
95 #region Private & Internal Methods
98 default_icon
= Locale
.GetResource("mono.ico") as Icon
;
101 // warning: this is only hooked up when an mdi container is created.
102 private void ControlAddedHandler (object sender
, ControlEventArgs e
)
104 if (mdi_container
!= null) {
105 mdi_container
.SendToBack ();
109 // Convenience method for fire BOTH OnClosing and OnFormClosing events
110 // Returns the value of Cancel, so true means the Close was cancelled,
111 // and you shouldn't close the form.
112 internal bool FireClosingEvents (CloseReason reason
)
114 CancelEventArgs cea
= new CancelEventArgs ();
115 this.OnClosing (cea
);
118 FormClosingEventArgs fcea
= new FormClosingEventArgs (reason
, cea
.Cancel
);
119 this.OnFormClosing (fcea
);
126 private void SelectActiveControl ()
128 if (this.IsMdiContainer
) {
129 SendControlFocus (this.mdi_container
);
133 if (this.ActiveControl
== null) {
136 // This visible hack is to work around CanSelect always being false if one of the parents
137 // is not visible; and we by default create Form invisible...
138 visible
= this.is_visible
;
139 this.is_visible
= true;
141 if (SelectNextControl (this, true, true, true, true) == false) {
145 this.is_visible
= visible
;
147 Select (ActiveControl
);
150 #endregion // Private & Internal Methods
152 #region Public Classes
153 public new class ControlCollection
: Control
.ControlCollection
{
156 public ControlCollection(Form owner
) : base(owner
) {
157 this.form_owner
= owner
;
160 public override void Add(Control
value) {
161 if (Contains (value))
164 ((Form
)value).owner
=form_owner
;
167 public override void Remove(Control
value) {
168 ((Form
)value).owner
= null;
172 #endregion // Public Classes
174 #region Public Constructor & Destructor
177 SizeF current_scale
= GetAutoScaleSize (DeviceContext
, Font
);
180 autoscale_base_size
= new Size ((int)current_scale
.Width
, (int) current_scale
.Height
);
181 allow_transparency
= false;
184 dialog_result
= DialogResult
.None
;
185 start_position
= FormStartPosition
.WindowsDefaultLocation
;
186 form_border_style
= FormBorderStyle
.Sizable
;
187 window_state
= FormWindowState
.Normal
;
192 minimum_size
= Size
.Empty
;
193 maximum_size
= Size
.Empty
;
194 clientsize_set
= Size
.Empty
;
199 show_in_taskbar
= true;
202 size_grip_style
= SizeGripStyle
.Auto
;
203 maximized_bounds
= Rectangle
.Empty
;
204 default_maximized_bounds
= Rectangle
.Empty
;
205 owned_forms
= new Form
.ControlCollection(this);
206 transparency_key
= Color
.Empty
;
208 // FIXME: this should disappear just as soon as the handle creation is done in the right place (here is too soon()
212 #endregion // Public Constructor & Destructor
214 #region Public Static Properties
216 public static Form ActiveForm
{
220 active
= FromHandle(XplatUI
.GetActive());
222 if (active
!= null) {
223 if ( !(active
is Form
)) {
226 parent
= active
.Parent
;
227 while (parent
!= null) {
228 if (parent
is Form
) {
231 parent
= parent
.Parent
;
241 #endregion // Public Static Properties
243 #region Public Instance Properties
245 public IButtonControl AcceptButton
{
247 return accept_button
;
251 accept_button
= value;
257 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
258 public bool AllowTransparency
{
260 return allow_transparency
;
264 if (value == allow_transparency
) {
268 allow_transparency
= value;
271 if (IsHandleCreated
) {
272 if ((XplatUI
.SupportsTransparency() & TransparencySupport
.Set
) != 0) {
273 XplatUI
.SetWindowTransparency(Handle
, Opacity
, TransparencyKey
);
276 UpdateStyles(); // Remove the WS_EX_LAYERED style
284 [EditorBrowsable (EditorBrowsableState
.Never
)]
285 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
286 [Obsolete ("This property has been deprecated in favor of AutoScaleMode.")]
290 [MWFCategory("Layout")]
291 public bool AutoScale
{
302 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
303 [EditorBrowsable(EditorBrowsableState
.Never
)]
305 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
309 public virtual Size AutoScaleBaseSize
{
311 return autoscale_base_size
;
315 autoscale_base_size
= value;
320 public override bool AutoScroll
{
322 return base.AutoScroll
;
325 base.AutoScroll
= value;
329 public override Color BackColor
{
331 /* we don't let parents override our
332 default background color for forms.
333 this fixes the default color for mdi
335 if (background_color
.IsEmpty
)
336 return DefaultBackColor
;
338 return background_color
;
341 base.BackColor
= value;
346 public IButtonControl CancelButton
{
348 return cancel_button
;
352 cancel_button
= value;
353 if (cancel_button
!= null && cancel_button
.DialogResult
== DialogResult
.None
)
354 cancel_button
.DialogResult
= DialogResult
.Cancel
;
358 // new property so we can change the DesignerSerializationVisibility
359 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Visible
)]
361 public new Size ClientSize
{
362 get { return base.ClientSize; }
363 set { base.ClientSize = value; }
367 [MWFCategory("Window Style")]
368 public bool ControlBox
{
374 if (control_box
!= value) {
382 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
383 public Rectangle DesktopBounds
{
385 return new Rectangle(Location
, Size
);
394 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
395 public Point DesktopLocation
{
406 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
407 public DialogResult DialogResult
{
409 return dialog_result
;
413 if (value < DialogResult
.None
|| value > DialogResult
.No
)
414 throw new InvalidEnumArgumentException ("value", (int) value,
415 typeof (DialogResult
));
417 dialog_result
= value;
418 closing
= (dialog_result
!= DialogResult
.None
&& is_modal
);
422 [DefaultValue(FormBorderStyle
.Sizable
)]
424 [MWFCategory("Appearance")]
425 public FormBorderStyle FormBorderStyle
{
427 return form_border_style
;
430 form_border_style
= value;
432 if (window_manager
== null) {
433 if (IsHandleCreated
) {
434 XplatUI
.SetBorderStyle(window
.Handle
, form_border_style
);
437 window_manager
.UpdateBorderStyle (value);
444 [DefaultValue(false)]
445 [MWFCategory("Window Style")]
446 public bool HelpButton
{
452 if (help_button
!= value) {
461 [MWFCategory("Window Style")]
471 if (IsHandleCreated
) {
472 XplatUI
.SetIcon(Handle
, icon
== null ? default_icon
: icon
);
479 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
480 public bool IsMdiChild
{
482 return mdi_parent
!= null;
486 [DefaultValue(false)]
487 [MWFCategory("Window Style")]
488 public bool IsMdiContainer
{
490 return mdi_container
!= null;
494 if (value && mdi_container
== null) {
495 mdi_container
= new MdiClient ();
496 Controls
.Add(mdi_container
);
497 ControlAdded
+= new ControlEventHandler (ControlAddedHandler
);
498 mdi_container
.SendToBack ();
499 mdi_container
.SetParentText (true);
500 } else if (!value && mdi_container
!= null) {
501 Controls
.Remove(mdi_container
);
502 mdi_container
= null;
508 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
509 public Form ActiveMdiChild
{
513 return (Form
) mdi_container
.ActiveMdiChild
;
518 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
519 public bool IsRestrictedWindow
{
525 [DefaultValue(false)]
526 public bool KeyPreview
{
537 [DefaultValue (null)]
538 [TypeConverter (typeof (ReferenceConverter
))]
539 public MenuStrip MainMenuStrip
{
540 get { return this.main_menu_strip; }
542 if (this.main_menu_strip
!= value) {
543 this.main_menu_strip
= value;
544 this.main_menu_strip
.RefreshMdiItems ();
551 [MWFCategory("Window Style")]
552 public bool MaximizeBox
{
557 if (maximize_box
!= value) {
558 maximize_box
= value;
559 if (IsHandleCreated
) {
567 [DefaultValue("{Width=0, Height=0}")]
569 [RefreshProperties(RefreshProperties
.Repaint
)]
570 [MWFCategory("Layout")]
581 if (maximum_size
!= value) {
582 maximum_size
= value;
583 OnMaximumSizeChanged(EventArgs
.Empty
);
584 if (IsHandleCreated
) {
585 XplatUI
.SetWindowMinMax(Handle
, maximized_bounds
, minimum_size
, maximum_size
);
592 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
593 public Form
[] MdiChildren
{
595 if (mdi_container
!= null)
596 return mdi_container
.MdiChildren
;
603 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
604 public Form MdiParent
{
610 if (value != null && !value.IsMdiContainer
)
611 throw new ArgumentException ();
613 if (mdi_parent
!= null) {
614 mdi_parent
.MdiContainer
.Controls
.Remove (this);
619 window_manager
= new MdiWindowManager (this,
620 mdi_parent
.MdiContainer
);
621 mdi_parent
.MdiContainer
.Controls
.Add (this);
622 mdi_parent
.MdiContainer
.Controls
.SetChildIndex (this, 0);
626 } else if (mdi_parent
!= null) {
629 // Create a new window manager
630 window_manager
= null;
631 FormBorderStyle
= form_border_style
;
638 internal MenuTracker ActiveTracker
{
639 get { return active_tracker; }
641 if (value == active_tracker
)
644 Capture
= value != null;
645 active_tracker
= value;
649 internal MdiClient MdiContainer
{
650 get { return mdi_container; }
653 internal InternalWindowManager WindowManager
{
654 get { return window_manager; }
659 [TypeConverter (typeof (ReferenceConverter
))]
662 [MWFCategory("Window Style")]
663 public MainMenu Menu
{
672 if (menu
!= null && !IsMdiChild
) {
675 if (IsHandleCreated
) {
676 XplatUI
.SetMenu (window
.Handle
, menu
);
679 if (clientsize_set
!= Size
.Empty
) {
680 SetClientSizeCore(clientsize_set
.Width
, clientsize_set
.Height
);
682 UpdateBounds (bounds
.X
, bounds
.Y
, bounds
.Width
, bounds
.Height
, ClientSize
.Width
, ClientSize
.Height
-
683 ThemeEngine
.Current
.CalcMenuBarSize (DeviceContext
, menu
, ClientSize
.Width
));
692 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
693 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
694 public MainMenu MergedMenu
{
696 if (!IsMdiChild
|| window_manager
== null)
698 return ((MdiWindowManager
) window_manager
).MergedMenu
;
702 // This is the menu in display and being used because of merging this can
703 // be different then the menu that is actually assosciated with the form
704 internal MainMenu ActiveMenu
{
709 if (IsMdiContainer
&& mdi_container
.Controls
.Count
> 0 &&
710 ((Form
) mdi_container
.Controls
[0]).WindowState
== FormWindowState
.Maximized
) {
711 MdiWindowManager wm
= (MdiWindowManager
) ((Form
) mdi_container
.Controls
[0]).WindowManager
;
712 return wm
.MaximizedMenu
;
715 Form amc
= ActiveMdiChild
;
716 if (amc
== null || amc
.Menu
== null)
718 return amc
.MergedMenu
;
722 internal MdiWindowManager ActiveMaximizedMdiChild
{
724 Form child
= ActiveMdiChild
;
727 if (child
.WindowManager
== null || child
.window_state
!= FormWindowState
.Maximized
)
729 return (MdiWindowManager
) child
.WindowManager
;
734 [MWFCategory("Window Style")]
735 public bool MinimizeBox
{
740 if (minimize_box
!= value) {
741 minimize_box
= value;
742 if (IsHandleCreated
) {
751 [DefaultValue("{Width=0, Height=0}")]
754 [RefreshProperties(RefreshProperties
.Repaint
)]
755 [MWFCategory("Layout")]
766 if (minimum_size
!= value) {
767 minimum_size
= value;
769 if ((Size
.Width
< value.Width
) || (Size
.Height
< value.Height
)) {
770 Size
= new Size(Math
.Max(Size
.Width
, value.Width
), Math
.Max(Size
.Height
, value.Height
));
774 OnMinimumSizeChanged(EventArgs
.Empty
);
775 if (IsHandleCreated
) {
776 XplatUI
.SetWindowMinMax(Handle
, maximized_bounds
, minimum_size
, maximum_size
);
783 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
791 [TypeConverter(typeof(OpacityConverter
))]
792 [MWFCategory("Window Style")]
793 public double Opacity
{
795 if (IsHandleCreated
) {
796 if ((XplatUI
.SupportsTransparency () & TransparencySupport
.Get
) != 0)
797 return XplatUI
.GetWindowTransparency (Handle
);
806 AllowTransparency
= true;
808 if (IsHandleCreated
) {
810 if ((XplatUI
.SupportsTransparency () & TransparencySupport
.Set
) != 0)
811 XplatUI
.SetWindowTransparency(Handle
, opacity
, TransparencyKey
);
818 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
819 public Form
[] OwnedForms
{
823 form_list
= new Form
[owned_forms
.Count
];
825 for (int i
=0; i
<owned_forms
.Count
; i
++) {
826 form_list
[i
] = (Form
)owned_forms
[i
];
834 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
841 if (owner
!= value) {
843 owner
.RemoveOwnedForm(this);
847 owner
.AddOwnedForm(this);
848 if (IsHandleCreated
) {
849 if (owner
!= null && owner
.IsHandleCreated
) {
850 XplatUI
.SetTopmost(this.window
.Handle
, owner
.window
.Handle
, true);
852 XplatUI
.SetTopmost(this.window
.Handle
, IntPtr
.Zero
, false);
860 [MWFCategory("Window Style")]
861 public bool ShowInTaskbar
{
863 return show_in_taskbar
;
866 if (show_in_taskbar
!= value) {
867 show_in_taskbar
= value;
868 if (IsHandleCreated
) {
876 // new property so we can set the DesignerSerializationVisibility
877 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
879 public new Size Size
{
880 get { return base.Size; }
881 set { base.Size = value; }
884 [MonoTODO("Trigger something when GripStyle is set")]
885 [DefaultValue(SizeGripStyle
.Auto
)]
886 [MWFCategory("Window Style")]
887 public SizeGripStyle SizeGripStyle
{
889 return size_grip_style
;
893 size_grip_style
= value;
897 [DefaultValue(FormStartPosition
.WindowsDefaultLocation
)]
899 [MWFCategory("Layout")]
900 public FormStartPosition StartPosition
{
902 return start_position
;
906 if (start_position
== FormStartPosition
.WindowsDefaultLocation
) { // Only do this if it's not set yet
907 start_position
= value;
908 if (IsHandleCreated
) {
909 switch(start_position
) {
910 case FormStartPosition
.CenterParent
: {
915 case FormStartPosition
.CenterScreen
: {
920 case FormStartPosition
.Manual
: {
921 Left
= CreateParams
.X
;
922 Top
= CreateParams
.Y
;
935 // new property so we can set EditorBrowsable to never
937 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
938 [EditorBrowsable(EditorBrowsableState
.Never
)]
939 public new int TabIndex
{
940 get { return base.TabIndex; }
941 set { base.TabIndex = value; }
945 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
946 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
947 public bool TopLevel
{
949 return GetTopLevel();
953 if (!value && IsMdiContainer
)
954 throw new ArgumentException ("MDI Container forms must be top level.");
959 [DefaultValue(false)]
960 [MWFCategory("Window Style")]
961 public bool TopMost
{
967 if (topmost
!= value) {
970 XplatUI
.SetTopmost(window
.Handle
, owner
!= null ? owner
.window
.Handle
: IntPtr
.Zero
, value);
975 [MWFCategory("Window Style")]
976 public Color TransparencyKey
{
978 return transparency_key
;
982 transparency_key
= value;
984 AllowTransparency
= true;
986 if ((XplatUI
.SupportsTransparency () & TransparencySupport
.Set
) != 0)
987 XplatUI
.SetWindowTransparency(Handle
, Opacity
, transparency_key
);
991 [DefaultValue(FormWindowState
.Normal
)]
992 [MWFCategory("Layout")]
993 public FormWindowState WindowState
{
995 if (IsHandleCreated
) {
997 if (window_manager
!= null)
998 return window_manager
.GetWindowState ();
1000 FormWindowState new_state
= XplatUI
.GetWindowState(Handle
);
1001 if (new_state
!= (FormWindowState
)(-1))
1002 window_state
= new_state
;
1005 return window_state
;
1009 FormWindowState old_state
= window_state
;
1010 window_state
= value;
1011 if (IsHandleCreated
) {
1013 if (window_manager
!= null) {
1014 window_manager
.SetWindowState (old_state
, value);
1018 XplatUI
.SetWindowState(Handle
, value);
1023 #endregion // Public Instance Properties
1025 #region Protected Instance Properties
1026 protected override CreateParams CreateParams
{
1028 CreateParams cp
= new CreateParams ();
1031 cp
.ClassName
= XplatUI
.DefaultClassName
;
1036 cp
.Parent
= IntPtr
.Zero
;
1037 cp
.menu
= ActiveMenu
;
1039 if (start_position
== FormStartPosition
.WindowsDefaultLocation
&& !IsMdiChild
) {
1040 cp
.X
= unchecked((int)0x80000000);
1041 cp
.Y
= unchecked((int)0x80000000);
1049 cp
.Style
= (int)(WindowStyles
.WS_CLIPCHILDREN
| WindowStyles
.WS_CLIPSIBLINGS
);
1052 cp
.Style
|= (int)(WindowStyles
.WS_CHILD
| WindowStyles
.WS_CAPTION
);
1053 if (Parent
!= null) {
1054 cp
.Parent
= Parent
.Handle
;
1057 cp
.ExStyle
|= (int) (WindowExStyles
.WS_EX_WINDOWEDGE
| WindowExStyles
.WS_EX_MDICHILD
);
1059 switch (FormBorderStyle
) {
1060 case FormBorderStyle
.None
:
1062 case FormBorderStyle
.FixedToolWindow
:
1063 case FormBorderStyle
.SizableToolWindow
:
1064 cp
.ExStyle
|= (int) WindowExStyles
.WS_EX_TOOLWINDOW
;
1067 cp
.Style
|= (int) WindowStyles
.WS_OVERLAPPEDWINDOW
;
1072 switch (FormBorderStyle
) {
1073 case FormBorderStyle
.Fixed3D
: {
1074 cp
.Style
|= (int)(WindowStyles
.WS_CAPTION
| WindowStyles
.WS_BORDER
);
1075 cp
.ExStyle
|= (int)WindowExStyles
.WS_EX_CLIENTEDGE
;
1079 case FormBorderStyle
.FixedDialog
: {
1080 cp
.Style
|= (int)(WindowStyles
.WS_CAPTION
| WindowStyles
.WS_BORDER
);
1081 cp
.ExStyle
|= (int)(WindowExStyles
.WS_EX_DLGMODALFRAME
| WindowExStyles
.WS_EX_CONTROLPARENT
);
1085 case FormBorderStyle
.FixedSingle
: {
1086 cp
.Style
|= (int)(WindowStyles
.WS_CAPTION
| WindowStyles
.WS_BORDER
);
1090 case FormBorderStyle
.FixedToolWindow
: {
1091 cp
.Style
|= (int)(WindowStyles
.WS_CAPTION
| WindowStyles
.WS_BORDER
);
1092 cp
.ExStyle
|= (int)(WindowExStyles
.WS_EX_TOOLWINDOW
);
1096 case FormBorderStyle
.Sizable
: {
1097 cp
.Style
|= (int)(WindowStyles
.WS_BORDER
| WindowStyles
.WS_THICKFRAME
| WindowStyles
.WS_CAPTION
);
1101 case FormBorderStyle
.SizableToolWindow
: {
1102 cp
.Style
|= (int)(WindowStyles
.WS_BORDER
| WindowStyles
.WS_THICKFRAME
| WindowStyles
.WS_CAPTION
);
1103 cp
.ExStyle
|= (int)(WindowExStyles
.WS_EX_TOOLWINDOW
);
1107 case FormBorderStyle
.None
: {
1113 switch(window_state
) {
1114 case FormWindowState
.Maximized
: {
1115 cp
.Style
|= (int)WindowStyles
.WS_MAXIMIZE
;
1119 case FormWindowState
.Minimized
: {
1120 cp
.Style
|= (int)WindowStyles
.WS_MINIMIZE
;
1126 cp
.ExStyle
|= (int) WindowExStyles
.WS_EX_TOPMOST
;
1129 if (ShowInTaskbar
) {
1130 cp
.ExStyle
|= (int)WindowExStyles
.WS_EX_APPWINDOW
;
1134 cp
.Style
|= (int)WindowStyles
.WS_MAXIMIZEBOX
;
1138 cp
.Style
|= (int)WindowStyles
.WS_MINIMIZEBOX
;
1142 cp
.Style
|= (int)WindowStyles
.WS_SYSMENU
;
1145 if (HelpButton
&& !MaximizeBox
&& !MinimizeBox
) {
1146 cp
.ExStyle
|= (int)WindowExStyles
.WS_EX_CONTEXTHELP
;
1150 cp
.Style
|= (int)WindowStyles
.WS_VISIBLE
;
1152 if (opacity
< 1.0 || TransparencyKey
!= Color
.Empty
) {
1153 cp
.ExStyle
|= (int)WindowExStyles
.WS_EX_LAYERED
;
1156 if (!is_enabled
&& context
== null) {
1157 cp
.Style
|= (int)(WindowStyles
.WS_DISABLED
);
1164 protected override ImeMode DefaultImeMode
{
1166 return ImeMode
.NoControl
;
1170 protected override Size DefaultSize
{
1172 return new Size (300, 300);
1176 protected Rectangle MaximizedBounds
{
1178 if (maximized_bounds
!= Rectangle
.Empty
) {
1179 return maximized_bounds
;
1181 return default_maximized_bounds
;
1185 maximized_bounds
= value;
1186 OnMaximizedBoundsChanged(EventArgs
.Empty
);
1187 if (IsHandleCreated
) {
1188 XplatUI
.SetWindowMinMax(Handle
, maximized_bounds
, minimum_size
, maximum_size
);
1192 #endregion // Protected Instance Properties
1194 #region Public Static Methods
1196 [EditorBrowsable(EditorBrowsableState
.Never
)]
1197 [Obsolete ("This method has been deprecated. Use AutoScaleDimensions instead")]
1199 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1201 public static SizeF
GetAutoScaleSize (Font font
)
1203 return XplatUI
.GetAutoScaleSize(font
);
1206 #endregion // Public Static Methods
1208 #region Public Instance Methods
1209 internal SizeF
GetAutoScaleSize (Graphics g
, Font font
)
1212 // The following constants come from the dotnet mailing list
1213 // discussion: http://discuss.develop.com/archives/wa.exe?A2=ind0203A&L=DOTNET&P=R3655
1215 // The magic number is "Its almost the length
1216 // of the string with a smattering added in
1217 // for compat with earlier code".
1220 string magic_string
= "The quick brown fox jumped over the lazy dog.";
1221 double magic_number
= 44.549996948242189;
1222 float width
= (float) (g
.MeasureString (magic_string
, font
).Width
/ magic_number
);
1224 return new SizeF (width
, font
.Height
);
1227 public void Activate ()
1231 // The docs say activate only activates if our app is already active
1232 if (IsHandleCreated
) {
1234 MdiParent
.ActivateMdiChild (this);
1235 } else if (IsMdiContainer
) {
1236 SendControlFocus (mdi_container
);
1238 active
= ActiveForm
;
1239 if ((active
!= null) && (this != active
)) {
1240 XplatUI
.Activate(window
.Handle
);
1246 public void AddOwnedForm(Form ownedForm
) {
1247 if (!owned_forms
.Contains(ownedForm
)) {
1248 owned_forms
.Add(ownedForm
);
1250 ownedForm
.Owner
= this;
1253 public void Close () {
1260 XplatUI
.SendMessage(this.Handle
, Msg
.WM_CLOSE
, IntPtr
.Zero
, IntPtr
.Zero
);
1263 public void LayoutMdi(MdiLayout
value) {
1264 if (mdi_container
!= null) {
1265 mdi_container
.LayoutMdi(value);
1269 public void RemoveOwnedForm(Form ownedForm
) {
1270 owned_forms
.Remove(ownedForm
);
1273 public void SetDesktopBounds(int x
, int y
, int width
, int height
) {
1274 DesktopBounds
= new Rectangle(x
, y
, width
, height
);
1277 public void SetDesktopLocation(int x
, int y
) {
1278 DesktopLocation
= new Point(x
, y
);
1281 public DialogResult
ShowDialog() {
1282 return ShowDialog(this.owner
);
1285 public DialogResult
ShowDialog(IWin32Window ownerWin32
) {
1288 IntPtr capture_window
;
1292 if (ownerWin32
!= null) {
1293 Control c
= Control
.FromHandle (ownerWin32
.Handle
);
1295 owner
= c
.TopLevelControl
as Form
;
1298 if (owner
== this) {
1299 throw new InvalidOperationException("The 'ownerWin32' cannot be the form being shown.");
1303 throw new InvalidOperationException("The form is already displayed as a modal dialog.");
1307 throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
1311 throw new InvalidOperationException("Cannot display a disabled form as modal dialog.");
1314 if (TopLevelControl
!= this) {
1315 throw new InvalidOperationException("Can only display TopLevel forms as modal dialog.");
1319 // Can't do this, will screw us in the modal loop
1320 form_parent_window
.Parent
= this.owner
;
1323 // Release any captures
1324 XplatUI
.GrabInfo(out capture_window
, out confined
, out area
);
1325 if (capture_window
!= IntPtr
.Zero
) {
1326 XplatUI
.UngrabWindow(capture_window
);
1330 // Commented out; we instead let the Visible=true inside the runloop create the control
1331 // otherwise setting DialogResult inside any of the events that are triggered by the
1332 // create will not actually cause the form to not be displayed.
1333 // Leaving this comment here in case there was an actual purpose to creating the control
1335 if (!IsHandleCreated
) {
1340 Application
.RunLoop(true, new ApplicationContext(this));
1342 if (owner
!= null) {
1343 // Cannot use Activate(), it has a check for the current active window...
1344 XplatUI
.Activate(owner
.window
.Handle
);
1347 if (DialogResult
!= DialogResult
.None
) {
1348 return DialogResult
;
1350 DialogResult
= DialogResult
.Cancel
;
1351 return DialogResult
.Cancel
;
1354 public override string ToString() {
1355 return GetType().FullName
.ToString() + ", Text: " + Text
;
1357 #endregion // Public Instance Methods
1359 #region Protected Instance Methods
1360 protected void ActivateMdiChild(Form form
) {
1361 if (!IsMdiContainer
)
1363 mdi_container
.ActivateChild (form
);
1364 OnMdiChildActivate(EventArgs
.Empty
);
1367 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1368 protected override void AdjustFormScrollbars(bool displayScrollbars
) {
1369 base.AdjustFormScrollbars (displayScrollbars
);
1373 [EditorBrowsable(EditorBrowsableState
.Never
)]
1374 [Obsolete ("This method has been deprecated")] // XXX what to use instead?
1376 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1378 protected void ApplyAutoScaling()
1380 SizeF current_size_f
= GetAutoScaleSize (DeviceContext
, Font
);
1381 Size current_size
= new Size ((int) current_size_f
.Width
, (int) current_size_f
.Height
);
1385 if (current_size
== autoscale_base_size
)
1388 if (Environment
.GetEnvironmentVariable ("MONO_MWF_SCALING") == "disable"){
1393 // I tried applying the Fudge height factor from:
1394 // http://blogs.msdn.com/mharsh/archive/2004/01/25/62621.aspx
1395 // but it makes things larger without looking better.
1397 if (current_size_f
.Width
!= AutoScaleBaseSize
.Width
) {
1398 dx
= current_size_f
.Width
/ AutoScaleBaseSize
.Width
+ 0.08f
;
1403 if (current_size_f
.Height
!= AutoScaleBaseSize
.Height
) {
1404 dy
= current_size_f
.Height
/ AutoScaleBaseSize
.Height
+ 0.08f
;
1411 AutoScaleBaseSize
= current_size
;
1414 protected void CenterToParent() {
1422 w
= DefaultSize
.Width
;
1428 h
= DefaultSize
.Height
;
1432 if (Parent
!= null) {
1434 } else if (owner
!= null) {
1438 if (owner
!= null) {
1439 this.Location
= new Point(ctl
.Left
+ ctl
.Width
/ 2 - w
/2, ctl
.Top
+ ctl
.Height
/ 2 - h
/ 2);
1443 protected void CenterToScreen() {
1451 w
= DefaultSize
.Width
;
1457 h
= DefaultSize
.Height
;
1460 XplatUI
.GetDisplaySize(out DisplaySize
);
1461 this.Location
= new Point(DisplaySize
.Width
/ 2 - w
/ 2, DisplaySize
.Height
/ 2 - h
/ 2);
1464 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1465 protected override Control
.ControlCollection
CreateControlsInstance() {
1466 return base.CreateControlsInstance ();
1469 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1470 protected override void CreateHandle() {
1471 base.CreateHandle ();
1473 Application
.AddForm (this);
1477 if ((XplatUI
.SupportsTransparency() & TransparencySupport
.Set
) != 0) {
1478 if (allow_transparency
) {
1479 XplatUI
.SetWindowTransparency(Handle
, Opacity
, TransparencyKey
);
1483 XplatUI
.SetWindowMinMax(window
.Handle
, maximized_bounds
, minimum_size
, maximum_size
);
1484 if ((FormBorderStyle
!= FormBorderStyle
.FixedDialog
) && (icon
!= null)) {
1485 XplatUI
.SetIcon(window
.Handle
, icon
);
1488 if ((owner
!= null) && (owner
.IsHandleCreated
)) {
1489 XplatUI
.SetTopmost(window
.Handle
, owner
.window
.Handle
, true);
1492 for (int i
= 0; i
< owned_forms
.Count
; i
++) {
1493 if (owned_forms
[i
].IsHandleCreated
)
1494 XplatUI
.SetTopmost(owned_forms
[i
].window
.Handle
, window
.Handle
, true);
1497 if (window_manager
!= null && window_state
!= FormWindowState
.Normal
) {
1498 window_manager
.SetWindowState (FormWindowState
.Normal
, window_state
);
1503 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1504 protected override void DefWndProc(ref Message m
) {
1505 base.DefWndProc (ref m
);
1508 protected override void Dispose(bool disposing
)
1510 for (int i
= 0; i
< owned_forms
.Count
; i
++)
1511 ((Form
)owned_forms
[i
]).Owner
= null;
1513 owned_forms
.Clear ();
1515 base.Dispose (disposing
);
1517 Application
.RemoveForm (this);
1520 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1521 protected virtual void OnActivated(EventArgs e
)
1523 EventHandler eh
= (EventHandler
)(Events
[ActivatedEvent
]);
1528 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1529 protected virtual void OnClosed(EventArgs e
) {
1530 EventHandler eh
= (EventHandler
)(Events
[ClosedEvent
]);
1535 // Consider calling FireClosingEvents instead of calling this directly.
1536 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
1537 protected virtual void OnClosing(System
.ComponentModel
.CancelEventArgs e
) {
1538 CancelEventHandler eh
= (CancelEventHandler
)(Events
[ClosingEvent
]);
1543 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1544 protected override void OnCreateControl() {
1545 base.OnCreateControl ();
1548 XplatUI
.SetMenu(window
.Handle
, menu
);
1551 OnLoad(EventArgs
.Empty
);
1553 // Send initial location
1554 OnLocationChanged(EventArgs
.Empty
);
1556 if (IsMdiContainer
) {
1557 mdi_container
.LayoutMdi (MdiLayout
.Cascade
);
1561 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1562 protected virtual void OnDeactivate(EventArgs e
) {
1563 EventHandler eh
= (EventHandler
)(Events
[DeactivateEvent
]);
1568 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1569 protected override void OnFontChanged(EventArgs e
) {
1570 base.OnFontChanged (e
);
1573 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1574 protected override void OnHandleCreated(EventArgs e
) {
1575 XplatUI
.SetBorderStyle(window
.Handle
, form_border_style
);
1576 base.OnHandleCreated (e
);
1579 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1580 protected override void OnHandleDestroyed(EventArgs e
) {
1581 base.OnHandleDestroyed (e
);
1584 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1585 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e
) {
1586 InputLanguageChangedEventHandler eh
= (InputLanguageChangedEventHandler
)(Events
[InputLanguageChangedEvent
]);
1591 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1592 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e
) {
1593 InputLanguageChangingEventHandler eh
= (InputLanguageChangingEventHandler
)(Events
[InputLanguageChangingEvent
]);
1598 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1599 protected virtual void OnLoad(EventArgs e
) {
1601 ApplyAutoScaling ();
1605 EventHandler eh
= (EventHandler
)(Events
[LoadEvent
]);
1610 switch (StartPosition
) {
1611 case FormStartPosition
.CenterScreen
:
1612 this.CenterToScreen();
1614 case FormStartPosition
.CenterParent
:
1615 this.CenterToParent ();
1617 case FormStartPosition
.Manual
:
1618 Left
= CreateParams
.X
;
1619 Top
= CreateParams
.Y
;
1626 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1627 protected virtual void OnMaximizedBoundsChanged(EventArgs e
) {
1628 EventHandler eh
= (EventHandler
)(Events
[MaximizedBoundsChangedEvent
]);
1633 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1634 protected virtual void OnMaximumSizeChanged(EventArgs e
) {
1635 EventHandler eh
= (EventHandler
)(Events
[MaximumSizeChangedEvent
]);
1640 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1641 protected virtual void OnMdiChildActivate(EventArgs e
) {
1642 EventHandler eh
= (EventHandler
)(Events
[MdiChildActivateEvent
]);
1647 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1648 protected virtual void OnMenuComplete(EventArgs e
) {
1649 EventHandler eh
= (EventHandler
)(Events
[MenuCompleteEvent
]);
1654 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1655 protected virtual void OnMenuStart(EventArgs e
) {
1656 EventHandler eh
= (EventHandler
)(Events
[MenuStartEvent
]);
1661 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1662 protected virtual void OnMinimumSizeChanged(EventArgs e
) {
1663 EventHandler eh
= (EventHandler
)(Events
[MinimumSizeChangedEvent
]);
1668 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1669 protected override void OnPaint (PaintEventArgs pevent
) {
1670 base.OnPaint (pevent
);
1673 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1674 protected override void OnResize(EventArgs e
) {
1677 if (this.IsMdiChild
&& ParentForm
!= null) {
1678 ParentForm
.PerformLayout();
1679 ParentForm
.Size
= ParentForm
.Size
;
1683 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1684 protected override void OnStyleChanged(EventArgs e
) {
1685 base.OnStyleChanged (e
);
1688 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1689 protected override void OnTextChanged(EventArgs e
) {
1690 base.OnTextChanged (e
);
1692 if (mdi_container
!= null)
1693 mdi_container
.SetParentText(true);
1696 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1697 protected override void OnVisibleChanged(EventArgs e
) {
1698 base.OnVisibleChanged (e
);
1701 if (window_manager
!= null)
1702 window_manager
.SetWindowState (WindowState
, WindowState
);
1706 protected override bool ProcessCmdKey(ref Message msg
, Keys keyData
) {
1707 if (base.ProcessCmdKey (ref msg
, keyData
)) {
1711 // Give our menu a shot
1712 if (ActiveMenu
!= null) {
1713 return ActiveMenu
.ProcessCmdKey(ref msg
, keyData
);
1719 // LAMESPEC - Not documented that Form overrides ProcessDialogChar; class-status showed
1720 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
1721 protected override bool ProcessDialogChar(char charCode
) {
1722 return base.ProcessDialogChar (charCode
);
1725 protected override bool ProcessDialogKey(Keys keyData
) {
1726 if ((keyData
& Keys
.Modifiers
) == 0) {
1727 if (keyData
== Keys
.Enter
) {
1728 IntPtr window
= XplatUI
.GetFocus ();
1729 Control c
= Control
.FromHandle (window
);
1730 if (c
is Button
&& c
.FindForm () == this) {
1731 ((Button
)c
).PerformClick ();
1734 else if (accept_button
!= null) {
1735 accept_button
.PerformClick();
1738 } else if (keyData
== Keys
.Escape
&& cancel_button
!= null) {
1739 cancel_button
.PerformClick();
1743 return base.ProcessDialogKey(keyData
);
1746 protected override bool ProcessKeyPreview(ref Message msg
) {
1748 if (ProcessKeyEventArgs(ref msg
)) {
1752 return base.ProcessKeyPreview (ref msg
);
1755 protected override bool ProcessTabKey(bool forward
) {
1756 return SelectNextControl(ActiveControl
, forward
, true, true, true);
1760 [EditorBrowsable(EditorBrowsableState
.Never
)]
1762 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1764 protected override void ScaleCore(float dx
, float dy
) {
1768 // We can't scale max or min windows
1769 if (WindowState
== FormWindowState
.Normal
) {
1770 // We cannot call base since base also adjusts X/Y, but
1771 // a form is toplevel and doesn't move
1775 if (!GetStyle(ControlStyles
.FixedWidth
)) {
1776 size
.Width
= (int)(size
.Width
* dx
);
1779 if (!GetStyle(ControlStyles
.FixedHeight
)) {
1780 size
.Height
= (int)(size
.Height
* dy
);
1786 /* Now scale our children */
1787 Control
[] controls
= Controls
.GetAllControls ();
1788 for (int i
=0; i
< controls
.Length
; i
++) {
1789 controls
[i
].Scale(dx
, dy
);
1798 protected override void Select(bool directed
, bool forward
) {
1802 base.SelectNextControl(null, forward
, true, true, true);
1805 parent
= this.ParentForm
;
1806 if (parent
!= null) {
1807 parent
.ActiveControl
= this;
1813 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1814 protected override void SetBoundsCore(int x
, int y
, int width
, int height
, BoundsSpecified specified
) {
1815 base.SetBoundsCore (x
, y
, width
, height
, specified
);
1818 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1819 protected override void SetClientSizeCore(int x
, int y
) {
1820 if ((minimum_size
.Width
!= 0) && (x
< minimum_size
.Width
)) {
1821 x
= minimum_size
.Width
;
1822 } else if ((maximum_size
.Width
!= 0) && (x
> maximum_size
.Width
)) {
1823 x
= maximum_size
.Width
;
1826 if ((minimum_size
.Height
!= 0) && (y
< minimum_size
.Height
)) {
1827 y
= minimum_size
.Height
;
1828 } else if ((maximum_size
.Height
!= 0) && (y
> maximum_size
.Height
)) {
1829 y
= maximum_size
.Height
;
1832 Rectangle ClientRect
= new Rectangle(0, 0, x
, y
);
1833 Rectangle WindowRect
;
1834 CreateParams cp
= this.CreateParams
;
1836 clientsize_set
= new Size(x
, y
);
1838 if (XplatUI
.CalculateWindowRect(ref ClientRect
, cp
.Style
, cp
.ExStyle
, cp
.menu
, out WindowRect
)) {
1839 SetBounds(bounds
.X
, bounds
.Y
, WindowRect
.Width
, WindowRect
.Height
, BoundsSpecified
.Size
);
1843 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1844 protected override void SetVisibleCore(bool value) {
1845 is_changing_visible_state
= true;
1846 has_been_visible
= value || has_been_visible
;
1847 base.SetVisibleCore (value);
1848 is_changing_visible_state
= false;
1851 protected override void UpdateDefaultButton() {
1852 base.UpdateDefaultButton ();
1855 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1856 protected override void WndProc(ref Message m
) {
1858 if (window_manager
!= null && window_manager
.HandleMessage (ref m
)) {
1862 switch((Msg
)m
.Msg
) {
1863 case Msg
.WM_DESTROY
: {
1864 base.WndProc(ref m
);
1865 if (!RecreatingHandle
) {
1866 this.closing
= true;
1871 case Msg
.WM_CLOSE_INTERNAL
: {
1876 case Msg
.WM_CLOSE
: {
1877 Form act
= Form
.ActiveForm
;
1878 if (act
!= null && act
!= this && act
.Modal
== true) {
1882 if (mdi_container
!= null) {
1883 foreach (Form mdi_child
in mdi_container
.MdiChildren
) {
1884 mdi_child
.FireClosingEvents (CloseReason
.MdiFormClosing
);
1889 if (!FireClosingEvents (CloseReason
.UserClosing
)) {
1890 OnClosed (EventArgs
.Empty
);
1898 if (FireClosingEvents (CloseReason
.UserClosing
)) {
1899 DialogResult
= DialogResult
.None
;
1903 OnClosed (EventArgs
.Empty
);
1912 case Msg
.WM_WINDOWPOSCHANGED
: {
1913 if (WindowState
!= FormWindowState
.Minimized
) {
1914 base.WndProc(ref m
);
1920 case Msg
.WM_SYSCOMMAND
: {
1921 // Let *Strips know the app's title bar was clicked
1922 if (XplatUI
.IsEnabled (Handle
))
1923 ToolStripManager
.FireAppClicked ();
1925 base.WndProc(ref m
);
1930 case Msg
.WM_ACTIVATE
: {
1931 if (m
.WParam
!= (IntPtr
)WindowActiveFlags
.WA_INACTIVE
) {
1933 SelectActiveControl ();
1935 if (ActiveControl
!= null && !ActiveControl
.Focused
)
1936 SendControlFocus (ActiveControl
);
1939 OnActivated(EventArgs
.Empty
);
1941 OnDeactivate(EventArgs
.Empty
);
1946 case Msg
.WM_KILLFOCUS
: {
1947 base.WndProc(ref m
);
1951 case Msg
.WM_SETFOCUS
: {
1952 if (ActiveControl
!= null && ActiveControl
!= this) {
1953 ActiveControl
.Focus();
1954 return; // FIXME - do we need to run base.WndProc, even though we just changed focus?
1956 base.WndProc(ref m
);
1961 case Msg
.WM_NCLBUTTONDOWN
: {
1962 if (XplatUI
.IsEnabled (Handle
) && ActiveMenu
!= null) {
1963 ActiveMenu
.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m
.WParam
.ToInt32()), mouse_clicks
, Control
.MousePosition
.X
, Control
.MousePosition
.Y
, 0));
1966 if (ActiveMaximizedMdiChild
!= null) {
1967 if (ActiveMaximizedMdiChild
.HandleMenuMouseDown (ActiveMenu
,
1968 LowOrder ((int) m
.LParam
.ToInt32 ()),
1969 HighOrder ((int) m
.LParam
.ToInt32 ()))) {
1970 // Don't let base process this message, otherwise we won't
1971 // get a WM_NCLBUTTONUP.
1975 base.WndProc(ref m
);
1978 case Msg
.WM_NCLBUTTONUP
: {
1979 if (ActiveMaximizedMdiChild
!= null) {
1980 ActiveMaximizedMdiChild
.HandleMenuMouseUp (ActiveMenu
,
1981 LowOrder ((int)m
.LParam
.ToInt32 ()),
1982 HighOrder ((int)m
.LParam
.ToInt32 ()));
1984 base.WndProc (ref m
);
1988 case Msg
.WM_NCMOUSELEAVE
: {
1989 if (ActiveMaximizedMdiChild
!= null) {
1990 ActiveMaximizedMdiChild
.HandleMenuMouseLeave(ActiveMenu
,
1991 LowOrder((int)m
.LParam
.ToInt32()),
1992 HighOrder((int)m
.LParam
.ToInt32()));
1994 base.WndProc(ref m
);
1998 case Msg
.WM_NCMOUSEMOVE
: {
1999 if (XplatUI
.IsEnabled (Handle
) && ActiveMenu
!= null) {
2000 ActiveMenu
.OnMouseMove(this, new MouseEventArgs (FromParamToMouseButtons ((int) m
.WParam
.ToInt32()), mouse_clicks
, LowOrder ((int) m
.LParam
.ToInt32 ()), HighOrder ((int) m
.LParam
.ToInt32 ()), 0));
2003 if (ActiveMaximizedMdiChild
!= null) {
2004 XplatUI
.RequestAdditionalWM_NCMessages (Handle
, false, true);
2005 ActiveMaximizedMdiChild
.HandleMenuMouseMove (ActiveMenu
,
2006 LowOrder ((int)m
.LParam
.ToInt32 ()),
2007 HighOrder ((int)m
.LParam
.ToInt32 ()));
2009 base.WndProc(ref m
);
2013 case Msg
.WM_NCPAINT
: {
2014 if (ActiveMenu
!= null) {
2018 pe
= XplatUI
.PaintEventStart(Handle
, false);
2019 pnt
= XplatUI
.GetMenuOrigin(window
.Handle
);
2021 // The entire menu has to be in the clip rectangle because the
2022 // control buttons are right-aligned and otherwise they would
2023 // stay painted when the window gets resized.
2024 Rectangle clip
= new Rectangle (pnt
.X
, pnt
.Y
, ClientSize
.Width
, 0);
2025 clip
= Rectangle
.Union(clip
, pe
.ClipRectangle
);
2027 pe
.Graphics
.SetClip(clip
);
2029 ActiveMenu
.Draw (pe
, new Rectangle (pnt
.X
, pnt
.Y
, ClientSize
.Width
, 0));
2031 if (ActiveMaximizedMdiChild
!= null) {
2032 ActiveMaximizedMdiChild
.DrawMaximizedButtons (ActiveMenu
, pe
);
2035 XplatUI
.PaintEventEnd(Handle
, false);
2038 base.WndProc(ref m
);
2042 case Msg
.WM_NCCALCSIZE
: {
2043 XplatUIWin32
.NCCALCSIZE_PARAMS ncp
;
2045 if ((ActiveMenu
!= null) && (m
.WParam
== (IntPtr
)1)) {
2046 ncp
= (XplatUIWin32
.NCCALCSIZE_PARAMS
)Marshal
.PtrToStructure(m
.LParam
, typeof(XplatUIWin32
.NCCALCSIZE_PARAMS
));
2049 ncp
.rgrc1
.top
+= ThemeEngine
.Current
.CalcMenuBarSize (DeviceContext
, ActiveMenu
, ncp
.rgrc1
.right
- ncp
.rgrc1
.left
);
2050 Marshal
.StructureToPtr(ncp
, m
.LParam
, true);
2056 case Msg
.WM_MOUSEMOVE
: {
2057 if (XplatUI
.IsEnabled (Handle
) && active_tracker
!= null) {
2058 MouseEventArgs args
;
2060 args
= new MouseEventArgs (FromParamToMouseButtons ((int) m
.WParam
.ToInt32()),
2061 mouse_clicks
, LowOrder ((int) m
.LParam
.ToInt32 ()), HighOrder ((int) m
.LParam
.ToInt32 ()), 0);
2062 active_tracker
.OnMotion(new MouseEventArgs (args
.Button
, args
.Clicks
, Control
.MousePosition
.X
, Control
.MousePosition
.Y
, args
.Delta
));
2065 base.WndProc(ref m
);
2069 case Msg
.WM_LBUTTONDOWN
:
2070 case Msg
.WM_MBUTTONDOWN
:
2071 case Msg
.WM_RBUTTONDOWN
: {
2072 if (XplatUI
.IsEnabled (Handle
) && active_tracker
!= null) {
2073 MouseEventArgs args
;
2075 args
= new MouseEventArgs (FromParamToMouseButtons ((int) m
.WParam
.ToInt32()),
2076 mouse_clicks
, LowOrder ((int) m
.LParam
.ToInt32 ()), HighOrder ((int) m
.LParam
.ToInt32 ()), 0);
2077 active_tracker
.OnMouseDown(new MouseEventArgs (args
.Button
, args
.Clicks
, Control
.MousePosition
.X
, Control
.MousePosition
.Y
, args
.Delta
));
2080 base.WndProc(ref m
);
2084 case Msg
.WM_LBUTTONUP
:
2085 case Msg
.WM_MBUTTONUP
:
2086 case Msg
.WM_RBUTTONUP
: {
2087 if (XplatUI
.IsEnabled (Handle
) && active_tracker
!= null) {
2088 MouseEventArgs args
;
2089 MouseButtons mb
= FromParamToMouseButtons ((int) m
.WParam
.ToInt32());
2091 // We add in the button that was released (not sent in WParam)
2092 switch((Msg
)m
.Msg
) {
2093 case Msg
.WM_LBUTTONUP
:
2094 mb
|= MouseButtons
.Left
;
2096 case Msg
.WM_MBUTTONUP
:
2097 mb
|= MouseButtons
.Middle
;
2099 case Msg
.WM_RBUTTONUP
:
2100 mb
|= MouseButtons
.Right
;
2104 args
= new MouseEventArgs (mb
, mouse_clicks
, LowOrder ((int) m
.LParam
.ToInt32 ()), HighOrder ((int) m
.LParam
.ToInt32 ()), 0);
2105 active_tracker
.OnMouseUp(new MouseEventArgs (args
.Button
, args
.Clicks
, Control
.MousePosition
.X
, Control
.MousePosition
.Y
, args
.Delta
));
2109 base.WndProc(ref m
);
2113 case Msg
.WM_GETMINMAXINFO
: {
2116 if (m
.LParam
!= IntPtr
.Zero
) {
2117 mmi
= (MINMAXINFO
)Marshal
.PtrToStructure(m
.LParam
, typeof(MINMAXINFO
));
2119 default_maximized_bounds
= new Rectangle(mmi
.ptMaxPosition
.x
, mmi
.ptMaxPosition
.y
, mmi
.ptMaxSize
.x
, mmi
.ptMaxSize
.y
);
2120 if (maximized_bounds
!= Rectangle
.Empty
) {
2121 mmi
.ptMaxPosition
.x
= maximized_bounds
.Left
;
2122 mmi
.ptMaxPosition
.y
= maximized_bounds
.Top
;
2123 mmi
.ptMaxSize
.x
= maximized_bounds
.Width
;
2124 mmi
.ptMaxSize
.y
= maximized_bounds
.Height
;
2127 if (minimum_size
!= Size
.Empty
) {
2128 mmi
.ptMinTrackSize
.x
= minimum_size
.Width
;
2129 mmi
.ptMinTrackSize
.y
= minimum_size
.Height
;
2132 if (maximum_size
!= Size
.Empty
) {
2133 mmi
.ptMaxTrackSize
.x
= maximum_size
.Width
;
2134 mmi
.ptMaxTrackSize
.y
= maximum_size
.Height
;
2136 Marshal
.StructureToPtr(mmi
, m
.LParam
, false);
2142 case Msg
.WM_MOUSEACTIVATE
: {
2143 // Let *Strips know the form or another control has been clicked
2144 if (XplatUI
.IsEnabled (Handle
))
2145 ToolStripManager
.FireAppClicked ();
2147 base.WndProc (ref m
);
2151 case Msg
.WM_ACTIVATEAPP
: {
2152 // Let *Strips know the app lost focus
2153 if (m
.WParam
== (IntPtr
)0)
2154 if (XplatUI
.IsEnabled (Handle
))
2155 ToolStripManager
.FireAppFocusChanged (this);
2157 base.WndProc (ref m
);
2163 base.WndProc (ref m
);
2168 #endregion // Protected Instance Methods
2170 internal override void FireEnter ()
2172 // do nothing - forms don't generate OnEnter
2175 internal override void FireLeave ()
2177 // do nothing - forms don't generate OnLeave
2180 internal void RemoveWindowManager ()
2182 window_manager
= null;
2185 internal override void CheckAcceptButton()
2187 if (accept_button
!= null) {
2188 Button a_button
= accept_button
as Button
;
2190 if (ActiveControl
== a_button
)
2193 if (ActiveControl
is Button
) {
2194 a_button
.paint_as_acceptbutton
= false;
2198 a_button
.paint_as_acceptbutton
= true;
2205 static object ActivatedEvent
= new object ();
2206 static object ClosedEvent
= new object ();
2207 static object ClosingEvent
= new object ();
2208 static object DeactivateEvent
= new object ();
2209 static object InputLanguageChangedEvent
= new object ();
2210 static object InputLanguageChangingEvent
= new object ();
2211 static object LoadEvent
= new object ();
2212 static object MaximizedBoundsChangedEvent
= new object ();
2213 static object MaximumSizeChangedEvent
= new object ();
2214 static object MdiChildActivateEvent
= new object ();
2215 static object MenuCompleteEvent
= new object ();
2216 static object MenuStartEvent
= new object ();
2217 static object MinimumSizeChangedEvent
= new object ();
2219 public event EventHandler Activated
{
2220 add { Events.AddHandler (ActivatedEvent, value); }
2221 remove { Events.RemoveHandler (ActivatedEvent, value); }
2226 [EditorBrowsable (EditorBrowsableState
.Never
)]
2228 public event EventHandler Closed
{
2229 add { Events.AddHandler (ClosedEvent, value); }
2230 remove { Events.RemoveHandler (ClosedEvent, value); }
2235 [EditorBrowsable (EditorBrowsableState
.Never
)]
2237 public event CancelEventHandler Closing
{
2238 add { Events.AddHandler (ClosingEvent, value); }
2239 remove { Events.RemoveHandler (ClosingEvent, value); }
2242 public event EventHandler Deactivate
{
2243 add { Events.AddHandler (DeactivateEvent, value); }
2244 remove { Events.RemoveHandler (DeactivateEvent, value); }
2247 public event InputLanguageChangedEventHandler InputLanguageChanged
{
2248 add { Events.AddHandler (InputLanguageChangedEvent, value); }
2249 remove { Events.RemoveHandler (InputLanguageChangedEvent, value); }
2252 public event InputLanguageChangingEventHandler InputLanguageChanging
{
2253 add { Events.AddHandler (InputLanguageChangingEvent, value); }
2254 remove { Events.RemoveHandler (InputLanguageChangingEvent, value); }
2257 public event EventHandler Load
{
2258 add { Events.AddHandler (LoadEvent, value); }
2259 remove { Events.RemoveHandler (LoadEvent, value); }
2262 public event EventHandler MaximizedBoundsChanged
{
2263 add { Events.AddHandler (MaximizedBoundsChangedEvent, value); }
2264 remove { Events.RemoveHandler (MaximizedBoundsChangedEvent, value); }
2267 public event EventHandler MaximumSizeChanged
{
2268 add { Events.AddHandler (MaximumSizeChangedEvent, value); }
2269 remove { Events.RemoveHandler (MaximumSizeChangedEvent, value); }
2272 public event EventHandler MdiChildActivate
{
2273 add { Events.AddHandler (MdiChildActivateEvent, value); }
2274 remove { Events.RemoveHandler (MdiChildActivateEvent, value); }
2280 public event EventHandler MenuComplete
{
2281 add { Events.AddHandler (MenuCompleteEvent, value); }
2282 remove { Events.RemoveHandler (MenuCompleteEvent, value); }
2288 public event EventHandler MenuStart
{
2289 add { Events.AddHandler (MenuStartEvent, value); }
2290 remove { Events.RemoveHandler (MenuStartEvent, value); }
2293 public event EventHandler MinimumSizeChanged
{
2294 add { Events.AddHandler (MinimumSizeChangedEvent, value); }
2295 remove { Events.RemoveHandler (MinimumSizeChangedEvent, value); }
2300 [EditorBrowsable(EditorBrowsableState
.Never
)]
2301 public new event EventHandler TabIndexChanged
{
2302 add { base.TabIndexChanged += value; }
2303 remove { base.TabIndexChanged -= value; }
2305 #endregion // Events
2308 [SettingsBindable (true)]
2309 public override string Text
{
2319 [SettingsBindable (true)]
2320 public new Point Location
{
2322 return base.Location
;
2326 base.Location
= value;
2330 static object AutoValidateChangedEvent
= new object ();
2331 static object FormClosingEvent
= new object ();
2332 static object FormClosedEvent
= new object ();
2333 static object HelpButtonClickedEvent
= new object ();
2334 static object ResizeEndEvent
= new object ();
2335 static object ResizeBeginEvent
= new object ();
2336 static object RightToLeftLayoutChangedEvent
= new object ();
2337 static object ShownEvent
= new object ();
2340 [EditorBrowsable (EditorBrowsableState
.Always
)]
2341 public new event EventHandler AutoSizeChanged
{
2342 add { base.AutoSizeChanged += value; }
2343 remove { base.AutoSizeChanged -= value; }
2347 [EditorBrowsable (EditorBrowsableState
.Always
)]
2348 public event EventHandler AutoValidateChanged
{
2349 add { Events.AddHandler (AutoValidateChangedEvent, value); }
2350 remove { Events.RemoveHandler (AutoValidateChangedEvent, value); }
2353 public event FormClosingEventHandler FormClosing
{
2354 add { Events.AddHandler (FormClosingEvent, value); }
2355 remove { Events.RemoveHandler (FormClosingEvent, value); }
2358 public event FormClosedEventHandler FormClosed
{
2359 add { Events.AddHandler (FormClosedEvent, value); }
2360 remove { Events.RemoveHandler (FormClosedEvent, value); }
2364 [EditorBrowsable (EditorBrowsableState
.Always
)]
2365 public event CancelEventHandler HelpButtonClicked
{
2366 add { Events.AddHandler (HelpButtonClickedEvent, value); }
2367 remove { Events.RemoveHandler (HelpButtonClickedEvent, value); }
2371 [EditorBrowsable (EditorBrowsableState
.Never
)]
2372 public new event EventHandler MarginChanged
{
2373 add { base.MarginChanged += value; }
2374 remove { base.MarginChanged -= value; }
2377 public event EventHandler RightToLeftLayoutChanged
{
2378 add { Events.AddHandler (RightToLeftLayoutChangedEvent, value); }
2379 remove { Events.RemoveHandler (RightToLeftLayoutChangedEvent, value); }
2382 public event EventHandler ResizeBegin
{
2383 add { Events.AddHandler (ResizeBeginEvent, value); }
2384 remove { Events.RemoveHandler (ResizeBeginEvent, value); }
2387 public event EventHandler ResizeEnd
{
2388 add { Events.AddHandler (ResizeEndEvent, value); }
2389 remove { Events.RemoveHandler (ResizeEndEvent, value); }
2392 public event EventHandler Shown
{
2393 add { Events.AddHandler (ShownEvent, value); }
2394 remove { Events.RemoveHandler (ShownEvent, value); }
2398 [EditorBrowsable (EditorBrowsableState
.Never
)]
2399 public new event EventHandler TabStopChanged
{
2400 add { base.TabStopChanged += value; }
2401 remove { base.TabStopChanged -= value; }
2404 // Consider calling FireClosingEvents instead of calling this directly.
2405 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2406 protected virtual void OnFormClosing (FormClosingEventArgs e
)
2408 FormClosingEventHandler eh
= (FormClosingEventHandler
)(Events
[FormClosingEvent
]);
2413 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2414 protected virtual void OnResizeBegin (EventArgs e
)
2416 EventHandler eh
= (EventHandler
) (Events
[ResizeBeginEvent
]);
2421 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
2422 protected virtual void OnResizeEnd (EventArgs e
)
2424 EventHandler eh
= (EventHandler
) (Events
[ResizeEndEvent
]);