Patch: Bug 50278



Here's a patch for the SpinButton convenience API enhancements.

Let me know if it's broke-ass or if I can commit. 

I tried it out with testgtk using some funky step values and it seems to
pick proper digits values and such. 

Mike
Index: gtk/gtkspinbutton.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkspinbutton.c,v
retrieving revision 1.50
diff -u -r1.50 gtkspinbutton.c
--- gtk/gtkspinbutton.c	2000/12/13 01:34:40	1.50
+++ gtk/gtkspinbutton.c	2001/03/08 03:55:22
@@ -64,6 +64,7 @@
 {
   INPUT,
   OUTPUT,
+  VALUE_CHANGED,
   LAST_SIGNAL
 };
 
@@ -245,6 +246,14 @@
 		    GTK_SIGNAL_OFFSET (GtkSpinButtonClass, output),
 		    gtk_marshal_BOOLEAN__VOID,
 		    GTK_TYPE_BOOL, 0);
+
+  spinbutton_signals[VALUE_CHANGED] =
+    gtk_signal_new ("value_changed",
+		    GTK_RUN_LAST,
+		    GTK_CLASS_TYPE (object_class),
+		    GTK_SIGNAL_OFFSET (GtkSpinButtonClass, value_changed),
+		    gtk_marshal_VOID__VOID,
+		    GTK_TYPE_NONE, 0);
 }
 
 static void
@@ -1338,6 +1347,11 @@
   return FALSE;
 }
 
+static void
+adjustment_value_changed_cb (GtkAdjustment *adj, GtkSpinButton *spin)
+{
+  gtk_signal_emit (GTK_OBJECT (spin), spinbutton_signals[VALUE_CHANGED]);
+}
 
 /***********************************************************
  ***********************************************************
@@ -1363,6 +1377,11 @@
 
   spin_button->digits = digits;
   spin_button->climb_rate = climb_rate;
+
+  gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
+		      GTK_SIGNAL_FUNC (adjustment_value_changed_cb), 
+		      spin_button);
+
   gtk_adjustment_value_changed (adjustment);
 }
 
@@ -1384,6 +1403,36 @@
   return GTK_WIDGET (spin);
 }
 
+GtkWidget *
+gtk_spin_button_new_with_range (gfloat value,
+		     		gfloat min,
+		     		gfloat max,
+		     		gfloat step,
+		     		gfloat page)
+{
+  GtkSpinButton *spin;
+  GtkObject *adj;
+  gint digits;
+
+  spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
+
+  adj = gtk_adjustment_new (value, min, max, step, page, page);
+
+  if (fabs (step) >= 1.0)
+    digits = 0;
+  else {
+    digits = abs ((gint) floor (log10 (fabs (step))));
+    if (digits > 6)
+      digits = 6;
+  }
+
+  gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
+
+  gtk_spin_button_set_numeric (spin, TRUE);
+
+  return GTK_WIDGET (spin);
+}
+
 /* Callback used when the spin button's adjustment changes.  We need to redraw
  * the arrows when the adjustment's range changes.
  */
@@ -1449,6 +1498,30 @@
       spin_button->digits = digits;
       gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
     }
+}
+
+void 
+gtk_spin_button_set_increment (GtkSpinButton *spin_button, 
+			       gfloat         step,
+			       gfloat	      page)
+{
+  g_return_if_fail (spin_button != NULL);
+  g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
+
+  spin_button->adjustment->step_increment = step;
+  spin_button->adjustment->page_increment = page;
+}
+
+void 
+gtk_spin_button_set_range (GtkSpinButton *spin_button, 
+			   gfloat         min,
+			   gfloat	  max)
+{
+  g_return_if_fail (spin_button != NULL);
+  g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
+
+  spin_button->adjustment->lower = min;
+  spin_button->adjustment->upper = max;
 }
 
 gfloat
