* Form.cs: move the call to SendControlFocus into the same
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Form.cs
blobcb19377a7c9cd735d128f3dbb736f371e58dbf21
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 #if NET_2_0
43 [ClassInterface (ClassInterfaceType.AutoDispatch)]
44 [InitializationEvent ("Load")]
45 [ComVisible (true)]
46 #endif
47 [ToolboxItem(false)]
48 public class Form : ContainerControl {
49 #region Local Variables
50 internal bool closing;
51 FormBorderStyle form_border_style;
52 private bool autoscale;
53 private Size clientsize_set;
54 private Size autoscale_base_size;
55 private bool allow_transparency;
56 private static Icon default_icon;
57 internal bool is_modal;
58 internal FormWindowState window_state;
59 private bool control_box;
60 private bool minimize_box;
61 private bool maximize_box;
62 private bool help_button;
63 private bool show_in_taskbar;
64 private bool topmost;
65 private IButtonControl accept_button;
66 private IButtonControl cancel_button;
67 private DialogResult dialog_result;
68 private FormStartPosition start_position;
69 private Form owner;
70 private Form.ControlCollection owned_forms;
71 private MdiClient mdi_container;
72 internal InternalWindowManager window_manager;
73 private Form mdi_parent;
74 private bool key_preview;
75 private MainMenu menu;
76 private Icon icon;
77 private Size maximum_size;
78 private Size minimum_size;
79 private SizeGripStyle size_grip_style;
80 private Rectangle maximized_bounds;
81 private Rectangle default_maximized_bounds;
82 private double opacity;
83 internal ApplicationContext context;
84 Color transparency_key;
85 internal MenuTracker active_tracker;
86 private bool is_loaded;
87 internal bool is_changing_visible_state;
88 internal bool has_been_visible;
90 #if NET_2_0
91 private MenuStrip main_menu_strip;
92 #endif
93 #endregion // Local Variables
95 #region Private & Internal Methods
96 static Form ()
98 default_icon = Locale.GetResource("mono.ico") as Icon;
101 // warning: this is only hooked up when an mdi container is created.
102 private void ControlAddedHandler (object sender, ControlEventArgs e)
104 if (mdi_container != null) {
105 mdi_container.SendToBack ();
109 // Convenience method for fire BOTH OnClosing and OnFormClosing events
110 // Returns the value of Cancel, so true means the Close was cancelled,
111 // and you shouldn't close the form.
112 internal bool FireClosingEvents (CloseReason reason)
114 CancelEventArgs cea = new CancelEventArgs ();
115 this.OnClosing (cea);
117 #if NET_2_0
118 FormClosingEventArgs fcea = new FormClosingEventArgs (reason, cea.Cancel);
119 this.OnFormClosing (fcea);
120 return fcea.Cancel;
121 #else
122 return cea.Cancel;
123 #endif
126 private void SelectActiveControl ()
128 if (this.IsMdiContainer) {
129 SendControlFocus (this.mdi_container);
130 return;
133 if (this.ActiveControl == null) {
134 bool visible;
136 // This visible hack is to work around CanSelect always being false if one of the parents
137 // is not visible; and we by default create Form invisible...
138 visible = this.is_visible;
139 this.is_visible = true;
141 if (SelectNextControl (this, true, true, true, true) == false) {
142 Select (this);
145 this.is_visible = visible;
146 } else {
147 Select (ActiveControl);
150 #endregion // Private & Internal Methods
152 #region Public Classes
153 public new class ControlCollection : Control.ControlCollection {
154 Form form_owner;
156 public ControlCollection(Form owner) : base(owner) {
157 this.form_owner = owner;
160 public override void Add(Control value) {
161 if (Contains (value))
162 return;
163 AddToList (value);
164 ((Form)value).owner=form_owner;
167 public override void Remove(Control value) {
168 ((Form)value).owner = null;
169 base.Remove (value);
172 #endregion // Public Classes
174 #region Public Constructor & Destructor
175 public Form ()
177 SizeF current_scale = GetAutoScaleSize (DeviceContext, Font);
179 autoscale = true;
180 autoscale_base_size = new Size ((int)current_scale.Width, (int) current_scale.Height);
181 allow_transparency = false;
182 closing = false;
183 is_modal = false;
184 dialog_result = DialogResult.None;
185 start_position = FormStartPosition.WindowsDefaultLocation;
186 form_border_style = FormBorderStyle.Sizable;
187 window_state = FormWindowState.Normal;
188 key_preview = false;
189 opacity = 1D;
190 menu = null;
191 icon = default_icon;
192 minimum_size = Size.Empty;
193 maximum_size = Size.Empty;
194 clientsize_set = Size.Empty;
195 control_box = true;
196 minimize_box = true;
197 maximize_box = true;
198 help_button = false;
199 show_in_taskbar = true;
200 is_visible = false;
201 is_toplevel = true;
202 size_grip_style = SizeGripStyle.Auto;
203 maximized_bounds = Rectangle.Empty;
204 default_maximized_bounds = Rectangle.Empty;
205 owned_forms = new Form.ControlCollection(this);
206 transparency_key = Color.Empty;
208 // FIXME: this should disappear just as soon as the handle creation is done in the right place (here is too soon()
209 UpdateBounds();
212 #endregion // Public Constructor & Destructor
214 #region Public Static Properties
216 public static Form ActiveForm {
217 get {
218 Control active;
220 active = FromHandle(XplatUI.GetActive());
222 if (active != null) {
223 if ( !(active is Form)) {
224 Control parent;
226 parent = active.Parent;
227 while (parent != null) {
228 if (parent is Form) {
229 return (Form)parent;
231 parent = parent.Parent;
233 } else {
234 return (Form)active;
237 return null;
241 #endregion // Public Static Properties
243 #region Public Instance Properties
244 [DefaultValue(null)]
245 public IButtonControl AcceptButton {
246 get {
247 return accept_button;
250 set {
251 accept_button = value;
252 CheckAcceptButton();
256 [Browsable(false)]
257 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
258 public bool AllowTransparency {
259 get {
260 return allow_transparency;
263 set {
264 if (value == allow_transparency) {
265 return;
268 allow_transparency = value;
270 if (value) {
271 if (IsHandleCreated) {
272 if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
273 XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
275 } else {
276 UpdateStyles(); // Remove the WS_EX_LAYERED style
282 #if NET_2_0
283 [Browsable (false)]
284 [EditorBrowsable (EditorBrowsableState.Never)]
285 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
286 [Obsolete ("This property has been deprecated in favor of AutoScaleMode.")]
287 #else
288 [DefaultValue(true)]
289 #endif
290 [MWFCategory("Layout")]
291 public bool AutoScale {
292 get {
293 return autoscale;
296 set {
297 autoscale = value;
301 #if NET_2_0
302 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
303 [EditorBrowsable(EditorBrowsableState.Never)]
304 #else
305 [EditorBrowsable(EditorBrowsableState.Advanced)]
306 #endif
307 [Localizable(true)]
308 [Browsable(false)]
309 public virtual Size AutoScaleBaseSize {
310 get {
311 return autoscale_base_size;
314 set {
315 autoscale_base_size = value;
319 [Localizable(true)]
320 public override bool AutoScroll {
321 get {
322 return base.AutoScroll;
324 set {
325 base.AutoScroll = value;
329 public override Color BackColor {
330 get {
331 /* we don't let parents override our
332 default background color for forms.
333 this fixes the default color for mdi
334 children. */
335 if (background_color.IsEmpty)
336 return DefaultBackColor;
337 else
338 return background_color;
340 set {
341 base.BackColor = value;
345 [DefaultValue(null)]
346 public IButtonControl CancelButton {
347 get {
348 return cancel_button;
351 set {
352 cancel_button = value;
353 if (cancel_button != null && cancel_button.DialogResult == DialogResult.None)
354 cancel_button.DialogResult = DialogResult.Cancel;
358 // new property so we can change the DesignerSerializationVisibility
359 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
360 [Localizable(true)]
361 public new Size ClientSize {
362 get { return base.ClientSize; }
363 set { base.ClientSize = value; }
366 [DefaultValue(true)]
367 [MWFCategory("Window Style")]
368 public bool ControlBox {
369 get {
370 return control_box;
373 set {
374 if (control_box != value) {
375 control_box = value;
376 UpdateStyles();
381 [Browsable(false)]
382 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
383 public Rectangle DesktopBounds {
384 get {
385 return new Rectangle(Location, Size);
388 set {
389 Bounds = value;
393 [Browsable(false)]
394 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
395 public Point DesktopLocation {
396 get {
397 return Location;
400 set {
401 Location = value;
405 [Browsable(false)]
406 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
407 public DialogResult DialogResult {
408 get {
409 return dialog_result;
412 set {
413 if (value < DialogResult.None || value > DialogResult.No)
414 throw new InvalidEnumArgumentException ("value", (int) value,
415 typeof (DialogResult));
417 dialog_result = value;
418 closing = (dialog_result != DialogResult.None && is_modal);
422 [DefaultValue(FormBorderStyle.Sizable)]
423 [DispId(-504)]
424 [MWFCategory("Appearance")]
425 public FormBorderStyle FormBorderStyle {
426 get {
427 return form_border_style;
429 set {
430 form_border_style = value;
432 if (window_manager == null) {
433 if (IsHandleCreated) {
434 XplatUI.SetBorderStyle(window.Handle, form_border_style);
436 } else {
437 window_manager.UpdateBorderStyle (value);
440 UpdateStyles();
444 [DefaultValue(false)]
445 [MWFCategory("Window Style")]
446 public bool HelpButton {
447 get {
448 return help_button;
451 set {
452 if (help_button != value) {
453 help_button = value;
454 UpdateStyles();
459 [Localizable(true)]
460 [AmbientValue(null)]
461 [MWFCategory("Window Style")]
462 public Icon Icon {
463 get {
464 return icon;
467 set {
468 if (icon != value) {
469 icon = value;
471 if (IsHandleCreated) {
472 XplatUI.SetIcon(Handle, icon == null ? default_icon : icon);
478 [Browsable(false)]
479 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
480 public bool IsMdiChild {
481 get {
482 return mdi_parent != null;
486 [DefaultValue(false)]
487 [MWFCategory("Window Style")]
488 public bool IsMdiContainer {
489 get {
490 return mdi_container != null;
493 set {
494 if (value && mdi_container == null) {
495 mdi_container = new MdiClient ();
496 Controls.Add(mdi_container);
497 ControlAdded += new ControlEventHandler (ControlAddedHandler);
498 mdi_container.SendToBack ();
499 mdi_container.SetParentText (true);
500 } else if (!value && mdi_container != null) {
501 Controls.Remove(mdi_container);
502 mdi_container = null;
507 [Browsable(false)]
508 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
509 public Form ActiveMdiChild {
510 get {
511 if (!IsMdiContainer)
512 return null;
513 return (Form) mdi_container.ActiveMdiChild;
517 [Browsable(false)]
518 [EditorBrowsable(EditorBrowsableState.Advanced)]
519 public bool IsRestrictedWindow {
520 get {
521 return false;
525 [DefaultValue(false)]
526 public bool KeyPreview {
527 get {
528 return key_preview;
531 set {
532 key_preview = value;
536 #if NET_2_0
537 [DefaultValue (null)]
538 [TypeConverter (typeof (ReferenceConverter))]
539 public MenuStrip MainMenuStrip {
540 get { return this.main_menu_strip; }
541 set {
542 if (this.main_menu_strip != value) {
543 this.main_menu_strip = value;
544 this.main_menu_strip.RefreshMdiItems ();
548 #endif
550 [DefaultValue(true)]
551 [MWFCategory("Window Style")]
552 public bool MaximizeBox {
553 get {
554 return maximize_box;
556 set {
557 if (maximize_box != value) {
558 maximize_box = value;
559 if (IsHandleCreated) {
560 RecreateHandle();
562 UpdateStyles();
567 [DefaultValue("{Width=0, Height=0}")]
568 [Localizable(true)]
569 [RefreshProperties(RefreshProperties.Repaint)]
570 [MWFCategory("Layout")]
571 public
572 #if NET_2_0
573 override
574 #endif
575 Size MaximumSize {
576 get {
577 return maximum_size;
580 set {
581 if (maximum_size != value) {
582 maximum_size = value;
583 OnMaximumSizeChanged(EventArgs.Empty);
584 if (IsHandleCreated) {
585 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
591 [Browsable(false)]
592 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
593 public Form[] MdiChildren {
594 get {
595 if (mdi_container != null)
596 return mdi_container.MdiChildren;
597 else
598 return new Form[0];
602 [Browsable(false)]
603 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
604 public Form MdiParent {
605 get {
606 return mdi_parent;
609 set {
610 if (value != null && !value.IsMdiContainer)
611 throw new ArgumentException ();
613 if (mdi_parent != null) {
614 mdi_parent.MdiContainer.Controls.Remove (this);
617 if (value != null) {
618 mdi_parent = value;
619 window_manager = new MdiWindowManager (this,
620 mdi_parent.MdiContainer);
621 mdi_parent.MdiContainer.Controls.Add (this);
622 mdi_parent.MdiContainer.Controls.SetChildIndex (this, 0);
624 RecreateHandle ();
626 } else if (mdi_parent != null) {
627 mdi_parent = null;
629 // Create a new window manager
630 window_manager = null;
631 FormBorderStyle = form_border_style;
633 RecreateHandle ();
638 internal MenuTracker ActiveTracker {
639 get { return active_tracker; }
640 set {
641 if (value == active_tracker)
642 return;
644 Capture = value != null;
645 active_tracker = value;
649 internal MdiClient MdiContainer {
650 get { return mdi_container; }
653 internal InternalWindowManager WindowManager {
654 get { return window_manager; }
657 #if NET_2_0
658 [Browsable (false)]
659 [TypeConverter (typeof (ReferenceConverter))]
660 #endif
661 [DefaultValue(null)]
662 [MWFCategory("Window Style")]
663 public MainMenu Menu {
664 get {
665 return menu;
668 set {
669 if (menu != value) {
670 menu = value;
672 if (menu != null && !IsMdiChild) {
673 menu.SetForm (this);
675 if (IsHandleCreated) {
676 XplatUI.SetMenu (window.Handle, menu);
679 if (clientsize_set != Size.Empty) {
680 SetClientSizeCore(clientsize_set.Width, clientsize_set.Height);
681 } else {
682 UpdateBounds (bounds.X, bounds.Y, bounds.Width, bounds.Height, ClientSize.Width, ClientSize.Height -
683 ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu, ClientSize.Width));
685 } else
686 UpdateBounds ();
691 [Browsable(false)]
692 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
693 [EditorBrowsable(EditorBrowsableState.Advanced)]
694 public MainMenu MergedMenu {
695 get {
696 if (!IsMdiChild || window_manager == null)
697 return null;
698 return ((MdiWindowManager) window_manager).MergedMenu;
702 // This is the menu in display and being used because of merging this can
703 // be different then the menu that is actually assosciated with the form
704 internal MainMenu ActiveMenu {
705 get {
706 if (IsMdiChild)
707 return null;
709 if (IsMdiContainer && mdi_container.Controls.Count > 0 &&
710 ((Form) mdi_container.Controls [0]).WindowState == FormWindowState.Maximized) {
711 MdiWindowManager wm = (MdiWindowManager) ((Form) mdi_container.Controls [0]).WindowManager;
712 return wm.MaximizedMenu;
715 Form amc = ActiveMdiChild;
716 if (amc == null || amc.Menu == null)
717 return menu;
718 return amc.MergedMenu;
722 internal MdiWindowManager ActiveMaximizedMdiChild {
723 get {
724 Form child = ActiveMdiChild;
725 if (child == null)
726 return null;
727 if (child.WindowManager == null || child.window_state != FormWindowState.Maximized)
728 return null;
729 return (MdiWindowManager) child.WindowManager;
733 [DefaultValue(true)]
734 [MWFCategory("Window Style")]
735 public bool MinimizeBox {
736 get {
737 return minimize_box;
739 set {
740 if (minimize_box != value) {
741 minimize_box = value;
742 if (IsHandleCreated) {
743 RecreateHandle();
745 UpdateStyles();
750 #if !NET_2_0
751 [DefaultValue("{Width=0, Height=0}")]
752 #endif
753 [Localizable(true)]
754 [RefreshProperties(RefreshProperties.Repaint)]
755 [MWFCategory("Layout")]
756 public
757 #if NET_2_0
758 override
759 #endif
760 Size MinimumSize {
761 get {
762 return minimum_size;
765 set {
766 if (minimum_size != value) {
767 minimum_size = value;
769 if ((Size.Width < value.Width) || (Size.Height < value.Height)) {
770 Size = new Size(Math.Max(Size.Width, value.Width), Math.Max(Size.Height, value.Height));
774 OnMinimumSizeChanged(EventArgs.Empty);
775 if (IsHandleCreated) {
776 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
782 [Browsable(false)]
783 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
784 public bool Modal {
785 get {
786 return is_modal;
790 [DefaultValue(1D)]
791 [TypeConverter(typeof(OpacityConverter))]
792 [MWFCategory("Window Style")]
793 public double Opacity {
794 get {
795 if (IsHandleCreated) {
796 if ((XplatUI.SupportsTransparency () & TransparencySupport.Get) != 0)
797 return XplatUI.GetWindowTransparency (Handle);
800 return opacity;
803 set {
804 opacity = value;
806 AllowTransparency = true;
808 if (IsHandleCreated) {
809 UpdateStyles();
810 if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
811 XplatUI.SetWindowTransparency(Handle, opacity, TransparencyKey);
817 [Browsable(false)]
818 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
819 public Form[] OwnedForms {
820 get {
821 Form[] form_list;
823 form_list = new Form[owned_forms.Count];
825 for (int i=0; i<owned_forms.Count; i++) {
826 form_list[i] = (Form)owned_forms[i];
829 return form_list;
833 [Browsable(false)]
834 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
835 public Form Owner {
836 get {
837 return owner;
840 set {
841 if (owner != value) {
842 if (owner != null) {
843 owner.RemoveOwnedForm(this);
845 owner = value;
846 if (owner != null)
847 owner.AddOwnedForm(this);
848 if (IsHandleCreated) {
849 if (owner != null && owner.IsHandleCreated) {
850 XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
851 } else {
852 XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
859 [DefaultValue(true)]
860 [MWFCategory("Window Style")]
861 public bool ShowInTaskbar {
862 get {
863 return show_in_taskbar;
865 set {
866 if (show_in_taskbar != value) {
867 show_in_taskbar = value;
868 if (IsHandleCreated) {
869 RecreateHandle();
871 UpdateStyles();
876 // new property so we can set the DesignerSerializationVisibility
877 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
878 [Localizable(false)]
879 public new Size Size {
880 get { return base.Size; }
881 set { base.Size = value; }
884 [MonoTODO("Trigger something when GripStyle is set")]
885 [DefaultValue(SizeGripStyle.Auto)]
886 [MWFCategory("Window Style")]
887 public SizeGripStyle SizeGripStyle {
888 get {
889 return size_grip_style;
892 set {
893 size_grip_style = value;
897 [DefaultValue(FormStartPosition.WindowsDefaultLocation)]
898 [Localizable(true)]
899 [MWFCategory("Layout")]
900 public FormStartPosition StartPosition {
901 get {
902 return start_position;
905 set {
906 if (start_position == FormStartPosition.WindowsDefaultLocation) { // Only do this if it's not set yet
907 start_position = value;
908 if (IsHandleCreated) {
909 switch(start_position) {
910 case FormStartPosition.CenterParent: {
911 CenterToParent();
912 break;
915 case FormStartPosition.CenterScreen: {
916 CenterToScreen();
917 break;
920 case FormStartPosition.Manual: {
921 Left = CreateParams.X;
922 Top = CreateParams.Y;
923 break;
926 default: {
927 break;
935 // new property so we can set EditorBrowsable to never
936 [Browsable(false)]
937 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
938 [EditorBrowsable(EditorBrowsableState.Never)]
939 public new int TabIndex {
940 get { return base.TabIndex; }
941 set { base.TabIndex = value; }
944 [Browsable(false)]
945 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
946 [EditorBrowsable(EditorBrowsableState.Advanced)]
947 public bool TopLevel {
948 get {
949 return GetTopLevel();
952 set {
953 if (!value && IsMdiContainer)
954 throw new ArgumentException ("MDI Container forms must be top level.");
955 SetTopLevel(value);
959 [DefaultValue(false)]
960 [MWFCategory("Window Style")]
961 public bool TopMost {
962 get {
963 return topmost;
966 set {
967 if (topmost != value) {
968 topmost = value;
969 if (IsHandleCreated)
970 XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, value);
975 [MWFCategory("Window Style")]
976 public Color TransparencyKey {
977 get {
978 return transparency_key;
981 set {
982 transparency_key = value;
984 AllowTransparency = true;
985 UpdateStyles();
986 if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0)
987 XplatUI.SetWindowTransparency(Handle, Opacity, transparency_key);
991 [DefaultValue(FormWindowState.Normal)]
992 [MWFCategory("Layout")]
993 public FormWindowState WindowState {
994 get {
995 if (IsHandleCreated) {
997 if (window_manager != null)
998 return window_manager.GetWindowState ();
1000 FormWindowState new_state = XplatUI.GetWindowState(Handle);
1001 if (new_state != (FormWindowState)(-1))
1002 window_state = new_state;
1005 return window_state;
1008 set {
1009 FormWindowState old_state = window_state;
1010 window_state = value;
1011 if (IsHandleCreated) {
1013 if (window_manager != null) {
1014 window_manager.SetWindowState (old_state, value);
1015 return;
1018 XplatUI.SetWindowState(Handle, value);
1023 #endregion // Public Instance Properties
1025 #region Protected Instance Properties
1026 protected override CreateParams CreateParams {
1027 get {
1028 CreateParams cp = new CreateParams ();
1030 cp.Caption = Text;
1031 cp.ClassName = XplatUI.DefaultClassName;
1032 cp.ClassStyle = 0;
1033 cp.Style = 0;
1034 cp.ExStyle = 0;
1035 cp.Param = 0;
1036 cp.Parent = IntPtr.Zero;
1037 cp.menu = ActiveMenu;
1039 if (start_position == FormStartPosition.WindowsDefaultLocation && !IsMdiChild) {
1040 cp.X = unchecked((int)0x80000000);
1041 cp.Y = unchecked((int)0x80000000);
1042 } else {
1043 cp.X = Left;
1044 cp.Y = Top;
1046 cp.Width = Width;
1047 cp.Height = Height;
1049 cp.Style = (int)(WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS);
1051 if (IsMdiChild) {
1052 cp.Style |= (int)(WindowStyles.WS_CHILD | WindowStyles.WS_CAPTION);
1053 if (Parent != null) {
1054 cp.Parent = Parent.Handle;
1057 cp.ExStyle |= (int) (WindowExStyles.WS_EX_WINDOWEDGE | WindowExStyles.WS_EX_MDICHILD);
1059 switch (FormBorderStyle) {
1060 case FormBorderStyle.None:
1061 break;
1062 case FormBorderStyle.FixedToolWindow:
1063 case FormBorderStyle.SizableToolWindow:
1064 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW;
1065 goto default;
1066 default:
1067 cp.Style |= (int) WindowStyles.WS_OVERLAPPEDWINDOW;
1068 break;
1071 } else {
1072 switch (FormBorderStyle) {
1073 case FormBorderStyle.Fixed3D: {
1074 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1075 cp.ExStyle |= (int)WindowExStyles.WS_EX_CLIENTEDGE;
1076 break;
1079 case FormBorderStyle.FixedDialog: {
1080 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1081 cp.ExStyle |= (int)(WindowExStyles.WS_EX_DLGMODALFRAME | WindowExStyles.WS_EX_CONTROLPARENT);
1082 break;
1085 case FormBorderStyle.FixedSingle: {
1086 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1087 break;
1090 case FormBorderStyle.FixedToolWindow: {
1091 cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
1092 cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1093 break;
1096 case FormBorderStyle.Sizable: {
1097 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
1098 break;
1101 case FormBorderStyle.SizableToolWindow: {
1102 cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
1103 cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW);
1104 break;
1107 case FormBorderStyle.None: {
1108 break;
1113 switch(window_state) {
1114 case FormWindowState.Maximized: {
1115 cp.Style |= (int)WindowStyles.WS_MAXIMIZE;
1116 break;
1119 case FormWindowState.Minimized: {
1120 cp.Style |= (int)WindowStyles.WS_MINIMIZE;
1121 break;
1125 if (TopMost) {
1126 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOPMOST;
1129 if (ShowInTaskbar) {
1130 cp.ExStyle |= (int)WindowExStyles.WS_EX_APPWINDOW;
1133 if (MaximizeBox) {
1134 cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
1137 if (MinimizeBox) {
1138 cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
1141 if (ControlBox) {
1142 cp.Style |= (int)WindowStyles.WS_SYSMENU;
1145 if (HelpButton && !MaximizeBox && !MinimizeBox) {
1146 cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTEXTHELP;
1149 if (Visible)
1150 cp.Style |= (int)WindowStyles.WS_VISIBLE;
1152 if (opacity < 1.0 || TransparencyKey != Color.Empty) {
1153 cp.ExStyle |= (int)WindowExStyles.WS_EX_LAYERED;
1156 if (!is_enabled && context == null) {
1157 cp.Style |= (int)(WindowStyles.WS_DISABLED);
1160 return cp;
1164 protected override ImeMode DefaultImeMode {
1165 get {
1166 return ImeMode.NoControl;
1170 protected override Size DefaultSize {
1171 get {
1172 return new Size (300, 300);
1176 protected Rectangle MaximizedBounds {
1177 get {
1178 if (maximized_bounds != Rectangle.Empty) {
1179 return maximized_bounds;
1181 return default_maximized_bounds;
1184 set {
1185 maximized_bounds = value;
1186 OnMaximizedBoundsChanged(EventArgs.Empty);
1187 if (IsHandleCreated) {
1188 XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
1192 #endregion // Protected Instance Properties
1194 #region Public Static Methods
1195 #if NET_2_0
1196 [EditorBrowsable(EditorBrowsableState.Never)]
1197 [Obsolete ("This method has been deprecated. Use AutoScaleDimensions instead")]
1198 #else
1199 [EditorBrowsable(EditorBrowsableState.Advanced)]
1200 #endif
1201 public static SizeF GetAutoScaleSize (Font font)
1203 return XplatUI.GetAutoScaleSize(font);
1206 #endregion // Public Static Methods
1208 #region Public Instance Methods
1209 internal SizeF GetAutoScaleSize (Graphics g, Font font)
1212 // The following constants come from the dotnet mailing list
1213 // discussion: http://discuss.develop.com/archives/wa.exe?A2=ind0203A&L=DOTNET&P=R3655
1215 // The magic number is "Its almost the length
1216 // of the string with a smattering added in
1217 // for compat with earlier code".
1220 string magic_string = "The quick brown fox jumped over the lazy dog.";
1221 double magic_number = 44.549996948242189;
1222 float width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
1224 return new SizeF (width, font.Height);
1227 public void Activate ()
1229 Form active;
1231 // The docs say activate only activates if our app is already active
1232 if (IsHandleCreated) {
1233 if (IsMdiChild) {
1234 MdiParent.ActivateMdiChild (this);
1235 } else if (IsMdiContainer) {
1236 SendControlFocus (mdi_container);
1237 } else {
1238 active = ActiveForm;
1239 if ((active != null) && (this != active)) {
1240 XplatUI.Activate(window.Handle);
1246 public void AddOwnedForm(Form ownedForm) {
1247 if (!owned_forms.Contains(ownedForm)) {
1248 owned_forms.Add(ownedForm);
1250 ownedForm.Owner = this;
1253 public void Close () {
1254 if (IsDisposed)
1255 return;
1257 if (!is_visible)
1258 return;
1260 XplatUI.SendMessage(this.Handle, Msg.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
1263 public void LayoutMdi(MdiLayout value) {
1264 if (mdi_container != null) {
1265 mdi_container.LayoutMdi(value);
1269 public void RemoveOwnedForm(Form ownedForm) {
1270 owned_forms.Remove(ownedForm);
1273 public void SetDesktopBounds(int x, int y, int width, int height) {
1274 DesktopBounds = new Rectangle(x, y, width, height);
1277 public void SetDesktopLocation(int x, int y) {
1278 DesktopLocation = new Point(x, y);
1281 public DialogResult ShowDialog() {
1282 return ShowDialog(this.owner);
1285 public DialogResult ShowDialog(IWin32Window ownerWin32) {
1286 Rectangle area;
1287 bool confined;
1288 IntPtr capture_window;
1290 owner = null;
1292 if (ownerWin32 != null) {
1293 Control c = Control.FromHandle (ownerWin32.Handle);
1294 if (c != null)
1295 owner = c.TopLevelControl as Form;
1298 if (owner == this) {
1299 throw new InvalidOperationException("The 'ownerWin32' cannot be the form being shown.");
1302 if (is_modal) {
1303 throw new InvalidOperationException("The form is already displayed as a modal dialog.");
1306 if (Visible) {
1307 throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
1310 if (!Enabled) {
1311 throw new InvalidOperationException("Cannot display a disabled form as modal dialog.");
1314 if (TopLevelControl != this) {
1315 throw new InvalidOperationException("Can only display TopLevel forms as modal dialog.");
1318 #if broken
1319 // Can't do this, will screw us in the modal loop
1320 form_parent_window.Parent = this.owner;
1321 #endif
1323 // Release any captures
1324 XplatUI.GrabInfo(out capture_window, out confined, out area);
1325 if (capture_window != IntPtr.Zero) {
1326 XplatUI.UngrabWindow(capture_window);
1329 #if not
1330 // Commented out; we instead let the Visible=true inside the runloop create the control
1331 // otherwise setting DialogResult inside any of the events that are triggered by the
1332 // create will not actually cause the form to not be displayed.
1333 // Leaving this comment here in case there was an actual purpose to creating the control
1334 // in here.
1335 if (!IsHandleCreated) {
1336 CreateControl();
1338 #endif
1340 Application.RunLoop(true, new ApplicationContext(this));
1342 if (owner != null) {
1343 // Cannot use Activate(), it has a check for the current active window...
1344 XplatUI.Activate(owner.window.Handle);
1347 if (DialogResult != DialogResult.None) {
1348 return DialogResult;
1350 DialogResult = DialogResult.Cancel;
1351 return DialogResult.Cancel;
1354 public override string ToString() {
1355 return GetType().FullName.ToString() + ", Text: " + Text;
1357 #endregion // Public Instance Methods
1359 #region Protected Instance Methods
1360 protected void ActivateMdiChild(Form form) {
1361 if (!IsMdiContainer)
1362 return;
1363 mdi_container.ActivateChild (form);
1364 OnMdiChildActivate(EventArgs.Empty);
1367 [EditorBrowsable(EditorBrowsableState.Advanced)]
1368 protected override void AdjustFormScrollbars(bool displayScrollbars) {
1369 base.AdjustFormScrollbars (displayScrollbars);
1372 #if NET_2_0
1373 [EditorBrowsable(EditorBrowsableState.Never)]
1374 [Obsolete ("This method has been deprecated")] // XXX what to use instead?
1375 #else
1376 [EditorBrowsable(EditorBrowsableState.Advanced)]
1377 #endif
1378 protected void ApplyAutoScaling()
1380 SizeF current_size_f = GetAutoScaleSize (DeviceContext, Font);
1381 Size current_size = new Size ((int) current_size_f.Width, (int) current_size_f.Height);
1382 float dx;
1383 float dy;
1385 if (current_size == autoscale_base_size)
1386 return;
1388 if (Environment.GetEnvironmentVariable ("MONO_MWF_SCALING") == "disable"){
1389 return;
1393 // I tried applying the Fudge height factor from:
1394 // http://blogs.msdn.com/mharsh/archive/2004/01/25/62621.aspx
1395 // but it makes things larger without looking better.
1397 if (current_size_f.Width != AutoScaleBaseSize.Width) {
1398 dx = current_size_f.Width / AutoScaleBaseSize.Width + 0.08f;
1399 } else {
1400 dx = 1;
1403 if (current_size_f.Height != AutoScaleBaseSize.Height) {
1404 dy = current_size_f.Height / AutoScaleBaseSize.Height + 0.08f;
1405 } else {
1406 dy = 1;
1409 Scale (dx, dy);
1411 AutoScaleBaseSize = current_size;
1414 protected void CenterToParent() {
1415 Control ctl;
1416 int w;
1417 int h;
1419 if (Width > 0) {
1420 w = Width;
1421 } else {
1422 w = DefaultSize.Width;
1425 if (Height > 0) {
1426 h = Height;
1427 } else {
1428 h = DefaultSize.Height;
1431 ctl = null;
1432 if (Parent != null) {
1433 ctl = Parent;
1434 } else if (owner != null) {
1435 ctl = owner;
1438 if (owner != null) {
1439 this.Location = new Point(ctl.Left + ctl.Width / 2 - w /2, ctl.Top + ctl.Height / 2 - h / 2);
1443 protected void CenterToScreen() {
1444 Size DisplaySize;
1445 int w;
1446 int h;
1448 if (Width > 0) {
1449 w = Width;
1450 } else {
1451 w = DefaultSize.Width;
1454 if (Height > 0) {
1455 h = Height;
1456 } else {
1457 h = DefaultSize.Height;
1460 XplatUI.GetDisplaySize(out DisplaySize);
1461 this.Location = new Point(DisplaySize.Width / 2 - w / 2, DisplaySize.Height / 2 - h / 2);
1464 [EditorBrowsable(EditorBrowsableState.Advanced)]
1465 protected override Control.ControlCollection CreateControlsInstance() {
1466 return base.CreateControlsInstance ();
1469 [EditorBrowsable(EditorBrowsableState.Advanced)]
1470 protected override void CreateHandle() {
1471 base.CreateHandle ();
1473 Application.AddForm (this);
1475 UpdateBounds();
1477 if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
1478 if (allow_transparency) {
1479 XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
1483 XplatUI.SetWindowMinMax(window.Handle, maximized_bounds, minimum_size, maximum_size);
1484 if ((FormBorderStyle != FormBorderStyle.FixedDialog) && (icon != null)) {
1485 XplatUI.SetIcon(window.Handle, icon);
1488 if ((owner != null) && (owner.IsHandleCreated)) {
1489 XplatUI.SetTopmost(window.Handle, owner.window.Handle, true);
1492 for (int i = 0; i < owned_forms.Count; i++) {
1493 if (owned_forms[i].IsHandleCreated)
1494 XplatUI.SetTopmost(owned_forms[i].window.Handle, window.Handle, true);
1497 if (window_manager != null && window_state != FormWindowState.Normal) {
1498 window_manager.SetWindowState (FormWindowState.Normal, window_state);
1503 [EditorBrowsable(EditorBrowsableState.Advanced)]
1504 protected override void DefWndProc(ref Message m) {
1505 base.DefWndProc (ref m);
1508 protected override void Dispose(bool disposing)
1510 for (int i = 0; i < owned_forms.Count; i++)
1511 ((Form)owned_forms[i]).Owner = null;
1513 owned_forms.Clear ();
1515 base.Dispose (disposing);
1517 Application.RemoveForm (this);
1520 [EditorBrowsable(EditorBrowsableState.Advanced)]
1521 protected virtual void OnActivated(EventArgs e)
1523 EventHandler eh = (EventHandler)(Events [ActivatedEvent]);
1524 if (eh != null)
1525 eh (this, e);
1528 [EditorBrowsable(EditorBrowsableState.Advanced)]
1529 protected virtual void OnClosed(EventArgs e) {
1530 EventHandler eh = (EventHandler)(Events [ClosedEvent]);
1531 if (eh != null)
1532 eh (this, e);
1535 // Consider calling FireClosingEvents instead of calling this directly.
1536 [EditorBrowsable (EditorBrowsableState.Advanced)]
1537 protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
1538 CancelEventHandler eh = (CancelEventHandler)(Events [ClosingEvent]);
1539 if (eh != null)
1540 eh (this, e);
1543 [EditorBrowsable(EditorBrowsableState.Advanced)]
1544 protected override void OnCreateControl() {
1545 base.OnCreateControl ();
1547 if (menu != null) {
1548 XplatUI.SetMenu(window.Handle, menu);
1551 OnLoad(EventArgs.Empty);
1553 // Send initial location
1554 OnLocationChanged(EventArgs.Empty);
1556 if (IsMdiContainer) {
1557 mdi_container.LayoutMdi (MdiLayout.Cascade);
1561 [EditorBrowsable(EditorBrowsableState.Advanced)]
1562 protected virtual void OnDeactivate(EventArgs e) {
1563 EventHandler eh = (EventHandler)(Events [DeactivateEvent]);
1564 if (eh != null)
1565 eh (this, e);
1568 [EditorBrowsable(EditorBrowsableState.Advanced)]
1569 protected override void OnFontChanged(EventArgs e) {
1570 base.OnFontChanged (e);
1573 [EditorBrowsable(EditorBrowsableState.Advanced)]
1574 protected override void OnHandleCreated(EventArgs e) {
1575 XplatUI.SetBorderStyle(window.Handle, form_border_style);
1576 base.OnHandleCreated (e);
1579 [EditorBrowsable(EditorBrowsableState.Advanced)]
1580 protected override void OnHandleDestroyed(EventArgs e) {
1581 base.OnHandleDestroyed (e);
1584 [EditorBrowsable(EditorBrowsableState.Advanced)]
1585 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
1586 InputLanguageChangedEventHandler eh = (InputLanguageChangedEventHandler)(Events [InputLanguageChangedEvent]);
1587 if (eh != null)
1588 eh (this, e);
1591 [EditorBrowsable(EditorBrowsableState.Advanced)]
1592 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
1593 InputLanguageChangingEventHandler eh = (InputLanguageChangingEventHandler)(Events [InputLanguageChangingEvent]);
1594 if (eh != null)
1595 eh (this, e);
1598 [EditorBrowsable(EditorBrowsableState.Advanced)]
1599 protected virtual void OnLoad(EventArgs e) {
1600 if (AutoScale){
1601 ApplyAutoScaling ();
1602 AutoScale = false;
1605 EventHandler eh = (EventHandler)(Events [LoadEvent]);
1606 if (eh != null)
1607 eh (this, e);
1609 if (!IsMdiChild) {
1610 switch (StartPosition) {
1611 case FormStartPosition.CenterScreen:
1612 this.CenterToScreen();
1613 break;
1614 case FormStartPosition.CenterParent:
1615 this.CenterToParent ();
1616 break;
1617 case FormStartPosition.Manual:
1618 Left = CreateParams.X;
1619 Top = CreateParams.Y;
1620 break;
1623 is_loaded = true;
1626 [EditorBrowsable(EditorBrowsableState.Advanced)]
1627 protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
1628 EventHandler eh = (EventHandler)(Events [MaximizedBoundsChangedEvent]);
1629 if (eh != null)
1630 eh (this, e);
1633 [EditorBrowsable(EditorBrowsableState.Advanced)]
1634 protected virtual void OnMaximumSizeChanged(EventArgs e) {
1635 EventHandler eh = (EventHandler)(Events [MaximumSizeChangedEvent]);
1636 if (eh != null)
1637 eh (this, e);
1640 [EditorBrowsable(EditorBrowsableState.Advanced)]
1641 protected virtual void OnMdiChildActivate(EventArgs e) {
1642 EventHandler eh = (EventHandler)(Events [MdiChildActivateEvent]);
1643 if (eh != null)
1644 eh (this, e);
1647 [EditorBrowsable(EditorBrowsableState.Advanced)]
1648 protected virtual void OnMenuComplete(EventArgs e) {
1649 EventHandler eh = (EventHandler)(Events [MenuCompleteEvent]);
1650 if (eh != null)
1651 eh (this, e);
1654 [EditorBrowsable(EditorBrowsableState.Advanced)]
1655 protected virtual void OnMenuStart(EventArgs e) {
1656 EventHandler eh = (EventHandler)(Events [MenuStartEvent]);
1657 if (eh != null)
1658 eh (this, e);
1661 [EditorBrowsable(EditorBrowsableState.Advanced)]
1662 protected virtual void OnMinimumSizeChanged(EventArgs e) {
1663 EventHandler eh = (EventHandler)(Events [MinimumSizeChangedEvent]);
1664 if (eh != null)
1665 eh (this, e);
1668 [EditorBrowsable(EditorBrowsableState.Advanced)]
1669 protected override void OnPaint (PaintEventArgs pevent) {
1670 base.OnPaint (pevent);
1673 [EditorBrowsable(EditorBrowsableState.Advanced)]
1674 protected override void OnResize(EventArgs e) {
1675 base.OnResize(e);
1677 if (this.IsMdiChild && ParentForm != null) {
1678 ParentForm.PerformLayout();
1679 ParentForm.Size = ParentForm.Size;
1683 [EditorBrowsable(EditorBrowsableState.Advanced)]
1684 protected override void OnStyleChanged(EventArgs e) {
1685 base.OnStyleChanged (e);
1688 [EditorBrowsable(EditorBrowsableState.Advanced)]
1689 protected override void OnTextChanged(EventArgs e) {
1690 base.OnTextChanged (e);
1692 if (mdi_container != null)
1693 mdi_container.SetParentText(true);
1696 [EditorBrowsable(EditorBrowsableState.Advanced)]
1697 protected override void OnVisibleChanged(EventArgs e) {
1698 base.OnVisibleChanged (e);
1700 if (Visible) {
1701 if (window_manager != null)
1702 window_manager.SetWindowState (WindowState, WindowState);
1706 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
1707 if (base.ProcessCmdKey (ref msg, keyData)) {
1708 return true;
1711 // Give our menu a shot
1712 if (ActiveMenu != null) {
1713 return ActiveMenu.ProcessCmdKey(ref msg, keyData);
1716 return false;
1719 // LAMESPEC - Not documented that Form overrides ProcessDialogChar; class-status showed
1720 [EditorBrowsable (EditorBrowsableState.Advanced)]
1721 protected override bool ProcessDialogChar(char charCode) {
1722 return base.ProcessDialogChar (charCode);
1725 protected override bool ProcessDialogKey(Keys keyData) {
1726 if ((keyData & Keys.Modifiers) == 0) {
1727 if (keyData == Keys.Enter) {
1728 IntPtr window = XplatUI.GetFocus ();
1729 Control c = Control.FromHandle (window);
1730 if (c is Button && c.FindForm () == this) {
1731 ((Button)c).PerformClick ();
1732 return true;
1734 else if (accept_button != null) {
1735 accept_button.PerformClick();
1736 return true;
1738 } else if (keyData == Keys.Escape && cancel_button != null) {
1739 cancel_button.PerformClick();
1740 return true;
1743 return base.ProcessDialogKey(keyData);
1746 protected override bool ProcessKeyPreview(ref Message msg) {
1747 if (key_preview) {
1748 if (ProcessKeyEventArgs(ref msg)) {
1749 return true;
1752 return base.ProcessKeyPreview (ref msg);
1755 protected override bool ProcessTabKey(bool forward) {
1756 return SelectNextControl(ActiveControl, forward, true, true, true);
1759 #if NET_2_0
1760 [EditorBrowsable(EditorBrowsableState.Never)]
1761 #else
1762 [EditorBrowsable(EditorBrowsableState.Advanced)]
1763 #endif
1764 protected override void ScaleCore(float dx, float dy) {
1765 try {
1766 SuspendLayout();
1768 // We can't scale max or min windows
1769 if (WindowState == FormWindowState.Normal) {
1770 // We cannot call base since base also adjusts X/Y, but
1771 // a form is toplevel and doesn't move
1772 Size size;
1774 size = ClientSize;
1775 if (!GetStyle(ControlStyles.FixedWidth)) {
1776 size.Width = (int)(size.Width * dx);
1779 if (!GetStyle(ControlStyles.FixedHeight)) {
1780 size.Height = (int)(size.Height * dy);
1783 ClientSize = size;
1786 /* Now scale our children */
1787 Control [] controls = Controls.GetAllControls ();
1788 for (int i=0; i < controls.Length; i++) {
1789 controls[i].Scale(dx, dy);
1793 finally {
1794 ResumeLayout();
1798 protected override void Select(bool directed, bool forward) {
1799 Form parent;
1801 if (directed) {
1802 base.SelectNextControl(null, forward, true, true, true);
1805 parent = this.ParentForm;
1806 if (parent != null) {
1807 parent.ActiveControl = this;
1810 Activate();
1813 [EditorBrowsable(EditorBrowsableState.Advanced)]
1814 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
1815 base.SetBoundsCore (x, y, width, height, specified);
1818 [EditorBrowsable(EditorBrowsableState.Advanced)]
1819 protected override void SetClientSizeCore(int x, int y) {
1820 if ((minimum_size.Width != 0) && (x < minimum_size.Width)) {
1821 x = minimum_size.Width;
1822 } else if ((maximum_size.Width != 0) && (x > maximum_size.Width)) {
1823 x = maximum_size.Width;
1826 if ((minimum_size.Height != 0) && (y < minimum_size.Height)) {
1827 y = minimum_size.Height;
1828 } else if ((maximum_size.Height != 0) && (y > maximum_size.Height)) {
1829 y = maximum_size.Height;
1832 Rectangle ClientRect = new Rectangle(0, 0, x, y);
1833 Rectangle WindowRect;
1834 CreateParams cp = this.CreateParams;
1836 clientsize_set = new Size(x, y);
1838 if (XplatUI.CalculateWindowRect(ref ClientRect, cp.Style, cp.ExStyle, cp.menu, out WindowRect)) {
1839 SetBounds(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
1843 [EditorBrowsable(EditorBrowsableState.Advanced)]
1844 protected override void SetVisibleCore(bool value) {
1845 is_changing_visible_state = true;
1846 has_been_visible = value || has_been_visible;
1847 base.SetVisibleCore (value);
1848 is_changing_visible_state = false;
1851 protected override void UpdateDefaultButton() {
1852 base.UpdateDefaultButton ();
1855 [EditorBrowsable(EditorBrowsableState.Advanced)]
1856 protected override void WndProc(ref Message m) {
1858 if (window_manager != null && window_manager.HandleMessage (ref m)) {
1859 return;
1862 switch((Msg)m.Msg) {
1863 case Msg.WM_DESTROY: {
1864 base.WndProc(ref m);
1865 if (!RecreatingHandle) {
1866 this.closing = true;
1868 return;
1871 case Msg.WM_CLOSE_INTERNAL: {
1872 DestroyHandle();
1873 break;
1876 case Msg.WM_CLOSE: {
1877 Form act = Form.ActiveForm;
1878 if (act != null && act != this && act.Modal == true) {
1879 return;
1882 if (mdi_container != null) {
1883 foreach (Form mdi_child in mdi_container.MdiChildren) {
1884 mdi_child.FireClosingEvents (CloseReason.MdiFormClosing);
1888 if (!is_modal) {
1889 if (!FireClosingEvents (CloseReason.UserClosing)) {
1890 OnClosed (EventArgs.Empty);
1891 closing = true;
1892 Dispose ();
1894 else {
1895 closing = false;
1897 } else {
1898 if (FireClosingEvents (CloseReason.UserClosing)) {
1899 DialogResult = DialogResult.None;
1900 closing = false;
1902 else {
1903 OnClosed (EventArgs.Empty);
1904 closing = true;
1905 Hide ();
1909 return;
1912 case Msg.WM_WINDOWPOSCHANGED: {
1913 if (WindowState != FormWindowState.Minimized) {
1914 base.WndProc(ref m);
1916 return;
1919 #if NET_2_0
1920 case Msg.WM_SYSCOMMAND: {
1921 // Let *Strips know the app's title bar was clicked
1922 if (XplatUI.IsEnabled (Handle))
1923 ToolStripManager.FireAppClicked ();
1925 base.WndProc(ref m);
1926 break;
1928 #endif
1930 case Msg.WM_ACTIVATE: {
1931 if (m.WParam != (IntPtr)WindowActiveFlags.WA_INACTIVE) {
1932 if (is_loaded) {
1933 SelectActiveControl ();
1935 if (ActiveControl != null && !ActiveControl.Focused)
1936 SendControlFocus (ActiveControl);
1939 OnActivated(EventArgs.Empty);
1940 } else {
1941 OnDeactivate(EventArgs.Empty);
1943 return;
1946 case Msg.WM_KILLFOCUS: {
1947 base.WndProc(ref m);
1948 return;
1951 case Msg.WM_SETFOCUS: {
1952 if (ActiveControl != null && ActiveControl != this) {
1953 ActiveControl.Focus();
1954 return; // FIXME - do we need to run base.WndProc, even though we just changed focus?
1956 base.WndProc(ref m);
1957 return;
1960 // Menu drawing
1961 case Msg.WM_NCLBUTTONDOWN: {
1962 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
1963 ActiveMenu.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, Control.MousePosition.X, Control.MousePosition.Y, 0));
1966 if (ActiveMaximizedMdiChild != null) {
1967 if (ActiveMaximizedMdiChild.HandleMenuMouseDown (ActiveMenu,
1968 LowOrder ((int) m.LParam.ToInt32 ()),
1969 HighOrder ((int) m.LParam.ToInt32 ()))) {
1970 // Don't let base process this message, otherwise we won't
1971 // get a WM_NCLBUTTONUP.
1972 return;
1975 base.WndProc(ref m);
1976 return;
1978 case Msg.WM_NCLBUTTONUP: {
1979 if (ActiveMaximizedMdiChild != null) {
1980 ActiveMaximizedMdiChild.HandleMenuMouseUp (ActiveMenu,
1981 LowOrder ((int)m.LParam.ToInt32 ()),
1982 HighOrder ((int)m.LParam.ToInt32 ()));
1984 base.WndProc (ref m);
1985 return;
1988 case Msg.WM_NCMOUSELEAVE: {
1989 if (ActiveMaximizedMdiChild != null) {
1990 ActiveMaximizedMdiChild.HandleMenuMouseLeave(ActiveMenu,
1991 LowOrder((int)m.LParam.ToInt32()),
1992 HighOrder((int)m.LParam.ToInt32()));
1994 base.WndProc(ref m);
1995 return;
1998 case Msg.WM_NCMOUSEMOVE: {
1999 if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
2000 ActiveMenu.OnMouseMove(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0));
2003 if (ActiveMaximizedMdiChild != null) {
2004 XplatUI.RequestAdditionalWM_NCMessages (Handle, false, true);
2005 ActiveMaximizedMdiChild.HandleMenuMouseMove (ActiveMenu,
2006 LowOrder ((int)m.LParam.ToInt32 ()),
2007 HighOrder ((int)m.LParam.ToInt32 ()));
2009 base.WndProc(ref m);
2010 return;
2013 case Msg.WM_NCPAINT: {
2014 if (ActiveMenu != null) {
2015 PaintEventArgs pe;
2016 Point pnt;
2018 pe = XplatUI.PaintEventStart(Handle, false);
2019 pnt = XplatUI.GetMenuOrigin(window.Handle);
2021 // The entire menu has to be in the clip rectangle because the
2022 // control buttons are right-aligned and otherwise they would
2023 // stay painted when the window gets resized.
2024 Rectangle clip = new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0);
2025 clip = Rectangle.Union(clip, pe.ClipRectangle);
2026 pe.SetClip(clip);
2027 pe.Graphics.SetClip(clip);
2029 ActiveMenu.Draw (pe, new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0));
2031 if (ActiveMaximizedMdiChild != null) {
2032 ActiveMaximizedMdiChild.DrawMaximizedButtons (ActiveMenu, pe);
2035 XplatUI.PaintEventEnd(Handle, false);
2038 base.WndProc(ref m);
2039 return;
2042 case Msg.WM_NCCALCSIZE: {
2043 XplatUIWin32.NCCALCSIZE_PARAMS ncp;
2045 if ((ActiveMenu != null) && (m.WParam == (IntPtr)1)) {
2046 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
2048 // Adjust for menu
2049 ncp.rgrc1.top += ThemeEngine.Current.CalcMenuBarSize (DeviceContext, ActiveMenu, ncp.rgrc1.right - ncp.rgrc1.left);
2050 Marshal.StructureToPtr(ncp, m.LParam, true);
2052 DefWndProc(ref m);
2053 break;
2056 case Msg.WM_MOUSEMOVE: {
2057 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2058 MouseEventArgs args;
2060 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
2061 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2062 active_tracker.OnMotion(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2063 break;
2065 base.WndProc(ref m);
2066 break;
2069 case Msg.WM_LBUTTONDOWN:
2070 case Msg.WM_MBUTTONDOWN:
2071 case Msg.WM_RBUTTONDOWN: {
2072 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2073 MouseEventArgs args;
2075 args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
2076 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2077 active_tracker.OnMouseDown(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2078 return;
2080 base.WndProc(ref m);
2081 return;
2084 case Msg.WM_LBUTTONUP:
2085 case Msg.WM_MBUTTONUP:
2086 case Msg.WM_RBUTTONUP: {
2087 if (XplatUI.IsEnabled (Handle) && active_tracker != null) {
2088 MouseEventArgs args;
2089 MouseButtons mb = FromParamToMouseButtons ((int) m.WParam.ToInt32());
2091 // We add in the button that was released (not sent in WParam)
2092 switch((Msg)m.Msg) {
2093 case Msg.WM_LBUTTONUP:
2094 mb |= MouseButtons.Left;
2095 break;
2096 case Msg.WM_MBUTTONUP:
2097 mb |= MouseButtons.Middle;
2098 break;
2099 case Msg.WM_RBUTTONUP:
2100 mb |= MouseButtons.Right;
2101 break;
2104 args = new MouseEventArgs (mb, mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
2105 active_tracker.OnMouseUp(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
2106 mouse_clicks = 1;
2107 return;
2109 base.WndProc(ref m);
2110 return;
2113 case Msg.WM_GETMINMAXINFO: {
2114 MINMAXINFO mmi;
2116 if (m.LParam != IntPtr.Zero) {
2117 mmi = (MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO));
2119 default_maximized_bounds = new Rectangle(mmi.ptMaxPosition.x, mmi.ptMaxPosition.y, mmi.ptMaxSize.x, mmi.ptMaxSize.y);
2120 if (maximized_bounds != Rectangle.Empty) {
2121 mmi.ptMaxPosition.x = maximized_bounds.Left;
2122 mmi.ptMaxPosition.y = maximized_bounds.Top;
2123 mmi.ptMaxSize.x = maximized_bounds.Width;
2124 mmi.ptMaxSize.y = maximized_bounds.Height;
2127 if (minimum_size != Size.Empty) {
2128 mmi.ptMinTrackSize.x = minimum_size.Width;
2129 mmi.ptMinTrackSize.y = minimum_size.Height;
2132 if (maximum_size != Size.Empty) {
2133 mmi.ptMaxTrackSize.x = maximum_size.Width;
2134 mmi.ptMaxTrackSize.y = maximum_size.Height;
2136 Marshal.StructureToPtr(mmi, m.LParam, false);
2138 break;
2141 #if NET_2_0
2142 case Msg.WM_MOUSEACTIVATE: {
2143 // Let *Strips know the form or another control has been clicked
2144 if (XplatUI.IsEnabled (Handle))
2145 ToolStripManager.FireAppClicked ();
2147 base.WndProc (ref m);
2148 break;
2151 case Msg.WM_ACTIVATEAPP: {
2152 // Let *Strips know the app lost focus
2153 if (m.WParam == (IntPtr)0)
2154 if (XplatUI.IsEnabled (Handle))
2155 ToolStripManager.FireAppFocusChanged (this);
2157 base.WndProc (ref m);
2158 break;
2160 #endif
2162 default: {
2163 base.WndProc (ref m);
2164 break;
2168 #endregion // Protected Instance Methods
2170 internal override void FireEnter ()
2172 // do nothing - forms don't generate OnEnter
2175 internal override void FireLeave ()
2177 // do nothing - forms don't generate OnLeave
2180 internal void RemoveWindowManager ()
2182 window_manager = null;
2185 internal override void CheckAcceptButton()
2187 if (accept_button != null) {
2188 Button a_button = accept_button as Button;
2190 if (ActiveControl == a_button)
2191 return;
2193 if (ActiveControl is Button) {
2194 a_button.paint_as_acceptbutton = false;
2195 a_button.Redraw();
2196 return;
2197 } else {
2198 a_button.paint_as_acceptbutton = true;
2199 a_button.Redraw();
2204 #region Events
2205 static object ActivatedEvent = new object ();
2206 static object ClosedEvent = new object ();
2207 static object ClosingEvent = new object ();
2208 static object DeactivateEvent = new object ();
2209 static object InputLanguageChangedEvent = new object ();
2210 static object InputLanguageChangingEvent = new object ();
2211 static object LoadEvent = new object ();
2212 static object MaximizedBoundsChangedEvent = new object ();
2213 static object MaximumSizeChangedEvent = new object ();
2214 static object MdiChildActivateEvent = new object ();
2215 static object MenuCompleteEvent = new object ();
2216 static object MenuStartEvent = new object ();
2217 static object MinimumSizeChangedEvent = new object ();
2219 public event EventHandler Activated {
2220 add { Events.AddHandler (ActivatedEvent, value); }
2221 remove { Events.RemoveHandler (ActivatedEvent, value); }
2224 #if NET_2_0
2225 [Browsable (false)]
2226 [EditorBrowsable (EditorBrowsableState.Never)]
2227 #endif
2228 public event EventHandler Closed {
2229 add { Events.AddHandler (ClosedEvent, value); }
2230 remove { Events.RemoveHandler (ClosedEvent, value); }
2233 #if NET_2_0
2234 [Browsable (false)]
2235 [EditorBrowsable (EditorBrowsableState.Never)]
2236 #endif
2237 public event CancelEventHandler Closing {
2238 add { Events.AddHandler (ClosingEvent, value); }
2239 remove { Events.RemoveHandler (ClosingEvent, value); }
2242 public event EventHandler Deactivate {
2243 add { Events.AddHandler (DeactivateEvent, value); }
2244 remove { Events.RemoveHandler (DeactivateEvent, value); }
2247 public event InputLanguageChangedEventHandler InputLanguageChanged {
2248 add { Events.AddHandler (InputLanguageChangedEvent, value); }
2249 remove { Events.RemoveHandler (InputLanguageChangedEvent, value); }
2252 public event InputLanguageChangingEventHandler InputLanguageChanging {
2253 add { Events.AddHandler (InputLanguageChangingEvent, value); }
2254 remove { Events.RemoveHandler (InputLanguageChangingEvent, value); }
2257 public event EventHandler Load {
2258 add { Events.AddHandler (LoadEvent, value); }
2259 remove { Events.RemoveHandler (LoadEvent, value); }
2262 public event EventHandler MaximizedBoundsChanged {
2263 add { Events.AddHandler (MaximizedBoundsChangedEvent, value); }
2264 remove { Events.RemoveHandler (MaximizedBoundsChangedEvent, value); }
2267 public event EventHandler MaximumSizeChanged {
2268 add { Events.AddHandler (MaximumSizeChangedEvent, value); }
2269 remove { Events.RemoveHandler (MaximumSizeChangedEvent, value); }
2272 public event EventHandler MdiChildActivate {
2273 add { Events.AddHandler (MdiChildActivateEvent, value); }
2274 remove { Events.RemoveHandler (MdiChildActivateEvent, value); }
2277 #if NET_2_0
2278 [Browsable (false)]
2279 #endif
2280 public event EventHandler MenuComplete {
2281 add { Events.AddHandler (MenuCompleteEvent, value); }
2282 remove { Events.RemoveHandler (MenuCompleteEvent, value); }
2285 #if NET_2_0
2286 [Browsable (false)]
2287 #endif
2288 public event EventHandler MenuStart {
2289 add { Events.AddHandler (MenuStartEvent, value); }
2290 remove { Events.RemoveHandler (MenuStartEvent, value); }
2293 public event EventHandler MinimumSizeChanged {
2294 add { Events.AddHandler (MinimumSizeChangedEvent, value); }
2295 remove { Events.RemoveHandler (MinimumSizeChangedEvent, value); }
2299 [Browsable(false)]
2300 [EditorBrowsable(EditorBrowsableState.Never)]
2301 public new event EventHandler TabIndexChanged {
2302 add { base.TabIndexChanged += value; }
2303 remove { base.TabIndexChanged -= value; }
2305 #endregion // Events
2307 #if NET_2_0
2308 [SettingsBindable (true)]
2309 public override string Text {
2310 get {
2311 return base.Text;
2314 set {
2315 base.Text = value;
2319 [SettingsBindable (true)]
2320 public new Point Location {
2321 get {
2322 return base.Location;
2325 set {
2326 base.Location = value;
2330 static object AutoValidateChangedEvent = new object ();
2331 static object FormClosingEvent = new object ();
2332 static object FormClosedEvent = new object ();
2333 static object HelpButtonClickedEvent = new object ();
2334 static object ResizeEndEvent = new object ();
2335 static object ResizeBeginEvent = new object ();
2336 static object RightToLeftLayoutChangedEvent = new object ();
2337 static object ShownEvent = new object ();
2339 [Browsable (true)]
2340 [EditorBrowsable (EditorBrowsableState.Always)]
2341 public new event EventHandler AutoSizeChanged {
2342 add { base.AutoSizeChanged += value; }
2343 remove { base.AutoSizeChanged -= value; }
2346 [Browsable (true)]
2347 [EditorBrowsable (EditorBrowsableState.Always)]
2348 public event EventHandler AutoValidateChanged {
2349 add { Events.AddHandler (AutoValidateChangedEvent, value); }
2350 remove { Events.RemoveHandler (AutoValidateChangedEvent, value); }
2353 public event FormClosingEventHandler FormClosing {
2354 add { Events.AddHandler (FormClosingEvent, value); }
2355 remove { Events.RemoveHandler (FormClosingEvent, value); }
2358 public event FormClosedEventHandler FormClosed {
2359 add { Events.AddHandler (FormClosedEvent, value); }
2360 remove { Events.RemoveHandler (FormClosedEvent, value); }
2363 [Browsable (true)]
2364 [EditorBrowsable (EditorBrowsableState.Always)]
2365 public event CancelEventHandler HelpButtonClicked {
2366 add { Events.AddHandler (HelpButtonClickedEvent, value); }
2367 remove { Events.RemoveHandler (HelpButtonClickedEvent, value); }
2370 [Browsable (false)]
2371 [EditorBrowsable (EditorBrowsableState.Never)]
2372 public new event EventHandler MarginChanged {
2373 add { base.MarginChanged += value; }
2374 remove { base.MarginChanged -= value; }
2377 public event EventHandler RightToLeftLayoutChanged {
2378 add { Events.AddHandler (RightToLeftLayoutChangedEvent, value); }
2379 remove { Events.RemoveHandler (RightToLeftLayoutChangedEvent, value); }
2382 public event EventHandler ResizeBegin {
2383 add { Events.AddHandler (ResizeBeginEvent, value); }
2384 remove { Events.RemoveHandler (ResizeBeginEvent, value); }
2387 public event EventHandler ResizeEnd {
2388 add { Events.AddHandler (ResizeEndEvent, value); }
2389 remove { Events.RemoveHandler (ResizeEndEvent, value); }
2392 public event EventHandler Shown {
2393 add { Events.AddHandler (ShownEvent, value); }
2394 remove { Events.RemoveHandler (ShownEvent, value); }
2397 [Browsable (false)]
2398 [EditorBrowsable (EditorBrowsableState.Never)]
2399 public new event EventHandler TabStopChanged {
2400 add { base.TabStopChanged += value; }
2401 remove { base.TabStopChanged -= value; }
2404 // Consider calling FireClosingEvents instead of calling this directly.
2405 [EditorBrowsable (EditorBrowsableState.Advanced)]
2406 protected virtual void OnFormClosing (FormClosingEventArgs e)
2408 FormClosingEventHandler eh = (FormClosingEventHandler)(Events [FormClosingEvent]);
2409 if (eh != null)
2410 eh (this, e);
2413 [EditorBrowsable (EditorBrowsableState.Advanced)]
2414 protected virtual void OnResizeBegin (EventArgs e)
2416 EventHandler eh = (EventHandler) (Events [ResizeBeginEvent]);
2417 if (eh != null)
2418 eh (this, e);
2421 [EditorBrowsable (EditorBrowsableState.Advanced)]
2422 protected virtual void OnResizeEnd (EventArgs e)
2424 EventHandler eh = (EventHandler) (Events [ResizeEndEvent]);
2425 if (eh != null)
2426 eh (this, e);
2428 #endif