From dad9a02a401983ce2f8d640b11b223166671b087 Mon Sep 17 00:00:00 2001 From: wangqr Date: Tue, 19 May 2020 00:18:51 -0400 Subject: [PATCH 1/5] Remeasure magic values for wxTextCtrl's border width on wxOSX/Cocoa Measured on macOS Catalina. Also implements GetSizeFromTextSize --- include/wx/osx/textctrl.h | 1 + src/osx/textctrl_osx.cpp | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/wx/osx/textctrl.h b/include/wx/osx/textctrl.h index 96ee937dc1..4afe69def4 100644 --- a/include/wx/osx/textctrl.h +++ b/include/wx/osx/textctrl.h @@ -139,6 +139,7 @@ protected: void Init(); virtual wxSize DoGetBestSize() const wxOVERRIDE; + virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen) const wxOVERRIDE; // flag is set to true when the user edits the controls contents bool m_dirty; diff --git a/src/osx/textctrl_osx.cpp b/src/osx/textctrl_osx.cpp index 8fd8201d26..a6da73bb9c 100644 --- a/src/osx/textctrl_osx.cpp +++ b/src/osx/textctrl_osx.cpp @@ -206,19 +206,19 @@ wxSize wxTextCtrl::DoGetBestSize() const switch ( m_windowVariant ) { case wxWINDOW_VARIANT_NORMAL : - hText = 22 - 6 ; + hText = 22 - 5 ; break ; case wxWINDOW_VARIANT_SMALL : - hText = 19 - 6 ; + hText = 19 - 5 ; break ; case wxWINDOW_VARIANT_MINI : - hText = 15 - 6 ; + hText = 15 - 5 ; break ; default : - hText = 22 - 6; + hText = 22 - 5; break ; } } @@ -229,11 +229,19 @@ wxSize wxTextCtrl::DoGetBestSize() const hText *= 5 ; if ( !HasFlag(wxNO_BORDER) ) - hText += 6 ; + hText += 5 ; return wxSize(wText, hText); } +wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const +{ + wxSize size = wxDefaultSize; + if (xlen > 0) size.SetWidth(HasFlag(wxNO_BORDER) ? xlen + 4 : xlen + 9); + if (ylen > 0) size.SetHeight(HasFlag(wxNO_BORDER) ? ylen + 2 : ylen + 7); + return size; +} + bool wxTextCtrl::GetStyle(long position, wxTextAttr& style) { return GetTextPeer()->GetStyle(position, style); From a06fb27f211db6e9ec2f7602a3301550e454ae92 Mon Sep 17 00:00:00 2001 From: wangqr Date: Tue, 19 May 2020 00:22:01 -0400 Subject: [PATCH 2/5] Implement wxSpinCtrl::GetSizeFromTextSize for wxOSX/Cocoa The value is calculated based on size of wxTextCtrl --- src/generic/spinctlg.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/generic/spinctlg.cpp b/src/generic/spinctlg.cpp index 29443b6f3d..3b3ccf64d2 100644 --- a/src/generic/spinctlg.cpp +++ b/src/generic/spinctlg.cpp @@ -278,6 +278,10 @@ wxSize wxSpinCtrlGenericBase::DoGetBestSize() const wxSize wxSpinCtrlGenericBase::DoGetSizeFromTextSize(int xlen, int ylen) const { +#ifdef __WXOSX__ + wxSize sizeBtn = m_spinButton->GetBestSize(), sizeText = m_textCtrl->GetSizeFromTextSize(xlen, ylen); + return wxSize(sizeBtn.GetWidth() + sizeText.GetWidth() + MARGIN, wxMax(sizeBtn.GetHeight(), sizeText.GetHeight())); +#else wxSize sizeBtn = m_spinButton->GetBestSize(); wxSize totalS( m_textCtrl->GetBestSize() ); @@ -293,6 +297,7 @@ wxSize wxSpinCtrlGenericBase::DoGetSizeFromTextSize(int xlen, int ylen) const tsize.IncBy(0, ylen - GetCharHeight()); return tsize; +#endif } void wxSpinCtrlGenericBase::DoMoveWindow(int x, int y, int width, int height) From 43c9b9f6585ae304416b67aa68e25558c7b4b8ee Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 28 May 2020 01:34:59 +0200 Subject: [PATCH 3/5] Use TEXTCTRL_BORDER_SIZE symbolic constant in wxTextCtrl code This is more readable than hardcoded 5. No real changes. --- src/osx/textctrl_osx.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/osx/textctrl_osx.cpp b/src/osx/textctrl_osx.cpp index a6da73bb9c..caf9530137 100644 --- a/src/osx/textctrl_osx.cpp +++ b/src/osx/textctrl_osx.cpp @@ -43,6 +43,8 @@ #include "wx/osx/private.h" +static const int TEXTCTRL_BORDER_SIZE = 5; + wxBEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase) EVT_DROP_FILES(wxTextCtrl::OnDropFiles) EVT_CHAR(wxTextCtrl::OnChar) @@ -199,28 +201,31 @@ wxSize wxTextCtrl::DoGetBestSize() const if ( hText == - 1) { - // these are the numbers from the HIG: - // we reduce them by the borders first wText = 100 ; + // these are the numbers from the HIG: switch ( m_windowVariant ) { case wxWINDOW_VARIANT_NORMAL : - hText = 22 - 5 ; + hText = 22; break ; case wxWINDOW_VARIANT_SMALL : - hText = 19 - 5 ; + hText = 19; break ; case wxWINDOW_VARIANT_MINI : - hText = 15 - 5 ; + hText = 15; break ; default : - hText = 22 - 5; + hText = 22; break ; } + + // the numbers above include the border size, so subtract it before + // possibly adding it back below + hText -= TEXTCTRL_BORDER_SIZE; } // as the above numbers have some free space around the text @@ -229,7 +234,7 @@ wxSize wxTextCtrl::DoGetBestSize() const hText *= 5 ; if ( !HasFlag(wxNO_BORDER) ) - hText += 5 ; + hText += TEXTCTRL_BORDER_SIZE ; return wxSize(wText, hText); } From 8189ce89ed4855fb0871ce1a19959b730dc5ab30 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 28 May 2020 02:12:43 +0200 Subject: [PATCH 4/5] Improve wxTextCtrl::DoGetSizeFromTextSize() in wxOSX Always return fully initialized wxSize value, even if one of the input values is unspecified. Also use TEXTCTRL_BORDER_SIZE to make it slightly more clear what's going on. --- src/osx/textctrl_osx.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/osx/textctrl_osx.cpp b/src/osx/textctrl_osx.cpp index caf9530137..4e64b8c530 100644 --- a/src/osx/textctrl_osx.cpp +++ b/src/osx/textctrl_osx.cpp @@ -241,9 +241,24 @@ wxSize wxTextCtrl::DoGetBestSize() const wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const { - wxSize size = wxDefaultSize; - if (xlen > 0) size.SetWidth(HasFlag(wxNO_BORDER) ? xlen + 4 : xlen + 9); - if (ylen > 0) size.SetHeight(HasFlag(wxNO_BORDER) ? ylen + 2 : ylen + 7); + wxSize size; + + // Initialize to defaults unless both components are specified (at least + // one of them should be, but the other one could be -1). + if ( xlen <= 0 || ylen <= 0 ) + size = DoGetBestSize(); + + // Use extra margin size which works under macOS 10.15 and also add the + // border for consistency with DoGetBestSize() -- we'll remove it below if + // it's not needed. + if ( xlen > 0 ) + size.x = xlen + 4 + TEXTCTRL_BORDER_SIZE; + if ( ylen > 0 ) + size.y = ylen + 2 + TEXTCTRL_BORDER_SIZE; + + if ( HasFlag(wxNO_BORDER) ) + size.DecBy(TEXTCTRL_BORDER_SIZE); + return size; } From 316bbd2189976c34699da69740e8ca2e82903d2b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 28 May 2020 02:02:12 +0200 Subject: [PATCH 5/5] Use the same code in generic wxSpinCtrl in all ports Use Mac version for the other ports too, instead of the weird hardcoded numbers introduced back in 40aa1a7e60 (Implement GetSizeFromTextSize() for wxSpinCtrl., 2012-11-20) which don't make much sense, as they use vertical text control size to determine the horizontal size of the spin control. Do not use the max of spin button height and text control height, however, as we really want to have the same height as just a normal text control. --- src/generic/spinctlg.cpp | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/generic/spinctlg.cpp b/src/generic/spinctlg.cpp index 3b3ccf64d2..9e8e11bd2d 100644 --- a/src/generic/spinctlg.cpp +++ b/src/generic/spinctlg.cpp @@ -278,26 +278,12 @@ wxSize wxSpinCtrlGenericBase::DoGetBestSize() const wxSize wxSpinCtrlGenericBase::DoGetSizeFromTextSize(int xlen, int ylen) const { -#ifdef __WXOSX__ - wxSize sizeBtn = m_spinButton->GetBestSize(), sizeText = m_textCtrl->GetSizeFromTextSize(xlen, ylen); - return wxSize(sizeBtn.GetWidth() + sizeText.GetWidth() + MARGIN, wxMax(sizeBtn.GetHeight(), sizeText.GetHeight())); -#else - wxSize sizeBtn = m_spinButton->GetBestSize(); - wxSize totalS( m_textCtrl->GetBestSize() ); + const wxSize sizeBtn = m_spinButton->GetBestSize(); + const wxSize sizeText = m_textCtrl->GetSizeFromTextSize(xlen, ylen); - wxSize tsize(xlen + sizeBtn.x + MARGIN, totalS.y); -#if defined(__WXMSW__) - tsize.IncBy(4*totalS.y/10 + 4, 0); -#elif defined(__WXGTK__) - tsize.IncBy(totalS.y + 10, 0); -#endif // MSW GTK - - // Check if the user requested a non-standard height. - if ( ylen > 0 ) - tsize.IncBy(0, ylen - GetCharHeight()); - - return tsize; -#endif + // Note that we don't use the button height here, as it can be + // much greater than that of a text control that we want to resemble. + return wxSize(sizeText.x + sizeBtn.x + MARGIN, sizeText.y); } void wxSpinCtrlGenericBase::DoMoveWindow(int x, int y, int width, int height)