[patch] gtk_spin_buttons



I brought up the subject couple of days ago on gtk-list about how the
gtk_spin_button_get_value* functions return incorrect values if
they're called from a callback function that's attached to a button
that has had the GTK_CAN_FOCUS flag unset, since the spin button
widget only updates its internal value when it gets a focus_out event.

No gtk maintainers replied but one person did, mentioning that he'd
brought up the same subject a long time ago and never did get a
response. So I figured I'd hack together a patch to fix it and see if
that'd trigger a response. The patch adds code to keep a flag,
text_changed, that indicates whether the value should be updated. It
works for me with gtk 1.1.11 and should patch cleanly against 1.1.12
too.

--- gtkspinbutton_old.h	Mon Jan  4 23:20:35 1999
+++ gtkspinbutton.h	Mon Jan  4 23:22:49 1999
@@ -89,6 +89,7 @@
   guint numeric : 1;
   guint wrap : 1;
   guint snap_to_ticks : 1;
+  guint text_changed : 1;
 };
 
 struct _GtkSpinButtonClass
--- gtkspinbutton_old.c	Mon Jan  4 23:20:28 1999
+++ gtkspinbutton.c	Mon Jan  4 23:34:59 1999
@@ -99,6 +99,7 @@
 					    GdkEventKey        *event);
 static void gtk_spin_button_update         (GtkSpinButton      *spin_button);
 static void gtk_spin_button_activate       (GtkEditable        *editable);
+static void gtk_spin_button_text_changed   (GtkEditable        *editable);
 static void gtk_spin_button_snap           (GtkSpinButton      *spin_button,
 					    gfloat              val);
 static void gtk_spin_button_insert_text    (GtkEditable        *editable,
@@ -210,6 +211,7 @@
 
   editable_class->insert_text = gtk_spin_button_insert_text;
   editable_class->activate = gtk_spin_button_activate;
+  editable_class->changed = gtk_spin_button_text_changed;
 }
 
 static void
@@ -330,6 +332,7 @@
   spin_button->numeric = FALSE;
   spin_button->wrap = FALSE;
   spin_button->snap_to_ticks = FALSE;
+  spin_button->text_changed = FALSE;
 
   gtk_spin_button_set_adjustment (spin_button,
 				  (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
@@ -1124,6 +1127,8 @@
   g_return_if_fail (spin_button != NULL);
   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
 
+  spin_button->text_changed = FALSE;
+  
   val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &error);
 
   if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
@@ -1171,6 +1176,15 @@
 }
 
 static void
+gtk_spin_button_text_changed (GtkEditable *editable)
+{
+  g_return_if_fail (editable != NULL);
+  g_return_if_fail (GTK_IS_SPIN_BUTTON (editable));
+
+  GTK_SPIN_BUTTON(editable)->text_changed = TRUE;
+}
+
+static void
 gtk_spin_button_insert_text (GtkEditable *editable,
 			     const gchar *new_text,
 			     gint         new_text_length,
@@ -1414,6 +1428,9 @@
   g_return_val_if_fail (spin_button != NULL, 0.0);
   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
 
+  if (spin_button->text_changed)
+    gtk_spin_button_update(spin_button);
+  
   return spin_button->adjustment->value;
 }
 
@@ -1424,6 +1441,9 @@
 
   g_return_val_if_fail (spin_button != NULL, 0);
   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
+
+  if (spin_button->text_changed)
+    gtk_spin_button_update(spin_button);
 
   val = spin_button->adjustment->value;
   if (val - floor (val) < ceil (val) - val)


--
Osku Salerma - osku@iki.fi - http://www.iki.fi/osku/
Nostalgia isn't what it used to be.



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