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;
92 private MenuStrip main_menu_strip;
93 private bool show_icon = true;
94 private bool shown_raised; // The shown event is only raised once
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 #endregion // Private & Internal Methods
190 #region Public Classes
194 public new class ControlCollection : Control.ControlCollection {
197 public ControlCollection(Form owner) : base(owner) {
198 this.form_owner = owner;
201 public override void Add(Control value) {
202 if (Contains (value))
205 ((Form)value).owner=form_owner;
208 public override void Remove(Control value) {
209 ((Form)value).owner = null;
213 #endregion // Public Classes
215 #region Public Constructor & Destructor
218 SizeF current_scale = GetAutoScaleSize (DeviceContext, Font);
221 autoscale_base_size = new Size ((int)current_scale.Width, (int) current_scale.Height);
222 allow_transparency = false;
225 dialog_result = DialogResult.None;
226 start_position = FormStartPosition.WindowsDefaultLocation;
227 form_border_style = FormBorderStyle.Sizable;
228 window_state = FormWindowState.Normal;
233 minimum_size = Size.Empty;
234 maximum_size = Size.Empty;
235 clientsize_set = Size.Empty;
240 show_in_taskbar = true;
243 size_grip_style = SizeGripStyle.Auto;
244 maximized_bounds = Rectangle.Empty;
245 default_maximized_bounds = Rectangle.Empty;
246 owned_forms = new Form.ControlCollection(this);
247 transparency_key = Color.Empty;
249 // FIXME: this should disappear just as soon as the handle creation is done in the right place (here is too soon()
253 #endregion // Public Constructor & Destructor
255 #region Public Static Properties
257 public static Form ActiveForm {
261 active = FromHandle(XplatUI.GetActive());
263 if (active != null) {
264 if ( !(active is Form)) {
267 parent = active.Parent;
268 while (parent != null) {
269 if (parent is Form) {
272 parent = parent.Parent;
282 #endregion // Public Static Properties
284 #region Public Instance Properties
286 public IButtonControl AcceptButton {
288 return accept_button;
292 accept_button = value;
298 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
299 public bool AllowTransparency {
301 return allow_transparency;
305 if (value == allow_transparency) {
309 allow_transparency = value;
312 if (IsHandleCreated) {
313 if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
314 XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
317 UpdateStyles(); // Remove the WS_EX_LAYERED style
325 [EditorBrowsable (EditorBrowsableState.Never)]
326 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
327 [Obsolete ("This property has been deprecated in favor of AutoScaleMode.")]
331 [MWFCategory("Layout")]
332 public bool AutoScale {
343 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
344 [EditorBrowsable(EditorBrowsableState.Never)]
346 [EditorBrowsable(EditorBrowsableState.Advanced)]
350 public virtual Size AutoScaleBaseSize {
352 return autoscale_base_size;
356 autoscale_base_size = value;
361 public override bool AutoScroll {
363 return base.AutoScroll;
366 base.AutoScroll = value;
370 public override Color BackColor {
372 /* we don't let parents override our
373 default background color for forms.
374 this fixes the default color for mdi
376 if (background_color.IsEmpty)
377 return DefaultBackColor;
379 return background_color;
382 base.BackColor = value;
387 public IButtonControl CancelButton {
389 return cancel_button;
393 cancel_button = value;
394 if (cancel_button != null && cancel_button.DialogResult == DialogResult.None)
395 cancel_button.DialogResult = DialogResult.Cancel;
399 // new property so we can change the DesignerSerializationVisibility
400 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
402 public new Size ClientSize {
403 get { return base.ClientSize; }
404 set { base.ClientSize = value; }
408 [MWFCategory("Window Style")]
409 public bool ControlBox {
415 if (control_box != value) {
423 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
424 public Rectangle DesktopBounds {
426 return new Rectangle(Location, Size);
435 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
436 public Point DesktopLocation {
447 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
448 public DialogResult DialogResult {
450 return dialog_result;
454 if (value < DialogResult.None || value > DialogResult.No)
455 throw new InvalidEnumArgumentException ("value", (int) value,
456 typeof (DialogResult));
458 dialog_result = value;
459 closing = (dialog_result != DialogResult.None && is_modal);
463 [DefaultValue(FormBorderStyle.Sizable)]
465 [MWFCategory("Appearance")]
466 public FormBorderStyle FormBorderStyle {
468 return form_border_style;
471 form_border_style = value;
473 if (window_manager == null) {
474 if (IsHandleCreated) {
475 XplatUI.SetBorderStyle(window.Handle, form_border_style);
478 window_manager.UpdateBorderStyle (value);
484 this.Size = SizeFromClientSize (this.ClientSize);
486 XplatUI.InvalidateNC (this.Handle);
490 [DefaultValue(false)]
491 [MWFCategory("Window Style")]
492 public bool HelpButton {
498 if (help_button != value) {
507 [MWFCategory("Window Style")]
517 if (IsHandleCreated) {
518 XplatUI.SetIcon(Handle, icon == null ? default_icon : icon);
525 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
526 public bool IsMdiChild {
528 return mdi_parent != null;
532 [DefaultValue(false)]
533 [MWFCategory("Window Style")]
534 public bool IsMdiContainer {
536 return mdi_container != null;
540 if (value && mdi_container == null) {
541 mdi_container = new MdiClient ();
542 Controls.Add(mdi_container);
543 ControlAdded += new ControlEventHandler (ControlAddedHandler);
544 mdi_container.SendToBack ();
545 mdi_container.SetParentText (true);
546 } else if (!value && mdi_container != null) {
547 Controls.Remove(mdi_container);
548 mdi_container = null;
554 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
555 public Form ActiveMdiChild {
559 return (Form) mdi_container.ActiveMdiChild;
564 [EditorBrowsable(EditorBrowsableState.Advanced)]
565 public bool IsRestrictedWindow {
571 [DefaultValue(false)]
572 public bool KeyPreview {
583 [DefaultValue (null)]
584 [TypeConverter (typeof (ReferenceConverter))]
585 public MenuStrip MainMenuStrip {
586 get { return this.main_menu_strip; }
588 if (this.main_menu_strip != value) {
589 this.main_menu_strip = value;
590 this.main_menu_strip.RefreshMdiItems ();
595 [EditorBrowsable (EditorBrowsableState.Never)]
597 public new Padding Margin {
598 get { return base.Margin; }
599 set { base.Margin = value; }
604 [MWFCategory("Window Style")]
605 public bool MaximizeBox {
610 if (maximize_box != value) {
611 maximize_box = value;
612 if (IsHandleCreated) {
620 [DefaultValue("{Width=0, Height=0}")]
622 [RefreshProperties(RefreshProperties.Repaint)]
623 [MWFCategory("Layout")]
634 if (maximum_size != value) {
635 maximum_size = value;
636 OnMaximumSizeChanged(EventArgs.Empty);
637 if (IsHandleCreated) {
638 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
645 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
646 public Form[] MdiChildren {
648 if (mdi_container != null)
649 return mdi_container.MdiChildren;
656 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
657 public Form MdiParent {
663 if (value != null && !value.IsMdiContainer)
664 throw new ArgumentException ("Form that was specified to be "
665 + "the MdiParent for this form is not an MdiContainer.");
667 if (mdi_parent != null) {
668 mdi_parent.MdiContainer.Controls.Remove (this);
673 window_manager = new MdiWindowManager (this,
674 mdi_parent.MdiContainer);
675 mdi_parent.MdiContainer.Controls.Add (this);
676 mdi_parent.MdiContainer.Controls.SetChildIndex (this, 0);
680 } else if (mdi_parent != null) {
683 // Create a new window manager
684 window_manager = null;
685 FormBorderStyle = form_border_style;
692 internal MenuTracker ActiveTracker {
693 get { return active_tracker; }
695 if (value == active_tracker)
698 Capture = value != null;
699 active_tracker = value;
703 internal MdiClient MdiContainer {
704 get { return mdi_container; }
707 internal InternalWindowManager WindowManager {
708 get { return window_manager; }
713 [TypeConverter (typeof (ReferenceConverter))]
716 [MWFCategory("Window Style")]
717 public MainMenu Menu {
726 if (menu != null && !IsMdiChild) {
729 if (IsHandleCreated) {
730 XplatUI.SetMenu (window.Handle, menu);
733 if (clientsize_set != Size.Empty) {
734 SetClientSizeCore(clientsize_set.Width, clientsize_set.Height);
736 UpdateBounds (bounds.X, bounds.Y, bounds.Width, bounds.Height, ClientSize.Width, ClientSize.Height -
737 ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu, ClientSize.Width));
746 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
747 [EditorBrowsable(EditorBrowsableState.Advanced)]
748 public MainMenu MergedMenu {
750 if (!IsMdiChild || window_manager == null)
752 return ((MdiWindowManager) window_manager).MergedMenu;
756 // This is the menu in display and being used because of merging this can
757 // be different then the menu that is actually assosciated with the form
758 internal MainMenu ActiveMenu {
763 if (IsMdiContainer && mdi_container.Controls.Count > 0 &&
764 ((Form) mdi_container.Controls [0]).WindowState == FormWindowState.Maximized) {
765 MdiWindowManager wm = (MdiWindowManager) ((Form) mdi_container.Controls [0]).WindowManager;
766 return wm.MaximizedMenu;
769 Form amc = ActiveMdiChild;
770 if (amc == null || amc.Menu == null)
772 return amc.MergedMenu;
776 internal MdiWindowManager ActiveMaximizedMdiChild {
778 Form child = ActiveMdiChild;
781 if (child.WindowManager == null || child.window_state != FormWindowState.Maximized)
783 return (MdiWindowManager) child.WindowManager;
788 [MWFCategory("Window Style")]
789 public bool MinimizeBox {
794 if (minimize_box != value) {
795 minimize_box = value;
796 if (IsHandleCreated) {
805 [DefaultValue("{Width=0, Height=0}")]
808 [RefreshProperties(RefreshProperties.Repaint)]
809 [MWFCategory("Layout")]
820 if (minimum_size != value) {
821 minimum_size = value;
823 if ((Size.Width < value.Width) || (Size.Height < value.Height)) {
824 Size = new Size(Math.Max(Size.Width, value.Width), Math.Max(Size.Height, value.Height));
828 OnMinimumSizeChanged(EventArgs.Empty);
829 if (IsHandleCreated) {
830 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
837 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
845 [TypeConverter(typeof(OpacityConverter))]
846 [MWFCategory("Window Style")]
847 public double Opacity {
849 if (IsHandleCreated) {
850 if ((XplatUI.SupportsTransparency () & TransparencySupport.Get) != 0)
851 return XplatUI.GetWindowTransparency (Handle);
860 AllowTransparency = true;
862 if (IsHandleCreated) {
864 if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
865 XplatUI.SetWindowTransparency(Handle, opacity, TransparencyKey);
872 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
873 public Form[] OwnedForms {
877 form_list = new Form[owned_forms.Count];
879 for (int i=0; i<owned_forms.Count; i++) {
880 form_list[i] = (Form)owned_forms[i];
888 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
895 if (owner != value) {
897 owner.RemoveOwnedForm(this);
901 owner.AddOwnedForm(this);
902 if (IsHandleCreated) {
903 if (owner != null && owner.IsHandleCreated) {
904 XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
906 XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
914 [DefaultValue (true)]
915 public bool ShowIcon {
916 get { return this.show_icon; }
918 if (this.show_icon != value ) {
919 this.show_icon = value;
922 XplatUI.SetIcon (this.Handle, value == true ? this.Icon : null);
923 XplatUI.InvalidateNC (this.Handle);
930 [MWFCategory("Window Style")]
931 public bool ShowInTaskbar {
933 return show_in_taskbar;
936 if (show_in_taskbar != value) {
937 show_in_taskbar = value;
938 if (IsHandleCreated) {
946 // new property so we can set the DesignerSerializationVisibility
947 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
949 public new Size Size {
950 get { return base.Size; }
951 set { base.Size = value; }
954 [DefaultValue(SizeGripStyle.Auto)]
955 [MWFCategory("Window Style")]
956 public SizeGripStyle SizeGripStyle {
958 return size_grip_style;
962 size_grip_style = value;
963 UpdateSizeGripVisible ();
967 [DefaultValue(FormStartPosition.WindowsDefaultLocation)]
969 [MWFCategory("Layout")]
970 public FormStartPosition StartPosition {
972 return start_position;
976 if (start_position == FormStartPosition.WindowsDefaultLocation) { // Only do this if it's not set yet
977 start_position = value;
978 if (IsHandleCreated) {
979 switch(start_position) {
980 case FormStartPosition.CenterParent: {
985 case FormStartPosition.CenterScreen: {
990 case FormStartPosition.Manual: {
991 Left = CreateParams.X;
992 Top = CreateParams.Y;
1005 // new property so we can set EditorBrowsable to never
1007 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1008 [EditorBrowsable(EditorBrowsableState.Never)]
1009 public new int TabIndex {
1010 get { return base.TabIndex; }
1011 set { base.TabIndex = value; }
1016 [DefaultValue (true)]
1017 [DispIdAttribute (-516)]
1018 [EditorBrowsable(EditorBrowsableState.Never)]
1019 public new bool TabStop {
1020 get { return base.TabStop; }
1021 set { base.TabStop = value; }
1026 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1027 [EditorBrowsable(EditorBrowsableState.Advanced)]
1028 public bool TopLevel {
1030 return GetTopLevel();
1034 if (!value && IsMdiContainer)
1035 throw new ArgumentException ("MDI Container forms must be top level.");
1040 [DefaultValue(false)]
1041 [MWFCategory("Window Style")]
1042 public bool TopMost {
1048 if (topmost != value) {
1050 if (IsHandleCreated)
1051 XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, value);
1056 [MWFCategory("Window Style")]
1057 public Color TransparencyKey {
1059 return transparency_key;
1063 transparency_key = value;
1065 AllowTransparency = true;
1067 if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
1068 XplatUI.SetWindowTransparency(Handle, Opacity, transparency_key);
1072 [DefaultValue(FormWindowState.Normal)]
1073 [MWFCategory("Layout")]
1074 public FormWindowState WindowState {
1076 if (IsHandleCreated) {
1078 if (window_manager != null)
1079 return window_manager.GetWindowState ();
1081 FormWindowState new_state = XplatUI.GetWindowState(Handle);
1082 if (new_state != (FormWindowState)(-1))
1083 window_state = new_state;
1086 return window_state;
1090 FormWindowState old_state = window_state;
1091 window_state = value;
1092 if (IsHandleCreated) {
1094 if (window_manager != null) {
1095 window_manager.SetWindowState (old_state, value);
1099 XplatUI.SetWindowState(Handle, value);
1104 #endregion // Public Instance Properties
1106 #region Protected Instance Properties
1107 protected override CreateParams CreateParams {
1109 CreateParams cp = new CreateParams ();
1112 cp.ClassName = XplatUI.DefaultClassName;
1117 cp.Parent = IntPtr.Zero;
1118 cp.menu = ActiveMenu;
1120 if (start_position == FormStartPosition.WindowsDefaultLocation && !IsMdiChild) {
1121 cp.X = unchecked((int)0x80000000);
1122 cp.Y = unchecked((int)0x80000000);
1130 cp.Style = (int)(WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS);
1132 if (Parent != null) {
1133 cp.Parent = Parent.Handle;
1134 cp.Style |= (int) WindowStyles.WS_CHILD;
1138 cp.Style |= (int)(WindowStyles.WS_CHILD | WindowStyles.WS_CAPTION);
1139 if (Parent != null) {
1140 cp.Parent = Parent.Handle;
1143 cp.ExStyle |= (int) (WindowExStyles.WS_EX_WINDOWEDGE | WindowExStyles.WS_EX_MDICHILD);
1145 switch (FormBorderStyle) {
1146 case FormBorderStyle.None:
1148 case FormBorderStyle.FixedToolWindow:
1149 case FormBorderStyle.SizableToolWindow:
1150 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW;
1153 cp.Style |= (int) WindowStyles.WS_OVERLAPPEDWINDOW;
1158 switch (FormBorderStyle) {
1159 case FormBorderStyle.Fixed3D: {
1160 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1161 cp.ExStyle |= (int)WindowExStyles.WS_EX_CLIENTEDGE;
1165 case FormBorderStyle.FixedDialog: {
1166 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1167 cp.ExStyle |= (int)(WindowExStyles.WS_EX_DLGMODALFRAME | WindowExStyles.WS_EX_CONTROLPARENT);
1171 case FormBorderStyle.FixedSingle: {
1172 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1176 case FormBorderStyle.FixedToolWindow: {
1177 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1178 cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1182 case FormBorderStyle.Sizable: {
1183 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
1187 case FormBorderStyle.SizableToolWindow: {
1188 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
1189 cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1193 case FormBorderStyle.None: {
1199 switch(window_state) {
1200 case FormWindowState.Maximized: {
1201 cp.Style |= (int)WindowStyles.WS_MAXIMIZE;
1205 case FormWindowState.Minimized: {
1206 cp.Style |= (int)WindowStyles.WS_MINIMIZE;
1212 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOPMOST;
1215 if (ShowInTaskbar) {
1216 cp.ExStyle |= (int)WindowExStyles.WS_EX_APPWINDOW;
1220 cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
1224 cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
1228 cp.Style |= (int)WindowStyles.WS_SYSMENU;
1232 if (!this.show_icon) {
1233 cp.ExStyle |= (int)WindowExStyles.WS_EX_DLGMODALFRAME;
1237 if (HelpButton && !MaximizeBox && !MinimizeBox) {
1238 cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTEXTHELP;
1242 cp.Style |= (int)WindowStyles.WS_VISIBLE;
1244 if (opacity < 1.0 || TransparencyKey != Color.Empty) {
1245 cp.ExStyle |= (int)WindowExStyles.WS_EX_LAYERED;
1248 if (!is_enabled && context == null) {
1249 cp.Style |= (int)(WindowStyles.WS_DISABLED);
1256 protected override ImeMode DefaultImeMode {
1258 return ImeMode.NoControl;
1262 protected override Size DefaultSize {
1264 return new Size (300, 300);
1268 protected Rectangle MaximizedBounds {
1270 if (maximized_bounds != Rectangle.Empty) {
1271 return maximized_bounds;
1273 return default_maximized_bounds;
1277 maximized_bounds = value;
1278 OnMaximizedBoundsChanged(EventArgs.Empty);
1279 if (IsHandleCreated) {
1280 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
1285 [MonoTODO ("Implemented for Win32, needs X11 implementation")]
1286 protected virtual bool ShowWithoutActivation {
1287 get { return false; }
1290 #endregion // Protected Instance Properties
1292 #region Public Static Methods
1294 [EditorBrowsable(EditorBrowsableState.Never)]
1295 [Obsolete ("This method has been deprecated. Use AutoScaleDimensions instead")]
1297 [EditorBrowsable(EditorBrowsableState.Advanced)]
1299 public static SizeF GetAutoScaleSize (Font font)
1301 return XplatUI.GetAutoScaleSize(font);
1304 #endregion // Public Static Methods
1306 #region Public Instance Methods
1307 internal SizeF GetAutoScaleSize (Graphics g, Font font)
1310 // The following constants come from the dotnet mailing list
1311 // discussion: http://discuss.develop.com/archives/wa.exe?A2=ind0203A&L=DOTNET&P=R3655
1313 // The magic number is "Its almost the length
1314 // of the string with a smattering added in
1315 // for compat with earlier code".
1318 string magic_string = "The quick brown fox jumped over the lazy dog.";
1319 double magic_number = 44.549996948242189;
1320 float width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
1322 return new SizeF (width, font.Height);
1325 public void Activate ()
1329 // The docs say activate only activates if our app is already active
1330 if (IsHandleCreated) {
1332 MdiParent.ActivateMdiChild (this);
1333 } else if (IsMdiContainer) {
1334 mdi_container.SendFocusToActiveChild ();
1336 active = ActiveForm;
1337 if ((active != null) && (this != active)) {
1338 XplatUI.Activate(window.Handle);
1344 public void AddOwnedForm(Form ownedForm) {
1345 if (!owned_forms.Contains(ownedForm)) {
1346 owned_forms.Add(ownedForm);
1348 ownedForm.Owner = this;
1351 public void Close () {
1358 XplatUI.SendMessage(this.Handle, Msg.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
1361 public void LayoutMdi(MdiLayout value) {
1362 if (mdi_container != null) {
1363 mdi_container.LayoutMdi(value);
1367 public void RemoveOwnedForm(Form ownedForm) {
1368 owned_forms.Remove(ownedForm);
1371 public void SetDesktopBounds(int x, int y, int width, int height) {
1372 DesktopBounds = new Rectangle(x, y, width, height);
1375 public void SetDesktopLocation(int x, int y) {
1376 DesktopLocation = new Point(x, y);
1380 public void Show (IWin32Window owner)
1385 this.Owner = Control.FromHandle (owner.Handle).TopLevelControl as Form;
1388 throw new InvalidOperationException ("The 'owner' cannot be the form being shown.");
1394 public DialogResult ShowDialog() {
1395 return ShowDialog(this.owner);
1398 public DialogResult ShowDialog(IWin32Window ownerWin32) {
1401 IntPtr capture_window;
1405 if (ownerWin32 != null) {
1406 Control c = Control.FromHandle (ownerWin32.Handle);
1408 owner = c.TopLevelControl as Form;
1411 if (owner == this) {
1412 throw new ArgumentException ("Forms cannot own themselves or their owners.", "owner");
1416 throw new InvalidOperationException ("The form is already displayed as a modal dialog.");
1420 throw new InvalidOperationException ("Forms that are already "
1421 + " visible cannot be displayed as a modal dialog. Set the"
1422 + " form's visible property to false before calling"
1427 throw new InvalidOperationException ("Forms that are not enabled"
1428 + " cannot be displayed as a modal dialog. Set the form's"
1429 + " enabled property to true before calling ShowDialog.");
1432 if (TopLevelControl != this) {
1433 throw new InvalidOperationException ("Forms that are not top level"
1434 + " forms cannot be displayed as a modal dialog. Remove the"
1435 + " form from any parent form before calling ShowDialog.");
1439 // Can't do this, will screw us in the modal loop
1440 form_parent_window.Parent = this.owner;
1443 // Release any captures
1444 XplatUI.GrabInfo(out capture_window, out confined, out area);
1445 if (capture_window != IntPtr.Zero) {
1446 XplatUI.UngrabWindow(capture_window);
1450 // Commented out; we instead let the Visible=true inside the runloop create the control
1451 // otherwise setting DialogResult inside any of the events that are triggered by the
1452 // create will not actually cause the form to not be displayed.
1453 // Leaving this comment here in case there was an actual purpose to creating the control
1455 if (!IsHandleCreated) {
1460 Application.RunLoop(true, new ApplicationContext(this));
1462 if (owner != null) {
1463 // Cannot use Activate(), it has a check for the current active window...
1464 XplatUI.Activate(owner.window.Handle);
1467 if (DialogResult != DialogResult.None) {
1468 return DialogResult;
1470 DialogResult = DialogResult.Cancel;
1471 return DialogResult.Cancel;
1474 public override string ToString() {
1475 return GetType().FullName.ToString() + ", Text: " + Text;
1477 #endregion // Public Instance Methods
1479 #region Protected Instance Methods
1480 protected void ActivateMdiChild(Form form) {
1481 if (!IsMdiContainer)
1483 mdi_container.ActivateChild (form);
1484 OnMdiChildActivate(EventArgs.Empty);
1487 [EditorBrowsable(EditorBrowsableState.Advanced)]
1488 protected override void AdjustFormScrollbars(bool displayScrollbars) {
1489 base.AdjustFormScrollbars (displayScrollbars);
1493 [EditorBrowsable(EditorBrowsableState.Never)]
1494 [Obsolete ("This method has been deprecated")] // XXX what to use instead?
1496 [EditorBrowsable(EditorBrowsableState.Advanced)]
1498 protected void ApplyAutoScaling()
1500 SizeF current_size_f = GetAutoScaleSize (DeviceContext, Font);
1501 Size current_size = new Size ((int) current_size_f.Width, (int) current_size_f.Height);
1505 if (current_size == autoscale_base_size)
1508 if (Environment.GetEnvironmentVariable ("MONO_MWF_SCALING") == "disable"){
1513 // I tried applying the Fudge height factor from:
1514 // http://blogs.msdn.com/mharsh/archive/2004/01/25/62621.aspx
1515 // but it makes things larger without looking better.
1517 if (current_size_f.Width != AutoScaleBaseSize.Width) {
1518 dx = current_size_f.Width / AutoScaleBaseSize.Width + 0.08f;
1523 if (current_size_f.Height != AutoScaleBaseSize.Height) {
1524 dy = current_size_f.Height / AutoScaleBaseSize.Height + 0.08f;
1531 AutoScaleBaseSize = current_size;
1534 protected void CenterToParent() {
1542 w = DefaultSize.Width;
1548 h = DefaultSize.Height;
1552 if (Parent != null) {
1554 } else if (owner != null) {
1558 if (owner != null) {
1559 this.Location = new Point(ctl.Left + ctl.Width / 2 - w /2, ctl.Top + ctl.Height / 2 - h / 2);
1563 protected void CenterToScreen() {
1571 w = DefaultSize.Width;
1577 h = DefaultSize.Height;
1580 XplatUI.GetDisplaySize(out DisplaySize);
1581 this.Location = new Point(DisplaySize.Width / 2 - w / 2, DisplaySize.Height / 2 - h / 2);
1584 [EditorBrowsable(EditorBrowsableState.Advanced)]
1585 protected override Control.ControlCollection CreateControlsInstance() {
1586 return base.CreateControlsInstance ();
1589 [EditorBrowsable(EditorBrowsableState.Advanced)]
1590 protected override void CreateHandle() {
1591 base.CreateHandle ();
1593 Application.AddForm (this);
1597 if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
1598 if (allow_transparency) {
1599 XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
1604 if (this.ShowWithoutActivation)
1605 Hwnd.ObjectFromHandle (this.Handle).no_activate = true;
1608 XplatUI.SetWindowMinMax(window.Handle, maximized_bounds, minimum_size, maximum_size);
1609 if ((FormBorderStyle != FormBorderStyle.FixedDialog) && (icon != null)) {
1610 XplatUI.SetIcon(window.Handle, icon);
1613 if ((owner != null) && (owner.IsHandleCreated)) {
1614 XplatUI.SetTopmost(window.Handle, owner.window.Handle, true);
1617 for (int i = 0; i < owned_forms.Count; i++) {
1618 if (owned_forms[i].IsHandleCreated)
1619 XplatUI.SetTopmost(owned_forms[i].window.Handle, window.Handle, true);
1622 if (window_manager != null) {
1623 if (window_state != FormWindowState.Normal) {
1624 window_manager.SetWindowState (FormWindowState.Normal, window_state);
1626 XplatUI.RequestNCRecalc (window.Handle);
1631 [EditorBrowsable(EditorBrowsableState.Advanced)]
1632 protected override void DefWndProc(ref Message m) {
1633 base.DefWndProc (ref m);
1636 protected override void Dispose(bool disposing)
1638 for (int i = 0; i < owned_forms.Count; i++)
1639 ((Form)owned_forms[i]).Owner = null;
1641 owned_forms.Clear ();
1643 base.Dispose (disposing);
1645 Application.RemoveForm (this);
1648 [EditorBrowsable(EditorBrowsableState.Advanced)]
1649 protected virtual void OnActivated(EventArgs e)
1651 EventHandler eh = (EventHandler)(Events [ActivatedEvent]);
1656 [EditorBrowsable(EditorBrowsableState.Advanced)]
1657 protected virtual void OnClosed(EventArgs e) {
1658 EventHandler eh = (EventHandler)(Events [ClosedEvent]);
1663 // Consider calling FireClosingEvents instead of calling this directly.
1664 [EditorBrowsable (EditorBrowsableState.Advanced)]
1665 protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
1666 CancelEventHandler eh = (CancelEventHandler)(Events [ClosingEvent]);
1671 [EditorBrowsable(EditorBrowsableState.Advanced)]
1672 protected override void OnCreateControl() {
1673 base.OnCreateControl ();
1676 XplatUI.SetMenu(window.Handle, menu);
1679 OnLoad(EventArgs.Empty);
1681 // Send initial location
1682 OnLocationChanged(EventArgs.Empty);
1684 if (IsMdiContainer) {
1685 mdi_container.LayoutMdi (MdiLayout.Cascade);
1689 [EditorBrowsable(EditorBrowsableState.Advanced)]
1690 protected virtual void OnDeactivate(EventArgs e) {
1691 EventHandler eh = (EventHandler)(Events [DeactivateEvent]);
1696 [EditorBrowsable(EditorBrowsableState.Advanced)]
1697 protected override void OnFontChanged(EventArgs e) {
1698 base.OnFontChanged (e);
1701 [EditorBrowsable(EditorBrowsableState.Advanced)]
1702 protected override void OnHandleCreated(EventArgs e) {
1703 XplatUI.SetBorderStyle(window.Handle, form_border_style);
1704 base.OnHandleCreated (e);
1707 [EditorBrowsable(EditorBrowsableState.Advanced)]
1708 protected override void OnHandleDestroyed(EventArgs e) {
1709 base.OnHandleDestroyed (e);
1712 [EditorBrowsable(EditorBrowsableState.Advanced)]
1713 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
1714 InputLanguageChangedEventHandler eh = (InputLanguageChangedEventHandler)(Events [InputLanguageChangedEvent]);
1719 [EditorBrowsable(EditorBrowsableState.Advanced)]
1720 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
1721 InputLanguageChangingEventHandler eh = (InputLanguageChangingEventHandler)(Events [InputLanguageChangingEvent]);
1726 [EditorBrowsable(EditorBrowsableState.Advanced)]
1727 protected virtual void OnLoad(EventArgs e) {
1729 ApplyAutoScaling ();
1733 EventHandler eh = (EventHandler)(Events [LoadEvent]);
1738 switch (StartPosition) {
1739 case FormStartPosition.CenterScreen:
1740 this.CenterToScreen();
1742 case FormStartPosition.CenterParent:
1743 this.CenterToParent ();
1745 case FormStartPosition.Manual:
1746 Left = CreateParams.X;
1747 Top = CreateParams.Y;
1754 [EditorBrowsable(EditorBrowsableState.Advanced)]
1755 protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
1756 EventHandler eh = (EventHandler)(Events [MaximizedBoundsChangedEvent]);
1761 [EditorBrowsable(EditorBrowsableState.Advanced)]
1762 protected virtual void OnMaximumSizeChanged(EventArgs e) {
1763 EventHandler eh = (EventHandler)(Events [MaximumSizeChangedEvent]);
1768 [EditorBrowsable(EditorBrowsableState.Advanced)]
1769 protected virtual void OnMdiChildActivate(EventArgs e) {
1770 EventHandler eh = (EventHandler)(Events [MdiChildActivateEvent]);
1775 [EditorBrowsable(EditorBrowsableState.Advanced)]
1776 protected virtual void OnMenuComplete(EventArgs e) {
1777 EventHandler eh = (EventHandler)(Events [MenuCompleteEvent]);
1782 [EditorBrowsable(EditorBrowsableState.Advanced)]
1783 protected virtual void OnMenuStart(EventArgs e) {
1784 EventHandler eh = (EventHandler)(Events [MenuStartEvent]);
1789 [EditorBrowsable(EditorBrowsableState.Advanced)]
1790 protected virtual void OnMinimumSizeChanged(EventArgs e) {
1791 EventHandler eh = (EventHandler)(Events [MinimumSizeChangedEvent]);
1796 [EditorBrowsable(EditorBrowsableState.Advanced)]
1797 protected override void OnPaint (PaintEventArgs pevent) {
1798 base.OnPaint (pevent);
1800 if (size_grip != null) {
1801 size_grip.HandlePaint (this, pevent);
1805 [EditorBrowsable(EditorBrowsableState.Advanced)]
1806 protected override void OnResize(EventArgs e) {
1811 [EditorBrowsable(EditorBrowsableState.Advanced)]
1812 protected override void OnStyleChanged(EventArgs e) {
1813 base.OnStyleChanged (e);
1816 [EditorBrowsable(EditorBrowsableState.Advanced)]
1817 protected override void OnTextChanged(EventArgs e) {
1818 base.OnTextChanged (e);
1820 if (mdi_container != null)
1821 mdi_container.SetParentText(true);
1824 [EditorBrowsable(EditorBrowsableState.Advanced)]
1825 protected override void OnVisibleChanged(EventArgs e) {
1826 base.OnVisibleChanged (e);
1829 if (window_manager != null)
1830 window_manager.SetWindowState (WindowState, WindowState);
1834 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
1835 if (base.ProcessCmdKey (ref msg, keyData)) {
1839 // Give our menu a shot
1840 if (ActiveMenu != null) {
1841 return ActiveMenu.ProcessCmdKey(ref msg, keyData);
1847 case Keys.Control | Keys.F4:
1848 case Keys.Control | Keys.Shift | Keys.F4:
1851 case Keys.Control | Keys.Tab:
1852 case Keys.Control | Keys.F6:
1853 MdiParent.MdiContainer.ActivateNextChild ();
1855 case Keys.Control | Keys.Shift | Keys.Tab:
1856 case Keys.Control | Keys.Shift | Keys.F6:
1857 MdiParent.MdiContainer.ActivatePreviousChild ();
1865 // LAMESPEC - Not documented that Form overrides ProcessDialogChar; class-status showed
1866 [EditorBrowsable (EditorBrowsableState.Advanced)]
1867 protected override bool ProcessDialogChar(char charCode) {
1868 return base.ProcessDialogChar (charCode);
1871 protected override bool ProcessDialogKey(Keys keyData) {
1872 if ((keyData & Keys.Modifiers) == 0) {
1873 if (keyData == Keys.Enter) {
1874 IntPtr window = XplatUI.GetFocus ();
1875 Control c = Control.FromHandle (window);
1876 if (c is Button && c.FindForm () == this) {
1877 ((Button)c).PerformClick ();
1880 else if (accept_button != null) {
1881 accept_button.PerformClick();
1884 } else if (keyData == Keys.Escape && cancel_button != null) {
1885 cancel_button.PerformClick();
1889 return base.ProcessDialogKey(keyData);
1892 protected override bool ProcessKeyPreview(ref Message msg) {
1894 if (ProcessKeyEventArgs(ref msg)) {
1898 return base.ProcessKeyPreview (ref msg);
1901 protected override bool ProcessTabKey(bool forward) {
1902 return SelectNextControl(ActiveControl, forward, true, true, true);
1906 [EditorBrowsable(EditorBrowsableState.Never)]
1908 [EditorBrowsable(EditorBrowsableState.Advanced)]
1910 protected override void ScaleCore(float dx, float dy) {
1914 // We can't scale max or min windows
1915 if (WindowState == FormWindowState.Normal) {
1916 // We cannot call base since base also adjusts X/Y, but
1917 // a form is toplevel and doesn't move
1921 if (!GetStyle(ControlStyles.FixedWidth)) {
1922 size.Width = (int)(size.Width * dx);
1925 if (!GetStyle(ControlStyles.FixedHeight)) {
1926 size.Height = (int)(size.Height * dy);
1932 /* Now scale our children */
1933 Control [] controls = Controls.GetAllControls ();
1934 for (int i=0; i < controls.Length; i++) {
1935 controls[i].Scale(dx, dy);
1944 protected override void Select(bool directed, bool forward) {
1948 base.SelectNextControl(null, forward, true, true, true);
1951 parent = this.ParentForm;
1952 if (parent != null) {
1953 parent.ActiveControl = this;
1959 [EditorBrowsable(EditorBrowsableState.Advanced)]
1960 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
1961 base.SetBoundsCore (x, y, width, height, specified);
1964 [EditorBrowsable(EditorBrowsableState.Advanced)]
1965 protected override void SetClientSizeCore(int x, int y) {
1966 if ((minimum_size.Width != 0) && (x < minimum_size.Width)) {
1967 x = minimum_size.Width;
1968 } else if ((maximum_size.Width != 0) && (x > maximum_size.Width)) {
1969 x = maximum_size.Width;
1972 if ((minimum_size.Height != 0) && (y < minimum_size.Height)) {
1973 y = minimum_size.Height;
1974 } else if ((maximum_size.Height != 0) && (y > maximum_size.Height)) {
1975 y = maximum_size.Height;
1978 Rectangle ClientRect = new Rectangle(0, 0, x, y);
1979 Rectangle WindowRect;
1980 CreateParams cp = this.CreateParams;
1982 clientsize_set = new Size(x, y);
1984 if (XplatUI.CalculateWindowRect(ref ClientRect, cp.Style, cp.ExStyle, cp.menu, out WindowRect)) {
1985 SetBounds(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
1989 [EditorBrowsable(EditorBrowsableState.Advanced)]
1990 protected override void SetVisibleCore(bool value) {
1991 is_changing_visible_state = true;
1992 has_been_visible = value || has_been_visible;
1993 base.SetVisibleCore (value);
1994 is_changing_visible_state = false;
1997 // Shown event is only called once, the first time the form is made visible
1998 if (value && !shown_raised) {
1999 this.OnShown (EventArgs.Empty);
2000 shown_raised = true;
2005 protected override void UpdateDefaultButton() {
2006 base.UpdateDefaultButton ();
2009 [EditorBrowsable(EditorBrowsableState.Advanced)]
2010 protected override void WndProc(ref Message m) {
2012 Console.WriteLine(DateTime.Now.ToLongTimeString () + " Form {0} ({2}) received message {1}", window.Handle == IntPtr.Zero ? this.Text : XplatUI.Window(window.Handle), m.ToString (), Text);
2015 if (window_manager != null && window_manager.HandleMessage (ref m)) {
2019 switch((Msg)m.Msg) {
2020 case Msg.WM_DESTROY: {
2021 base.WndProc(ref m);
2022 if (!RecreatingHandle) {
2023 this.closing = true;
2028 case Msg.WM_CLOSE_INTERNAL: {
2033 case Msg.WM_CLOSE: {
2034 Form act = Form.ActiveForm;
2035 if (act != null && act != this && act.Modal == true) {
2039 if (mdi_container != null) {
2040 foreach (Form mdi_child in mdi_container.MdiChildren) {
2041 mdi_child.FireClosingEvents (CloseReason.MdiFormClosing);
2046 if (!FireClosingEvents (CloseReason.UserClosing)) {
2047 OnClosed (EventArgs.Empty);
2049 OnFormClosed (new FormClosedEventArgs (CloseReason.UserClosing));
2058 if (FireClosingEvents (CloseReason.UserClosing)) {
2059 DialogResult = DialogResult.None;
2063 OnClosed (EventArgs.Empty);
2065 OnFormClosed (new FormClosedEventArgs (CloseReason.UserClosing));
2075 case Msg.WM_WINDOWPOSCHANGED: {
2076 if (WindowState != FormWindowState.Minimized) {
2077 base.WndProc(ref m);
2083 case Msg.WM_SYSCOMMAND: {
2084 // Let *Strips know the app's title bar was clicked
2085 if (XplatUI.IsEnabled (Handle))
2086 ToolStripManager.FireAppClicked ();
2088 base.WndProc(ref m);
2093 case Msg.WM_ACTIVATE: {
2094 if (m.WParam != (IntPtr)WindowActiveFlags.WA_INACTIVE) {
2096 SelectActiveControl ();
2098 if (ActiveControl != null && !ActiveControl.Focused)
2099 SendControlFocus (ActiveControl);
2102 OnActivated(EventArgs.Empty);
2104 OnDeactivate(EventArgs.Empty);
2109 case Msg.WM_KILLFOCUS: {
2110 base.WndProc(ref m);
2114 case Msg.WM_SETFOCUS: {
2115 if (ActiveControl != null && ActiveControl != this) {
2116 ActiveControl.Focus();
2117 return; // FIXME - do we need to run base.WndProc, even though we just changed focus?
2119 if (IsMdiContainer) {
2120 mdi_container.SendFocusToActiveChild ();
2123 base.WndProc(ref m);
2128 case Msg.WM_NCHITTEST: {
2129 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
2130 int x = LowOrder ((int)m.LParam.ToInt32 ());
2131 int y = HighOrder ((int)m.LParam.ToInt32 ());
2133 XplatUI.ScreenToMenu (ActiveMenu.Wnd.window.Handle, ref x, ref y);
2135 // If point is under menu return HTMENU, it prevents Win32 to return HTMOVE.
2136 if ((x > 0) && (y > 0) && (x < ActiveMenu.Rect.Width) && (y < ActiveMenu.Rect.Height)) {
2137 m.Result = new IntPtr ((int)HitTest.HTMENU);
2142 base.WndProc (ref m);
2146 case Msg.WM_NCLBUTTONDOWN: {
2147 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
2148 ActiveMenu.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, Control.MousePosition.X, Control.MousePosition.Y, 0));
2151 if (ActiveMaximizedMdiChild != null) {
2152 if (ActiveMaximizedMdiChild.HandleMenuMouseDown (ActiveMenu,
2153 LowOrder ((int) m.LParam.ToInt32 ()),
2154 HighOrder ((int) m.LParam.ToInt32 ()))) {
2155 // Don't let base process this message, otherwise we won't
2156 // get a WM_NCLBUTTONUP.
2160 base.WndProc(ref m);
2164 case Msg.WM_NCLBUTTONUP: {
2165 if (ActiveMaximizedMdiChild != null) {
2166 ActiveMaximizedMdiChild.HandleMenuMouseUp (ActiveMenu,
2167 LowOrder ((int)m.LParam.ToInt32 ()),
2168 HighOrder ((int)m.LParam.ToInt32 ()));
2170 base.WndProc (ref m);
2174 case Msg.WM_NCMOUSELEAVE: {
2175 if (ActiveMaximizedMdiChild != null) {
2176 ActiveMaximizedMdiChild.HandleMenuMouseLeave(ActiveMenu,
2177 LowOrder((int)m.LParam.ToInt32()),
2178 HighOrder((int)m.LParam.ToInt32()));
2180 base.WndProc(ref m);
2184 case Msg.WM_NCMOUSEMOVE: {
2185 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
2186 ActiveMenu.OnMouseMove(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0));
2189 if (ActiveMaximizedMdiChild != null) {
2190 XplatUI.RequestAdditionalWM_NCMessages (Handle, false, true);
2191 ActiveMaximizedMdiChild.HandleMenuMouseMove (ActiveMenu,
2192 LowOrder ((int)m.LParam.ToInt32 ()),
2193 HighOrder ((int)m.LParam.ToInt32 ()));
2195 base.WndProc(ref m);
2199 case Msg.WM_NCPAINT: {
2200 if (ActiveMenu != null) {
2204 pe = XplatUI.PaintEventStart(Handle, false);
2205 pnt = XplatUI.GetMenuOrigin(window.Handle);
2207 // The entire menu has to be in the clip rectangle because the
2208 // control buttons are right-aligned and otherwise they would
2209 // stay painted when the window gets resized.
2210 Rectangle clip = new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0);
2211 clip = Rectangle.Union(clip, pe.ClipRectangle);
2213 pe.Graphics.SetClip(clip);
2215 ActiveMenu.Draw (pe, new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0));
2217 if (ActiveMaximizedMdiChild != null) {
2218 ActiveMaximizedMdiChild.DrawMaximizedButtons (ActiveMenu, pe);
2221 XplatUI.PaintEventEnd(Handle, false);
2224 base.WndProc(ref m);
2228 case Msg.WM_NCCALCSIZE: {
2229 XplatUIWin32.NCCALCSIZE_PARAMS ncp;
2231 if ((ActiveMenu != null) && (m.WParam == (IntPtr)1)) {
2232 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
2235 ncp.rgrc1.top += ThemeEngine.Current.CalcMenuBarSize (DeviceContext, ActiveMenu, ClientSize.Width);
2236 Marshal.StructureToPtr(ncp, m.LParam, true);
2242 case Msg.WM_MOUSEMOVE: {
2243 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2244 MouseEventArgs args;
2246 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
2247 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2248 active_tracker.OnMotion(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2251 base.WndProc(ref m);
2255 case Msg.WM_LBUTTONDOWN:
2256 case Msg.WM_MBUTTONDOWN:
2257 case Msg.WM_RBUTTONDOWN: {
2258 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2259 MouseEventArgs args;
2261 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
2262 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2263 active_tracker.OnMouseDown(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2267 ToolStripManager.FireAppClicked ();
2269 base.WndProc (ref m);
2273 case Msg.WM_LBUTTONUP:
2274 case Msg.WM_MBUTTONUP:
2275 case Msg.WM_RBUTTONUP: {
2276 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2277 MouseEventArgs args;
2278 MouseButtons mb = FromParamToMouseButtons ((int) m.WParam.ToInt32());
2280 // We add in the button that was released (not sent in WParam)
2281 switch((Msg)m.Msg) {
2282 case Msg.WM_LBUTTONUP:
2283 mb |= MouseButtons.Left;
2285 case Msg.WM_MBUTTONUP:
2286 mb |= MouseButtons.Middle;
2288 case Msg.WM_RBUTTONUP:
2289 mb |= MouseButtons.Right;
2293 args = new MouseEventArgs (mb, mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2294 active_tracker.OnMouseUp(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2298 base.WndProc(ref m);
2302 case Msg.WM_GETMINMAXINFO: {
2305 if (m.LParam != IntPtr.Zero) {
2306 mmi = (MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO));
2308 default_maximized_bounds = new Rectangle(mmi.ptMaxPosition.x, mmi.ptMaxPosition.y, mmi.ptMaxSize.x, mmi.ptMaxSize.y);
2309 if (maximized_bounds != Rectangle.Empty) {
2310 mmi.ptMaxPosition.x = maximized_bounds.Left;
2311 mmi.ptMaxPosition.y = maximized_bounds.Top;
2312 mmi.ptMaxSize.x = maximized_bounds.Width;
2313 mmi.ptMaxSize.y = maximized_bounds.Height;
2316 if (minimum_size != Size.Empty) {
2317 mmi.ptMinTrackSize.x = minimum_size.Width;
2318 mmi.ptMinTrackSize.y = minimum_size.Height;
2321 if (maximum_size != Size.Empty) {
2322 mmi.ptMaxTrackSize.x = maximum_size.Width;
2323 mmi.ptMaxTrackSize.y = maximum_size.Height;
2325 Marshal.StructureToPtr(mmi, m.LParam, false);
2331 case Msg.WM_MOUSEACTIVATE: {
2332 // Let *Strips know the form or another control has been clicked
2333 if (XplatUI.IsEnabled (Handle))
2334 ToolStripManager.FireAppClicked ();
2336 base.WndProc (ref m);
2340 case Msg.WM_ACTIVATEAPP: {
2341 // Let *Strips know the app lost focus
2342 if (m.WParam == (IntPtr)0)
2343 if (XplatUI.IsEnabled (Handle))
2344 ToolStripManager.FireAppFocusChanged (this);
2346 base.WndProc (ref m);
2352 base.WndProc (ref m);
2357 #endregion // Protected Instance Methods
2359 internal override void FireEnter ()
2361 // do nothing - forms don't generate OnEnter
2364 internal override void FireLeave ()
2366 // do nothing - forms don't generate OnLeave
2369 internal void RemoveWindowManager ()
2371 window_manager = null;
2374 internal override void CheckAcceptButton()
2376 if (accept_button != null) {
2377 Button a_button = accept_button as Button;
2379 if (ActiveControl == a_button)
2382 if (ActiveControl is Button) {
2383 a_button.paint_as_acceptbutton = false;
2387 a_button.paint_as_acceptbutton = true;
2394 static object ActivatedEvent = new object ();
2395 static object ClosedEvent = new object ();
2396 static object ClosingEvent = new object ();
2397 static object DeactivateEvent = new object ();
2398 static object InputLanguageChangedEvent = new object ();
2399 static object InputLanguageChangingEvent = new object ();
2400 static object LoadEvent = new object ();
2401 static object MaximizedBoundsChangedEvent = new object ();
2402 static object MaximumSizeChangedEvent = new object ();
2403 static object MdiChildActivateEvent = new object ();
2404 static object MenuCompleteEvent = new object ();
2405 static object MenuStartEvent = new object ();
2406 static object MinimumSizeChangedEvent = new object ();
2408 public event EventHandler Activated {
2409 add { Events.AddHandler (ActivatedEvent, value); }
2410 remove { Events.RemoveHandler (ActivatedEvent, value); }
2415 [EditorBrowsable (EditorBrowsableState.Never)]
2417 public event EventHandler Closed {
2418 add { Events.AddHandler (ClosedEvent, value); }
2419 remove { Events.RemoveHandler (ClosedEvent, value); }
2424 [EditorBrowsable (EditorBrowsableState.Never)]
2426 public event CancelEventHandler Closing {
2427 add { Events.AddHandler (ClosingEvent, value); }
2428 remove { Events.RemoveHandler (ClosingEvent, value); }
2431 public event EventHandler Deactivate {
2432 add { Events.AddHandler (DeactivateEvent, value); }
2433 remove { Events.RemoveHandler (DeactivateEvent, value); }
2436 public event InputLanguageChangedEventHandler InputLanguageChanged {
2437 add { Events.AddHandler (InputLanguageChangedEvent, value); }
2438 remove { Events.RemoveHandler (InputLanguageChangedEvent, value); }
2441 public event InputLanguageChangingEventHandler InputLanguageChanging {
2442 add { Events.AddHandler (InputLanguageChangingEvent, value); }
2443 remove { Events.RemoveHandler (InputLanguageChangingEvent, value); }
2446 public event EventHandler Load {
2447 add { Events.AddHandler (LoadEvent, value); }
2448 remove { Events.RemoveHandler (LoadEvent, value); }
2451 public event EventHandler MaximizedBoundsChanged {
2452 add { Events.AddHandler (MaximizedBoundsChangedEvent, value); }
2453 remove { Events.RemoveHandler (MaximizedBoundsChangedEvent, value); }
2456 public event EventHandler MaximumSizeChanged {
2457 add { Events.AddHandler (MaximumSizeChangedEvent, value); }
2458 remove { Events.RemoveHandler (MaximumSizeChangedEvent, value); }
2461 public event EventHandler MdiChildActivate {
2462 add { Events.AddHandler (MdiChildActivateEvent, value); }
2463 remove { Events.RemoveHandler (MdiChildActivateEvent, value); }
2469 public event EventHandler MenuComplete {
2470 add { Events.AddHandler (MenuCompleteEvent, value); }
2471 remove { Events.RemoveHandler (MenuCompleteEvent, value); }
2477 public event EventHandler MenuStart {
2478 add { Events.AddHandler (MenuStartEvent, value); }
2479 remove { Events.RemoveHandler (MenuStartEvent, value); }
2482 public event EventHandler MinimumSizeChanged {
2483 add { Events.AddHandler (MinimumSizeChangedEvent, value); }
2484 remove { Events.RemoveHandler (MinimumSizeChangedEvent, value); }
2489 [EditorBrowsable(EditorBrowsableState.Never)]
2490 public new event EventHandler TabIndexChanged {
2491 add { base.TabIndexChanged += value; }
2492 remove { base.TabIndexChanged -= value; }
2494 #endregion // Events
2497 [SettingsBindable (true)]
2498 public override string Text {
2508 [SettingsBindable (true)]
2509 public new Point Location {
2511 return base.Location;
2515 base.Location = value;
2519 static object FormClosingEvent = new object ();
2520 static object FormClosedEvent = new object ();
2521 static object HelpButtonClickedEvent = new object ();
2522 static object ResizeEndEvent = new object ();
2523 static object ResizeBeginEvent = new object ();
2524 static object RightToLeftLayoutChangedEvent = new object ();
2525 static object ShownEvent = new object ();
2528 [EditorBrowsable (EditorBrowsableState.Always)]
2529 public new event EventHandler AutoSizeChanged {
2530 add { base.AutoSizeChanged += value; }
2531 remove { base.AutoSizeChanged -= value; }
2535 [EditorBrowsable (EditorBrowsableState.Always)]
2536 public new event EventHandler AutoValidateChanged {
2537 add { base.AutoValidateChanged += value; }
2538 remove { base.AutoValidateChanged -= value; }
2541 public event FormClosingEventHandler FormClosing {
2542 add { Events.AddHandler (FormClosingEvent, value); }
2543 remove { Events.RemoveHandler (FormClosingEvent, value); }
2546 public event FormClosedEventHandler FormClosed {
2547 add { Events.AddHandler (FormClosedEvent, value); }
2548 remove { Events.RemoveHandler (FormClosedEvent, value); }
2552 [EditorBrowsable (EditorBrowsableState.Always)]
2553 public event CancelEventHandler HelpButtonClicked {
2554 add { Events.AddHandler (HelpButtonClickedEvent, value); }
2555 remove { Events.RemoveHandler (HelpButtonClickedEvent, value); }
2559 [EditorBrowsable (EditorBrowsableState.Never)]
2560 public new event EventHandler MarginChanged {
2561 add { base.MarginChanged += value; }
2562 remove { base.MarginChanged -= value; }
2565 public event EventHandler RightToLeftLayoutChanged {
2566 add { Events.AddHandler (RightToLeftLayoutChangedEvent, value); }
2567 remove { Events.RemoveHandler (RightToLeftLayoutChangedEvent, value); }
2570 public event EventHandler ResizeBegin {
2571 add { Events.AddHandler (ResizeBeginEvent, value); }
2572 remove { Events.RemoveHandler (ResizeBeginEvent, value); }
2575 public event EventHandler ResizeEnd {
2576 add { Events.AddHandler (ResizeEndEvent, value); }
2577 remove { Events.RemoveHandler (ResizeEndEvent, value); }
2580 public event EventHandler Shown {
2581 add { Events.AddHandler (ShownEvent, value); }
2582 remove { Events.RemoveHandler (ShownEvent, value); }
2586 [EditorBrowsable (EditorBrowsableState.Never)]
2587 public new event EventHandler TabStopChanged {
2588 add { base.TabStopChanged += value; }
2589 remove { base.TabStopChanged -= value; }
2592 protected override void OnBackgroundImageChanged (EventArgs e)
2594 base.OnBackgroundImageChanged (e);
2597 protected override void OnBackgroundImageLayoutChanged (EventArgs e)
2599 base.OnBackgroundImageLayoutChanged (e);
2602 [EditorBrowsable (EditorBrowsableState.Advanced)]
2603 protected override void OnEnabledChanged (EventArgs e)
2605 base.OnEnabledChanged (e);
2608 [EditorBrowsable (EditorBrowsableState.Advanced)]
2609 protected override void OnEnter (EventArgs e)
2614 [EditorBrowsable (EditorBrowsableState.Advanced)]
2615 protected virtual void OnFormClosed (FormClosedEventArgs e) {
2616 FormClosedEventHandler eh = (FormClosedEventHandler)(Events[FormClosedEvent]);
2621 // Consider calling FireClosingEvents instead of calling this directly.
2622 [EditorBrowsable (EditorBrowsableState.Advanced)]
2623 protected virtual void OnFormClosing (FormClosingEventArgs e)
2625 FormClosingEventHandler eh = (FormClosingEventHandler)(Events [FormClosingEvent]);
2630 [MonoTODO ("Not hooked up to event")]
2631 [EditorBrowsable (EditorBrowsableState.Advanced)]
2632 protected virtual void OnHelpButtonClicked (CancelEventArgs e)
2634 CancelEventHandler eh = (CancelEventHandler)(Events[HelpButtonClickedEvent]);
2639 protected override void OnLayout (LayoutEventArgs levent)
2641 base.OnLayout (levent);
2644 [EditorBrowsable (EditorBrowsableState.Advanced)]
2645 protected virtual void OnResizeBegin (EventArgs e)
2647 EventHandler eh = (EventHandler) (Events [ResizeBeginEvent]);
2652 [EditorBrowsable (EditorBrowsableState.Advanced)]
2653 protected virtual void OnResizeEnd (EventArgs e)
2655 EventHandler eh = (EventHandler) (Events [ResizeEndEvent]);
2660 [EditorBrowsable (EditorBrowsableState.Advanced)]
2661 protected virtual void OnShown (EventArgs e)
2663 EventHandler eh = (EventHandler) (Events [ShownEvent]);