* AccessibleObject.cs, Control.cs, XplatUIX11GTK.cs,
[mcs.git] / class / Managed.Windows.Forms / System.Windows.Forms / Form.cs
blob36bfea95b4bc35aaedeb865a496e5fa79e61aa99
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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.
22 // Authors:
23 // Peter Bartok pbartok@novell.com
26 // NOT COMPLETE
28 using System;
29 using System.Drawing;
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")]
42 [ToolboxItem(false)]
43 public class Form : ContainerControl {
44 #region Local Variables
45 internal bool closing;
46 FormBorderStyle form_border_style;
47 private bool autoscale;
48 private Size clientsize_set;
49 private Size autoscale_base_size;
50 private bool allow_transparency;
51 private static Icon default_icon;
52 internal bool is_modal;
53 internal FormWindowState window_state;
54 private bool control_box;
55 private bool minimize_box;
56 private bool maximize_box;
57 private bool help_button;
58 private bool show_in_taskbar;
59 private bool topmost;
60 private IButtonControl accept_button;
61 private IButtonControl cancel_button;
62 private DialogResult dialog_result;
63 private FormStartPosition start_position;
64 private Form owner;
65 private Form.ControlCollection owned_forms;
66 private MdiClient mdi_container;
67 internal InternalWindowManager window_manager;
68 private Form mdi_parent;
69 private bool key_preview;
70 private MainMenu menu;
71 private Icon icon;
72 private Size maximum_size;
73 private Size minimum_size;
74 private SizeGripStyle size_grip_style;
75 private Rectangle maximized_bounds;
76 private Rectangle default_maximized_bounds;
77 private double opacity;
78 internal ApplicationContext context;
79 Color transparency_key;
80 internal MenuTracker active_tracker;
81 private bool is_loaded;
82 internal bool is_changing_visible_state;
83 internal bool has_been_visible;
85 #endregion // Local Variables
87 #region Private & Internal Methods
88 static Form ()
90 default_icon = Locale.GetResource("mono.ico") as Icon;
93 // warning: this is only hooked up when an mdi container is created.
94 private void ControlAddedHandler (object sender, ControlEventArgs e)
96 if (mdi_container != null) {
97 mdi_container.SendToBack ();
101 private void SelectActiveControl ()
103 if (this.IsMdiContainer)
104 return;
106 if (this.ActiveControl == null) {
107 bool visible;
109 // This visible hack is to work around CanSelect always being false if one of the parents
110 // is not visible; and we by default create Form invisible...
111 visible = this.is_visible;
112 this.is_visible = true;
114 if (SelectNextControl (this, true, true, true, true) == false) {
115 Select (this);
118 this.is_visible = visible;
119 } else {
120 Select (ActiveControl);
123 #endregion // Private & Internal Methods
125 #region Public Classes
126 public new class ControlCollection : Control.ControlCollection {
127 Form form_owner;
129 public ControlCollection(Form owner) : base(owner) {
130 this.form_owner = owner;
133 public override void Add(Control value) {
134 if (Contains (value))
135 return;
136 AddToList (value);
137 ((Form)value).owner=form_owner;
140 public override void Remove(Control value) {
141 ((Form)value).owner = null;
142 base.Remove (value);
145 #endregion // Public Classes
147 #region Public Constructor & Destructor
148 public Form ()
150 SizeF current_scale = GetAutoScaleSize (DeviceContext, Font);
152 autoscale = true;
153 autoscale_base_size = new Size ((int)current_scale.Width, (int) current_scale.Height);
154 allow_transparency = false;
155 closing = false;
156 is_modal = false;
157 dialog_result = DialogResult.None;
158 start_position = FormStartPosition.WindowsDefaultLocation;
159 form_border_style = FormBorderStyle.Sizable;
160 window_state = FormWindowState.Normal;
161 key_preview = false;
162 opacity = 1D;
163 menu = null;
164 icon = default_icon;
165 minimum_size = Size.Empty;
166 maximum_size = Size.Empty;
167 clientsize_set = Size.Empty;
168 control_box = true;
169 minimize_box = true;
170 maximize_box = true;
171 help_button = false;
172 show_in_taskbar = true;
173 ime_mode = ImeMode.NoControl;
174 is_visible = false;
175 is_toplevel = true;
176 size_grip_style = SizeGripStyle.Auto;
177 maximized_bounds = Rectangle.Empty;
178 default_maximized_bounds = Rectangle.Empty;
179 owned_forms = new Form.ControlCollection(this);
180 transparency_key = Color.Empty;
182 // FIXME: this should disappear just as soon as the handle creation is done in the right place (here is too soon()
183 UpdateBounds();
186 #endregion // Public Constructor & Destructor
188 #region Public Static Properties
190 public static Form ActiveForm {
191 get {
192 Control active;
194 active = FromHandle(XplatUI.GetActive());
196 if (active != null) {
197 if ( !(active is Form)) {
198 Control parent;
200 parent = active.Parent;
201 while (parent != null) {
202 if (parent is Form) {
203 return (Form)parent;
205 parent = parent.Parent;
207 } else {
208 return (Form)active;
211 return null;
215 #endregion // Public Static Properties
217 #region Public Instance Properties
218 [DefaultValue(null)]
219 public IButtonControl AcceptButton {
220 get {
221 return accept_button;
224 set {
225 accept_button = value;
226 CheckAcceptButton();
230 [Browsable(false)]
231 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
232 public bool AllowTransparency {
233 get {
234 return allow_transparency;
237 set {
238 if (value == allow_transparency) {
239 return;
242 allow_transparency = value;
244 if (value) {
245 if (IsHandleCreated) {
246 if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
247 XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
249 } else {
250 UpdateStyles(); // Remove the WS_EX_LAYERED style
256 #if NET_2_0
257 #else
258 [DefaultValue(true)]
259 #endif
260 [MWFCategory("Layout")]
261 public bool AutoScale {
262 get {
263 return autoscale;
266 set {
267 autoscale = value;
271 [Localizable(true)]
272 [Browsable(false)]
273 [EditorBrowsable(EditorBrowsableState.Advanced)]
274 public virtual Size AutoScaleBaseSize {
275 get {
276 return autoscale_base_size;
279 set {
280 autoscale_base_size = value;
284 [Localizable(true)]
285 public override bool AutoScroll {
286 get {
287 return base.AutoScroll;
289 set {
290 base.AutoScroll = value;
294 public override Color BackColor {
295 get {
296 /* we don't let parents override our
297 default background color for forms.
298 this fixes the default color for mdi
299 children. */
300 if (background_color.IsEmpty)
301 return DefaultBackColor;
302 else
303 return background_color;
305 set {
306 base.BackColor = value;
310 [DefaultValue(null)]
311 public IButtonControl CancelButton {
312 get {
313 return cancel_button;
316 set {
317 cancel_button = value;
321 // new property so we can change the DesignerSerializationVisibility
322 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
323 [Localizable(true)]
324 public new Size ClientSize {
325 get { return base.ClientSize; }
326 set { base.ClientSize = value; }
329 [DefaultValue(true)]
330 [MWFCategory("Window Style")]
331 public bool ControlBox {
332 get {
333 return control_box;
336 set {
337 if (control_box != value) {
338 control_box = value;
339 UpdateStyles();
344 [Browsable(false)]
345 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
346 public Rectangle DesktopBounds {
347 get {
348 return new Rectangle(Location, Size);
351 set {
352 Bounds = value;
356 [Browsable(false)]
357 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
358 public Point DesktopLocation {
359 get {
360 return Location;
363 set {
364 Location = value;
368 [Browsable(false)]
369 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
370 public DialogResult DialogResult {
371 get {
372 return dialog_result;
375 set {
376 if (value < DialogResult.None || value > DialogResult.No)
377 throw new InvalidEnumArgumentException ("value", (int) value,
378 typeof (DialogResult));
380 dialog_result = value;
381 closing = (dialog_result != DialogResult.None && is_modal);
385 [DefaultValue(FormBorderStyle.Sizable)]
386 [DispId(-504)]
387 [MWFCategory("Appearance")]
388 public FormBorderStyle FormBorderStyle {
389 get {
390 return form_border_style;
392 set {
393 form_border_style = value;
395 if (window_manager == null) {
396 if (IsHandleCreated) {
397 XplatUI.SetBorderStyle(window.Handle, form_border_style);
399 } else {
400 window_manager.UpdateBorderStyle (value);
403 UpdateStyles();
407 [DefaultValue(false)]
408 [MWFCategory("Window Style")]
409 public bool HelpButton {
410 get {
411 return help_button;
414 set {
415 if (help_button != value) {
416 help_button = value;
417 UpdateStyles();
422 [Localizable(true)]
423 [AmbientValue(null)]
424 [MWFCategory("Window Style")]
425 public Icon Icon {
426 get {
427 return icon;
430 set {
431 if (icon != value) {
432 icon = value;
434 if (IsHandleCreated) {
435 XplatUI.SetIcon(Handle, icon == null ? default_icon : icon);
441 [Browsable(false)]
442 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
443 public bool IsMdiChild {
444 get {
445 return mdi_parent != null;
449 [DefaultValue(false)]
450 [MWFCategory("Window Style")]
451 public bool IsMdiContainer {
452 get {
453 return mdi_container != null;
456 set {
457 if (value && mdi_container == null) {
458 mdi_container = new MdiClient ();
459 Controls.Add(mdi_container);
460 ControlAdded += new ControlEventHandler (ControlAddedHandler);
461 mdi_container.SendToBack ();
462 mdi_container.SetParentText (true);
463 } else if (!value && mdi_container != null) {
464 Controls.Remove(mdi_container);
465 mdi_container = null;
470 [Browsable(false)]
471 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
472 public Form ActiveMdiChild {
473 get {
474 if (!IsMdiContainer)
475 return null;
476 return (Form) mdi_container.ActiveMdiChild;
480 [Browsable(false)]
481 [EditorBrowsable(EditorBrowsableState.Advanced)]
482 public bool IsRestrictedWindow {
483 get {
484 return false;
488 [DefaultValue(false)]
489 public bool KeyPreview {
490 get {
491 return key_preview;
494 set {
495 key_preview = value;
499 [DefaultValue(true)]
500 [MWFCategory("Window Style")]
501 public bool MaximizeBox {
502 get {
503 return maximize_box;
505 set {
506 if (maximize_box != value) {
507 maximize_box = value;
508 if (IsHandleCreated) {
509 RecreateHandle();
511 UpdateStyles();
516 [DefaultValue("{Width=0, Height=0}")]
517 [Localizable(true)]
518 [RefreshProperties(RefreshProperties.Repaint)]
519 [MWFCategory("Layout")]
520 public
521 #if NET_2_0
522 override
523 #endif
524 Size MaximumSize {
525 get {
526 return maximum_size;
529 set {
530 if (maximum_size != value) {
531 maximum_size = value;
532 OnMaximumSizeChanged(EventArgs.Empty);
533 if (IsHandleCreated) {
534 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
540 [Browsable(false)]
541 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
542 public Form[] MdiChildren {
543 get {
544 if (mdi_container != null)
545 return mdi_container.MdiChildren;
546 else
547 return new Form[0];
551 [Browsable(false)]
552 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
553 public Form MdiParent {
554 get {
555 return mdi_parent;
558 set {
559 if (value != null && !value.IsMdiContainer)
560 throw new ArgumentException ();
562 if (mdi_parent != null) {
563 mdi_parent.MdiContainer.Controls.Remove (this);
566 if (value != null) {
567 mdi_parent = value;
568 window_manager = new MdiWindowManager (this,
569 mdi_parent.MdiContainer);
570 mdi_parent.MdiContainer.Controls.Add (this);
571 mdi_parent.MdiContainer.Controls.SetChildIndex (this, 0);
573 RecreateHandle ();
575 } else if (mdi_parent != null) {
576 mdi_parent = null;
578 // Create a new window manager
579 window_manager = null;
580 FormBorderStyle = form_border_style;
582 RecreateHandle ();
587 internal MenuTracker ActiveTracker {
588 get { return active_tracker; }
589 set {
590 if (value == active_tracker)
591 return;
593 Capture = value != null;
594 active_tracker = value;
598 internal MdiClient MdiContainer {
599 get { return mdi_container; }
602 internal InternalWindowManager WindowManager {
603 get { return window_manager; }
606 [DefaultValue(null)]
607 [MWFCategory("Window Style")]
608 public MainMenu Menu {
609 get {
610 return menu;
613 set {
614 if (menu != value) {
615 menu = value;
617 if (menu != null && !IsMdiChild) {
618 menu.SetForm (this);
620 if (IsHandleCreated) {
621 XplatUI.SetMenu (window.Handle, menu);
624 if (clientsize_set != Size.Empty) {
625 SetClientSizeCore(clientsize_set.Width, clientsize_set.Height);
626 } else {
627 UpdateBounds (bounds.X, bounds.Y, bounds.Width, bounds.Height, ClientSize.Width, ClientSize.Height -
628 ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu, ClientSize.Width));
630 } else
631 UpdateBounds ();
636 [Browsable(false)]
637 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
638 [EditorBrowsable(EditorBrowsableState.Advanced)]
639 public MainMenu MergedMenu {
640 get {
641 if (!IsMdiChild || window_manager == null)
642 return null;
643 return ((MdiWindowManager) window_manager).MergedMenu;
647 // This is the menu in display and being used because of merging this can
648 // be different then the menu that is actually assosciated with the form
649 internal MainMenu ActiveMenu {
650 get {
651 if (IsMdiChild)
652 return null;
654 if (IsMdiContainer && mdi_container.Controls.Count > 0 &&
655 ((Form) mdi_container.Controls [0]).WindowState == FormWindowState.Maximized) {
656 MdiWindowManager wm = (MdiWindowManager) ((Form) mdi_container.Controls [0]).WindowManager;
657 return wm.MaximizedMenu;
660 Form amc = ActiveMdiChild;
661 if (amc == null || amc.Menu == null)
662 return menu;
663 return amc.MergedMenu;
667 internal MdiWindowManager ActiveMaximizedMdiChild {
668 get {
669 Form child = ActiveMdiChild;
670 if (child == null)
671 return null;
672 if (child.WindowManager == null || child.window_state != FormWindowState.Maximized)
673 return null;
674 return (MdiWindowManager) child.WindowManager;
678 [DefaultValue(true)]
679 [MWFCategory("Window Style")]
680 public bool MinimizeBox {
681 get {
682 return minimize_box;
684 set {
685 if (minimize_box != value) {
686 minimize_box = value;
687 if (IsHandleCreated) {
688 RecreateHandle();
690 UpdateStyles();
695 #if !NET_2_0
696 [DefaultValue("{Width=0, Height=0}")]
697 #endif
698 [Localizable(true)]
699 [RefreshProperties(RefreshProperties.Repaint)]
700 [MWFCategory("Layout")]
701 public
702 #if NET_2_0
703 override
704 #endif
705 Size MinimumSize {
706 get {
707 return minimum_size;
710 set {
711 if (minimum_size != value) {
712 minimum_size = value;
714 if ((Size.Width < value.Width) || (Size.Height < value.Height)) {
715 Size = new Size(Math.Max(Size.Width, value.Width), Math.Max(Size.Height, value.Height));
719 OnMinimumSizeChanged(EventArgs.Empty);
720 if (IsHandleCreated) {
721 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
727 [Browsable(false)]
728 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
729 public bool Modal {
730 get {
731 return is_modal;
735 [DefaultValue(1D)]
736 [TypeConverter(typeof(OpacityConverter))]
737 [MWFCategory("Window Style")]
738 public double Opacity {
739 get {
740 if (IsHandleCreated) {
741 if ((XplatUI.SupportsTransparency () & TransparencySupport.Get) != 0)
742 return XplatUI.GetWindowTransparency (Handle);
745 return opacity;
748 set {
749 opacity = value;
751 AllowTransparency = true;
753 if (IsHandleCreated) {
754 UpdateStyles();
755 if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
756 XplatUI.SetWindowTransparency(Handle, opacity, TransparencyKey);
762 [Browsable(false)]
763 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
764 public Form[] OwnedForms {
765 get {
766 Form[] form_list;
768 form_list = new Form[owned_forms.Count];
770 for (int i=0; i<owned_forms.Count; i++) {
771 form_list[i] = (Form)owned_forms[i];
774 return form_list;
778 [Browsable(false)]
779 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
780 public Form Owner {
781 get {
782 return owner;
785 set {
786 if (owner != value) {
787 if (owner != null) {
788 owner.RemoveOwnedForm(this);
790 owner = value;
791 if (owner != null)
792 owner.AddOwnedForm(this);
793 if (IsHandleCreated) {
794 if (owner != null && owner.IsHandleCreated) {
795 XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
796 } else {
797 XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
804 [DefaultValue(true)]
805 [MWFCategory("Window Style")]
806 public bool ShowInTaskbar {
807 get {
808 return show_in_taskbar;
810 set {
811 if (show_in_taskbar != value) {
812 show_in_taskbar = value;
813 if (IsHandleCreated) {
814 RecreateHandle();
816 UpdateStyles();
821 // new property so we can set the DesignerSerializationVisibility
822 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
823 [Localizable(false)]
824 public new Size Size {
825 get { return base.Size; }
826 set { base.Size = value; }
829 [MonoTODO("Trigger something when GripStyle is set")]
830 [DefaultValue(SizeGripStyle.Auto)]
831 [MWFCategory("Window Style")]
832 public SizeGripStyle SizeGripStyle {
833 get {
834 return size_grip_style;
837 set {
838 size_grip_style = value;
842 [DefaultValue(FormStartPosition.WindowsDefaultLocation)]
843 [Localizable(true)]
844 [MWFCategory("Layout")]
845 public FormStartPosition StartPosition {
846 get {
847 return start_position;
850 set {
851 if (start_position == FormStartPosition.WindowsDefaultLocation) { // Only do this if it's not set yet
852 start_position = value;
853 if (IsHandleCreated) {
854 switch(start_position) {
855 case FormStartPosition.CenterParent: {
856 CenterToParent();
857 break;
860 case FormStartPosition.CenterScreen: {
861 CenterToScreen();
862 break;
865 case FormStartPosition.Manual: {
866 Left = CreateParams.X;
867 Top = CreateParams.Y;
868 break;
871 default: {
872 break;
880 // new property so we can set EditorBrowsable to never
881 [Browsable(false)]
882 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
883 [EditorBrowsable(EditorBrowsableState.Never)]
884 public new int TabIndex {
885 get { return base.TabIndex; }
886 set { base.TabIndex = value; }
889 [Browsable(false)]
890 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
891 [EditorBrowsable(EditorBrowsableState.Advanced)]
892 public bool TopLevel {
893 get {
894 return GetTopLevel();
897 set {
898 if (!value && IsMdiContainer)
899 throw new ArgumentException ("MDI Container forms must be top level.");
900 SetTopLevel(value);
904 [DefaultValue(false)]
905 [MWFCategory("Window Style")]
906 public bool TopMost {
907 get {
908 return topmost;
911 set {
912 if (topmost != value) {
913 topmost = value;
914 if (IsHandleCreated)
915 XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, value);
920 [MWFCategory("Window Style")]
921 public Color TransparencyKey {
922 get {
923 return transparency_key;
926 set {
927 transparency_key = value;
929 AllowTransparency = true;
930 UpdateStyles();
931 if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
932 XplatUI.SetWindowTransparency(Handle, Opacity, transparency_key);
936 [DefaultValue(FormWindowState.Normal)]
937 [MWFCategory("Layout")]
938 public FormWindowState WindowState {
939 get {
940 if (IsHandleCreated) {
942 if (window_manager != null)
943 return window_manager.GetWindowState ();
945 FormWindowState new_state = XplatUI.GetWindowState(Handle);
946 if (new_state != (FormWindowState)(-1))
947 window_state = new_state;
950 return window_state;
953 set {
954 FormWindowState old_state = window_state;
955 window_state = value;
956 if (IsHandleCreated) {
958 if (window_manager != null) {
959 window_manager.SetWindowState (old_state, value);
960 return;
963 XplatUI.SetWindowState(Handle, value);
968 #endregion // Public Instance Properties
970 #region Protected Instance Properties
971 protected override CreateParams CreateParams {
972 get {
973 CreateParams cp = new CreateParams ();
975 cp.Caption = Text;
976 cp.ClassName = XplatUI.DefaultClassName;
977 cp.ClassStyle = 0;
978 cp.Style = 0;
979 cp.ExStyle = 0;
980 cp.Param = 0;
981 cp.Parent = IntPtr.Zero;
982 cp.menu = ActiveMenu;
984 if (start_position == FormStartPosition.WindowsDefaultLocation && !IsMdiChild) {
985 cp.X = unchecked((int)0x80000000);
986 cp.Y = unchecked((int)0x80000000);
987 } else {
988 cp.X = Left;
989 cp.Y = Top;
991 cp.Width = Width;
992 cp.Height = Height;
994 cp.Style = (int)(WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS);
996 if (IsMdiChild) {
997 cp.Style |= (int)(WindowStyles.WS_CHILD | WindowStyles.WS_CAPTION);
998 if (Parent != null) {
999 cp.Parent = Parent.Handle;
1002 cp.ExStyle |= (int) (WindowExStyles.WS_EX_WINDOWEDGE | WindowExStyles.WS_EX_MDICHILD);
1004 switch (FormBorderStyle) {
1005 case FormBorderStyle.None:
1006 break;
1007 case FormBorderStyle.FixedToolWindow:
1008 case FormBorderStyle.SizableToolWindow:
1009 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW;
1010 goto default;
1011 default:
1012 cp.Style |= (int) WindowStyles.WS_OVERLAPPEDWINDOW;
1013 break;
1016 } else {
1017 switch (FormBorderStyle) {
1018 case FormBorderStyle.Fixed3D: {
1019 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1020 cp.ExStyle |= (int)WindowExStyles.WS_EX_CLIENTEDGE;
1021 break;
1024 case FormBorderStyle.FixedDialog: {
1025 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1026 cp.ExStyle |= (int)(WindowExStyles.WS_EX_DLGMODALFRAME | WindowExStyles.WS_EX_CONTROLPARENT);
1027 break;
1030 case FormBorderStyle.FixedSingle: {
1031 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1032 break;
1035 case FormBorderStyle.FixedToolWindow: {
1036 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1037 cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1038 break;
1041 case FormBorderStyle.Sizable: {
1042 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
1043 break;
1046 case FormBorderStyle.SizableToolWindow: {
1047 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
1048 cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1049 break;
1052 case FormBorderStyle.None: {
1053 break;
1058 switch(window_state) {
1059 case FormWindowState.Maximized: {
1060 cp.Style |= (int)WindowStyles.WS_MAXIMIZE;
1061 break;
1064 case FormWindowState.Minimized: {
1065 cp.Style |= (int)WindowStyles.WS_MINIMIZE;
1066 break;
1070 if (TopMost) {
1071 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOPMOST;
1074 if (ShowInTaskbar) {
1075 cp.ExStyle |= (int)WindowExStyles.WS_EX_APPWINDOW;
1078 if (MaximizeBox) {
1079 cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
1082 if (MinimizeBox) {
1083 cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
1086 if (ControlBox) {
1087 cp.Style |= (int)WindowStyles.WS_SYSMENU;
1090 if (HelpButton && !MaximizeBox && !MinimizeBox) {
1091 cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTEXTHELP;
1094 if (Visible)
1095 cp.Style |= (int)WindowStyles.WS_VISIBLE;
1097 if (Opacity < 1.0 || TransparencyKey != Color.Empty) {
1098 cp.ExStyle |= (int)WindowExStyles.WS_EX_LAYERED;
1101 if (!is_enabled && context == null) {
1102 cp.Style |= (int)(WindowStyles.WS_DISABLED);
1105 return cp;
1109 protected override ImeMode DefaultImeMode {
1110 get {
1111 return ImeMode.NoControl;
1115 protected override Size DefaultSize {
1116 get {
1117 return new Size (300, 300);
1121 protected Rectangle MaximizedBounds {
1122 get {
1123 if (maximized_bounds != Rectangle.Empty) {
1124 return maximized_bounds;
1126 return default_maximized_bounds;
1129 set {
1130 maximized_bounds = value;
1131 OnMaximizedBoundsChanged(EventArgs.Empty);
1132 if (IsHandleCreated) {
1133 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
1137 #endregion // Protected Instance Properties
1139 #region Public Static Methods
1140 [EditorBrowsable(EditorBrowsableState.Advanced)]
1141 public static SizeF GetAutoScaleSize (Font font)
1143 return XplatUI.GetAutoScaleSize(font);
1146 #endregion // Public Static Methods
1148 #region Public Instance Methods
1149 internal SizeF GetAutoScaleSize (Graphics g, Font font)
1152 // The following constants come from the dotnet mailing list
1153 // discussion: http://discuss.develop.com/archives/wa.exe?A2=ind0203A&L=DOTNET&P=R3655
1155 // The magic number is "Its almost the length
1156 // of the string with a smattering added in
1157 // for compat with earlier code".
1160 string magic_string = "The quick brown fox jumped over the lazy dog.";
1161 double magic_number = 44.549996948242189;
1162 float width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
1164 return new SizeF (width, font.Height);
1167 public void Activate() {
1168 Form active;
1170 // The docs say activate only activates if our app is already active
1171 if (IsHandleCreated) {
1172 if (IsMdiChild) {
1173 MdiParent.ActivateMdiChild (this);
1174 } else {
1175 active = ActiveForm;
1176 if ((active != null) && (this != active)) {
1177 XplatUI.Activate(window.Handle);
1183 public void AddOwnedForm(Form ownedForm) {
1184 if (!owned_forms.Contains(ownedForm)) {
1185 owned_forms.Add(ownedForm);
1187 ownedForm.Owner = this;
1190 public void Close () {
1191 if (IsDisposed)
1192 return;
1194 if (!is_visible)
1195 return;
1197 #if NET_2_0
1198 FormClosingEventArgs ce = new FormClosingEventArgs (CloseReason.FormOwnerClosing, false);
1199 OnFormClosing (ce);
1200 if (ce.Cancel)
1201 return;
1202 #endif
1203 XplatUI.SendMessage(this.Handle, Msg.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
1206 public void LayoutMdi(MdiLayout value) {
1207 if (mdi_container != null) {
1208 mdi_container.LayoutMdi(value);
1212 public void RemoveOwnedForm(Form ownedForm) {
1213 owned_forms.Remove(ownedForm);
1216 public void SetDesktopBounds(int x, int y, int width, int height) {
1217 DesktopBounds = new Rectangle(x, y, width, height);
1220 public void SetDesktopLocation(int x, int y) {
1221 DesktopLocation = new Point(x, y);
1224 public DialogResult ShowDialog() {
1225 return ShowDialog(this.owner);
1228 public DialogResult ShowDialog(IWin32Window ownerWin32) {
1229 Rectangle area;
1230 bool confined;
1231 IntPtr capture_window;
1233 owner = null;
1235 if (ownerWin32 != null) {
1236 Control c = Control.FromHandle (ownerWin32.Handle);
1237 if (c != null)
1238 owner = c.TopLevelControl as Form;
1241 if (owner == this) {
1242 throw new InvalidOperationException("The 'ownerWin32' cannot be the form being shown.");
1245 if (is_modal) {
1246 throw new InvalidOperationException("The form is already displayed as a modal dialog.");
1249 if (Visible) {
1250 throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
1253 if (!Enabled) {
1254 throw new InvalidOperationException("Cannot display a disabled form as modal dialog.");
1257 if (TopLevelControl != this) {
1258 throw new InvalidOperationException("Can only display TopLevel forms as modal dialog.");
1261 #if broken
1262 // Can't do this, will screw us in the modal loop
1263 form_parent_window.Parent = this.owner;
1264 #endif
1266 // Release any captures
1267 XplatUI.GrabInfo(out capture_window, out confined, out area);
1268 if (capture_window != IntPtr.Zero) {
1269 XplatUI.UngrabWindow(capture_window);
1272 #if not
1273 // Commented out; we instead let the Visible=true inside the runloop create the control
1274 // otherwise setting DialogResult inside any of the events that are triggered by the
1275 // create will not actually cause the form to not be displayed.
1276 // Leaving this comment here in case there was an actual purpose to creating the control
1277 // in here.
1278 if (!IsHandleCreated) {
1279 CreateControl();
1281 #endif
1283 Application.RunLoop(true, new ApplicationContext(this));
1285 if (owner != null) {
1286 // Cannot use Activate(), it has a check for the current active window...
1287 XplatUI.Activate(owner.window.Handle);
1290 if (DialogResult != DialogResult.None) {
1291 return DialogResult;
1293 DialogResult = DialogResult.Cancel;
1294 return DialogResult.Cancel;
1297 public override string ToString() {
1298 return GetType().FullName.ToString() + ", Text: " + Text;
1300 #endregion // Public Instance Methods
1302 #region Protected Instance Methods
1303 protected void ActivateMdiChild(Form form) {
1304 if (!IsMdiContainer)
1305 return;
1306 mdi_container.ActivateChild (form);
1307 OnMdiChildActivate(EventArgs.Empty);
1310 [EditorBrowsable(EditorBrowsableState.Advanced)]
1311 protected override void AdjustFormScrollbars(bool displayScrollbars) {
1312 base.AdjustFormScrollbars (displayScrollbars);
1315 [EditorBrowsable(EditorBrowsableState.Advanced)]
1316 protected void ApplyAutoScaling()
1318 SizeF current_size_f = GetAutoScaleSize (DeviceContext, Font);
1319 Size current_size = new Size ((int) current_size_f.Width, (int) current_size_f.Height);
1320 float dx;
1321 float dy;
1323 if (current_size == autoscale_base_size)
1324 return;
1326 if (Environment.GetEnvironmentVariable ("MONO_MWF_SCALING") == "disable"){
1327 return;
1331 // I tried applying the Fudge height factor from:
1332 // http://blogs.msdn.com/mharsh/archive/2004/01/25/62621.aspx
1333 // but it makes things larger without looking better.
1335 if (current_size_f.Width != AutoScaleBaseSize.Width) {
1336 dx = current_size_f.Width / AutoScaleBaseSize.Width + 0.08f;
1337 } else {
1338 dx = 1;
1341 if (current_size_f.Height != AutoScaleBaseSize.Height) {
1342 dy = current_size_f.Height / AutoScaleBaseSize.Height + 0.08f;
1343 } else {
1344 dy = 1;
1347 Scale (dx, dy);
1349 AutoScaleBaseSize = current_size;
1352 protected void CenterToParent() {
1353 Control ctl;
1354 int w;
1355 int h;
1357 if (Width > 0) {
1358 w = Width;
1359 } else {
1360 w = DefaultSize.Width;
1363 if (Height > 0) {
1364 h = Height;
1365 } else {
1366 h = DefaultSize.Height;
1369 ctl = null;
1370 if (Parent != null) {
1371 ctl = Parent;
1372 } else if (owner != null) {
1373 ctl = owner;
1376 if (owner != null) {
1377 this.Location = new Point(ctl.Left + ctl.Width / 2 - w /2, ctl.Top + ctl.Height / 2 - h / 2);
1381 protected void CenterToScreen() {
1382 Size DisplaySize;
1383 int w;
1384 int h;
1386 if (Width > 0) {
1387 w = Width;
1388 } else {
1389 w = DefaultSize.Width;
1392 if (Height > 0) {
1393 h = Height;
1394 } else {
1395 h = DefaultSize.Height;
1398 XplatUI.GetDisplaySize(out DisplaySize);
1399 this.Location = new Point(DisplaySize.Width / 2 - w / 2, DisplaySize.Height / 2 - h / 2);
1402 [EditorBrowsable(EditorBrowsableState.Advanced)]
1403 protected override Control.ControlCollection CreateControlsInstance() {
1404 return base.CreateControlsInstance ();
1407 [EditorBrowsable(EditorBrowsableState.Advanced)]
1408 protected override void CreateHandle() {
1409 base.CreateHandle ();
1411 UpdateBounds();
1413 if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
1414 if (allow_transparency) {
1415 XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
1419 XplatUI.SetWindowMinMax(window.Handle, maximized_bounds, minimum_size, maximum_size);
1420 if ((FormBorderStyle != FormBorderStyle.FixedDialog) && (icon != null)) {
1421 XplatUI.SetIcon(window.Handle, icon);
1424 if ((owner != null) && (owner.IsHandleCreated)) {
1425 XplatUI.SetTopmost(window.Handle, owner.window.Handle, true);
1428 for (int i = 0; i < owned_forms.Count; i++) {
1429 if (owned_forms[i].IsHandleCreated)
1430 XplatUI.SetTopmost(owned_forms[i].window.Handle, window.Handle, true);
1433 if (window_manager != null && window_state != FormWindowState.Normal) {
1434 window_manager.SetWindowState (FormWindowState.Normal, window_state);
1439 [EditorBrowsable(EditorBrowsableState.Advanced)]
1440 protected override void DefWndProc(ref Message m) {
1441 base.DefWndProc (ref m);
1444 protected override void Dispose(bool disposing)
1446 for (int i = 0; i < owned_forms.Count; i++)
1447 ((Form)owned_forms[i]).Owner = null;
1449 owned_forms.Clear ();
1451 base.Dispose (disposing);
1454 [EditorBrowsable(EditorBrowsableState.Advanced)]
1455 protected virtual void OnActivated(EventArgs e)
1457 if (is_loaded)
1458 SelectActiveControl ();
1460 EventHandler eh = (EventHandler)(Events [ActivatedEvent]);
1461 if (eh != null)
1462 eh (this, e);
1465 [EditorBrowsable(EditorBrowsableState.Advanced)]
1466 protected virtual void OnClosed(EventArgs e) {
1467 EventHandler eh = (EventHandler)(Events [ClosedEvent]);
1468 if (eh != null)
1469 eh (this, e);
1472 [EditorBrowsable(EditorBrowsableState.Advanced)]
1473 protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
1474 CancelEventHandler eh = (CancelEventHandler)(Events [ClosingEvent]);
1475 if (eh != null)
1476 eh (this, e);
1479 [EditorBrowsable(EditorBrowsableState.Advanced)]
1480 protected override void OnCreateControl() {
1481 base.OnCreateControl ();
1483 if (menu != null) {
1484 XplatUI.SetMenu(window.Handle, menu);
1487 OnLoad(EventArgs.Empty);
1489 SelectActiveControl ();
1491 // Send initial location
1492 OnLocationChanged(EventArgs.Empty);
1494 if (IsMdiContainer) {
1495 mdi_container.LayoutMdi (MdiLayout.Cascade);
1499 [EditorBrowsable(EditorBrowsableState.Advanced)]
1500 protected virtual void OnDeactivate(EventArgs e) {
1501 EventHandler eh = (EventHandler)(Events [DeactivateEvent]);
1502 if (eh != null)
1503 eh (this, e);
1506 [EditorBrowsable(EditorBrowsableState.Advanced)]
1507 protected override void OnFontChanged(EventArgs e) {
1508 base.OnFontChanged (e);
1511 [EditorBrowsable(EditorBrowsableState.Advanced)]
1512 protected override void OnHandleCreated(EventArgs e) {
1513 XplatUI.SetBorderStyle(window.Handle, form_border_style);
1514 base.OnHandleCreated (e);
1517 [EditorBrowsable(EditorBrowsableState.Advanced)]
1518 protected override void OnHandleDestroyed(EventArgs e) {
1519 base.OnHandleDestroyed (e);
1522 [EditorBrowsable(EditorBrowsableState.Advanced)]
1523 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
1524 InputLanguageChangedEventHandler eh = (InputLanguageChangedEventHandler)(Events [InputLanguageChangedEvent]);
1525 if (eh != null)
1526 eh (this, e);
1529 [EditorBrowsable(EditorBrowsableState.Advanced)]
1530 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
1531 InputLanguageChangingEventHandler eh = (InputLanguageChangingEventHandler)(Events [InputLanguageChangingEvent]);
1532 if (eh != null)
1533 eh (this, e);
1536 [EditorBrowsable(EditorBrowsableState.Advanced)]
1537 protected virtual void OnLoad(EventArgs e) {
1538 if (AutoScale){
1539 ApplyAutoScaling ();
1540 AutoScale = false;
1543 EventHandler eh = (EventHandler)(Events [LoadEvent]);
1544 if (eh != null)
1545 eh (this, e);
1547 if (!IsMdiChild) {
1548 switch (StartPosition) {
1549 case FormStartPosition.CenterScreen:
1550 this.CenterToScreen();
1551 break;
1552 case FormStartPosition.CenterParent:
1553 this.CenterToParent ();
1554 break;
1555 case FormStartPosition.Manual:
1556 Left = CreateParams.X;
1557 Top = CreateParams.Y;
1558 break;
1561 is_loaded = true;
1564 [EditorBrowsable(EditorBrowsableState.Advanced)]
1565 protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
1566 EventHandler eh = (EventHandler)(Events [MaximizedBoundsChangedEvent]);
1567 if (eh != null)
1568 eh (this, e);
1571 [EditorBrowsable(EditorBrowsableState.Advanced)]
1572 protected virtual void OnMaximumSizeChanged(EventArgs e) {
1573 EventHandler eh = (EventHandler)(Events [MaximumSizeChangedEvent]);
1574 if (eh != null)
1575 eh (this, e);
1578 [EditorBrowsable(EditorBrowsableState.Advanced)]
1579 protected virtual void OnMdiChildActivate(EventArgs e) {
1580 EventHandler eh = (EventHandler)(Events [MdiChildActivateEvent]);
1581 if (eh != null)
1582 eh (this, e);
1585 [EditorBrowsable(EditorBrowsableState.Advanced)]
1586 protected virtual void OnMenuComplete(EventArgs e) {
1587 EventHandler eh = (EventHandler)(Events [MenuCompleteEvent]);
1588 if (eh != null)
1589 eh (this, e);
1592 [EditorBrowsable(EditorBrowsableState.Advanced)]
1593 protected virtual void OnMenuStart(EventArgs e) {
1594 EventHandler eh = (EventHandler)(Events [MenuStartEvent]);
1595 if (eh != null)
1596 eh (this, e);
1599 [EditorBrowsable(EditorBrowsableState.Advanced)]
1600 protected virtual void OnMinimumSizeChanged(EventArgs e) {
1601 EventHandler eh = (EventHandler)(Events [MinimumSizeChangedEvent]);
1602 if (eh != null)
1603 eh (this, e);
1606 [EditorBrowsable(EditorBrowsableState.Advanced)]
1607 protected override void OnPaint (PaintEventArgs pevent) {
1608 base.OnPaint (pevent);
1611 [EditorBrowsable(EditorBrowsableState.Advanced)]
1612 protected override void OnResize(EventArgs e) {
1613 base.OnResize(e);
1615 if (this.IsMdiChild && ParentForm != null) {
1616 ParentForm.PerformLayout();
1617 ParentForm.Size = ParentForm.Size;
1621 [EditorBrowsable(EditorBrowsableState.Advanced)]
1622 protected override void OnStyleChanged(EventArgs e) {
1623 base.OnStyleChanged (e);
1626 [EditorBrowsable(EditorBrowsableState.Advanced)]
1627 protected override void OnTextChanged(EventArgs e) {
1628 base.OnTextChanged (e);
1630 if (mdi_container != null)
1631 mdi_container.SetParentText(true);
1634 [EditorBrowsable(EditorBrowsableState.Advanced)]
1635 protected override void OnVisibleChanged(EventArgs e) {
1636 base.OnVisibleChanged (e);
1638 if (Visible) {
1639 if (window_manager != null)
1640 window_manager.SetWindowState (WindowState, WindowState);
1644 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
1645 if (base.ProcessCmdKey (ref msg, keyData)) {
1646 return true;
1649 // Give our menu a shot
1650 if (ActiveMenu != null) {
1651 return ActiveMenu.ProcessCmdKey(ref msg, keyData);
1654 return false;
1657 // LAMESPEC - Not documented that Form overrides ProcessDialogChar; class-status showed
1658 [EditorBrowsable (EditorBrowsableState.Advanced)]
1659 protected override bool ProcessDialogChar(char charCode) {
1660 return base.ProcessDialogChar (charCode);
1663 protected override bool ProcessDialogKey(Keys keyData) {
1664 if ((keyData & Keys.Modifiers) == 0) {
1665 if (keyData == Keys.Enter) {
1666 IntPtr window = XplatUI.GetFocus ();
1667 Control c = Control.FromHandle (window);
1668 if (c is Button && c.FindForm () == this) {
1669 ((Button)c).PerformClick ();
1670 return true;
1672 else if (accept_button != null) {
1673 accept_button.PerformClick();
1674 return true;
1676 } else if (keyData == Keys.Escape && cancel_button != null) {
1677 cancel_button.PerformClick();
1678 return true;
1681 return base.ProcessDialogKey(keyData);
1684 protected override bool ProcessKeyPreview(ref Message msg) {
1685 if (key_preview) {
1686 if (ProcessKeyEventArgs(ref msg)) {
1687 return true;
1690 return base.ProcessKeyPreview (ref msg);
1693 protected override bool ProcessTabKey(bool forward) {
1694 return SelectNextControl(ActiveControl, forward, true, true, true);
1697 [EditorBrowsable(EditorBrowsableState.Advanced)]
1698 protected override void ScaleCore(float dx, float dy) {
1699 try {
1700 SuspendLayout();
1702 // We can't scale max or min windows
1703 if (WindowState == FormWindowState.Normal) {
1704 // We cannot call base since base also adjusts X/Y, but
1705 // a form is toplevel and doesn't move
1706 Size size;
1708 size = ClientSize;
1709 if (!GetStyle(ControlStyles.FixedWidth)) {
1710 size.Width = (int)(size.Width * dx);
1713 if (!GetStyle(ControlStyles.FixedHeight)) {
1714 size.Height = (int)(size.Height * dy);
1717 ClientSize = size;
1720 /* Now scale our children */
1721 Control [] controls = Controls.GetAllControls ();
1722 for (int i=0; i < controls.Length; i++) {
1723 controls[i].Scale(dx, dy);
1727 finally {
1728 ResumeLayout();
1732 protected override void Select(bool directed, bool forward) {
1733 Form parent;
1735 if (directed) {
1736 base.SelectNextControl(null, forward, true, true, true);
1739 parent = this.ParentForm;
1740 if (parent != null) {
1741 parent.ActiveControl = this;
1744 Activate();
1747 [EditorBrowsable(EditorBrowsableState.Advanced)]
1748 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
1749 base.SetBoundsCore (x, y, width, height, specified);
1752 [EditorBrowsable(EditorBrowsableState.Advanced)]
1753 protected override void SetClientSizeCore(int x, int y) {
1754 if ((minimum_size.Width != 0) && (x < minimum_size.Width)) {
1755 x = minimum_size.Width;
1756 } else if ((maximum_size.Width != 0) && (x > maximum_size.Width)) {
1757 x = maximum_size.Width;
1760 if ((minimum_size.Height != 0) && (y < minimum_size.Height)) {
1761 y = minimum_size.Height;
1762 } else if ((maximum_size.Height != 0) && (y > maximum_size.Height)) {
1763 y = maximum_size.Height;
1766 Rectangle ClientRect = new Rectangle(0, 0, x, y);
1767 Rectangle WindowRect;
1768 CreateParams cp = this.CreateParams;
1770 clientsize_set = new Size(x, y);
1772 if (XplatUI.CalculateWindowRect(ref ClientRect, cp.Style, cp.ExStyle, cp.menu, out WindowRect)) {
1773 SetBounds(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
1777 [EditorBrowsable(EditorBrowsableState.Advanced)]
1778 protected override void SetVisibleCore(bool value) {
1779 is_changing_visible_state = true;
1780 has_been_visible = value || has_been_visible;
1781 base.SetVisibleCore (value);
1782 is_changing_visible_state = false;
1785 protected override void UpdateDefaultButton() {
1786 base.UpdateDefaultButton ();
1789 [EditorBrowsable(EditorBrowsableState.Advanced)]
1790 protected override void WndProc(ref Message m) {
1792 if (window_manager != null && window_manager.HandleMessage (ref m)) {
1793 return;
1796 switch((Msg)m.Msg) {
1797 case Msg.WM_DESTROY: {
1798 base.WndProc(ref m);
1799 if (!RecreatingHandle) {
1800 this.closing = true;
1802 return;
1805 case Msg.WM_CLOSE_INTERNAL: {
1806 DestroyHandle();
1807 break;
1810 case Msg.WM_CLOSE: {
1811 Form act = Form.ActiveForm;
1812 if (act != null && act != this && act.Modal == true) {
1813 return;
1816 CancelEventArgs args = new CancelEventArgs ();
1818 if (mdi_container != null) {
1819 foreach (Form mdi_child in mdi_container.MdiChildren) {
1820 mdi_child.OnClosing (args);
1824 if (!is_modal) {
1825 OnClosing (args);
1826 if (!args.Cancel) {
1827 OnClosed (EventArgs.Empty);
1828 closing = true;
1830 Dispose ();
1831 } else {
1832 OnClosing (args);
1833 if (args.Cancel) {
1834 DialogResult = DialogResult.None;
1835 closing = false;
1836 } else {
1837 OnClosed (EventArgs.Empty);
1838 closing = true;
1839 Hide ();
1843 return;
1846 case Msg.WM_WINDOWPOSCHANGED: {
1847 if (WindowState != FormWindowState.Minimized) {
1848 base.WndProc(ref m);
1850 return;
1853 #if NET_2_0
1854 case Msg.WM_SYSCOMMAND: {
1855 // Let *Strips know the app's title bar was clicked
1856 if (XplatUI.IsEnabled (Handle))
1857 ToolStripManager.FireAppClicked ();
1859 base.WndProc(ref m);
1860 break;
1862 #endif
1864 case Msg.WM_ACTIVATE: {
1865 if (m.WParam != (IntPtr)WindowActiveFlags.WA_INACTIVE) {
1866 OnActivated(EventArgs.Empty);
1867 } else {
1868 OnDeactivate(EventArgs.Empty);
1870 return;
1873 case Msg.WM_KILLFOCUS: {
1874 base.WndProc(ref m);
1875 return;
1878 case Msg.WM_SETFOCUS: {
1879 if (ActiveControl != null && ActiveControl != this) {
1880 ActiveControl.Focus();
1881 return; // FIXME - do we need to run base.WndProc, even though we just changed focus?
1883 base.WndProc(ref m);
1884 return;
1887 // Menu drawing
1888 case Msg.WM_NCLBUTTONDOWN: {
1889 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
1890 ActiveMenu.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, Control.MousePosition.X, Control.MousePosition.Y, 0));
1893 if (ActiveMaximizedMdiChild != null) {
1894 if (ActiveMaximizedMdiChild.HandleMenuMouseDown (ActiveMenu,
1895 LowOrder ((int) m.LParam.ToInt32 ()),
1896 HighOrder ((int) m.LParam.ToInt32 ()))) {
1897 // Don't let base process this message, otherwise we won't
1898 // get a WM_NCLBUTTONUP.
1899 return;
1902 base.WndProc(ref m);
1903 return;
1905 case Msg.WM_NCLBUTTONUP: {
1906 if (ActiveMaximizedMdiChild != null) {
1907 ActiveMaximizedMdiChild.HandleMenuMouseUp (ActiveMenu,
1908 LowOrder ((int)m.LParam.ToInt32 ()),
1909 HighOrder ((int)m.LParam.ToInt32 ()));
1911 base.WndProc (ref m);
1912 return;
1915 case Msg.WM_NCMOUSELEAVE: {
1916 if (ActiveMaximizedMdiChild != null) {
1917 ActiveMaximizedMdiChild.HandleMenuMouseLeave(ActiveMenu,
1918 LowOrder((int)m.LParam.ToInt32()),
1919 HighOrder((int)m.LParam.ToInt32()));
1921 base.WndProc(ref m);
1922 return;
1925 case Msg.WM_NCMOUSEMOVE: {
1926 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
1927 ActiveMenu.OnMouseMove(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0));
1930 if (ActiveMaximizedMdiChild != null) {
1931 ActiveMaximizedMdiChild.HandleMenuMouseMove (ActiveMenu,
1932 LowOrder ((int)m.LParam.ToInt32 ()),
1933 HighOrder ((int)m.LParam.ToInt32 ()));
1935 base.WndProc(ref m);
1936 return;
1939 case Msg.WM_NCPAINT: {
1940 if (ActiveMenu != null) {
1941 PaintEventArgs pe;
1942 Point pnt;
1944 pe = XplatUI.PaintEventStart(Handle, false);
1945 pnt = XplatUI.GetMenuOrigin(window.Handle);
1947 // The entire menu has to be in the clip rectangle because the
1948 // control buttons are right-aligned and otherwise they would
1949 // stay painted when the window gets resized.
1950 Rectangle clip = new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0);
1951 clip = Rectangle.Union(clip, pe.ClipRectangle);
1952 pe.SetClip(clip);
1953 pe.Graphics.SetClip(clip);
1955 ActiveMenu.Draw (pe, new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0));
1957 if (ActiveMaximizedMdiChild != null) {
1958 ActiveMaximizedMdiChild.DrawMaximizedButtons (ActiveMenu, pe);
1961 XplatUI.PaintEventEnd(Handle, false);
1964 base.WndProc(ref m);
1965 return;
1968 case Msg.WM_NCCALCSIZE: {
1969 XplatUIWin32.NCCALCSIZE_PARAMS ncp;
1971 if ((ActiveMenu != null) && (m.WParam == (IntPtr)1)) {
1972 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
1974 // Adjust for menu
1975 ncp.rgrc1.top += ThemeEngine.Current.CalcMenuBarSize (DeviceContext, ActiveMenu, ncp.rgrc1.right - ncp.rgrc1.left);
1976 Marshal.StructureToPtr(ncp, m.LParam, true);
1978 DefWndProc(ref m);
1979 break;
1982 case Msg.WM_MOUSEMOVE: {
1983 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
1984 MouseEventArgs args;
1986 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
1987 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
1988 active_tracker.OnMotion(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
1989 break;
1991 base.WndProc(ref m);
1992 break;
1995 case Msg.WM_LBUTTONDOWN:
1996 case Msg.WM_MBUTTONDOWN:
1997 case Msg.WM_RBUTTONDOWN: {
1998 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
1999 MouseEventArgs args;
2001 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
2002 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2003 active_tracker.OnMouseDown(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2004 return;
2006 base.WndProc(ref m);
2007 return;
2010 case Msg.WM_LBUTTONUP:
2011 case Msg.WM_MBUTTONUP:
2012 case Msg.WM_RBUTTONUP: {
2013 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2014 MouseEventArgs args;
2015 MouseButtons mb = FromParamToMouseButtons ((int) m.WParam.ToInt32());
2017 // We add in the button that was released (not sent in WParam)
2018 switch((Msg)m.Msg) {
2019 case Msg.WM_LBUTTONUP:
2020 mb |= MouseButtons.Left;
2021 break;
2022 case Msg.WM_MBUTTONUP:
2023 mb |= MouseButtons.Middle;
2024 break;
2025 case Msg.WM_RBUTTONUP:
2026 mb |= MouseButtons.Right;
2027 break;
2030 args = new MouseEventArgs (mb, mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2031 active_tracker.OnMouseUp(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2032 mouse_clicks = 1;
2033 return;
2035 base.WndProc(ref m);
2036 return;
2039 case Msg.WM_GETMINMAXINFO: {
2040 MINMAXINFO mmi;
2042 if (m.LParam != IntPtr.Zero) {
2043 mmi = (MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO));
2045 default_maximized_bounds = new Rectangle(mmi.ptMaxPosition.x, mmi.ptMaxPosition.y, mmi.ptMaxSize.x, mmi.ptMaxSize.y);
2046 if (maximized_bounds != Rectangle.Empty) {
2047 mmi.ptMaxPosition.x = maximized_bounds.Left;
2048 mmi.ptMaxPosition.y = maximized_bounds.Top;
2049 mmi.ptMaxSize.x = maximized_bounds.Width;
2050 mmi.ptMaxSize.y = maximized_bounds.Height;
2053 if (minimum_size != Size.Empty) {
2054 mmi.ptMinTrackSize.x = minimum_size.Width;
2055 mmi.ptMinTrackSize.y = minimum_size.Height;
2058 if (maximum_size != Size.Empty) {
2059 mmi.ptMaxTrackSize.x = maximum_size.Width;
2060 mmi.ptMaxTrackSize.y = maximum_size.Height;
2062 Marshal.StructureToPtr(mmi, m.LParam, false);
2064 break;
2067 #if NET_2_0
2068 case Msg.WM_MOUSEACTIVATE: {
2069 // Let *Strips know the form or another control has been clicked
2070 if (XplatUI.IsEnabled (Handle))
2071 ToolStripManager.FireAppClicked ();
2073 base.WndProc (ref m);
2074 break;
2077 case Msg.WM_ACTIVATEAPP: {
2078 // Let *Strips know the app lost focus
2079 if (m.WParam == (IntPtr)0)
2080 if (XplatUI.IsEnabled (Handle))
2081 ToolStripManager.FireAppFocusChanged (this);
2083 base.WndProc (ref m);
2084 break;
2086 #endif
2088 default: {
2089 base.WndProc (ref m);
2090 break;
2094 #endregion // Protected Instance Methods
2096 internal void RemoveWindowManager ()
2098 window_manager = null;
2101 internal override void CheckAcceptButton()
2103 if (accept_button != null) {
2104 Button a_button = accept_button as Button;
2106 if (ActiveControl == a_button)
2107 return;
2109 if (ActiveControl is Button) {
2110 a_button.paint_as_acceptbutton = false;
2111 a_button.Redraw();
2112 return;
2113 } else {
2114 a_button.paint_as_acceptbutton = true;
2115 a_button.Redraw();
2120 #region Events
2121 static object ActivatedEvent = new object ();
2122 static object ClosedEvent = new object ();
2123 static object ClosingEvent = new object ();
2124 static object DeactivateEvent = new object ();
2125 static object InputLanguageChangedEvent = new object ();
2126 static object InputLanguageChangingEvent = new object ();
2127 static object LoadEvent = new object ();
2128 static object MaximizedBoundsChangedEvent = new object ();
2129 static object MaximumSizeChangedEvent = new object ();
2130 static object MdiChildActivateEvent = new object ();
2131 static object MenuCompleteEvent = new object ();
2132 static object MenuStartEvent = new object ();
2133 static object MinimumSizeChangedEvent = new object ();
2135 public event EventHandler Activated {
2136 add { Events.AddHandler (ActivatedEvent, value); }
2137 remove { Events.RemoveHandler (ActivatedEvent, value); }
2140 public event EventHandler Closed {
2141 add { Events.AddHandler (ClosedEvent, value); }
2142 remove { Events.RemoveHandler (ClosedEvent, value); }
2145 public event CancelEventHandler Closing {
2146 add { Events.AddHandler (ClosingEvent, value); }
2147 remove { Events.RemoveHandler (ClosingEvent, value); }
2150 public event EventHandler Deactivate {
2151 add { Events.AddHandler (DeactivateEvent, value); }
2152 remove { Events.RemoveHandler (DeactivateEvent, value); }
2155 public event InputLanguageChangedEventHandler InputLanguageChanged {
2156 add { Events.AddHandler (InputLanguageChangedEvent, value); }
2157 remove { Events.RemoveHandler (InputLanguageChangedEvent, value); }
2160 public event InputLanguageChangingEventHandler InputLanguageChanging {
2161 add { Events.AddHandler (InputLanguageChangingEvent, value); }
2162 remove { Events.RemoveHandler (InputLanguageChangingEvent, value); }
2165 public event EventHandler Load {
2166 add { Events.AddHandler (LoadEvent, value); }
2167 remove { Events.RemoveHandler (LoadEvent, value); }
2170 public event EventHandler MaximizedBoundsChanged {
2171 add { Events.AddHandler (MaximizedBoundsChangedEvent, value); }
2172 remove { Events.RemoveHandler (MaximizedBoundsChangedEvent, value); }
2175 public event EventHandler MaximumSizeChanged {
2176 add { Events.AddHandler (MaximumSizeChangedEvent, value); }
2177 remove { Events.RemoveHandler (MaximumSizeChangedEvent, value); }
2180 public event EventHandler MdiChildActivate {
2181 add { Events.AddHandler (MdiChildActivateEvent, value); }
2182 remove { Events.RemoveHandler (MdiChildActivateEvent, value); }
2185 public event EventHandler MenuComplete {
2186 add { Events.AddHandler (MenuCompleteEvent, value); }
2187 remove { Events.RemoveHandler (MenuCompleteEvent, value); }
2190 public event EventHandler MenuStart {
2191 add { Events.AddHandler (MenuStartEvent, value); }
2192 remove { Events.RemoveHandler (MenuStartEvent, value); }
2195 public event EventHandler MinimumSizeChanged {
2196 add { Events.AddHandler (MinimumSizeChangedEvent, value); }
2197 remove { Events.RemoveHandler (MinimumSizeChangedEvent, value); }
2201 [Browsable(false)]
2202 [EditorBrowsable(EditorBrowsableState.Never)]
2203 public new event EventHandler TabIndexChanged {
2204 add { base.TabIndexChanged += value; }
2205 remove { base.TabIndexChanged -= value; }
2207 #endregion // Events
2209 #if NET_2_0
2210 public override string Text {
2211 get {
2212 return base.Text;
2215 set {
2216 base.Text = value;
2220 public new Point Location {
2221 get {
2222 return base.Location;
2225 set {
2226 base.Location = value;
2230 static object FormClosingEvent = new object ();
2231 static object FormClosedEvent = new object ();
2233 public event FormClosingEventHandler FormClosing {
2234 add { Events.AddHandler (FormClosingEvent, value); }
2235 remove { Events.RemoveHandler (FormClosingEvent, value); }
2238 public event FormClosedEventHandler FormClosed {
2239 add { Events.AddHandler (FormClosedEvent, value); }
2240 remove { Events.RemoveHandler (FormClosedEvent, value); }
2243 protected virtual void OnFormClosing (FormClosingEventArgs e)
2245 FormClosingEventHandler eh = (FormClosingEventHandler)(Events [FormClosingEvent]);
2246 if (eh != null)
2247 eh (this, e);
2249 #endif