From 9f04318720729dec8ca875a1fbd04c0805a2774a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jul 2018 20:24:58 +0200 Subject: [PATCH] Fix MSW EDIT_HEIGHT_FROM_CHAR_HEIGHT() for high DPI Apply DPI scaling to the extra vertical margin added by this macro to the font height. This makes sense and results in wxTextCtrl (and other controls, e.g. wxSearchCtrl) having the same height as native ones when using high DPI too. Note that this required adding wxGetEditHeightFromCharHeight() function as one of the existing uses of EDIT_HEIGHT_FROM_CHAR_HEIGHT() wasn't inside a wxWindow-derived class method and also adding wxUSE_GUI guard around these GUI-only definitions as a function, unlike a macro, can't be compiled without full wxWindow declaration in scope. --- include/wx/msw/private.h | 22 +++++++++++++++++++--- src/msw/anybutton.cpp | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index 8e10ccd170..9b47c6f528 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -82,19 +82,35 @@ WXDLLIMPEXP_BASE void wxSetInstance(HINSTANCE hInst); // misc macros // --------------------------------------------------------------------------- +#if wxUSE_GUI + #define MEANING_CHARACTER '0' #define DEFAULT_ITEM_WIDTH 100 #define DEFAULT_ITEM_HEIGHT 80 -// Scale font to get edit control height -//#define EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) (3*(cy)/2) -#define EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) (cy+8) +// Return the height of a native text control corresponding to the given +// character height (as returned by GetCharHeight() or wxGetCharSize()). +// +// The wxWindow parameter must be valid and used for getting the DPI. +inline int wxGetEditHeightFromCharHeight(int cy, const wxWindow* w) +{ + // The value 8 here is empiric, i.e. it's not necessarily correct, but + // seems to work relatively well. + return cy + w->FromDIP(8); +} + +// Compatibility macro used in the existing code. It assumes that it's called +// from a method of wxWindow-derived object. +#define EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) \ + wxGetEditHeightFromCharHeight((cy), this) // Generic subclass proc, for panel item moving/sizing and intercept // EDIT control VK_RETURN messages extern LONG APIENTRY wxSubclassedGenericControlProc(WXHWND hWnd, WXUINT message, WXWPARAM wParam, WXLPARAM lParam); +#endif // wxUSE_GUI + // --------------------------------------------------------------------------- // useful macros and functions // --------------------------------------------------------------------------- diff --git a/src/msw/anybutton.cpp b/src/msw/anybutton.cpp index 85a7a79062..a589aed3d7 100644 --- a/src/msw/anybutton.cpp +++ b/src/msw/anybutton.cpp @@ -441,7 +441,7 @@ wxSize wxMSWButton::IncreaseToStdSizeAndCache(wxControl *btn, const wxSize& size // so make them as high as it. int yText; wxGetCharSize(GetHwndOf(btn), NULL, &yText, btn->GetFont()); - yText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(yText); + yText = wxGetEditHeightFromCharHeight(yText, btn); sizeBtn.IncTo(wxSize(-1, yText)); }