Create primary monitor when using wxDisplay default ctor

Previously, the first monitor was created instead and while it was often
also the primary one, this wasn't always the case.

In particular, this makes wxGetDisplayPPI() always return something
reasonable instead of returning (0, 0) when the first monitor is not the
primary one, as expected by plenty of code, including our own printing
sample, which divides by the values returned from wxGetDisplayPPI()
without checking if they're zero.
This commit is contained in:
Vadim Zeitlin
2019-11-08 00:26:20 +01:00
parent 46cb23b030
commit d8cd02b480
4 changed files with 40 additions and 5 deletions

View File

@@ -77,6 +77,11 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxDisplayModule, wxModule);
// ctor/dtor
// ----------------------------------------------------------------------------
wxDisplay::wxDisplay()
{
m_impl = Factory().GetPrimaryDisplay();
}
wxDisplay::wxDisplay(unsigned n)
{
wxASSERT_MSG( n == 0 || n < GetCount(),
@@ -89,7 +94,8 @@ wxDisplay::wxDisplay(const wxWindow* 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();
}
// ----------------------------------------------------------------------------
@@ -245,6 +251,24 @@ void wxDisplayFactory::ClearImpls()
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)
{
wxCHECK_MSG( window, wxNOT_FOUND, "window can't be NULL" );