[patch] Main UI: saner moving when frames are present

David Vrabel <[email protected]> Fri, 27 Feb 2004 01:09:18 +0000
Newsgroups gmane.comp.audio.zinf.devel
Message-ID <[email protected]>
Hi,

If you dragged the main UI around when it had window manager frames on 
it it would behave oddly as the window position is different from the 
position of the canvas rectangle.

The attached patches fixes this and makes some of the Window API a bit 
saner.

Window::HandleMouseMove(..) and friends take a position relative to the 
window instead of relative to the root window.  Only moving a window 
needs the position relative to the root window so that's handled specially.

Window::GetWindowPosition(..) returns the position/size of the window 
including any window manager frames present.  This is so when moving a 
framed window the frames "stick" to the root window edges rather than 
the edge of the canvas being "sticky".

David Vrabel
zinf-saner-move.patch (text/x-patch, 10.9 KB)
Index: ui/zinf/include/Window.h
===================================================================
RCS file: /cvsroot/zinf/zinf/ui/zinf/include/Window.h,v
retrieving revision 1.5
diff -u -p -r1.5 Window.h
--- ui/zinf/include/Window.h	16 Sep 2003 17:35:32 -0000	1.5
+++ ui/zinf/include/Window.h	27 Feb 2004 00:43:01 -0000
@@ -160,10 +160,10 @@ class Window
       Theme                    *m_pTheme;
       Control                  *m_pMouseInControl, *m_pMouseDownControl;
       Control                  *m_pCaptureControl;
-      Pos                       m_oMovePos;
+      Rect                      m_oMoveStartWindowPos;
+      Pos                       m_oMoveStartPos;
       bool                      m_bStayOnTop, m_bLiveInToolbar;
       bool                      m_bIsVulcanMindMeldHost, m_bNoOutsideMessages;
-      Rect                      m_oMoveStart;
       int32_t                   m_iDesktopWidth, m_iDesktopHeight;
       bool                      m_bMindMeldInProgress, m_bTimerEnabled;
       Mutex                    *m_pUsageMutex;
Index: ui/zinf/src/Window.cpp
===================================================================
RCS file: /cvsroot/zinf/zinf/ui/zinf/src/Window.cpp,v
retrieving revision 1.5
diff -u -p -r1.5 Window.cpp
--- ui/zinf/src/Window.cpp	16 Sep 2003 17:36:22 -0000	1.5
+++ ui/zinf/src/Window.cpp	27 Feb 2004 00:43:01 -0000
@@ -224,12 +224,6 @@ void Window::PanelStateChanged(void)
     for(i = m_oPanels.begin(); i != m_oPanels.end(); i++)
         (*i)->ShowAllControls();
 
-    GetWindowPosition(oWindowRect);
-    m_pCanvas->GetBackgroundRect(oRect);
-    oWindowRect.x2 = oWindowRect.x1 + oRect.Width();
-    oWindowRect.y2 = oWindowRect.y1 + oRect.Height();
-    SetWindowPosition(oWindowRect);
-
     m_pCanvas->Invalidate(oRect);
 
     DecUsageRef();
@@ -430,61 +424,66 @@ void Window::HandleMouseWheelChange(int 
     m_pTheme->HandleMouseWheelChange(iSteps);
 }
 
