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/generic/spinctlg.cpp b/src/generic/spinctlg.cpp index 7efc9355a4..438ed14da8 100644 --- a/src/generic/spinctlg.cpp +++ b/src/generic/spinctlg.cpp @@ -296,21 +296,12 @@ wxSize wxSpinCtrlGenericBase::DoGetBestSize() const wxSize wxSpinCtrlGenericBase::DoGetSizeFromTextSize(int xlen, int ylen) const { - 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; + // 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) diff --git a/src/osx/textctrl_osx.cpp b/src/osx/textctrl_osx.cpp index 8fd8201d26..4e64b8c530 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 - 6 ; + hText = 22; break ; case wxWINDOW_VARIANT_SMALL : - hText = 19 - 6 ; + hText = 19; break ; case wxWINDOW_VARIANT_MINI : - hText = 15 - 6 ; + hText = 15; break ; default : - hText = 22 - 6; + 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,11 +234,34 @@ wxSize wxTextCtrl::DoGetBestSize() const hText *= 5 ; if ( !HasFlag(wxNO_BORDER) ) - hText += 6 ; + hText += TEXTCTRL_BORDER_SIZE ; return wxSize(wText, hText); } +wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const +{ + 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; +} + bool wxTextCtrl::GetStyle(long position, wxTextAttr& style) { return GetTextPeer()->GetStyle(position, style);