Index: gtk/gtkspinbutton.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkspinbutton.h,v
retrieving revision 1.21
diff -u -r1.21 gtkspinbutton.h
--- gtk/gtkspinbutton.h	2000/09/07 18:07:57	1.21
+++ gtk/gtkspinbutton.h	2001/03/08 03:55:23
@@ -104,9 +104,11 @@
 {
   GtkEntryClass parent_class;
 
-  gint (*input)  (GtkSpinButton *spin_button,
-		  gfloat        *new_value);
-  gint (*output) (GtkSpinButton *spin_button);
+  gint (*input)  	(GtkSpinButton *spin_button,
+		  	 gfloat        *new_value);
+  gint (*output) 	(GtkSpinButton *spin_button);
+  void (*value_changed) (GtkSpinButton *spin_button);
+
 };
 
 
@@ -121,6 +123,12 @@
 						    gfloat	    climb_rate,
 						    guint	    digits);
 
+GtkWidget*	gtk_spin_button_new_with_range	   (gfloat  value,
+						    gfloat  min,
+						    gfloat  max,
+						    gfloat  step,
+						    gfloat  page);
+
 void		gtk_spin_button_set_adjustment	   (GtkSpinButton  *spin_button,
 						    GtkAdjustment  *adjustment);
 
@@ -128,6 +136,14 @@
 
 void		gtk_spin_button_set_digits	   (GtkSpinButton  *spin_button,
 						    guint	    digits);
+
+void		gtk_spin_button_set_increment	   (GtkSpinButton  *spin_button,
+						    gfloat	    step,
+						    gfloat	    page);
+
+void		gtk_spin_button_set_range	   (GtkSpinButton  *spin_button,
+						    gfloat	    min,
+						    gfloat	    max);
 
 gfloat		gtk_spin_button_get_value_as_float (GtkSpinButton  *spin_button);
 
Index: gtk/testgtk.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/testgtk.c,v
retrieving revision 1.222
diff -u -r1.222 testgtk.c
--- gtk/testgtk.c	2001/03/02 20:02:17	1.222
+++ gtk/testgtk.c	2001/03/08 03:55:44
@@ -3623,6 +3623,23 @@
   gtk_label_set_text (label, buf);
 }
 
+static void
+get_spin_value (GtkWidget *widget, gpointer data)
+{
+  gchar buf[32];
+  GtkLabel *label;
+  GtkSpinButton *spin;
+
+  spin = GTK_SPIN_BUTTON (widget);
+  label = GTK_LABEL (gtk_object_get_user_data (GTK_OBJECT (widget)));
+  if (GPOINTER_TO_INT (data) == 1)
+    sprintf (buf, "%d", gtk_spin_button_get_value_as_int (spin));
+  else
+    sprintf (buf, "%0.*f", spin->digits,
+	     gtk_spin_button_get_value_as_float (spin));
+  gtk_label_set_text (label, buf);
+}
+
 static gint
 spin_button_time_output_func (GtkSpinButton *spin_button)
 {
@@ -3903,6 +3920,23 @@
 
       gtk_box_pack_start (GTK_BOX (vbox), val_label, TRUE, TRUE, 0);
       gtk_label_set_text (GTK_LABEL (val_label), "0");
+
+      frame = gtk_frame_new ("Using Convenience Constructor");
+      gtk_box_pack_start (GTK_BOX (main_vbox), frame, TRUE, TRUE, 0);
+      
+      hbox = gtk_hbox_new (FALSE, 0);
+      gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
+      gtk_container_add (GTK_CONTAINER (frame), hbox);
+
+      val_label = gtk_label_new ("0.0");
+
+      spinner = gtk_spin_button_new_with_range (0.0, 0.0, 10.0, 0.09, 1.5);
+      gtk_object_set_user_data (GTK_OBJECT (spinner), val_label);
+      gtk_signal_connect (GTK_OBJECT (spinner), "value_changed",
+			  GTK_SIGNAL_FUNC (get_spin_value),
+			  GINT_TO_POINTER (2));
+      gtk_box_pack_start (GTK_BOX (hbox), spinner, TRUE, TRUE, 5);
+      gtk_box_pack_start (GTK_BOX (hbox), val_label, TRUE, TRUE, 5);
 
       hbox = gtk_hbox_new (FALSE, 0);
       gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, TRUE, 0);


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