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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
33 #include "gtkscaleprivate.h"
34 #include "gtkiconfactory.h"
35 #include "gtkicontheme.h"
36 #include "gtkmarshalers.h"
37 #include "gtkbindings.h"
38 #include "gtkorientable.h"
39 #include "gtktypebuiltins.h"
40 #include "gtkprivate.h"
42 #include "gtkbuildable.h"
43 #include "gtkbuilderprivate.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);
199 scale->priv->marks = g_slist_sort_with_data (scale->priv->marks,
201 GINT_TO_POINTER (gtk_range_get_inverted (GTK_RANGE (scale))));
204 if (G_OBJECT_CLASS (gtk_scale_parent_class)->notify)
205 G_OBJECT_CLASS (gtk_scale_parent_class)->notify (object, pspec);
209 #define add_slider_binding(binding_set, keyval, mask, scroll) \
210 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
211 I_("move-slider"), 1, \
212 GTK_TYPE_SCROLL_TYPE, scroll)
215 gtk_scale_class_init (GtkScaleClass *class)
217 GObjectClass *gobject_class;
218 GtkWidgetClass *widget_class;
219 GtkRangeClass *range_class;
220 GtkBindingSet *binding_set;
222 gobject_class = G_OBJECT_CLASS (class);
223 range_class = (GtkRangeClass*) class;
224 widget_class = (GtkWidgetClass*) class;
226 gobject_class->set_property = gtk_scale_set_property;
227 gobject_class->get_property = gtk_scale_get_property;
228 gobject_class->notify = gtk_scale_notify;
229 gobject_class->finalize = gtk_scale_finalize;
231 widget_class->style_updated = gtk_scale_style_updated;
232 widget_class->screen_changed = gtk_scale_screen_changed;
233 widget_class->draw = gtk_scale_draw;
234 widget_class->get_preferred_width = gtk_scale_get_preferred_width;
235 widget_class->get_preferred_height = gtk_scale_get_preferred_height;
237 range_class->slider_detail = "Xscale";
238 range_class->get_range_border = gtk_scale_get_range_border;
240 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
243 * GtkScale::format-value:
244 * @scale: the object which received the signal
245 * @value: the value to format
247 * Signal which allows you to change how the scale value is displayed.
248 * Connect a signal handler which returns an allocated string representing
249 * @value. That string will then be used to display the scale's value.
251 * Here's an example signal handler which displays a value 1.0 as
252 * with "-->1.0<--".
255 * format_value_callback (GtkScale *scale,
258 * return g_strdup_printf ("-->%0.*g<--",
259 * gtk_scale_get_digits (scale), value);
263 * Return value: allocated string representing @value
265 signals[FORMAT_VALUE] =
266 g_signal_new (I_("format-value"),
267 G_TYPE_FROM_CLASS (gobject_class),
269 G_STRUCT_OFFSET (GtkScaleClass, format_value),
270 _gtk_single_string_accumulator, NULL,
271 _gtk_marshal_STRING__DOUBLE,
275 g_object_class_install_property (gobject_class,
277 g_param_spec_int ("digits",
279 P_("The number of decimal places that are displayed in the value"),
283 GTK_PARAM_READWRITE));
285 g_object_class_install_property (gobject_class,
287 g_param_spec_boolean ("draw-value",
289 P_("Whether the current value is displayed as a string next to the slider"),
291 GTK_PARAM_READWRITE));
293 g_object_class_install_property (gobject_class,
295 g_param_spec_boolean ("has-origin",
297 P_("Whether the scale has an origin"),
299 GTK_PARAM_READWRITE));
301 g_object_class_install_property (gobject_class,
303 g_param_spec_enum ("value-pos",
304 P_("Value Position"),
305 P_("The position in which the current value is displayed"),
306 GTK_TYPE_POSITION_TYPE,
308 GTK_PARAM_READWRITE));
310 gtk_widget_class_install_style_property (widget_class,
311 g_param_spec_int ("slider-length",
313 P_("Length of scale's slider"),
317 GTK_PARAM_READABLE));
319 gtk_widget_class_install_style_property (widget_class,
320 g_param_spec_int ("value-spacing",
322 P_("Space between value text and the slider/trough area"),
326 GTK_PARAM_READABLE));
328 /* All bindings (even arrow keys) are on both h/v scale, because
329 * blind users etc. don't care about scale orientation.
332 binding_set = gtk_binding_set_by_class (class);
334 add_slider_binding (binding_set, GDK_KEY_Left, 0,
335 GTK_SCROLL_STEP_LEFT);
337 add_slider_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
338 GTK_SCROLL_PAGE_LEFT);
340 add_slider_binding (binding_set, GDK_KEY_KP_Left, 0,
341 GTK_SCROLL_STEP_LEFT);
343 add_slider_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
344 GTK_SCROLL_PAGE_LEFT);
346 add_slider_binding (binding_set, GDK_KEY_Right, 0,
347 GTK_SCROLL_STEP_RIGHT);
349 add_slider_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
350 GTK_SCROLL_PAGE_RIGHT);
352 add_slider_binding (binding_set, GDK_KEY_KP_Right, 0,
353 GTK_SCROLL_STEP_RIGHT);
355 add_slider_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
356 GTK_SCROLL_PAGE_RIGHT);
358 add_slider_binding (binding_set, GDK_KEY_Up, 0,
361 add_slider_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
364 add_slider_binding (binding_set, GDK_KEY_KP_Up, 0,
367 add_slider_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
370 add_slider_binding (binding_set, GDK_KEY_Down, 0,
371 GTK_SCROLL_STEP_DOWN);
373 add_slider_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
374 GTK_SCROLL_PAGE_DOWN);
376 add_slider_binding (binding_set, GDK_KEY_KP_Down, 0,
377 GTK_SCROLL_STEP_DOWN);
379 add_slider_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
380 GTK_SCROLL_PAGE_DOWN);
382 add_slider_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
383 GTK_SCROLL_PAGE_LEFT);
385 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
386 GTK_SCROLL_PAGE_LEFT);
388 add_slider_binding (binding_set, GDK_KEY_Page_Up, 0,
391 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
394 add_slider_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
395 GTK_SCROLL_PAGE_RIGHT);
397 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
398 GTK_SCROLL_PAGE_RIGHT);
400 add_slider_binding (binding_set, GDK_KEY_Page_Down, 0,
401 GTK_SCROLL_PAGE_DOWN);
403 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
404 GTK_SCROLL_PAGE_DOWN);
406 /* Logical bindings (vs. visual bindings above) */
408 add_slider_binding (binding_set, GDK_KEY_plus, 0,
409 GTK_SCROLL_STEP_FORWARD);
411 add_slider_binding (binding_set, GDK_KEY_minus, 0,
412 GTK_SCROLL_STEP_BACKWARD);
414 add_slider_binding (binding_set, GDK_KEY_plus, GDK_CONTROL_MASK,
415 GTK_SCROLL_PAGE_FORWARD);
417 add_slider_binding (binding_set, GDK_KEY_minus, GDK_CONTROL_MASK,
418 GTK_SCROLL_PAGE_BACKWARD);
421 add_slider_binding (binding_set, GDK_KEY_KP_Add, 0,
422 GTK_SCROLL_STEP_FORWARD);
424 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, 0,
425 GTK_SCROLL_STEP_BACKWARD);
427 add_slider_binding (binding_set, GDK_KEY_KP_Add, GDK_CONTROL_MASK,
428 GTK_SCROLL_PAGE_FORWARD);
430 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
431 GTK_SCROLL_PAGE_BACKWARD);
434 add_slider_binding (binding_set, GDK_KEY_Home, 0,
437 add_slider_binding (binding_set, GDK_KEY_KP_Home, 0,
440 add_slider_binding (binding_set, GDK_KEY_End, 0,
443 add_slider_binding (binding_set, GDK_KEY_KP_End, 0,
446 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
448 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCALE_ACCESSIBLE);
452 gtk_scale_init (GtkScale *scale)
454 GtkScalePrivate *priv;
455 GtkRange *range = GTK_RANGE (scale);
456 GtkStyleContext *context;
458 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
463 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
465 gtk_range_set_slider_size_fixed (range, TRUE);
467 _gtk_range_set_has_origin (range, TRUE);
469 priv->draw_value = TRUE;
470 priv->value_pos = GTK_POS_TOP;
472 gtk_range_set_round_digits (range, priv->digits);
474 gtk_range_set_flippable (range,
475 gtk_orientable_get_orientation (GTK_ORIENTABLE (range))== GTK_ORIENTATION_HORIZONTAL);
477 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
478 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE);
482 gtk_scale_set_property (GObject *object,
489 scale = GTK_SCALE (object);
494 gtk_scale_set_digits (scale, g_value_get_int (value));
496 case PROP_DRAW_VALUE:
497 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
499 case PROP_HAS_ORIGIN:
500 gtk_scale_set_has_origin (scale, g_value_get_boolean (value));
503 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
506 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
512 gtk_scale_get_property (GObject *object,
517 GtkScale *scale = GTK_SCALE (object);
518 GtkScalePrivate *priv = scale->priv;
523 g_value_set_int (value, priv->digits);
525 case PROP_DRAW_VALUE:
526 g_value_set_boolean (value, priv->draw_value);
528 case PROP_HAS_ORIGIN:
529 g_value_set_boolean (value, gtk_scale_get_has_origin (scale));
532 g_value_set_enum (value, priv->value_pos);
535 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
542 * @orientation: the scale's orientation.
543 * @adjustment: (allow-none): the #GtkAdjustment which sets the range
544 * of the scale, or %NULL to create a new adjustment.
546 * Creates a new #GtkScale.
548 * Return value: a new #GtkScale
553 gtk_scale_new (GtkOrientation orientation,
554 GtkAdjustment *adjustment)
556 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
559 return g_object_new (GTK_TYPE_SCALE,
560 "orientation", orientation,
561 "adjustment", adjustment,
566 * gtk_scale_new_with_range:
567 * @orientation: the scale's orientation.
568 * @min: minimum value
569 * @max: maximum value
570 * @step: step increment (tick size) used with keyboard shortcuts
572 * Creates a new scale widget with the given orientation that lets the
573 * user input a number between @min and @max (including @min and @max)
574 * with the increment @step. @step must be nonzero; it's the distance
575 * the slider moves when using the arrow keys to adjust the scale
578 * Note that the way in which the precision is derived works best if @step
579 * is a power of ten. If the resulting precision is not suitable for your
580 * needs, use gtk_scale_set_digits() to correct it.
582 * Return value: a new #GtkScale
587 gtk_scale_new_with_range (GtkOrientation orientation,
595 g_return_val_if_fail (min < max, NULL);
596 g_return_val_if_fail (step != 0.0, NULL);
598 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
600 if (fabs (step) >= 1.0 || step == 0.0)
606 digits = abs ((gint) floor (log10 (fabs (step))));
611 return g_object_new (GTK_TYPE_SCALE,
612 "orientation", orientation,
619 * gtk_scale_set_digits:
620 * @scale: a #GtkScale
621 * @digits: the number of decimal places to display,
622 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
624 * Sets the number of decimal places that are displayed in the value.
625 * Also causes the value of the adjustment to be rounded off to this
626 * number of digits, so the retrieved value matches the value the user saw.
629 gtk_scale_set_digits (GtkScale *scale,
632 GtkScalePrivate *priv;
635 g_return_if_fail (GTK_IS_SCALE (scale));
638 range = GTK_RANGE (scale);
640 digits = CLAMP (digits, -1, MAX_DIGITS);
642 if (priv->digits != digits)
644 priv->digits = digits;
645 if (priv->draw_value)
646 gtk_range_set_round_digits (range, digits);
648 _gtk_scale_clear_layout (scale);
649 gtk_widget_queue_resize (GTK_WIDGET (scale));
651 g_object_notify (G_OBJECT (scale), "digits");
656 * gtk_scale_get_digits:
657 * @scale: a #GtkScale
659 * Gets the number of decimal places that are displayed in the value.
661 * Returns: the number of decimal places that are displayed
664 gtk_scale_get_digits (GtkScale *scale)
666 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
668 return scale->priv->digits;
672 * gtk_scale_set_draw_value:
673 * @scale: a #GtkScale
674 * @draw_value: %TRUE to draw the value
676 * Specifies whether the current value is displayed as a string next
680 gtk_scale_set_draw_value (GtkScale *scale,
683 GtkScalePrivate *priv;
685 g_return_if_fail (GTK_IS_SCALE (scale));
689 draw_value = draw_value != FALSE;
691 if (priv->draw_value != draw_value)
693 priv->draw_value = draw_value;
695 gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
697 gtk_range_set_round_digits (GTK_RANGE (scale), -1);
699 _gtk_scale_clear_layout (scale);
701 gtk_widget_queue_resize (GTK_WIDGET (scale));
703 g_object_notify (G_OBJECT (scale), "draw-value");
708 * gtk_scale_get_draw_value:
709 * @scale: a #GtkScale
711 * Returns whether the current value is displayed as a string
712 * next to the slider.
714 * Returns: whether the current value is displayed as a string
717 gtk_scale_get_draw_value (GtkScale *scale)
719 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
721 return scale->priv->draw_value;
725 * gtk_scale_set_has_origin:
726 * @scale: a #GtkScale
727 * @has_origin: %TRUE if the scale has an origin
729 * If @has_origin is set to %TRUE (the default),
730 * the scale will highlight the part of the scale
731 * between the origin (bottom or left side) of the scale
732 * and the current value.
737 gtk_scale_set_has_origin (GtkScale *scale,
740 g_return_if_fail (GTK_IS_SCALE (scale));
742 has_origin = has_origin != FALSE;
744 if (_gtk_range_get_has_origin (GTK_RANGE (scale)) != has_origin)
746 _gtk_range_set_has_origin (GTK_RANGE (scale), has_origin);
748 gtk_widget_queue_draw (GTK_WIDGET (scale));
750 g_object_notify (G_OBJECT (scale), "has-origin");
755 * gtk_scale_get_has_origin:
756 * @scale: a #GtkScale
758 * Returns whether the scale has an origin.
760 * Returns: %TRUE if the scale has an origin.
765 gtk_scale_get_has_origin (GtkScale *scale)
767 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
769 return _gtk_range_get_has_origin (GTK_RANGE (scale));
773 * gtk_scale_set_value_pos:
774 * @scale: a #GtkScale
775 * @pos: the position in which the current value is displayed
777 * Sets the position in which the current value is displayed.
780 gtk_scale_set_value_pos (GtkScale *scale,
783 GtkScalePrivate *priv;
786 g_return_if_fail (GTK_IS_SCALE (scale));
790 if (priv->value_pos != pos)
792 priv->value_pos = pos;
793 widget = GTK_WIDGET (scale);
795 _gtk_scale_clear_layout (scale);
796 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
797 gtk_widget_queue_resize (widget);
799 g_object_notify (G_OBJECT (scale), "value-pos");
804 * gtk_scale_get_value_pos:
805 * @scale: a #GtkScale
807 * Gets the position in which the current value is displayed.
809 * Returns: the position in which the current value is displayed
812 gtk_scale_get_value_pos (GtkScale *scale)
814 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
816 return scale->priv->value_pos;
820 gtk_scale_get_range_border (GtkRange *range,
823 GtkScalePrivate *priv;
828 widget = GTK_WIDGET (range);
829 scale = GTK_SCALE (range);
832 _gtk_scale_get_value_size (scale, &w, &h);
839 if (priv->draw_value)
842 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
844 switch (priv->value_pos)
847 border->left += w + value_spacing;
850 border->right += w + value_spacing;
853 border->top += h + value_spacing;
856 border->bottom += h + value_spacing;
865 gint n1, w1, h1, n2, w2, h2;
867 gtk_widget_style_get (widget,
868 "slider-width", &slider_width,
869 "value-spacing", &value_spacing,
873 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
875 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
878 border->top += h1 + value_spacing + slider_width / 2;
880 border->bottom += h2 + value_spacing + slider_width / 2;
885 border->left += w1 + value_spacing + slider_width / 2;
887 border->right += w2 + value_spacing + slider_width / 2;
892 /* FIXME this could actually be static at the moment. */
894 _gtk_scale_get_value_size (GtkScale *scale,
898 GtkScalePrivate *priv = scale->priv;
901 if (priv->draw_value)
903 GtkAdjustment *adjustment;
905 PangoRectangle logical_rect;
908 range = GTK_RANGE (scale);
910 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
911 adjustment = gtk_range_get_adjustment (range);
913 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
914 pango_layout_set_text (layout, txt, -1);
917 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
920 *width = logical_rect.width;
922 *height = logical_rect.height;
924 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
925 pango_layout_set_text (layout, txt, -1);
928 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
931 *width = MAX (*width, logical_rect.width);
933 *height = MAX (*height, logical_rect.height);
935 g_object_unref (layout);
948 gtk_scale_get_mark_label_size (GtkScale *scale,
949 GtkPositionType position,
957 GtkScalePrivate *priv = scale->priv;
959 PangoRectangle logical_rect;
963 *count1 = *count2 = 0;
964 *width1 = *width2 = 0;
965 *height1 = *height2 = 0;
967 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
969 for (m = priv->marks; m; m = m->next)
971 GtkScaleMark *mark = m->data;
973 if (mark->markup && *mark->markup)
975 pango_layout_set_markup (layout, mark->markup, -1);
976 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
978 w = logical_rect.width;
979 h = logical_rect.height;
987 if (mark->position == position)
990 *width1 = MAX (*width1, w);
991 *height1 = MAX (*height1, h);
996 *width2 = MAX (*width2, w);
997 *height2 = MAX (*height2, h);
1001 g_object_unref (layout);
1005 gtk_scale_style_updated (GtkWidget *widget)
1010 range = GTK_RANGE (widget);
1012 gtk_widget_style_get (widget,
1013 "slider-length", &slider_length,
1016 gtk_range_set_min_slider_size (range, slider_length);
1018 _gtk_scale_clear_layout (GTK_SCALE (widget));
1020 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_updated (widget);
1024 gtk_scale_screen_changed (GtkWidget *widget,
1025 GdkScreen *old_screen)
1027 _gtk_scale_clear_layout (GTK_SCALE (widget));
1031 gtk_scale_get_preferred_width (GtkWidget *widget,
1035 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural);
1037 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
1039 gint n1, w1, h1, n2, w2, h2;
1043 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
1045 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
1047 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
1048 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
1051 *minimum = MAX (*minimum, w);
1052 *natural = MAX (*natural, w);
1057 gtk_scale_get_preferred_height (GtkWidget *widget,
1061 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural);
1064 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
1066 gint n1, w1, h1, n2, w2, h2;
1070 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
1072 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
1073 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
1074 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
1077 *minimum = MAX (*minimum, h);
1078 *natural = MAX (*natural, h);
1083 find_next_pos (GtkWidget *widget,
1086 GtkPositionType pos)
1088 GtkAllocation allocation;
1092 for (m = list->next, i = 1; m; m = m->next, i++)
1094 GtkScaleMark *mark = m->data;
1096 if (mark->position == pos)
1100 gtk_widget_get_allocation (widget, &allocation);
1101 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
1102 return allocation.width;
1104 return allocation.height;
1108 gtk_scale_draw (GtkWidget *widget,
1111 GtkScale *scale = GTK_SCALE (widget);
1112 GtkScalePrivate *priv = scale->priv;
1113 GtkRange *range = GTK_RANGE (scale);
1114 GtkStyleContext *context;
1121 context = gtk_widget_get_style_context (widget);
1122 gtk_widget_style_get (widget,
1123 "focus-padding", &focus_padding,
1124 "slider-width", &slider_width,
1125 "value-spacing", &value_spacing,
1128 /* We need to chain up _first_ so the various geometry members of
1129 * GtkRange struct are updated.
1131 GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1135 GtkOrientation orientation;
1136 GdkRectangle range_rect;
1138 gint x1, x2, x3, y1, y2, y3;
1139 PangoLayout *layout;
1140 PangoRectangle logical_rect;
1142 gint min_pos_before, min_pos_after;
1143 gint min_pos, max_pos;
1146 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1147 n_marks = _gtk_range_get_stop_positions (range, &marks);
1148 /* We always draw the marks in increasing direction, so flip
1149 * the stop positions to match the marks (which we flip in
1152 if (gtk_range_get_inverted (range))
1154 for (i = 0; i < n_marks / 2; i++)
1157 marks[i] = marks[n_marks - 1 - i];
1158 marks[n_marks - 1 - i] = x1;
1162 layout = gtk_widget_create_pango_layout (widget, NULL);
1163 gtk_range_get_range_rect (range, &range_rect);
1165 min_pos_before = min_pos_after = 0;
1167 for (m = priv->marks, i = 0; m; m = m->next, i++)
1169 GtkScaleMark *mark = m->data;
1171 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1174 if (mark->position == GTK_POS_TOP)
1177 y2 = y1 - slider_width / 2;
1178 min_pos = min_pos_before;
1179 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP) - min_sep;
1183 y1 = range_rect.y + range_rect.height;
1184 y2 = y1 + slider_width / 2;
1185 min_pos = min_pos_after;
1186 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM) - min_sep;
1189 gtk_style_context_save (context);
1190 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1192 gtk_render_line (context, cr, x1, y1, x1, y2);
1196 pango_layout_set_markup (layout, mark->markup, -1);
1197 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1199 x3 = x1 - logical_rect.width / 2;
1202 if (x3 + logical_rect.width > max_pos)
1203 x3 = max_pos - logical_rect.width;
1206 if (mark->position == GTK_POS_TOP)
1208 y3 = y2 - value_spacing - logical_rect.height;
1209 min_pos_before = x3 + logical_rect.width + min_sep;
1213 y3 = y2 + value_spacing;
1214 min_pos_after = x3 + logical_rect.width + min_sep;
1217 gtk_render_layout (context, cr, x3, y3, layout);
1220 gtk_style_context_restore (context);
1224 if (mark->position == GTK_POS_TOP)
1227 x2 = range_rect.x - slider_width / 2;
1228 min_pos = min_pos_before;
1229 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP) - min_sep;
1233 x1 = range_rect.x + range_rect.width;
1234 x2 = range_rect.x + range_rect.width + slider_width / 2;
1235 min_pos = min_pos_after;
1236 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM) - min_sep;
1240 gtk_style_context_save (context);
1241 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1243 gtk_render_line (context, cr, x1, y1, x2, y1);
1247 pango_layout_set_markup (layout, mark->markup, -1);
1248 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1250 y3 = y1 - logical_rect.height / 2;
1253 if (y3 + logical_rect.height > max_pos)
1254 y3 = max_pos - logical_rect.height;
1257 if (mark->position == GTK_POS_TOP)
1259 x3 = x2 - value_spacing - logical_rect.width;
1260 min_pos_before = y3 + logical_rect.height + min_sep;
1264 x3 = x2 + value_spacing;
1265 min_pos_after = y3 + logical_rect.height + min_sep;
1268 gtk_render_layout (context, cr, x3, y3, layout);
1271 gtk_style_context_restore (context);
1275 g_object_unref (layout);
1279 if (priv->draw_value)
1281 GtkAllocation allocation;
1283 PangoLayout *layout;
1286 layout = gtk_scale_get_layout (scale);
1287 gtk_scale_get_layout_offsets (scale, &x, &y);
1288 gtk_widget_get_allocation (widget, &allocation);
1290 gtk_render_layout (context, cr,
1300 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1304 GtkScalePrivate *priv = scale->priv;
1305 GtkAllocation allocation;
1306 GtkWidget *widget = GTK_WIDGET (scale);
1307 GtkRange *range = GTK_RANGE (widget);
1308 GdkRectangle range_rect;
1309 PangoLayout *layout = gtk_scale_get_layout (scale);
1310 PangoRectangle logical_rect;
1311 gint slider_start, slider_end;
1322 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1324 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1326 gtk_widget_get_allocation (widget, &allocation);
1327 gtk_range_get_range_rect (range, &range_rect);
1328 gtk_range_get_slider_range (range,
1332 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1334 switch (priv->value_pos)
1337 *x = range_rect.x - value_spacing - logical_rect.width;
1338 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1342 *x = range_rect.x + range_rect.width + value_spacing;
1343 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1347 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1348 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1349 *y = range_rect.y - logical_rect.height - value_spacing;
1352 case GTK_POS_BOTTOM:
1353 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1354 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1355 *y = range_rect.y + range_rect.height + value_spacing;
1359 g_return_if_reached ();
1365 switch (priv->value_pos)
1368 *x = range_rect.x - logical_rect.width - value_spacing;
1369 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1370 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1374 *x = range_rect.x + range_rect.width + value_spacing;
1375 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1376 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1380 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1381 *y = range_rect.y - logical_rect.height - value_spacing;
1384 case GTK_POS_BOTTOM:
1385 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1386 *y = range_rect.y + range_rect.height + value_spacing;
1390 g_return_if_reached ();
1399 * _gtk_scale_format_value:
1400 * @scale: a #GtkScale
1401 * @value: adjustment value
1403 * Emits #GtkScale::format-value signal to format the value,
1404 * if no user signal handlers, falls back to a default format.
1406 * Return value: formatted value
1409 _gtk_scale_format_value (GtkScale *scale,
1412 GtkScalePrivate *priv = scale->priv;
1415 g_signal_emit (scale,
1416 signals[FORMAT_VALUE],
1424 return g_strdup_printf ("%0.*f", priv->digits, value);
1428 gtk_scale_finalize (GObject *object)
1430 GtkScale *scale = GTK_SCALE (object);
1432 _gtk_scale_clear_layout (scale);
1433 gtk_scale_clear_marks (scale);
1435 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1439 * gtk_scale_get_layout:
1440 * @scale: A #GtkScale
1442 * Gets the #PangoLayout used to display the scale. The returned
1443 * object is owned by the scale so does not need to be freed by
1446 * Return value: (transfer none): the #PangoLayout for this scale,
1447 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1452 gtk_scale_get_layout (GtkScale *scale)
1454 GtkScalePrivate *priv;
1457 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1463 if (priv->draw_value)
1464 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1467 if (priv->draw_value)
1469 txt = _gtk_scale_format_value (scale,
1470 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1471 pango_layout_set_text (priv->layout, txt, -1);
1475 return priv->layout;
1479 * gtk_scale_get_layout_offsets:
1480 * @scale: a #GtkScale
1481 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
1482 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
1484 * Obtains the coordinates where the scale will draw the
1485 * #PangoLayout representing the text in the scale. Remember
1486 * when using the #PangoLayout function you need to convert to
1487 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1489 * If the #GtkScale:draw-value property is %FALSE, the return
1490 * values are undefined.
1495 gtk_scale_get_layout_offsets (GtkScale *scale,
1502 g_return_if_fail (GTK_IS_SCALE (scale));
1504 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1505 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1515 _gtk_scale_clear_layout (GtkScale *scale)
1517 GtkScalePrivate *priv = scale->priv;
1519 g_return_if_fail (GTK_IS_SCALE (scale));
1523 g_object_unref (priv->layout);
1524 priv->layout = NULL;
1529 gtk_scale_mark_free (gpointer data)
1531 GtkScaleMark *mark = data;
1533 g_free (mark->markup);
1538 * gtk_scale_clear_marks:
1539 * @scale: a #GtkScale
1541 * Removes any marks that have been added with gtk_scale_add_mark().
1546 gtk_scale_clear_marks (GtkScale *scale)
1548 GtkScalePrivate *priv;
1549 GtkStyleContext *context;
1551 g_return_if_fail (GTK_IS_SCALE (scale));
1555 g_slist_free_full (priv->marks, gtk_scale_mark_free);
1558 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1559 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1560 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1562 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1564 gtk_widget_queue_resize (GTK_WIDGET (scale));
1568 * gtk_scale_add_mark:
1569 * @scale: a #GtkScale
1570 * @value: the value at which the mark is placed, must be between
1571 * the lower and upper limits of the scales' adjustment
1572 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1573 * and %GTK_POS_LEFT are drawn above the scale, anything else below.
1574 * For a vertical scale, #GTK_POS_LEFT and %GTK_POS_TOP are drawn to
1575 * the left of the scale, anything else to the right.
1576 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1579 * Adds a mark at @value.
1581 * A mark is indicated visually by drawing a tick mark next to the scale,
1582 * and GTK+ makes it easy for the user to position the scale exactly at the
1585 * If @markup is not %NULL, text is shown next to the tick mark.
1587 * To remove marks from a scale, use gtk_scale_clear_marks().
1592 gtk_scale_add_mark (GtkScale *scale,
1594 GtkPositionType position,
1595 const gchar *markup)
1597 GtkScalePrivate *priv;
1602 GtkStyleContext *context;
1605 g_return_if_fail (GTK_IS_SCALE (scale));
1609 mark = g_new (GtkScaleMark, 1);
1610 mark->value = value;
1611 mark->markup = g_strdup (markup);
1612 if (position == GTK_POS_LEFT ||
1613 position == GTK_POS_TOP)
1614 mark->position = GTK_POS_TOP;
1616 mark->position = GTK_POS_BOTTOM;
1618 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1619 (GCompareFunc) compare_marks);
1621 #define MARKS_ABOVE 1
1622 #define MARKS_BELOW 2
1625 n = g_slist_length (priv->marks);
1626 values = g_new (gdouble, n);
1627 for (m = priv->marks, i = 0; m; m = m->next, i++)
1630 values[i] = mark->value;
1631 if (mark->position == GTK_POS_TOP)
1632 all_pos |= MARKS_ABOVE;
1634 all_pos |= MARKS_BELOW;
1637 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1641 /* Set the style classes for the slider, so it could
1642 * point to the right direction when marks are present
1644 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1646 if (all_pos & MARKS_ABOVE)
1647 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1649 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1650 if (all_pos & MARKS_BELOW)
1651 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1653 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1655 gtk_widget_queue_resize (GTK_WIDGET (scale));
1658 static GtkBuildableIface *parent_buildable_iface;
1661 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1663 parent_buildable_iface = g_type_interface_peek_parent (iface);
1664 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1665 iface->custom_finished = gtk_scale_buildable_custom_finished;
1671 GtkBuilder *builder;
1673 } MarksSubparserData;
1678 GtkPositionType position;
1681 gboolean translatable;
1685 mark_data_free (MarkData *data)
1687 g_string_free (data->markup, TRUE);
1688 g_free (data->context);
1689 g_slice_free (MarkData, data);
1693 marks_start_element (GMarkupParseContext *context,
1694 const gchar *element_name,
1695 const gchar **names,
1696 const gchar **values,
1700 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1702 gint line_number, char_number;
1704 if (strcmp (element_name, "marks") == 0)
1706 else if (strcmp (element_name, "mark") == 0)
1709 gboolean has_value = FALSE;
1710 GtkPositionType position = GTK_POS_BOTTOM;
1711 const gchar *msg_context = NULL;
1712 gboolean translatable = FALSE;
1715 for (i = 0; names[i]; i++)
1717 if (strcmp (names[i], "translatable") == 0)
1719 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1722 else if (strcmp (names[i], "comments") == 0)
1724 /* do nothing, comments are for translators */
1726 else if (strcmp (names[i], "context") == 0)
1727 msg_context = values[i];
1728 else if (strcmp (names[i], "value") == 0)
1730 GValue gvalue = G_VALUE_INIT;
1732 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1735 value = g_value_get_double (&gvalue);
1738 else if (strcmp (names[i], "position") == 0)
1740 GValue gvalue = G_VALUE_INIT;
1742 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1745 position = g_value_get_enum (&gvalue);
1749 g_markup_parse_context_get_position (context,
1754 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1755 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1757 line_number, char_number, names[i], "mark");
1764 g_markup_parse_context_get_position (context,
1769 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1770 "%s:%d:%d <%s> requires attribute \"%s\"",
1772 line_number, char_number, "mark",
1777 mark = g_slice_new (MarkData);
1778 mark->value = value;
1779 if (position == GTK_POS_LEFT ||
1780 position == GTK_POS_TOP)
1781 mark->position = GTK_POS_TOP;
1783 mark->position = GTK_POS_BOTTOM;
1784 mark->markup = g_string_new ("");
1785 mark->context = g_strdup (msg_context);
1786 mark->translatable = translatable;
1788 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1792 g_markup_parse_context_get_position (context,
1797 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1798 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1800 line_number, char_number, element_name);
1806 marks_text (GMarkupParseContext *context,
1812 MarksSubparserData *data = (MarksSubparserData*)user_data;
1814 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1816 MarkData *mark = data->marks->data;
1818 g_string_append_len (mark->markup, text, text_len);
1822 static const GMarkupParser marks_parser =
1824 marks_start_element,
1831 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1832 GtkBuilder *builder,
1834 const gchar *tagname,
1835 GMarkupParser *parser,
1838 MarksSubparserData *parser_data;
1843 if (strcmp (tagname, "marks") == 0)
1845 parser_data = g_slice_new0 (MarksSubparserData);
1846 parser_data->scale = GTK_SCALE (buildable);
1847 parser_data->marks = NULL;
1849 *parser = marks_parser;
1850 *data = parser_data;
1854 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1855 tagname, parser, data);
1859 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1860 GtkBuilder *builder,
1862 const gchar *tagname,
1865 GtkScale *scale = GTK_SCALE (buildable);
1866 MarksSubparserData *marks_data;
1868 if (strcmp (tagname, "marks") == 0)
1873 marks_data = (MarksSubparserData *)user_data;
1875 for (m = marks_data->marks; m; m = m->next)
1877 MarkData *mdata = m->data;
1879 if (mdata->translatable && mdata->markup->len)
1880 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1882 mdata->markup->str);
1884 markup = mdata->markup->str;
1886 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1888 mark_data_free (mdata);
1891 g_slist_free (marks_data->marks);
1892 g_slice_free (MarksSubparserData, marks_data);