]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkscale.c
Doc fixes
[~andy/gtk] / gtk / gtkscale.c
index 9454cbebdf845a2357f902737ac04e5ad2488276..29e5e6561072466ecb46909c95915de612116711 100644 (file)
  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
  * file for a list of people on the GTK+ Team.  See the ChangeLog
  * files for a list of changes.  These files are distributed with
- * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
  */
 
-#include <config.h>
+#include "config.h"
+
 #include <math.h>
-#include "gtkintl.h"
+#include <stdlib.h>
+
+#include "gdk/gdkkeysyms.h"
 #include "gtkscale.h"
 #include "gtkmarshalers.h"
-#include "gdk/gdkkeysyms.h"
 #include "gtkbindings.h"
 #include "gtkprivate.h"
+#include "gtkintl.h"
 #include "gtkalias.h"
 
 
@@ -64,53 +67,29 @@ enum {
 };
 
 static guint signals[LAST_SIGNAL];
-static GtkRangeClass *parent_class = NULL;
-
-static void gtk_scale_class_init       (GtkScaleClass *klass);
-static void gtk_scale_init             (GtkScale      *scale);
-static void gtk_scale_set_property     (GObject       *object,
-                                        guint          prop_id,
-                                        const GValue  *value,
-                                        GParamSpec    *pspec);
-static void gtk_scale_get_property     (GObject       *object,
-                                        guint          prop_id,
-                                        GValue        *value,
-                                        GParamSpec    *pspec);
-static void gtk_scale_style_set        (GtkWidget     *widget,
-                                        GtkStyle      *previous);
-static void gtk_scale_get_range_border (GtkRange      *range,
-                                        GtkBorder     *border);
-static void gtk_scale_finalize         (GObject       *object);
-static void gtk_scale_screen_changed   (GtkWidget     *widget,
-                                        GdkScreen     *old_screen);
-
-GType
-gtk_scale_get_type (void)
-{
-  static GType scale_type = 0;
 
-  if (!scale_type)
-    {
-      static const GTypeInfo scale_info =
-      {
-       sizeof (GtkScaleClass),
-       NULL,           /* base_init */
-       NULL,           /* base_finalize */
-       (GClassInitFunc) gtk_scale_class_init,
-       NULL,           /* class_finalize */
-       NULL,           /* class_data */
-       sizeof (GtkScale),
-       0,              /* n_preallocs */
-       (GInstanceInitFunc) gtk_scale_init,
-       NULL,           /* value_table */
-      };
-
-      scale_type = g_type_register_static (GTK_TYPE_RANGE, g_intern_static_string ("GtkScale"),
-                                          &scale_info, G_TYPE_FLAG_ABSTRACT);
-    }
-
-  return scale_type;
-}
+static void     gtk_scale_set_property            (GObject        *object,
+                                                   guint           prop_id,
+                                                   const GValue   *value,
+                                                   GParamSpec     *pspec);
+static void     gtk_scale_get_property            (GObject        *object,
+                                                   guint           prop_id,
+                                                   GValue         *value,
+                                                   GParamSpec     *pspec);
+static void     gtk_scale_style_set               (GtkWidget      *widget,
+                                                   GtkStyle       *previous);
+static void     gtk_scale_get_range_border        (GtkRange       *range,
+                                                   GtkBorder      *border);
+static void     gtk_scale_finalize                (GObject        *object);
+static void     gtk_scale_screen_changed          (GtkWidget      *widget,
+                                                   GdkScreen      *old_screen);
+static gboolean gtk_scale_expose                  (GtkWidget      *widget,
+                                                   GdkEventExpose *event);
+static void     gtk_scale_real_get_layout_offsets (GtkScale       *scale,
+                                                   gint           *x,
+                                                   gint           *y);
+
+G_DEFINE_TYPE (GtkScale, gtk_scale, GTK_TYPE_RANGE)
 
 static gboolean
 single_string_accumulator (GSignalInvocationHint *ihint,
@@ -129,9 +108,9 @@ single_string_accumulator (GSignalInvocationHint *ihint,
 }
 
 
-#define add_slider_binding(binding_set, keyval, mask, scroll)          \
-  gtk_binding_entry_add_signal (binding_set, keyval, mask,             \
-                                "move_slider", 1,                      \
+#define add_slider_binding(binding_set, keyval, mask, scroll)              \
+  gtk_binding_entry_add_signal (binding_set, keyval, mask,                 \
+                                I_("move-slider"), 1, \
                                 GTK_TYPE_SCROLL_TYPE, scroll)
 
 static void
@@ -146,19 +125,44 @@ gtk_scale_class_init (GtkScaleClass *class)
   range_class = (GtkRangeClass*) class;
   widget_class = (GtkWidgetClass*) class;
   
-  parent_class = g_type_class_peek_parent (class);
-  
   gobject_class->set_property = gtk_scale_set_property;
   gobject_class->get_property = gtk_scale_get_property;
   gobject_class->finalize = gtk_scale_finalize;
 
   widget_class->style_set = gtk_scale_style_set;
   widget_class->screen_changed = gtk_scale_screen_changed;
+  widget_class->expose_event = gtk_scale_expose;
 
+  range_class->slider_detail = "Xscale";
   range_class->get_range_border = gtk_scale_get_range_border;
-  
+
+  class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
+
+  /**
+   * GtkScale::format-value:
+   * @scale: the object which received the signal
+   * @value: the value to format
+   *
+   * Signal which allows you to change how the scale value is displayed.
+   * Connect a signal handler which returns an allocated string representing 
+   * @value. That string will then be used to display the scale's value.
+   *
+   * Here's an example signal handler which displays a value 1.0 as
+   * with "--&gt;1.0&lt;--".
+   * |[
+   * static gchar*
+   * format_value_callback (GtkScale *scale,
+   *                        gdouble   value)
+   * {
+   *   return g_strdup_printf ("--&gt;&percnt;0.*g&lt;--",
+   *                           gtk_scale_get_digits (scale), value);
+   *  }
+   * ]|
+   *
+   * Return value: allocated string representing @value
+   */
   signals[FORMAT_VALUE] =
-    g_signal_new ("format_value",
+    g_signal_new (I_("format-value"),
                   G_TYPE_FROM_CLASS (gobject_class),
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkScaleClass, format_value),
@@ -333,6 +337,37 @@ gtk_scale_class_init (GtkScaleClass *class)
   g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
 }
 
+static void
+gtk_scale_orientation_notify (GtkRange         *range,
+                              const GParamSpec *pspec)
+{
+  range->flippable = (range->orientation == GTK_ORIENTATION_HORIZONTAL);
+}
+
+static void
+gtk_scale_init (GtkScale *scale)
+{
+  GtkRange *range = GTK_RANGE (scale);
+
+  GTK_WIDGET_SET_FLAGS (scale, GTK_CAN_FOCUS);
+
+  range->slider_size_fixed = TRUE;
+  range->has_stepper_a = FALSE;
+  range->has_stepper_b = FALSE;
+  range->has_stepper_c = FALSE;
+  range->has_stepper_d = FALSE;
+
+  scale->draw_value = TRUE;
+  scale->value_pos = GTK_POS_TOP;
+  scale->digits = 1;
+  range->round_digits = scale->digits;
+
+  gtk_scale_orientation_notify (range, NULL);
+  g_signal_connect (scale, "notify::orientation",
+                    G_CALLBACK (gtk_scale_orientation_notify),
+                    NULL);
+}
+
 static void
 gtk_scale_set_property (GObject      *object,
                        guint         prop_id,
@@ -387,27 +422,94 @@ gtk_scale_get_property (GObject      *object,
     }
 }
 
-static void
-gtk_scale_init (GtkScale *scale)
+/**
+ * gtk_scale_new:
+ * @orientation: the scale's orientation.
+ * @adjustment: the #GtkAdjustment which sets the range of the scale, or
+ *              %NULL to create a new adjustment.
+ *
+ * Creates a new #GtkScale.
+ *
+ * Return value: a new #GtkScale
+ *
+ * Since: 2.16
+ **/
+GtkWidget *
+gtk_scale_new (GtkOrientation  orientation,
+               GtkAdjustment  *adjustment)
 {
-  GtkRange *range;
+  g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
+                        NULL);
 
-  range = GTK_RANGE (scale);
-  
-  GTK_WIDGET_SET_FLAGS (scale, GTK_CAN_FOCUS);
+  return g_object_new (GTK_TYPE_SCALE,
+                       "orientation", orientation,
+                       "adjustment",  adjustment,
+                       NULL);
+}
 
-  range->slider_size_fixed = TRUE;
-  range->has_stepper_a = FALSE;
-  range->has_stepper_b = FALSE;
-  range->has_stepper_c = FALSE;
-  range->has_stepper_d = FALSE;
-  
-  scale->draw_value = TRUE;
-  scale->value_pos = GTK_POS_TOP;
-  scale->digits = 1;
-  range->round_digits = scale->digits;
+/**
+ * gtk_scale_new_with_range:
+ * @orientation: the scale's orientation.
+ * @min: minimum value
+ * @max: maximum value
+ * @step: step increment (tick size) used with keyboard shortcuts
+ *
+ * Creates a new scale widget with the given orientation that lets the
+ * user input a number between @min and @max (including @min and @max)
+ * with the increment @step.  @step must be nonzero; it's the distance
+ * the slider moves when using the arrow keys to adjust the scale
+ * value.
+ *
+ * Note that the way in which the precision is derived works best if @step
+ * is a power of ten. If the resulting precision is not suitable for your
+ * needs, use gtk_scale_set_digits() to correct it.
+ *
+ * Return value: a new #GtkScale
+ *
+ * Since: 2.16
+ **/
+GtkWidget *
+gtk_scale_new_with_range (GtkOrientation orientation,
+                          gdouble        min,
+                          gdouble        max,
+                          gdouble        step)
+{
+  GtkObject *adj;
+  gint digits;
+
+  g_return_val_if_fail (min < max, NULL);
+  g_return_val_if_fail (step != 0.0, NULL);
+
+  adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
+
+  if (fabs (step) >= 1.0 || step == 0.0)
+    {
+      digits = 0;
+    }
+  else
+    {
+      digits = abs ((gint) floor (log10 (fabs (step))));
+      if (digits > 5)
+        digits = 5;
+    }
+
+  return g_object_new (GTK_TYPE_SCALE,
+                       "orientation", orientation,
+                       "adjustment",  adj,
+                       "digits",      digits,
+                       NULL);
 }
 
+/**
+ * gtk_scale_set_digits:
+ * @scale: a #GtkScale
+ * @digits: the number of decimal places to display, 
+ *     e.g. use 1 to display 1.0, 2 to display 1.00, etc
+ * 
+ * Sets the number of decimal places that are displayed in the value.
+ * Also causes the value of the adjustment to be rounded off to this
+ * number of digits, so the retrieved value matches the value the user saw.
+ */
 void
 gtk_scale_set_digits (GtkScale *scale,
                      gint      digits)
@@ -433,6 +535,14 @@ gtk_scale_set_digits (GtkScale *scale,
     }
 }
 
