Add wxWindow::GetDPI()

This is simpler to use than wxDisplay(window).GetPPI() which was used
instead of it so far in all ports and can be implemented more
efficiently for wxMSW.

Remove wxGetWinTLW, GetDPI already tries to get the top window.
This commit is contained in:
Maarten Bent
2019-08-20 22:23:11 +02:00
parent 70f0900799
commit 9c193e1774
5 changed files with 76 additions and 24 deletions

View File

@@ -4719,23 +4719,28 @@ wxWindowMSW::MSWOnMeasureItem(int id, WXMEASUREITEMSTRUCT *itemStruct)
namespace
{
static inline const wxTopLevelWindow* wxGetWinTLW(const wxWindow* win)
static wxSize GetWindowDPI(HWND hwnd)
{
if ( win )
#if wxUSE_DYNLIB_CLASS
typedef UINT (WINAPI *GetDpiForWindow_t)(HWND hwnd);
static GetDpiForWindow_t s_pfnGetDpiForWindow = NULL;
static bool s_initDone = false;
if ( !s_initDone )
{
const wxWindow* tlwWin = wxGetTopLevelParent(const_cast<wxWindow*>(win));
return wxDynamicCast(tlwWin, wxTopLevelWindow);
}
else if ( wxTheApp )
{
wxWindow* window = wxTheApp->GetTopWindow();
if ( window )
{
return wxDynamicCast(wxGetTopLevelParent(window), wxTopLevelWindow);
}
wxLoadedDLL dllUser32("user32.dll");
wxDL_INIT_FUNC(s_pfn, GetDpiForWindow, dllUser32);
s_initDone = true;
}
return NULL;
if ( s_pfnGetDpiForWindow )
{
const int dpi = static_cast<int>(s_pfnGetDpiForWindow(hwnd));
return wxSize(dpi, dpi);
}
#endif // wxUSE_DYNLIB_CLASS
return wxSize();
}
}
@@ -4744,9 +4749,9 @@ static inline const wxTopLevelWindow* wxGetWinTLW(const wxWindow* win)
int wxGetSystemMetrics(int nIndex, const wxWindow* win)
{
#if wxUSE_DYNLIB_CLASS
const wxTopLevelWindow* tlw = wxGetWinTLW(win);
const wxWindow* window = (!win && wxTheApp) ? wxTheApp->GetTopWindow() : win;
if ( tlw )
if ( window )
{
typedef int (WINAPI * GetSystemMetricsForDpi_t)(int nIndex, UINT dpi);
static GetSystemMetricsForDpi_t s_pfnGetSystemMetricsForDpi = NULL;
@@ -4761,8 +4766,7 @@ int wxGetSystemMetrics(int nIndex, const wxWindow* win)
if ( s_pfnGetSystemMetricsForDpi )
{
WindowHDC hdc(tlw->GetHWND());
const int dpi = ::GetDeviceCaps(hdc, LOGPIXELSY);
const int dpi = window->GetDPI().y;
return s_pfnGetSystemMetricsForDpi(nIndex, (UINT)dpi);
}
}
@@ -4777,9 +4781,9 @@ int wxGetSystemMetrics(int nIndex, const wxWindow* win)
bool wxSystemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, const wxWindow* win)
{
#if wxUSE_DYNLIB_CLASS
const wxTopLevelWindow* tlw = wxGetWinTLW(win);
const wxWindow* window = (!win && wxTheApp) ? wxTheApp->GetTopWindow() : win;
if ( tlw )
if ( window )
{
typedef int (WINAPI * SystemParametersInfoForDpi_t)(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, UINT dpi);
static SystemParametersInfoForDpi_t s_pfnSystemParametersInfoForDpi = NULL;
@@ -4794,8 +4798,7 @@ bool wxSystemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWi
if ( s_pfnSystemParametersInfoForDpi )
{
WindowHDC hdc(tlw->GetHWND());
const int dpi = ::GetDeviceCaps(hdc, LOGPIXELSY);
const int dpi = window->GetDPI().y;
if ( s_pfnSystemParametersInfoForDpi(uiAction, uiParam, pvParam, fWinIni, (UINT)dpi) == TRUE )
{
return true;
@@ -4809,6 +4812,31 @@ bool wxSystemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWi
return ::SystemParametersInfo(uiAction, uiParam, pvParam, fWinIni) == TRUE;
}
wxSize wxWindowMSW::GetDPI() const
{
HWND hwnd = GetHwnd();
if ( hwnd == NULL )
{
const wxWindow* topWin = wxGetTopLevelParent(const_cast<wxWindow*>(this));
if ( topWin )
{
hwnd = GetHwndOf(topWin);
}
}
wxSize dpi = GetWindowDPI(hwnd);
if ( !dpi.x || !dpi.y )
{
WindowHDC hdc(GetHwnd());
dpi.x = ::GetDeviceCaps(hdc, LOGPIXELSX);
dpi.y = ::GetDeviceCaps(hdc, LOGPIXELSY);
}
return dpi;
}
// ---------------------------------------------------------------------------
// colours and palettes
// ---------------------------------------------------------------------------