Fix wxSpinCtrl best size calculation for GTK+ 3

Don't hard code the width of spin buttons in wxGTK, this more or less worked
for GTK+ 2 (at least with the default theme, it was perfectly possible that it
also didn't work with some other ones), but not at all with GTK+ 3 where the
buttons are much wider than the hardcoded value.

Instead, do the same thing as wxSpinButton wxGTK implementation already does
and force the GTK+ widget to compute its preferred size as we need it, i.e.
without taking the text width into account, by forcing the width of the text
entry to 0.

Notice that for GTK+ 3.12+ we also need to set the max width to 0 as well to
prevent the entry from making itself big enough for that many characters,
see #17051.
This commit is contained in:
Vadim Zeitlin
2016-04-03 17:11:09 +02:00
parent d8b92580bc
commit c1cd1e01b5

View File

@@ -362,24 +362,24 @@ wxSize wxSpinCtrlGTKBase::DoGetSizeFromTextSize(int xlen, int ylen) const
{ {
wxASSERT_MSG( m_widget, wxS("GetSizeFromTextSize called before creation") ); wxASSERT_MSG( m_widget, wxS("GetSizeFromTextSize called before creation") );
// Set an as small as possible size for the control, so preferred sizes const gint widthChars = gtk_entry_get_width_chars(GTK_ENTRY(m_widget));
// return "natural" sizes, not taking into account the previous ones (which gtk_entry_set_width_chars(GTK_ENTRY(m_widget), 0);
// seems to be GTK+3 behaviour) #if GTK_CHECK_VERSION(3,12,0)
gtk_widget_set_size_request(m_widget, 0, 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);
}
#endif // GTK+ 3.12+
// Both Gtk+2 and Gtk+3 use current value/range to measure control's width.
// So, we can't ask Gtk+ for its width. Instead, we used hardcoded values.
// Returned height is OK
wxSize totalS = GTKGetPreferredSize(m_widget); wxSize totalS = GTKGetPreferredSize(m_widget);
#if GTK_CHECK_VERSION(3,4,0) #if GTK_CHECK_VERSION(3,12,0)
// two buttons in horizontal if ( gtk_check_version(3,12,0) == NULL )
totalS.x = 46 + 15; // margins included gtk_entry_set_max_width_chars(GTK_ENTRY(m_widget), maxWidthChars);
#else #endif // GTK+ 3.12+
// two small buttons in vertical gtk_entry_set_width_chars(GTK_ENTRY(m_widget), widthChars);
totalS.x = GetFont().GetPixelSize().y + 13; // margins included
#endif
wxSize tsize(xlen + totalS.x, totalS.y); wxSize tsize(xlen + totalS.x, totalS.y);