[gtk/fix-spin-button-auto-sizing] spinbutton: Bring back auto-sizing



commit df74bcddede62cdcd6ce72770c8672c409c73ce1
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Jun 14 09:12:49 2020 -0400

    spinbutton: Bring back auto-sizing
    
    We lost this when GtkSpinButton was first ported
    to the new editable regime, and then the GtkBoxLayout.
    
    Bring it back, but without text measurement, by overriding
    width-chars for the GtkText inside, and only do it if
    GtkSpinButton::width-chars is unset (ie -1).

 gtk/gtkspinbutton.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
---
diff --git a/gtk/gtkspinbutton.c b/gtk/gtkspinbutton.c
index 5a80cbbe2d..f7a4389e3e 100644
--- a/gtk/gtkspinbutton.c
+++ b/gtk/gtkspinbutton.c
@@ -204,6 +204,8 @@ struct _GtkSpinButton
   gdouble        climb_rate;
   gdouble        timer_step;
 
+  int            width_chars;
+
   GtkOrientation orientation;
 
   guint          digits        : 10;
@@ -299,6 +301,8 @@ static gint gtk_spin_button_default_input  (GtkSpinButton      *spin_button,
                                             gdouble            *new_val);
 static void gtk_spin_button_default_output (GtkSpinButton      *spin_button);
 
+static void gtk_spin_button_update_width_chars (GtkSpinButton *spin_button);
+
 
 static guint spinbutton_signals[LAST_SIGNAL] = {0};
 static GParamSpec *spinbutton_props[NUM_SPINBUTTON_PROPS] = {NULL, };
@@ -635,6 +639,13 @@ gtk_spin_button_set_property (GObject      *object,
 {
   GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
 
+  if (prop_id == PROP_EDITING_CANCELED + 1 + GTK_EDITABLE_PROP_WIDTH_CHARS)
+    {
+      spin_button->width_chars = g_value_get_int (value);
+      gtk_spin_button_update_width_chars (spin_button);
+      return;
+    }
+
   if (gtk_editable_delegate_set_property (object, prop_id, value, pspec))
     return;
 
@@ -697,6 +708,11 @@ gtk_spin_button_get_property (GObject      *object,
 {
   GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
 
+  if (prop_id == PROP_EDITING_CANCELED + 1 + GTK_EDITABLE_PROP_WIDTH_CHARS)
+    {
+      g_value_set_int (value, spin_button->width_chars);
+      return;
+    }
   if (gtk_editable_delegate_get_property (object, prop_id, value, pspec))
     return;
 
@@ -918,6 +934,7 @@ gtk_spin_button_init (GtkSpinButton *spin_button)
   spin_button->numeric = FALSE;
   spin_button->wrap = FALSE;
   spin_button->snap_to_ticks = FALSE;
+  spin_button->width_chars = -1;
 
   spin_button->orientation = GTK_ORIENTATION_HORIZONTAL;
 
@@ -1139,6 +1156,39 @@ gtk_spin_button_format_for_value (GtkSpinButton *spin_button,
   return weed_out_neg_zero (buf, spin_button->digits);
 }
 
+static void
+gtk_spin_button_update_width_chars (GtkSpinButton *spin_button)
+{
+  char *str;
+  double value;
+  int width_chars, c;
+
+  if (spin_button->width_chars == -1)
+    {
+      width_chars = 0;
+
+      value = gtk_adjustment_get_lower (spin_button->adjustment);
+      str = gtk_spin_button_format_for_value (spin_button, value);
+      c = g_utf8_strlen (str, -1);
+      g_free (str);
+
+      width_chars = MAX (width_chars, c);
+
+      value = gtk_adjustment_get_upper (spin_button->adjustment);
+      str = gtk_spin_button_format_for_value (spin_button, value);
+      c = g_utf8_strlen (str, -1);
+      g_free (str);
+
+      width_chars = MAX (width_chars, c);
+
+      width_chars = MIN (width_chars, 10);
+    }
+  else
+    width_chars = spin_button->width_chars;
+
+  gtk_editable_set_width_chars (GTK_EDITABLE (spin_button->entry), width_chars);
+}
+
 static void
 gtk_spin_button_grab_notify (GtkWidget *widget,
                              gboolean   was_grabbed)
@@ -1536,7 +1586,6 @@ gtk_spin_button_default_output (GtkSpinButton *spin_button)
  ***********************************************************
  ***********************************************************/
 
-
 /**
  * gtk_spin_button_configure:
  * @spin_button: a #GtkSpinButton
@@ -1591,6 +1640,8 @@ gtk_spin_button_configure (GtkSpinButton *spin_button,
       g_object_notify_by_pspec (G_OBJECT (spin_button), spinbutton_props[PROP_CLIMB_RATE]);
     }
 
+  gtk_spin_button_update_width_chars (spin_button);
+
   g_object_thaw_notify (G_OBJECT (spin_button));
 
   gtk_spin_button_value_changed (adjustment, spin_button);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]