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 #define add_slider_binding(binding_set, keyval, mask, scroll) \
171 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
172 I_("move-slider"), 1, \
173 GTK_TYPE_SCROLL_TYPE, scroll)
176 gtk_scale_class_init (GtkScaleClass *class)
178 GObjectClass *gobject_class;
179 GtkWidgetClass *widget_class;
180 GtkRangeClass *range_class;
181 GtkBindingSet *binding_set;
183 gobject_class = G_OBJECT_CLASS (class);
184 range_class = (GtkRangeClass*) class;
185 widget_class = (GtkWidgetClass*) class;
187 gobject_class->set_property = gtk_scale_set_property;
188 gobject_class->get_property = gtk_scale_get_property;
189 gobject_class->finalize = gtk_scale_finalize;
191 widget_class->style_updated = gtk_scale_style_updated;
192 widget_class->screen_changed = gtk_scale_screen_changed;
193 widget_class->draw = gtk_scale_draw;
194 widget_class->get_preferred_width = gtk_scale_get_preferred_width;
195 widget_class->get_preferred_height = gtk_scale_get_preferred_height;
197 range_class->slider_detail = "Xscale";
198 range_class->get_range_border = gtk_scale_get_range_border;
200 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
203 * GtkScale::format-value:
204 * @scale: the object which received the signal
205 * @value: the value to format
207 * Signal which allows you to change how the scale value is displayed.
208 * Connect a signal handler which returns an allocated string representing
209 * @value. That string will then be used to display the scale's value.
211 * Here's an example signal handler which displays a value 1.0 as
212 * with "-->1.0<--".
215 * format_value_callback (GtkScale *scale,
218 * return g_strdup_printf ("-->%0.*g<--",
219 * gtk_scale_get_digits (scale), value);
223 * Return value: allocated string representing @value
225 signals[FORMAT_VALUE] =
226 g_signal_new (I_("format-value"),
227 G_TYPE_FROM_CLASS (gobject_class),
229 G_STRUCT_OFFSET (GtkScaleClass, format_value),
230 _gtk_single_string_accumulator, NULL,
231 _gtk_marshal_STRING__DOUBLE,
235 g_object_class_install_property (gobject_class,
237 g_param_spec_int ("digits",
239 P_("The number of decimal places that are displayed in the value"),
243 GTK_PARAM_READWRITE));
245 g_object_class_install_property (gobject_class,
247 g_param_spec_boolean ("draw-value",
249 P_("Whether the current value is displayed as a string next to the slider"),
251 GTK_PARAM_READWRITE));
253 g_object_class_install_property (gobject_class,
255 g_param_spec_boolean ("has-origin",
257 P_("Whether the scale has an origin"),
259 GTK_PARAM_READWRITE));
261 g_object_class_install_property (gobject_class,
263 g_param_spec_enum ("value-pos",
264 P_("Value Position"),
265 P_("The position in which the current value is displayed"),
266 GTK_TYPE_POSITION_TYPE,
268 GTK_PARAM_READWRITE));
270 gtk_widget_class_install_style_property (widget_class,
271 g_param_spec_int ("slider-length",
273 P_("Length of scale's slider"),
277 GTK_PARAM_READABLE));
279 gtk_widget_class_install_style_property (widget_class,
280 g_param_spec_int ("value-spacing",
282 P_("Space between value text and the slider/trough area"),
286 GTK_PARAM_READABLE));
288 /* All bindings (even arrow keys) are on both h/v scale, because
289 * blind users etc. don't care about scale orientation.
292 binding_set = gtk_binding_set_by_class (class);
294 add_slider_binding (binding_set, GDK_KEY_Left, 0,
295 GTK_SCROLL_STEP_LEFT);
297 add_slider_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
298 GTK_SCROLL_PAGE_LEFT);
300 add_slider_binding (binding_set, GDK_KEY_KP_Left, 0,
301 GTK_SCROLL_STEP_LEFT);
303 add_slider_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
304 GTK_SCROLL_PAGE_LEFT);
306 add_slider_binding (binding_set, GDK_KEY_Right, 0,
307 GTK_SCROLL_STEP_RIGHT);
309 add_slider_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
310 GTK_SCROLL_PAGE_RIGHT);
312 add_slider_binding (binding_set, GDK_KEY_KP_Right, 0,
313 GTK_SCROLL_STEP_RIGHT);
315 add_slider_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
316 GTK_SCROLL_PAGE_RIGHT);
318 add_slider_binding (binding_set, GDK_KEY_Up, 0,
321 add_slider_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
324 add_slider_binding (binding_set, GDK_KEY_KP_Up, 0,
327 add_slider_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
330 add_slider_binding (binding_set, GDK_KEY_Down, 0,
331 GTK_SCROLL_STEP_DOWN);
333 add_slider_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
334 GTK_SCROLL_PAGE_DOWN);
336 add_slider_binding (binding_set, GDK_KEY_KP_Down, 0,
337 GTK_SCROLL_STEP_DOWN);
339 add_slider_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
340 GTK_SCROLL_PAGE_DOWN);
342 add_slider_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
343 GTK_SCROLL_PAGE_LEFT);
345 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
346 GTK_SCROLL_PAGE_LEFT);
348 add_slider_binding (binding_set, GDK_KEY_Page_Up, 0,
351 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
354 add_slider_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
355 GTK_SCROLL_PAGE_RIGHT);
357 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
358 GTK_SCROLL_PAGE_RIGHT);
360 add_slider_binding (binding_set, GDK_KEY_Page_Down, 0,
361 GTK_SCROLL_PAGE_DOWN);
363 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
364 GTK_SCROLL_PAGE_DOWN);
366 /* Logical bindings (vs. visual bindings above) */
368 add_slider_binding (binding_set, GDK_KEY_plus, 0,
369 GTK_SCROLL_STEP_FORWARD);
371 add_slider_binding (binding_set, GDK_KEY_minus, 0,
372 GTK_SCROLL_STEP_BACKWARD);
374 add_slider_binding (binding_set, GDK_KEY_plus, GDK_CONTROL_MASK,
375 GTK_SCROLL_PAGE_FORWARD);
377 add_slider_binding (binding_set, GDK_KEY_minus, GDK_CONTROL_MASK,
378 GTK_SCROLL_PAGE_BACKWARD);
381 add_slider_binding (binding_set, GDK_KEY_KP_Add, 0,
382 GTK_SCROLL_STEP_FORWARD);
384 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, 0,
385 GTK_SCROLL_STEP_BACKWARD);
387 add_slider_binding (binding_set, GDK_KEY_KP_Add, GDK_CONTROL_MASK,
388 GTK_SCROLL_PAGE_FORWARD);
390 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
391 GTK_SCROLL_PAGE_BACKWARD);
394 add_slider_binding (binding_set, GDK_KEY_Home, 0,
397 add_slider_binding (binding_set, GDK_KEY_KP_Home, 0,
400 add_slider_binding (binding_set, GDK_KEY_End, 0,
403 add_slider_binding (binding_set, GDK_KEY_KP_End, 0,
406 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
408 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCALE_ACCESSIBLE);
412 gtk_scale_orientation_notify (GtkRange *range,
413 const GParamSpec *pspec)
415 GtkOrientation orientation;
417 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
418 gtk_range_set_flippable (range,
419 orientation == GTK_ORIENTATION_HORIZONTAL);
423 gtk_scale_init (GtkScale *scale)
425 GtkScalePrivate *priv;
426 GtkRange *range = GTK_RANGE (scale);
427 GtkStyleContext *context;
429 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
434 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
436 gtk_range_set_slider_size_fixed (range, TRUE);
438 _gtk_range_set_has_origin (range, TRUE);
440 priv->draw_value = TRUE;
441 priv->value_pos = GTK_POS_TOP;
443 gtk_range_set_round_digits (range, priv->digits);
445 gtk_scale_orientation_notify (range, NULL);
446 g_signal_connect (scale, "notify::orientation",
447 G_CALLBACK (gtk_scale_orientation_notify),
450 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
451 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE);
455 gtk_scale_set_property (GObject *object,
462 scale = GTK_SCALE (object);
467 gtk_scale_set_digits (scale, g_value_get_int (value));
469 case PROP_DRAW_VALUE:
470 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
472 case PROP_HAS_ORIGIN:
473 gtk_scale_set_has_origin (scale, g_value_get_boolean (value));
476 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
479 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
485 gtk_scale_get_property (GObject *object,
490 GtkScale *scale = GTK_SCALE (object);
491 GtkScalePrivate *priv = scale->priv;
496 g_value_set_int (value, priv->digits);
498 case PROP_DRAW_VALUE:
499 g_value_set_boolean (value, priv->draw_value);
501 case PROP_HAS_ORIGIN:
502 g_value_set_boolean (value, gtk_scale_get_has_origin (scale));
505 g_value_set_enum (value, priv->value_pos);
508 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
515 * @orientation: the scale's orientation.
516 * @adjustment: (allow-none): the #GtkAdjustment which sets the range
517 * of the scale, or %NULL to create a new adjustment.
519 * Creates a new #GtkScale.
521 * Return value: a new #GtkScale
526 gtk_scale_new (GtkOrientation orientation,
527 GtkAdjustment *adjustment)
529 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
532 return g_object_new (GTK_TYPE_SCALE,
533 "orientation", orientation,
534 "adjustment", adjustment,
539 * gtk_scale_new_with_range:
540 * @orientation: the scale's orientation.
541 * @min: minimum value
542 * @max: maximum value
543 * @step: step increment (tick size) used with keyboard shortcuts
545 * Creates a new scale widget with the given orientation that lets the
546 * user input a number between @min and @max (including @min and @max)
547 * with the increment @step. @step must be nonzero; it's the distance
548 * the slider moves when using the arrow keys to adjust the scale
551 * Note that the way in which the precision is derived works best if @step
552 * is a power of ten. If the resulting precision is not suitable for your
553 * needs, use gtk_scale_set_digits() to correct it.
555 * Return value: a new #GtkScale
560 gtk_scale_new_with_range (GtkOrientation orientation,
568 g_return_val_if_fail (min < max, NULL);
569 g_return_val_if_fail (step != 0.0, NULL);
571 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
573 if (fabs (step) >= 1.0 || step == 0.0)
579 digits = abs ((gint) floor (log10 (fabs (step))));
584 return g_object_new (GTK_TYPE_SCALE,
585 "orientation", orientation,
592 * gtk_scale_set_digits:
593 * @scale: a #GtkScale
594 * @digits: the number of decimal places to display,
595 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
597 * Sets the number of decimal places that are displayed in the value.
598 * Also causes the value of the adjustment to be rounded off to this
599 * number of digits, so the retrieved value matches the value the user saw.
602 gtk_scale_set_digits (GtkScale *scale,
605 GtkScalePrivate *priv;
608 g_return_if_fail (GTK_IS_SCALE (scale));
611 range = GTK_RANGE (scale);
613 digits = CLAMP (digits, -1, MAX_DIGITS);
615 if (priv->digits != digits)
617 priv->digits = digits;
618 if (priv->draw_value)
619 gtk_range_set_round_digits (range, digits);
621 _gtk_scale_clear_layout (scale);
622 gtk_widget_queue_resize (GTK_WIDGET (scale));
624 g_object_notify (G_OBJECT (scale), "digits");
629 * gtk_scale_get_digits:
630 * @scale: a #GtkScale
632 * Gets the number of decimal places that are displayed in the value.
634 * Returns: the number of decimal places that are displayed
637 gtk_scale_get_digits (GtkScale *scale)
639 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
641 return scale->priv->digits;
645 * gtk_scale_set_draw_value:
646 * @scale: a #GtkScale
647 * @draw_value: %TRUE to draw the value
649 * Specifies whether the current value is displayed as a string next
653 gtk_scale_set_draw_value (GtkScale *scale,
656 GtkScalePrivate *priv;
658 g_return_if_fail (GTK_IS_SCALE (scale));
662 draw_value = draw_value != FALSE;
664 if (priv->draw_value != draw_value)
666 priv->draw_value = draw_value;
668 gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
670 gtk_range_set_round_digits (GTK_RANGE (scale), -1);
672 _gtk_scale_clear_layout (scale);
674 gtk_widget_queue_resize (GTK_WIDGET (scale));
676 g_object_notify (G_OBJECT (scale), "draw-value");
681 * gtk_scale_get_draw_value:
682 * @scale: a #GtkScale
684 * Returns whether the current value is displayed as a string
685 * next to the slider.
687 * Returns: whether the current value is displayed as a string
690 gtk_scale_get_draw_value (GtkScale *scale)
692 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
694 return scale->priv->draw_value;
698 * gtk_scale_set_has_origin:
699 * @scale: a #GtkScale
700 * @has_origin: %TRUE if the scale has an origin
702 * If @has_origin is set to %TRUE (the default),
703 * the scale will highlight the part of the scale
704 * between the origin (bottom or left side) of the scale
705 * and the current value.
710 gtk_scale_set_has_origin (GtkScale *scale,
713 GtkScalePrivate *priv;
715 g_return_if_fail (GTK_IS_SCALE (scale));
719 has_origin = has_origin != FALSE;
721 if (_gtk_range_get_has_origin (GTK_RANGE (scale)) != has_origin)
723 _gtk_range_set_has_origin (GTK_RANGE (scale), has_origin);
725 gtk_widget_queue_draw (GTK_WIDGET (scale));
727 g_object_notify (G_OBJECT (scale), "has-origin");
732 * gtk_scale_get_has_origin:
733 * @scale: a #GtkScale
735 * Returns whether the scale has an origin.
737 * Returns: %TRUE if the scale has an origin.
742 gtk_scale_get_has_origin (GtkScale *scale)
744 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
746 return _gtk_range_get_has_origin (GTK_RANGE (scale));
750 * gtk_scale_set_value_pos:
751 * @scale: a #GtkScale
752 * @pos: the position in which the current value is displayed
754 * Sets the position in which the current value is displayed.
757 gtk_scale_set_value_pos (GtkScale *scale,
760 GtkScalePrivate *priv;
763 g_return_if_fail (GTK_IS_SCALE (scale));
767 if (priv->value_pos != pos)
769 priv->value_pos = pos;
770 widget = GTK_WIDGET (scale);
772 _gtk_scale_clear_layout (scale);
773 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
774 gtk_widget_queue_resize (widget);
776 g_object_notify (G_OBJECT (scale), "value-pos");
781 * gtk_scale_get_value_pos:
782 * @scale: a #GtkScale
784 * Gets the position in which the current value is displayed.
786 * Returns: the position in which the current value is displayed
789 gtk_scale_get_value_pos (GtkScale *scale)
791 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
793 return scale->priv->value_pos;
797 gtk_scale_get_range_border (GtkRange *range,
800 GtkScalePrivate *priv;
805 widget = GTK_WIDGET (range);
806 scale = GTK_SCALE (range);
809 _gtk_scale_get_value_size (scale, &w, &h);
816 if (priv->draw_value)
819 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
821 switch (priv->value_pos)
824 border->left += w + value_spacing;
827 border->right += w + value_spacing;
830 border->top += h + value_spacing;
833 border->bottom += h + value_spacing;
842 gint n1, w1, h1, n2, w2, h2;
844 gtk_widget_style_get (widget,
845 "slider-width", &slider_width,
846 "value-spacing", &value_spacing,
850 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
852 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
855 border->top += h1 + value_spacing + slider_width / 2;
857 border->bottom += h2 + value_spacing + slider_width / 2;
862 border->left += w1 + value_spacing + slider_width / 2;
864 border->right += w2 + value_spacing + slider_width / 2;
869 /* FIXME this could actually be static at the moment. */
871 _gtk_scale_get_value_size (GtkScale *scale,
875 GtkScalePrivate *priv = scale->priv;
878 if (priv->draw_value)
880 GtkAdjustment *adjustment;
882 PangoRectangle logical_rect;
885 range = GTK_RANGE (scale);
887 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
888 adjustment = gtk_range_get_adjustment (range);
890 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
891 pango_layout_set_text (layout, txt, -1);
894 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
897 *width = logical_rect.width;
899 *height = logical_rect.height;
901 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
902 pango_layout_set_text (layout, txt, -1);
905 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
908 *width = MAX (*width, logical_rect.width);
910 *height = MAX (*height, logical_rect.height);
912 g_object_unref (layout);
925 gtk_scale_get_mark_label_size (GtkScale *scale,
926 GtkPositionType position,
934 GtkScalePrivate *priv = scale->priv;
936 PangoRectangle logical_rect;
940 *count1 = *count2 = 0;
941 *width1 = *width2 = 0;
942 *height1 = *height2 = 0;
944 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
946 for (m = priv->marks; m; m = m->next)
948 GtkScaleMark *mark = m->data;
950 if (mark->markup && *mark->markup)
952 pango_layout_set_markup (layout, mark->markup, -1);
953 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
955 w = logical_rect.width;
956 h = logical_rect.height;
964 if (mark->position == position)
967 *width1 = MAX (*width1, w);
968 *height1 = MAX (*height1, h);
973 *width2 = MAX (*width2, w);
974 *height2 = MAX (*height2, h);
978 g_object_unref (layout);
982 gtk_scale_style_updated (GtkWidget *widget)
987 range = GTK_RANGE (widget);
989 gtk_widget_style_get (widget,
990 "slider-length", &slider_length,
993 gtk_range_set_min_slider_size (range, slider_length);
995 _gtk_scale_clear_layout (GTK_SCALE (widget));
997 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_updated (widget);
1001 gtk_scale_screen_changed (GtkWidget *widget,
1002 GdkScreen *old_screen)
1004 _gtk_scale_clear_layout (GTK_SCALE (widget));
1008 gtk_scale_get_preferred_width (GtkWidget *widget,
1012 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural);
1014 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
1016 gint n1, w1, h1, n2, w2, h2;
1020 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
1022 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
1024 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
1025 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
1028 *minimum = MAX (*minimum, w);
1029 *natural = MAX (*natural, w);
1034 gtk_scale_get_preferred_height (GtkWidget *widget,
1038 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural);
1041 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
1043 gint n1, w1, h1, n2, w2, h2;
1047 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
1049 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
1050 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
1051 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
1054 *minimum = MAX (*minimum, h);
1055 *natural = MAX (*natural, h);
1060 find_next_pos (GtkWidget *widget,
1063 GtkPositionType pos,
1066 GtkAllocation allocation;
1070 for (m = list->next, i = 1; m; m = m->next, i++)
1072 GtkScaleMark *mark = m->data;
1074 if (match == (mark->position == pos))
1078 gtk_widget_get_allocation (widget, &allocation);
1079 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
1080 return allocation.width;
1082 return allocation.height;
1086 gtk_scale_draw (GtkWidget *widget,
1089 GtkScale *scale = GTK_SCALE (widget);
1090 GtkScalePrivate *priv = scale->priv;
1091 GtkRange *range = GTK_RANGE (scale);
1092 GtkStateFlags state = 0;
1093 GtkStyleContext *context;
1100 context = gtk_widget_get_style_context (widget);
1101 gtk_widget_style_get (widget,
1102 "focus-padding", &focus_padding,
1103 "slider-width", &slider_width,
1104 "value-spacing", &value_spacing,
1107 /* We need to chain up _first_ so the various geometry members of
1108 * GtkRange struct are updated.
1110 GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1112 if (!gtk_widget_is_sensitive (widget))
1113 state |= GTK_STATE_FLAG_INSENSITIVE;
1117 GtkOrientation orientation;
1118 GdkRectangle range_rect;
1120 gint x1, x2, x3, y1, y2, y3;
1121 PangoLayout *layout;
1122 PangoRectangle logical_rect;
1124 gint min_pos_before, min_pos_after;
1125 gint min_pos, max_pos;
1127 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1128 _gtk_range_get_stop_positions (range, &marks);
1129 layout = gtk_widget_create_pango_layout (widget, NULL);
1130 gtk_range_get_range_rect (range, &range_rect);
1132 min_pos_before = min_pos_after = 0;
1134 for (m = priv->marks, i = 0; m; m = m->next, i++)
1136 GtkScaleMark *mark = m->data;
1138 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1141 if (mark->position == GTK_POS_TOP)
1144 y2 = y1 - slider_width / 2;
1145 min_pos = min_pos_before;
1146 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1150 y1 = range_rect.y + range_rect.height;
1151 y2 = y1 + slider_width / 2;
1152 min_pos = min_pos_after;
1153 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM, 1) - min_sep;
1156 gtk_style_context_save (context);
1157 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1158 gtk_style_context_set_state (context, state);
1160 gtk_render_line (context, cr, x1, y1, x1, y2);
1164 pango_layout_set_markup (layout, mark->markup, -1);
1165 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1167 x3 = x1 - logical_rect.width / 2;
1170 if (x3 + logical_rect.width > max_pos)
1171 x3 = max_pos - logical_rect.width;
1174 if (mark->position == GTK_POS_TOP)
1176 y3 = y2 - value_spacing - logical_rect.height;
1177 min_pos_before = x3 + logical_rect.width + min_sep;
1181 y3 = y2 + value_spacing;
1182 min_pos_after = x3 + logical_rect.width + min_sep;
1185 gtk_render_layout (context, cr, x3, y3, layout);
1188 gtk_style_context_restore (context);
1192 if (mark->position == GTK_POS_TOP)
1195 x2 = range_rect.x - slider_width / 2;
1196 min_pos = min_pos_before;
1197 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1201 x1 = range_rect.x + range_rect.width;
1202 x2 = range_rect.x + range_rect.width + slider_width / 2;
1203 min_pos = min_pos_after;
1204 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM, 1) - min_sep;
1208 gtk_style_context_save (context);
1209 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1210 gtk_style_context_set_state (context, state);
1212 gtk_render_line (context, cr, x1, y1, x2, y1);
1216 pango_layout_set_markup (layout, mark->markup, -1);
1217 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1219 y3 = y1 - logical_rect.height / 2;
1222 if (y3 + logical_rect.height > max_pos)
1223 y3 = max_pos - logical_rect.height;
1226 if (mark->position == GTK_POS_TOP)
1228 x3 = x2 - value_spacing - logical_rect.width;
1229 min_pos_before = y3 + logical_rect.height + min_sep;
1233 x3 = x2 + value_spacing;
1234 min_pos_after = y3 + logical_rect.height + min_sep;
1237 gtk_render_layout (context, cr, x3, y3, layout);
1240 gtk_style_context_restore (context);
1244 g_object_unref (layout);
1248 if (priv->draw_value)
1250 GtkAllocation allocation;
1252 PangoLayout *layout;
1255 layout = gtk_scale_get_layout (scale);
1256 gtk_scale_get_layout_offsets (scale, &x, &y);
1257 gtk_widget_get_allocation (widget, &allocation);
1259 gtk_render_layout (context, cr,
1269 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1273 GtkScalePrivate *priv = scale->priv;
1274 GtkAllocation allocation;
1275 GtkWidget *widget = GTK_WIDGET (scale);
1276 GtkRange *range = GTK_RANGE (widget);
1277 GdkRectangle range_rect;
1278 PangoLayout *layout = gtk_scale_get_layout (scale);
1279 PangoRectangle logical_rect;
1280 gint slider_start, slider_end;
1291 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1293 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1295 gtk_widget_get_allocation (widget, &allocation);
1296 gtk_range_get_range_rect (range, &range_rect);
1297 gtk_range_get_slider_range (range,
1301 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1303 switch (priv->value_pos)
1306 *x = range_rect.x - value_spacing - logical_rect.width;
1307 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1311 *x = range_rect.x + range_rect.width + value_spacing;
1312 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1316 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1317 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1318 *y = range_rect.y - logical_rect.height - value_spacing;
1321 case GTK_POS_BOTTOM:
1322 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1323 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1324 *y = range_rect.y + range_rect.height + value_spacing;
1328 g_return_if_reached ();
1334 switch (priv->value_pos)
1337 *x = range_rect.x - logical_rect.width - value_spacing;
1338 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1339 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1343 *x = range_rect.x + range_rect.width + value_spacing;
1344 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1345 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1349 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1350 *y = range_rect.y - logical_rect.height - value_spacing;
1353 case GTK_POS_BOTTOM:
1354 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1355 *y = range_rect.y + range_rect.height + value_spacing;
1359 g_return_if_reached ();
1368 * _gtk_scale_format_value:
1369 * @scale: a #GtkScale
1370 * @value: adjustment value
1372 * Emits #GtkScale::format-value signal to format the value,
1373 * if no user signal handlers, falls back to a default format.
1375 * Return value: formatted value
1378 _gtk_scale_format_value (GtkScale *scale,
1381 GtkScalePrivate *priv = scale->priv;
1384 g_signal_emit (scale,
1385 signals[FORMAT_VALUE],
1393 return g_strdup_printf ("%0.*f", priv->digits, value);
1397 gtk_scale_finalize (GObject *object)
1399 GtkScale *scale = GTK_SCALE (object);
1401 _gtk_scale_clear_layout (scale);
1402 gtk_scale_clear_marks (scale);
1404 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1408 * gtk_scale_get_layout:
1409 * @scale: A #GtkScale
1411 * Gets the #PangoLayout used to display the scale. The returned
1412 * object is owned by the scale so does not need to be freed by
1415 * Return value: (transfer none): the #PangoLayout for this scale,
1416 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1421 gtk_scale_get_layout (GtkScale *scale)
1423 GtkScalePrivate *priv;
1426 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1432 if (priv->draw_value)
1433 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1436 if (priv->draw_value)
1438 txt = _gtk_scale_format_value (scale,
1439 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1440 pango_layout_set_text (priv->layout, txt, -1);
1444 return priv->layout;
1448 * gtk_scale_get_layout_offsets:
1449 * @scale: a #GtkScale
1450 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
1451 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
1453 * Obtains the coordinates where the scale will draw the
1454 * #PangoLayout representing the text in the scale. Remember
1455 * when using the #PangoLayout function you need to convert to
1456 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1458 * If the #GtkScale:draw-value property is %FALSE, the return
1459 * values are undefined.
1464 gtk_scale_get_layout_offsets (GtkScale *scale,
1471 g_return_if_fail (GTK_IS_SCALE (scale));
1473 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1474 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1484 _gtk_scale_clear_layout (GtkScale *scale)
1486 GtkScalePrivate *priv = scale->priv;
1488 g_return_if_fail (GTK_IS_SCALE (scale));
1492 g_object_unref (priv->layout);
1493 priv->layout = NULL;
1498 gtk_scale_mark_free (GtkScaleMark *mark)
1500 g_free (mark->markup);
1505 * gtk_scale_clear_marks:
1506 * @scale: a #GtkScale
1508 * Removes any marks that have been added with gtk_scale_add_mark().
1513 gtk_scale_clear_marks (GtkScale *scale)
1515 GtkScalePrivate *priv;
1516 GtkStyleContext *context;
1518 g_return_if_fail (GTK_IS_SCALE (scale));
1522 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1523 g_slist_free (priv->marks);
1526 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1527 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1528 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1530 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1532 gtk_widget_queue_resize (GTK_WIDGET (scale));
1536 compare_marks (gpointer a, gpointer b)
1538 GtkScaleMark *ma, *mb;
1542 return (gint) (ma->value - mb->value);
1546 * gtk_scale_add_mark:
1547 * @scale: a #GtkScale
1548 * @value: the value at which the mark is placed, must be between
1549 * the lower and upper limits of the scales' adjustment
1550 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1551 * and %GTK_POS_LEFT are drawn above the scale, anything else below.
1552 * For a vertical scale, #GTK_POS_LEFT and %GTK_POS_TOP are drawn to
1553 * the left of the scale, anything else to the right.
1554 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1557 * Adds a mark at @value.
1559 * A mark is indicated visually by drawing a tick mark next to the scale,
1560 * and GTK+ makes it easy for the user to position the scale exactly at the
1563 * If @markup is not %NULL, text is shown next to the tick mark.
1565 * To remove marks from a scale, use gtk_scale_clear_marks().
1570 gtk_scale_add_mark (GtkScale *scale,
1572 GtkPositionType position,
1573 const gchar *markup)
1575 GtkScalePrivate *priv;
1580 GtkStyleContext *context;
1583 g_return_if_fail (GTK_IS_SCALE (scale));
1587 mark = g_new (GtkScaleMark, 1);
1588 mark->value = value;
1589 mark->markup = g_strdup (markup);
1590 if (position == GTK_POS_LEFT ||
1591 position == GTK_POS_TOP)
1592 mark->position = GTK_POS_TOP;
1594 mark->position = GTK_POS_BOTTOM;
1596 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1597 (GCompareFunc) compare_marks);
1599 #define MARKS_ABOVE 1
1600 #define MARKS_BELOW 2
1603 n = g_slist_length (priv->marks);
1604 values = g_new (gdouble, n);
1605 for (m = priv->marks, i = 0; m; m = m->next, i++)
1608 values[i] = mark->value;
1609 if (mark->position == GTK_POS_TOP)
1610 all_pos |= MARKS_ABOVE;
1612 all_pos |= MARKS_BELOW;
1615 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1619 /* Set the style classes for the slider, so it could
1620 * point to the right direction when marks are present
1622 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1624 if (all_pos & MARKS_ABOVE)
1625 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1627 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1628 if (all_pos & MARKS_BELOW)
1629 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1631 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1633 gtk_widget_queue_resize (GTK_WIDGET (scale));
1636 static GtkBuildableIface *parent_buildable_iface;
1639 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1641 parent_buildable_iface = g_type_interface_peek_parent (iface);
1642 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1643 iface->custom_finished = gtk_scale_buildable_custom_finished;
1649 GtkBuilder *builder;
1651 } MarksSubparserData;
1656 GtkPositionType position;
1659 gboolean translatable;
1663 mark_data_free (MarkData *data)
1665 g_string_free (data->markup, TRUE);
1666 g_free (data->context);
1667 g_slice_free (MarkData, data);
1671 marks_start_element (GMarkupParseContext *context,
1672 const gchar *element_name,
1673 const gchar **names,
1674 const gchar **values,
1678 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1680 gint line_number, char_number;
1682 if (strcmp (element_name, "marks") == 0)
1684 else if (strcmp (element_name, "mark") == 0)
1687 gboolean has_value = FALSE;
1688 GtkPositionType position = GTK_POS_BOTTOM;
1689 const gchar *msg_context = NULL;
1690 gboolean translatable = FALSE;
1693 for (i = 0; names[i]; i++)
1695 if (strcmp (names[i], "translatable") == 0)
1697 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1700 else if (strcmp (names[i], "comments") == 0)
1702 /* do nothing, comments are for translators */
1704 else if (strcmp (names[i], "context") == 0)
1705 msg_context = values[i];
1706 else if (strcmp (names[i], "value") == 0)
1708 GValue gvalue = G_VALUE_INIT;
1710 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1713 value = g_value_get_double (&gvalue);
1716 else if (strcmp (names[i], "position") == 0)
1718 GValue gvalue = G_VALUE_INIT;
1720 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1723 position = g_value_get_enum (&gvalue);
1727 g_markup_parse_context_get_position (context,
1732 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1733 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1735 line_number, char_number, names[i], "mark");
1742 g_markup_parse_context_get_position (context,
1747 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1748 "%s:%d:%d <%s> requires attribute \"%s\"",
1750 line_number, char_number, "mark",
1755 mark = g_slice_new (MarkData);
1756 mark->value = value;
1757 if (position == GTK_POS_LEFT ||
1758 position == GTK_POS_TOP)
1759 mark->position = GTK_POS_TOP;
1761 mark->position = GTK_POS_BOTTOM;
1762 mark->markup = g_string_new ("");
1763 mark->context = g_strdup (msg_context);
1764 mark->translatable = translatable;
1766 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1770 g_markup_parse_context_get_position (context,
1775 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1776 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1778 line_number, char_number, element_name);
1784 marks_text (GMarkupParseContext *context,
1790 MarksSubparserData *data = (MarksSubparserData*)user_data;
1792 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1794 MarkData *mark = data->marks->data;
1796 g_string_append_len (mark->markup, text, text_len);
1800 static const GMarkupParser marks_parser =
1802 marks_start_element,
1809 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1810 GtkBuilder *builder,
1812 const gchar *tagname,
1813 GMarkupParser *parser,
1816 MarksSubparserData *parser_data;
1821 if (strcmp (tagname, "marks") == 0)
1823 parser_data = g_slice_new0 (MarksSubparserData);
1824 parser_data->scale = GTK_SCALE (buildable);
1825 parser_data->marks = NULL;
1827 *parser = marks_parser;
1828 *data = parser_data;
1832 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1833 tagname, parser, data);
1837 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1838 GtkBuilder *builder,
1840 const gchar *tagname,
1843 GtkScale *scale = GTK_SCALE (buildable);
1844 MarksSubparserData *marks_data;
1846 if (strcmp (tagname, "marks") == 0)
1851 marks_data = (MarksSubparserData *)user_data;
1853 for (m = marks_data->marks; m; m = m->next)
1855 MarkData *mdata = m->data;
1857 if (mdata->translatable && mdata->markup->len)
1858 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1860 mdata->markup->str);
1862 markup = mdata->markup->str;
1864 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1866 mark_data_free (mdata);
1869 g_slist_free (marks_data->marks);
1870 g_slice_free (MarksSubparserData, marks_data);