scale text reformatting



Hi,

Patch to add format_value signal to GtkScale, used to display text
other than the usual digits.

Usage is e.g.:

      static gchar*
      reformat_value (GtkScale *scale,
                      gdouble   value)
      {
        return g_strdup_printf ("-->%g<--", value);
      }

      gtk_signal_connect (GTK_OBJECT (scale),
                          "format_value",
                          GTK_SIGNAL_FUNC (reformat_value),
                          NULL);

Not that exciting. Will commit soon if no complaints.

Havoc

Index: gtkscale.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkscale.c,v
retrieving revision 1.23
diff -u -u -r1.23 gtkscale.c
--- gtkscale.c	2001/04/02 15:51:26	1.23
+++ gtkscale.c	2001/04/13 19:20:23
@@ -27,6 +27,7 @@
 #include <math.h>
 #include "gtkintl.h"
 #include "gtkscale.h"
+#include "gtkmarshal.h"
 
 enum {
   ARG_0,
@@ -35,6 +36,12 @@
   ARG_VALUE_POS
 };
 
+enum {
+  FORMAT_VALUE,
+  LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL];
 
 static void gtk_scale_class_init      (GtkScaleClass *klass);
 static void gtk_scale_init            (GtkScale      *scale);
@@ -78,6 +85,22 @@
   return scale_type;
 }
 
+gboolean
+single_string_accumulator (GSignalInvocationHint *ihint,
+                           GValue                *return_accu,
+                           const GValue          *handler_return,
+                           gpointer               dummy)
+{
+  gboolean continue_emission;
+  gchar *str;
+  
+  str = g_value_get_string (handler_return);
+  g_value_set_string (return_accu, str);
+  continue_emission = str == NULL;
+  
+  return continue_emission;
+}
+
 static void
 gtk_scale_class_init (GtkScaleClass *class)
 {
@@ -104,6 +127,16 @@
 			   GTK_ARG_READWRITE,
 			   ARG_VALUE_POS);
 
+  signals[FORMAT_VALUE] =
+    g_signal_newc ("format_value",
+		   G_TYPE_FROM_CLASS (object_class),
+		   G_SIGNAL_RUN_LAST,
+		   G_STRUCT_OFFSET (GtkScaleClass, format_value),
+		   single_string_accumulator, NULL,
+		   gtk_marshal_STRING__DOUBLE,
+		   G_TYPE_STRING, 1,
+		   G_TYPE_DOUBLE);
+  
   object_class->set_arg = gtk_scale_set_arg;
   object_class->get_arg = gtk_scale_get_arg;
 
@@ -280,58 +313,27 @@
     {
       PangoLayout *layout;
       PangoRectangle logical_rect;
-      gchar buffer[128];
-      gdouble value;
-      gint digits;
-      gint i, j;
+      gchar *txt;
       
       range = GTK_RANGE (scale);
 
       layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
 
-      value = ABS (range->adjustment->lower);
-      if (value == 0) value = 1;
-      digits = log10 (value) + 1;
-      if (digits > 13)
-	digits = 13;
-
-      i = 0;
-      if (range->adjustment->lower < 0)
-        buffer[i++] = '-';
-      for (j = 0; j < digits; j++)
-        buffer[i++] = '0';
-      if (GTK_RANGE (scale)->digits)
-        buffer[i++] = '.';
-      for (j = 0; j < GTK_RANGE (scale)->digits; j++)
-        buffer[i++] = '0';
-      buffer[i] = '\0';
-
-      pango_layout_set_text (layout, buffer, i);
+      txt = _gtk_scale_format_value (scale, range->adjustment->lower);
+      pango_layout_set_text (layout, txt, -1);
+      g_free (txt);
+      
       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
 
       if (width)
 	*width = logical_rect.width;
       if (height)
-	*height = logical_rect.width;
-      
-      value = ABS (range->adjustment->upper);
-      if (value == 0) value = 1;
-      digits = log10 (value) + 1;
-      if (digits > 13)
-        digits = 13;
-
-      i = 0;
-      if (range->adjustment->upper < 0)
-        buffer[i++] = '-';
-      for (j = 0; j < digits; j++)
-        buffer[i++] = '0';
-      if (GTK_RANGE (scale)->digits)
-        buffer[i++] = '.';
-      for (j = 0; j < GTK_RANGE (scale)->digits; j++)
-        buffer[i++] = '0';
-      buffer[i] = '\0';
+	*height = logical_rect.height;
 
-      pango_layout_set_text (layout, buffer, i);
+      txt = _gtk_scale_format_value (scale, range->adjustment->upper);
+      pango_layout_set_text (layout, txt, -1);
+      g_free (txt);
+      
       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
 
       if (width)
@@ -382,4 +384,23 @@
   g_return_if_fail (GTK_IS_SCALE (range));
 
   gtk_scale_draw_value (GTK_SCALE (range));
+}
+
+gchar*
+_gtk_scale_format_value (GtkScale *scale,
+                         gdouble   value)
+{
+  gchar *fmt = NULL;
+
+  g_signal_emit (G_OBJECT (scale),
+                 signals[FORMAT_VALUE],
+                 0,
+                 value,
+                 &fmt);
+
+  if (fmt)
+    return fmt;
+  else
+    return g_strdup_printf ("%0.*f", GTK_RANGE (scale)->digits,
+                            value);
 }
