GHOST Cocoa: move y origin top/bottom conversions out of windowmanager module
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Thu, 23 Jun 2011 19:55:47 +0000 (19:55 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Thu, 23 Jun 2011 19:55:47 +0000 (19:55 +0000)
and into GHOST. Also fixes a problem where e.g. the user preferences window
would not open under the mouse cursor correctly.

intern/ghost/intern/GHOST_SystemCocoa.h
intern/ghost/intern/GHOST_SystemCocoa.mm
intern/ghost/intern/GHOST_Window.cpp
intern/ghost/intern/GHOST_Window.h
intern/ghost/intern/GHOST_WindowCarbon.cpp
intern/ghost/intern/GHOST_WindowCocoa.h
intern/ghost/intern/GHOST_WindowCocoa.mm
intern/ghost/intern/GHOST_WindowWin32.cpp
intern/ghost/intern/GHOST_WindowX11.cpp
source/blender/windowmanager/intern/wm_event_system.c
source/blender/windowmanager/intern/wm_window.c

index e7a8178a38259a2910a44e9b0901c3a3df280232..4779f1f056cf1db90b8f9bd880bdca558ef4f723 100644 (file)
@@ -272,6 +272,17 @@ protected:
         */
        GHOST_TSuccess setMouseCursorPosition(GHOST_TInt32 x, GHOST_TInt32 y);
 
+       /**
+        * Push cursor event, with coordinate conversion to follow GHOST convention.
+        */
+       void pushEventCursor(GHOST_TUns64 msec, GHOST_TEventType type, GHOST_IWindow* window, GHOST_TInt32 x, GHOST_TInt32 y);
+
+       /**
+        * Push trackpad event, with coordinate conversion to follow GHOST convention.
+        */
+       void pushEventTrackpad(GHOST_TUns64 msec, GHOST_IWindow* window, GHOST_TTrackpadEventSubTypes subtype,
+               GHOST_TInt32 x, GHOST_TInt32 y, GHOST_TInt32 deltaX, GHOST_TInt32 deltaY);
+
        /** Start time at initialization. */
        GHOST_TUns64 m_start_time;
        
index bf401138cf1c35585d0ac295bda2e9c7b9d9748b..63c8f10971bc4c726d999180b2fa59ce2f5d598e 100644 (file)
@@ -741,11 +741,13 @@ GHOST_IWindow* GHOST_SystemCocoa::createWindow(
        NSRect contentRect = [NSWindow contentRectForFrameRect:frame
                                                                                                 styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask)];
        
+       GHOST_TInt32 bottom = (contentRect.size.height - 1) - height - top;
+
        //Ensures window top left is inside this available rect
        left = left > contentRect.origin.x ? left : contentRect.origin.x;
-       top = top > contentRect.origin.y ? top : contentRect.origin.y;
-       
-       window = new GHOST_WindowCocoa (this, title, left, top, width, height, state, type, stereoVisual, numOfAASamples);
+       bottom = bottom > contentRect.origin.y ? bottom : contentRect.origin.y;
+
+       window = new GHOST_WindowCocoa (this, title, left, bottom, width, height, state, type, stereoVisual, numOfAASamples);
 
     if (window) {
         if (window->getValid()) {
@@ -804,6 +806,31 @@ GHOST_TSuccess GHOST_SystemCocoa::getCursorPosition(GHOST_TInt32& x, GHOST_TInt3
     return GHOST_kSuccess;
 }
 
+void GHOST_SystemCocoa::pushEventCursor(GHOST_TUns64 msec, GHOST_TEventType type, GHOST_IWindow* window, GHOST_TInt32 x, GHOST_TInt32 y)
+{
+       GHOST_Rect cBnds;
+       window->getClientBounds(cBnds);
+       y = (cBnds.getHeight() - 1) - y;
+
+       GHOST_TInt32 screen_x, screen_y;
+       window->clientToScreen(x, y, screen_x, screen_y);
+
+       pushEvent(new GHOST_EventCursor(msec, type, window, screen_x, screen_y));
+}
+
+void GHOST_SystemCocoa::pushEventTrackpad(GHOST_TUns64 msec, GHOST_IWindow* window, GHOST_TTrackpadEventSubTypes subtype, GHOST_TInt32 x, GHOST_TInt32 y, GHOST_TInt32 deltaX, GHOST_TInt32 deltaY)
+{
+       GHOST_Rect cBnds;
+       window->getClientBounds(cBnds);
+       y = (cBnds.getHeight() - 1) - y;
+       deltaY = -deltaY;
+
+       GHOST_TInt32 screen_x, screen_y;
+       window->clientToScreen(x, y, screen_x, screen_y);
+
+       pushEvent(new GHOST_EventTrackpad(msec, window, subtype, screen_x, screen_y, deltaX, deltaY));
+}
+
 /**
  * @note : expect Cocoa screen coordinates
  */
@@ -821,7 +848,7 @@ GHOST_TSuccess GHOST_SystemCocoa::setCursorPosition(GHOST_TInt32 x, GHOST_TInt32
        
        //Force mouse move event (not pushed by Cocoa)
        window->screenToClient(x, y, wx, wy);
-       pushEvent(new GHOST_EventCursor(getMilliSeconds(), GHOST_kEventCursorMove, window, wx,wy));
+       pushEventCursor(getMilliSeconds(), GHOST_kEventCursorMove, window, wx,wy);
        m_outsideLoopEventProcessed = true;
        
        return GHOST_kSuccess;
@@ -1508,7 +1535,7 @@ GHOST_TSuccess GHOST_SystemCocoa::handleMouseEvent(void *eventPtr)
                                                y_accum += -[event deltaY]; //Strange Apple implementation (inverted coordinates for the deltaY) ...
                                                window->setCursorGrabAccum(x_accum, y_accum);
                                                
-                                               pushEvent(new GHOST_EventCursor([event timestamp]*1000, GHOST_kEventCursorMove, window, x_warp+x_accum, y_warp+y_accum));
+                                               pushEventCursor([event timestamp]*1000, GHOST_kEventCursorMove, window, x_warp+x_accum, y_warp+y_accum);
                                        }
                                                break;
                                        case GHOST_kGrabWrap: //Wrap cursor at area/window boundaries
@@ -1552,14 +1579,14 @@ GHOST_TSuccess GHOST_SystemCocoa::handleMouseEvent(void *eventPtr)
                                                
                                                //Post event
                                                window->getCursorGrabInitPos(x_cur, y_cur);
-                                               pushEvent(new GHOST_EventCursor([event timestamp]*1000, GHOST_kEventCursorMove, window, x_cur + x_accum, y_cur + y_accum));
+                                               pushEventCursor([event timestamp]*1000, GHOST_kEventCursorMove, window, x_cur + x_accum, y_cur + y_accum);
                                        }
                                                break;
                                        default:
                                        {
                                                //Normal cursor operation: send mouse position in window
                                                NSPoint mousePos = [event locationInWindow];
-                                               pushEvent(new GHOST_EventCursor([event timestamp]*1000, GHOST_kEventCursorMove, window, mousePos.x, mousePos.y));
+                                               pushEventCursor([event timestamp]*1000, GHOST_kEventCursorMove, window, mousePos.x, mousePos.y);
                                                m_cursorDelta_x=0;
                                                m_cursorDelta_y=0; //Mouse motion occurred between two cursor warps, so we can reset the delta counter
                                        }
@@ -1597,7 +1624,7 @@ GHOST_TSuccess GHOST_SystemCocoa::handleMouseEvent(void *eventPtr)
                                        if (dy<0.0) dy-=0.5; else dy+=0.5;
                                        if (dy< -deltaMax) dy= -deltaMax; else if (dy>deltaMax) dy=deltaMax;
 
-                                       pushEvent(new GHOST_EventTrackpad([event timestamp]*1000, window, GHOST_kTrackpadEventScroll, mousePos.x, mousePos.y, dx, dy));
+                                       pushEventTrackpad([event timestamp]*1000, window, GHOST_kTrackpadEventScroll, mousePos.x, mousePos.y, dx, dy);
                                }
                        }
                        break;
