diff --git a/intern/ghost/intern/GHOST_System.cc b/intern/ghost/intern/GHOST_System.cc index 729ce8534da..9625bd45504 100644 --- a/intern/ghost/intern/GHOST_System.cc +++ b/intern/ghost/intern/GHOST_System.cc @@ -204,16 +204,27 @@ bool GHOST_System::getFullScreen() GHOST_IWindow *GHOST_System::getWindowUnderCursor(int32_t x, int32_t y) { /* TODO: This solution should follow the order of the activated windows (Z-order). - * It is imperfect but usable in most cases. */ - for (GHOST_IWindow *iwindow : m_windowManager->getWindows()) { - if (iwindow->getState() == GHOST_kWindowStateMinimized) { + * It is imperfect but usable in most cases. Ideally each platform should provide + * a custom version of this function that properly considers z-order. */ + + std::vector windows = m_windowManager->getWindows(); + std::vector::reverse_iterator iwindow_iter; + + /* Search through the windows in reverse order because in most cases + * the window that is on top was created after those that are below it. */ + + for (iwindow_iter = windows.rbegin(); iwindow_iter != windows.rend(); ++iwindow_iter) { + + GHOST_IWindow *win = *iwindow_iter; + + if (win->getState() == GHOST_kWindowStateMinimized) { continue; } GHOST_Rect bounds; - iwindow->getClientBounds(bounds); + win->getClientBounds(bounds); if (bounds.isInside(x, y)) { - return iwindow; + return win; } }