Return fitting size from wxSpinCtrl::GetSizeFromTextSize() in GTK

Previously we added the extent of the text to the preferred size of a
spin button with 0 digits, but this didn't account for the fact that the
control still reserved enough space for 3 digits in this case, and so
the returned size was always too big than the required size by the width
needed to show 3 digits.

Fix this by directly asking for the size required to show the number of
digits corresponding to the given text size.

Note that ideal would be to provide GTK API at wx level directly, i.e.
allow to just specify the number of digits instead of the text extent.
But we still need to fix the already existing GetSizeFromTextSize()
anyhow, so do this for now.

See #18568.
This commit is contained in:
Vadim Zeitlin
2019-11-11 23:54:09 +01:00
parent 7f368872d7
commit b472a791c1

View File

@@ -16,6 +16,7 @@
#ifndef WX_PRECOMP
#include "wx/textctrl.h" // for wxEVT_TEXT
#include "wx/math.h" // wxRound()
#include "wx/utils.h"
#include "wx/wxcrtvararg.h"
#endif
@@ -375,18 +376,24 @@ wxSize wxSpinCtrlGTKBase::DoGetSizeFromTextSize(int xlen, int ylen) const
{
wxASSERT_MSG( m_widget, wxS("GetSizeFromTextSize called before creation") );
// This is a bit stupid as we typically compute xlen by measuring some
// string of digits in the first place, but there doesn't seem to be
// anything better to do (unless we add some GetSizeFromNumberOfDigits()).
const double widthDigit = GetTextExtent("0123456789").GetWidth() / 10.0;
const int numDigits = wxRound(xlen / widthDigit);
const gint widthChars = gtk_entry_get_width_chars(GTK_ENTRY(m_widget));
gtk_entry_set_width_chars(GTK_ENTRY(m_widget), 0);
gtk_entry_set_width_chars(GTK_ENTRY(m_widget), numDigits);
#if GTK_CHECK_VERSION(3,12,0)
gint maxWidthChars = 0;
if ( gtk_check_version(3,12,0) == NULL )
{
maxWidthChars = gtk_entry_get_max_width_chars(GTK_ENTRY(m_widget));
gtk_entry_set_max_width_chars(GTK_ENTRY(m_widget), 0);
gtk_entry_set_max_width_chars(GTK_ENTRY(m_widget), numDigits);
}
#endif // GTK+ 3.12+
wxSize totalS = GTKGetPreferredSize(m_widget);
wxSize tsize = GTKGetPreferredSize(m_widget);
#if GTK_CHECK_VERSION(3,12,0)
if ( gtk_check_version(3,12,0) == NULL )
@@ -394,8 +401,6 @@ wxSize wxSpinCtrlGTKBase::DoGetSizeFromTextSize(int xlen, int ylen) const
#endif // GTK+ 3.12+
gtk_entry_set_width_chars(GTK_ENTRY(m_widget), widthChars);
wxSize tsize(xlen + totalS.x, totalS.y);
// Check if the user requested a non-standard height.
if ( ylen > 0 )
tsize.IncBy(0, ylen - GetCharHeight());