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 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 ();
181 this.Controls.AddImplicit (size_grip);
183 size_grip.Width = SystemInformation.VerticalScrollBarWidth;
184 size_grip.Height = SystemInformation.HorizontalScrollBarHeight;
185 size_grip.Location = new Point (ClientSize.Width - size_grip.Width, ClientSize.Height - size_grip.Height);
186 size_grip.Visible = true;
190 #endregion // Private & Internal Methods
192 #region Public Classes
196 public new class ControlCollection : Control.ControlCollection {
199 public ControlCollection(Form owner) : base(owner) {
200 this.form_owner = owner;
203 public override void Add(Control value) {
204 if (Contains (value))
207 ((Form)value).owner=form_owner;
210 public override void Remove(Control value) {
211 ((Form)value).owner = null;
215 #endregion // Public Classes
217 #region Public Constructor & Destructor
220 SizeF current_scale = GetAutoScaleSize (DeviceContext, Font);
223 autoscale_base_size = new Size ((int)current_scale.Width, (int) current_scale.Height);
224 allow_transparency = false;
227 dialog_result = DialogResult.None;
228 start_position = FormStartPosition.WindowsDefaultLocation;
229 form_border_style = FormBorderStyle.Sizable;
230 window_state = FormWindowState.Normal;
235 minimum_size = Size.Empty;
236 maximum_size = Size.Empty;
237 clientsize_set = Size.Empty;
242 show_in_taskbar = true;
245 size_grip_style = SizeGripStyle.Auto;
246 maximized_bounds = Rectangle.Empty;
247 default_maximized_bounds = Rectangle.Empty;
248 owned_forms = new Form.ControlCollection(this);
249 transparency_key = Color.Empty;
251 // FIXME: this should disappear just as soon as the handle creation is done in the right place (here is too soon()
255 #endregion // Public Constructor & Destructor
257 #region Public Static Properties
259 public static Form ActiveForm {
263 active = FromHandle(XplatUI.GetActive());
265 if (active != null) {
266 if ( !(active is Form)) {
269 parent = active.Parent;
270 while (parent != null) {
271 if (parent is Form) {
274 parent = parent.Parent;
284 #endregion // Public Static Properties
286 #region Public Instance Properties
288 public IButtonControl AcceptButton {
290 return accept_button;
294 accept_button = value;
300 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
301 public bool AllowTransparency {
303 return allow_transparency;
307 if (value == allow_transparency) {
311 allow_transparency = value;
314 if (IsHandleCreated) {
315 if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
316 XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
319 UpdateStyles(); // Remove the WS_EX_LAYERED style
327 [EditorBrowsable (EditorBrowsableState.Never)]
328 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
329 [Obsolete ("This property has been deprecated in favor of AutoScaleMode.")]
333 [MWFCategory("Layout")]
334 public bool AutoScale {
345 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
346 [EditorBrowsable(EditorBrowsableState.Never)]
348 [EditorBrowsable(EditorBrowsableState.Advanced)]
352 public virtual Size AutoScaleBaseSize {
354 return autoscale_base_size;
358 autoscale_base_size = value;
363 public override bool AutoScroll {
365 return base.AutoScroll;
368 base.AutoScroll = value;
372 public override Color BackColor {
374 /* we don't let parents override our
375 default background color for forms.
376 this fixes the default color for mdi
378 if (background_color.IsEmpty)
379 return DefaultBackColor;
381 return background_color;
384 base.BackColor = value;
389 public IButtonControl CancelButton {
391 return cancel_button;
395 cancel_button = value;
396 if (cancel_button != null && cancel_button.DialogResult == DialogResult.None)
397 cancel_button.DialogResult = DialogResult.Cancel;
401 // new property so we can change the DesignerSerializationVisibility
402 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
404 public new Size ClientSize {
405 get { return base.ClientSize; }
406 set { base.ClientSize = value; }
410 [MWFCategory("Window Style")]
411 public bool ControlBox {
417 if (control_box != value) {
425 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
426 public Rectangle DesktopBounds {
428 return new Rectangle(Location, Size);
437 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
438 public Point DesktopLocation {
449 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
450 public DialogResult DialogResult {
452 return dialog_result;
456 if (value < DialogResult.None || value > DialogResult.No)
457 throw new InvalidEnumArgumentException ("value", (int) value,
458 typeof (DialogResult));
460 dialog_result = value;
461 closing = (dialog_result != DialogResult.None && is_modal);
465 [DefaultValue(FormBorderStyle.Sizable)]
467 [MWFCategory("Appearance")]
468 public FormBorderStyle FormBorderStyle {
470 return form_border_style;
473 form_border_style = value;
475 if (window_manager == null) {
476 if (IsHandleCreated) {
477 XplatUI.SetBorderStyle(window.Handle, form_border_style);
480 window_manager.UpdateBorderStyle (value);
486 this.Size = SizeFromClientSize (this.ClientSize);
488 XplatUI.InvalidateNC (this.Handle);
492 [DefaultValue(false)]
493 [MWFCategory("Window Style")]
494 public bool HelpButton {
500 if (help_button != value) {
509 [MWFCategory("Window Style")]
519 if (IsHandleCreated) {
520 XplatUI.SetIcon(Handle, icon == null ? default_icon : icon);
527 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
528 public bool IsMdiChild {
530 return mdi_parent != null;
534 [DefaultValue(false)]
535 [MWFCategory("Window Style")]
536 public bool IsMdiContainer {
538 return mdi_container != null;
542 if (value && mdi_container == null) {
543 mdi_container = new MdiClient ();
544 Controls.Add(mdi_container);
545 ControlAdded += new ControlEventHandler (ControlAddedHandler);
546 mdi_container.SendToBack ();
547 mdi_container.SetParentText (true);
548 } else if (!value && mdi_container != null) {
549 Controls.Remove(mdi_container);
550 mdi_container = null;
556 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
557 public Form ActiveMdiChild {
561 return (Form) mdi_container.ActiveMdiChild;
566 [EditorBrowsable(EditorBrowsableState.Advanced)]
567 public bool IsRestrictedWindow {
573 [DefaultValue(false)]
574 public bool KeyPreview {
585 [DefaultValue (null)]
586 [TypeConverter (typeof (ReferenceConverter))]
587 public MenuStrip MainMenuStrip {
588 get { return this.main_menu_strip; }
590 if (this.main_menu_strip != value) {
591 this.main_menu_strip = value;
592 this.main_menu_strip.RefreshMdiItems ();
597 [EditorBrowsable (EditorBrowsableState.Never)]
599 public new Padding Margin {
600 get { return base.Margin; }
601 set { base.Margin = value; }
606 [MWFCategory("Window Style")]
607 public bool MaximizeBox {
612 if (maximize_box != value) {
613 maximize_box = value;
614 if (IsHandleCreated) {
622 [DefaultValue("{Width=0, Height=0}")]
624 [RefreshProperties(RefreshProperties.Repaint)]
625 [MWFCategory("Layout")]
636 if (maximum_size != value) {
637 maximum_size = value;
638 OnMaximumSizeChanged(EventArgs.Empty);
639 if (IsHandleCreated) {
640 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
647 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
648 public Form[] MdiChildren {
650 if (mdi_container != null)
651 return mdi_container.MdiChildren;
658 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
659 public Form MdiParent {
665 if (value != null && !value.IsMdiContainer)
666 throw new ArgumentException ("Form that was specified to be "
667 + "the MdiParent for this form is not an MdiContainer.");
669 if (mdi_parent != null) {
670 mdi_parent.MdiContainer.Controls.Remove (this);
675 window_manager = new MdiWindowManager (this,
676 mdi_parent.MdiContainer);
677 mdi_parent.MdiContainer.Controls.Add (this);
678 mdi_parent.MdiContainer.Controls.SetChildIndex (this, 0);
682 } else if (mdi_parent != null) {
685 // Create a new window manager
686 window_manager = null;
687 FormBorderStyle = form_border_style;
694 internal MenuTracker ActiveTracker {
695 get { return active_tracker; }
697 if (value == active_tracker)
700 Capture = value != null;
701 active_tracker = value;
705 internal MdiClient MdiContainer {
706 get { return mdi_container; }
709 internal InternalWindowManager WindowManager {
710 get { return window_manager; }
715 [TypeConverter (typeof (ReferenceConverter))]
718 [MWFCategory("Window Style")]
719 public MainMenu Menu {
728 if (menu != null && !IsMdiChild) {
731 if (IsHandleCreated) {
732 XplatUI.SetMenu (window.Handle, menu);
735 if (clientsize_set != Size.Empty) {
736 SetClientSizeCore(clientsize_set.Width, clientsize_set.Height);
738 UpdateBounds (bounds.X, bounds.Y, bounds.Width, bounds.Height, ClientSize.Width, ClientSize.Height -
739 ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu, ClientSize.Width));
748 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
749 [EditorBrowsable(EditorBrowsableState.Advanced)]
750 public MainMenu MergedMenu {
752 if (!IsMdiChild || window_manager == null)
754 return ((MdiWindowManager) window_manager).MergedMenu;
758 // This is the menu in display and being used because of merging this can
759 // be different then the menu that is actually assosciated with the form
760 internal MainMenu ActiveMenu {
765 if (IsMdiContainer && mdi_container.Controls.Count > 0 &&
766 ((Form) mdi_container.Controls [0]).WindowState == FormWindowState.Maximized) {
767 MdiWindowManager wm = (MdiWindowManager) ((Form) mdi_container.Controls [0]).WindowManager;
768 return wm.MaximizedMenu;
771 Form amc = ActiveMdiChild;
772 if (amc == null || amc.Menu == null)
774 return amc.MergedMenu;
778 internal MdiWindowManager ActiveMaximizedMdiChild {
780 Form child = ActiveMdiChild;
783 if (child.WindowManager == null || child.window_state != FormWindowState.Maximized)
785 return (MdiWindowManager) child.WindowManager;
790 [MWFCategory("Window Style")]
791 public bool MinimizeBox {
796 if (minimize_box != value) {
797 minimize_box = value;
798 if (IsHandleCreated) {
807 [DefaultValue("{Width=0, Height=0}")]
810 [RefreshProperties(RefreshProperties.Repaint)]
811 [MWFCategory("Layout")]
822 if (minimum_size != value) {
823 minimum_size = value;
825 if ((Size.Width < value.Width) || (Size.Height < value.Height)) {
826 Size = new Size(Math.Max(Size.Width, value.Width), Math.Max(Size.Height, value.Height));
830 OnMinimumSizeChanged(EventArgs.Empty);
831 if (IsHandleCreated) {
832 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
839 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
847 [TypeConverter(typeof(OpacityConverter))]
848 [MWFCategory("Window Style")]
849 public double Opacity {
851 if (IsHandleCreated) {
852 if ((XplatUI.SupportsTransparency () & TransparencySupport.Get) != 0)
853 return XplatUI.GetWindowTransparency (Handle);
862 AllowTransparency = true;
864 if (IsHandleCreated) {
866 if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
867 XplatUI.SetWindowTransparency(Handle, opacity, TransparencyKey);
874 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
875 public Form[] OwnedForms {
879 form_list = new Form[owned_forms.Count];
881 for (int i=0; i<owned_forms.Count; i++) {
882 form_list[i] = (Form)owned_forms[i];
890 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
897 if (owner != value) {
899 owner.RemoveOwnedForm(this);
903 owner.AddOwnedForm(this);
904 if (IsHandleCreated) {
905 if (owner != null && owner.IsHandleCreated) {
906 XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
908 XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
916 [DefaultValue (true)]
917 public bool ShowIcon {
918 get { return this.show_icon; }
920 if (this.show_icon != value ) {
921 this.show_icon = value;
924 XplatUI.SetIcon (this.Handle, value == true ? this.Icon : null);
925 XplatUI.InvalidateNC (this.Handle);
932 [MWFCategory("Window Style")]
933 public bool ShowInTaskbar {
935 return show_in_taskbar;
938 if (show_in_taskbar != value) {
939 show_in_taskbar = value;
940 if (IsHandleCreated) {
948 // new property so we can set the DesignerSerializationVisibility
949 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
951 public new Size Size {
952 get { return base.Size; }
953 set { base.Size = value; }
956 [DefaultValue(SizeGripStyle.Auto)]
957 [MWFCategory("Window Style")]
958 public SizeGripStyle SizeGripStyle {
960 return size_grip_style;
964 size_grip_style = value;
965 UpdateSizeGripVisible ();
969 [DefaultValue(FormStartPosition.WindowsDefaultLocation)]
971 [MWFCategory("Layout")]
972 public FormStartPosition StartPosition {
974 return start_position;
978 if (start_position == FormStartPosition.WindowsDefaultLocation) { // Only do this if it's not set yet
979 start_position = value;
980 if (IsHandleCreated) {
981 switch(start_position) {
982 case FormStartPosition.CenterParent: {
987 case FormStartPosition.CenterScreen: {
992 case FormStartPosition.Manual: {
993 Left = CreateParams.X;
994 Top = CreateParams.Y;
1007 // new property so we can set EditorBrowsable to never
1009 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1010 [EditorBrowsable(EditorBrowsableState.Never)]
1011 public new int TabIndex {
1012 get { return base.TabIndex; }
1013 set { base.TabIndex = value; }
1018 [DefaultValue (true)]
1019 [DispIdAttribute (-516)]
1020 [EditorBrowsable(EditorBrowsableState.Never)]
1021 public new bool TabStop {
1022 get { return base.TabStop; }
1023 set { base.TabStop = value; }
1028 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1029 [EditorBrowsable(EditorBrowsableState.Advanced)]
1030 public bool TopLevel {
1032 return GetTopLevel();
1036 if (!value && IsMdiContainer)
1037 throw new ArgumentException ("MDI Container forms must be top level.");
1042 [DefaultValue(false)]
1043 [MWFCategory("Window Style")]
1044 public bool TopMost {
1050 if (topmost != value) {
1052 if (IsHandleCreated)
1053 XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, value);
1058 [MWFCategory("Window Style")]
1059 public Color TransparencyKey {
1061 return transparency_key;
1065 transparency_key = value;
1067 AllowTransparency = true;
1069 if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
1070 XplatUI.SetWindowTransparency(Handle, Opacity, transparency_key);
1074 [DefaultValue(FormWindowState.Normal)]
1075 [MWFCategory("Layout")]
1076 public FormWindowState WindowState {
1078 if (IsHandleCreated) {
1080 if (window_manager != null)
1081 return window_manager.GetWindowState ();
1083 FormWindowState new_state = XplatUI.GetWindowState(Handle);
1084 if (new_state != (FormWindowState)(-1))
1085 window_state = new_state;
1088 return window_state;
1092 FormWindowState old_state = window_state;
1093 window_state = value;
1094 if (IsHandleCreated) {
1096 if (window_manager != null) {
1097 window_manager.SetWindowState (old_state, value);
1101 XplatUI.SetWindowState(Handle, value);
1106 #endregion // Public Instance Properties
1108 #region Protected Instance Properties
1109 protected override CreateParams CreateParams {
1111 CreateParams cp = new CreateParams ();
1114 cp.ClassName = XplatUI.DefaultClassName;
1119 cp.Parent = IntPtr.Zero;
1120 cp.menu = ActiveMenu;
1122 if (start_position == FormStartPosition.WindowsDefaultLocation && !IsMdiChild) {
1123 cp.X = unchecked((int)0x80000000);
1124 cp.Y = unchecked((int)0x80000000);
1132 cp.Style = (int)(WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS);
1135 cp.Style |= (int)(WindowStyles.WS_CHILD | WindowStyles.WS_CAPTION);
1136 if (Parent != null) {
1137 cp.Parent = Parent.Handle;
1140 cp.ExStyle |= (int) (WindowExStyles.WS_EX_WINDOWEDGE | WindowExStyles.WS_EX_MDICHILD);
1142 switch (FormBorderStyle) {
1143 case FormBorderStyle.None:
1145 case FormBorderStyle.FixedToolWindow:
1146 case FormBorderStyle.SizableToolWindow:
1147 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW;
1150 cp.Style |= (int) WindowStyles.WS_OVERLAPPEDWINDOW;
1155 switch (FormBorderStyle) {
1156 case FormBorderStyle.Fixed3D: {
1157 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1158 cp.ExStyle |= (int)WindowExStyles.WS_EX_CLIENTEDGE;
1162 case FormBorderStyle.FixedDialog: {
1163 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1164 cp.ExStyle |= (int)(WindowExStyles.WS_EX_DLGMODALFRAME | WindowExStyles.WS_EX_CONTROLPARENT);
1168 case FormBorderStyle.FixedSingle: {
1169 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1173 case FormBorderStyle.FixedToolWindow: {
1174 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1175 cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1179 case FormBorderStyle.Sizable: {
1180 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
1184 case FormBorderStyle.SizableToolWindow: {
1185 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
1186 cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1190 case FormBorderStyle.None: {
1196 switch(window_state) {
1197 case FormWindowState.Maximized: {
1198 cp.Style |= (int)WindowStyles.WS_MAXIMIZE;
1202 case FormWindowState.Minimized: {
1203 cp.Style |= (int)WindowStyles.WS_MINIMIZE;
1209 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOPMOST;
1212 if (ShowInTaskbar) {
1213 cp.ExStyle |= (int)WindowExStyles.WS_EX_APPWINDOW;
1217 cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
1221 cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
1225 cp.Style |= (int)WindowStyles.WS_SYSMENU;
1229 if (!this.show_icon) {
1230 cp.ExStyle |= (int)WindowExStyles.WS_EX_DLGMODALFRAME;
1234 if (HelpButton && !MaximizeBox && !MinimizeBox) {
1235 cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTEXTHELP;
1239 cp.Style |= (int)WindowStyles.WS_VISIBLE;
1241 if (opacity < 1.0 || TransparencyKey != Color.Empty) {
1242 cp.ExStyle |= (int)WindowExStyles.WS_EX_LAYERED;
1245 if (!is_enabled && context == null) {
1246 cp.Style |= (int)(WindowStyles.WS_DISABLED);
1253 protected override ImeMode DefaultImeMode {
1255 return ImeMode.NoControl;
1259 protected override Size DefaultSize {
1261 return new Size (300, 300);
1265 protected Rectangle MaximizedBounds {
1267 if (maximized_bounds != Rectangle.Empty) {
1268 return maximized_bounds;
1270 return default_maximized_bounds;
1274 maximized_bounds = value;
1275 OnMaximizedBoundsChanged(EventArgs.Empty);
1276 if (IsHandleCreated) {
1277 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
1281 #endregion // Protected Instance Properties
1283 #region Public Static Methods
1285 [EditorBrowsable(EditorBrowsableState.Never)]
1286 [Obsolete ("This method has been deprecated. Use AutoScaleDimensions instead")]
1288 [EditorBrowsable(EditorBrowsableState.Advanced)]
1290 public static SizeF GetAutoScaleSize (Font font)
1292 return XplatUI.GetAutoScaleSize(font);
1295 #endregion // Public Static Methods
1297 #region Public Instance Methods
1298 internal SizeF GetAutoScaleSize (Graphics g, Font font)
1301 // The following constants come from the dotnet mailing list
1302 // discussion: http://discuss.develop.com/archives/wa.exe?A2=ind0203A&L=DOTNET&P=R3655
1304 // The magic number is "Its almost the length
1305 // of the string with a smattering added in
1306 // for compat with earlier code".
1309 string magic_string = "The quick brown fox jumped over the lazy dog.";
1310 double magic_number = 44.549996948242189;
1311 float width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
1313 return new SizeF (width, font.Height);
1316 public void Activate ()
1320 // The docs say activate only activates if our app is already active
1321 if (IsHandleCreated) {
1323 MdiParent.ActivateMdiChild (this);
1324 } else if (IsMdiContainer) {
1325 mdi_container.SendFocusToActiveChild ();
1327 active = ActiveForm;
1328 if ((active != null) && (this != active)) {
1329 XplatUI.Activate(window.Handle);
1335 public void AddOwnedForm(Form ownedForm) {
1336 if (!owned_forms.Contains(ownedForm)) {
1337 owned_forms.Add(ownedForm);
1339 ownedForm.Owner = this;
1342 public void Close () {
1349 XplatUI.SendMessage(this.Handle, Msg.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
1352 public void LayoutMdi(MdiLayout value) {
1353 if (mdi_container != null) {
1354 mdi_container.LayoutMdi(value);
1358 public void RemoveOwnedForm(Form ownedForm) {
1359 owned_forms.Remove(ownedForm);
1362 public void SetDesktopBounds(int x, int y, int width, int height) {
1363 DesktopBounds = new Rectangle(x, y, width, height);
1366 public void SetDesktopLocation(int x, int y) {
1367 DesktopLocation = new Point(x, y);
1371 public void Show (IWin32Window owner)
1376 this.Owner = Control.FromHandle (owner.Handle).TopLevelControl as Form;
1379 throw new InvalidOperationException ("The 'owner' cannot be the form being shown.");
1385 public DialogResult ShowDialog() {
1386 return ShowDialog(this.owner);
1389 public DialogResult ShowDialog(IWin32Window ownerWin32) {
1392 IntPtr capture_window;
1396 if (ownerWin32 != null) {
1397 Control c = Control.FromHandle (ownerWin32.Handle);
1399 owner = c.TopLevelControl as Form;
1402 if (owner == this) {
1403 throw new InvalidOperationException("The 'ownerWin32' cannot be the form being shown.");
1407 throw new InvalidOperationException("The form is already displayed as a modal dialog.");
1411 throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
1415 throw new InvalidOperationException("Cannot display a disabled form as modal dialog.");
1418 if (TopLevelControl != this) {
1419 throw new InvalidOperationException("Can only display TopLevel forms as modal dialog.");
1423 // Can't do this, will screw us in the modal loop
1424 form_parent_window.Parent = this.owner;
1427 // Release any captures
1428 XplatUI.GrabInfo(out capture_window, out confined, out area);
1429 if (capture_window != IntPtr.Zero) {
1430 XplatUI.UngrabWindow(capture_window);
1434 // Commented out; we instead let the Visible=true inside the runloop create the control
1435 // otherwise setting DialogResult inside any of the events that are triggered by the
1436 // create will not actually cause the form to not be displayed.
1437 // Leaving this comment here in case there was an actual purpose to creating the control
1439 if (!IsHandleCreated) {
1444 Application.RunLoop(true, new ApplicationContext(this));
1446 if (owner != null) {
1447 // Cannot use Activate(), it has a check for the current active window...
1448 XplatUI.Activate(owner.window.Handle);
1451 if (DialogResult != DialogResult.None) {
1452 return DialogResult;
1454 DialogResult = DialogResult.Cancel;
1455 return DialogResult.Cancel;
1458 public override string ToString() {
1459 return GetType().FullName.ToString() + ", Text: " + Text;
1461 #endregion // Public Instance Methods
1463 #region Protected Instance Methods
1464 protected void ActivateMdiChild(Form form) {
1465 if (!IsMdiContainer)
1467 mdi_container.ActivateChild (form);
1468 OnMdiChildActivate(EventArgs.Empty);
1471 [EditorBrowsable(EditorBrowsableState.Advanced)]
1472 protected override void AdjustFormScrollbars(bool displayScrollbars) {
1473 base.AdjustFormScrollbars (displayScrollbars);
1477 [EditorBrowsable(EditorBrowsableState.Never)]
1478 [Obsolete ("This method has been deprecated")] // XXX what to use instead?
1480 [EditorBrowsable(EditorBrowsableState.Advanced)]
1482 protected void ApplyAutoScaling()
1484 SizeF current_size_f = GetAutoScaleSize (DeviceContext, Font);
1485 Size current_size = new Size ((int) current_size_f.Width, (int) current_size_f.Height);
1489 if (current_size == autoscale_base_size)
1492 if (Environment.GetEnvironmentVariable ("MONO_MWF_SCALING") == "disable"){
1497 // I tried applying the Fudge height factor from:
1498 // http://blogs.msdn.com/mharsh/archive/2004/01/25/62621.aspx
1499 // but it makes things larger without looking better.
1501 if (current_size_f.Width != AutoScaleBaseSize.Width) {
1502 dx = current_size_f.Width / AutoScaleBaseSize.Width + 0.08f;
1507 if (current_size_f.Height != AutoScaleBaseSize.Height) {
1508 dy = current_size_f.Height / AutoScaleBaseSize.Height + 0.08f;
1515 AutoScaleBaseSize = current_size;
1518 protected void CenterToParent() {
1526 w = DefaultSize.Width;
1532 h = DefaultSize.Height;
1536 if (Parent != null) {
1538 } else if (owner != null) {
1542 if (owner != null) {
1543 this.Location = new Point(ctl.Left + ctl.Width / 2 - w /2, ctl.Top + ctl.Height / 2 - h / 2);
1547 protected void CenterToScreen() {
1555 w = DefaultSize.Width;
1561 h = DefaultSize.Height;
1564 XplatUI.GetDisplaySize(out DisplaySize);
1565 this.Location = new Point(DisplaySize.Width / 2 - w / 2, DisplaySize.Height / 2 - h / 2);
1568 [EditorBrowsable(EditorBrowsableState.Advanced)]
1569 protected override Control.ControlCollection CreateControlsInstance() {
1570 return base.CreateControlsInstance ();
1573 [EditorBrowsable(EditorBrowsableState.Advanced)]
1574 protected override void CreateHandle() {
1575 base.CreateHandle ();
1577 Application.AddForm (this);
1581 if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
1582 if (allow_transparency) {
1583 XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
1587 XplatUI.SetWindowMinMax(window.Handle, maximized_bounds, minimum_size, maximum_size);
1588 if ((FormBorderStyle != FormBorderStyle.FixedDialog) && (icon != null)) {
1589 XplatUI.SetIcon(window.Handle, icon);
1592 if ((owner != null) && (owner.IsHandleCreated)) {
1593 XplatUI.SetTopmost(window.Handle, owner.window.Handle, true);
1596 for (int i = 0; i < owned_forms.Count; i++) {
1597 if (owned_forms[i].IsHandleCreated)
1598 XplatUI.SetTopmost(owned_forms[i].window.Handle, window.Handle, true);
1601 if (window_manager != null) {
1602 if (window_state != FormWindowState.Normal) {
1603 window_manager.SetWindowState (FormWindowState.Normal, window_state);
1605 XplatUI.RequestNCRecalc (window.Handle);
1610 [EditorBrowsable(EditorBrowsableState.Advanced)]
1611 protected override void DefWndProc(ref Message m) {
1612 base.DefWndProc (ref m);
1615 protected override void Dispose(bool disposing)
1617 for (int i = 0; i < owned_forms.Count; i++)
1618 ((Form)owned_forms[i]).Owner = null;
1620 owned_forms.Clear ();
1622 base.Dispose (disposing);
1624 Application.RemoveForm (this);
1627 [EditorBrowsable(EditorBrowsableState.Advanced)]
1628 protected virtual void OnActivated(EventArgs e)
1630 EventHandler eh = (EventHandler)(Events [ActivatedEvent]);
1635 [EditorBrowsable(EditorBrowsableState.Advanced)]
1636 protected virtual void OnClosed(EventArgs e) {
1637 EventHandler eh = (EventHandler)(Events [ClosedEvent]);
1642 // Consider calling FireClosingEvents instead of calling this directly.
1643 [EditorBrowsable (EditorBrowsableState.Advanced)]
1644 protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
1645 CancelEventHandler eh = (CancelEventHandler)(Events [ClosingEvent]);
1650 [EditorBrowsable(EditorBrowsableState.Advanced)]
1651 protected override void OnCreateControl() {
1652 base.OnCreateControl ();
1655 XplatUI.SetMenu(window.Handle, menu);
1658 OnLoad(EventArgs.Empty);
1660 // Send initial location
1661 OnLocationChanged(EventArgs.Empty);
1663 if (IsMdiContainer) {
1664 mdi_container.LayoutMdi (MdiLayout.Cascade);
1668 [EditorBrowsable(EditorBrowsableState.Advanced)]
1669 protected virtual void OnDeactivate(EventArgs e) {
1670 EventHandler eh = (EventHandler)(Events [DeactivateEvent]);
1675 [EditorBrowsable(EditorBrowsableState.Advanced)]
1676 protected override void OnFontChanged(EventArgs e) {
1677 base.OnFontChanged (e);
1680 [EditorBrowsable(EditorBrowsableState.Advanced)]
1681 protected override void OnHandleCreated(EventArgs e) {
1682 XplatUI.SetBorderStyle(window.Handle, form_border_style);
1683 base.OnHandleCreated (e);
1686 [EditorBrowsable(EditorBrowsableState.Advanced)]
1687 protected override void OnHandleDestroyed(EventArgs e) {
1688 base.OnHandleDestroyed (e);
1691 [EditorBrowsable(EditorBrowsableState.Advanced)]
1692 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
1693 InputLanguageChangedEventHandler eh = (InputLanguageChangedEventHandler)(Events [InputLanguageChangedEvent]);
1698 [EditorBrowsable(EditorBrowsableState.Advanced)]
1699 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
1700 InputLanguageChangingEventHandler eh = (InputLanguageChangingEventHandler)(Events [InputLanguageChangingEvent]);
1705 [EditorBrowsable(EditorBrowsableState.Advanced)]
1706 protected virtual void OnLoad(EventArgs e) {
1708 ApplyAutoScaling ();
1712 EventHandler eh = (EventHandler)(Events [LoadEvent]);
1717 switch (StartPosition) {
1718 case FormStartPosition.CenterScreen:
1719 this.CenterToScreen();
1721 case FormStartPosition.CenterParent:
1722 this.CenterToParent ();
1724 case FormStartPosition.Manual:
1725 Left = CreateParams.X;
1726 Top = CreateParams.Y;
1733 [EditorBrowsable(EditorBrowsableState.Advanced)]
1734 protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
1735 EventHandler eh = (EventHandler)(Events [MaximizedBoundsChangedEvent]);
1740 [EditorBrowsable(EditorBrowsableState.Advanced)]
1741 protected virtual void OnMaximumSizeChanged(EventArgs e) {
1742 EventHandler eh = (EventHandler)(Events [MaximumSizeChangedEvent]);
1747 [EditorBrowsable(EditorBrowsableState.Advanced)]
1748 protected virtual void OnMdiChildActivate(EventArgs e) {
1749 EventHandler eh = (EventHandler)(Events [MdiChildActivateEvent]);
1754 [EditorBrowsable(EditorBrowsableState.Advanced)]
1755 protected virtual void OnMenuComplete(EventArgs e) {
1756 EventHandler eh = (EventHandler)(Events [MenuCompleteEvent]);
1761 [EditorBrowsable(EditorBrowsableState.Advanced)]
1762 protected virtual void OnMenuStart(EventArgs e) {
1763 EventHandler eh = (EventHandler)(Events [MenuStartEvent]);
1768 [EditorBrowsable(EditorBrowsableState.Advanced)]
1769 protected virtual void OnMinimumSizeChanged(EventArgs e) {
1770 EventHandler eh = (EventHandler)(Events [MinimumSizeChangedEvent]);
1775 [EditorBrowsable(EditorBrowsableState.Advanced)]
1776 protected override void OnPaint (PaintEventArgs pevent) {
1777 base.OnPaint (pevent);
1780 [EditorBrowsable(EditorBrowsableState.Advanced)]
1781 protected override void OnResize(EventArgs e) {
1785 if (size_grip != null) {
1786 size_grip.Location = new Point (ClientSize.Width - size_grip.Width, ClientSize.Height - size_grip.Height);
1790 [EditorBrowsable(EditorBrowsableState.Advanced)]
1791 protected override void OnStyleChanged(EventArgs e) {
1792 base.OnStyleChanged (e);
1795 [EditorBrowsable(EditorBrowsableState.Advanced)]
1796 protected override void OnTextChanged(EventArgs e) {
1797 base.OnTextChanged (e);
1799 if (mdi_container != null)
1800 mdi_container.SetParentText(true);
1803 [EditorBrowsable(EditorBrowsableState.Advanced)]
1804 protected override void OnVisibleChanged(EventArgs e) {
1805 base.OnVisibleChanged (e);
1808 if (window_manager != null)
1809 window_manager.SetWindowState (WindowState, WindowState);
1813 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
1814 if (base.ProcessCmdKey (ref msg, keyData)) {
1818 // Give our menu a shot
1819 if (ActiveMenu != null) {
1820 return ActiveMenu.ProcessCmdKey(ref msg, keyData);
1826 case Keys.Control | Keys.F4:
1827 case Keys.Control | Keys.Shift | Keys.F4:
1830 case Keys.Control | Keys.Tab:
1831 case Keys.Control | Keys.F6:
1832 MdiParent.MdiContainer.ActivateNextChild ();
1834 case Keys.Control | Keys.Shift | Keys.Tab:
1835 case Keys.Control | Keys.Shift | Keys.F6:
1836 MdiParent.MdiContainer.ActivatePreviousChild ();
1844 // LAMESPEC - Not documented that Form overrides ProcessDialogChar; class-status showed
1845 [EditorBrowsable (EditorBrowsableState.Advanced)]
1846 protected override bool ProcessDialogChar(char charCode) {
1847 return base.ProcessDialogChar (charCode);
1850 protected override bool ProcessDialogKey(Keys keyData) {
1851 if ((keyData & Keys.Modifiers) == 0) {
1852 if (keyData == Keys.Enter) {
1853 IntPtr window = XplatUI.GetFocus ();
1854 Control c = Control.FromHandle (window);
1855 if (c is Button && c.FindForm () == this) {
1856 ((Button)c).PerformClick ();
1859 else if (accept_button != null) {
1860 accept_button.PerformClick();
1863 } else if (keyData == Keys.Escape && cancel_button != null) {
1864 cancel_button.PerformClick();
1868 return base.ProcessDialogKey(keyData);
1871 protected override bool ProcessKeyPreview(ref Message msg) {
1873 if (ProcessKeyEventArgs(ref msg)) {
1877 return base.ProcessKeyPreview (ref msg);
1880 protected override bool ProcessTabKey(bool forward) {
1881 return SelectNextControl(ActiveControl, forward, true, true, true);
1885 [EditorBrowsable(EditorBrowsableState.Never)]
1887 [EditorBrowsable(EditorBrowsableState.Advanced)]
1889 protected override void ScaleCore(float dx, float dy) {
1893 // We can't scale max or min windows
1894 if (WindowState == FormWindowState.Normal) {
1895 // We cannot call base since base also adjusts X/Y, but
1896 // a form is toplevel and doesn't move
1900 if (!GetStyle(ControlStyles.FixedWidth)) {
1901 size.Width = (int)(size.Width * dx);
1904 if (!GetStyle(ControlStyles.FixedHeight)) {
1905 size.Height = (int)(size.Height * dy);
1911 /* Now scale our children */
1912 Control [] controls = Controls.GetAllControls ();
1913 for (int i=0; i < controls.Length; i++) {
1914 controls[i].Scale(dx, dy);
1923 protected override void Select(bool directed, bool forward) {
1927 base.SelectNextControl(null, forward, true, true, true);
1930 parent = this.ParentForm;
1931 if (parent != null) {
1932 parent.ActiveControl = this;
1938 [EditorBrowsable(EditorBrowsableState.Advanced)]
1939 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
1940 base.SetBoundsCore (x, y, width, height, specified);
1943 [EditorBrowsable(EditorBrowsableState.Advanced)]
1944 protected override void SetClientSizeCore(int x, int y) {
1945 if ((minimum_size.Width != 0) && (x < minimum_size.Width)) {
1946 x = minimum_size.Width;
1947 } else if ((maximum_size.Width != 0) && (x > maximum_size.Width)) {
1948 x = maximum_size.Width;
1951 if ((minimum_size.Height != 0) && (y < minimum_size.Height)) {
1952 y = minimum_size.Height;
1953 } else if ((maximum_size.Height != 0) && (y > maximum_size.Height)) {
1954 y = maximum_size.Height;
1957 Rectangle ClientRect = new Rectangle(0, 0, x, y);
1958 Rectangle WindowRect;
1959 CreateParams cp = this.CreateParams;
1961 clientsize_set = new Size(x, y);
1963 if (XplatUI.CalculateWindowRect(ref ClientRect, cp.Style, cp.ExStyle, cp.menu, out WindowRect)) {
1964 SetBounds(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
1968 [EditorBrowsable(EditorBrowsableState.Advanced)]
1969 protected override void SetVisibleCore(bool value) {
1970 is_changing_visible_state = true;
1971 has_been_visible = value || has_been_visible;
1972 base.SetVisibleCore (value);
1973 is_changing_visible_state = false;
1976 // Shown event is only called once, the first time the form is made visible
1977 if (value && !shown_raised) {
1978 this.OnShown (EventArgs.Empty);
1979 shown_raised = true;
1984 protected override void UpdateDefaultButton() {
1985 base.UpdateDefaultButton ();
1988 [EditorBrowsable(EditorBrowsableState.Advanced)]
1989 protected override void WndProc(ref Message m) {
1991 Console.WriteLine(DateTime.Now.ToLongTimeString () + " Form {0} ({2}) received message {1}", window.Handle == IntPtr.Zero ? this.Text : XplatUI.Window(window.Handle), m.ToString (), Text);
1994 if (window_manager != null && window_manager.HandleMessage (ref m)) {
1998 switch((Msg)m.Msg) {
1999 case Msg.WM_DESTROY: {
2000 base.WndProc(ref m);
2001 if (!RecreatingHandle) {
2002 this.closing = true;
2007 case Msg.WM_CLOSE_INTERNAL: {
2012 case Msg.WM_CLOSE: {
2013 Form act = Form.ActiveForm;
2014 if (act != null && act != this && act.Modal == true) {
2018 if (mdi_container != null) {
2019 foreach (Form mdi_child in mdi_container.MdiChildren) {
2020 mdi_child.FireClosingEvents (CloseReason.MdiFormClosing);
2025 if (!FireClosingEvents (CloseReason.UserClosing)) {
2026 OnClosed (EventArgs.Empty);
2028 OnFormClosed (new FormClosedEventArgs (CloseReason.UserClosing));
2037 if (FireClosingEvents (CloseReason.UserClosing)) {
2038 DialogResult = DialogResult.None;
2042 OnClosed (EventArgs.Empty);
2044 OnFormClosed (new FormClosedEventArgs (CloseReason.UserClosing));
2054 case Msg.WM_WINDOWPOSCHANGED: {
2055 if (WindowState != FormWindowState.Minimized) {
2056 base.WndProc(ref m);
2062 case Msg.WM_SYSCOMMAND: {
2063 // Let *Strips know the app's title bar was clicked
2064 if (XplatUI.IsEnabled (Handle))
2065 ToolStripManager.FireAppClicked ();
2067 base.WndProc(ref m);
2072 case Msg.WM_ACTIVATE: {
2073 if (m.WParam != (IntPtr)WindowActiveFlags.WA_INACTIVE) {
2075 SelectActiveControl ();
2077 if (ActiveControl != null && !ActiveControl.Focused)
2078 SendControlFocus (ActiveControl);
2081 OnActivated(EventArgs.Empty);
2083 OnDeactivate(EventArgs.Empty);
2088 case Msg.WM_KILLFOCUS: {
2089 base.WndProc(ref m);
2093 case Msg.WM_SETFOCUS: {
2094 if (ActiveControl != null && ActiveControl != this) {
2095 ActiveControl.Focus();
2096 return; // FIXME - do we need to run base.WndProc, even though we just changed focus?
2098 if (IsMdiContainer) {
2099 mdi_container.SendFocusToActiveChild ();
2102 base.WndProc(ref m);
2107 case Msg.WM_NCLBUTTONDOWN: {
2108 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
2109 ActiveMenu.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, Control.MousePosition.X, Control.MousePosition.Y, 0));
2112 if (ActiveMaximizedMdiChild != null) {
2113 if (ActiveMaximizedMdiChild.HandleMenuMouseDown (ActiveMenu,
2114 LowOrder ((int) m.LParam.ToInt32 ()),
2115 HighOrder ((int) m.LParam.ToInt32 ()))) {
2116 // Don't let base process this message, otherwise we won't
2117 // get a WM_NCLBUTTONUP.
2121 base.WndProc(ref m);
2124 case Msg.WM_NCLBUTTONUP: {
2125 if (ActiveMaximizedMdiChild != null) {
2126 ActiveMaximizedMdiChild.HandleMenuMouseUp (ActiveMenu,
2127 LowOrder ((int)m.LParam.ToInt32 ()),
2128 HighOrder ((int)m.LParam.ToInt32 ()));
2130 base.WndProc (ref m);
2134 case Msg.WM_NCMOUSELEAVE: {
2135 if (ActiveMaximizedMdiChild != null) {
2136 ActiveMaximizedMdiChild.HandleMenuMouseLeave(ActiveMenu,
2137 LowOrder((int)m.LParam.ToInt32()),
2138 HighOrder((int)m.LParam.ToInt32()));
2140 base.WndProc(ref m);
2144 case Msg.WM_NCMOUSEMOVE: {
2145 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
2146 ActiveMenu.OnMouseMove(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0));
2149 if (ActiveMaximizedMdiChild != null) {
2150 XplatUI.RequestAdditionalWM_NCMessages (Handle, false, true);
2151 ActiveMaximizedMdiChild.HandleMenuMouseMove (ActiveMenu,
2152 LowOrder ((int)m.LParam.ToInt32 ()),
2153 HighOrder ((int)m.LParam.ToInt32 ()));
2155 base.WndProc(ref m);
2159 case Msg.WM_NCPAINT: {
2160 if (ActiveMenu != null) {
2164 pe = XplatUI.PaintEventStart(Handle, false);
2165 pnt = XplatUI.GetMenuOrigin(window.Handle);
2167 // The entire menu has to be in the clip rectangle because the
2168 // control buttons are right-aligned and otherwise they would
2169 // stay painted when the window gets resized.
2170 Rectangle clip = new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0);
2171 clip = Rectangle.Union(clip, pe.ClipRectangle);
2173 pe.Graphics.SetClip(clip);
2175 ActiveMenu.Draw (pe, new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0));
2177 if (ActiveMaximizedMdiChild != null) {
2178 ActiveMaximizedMdiChild.DrawMaximizedButtons (ActiveMenu, pe);
2181 XplatUI.PaintEventEnd(Handle, false);
2184 base.WndProc(ref m);
2188 case Msg.WM_NCCALCSIZE: {
2189 XplatUIWin32.NCCALCSIZE_PARAMS ncp;
2191 if ((ActiveMenu != null) && (m.WParam == (IntPtr)1)) {
2192 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
2195 ncp.rgrc1.top += ThemeEngine.Current.CalcMenuBarSize (DeviceContext, ActiveMenu, ClientSize.Width);
2196 Marshal.StructureToPtr(ncp, m.LParam, true);
2202 case Msg.WM_MOUSEMOVE: {
2203 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2204 MouseEventArgs args;
2206 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
2207 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2208 active_tracker.OnMotion(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2211 base.WndProc(ref m);
2215 case Msg.WM_LBUTTONDOWN:
2216 case Msg.WM_MBUTTONDOWN:
2217 case Msg.WM_RBUTTONDOWN: {
2218 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2219 MouseEventArgs args;
2221 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
2222 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2223 active_tracker.OnMouseDown(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2226 base.WndProc(ref m);
2230 case Msg.WM_LBUTTONUP:
2231 case Msg.WM_MBUTTONUP:
2232 case Msg.WM_RBUTTONUP: {
2233 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2234 MouseEventArgs args;
2235 MouseButtons mb = FromParamToMouseButtons ((int) m.WParam.ToInt32());
2237 // We add in the button that was released (not sent in WParam)
2238 switch((Msg)m.Msg) {
2239 case Msg.WM_LBUTTONUP:
2240 mb |= MouseButtons.Left;
2242 case Msg.WM_MBUTTONUP:
2243 mb |= MouseButtons.Middle;
2245 case Msg.WM_RBUTTONUP:
2246 mb |= MouseButtons.Right;
2250 args = new MouseEventArgs (mb, mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2251 active_tracker.OnMouseUp(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2255 base.WndProc(ref m);
2259 case Msg.WM_GETMINMAXINFO: {
2262 if (m.LParam != IntPtr.Zero) {
2263 mmi = (MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO));
2265 default_maximized_bounds = new Rectangle(mmi.ptMaxPosition.x, mmi.ptMaxPosition.y, mmi.ptMaxSize.x, mmi.ptMaxSize.y);
2266 if (maximized_bounds != Rectangle.Empty) {
2267 mmi.ptMaxPosition.x = maximized_bounds.Left;
2268 mmi.ptMaxPosition.y = maximized_bounds.Top;
2269 mmi.ptMaxSize.x = maximized_bounds.Width;
2270 mmi.ptMaxSize.y = maximized_bounds.Height;
2273 if (minimum_size != Size.Empty) {
2274 mmi.ptMinTrackSize.x = minimum_size.Width;
2275 mmi.ptMinTrackSize.y = minimum_size.Height;
2278 if (maximum_size != Size.Empty) {
2279 mmi.ptMaxTrackSize.x = maximum_size.Width;
2280 mmi.ptMaxTrackSize.y = maximum_size.Height;
2282 Marshal.StructureToPtr(mmi, m.LParam, false);
2288 case Msg.WM_MOUSEACTIVATE: {
2289 // Let *Strips know the form or another control has been clicked
2290 if (XplatUI.IsEnabled (Handle))
2291 ToolStripManager.FireAppClicked ();
2293 base.WndProc (ref m);
2297 case Msg.WM_ACTIVATEAPP: {
2298 // Let *Strips know the app lost focus
2299 if (m.WParam == (IntPtr)0)
2300 if (XplatUI.IsEnabled (Handle))
2301 ToolStripManager.FireAppFocusChanged (this);
2303 base.WndProc (ref m);
2309 base.WndProc (ref m);
2314 #endregion // Protected Instance Methods
2316 internal override void FireEnter ()
2318 // do nothing - forms don't generate OnEnter
2321 internal override void FireLeave ()
2323 // do nothing - forms don't generate OnLeave
2326 internal void RemoveWindowManager ()
2328 window_manager = null;
2331 internal override void CheckAcceptButton()
2333 if (accept_button != null) {
2334 Button a_button = accept_button as Button;
2336 if (ActiveControl == a_button)
2339 if (ActiveControl is Button) {
2340 a_button.paint_as_acceptbutton = false;
2344 a_button.paint_as_acceptbutton = true;
2351 static object ActivatedEvent = new object ();
2352 static object ClosedEvent = new object ();
2353 static object ClosingEvent = new object ();
2354 static object DeactivateEvent = new object ();
2355 static object InputLanguageChangedEvent = new object ();
2356 static object InputLanguageChangingEvent = new object ();
2357 static object LoadEvent = new object ();
2358 static object MaximizedBoundsChangedEvent = new object ();
2359 static object MaximumSizeChangedEvent = new object ();
2360 static object MdiChildActivateEvent = new object ();
2361 static object MenuCompleteEvent = new object ();
2362 static object MenuStartEvent = new object ();
2363 static object MinimumSizeChangedEvent = new object ();
2365 public event EventHandler Activated {
2366 add { Events.AddHandler (ActivatedEvent, value); }
2367 remove { Events.RemoveHandler (ActivatedEvent, value); }
2372 [EditorBrowsable (EditorBrowsableState.Never)]
2374 public event EventHandler Closed {
2375 add { Events.AddHandler (ClosedEvent, value); }
2376 remove { Events.RemoveHandler (ClosedEvent, value); }
2381 [EditorBrowsable (EditorBrowsableState.Never)]
2383 public event CancelEventHandler Closing {
2384 add { Events.AddHandler (ClosingEvent, value); }
2385 remove { Events.RemoveHandler (ClosingEvent, value); }
2388 public event EventHandler Deactivate {
2389 add { Events.AddHandler (DeactivateEvent, value); }
2390 remove { Events.RemoveHandler (DeactivateEvent, value); }
2393 public event InputLanguageChangedEventHandler InputLanguageChanged {
2394 add { Events.AddHandler (InputLanguageChangedEvent, value); }
2395 remove { Events.RemoveHandler (InputLanguageChangedEvent, value); }
2398 public event InputLanguageChangingEventHandler InputLanguageChanging {
2399 add { Events.AddHandler (InputLanguageChangingEvent, value); }
2400 remove { Events.RemoveHandler (InputLanguageChangingEvent, value); }
2403 public event EventHandler Load {
2404 add { Events.AddHandler (LoadEvent, value); }
2405 remove { Events.RemoveHandler (LoadEvent, value); }
2408 public event EventHandler MaximizedBoundsChanged {
2409 add { Events.AddHandler (MaximizedBoundsChangedEvent, value); }
2410 remove { Events.RemoveHandler (MaximizedBoundsChangedEvent, value); }
2413 public event EventHandler MaximumSizeChanged {
2414 add { Events.AddHandler (MaximumSizeChangedEvent, value); }
2415 remove { Events.RemoveHandler (MaximumSizeChangedEvent, value); }
2418 public event EventHandler MdiChildActivate {
2419 add { Events.AddHandler (MdiChildActivateEvent, value); }
2420 remove { Events.RemoveHandler (MdiChildActivateEvent, value); }
2426 public event EventHandler MenuComplete {
2427 add { Events.AddHandler (MenuCompleteEvent, value); }
2428 remove { Events.RemoveHandler (MenuCompleteEvent, value); }
2434 public event EventHandler MenuStart {
2435 add { Events.AddHandler (MenuStartEvent, value); }
2436 remove { Events.RemoveHandler (MenuStartEvent, value); }
2439 public event EventHandler MinimumSizeChanged {
2440 add { Events.AddHandler (MinimumSizeChangedEvent, value); }
2441 remove { Events.RemoveHandler (MinimumSizeChangedEvent, value); }
2446 [EditorBrowsable(EditorBrowsableState.Never)]
2447 public new event EventHandler TabIndexChanged {
2448 add { base.TabIndexChanged += value; }
2449 remove { base.TabIndexChanged -= value; }
2451 #endregion // Events
2454 [SettingsBindable (true)]
2455 public override string Text {
2465 [SettingsBindable (true)]
2466 public new Point Location {
2468 return base.Location;
2472 base.Location = value;
2476 static object FormClosingEvent = new object ();
2477 static object FormClosedEvent = new object ();
2478 static object HelpButtonClickedEvent = new object ();
2479 static object ResizeEndEvent = new object ();
2480 static object ResizeBeginEvent = new object ();
2481 static object RightToLeftLayoutChangedEvent = new object ();
2482 static object ShownEvent = new object ();
2485 [EditorBrowsable (EditorBrowsableState.Always)]
2486 public new event EventHandler AutoSizeChanged {
2487 add { base.AutoSizeChanged += value; }
2488 remove { base.AutoSizeChanged -= value; }
2492 [EditorBrowsable (EditorBrowsableState.Always)]
2493 public new event EventHandler AutoValidateChanged {
2494 add { base.AutoValidateChanged += value; }
2495 remove { base.AutoValidateChanged -= value; }
2498 public event FormClosingEventHandler FormClosing {
2499 add { Events.AddHandler (FormClosingEvent, value); }
2500 remove { Events.RemoveHandler (FormClosingEvent, value); }
2503 public event FormClosedEventHandler FormClosed {
2504 add { Events.AddHandler (FormClosedEvent, value); }
2505 remove { Events.RemoveHandler (FormClosedEvent, value); }
2509 [EditorBrowsable (EditorBrowsableState.Always)]
2510 public event CancelEventHandler HelpButtonClicked {
2511 add { Events.AddHandler (HelpButtonClickedEvent, value); }
2512 remove { Events.RemoveHandler (HelpButtonClickedEvent, value); }
2516 [EditorBrowsable (EditorBrowsableState.Never)]
2517 public new event EventHandler MarginChanged {
2518 add { base.MarginChanged += value; }
2519 remove { base.MarginChanged -= value; }
2522 public event EventHandler RightToLeftLayoutChanged {
2523 add { Events.AddHandler (RightToLeftLayoutChangedEvent, value); }
2524 remove { Events.RemoveHandler (RightToLeftLayoutChangedEvent, value); }
2527 public event EventHandler ResizeBegin {
2528 add { Events.AddHandler (ResizeBeginEvent, value); }
2529 remove { Events.RemoveHandler (ResizeBeginEvent, value); }
2532 public event EventHandler ResizeEnd {
2533 add { Events.AddHandler (ResizeEndEvent, value); }
2534 remove { Events.RemoveHandler (ResizeEndEvent, value); }
2537 public event EventHandler Shown {
2538 add { Events.AddHandler (ShownEvent, value); }
2539 remove { Events.RemoveHandler (ShownEvent, value); }
2543 [EditorBrowsable (EditorBrowsableState.Never)]
2544 public new event EventHandler TabStopChanged {
2545 add { base.TabStopChanged += value; }
2546 remove { base.TabStopChanged -= value; }
2549 protected override void OnBackgroundImageChanged (EventArgs e)
2551 base.OnBackgroundImageChanged (e);
2554 protected override void OnBackgroundImageLayoutChanged (EventArgs e)
2556 base.OnBackgroundImageLayoutChanged (e);
2559 [EditorBrowsable (EditorBrowsableState.Advanced)]
2560 protected override void OnEnabledChanged (EventArgs e)
2562 base.OnEnabledChanged (e);
2565 [EditorBrowsable (EditorBrowsableState.Advanced)]
2566 protected override void OnEnter (EventArgs e)
2571 [EditorBrowsable (EditorBrowsableState.Advanced)]
2572 protected virtual void OnFormClosed (FormClosedEventArgs e) {
2573 FormClosedEventHandler eh = (FormClosedEventHandler)(Events[FormClosedEvent]);
2578 // Consider calling FireClosingEvents instead of calling this directly.
2579 [EditorBrowsable (EditorBrowsableState.Advanced)]
2580 protected virtual void OnFormClosing (FormClosingEventArgs e)
2582 FormClosingEventHandler eh = (FormClosingEventHandler)(Events [FormClosingEvent]);
2587 [MonoTODO ("Not hooked up to event")]
2588 [EditorBrowsable (EditorBrowsableState.Advanced)]
2589 protected virtual void OnHelpButtonClicked (CancelEventArgs e)
2591 CancelEventHandler eh = (CancelEventHandler)(Events[HelpButtonClickedEvent]);
2596 protected override void OnLayout (LayoutEventArgs levent)
2598 base.OnLayout (levent);
2601 [EditorBrowsable (EditorBrowsableState.Advanced)]
2602 protected virtual void OnResizeBegin (EventArgs e)
2604 EventHandler eh = (EventHandler) (Events [ResizeBeginEvent]);
2609 [EditorBrowsable (EditorBrowsableState.Advanced)]
2610 protected virtual void OnResizeEnd (EventArgs e)
2612 EventHandler eh = (EventHandler) (Events [ResizeEndEvent]);
2617 [EditorBrowsable (EditorBrowsableState.Advanced)]
2618 protected virtual void OnShown (EventArgs e)
2620 EventHandler eh = (EventHandler) (Events [ShownEvent]);