@@ -1605,16 +1632,16 @@ GHOST_TSuccess GHOST_SystemCocoa::handleMouseEvent(void *eventPtr)
                case NSEventTypeMagnify:
                        {
                                NSPoint mousePos = [event locationInWindow];
-                               pushEvent(new GHOST_EventTrackpad([event timestamp]*1000, window, GHOST_kTrackpadEventMagnify, mousePos.x, mousePos.y,
-                                                                                                 [event magnification]*250.0 + 0.1, 0));
+                               pushEventTrackpad([event timestamp]*1000, window, GHOST_kTrackpadEventMagnify, mousePos.x, mousePos.y,
+                                                                                                 [event magnification]*250.0 + 0.1, 0);
                        }
                        break;
 
                case NSEventTypeRotate:
                        {
                                NSPoint mousePos = [event locationInWindow];
-                               pushEvent(new GHOST_EventTrackpad([event timestamp]*1000, window, GHOST_kTrackpadEventRotate, mousePos.x, mousePos.y,
-                                                                                                 -[event rotation] * 5.0, 0));
+                               pushEventTrackpad([event timestamp]*1000, window, GHOST_kTrackpadEventRotate, mousePos.x, mousePos.y,
+                                                                                                 -[event rotation] * 5.0, 0);
                        }
                case NSEventTypeBeginGesture:
                        m_isGestureInProgress = true;
