From f57cb6c1d99e53d140626785938e594bd2829a2f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 1 Nov 2018 00:02:47 +0100 Subject: [PATCH] Add wxDisplay(wxWindow*) ctor with fall back on primary display This is more convenient than calling GetFromWindow() and then checking its return value. --- include/wx/display.h | 4 ++++ interface/wx/display.h | 17 +++++++++++++++++ src/common/dpycmn.cpp | 9 +++++++++ 3 files changed, 30 insertions(+) diff --git a/include/wx/display.h b/include/wx/display.h index af299a74e0..1c3a67298a 100644 --- a/include/wx/display.h +++ b/include/wx/display.h @@ -49,6 +49,10 @@ public: // primary display and the only one which is always supported wxDisplay(unsigned n = 0); + // create display object corresponding to the display of the given window + // or the default one if the window display couldn't be found + explicit wxDisplay(const wxWindow* window); + // dtor is not virtual as this is a concrete class not meant to be derived // from diff --git a/interface/wx/display.h b/interface/wx/display.h index 1a3ca71450..3c4e28f4bb 100644 --- a/interface/wx/display.h +++ b/interface/wx/display.h @@ -26,6 +26,23 @@ public: */ wxDisplay(unsigned int index = 0); + /** + Constructor creating the display object associated with the given + window. + + This is the most convenient way of finding the display on which the + given window is shown while falling back to the default display if it + is not shown at all or positioned outside of any display. + + @param window + A valid, i.e. non-null, window. + + @see GetFromWindow() + + @since 3.1.2 + */ + explicit wxDisplay(const wxWindow* window); + /** Destructor. */ diff --git a/src/common/dpycmn.cpp b/src/common/dpycmn.cpp index 53311f86ef..6171335b1a 100644 --- a/src/common/dpycmn.cpp +++ b/src/common/dpycmn.cpp @@ -85,6 +85,13 @@ wxDisplay::wxDisplay(unsigned n) m_impl = Factory().GetDisplay(n); } +wxDisplay::wxDisplay(const wxWindow* window) +{ + const int n = GetFromWindow(window); + + m_impl = Factory().GetDisplay(n != wxNOT_FOUND ? n : 0); +} + // ---------------------------------------------------------------------------- // static functions forwarded to wxDisplayFactory // ---------------------------------------------------------------------------- @@ -233,6 +240,8 @@ wxDisplayFactory::~wxDisplayFactory() int wxDisplayFactory::GetFromWindow(const wxWindow *window) { + wxCHECK_MSG( window, wxNOT_FOUND, "window can't be NULL" ); + // 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));