2007-02-23 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Form.cs
index 7d706e745d44269114a164293f23baafe6119025..724b3b2d344c7c8b31e77329621bba8690eeea9c 100644 (file)
@@ -77,6 +77,7 @@ namespace System.Windows.Forms {
                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;
@@ -149,9 +150,47 @@ namespace System.Windows.Forms {
                                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;
+                       }
+               }
+               
                #endregion      // Private & Internal Methods
 
                #region Public Classes
+#if NET_2_0
+               [ComVisible (false)]
+#endif         
                public new class ControlCollection : Control.ControlCollection {
                        Form    form_owner;
 
@@ -440,6 +479,11 @@ namespace System.Windows.Forms {
                                }
 
                                UpdateStyles();
+                               
+                               if (this.Visible) 
+                                       this.Size = SizeFromClientSize (this.ClientSize);
+                               else
+                                       XplatUI.InvalidateNC (this.Handle);
                        }
                }
 
@@ -548,6 +592,7 @@ namespace System.Windows.Forms {
                        }
                }
                
+               [EditorBrowsable (EditorBrowsableState.Never)]
                [Browsable (false)]
                public new Padding Margin {
                        get { return base.Margin; }
@@ -906,7 +951,6 @@ namespace System.Windows.Forms {
                        set { base.Size = value; }
                }
 
-               [MonoTODO("Trigger something when GripStyle is set")]
                [DefaultValue(SizeGripStyle.Auto)]
                [MWFCategory("Window Style")]
                public SizeGripStyle SizeGripStyle {
@@ -916,6 +960,7 @@ namespace System.Windows.Forms {
 
                        set {
                                size_grip_style = value;
+                               UpdateSizeGripVisible ();
                        }
                }
 
@@ -968,7 +1013,8 @@ namespace System.Windows.Forms {
 
 #if NET_2_0
                [Browsable(false)]
-               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+               [DefaultValue (true)]
+               [DispIdAttribute (-516)]
                [EditorBrowsable(EditorBrowsableState.Never)]
                public new bool TabStop {
                        get { return base.TabStop; }
@@ -1083,6 +1129,11 @@ namespace System.Windows.Forms {
 
                                cp.Style = (int)(WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS);
 
+                               if (Parent != null) {
+                                       cp.Parent = Parent.Handle;
+                                       cp.Style |= (int) WindowStyles.WS_CHILD;
+                               }
+
                                if (IsMdiChild) {
                                        cp.Style |= (int)(WindowStyles.WS_CHILD | WindowStyles.WS_CAPTION);
                                        if (Parent != null) {
@@ -1230,6 +1281,12 @@ namespace System.Windows.Forms {
                                }
                        }
                }
+#if NET_2_0
+               [MonoTODO ("Implemented for Win32, needs X11 implementation")]
+               protected virtual bool ShowWithoutActivation {
+                       get { return false; }
+               }
+#endif
                #endregion      // Protected Instance Properties
 
                #region Public Static Methods
@@ -1352,23 +1409,30 @@ namespace System.Windows.Forms {
                        }
 
                        if (owner == this) {
-                               throw new InvalidOperationException("The 'ownerWin32' cannot be the form being shown.");
+                               throw new ArgumentException ("Forms cannot own themselves or their owners.", "owner");
                        }
 
                        if (is_modal) {
-                               throw new InvalidOperationException("The form is already displayed as a modal dialog.");
+                               throw new InvalidOperationException ("The form is already displayed as a modal dialog.");
                        }
 
                        if (Visible) {
-                               throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
+                               throw new InvalidOperationException ("Forms that are already "
+                                       + " visible cannot be displayed as a modal dialog. Set the"
+                                       + " form's visible property to false before calling"
+                                       + " ShowDialog.");
                        }
 
                        if (!Enabled) {
-                               throw new InvalidOperationException("Cannot display a disabled form as modal dialog.");
+                               throw new InvalidOperationException ("Forms that are not enabled"
+                                       + " cannot be displayed as a modal dialog. Set the form's"
+                                       + " enabled property to true before calling ShowDialog.");
                        }
 
                        if (TopLevelControl != this) {
-                               throw new InvalidOperationException("Can only display TopLevel forms as modal dialog.");
+                               throw new InvalidOperationException ("Forms that are not top level"
+                                       + " forms cannot be displayed as a modal dialog. Remove the"
+                                       + " form from any parent form before calling ShowDialog.");
                        }
 
                        #if broken
@@ -1536,6 +1600,9 @@ namespace System.Windows.Forms {
                                }
                        }
 
+                       if (this.ShowWithoutActivation)
+                               Hwnd.ObjectFromHandle (this.Handle).no_activate = true;
+
                        XplatUI.SetWindowMinMax(window.Handle, maximized_bounds, minimum_size, maximum_size);
                        if ((FormBorderStyle != FormBorderStyle.FixedDialog) && (icon != null)) {
                                XplatUI.SetIcon(window.Handle, icon);
@@ -1727,16 +1794,16 @@ namespace System.Windows.Forms {
                [EditorBrowsable(EditorBrowsableState.Advanced)]
                protected override void OnPaint (PaintEventArgs pevent) {
                        base.OnPaint (pevent);
+
+                       if (size_grip != null) {
+                               size_grip.HandlePaint (this, pevent);
+                       }
                }
 
                [EditorBrowsable(EditorBrowsableState.Advanced)]
                protected override void OnResize(EventArgs e) {
                        base.OnResize(e);
 
-                       if (this.IsMdiChild && ParentForm != null) {
-                               ParentForm.PerformLayout();
-                               ParentForm.Size = ParentForm.Size;
-                       }
                }
 
                [EditorBrowsable(EditorBrowsableState.Advanced)]
@@ -1939,6 +2006,9 @@ namespace System.Windows.Forms {
 
                [EditorBrowsable(EditorBrowsableState.Advanced)]
                protected override void WndProc(ref Message m) {
+#if debug
+                       Console.WriteLine(DateTime.Now.ToLongTimeString () + " Form {0} ({2}) received message {1}", window.Handle == IntPtr.Zero ? this.Text : XplatUI.Window(window.Handle), m.ToString (), Text);
+#endif
 
                        if (window_manager != null && window_manager.HandleMessage (ref m)) {
                                return;
@@ -2053,7 +2123,25 @@ namespace System.Windows.Forms {
                                }
 
                                // Menu drawing
-                               case Msg.WM_NCLBUTTONDOWN: {
+                case Msg.WM_NCHITTEST: {
+                                       if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
+                                               int x = LowOrder ((int)m.LParam.ToInt32 ());
+                                               int y = HighOrder ((int)m.LParam.ToInt32 ());
+
+                                               XplatUI.ScreenToMenu (ActiveMenu.Wnd.window.Handle, ref x, ref y);
+
+                                               // If point is under menu return HTMENU, it prevents Win32 to return HTMOVE.
+                                               if ((x > 0) && (y > 0) && (x < ActiveMenu.Rect.Width) && (y < ActiveMenu.Rect.Height)) {
+                                                       m.Result = new IntPtr ((int)HitTest.HTMENU);
+                                                       return;
+                                               }
+                                       }
+
+                                       base.WndProc (ref m);
+                                       return;
+                               }
+
+                case Msg.WM_NCLBUTTONDOWN: {
                                        if (XplatUI.IsEnabled (Handle) && ActiveMenu != null) {
                                                ActiveMenu.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, Control.MousePosition.X, Control.MousePosition.Y, 0));
                                        }
@@ -2070,7 +2158,8 @@ namespace System.Windows.Forms {
                                        base.WndProc(ref m);
                                        return;
                                }
-                               case Msg.WM_NCLBUTTONUP: {
+
+                case Msg.WM_NCLBUTTONUP: {
                                        if (ActiveMaximizedMdiChild != null) {
                                                ActiveMaximizedMdiChild.HandleMenuMouseUp (ActiveMenu,
                                                                LowOrder ((int)m.LParam.ToInt32 ()),
@@ -2172,7 +2261,10 @@ namespace System.Windows.Forms {
                                                active_tracker.OnMouseDown(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
                                                return;
                                        }
-                                       base.WndProc(ref m);
+#if NET_2_0
+                                       ToolStripManager.FireAppClicked ();
+#endif
+                                       base.WndProc (ref m);
                                        return;
                                }
 
@@ -2422,7 +2514,6 @@ namespace System.Windows.Forms {
                        }
                }
 
-               static object AutoValidateChangedEvent = new object ();
                static object FormClosingEvent = new object ();
                static object FormClosedEvent = new object ();
                static object HelpButtonClickedEvent = new object ();
@@ -2440,9 +2531,9 @@ namespace System.Windows.Forms {
 
                [Browsable (true)]
                [EditorBrowsable (EditorBrowsableState.Always)]
-               public event EventHandler AutoValidateChanged {
-                       add { Events.AddHandler (AutoValidateChangedEvent, value); }
-                       remove { Events.RemoveHandler (AutoValidateChangedEvent, value); }
+               public new event EventHandler AutoValidateChanged {
+                       add { base.AutoValidateChanged += value; }
+                       remove { base.AutoValidateChanged -= value; }
                }
 
                public event FormClosingEventHandler FormClosing {
@@ -2496,14 +2587,11 @@ namespace System.Windows.Forms {
                        remove { base.TabStopChanged -= value; }
                }
 
-
-               [EditorBrowsable (EditorBrowsableState.Always)]
                protected override void OnBackgroundImageChanged (EventArgs e)
                {
                        base.OnBackgroundImageChanged (e);
                }
 
-               [EditorBrowsable (EditorBrowsableState.Always)]
                protected override void OnBackgroundImageLayoutChanged (EventArgs e)
                {
                        base.OnBackgroundImageLayoutChanged (e);
@@ -2546,7 +2634,6 @@ namespace System.Windows.Forms {
                                eh (this, e);
                }
 
-               [EditorBrowsable (EditorBrowsableState.Advanced)]
                protected override void OnLayout (LayoutEventArgs levent)
                {
                        base.OnLayout (levent);