Merge branch 'primary-display'

Handle the situation when primary display is not the first one.

See https://github.com/wxWidgets/wxWidgets/pull/1641
This commit is contained in:
Vadim Zeitlin
2019-11-10 00:39:14 +01:00
6 changed files with 53 additions and 53 deletions

View File

@@ -42,12 +42,14 @@ class WXDLLIMPEXP_FWD_CORE wxDisplayImpl;
class WXDLLIMPEXP_CORE wxDisplay class WXDLLIMPEXP_CORE wxDisplay
{ {
public: public:
// default ctor creates the object corresponding to the primary display
wxDisplay();
// initialize the object containing all information about the given // initialize the object containing all information about the given
// display // display
// //
// the displays are numbered from 0 to GetCount() - 1, 0 is always the // the displays are numbered from 0 to GetCount() - 1
// primary display and the only one which is always supported explicit wxDisplay(unsigned n);
wxDisplay(unsigned n = 0);
// create display object corresponding to the display of the given window // create display object corresponding to the display of the given window
// or the default one if the window display couldn't be found // or the default one if the window display couldn't be found

View File

@@ -37,6 +37,9 @@ public:
return m_impls[n]; return m_impls[n];
} }
// Return the primary display object, creating it if necessary.
wxDisplayImpl* GetPrimaryDisplay();
// get the total number of displays // get the total number of displays
virtual unsigned GetCount() = 0; virtual unsigned GetCount() = 0;

View File

