2007-02-23 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Form.cs
index cb19377a7c9cd735d128f3dbb736f371e58dbf21..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;
@@ -89,6 +90,8 @@ namespace System.Windows.Forms {
 
 #if NET_2_0
                private MenuStrip               main_menu_strip;
+               private bool                    show_icon = true;
+               private bool                    shown_raised;  // The shown event is only raised once
 #endif
                #endregion      // Local Variables
 
@@ -126,7 +129,7 @@ namespace System.Windows.Forms {
                private void SelectActiveControl ()
                {
                        if (this.IsMdiContainer) {
-                               SendControlFocus (this.mdi_container);
+                               mdi_container.SendFocusToActiveChild ();
                                return;
                        }
                                
@@ -147,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;
 
@@ -438,6 +479,11 @@ namespace System.Windows.Forms {
                                }
 
                                UpdateStyles();
+                               
+                               if (this.Visible) 
+                                       this.Size = SizeFromClientSize (this.ClientSize);
+                               else
+                                       XplatUI.InvalidateNC (this.Handle);
                        }
                }
 
@@ -545,6 +591,13 @@ namespace System.Windows.Forms {
                                }
                        }
                }
+               
+               [EditorBrowsable (EditorBrowsableState.Never)]
+               [Browsable (false)]
+               public new Padding Margin {
+                       get { return base.Margin; }
+                       set { base.Margin = value; }
+               }
 #endif
 
                [DefaultValue(true)]
@@ -608,7 +661,8 @@ namespace System.Windows.Forms {
 
                        set {
                                if (value != null && !value.IsMdiContainer)
-                                       throw new ArgumentException ();
+                                       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);
@@ -856,6 +910,22 @@ namespace System.Windows.Forms {
                        }
                }
 
