From 0da3f1c11f60c4fdd50df178a89335a240701166 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 8 May 2023 22:47:04 +0100 Subject: [PATCH] Add wxDisplay::GetFromRect() For now this is just the same thing as GetFromPoint(center-of-rectangle) but it's already more convenient to use and it will also be changed in the upcoming commits. --- include/wx/display.h | 4 ++++ include/wx/private/display.h | 4 ++++ interface/wx/display.h | 12 ++++++++++++ src/common/dpycmn.cpp | 15 ++++++++++++--- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/include/wx/display.h b/include/wx/display.h index 3bcb0274c4..4589279804 100644 --- a/include/wx/display.h +++ b/include/wx/display.h @@ -67,6 +67,10 @@ public: // it doesn't belong to any display static int GetFromPoint(const wxPoint& pt); + // find the display which has the biggest intersection with the given + // rectangle or wxNOT_FOUND if the rectangle doesn't intersect any display + static int GetFromRect(const wxRect& rect); + // find the display where the given window lies, return wxNOT_FOUND if it // is not shown at all static int GetFromWindow(const wxWindow *window); diff --git a/include/wx/private/display.h b/include/wx/private/display.h index 29d73003b9..8950fa3e80 100644 --- a/include/wx/private/display.h +++ b/include/wx/private/display.h @@ -68,6 +68,10 @@ public: // return the display for the given point or wxNOT_FOUND virtual int GetFromPoint(const wxPoint& pt) = 0; + // return the display with biggest intersection with the given rectangle or + // wxNOT_FOUND + virtual int GetFromRect(const wxRect& rect); + // return the display for the given window or wxNOT_FOUND // // the window pointer must not be null (i.e. caller should check it) diff --git a/interface/wx/display.h b/interface/wx/display.h index 202dc3a1d6..043f6eabbb 100644 --- a/interface/wx/display.h +++ b/interface/wx/display.h @@ -94,6 +94,18 @@ public: */ static int GetFromPoint(const wxPoint& pt); + /** + Returns the index of the display with biggest intersection with the + given rectangle or @c wxNOT_FOUND if the rectangle doesn't intersect + any display. + + @param rect + The rectangle to check. + + @since 3.3.0 + */ + static int GetFromRect(const wxRect& rect); + /** Returns the index of the display on which the given window lies. diff --git a/src/common/dpycmn.cpp b/src/common/dpycmn.cpp index 4a9b97871e..2ec3377aa4 100644 --- a/src/common/dpycmn.cpp +++ b/src/common/dpycmn.cpp @@ -106,6 +106,11 @@ wxDisplay::wxDisplay(const wxWindow* window) return Factory().GetFromPoint(pt); } +/* static */ int wxDisplay::GetFromRect(const wxRect& rect) +{ + return Factory().GetFromRect(rect); +} + /* static */ int wxDisplay::GetFromWindow(const wxWindow *window) { wxCHECK_MSG( window, wxNOT_FOUND, wxT("invalid window") ); @@ -241,6 +246,12 @@ wxDisplayImpl* wxDisplayFactory::GetPrimaryDisplay() return nullptr; } +int wxDisplayFactory::GetFromRect(const wxRect& r) +{ + // consider that the window belongs to the display containing its centre + return GetFromPoint(wxPoint(r.x + r.width/2, r.y + r.height/2)); +} + int wxDisplayFactory::GetFromWindow(const wxWindow *window) { wxCHECK_MSG( window, wxNOT_FOUND, "window can't be null" ); @@ -252,9 +263,7 @@ int wxDisplayFactory::GetFromWindow(const wxWindow *window) if ( !window->GetHandle() ) return wxNOT_FOUND; - // consider that the window belongs to the display containing its centre - const wxRect r(window->GetScreenRect()); - return GetFromPoint(wxPoint(r.x + r.width/2, r.y + r.height/2)); + return GetFromRect(window->GetScreenRect()); } // ============================================================================