-void Window::HandleMouseMove(Pos &oScreenPos)
+void Window::HandleMouseMove(Pos &oPos)
 {
     Control *pControl;
-    Pos      oPos;
     Rect     oRect;
 
     IncUsageRef();
 
     if (m_bWindowMove)
     {
-       Rect oActualPos;
-
-       m_oMoveStart.x1 += (oScreenPos.x - m_oMovePos.x);
-       m_oMoveStart.x2 += (oScreenPos.x - m_oMovePos.x);
-       m_oMoveStart.y1 += (oScreenPos.y - m_oMovePos.y);
-       m_oMoveStart.y2 += (oScreenPos.y - m_oMovePos.y);
+       Pos delta;
+       Rect oWindowPos;
+       
+       GetWindowPosition(oWindowPos);
+       delta.x = (oWindowPos.x1 + oPos.x) - m_oMoveStartPos.x;
+       delta.y = (oWindowPos.y1 + oPos.y) - m_oMoveStartPos.y;
+
+       oWindowPos.x1 = m_oMoveStartWindowPos.x1 + delta.x;
+       oWindowPos.y1 = m_oMoveStartWindowPos.y1 + delta.y;
+       oWindowPos.x2 = m_oMoveStartWindowPos.x2 + delta.x;
+       oWindowPos.y2 = m_oMoveStartWindowPos.y2 + delta.y;
 
-       oActualPos = m_oMoveStart;
+       int width = oWindowPos.x2 - oWindowPos.x1;
+       int height = oWindowPos.y2 - oWindowPos.y1;
 
        if (m_iDesktopWidth > 0 && m_iDesktopHeight > 0)
        {
-           if ((oActualPos.x1 >= 0 && oActualPos.x1 < iDesktopSnapAmount) || 
-               (oActualPos.x1 < 0 && oActualPos.x1 > -iDesktopSnapAmount)) 
+           if ((oWindowPos.x1 >= 0 && oWindowPos.x1 < iDesktopSnapAmount) || 
+               (oWindowPos.x1 < 0 && oWindowPos.x1 > -iDesktopSnapAmount)) 
            {
-               oActualPos.x2 -= oActualPos.x1;
-               oActualPos.x1 = 0;           
+               oWindowPos.x1 = 0;
+               oWindowPos.x2 = width;
            }
-           if ((oActualPos.y1 >= 0 && oActualPos.y1 < iDesktopSnapAmount) ||
-               (oActualPos.y1 < 0 && oActualPos.y1 > -iDesktopSnapAmount)) 
+           if ((oWindowPos.y1 >= 0 && oWindowPos.y1 < iDesktopSnapAmount) ||
+               (oWindowPos.y1 < 0 && oWindowPos.y1 > -iDesktopSnapAmount)) 
            {
-               oActualPos.y2 -= oActualPos.y1;
-               oActualPos.y1 = 0;           
+               oWindowPos.y1 = 0;
+               oWindowPos.y2 = height;
            }
-           if ((oActualPos.x2 < m_iDesktopWidth && 
-                oActualPos.x2 >= m_iDesktopWidth - iDesktopSnapAmount) || 
-               (oActualPos.x2 > m_iDesktopWidth && 
-                oActualPos.x2 <= m_iDesktopWidth + iDesktopSnapAmount)) 
+           if ((oWindowPos.x2 < m_iDesktopWidth && 
+                oWindowPos.x2 >= m_iDesktopWidth - iDesktopSnapAmount) || 
+               (oWindowPos.x2 > m_iDesktopWidth && 
+                oWindowPos.x2 <= m_iDesktopWidth + iDesktopSnapAmount)) 
            {
-               oActualPos.x1 += m_iDesktopWidth - oActualPos.x2;
-               oActualPos.x2 = m_iDesktopWidth;           
+               oWindowPos.x1 = m_iDesktopWidth - width;
+               oWindowPos.x2 = m_iDesktopWidth;
            }
-           if ((oActualPos.y2 < m_iDesktopHeight && 
-                oActualPos.y2 >= m_iDesktopHeight - iDesktopSnapAmount) || 
-               (oActualPos.y2 > m_iDesktopHeight && 
-                oActualPos.y2 <= m_iDesktopHeight + iDesktopSnapAmount)) 
+           if ((oWindowPos.y2 < m_iDesktopHeight && 
+                oWindowPos.y2 >= m_iDesktopHeight - iDesktopSnapAmount) || 
+               (oWindowPos.y2 > m_iDesktopHeight && 
+                oWindowPos.y2 <= m_iDesktopHeight + iDesktopSnapAmount)) 
            {
-               oActualPos.y1 += m_iDesktopHeight - oActualPos.y2;
-               oActualPos.y2 = m_iDesktopHeight;           
+               oWindowPos.y1 = m_iDesktopHeight - height;
+               oWindowPos.y2 = m_iDesktopHeight;
            }
        }       
 
-       m_oMovePos = oScreenPos;
-       SetWindowPosition(oActualPos);
+       SetWindowPosition(oWindowPos);
+
        DecUsageRef();
-       
+
        return; 
     }
 
@@ -494,10 +493,6 @@ void Window::HandleMouseMove(Pos &oScree
        m_pMouseDownControl = NULL;
     }
 
-    GetWindowPosition(oRect);
-    oPos.x = oScreenPos.x - oRect.x1;
-    oPos.y = oScreenPos.y - oRect.y1;
-   
     if (m_pCaptureControl)
     {
        m_pCaptureControl->AcceptTransition(CT_MouseMove, &oPos);
@@ -564,18 +559,13 @@ void Window::HandleMouseMove(Pos &oScree
     return;
 }
 
-void Window::HandleMouseLButtonDown(Pos &oScreenPos)
+void Window::HandleMouseLButtonDown(Pos &oPos)
 {
     Control *pControl;
     Rect     oRect;
-    Pos      oPos;
 
     IncUsageRef();
 
-    GetWindowPosition(oRect);
-    oPos.x = oScreenPos.x - oRect.x1;
-    oPos.y = oScreenPos.y - oRect.y1;
-
     if (m_pCaptureControl)
     {
        m_pCaptureControl->AcceptTransition(CT_MouseLButtonDown, &oPos);
@@ -612,11 +602,9 @@ void Window::HandleMouseLButtonDown(Pos 
     CaptureMouse(true);
 #endif
        
-    GetWindowPosition(m_oMoveStart);
-    m_pCanvas->GetBackgroundRect(oRect);
-    m_oMoveStart.x2 = m_oMoveStart.x1 + oRect.Width();
-    m_oMoveStart.y2 = m_oMoveStart.y1 + oRect.Height();
-    m_oMovePos = oScreenPos;
+    GetWindowPosition(m_oMoveStartWindowPos);
+    m_oMoveStartPos.x = m_oMoveStartWindowPos.x1 + oPos.x;
+    m_oMoveStartPos.y = m_oMoveStartWindowPos.y1 + oPos.y;
 
     if (IsError(GetDesktopSize(m_iDesktopWidth, m_iDesktopHeight)))
        m_iDesktopWidth = m_iDesktopHeight = 0;
@@ -626,18 +614,13 @@ void Window::HandleMouseLButtonDown(Pos 
     return;
 }
 
-void Window::HandleMouseLButtonDoubleClick(Pos &oScreenPos)
+void Window::HandleMouseLButtonDoubleClick(Pos &oPos)
 {
     Control *pControl;
     Rect     oRect;
-    Pos      oPos;
 
     IncUsageRef();
 
-    GetWindowPosition(oRect);
-    oPos.x = oScreenPos.x - oRect.x1;
-    oPos.y = oScreenPos.y - oRect.y1;
-
     pControl = ControlFromPos(oPos);
     if (pControl)
         m_pMouseInControl->AcceptTransition(CT_MouseLButtonDoubleClick);
@@ -647,18 +630,13 @@ void Window::HandleMouseLButtonDoubleCli
     return;
 }
 
-void Window::HandleMouseLButtonUp(Pos &oScreenPos)
+void Window::HandleMouseLButtonUp(Pos &oPos)
 {
     Control *pControl;
-    Pos      oPos;
     Rect     oRect;
 
     IncUsageRef();
 
-    GetWindowPosition(oRect);
-    oPos.x = oScreenPos.x - oRect.x1;
-    oPos.y = oScreenPos.y - oRect.y1;
-
     if (m_bWindowMove)
     {
        m_bWindowMove = false;
@@ -692,12 +670,12 @@ void Window::HandleMouseLButtonUp(Pos &o
     return;
 }
 
-void Window::HandleMouseMButtonDown(Pos &oScreenPos)
+void Window::HandleMouseMButtonDown(Pos &oPos)
 {
     return;
 }
 
-void Window::HandleMouseMButtonUp(Pos &oScreenPos)
+void Window::HandleMouseMButtonUp(Pos &oPos)
 {
     m_pTheme->HandleMouseWheelClick();
     return;
Index: ui/zinf/unix/src/GTKWindow.cpp
===================================================================
RCS file: /cvsroot/zinf/zinf/ui/zinf/unix/src/GTKWindow.cpp,v
retrieving revision 1.10
diff -u -p -r1.10 GTKWindow.cpp
--- ui/zinf/unix/src/GTKWindow.cpp	14 Feb 2004 21:56:39 -0000	1.10
+++ ui/zinf/unix/src/GTKWindow.cpp	27 Feb 2004 00:43:01 -0000
@@ -48,8 +48,8 @@ void mouse_move(GtkWidget *w, GdkEvent *
 {
     Pos oPos;
 
-    oPos.x = (int)e->motion.x_root;
-    oPos.y = (int)e->motion.y_root;
+    oPos.x = (int)e->motion.x;
+    oPos.y = (int)e->motion.y;
     gdk_threads_leave();
     ui->m_pMindMeldMutex->Acquire();
     ui->HandleMouseMove(oPos);
@@ -72,8 +72,8 @@ void scroll(GtkWidget *w, GdkEvent *e, G
 void button_down(GtkWidget *w, GdkEvent *e, GTKWindow *ui)
 {
     Pos oPos;
-    oPos.x = (int)e->button.x_root;
-    oPos.y = (int)e->button.y_root;
+    oPos.x = (int)e->button.x;
+    oPos.y = (int)e->button.y;
     gdk_threads_leave();
     ui->m_pMindMeldMutex->Acquire();
     if (e->button.button == 1) 
@@ -86,8 +86,7 @@ void button_down(GtkWidget *w, GdkEvent 
     else
     if (e->button.button == 3)
     {
-        //ui->BringWindowToFront();
-        ui->ContextClick(oPos.x, oPos.y, e->button.time);
+        ui->ContextClick(int(e->button.x_root), int(e->button.y_root), e->button.time);
     }
     else
     if (e->button.button == 4) 
@@ -107,8 +106,8 @@ void button_down(GtkWidget *w, GdkEvent 
 void button_up(GtkWidget *w, GdkEvent *e, GTKWindow *ui)
 {
     Pos oPos;
-    oPos.x = (int)e->button.x_root;
-    oPos.y = (int)e->button.y_root;
+    oPos.x = (int)e->button.x;
+    oPos.y = (int)e->button.y;
     gdk_threads_leave();
     ui->m_pMindMeldMutex->Acquire();
     if (e->button.button == 1)
@@ -535,23 +547,17 @@ Error GTKWindow::GetWindowPosition(Rect 
 {
     if (!mainWindow->window)
         return kError_NoErr;
+
+    GdkRectangle r;
+
     gdk_threads_enter();
-    gdk_window_get_position(mainWindow->window, &oWindowRect.x1, 
-                            &oWindowRect.y1);
+    gdk_window_get_frame_extents(mainWindow->window, &r);
     gdk_threads_leave();
 
-    Rect oRect;
-
-    if (GetCanvas()) {
-        GetCanvas()->GetBackgroundRect(oRect);
-   
-        oWindowRect.x2 = oWindowRect.x1 + oRect.Width();
-        oWindowRect.y2 = oWindowRect.y1 + oRect.Height();
-    }
-    else {
-        oWindowRect.x2 = oWindowRect.x1;
-        oWindowRect.y2 = oWindowRect.y1;
-    }
+    oWindowRect.x1 = r.x;
+    oWindowRect.y1 = r.y;
+    oWindowRect.x2 = r.x + r.width;
+    oWindowRect.y2 = r.y + r.height;
 
     return kError_NoErr;
 }