Fixed tabs/spaces indentation mistake.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / InternalWindowManager.cs
index 5019c584679509dc924b01258dc01eea29f64152..5acda6776cdb752c6bc119faeacca08f1530c0b2 100644 (file)
@@ -52,7 +52,6 @@ namespace System.Windows.Forms {
                internal State state;
                private FormPos sizing_edge;
                internal Rectangle virtual_position;
-               private Rectangle prev_virtual_position;
 
                public class TitleButton {
                        public Rectangle Rectangle;
@@ -74,7 +73,7 @@ namespace System.Windows.Forms {
                }
 
                [Flags]
-               private enum FormPos {
+               public enum FormPos {
                        None,
 
                        TitleBar = 1,
@@ -155,8 +154,7 @@ namespace System.Windows.Forms {
                                int x = Control.LowOrder ((int) m.LParam.ToInt32 ());
                                int y = Control.HighOrder ((int) m.LParam.ToInt32 ());
 
-                               form.PointToClient (ref x, ref y);
-                               y += TitleBarHeight;
+                               NCPointToClient (ref x, ref y);
 
                                FormPos pos = FormPosForCoords (x, y);
                                
@@ -199,14 +197,18 @@ namespace System.Windows.Forms {
                                }
                                return true;
 
-                       case Msg.WM_NCMOUSEMOVE:
-                               return HandleNCMouseMove (form, ref m);
-
+                               // Return true from these guys, otherwise win32 will mess up z-order
                        case Msg.WM_NCLBUTTONUP:
-                               return HandleNCLButtonUp (ref m);
+                               HandleNCLButtonUp (ref m);
+                               return true;
 
                        case Msg.WM_NCLBUTTONDOWN:
-                               return HandleNCLButtonDown (ref m);
+                               HandleNCLButtonDown (ref m);
+                               return true;
+
+                       case Msg.WM_NCLBUTTONDBLCLK:
+                               HandleNCLButtonDblClick (ref m);
+                               break;
 
                        case Msg.WM_MOUSE_LEAVE:
                                FormMouseLeave (ref m);
@@ -234,15 +236,23 @@ namespace System.Windows.Forms {
                                break;
 
                        case Msg.WM_NCPAINT:
-                               PaintEventArgs pe;
-                               pe = XplatUI.PaintEventStart(m.HWnd, false);
-                               ThemeEngine.Current.DrawManagedWindowDecorations (pe.Graphics, pe.ClipRectangle, this);
-//                             PaintWindowDecorations (pe);
-                               XplatUI.PaintEventEnd(m.HWnd, false);
-
-                               
+                               PaintEventArgs pe = XplatUI.PaintEventStart (form.Handle, false);
+
+                               Rectangle clip;
+                               // clip region is not correct on win32.
+                               // if (m.WParam.ToInt32 () > 1) {
+                               //      Region r = Region.FromHrgn (m.WParam);
+                               //      RectangleF rf = r.GetBounds (pe.Graphics);
+                               //      clip = new Rectangle ((int) rf.X, (int) rf.Y, (int) rf.Width, (int) rf.Height);
+                               //} else {      
+                               clip = new Rectangle (0, 0, form.Width, form.Height);
+                               //}
+
+                               ThemeEngine.Current.DrawManagedWindowDecorations (pe.Graphics, clip, this);
+                               XplatUI.PaintEventEnd (form.Handle, false);
                                return true;
                        }
+
                        return false;
                }
 
@@ -282,7 +292,19 @@ namespace System.Windows.Forms {
 
                public virtual void PointToClient (ref int x, ref int y)
                {
-                       // toolwindows stay in screencoords
+                       // toolwindows stay in screencoords we just have to make sure
+                       // they obey the working area
+                       Rectangle working = SystemInformation.WorkingArea;
+
+                       if (x > working.Right)
+                               x = working.Right;
+                       if (x < working.Left)
+                               x = working.Left;
+
+                       if (y < working.Top)
+                               y = working.Top;
+                       if (y > working.Bottom)
+                               y = working.Bottom;
                }
 
                public virtual void PointToScreen (ref int x, ref int y)
@@ -298,7 +320,7 @@ namespace System.Windows.Forms {
                protected virtual void Activate ()
                {
                        // Hack to get a paint
-                       NativeWindow.WndProc (form.Handle, Msg.WM_NCPAINT, IntPtr.Zero, IntPtr.Zero);
+                       //NativeWindow.WndProc (form.Handle, Msg.WM_NCPAINT, IntPtr.Zero, IntPtr.Zero);
                        form.Refresh ();
                }
 
@@ -307,22 +329,33 @@ namespace System.Windows.Forms {
                        return true;
                }
 
+
                private void FormSizeChangedHandler (object sender, EventArgs e)
                {
                        ThemeEngine.Current.ManagedWindowSetButtonLocations (this);
-
-                       PaintEventArgs pe = XplatUI.PaintEventStart (form.Handle, false);
-                       ThemeEngine.Current.DrawManagedWindowDecorations (pe.Graphics, pe.ClipRectangle, this);
-                       XplatUI.PaintEventEnd (form.Handle, false);
+                       Message m = new Message ();
+                       m.Msg = (int) Msg.WM_NCPAINT;
+                       m.HWnd = form.Handle;
+                       m.LParam = IntPtr.Zero;
+                       m.WParam = new IntPtr (1);
+                       XplatUI.SendMessage (ref m);
                }
 
-               private void CreateButtons ()
+               protected void CreateButtons ()
                {
                        switch (form.FormBorderStyle) {
                        case FormBorderStyle.None:
                                close_button = null;
                                minimize_button = null;
                                maximize_button = null;
+                               if (IsMaximized || IsMinimized)
+                                       goto case FormBorderStyle.Sizable;
+                               break;
+                       case FormBorderStyle.FixedToolWindow:
+                       case FormBorderStyle.SizableToolWindow:
+                               close_button = new TitleButton (CaptionButton.Close, new EventHandler (CloseClicked));
+                               if (IsMaximized || IsMinimized)
+                                       goto case FormBorderStyle.Sizable;
                                break;
                        case FormBorderStyle.FixedSingle:
                        case FormBorderStyle.Fixed3D:
@@ -332,10 +365,6 @@ namespace System.Windows.Forms {
                                minimize_button = new TitleButton (CaptionButton.Minimize, new EventHandler (MinimizeClicked));
                                maximize_button = new TitleButton (CaptionButton.Maximize, new EventHandler (MaximizeClicked));
                                break;
-                       case FormBorderStyle.FixedToolWindow:
-                       case FormBorderStyle.SizableToolWindow:
-                               close_button = new TitleButton (CaptionButton.Close, new EventHandler (CloseClicked));
-                               break;
                        }
 
                        title_buttons [0] = close_button;
@@ -355,28 +384,26 @@ namespace System.Windows.Forms {
                {
                        Activate ();
 
-                       int x = Control.LowOrder ((int) m.LParam.ToInt32 ());
-                       int y = Control.HighOrder ((int) m.LParam.ToInt32 ());
-
                        start = Cursor.Position;
                        virtual_position = form.Bounds;
 
-                       form.PointToClient (ref x, ref y);
+                       int x = Control.LowOrder ((int) m.LParam.ToInt32 ());
+                       int y = Control.HighOrder ((int) m.LParam.ToInt32 ());
+                       
                        // Need to adjust because we are in NC land
-                       y += TitleBarHeight;
+                       NCPointToClient (ref x, ref y);
                        FormPos pos = FormPosForCoords (x, y);
-
+                       
                        if (pos == FormPos.TitleBar) {
                                HandleTitleBarDown (x, y);
                                return true;
                        }
 
                        if (IsSizable) {
-                               SetCursorForPos (pos);
-                       
                                if ((pos & FormPos.AnyEdge) == 0)
                                        return false;
 
+                               virtual_position = form.Bounds;
                                state = State.Sizing;
                                sizing_edge = pos;
                                form.Capture = true;
@@ -386,6 +413,10 @@ namespace System.Windows.Forms {
                        return false;
                }
 
+               protected virtual void HandleNCLButtonDblClick (ref Message m)
+               {
+               }
+
                protected virtual void HandleTitleBarDown (int x, int y)
                {
                        foreach (TitleButton button in title_buttons) {
@@ -429,48 +460,10 @@ namespace System.Windows.Forms {
                        return false;
                }
 
-               private bool HandleNCMouseMove (Form form, ref Message m)
-               {
-                       int x = Control.LowOrder ((int) m.LParam.ToInt32 ());
-                       int y = Control.HighOrder ((int) m.LParam.ToInt32 ());
-                       
-                       if (IsSizable) {
-                               form.PointToClient (ref x, ref y);
-                               y += TitleBarHeight;
-                       }
-
-                       return false;
-               }
-
                private void FormMouseLeave (ref Message m)
                {
                        form.ResetCursor ();
                }
-
-               private void SetCursorForPos (FormPos pos)
-               {
-                       switch (pos) {
-                       case FormPos.TopLeft:
-                       case FormPos.BottomRight:
-                               form.Cursor = Cursors.SizeNWSE;
-                               break;
-                       case FormPos.TopRight:
-                       case FormPos.BottomLeft:
-                               form.Cursor = Cursors.SizeNESW;
-                               break;
-                       case FormPos.Top:
-                       case FormPos.Bottom:
-                               form.Cursor = Cursors.SizeNS;
-                               break;
-                       case FormPos.Left:
-                       case FormPos.Right:
-                               form.Cursor = Cursors.SizeWE;
-                               break;
-                       default:
-                               form.ResetCursor ();
-                               break;
-                       }
-               }
        
                protected virtual void HandleWindowMove (Message m)
                {
@@ -482,40 +475,37 @@ namespace System.Windows.Forms {
 
                private void HandleSizing (Message m)
                {
-                       Point move = MouseMove (m);
                        Rectangle pos = virtual_position;
                        int bw = ThemeEngine.Current.ManagedWindowBorderWidth (this);
                        int mw = MinTitleBarSize.Width + (bw * 2);
                        int mh = MinTitleBarSize.Height + (bw * 2);
+                       int x = Cursor.Position.X;
+                       int y = Cursor.Position.Y;
+
+                       PointToClient (ref x, ref y);
 
                        if ((sizing_edge & FormPos.Top) != 0) {
-                               int height = form.Height - move.Y;
-                               if (height <= mh) {
-                                       move.Y += height - mh;
-                                       height = mh;
-                               }
-                               pos.Y = form.Top + move.Y;
-                               pos.Height = height;
+                               if (pos.Bottom - y < mh)
+                                       y = pos.Bottom - mh;
+                               pos.Height = pos.Bottom - y;
+                               pos.Y = y;
                        } else if ((sizing_edge & FormPos.Bottom) != 0) {
-                               int height = form.Height + move.Y;
+                               int height = y - pos.Top;
                                if (height <= mh)
-                                       move.Y -= height - mh;
-                               pos.Height = form.Height + move.Y;
+                                       height = mh;
+                               pos.Height = height;
                        }
 
                        if ((sizing_edge & FormPos.Left) != 0) {
-                               int width = form.Width - move.X;
-                               if (width <= mw) {
-                                       move.X += width - mw;
-                                       width = mw;
-                               }
-                               pos.X = form.Left + move.X;
-                               pos.Width = width;
+                               if (pos.Right - x < mw)
+                                       x = pos.Right - mw;
+                               pos.Width = pos.Right - x;
+                               pos.X = x;
                        } else if ((sizing_edge & FormPos.Right) != 0) {
-                               int width = form.Width + move.X;
+                               int width = x - form.Left;
                                if (width <= mw)
-                                       move.X -= width - mw;
-                               pos.Width = form.Width + move.X;
+                                       width = mw;
+                               pos.Width = width;
                        }
 
                        UpdateVP (pos);
@@ -525,6 +515,10 @@ namespace System.Windows.Forms {
                        get { return GetWindowState () == FormWindowState.Maximized; }
                }
 
+               public bool IsMinimized {
+                       get { return GetWindowState () == FormWindowState.Minimized; }
+               }
+
                public bool IsSizable {
                        get {
                                switch (form.FormBorderStyle) {
@@ -558,19 +552,6 @@ namespace System.Windows.Forms {
                        }
                }
 
-               public Size ButtonSize {
-                       get {
-                               int height = TitleBarHeight;
-                               if (IsToolWindow)
-                                       return new Size (SystemInformation.ToolWindowCaptionButtonSize.Width - 2,
-                                                       height - 5);
-                               if (form.FormBorderStyle == FormBorderStyle.None)
-                                       return Size.Empty;
-                               return new Size (SystemInformation.CaptionButtonSize.Width - 2,
-                                               height - 5);
-                       }
-               }
-
                protected void UpdateVP (Rectangle r)
                {
                        UpdateVP (r.X, r.Y, r.Width, r.Height);
@@ -607,13 +588,19 @@ namespace System.Windows.Forms {
 
                private bool HandleNCLButtonUp (ref Message m)
                {
+                       if (form.Capture) {
+                               ClearVirtualPosition ();
+
+                               form.Capture = false;
+                               state = State.Idle;
+                               if (form.MdiContainer != null)
+                                       form.MdiContainer.SizeScrollBars();
+                       }
+                               
                        int x = Control.LowOrder ((int) m.LParam.ToInt32 ());
                        int y = Control.HighOrder ((int) m.LParam.ToInt32 ());
 
-                       form.PointToClient (ref x, ref y);
-
-                       // Need to adjust because we are in NC land
-                       y += TitleBarHeight;
+                       NCPointToClient (ref x, ref y);
 
                        foreach (TitleButton button in title_buttons) {
                                if (button != null && button.Rectangle.Contains (x, y)) {
@@ -636,9 +623,8 @@ namespace System.Windows.Forms {
                                        button.Caption, ButtonState.Normal);
                }
 
-               public virtual void DrawMaximizedButtons (PaintEventArgs pe, MainMenu menu)
+               public virtual void DrawMaximizedButtons (object sender, PaintEventArgs pe)
                {
-
                }
 
                protected virtual void CloseClicked (object sender, EventArgs e)
@@ -685,7 +671,13 @@ namespace System.Windows.Forms {
                {
                }
 
-               private FormPos FormPosForCoords (int x, int y)
+               protected virtual void NCPointToClient(ref int x, ref int y) {
+                       form.PointToClient(ref x, ref y);
+                       y += TitleBarHeight;
+                       y += ThemeEngine.Current.ManagedWindowBorderWidth (this);
+               }
+
+               protected FormPos FormPosForCoords (int x, int y)
                {
                        int bw = ThemeEngine.Current.ManagedWindowBorderWidth (this);
                        if (y < TitleBarHeight + bw) {