1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 2001 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkscaleprivate.h"
33 #include "gtkadjustment.h"
34 #include "gtkbindings.h"
35 #include "gtkbuildable.h"
36 #include "gtkbuilderprivate.h"
37 #include "gtkiconfactory.h"
38 #include "gtkicontheme.h"
40 #include "gtkmarshalers.h"
41 #include "gtkorientable.h"
42 #include "gtkprivate.h"
43 #include "gtktypebuiltins.h"
45 #include "a11y/gtkscaleaccessible.h"
50 * @Short_description: A slider widget for selecting a value from a range
53 * A GtkScale is a slider control used to select a numeric value.
54 * To use it, you'll probably want to investigate the methods on
55 * its base class, #GtkRange, in addition to the methods for GtkScale itself.
56 * To set the value of a scale, you would normally use gtk_range_set_value().
57 * To detect changes to the value, you would normally use the
58 * #GtkRange::value-changed signal.
60 * Note that using the same upper and lower bounds for the #GtkScale (through
61 * the #GtkRange methods) will hide the slider itself. This is useful for
62 * applications that want to show an undeterminate value on the scale, without
63 * changing the layout of the application (such as movie or music players).
65 * <refsect2 id="GtkScale-BUILDER-UI"><title>GtkScale as GtkBuildable</title>
66 * GtkScale supports a custom <marks> element, which
67 * can contain multiple <mark> elements. The "value" and "position"
68 * attributes have the same meaning as gtk_scale_add_mark() parameters of the
69 * same name. If the element is not empty, its content is taken as the markup
70 * to show at the mark. It can be translated with the usual "translatable and
71 * "context" attributes.
76 #define MAX_DIGITS (64) /* don't change this,
77 * a) you don't need to and
78 * b) you might cause buffer owerflows in
79 * unrelated code portions otherwise
82 typedef struct _GtkScaleMark GtkScaleMark;
84 struct _GtkScalePrivate
100 GtkPositionType position; /* always GTK_POS_TOP or GTK_POS_BOTTOM */
116 static guint signals[LAST_SIGNAL];
118 static void gtk_scale_set_property (GObject *object,
122 static void gtk_scale_get_property (GObject *object,
126 static void gtk_scale_get_preferred_width (GtkWidget *widget,
129 static void gtk_scale_get_preferred_height (GtkWidget *widget,
132 static void gtk_scale_style_updated (GtkWidget *widget);
133 static void gtk_scale_get_range_border (GtkRange *range,
135 static void gtk_scale_get_mark_label_size (GtkScale *scale,
136 GtkPositionType position,
143 static void gtk_scale_finalize (GObject *object);
144 static void gtk_scale_screen_changed (GtkWidget *widget,
145 GdkScreen *old_screen);
146 static gboolean gtk_scale_draw (GtkWidget *widget,
148 static void gtk_scale_real_get_layout_offsets (GtkScale *scale,
151 static void gtk_scale_buildable_interface_init (GtkBuildableIface *iface);
152 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
155 const gchar *tagname,
156 GMarkupParser *parser,
158 static void gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
161 const gchar *tagname,
165 G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
166 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
167 gtk_scale_buildable_interface_init))
170 compare_marks (gconstpointer a, gconstpointer b, gpointer data)
172 gboolean inverted = GPOINTER_TO_INT (data);
174 const GtkScaleMark *ma, *mb;
176 val = inverted ? -1 : 1;
180 return (ma->value > mb->value) ? val : ((ma->value < mb->value) ? -val : 0);
184 gtk_scale_notify (GObject *object,
187 if (strcmp (pspec->name, "orientation") == 0)
189 GtkOrientation orientation;
191 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (object));
192 gtk_range_set_flippable (GTK_RANGE (object),
193 orientation == GTK_ORIENTATION_HORIZONTAL);
195 else if (strcmp (pspec->name, "inverted") == 0)
197 GtkScale *scale = GTK_SCALE (object);
203 scale->priv->marks = g_slist_sort_with_data (scale->priv->marks,
205 GINT_TO_POINTER (gtk_range_get_inverted (GTK_RANGE (scale))));
207 n = g_slist_length (scale->priv->marks);
208 values = g_new (gdouble, n);
209 for (m = scale->priv->marks, i = 0; m; m = m->next, i++)
212 values[i] = mark->value;
215 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
220 if (G_OBJECT_CLASS (gtk_scale_parent_class)->notify)
221 G_OBJECT_CLASS (gtk_scale_parent_class)->notify (object, pspec);
225 #define add_slider_binding(binding_set, keyval, mask, scroll) \
226 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
227 I_("move-slider"), 1, \
228 GTK_TYPE_SCROLL_TYPE, scroll)
231 gtk_scale_class_init (GtkScaleClass *class)
233 GObjectClass *gobject_class;
234 GtkWidgetClass *widget_class;
235 GtkRangeClass *range_class;
236 GtkBindingSet *binding_set;
238 gobject_class = G_OBJECT_CLASS (class);
239 range_class = (GtkRangeClass*) class;
240 widget_class = (GtkWidgetClass*) class;
242 gobject_class->set_property = gtk_scale_set_property;
243 gobject_class->get_property = gtk_scale_get_property;
244 gobject_class->notify = gtk_scale_notify;
245 gobject_class->finalize = gtk_scale_finalize;
247 widget_class->style_updated = gtk_scale_style_updated;
248 widget_class->screen_changed = gtk_scale_screen_changed;
249 widget_class->draw = gtk_scale_draw;
250 widget_class->get_preferred_width = gtk_scale_get_preferred_width;
251 widget_class->get_preferred_height = gtk_scale_get_preferred_height;
253 range_class->slider_detail = "Xscale";
254 range_class->get_range_border = gtk_scale_get_range_border;
256 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
259 * GtkScale::format-value:
260 * @scale: the object which received the signal
261 * @value: the value to format
263 * Signal which allows you to change how the scale value is displayed.
264 * Connect a signal handler which returns an allocated string representing
265 * @value. That string will then be used to display the scale's value.
267 * Here's an example signal handler which displays a value 1.0 as
268 * with "-->1.0<--".
271 * format_value_callback (GtkScale *scale,
274 * return g_strdup_printf ("-->%0.*g<--",
275 * gtk_scale_get_digits (scale), value);
279 * Return value: allocated string representing @value
281 signals[FORMAT_VALUE] =
282 g_signal_new (I_("format-value"),
283 G_TYPE_FROM_CLASS (gobject_class),
285 G_STRUCT_OFFSET (GtkScaleClass, format_value),
286 _gtk_single_string_accumulator, NULL,
287 _gtk_marshal_STRING__DOUBLE,
291 g_object_class_install_property (gobject_class,
293 g_param_spec_int ("digits",
295 P_("The number of decimal places that are displayed in the value"),
299 GTK_PARAM_READWRITE));
301 g_object_class_install_property (gobject_class,
303 g_param_spec_boolean ("draw-value",
305 P_("Whether the current value is displayed as a string next to the slider"),
307 GTK_PARAM_READWRITE));
309 g_object_class_install_property (gobject_class,
311 g_param_spec_boolean ("has-origin",
313 P_("Whether the scale has an origin"),
315 GTK_PARAM_READWRITE));
317 g_object_class_install_property (gobject_class,
319 g_param_spec_enum ("value-pos",
320 P_("Value Position"),
321 P_("The position in which the current value is displayed"),
322 GTK_TYPE_POSITION_TYPE,
324 GTK_PARAM_READWRITE));
326 gtk_widget_class_install_style_property (widget_class,
327 g_param_spec_int ("slider-length",
329 P_("Length of scale's slider"),
333 GTK_PARAM_READABLE));
335 gtk_widget_class_install_style_property (widget_class,
336 g_param_spec_int ("value-spacing",
338 P_("Space between value text and the slider/trough area"),
342 GTK_PARAM_READABLE));
344 /* All bindings (even arrow keys) are on both h/v scale, because
345 * blind users etc. don't care about scale orientation.
348 binding_set = gtk_binding_set_by_class (class);
350 add_slider_binding (binding_set, GDK_KEY_Left, 0,
351 GTK_SCROLL_STEP_LEFT);
353 add_slider_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
354 GTK_SCROLL_PAGE_LEFT);
356 add_slider_binding (binding_set, GDK_KEY_KP_Left, 0,
357 GTK_SCROLL_STEP_LEFT);
359 add_slider_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
360 GTK_SCROLL_PAGE_LEFT);
362 add_slider_binding (binding_set, GDK_KEY_Right, 0,
363 GTK_SCROLL_STEP_RIGHT);
365 add_slider_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
366 GTK_SCROLL_PAGE_RIGHT);
368 add_slider_binding (binding_set, GDK_KEY_KP_Right, 0,
369 GTK_SCROLL_STEP_RIGHT);
371 add_slider_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
372 GTK_SCROLL_PAGE_RIGHT);
374 add_slider_binding (binding_set, GDK_KEY_Up, 0,
377 add_slider_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
380 add_slider_binding (binding_set, GDK_KEY_KP_Up, 0,
383 add_slider_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
386 add_slider_binding (binding_set, GDK_KEY_Down, 0,
387 GTK_SCROLL_STEP_DOWN);
389 add_slider_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
390 GTK_SCROLL_PAGE_DOWN);
392 add_slider_binding (binding_set, GDK_KEY_KP_Down, 0,
393 GTK_SCROLL_STEP_DOWN);
395 add_slider_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
396 GTK_SCROLL_PAGE_DOWN);
398 add_slider_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
399 GTK_SCROLL_PAGE_LEFT);
401 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
402 GTK_SCROLL_PAGE_LEFT);
404 add_slider_binding (binding_set, GDK_KEY_Page_Up, 0,
407 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
410 add_slider_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
411 GTK_SCROLL_PAGE_RIGHT);
413 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
414 GTK_SCROLL_PAGE_RIGHT);
416 add_slider_binding (binding_set, GDK_KEY_Page_Down, 0,
417 GTK_SCROLL_PAGE_DOWN);
419 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
420 GTK_SCROLL_PAGE_DOWN);
422 /* Logical bindings (vs. visual bindings above) */
424 add_slider_binding (binding_set, GDK_KEY_plus, 0,
425 GTK_SCROLL_STEP_FORWARD);
427 add_slider_binding (binding_set, GDK_KEY_minus, 0,
428 GTK_SCROLL_STEP_BACKWARD);
430 add_slider_binding (binding_set, GDK_KEY_plus, GDK_CONTROL_MASK,
431 GTK_SCROLL_PAGE_FORWARD);
433 add_slider_binding (binding_set, GDK_KEY_minus, GDK_CONTROL_MASK,
434 GTK_SCROLL_PAGE_BACKWARD);
437 add_slider_binding (binding_set, GDK_KEY_KP_Add, 0,
438 GTK_SCROLL_STEP_FORWARD);
440 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, 0,
441 GTK_SCROLL_STEP_BACKWARD);
443 add_slider_binding (binding_set, GDK_KEY_KP_Add, GDK_CONTROL_MASK,
444 GTK_SCROLL_PAGE_FORWARD);
446 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
447 GTK_SCROLL_PAGE_BACKWARD);
450 add_slider_binding (binding_set, GDK_KEY_Home, 0,
453 add_slider_binding (binding_set, GDK_KEY_KP_Home, 0,
456 add_slider_binding (binding_set, GDK_KEY_End, 0,
459 add_slider_binding (binding_set, GDK_KEY_KP_End, 0,
462 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
464 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCALE_ACCESSIBLE);
468 gtk_scale_init (GtkScale *scale)
470 GtkScalePrivate *priv;
471 GtkRange *range = GTK_RANGE (scale);
472 GtkStyleContext *context;
474 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
479 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
481 gtk_range_set_slider_size_fixed (range, TRUE);
483 _gtk_range_set_has_origin (range, TRUE);
485 priv->draw_value = TRUE;
486 priv->value_pos = GTK_POS_TOP;
488 gtk_range_set_round_digits (range, priv->digits);
490 gtk_range_set_flippable (range,
491 gtk_orientable_get_orientation (GTK_ORIENTABLE (range))== GTK_ORIENTATION_HORIZONTAL);
493 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
494 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE);
498 gtk_scale_set_property (GObject *object,
505 scale = GTK_SCALE (object);
510 gtk_scale_set_digits (scale, g_value_get_int (value));
512 case PROP_DRAW_VALUE:
513 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
515 case PROP_HAS_ORIGIN:
516 gtk_scale_set_has_origin (scale, g_value_get_boolean (value));
519 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
528 gtk_scale_get_property (GObject *object,
533 GtkScale *scale = GTK_SCALE (object);
534 GtkScalePrivate *priv = scale->priv;
539 g_value_set_int (value, priv->digits);
541 case PROP_DRAW_VALUE:
542 g_value_set_boolean (value, priv->draw_value);
544 case PROP_HAS_ORIGIN:
545 g_value_set_boolean (value, gtk_scale_get_has_origin (scale));
548 g_value_set_enum (value, priv->value_pos);
551 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
558 * @orientation: the scale's orientation.
559 * @adjustment: (allow-none): the #GtkAdjustment which sets the range
560 * of the scale, or %NULL to create a new adjustment.
562 * Creates a new #GtkScale.
564 * Return value: a new #GtkScale
569 gtk_scale_new (GtkOrientation orientation,
570 GtkAdjustment *adjustment)
572 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
575 return g_object_new (GTK_TYPE_SCALE,
576 "orientation", orientation,
577 "adjustment", adjustment,
582 * gtk_scale_new_with_range:
583 * @orientation: the scale's orientation.
584 * @min: minimum value
585 * @max: maximum value
586 * @step: step increment (tick size) used with keyboard shortcuts
588 * Creates a new scale widget with the given orientation that lets the
589 * user input a number between @min and @max (including @min and @max)
590 * with the increment @step. @step must be nonzero; it's the distance
591 * the slider moves when using the arrow keys to adjust the scale
594 * Note that the way in which the precision is derived works best if @step
595 * is a power of ten. If the resulting precision is not suitable for your
596 * needs, use gtk_scale_set_digits() to correct it.
598 * Return value: a new #GtkScale
603 gtk_scale_new_with_range (GtkOrientation orientation,
611 g_return_val_if_fail (min < max, NULL);
612 g_return_val_if_fail (step != 0.0, NULL);
614 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
616 if (fabs (step) >= 1.0 || step == 0.0)
622 digits = abs ((gint) floor (log10 (fabs (step))));
627 return g_object_new (GTK_TYPE_SCALE,
628 "orientation", orientation,
635 * gtk_scale_set_digits:
636 * @scale: a #GtkScale
637 * @digits: the number of decimal places to display,
638 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
640 * Sets the number of decimal places that are displayed in the value.
641 * Also causes the value of the adjustment to be rounded off to this
642 * number of digits, so the retrieved value matches the value the user saw.
645 gtk_scale_set_digits (GtkScale *scale,
648 GtkScalePrivate *priv;
651 g_return_if_fail (GTK_IS_SCALE (scale));
654 range = GTK_RANGE (scale);
656 digits = CLAMP (digits, -1, MAX_DIGITS);
658 if (priv->digits != digits)
660 priv->digits = digits;
661 if (priv->draw_value)
662 gtk_range_set_round_digits (range, digits);
664 _gtk_scale_clear_layout (scale);
665 gtk_widget_queue_resize (GTK_WIDGET (scale));
667 g_object_notify (G_OBJECT (scale), "digits");
672 * gtk_scale_get_digits:
673 * @scale: a #GtkScale
675 * Gets the number of decimal places that are displayed in the value.
677 * Returns: the number of decimal places that are displayed
680 gtk_scale_get_digits (GtkScale *scale)
682 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
684 return scale->priv->digits;
688 * gtk_scale_set_draw_value:
689 * @scale: a #GtkScale
690 * @draw_value: %TRUE to draw the value
692 * Specifies whether the current value is displayed as a string next
696 gtk_scale_set_draw_value (GtkScale *scale,
699 GtkScalePrivate *priv;
701 g_return_if_fail (GTK_IS_SCALE (scale));
705 draw_value = draw_value != FALSE;
707 if (priv->draw_value != draw_value)
709 priv->draw_value = draw_value;
711 gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
713 gtk_range_set_round_digits (GTK_RANGE (scale), -1);
715 _gtk_scale_clear_layout (scale);
717 gtk_widget_queue_resize (GTK_WIDGET (scale));
719 g_object_notify (G_OBJECT (scale), "draw-value");
724 * gtk_scale_get_draw_value:
725 * @scale: a #GtkScale
727 * Returns whether the current value is displayed as a string
728 * next to the slider.
730 * Returns: whether the current value is displayed as a string
733 gtk_scale_get_draw_value (GtkScale *scale)
735 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
737 return scale->priv->draw_value;
741 * gtk_scale_set_has_origin:
742 * @scale: a #GtkScale
743 * @has_origin: %TRUE if the scale has an origin
745 * If @has_origin is set to %TRUE (the default),
746 * the scale will highlight the part of the scale
747 * between the origin (bottom or left side) of the scale
748 * and the current value.
753 gtk_scale_set_has_origin (GtkScale *scale,
756 g_return_if_fail (GTK_IS_SCALE (scale));
758 has_origin = has_origin != FALSE;
760 if (_gtk_range_get_has_origin (GTK_RANGE (scale)) != has_origin)
762 _gtk_range_set_has_origin (GTK_RANGE (scale), has_origin);
764 gtk_widget_queue_draw (GTK_WIDGET (scale));
766 g_object_notify (G_OBJECT (scale), "has-origin");
771 * gtk_scale_get_has_origin:
772 * @scale: a #GtkScale
774 * Returns whether the scale has an origin.
776 * Returns: %TRUE if the scale has an origin.
781 gtk_scale_get_has_origin (GtkScale *scale)
783 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
785 return _gtk_range_get_has_origin (GTK_RANGE (scale));
789 * gtk_scale_set_value_pos:
790 * @scale: a #GtkScale
791 * @pos: the position in which the current value is displayed
793 * Sets the position in which the current value is displayed.
796 gtk_scale_set_value_pos (GtkScale *scale,
799 GtkScalePrivate *priv;
802 g_return_if_fail (GTK_IS_SCALE (scale));
806 if (priv->value_pos != pos)
808 priv->value_pos = pos;
809 widget = GTK_WIDGET (scale);
811 _gtk_scale_clear_layout (scale);
812 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
813 gtk_widget_queue_resize (widget);
815 g_object_notify (G_OBJECT (scale), "value-pos");
820 * gtk_scale_get_value_pos:
821 * @scale: a #GtkScale
823 * Gets the position in which the current value is displayed.
825 * Returns: the position in which the current value is displayed
828 gtk_scale_get_value_pos (GtkScale *scale)
830 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
832 return scale->priv->value_pos;
836 gtk_scale_get_range_border (GtkRange *range,
839 GtkScalePrivate *priv;
844 widget = GTK_WIDGET (range);
845 scale = GTK_SCALE (range);
848 _gtk_scale_get_value_size (scale, &w, &h);
855 if (priv->draw_value)
858 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
860 switch (priv->value_pos)
863 border->left += w + value_spacing;
866 border->right += w + value_spacing;
869 border->top += h + value_spacing;
872 border->bottom += h + value_spacing;
881 gint n1, w1, h1, n2, w2, h2;
883 gtk_widget_style_get (widget,
884 "slider-width", &slider_width,
885 "value-spacing", &value_spacing,
889 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
891 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
894 border->top += h1 + value_spacing + slider_width / 2;
896 border->bottom += h2 + value_spacing + slider_width / 2;
901 border->left += w1 + value_spacing + slider_width / 2;
903 border->right += w2 + value_spacing + slider_width / 2;
908 /* FIXME this could actually be static at the moment. */
910 _gtk_scale_get_value_size (GtkScale *scale,
914 GtkScalePrivate *priv = scale->priv;
917 if (priv->draw_value)
919 GtkAdjustment *adjustment;
921 PangoRectangle logical_rect;
924 range = GTK_RANGE (scale);
926 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
927 adjustment = gtk_range_get_adjustment (range);
929 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
930 pango_layout_set_text (layout, txt, -1);
933 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
936 *width = logical_rect.width;
938 *height = logical_rect.height;
940 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
941 pango_layout_set_text (layout, txt, -1);
944 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
947 *width = MAX (*width, logical_rect.width);
949 *height = MAX (*height, logical_rect.height);
951 g_object_unref (layout);
964 gtk_scale_get_mark_label_size (GtkScale *scale,
965 GtkPositionType position,
973 GtkScalePrivate *priv = scale->priv;
975 PangoRectangle logical_rect;
979 *count1 = *count2 = 0;
980 *width1 = *width2 = 0;
981 *height1 = *height2 = 0;
983 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
985 for (m = priv->marks; m; m = m->next)
987 GtkScaleMark *mark = m->data;
989 if (mark->markup && *mark->markup)
991 pango_layout_set_markup (layout, mark->markup, -1);
992 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
994 w = logical_rect.width;
995 h = logical_rect.height;
1003 if (mark->position == position)
1006 *width1 = MAX (*width1, w);
1007 *height1 = MAX (*height1, h);
1012 *width2 = MAX (*width2, w);
1013 *height2 = MAX (*height2, h);
1017 g_object_unref (layout);
1021 gtk_scale_style_updated (GtkWidget *widget)
1026 range = GTK_RANGE (widget);
1028 gtk_widget_style_get (widget,
1029 "slider-length", &slider_length,
1032 gtk_range_set_min_slider_size (range, slider_length);
1034 _gtk_scale_clear_layout (GTK_SCALE (widget));
1036 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_updated (widget);
1040 gtk_scale_screen_changed (GtkWidget *widget,
1041 GdkScreen *old_screen)
1043 _gtk_scale_clear_layout (GTK_SCALE (widget));
1047 gtk_scale_get_preferred_width (GtkWidget *widget,
1051 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural);
1053 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
1055 gint n1, w1, h1, n2, w2, h2;
1059 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
1061 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
1063 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
1064 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
1067 *minimum = MAX (*minimum, w);
1068 *natural = MAX (*natural, w);
1073 gtk_scale_get_preferred_height (GtkWidget *widget,
1077 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural);
1080 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
1082 gint n1, w1, h1, n2, w2, h2;
1086 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
1088 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
1089 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
1090 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
1093 *minimum = MAX (*minimum, h);
1094 *natural = MAX (*natural, h);
1099 find_next_pos (GtkWidget *widget,
1102 GtkPositionType pos)
1104 GtkAllocation allocation;
1108 for (m = list->next, i = 1; m; m = m->next, i++)
1110 GtkScaleMark *mark = m->data;
1112 if (mark->position == pos)
1116 gtk_widget_get_allocation (widget, &allocation);
1117 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
1118 return allocation.width;
1120 return allocation.height;
1124 gtk_scale_draw (GtkWidget *widget,
1127 GtkScale *scale = GTK_SCALE (widget);
1128 GtkScalePrivate *priv = scale->priv;
1129 GtkRange *range = GTK_RANGE (scale);
1130 GtkStyleContext *context;
1137 context = gtk_widget_get_style_context (widget);
1138 gtk_widget_style_get (widget,
1139 "focus-padding", &focus_padding,
1140 "slider-width", &slider_width,
1141 "value-spacing", &value_spacing,
1144 /* We need to chain up _first_ so the various geometry members of
1145 * GtkRange struct are updated.
1147 GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1151 GtkOrientation orientation;
1152 GdkRectangle range_rect;
1154 gint x1, x2, x3, y1, y2, y3;
1155 PangoLayout *layout;
1156 PangoRectangle logical_rect;
1158 gint min_pos_before, min_pos_after;
1159 gint min_pos, max_pos;
1161 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1162 _gtk_range_get_stop_positions (range, &marks);
1164 layout = gtk_widget_create_pango_layout (widget, NULL);
1165 gtk_range_get_range_rect (range, &range_rect);
1167 min_pos_before = min_pos_after = 0;
1169 for (m = priv->marks, i = 0; m; m = m->next, i++)
1171 GtkScaleMark *mark = m->data;
1173 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1176 if (mark->position == GTK_POS_TOP)
1179 y2 = y1 - slider_width / 2;
1180 min_pos = min_pos_before;
1181 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP) - min_sep;
1185 y1 = range_rect.y + range_rect.height;
1186 y2 = y1 + slider_width / 2;
1187 min_pos = min_pos_after;
1188 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM) - min_sep;
1191 gtk_style_context_save (context);
1192 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1194 gtk_render_line (context, cr, x1, y1, x1, y2);
1198 pango_layout_set_markup (layout, mark->markup, -1);
1199 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1201 x3 = x1 - logical_rect.width / 2;
1204 if (x3 + logical_rect.width > max_pos)
1205 x3 = max_pos - logical_rect.width;
1208 if (mark->position == GTK_POS_TOP)
1210 y3 = y2 - value_spacing - logical_rect.height;
1211 min_pos_before = x3 + logical_rect.width + min_sep;
1215 y3 = y2 + value_spacing;
1216 min_pos_after = x3 + logical_rect.width + min_sep;
1219 gtk_render_layout (context, cr, x3, y3, layout);
1222 gtk_style_context_restore (context);
1226 if (mark->position == GTK_POS_TOP)
1229 x2 = range_rect.x - slider_width / 2;
1230 min_pos = min_pos_before;
1231 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP) - min_sep;
1235 x1 = range_rect.x + range_rect.width;
1236 x2 = range_rect.x + range_rect.width + slider_width / 2;
1237 min_pos = min_pos_after;
1238 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM) - min_sep;
1242 gtk_style_context_save (context);
1243 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1245 gtk_render_line (context, cr, x1, y1, x2, y1);
1249 pango_layout_set_markup (layout, mark->markup, -1);
1250 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1252 y3 = y1 - logical_rect.height / 2;
1255 if (y3 + logical_rect.height > max_pos)
1256 y3 = max_pos - logical_rect.height;
1259 if (mark->position == GTK_POS_TOP)
1261 x3 = x2 - value_spacing - logical_rect.width;
1262 min_pos_before = y3 + logical_rect.height + min_sep;
1266 x3 = x2 + value_spacing;
1267 min_pos_after = y3 + logical_rect.height + min_sep;
1270 gtk_render_layout (context, cr, x3, y3, layout);
1273 gtk_style_context_restore (context);
1277 g_object_unref (layout);
1281 if (priv->draw_value)
1283 GtkAllocation allocation;
1285 PangoLayout *layout;
1288 layout = gtk_scale_get_layout (scale);
1289 gtk_scale_get_layout_offsets (scale, &x, &y);
1290 gtk_widget_get_allocation (widget, &allocation);
1292 gtk_render_layout (context, cr,
1302 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1306 GtkScalePrivate *priv = scale->priv;
1307 GtkAllocation allocation;
1308 GtkWidget *widget = GTK_WIDGET (scale);
1309 GtkRange *range = GTK_RANGE (widget);
1310 GdkRectangle range_rect;
1311 PangoLayout *layout = gtk_scale_get_layout (scale);
1312 PangoRectangle logical_rect;
1313 gint slider_start, slider_end;
1324 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1326 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1328 gtk_widget_get_allocation (widget, &allocation);
1329 gtk_range_get_range_rect (range, &range_rect);
1330 gtk_range_get_slider_range (range,
1334 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1336 switch (priv->value_pos)
1339 *x = range_rect.x - value_spacing - logical_rect.width;
1340 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1344 *x = range_rect.x + range_rect.width + value_spacing;
1345 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1349 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1350 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1351 *y = range_rect.y - logical_rect.height - value_spacing;
1354 case GTK_POS_BOTTOM:
1355 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1356 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1357 *y = range_rect.y + range_rect.height + value_spacing;
1361 g_return_if_reached ();
1367 switch (priv->value_pos)
1370 *x = range_rect.x - logical_rect.width - value_spacing;
1371 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1372 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1376 *x = range_rect.x + range_rect.width + value_spacing;
1377 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1378 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1382 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1383 *y = range_rect.y - logical_rect.height - value_spacing;
1386 case GTK_POS_BOTTOM:
1387 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1388 *y = range_rect.y + range_rect.height + value_spacing;
1392 g_return_if_reached ();
1401 * _gtk_scale_format_value:
1402 * @scale: a #GtkScale
1403 * @value: adjustment value
1405 * Emits #GtkScale::format-value signal to format the value,
1406 * if no user signal handlers, falls back to a default format.
1408 * Return value: formatted value
1411 _gtk_scale_format_value (GtkScale *scale,
1414 GtkScalePrivate *priv = scale->priv;
1417 g_signal_emit (scale,
1418 signals[FORMAT_VALUE],
1426 return g_strdup_printf ("%0.*f", priv->digits, value);
1430 gtk_scale_finalize (GObject *object)
1432 GtkScale *scale = GTK_SCALE (object);
1434 _gtk_scale_clear_layout (scale);
1435 gtk_scale_clear_marks (scale);
1437 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1441 * gtk_scale_get_layout:
1442 * @scale: A #GtkScale
1444 * Gets the #PangoLayout used to display the scale. The returned
1445 * object is owned by the scale so does not need to be freed by
1448 * Return value: (transfer none): the #PangoLayout for this scale,
1449 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1454 gtk_scale_get_layout (GtkScale *scale)
1456 GtkScalePrivate *priv;
1459 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1465 if (priv->draw_value)
1466 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1469 if (priv->draw_value)
1471 txt = _gtk_scale_format_value (scale,
1472 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1473 pango_layout_set_text (priv->layout, txt, -1);
1477 return priv->layout;
1481 * gtk_scale_get_layout_offsets:
1482 * @scale: a #GtkScale
1483 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
1484 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
1486 * Obtains the coordinates where the scale will draw the
1487 * #PangoLayout representing the text in the scale. Remember
1488 * when using the #PangoLayout function you need to convert to
1489 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1491 * If the #GtkScale:draw-value property is %FALSE, the return
1492 * values are undefined.
1497 gtk_scale_get_layout_offsets (GtkScale *scale,
1504 g_return_if_fail (GTK_IS_SCALE (scale));
1506 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1507 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1517 _gtk_scale_clear_layout (GtkScale *scale)
1519 GtkScalePrivate *priv = scale->priv;
1521 g_return_if_fail (GTK_IS_SCALE (scale));
1525 g_object_unref (priv->layout);
1526 priv->layout = NULL;
1531 gtk_scale_mark_free (gpointer data)
1533 GtkScaleMark *mark = data;
1535 g_free (mark->markup);
1540 * gtk_scale_clear_marks:
1541 * @scale: a #GtkScale
1543 * Removes any marks that have been added with gtk_scale_add_mark().
1548 gtk_scale_clear_marks (GtkScale *scale)
1550 GtkScalePrivate *priv;
1551 GtkStyleContext *context;
1553 g_return_if_fail (GTK_IS_SCALE (scale));
1557 g_slist_free_full (priv->marks, gtk_scale_mark_free);
1560 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1561 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1562 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1564 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1566 gtk_widget_queue_resize (GTK_WIDGET (scale));
1570 * gtk_scale_add_mark:
1571 * @scale: a #GtkScale
1572 * @value: the value at which the mark is placed, must be between
1573 * the lower and upper limits of the scales' adjustment
1574 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1575 * and %GTK_POS_LEFT are drawn above the scale, anything else below.
1576 * For a vertical scale, #GTK_POS_LEFT and %GTK_POS_TOP are drawn to
1577 * the left of the scale, anything else to the right.
1578 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1581 * Adds a mark at @value.
1583 * A mark is indicated visually by drawing a tick mark next to the scale,
1584 * and GTK+ makes it easy for the user to position the scale exactly at the
1587 * If @markup is not %NULL, text is shown next to the tick mark.
1589 * To remove marks from a scale, use gtk_scale_clear_marks().
1594 gtk_scale_add_mark (GtkScale *scale,
1596 GtkPositionType position,
1597 const gchar *markup)
1599 GtkScalePrivate *priv;
1604 GtkStyleContext *context;
1607 g_return_if_fail (GTK_IS_SCALE (scale));
1611 mark = g_new (GtkScaleMark, 1);
1612 mark->value = value;
1613 mark->markup = g_strdup (markup);
1614 if (position == GTK_POS_LEFT ||
1615 position == GTK_POS_TOP)
1616 mark->position = GTK_POS_TOP;
1618 mark->position = GTK_POS_BOTTOM;
1620 priv->marks = g_slist_insert_sorted_with_data (priv->marks, mark,
1622 GINT_TO_POINTER (gtk_range_get_inverted (GTK_RANGE (scale))));
1624 #define MARKS_ABOVE 1
1625 #define MARKS_BELOW 2
1628 n = g_slist_length (priv->marks);
1629 values = g_new (gdouble, n);
1630 for (m = priv->marks, i = 0; m; m = m->next, i++)
1633 values[i] = mark->value;
1634 if (mark->position == GTK_POS_TOP)
1635 all_pos |= MARKS_ABOVE;
1637 all_pos |= MARKS_BELOW;
1640 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1644 /* Set the style classes for the slider, so it could
1645 * point to the right direction when marks are present
1647 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1649 if (all_pos & MARKS_ABOVE)
1650 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1652 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1653 if (all_pos & MARKS_BELOW)
1654 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1656 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1658 gtk_widget_queue_resize (GTK_WIDGET (scale));
1661 static GtkBuildableIface *parent_buildable_iface;
1664 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1666 parent_buildable_iface = g_type_interface_peek_parent (iface);
1667 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1668 iface->custom_finished = gtk_scale_buildable_custom_finished;
1674 GtkBuilder *builder;
1676 } MarksSubparserData;
1681 GtkPositionType position;
1684 gboolean translatable;
1688 mark_data_free (MarkData *data)
1690 g_string_free (data->markup, TRUE);
1691 g_free (data->context);
1692 g_slice_free (MarkData, data);
1696 marks_start_element (GMarkupParseContext *context,
1697 const gchar *element_name,
1698 const gchar **names,
1699 const gchar **values,
1703 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1705 gint line_number, char_number;
1707 if (strcmp (element_name, "marks") == 0)
1709 else if (strcmp (element_name, "mark") == 0)
1712 gboolean has_value = FALSE;
1713 GtkPositionType position = GTK_POS_BOTTOM;
1714 const gchar *msg_context = NULL;
1715 gboolean translatable = FALSE;
1718 for (i = 0; names[i]; i++)
1720 if (strcmp (names[i], "translatable") == 0)
1722 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1725 else if (strcmp (names[i], "comments") == 0)
1727 /* do nothing, comments are for translators */
1729 else if (strcmp (names[i], "context") == 0)
1730 msg_context = values[i];
1731 else if (strcmp (names[i], "value") == 0)
1733 GValue gvalue = G_VALUE_INIT;
1735 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1738 value = g_value_get_double (&gvalue);
1741 else if (strcmp (names[i], "position") == 0)
1743 GValue gvalue = G_VALUE_INIT;
1745 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1748 position = g_value_get_enum (&gvalue);
1752 g_markup_parse_context_get_position (context,
1757 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1758 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1760 line_number, char_number, names[i], "mark");
1767 g_markup_parse_context_get_position (context,
1772 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1773 "%s:%d:%d <%s> requires attribute \"%s\"",
1775 line_number, char_number, "mark",
1780 mark = g_slice_new (MarkData);
1781 mark->value = value;
1782 if (position == GTK_POS_LEFT ||
1783 position == GTK_POS_TOP)
1784 mark->position = GTK_POS_TOP;
1786 mark->position = GTK_POS_BOTTOM;
1787 mark->markup = g_string_new ("");
1788 mark->context = g_strdup (msg_context);
1789 mark->translatable = translatable;
1791 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1795 g_markup_parse_context_get_position (context,
1800 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1801 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1803 line_number, char_number, element_name);
1809 marks_text (GMarkupParseContext *context,
1815 MarksSubparserData *data = (MarksSubparserData*)user_data;
1817 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1819 MarkData *mark = data->marks->data;
1821 g_string_append_len (mark->markup, text, text_len);
1825 static const GMarkupParser marks_parser =
1827 marks_start_element,
1834 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1835 GtkBuilder *builder,
1837 const gchar *tagname,
1838 GMarkupParser *parser,
1841 MarksSubparserData *parser_data;
1846 if (strcmp (tagname, "marks") == 0)
1848 parser_data = g_slice_new0 (MarksSubparserData);
1849 parser_data->scale = GTK_SCALE (buildable);
1850 parser_data->marks = NULL;
1852 *parser = marks_parser;
1853 *data = parser_data;
1857 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1858 tagname, parser, data);
1862 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1863 GtkBuilder *builder,
1865 const gchar *tagname,
1868 GtkScale *scale = GTK_SCALE (buildable);
1869 MarksSubparserData *marks_data;
1871 if (strcmp (tagname, "marks") == 0)
1876 marks_data = (MarksSubparserData *)user_data;
1878 for (m = marks_data->marks; m; m = m->next)
1880 MarkData *mdata = m->data;
1882 if (mdata->translatable && mdata->markup->len)
1883 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1885 mdata->markup->str);
1887 markup = mdata->markup->str;
1889 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1891 mark_data_free (mdata);
1894 g_slist_free (marks_data->marks);
1895 g_slice_free (MarksSubparserData, marks_data);