Index: gtkscale.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkscale.h,v
retrieving revision 1.11
diff -u -u -r1.11 gtkscale.h
--- gtkscale.h	2001/04/02 15:51:26	1.11
+++ gtkscale.h	2001/04/13 19:20:23
@@ -61,6 +61,9 @@
   GtkRangeClass parent_class;
 
   gint value_spacing;
+
+  gchar* (* format_value) (GtkRange *range,
+                           gdouble   value);  
   
   void (* draw_value) (GtkScale *scale);
 };
@@ -78,6 +81,9 @@
 				   gint            *height);
 
 void    gtk_scale_draw_value      (GtkScale        *scale);
+
+gchar  *_gtk_scale_format_value   (GtkScale        *scale,
+                                   gdouble          value);
 
 #ifdef __cplusplus
 }
Index: gtkhscale.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkhscale.c,v
retrieving revision 1.31
diff -u -u -r1.31 gtkhscale.c
--- gtkhscale.c	2001/04/02 15:51:25	1.31
+++ gtkhscale.c	2001/04/13 19:20:23
@@ -481,7 +481,6 @@
 {
   GtkStateType state_type;
   GtkWidget *widget;
-  gchar buffer[32];
   gint width, height;
   gint x, y;
   
@@ -494,9 +493,14 @@
     {
       PangoLayout *layout;
       PangoRectangle logical_rect;
+      gchar *txt;
+
+      txt = _gtk_scale_format_value (scale,
+                                     GTK_RANGE (scale)->adjustment->value);
       
-      sprintf (buffer, "%0.*f", GTK_RANGE (scale)->digits, GTK_RANGE (scale)->adjustment->value);
-      layout = gtk_widget_create_pango_layout (widget, buffer);
+      layout = gtk_widget_create_pango_layout (widget, txt);
+      g_free (txt);
+      
       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
 
       switch (scale->value_pos)
Index: gtkvscale.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkvscale.c,v
retrieving revision 1.30
diff -u -u -r1.30 gtkvscale.c
--- gtkvscale.c	2001/04/02 15:51:27	1.30
+++ gtkvscale.c	2001/04/13 19:20:23
@@ -477,7 +477,6 @@
 {
   GtkStateType state_type;
   GtkWidget *widget;
-  gchar buffer[32];
   gint width, height;
   gint x, y;
   
@@ -490,10 +489,14 @@
     {
       PangoLayout *layout;
       PangoRectangle logical_rect;
+      gchar *txt;
       
-      sprintf (buffer, "%0.*f", GTK_RANGE (scale)->digits, GTK_RANGE (scale)->adjustment->value);
-
-      layout = gtk_widget_create_pango_layout (widget, buffer);
+      txt = _gtk_scale_format_value (scale,
+                                     GTK_RANGE (scale)->adjustment->value);
+      
+      layout = gtk_widget_create_pango_layout (widget, txt);
+      g_free (txt);
+      
       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
 
       switch (scale->value_pos)





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