From df4d1d456f38fdda6e998b8a3f57bb621aaaf6f6 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 7 Jul 2018 00:52:26 +0200 Subject: [PATCH 01/13] Make wxSearchCtrl margins proportional to DPI Use FromDIP() in wxSearchCtrl implementation to make the margins of the same size, proportionally, at any DPI. --- src/generic/srchctlg.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index 0bc04c38e0..e5ad2a05d6 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -130,7 +130,7 @@ protected: // can't use wxBORDER_NONE to calculate a good height, in which case we just have to // assume a border in the code above and then subtract the space that would be taken up // by a themed border (the thin blue border and the white internal border). - size.y -= 4; + size.y -= FromDIP(4); self->SetWindowStyleFlag(flags); @@ -436,15 +436,15 @@ wxSize wxSearchCtrl::DoGetBestClientSize() const if ( m_searchButtonVisible || HasMenu() ) { sizeSearch = m_searchButton->GetBestSize(); - searchMargin = MARGIN; + searchMargin = FromDIP(MARGIN); } if ( m_cancelButtonVisible ) { sizeCancel = m_cancelButton->GetBestSize(); - cancelMargin = MARGIN; + cancelMargin = FromDIP(MARGIN); } - int horizontalBorder = 1 + ( sizeText.y - sizeText.y * 14 / 21 ) / 2; + int horizontalBorder = FromDIP(1) + ( sizeText.y - sizeText.y * 14 / 21 ) / 2; // buttons are square and equal to the height of the text control int height = sizeText.y; @@ -470,7 +470,7 @@ void wxSearchCtrl::LayoutControls() wxSize sizeText = m_text->GetBestSize(); // make room for the search menu & clear button - int horizontalBorder = 1 + ( sizeText.y - sizeText.y * 14 / 21 ) / 2; + int horizontalBorder = FromDIP(1) + ( sizeText.y - sizeText.y * 14 / 21 ) / 2; int x = horizontalBorder; width -= horizontalBorder*2; if (width < 0) width = 0; @@ -482,12 +482,12 @@ void wxSearchCtrl::LayoutControls() if ( m_searchButtonVisible || HasMenu() ) { sizeSearch = m_searchButton->GetBestSize(); - searchMargin = MARGIN; + searchMargin = FromDIP(MARGIN); } if ( m_cancelButtonVisible ) { sizeCancel = m_cancelButton->GetBestSize(); - cancelMargin = MARGIN; + cancelMargin = FromDIP(MARGIN); } m_searchButton->Show( m_searchButtonVisible || HasMenu() ); m_cancelButton->Show( m_cancelButtonVisible ); @@ -499,7 +499,7 @@ void wxSearchCtrl::LayoutControls() searchMargin = 0; cancelMargin = 0; } - wxCoord textWidth = width - sizeSearch.x - sizeCancel.x - searchMargin - cancelMargin - 1; + wxCoord textWidth = width - sizeSearch.x - sizeCancel.x - searchMargin - cancelMargin - FromDIP(1); if (textWidth < 0) textWidth = 0; // position the subcontrols inside the client area @@ -514,7 +514,7 @@ void wxSearchCtrl::LayoutControls() // of the white border that's part of the theme border. We can also remove a pixel from // the height to fit the text control in, because the padding in EDIT_HEIGHT_FROM_CHAR_HEIGHT // is already generous. - int textY = 1; + int textY = FromDIP(1); #else int textY = 0; #endif @@ -1167,7 +1167,7 @@ void wxSearchCtrl::RecalcBitmaps() } wxSize sizeText = m_text->GetBestSize(); - int bitmapHeight = sizeText.y - 4; + int bitmapHeight = sizeText.y - FromDIP(4); int bitmapWidth = sizeText.y * 20 / 14; if ( !m_searchBitmapUser ) From 6ed3865925c3571167e572823d1d786178101c69 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 7 Jul 2018 00:54:30 +0200 Subject: [PATCH 02/13] Remove commented out stream operations in wxSearchCtrl Just remove unused and unnecessary declarations. --- include/wx/generic/srchctlg.h | 17 ----------------- src/generic/srchctlg.cpp | 17 ----------------- 2 files changed, 34 deletions(-) diff --git a/include/wx/generic/srchctlg.h b/include/wx/generic/srchctlg.h index 188a92aa2e..a8d2aab843 100644 --- a/include/wx/generic/srchctlg.h +++ b/include/wx/generic/srchctlg.h @@ -172,23 +172,6 @@ public: virtual bool DoAutoCompleteFileNames(int flags) wxOVERRIDE; virtual bool DoAutoCompleteCustom(wxTextCompleter *completer) wxOVERRIDE; -#if 0 - - // override streambuf method -#if wxHAS_TEXT_WINDOW_STREAM - int overflow(int i); -#endif // wxHAS_TEXT_WINDOW_STREAM - - // stream-like insertion operators: these are always available, whether we - // were, or not, compiled with streambuf support - wxTextCtrl& operator<<(const wxString& s); - wxTextCtrl& operator<<(int i); - wxTextCtrl& operator<<(long i); - wxTextCtrl& operator<<(float f); - wxTextCtrl& operator<<(double d); - wxTextCtrl& operator<<(wxChar c); -#endif - // do the window-specific processing after processing the update event virtual void DoUpdateWindowUI(wxUpdateUIEvent& event) wxOVERRIDE; diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index e5ad2a05d6..eefc469fb9 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -883,23 +883,6 @@ void wxSearchCtrl::SetCancelBitmap( const wxBitmap& bitmap ) } } -#if 0 - -// override streambuf method -#if wxHAS_TEXT_WINDOW_STREAM -int overflow(int i); -#endif // wxHAS_TEXT_WINDOW_STREAM - -// stream-like insertion operators: these are always available, whether we -// were, or not, compiled with streambuf support -wxTextCtrl& operator<<(const wxString& s); -wxTextCtrl& operator<<(int i); -wxTextCtrl& operator<<(long i); -wxTextCtrl& operator<<(float f); -wxTextCtrl& operator<<(double d); -wxTextCtrl& operator<<(const wxChar c); -#endif - // Note that overriding DoSetValue() is currently insufficient because the base // class ChangeValue() only updates m_hintData of this object (which is null // anyhow), instead of updating m_text->m_hintData, see #16998. From 18899ce4bca3036144ad32ca4ddbe211572dc2da Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 7 Jul 2018 00:55:35 +0200 Subject: [PATCH 03/13] Replace WXMAX() macro in wxSearchCtrl code with wxMax() Don't invent yet another std::max-macro. We probably could just use the standard function instead but for now use wxMax(), for consistency with the rest of the code base. No real changes. --- src/generic/srchctlg.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index eefc469fb9..6659dc6bd3 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -28,8 +28,7 @@ #if !wxUSE_NATIVE_SEARCH_CONTROL #include "wx/image.h" - -#define WXMAX(a,b) ((a)>(b)?(a):(b)) +#include "wx/utils.h" // ---------------------------------------------------------------------------- // constants @@ -1013,7 +1012,7 @@ wxBitmap wxSearchCtrl::RenderSearchBitmap( int x, int y, bool renderDrop ) mem.SetPen( wxPen(fg) ); mem.SetBrush( wxBrush(fg) ); int handleCornerShift = penWidth * 707 / 1000 / 2; // 707 / 1000 = 0.707 = 1/sqrt(2); - handleCornerShift = WXMAX( handleCornerShift, 1 ); + handleCornerShift = wxMax( handleCornerShift, 1 ); int handleBase = 4 * x / 20; int handleLength = 2*handleBase+1; wxPoint handlePolygon[] = @@ -1112,7 +1111,7 @@ wxBitmap wxSearchCtrl::RenderCancelBitmap( int x, int y ) mem.SetPen( wxPen(bg) ); mem.SetBrush( wxBrush(bg) ); int handleCornerShift = penWidth/2; - handleCornerShift = WXMAX( handleCornerShift, 1 ); + handleCornerShift = wxMax( handleCornerShift, 1 ); wxPoint handlePolygon[] = { wxPoint(-handleCornerShift,+handleCornerShift), From 03b7491beaca148aaa25f66348da08ad6e84fd3f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 7 Jul 2018 00:57:44 +0200 Subject: [PATCH 04/13] Remove unnecessary DoUpdateWindowUI() override in wxSearchCtrl There is no need to override this virtual method just to call the base class version. --- include/wx/generic/srchctlg.h | 3 --- src/generic/srchctlg.cpp | 6 ------ 2 files changed, 9 deletions(-) diff --git a/include/wx/generic/srchctlg.h b/include/wx/generic/srchctlg.h index a8d2aab843..7ac2878698 100644 --- a/include/wx/generic/srchctlg.h +++ b/include/wx/generic/srchctlg.h @@ -172,9 +172,6 @@ public: virtual bool DoAutoCompleteFileNames(int flags) wxOVERRIDE; virtual bool DoAutoCompleteCustom(wxTextCompleter *completer) wxOVERRIDE; - // do the window-specific processing after processing the update event - virtual void DoUpdateWindowUI(wxUpdateUIEvent& event) wxOVERRIDE; - virtual bool ShouldInheritColours() const wxOVERRIDE; // wxWindow overrides diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index 6659dc6bd3..3eba2795d5 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -905,12 +905,6 @@ bool wxSearchCtrl::DoSaveFile(const wxString& file, int fileType) return m_text->DoSaveFile(file, fileType); } -// do the window-specific processing after processing the update event -void wxSearchCtrl::DoUpdateWindowUI(wxUpdateUIEvent& event) -{ - wxSearchCtrlBase::DoUpdateWindowUI(event); -} - bool wxSearchCtrl::ShouldInheritColours() const { return true; From 587e0d8e64e08d745f8acddaca6050879f2dd243 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jul 2018 14:37:43 +0200 Subject: [PATCH 05/13] Fix search button height in generic wxSearchCtrl Make this button of exactly the bitmap size instead of using the full control height for it, even if/when the bitmap is smaller. This fixes some random junk being shown in the bottom two pixels of the search button sometimes: this was due to only redrawing the part covered by the bitmap in wxSearchButton::OnPaint(), leaving the rest of the button unpainted (because wxSearchButton uses wxBG_STYLE_PAINT). --- src/generic/srchctlg.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index 3eba2795d5..c9ea71e5d2 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -504,7 +504,7 @@ void wxSearchCtrl::LayoutControls() // position the subcontrols inside the client area m_searchButton->SetSize(x, (height - sizeSearch.y) / 2, - sizeSearch.x, height); + sizeSearch.x, sizeSearch.y); x += sizeSearch.x; x += searchMargin; From ed8ac7059c65bf60f9fff07f7a3a209168273faf Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jul 2018 16:50:10 +0200 Subject: [PATCH 06/13] Use more descriptive symbolic constants in wxSearchCtrl code Replace LIGHT_STEP macro and 20 magic number with SEARCH_BITMAP_LIGHTNESS and CANCEL_BITMAP_LIGHTNESS constants which are a bit more clear, hopefully. No real changes. --- src/generic/srchctlg.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index c9ea71e5d2..9482c1e3c1 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -37,7 +37,10 @@ // the margin between the text control and the search/cancel buttons static const wxCoord MARGIN = 2; -#define LIGHT_STEP 160 +// arguments to wxColour::ChangeLightness() for making the search/cancel +// bitmaps foreground colour, respectively +static const int SEARCH_BITMAP_LIGHTNESS = 140; // slightly lighter +static const int CANCEL_BITMAP_LIGHTNESS = 160; // a bit more lighter // ---------------------------------------------------------------------------- // wxSearchTextCtrl: text control used by search control @@ -952,7 +955,7 @@ static void RescaleBitmap(wxBitmap& bmp, const wxSize& sizeNeeded) wxBitmap wxSearchCtrl::RenderSearchBitmap( int x, int y, bool renderDrop ) { wxColour bg = GetBackgroundColour(); - wxColour fg = GetForegroundColour().ChangeLightness(LIGHT_STEP-20); + wxColour fg = GetForegroundColour().ChangeLightness(SEARCH_BITMAP_LIGHTNESS); //=============================================================================== // begin drawing code @@ -1055,7 +1058,7 @@ wxBitmap wxSearchCtrl::RenderSearchBitmap( int x, int y, bool renderDrop ) wxBitmap wxSearchCtrl::RenderCancelBitmap( int x, int y ) { wxColour bg = GetBackgroundColour(); - wxColour fg = GetForegroundColour().ChangeLightness(LIGHT_STEP); + wxColour fg = GetForegroundColour().ChangeLightness(CANCEL_BITMAP_LIGHTNESS); //=============================================================================== // begin drawing code From 67bceedfdab3b9ad8163d0ad75fdbae5163b69fd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jul 2018 17:12:02 +0200 Subject: [PATCH 07/13] Get rid of m_searchButtonVisible and m_cancelButtonVisible There doesn't seem to be any reason to have these flags, which duplicate the visibility state of m_searchButton and m_cancelButton respectively. Also update the buttons visibility immediately in ShowSearchButton() and ShowCancelButton() instead of doing it in LayoutControls() as before, which was confusing as laying out is not supposed to show/hide anything. Finally, return true, not false, from IsSearchButtonVisible() if the button is actually visible because there is an associated menu, even if ShowSearchButton(false) had been called. This seems more logical and makes the code simpler, but we need to check whether the native Mac version also behaves like this or not. No other changes in behaviour. --- include/wx/generic/srchctlg.h | 3 -- src/generic/srchctlg.cpp | 61 ++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/include/wx/generic/srchctlg.h b/include/wx/generic/srchctlg.h index 7ac2878698..996f4e8560 100644 --- a/include/wx/generic/srchctlg.h +++ b/include/wx/generic/srchctlg.h @@ -237,9 +237,6 @@ private: wxMenu *m_menu; #endif // wxUSE_MENUS - bool m_searchButtonVisible; - bool m_cancelButtonVisible; - bool m_searchBitmapUser; bool m_cancelBitmapUser; #if wxUSE_MENUS diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index 9482c1e3c1..455dd1efa6 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -284,9 +284,6 @@ void wxSearchCtrl::Init() m_menu = NULL; #endif // wxUSE_MENUS - m_searchButtonVisible = true; - m_cancelButtonVisible = false; - m_searchBitmapUser = false; m_cancelBitmapUser = false; #if wxUSE_MENUS @@ -313,9 +310,6 @@ bool wxSearchCtrl::Create(wxWindow *parent, wxWindowID id, m_searchButton = new wxSearchButton(this, wxEVT_SEARCH, m_searchBitmap); - m_cancelButton = new wxSearchButton(this, - wxEVT_SEARCH_CANCEL, - m_cancelBitmap); SetBackgroundColour( m_text->GetBackgroundColour() ); m_text->SetBackgroundColour(wxColour()); @@ -354,13 +348,14 @@ void wxSearchCtrl::SetMenu( wxMenu* menu ) if ( m_menu && !hadMenu ) { + m_searchButton->Show(); m_searchButton->SetBitmapLabel(m_searchMenuBitmap); m_searchButton->Refresh(); } else if ( !m_menu && hadMenu ) { m_searchButton->SetBitmapLabel(m_searchBitmap); - if ( m_searchButtonVisible ) + if ( m_searchButton->IsShown() ) { m_searchButton->Refresh(); } @@ -377,15 +372,23 @@ wxMenu* wxSearchCtrl::GetMenu() void wxSearchCtrl::ShowSearchButton( bool show ) { - if ( m_searchButtonVisible == show ) + if ( show == IsSearchButtonVisible() ) { // no change return; } - m_searchButtonVisible = show; - if ( m_searchButtonVisible ) + if ( show ) { RecalcBitmaps(); + + m_searchButton->Show(); + } + else // Requested to hide it. + { + // Only hide the button if we don't need it for the menu, otherwise it + // needs to remain shown. + if ( !HasMenu() ) + m_searchButton->Hide(); } LayoutControls(); @@ -393,25 +396,36 @@ void wxSearchCtrl::ShowSearchButton( bool show ) bool wxSearchCtrl::IsSearchButtonVisible() const { - return m_searchButtonVisible; + return m_searchButton->IsShown() || HasMenu(); } void wxSearchCtrl::ShowCancelButton( bool show ) { - if ( m_cancelButtonVisible == show ) + if ( show == IsCancelButtonVisible() ) { // no change return; } - m_cancelButtonVisible = show; + + // This button is not shown initially, so create it on demand if necessary, + // i.e. if it's the first time we show it. + if ( !m_cancelButton ) + { + m_cancelButton = new wxSearchButton(this, + wxEVT_SEARCH_CANCEL, + m_cancelBitmap); + RecalcBitmaps(); + } + + m_cancelButton->Show(show); LayoutControls(); } bool wxSearchCtrl::IsCancelButtonVisible() const { - return m_cancelButtonVisible; + return m_cancelButton && m_cancelButton->IsShown(); } void wxSearchCtrl::SetDescriptiveText(const wxString& text) @@ -435,12 +449,12 @@ wxSize wxSearchCtrl::DoGetBestClientSize() const wxSize sizeCancel(0,0); int searchMargin = 0; int cancelMargin = 0; - if ( m_searchButtonVisible || HasMenu() ) + if ( IsSearchButtonVisible() ) { sizeSearch = m_searchButton->GetBestSize(); searchMargin = FromDIP(MARGIN); } - if ( m_cancelButtonVisible ) + if ( IsCancelButtonVisible() ) { sizeCancel = m_cancelButton->GetBestSize(); cancelMargin = FromDIP(MARGIN); @@ -481,18 +495,16 @@ void wxSearchCtrl::LayoutControls() wxSize sizeCancel(0,0); int searchMargin = 0; int cancelMargin = 0; - if ( m_searchButtonVisible || HasMenu() ) + if ( IsSearchButtonVisible() ) { sizeSearch = m_searchButton->GetBestSize(); searchMargin = FromDIP(MARGIN); } - if ( m_cancelButtonVisible ) + if ( IsCancelButtonVisible() ) { sizeCancel = m_cancelButton->GetBestSize(); cancelMargin = FromDIP(MARGIN); } - m_searchButton->Show( m_searchButtonVisible || HasMenu() ); - m_cancelButton->Show( m_cancelButtonVisible ); if ( sizeSearch.x + sizeCancel.x > width ) { @@ -525,8 +537,11 @@ void wxSearchCtrl::LayoutControls() x += textWidth; x += cancelMargin; - m_cancelButton->SetSize(x, (height - sizeCancel.y) / 2, - sizeCancel.x, height); + if ( m_cancelButton ) + { + m_cancelButton->SetSize(x, (height - sizeCancel.y) / 2, + sizeCancel.x, height); + } } wxWindowList wxSearchCtrl::GetCompositeWindowParts() const @@ -1185,7 +1200,7 @@ void wxSearchCtrl::RecalcBitmaps() } #endif // wxUSE_MENUS - if ( !m_cancelBitmapUser ) + if ( m_cancelButton && !m_cancelBitmapUser ) { if ( !m_cancelBitmap.IsOk() || From c713bee7ea726a46a19f6d6c7676f19f0157bcb6 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jul 2018 17:54:09 +0200 Subject: [PATCH 08/13] Remove unnecessary wxSearchCtrl::DoMoveWindow() override There is no need to override this virtual function, if the control size changes, a wxEVT_SIZE event is generated in any case, resulting in a call to LayoutControls() without explicitly calling it from here. --- include/wx/generic/srchctlg.h | 1 - src/generic/srchctlg.cpp | 7 ------- 2 files changed, 8 deletions(-) diff --git a/include/wx/generic/srchctlg.h b/include/wx/generic/srchctlg.h index 996f4e8560..79151ea5af 100644 --- a/include/wx/generic/srchctlg.h +++ b/include/wx/generic/srchctlg.h @@ -194,7 +194,6 @@ protected: // override the base class virtuals involved into geometry calculations virtual wxSize DoGetBestClientSize() const wxOVERRIDE; - virtual void DoMoveWindow(int x, int y, int width, int height) wxOVERRIDE; virtual void RecalcBitmaps(); diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index 455dd1efa6..15a99c0927 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -468,13 +468,6 @@ wxSize wxSearchCtrl::DoGetBestClientSize() const height); } -void wxSearchCtrl::DoMoveWindow(int x, int y, int width, int height) -{ - wxSearchCtrlBase::DoMoveWindow(x, y, width, height); - - LayoutControls(); -} - void wxSearchCtrl::LayoutControls() { if ( !m_text ) From 9f04318720729dec8ca875a1fbd04c0805a2774a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jul 2018 20:24:58 +0200 Subject: [PATCH 09/13] 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)); } From 50aee9a0b32d313eefb0cd666240ed2000b2e1eb Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jul 2018 20:35:42 +0200 Subject: [PATCH 10/13] Fix default size for wxSpinCtrl in high DPI DEFAULT_ITEM_WIDTH is a constant, use FromDIP() around it to avoid creating too narrow controls when using high DPI. --- src/msw/spinctrl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/msw/spinctrl.cpp b/src/msw/spinctrl.cpp index 1e4e09cd46..fbb9e2b387 100644 --- a/src/msw/spinctrl.cpp +++ b/src/msw/spinctrl.cpp @@ -726,7 +726,7 @@ int wxSpinCtrl::GetOverlap() const wxSize wxSpinCtrl::DoGetBestSize() const { - return DoGetSizeFromTextSize(DEFAULT_ITEM_WIDTH); + return DoGetSizeFromTextSize(FromDIP(DEFAULT_ITEM_WIDTH)); } wxSize wxSpinCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const From a38176c67a63ce925a591f3d8514f45eccc4d27b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jul 2018 20:36:16 +0200 Subject: [PATCH 11/13] Make wxTextCtrl horizontal margins proportional to DPI Add more FromDIP() calls. --- src/msw/textctrl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index 7005edc332..ec96a10635 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -2429,7 +2429,7 @@ wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const int cx, cy; wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); - DWORD wText = 1; + DWORD wText = FromDIP(1); ::SystemParametersInfo(SPI_GETCARETWIDTH, 0, &wText, 0); wText += xlen; @@ -2464,7 +2464,7 @@ wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const // stand out). if ( !HasFlag(wxBORDER_NONE) ) { - wText += 9; // borders and inner margins + wText += FromDIP(9); // borders and inner margins // we have to add the adjustments for the control height only once, not // once per line, so do it after multiplication above From 4d4e95849a0f973f5827e65807bf643da3ac2d57 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jul 2018 20:50:22 +0200 Subject: [PATCH 12/13] Fix size of wxSearchCtrl in the widgets sample in high DPI Add missing FromDIP(), without it the control was too small. --- samples/widgets/searchctrl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/widgets/searchctrl.cpp b/samples/widgets/searchctrl.cpp index 623d2b92e3..a4607cafa7 100644 --- a/samples/widgets/searchctrl.cpp +++ b/samples/widgets/searchctrl.cpp @@ -178,7 +178,7 @@ void SearchCtrlWidgetsPage::CreateControl() long style = GetAttrs().m_defaultFlags; m_srchCtrl = new wxSearchCtrl(this, -1, wxEmptyString, wxDefaultPosition, - wxSize(150, -1), style); + FromDIP(wxSize(150, -1)), style); m_srchCtrl->Bind(wxEVT_SET_FOCUS, &SearchCtrlWidgetsPage::OnSetFocus, this); m_srchCtrl->Bind(wxEVT_KILL_FOCUS, &SearchCtrlWidgetsPage::OnKillFocus, this); From 42ab122912ce2a04c754f97367e706b28898a01e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jul 2018 20:50:48 +0200 Subject: [PATCH 13/13] Shift the text control another pixel down in MSW wxSearchCtrl It was too high otherwise, which was especially noticeable in high DPI. This is still not perfect, as the default search bitmap is not centered neither and is a couple of pixels too high, but better than before. --- src/generic/srchctlg.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index 15a99c0927..e6cc4b1977 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -521,7 +521,7 @@ void wxSearchCtrl::LayoutControls() // of the white border that's part of the theme border. We can also remove a pixel from // the height to fit the text control in, because the padding in EDIT_HEIGHT_FROM_CHAR_HEIGHT // is already generous. - int textY = FromDIP(1); + int textY = FromDIP(2); #else int textY = 0; #endif