From 7f368872d79055902148ca76e74b11dae72fe65a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 13 Nov 2019 02:52:42 +0100 Subject: [PATCH] Adjust entry width of wxSpinCtrl in wxGTK to its range Ensure that the entry is always (just) big enough to show any value valid in this spin control. This also ensures that GetBestSize() doesn't need to be overridden to use GetSizeFromTextSize() any longer as the best size will be determined correctly by GTK itself. --- include/wx/gtk/spinctrl.h | 4 +++- src/gtk/spinctrl.cpp | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/wx/gtk/spinctrl.h b/include/wx/gtk/spinctrl.h index be7e1013eb..0b96827dd6 100644 --- a/include/wx/gtk/spinctrl.h +++ b/include/wx/gtk/spinctrl.h @@ -71,7 +71,9 @@ protected: void GtkDisableEvents() const; void GtkEnableEvents() const; - virtual wxSize DoGetBestSize() const wxOVERRIDE; + // Update the number of digits used to match our range (and base). + void GtkSetEntryWidth(); + virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen = -1) const wxOVERRIDE; virtual GdkWindow *GTKGetWindow(wxArrayGdkWindows& windows) const wxOVERRIDE; diff --git a/src/gtk/spinctrl.cpp b/src/gtk/spinctrl.cpp index 76711360f9..6ed8512381 100644 --- a/src/gtk/spinctrl.cpp +++ b/src/gtk/spinctrl.cpp @@ -137,6 +137,8 @@ bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id, gtk_entry_set_alignment(GTK_ENTRY(m_widget), align); + GtkSetEntryWidth(); + gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (int)(m_windowStyle & wxSP_WRAP) ); @@ -271,6 +273,8 @@ void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal) gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal); InvalidateBestSize(); + + GtkSetEntryWidth(); } void wxSpinCtrlGTKBase::DoSetIncrement(double inc) @@ -355,11 +359,16 @@ GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const return NULL; } -wxSize wxSpinCtrlGTKBase::DoGetBestSize() const +void wxSpinCtrlGTKBase::GtkSetEntryWidth() { const int minVal = static_cast(DoGetMin()); const int maxVal = static_cast(DoGetMax()); - return wxSpinCtrlImpl::GetBestSize(this, minVal, maxVal, GetBase()); + + gtk_entry_set_width_chars + ( + GTK_ENTRY(m_widget), + wxSpinCtrlImpl::GetMaxValueLength(minVal, maxVal, GetBase()) + ); } wxSize wxSpinCtrlGTKBase::DoGetSizeFromTextSize(int xlen, int ylen) const @@ -476,6 +485,8 @@ bool wxSpinCtrl::SetBase(int base) InvalidateBestSize(); + GtkSetEntryWidth(); + // Update the displayed text after changing the base it uses. SetValue(GetValue());