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-2005 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 public class Form : ContainerControl {
44 #region Local Variables
45 internal static Form active_form;
46 internal bool closing;
47 FormBorderStyle form_border_style;
48 private bool autoscale;
49 private bool clientsize_set;
50 private Size autoscale_base_size;
51 private bool allow_transparency;
52 private static Icon default_icon;
53 internal bool is_modal;
54 internal FormWindowState window_state;
55 private bool control_box;
56 private bool minimize_box;
57 private bool maximize_box;
58 private bool help_button;
59 private bool show_in_taskbar;
61 private IButtonControl accept_button;
62 private IButtonControl cancel_button;
63 private DialogResult dialog_result;
64 private FormStartPosition start_position;
66 private Form.ControlCollection owned_forms;
67 private MdiClient mdi_container;
68 private InternalWindowManager window_manager;
69 private Form mdi_parent;
70 private bool key_preview;
71 private MainMenu menu;
73 private Size maximum_size;
74 private Size minimum_size;
75 private SizeGripStyle size_grip_style;
76 private Rectangle maximized_bounds;
77 private Rectangle default_maximized_bounds;
78 private double opacity;
79 internal ApplicationContext context;
80 Color transparency_key;
81 internal MenuTracker active_tracker;
83 #endregion // Local Variables
85 #region Private & Internal Methods
88 default_icon = (Icon)Locale.GetResource("mono.ico");
90 #endregion // Private & Internal Methods
92 #region Public Classes
93 public new class ControlCollection : Control.ControlCollection {
96 public ControlCollection(Form owner) : base(owner) {
97 this.form_owner = owner;
100 public override void Add(Control value) {
101 if (Contains (value))
104 ((Form)value).owner=(Form)owner;
107 public override void Remove(Control value) {
108 ((Form)value).owner = null;
112 #endregion // Public Classes
114 #region Public Constructor & Destructor
117 SizeF current_scale = GetAutoScaleSize (DeviceContext, Font);
120 autoscale_base_size = new Size ((int)current_scale.Width, (int) current_scale.Height);
121 allow_transparency = false;
124 dialog_result = DialogResult.None;
125 start_position = FormStartPosition.WindowsDefaultLocation;
126 form_border_style = FormBorderStyle.Sizable;
127 window_state = FormWindowState.Normal;
132 minimum_size = Size.Empty;
133 maximum_size = Size.Empty;
138 show_in_taskbar = true;
139 ime_mode = ImeMode.NoControl;
142 size_grip_style = SizeGripStyle.Auto;
143 maximized_bounds = Rectangle.Empty;
144 default_maximized_bounds = Rectangle.Empty;
145 owned_forms = new Form.ControlCollection(this);
146 transparency_key = Color.Empty;
148 #endregion // Public Constructor & Destructor
150 #region Public Static Properties
152 public static Form ActiveForm {
156 active = FromHandle(XplatUI.GetActive());
158 if (active != null) {
159 if ( !(active is Form)) {
162 parent = active.Parent;
163 while (parent != null) {
164 if (parent is Form) {
167 parent = parent.Parent;
177 #endregion // Public Static Properties
179 #region Public Instance Properties
181 public IButtonControl AcceptButton {
183 return accept_button;
187 accept_button = value;
192 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
193 public bool AllowTransparency {
195 return allow_transparency;
199 if (XplatUI.SupportsTransparency()) {
200 allow_transparency = value;
203 XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
205 UpdateStyles(); // Remove the WS_EX_LAYERED style
212 public bool AutoScale {
224 [EditorBrowsable(EditorBrowsableState.Advanced)]
225 public virtual Size AutoScaleBaseSize {
227 return autoscale_base_size;
231 autoscale_base_size = value;
236 public override bool AutoScroll {
238 return base.AutoScroll;
241 base.AutoScroll = value;
245 public override Color BackColor {
247 return base.BackColor;
250 base.BackColor = value;
255 public IButtonControl CancelButton {
257 return cancel_button;
261 cancel_button = value;
265 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
267 public Size ClientSize {
269 return base.ClientSize;
273 base.ClientSize = value;
278 public bool ControlBox {
284 if (control_box != value) {
292 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
293 public Rectangle DesktopBounds {
295 return new Rectangle(Location, Size);
304 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
305 public Point DesktopLocation {
316 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
317 public DialogResult DialogResult {
319 return dialog_result;
323 dialog_result = value;
330 [DefaultValue(FormBorderStyle.Sizable)]
332 public FormBorderStyle FormBorderStyle {
334 return form_border_style;
337 form_border_style = value;
339 if (window_manager == null) {
340 if (IsHandleCreated) {
341 XplatUI.SetBorderStyle(window.Handle, form_border_style);
344 if (value == FormBorderStyle.FixedToolWindow ||
345 value == FormBorderStyle.SizableToolWindow)
346 window_manager = new InternalWindowManager (this);
348 window_manager.UpdateBorderStyle (value);
355 [DefaultValue(false)]
356 public bool HelpButton {
362 if (help_button != value) {
380 if (IsHandleCreated) {
381 XplatUI.SetIcon(Handle, icon == null ? default_icon : icon);
388 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
389 public bool IsMdiChild {
391 return mdi_parent != null;
395 [DefaultValue(false)]
396 public bool IsMdiContainer {
398 return mdi_container != null;
402 if (value && mdi_container == null) {
403 mdi_container = new MdiClient();
404 Controls.Add(mdi_container);
405 } else if (!value && mdi_container != null) {
406 Controls.Remove(mdi_container);
407 mdi_container = null;
413 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
414 public Form ActiveMdiChild {
418 return (Form) mdi_container.ActiveMdiChild;
423 [EditorBrowsable(EditorBrowsableState.Advanced)]
424 public bool IsRestrictedWindow {
430 [DefaultValue(false)]
431 public bool KeyPreview {
442 public bool MaximizeBox {
447 if (maximize_box != value) {
448 maximize_box = value;
449 if (IsHandleCreated) {
457 [DefaultValue("{Width=0, Height=0}")]
459 [RefreshProperties(RefreshProperties.Repaint)]
460 public Size MaximumSize {
466 if (maximum_size != value) {
467 maximum_size = value;
468 OnMaximumSizeChanged(EventArgs.Empty);
469 if (IsHandleCreated) {
470 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
477 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
478 public Form[] MdiChildren {
480 if (mdi_container != null) {
483 form_list = new Form[mdi_container.Controls.Count];
484 for (int i = 0; i < mdi_container.Controls.Count; i++) {
485 form_list[i] = (Form)mdi_container.Controls[i];
494 [MonoTODO("Finish setter")]
496 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
497 public Form MdiParent {
507 if (!value.IsMdiContainer)
508 throw new ArgumentException ();
510 if (mdi_parent != null) {
511 mdi_parent.MdiContainer.Controls.Remove (this);
515 if (mdi_parent != null) {
516 window_manager = new MdiWindowManager (this,
517 mdi_parent.MdiContainer);
518 mdi_parent.MdiContainer.Controls.Add (this);
526 internal MenuTracker ActiveTracker {
527 get { return active_tracker; }
529 if (value == active_tracker)
532 Capture = value != null;
533 active_tracker = value;
537 internal MdiClient MdiContainer {
538 get { return mdi_container; }
541 internal InternalWindowManager WindowManager {
542 get { return window_manager; }
546 public MainMenu Menu {
555 if (menu != null && !IsMdiChild) {
558 if (IsHandleCreated) {
559 XplatUI.SetMenu (window.Handle, menu);
562 if (clientsize_set) {
563 SetClientSizeCore(client_size.Width, client_size.Height);
565 UpdateBounds (bounds.X, bounds.Y, bounds.Width, bounds.Height, ClientSize.Width, ClientSize.Height -
566 ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu, ClientSize.Width));
574 public MainMenu MergedMenu {
576 if (!IsMdiChild || window_manager == null)
578 return ((MdiWindowManager) window_manager).MergedMenu;
582 // This is the menu in display and being used because of merging this can
583 // be different then the menu that is actually assosciated with the form
584 internal MainMenu ActiveMenu {
589 if (IsMdiContainer && mdi_container.Controls.Count > 0 &&
590 ((Form) mdi_container.Controls [0]).WindowState == FormWindowState.Maximized) {
591 MdiWindowManager wm = (MdiWindowManager) ((Form) mdi_container.Controls [0]).WindowManager;
592 return wm.MaximizedMenu;
595 Form amc = ActiveMdiChild;
596 if (amc == null || amc.Menu == null)
598 return amc.MergedMenu;
602 internal MdiWindowManager ActiveMaximizedMdiChild {
604 Form child = ActiveMdiChild;
607 if (child.WindowManager == null || child.window_state != FormWindowState.Maximized)
609 return (MdiWindowManager) child.WindowManager;
614 public bool MinimizeBox {
619 if (minimize_box != value) {
620 minimize_box = value;
621 if (IsHandleCreated) {
629 [DefaultValue("{Width=0, Height=0}")]
631 [RefreshProperties(RefreshProperties.Repaint)]
632 public Size MinimumSize {
638 if (minimum_size != value) {
639 minimum_size = value;
640 OnMinimumSizeChanged(EventArgs.Empty);
641 if (IsHandleCreated) {
642 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
649 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
657 [TypeConverter(typeof(OpacityConverter))]
658 public double Opacity {
666 AllowTransparency = true;
668 XplatUI.SetWindowTransparency(Handle, opacity, TransparencyKey);
674 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
675 public Form[] OwnedForms {
679 form_list = new Form[owned_forms.Count];
681 for (int i=0; i<owned_forms.Count; i++) {
682 form_list[i] = (Form)owned_forms[i];
690 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
697 if (owner != value) {
699 owner.RemoveOwnedForm(this);
702 owner.AddOwnedForm(this);
704 XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
706 XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
713 public bool ShowInTaskbar {
715 return show_in_taskbar;
718 if (show_in_taskbar != value) {
719 show_in_taskbar = value;
720 if (IsHandleCreated) {
728 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
740 [MonoTODO("Trigger something when GripStyle is set")]
741 [DefaultValue(SizeGripStyle.Auto)]
742 public SizeGripStyle SizeGripStyle {
744 return size_grip_style;
748 size_grip_style = value;
752 [DefaultValue(FormStartPosition.WindowsDefaultLocation)]
754 public FormStartPosition StartPosition {
756 return start_position;
760 if (start_position == FormStartPosition.WindowsDefaultLocation) { // Only do this if it's not set yet
761 start_position = value;
762 if (IsHandleCreated) {
763 switch(start_position) {
764 case FormStartPosition.CenterParent: {
769 case FormStartPosition.CenterScreen: {
784 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
785 [EditorBrowsable(EditorBrowsableState.Never)]
786 public int TabIndex {
788 return base.TabIndex;
792 base.TabIndex = value;
797 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
798 [EditorBrowsable(EditorBrowsableState.Advanced)]
799 public bool TopLevel {
801 return GetTopLevel();
805 if (!value && IsMdiContainer)
806 throw new ArgumentException ("MDI Container forms must be top level.");
811 [DefaultValue(false)]
812 public bool TopMost {
818 if (topmost != value) {
821 XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, value);
826 public Color TransparencyKey {
828 return transparency_key;
832 transparency_key = value;
834 AllowTransparency = true;
836 XplatUI.SetWindowTransparency(Handle, Opacity, transparency_key);
840 [DefaultValue(FormWindowState.Normal)]
841 public FormWindowState WindowState {
843 if (IsHandleCreated) {
845 if (window_manager != null)
846 return window_manager.GetWindowState ();
849 window_state = XplatUI.GetWindowState(Handle);
852 catch(NotSupportedException) {
860 window_state = value;
861 if (IsHandleCreated) {
863 if (window_manager != null) {
864 window_manager.SetWindowState (window_state);
869 XplatUI.SetWindowState(Handle, value);
872 catch(NotSupportedException) {
878 #endregion // Public Instance Properties
880 #region Protected Instance Properties
881 [MonoTODO("Need to set start position properly")]
882 protected override CreateParams CreateParams {
884 CreateParams cp = new CreateParams ();
887 cp.ClassName = XplatUI.DefaultClassName;
892 cp.Parent = IntPtr.Zero;
894 // if (start_position == FormStartPosition.WindowsDefaultLocation) {
895 cp.X = unchecked((int)0x80000000);
896 cp.Y = unchecked((int)0x80000000);
904 cp.Style = (int)(WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS);
907 cp.Style |= (int)(WindowStyles.WS_CHILD | WindowStyles.WS_CAPTION);
908 cp.Parent = Parent.Handle;
910 cp.ExStyle |= (int) (WindowStyles.WS_EX_WINDOWEDGE | WindowStyles.WS_EX_MDICHILD);
911 switch (FormBorderStyle) {
912 case FormBorderStyle.None:
914 case FormBorderStyle.FixedToolWindow:
915 case FormBorderStyle.SizableToolWindow:
916 cp.ExStyle |= (int) WindowStyles.WS_EX_TOOLWINDOW;
919 cp.Style |= (int) WindowStyles.WS_OVERLAPPEDWINDOW;
924 switch (FormBorderStyle) {
925 case FormBorderStyle.Fixed3D: {
926 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
927 cp.ExStyle |= (int)WindowStyles.WS_EX_CLIENTEDGE;
931 case FormBorderStyle.FixedDialog: {
932 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
933 cp.ExStyle |= (int)(WindowStyles.WS_EX_DLGMODALFRAME);
937 case FormBorderStyle.FixedSingle: {
938 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
942 case FormBorderStyle.FixedToolWindow: {
943 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
944 cp.ExStyle |= (int)(WindowStyles.WS_EX_TOOLWINDOW);
948 case FormBorderStyle.Sizable: {
949 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
953 case FormBorderStyle.SizableToolWindow: {
954 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
955 cp.ExStyle |= (int)(WindowStyles.WS_EX_TOOLWINDOW);
959 case FormBorderStyle.None: {
965 switch(window_state) {
966 case FormWindowState.Maximized: {
967 cp.Style |= (int)WindowStyles.WS_MAXIMIZE;
971 case FormWindowState.Minimized: {
972 cp.Style |= (int)WindowStyles.WS_MINIMIZE;
978 cp.ExStyle |= (int) WindowStyles.WS_EX_TOPMOST;
982 cp.ExStyle |= (int)WindowStyles.WS_EX_APPWINDOW;
986 cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
990 cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
994 cp.Style |= (int)WindowStyles.WS_SYSMENU;
997 if (HelpButton && !MaximizeBox && !MinimizeBox) {
998 cp.ExStyle |= (int)WindowStyles.WS_EX_CONTEXTHELP;
1002 cp.Style |= (int)WindowStyles.WS_VISIBLE;
1004 if (Opacity < 1.0 || TransparencyKey != Color.Empty) {
1005 cp.ExStyle |= (int)WindowStyles.WS_EX_LAYERED;
1008 if (!is_enabled && context == null) {
1009 cp.Style |= (int)(WindowStyles.WS_DISABLED);
1016 protected override ImeMode DefaultImeMode {
1018 return ImeMode.NoControl;
1022 protected override Size DefaultSize {
1024 return new Size (250, 250);
1028 protected Rectangle MaximizedBounds {
1030 if (maximized_bounds != Rectangle.Empty) {
1031 return maximized_bounds;
1033 return default_maximized_bounds;
1037 maximized_bounds = value;
1038 OnMaximizedBoundsChanged(EventArgs.Empty);
1039 if (IsHandleCreated) {
1040 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
1044 #endregion // Protected Instance Properties
1046 #region Public Static Methods
1047 [EditorBrowsable(EditorBrowsableState.Advanced)]
1048 public static SizeF GetAutoScaleSize (Font font)
1050 return XplatUI.GetAutoScaleSize(font);
1053 #endregion // Public Static Methods
1055 #region Public Instance Methods
1056 internal SizeF GetAutoScaleSize (Graphics g, Font font)
1059 // The following constants come from the dotnet mailing list
1060 // discussion: http://discuss.develop.com/archives/wa.exe?A2=ind0203A&L=DOTNET&P=R3655
1062 // The magic number is "Its almost the length
1063 // of the string with a smattering added in
1064 // for compat with earlier code".
1067 string magic_string = "The quick brown fox jumped over the lazy dog.";
1068 double magic_number = 44.549996948242189;
1069 float width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
1071 return new SizeF (width, font.Height);
1074 public void Activate() {
1077 // The docs say activate only activates if our app is already active
1078 active = ActiveForm;
1079 if ((active != null) && (this != active)) {
1080 XplatUI.Activate(window.Handle);
1084 public void AddOwnedForm(Form ownedForm) {
1085 owned_forms.Add(ownedForm);
1088 public void Close () {
1090 XplatUI.SendMessage(this.Handle, Msg.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
1094 public void LayoutMdi(MdiLayout value) {
1095 if (mdi_container != null) {
1096 mdi_container.LayoutMdi(value);
1100 public void RemoveOwnedForm(Form ownedForm) {
1101 owned_forms.Remove(ownedForm);
1104 public void SetDesktopBounds(int x, int y, int width, int height) {
1105 DesktopBounds = new Rectangle(x, y, width, height);
1108 public void SetDesktopLocation(int x, int y) {
1109 DesktopLocation = new Point(x, y);
1112 public DialogResult ShowDialog() {
1113 return ShowDialog(null);
1116 public DialogResult ShowDialog(IWin32Window ownerWin32) {
1119 if (ownerWin32 != null) {
1120 owner = (Form)Control.FromHandle(ownerWin32.Handle);
1123 if (owner == this) {
1124 throw new InvalidOperationException("The 'ownerWin32' cannot be the form being shown.");
1128 throw new InvalidOperationException("The form is already displayed as a modal dialog.");
1132 throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
1136 throw new InvalidOperationException("Cannot display a disabled form as modal dialog.");
1139 if (TopLevelControl != this) {
1140 throw new InvalidOperationException("Can only display TopLevel forms as modal dialog.");
1144 // Can't do this, will screw us in the modal loop
1145 form_parent_window.Parent = this.owner;
1148 previous = Form.ActiveForm;
1150 if (!IsHandleCreated) {
1154 Application.RunLoop(true, new ApplicationContext(this));
1156 if (previous != null) {
1157 // Cannot use Activate(), it has a check for the current active window...
1158 XplatUI.Activate(previous.window.Handle);
1161 return DialogResult;
1164 public override string ToString() {
1165 return GetType().FullName.ToString() + ", Text: " + Text;
1167 #endregion // Public Instance Methods
1169 #region Protected Instance Methods
1170 [MonoTODO("Finish when MDI is more complete")]
1171 protected void ActivateMdiChild(Form form) {
1172 OnMdiChildActivate(EventArgs.Empty);
1173 throw new NotImplementedException();
1176 [EditorBrowsable(EditorBrowsableState.Advanced)]
1177 protected override void AdjustFormScrollbars(bool displayScrollbars) {
1178 base.AdjustFormScrollbars (displayScrollbars);
1181 [EditorBrowsable(EditorBrowsableState.Advanced)]
1182 protected void ApplyAutoScaling()
1184 SizeF current_size_f = GetAutoScaleSize (DeviceContext, Font);
1185 Size current_size = new Size ((int) current_size_f.Width, (int) current_size_f.Height);
1187 if (current_size == autoscale_base_size)
1190 if (Environment.GetEnvironmentVariable ("MONO_MWF_SCALING") == "disable"){
1191 Console.WriteLine ("Not scaling");
1196 // I tried applying the Fudge height factor from:
1197 // http://blogs.msdn.com/mharsh/archive/2004/01/25/62621.aspx
1198 // but it makes things larger without looking better.
1200 Scale (current_size_f.Width / AutoScaleBaseSize.Width + 0.08f,
1201 current_size_f.Height / AutoScaleBaseSize.Height + 0.08f);
1203 AutoScaleBaseSize = current_size;
1206 protected void CenterToParent() {
1214 w = DefaultSize.Width;
1220 h = DefaultSize.Height;
1224 if (parent != null) {
1226 } else if (owner != null) {
1230 if (owner != null) {
1231 this.Location = new Point(ctl.Left + ctl.Width / 2 - w /2, ctl.Top + ctl.Height / 2 - h / 2);
1235 protected void CenterToScreen() {
1243 w = DefaultSize.Width;
1249 h = DefaultSize.Height;
1252 XplatUI.GetDisplaySize(out DisplaySize);
1253 this.Location = new Point(DisplaySize.Width / 2 - w / 2, DisplaySize.Height / 2 - h / 2);
1256 [EditorBrowsable(EditorBrowsableState.Advanced)]
1257 protected override Control.ControlCollection CreateControlsInstance() {
1258 return base.CreateControlsInstance ();
1261 [EditorBrowsable(EditorBrowsableState.Advanced)]
1262 protected override void CreateHandle() {
1263 base.CreateHandle ();
1265 XplatUI.SetWindowMinMax(window.Handle, maximized_bounds, minimum_size, maximum_size);
1267 XplatUI.SetIcon(window.Handle, icon);
1271 [EditorBrowsable(EditorBrowsableState.Advanced)]
1272 protected override void DefWndProc(ref Message m) {
1273 base.DefWndProc (ref m);
1276 protected override void Dispose(bool disposing) {
1277 base.Dispose (disposing);
1280 [EditorBrowsable(EditorBrowsableState.Advanced)]
1281 protected virtual void OnActivated(EventArgs e) {
1282 if (Activated != null) {
1287 [EditorBrowsable(EditorBrowsableState.Advanced)]
1288 protected virtual void OnClosed(EventArgs e) {
1289 if (Closed != null) {
1294 [EditorBrowsable(EditorBrowsableState.Advanced)]
1295 protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
1296 if (Closing != null) {
1301 [EditorBrowsable(EditorBrowsableState.Advanced)]
1302 protected override void OnCreateControl() {
1303 base.OnCreateControl ();
1304 if (this.ActiveControl == null) {
1307 // This visible hack is to work around CanSelect always being false if one of the parents
1308 // is not visible; and we by default create Form invisible...
1309 visible = this.is_visible;
1310 this.is_visible = true;
1312 if (SelectNextControl(this, true, true, true, true) == false) {
1316 this.is_visible = visible;
1320 XplatUI.SetMenu(window.Handle, menu);
1323 OnLoad(EventArgs.Empty);
1325 // Send initial location
1326 OnLocationChanged(EventArgs.Empty);
1329 [EditorBrowsable(EditorBrowsableState.Advanced)]
1330 protected virtual void OnDeactivate(EventArgs e) {
1331 if (Deactivate != null) {
1332 Deactivate(this, e);
1336 [EditorBrowsable(EditorBrowsableState.Advanced)]
1337 protected override void OnFontChanged(EventArgs e) {
1338 base.OnFontChanged (e);
1341 [EditorBrowsable(EditorBrowsableState.Advanced)]
1342 protected override void OnHandleCreated(EventArgs e) {
1343 XplatUI.SetBorderStyle(window.Handle, form_border_style);
1344 base.OnHandleCreated (e);
1347 [EditorBrowsable(EditorBrowsableState.Advanced)]
1348 protected override void OnHandleDestroyed(EventArgs e) {
1349 base.OnHandleDestroyed (e);
1352 [EditorBrowsable(EditorBrowsableState.Advanced)]
1353 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
1354 if (InputLanguageChanged!=null) {
1355 InputLanguageChanged(this, e);
1359 [EditorBrowsable(EditorBrowsableState.Advanced)]
1360 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
1361 if (InputLanguageChanging!=null) {
1362 InputLanguageChanging(this, e);
1366 [EditorBrowsable(EditorBrowsableState.Advanced)]
1367 protected virtual void OnLoad(EventArgs e) {
1373 ApplyAutoScaling ();
1378 switch (StartPosition) {
1379 case FormStartPosition.CenterScreen:
1380 this.CenterToScreen();
1382 case FormStartPosition.CenterParent:
1383 this.CenterToParent ();
1387 Left = 25 * MdiParent.MdiContainer.ChildrenCreated + 1;
1388 Top = 25 * MdiParent.MdiContainer.ChildrenCreated + 1;
1389 MdiParent.MdiContainer.ChildrenCreated++;
1394 [EditorBrowsable(EditorBrowsableState.Advanced)]
1395 protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
1396 if (MaximizedBoundsChanged != null) {
1397 MaximizedBoundsChanged(this, e);
1401 [EditorBrowsable(EditorBrowsableState.Advanced)]
1402 protected virtual void OnMaximumSizeChanged(EventArgs e) {
1403 if (MaximumSizeChanged != null) {
1404 MaximumSizeChanged(this, e);
1408 [EditorBrowsable(EditorBrowsableState.Advanced)]
1409 protected virtual void OnMdiChildActivate(EventArgs e) {
1410 if (MdiChildActivate != null) {
1411 MdiChildActivate(this, e);
1415 [EditorBrowsable(EditorBrowsableState.Advanced)]
1416 protected virtual void OnMenuComplete(EventArgs e) {
1417 if (MenuComplete != null) {
1418 MenuComplete(this, e);
1422 [EditorBrowsable(EditorBrowsableState.Advanced)]
1423 protected virtual void OnMenuStart(EventArgs e) {
1424 if (MenuStart != null) {
1429 [EditorBrowsable(EditorBrowsableState.Advanced)]
1430 protected virtual void OnMinimumSizeChanged(EventArgs e) {
1431 if (MinimumSizeChanged != null) {
1432 MinimumSizeChanged(this, e);
1436 [EditorBrowsable(EditorBrowsableState.Advanced)]
1437 protected override void OnPaint (PaintEventArgs pevent) {
1438 base.OnPaint (pevent);
1441 [EditorBrowsable(EditorBrowsableState.Advanced)]
1442 protected override void OnResize(EventArgs e) {
1446 [EditorBrowsable(EditorBrowsableState.Advanced)]
1447 protected override void OnStyleChanged(EventArgs e) {
1448 base.OnStyleChanged (e);
1451 [EditorBrowsable(EditorBrowsableState.Advanced)]
1452 protected override void OnTextChanged(EventArgs e) {
1453 base.OnTextChanged (e);
1456 [EditorBrowsable(EditorBrowsableState.Advanced)]
1457 protected override void OnVisibleChanged(EventArgs e) {
1458 base.OnVisibleChanged (e);
1461 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
1462 if (base.ProcessCmdKey (ref msg, keyData)) {
1466 // Give our menu a shot
1467 if (ActiveMenu != null) {
1468 return ActiveMenu.ProcessCmdKey(ref msg, keyData);
1474 // LAMESPEC - Not documented that Form overrides ProcessDialogChar; class-status showed
1475 [EditorBrowsable (EditorBrowsableState.Advanced)]
1476 protected override bool ProcessDialogChar(char charCode) {
1477 return base.ProcessDialogChar (charCode);
1480 protected override bool ProcessDialogKey(Keys keyData) {
1481 if ((keyData & Keys.Modifiers) == 0) {
1482 if (keyData == Keys.Enter && accept_button != null) {
1483 accept_button.PerformClick();
1485 } else if (keyData == Keys.Escape && cancel_button != null) {
1486 cancel_button.PerformClick();
1490 return base.ProcessDialogKey(keyData);
1493 protected override bool ProcessKeyPreview(ref Message msg) {
1495 if (ProcessKeyEventArgs(ref msg)) {
1499 return base.ProcessKeyPreview (ref msg);
1502 protected override bool ProcessTabKey(bool forward) {
1503 return SelectNextControl(ActiveControl, forward, true, true, true);
1506 [EditorBrowsable(EditorBrowsableState.Advanced)]
1507 protected override void ScaleCore(float dx, float dy) {
1508 // We can't scale max or min windows
1509 if (WindowState == FormWindowState.Normal) {
1510 // We cannot call base since base also adjusts X/Y, but
1511 // a form is toplevel and doesn't move
1517 if (!GetStyle(ControlStyles.FixedWidth)) {
1518 size.Width = (int)(size.Width * dx);
1521 if (!GetStyle(ControlStyles.FixedHeight)) {
1522 size.Height = (int)(size.Height * dy);
1527 /* Now scale our children */
1528 Control [] controls = child_controls.GetAllControls ();
1529 for (int i=0; i < controls.Length; i++) {
1530 controls[i].Scale(dx, dy);
1537 protected override void Select(bool directed, bool forward) {
1541 base.SelectNextControl(null, forward, true, true, true);
1544 parent = this.ParentForm;
1545 if (parent != null) {
1546 parent.ActiveControl = this;
1552 [EditorBrowsable(EditorBrowsableState.Advanced)]
1553 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
1554 base.SetBoundsCore (x, y, width, height, specified);
1557 [EditorBrowsable(EditorBrowsableState.Advanced)]
1558 protected override void SetClientSizeCore(int x, int y) {
1559 if ((minimum_size.Width != 0) && (x < minimum_size.Width)) {
1560 x = minimum_size.Width;
1561 } else if ((maximum_size.Width != 0) && (x > maximum_size.Width)) {
1562 x = maximum_size.Width;
1565 if ((minimum_size.Height != 0) && (y < minimum_size.Height)) {
1566 y = minimum_size.Height;
1567 } else if ((maximum_size.Height != 0) && (y > maximum_size.Height)) {
1568 y = maximum_size.Height;
1571 Rectangle ClientRect = new Rectangle(0, 0, x, y);
1572 Rectangle WindowRect;
1573 CreateParams cp = this.CreateParams;
1575 clientsize_set = true;
1577 if (XplatUI.CalculateWindowRect(ref ClientRect, cp.Style, cp.ExStyle, ActiveMenu, out WindowRect)) {
1578 SetBoundsCore(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
1582 [EditorBrowsable(EditorBrowsableState.Advanced)]
1583 protected override void SetVisibleCore(bool value) {
1584 base.SetVisibleCore (value);
1587 protected override void UpdateDefaultButton() {
1588 base.UpdateDefaultButton ();
1591 [EditorBrowsable(EditorBrowsableState.Advanced)]
1592 protected override void WndProc(ref Message m) {
1594 if (window_manager != null && window_manager.HandleMessage (ref m)) {
1598 switch((Msg)m.Msg) {
1599 case Msg.WM_DESTROY: {
1600 base.WndProc(ref m);
1601 if (!is_recreating) {
1602 this.closing = true;
1607 case Msg.WM_CLOSE_INTERNAL: {
1612 case Msg.WM_CLOSE: {
1614 CancelEventArgs args = new CancelEventArgs ();
1618 OnClosed (EventArgs.Empty);
1628 case Msg.WM_WINDOWPOSCHANGED: {
1629 if (WindowState != FormWindowState.Minimized) {
1630 base.WndProc(ref m);
1635 case Msg.WM_ACTIVATE: {
1636 if (m.WParam != (IntPtr)WindowActiveFlags.WA_INACTIVE) {
1637 OnActivated(EventArgs.Empty);
1639 OnDeactivate(EventArgs.Empty);
1644 case Msg.WM_KILLFOCUS: {
1645 base.WndProc(ref m);
1649 case Msg.WM_SETFOCUS: {
1650 if (ActiveControl != null && ActiveControl != this) {
1651 ActiveControl.Focus();
1652 return; // FIXME - do we need to run base.WndProc, even though we just changed focus?
1654 base.WndProc(ref m);
1659 case Msg.WM_NCLBUTTONDOWN: {
1660 if (ActiveMenu != null) {
1661 ActiveMenu.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0));
1664 if (ActiveMaximizedMdiChild != null) {
1665 ActiveMaximizedMdiChild.HandleMenuMouseDown (ActiveMenu,
1666 LowOrder ((int) m.LParam.ToInt32 ()),
1667 HighOrder ((int) m.LParam.ToInt32 ()));
1669 base.WndProc(ref m);
1673 case Msg.WM_NCMOUSEMOVE: {
1674 if (ActiveMenu != null) {
1675 ActiveMenu.OnMouseMove(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0));
1677 base.WndProc(ref m);
1681 case Msg.WM_NCPAINT: {
1682 if (ActiveMenu != null) {
1686 pe = XplatUI.PaintEventStart(Handle, false);
1687 pnt = XplatUI.GetMenuOrigin(window.Handle);
1689 ActiveMenu.Draw (pe, new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0));
1691 if (ActiveMaximizedMdiChild != null) {
1692 ActiveMaximizedMdiChild.DrawMaximizedButtons (pe, ActiveMenu);
1695 XplatUI.PaintEventEnd(Handle, false);
1698 base.WndProc(ref m);
1702 case Msg.WM_NCCALCSIZE: {
1703 XplatUIWin32.NCCALCSIZE_PARAMS ncp;
1705 if ((ActiveMenu != null) && (m.WParam == (IntPtr)1)) {
1706 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
1709 ncp.rgrc1.top += ThemeEngine.Current.CalcMenuBarSize (DeviceContext, ActiveMenu, ClientSize.Width);
1710 Marshal.StructureToPtr(ncp, m.LParam, true);
1716 case Msg.WM_MOUSEMOVE: {
1717 if (active_tracker != null) {
1718 MouseEventArgs args;
1720 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
1721 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
1722 active_tracker.OnMotion(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
1725 base.WndProc(ref m);
1729 case Msg.WM_LBUTTONDOWN:
1730 case Msg.WM_MBUTTONDOWN:
1731 case Msg.WM_RBUTTONDOWN: {
1732 if (active_tracker != null) {
1733 MouseEventArgs args;
1735 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
1736 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
1737 active_tracker.OnClick(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
1740 base.WndProc(ref m);
1744 case Msg.WM_GETMINMAXINFO: {
1747 if (m.LParam != IntPtr.Zero) {
1748 mmi = (MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO));
1750 default_maximized_bounds = new Rectangle(mmi.ptMaxPosition.x, mmi.ptMaxPosition.y, mmi.ptMaxSize.x, mmi.ptMaxSize.y);
1751 if (maximized_bounds != Rectangle.Empty) {
1752 mmi.ptMaxPosition.x = maximized_bounds.Left;
1753 mmi.ptMaxPosition.y = maximized_bounds.Top;
1754 mmi.ptMaxSize.x = maximized_bounds.Width;
1755 mmi.ptMaxSize.y = maximized_bounds.Height;
1758 if (minimum_size != Size.Empty) {
1759 mmi.ptMinTrackSize.x = minimum_size.Width;
1760 mmi.ptMinTrackSize.y = minimum_size.Height;
1763 if (minimum_size != Size.Empty) {
1764 mmi.ptMaxTrackSize.x = maximum_size.Width;
1765 mmi.ptMaxTrackSize.y = maximum_size.Height;
1767 Marshal.StructureToPtr(mmi, m.LParam, false);
1773 base.WndProc (ref m);
1778 #endregion // Protected Instance Methods
1780 internal void RemoveWindowManager ()
1782 window_manager = null;
1785 internal override void CheckAcceptButton()
1787 if (accept_button != null) {
1788 Button a_button = accept_button as Button;
1790 if (ActiveControl == a_button)
1793 if (ActiveControl is Button) {
1794 a_button.paint_as_acceptbutton = false;
1798 a_button.paint_as_acceptbutton = true;
1805 public event EventHandler Activated;
1806 public event EventHandler Closed;
1807 public event CancelEventHandler Closing;
1808 public event EventHandler Deactivate;
1809 public event InputLanguageChangedEventHandler InputLanguageChanged;
1810 public event InputLanguageChangingEventHandler InputLanguageChanging;
1811 public event EventHandler Load;
1812 public event EventHandler MaximizedBoundsChanged;
1813 public event EventHandler MaximumSizeChanged;
1814 public event EventHandler MdiChildActivate;
1815 public event EventHandler MenuComplete;
1816 public event EventHandler MenuStart;
1817 public event EventHandler MinimumSizeChanged;
1820 [EditorBrowsable(EditorBrowsableState.Never)]
1821 public new event EventHandler TabIndexChanged;
1822 #endregion // Events