+/**
+ * gtk_scale_get_digits:
+ * @scale: a #GtkScale
+ *
+ * Gets the number of decimal places that are displayed in the value.
+ *
+ * Returns: the number of decimal places that are displayed
+ */
 gint
 gtk_scale_get_digits (GtkScale *scale)
 {
@@ -441,6 +551,14 @@ gtk_scale_get_digits (GtkScale *scale)
   return scale->digits;
 }
 
+/**
+ * gtk_scale_set_draw_value:
+ * @scale: a #GtkScale
+ * @draw_value: %TRUE to draw the value
+ * 
+ * Specifies whether the current value is displayed as a string next 
+ * to the slider.
+ */
 void
 gtk_scale_set_draw_value (GtkScale *scale,
                          gboolean  draw_value)
@@ -465,6 +583,15 @@ gtk_scale_set_draw_value (GtkScale *scale,
     }
 }
 
+/**
+ * gtk_scale_get_draw_value:
+ * @scale: a #GtkScale
+ *
+ * Returns whether the current value is displayed as a string 
+ * next to the slider.
+ *
+ * Returns: whether the current value is displayed as a string
+ */
 gboolean
 gtk_scale_get_draw_value (GtkScale *scale)
 {
@@ -473,6 +600,13 @@ gtk_scale_get_draw_value (GtkScale *scale)
   return scale->draw_value;
 }
 