+#if NET_2_0
+               [DefaultValue (true)]
+               public bool ShowIcon {
+                       get { return this.show_icon; }
+                       set {
+                               if (this.show_icon != value ) {
+                                       this.show_icon = value;
+                                       UpdateStyles ();
+                                       
+                                       XplatUI.SetIcon (this.Handle, value == true ? this.Icon : null);
+                                       XplatUI.InvalidateNC (this.Handle);
+                               }
+                       }
+               }                       
+#endif
+       
                [DefaultValue(true)]
                [MWFCategory("Window Style")]
                public bool ShowInTaskbar {
@@ -881,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 {
@@ -891,6 +960,7 @@ namespace System.Windows.Forms {
 
                        set {
                                size_grip_style = value;
+                               UpdateSizeGripVisible ();
                        }
                }
 
@@ -941,6 +1011,17 @@ namespace System.Windows.Forms {
                        set { base.TabIndex = value; }
                }
 
+#if NET_2_0
+               [Browsable(false)]
+               [DefaultValue (true)]
+               [DispIdAttribute (-516)]
+               [EditorBrowsable(EditorBrowsableState.Never)]
+               public new bool TabStop {
+                       get { return base.TabStop; }
+                       set { base.TabStop = value; }
+               }
+#endif
+
                [Browsable(false)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                [EditorBrowsable(EditorBrowsableState.Advanced)]
@@ -1048,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) {
@@ -1142,6 +1228,12 @@ namespace System.Windows.Forms {
                                        cp.Style |= (int)WindowStyles.WS_SYSMENU;
                                }
 
+#if NET_2_0
+                               if (!this.show_icon) {
+                                       cp.ExStyle |= (int)WindowExStyles.WS_EX_DLGMODALFRAME;
+                               }
+#endif
+
                                if (HelpButton && !MaximizeBox && !MinimizeBox) {
                                        cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTEXTHELP;
                                }
@@ -1189,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
@@ -1233,7 +1331,7 @@ namespace System.Windows.Forms {
                                if (IsMdiChild) {
                                        MdiParent.ActivateMdiChild (this);
                                } else if (IsMdiContainer) {
-                                       SendControlFocus (mdi_container);
+                                       mdi_container.SendFocusToActiveChild ();
                                } else {
                                        active = ActiveForm;
                                        if ((active != null) && (this != active)) {
@@ -1278,6 +1376,21 @@ namespace System.Windows.Forms {
                        DesktopLocation = new Point(x, y);
                }
 
+#if NET_2_0
+               public void Show (IWin32Window owner)
+               {
+                       if (owner == null)
+                               this.Owner = null;
+                       else
+                               this.Owner = Control.FromHandle (owner.Handle).TopLevelControl as Form;
+
+                       if (owner == this)
+                               throw new InvalidOperationException ("The 'owner' cannot be the form being shown.");
+
+                       base.Show ();
+               }
+#endif
+
                public DialogResult ShowDialog() {
                        return ShowDialog(this.owner);
                }
@@ -1296,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
@@ -1480,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);
@@ -1494,8 +1617,11 @@ namespace System.Windows.Forms {
                                        XplatUI.SetTopmost(owned_forms[i].window.Handle, window.Handle, true);
                        }
                        
-                       if (window_manager != null && window_state != FormWindowState.Normal) {
-                               window_manager.SetWindowState (FormWindowState.Normal, window_state);
+                       if (window_manager != null) {
+                               if (window_state != FormWindowState.Normal) {
+                                       window_manager.SetWindowState (FormWindowState.Normal, window_state);
+                               }
+                               XplatUI.RequestNCRecalc (window.Handle);
                        }
 
                }
@@ -1668,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)]
@@ -1713,6 +1839,24 @@ namespace System.Windows.Forms {
                                return ActiveMenu.ProcessCmdKey(ref msg, keyData);
                        }
 
+                       if (IsMdiChild) {
+                               switch (keyData)
+                               {
+                               case Keys.Control | Keys.F4:
+                               case Keys.Control | Keys.Shift | Keys.F4:
+                                       Close ();
+                                       return true;
+                               case Keys.Control | Keys.Tab:
+                               case Keys.Control | Keys.F6:
+                                       MdiParent.MdiContainer.ActivateNextChild ();
+                                       return true;
+                               case Keys.Control | Keys.Shift | Keys.Tab:
+                               case Keys.Control | Keys.Shift | Keys.F6:
+                                       MdiParent.MdiContainer.ActivatePreviousChild ();
+                                       return true;
+                               }
+                       }
+
                        return false;
                }
 
@@ -1846,6 +1990,14 @@ namespace System.Windows.Forms {
                        has_been_visible = value || has_been_visible;
                        base.SetVisibleCore (value);
                        is_changing_visible_state = false;
+                       
+#if NET_2_0
+                       // Shown event is only called once, the first time the form is made visible
+                       if (value && !shown_raised) {
+                               this.OnShown (EventArgs.Empty);
+                               shown_raised = true;
+                       }
+#endif
                }
 
                protected override void UpdateDefaultButton() {
@@ -1854,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;
@@ -1888,6 +2043,9 @@ namespace System.Windows.Forms {
                                        if (!is_modal) {
                                                if (!FireClosingEvents (CloseReason.UserClosing)) {
                                                        OnClosed (EventArgs.Empty);
+#if NET_2_0
+                                                       OnFormClosed (new FormClosedEventArgs (CloseReason.UserClosing));
+#endif
                                                        closing = true;
                                                        Dispose ();
                                                }
@@ -1901,6 +2059,9 @@ namespace System.Windows.Forms {
                                                }
                                                else {
                                                        OnClosed (EventArgs.Empty);
+#if NET_2_0
+                                                       OnFormClosed (new FormClosedEventArgs (CloseReason.UserClosing));
+#endif
                                                        closing = true;
                                                        Hide ();
                                                }
@@ -1953,12 +2114,34 @@ namespace System.Windows.Forms {
                                                ActiveControl.Focus();
                                                return; // FIXME - do we need to run base.WndProc, even though we just changed focus?
                                        }
+                                       if (IsMdiContainer) {
+                                               mdi_container.SendFocusToActiveChild ();
+                                               return;
+                                       }
                                        base.WndProc(ref m);
                                        return;
                                }
 
                                // 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));
                                        }
@@ -1975,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 ()),
@@ -2046,7 +2230,7 @@ namespace System.Windows.Forms {
                                                ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
 
                                                // Adjust for menu
-                                               ncp.rgrc1.top += ThemeEngine.Current.CalcMenuBarSize (DeviceContext, ActiveMenu, ncp.rgrc1.right - ncp.rgrc1.left);
+                                               ncp.rgrc1.top += ThemeEngine.Current.CalcMenuBarSize (DeviceContext, ActiveMenu, ClientSize.Width);
                                                Marshal.StructureToPtr(ncp, m.LParam, true);
                                        }
                                        DefWndProc(ref m);
@@ -2077,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;
                                }
 
@@ -2327,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 ();
@@ -2345,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 {
@@ -2401,6 +2587,35 @@ namespace System.Windows.Forms {
                        remove { base.TabStopChanged -= value; }
                }
 
+               protected override void OnBackgroundImageChanged (EventArgs e)
+               {
+                       base.OnBackgroundImageChanged (e);
+               }
+
+               protected override void OnBackgroundImageLayoutChanged (EventArgs e)
+               {
+                       base.OnBackgroundImageLayoutChanged (e);
+               }
+
+               [EditorBrowsable (EditorBrowsableState.Advanced)]
+               protected override void OnEnabledChanged (EventArgs e)
+               {
+                       base.OnEnabledChanged (e);
+               }
+
+               [EditorBrowsable (EditorBrowsableState.Advanced)]
+               protected override void OnEnter (EventArgs e)
+               {
+                       base.OnEnter (e);
+               }
+
+               [EditorBrowsable (EditorBrowsableState.Advanced)]
+               protected virtual void OnFormClosed (FormClosedEventArgs e) {
+                       FormClosedEventHandler eh = (FormClosedEventHandler)(Events[FormClosedEvent]);
+                       if (eh != null)
+                               eh (this, e);
+               }
+               
                // Consider calling FireClosingEvents instead of calling this directly.
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                protected virtual void OnFormClosing (FormClosingEventArgs e)
@@ -2410,6 +2625,20 @@ namespace System.Windows.Forms {
                                eh (this, e);
                }
 
+               [MonoTODO ("Not hooked up to event")]
+               [EditorBrowsable (EditorBrowsableState.Advanced)]
+               protected virtual void OnHelpButtonClicked (CancelEventArgs e)
+               {
+                       CancelEventHandler eh = (CancelEventHandler)(Events[HelpButtonClickedEvent]);
+                       if (eh != null)
+                               eh (this, e);
+               }
+
+               protected override void OnLayout (LayoutEventArgs levent)
+               {
+                       base.OnLayout (levent);
+               }
+
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                protected virtual void OnResizeBegin (EventArgs e)
                {
@@ -2425,6 +2654,14 @@ namespace System.Windows.Forms {
                        if (eh != null)
                                eh (this, e);
                }
+
+               [EditorBrowsable (EditorBrowsableState.Advanced)]
+               protected virtual void OnShown (EventArgs e)
+               {
+                       EventHandler eh = (EventHandler) (Events [ShownEvent]);
+                       if (eh != null)
+                               eh (this, e);
+               }
 #endif
        }
 }