context: // Permission is hereby granted, free of charge, to any person obtaining
  // a copy of this software and associated documentation files (the
  // "Software"), to deal in the Software without restriction, including
  // without limitation the rights to use, copy, modify, merge, publish,
  // distribute, sublicense, and/or sell copies of the Software, and to
  // permit persons to whom the Software is furnished to do so, subject to
  // the following conditions:
  // 
  // The above copyright notice and this permission notice shall be
  // included in all copies or substantial portions of the Software.
  // 
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  //
  // Copyright (c) 2004-2006 Novell, Inc.
  //
  // Authors:
  //	Peter Bartok	pbartok@novell.com
  //
  
  // NOT COMPLETE
  
  using System;
  using System.Drawing;
  using System.ComponentModel;
  using System.ComponentModel.Design;
  using System.ComponentModel.Design.Serialization;
  using System.Collections;
  using System.Runtime.InteropServices;
  using System.Threading;
  using System.Collections.Generic;
  
  namespace System.Windows.Forms {
  	[DesignerCategory("Form")]
  	[DesignTimeVisible(false)]
  	[Designer("System.Windows.Forms.Design.FormDocumentDesigner, " + Consts.AssemblySystem_Design, typeof(IRootDesigner))]
  	[DefaultEvent("Load")]
  	[ClassInterface (ClassInterfaceType.AutoDispatch)]
  	[InitializationEvent ("Load")]
  	[ComVisible (true)]
  	[ToolboxItemFilter ("System.Windows.Forms.Control.TopLevel")]
  	[ToolboxItem(false)]
  	public class Form : ContainerControl {
  		#region Local Variables
  		internal bool			closing;
  		private bool			closed;
  		FormBorderStyle			form_border_style;
  		private bool			is_active;
  		private bool		        autoscale;
  		private Size			clientsize_set;
  		private Size		        autoscale_base_size;
  		private bool			allow_transparency;
  		private static Icon		default_icon;
  		internal bool			is_modal;
  		internal FormWindowState	window_state;
  		private bool			control_box;
  		private bool			minimize_box;
  		private bool			maximize_box;
  		private bool			help_button;
  		private bool			show_in_taskbar;
  		private bool			topmost;
  		private IButtonControl		accept_button;
  		private IButtonControl		cancel_button;
  		private DialogResult		dialog_result;
  		private FormStartPosition	start_position;
  		private Form			owner;
  		private Form.ControlCollection	owned_forms;
  		private MdiClient		mdi_container;
  		internal InternalWindowManager	window_manager;
  		private Form			mdi_parent;
  		private bool			key_preview;
  		private MainMenu		menu;
  		private	Icon			icon;
  		private Size			maximum_size;
  		private Size			minimum_size;
  		private SizeGripStyle		size_grip_style;
  		private SizeGrip		size_grip;
  		private Rectangle		maximized_bounds;
  		private Rectangle		default_maximized_bounds;
  		private double			opacity;
  		internal ApplicationContext	context;
  		Color				transparency_key;
  		private bool			is_loaded;
  		internal int			is_changing_visible_state;
  		internal bool			has_been_visible;
  		private bool			shown_raised;
  		private bool                    close_raised;
  		private bool			is_clientsize_set;
  		internal bool			suppress_closing_events;
  		internal bool			waiting_showwindow; // for XplatUIX11
  		private bool                    is_minimizing;
  		private bool			show_icon = true;
  		private MenuStrip		main_menu_strip;
  		private bool			right_to_left_layout;
  		private Rectangle		restore_bounds;
  		private bool			autoscale_base_size_set;
  		internal ArrayList disabled_by_showdialog = new ArrayList();
  		internal static ArrayList modal_dialogs = new ArrayList();
  		#endregion	// Local Variables
  
  		#region Private & Internal Methods
  		static Form ()
  		{
  			default_icon = ResourceImageLoader.GetIcon ("mono.ico");
  		}
  
  		internal bool IsLoaded {
  			get { return is_loaded; }
  		}
  
  		internal bool IsActive {
  			get {
  				return is_active;
  			}
  			set {
  				if (is_active == value || IsRecreating) {
  					return;
  				}
  				
  				is_active = value;
  				if (is_active) {
  					Application.AddForm (this);
  					OnActivated (EventArgs.Empty);
  				} else {
  					OnDeactivate (EventArgs.Empty);
  				}
  			}
  		}
  
  		// warning: this is only hooked up when an mdi container is created.
  		private void ControlAddedHandler (object sender, ControlEventArgs e)
  		{
  			if (mdi_container != null) {
  				mdi_container.SendToBack ();
  			}
  		}
  
  		// Convenience method for fire BOTH OnClosing and OnFormClosing events
  		// Returns the value of Cancel, so true means the Close was cancelled,
  		// and you shouldn't close the form.
  		internal bool FireClosingEvents (CloseReason reason, bool cancel)
  		{
  			CancelEventArgs cea = new CancelEventArgs (cancel);
  			this.OnClosing (cea);
  			
  			FormClosingEventArgs fcea = new FormClosingEventArgs (reason, cea.Cancel);
  			this.OnFormClosing (fcea);
  			return fcea.Cancel;
  		}
  
  		// Convenience method for fire BOTH OnClosed and OnFormClosed events
  		private void FireClosedEvents (CloseReason reason)
  		{
  			this.OnClosed (EventArgs.Empty);
  			this.OnFormClosed (new FormClosedEventArgs (reason));
  		}
  		
  		internal override Size GetPreferredSizeCore (Size proposedSize)
  		{
  			Size retsize = Size.Empty;
  			
  			foreach (Control child in Controls) {
  				Size child_preferred_size;
  				if (child.AutoSize)
  					child_preferred_size = child.PreferredSize;
  				else
  					child_preferred_size = child.ExplicitBounds.Size;
  				int child_right = child.Bounds.X + child_preferred_size.Width;
  				int child_bottom = child.Bounds.Y + child_preferred_size.Height;
  
  				if (child.Dock == DockStyle.Fill) {
  					if (child_right > retsize.Width)
  						retsize.Width = child_right;
  				} 
  				else if (child.Dock != DockStyle.Top && child.Dock != DockStyle.Bottom && child_right > retsize.Width)
  					retsize.Width = child_right + child.Margin.Right;
  
  				if (child.Dock == DockStyle.Fill) {
  					if (child_bottom > retsize.Height)
  						retsize.Height = child_bottom;
  				}
  				else if (child.Dock != DockStyle.Left && child.Dock != DockStyle.Right && child_bottom > retsize.Height)
  					retsize.Height = child_bottom + child.Margin.Bottom;
  			}
  
  			if (retsize == Size.Empty) { // no child controls
  				retsize.Height += this.Padding.Top;
  				retsize.Width += this.Padding.Left;
  			}
  			retsize.Height += this.Padding.Bottom;
  			retsize.Width += this.Padding.Right;
  
  			return SizeFromClientSize (retsize);
  		}
  
  		[EditorBrowsable (EditorBrowsableState.Advanced)]
  		protected override Rectangle GetScaledBounds (Rectangle bounds, SizeF factor, BoundsSpecified specified)
  		{
  			if ((specified & BoundsSpecified.Width) == BoundsSpecified.Width) {
  				int border = Size.Width - ClientSize.Width;
  				bounds.Width = (int)Math.Round ((bounds.Width - border) * factor.Width) + border;
  			}
  			if ((specified & BoundsSpecified.Height) == BoundsSpecified.Height) {
  				int border = Size.Height - ClientSize.Height;
  				bounds.Height = (int)Math.Round ((bounds.Height - border) * factor.Height) + border;
  			}
  
  			return bounds;
  		}
  
  		protected override bool ProcessMnemonic (char charCode)
  		{
  			return base.ProcessMnemonic (charCode);
  		}
  		
  		[EditorBrowsable (EditorBrowsableState.Advanced)]
  		protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
  		{
  			base.ScaleControl (factor, specified);
  		}
  
  		internal void OnActivatedInternal ()
  		{
  			OnActivated (EventArgs.Empty);
  		}
  		
  		internal void OnDeactivateInternal ()
  		{
  			OnDeactivate (EventArgs.Empty);
  		}
  
  		internal override void UpdateWindowText ()
  		{
  			if (!IsHandleCreated) {
  				return;
  			}
  			
  			if (shown_raised) {
  				/* we need to call .SetWindowStyle here instead of just .Text
  				   because the presence/absence of Text (== "" or not) can cause
  				   other window style things to appear/disappear */
  				XplatUI.SetWindowStyle (window.Handle, CreateParams);
  			}
  			
  			XplatUI.Text (Handle, Text.Replace (Environment.NewLine, string.Empty));
  		}
  		
  		internal void SelectActiveControl ()
  		{
  			if (this.IsMdiContainer) {
  				mdi_container.SendFocusToActiveChild ();
  				return;
  			}
  				
  			if (this.ActiveControl == null) {
  				bool visible;
  
  				// This visible hack is to work around CanSelect always being false if one of the parents
  				// is not visible; and we by default create Form invisible...
  				visible = this.is_visible;
  				this.is_visible = true;
  
  				if (SelectNextControl (this, true, true, true, true) == false) {
  					Select (this);
  				}
  
  				this.is_visible = visible;
  			} else {
  				Select (ActiveControl);
  			}
  		}
  		
  		private new void UpdateSizeGripVisible ()
  		{
  			// Following link explains when to show size grip:
  			// http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=138687&SiteID=1
  			// if SizeGripStyle.Auto, only shown if form is shown using ShowDialog and is sizable
  			// if SizeGripStyle.Show, only shown if form is sizable
  			
  			bool show = false;
  			
  			switch (size_grip_style) {
  			case SizeGripStyle.Auto:
  				show = is_modal && (form_border_style == FormBorderStyle.Sizable || form_border_style == FormBorderStyle.SizableToolWindow);
  				break;
  			case SizeGripStyle.Hide:
  				show = false;
  				break;
  			case SizeGripStyle.Show:
  				show = (form_border_style == FormBorderStyle.Sizable || form_border_style == FormBorderStyle.SizableToolWindow);
  				break;
  			}
  			
  			if (!show) {
  				if (size_grip != null && size_grip.Visible)
  					size_grip.Visible = false;
  			} else {
  				if (size_grip == null) {
  					size_grip = new SizeGrip (this);
  					size_grip.Virtual = true;
  					size_grip.FillBackground = false;
  				}
  				size_grip.Visible = true;
  			}
  		}
  		
  		internal void ChangingParent (Control new_parent)
  		{
  			if (IsMdiChild) {
  				return;
  			}
  			
  			bool recreate_necessary = false;
  			
  			if (new_parent == null) {
  				window_manager = null;
  			} else if (new_parent is MdiClient) {
  				window_manager = new MdiWindowManager (this, (MdiClient) new_parent);
  			} else {
  				window_manager = new FormWindowManager (this);
  				recreate_necessary = true;
  			}
  			
  			if (recreate_necessary) {
  				if (IsHandleCreated) {
  					if (new_parent != null && new_parent.IsHandleCreated) {
  						RecreateHandle ();
  					} else {
  						DestroyHandle ();
  					}
  				}
  			} else {
  				if (IsHandleCreated) {
  					IntPtr new_handle = IntPtr.Zero;
  					if (new_parent != null && new_parent.IsHandleCreated) {
  						new_handle = new_parent.Handle;
  					}
  					XplatUI.SetParent (Handle, new_handle);
  				}
  			}
  		
  			if (window_manager != null) {
  				window_manager.UpdateWindowState (window_state, window_state, true);
  			}
  		}
  
  		internal override bool FocusInternal (bool skip_check)
  		{
  			if (IsMdiChild) {
  				// MS always creates handles when Focus () is called for mdi clients.
  				if (!IsHandleCreated)
  					CreateHandle ();
  			} 
  			return base.FocusInternal (skip_check);
  		}
  		#endregion	// Private & Internal Methods
  
  		#region Public Classes
  		[ComVisible (false)]
  		public new class ControlCollection : Control.ControlCollection {
  			Form	form_owner;
  
  			public ControlCollection(Form owner) : base(owner) {
  				this.form_owner = owner;
  			}
  
  			public override void Add(Control value) {
  				if (Contains (value))
  					return;
  				AddToList (value);
  				((Form)value).owner=form_owner;
  			}
  
  			public override void Remove(Control value) {
  				((Form)value).owner = null;
  				base.Remove (value);
  			}
  		}
  		#endregion	// Public Classes
  
  		#region Public Constructor & Destructor
  		public Form ()
  		{
  			SizeF current_scale = GetAutoScaleSize (Font);
  
  			autoscale = true;
  			autoscale_base_size = new Size ((int)Math.Round (current_scale.Width), (int)Math.Round(current_scale.Height));
  			allow_transparency = false;
  			closing = false;
  			is_modal = false;
  			dialog_result = DialogResult.None;
  			start_position = FormStartPosition.WindowsDefaultLocation;
  			form_border_style = FormBorderStyle.Sizable;
  			window_state = FormWindowState.Normal;
  			key_preview = false;
  			opacity = 1D;
  			menu = null;
  			icon = default_icon;
  			minimum_size = Size.Empty;
  			maximum_size = Size.Empty;
  			clientsize_set = Size.Empty;
  			control_box = true;
  			minimize_box = true;
  			maximize_box = true;
  			help_button = false;
  			show_in_taskbar = true;
  			is_visible = false;
  			is_toplevel = true;
  			size_grip_style = SizeGripStyle.Auto;
  			maximized_bounds = Rectangle.Empty;
  			default_maximized_bounds = Rectangle.Empty;
  			owned_forms = new Form.ControlCollection(this);
  			transparency_key = Color.Empty;
  			CreateDockPadding ();
  			InternalClientSize = new Size (this.Width - (SystemInformation.FrameBorderSize.Width * 2), this.Height - (SystemInformation.FrameBorderSize.Height * 2) - SystemInformation.CaptionHeight);
  			restore_bounds = Bounds;
  		}
  		#endregion	// Public Constructor & Destructor
  
  		#region Public Static Properties
  
  		public static Form ActiveForm {
  			get {
  				Control	active;
  
  				active = FromHandle(XplatUI.GetActive());
  
  				if (active != null) {
  					if ( !(active is Form)) {
  						Control	parent;
  
  						parent = active.Parent;
  						while (parent != null) {
  							if (parent is Form) {
  								return (Form)parent;
  							}
  							parent = parent.Parent;
  						}
  					} else {
  						return (Form)active;
  					}
  				}
  				return null;
  			}
  		}
  
  		#endregion	// Public Static Properties
  
  		#region Public Instance Properties
  		[DefaultValue(null)]
  		public IButtonControl AcceptButton {
  			get {
  				return accept_button;
  			}
  
  			set {
  				if (accept_button != null)
  					accept_button.NotifyDefault (false);
  
  				accept_button = value;
  				if (accept_button != null)
  					accept_button.NotifyDefault (true);
  
  				CheckAcceptButton ();
  			}
  		}
  
  		[Browsable(false)]
  		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  		public bool AllowTransparency {
  			get {
  				return allow_transparency;
  			}
  
  			set {
  				if (value == allow_transparency) {
  					return;
  				}
  
  				allow_transparency = value;
  
  				if (value) {
  					if (IsHandleCreated) {
  						if ((XplatUI.SupportsTransparency() & TransparencySupport.Set) != 0) {
  							XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
  						}
  					} else {
  						UpdateStyles(); // Remove the WS_EX_LAYERED style
  					}
  				}
  			}
  		}
  
  		[Browsable (false)]
  		[EditorBrowsable (EditorBrowsableState.Never)]
  		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  		[Obsolete ("This property has been deprecated in favor of AutoScaleMode.")]
  		[MWFCategory("Layout")]
  		public bool AutoScale {
  			get {
  				return autoscale;
  			}
  
  			set {
  				if (value)
  					AutoScaleMode = AutoScaleMode.None;
  
  				autoscale = value;
  			}
  		}
  
  		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  		[EditorBrowsable(EditorBrowsableState.Never)]
  		[Localizable(true)]
  		[Browsable(false)]
  		public virtual Size AutoScaleBaseSize {
  			get {
  				return autoscale_base_size;
  			}
  			[MonoTODO ("Setting this is probably unintentional and can cause Forms to be improperly sized.  See http://www.mono-project.com/FAQ:_Winforms#My_forms_are_sized_improperly for details.")]
  			set {
  				autoscale_base_size = value;
  				autoscale_base_size_set = true;
  			}
  		}
  
  		[Localizable(true)]
  		public override bool AutoScroll {
  			get {
  				return base.AutoScroll;
  			}
  			set {
  				base.AutoScroll = value;
  			}
  		}
  
  		internal bool ShouldSerializeAutoScroll ()
  		{
  			return this.AutoScroll != false;
  		}
  
  		[Browsable (true)]
  		[EditorBrowsable (EditorBrowsableState.Always)]
  		[DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
  		public override bool AutoSize {
  			get { return base.AutoSize; }
  			set { 
  				if (base.AutoSize != value) {
  					base.AutoSize = value;
  					PerformLayout (this, "AutoSize");
  				}
  			}
  		}
  
  		internal bool ShouldSerializeAutoSize ()
  		{
  			return this.AutoSize != false;
  		}
  		
  		[Browsable (true)]
  		[Localizable (true)]
  		[DefaultValue (AutoSizeMode.GrowOnly)]
  		public AutoSizeMode AutoSizeMode {
  			get { return base.GetAutoSizeMode (); }
  			set { 
  				if (base.GetAutoSizeMode () != value) {
  					if (!Enum.IsDefined (typeof (AutoSizeMode), value))
  						throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for AutoSizeMode", value));
  
  					base.SetAutoSizeMode (value);
  					PerformLayout (this, "AutoSizeMode");
  				}
  			}
  		}
  
  		[Browsable (true)]
  		[EditorBrowsable (EditorBrowsableState.Always)]
  		public override AutoValidate AutoValidate {
  			get { return base.AutoValidate; }
  			set { base.AutoValidate = value; }
  		}
  
  		public override Color BackColor {
  			get {
  				/* we don't let parents override our
  				 default background color for forms.
  				 this fixes the default color for mdi
  				 children. */
  				if (background_color.IsEmpty)
  					return DefaultBackColor;
  				else
  					return background_color;
  			}
  			set {
  				base.BackColor = value;
  			}
  		}
  
  		[DefaultValue(null)]
  		public IButtonControl CancelButton {
  			get {
  				return cancel_button;
  			}
  
  			set {
  				cancel_button = value;
  				if (cancel_button != null && cancel_button.DialogResult == DialogResult.None)
  					cancel_button.DialogResult = DialogResult.Cancel;
  			}
  		}
  
  		// new property so we can change the DesignerSerializationVisibility
  		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  		[Localizable(true)]
  		public new Size ClientSize {
  			get { return base.ClientSize; }
  			set {
  				is_clientsize_set = true;
  				base.ClientSize = value;
  			}
  		}
  
  		[DefaultValue(true)]
  		[MWFCategory("Window Style")]
  		public bool ControlBox {
  			get {
  				return control_box;
  			}
  
  			set {
  				if (control_box != value) {
  					control_box = value;
  					UpdateStyles();
  				}
  			}
  		}
  
  		[Browsable(false)]
  		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  		public Rectangle DesktopBounds {
  			get {
  				return new Rectangle(Location, Size);
  			}
  
  			set {
  				Bounds = value;
  			}
  		}
  
  		[Browsable(false)]
  		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  		public Point DesktopLocation {
  			get {
  				return Location;
  			}
  
  			set {
  				Location = value;
  			}
  		}
  
  		[Browsable(false)]
  		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  		public DialogResult DialogResult {
  			get {
  				return dialog_result;
  			}
  
  			set {
  				if (value < DialogResult.None || value > DialogResult.No)
  					throw new InvalidEnumArgumentException ("value", (int) value, 
  							typeof (DialogResult));
  
  				dialog_result = value;
  				if (dialog_result != DialogResult.None && is_modal)
  					RaiseCloseEvents (false, false); // .Net doesn't send WM_CLOSE here.
  			}
  		}
  
  		[DefaultValue(FormBorderStyle.Sizable)]
  		[DispId(-504)]
  		[MWFCategory("Appearance")]
  		public FormBorderStyle FormBorderStyle {
  			get {
  				return form_border_style;
  			}
  			set {
  				form_border_style = value;
  
  				if (window_manager == null) {
  					if (IsHandleCreated) {
  						XplatUI.SetBorderStyle(window.Handle, form_border_style);
  					}
  				} else {
  					window_manager.UpdateBorderStyle (value);
  				}
  
  				Size current_client_size = ClientSize;
  				UpdateStyles();
  				
  				if (this.IsHandleCreated) {
  					this.Size = InternalSizeFromClientSize (current_client_size);
  					XplatUI.InvalidateNC (this.Handle);
  				} else if (is_clientsize_set) {
  					this.Size = InternalSizeFromClientSize (current_client_size);
  				}
  			}
  		}
  
  		[DefaultValue(false)]
  		[MWFCategory("Window Style")]
  		public bool HelpButton {
  			get {
  				return help_button;
  			}
  
  			set {
  				if (help_button != value) {
  					help_button = value;
  					UpdateStyles();
  				}
  			}
  		}
  
  		[Localizable(true)]
  		[AmbientValue(null)]
  		[MWFCategory("Window Style")]
  		public Icon Icon {
  			get {
  				return icon;
  			}
  
  			set {
  				if (value == null)
  					value = default_icon;
  				if (icon == value)
  					return;
  				icon = value;
  				if (IsHandleCreated)
  					XplatUI.SetIcon (Handle, icon);
  			}
  		}
  
  		internal bool ShouldSerializeIcon ()
  		{
  			return this.Icon != default_icon;
  		}
  
  		[Browsable(false)]
  		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  		public bool IsMdiChild {
  			get {
  				return mdi_parent != null;
  			}
  		}
  
  		[DefaultValue(false)]
  		[MWFCategory("Window Style")]
  		public bool IsMdiContainer {
  			get {
  				return mdi_container != null;
  			}
  
  			set {
  				if (value && mdi_container == null) {
  					mdi_container = new MdiClient ();
  					Controls.Add(mdi_container);
  					ControlAdded += new ControlEventHandler (ControlAddedHandler);
  					mdi_container.SendToBack ();
  					mdi_container.SetParentText (true);
  				} else if (!value && mdi_container != null) {
  					Controls.Remove(mdi_container);
  					mdi_container = null;
  				}
  			}
  		}
  
  		[Browsable(false)]
  		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  		public Form ActiveMdiChild {
  			get {
  				if (!IsMdiContainer)
  					return null;
  				return (Form) mdi_container.ActiveMdiChild;
  			}
  		}
  
  		[Browsable(false)]
  		[EditorBrowsable(EditorBrowsableState.Advanced)]
  		public bool IsRestrictedWindow {
  			get {
  				return false;
  			}
  		}
  
  		[DefaultValue(false)]
  		public bool KeyPreview {
  			get {
  				return key_preview;
  			}
  
  			set {
  				key_preview = value;
  			}
  		}
  
  		[DefaultValue (null)]
  		[TypeConverter (typeof (ReferenceConverter))]
  		public MenuStrip MainMenuStrip {
  			get { return this.main_menu_strip; }
  			set { 
  				if (this.main_menu_strip != value) {
  					this.main_menu_strip = value;
  					this.main_menu_strip.RefreshMdiItems ();
  				}
  			}
  		}
  		
  		[EditorBrowsable (EditorBrowsableState.Never)]
  		[Browsable (false)]
  		public new Padding Margin {
  			get { return base.Margin; }
  			set { base.Margin = value; }
  		}
  
  		[DefaultValue(true)]
  		[MWFCategory("Window Style")]
  		public bool MaximizeBox {
  			get {
  				return maximize_box;
  			}
  			set {
  				if (maximize_box != value) {
  					maximize_box = value;
  					UpdateStyles();
  				}
  			}
  		}
  
  		[DefaultValue(typeof (Size),"0, 0")]
  		[Localizable(true)]
  		[RefreshProperties(RefreshProperties.Repaint)]
  		[MWFCategory("Layout")]
  		public override Size MaximumSize {
  			get {
  				return maximum_size;
  			}
  
  			set {
  				if (maximum_size != value) {
  					maximum_size = value;
  
  					// If this is smaller than the min, adjust the min
  					if (!minimum_size.IsEmpty) {
  						if (maximum_size.Width <= minimum_size.Width)
  							minimum_size.Width = maximum_size.Width;
  						if (maximum_size.Height <= minimum_size.Height)
  							minimum_size.Height = maximum_size.Height;
  					}
  						
  					OnMaximumSizeChanged(EventArgs.Empty);
  					if (IsHandleCreated) {
  						XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
  					}
  				}
  			}
  		}
  
  		[Browsable(false)]
  		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  		public Form[] MdiChildren {
  			get {
  				if (mdi_container != null)
  					return mdi_container.MdiChildren;
  				else
  					return new Form[0];
  			}
  		}
  
  		[Browsable(false)]
  		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  		public Form MdiParent {
  			get {
  				return mdi_parent;
  			}
  
  			set {
  				if (value == mdi_parent)
  					return;
  
  				if (value != null && !value.IsMdiContainer)
  					throw new ArgumentException ("Form that was specified to be "
  						+ "the MdiParent for this form is not an MdiContainer.");
  
  				if (mdi_parent != null) {
  					mdi_parent.MdiContainer.Controls.Remove (this);
  				}
  
  				if (value != null) {
  					mdi_parent = value;
  					if (window_manager == null) {
  						window_manager = new MdiWindowManager (this, mdi_parent.MdiContainer);
  					}
  					
  					mdi_parent.MdiContainer.Controls.Add (this);
  					mdi_parent.MdiContainer.Controls.SetChildIndex (this, 0);
  					
  					if (IsHandleCreated)
  						RecreateHandle ();
  				} else if (mdi_parent != null) {
  					mdi_parent = null;
  
  					// Create a new window manager
  					window_manager = null;
  					FormBorderStyle = form_border_style;
  
  					if (IsHandleCreated)
  						RecreateHandle ();
  				}
  				is_toplevel = mdi_parent == null;
  			}
  		}
  
  		internal MdiClient MdiContainer {
  			get { return mdi_container; }
  		}
  
  		internal InternalWindowManager WindowManager {
  			get { return window_manager; }
  		}
  
  		[Browsable (false)]
  		[TypeConverter (typeof (ReferenceConverter))]
  		[DefaultValue(null)]
  		[MWFCategory("Window Style")]
  		public MainMenu Menu {
  			get {
  				return menu;
  			}
  
  			set {
  				if (menu != value) {
  					menu = value;
  
  					if (menu != null && !IsMdiChild) {
  						menu.SetForm (this);
  
  						if (IsHandleCreated) {
  							XplatUI.SetMenu (window.Handle, menu);
  						}
  
  						if (clientsize_set != Size.Empty) {
  							SetClientSizeCore(clientsize_set.Width, clientsize_set.Height);
  						} else {
  							UpdateBounds (bounds.X, bounds.Y, bounds.Width, bounds.Height, ClientSize.Width, ClientSize.Height - 
  								ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu, ClientSize.Width));
  						}
  					} else
  						UpdateBounds ();
  
  					// UIA Framework Event: Menu Changed
  					OnUIAMenuChanged (EventArgs.Empty);
  				}
  			}
  		}
  
  		[Browsable(false)]
  		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  		[EditorBrowsable(EditorBrowsableState.Advanced)]
  		public MainMenu MergedMenu {
  			get {
  				if (!IsMdiChild || window_manager == null)
  					return null;
  				return ((MdiWindowManager) window_manager).MergedMenu;
  			}
  		}
  
  		// This is the menu in display and being used because of merging this can
  		// be different then the menu that is actually assosciated with the form
  		internal MainMenu ActiveMenu {
  			get {
  				if (IsMdiChild)
  					return null;
  
  				if (IsMdiContainer && mdi_container.Controls.Count > 0 &&
  						((Form) mdi_container.Controls [0]).WindowState == FormWindowState.Maximized) {
  					MdiWindowManager wm = (MdiWindowManager) ((Form) mdi_container.Controls [0]).WindowManager;
  					return wm.MaximizedMenu;
  				}
  
  				Form amc = ActiveMdiChild;
  				if (amc == null || amc.Menu == null)
  					return menu;
  				return amc.MergedMenu;
  			}
  		}
  
  		internal MdiWindowManager ActiveMaximizedMdiChild {
  			get {
  				Form child = ActiveMdiChild;
  				if (child == null)
  					return null;
  				if (child.WindowManager == null || child.window_state != FormWindowState.Maximized)
  					return null;
  				return (MdiWindowManager) child.WindowManager;
  			}
  		}
  
  		[DefaultValue(true)]
  		[MWFCategory("Window Style")]
  		public bool MinimizeBox {
  			get {
  				return minimize_box;
  			}
  			set {
  				if (minimize_box != value) {
  					minimize_box = value;
  					UpdateStyles();
  				}
  			}
  		}
  
  		[Localizable(true)]
  		[RefreshProperties(RefreshProperties.Repaint)]
  		[MWFCategory("Layout")]
  		public override Size MinimumSize {
  			get {
  				return minimum_size;
  			}
  
  			set {
  				if (minimum_size != value) {
  					minimum_size = value;
  
  					// If this is bigger than the max, adjust the max
  					if (!maximum_size.IsEmpty) {
  						if (minimum_size.Width >= maximum_size.Width)
  							maximum_size.Width = minimum_size.Width;
  						if (minimum_size.Height >= maximum_size.Height)
  							maximum_size.Height = minimum_size.Height;
  					}
  					
  					if ((Size.Width < value.Width) || (Size.Height < value.Height)) {
  						Size = new Size(Math.Max(Size.Width, value.Width), Math.Max(Size.Height, value.Height));
  					}
    
  
  					OnMinimumSizeChanged(EventArgs.Empty);
  					if (IsHandleCreated) {
  						XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
  					}
  				}
  			}
  		}
  
  		[Browsable(false)]
  		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  		public bool Modal  {
  			get {
  				return is_modal;
  			}
  		}
  
  		[DefaultValue(1D)]
  		[TypeConverter(typeof(OpacityConverter))]
  		[MWFCategory("Window Style")]
  		public double Opacity {
  			get {
  				if (IsHandleCreated) {
  					if ((XplatUI.SupportsTransparency () & TransparencySupport.Get) != 0)
  						return XplatUI.GetWindowTransparency (Handle);
  				}
  
  				return opacity;
  			}
  
  			set {
  				opacity = value;
  
  				if (opacity < 0)
  					opacity = 0;
  
  
gt: 				if (opacity > 1)