+/**
+ * gtk_scale_set_value_pos:
+ * @scale: a #GtkScale
+ * @pos: the position in which the current value is displayed
+ * 
+ * Sets the position in which the current value is displayed.
+ */
 void
 gtk_scale_set_value_pos (GtkScale        *scale,
                         GtkPositionType  pos)
@@ -491,6 +625,14 @@ gtk_scale_set_value_pos (GtkScale        *scale,
     }
 }
 
+/**
+ * gtk_scale_get_value_pos:
+ * @scale: a #GtkScale
+ *
+ * Gets the position in which the current value is displayed.
+ *
+ * Returns: the position in which the current value is displayed
+ */
 GtkPositionType
 gtk_scale_get_value_pos (GtkScale *scale)
 {
@@ -611,7 +753,7 @@ gtk_scale_style_set (GtkWidget *widget,
   
   _gtk_scale_clear_layout (GTK_SCALE (widget));
 
-  (* GTK_WIDGET_CLASS (parent_class)->style_set) (widget, previous);
+  GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_set (widget, previous);
 }
 
 static void
@@ -621,16 +763,148 @@ gtk_scale_screen_changed (GtkWidget *widget,
   _gtk_scale_clear_layout (GTK_SCALE (widget));
 }
 
+static gboolean
+gtk_scale_expose (GtkWidget      *widget,
+                  GdkEventExpose *event)
+{
+  GtkScale *scale = GTK_SCALE (widget);
+
+  /* We need to chain up _first_ so the various geometry members of
+   * GtkRange struct are updated.
+   */
+  GTK_WIDGET_CLASS (gtk_scale_parent_class)->expose_event (widget, event);
+
+  if (scale->draw_value)
+    {
+      GtkRange *range = GTK_RANGE (scale);
+      PangoLayout *layout;
+      gint x, y;
+      GtkStateType state_type;
+
+      layout = gtk_scale_get_layout (scale);
+      gtk_scale_get_layout_offsets (scale, &x, &y);
+
+      state_type = GTK_STATE_NORMAL;
+      if (!GTK_WIDGET_IS_SENSITIVE (scale))
+        state_type = GTK_STATE_INSENSITIVE;
+
+      gtk_paint_layout (widget->style,
+                        widget->window,
+                        state_type,
+                       FALSE,
+                        NULL,
+                        widget,
+                        range->orientation == GTK_ORIENTATION_HORIZONTAL ?
+                        "hscale" : "vscale",
+                        x, y,
+                        layout);
+
+    }
+
+  return FALSE;
+}
+
+static void
+gtk_scale_real_get_layout_offsets (GtkScale *scale,
+                                   gint     *x,
+                                   gint     *y)
+{
+  GtkWidget *widget = GTK_WIDGET (scale);
+  GtkRange *range = GTK_RANGE (widget);
+  PangoLayout *layout = gtk_scale_get_layout (scale);
+  PangoRectangle logical_rect;
+  gint value_spacing;
+
+  if (!layout)
+    {
+      *x = 0;
+      *y = 0;
+
+      return;
+    }
+
+  gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
+
+  pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
+
+  if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
+    {
+      switch (scale->value_pos)
+        {
+        case GTK_POS_LEFT:
+          *x = range->range_rect.x - value_spacing - logical_rect.width;
+          *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
+          break;
+
+        case GTK_POS_RIGHT:
+          *x = range->range_rect.x + range->range_rect.width + value_spacing;
+          *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
+          break;
+
+        case GTK_POS_TOP:
+          *x = range->slider_start +
+            (range->slider_end - range->slider_start - logical_rect.width) / 2;
+          *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
+          *y = range->range_rect.y - logical_rect.height - value_spacing;
+          break;
+
+        case GTK_POS_BOTTOM:
+          *x = range->slider_start +
+            (range->slider_end - range->slider_start - logical_rect.width) / 2;
+          *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
+          *y = range->range_rect.y + range->range_rect.height + value_spacing;
+          break;
+
+        default:
+          g_return_if_reached ();
+          break;
+        }
+    }
+  else
+    {
+      switch (scale->value_pos)
+        {
+        case GTK_POS_LEFT:
+          *x = range->range_rect.x - logical_rect.width - value_spacing;
+          *y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
+          *y = CLAMP (*y, 0, widget->allocation.height - logical_rect.height);
+          break;
+
+        case GTK_POS_RIGHT:
+          *x = range->range_rect.x + range->range_rect.width + value_spacing;
+          *y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
+          *y = CLAMP (*y, 0, widget->allocation.height - logical_rect.height);
+          break;
+
+        case GTK_POS_TOP:
+          *x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
+          *y = range->range_rect.y - logical_rect.height - value_spacing;
+          break;
+
+        case GTK_POS_BOTTOM:
+          *x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
+          *y = range->range_rect.y + range->range_rect.height + value_spacing;
+          break;
+
+        default:
+          g_return_if_reached ();
+        }
+    }
+
+  *x += widget->allocation.x;
+  *y += widget->allocation.y;
+}
+
 /**
  * _gtk_scale_format_value:
  * @scale: a #GtkScale
  * @value: adjustment value
  * 
- * Emits "format_value" signal to format the value, if no user
- * signal handlers, falls back to a default format.
+ * Emits #GtkScale::format-value signal to format the value, 
+ * if no user signal handlers, falls back to a default format.
  * 
  * Return value: formatted value
- **/
+ */
 gchar*
 _gtk_scale_format_value (GtkScale *scale,
                          gdouble   value)
@@ -646,36 +920,33 @@ _gtk_scale_format_value (GtkScale *scale,
   if (fmt)
     return fmt;
   else
-    return g_strdup_printf ("%0.*f", scale->digits,
-                            value);
+    /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
+    return g_strdup_printf ("\342\200\216%0.*f", scale->digits, value);
 }
 
 static void
 gtk_scale_finalize (GObject *object)
 {
-  GtkScale *scale;
-
-  g_return_if_fail (GTK_IS_SCALE (object));
-
-  scale = GTK_SCALE (object);
+  GtkScale *scale = GTK_SCALE (object);
 
   _gtk_scale_clear_layout (scale);
 
-  G_OBJECT_CLASS (parent_class)->finalize (object);
+  G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
 }
 
 /**
  * gtk_scale_get_layout:
  * @scale: A #GtkScale
  *
- * Gets the #PangoLayout used to display the scale. The returned object
- * is owned by the scale so does not need to be freed by the caller. 
+ * Gets the #PangoLayout used to display the scale. 
+ * The returned object is owned by the scale so does 
+ * not need to be freed by the caller. 
  *
- * Return value: the #PangoLayout for this scale, or %NULL if the draw_value property
- *    is %FALSE.
+ * Return value: the #PangoLayout for this scale, or %NULL 
+ *    if the #GtkScale:draw-value property is %FALSE.
  *   
  * Since: 2.4
- **/
+ */
 PangoLayout *
 gtk_scale_get_layout (GtkScale *scale)
 {
@@ -707,22 +978,23 @@ gtk_scale_get_layout (GtkScale *scale)
  * @x: location to store X offset of layout, or %NULL
  * @y: location to store Y offset of layout, or %NULL
  *
- * Obtains the coordinates where the scale will draw the #PangoLayout
- * representing the text in the scale. Remember
+ * Obtains the coordinates where the scale will draw the 
+ * #PangoLayout representing the text in the scale. Remember
  * when using the #PangoLayout function you need to convert to
  * and from pixels using PANGO_PIXELS() or #PANGO_SCALE. 
  *
- * If the draw_value property is %FALSE, the return values are 
- * undefined.
+ * If the #GtkScale:draw-value property is %FALSE, the return 
+ * values are undefined.
  *
  * Since: 2.4
- **/
+ */
 void 
 gtk_scale_get_layout_offsets (GtkScale *scale,
                               gint     *x,
                               gint     *y)
 {
-  gint local_x, local_y;
+  gint local_x = 0; 
+  gint local_y = 0;
 
   g_return_if_fail (GTK_IS_SCALE (scale));
 
@@ -736,7 +1008,8 @@ gtk_scale_get_layout_offsets (GtkScale *scale,
     *y = local_y;
 }
 
-void _gtk_scale_clear_layout (GtkScale *scale)
+void
+_gtk_scale_clear_layout (GtkScale *scale)
 {
   GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);