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 */
115 static guint signals[LAST_SIGNAL];
117 static void gtk_scale_set_property (GObject *object,
121 static void gtk_scale_get_property (GObject *object,
125 static void gtk_scale_get_preferred_width (GtkWidget *widget,
128 static void gtk_scale_get_preferred_height (GtkWidget *widget,
131 static void gtk_scale_style_updated (GtkWidget *widget);
132 static void gtk_scale_get_range_border (GtkRange *range,
134 static void gtk_scale_get_mark_label_size (GtkScale *scale,
135 GtkPositionType position,
142 static void gtk_scale_finalize (GObject *object);
143 static void gtk_scale_screen_changed (GtkWidget *widget,
144 GdkScreen *old_screen);
145 static gboolean gtk_scale_draw (GtkWidget *widget,
147 static void gtk_scale_real_get_layout_offsets (GtkScale *scale,
150 static void gtk_scale_buildable_interface_init (GtkBuildableIface *iface);
151 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
154 const gchar *tagname,
155 GMarkupParser *parser,
157 static void gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
160 const gchar *tagname,
164 G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
165 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
166 gtk_scale_buildable_interface_init))
170 single_string_accumulator (GSignalInvocationHint *ihint,
172 const GValue *handler_return,
175 gboolean continue_emission;
178 str = g_value_get_string (handler_return);
179 g_value_set_string (return_accu, str);
180 continue_emission = str == NULL;
182 return continue_emission;
186 #define add_slider_binding(binding_set, keyval, mask, scroll) \
187 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
188 I_("move-slider"), 1, \
189 GTK_TYPE_SCROLL_TYPE, scroll)
192 gtk_scale_class_init (GtkScaleClass *class)
194 GObjectClass *gobject_class;
195 GtkWidgetClass *widget_class;
196 GtkRangeClass *range_class;
197 GtkBindingSet *binding_set;
199 gobject_class = G_OBJECT_CLASS (class);
200 range_class = (GtkRangeClass*) class;
201 widget_class = (GtkWidgetClass*) class;
203 gobject_class->set_property = gtk_scale_set_property;
204 gobject_class->get_property = gtk_scale_get_property;
205 gobject_class->finalize = gtk_scale_finalize;
207 widget_class->style_updated = gtk_scale_style_updated;
208 widget_class->screen_changed = gtk_scale_screen_changed;
209 widget_class->draw = gtk_scale_draw;
210 widget_class->get_preferred_width = gtk_scale_get_preferred_width;
211 widget_class->get_preferred_height = gtk_scale_get_preferred_height;
213 range_class->slider_detail = "Xscale";
214 range_class->get_range_border = gtk_scale_get_range_border;
216 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
219 * GtkScale::format-value:
220 * @scale: the object which received the signal
221 * @value: the value to format
223 * Signal which allows you to change how the scale value is displayed.
224 * Connect a signal handler which returns an allocated string representing
225 * @value. That string will then be used to display the scale's value.
227 * Here's an example signal handler which displays a value 1.0 as
228 * with "-->1.0<--".
231 * format_value_callback (GtkScale *scale,
234 * return g_strdup_printf ("-->%0.*g<--",
235 * gtk_scale_get_digits (scale), value);
239 * Return value: allocated string representing @value
241 signals[FORMAT_VALUE] =
242 g_signal_new (I_("format-value"),
243 G_TYPE_FROM_CLASS (gobject_class),
245 G_STRUCT_OFFSET (GtkScaleClass, format_value),
246 single_string_accumulator, NULL,
247 _gtk_marshal_STRING__DOUBLE,
251 g_object_class_install_property (gobject_class,
253 g_param_spec_int ("digits",
255 P_("The number of decimal places that are displayed in the value"),
259 GTK_PARAM_READWRITE));
261 g_object_class_install_property (gobject_class,
263 g_param_spec_boolean ("draw-value",
265 P_("Whether the current value is displayed as a string next to the slider"),
267 GTK_PARAM_READWRITE));
269 g_object_class_install_property (gobject_class,
271 g_param_spec_enum ("value-pos",
272 P_("Value Position"),
273 P_("The position in which the current value is displayed"),
274 GTK_TYPE_POSITION_TYPE,
276 GTK_PARAM_READWRITE));
278 gtk_widget_class_install_style_property (widget_class,
279 g_param_spec_int ("slider-length",
281 P_("Length of scale's slider"),
285 GTK_PARAM_READABLE));
287 gtk_widget_class_install_style_property (widget_class,
288 g_param_spec_int ("value-spacing",
290 P_("Space between value text and the slider/trough area"),
294 GTK_PARAM_READABLE));
296 /* All bindings (even arrow keys) are on both h/v scale, because
297 * blind users etc. don't care about scale orientation.
300 binding_set = gtk_binding_set_by_class (class);
302 add_slider_binding (binding_set, GDK_KEY_Left, 0,
303 GTK_SCROLL_STEP_LEFT);
305 add_slider_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
306 GTK_SCROLL_PAGE_LEFT);
308 add_slider_binding (binding_set, GDK_KEY_KP_Left, 0,
309 GTK_SCROLL_STEP_LEFT);
311 add_slider_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
312 GTK_SCROLL_PAGE_LEFT);
314 add_slider_binding (binding_set, GDK_KEY_Right, 0,
315 GTK_SCROLL_STEP_RIGHT);
317 add_slider_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
318 GTK_SCROLL_PAGE_RIGHT);
320 add_slider_binding (binding_set, GDK_KEY_KP_Right, 0,
321 GTK_SCROLL_STEP_RIGHT);
323 add_slider_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
324 GTK_SCROLL_PAGE_RIGHT);
326 add_slider_binding (binding_set, GDK_KEY_Up, 0,
329 add_slider_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
332 add_slider_binding (binding_set, GDK_KEY_KP_Up, 0,
335 add_slider_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
338 add_slider_binding (binding_set, GDK_KEY_Down, 0,
339 GTK_SCROLL_STEP_DOWN);
341 add_slider_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
342 GTK_SCROLL_PAGE_DOWN);
344 add_slider_binding (binding_set, GDK_KEY_KP_Down, 0,
345 GTK_SCROLL_STEP_DOWN);
347 add_slider_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
348 GTK_SCROLL_PAGE_DOWN);
350 add_slider_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
351 GTK_SCROLL_PAGE_LEFT);
353 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
354 GTK_SCROLL_PAGE_LEFT);
356 add_slider_binding (binding_set, GDK_KEY_Page_Up, 0,
359 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
362 add_slider_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
363 GTK_SCROLL_PAGE_RIGHT);
365 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
366 GTK_SCROLL_PAGE_RIGHT);
368 add_slider_binding (binding_set, GDK_KEY_Page_Down, 0,
369 GTK_SCROLL_PAGE_DOWN);
371 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
372 GTK_SCROLL_PAGE_DOWN);
374 /* Logical bindings (vs. visual bindings above) */
376 add_slider_binding (binding_set, GDK_KEY_plus, 0,
377 GTK_SCROLL_STEP_FORWARD);
379 add_slider_binding (binding_set, GDK_KEY_minus, 0,
380 GTK_SCROLL_STEP_BACKWARD);
382 add_slider_binding (binding_set, GDK_KEY_plus, GDK_CONTROL_MASK,
383 GTK_SCROLL_PAGE_FORWARD);
385 add_slider_binding (binding_set, GDK_KEY_minus, GDK_CONTROL_MASK,
386 GTK_SCROLL_PAGE_BACKWARD);
389 add_slider_binding (binding_set, GDK_KEY_KP_Add, 0,
390 GTK_SCROLL_STEP_FORWARD);
392 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, 0,
393 GTK_SCROLL_STEP_BACKWARD);
395 add_slider_binding (binding_set, GDK_KEY_KP_Add, GDK_CONTROL_MASK,
396 GTK_SCROLL_PAGE_FORWARD);
398 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
399 GTK_SCROLL_PAGE_BACKWARD);
402 add_slider_binding (binding_set, GDK_KEY_Home, 0,
405 add_slider_binding (binding_set, GDK_KEY_KP_Home, 0,
408 add_slider_binding (binding_set, GDK_KEY_End, 0,
411 add_slider_binding (binding_set, GDK_KEY_KP_End, 0,
414 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
416 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCALE_ACCESSIBLE);
420 gtk_scale_orientation_notify (GtkRange *range,
421 const GParamSpec *pspec)
423 GtkOrientation orientation;
425 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
426 gtk_range_set_flippable (range,
427 orientation == GTK_ORIENTATION_HORIZONTAL);
431 gtk_scale_init (GtkScale *scale)
433 GtkScalePrivate *priv;
434 GtkRange *range = GTK_RANGE (scale);
435 GtkStyleContext *context;
437 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
442 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
444 gtk_range_set_slider_size_fixed (range, TRUE);
446 priv->draw_value = TRUE;
447 priv->value_pos = GTK_POS_TOP;
449 gtk_range_set_round_digits (range, priv->digits);
451 gtk_scale_orientation_notify (range, NULL);
452 g_signal_connect (scale, "notify::orientation",
453 G_CALLBACK (gtk_scale_orientation_notify),
456 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
457 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE);
461 gtk_scale_set_property (GObject *object,
468 scale = GTK_SCALE (object);
473 gtk_scale_set_digits (scale, g_value_get_int (value));
475 case PROP_DRAW_VALUE:
476 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
479 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
482 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
488 gtk_scale_get_property (GObject *object,
493 GtkScale *scale = GTK_SCALE (object);
494 GtkScalePrivate *priv = scale->priv;
499 g_value_set_int (value, priv->digits);
501 case PROP_DRAW_VALUE:
502 g_value_set_boolean (value, priv->draw_value);
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_value_pos:
699 * @scale: a #GtkScale
700 * @pos: the position in which the current value is displayed
702 * Sets the position in which the current value is displayed.
705 gtk_scale_set_value_pos (GtkScale *scale,
708 GtkScalePrivate *priv;
711 g_return_if_fail (GTK_IS_SCALE (scale));
715 if (priv->value_pos != pos)
717 priv->value_pos = pos;
718 widget = GTK_WIDGET (scale);
720 _gtk_scale_clear_layout (scale);
721 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
722 gtk_widget_queue_resize (widget);
724 g_object_notify (G_OBJECT (scale), "value-pos");
729 * gtk_scale_get_value_pos:
730 * @scale: a #GtkScale
732 * Gets the position in which the current value is displayed.
734 * Returns: the position in which the current value is displayed
737 gtk_scale_get_value_pos (GtkScale *scale)
739 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
741 return scale->priv->value_pos;
745 gtk_scale_get_range_border (GtkRange *range,
748 GtkScalePrivate *priv;
753 widget = GTK_WIDGET (range);
754 scale = GTK_SCALE (range);
757 _gtk_scale_get_value_size (scale, &w, &h);
764 if (priv->draw_value)
767 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
769 switch (priv->value_pos)
772 border->left += w + value_spacing;
775 border->right += w + value_spacing;
778 border->top += h + value_spacing;
781 border->bottom += h + value_spacing;
790 gint n1, w1, h1, n2, w2, h2;
792 gtk_widget_style_get (widget,
793 "slider-width", &slider_width,
794 "value-spacing", &value_spacing,
798 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
800 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
803 border->top += h1 + value_spacing + slider_width / 2;
805 border->bottom += h2 + value_spacing + slider_width / 2;
810 border->left += w1 + value_spacing + slider_width / 2;
812 border->right += w2 + value_spacing + slider_width / 2;
817 /* FIXME this could actually be static at the moment. */
819 _gtk_scale_get_value_size (GtkScale *scale,
823 GtkScalePrivate *priv = scale->priv;
826 if (priv->draw_value)
828 GtkAdjustment *adjustment;
830 PangoRectangle logical_rect;
833 range = GTK_RANGE (scale);
835 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
836 adjustment = gtk_range_get_adjustment (range);
838 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
839 pango_layout_set_text (layout, txt, -1);
842 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
845 *width = logical_rect.width;
847 *height = logical_rect.height;
849 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
850 pango_layout_set_text (layout, txt, -1);
853 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
856 *width = MAX (*width, logical_rect.width);
858 *height = MAX (*height, logical_rect.height);
860 g_object_unref (layout);
873 gtk_scale_get_mark_label_size (GtkScale *scale,
874 GtkPositionType position,
882 GtkScalePrivate *priv = scale->priv;
884 PangoRectangle logical_rect;
888 *count1 = *count2 = 0;
889 *width1 = *width2 = 0;
890 *height1 = *height2 = 0;
892 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
894 for (m = priv->marks; m; m = m->next)
896 GtkScaleMark *mark = m->data;
898 if (mark->markup && *mark->markup)
900 pango_layout_set_markup (layout, mark->markup, -1);
901 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
903 w = logical_rect.width;
904 h = logical_rect.height;
912 if (mark->position == position)
915 *width1 = MAX (*width1, w);
916 *height1 = MAX (*height1, h);
921 *width2 = MAX (*width2, w);
922 *height2 = MAX (*height2, h);
926 g_object_unref (layout);
930 gtk_scale_style_updated (GtkWidget *widget)
935 range = GTK_RANGE (widget);
937 gtk_widget_style_get (widget,
938 "slider-length", &slider_length,
941 gtk_range_set_min_slider_size (range, slider_length);
943 _gtk_scale_clear_layout (GTK_SCALE (widget));
945 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_updated (widget);
949 gtk_scale_screen_changed (GtkWidget *widget,
950 GdkScreen *old_screen)
952 _gtk_scale_clear_layout (GTK_SCALE (widget));
956 gtk_scale_get_preferred_width (GtkWidget *widget,
960 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural);
962 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
964 gint n1, w1, h1, n2, w2, h2;
968 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
970 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
972 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
973 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
976 *minimum = MAX (*minimum, w);
977 *natural = MAX (*natural, w);
982 gtk_scale_get_preferred_height (GtkWidget *widget,
986 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural);
989 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
991 gint n1, w1, h1, n2, w2, h2;
995 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
997 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
998 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
999 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
1002 *minimum = MAX (*minimum, h);
1003 *natural = MAX (*natural, h);
1008 find_next_pos (GtkWidget *widget,
1011 GtkPositionType pos,
1014 GtkAllocation allocation;
1018 for (m = list->next, i = 1; m; m = m->next, i++)
1020 GtkScaleMark *mark = m->data;
1022 if (match == (mark->position == pos))
1026 gtk_widget_get_allocation (widget, &allocation);
1027 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
1028 return allocation.width;
1030 return allocation.height;
1034 gtk_scale_draw (GtkWidget *widget,
1037 GtkScale *scale = GTK_SCALE (widget);
1038 GtkScalePrivate *priv = scale->priv;
1039 GtkRange *range = GTK_RANGE (scale);
1040 GtkStateFlags state = 0;
1041 GtkStyleContext *context;
1048 context = gtk_widget_get_style_context (widget);
1049 gtk_widget_style_get (widget,
1050 "focus-padding", &focus_padding,
1051 "slider-width", &slider_width,
1052 "value-spacing", &value_spacing,
1055 /* We need to chain up _first_ so the various geometry members of
1056 * GtkRange struct are updated.
1058 GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1060 if (!gtk_widget_is_sensitive (widget))
1061 state |= GTK_STATE_FLAG_INSENSITIVE;
1065 GtkOrientation orientation;
1066 GdkRectangle range_rect;
1068 gint x1, x2, x3, y1, y2, y3;
1069 PangoLayout *layout;
1070 PangoRectangle logical_rect;
1072 gint min_pos_before, min_pos_after;
1073 gint min_pos, max_pos;
1075 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1076 _gtk_range_get_stop_positions (range, &marks);
1077 layout = gtk_widget_create_pango_layout (widget, NULL);
1078 gtk_range_get_range_rect (range, &range_rect);
1080 min_pos_before = min_pos_after = 0;
1082 for (m = priv->marks, i = 0; m; m = m->next, i++)
1084 GtkScaleMark *mark = m->data;
1086 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1089 if (mark->position == GTK_POS_TOP)
1092 y2 = y1 - slider_width / 2;
1093 min_pos = min_pos_before;
1094 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1098 y1 = range_rect.y + range_rect.height;
1099 y2 = y1 + slider_width / 2;
1100 min_pos = min_pos_after;
1101 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM, 1) - min_sep;
1104 gtk_style_context_save (context);
1105 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1106 gtk_style_context_set_state (context, state);
1108 gtk_render_line (context, cr, x1, y1, x1, y2);
1112 pango_layout_set_markup (layout, mark->markup, -1);
1113 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1115 x3 = x1 - logical_rect.width / 2;
1118 if (x3 + logical_rect.width > max_pos)
1119 x3 = max_pos - logical_rect.width;
1122 if (mark->position == GTK_POS_TOP)
1124 y3 = y2 - value_spacing - logical_rect.height;
1125 min_pos_before = x3 + logical_rect.width + min_sep;
1129 y3 = y2 + value_spacing;
1130 min_pos_after = x3 + logical_rect.width + min_sep;
1133 gtk_render_layout (context, cr, x3, y3, layout);
1136 gtk_style_context_restore (context);
1140 if (mark->position == GTK_POS_TOP)
1143 x2 = range_rect.x - slider_width / 2;
1144 min_pos = min_pos_before;
1145 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1149 x1 = range_rect.x + range_rect.width;
1150 x2 = range_rect.x + range_rect.width + slider_width / 2;
1151 min_pos = min_pos_after;
1152 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, x2, y1);
1164 pango_layout_set_markup (layout, mark->markup, -1);
1165 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1167 y3 = y1 - logical_rect.height / 2;
1170 if (y3 + logical_rect.height > max_pos)
1171 y3 = max_pos - logical_rect.height;
1174 if (mark->position == GTK_POS_TOP)
1176 x3 = x2 - value_spacing - logical_rect.width;
1177 min_pos_before = y3 + logical_rect.height + min_sep;
1181 x3 = x2 + value_spacing;
1182 min_pos_after = y3 + logical_rect.height + min_sep;
1185 gtk_render_layout (context, cr, x3, y3, layout);
1188 gtk_style_context_restore (context);
1192 g_object_unref (layout);
1196 if (priv->draw_value)
1198 GtkAllocation allocation;
1200 PangoLayout *layout;
1203 layout = gtk_scale_get_layout (scale);
1204 gtk_scale_get_layout_offsets (scale, &x, &y);
1205 gtk_widget_get_allocation (widget, &allocation);
1207 gtk_render_layout (context, cr,
1217 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1221 GtkScalePrivate *priv = scale->priv;
1222 GtkAllocation allocation;
1223 GtkWidget *widget = GTK_WIDGET (scale);
1224 GtkRange *range = GTK_RANGE (widget);
1225 GdkRectangle range_rect;
1226 PangoLayout *layout = gtk_scale_get_layout (scale);
1227 PangoRectangle logical_rect;
1228 gint slider_start, slider_end;
1239 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1241 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1243 gtk_widget_get_allocation (widget, &allocation);
1244 gtk_range_get_range_rect (range, &range_rect);
1245 gtk_range_get_slider_range (range,
1249 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1251 switch (priv->value_pos)
1254 *x = range_rect.x - value_spacing - logical_rect.width;
1255 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1259 *x = range_rect.x + range_rect.width + value_spacing;
1260 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1264 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1265 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1266 *y = range_rect.y - logical_rect.height - value_spacing;
1269 case GTK_POS_BOTTOM:
1270 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1271 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1272 *y = range_rect.y + range_rect.height + value_spacing;
1276 g_return_if_reached ();
1282 switch (priv->value_pos)
1285 *x = range_rect.x - logical_rect.width - value_spacing;
1286 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1287 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1291 *x = range_rect.x + range_rect.width + value_spacing;
1292 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1293 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1297 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1298 *y = range_rect.y - logical_rect.height - value_spacing;
1301 case GTK_POS_BOTTOM:
1302 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1303 *y = range_rect.y + range_rect.height + value_spacing;
1307 g_return_if_reached ();
1316 * _gtk_scale_format_value:
1317 * @scale: a #GtkScale
1318 * @value: adjustment value
1320 * Emits #GtkScale::format-value signal to format the value,
1321 * if no user signal handlers, falls back to a default format.
1323 * Return value: formatted value
1326 _gtk_scale_format_value (GtkScale *scale,
1329 GtkScalePrivate *priv = scale->priv;
1332 g_signal_emit (scale,
1333 signals[FORMAT_VALUE],
1341 return g_strdup_printf ("%0.*f", priv->digits, value);
1345 gtk_scale_finalize (GObject *object)
1347 GtkScale *scale = GTK_SCALE (object);
1349 _gtk_scale_clear_layout (scale);
1350 gtk_scale_clear_marks (scale);
1352 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1356 * gtk_scale_get_layout:
1357 * @scale: A #GtkScale
1359 * Gets the #PangoLayout used to display the scale. The returned
1360 * object is owned by the scale so does not need to be freed by
1363 * Return value: (transfer none): the #PangoLayout for this scale,
1364 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1369 gtk_scale_get_layout (GtkScale *scale)
1371 GtkScalePrivate *priv;
1374 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1380 if (priv->draw_value)
1381 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1384 if (priv->draw_value)
1386 txt = _gtk_scale_format_value (scale,
1387 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1388 pango_layout_set_text (priv->layout, txt, -1);
1392 return priv->layout;
1396 * gtk_scale_get_layout_offsets:
1397 * @scale: a #GtkScale
1398 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
1399 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
1401 * Obtains the coordinates where the scale will draw the
1402 * #PangoLayout representing the text in the scale. Remember
1403 * when using the #PangoLayout function you need to convert to
1404 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1406 * If the #GtkScale:draw-value property is %FALSE, the return
1407 * values are undefined.
1412 gtk_scale_get_layout_offsets (GtkScale *scale,
1419 g_return_if_fail (GTK_IS_SCALE (scale));
1421 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1422 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1432 _gtk_scale_clear_layout (GtkScale *scale)
1434 GtkScalePrivate *priv = scale->priv;
1436 g_return_if_fail (GTK_IS_SCALE (scale));
1440 g_object_unref (priv->layout);
1441 priv->layout = NULL;
1446 gtk_scale_mark_free (GtkScaleMark *mark)
1448 g_free (mark->markup);
1453 * gtk_scale_clear_marks:
1454 * @scale: a #GtkScale
1456 * Removes any marks that have been added with gtk_scale_add_mark().
1461 gtk_scale_clear_marks (GtkScale *scale)
1463 GtkScalePrivate *priv;
1464 GtkStyleContext *context;
1466 g_return_if_fail (GTK_IS_SCALE (scale));
1470 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1471 g_slist_free (priv->marks);
1474 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1475 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1476 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1478 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1480 gtk_widget_queue_resize (GTK_WIDGET (scale));
1484 compare_marks (gpointer a, gpointer b)
1486 GtkScaleMark *ma, *mb;
1490 return (gint) (ma->value - mb->value);
1494 * gtk_scale_add_mark:
1495 * @scale: a #GtkScale
1496 * @value: the value at which the mark is placed, must be between
1497 * the lower and upper limits of the scales' adjustment
1498 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1499 * and %GTK_POS_LEFT are drawn above the scale, anything else below.
1500 * For a vertical scale, #GTK_POS_LEFT and %GTK_POS_TOP are drawn to
1501 * the left of the scale, anything else to the right.
1502 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1505 * Adds a mark at @value.
1507 * A mark is indicated visually by drawing a tick mark next to the scale,
1508 * and GTK+ makes it easy for the user to position the scale exactly at the
1511 * If @markup is not %NULL, text is shown next to the tick mark.
1513 * To remove marks from a scale, use gtk_scale_clear_marks().
1518 gtk_scale_add_mark (GtkScale *scale,
1520 GtkPositionType position,
1521 const gchar *markup)
1523 GtkScalePrivate *priv;
1528 GtkStyleContext *context;
1531 g_return_if_fail (GTK_IS_SCALE (scale));
1535 mark = g_new (GtkScaleMark, 1);
1536 mark->value = value;
1537 mark->markup = g_strdup (markup);
1538 if (position == GTK_POS_LEFT ||
1539 position == GTK_POS_TOP)
1540 mark->position = GTK_POS_TOP;
1542 mark->position = GTK_POS_BOTTOM;
1544 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1545 (GCompareFunc) compare_marks);
1547 #define MARKS_ABOVE 1
1548 #define MARKS_BELOW 2
1551 n = g_slist_length (priv->marks);
1552 values = g_new (gdouble, n);
1553 for (m = priv->marks, i = 0; m; m = m->next, i++)
1556 values[i] = mark->value;
1557 if (mark->position == GTK_POS_TOP)
1558 all_pos |= MARKS_ABOVE;
1560 all_pos |= MARKS_BELOW;
1563 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1567 /* Set the style classes for the slider, so it could
1568 * point to the right direction when marks are present
1570 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1572 if (all_pos & MARKS_ABOVE)
1573 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1575 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1576 if (all_pos & MARKS_BELOW)
1577 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1579 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1581 gtk_widget_queue_resize (GTK_WIDGET (scale));
1584 static GtkBuildableIface *parent_buildable_iface;
1587 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1589 parent_buildable_iface = g_type_interface_peek_parent (iface);
1590 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1591 iface->custom_finished = gtk_scale_buildable_custom_finished;
1597 GtkBuilder *builder;
1599 } MarksSubparserData;
1604 GtkPositionType position;
1607 gboolean translatable;
1611 mark_data_free (MarkData *data)
1613 g_string_free (data->markup, TRUE);
1614 g_free (data->context);
1615 g_slice_free (MarkData, data);
1619 marks_start_element (GMarkupParseContext *context,
1620 const gchar *element_name,
1621 const gchar **names,
1622 const gchar **values,
1626 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1628 gint line_number, char_number;
1630 if (strcmp (element_name, "marks") == 0)
1632 else if (strcmp (element_name, "mark") == 0)
1635 gboolean has_value = FALSE;
1636 GtkPositionType position = GTK_POS_BOTTOM;
1637 const gchar *msg_context = NULL;
1638 gboolean translatable = FALSE;
1641 for (i = 0; names[i]; i++)
1643 if (strcmp (names[i], "translatable") == 0)
1645 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1648 else if (strcmp (names[i], "comments") == 0)
1650 /* do nothing, comments are for translators */
1652 else if (strcmp (names[i], "context") == 0)
1653 msg_context = values[i];
1654 else if (strcmp (names[i], "value") == 0)
1656 GValue gvalue = { 0, };
1658 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1661 value = g_value_get_double (&gvalue);
1664 else if (strcmp (names[i], "position") == 0)
1666 GValue gvalue = { 0, };
1668 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1671 position = g_value_get_enum (&gvalue);
1675 g_markup_parse_context_get_position (context,
1680 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1681 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1683 line_number, char_number, names[i], "mark");
1690 g_markup_parse_context_get_position (context,
1695 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1696 "%s:%d:%d <%s> requires attribute \"%s\"",
1698 line_number, char_number, "mark",
1703 mark = g_slice_new (MarkData);
1704 mark->value = value;
1705 if (position == GTK_POS_LEFT ||
1706 position == GTK_POS_TOP)
1707 mark->position = GTK_POS_TOP;
1709 mark->position = GTK_POS_BOTTOM;
1710 mark->markup = g_string_new ("");
1711 mark->context = g_strdup (msg_context);
1712 mark->translatable = translatable;
1714 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1718 g_markup_parse_context_get_position (context,
1723 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1724 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1726 line_number, char_number, element_name);
1732 marks_text (GMarkupParseContext *context,
1738 MarksSubparserData *data = (MarksSubparserData*)user_data;
1740 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1742 MarkData *mark = data->marks->data;
1744 g_string_append_len (mark->markup, text, text_len);
1748 static const GMarkupParser marks_parser =
1750 marks_start_element,
1757 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1758 GtkBuilder *builder,
1760 const gchar *tagname,
1761 GMarkupParser *parser,
1764 MarksSubparserData *parser_data;
1769 if (strcmp (tagname, "marks") == 0)
1771 parser_data = g_slice_new0 (MarksSubparserData);
1772 parser_data->scale = GTK_SCALE (buildable);
1773 parser_data->marks = NULL;
1775 *parser = marks_parser;
1776 *data = parser_data;
1780 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1781 tagname, parser, data);
1785 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1786 GtkBuilder *builder,
1788 const gchar *tagname,
1791 GtkScale *scale = GTK_SCALE (buildable);
1792 MarksSubparserData *marks_data;
1794 if (strcmp (tagname, "marks") == 0)
1799 marks_data = (MarksSubparserData *)user_data;
1801 for (m = marks_data->marks; m; m = m->next)
1803 MarkData *mdata = m->data;
1805 if (mdata->translatable && mdata->markup->len)
1806 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1808 mdata->markup->str);
1810 markup = mdata->markup->str;
1812 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1814 mark_data_free (mdata);
1817 g_slist_free (marks_data->marks);
1818 g_slice_free (MarksSubparserData, marks_data);