index c518272d614cd1a30327ae8818e17d492517d69a..e34bfe7ff263bd8e3d7ce71778b024e6ca5390de 100644 (file)
@@ -41,8 +41,7 @@
 
 
 GHOST_Window::GHOST_Window(
-       const STR_String& /*title*/,
-       GHOST_TInt32 /*left*/, GHOST_TInt32 /*top*/, GHOST_TUns32 width, GHOST_TUns32 height,
+       GHOST_TUns32 width, GHOST_TUns32 height,
        GHOST_TWindowState state,
        GHOST_TDrawingContextType type,
        const bool stereoVisual,
index e0c676f53a579cbb0742f05cc7bd0a178b7a2187..66990abb555ea2b7e9f05907ff77c25ac2e53695 100644 (file)
@@ -76,9 +76,6 @@ public:
         * Constructor.
         * Creates a new window and opens it.
         * To check if the window was created properly, use the getValid() method.
-        * @param title                         The text shown in the title bar of the window.
-        * @param left                          The coordinate of the left edge of the window.
-        * @param top                           The coordinate of the top edge of the window.
         * @param width                         The width the window.
         * @param heigh                         The height the window.
         * @param state                         The state the window is initially opened with.
@@ -87,9 +84,6 @@ public:
         * @param numOfAASamples        Number of samples used for AA (zero if no AA)
         */
        GHOST_Window(
-               const STR_String& title, 
-               GHOST_TInt32 left,
-               GHOST_TInt32 top,
                GHOST_TUns32 width,
                GHOST_TUns32 height,
                GHOST_TWindowState state,
index 1ffd117d65880e5de213e57dcbbac7a3ed2bae65..c8bd67f9a5c66269829bd36ad4b34253d4f5aa2c 100644 (file)
@@ -102,7 +102,7 @@ GHOST_WindowCarbon::GHOST_WindowCarbon(
        const bool stereoVisual,
        const GHOST_TUns16 numOfAASamples
 ) :
-       GHOST_Window(title, left, top, width, height, state, GHOST_kDrawingContextTypeNone),
+       GHOST_Window(width, height, state, GHOST_kDrawingContextTypeNone),
        m_windowRef(0),
        m_grafPtr(0),
        m_aglCtx(0),
index 98e0f9cedd173a34f4ca6edea2add113ed3ebce4..b5485c6bcef908318b0f1be719cbc7d1dda5f1ad 100644 (file)
@@ -77,7 +77,7 @@ public:
                GHOST_SystemCocoa *systemCocoa,
                const STR_String& title,
                GHOST_TInt32 left,
-               GHOST_TInt32 top,
+               GHOST_TInt32 bottom,
                GHOST_TUns32 width,
                GHOST_TUns32 height,
                GHOST_TWindowState state,
index fb7d4a459c7497c3ef170d8f25da0c237783131b..224980c6a11612598d8b3e9b2ba8ad5ff1cf8a37 100644 (file)
@@ -308,14 +308,14 @@ GHOST_WindowCocoa::GHOST_WindowCocoa(
        GHOST_SystemCocoa *systemCocoa,
        const STR_String& title,
        GHOST_TInt32 left,
-       GHOST_TInt32 top,
+       GHOST_TInt32 bottom,
        GHOST_TUns32 width,
        GHOST_TUns32 height,
        GHOST_TWindowState state,
        GHOST_TDrawingContextType type,
        const bool stereoVisual, const GHOST_TUns16 numOfAASamples
 ) :
-       GHOST_Window(title, left, top, width, height, state, GHOST_kDrawingContextTypeNone, stereoVisual, numOfAASamples),
+       GHOST_Window(width, height, state, GHOST_kDrawingContextTypeNone, stereoVisual, numOfAASamples),
        m_customCursor(0)
 {
        NSOpenGLPixelFormatAttribute pixelFormatAttrsWindow[40];
@@ -327,13 +327,12 @@ GHOST_WindowCocoa::GHOST_WindowCocoa(
        
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        
-
        //Creates the window
        NSRect rect;
        NSSize  minSize;
        
        rect.origin.x = left;
-       rect.origin.y = top;
+       rect.origin.y = bottom;
        rect.size.width = width;
        rect.size.height = height;
        
@@ -703,12 +702,22 @@ void GHOST_WindowCocoa::screenToClient(GHOST_TInt32 inX, GHOST_TInt32 inY, GHOST
        
        outX = baseCoord.x;
        outY = baseCoord.y;
+
+       /* switch y to match ghost convention */
+       GHOST_Rect cBnds;
+       getClientBounds(cBnds);
+       outY = (cBnds.getHeight() - 1) - outY;
 }
 
 
 void GHOST_WindowCocoa::clientToScreen(GHOST_TInt32 inX, GHOST_TInt32 inY, GHOST_TInt32& outX, GHOST_TInt32& outY) const
 {
        GHOST_ASSERT(getValid(), "GHOST_WindowCocoa::clientToScreen(): window invalid")
+
+       /* switch y to match ghost convention */
+       GHOST_Rect cBnds;
+       getClientBounds(cBnds);
+       inY = (cBnds.getHeight() - 1) - inY;
        
        NSPoint screenCoord;
        NSPoint baseCoord;
index aae1509fda15ec3d5aeb9526a3ce810e7bef3002..1ba51376ff92a54837bb111bfe28dffb705990a3 100644 (file)
@@ -136,7 +136,7 @@ GHOST_WindowWin32::GHOST_WindowWin32(
        GHOST_TSuccess msEnabled,
        int msPixelFormat)
 :
-       GHOST_Window(title, left, top, width, height, state, GHOST_kDrawingContextTypeNone,
+       GHOST_Window(width, height, state, GHOST_kDrawingContextTypeNone,
        stereoVisual,numOfAASamples),
        m_system(system),
        m_hDC(0),
index fc72ea09f4072a32b36dd4fff0fb27bf95222e07..de7251516079b134faaf33bf0caf87708a38019d 100644 (file)
@@ -164,7 +164,7 @@ GHOST_WindowX11(
        const bool stereoVisual,
        const GHOST_TUns16 numOfAASamples
 ) :
-       GHOST_Window(title,left,top,width,height,state,type,stereoVisual,numOfAASamples),
+       GHOST_Window(width,height,state,type,stereoVisual,numOfAASamples),
        m_context(NULL),
        m_display(display),
        m_normal_state(GHOST_kWindowStateNormal),
index 2613cb8f14f5a86f5000cc5d34a30bce4bb9cb5c..83ef46d109c2c1f767786777edebc16aec27564d 100644 (file)
@@ -2365,18 +2365,11 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, int type, int U
                        if(win->active) {
                                GHOST_TEventCursorData *cd= customdata;
                                wmEvent *lastevent= win->queue.last;
-                               
-#if defined(__APPLE__) && defined(GHOST_COCOA)
-                               //Cocoa already uses coordinates with y=0 at bottom, and returns inwindow coordinates on mouse moved event
-                               evt->x= cd->x;
-                               evt->y= cd->y;
-#else
                                int cx, cy;
                                
                                GHOST_ScreenToClient(win->ghostwin, cd->x, cd->y, &cx, &cy);
                                evt->x= cx;
                                evt->y= (win->sizey-1) - cy;
-#endif
                                
                                event.x= evt->x;
                                event.y= evt->y;
@@ -2423,21 +2416,17 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, int type, int U
                                        event.type= MOUSEPAN;
                                        break;
                        }
-#if defined(__APPLE__) && defined(GHOST_COCOA)
-                       //Cocoa already uses coordinates with y=0 at bottom, and returns inwindow coordinates on mouse moved event
-                       event.x= evt->x = pd->x;
-                       event.y = evt->y = pd->y;
-#else
+
                        {
-                       int cx, cy;
-                       GHOST_ScreenToClient(win->ghostwin, pd->x, pd->y, &cx, &cy);
-                       event.x= evt->x= cx;
-                       event.y= evt->y= (win->sizey-1) - cy;
+                               int cx, cy;
+                               GHOST_ScreenToClient(win->ghostwin, pd->x, pd->y, &cx, &cy);
+                               event.x= evt->x= cx;
+                               event.y= evt->y= (win->sizey-1) - cy;
                        }
-#endif
+
                        // Use prevx/prevy so we can calculate the delta later
                        event.prevx= event.x - pd->deltaX;
-                       event.prevy= event.y - pd->deltaY;
+                       event.prevy= event.y - (-pd->deltaY);
                        
                        update_tablet_data(win, &event);
                        wm_event_add(win, &event);
index 9ee391325215a23ce12bdb2ea9b1143b4b3c5688..9b1695be67a066d4b22e1c409cfca2b7e1abd1df 100644 (file)
@@ -673,13 +673,7 @@ static int ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr private)
                                
                                GHOST_ScreenToClient(win->ghostwin, wx, wy, &cx, &cy);
                                win->eventstate->x= cx;
-
-#if defined(__APPLE__) && defined(GHOST_COCOA)
-                               //Cocoa already uses coordinates with y=0 at bottom
-                               win->eventstate->y= cy;
-#else
                                win->eventstate->y= (win->sizey-1) - cy;
-#endif
                                
                                win->addmousemove= 1;   /* enables highlighted buttons */
                                
@@ -796,20 +790,13 @@ static int ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr private)
                                wmEvent event;
                                GHOST_TEventDragnDropData *ddd= GHOST_GetEventData(evt);
                                int cx, cy, wx, wy;
-
                                
                                /* entering window, update mouse pos */
                                GHOST_GetCursorPosition(g_system, &wx, &wy);
                                
                                GHOST_ScreenToClient(win->ghostwin, wx, wy, &cx, &cy);
                                win->eventstate->x= cx;
-                               
-#if defined(__APPLE__) && defined(GHOST_COCOA)
-                               //Cocoa already uses coordinates with y=0 at bottom
-                               win->eventstate->y= cy;
-#else
                                win->eventstate->y= (win->sizey-1) - cy;
-#endif
                                
                                event= *(win->eventstate);      /* copy last state, like mouse coords */
                                
@@ -1149,12 +1136,7 @@ void wm_get_cursor_position(wmWindow *win, int *x, int *y)
 {
        GHOST_GetCursorPosition(g_system, x, y);
        GHOST_ScreenToClient(win->ghostwin, *x, *y, x, y);
-#if defined(__APPLE__) && defined(GHOST_COCOA)
-       //Cocoa has silly exception that should be fixed at the ghost level
-       //(ghost is an allegory for an invisible system specific code)
-#else
        *y = (win->sizey-1) - *y;
-#endif
 }
 
 /* ******************* exported api ***************** */
@@ -1187,9 +1169,8 @@ void WM_cursor_warp(wmWindow *win, int x, int y)
        if (win && win->ghostwin) {
                int oldx=x, oldy=y;
 
-#if !defined(__APPLE__) || !defined(GHOST_COCOA)
                y= win->sizey -y - 1;
-#endif
+
                GHOST_ClientToScreen(win->ghostwin, x, y, &x, &y);
                GHOST_SetCursorPosition(g_system, x, y);