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 SizeGrip size_grip;
81 private Rectangle maximized_bounds;
82 private Rectangle default_maximized_bounds;
83 private double opacity;
84 internal ApplicationContext context;
85 Color transparency_key;
86 internal MenuTracker active_tracker;
87 private bool is_loaded;
88 internal bool is_changing_visible_state;
89 internal bool has_been_visible;
90 private bool shown_raised; // The shown event is only raised once
93 private MenuStrip main_menu_strip;
94 private bool show_icon = true;
96 #endregion // Local Variables
98 #region Private & Internal Methods
101 default_icon = Locale.GetResource("mono.ico") as Icon;
104 // warning: this is only hooked up when an mdi container is created.
105 private void ControlAddedHandler (object sender, ControlEventArgs e)
107 if (mdi_container != null) {
108 mdi_container.SendToBack ();
112 // Convenience method for fire BOTH OnClosing and OnFormClosing events
113 // Returns the value of Cancel, so true means the Close was cancelled,
114 // and you shouldn't close the form.
115 internal bool FireClosingEvents (CloseReason reason)
117 CancelEventArgs cea = new CancelEventArgs ();
118 this.OnClosing (cea);
121 FormClosingEventArgs fcea = new FormClosingEventArgs (reason, cea.Cancel);
122 this.OnFormClosing (fcea);
129 private void SelectActiveControl ()
131 if (this.IsMdiContainer) {
132 mdi_container.SendFocusToActiveChild ();
136 if (this.ActiveControl == null) {
139 // This visible hack is to work around CanSelect always being false if one of the parents
140 // is not visible; and we by default create Form invisible...
141 visible = this.is_visible;
142 this.is_visible = true;
144 if (SelectNextControl (this, true, true, true, true) == false) {
148 this.is_visible = visible;
150 Select (ActiveControl);
154 private new void UpdateSizeGripVisible ()
156 // Following link explains when to show size grip:
157 // http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=138687&SiteID=1
158 // if SizeGripStyle.Auto, only shown if form is shown using ShowDialog and is sizable
159 // if SizeGripStyle.Show, only shown if form is sizable
163 switch (size_grip_style) {
164 case SizeGripStyle.Auto:
165 show = is_modal && (form_border_style == FormBorderStyle.Sizable || form_border_style == FormBorderStyle.SizableToolWindow);
167 case SizeGripStyle.Hide:
170 case SizeGripStyle.Show:
171 show = (form_border_style == FormBorderStyle.Sizable || form_border_style == FormBorderStyle.SizableToolWindow);
176 if (size_grip != null && size_grip.Visible)
177 size_grip.Visible = false;
179 if (size_grip == null) {
180 size_grip = new SizeGrip (this);
181 size_grip.Virtual = true;
182 size_grip.FillBackground = false;
184 size_grip.Visible = true;
188 internal void ChangingParent (Control new_parent)
194 if (new_parent == null) {
195 window_manager = null;
196 } else if (new_parent is MdiClient) {
197 window_manager = new MdiWindowManager (this, (MdiClient) new_parent);
199 if (IsHandleCreated) {
200 RecreateHandle ();//XplatUI.SetWindowStyle (Handle, CreateParams);
202 window_manager = new FormWindowManager (this);
205 if (window_manager != null) {
206 window_manager.UpdateWindowState (window_state, window_state, true);
210 internal override bool FocusInternal (bool skip_check)
213 // MS always creates handles when Focus () is called for mdi clients.
214 if (!IsHandleCreated)
217 return base.FocusInternal (skip_check);
219 #endregion // Private & Internal Methods
221 #region Public Classes
225 public new class ControlCollection : Control.ControlCollection {
228 public ControlCollection(Form owner) : base(owner) {
229 this.form_owner = owner;
232 public override void Add(Control value) {
233 if (Contains (value))
236 ((Form)value).owner=form_owner;
239 public override void Remove(Control value) {
240 ((Form)value).owner = null;
244 #endregion // Public Classes
246 #region Public Constructor & Destructor
249 SizeF current_scale = GetAutoScaleSize (DeviceContext, Font);
252 autoscale_base_size = new Size ((int)current_scale.Width, (int) current_scale.Height);
253 allow_transparency = false;
256 dialog_result = DialogResult.None;
257 start_position = FormStartPosition.WindowsDefaultLocation;
258 form_border_style = FormBorderStyle.Sizable;
259 window_state = FormWindowState.Normal;
264 minimum_size = Size.Empty;
265 maximum_size = Size.Empty;
266 clientsize_set = Size.Empty;
271 show_in_taskbar = true;
274 size_grip_style = SizeGripStyle.Auto;
275 maximized_bounds = Rectangle.Empty;
276 default_maximized_bounds = Rectangle.Empty;
277 owned_forms = new Form.ControlCollection(this);
278 transparency_key = Color.Empty;
279 InternalClientSize = new Size (this.Width - (SystemInformation.FrameBorderSize.Width * 2), this.Height - (SystemInformation.FrameBorderSize.Height * 2) - SystemInformation.CaptionHeight);
281 #endregion // Public Constructor & Destructor
283 #region Public Static Properties
285 public static Form ActiveForm {
289 active = FromHandle(XplatUI.GetActive());
291 if (active != null) {
292 if ( !(active is Form)) {
295 parent = active.Parent;
296 while (parent != null) {
297 if (parent is Form) {
300 parent = parent.Parent;
310 #endregion // Public Static Properties
312 #region Public Instance Properties
314 public IButtonControl AcceptButton {
316 return accept_button;
320 accept_button = value;
326 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
327 public bool AllowTransparency {
329 return allow_transparency;
333 if (value == allow_transparency) {
337 allow_transparency = value;
340 if (IsHandleCreated) {
341 if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
342 XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
345 UpdateStyles(); // Remove the WS_EX_LAYERED style
353 [EditorBrowsable (EditorBrowsableState.Never)]
354 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
355 [Obsolete ("This property has been deprecated in favor of AutoScaleMode.")]
359 [MWFCategory("Layout")]
360 public bool AutoScale {
371 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
372 [EditorBrowsable(EditorBrowsableState.Never)]
374 [EditorBrowsable(EditorBrowsableState.Advanced)]
378 public virtual Size AutoScaleBaseSize {
380 return autoscale_base_size;
384 autoscale_base_size = value;
389 public override bool AutoScroll {
391 return base.AutoScroll;
394 base.AutoScroll = value;
398 public override Color BackColor {
400 /* we don't let parents override our
401 default background color for forms.
402 this fixes the default color for mdi
404 if (background_color.IsEmpty)
405 return DefaultBackColor;
407 return background_color;
410 base.BackColor = value;
415 public IButtonControl CancelButton {
417 return cancel_button;
421 cancel_button = value;
422 if (cancel_button != null && cancel_button.DialogResult == DialogResult.None)
423 cancel_button.DialogResult = DialogResult.Cancel;
427 // new property so we can change the DesignerSerializationVisibility
428 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
430 public new Size ClientSize {
431 get { return base.ClientSize; }
432 set { base.ClientSize = value; }
436 [MWFCategory("Window Style")]
437 public bool ControlBox {
443 if (control_box != value) {
451 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
452 public Rectangle DesktopBounds {
454 return new Rectangle(Location, Size);
463 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
464 public Point DesktopLocation {
475 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
476 public DialogResult DialogResult {
478 return dialog_result;
482 if (value < DialogResult.None || value > DialogResult.No)
483 throw new InvalidEnumArgumentException ("value", (int) value,
484 typeof (DialogResult));
486 dialog_result = value;
487 closing = (dialog_result != DialogResult.None && is_modal);
491 [DefaultValue(FormBorderStyle.Sizable)]
493 [MWFCategory("Appearance")]
494 public FormBorderStyle FormBorderStyle {
496 return form_border_style;
499 form_border_style = value;
501 if (window_manager == null) {
502 if (IsHandleCreated) {
503 XplatUI.SetBorderStyle(window.Handle, form_border_style);
506 window_manager.UpdateBorderStyle (value);
509 Size current_client_size = ClientSize;
512 if (this.IsHandleCreated) {
513 this.Size = InternalSizeFromClientSize (current_client_size);
514 XplatUI.InvalidateNC (this.Handle);
519 [DefaultValue(false)]
520 [MWFCategory("Window Style")]
521 public bool HelpButton {
527 if (help_button != value) {
536 [MWFCategory("Window Style")]
546 if (IsHandleCreated) {
547 XplatUI.SetIcon(Handle, icon == null ? default_icon : icon);
554 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
555 public bool IsMdiChild {
557 return mdi_parent != null;
561 [DefaultValue(false)]
562 [MWFCategory("Window Style")]
563 public bool IsMdiContainer {
565 return mdi_container != null;
569 if (value && mdi_container == null) {
570 mdi_container = new MdiClient ();
571 Controls.Add(mdi_container);
572 ControlAdded += new ControlEventHandler (ControlAddedHandler);
573 mdi_container.SendToBack ();
574 mdi_container.SetParentText (true);
575 } else if (!value && mdi_container != null) {
576 Controls.Remove(mdi_container);
577 mdi_container = null;
583 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
584 public Form ActiveMdiChild {
588 return (Form) mdi_container.ActiveMdiChild;
593 [EditorBrowsable(EditorBrowsableState.Advanced)]
594 public bool IsRestrictedWindow {
600 [DefaultValue(false)]
601 public bool KeyPreview {
612 [DefaultValue (null)]
613 [TypeConverter (typeof (ReferenceConverter))]
614 public MenuStrip MainMenuStrip {
615 get { return this.main_menu_strip; }
617 if (this.main_menu_strip != value) {
618 this.main_menu_strip = value;
619 this.main_menu_strip.RefreshMdiItems ();
624 [EditorBrowsable (EditorBrowsableState.Never)]
626 public new Padding Margin {
627 get { return base.Margin; }
628 set { base.Margin = value; }
633 [MWFCategory("Window Style")]
634 public bool MaximizeBox {
639 if (maximize_box != value) {
640 maximize_box = value;
646 [DefaultValue("{Width=0, Height=0}")]
648 [RefreshProperties(RefreshProperties.Repaint)]
649 [MWFCategory("Layout")]
660 if (maximum_size != value) {
661 maximum_size = value;
662 OnMaximumSizeChanged(EventArgs.Empty);
663 if (IsHandleCreated) {
664 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
671 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
672 public Form[] MdiChildren {
674 if (mdi_container != null)
675 return mdi_container.MdiChildren;
682 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
683 public Form MdiParent {
689 if (value != null && !value.IsMdiContainer)
690 throw new ArgumentException ("Form that was specified to be "
691 + "the MdiParent for this form is not an MdiContainer.");
693 if (mdi_parent != null) {
694 mdi_parent.MdiContainer.Controls.Remove (this);
699 if (window_manager == null) {
700 window_manager = new MdiWindowManager (this, mdi_parent.MdiContainer);
703 mdi_parent.MdiContainer.Controls.Add (this);
704 mdi_parent.MdiContainer.Controls.SetChildIndex (this, 0);
708 } else if (mdi_parent != null) {
711 // Create a new window manager
712 window_manager = null;
713 FormBorderStyle = form_border_style;
718 is_toplevel = mdi_parent == null;
722 internal MenuTracker ActiveTracker {
723 get { return active_tracker; }
725 if (value == active_tracker)
728 Capture = value != null;
729 active_tracker = value;
733 internal MdiClient MdiContainer {
734 get { return mdi_container; }
737 internal InternalWindowManager WindowManager {
738 get { return window_manager; }
743 [TypeConverter (typeof (ReferenceConverter))]
746 [MWFCategory("Window Style")]
747 public MainMenu Menu {
756 if (menu != null && !IsMdiChild) {
759 if (IsHandleCreated) {
760 XplatUI.SetMenu (window.Handle, menu);
763 if (clientsize_set != Size.Empty) {
764 SetClientSizeCore(clientsize_set.Width, clientsize_set.Height);
766 UpdateBounds (bounds.X, bounds.Y, bounds.Width, bounds.Height, ClientSize.Width, ClientSize.Height -
767 ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu, ClientSize.Width));
776 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
777 [EditorBrowsable(EditorBrowsableState.Advanced)]
778 public MainMenu MergedMenu {
780 if (!IsMdiChild || window_manager == null)
782 return ((MdiWindowManager) window_manager).MergedMenu;
786 // This is the menu in display and being used because of merging this can
787 // be different then the menu that is actually assosciated with the form
788 internal MainMenu ActiveMenu {
793 if (IsMdiContainer && mdi_container.Controls.Count > 0 &&
794 ((Form) mdi_container.Controls [0]).WindowState == FormWindowState.Maximized) {
795 MdiWindowManager wm = (MdiWindowManager) ((Form) mdi_container.Controls [0]).WindowManager;
796 return wm.MaximizedMenu;
799 Form amc = ActiveMdiChild;
800 if (amc == null || amc.Menu == null)
802 return amc.MergedMenu;
806 internal MdiWindowManager ActiveMaximizedMdiChild {
808 Form child = ActiveMdiChild;
811 if (child.WindowManager == null || child.window_state != FormWindowState.Maximized)
813 return (MdiWindowManager) child.WindowManager;
818 [MWFCategory("Window Style")]
819 public bool MinimizeBox {
824 if (minimize_box != value) {
825 minimize_box = value;
832 [DefaultValue("{Width=0, Height=0}")]
835 [RefreshProperties(RefreshProperties.Repaint)]
836 [MWFCategory("Layout")]
847 if (minimum_size != value) {
848 minimum_size = value;
850 if ((Size.Width < value.Width) || (Size.Height < value.Height)) {
851 Size = new Size(Math.Max(Size.Width, value.Width), Math.Max(Size.Height, value.Height));
855 OnMinimumSizeChanged(EventArgs.Empty);
856 if (IsHandleCreated) {
857 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
864 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
872 [TypeConverter(typeof(OpacityConverter))]
873 [MWFCategory("Window Style")]
874 public double Opacity {
876 if (IsHandleCreated) {
877 if ((XplatUI.SupportsTransparency () & TransparencySupport.Get) != 0)
878 return XplatUI.GetWindowTransparency (Handle);
887 AllowTransparency = true;
889 if (IsHandleCreated) {
891 if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
892 XplatUI.SetWindowTransparency(Handle, opacity, TransparencyKey);
899 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
900 public Form[] OwnedForms {
904 form_list = new Form[owned_forms.Count];
906 for (int i=0; i<owned_forms.Count; i++) {
907 form_list[i] = (Form)owned_forms[i];
915 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
922 if (owner != value) {
924 owner.RemoveOwnedForm(this);
928 owner.AddOwnedForm(this);
929 if (IsHandleCreated) {
930 if (owner != null && owner.IsHandleCreated) {
931 XplatUI.SetOwner(this.window.Handle, owner.window.Handle);
933 XplatUI.SetOwner(this.window.Handle, IntPtr.Zero);
941 [DefaultValue (true)]
942 public bool ShowIcon {
943 get { return this.show_icon; }
945 if (this.show_icon != value ) {
946 this.show_icon = value;
949 if (IsHandleCreated) {
950 XplatUI.SetIcon (this.Handle, value == true ? this.Icon : null);
951 XplatUI.InvalidateNC (this.Handle);
959 [MWFCategory("Window Style")]
960 public bool ShowInTaskbar {
962 return show_in_taskbar;
965 if (show_in_taskbar != value) {
966 show_in_taskbar = value;
967 if (IsHandleCreated) {
975 // new property so we can set the DesignerSerializationVisibility
976 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
978 public new Size Size {
979 get { return base.Size; }
980 set { base.Size = value; }
983 [DefaultValue(SizeGripStyle.Auto)]
984 [MWFCategory("Window Style")]
985 public SizeGripStyle SizeGripStyle {
987 return size_grip_style;
991 size_grip_style = value;
992 UpdateSizeGripVisible ();
996 [DefaultValue(FormStartPosition.WindowsDefaultLocation)]
998 [MWFCategory("Layout")]
999 public FormStartPosition StartPosition {
1001 return start_position;
1005 start_position = value;
1009 // new property so we can set EditorBrowsable to never
1011 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1012 [EditorBrowsable(EditorBrowsableState.Never)]
1013 public new int TabIndex {
1014 get { return base.TabIndex; }
1015 set { base.TabIndex = value; }
1020 [DefaultValue (true)]
1021 [DispIdAttribute (-516)]
1022 [EditorBrowsable(EditorBrowsableState.Never)]
1023 public new bool TabStop {
1024 get { return base.TabStop; }
1025 set { base.TabStop = value; }
1030 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1031 [EditorBrowsable(EditorBrowsableState.Advanced)]
1032 public bool TopLevel {
1034 return GetTopLevel();
1038 if (!value && IsMdiContainer)
1039 throw new ArgumentException ("MDI Container forms must be top level.");
1044 [DefaultValue(false)]
1045 [MWFCategory("Window Style")]
1046 public bool TopMost {
1052 if (topmost != value) {
1054 if (IsHandleCreated)
1055 XplatUI.SetTopmost(window.Handle, value);
1060 [MWFCategory("Window Style")]
1061 public Color TransparencyKey {
1063 return transparency_key;
1067 transparency_key = value;
1069 AllowTransparency = true;
1071 if (IsHandleCreated && (XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
1072 XplatUI.SetWindowTransparency(Handle, Opacity, transparency_key);
1076 [DefaultValue(FormWindowState.Normal)]
1077 [MWFCategory("Layout")]
1078 public FormWindowState WindowState {
1080 // Don't actually rely on the WM until we've been shown
1081 if (IsHandleCreated && shown_raised) {
1083 if (window_manager != null)
1084 return window_manager.GetWindowState ();
1086 FormWindowState new_state = XplatUI.GetWindowState(Handle);
1087 if (new_state != (FormWindowState)(-1))
1088 window_state = new_state;
1091 return window_state;
1095 FormWindowState old_state = window_state;
1096 window_state = value;
1097 if (IsHandleCreated && shown_raised) {
1099 if (window_manager != null) {
1100 window_manager.SetWindowState (old_state, value);
1104 XplatUI.SetWindowState(Handle, value);
1109 #endregion // Public Instance Properties
1111 #region Protected Instance Properties
1112 protected override CreateParams CreateParams {
1114 CreateParams cp = new CreateParams ();
1117 cp.ClassName = XplatUI.DefaultClassName;
1122 cp.Parent = IntPtr.Zero;
1123 cp.menu = ActiveMenu;
1126 if (Parent != null && !IsMdiChild) {
1127 // Parented forms always gets the specified location, no matter what
1131 switch (start_position) {
1132 case FormStartPosition.Manual:
1136 case FormStartPosition.CenterScreen:
1138 cp.X = Math.Max ((MdiParent.mdi_container.ClientSize.Width - Width) / 2, 0);
1139 cp.Y = Math.Max ((MdiParent.mdi_container.ClientSize.Height - Height) / 2, 0);
1141 cp.X = Math.Max ((Screen.PrimaryScreen.WorkingArea.Width - Width) / 2, 0);
1142 cp.Y = Math.Max ((Screen.PrimaryScreen.WorkingArea.Height - Height) / 2, 0);
1145 case FormStartPosition.CenterParent:
1146 case FormStartPosition.WindowsDefaultBounds:
1147 case FormStartPosition.WindowsDefaultLocation:
1148 cp.X = int.MinValue;
1149 cp.Y = int.MinValue;
1156 cp.Style = (int)(WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS);
1158 if (Parent != null) {
1159 cp.Parent = Parent.Handle;
1160 cp.Style |= (int) WindowStyles.WS_CHILD;
1164 cp.Style |= (int)(WindowStyles.WS_CHILD | WindowStyles.WS_CAPTION);
1165 if (Parent != null) {
1166 cp.Parent = Parent.Handle;
1169 cp.ExStyle |= (int) (WindowExStyles.WS_EX_WINDOWEDGE | WindowExStyles.WS_EX_MDICHILD);
1171 switch (FormBorderStyle) {
1172 case FormBorderStyle.None:
1174 case FormBorderStyle.FixedToolWindow:
1175 case FormBorderStyle.SizableToolWindow:
1176 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW;
1179 cp.Style |= (int) WindowStyles.WS_OVERLAPPEDWINDOW;
1184 switch (FormBorderStyle) {
1185 case FormBorderStyle.Fixed3D: {
1186 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1187 cp.ExStyle |= (int)WindowExStyles.WS_EX_CLIENTEDGE;
1191 case FormBorderStyle.FixedDialog: {
1192 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1193 cp.ExStyle |= (int)(WindowExStyles.WS_EX_DLGMODALFRAME | WindowExStyles.WS_EX_CONTROLPARENT);
1197 case FormBorderStyle.FixedSingle: {
1198 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1202 case FormBorderStyle.FixedToolWindow: {
1203 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1204 cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1208 case FormBorderStyle.Sizable: {
1209 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
1213 case FormBorderStyle.SizableToolWindow: {
1214 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
1215 cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1219 case FormBorderStyle.None: {
1225 switch(window_state) {
1226 case FormWindowState.Maximized: {
1227 cp.Style |= (int)WindowStyles.WS_MAXIMIZE;
1231 case FormWindowState.Minimized: {
1232 cp.Style |= (int)WindowStyles.WS_MINIMIZE;
1238 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOPMOST;
1241 if (ShowInTaskbar) {
1242 cp.ExStyle |= (int)WindowExStyles.WS_EX_APPWINDOW;
1246 cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
1250 cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
1254 cp.Style |= (int)WindowStyles.WS_SYSMENU;
1258 if (!this.show_icon) {
1259 cp.ExStyle |= (int)WindowExStyles.WS_EX_DLGMODALFRAME;
1263 if (HelpButton && !MaximizeBox && !MinimizeBox) {
1264 cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTEXTHELP;
1267 if (VisibleInternal && this.IsRecreating)
1268 cp.Style |= (int)WindowStyles.WS_VISIBLE;
1270 if (opacity < 1.0 || TransparencyKey != Color.Empty) {
1271 cp.ExStyle |= (int)WindowExStyles.WS_EX_LAYERED;
1274 if (!is_enabled && context == null) {
1275 cp.Style |= (int)(WindowStyles.WS_DISABLED);
1278 if (!ControlBox && Text == string.Empty) {
1279 cp.WindowStyle &= ~WindowStyles.WS_DLGFRAME;
1286 protected override ImeMode DefaultImeMode {
1288 return ImeMode.NoControl;
1292 protected override Size DefaultSize {
1294 return new Size (300, 300);
1298 protected Rectangle MaximizedBounds {
1300 if (maximized_bounds != Rectangle.Empty) {
1301 return maximized_bounds;
1303 return default_maximized_bounds;
1307 maximized_bounds = value;
1308 OnMaximizedBoundsChanged(EventArgs.Empty);
1309 if (IsHandleCreated) {
1310 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
1318 [MonoTODO ("Implemented for Win32, needs X11 implementation")]
1321 virtual bool ShowWithoutActivation {
1322 get { return false; }
1324 #endregion // Protected Instance Properties
1326 #region Public Static Methods
1328 [EditorBrowsable(EditorBrowsableState.Never)]
1329 [Obsolete ("This method has been deprecated. Use AutoScaleDimensions instead")]
1331 [EditorBrowsable(EditorBrowsableState.Advanced)]
1333 public static SizeF GetAutoScaleSize (Font font)
1335 return XplatUI.GetAutoScaleSize(font);
1338 #endregion // Public Static Methods
1340 #region Public Instance Methods
1341 internal SizeF GetAutoScaleSize (Graphics g, Font font)
1344 // The following constants come from the dotnet mailing list
1345 // discussion: http://discuss.develop.com/archives/wa.exe?A2=ind0203A&L=DOTNET&P=R3655
1347 // The magic number is "Its almost the length
1348 // of the string with a smattering added in
1349 // for compat with earlier code".
1352 string magic_string = "The quick brown fox jumped over the lazy dog.";
1353 double magic_number = 44.549996948242189;
1354 float width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
1356 return new SizeF (width, font.Height);
1359 public void Activate ()
1363 // The docs say activate only activates if our app is already active
1364 if (IsHandleCreated) {
1366 MdiParent.ActivateMdiChild (this);
1367 } else if (IsMdiContainer) {
1368 mdi_container.SendFocusToActiveChild ();
1370 active = ActiveForm;
1371 if ((active != null) && (this != active)) {
1372 XplatUI.Activate(window.Handle);
1378 public void AddOwnedForm(Form ownedForm) {
1379 if (!owned_forms.Contains(ownedForm)) {
1380 owned_forms.Add(ownedForm);
1382 ownedForm.Owner = this;
1385 public void Close () {
1392 XplatUI.SendMessage(this.Handle, Msg.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
1395 public void LayoutMdi(MdiLayout value) {
1396 if (mdi_container != null) {
1397 mdi_container.LayoutMdi(value);
1401 public void RemoveOwnedForm(Form ownedForm) {
1402 owned_forms.Remove(ownedForm);
1405 public void SetDesktopBounds(int x, int y, int width, int height) {
1406 DesktopBounds = new Rectangle(x, y, width, height);
1409 public void SetDesktopLocation(int x, int y) {
1410 DesktopLocation = new Point(x, y);
1414 public void Show (IWin32Window owner)
1419 this.Owner = Control.FromHandle (owner.Handle).TopLevelControl as Form;
1422 throw new InvalidOperationException ("The 'owner' cannot be the form being shown.");
1424 if (TopLevelControl != this) {
1425 throw new InvalidOperationException ("Forms that are not top level"
1426 + " forms cannot be displayed as a modal dialog. Remove the"
1427 + " form from any parent form before calling Show.");
1434 public DialogResult ShowDialog() {
1435 return ShowDialog(this.owner);
1438 public DialogResult ShowDialog(IWin32Window ownerWin32) {
1441 IntPtr capture_window;
1443 Form owner_to_be = null;
1445 if ((ownerWin32 == null) && (Application.MWFThread.Current.Context != null)) {
1446 ownerWin32 = Application.MWFThread.Current.Context.MainForm;
1449 if (ownerWin32 != null) {
1450 Control c = Control.FromHandle (ownerWin32.Handle);
1452 owner_to_be = c.TopLevelControl as Form;
1455 if (owner_to_be == this) {
1456 throw new ArgumentException ("Forms cannot own themselves or their owners.", "owner");
1460 throw new InvalidOperationException ("The form is already displayed as a modal dialog.");
1464 throw new InvalidOperationException ("Forms that are already "
1465 + " visible cannot be displayed as a modal dialog. Set the"
1466 + " form's visible property to false before calling"
1471 throw new InvalidOperationException ("Forms that are not enabled"
1472 + " cannot be displayed as a modal dialog. Set the form's"
1473 + " enabled property to true before calling ShowDialog.");
1476 if (TopLevelControl != this) {
1477 throw new InvalidOperationException ("Forms that are not top level"
1478 + " forms cannot be displayed as a modal dialog. Remove the"
1479 + " form from any parent form before calling ShowDialog.");
1482 if (owner_to_be != null)
1483 owner = owner_to_be;
1486 // Can't do this, will screw us in the modal loop
1487 form_parent_window.Parent = this.owner;
1490 // Release any captures
1491 XplatUI.GrabInfo(out capture_window, out confined, out area);
1492 if (capture_window != IntPtr.Zero) {
1493 XplatUI.UngrabWindow(capture_window);
1497 // Commented out; we instead let the Visible=true inside the runloop create the control
1498 // otherwise setting DialogResult inside any of the events that are triggered by the
1499 // create will not actually cause the form to not be displayed.
1500 // Leaving this comment here in case there was an actual purpose to creating the control
1502 if (!IsHandleCreated) {
1507 Application.RunLoop(true, new ApplicationContext(this));
1509 if (owner != null) {
1510 // Cannot use Activate(), it has a check for the current active window...
1511 XplatUI.Activate(owner.window.Handle);
1514 if (DialogResult != DialogResult.None) {
1515 return DialogResult;
1517 DialogResult = DialogResult.Cancel;
1518 return DialogResult.Cancel;
1521 public override string ToString() {
1522 return GetType().FullName.ToString() + ", Text: " + Text;
1524 #endregion // Public Instance Methods
1526 #region Protected Instance Methods
1527 protected void ActivateMdiChild(Form form) {
1528 if (!IsMdiContainer)
1530 mdi_container.ActivateChild (form);
1531 OnMdiChildActivate(EventArgs.Empty);
1534 [EditorBrowsable(EditorBrowsableState.Advanced)]
1535 protected override void AdjustFormScrollbars(bool displayScrollbars) {
1536 base.AdjustFormScrollbars (displayScrollbars);
1540 [EditorBrowsable(EditorBrowsableState.Never)]
1541 [Obsolete ("This method has been deprecated")] // XXX what to use instead?
1543 [EditorBrowsable(EditorBrowsableState.Advanced)]
1545 protected void ApplyAutoScaling()
1547 SizeF current_size_f = GetAutoScaleSize (DeviceContext, Font);
1548 Size current_size = new Size ((int) current_size_f.Width, (int) current_size_f.Height);
1552 if (current_size == autoscale_base_size)
1555 if (Environment.GetEnvironmentVariable ("MONO_MWF_SCALING") == "disable"){
1560 // I tried applying the Fudge height factor from:
1561 // http://blogs.msdn.com/mharsh/archive/2004/01/25/62621.aspx
1562 // but it makes things larger without looking better.
1564 if (current_size_f.Width != AutoScaleBaseSize.Width) {
1565 dx = current_size_f.Width / AutoScaleBaseSize.Width + 0.08f;
1570 if (current_size_f.Height != AutoScaleBaseSize.Height) {
1571 dy = current_size_f.Height / AutoScaleBaseSize.Height + 0.08f;
1578 AutoScaleBaseSize = current_size;
1581 protected void CenterToParent() {
1586 // MS creates the handle here.
1588 if (!IsHandleCreated)
1595 w = DefaultSize.Width;
1601 h = DefaultSize.Height;
1605 if (Parent != null) {
1607 } else if (owner != null) {
1611 if (owner != null) {
1612 this.Location = new Point(ctl.Left + ctl.Width / 2 - w /2, ctl.Top + ctl.Height / 2 - h / 2);
1616 protected void CenterToScreen() {
1621 // MS creates the handle here.
1623 if (!IsHandleCreated)
1630 w = DefaultSize.Width;
1636 h = DefaultSize.Height;
1639 XplatUI.GetDisplaySize(out DisplaySize);
1640 this.Location = new Point(DisplaySize.Width / 2 - w / 2, DisplaySize.Height / 2 - h / 2);
1643 [EditorBrowsable(EditorBrowsableState.Advanced)]
1644 protected override Control.ControlCollection CreateControlsInstance() {
1645 return base.CreateControlsInstance ();
1648 [EditorBrowsable(EditorBrowsableState.Advanced)]
1649 protected override void CreateHandle() {
1650 base.CreateHandle ();
1652 if (!IsHandleCreated) {
1656 Application.AddForm (this);
1660 if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
1661 if (allow_transparency) {
1662 XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
1666 XplatUI.SetWindowMinMax(window.Handle, maximized_bounds, minimum_size, maximum_size);
1667 if ((FormBorderStyle != FormBorderStyle.FixedDialog) && (icon != null)) {
1668 XplatUI.SetIcon(window.Handle, icon);
1671 if ((owner != null) && (owner.IsHandleCreated)) {
1672 XplatUI.SetOwner(window.Handle, owner.window.Handle);
1676 XplatUI.SetTopmost(window.Handle, topmost);
1679 for (int i = 0; i < owned_forms.Count; i++) {
1680 if (owned_forms[i].IsHandleCreated)
1681 XplatUI.SetOwner(owned_forms[i].window.Handle, window.Handle);
1684 if (window_manager != null) {
1685 if (window_state != FormWindowState.Normal) {
1686 window_manager.SetWindowState ((FormWindowState) int.MaxValue, window_state);
1688 XplatUI.RequestNCRecalc (window.Handle);
1693 [EditorBrowsable(EditorBrowsableState.Advanced)]
1694 protected override void DefWndProc(ref Message m) {
1695 base.DefWndProc (ref m);
1698 protected override void Dispose(bool disposing)
1700 for (int i = 0; i < owned_forms.Count; i++)
1701 ((Form)owned_forms[i]).Owner = null;
1703 owned_forms.Clear ();
1705 base.Dispose (disposing);
1707 Application.RemoveForm (this);
1710 [EditorBrowsable(EditorBrowsableState.Advanced)]
1711 protected virtual void OnActivated(EventArgs e)
1713 EventHandler eh = (EventHandler)(Events [ActivatedEvent]);
1718 [EditorBrowsable(EditorBrowsableState.Advanced)]
1719 protected virtual void OnClosed(EventArgs e) {
1720 EventHandler eh = (EventHandler)(Events [ClosedEvent]);
1725 // Consider calling FireClosingEvents instead of calling this directly.
1726 [EditorBrowsable (EditorBrowsableState.Advanced)]
1727 protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
1728 CancelEventHandler eh = (CancelEventHandler)(Events [ClosingEvent]);
1733 [EditorBrowsable(EditorBrowsableState.Advanced)]
1734 protected override void OnCreateControl() {
1735 base.OnCreateControl ();
1738 XplatUI.SetMenu(window.Handle, menu);
1741 OnLoad(EventArgs.Empty);
1743 // Send initial location
1744 OnLocationChanged(EventArgs.Empty);
1747 [EditorBrowsable(EditorBrowsableState.Advanced)]
1748 protected virtual void OnDeactivate(EventArgs e) {
1749 EventHandler eh = (EventHandler)(Events [DeactivateEvent]);
1754 [EditorBrowsable(EditorBrowsableState.Advanced)]
1755 protected override void OnFontChanged(EventArgs e) {
1756 base.OnFontChanged (e);
1759 [EditorBrowsable(EditorBrowsableState.Advanced)]
1760 protected override void OnHandleCreated(EventArgs e) {
1761 XplatUI.SetBorderStyle(window.Handle, form_border_style);
1762 base.OnHandleCreated (e);
1765 [EditorBrowsable(EditorBrowsableState.Advanced)]
1766 protected override void OnHandleDestroyed(EventArgs e) {
1767 base.OnHandleDestroyed (e);
1770 [EditorBrowsable(EditorBrowsableState.Advanced)]
1771 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
1772 InputLanguageChangedEventHandler eh = (InputLanguageChangedEventHandler)(Events [InputLanguageChangedEvent]);
1777 [EditorBrowsable(EditorBrowsableState.Advanced)]
1778 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
1779 InputLanguageChangingEventHandler eh = (InputLanguageChangingEventHandler)(Events [InputLanguageChangingEvent]);
1784 [EditorBrowsable(EditorBrowsableState.Advanced)]
1785 protected virtual void OnLoad(EventArgs e) {
1787 ApplyAutoScaling ();
1791 EventHandler eh = (EventHandler)(Events [LoadEvent]);
1796 switch (StartPosition) {
1797 case FormStartPosition.CenterScreen:
1798 this.CenterToScreen();
1800 case FormStartPosition.CenterParent:
1801 this.CenterToParent ();
1803 case FormStartPosition.Manual:
1804 Left = CreateParams.X;
1805 Top = CreateParams.Y;
1812 [EditorBrowsable(EditorBrowsableState.Advanced)]
1813 protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
1814 EventHandler eh = (EventHandler)(Events [MaximizedBoundsChangedEvent]);
1819 [EditorBrowsable(EditorBrowsableState.Advanced)]
1820 protected virtual void OnMaximumSizeChanged(EventArgs e) {
1821 EventHandler eh = (EventHandler)(Events [MaximumSizeChangedEvent]);
1826 [EditorBrowsable(EditorBrowsableState.Advanced)]
1827 protected virtual void OnMdiChildActivate(EventArgs e) {
1828 EventHandler eh = (EventHandler)(Events [MdiChildActivateEvent]);
1833 [EditorBrowsable(EditorBrowsableState.Advanced)]
1834 protected virtual void OnMenuComplete(EventArgs e) {
1835 EventHandler eh = (EventHandler)(Events [MenuCompleteEvent]);
1840 [EditorBrowsable(EditorBrowsableState.Advanced)]
1841 protected virtual void OnMenuStart(EventArgs e) {
1842 EventHandler eh = (EventHandler)(Events [MenuStartEvent]);
1847 [EditorBrowsable(EditorBrowsableState.Advanced)]
1848 protected virtual void OnMinimumSizeChanged(EventArgs e) {
1849 EventHandler eh = (EventHandler)(Events [MinimumSizeChangedEvent]);
1854 [EditorBrowsable(EditorBrowsableState.Advanced)]
1855 protected override void OnPaint (PaintEventArgs pevent) {
1856 base.OnPaint (pevent);
1858 if (size_grip != null) {
1859 size_grip.HandlePaint (this, pevent);
1863 [EditorBrowsable(EditorBrowsableState.Advanced)]
1864 protected override void OnResize(EventArgs e) {
1869 [EditorBrowsable(EditorBrowsableState.Advanced)]
1870 protected override void OnStyleChanged(EventArgs e) {
1871 base.OnStyleChanged (e);
1874 [EditorBrowsable(EditorBrowsableState.Advanced)]
1875 protected override void OnTextChanged(EventArgs e) {
1876 base.OnTextChanged (e);
1878 if (mdi_container != null)
1879 mdi_container.SetParentText(true);
1882 [EditorBrowsable(EditorBrowsableState.Advanced)]
1883 protected override void OnVisibleChanged(EventArgs e) {
1884 base.OnVisibleChanged (e);
1887 if (window_manager != null)
1888 window_manager.SetWindowState (WindowState, WindowState);
1892 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
1893 if (base.ProcessCmdKey (ref msg, keyData)) {
1897 // Give our menu a shot
1898 if (ActiveMenu != null) {
1899 return ActiveMenu.ProcessCmdKey(ref msg, keyData);
1905 case Keys.Control | Keys.F4:
1906 case Keys.Control | Keys.Shift | Keys.F4:
1909 case Keys.Control | Keys.Tab:
1910 case Keys.Control | Keys.F6:
1911 MdiParent.MdiContainer.ActivateNextChild ();
1913 case Keys.Control | Keys.Shift | Keys.Tab:
1914 case Keys.Control | Keys.Shift | Keys.F6:
1915 MdiParent.MdiContainer.ActivatePreviousChild ();
1923 // LAMESPEC - Not documented that Form overrides ProcessDialogChar; class-status showed
1924 [EditorBrowsable (EditorBrowsableState.Advanced)]
1925 protected override bool ProcessDialogChar(char charCode) {
1926 return base.ProcessDialogChar (charCode);
1929 protected override bool ProcessDialogKey(Keys keyData) {
1930 if ((keyData & Keys.Modifiers) == 0) {
1931 if (keyData == Keys.Enter) {
1932 IntPtr window = XplatUI.GetFocus ();
1933 Control c = Control.FromHandle (window);
1934 if (c is Button && c.FindForm () == this) {
1935 ((Button)c).PerformClick ();
1938 else if (accept_button != null) {
1939 accept_button.PerformClick();
1942 } else if (keyData == Keys.Escape && cancel_button != null) {
1943 cancel_button.PerformClick();
1947 return base.ProcessDialogKey(keyData);
1950 protected override bool ProcessKeyPreview(ref Message msg) {
1952 if (ProcessKeyEventArgs(ref msg)) {
1956 return base.ProcessKeyPreview (ref msg);
1959 protected override bool ProcessTabKey(bool forward) {
1960 return SelectNextControl(ActiveControl, forward, true, true, true);
1964 [EditorBrowsable(EditorBrowsableState.Never)]
1966 [EditorBrowsable(EditorBrowsableState.Advanced)]
1968 protected override void ScaleCore(float dx, float dy) {
1972 // We can't scale max or min windows
1973 if (WindowState == FormWindowState.Normal) {
1974 // We cannot call base since base also adjusts X/Y, but
1975 // a form is toplevel and doesn't move
1979 if (!GetStyle(ControlStyles.FixedWidth)) {
1980 size.Width = (int)(size.Width * dx);
1983 if (!GetStyle(ControlStyles.FixedHeight)) {
1984 size.Height = (int)(size.Height * dy);
1990 /* Now scale our children */
1991 Control [] controls = Controls.GetAllControls ();
1992 for (int i=0; i < controls.Length; i++) {
1993 controls[i].Scale(dx, dy);
2002 protected override void Select(bool directed, bool forward) {
2006 // MS causes the handle to be created here.
2007 if (!IsHandleCreated)
2008 if (!IsHandleCreated)
2012 base.SelectNextControl(null, forward, true, true, true);
2015 parent = this.ParentForm;
2016 if (parent != null) {
2017 parent.ActiveControl = this;
2023 [EditorBrowsable(EditorBrowsableState.Advanced)]
2024 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
2025 base.SetBoundsCore (x, y, width, height, specified);
2028 [EditorBrowsable(EditorBrowsableState.Advanced)]
2029 protected override void SetClientSizeCore(int x, int y) {
2030 if ((minimum_size.Width != 0) && (x < minimum_size.Width)) {
2031 x = minimum_size.Width;
2032 } else if ((maximum_size.Width != 0) && (x > maximum_size.Width)) {
2033 x = maximum_size.Width;
2036 if ((minimum_size.Height != 0) && (y < minimum_size.Height)) {
2037 y = minimum_size.Height;
2038 } else if ((maximum_size.Height != 0) && (y > maximum_size.Height)) {
2039 y = maximum_size.Height;
2042 Rectangle ClientRect = new Rectangle(0, 0, x, y);
2043 Rectangle WindowRect;
2044 CreateParams cp = this.CreateParams;
2046 clientsize_set = new Size(x, y);
2048 if (XplatUI.CalculateWindowRect(ref ClientRect, cp.Style, cp.ExStyle, cp.menu, out WindowRect)) {
2049 SetBounds(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
2053 [EditorBrowsable(EditorBrowsableState.Advanced)]
2054 protected override void SetVisibleCore(bool value)
2056 if (IsMdiChild && !MdiParent.Visible) {
2057 if (value != Visible) {
2058 MdiWindowManager wm = (MdiWindowManager) window_manager;
2059 wm.IsVisiblePending = value;
2060 OnVisibleChanged (EventArgs.Empty);
2064 is_changing_visible_state = true;
2065 has_been_visible = value || has_been_visible;
2066 base.SetVisibleCore (value);
2068 if (value && WindowState != FormWindowState.Normal)
2069 XplatUI.SendMessage (Handle, Msg.WM_SHOWWINDOW, (IntPtr)1, IntPtr.Zero);
2071 is_changing_visible_state = false;
2074 if (value && IsMdiContainer) {
2075 Form [] children = MdiChildren;
2076 for (int i = 0; i < children.Length; i++) {
2077 Form child = children [i];
2078 MdiWindowManager wm = (MdiWindowManager) child.window_manager;
2079 if (!child.IsHandleCreated && wm.IsVisiblePending) {
2080 wm.IsVisiblePending = false;
2081 child.Visible = true;
2086 if (value && IsMdiChild){
2088 ThemeEngine.Current.ManagedWindowSetButtonLocations (window_manager);
2091 // Shown event is only called once, the first time the form is made visible
2092 if (value && !shown_raised) {
2094 this.OnShown (EventArgs.Empty);
2096 shown_raised = true;
2100 protected override void UpdateDefaultButton() {
2101 base.UpdateDefaultButton ();
2104 [EditorBrowsable(EditorBrowsableState.Advanced)]
2105 protected override void WndProc(ref Message m) {
2107 Console.WriteLine(DateTime.Now.ToLongTimeString () + " Form {0} ({2}) received message {1}", window.Handle == IntPtr.Zero ? this.Text : XplatUI.Window(window.Handle), m.ToString (), Text);
2110 if (window_manager != null && window_manager.WndProc (ref m)) {
2114 switch((Msg)m.Msg) {
2115 case Msg.WM_DESTROY: {
2116 if (!RecreatingHandle)
2117 this.closing = true;
2119 base.WndProc(ref m);
2123 case Msg.WM_CLOSE: {
2124 Form act = Form.ActiveForm;
2125 // Don't close this form if there's another modal form visible.
2126 if (act != null && act != this && act.Modal == true) {
2127 // Check if any of the parents up the tree is the modal form,
2128 // in which case we can still close this form.
2129 Control current = this;
2130 while (current != null && current.Parent != act) {
2131 current = current.Parent;
2133 if (current == null || current.Parent != act) {
2138 if (mdi_container != null) {
2139 foreach (Form mdi_child in mdi_container.MdiChildren) {
2140 mdi_child.FireClosingEvents (CloseReason.MdiFormClosing);
2145 if (!FireClosingEvents (CloseReason.UserClosing)) {
2146 OnClosed (EventArgs.Empty);
2148 OnFormClosed (new FormClosedEventArgs (CloseReason.UserClosing));
2157 if (FireClosingEvents (CloseReason.UserClosing)) {
2158 DialogResult = DialogResult.None;
2162 OnClosed (EventArgs.Empty);
2164 OnFormClosed (new FormClosedEventArgs (CloseReason.UserClosing));
2177 case Msg.WM_WINDOWPOSCHANGED: {
2178 if (WindowState != FormWindowState.Minimized) {
2179 base.WndProc(ref m);
2185 case Msg.WM_SYSCOMMAND: {
2186 // Let *Strips know the app's title bar was clicked
2187 if (XplatUI.IsEnabled (Handle))
2188 ToolStripManager.FireAppClicked ();
2190 base.WndProc(ref m);
2195 case Msg.WM_ACTIVATE: {
2196 if (m.WParam != (IntPtr)WindowActiveFlags.WA_INACTIVE) {
2198 SelectActiveControl ();
2200 if (ActiveControl != null && !ActiveControl.Focused)
2201 SendControlFocus (ActiveControl);
2204 OnActivated(EventArgs.Empty);
2206 OnDeactivate(EventArgs.Empty);
2211 case Msg.WM_KILLFOCUS: {
2212 base.WndProc(ref m);
2216 case Msg.WM_SETFOCUS: {
2217 if (ActiveControl != null && ActiveControl != this) {
2218 ActiveControl.Focus();
2219 return; // FIXME - do we need to run base.WndProc, even though we just changed focus?
2221 if (IsMdiContainer) {
2222 mdi_container.SendFocusToActiveChild ();
2225 base.WndProc(ref m);
2230 case Msg.WM_NCHITTEST: {
2231 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
2232 int x = LowOrder ((int)m.LParam.ToInt32 ());
2233 int y = HighOrder ((int)m.LParam.ToInt32 ());
2235 XplatUI.ScreenToMenu (ActiveMenu.Wnd.window.Handle, ref x, ref y);
2237 // If point is under menu return HTMENU, it prevents Win32 to return HTMOVE.
2238 if ((x > 0) && (y > 0) && (x < ActiveMenu.Rect.Width) && (y < ActiveMenu.Rect.Height)) {
2239 m.Result = new IntPtr ((int)HitTest.HTMENU);
2244 base.WndProc (ref m);
2248 case Msg.WM_NCLBUTTONDOWN: {
2249 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
2250 ActiveMenu.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, Control.MousePosition.X, Control.MousePosition.Y, 0));
2253 if (ActiveMaximizedMdiChild != null) {
2254 if (ActiveMaximizedMdiChild.HandleMenuMouseDown (ActiveMenu,
2255 LowOrder ((int) m.LParam.ToInt32 ()),
2256 HighOrder ((int) m.LParam.ToInt32 ()))) {
2257 // Don't let base process this message, otherwise we won't
2258 // get a WM_NCLBUTTONUP.
2262 base.WndProc(ref m);
2266 case Msg.WM_NCLBUTTONUP: {
2267 if (ActiveMaximizedMdiChild != null) {
2268 ActiveMaximizedMdiChild.HandleMenuMouseUp (ActiveMenu,
2269 LowOrder ((int)m.LParam.ToInt32 ()),
2270 HighOrder ((int)m.LParam.ToInt32 ()));
2272 base.WndProc (ref m);
2276 case Msg.WM_NCMOUSELEAVE: {
2277 if (ActiveMaximizedMdiChild != null) {
2278 ActiveMaximizedMdiChild.HandleMenuMouseLeave(ActiveMenu,
2279 LowOrder((int)m.LParam.ToInt32()),
2280 HighOrder((int)m.LParam.ToInt32()));
2282 base.WndProc(ref m);
2286 case Msg.WM_NCMOUSEMOVE: {
2287 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
2288 ActiveMenu.OnMouseMove(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0));
2291 if (ActiveMaximizedMdiChild != null) {
2292 XplatUI.RequestAdditionalWM_NCMessages (Handle, false, true);
2293 ActiveMaximizedMdiChild.HandleMenuMouseMove (ActiveMenu,
2294 LowOrder ((int)m.LParam.ToInt32 ()),
2295 HighOrder ((int)m.LParam.ToInt32 ()));
2297 base.WndProc(ref m);
2301 case Msg.WM_NCPAINT: {
2302 if (ActiveMenu != null) {
2306 pe = XplatUI.PaintEventStart(Handle, false);
2307 pnt = XplatUI.GetMenuOrigin(window.Handle);
2309 // The entire menu has to be in the clip rectangle because the
2310 // control buttons are right-aligned and otherwise they would
2311 // stay painted when the window gets resized.
2312 Rectangle clip = new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0);
2313 clip = Rectangle.Union(clip, pe.ClipRectangle);
2315 pe.Graphics.SetClip(clip);
2317 ActiveMenu.Draw (pe, new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0));
2319 if (ActiveMaximizedMdiChild != null) {
2320 ActiveMaximizedMdiChild.DrawMaximizedButtons (ActiveMenu, pe);
2323 XplatUI.PaintEventEnd(Handle, false);
2326 base.WndProc(ref m);
2330 case Msg.WM_NCCALCSIZE: {
2331 XplatUIWin32.NCCALCSIZE_PARAMS ncp;
2333 if ((ActiveMenu != null) && (m.WParam == (IntPtr)1)) {
2334 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
2337 ncp.rgrc1.top += ThemeEngine.Current.CalcMenuBarSize (DeviceContext, ActiveMenu, ClientSize.Width);
2338 Marshal.StructureToPtr(ncp, m.LParam, true);
2344 case Msg.WM_MOUSEMOVE: {
2345 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2346 MouseEventArgs args;
2348 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
2349 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2350 active_tracker.OnMotion(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2353 base.WndProc(ref m);
2357 case Msg.WM_LBUTTONDOWN:
2358 case Msg.WM_MBUTTONDOWN:
2359 case Msg.WM_RBUTTONDOWN: {
2360 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2361 MouseEventArgs args;
2363 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
2364 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2366 if (!active_tracker.OnMouseDown (new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta))) {
2367 Point pt = new Point (args.X, args.Y);
2368 Control child_control = this.GetChildAtPoint (pt);
2369 if (child_control != null)
2370 XplatUI.SendMessage(child_control.Handle, (Msg) m.Msg, m.WParam, m.LParam);
2376 ToolStripManager.FireAppClicked ();
2378 base.WndProc (ref m);
2382 case Msg.WM_LBUTTONUP:
2383 case Msg.WM_MBUTTONUP:
2384 case Msg.WM_RBUTTONUP: {
2385 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2386 MouseEventArgs args;
2387 MouseButtons mb = FromParamToMouseButtons ((int) m.WParam.ToInt32());
2389 // We add in the button that was released (not sent in WParam)
2390 switch((Msg)m.Msg) {
2391 case Msg.WM_LBUTTONUP:
2392 mb |= MouseButtons.Left;
2394 case Msg.WM_MBUTTONUP:
2395 mb |= MouseButtons.Middle;
2397 case Msg.WM_RBUTTONUP:
2398 mb |= MouseButtons.Right;
2402 args = new MouseEventArgs (mb, mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2403 active_tracker.OnMouseUp(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2407 base.WndProc(ref m);
2411 case Msg.WM_GETMINMAXINFO: {
2414 if (m.LParam != IntPtr.Zero) {
2415 mmi = (MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO));
2417 default_maximized_bounds = new Rectangle(mmi.ptMaxPosition.x, mmi.ptMaxPosition.y, mmi.ptMaxSize.x, mmi.ptMaxSize.y);
2418 if (maximized_bounds != Rectangle.Empty) {
2419 mmi.ptMaxPosition.x = maximized_bounds.Left;
2420 mmi.ptMaxPosition.y = maximized_bounds.Top;
2421 mmi.ptMaxSize.x = maximized_bounds.Width;
2422 mmi.ptMaxSize.y = maximized_bounds.Height;
2425 if (minimum_size != Size.Empty) {
2426 mmi.ptMinTrackSize.x = minimum_size.Width;
2427 mmi.ptMinTrackSize.y = minimum_size.Height;
2430 if (maximum_size != Size.Empty) {
2431 mmi.ptMaxTrackSize.x = maximum_size.Width;
2432 mmi.ptMaxTrackSize.y = maximum_size.Height;
2434 Marshal.StructureToPtr(mmi, m.LParam, false);
2440 case Msg.WM_MOUSEACTIVATE: {
2441 // Let *Strips know the form or another control has been clicked
2442 if (XplatUI.IsEnabled (Handle))
2443 ToolStripManager.FireAppClicked ();
2445 base.WndProc (ref m);
2449 case Msg.WM_ACTIVATEAPP: {
2450 // Let *Strips know the app lost focus
2451 if (m.WParam == (IntPtr)0)
2452 if (XplatUI.IsEnabled (Handle))
2453 ToolStripManager.FireAppFocusChanged (this);
2455 base.WndProc (ref m);
2461 base.WndProc (ref m);
2466 #endregion // Protected Instance Methods
2468 internal override void FireEnter ()
2470 // do nothing - forms don't generate OnEnter
2473 internal override void FireLeave ()
2475 // do nothing - forms don't generate OnLeave
2478 internal void RemoveWindowManager ()
2480 window_manager = null;
2483 internal override void CheckAcceptButton()
2485 if (accept_button != null) {
2486 Button a_button = accept_button as Button;
2488 if (ActiveControl == a_button)
2491 if (ActiveControl is Button) {
2492 a_button.paint_as_acceptbutton = false;
2496 a_button.paint_as_acceptbutton = true;
2502 internal override bool ActivateOnShow { get { return !this.ShowWithoutActivation; } }
2505 static object ActivatedEvent = new object ();
2506 static object ClosedEvent = new object ();
2507 static object ClosingEvent = new object ();
2508 static object DeactivateEvent = new object ();
2509 static object InputLanguageChangedEvent = new object ();
2510 static object InputLanguageChangingEvent = new object ();
2511 static object LoadEvent = new object ();
2512 static object MaximizedBoundsChangedEvent = new object ();
2513 static object MaximumSizeChangedEvent = new object ();
2514 static object MdiChildActivateEvent = new object ();
2515 static object MenuCompleteEvent = new object ();
2516 static object MenuStartEvent = new object ();
2517 static object MinimumSizeChangedEvent = new object ();
2519 public event EventHandler Activated {
2520 add { Events.AddHandler (ActivatedEvent, value); }
2521 remove { Events.RemoveHandler (ActivatedEvent, value); }
2526 [EditorBrowsable (EditorBrowsableState.Never)]
2528 public event EventHandler Closed {
2529 add { Events.AddHandler (ClosedEvent, value); }
2530 remove { Events.RemoveHandler (ClosedEvent, value); }
2535 [EditorBrowsable (EditorBrowsableState.Never)]
2537 public event CancelEventHandler Closing {
2538 add { Events.AddHandler (ClosingEvent, value); }
2539 remove { Events.RemoveHandler (ClosingEvent, value); }
2542 public event EventHandler Deactivate {
2543 add { Events.AddHandler (DeactivateEvent, value); }
2544 remove { Events.RemoveHandler (DeactivateEvent, value); }
2547 public event InputLanguageChangedEventHandler InputLanguageChanged {
2548 add { Events.AddHandler (InputLanguageChangedEvent, value); }
2549 remove { Events.RemoveHandler (InputLanguageChangedEvent, value); }
2552 public event InputLanguageChangingEventHandler InputLanguageChanging {
2553 add { Events.AddHandler (InputLanguageChangingEvent, value); }
2554 remove { Events.RemoveHandler (InputLanguageChangingEvent, value); }
2557 public event EventHandler Load {
2558 add { Events.AddHandler (LoadEvent, value); }
2559 remove { Events.RemoveHandler (LoadEvent, value); }
2562 public event EventHandler MaximizedBoundsChanged {
2563 add { Events.AddHandler (MaximizedBoundsChangedEvent, value); }
2564 remove { Events.RemoveHandler (MaximizedBoundsChangedEvent, value); }
2567 public event EventHandler MaximumSizeChanged {
2568 add { Events.AddHandler (MaximumSizeChangedEvent, value); }
2569 remove { Events.RemoveHandler (MaximumSizeChangedEvent, value); }
2572 public event EventHandler MdiChildActivate {
2573 add { Events.AddHandler (MdiChildActivateEvent, value); }
2574 remove { Events.RemoveHandler (MdiChildActivateEvent, value); }
2580 public event EventHandler MenuComplete {
2581 add { Events.AddHandler (MenuCompleteEvent, value); }
2582 remove { Events.RemoveHandler (MenuCompleteEvent, value); }
2588 public event EventHandler MenuStart {
2589 add { Events.AddHandler (MenuStartEvent, value); }
2590 remove { Events.RemoveHandler (MenuStartEvent, value); }
2593 public event EventHandler MinimumSizeChanged {
2594 add { Events.AddHandler (MinimumSizeChangedEvent, value); }
2595 remove { Events.RemoveHandler (MinimumSizeChangedEvent, value); }
2600 [EditorBrowsable(EditorBrowsableState.Never)]
2601 public new event EventHandler TabIndexChanged {
2602 add { base.TabIndexChanged += value; }
2603 remove { base.TabIndexChanged -= value; }
2607 [SettingsBindable (true)]
2608 public override string Text {
2618 [SettingsBindable (true)]
2619 public new Point Location {
2621 return base.Location;
2625 base.Location = value;
2629 static object FormClosingEvent = new object ();
2630 static object FormClosedEvent = new object ();
2631 static object HelpButtonClickedEvent = new object ();
2632 static object ResizeEndEvent = new object ();
2633 static object ResizeBeginEvent = new object ();
2634 static object RightToLeftLayoutChangedEvent = new object ();
2635 static object ShownEvent = new object ();
2638 [EditorBrowsable (EditorBrowsableState.Always)]
2639 public new event EventHandler AutoSizeChanged {
2640 add { base.AutoSizeChanged += value; }
2641 remove { base.AutoSizeChanged -= value; }
2645 [EditorBrowsable (EditorBrowsableState.Always)]
2646 public new event EventHandler AutoValidateChanged {
2647 add { base.AutoValidateChanged += value; }
2648 remove { base.AutoValidateChanged -= value; }
2651 public event FormClosingEventHandler FormClosing {
2652 add { Events.AddHandler (FormClosingEvent, value); }
2653 remove { Events.RemoveHandler (FormClosingEvent, value); }
2656 public event FormClosedEventHandler FormClosed {
2657 add { Events.AddHandler (FormClosedEvent, value); }
2658 remove { Events.RemoveHandler (FormClosedEvent, value); }
2662 [EditorBrowsable (EditorBrowsableState.Always)]
2663 public event CancelEventHandler HelpButtonClicked {
2664 add { Events.AddHandler (HelpButtonClickedEvent, value); }
2665 remove { Events.RemoveHandler (HelpButtonClickedEvent, value); }
2669 [EditorBrowsable (EditorBrowsableState.Never)]
2670 public new event EventHandler MarginChanged {
2671 add { base.MarginChanged += value; }
2672 remove { base.MarginChanged -= value; }
2675 public event EventHandler RightToLeftLayoutChanged {
2676 add { Events.AddHandler (RightToLeftLayoutChangedEvent, value); }
2677 remove { Events.RemoveHandler (RightToLeftLayoutChangedEvent, value); }
2680 public event EventHandler ResizeBegin {
2681 add { Events.AddHandler (ResizeBeginEvent, value); }
2682 remove { Events.RemoveHandler (ResizeBeginEvent, value); }
2685 public event EventHandler ResizeEnd {
2686 add { Events.AddHandler (ResizeEndEvent, value); }
2687 remove { Events.RemoveHandler (ResizeEndEvent, value); }
2690 public event EventHandler Shown {
2691 add { Events.AddHandler (ShownEvent, value); }
2692 remove { Events.RemoveHandler (ShownEvent, value); }
2696 [EditorBrowsable (EditorBrowsableState.Never)]
2697 public new event EventHandler TabStopChanged {
2698 add { base.TabStopChanged += value; }
2699 remove { base.TabStopChanged -= value; }
2702 protected override void OnBackgroundImageChanged (EventArgs e)
2704 base.OnBackgroundImageChanged (e);
2707 protected override void OnBackgroundImageLayoutChanged (EventArgs e)
2709 base.OnBackgroundImageLayoutChanged (e);
2712 [EditorBrowsable (EditorBrowsableState.Advanced)]
2713 protected override void OnEnabledChanged (EventArgs e)
2715 base.OnEnabledChanged (e);
2718 [EditorBrowsable (EditorBrowsableState.Advanced)]
2719 protected override void OnEnter (EventArgs e)
2724 [EditorBrowsable (EditorBrowsableState.Advanced)]
2725 protected virtual void OnFormClosed (FormClosedEventArgs e) {
2726 FormClosedEventHandler eh = (FormClosedEventHandler)(Events[FormClosedEvent]);
2731 // Consider calling FireClosingEvents instead of calling this directly.
2732 [EditorBrowsable (EditorBrowsableState.Advanced)]
2733 protected virtual void OnFormClosing (FormClosingEventArgs e)
2735 FormClosingEventHandler eh = (FormClosingEventHandler)(Events [FormClosingEvent]);
2740 [MonoTODO ("Not hooked up to event")]
2741 [EditorBrowsable (EditorBrowsableState.Advanced)]
2742 protected virtual void OnHelpButtonClicked (CancelEventArgs e)
2744 CancelEventHandler eh = (CancelEventHandler)(Events[HelpButtonClickedEvent]);
2749 protected override void OnLayout (LayoutEventArgs levent)
2751 base.OnLayout (levent);
2754 [EditorBrowsable (EditorBrowsableState.Advanced)]
2755 protected virtual void OnResizeBegin (EventArgs e)
2757 EventHandler eh = (EventHandler) (Events [ResizeBeginEvent]);
2762 [EditorBrowsable (EditorBrowsableState.Advanced)]
2763 protected virtual void OnResizeEnd (EventArgs e)
2765 EventHandler eh = (EventHandler) (Events [ResizeEndEvent]);
2770 [EditorBrowsable (EditorBrowsableState.Advanced)]
2771 protected virtual void OnShown (EventArgs e)
2773 EventHandler eh = (EventHandler) (Events [ShownEvent]);
2778 #endregion // Events