@@ -16,6 +16,12 @@
class wxDisplay class wxDisplay
{ {
public: public:
/**
Default constructor creating wxDisplay object representing the primary
display.
*/
wxDisplay();
/** /**
Constructor, setting up a wxDisplay instance with the specified Constructor, setting up a wxDisplay instance with the specified
display. display.
@@ -24,7 +30,7 @@ public:
The index of the display to use. This must be non-negative and The index of the display to use. This must be non-negative and
lower than the value returned by GetCount(). lower than the value returned by GetCount().
*/ */
wxDisplay(unsigned int index = 0); explicit wxDisplay(unsigned int index);
/** /**
Constructor creating the display object associated with the given Constructor creating the display object associated with the given

View File

@@ -327,9 +327,7 @@ void MyFrame::PopuplateWithDisplayInfo()
page->SetSizer(sizerTop); page->SetSizer(sizerTop);
page->Layout(); page->Layout();
m_book->AddPage(page, m_book->AddPage(page, wxString::Format("Display %zu", nDpy + 1));
wxString::Format("Display %lu",
(unsigned long)nDpy));
} }
SetClientSize(m_book->GetBestSize()); SetClientSize(m_book->GetBestSize());

View File

@@ -77,6 +77,11 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxDisplayModule, wxModule);
// ctor/dtor // ctor/dtor
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxDisplay::wxDisplay()
{
m_impl = Factory().GetPrimaryDisplay();
}
wxDisplay::wxDisplay(unsigned n) wxDisplay::wxDisplay(unsigned n)
{ {
wxASSERT_MSG( n == 0 || n < GetCount(), wxASSERT_MSG( n == 0 || n < GetCount(),
@@ -89,7 +94,8 @@ wxDisplay::wxDisplay(const wxWindow* window)
{ {
const int n = GetFromWindow(window); const int n = GetFromWindow(window);
m_impl = Factory().GetDisplay(n != wxNOT_FOUND ? n : 0); m_impl = n != wxNOT_FOUND ? Factory().GetDisplay(n)
: Factory().GetPrimaryDisplay();
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -159,7 +165,7 @@ wxString wxDisplay::GetName() const
bool wxDisplay::IsPrimary() const bool wxDisplay::IsPrimary() const
{ {
return m_impl && m_impl->GetIndex() == 0; return m_impl && m_impl->IsPrimary();
} }
#if wxUSE_DISPLAY #if wxUSE_DISPLAY
@@ -245,6 +251,24 @@ void wxDisplayFactory::ClearImpls()
m_impls.clear(); m_impls.clear();
} }
wxDisplayImpl* wxDisplayFactory::GetPrimaryDisplay()
{
// Just use dumb linear search -- there seems to be the most reliable way
// to do this in general. In particular, primary monitor is not guaranteed
// to be the first one and it's not obvious if it always contains (0, 0).
const unsigned count = GetCount();
for ( unsigned n = 0; n < count; ++n )
{
wxDisplayImpl* const d = GetDisplay(n);
if ( d && d->IsPrimary() )
return d;
}
// This is not supposed to happen, but what else can we do if it
// somehow does?
return NULL;
}
int wxDisplayFactory::GetFromWindow(const wxWindow *window) int wxDisplayFactory::GetFromWindow(const wxWindow *window)
{ {
wxCHECK_MSG( window, wxNOT_FOUND, "window can't be NULL" ); wxCHECK_MSG( window, wxNOT_FOUND, "window can't be NULL" );

View File

@@ -111,9 +111,14 @@ namespace
// Simple struct storing the information needed by wxDisplayMSW. // Simple struct storing the information needed by wxDisplayMSW.
struct wxDisplayInfo struct wxDisplayInfo
{ {
wxDisplayInfo(HMONITOR hmon_, int depth_) : hmon(hmon_), depth(depth_) {} wxDisplayInfo(HMONITOR hmon_,
const MONITORINFOEX& monInfo_,
int depth_)
: hmon(hmon_), monInfo(monInfo_), depth(depth_)
{}
HMONITOR hmon; HMONITOR hmon;
MONITORINFOEX monInfo;
int depth; int depth;
}; };
@@ -157,10 +162,6 @@ protected:
dm.dmDisplayFrequency > 1 ? dm.dmDisplayFrequency : 0); dm.dmDisplayFrequency > 1 ? dm.dmDisplayFrequency : 0);
} }
// Call GetMonitorInfo() and fill in the provided struct and return true if
// it succeeded, otherwise return false.
bool GetMonInfo(MONITORINFOEX& monInfo) const;
wxDisplayInfo m_info; wxDisplayInfo m_info;
private: private:
@@ -298,37 +299,14 @@ wxDisplayFactoryMSW::GetDpiForMonitorData
// wxDisplayMSW implementation // wxDisplayMSW implementation
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
bool wxDisplayMSW::GetMonInfo(MONITORINFOEX& monInfo) const
{
if ( !::GetMonitorInfo(m_info.hmon, &monInfo) )
{
wxLogLastError(wxT("GetMonitorInfo"));
return false;
}
return true;
}
wxRect wxDisplayMSW::GetGeometry() const wxRect wxDisplayMSW::GetGeometry() const
{ {
WinStruct<MONITORINFOEX> monInfo; return wxRectFromRECT(m_info.monInfo.rcMonitor);
wxRect rect;
if ( GetMonInfo(monInfo) )
wxCopyRECTToRect(monInfo.rcMonitor, rect);
return rect;
} }
wxRect wxDisplayMSW::GetClientArea() const wxRect wxDisplayMSW::GetClientArea() const
{ {
WinStruct<MONITORINFOEX> monInfo; return wxRectFromRECT(m_info.monInfo.rcWork);
wxRect rectClient;
if ( GetMonInfo(monInfo) )
wxCopyRECTToRect(monInfo.rcWork, rectClient);
return rectClient;
} }
int wxDisplayMSW::GetDepth() const int wxDisplayMSW::GetDepth() const
@@ -356,23 +334,12 @@ wxSize wxDisplayMSW::GetPPI() const
wxString wxDisplayMSW::GetName() const wxString wxDisplayMSW::GetName() const
{ {
WinStruct<MONITORINFOEX> monInfo; return m_info.monInfo.szDevice;
wxString name;
if ( GetMonInfo(monInfo) )
name = monInfo.szDevice;
return name;
} }
bool wxDisplayMSW::IsPrimary() const bool wxDisplayMSW::IsPrimary() const
{ {
WinStruct<MONITORINFOEX> monInfo; return (m_info.monInfo.dwFlags & MONITORINFOF_PRIMARY) != 0;
if ( !GetMonInfo(monInfo) )
return false;
return (monInfo.dwFlags & MONITORINFOF_PRIMARY) != 0;
} }
wxVideoMode wxDisplayMSW::GetCurrentMode() const wxVideoMode wxDisplayMSW::GetCurrentMode() const
@@ -623,7 +590,7 @@ wxDisplayFactoryMSW::MultimonEnumProc(
const int hdcDepth = wxGetHDCDepth(hdcMonitor); const int hdcDepth = wxGetHDCDepth(hdcMonitor);
::DeleteDC(hdcMonitor); ::DeleteDC(hdcMonitor);
self->m_displays.push_back(wxDisplayInfo(hMonitor, hdcDepth)); self->m_displays.push_back(wxDisplayInfo(hMonitor, monInfo, hdcDepth));
// continue the enumeration // continue the enumeration
return TRUE; return TRUE;