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 "gdk/gdkkeysyms.h"
35 #include "gtkiconfactory.h"
36 #include "gtkicontheme.h"
37 #include "gtkmarshalers.h"
38 #include "gtkbindings.h"
39 #include "gtkorientable.h"
40 #include "gtkprivate.h"
42 #include "gtkbuildable.h"
43 #include "gtkbuilderprivate.h"
48 * @Short_description: Base class for GtkHScale and GtkVScale
51 * A GtkScale is a slider control used to select a numeric value.
52 * To use it, you'll probably want to investigate the methods on
53 * its base class, #GtkRange, in addition to the methods for GtkScale itself.
54 * To set the value of a scale, you would normally use gtk_range_set_value().
55 * To detect changes to the value, you would normally use the
56 * #GtkRange::value-changed signal.
58 * Note that using the same upper and lower bounds for the #GtkScale (through
59 * the #GtkRange methods) will hide the slider itself. This is useful for
60 * applications that want to show an undeterminate value on the scale, without
61 * changing the layout of the application (such as movie or music players).
63 * <refsect2 id="GtkScale-BUILDER-UI"><title>GtkScale as GtkBuildable</title>
64 * GtkScale supports a custom <marks> element, which
65 * can contain multiple <mark> elements. The "value" and "position"
66 * attributes have the same meaning as gtk_scale_add_mark() parameters of the
67 * same name. If the element is not empty, its content is taken as the markup
68 * to show at the mark. It can be translated with the usual "translatable and
69 * "context" attributes.
74 #define MAX_DIGITS (64) /* don't change this,
75 * a) you don't need to and
76 * b) you might cause buffer owerflows in
77 * unrelated code portions otherwise
81 typedef struct _GtkScaleMark GtkScaleMark;
83 struct _GtkScalePrivate
99 GtkPositionType position;
114 static guint signals[LAST_SIGNAL];
116 static void gtk_scale_set_property (GObject *object,
120 static void gtk_scale_get_property (GObject *object,
124 static void gtk_scale_get_preferred_width (GtkWidget *widget,
127 static void gtk_scale_get_preferred_height (GtkWidget *widget,
130 static void gtk_scale_style_updated (GtkWidget *widget);
131 static void gtk_scale_get_range_border (GtkRange *range,
133 static void gtk_scale_get_mark_label_size (GtkScale *scale,
134 GtkPositionType position,
141 static void gtk_scale_finalize (GObject *object);
142 static void gtk_scale_screen_changed (GtkWidget *widget,
143 GdkScreen *old_screen);
144 static gboolean gtk_scale_draw (GtkWidget *widget,
146 static void gtk_scale_real_get_layout_offsets (GtkScale *scale,
149 static void gtk_scale_buildable_interface_init (GtkBuildableIface *iface);
150 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
153 const gchar *tagname,
154 GMarkupParser *parser,
156 static void gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
159 const gchar *tagname,
163 G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
164 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
165 gtk_scale_buildable_interface_init))
169 single_string_accumulator (GSignalInvocationHint *ihint,
171 const GValue *handler_return,
174 gboolean continue_emission;
177 str = g_value_get_string (handler_return);
178 g_value_set_string (return_accu, str);
179 continue_emission = str == NULL;
181 return continue_emission;
185 #define add_slider_binding(binding_set, keyval, mask, scroll) \
186 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
187 I_("move-slider"), 1, \
188 GTK_TYPE_SCROLL_TYPE, scroll)
191 gtk_scale_class_init (GtkScaleClass *class)
193 GObjectClass *gobject_class;
194 GtkWidgetClass *widget_class;
195 GtkRangeClass *range_class;
196 GtkBindingSet *binding_set;
198 gobject_class = G_OBJECT_CLASS (class);
199 range_class = (GtkRangeClass*) class;
200 widget_class = (GtkWidgetClass*) class;
202 gobject_class->set_property = gtk_scale_set_property;
203 gobject_class->get_property = gtk_scale_get_property;
204 gobject_class->finalize = gtk_scale_finalize;
206 widget_class->style_updated = gtk_scale_style_updated;
207 widget_class->screen_changed = gtk_scale_screen_changed;
208 widget_class->draw = gtk_scale_draw;
209 widget_class->get_preferred_width = gtk_scale_get_preferred_width;
210 widget_class->get_preferred_height = gtk_scale_get_preferred_height;
212 range_class->slider_detail = "Xscale";
213 range_class->get_range_border = gtk_scale_get_range_border;
215 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
218 * GtkScale::format-value:
219 * @scale: the object which received the signal
220 * @value: the value to format
222 * Signal which allows you to change how the scale value is displayed.
223 * Connect a signal handler which returns an allocated string representing
224 * @value. That string will then be used to display the scale's value.
226 * Here's an example signal handler which displays a value 1.0 as
227 * with "-->1.0<--".
230 * format_value_callback (GtkScale *scale,
233 * return g_strdup_printf ("-->%0.*g<--",
234 * gtk_scale_get_digits (scale), value);
238 * Return value: allocated string representing @value
240 signals[FORMAT_VALUE] =
241 g_signal_new (I_("format-value"),
242 G_TYPE_FROM_CLASS (gobject_class),
244 G_STRUCT_OFFSET (GtkScaleClass, format_value),
245 single_string_accumulator, NULL,
246 _gtk_marshal_STRING__DOUBLE,
250 g_object_class_install_property (gobject_class,
252 g_param_spec_int ("digits",
254 P_("The number of decimal places that are displayed in the value"),
258 GTK_PARAM_READWRITE));
260 g_object_class_install_property (gobject_class,
262 g_param_spec_boolean ("draw-value",
264 P_("Whether the current value is displayed as a string next to the slider"),
266 GTK_PARAM_READWRITE));
268 g_object_class_install_property (gobject_class,
270 g_param_spec_enum ("value-pos",
271 P_("Value Position"),
272 P_("The position in which the current value is displayed"),
273 GTK_TYPE_POSITION_TYPE,
275 GTK_PARAM_READWRITE));
277 gtk_widget_class_install_style_property (widget_class,
278 g_param_spec_int ("slider-length",
280 P_("Length of scale's slider"),
284 GTK_PARAM_READABLE));
286 gtk_widget_class_install_style_property (widget_class,
287 g_param_spec_int ("value-spacing",
289 P_("Space between value text and the slider/trough area"),
293 GTK_PARAM_READABLE));
295 /* All bindings (even arrow keys) are on both h/v scale, because
296 * blind users etc. don't care about scale orientation.
299 binding_set = gtk_binding_set_by_class (class);
301 add_slider_binding (binding_set, GDK_KEY_Left, 0,
302 GTK_SCROLL_STEP_LEFT);
304 add_slider_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
305 GTK_SCROLL_PAGE_LEFT);
307 add_slider_binding (binding_set, GDK_KEY_KP_Left, 0,
308 GTK_SCROLL_STEP_LEFT);
310 add_slider_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
311 GTK_SCROLL_PAGE_LEFT);
313 add_slider_binding (binding_set, GDK_KEY_Right, 0,
314 GTK_SCROLL_STEP_RIGHT);
316 add_slider_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
317 GTK_SCROLL_PAGE_RIGHT);
319 add_slider_binding (binding_set, GDK_KEY_KP_Right, 0,
320 GTK_SCROLL_STEP_RIGHT);
322 add_slider_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
323 GTK_SCROLL_PAGE_RIGHT);
325 add_slider_binding (binding_set, GDK_KEY_Up, 0,
328 add_slider_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
331 add_slider_binding (binding_set, GDK_KEY_KP_Up, 0,
334 add_slider_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
337 add_slider_binding (binding_set, GDK_KEY_Down, 0,
338 GTK_SCROLL_STEP_DOWN);
340 add_slider_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
341 GTK_SCROLL_PAGE_DOWN);
343 add_slider_binding (binding_set, GDK_KEY_KP_Down, 0,
344 GTK_SCROLL_STEP_DOWN);
346 add_slider_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
347 GTK_SCROLL_PAGE_DOWN);
349 add_slider_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
350 GTK_SCROLL_PAGE_LEFT);
352 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
353 GTK_SCROLL_PAGE_LEFT);
355 add_slider_binding (binding_set, GDK_KEY_Page_Up, 0,
358 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
361 add_slider_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
362 GTK_SCROLL_PAGE_RIGHT);
364 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
365 GTK_SCROLL_PAGE_RIGHT);
367 add_slider_binding (binding_set, GDK_KEY_Page_Down, 0,
368 GTK_SCROLL_PAGE_DOWN);
370 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
371 GTK_SCROLL_PAGE_DOWN);
373 /* Logical bindings (vs. visual bindings above) */
375 add_slider_binding (binding_set, GDK_KEY_plus, 0,
376 GTK_SCROLL_STEP_FORWARD);
378 add_slider_binding (binding_set, GDK_KEY_minus, 0,
379 GTK_SCROLL_STEP_BACKWARD);
381 add_slider_binding (binding_set, GDK_KEY_plus, GDK_CONTROL_MASK,
382 GTK_SCROLL_PAGE_FORWARD);
384 add_slider_binding (binding_set, GDK_KEY_minus, GDK_CONTROL_MASK,
385 GTK_SCROLL_PAGE_BACKWARD);
388 add_slider_binding (binding_set, GDK_KEY_KP_Add, 0,
389 GTK_SCROLL_STEP_FORWARD);
391 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, 0,
392 GTK_SCROLL_STEP_BACKWARD);
394 add_slider_binding (binding_set, GDK_KEY_KP_Add, GDK_CONTROL_MASK,
395 GTK_SCROLL_PAGE_FORWARD);
397 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
398 GTK_SCROLL_PAGE_BACKWARD);
401 add_slider_binding (binding_set, GDK_KEY_Home, 0,
404 add_slider_binding (binding_set, GDK_KEY_KP_Home, 0,
407 add_slider_binding (binding_set, GDK_KEY_End, 0,
410 add_slider_binding (binding_set, GDK_KEY_KP_End, 0,
413 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
417 gtk_scale_orientation_notify (GtkRange *range,
418 const GParamSpec *pspec)
420 GtkOrientation orientation;
422 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
423 gtk_range_set_flippable (range,
424 orientation == GTK_ORIENTATION_HORIZONTAL);
428 gtk_scale_init (GtkScale *scale)
430 GtkScalePrivate *priv;
431 GtkRange *range = GTK_RANGE (scale);
432 GtkStyleContext *context;
434 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
439 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
441 gtk_range_set_slider_size_fixed (range, TRUE);
443 priv->draw_value = TRUE;
444 priv->value_pos = GTK_POS_TOP;
446 _gtk_range_set_round_digits (range, priv->digits);
448 gtk_scale_orientation_notify (range, NULL);
449 g_signal_connect (scale, "notify::orientation",
450 G_CALLBACK (gtk_scale_orientation_notify),
453 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
454 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE);
458 gtk_scale_set_property (GObject *object,
465 scale = GTK_SCALE (object);
470 gtk_scale_set_digits (scale, g_value_get_int (value));
472 case PROP_DRAW_VALUE:
473 gtk_scale_set_draw_value (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);
502 g_value_set_enum (value, priv->value_pos);
505 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
512 * @orientation: the scale's orientation.
513 * @adjustment: the #GtkAdjustment which sets the range of the scale, or
514 * %NULL to create a new adjustment.
516 * Creates a new #GtkScale.
518 * Return value: a new #GtkScale
523 gtk_scale_new (GtkOrientation orientation,
524 GtkAdjustment *adjustment)
526 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
529 return g_object_new (GTK_TYPE_SCALE,
530 "orientation", orientation,
531 "adjustment", adjustment,
536 * gtk_scale_new_with_range:
537 * @orientation: the scale's orientation.
538 * @min: minimum value
539 * @max: maximum value
540 * @step: step increment (tick size) used with keyboard shortcuts
542 * Creates a new scale widget with the given orientation that lets the
543 * user input a number between @min and @max (including @min and @max)
544 * with the increment @step. @step must be nonzero; it's the distance
545 * the slider moves when using the arrow keys to adjust the scale
548 * Note that the way in which the precision is derived works best if @step
549 * is a power of ten. If the resulting precision is not suitable for your
550 * needs, use gtk_scale_set_digits() to correct it.
552 * Return value: a new #GtkScale
557 gtk_scale_new_with_range (GtkOrientation orientation,
565 g_return_val_if_fail (min < max, NULL);
566 g_return_val_if_fail (step != 0.0, NULL);
568 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
570 if (fabs (step) >= 1.0 || step == 0.0)
576 digits = abs ((gint) floor (log10 (fabs (step))));
581 return g_object_new (GTK_TYPE_SCALE,
582 "orientation", orientation,
589 * gtk_scale_set_digits:
590 * @scale: a #GtkScale
591 * @digits: the number of decimal places to display,
592 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
594 * Sets the number of decimal places that are displayed in the value.
595 * Also causes the value of the adjustment to be rounded off to this
596 * number of digits, so the retrieved value matches the value the user saw.
599 gtk_scale_set_digits (GtkScale *scale,
602 GtkScalePrivate *priv;
605 g_return_if_fail (GTK_IS_SCALE (scale));
608 range = GTK_RANGE (scale);
610 digits = CLAMP (digits, -1, MAX_DIGITS);
612 if (priv->digits != digits)
614 priv->digits = digits;
615 if (priv->draw_value)
616 _gtk_range_set_round_digits (range, digits);
618 _gtk_scale_clear_layout (scale);
619 gtk_widget_queue_resize (GTK_WIDGET (scale));
621 g_object_notify (G_OBJECT (scale), "digits");
626 * gtk_scale_get_digits:
627 * @scale: a #GtkScale
629 * Gets the number of decimal places that are displayed in the value.
631 * Returns: the number of decimal places that are displayed
634 gtk_scale_get_digits (GtkScale *scale)
636 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
638 return scale->priv->digits;
642 * gtk_scale_set_draw_value:
643 * @scale: a #GtkScale
644 * @draw_value: %TRUE to draw the value
646 * Specifies whether the current value is displayed as a string next
650 gtk_scale_set_draw_value (GtkScale *scale,
653 GtkScalePrivate *priv;
655 g_return_if_fail (GTK_IS_SCALE (scale));
659 draw_value = draw_value != FALSE;
661 if (priv->draw_value != draw_value)
663 priv->draw_value = draw_value;
665 _gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
667 _gtk_range_set_round_digits (GTK_RANGE (scale), -1);
669 _gtk_scale_clear_layout (scale);
671 gtk_widget_queue_resize (GTK_WIDGET (scale));
673 g_object_notify (G_OBJECT (scale), "draw-value");
678 * gtk_scale_get_draw_value:
679 * @scale: a #GtkScale
681 * Returns whether the current value is displayed as a string
682 * next to the slider.
684 * Returns: whether the current value is displayed as a string
687 gtk_scale_get_draw_value (GtkScale *scale)
689 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
691 return scale->priv->draw_value;
695 * gtk_scale_set_value_pos:
696 * @scale: a #GtkScale
697 * @pos: the position in which the current value is displayed
699 * Sets the position in which the current value is displayed.
702 gtk_scale_set_value_pos (GtkScale *scale,
705 GtkScalePrivate *priv;
708 g_return_if_fail (GTK_IS_SCALE (scale));
712 if (priv->value_pos != pos)
714 priv->value_pos = pos;
715 widget = GTK_WIDGET (scale);
717 _gtk_scale_clear_layout (scale);
718 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
719 gtk_widget_queue_resize (widget);
721 g_object_notify (G_OBJECT (scale), "value-pos");
726 * gtk_scale_get_value_pos:
727 * @scale: a #GtkScale
729 * Gets the position in which the current value is displayed.
731 * Returns: the position in which the current value is displayed
734 gtk_scale_get_value_pos (GtkScale *scale)
736 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
738 return scale->priv->value_pos;
742 gtk_scale_get_range_border (GtkRange *range,
745 GtkScalePrivate *priv;
750 widget = GTK_WIDGET (range);
751 scale = GTK_SCALE (range);
754 _gtk_scale_get_value_size (scale, &w, &h);
761 if (priv->draw_value)
764 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
766 switch (priv->value_pos)
769 border->left += w + value_spacing;
772 border->right += w + value_spacing;
775 border->top += h + value_spacing;
778 border->bottom += h + value_spacing;
787 gint n1, w1, h1, n2, w2, h2;
789 gtk_widget_style_get (widget,
790 "slider-width", &slider_width,
791 "value-spacing", &value_spacing,
795 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
797 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
799 border->top += h1 + value_spacing + slider_width / 2;
801 border->bottom += h2 + value_spacing + slider_width / 2;
805 gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
807 border->left += w1 + value_spacing + slider_width / 2;
809 border->right += w2 + value_spacing + slider_width / 2;
814 /* FIXME this could actually be static at the moment. */
816 _gtk_scale_get_value_size (GtkScale *scale,
820 GtkScalePrivate *priv = scale->priv;
823 if (priv->draw_value)
825 GtkAdjustment *adjustment;
827 PangoRectangle logical_rect;
830 range = GTK_RANGE (scale);
832 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
833 adjustment = gtk_range_get_adjustment (range);
835 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
836 pango_layout_set_text (layout, txt, -1);
839 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
842 *width = logical_rect.width;
844 *height = logical_rect.height;
846 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
847 pango_layout_set_text (layout, txt, -1);
850 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
853 *width = MAX (*width, logical_rect.width);
855 *height = MAX (*height, logical_rect.height);
857 g_object_unref (layout);
870 gtk_scale_get_mark_label_size (GtkScale *scale,
871 GtkPositionType position,
879 GtkScalePrivate *priv = scale->priv;
881 PangoRectangle logical_rect;
885 *count1 = *count2 = 0;
886 *width1 = *width2 = 0;
887 *height1 = *height2 = 0;
889 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
891 for (m = priv->marks; m; m = m->next)
893 GtkScaleMark *mark = m->data;
895 if (mark->markup && *mark->markup)
897 pango_layout_set_markup (layout, mark->markup, -1);
898 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
900 w = logical_rect.width;
901 h = logical_rect.height;
909 if (mark->position == position)
912 *width1 = MAX (*width1, w);
913 *height1 = MAX (*height1, h);
918 *width2 = MAX (*width2, w);
919 *height2 = MAX (*height2, h);
923 g_object_unref (layout);
927 gtk_scale_style_updated (GtkWidget *widget)
932 range = GTK_RANGE (widget);
934 gtk_widget_style_get (widget,
935 "slider-length", &slider_length,
938 gtk_range_set_min_slider_size (range, slider_length);
940 _gtk_scale_clear_layout (GTK_SCALE (widget));
942 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_updated (widget);
946 gtk_scale_screen_changed (GtkWidget *widget,
947 GdkScreen *old_screen)
949 _gtk_scale_clear_layout (GTK_SCALE (widget));
953 gtk_scale_get_preferred_width (GtkWidget *widget,
957 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural);
959 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
961 gint n1, w1, h1, n2, w2, h2;
965 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
967 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
969 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
970 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
973 *minimum = MAX (*minimum, w);
974 *natural = MAX (*natural, w);
979 gtk_scale_get_preferred_height (GtkWidget *widget,
983 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural);
986 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
988 gint n1, w1, h1, n2, w2, h2;
992 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
994 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
995 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
996 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
999 *minimum = MAX (*minimum, h);
1000 *natural = MAX (*natural, h);
1005 find_next_pos (GtkWidget *widget,
1008 GtkPositionType pos,
1011 GtkAllocation allocation;
1015 for (m = list->next, i = 1; m; m = m->next, i++)
1017 GtkScaleMark *mark = m->data;
1019 if (match == (mark->position == pos))
1023 gtk_widget_get_allocation (widget, &allocation);
1024 if (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM)
1025 return allocation.width;
1027 return allocation.height;
1031 gtk_scale_draw (GtkWidget *widget,
1034 GtkScale *scale = GTK_SCALE (widget);
1035 GtkScalePrivate *priv = scale->priv;
1036 GtkRange *range = GTK_RANGE (scale);
1037 GtkStateFlags state = 0;
1038 GtkStyleContext *context;
1046 context = gtk_widget_get_style_context (widget);
1047 gtk_widget_style_get (widget,
1048 "focus-padding", &focus_padding,
1049 "slider-width", &slider_width,
1050 "value-spacing", &value_spacing,
1053 /* We need to chain up _first_ so the various geometry members of
1054 * GtkRange struct are updated.
1056 GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1058 if (!gtk_widget_is_sensitive (widget))
1059 state |= GTK_STATE_FLAG_INSENSITIVE;
1063 GtkOrientation orientation;
1064 GdkRectangle range_rect;
1066 gint x1, x2, x3, y1, y2, y3;
1067 PangoLayout *layout;
1068 PangoRectangle logical_rect;
1070 gint min_pos_before, min_pos_after;
1071 gint min_pos, max_pos;
1073 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1074 n_marks = _gtk_range_get_stop_positions (range, &marks);
1075 layout = gtk_widget_create_pango_layout (widget, NULL);
1076 gtk_range_get_range_rect (range, &range_rect);
1078 min_pos_before = min_pos_after = 0;
1080 for (m = priv->marks, i = 0; m; m = m->next, i++)
1082 GtkScaleMark *mark = m->data;
1084 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1087 if (mark->position == GTK_POS_TOP)
1090 y2 = y1 - slider_width / 2;
1091 min_pos = min_pos_before;
1092 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1096 y1 = range_rect.y + range_rect.height;
1097 y2 = y1 + slider_width / 2;
1098 min_pos = min_pos_after;
1099 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep;
1102 gtk_style_context_save (context);
1103 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1104 gtk_style_context_set_state (context, state);
1106 gtk_render_line (context, cr,
1111 pango_layout_set_markup (layout, mark->markup, -1);
1112 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1114 x3 = x1 - logical_rect.width / 2;
1117 if (x3 + logical_rect.width > max_pos)
1118 x3 = max_pos - logical_rect.width;
1121 if (mark->position == GTK_POS_TOP)
1123 y3 = y2 - value_spacing - logical_rect.height;
1124 min_pos_before = x3 + logical_rect.width + min_sep;
1128 y3 = y2 + value_spacing;
1129 min_pos_after = x3 + logical_rect.width + min_sep;
1132 gtk_render_layout (context, cr,
1136 gtk_style_context_restore (context);
1140 if (mark->position == GTK_POS_LEFT)
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_LEFT, 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_LEFT, 0) - 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,
1165 pango_layout_set_markup (layout, mark->markup, -1);
1166 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1168 y3 = y1 - logical_rect.height / 2;
1171 if (y3 + logical_rect.height > max_pos)
1172 y3 = max_pos - logical_rect.height;
1175 if (mark->position == GTK_POS_LEFT)
1177 x3 = x2 - value_spacing - logical_rect.width;
1178 min_pos_before = y3 + logical_rect.height + min_sep;
1182 x3 = x2 + value_spacing;
1183 min_pos_after = y3 + logical_rect.height + min_sep;
1186 gtk_render_layout (context, cr,
1190 gtk_style_context_restore (context);
1194 g_object_unref (layout);
1198 if (priv->draw_value)
1200 GtkOrientation orientation;
1201 GtkAllocation allocation;
1203 PangoLayout *layout;
1206 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1207 layout = gtk_scale_get_layout (scale);
1208 gtk_scale_get_layout_offsets (scale, &x, &y);
1209 gtk_widget_get_allocation (widget, &allocation);
1211 gtk_render_layout (context, cr,
1221 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1225 GtkScalePrivate *priv = scale->priv;
1226 GtkAllocation allocation;
1227 GtkWidget *widget = GTK_WIDGET (scale);
1228 GtkRange *range = GTK_RANGE (widget);
1229 GdkRectangle range_rect;
1230 PangoLayout *layout = gtk_scale_get_layout (scale);
1231 PangoRectangle logical_rect;
1232 gint slider_start, slider_end;
1243 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1245 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1247 gtk_widget_get_allocation (widget, &allocation);
1248 gtk_range_get_range_rect (range, &range_rect);
1249 gtk_range_get_slider_range (range,
1253 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1255 switch (priv->value_pos)
1258 *x = range_rect.x - value_spacing - logical_rect.width;
1259 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1263 *x = range_rect.x + range_rect.width + value_spacing;
1264 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1268 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1269 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1270 *y = range_rect.y - logical_rect.height - value_spacing;
1273 case GTK_POS_BOTTOM:
1274 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1275 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1276 *y = range_rect.y + range_rect.height + value_spacing;
1280 g_return_if_reached ();
1286 switch (priv->value_pos)
1289 *x = range_rect.x - logical_rect.width - value_spacing;
1290 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1291 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1295 *x = range_rect.x + range_rect.width + value_spacing;
1296 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1297 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1301 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1302 *y = range_rect.y - logical_rect.height - value_spacing;
1305 case GTK_POS_BOTTOM:
1306 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1307 *y = range_rect.y + range_rect.height + value_spacing;
1311 g_return_if_reached ();
1320 * _gtk_scale_format_value:
1321 * @scale: a #GtkScale
1322 * @value: adjustment value
1324 * Emits #GtkScale::format-value signal to format the value,
1325 * if no user signal handlers, falls back to a default format.
1327 * Return value: formatted value
1330 _gtk_scale_format_value (GtkScale *scale,
1333 GtkScalePrivate *priv = scale->priv;
1336 g_signal_emit (scale,
1337 signals[FORMAT_VALUE],
1345 /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1346 return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value);
1350 gtk_scale_finalize (GObject *object)
1352 GtkScale *scale = GTK_SCALE (object);
1354 _gtk_scale_clear_layout (scale);
1355 gtk_scale_clear_marks (scale);
1357 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1361 * gtk_scale_get_layout:
1362 * @scale: A #GtkScale
1364 * Gets the #PangoLayout used to display the scale. The returned
1365 * object is owned by the scale so does not need to be freed by
1368 * Return value: (transfer none): the #PangoLayout for this scale,
1369 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1374 gtk_scale_get_layout (GtkScale *scale)
1376 GtkScalePrivate *priv;
1379 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1385 if (priv->draw_value)
1386 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1389 if (priv->draw_value)
1391 txt = _gtk_scale_format_value (scale,
1392 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1393 pango_layout_set_text (priv->layout, txt, -1);
1397 return priv->layout;
1401 * gtk_scale_get_layout_offsets:
1402 * @scale: a #GtkScale
1403 * @x: (allow-none): location to store X offset of layout, or %NULL
1404 * @y: (allow-none): location to store Y offset of layout, or %NULL
1406 * Obtains the coordinates where the scale will draw the
1407 * #PangoLayout representing the text in the scale. Remember
1408 * when using the #PangoLayout function you need to convert to
1409 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1411 * If the #GtkScale:draw-value property is %FALSE, the return
1412 * values are undefined.
1417 gtk_scale_get_layout_offsets (GtkScale *scale,
1424 g_return_if_fail (GTK_IS_SCALE (scale));
1426 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1427 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1437 _gtk_scale_clear_layout (GtkScale *scale)
1439 GtkScalePrivate *priv = scale->priv;
1441 g_return_if_fail (GTK_IS_SCALE (scale));
1445 g_object_unref (priv->layout);
1446 priv->layout = NULL;
1451 gtk_scale_mark_free (GtkScaleMark *mark)
1453 g_free (mark->markup);
1458 * gtk_scale_clear_marks:
1459 * @scale: a #GtkScale
1461 * Removes any marks that have been added with gtk_scale_add_mark().
1466 gtk_scale_clear_marks (GtkScale *scale)
1468 GtkScalePrivate *priv;
1470 g_return_if_fail (GTK_IS_SCALE (scale));
1474 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1475 g_slist_free (priv->marks);
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 * is drawn above the scale, anything else below. For a vertical scale,
1500 * #GTK_POS_LEFT is drawn to the left of the scale, anything else to the
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;
1529 g_return_if_fail (GTK_IS_SCALE (scale));
1533 mark = g_new (GtkScaleMark, 1);
1534 mark->value = value;
1535 mark->markup = g_strdup (markup);
1536 mark->position = position;
1538 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1539 (GCompareFunc) compare_marks);
1541 n = g_slist_length (priv->marks);
1542 values = g_new (gdouble, n);
1543 for (m = priv->marks, i = 0; m; m = m->next, i++)
1546 values[i] = mark->value;
1549 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1553 gtk_widget_queue_resize (GTK_WIDGET (scale));
1556 static GtkBuildableIface *parent_buildable_iface;
1559 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1561 parent_buildable_iface = g_type_interface_peek_parent (iface);
1562 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1563 iface->custom_finished = gtk_scale_buildable_custom_finished;
1569 GtkBuilder *builder;
1571 } MarksSubparserData;
1576 GtkPositionType position;
1579 gboolean translatable;
1583 mark_data_free (MarkData *data)
1585 g_string_free (data->markup, TRUE);
1586 g_free (data->context);
1587 g_slice_free (MarkData, data);
1591 marks_start_element (GMarkupParseContext *context,
1592 const gchar *element_name,
1593 const gchar **names,
1594 const gchar **values,
1598 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1600 gint line_number, char_number;
1602 if (strcmp (element_name, "marks") == 0)
1604 else if (strcmp (element_name, "mark") == 0)
1607 gboolean has_value = FALSE;
1608 GtkPositionType position = GTK_POS_BOTTOM;
1609 const gchar *msg_context = NULL;
1610 gboolean translatable = FALSE;
1613 for (i = 0; names[i]; i++)
1615 if (strcmp (names[i], "translatable") == 0)
1617 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1620 else if (strcmp (names[i], "comments") == 0)
1622 /* do nothing, comments are for translators */
1624 else if (strcmp (names[i], "context") == 0)
1625 msg_context = values[i];
1626 else if (strcmp (names[i], "value") == 0)
1628 GValue gvalue = { 0, };
1630 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1633 value = g_value_get_double (&gvalue);
1636 else if (strcmp (names[i], "position") == 0)
1638 GValue gvalue = { 0, };
1640 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1643 position = g_value_get_enum (&gvalue);
1647 g_markup_parse_context_get_position (context,
1652 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1653 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1655 line_number, char_number, names[i], "mark");
1662 g_markup_parse_context_get_position (context,
1667 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1668 "%s:%d:%d <%s> requires attribute \"%s\"",
1670 line_number, char_number, "mark",
1675 mark = g_slice_new (MarkData);
1676 mark->value = value;
1677 mark->position = position;
1678 mark->markup = g_string_new ("");
1679 mark->context = g_strdup (msg_context);
1680 mark->translatable = translatable;
1682 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1686 g_markup_parse_context_get_position (context,
1691 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1692 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1694 line_number, char_number, element_name);
1700 marks_text (GMarkupParseContext *context,
1706 MarksSubparserData *data = (MarksSubparserData*)user_data;
1708 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1710 MarkData *mark = data->marks->data;
1712 g_string_append_len (mark->markup, text, text_len);
1716 static const GMarkupParser marks_parser =
1718 marks_start_element,
1725 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1726 GtkBuilder *builder,
1728 const gchar *tagname,
1729 GMarkupParser *parser,
1732 MarksSubparserData *parser_data;
1737 if (strcmp (tagname, "marks") == 0)
1739 parser_data = g_slice_new0 (MarksSubparserData);
1740 parser_data->scale = GTK_SCALE (buildable);
1741 parser_data->marks = NULL;
1743 *parser = marks_parser;
1744 *data = parser_data;
1748 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1749 tagname, parser, data);
1753 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1754 GtkBuilder *builder,
1756 const gchar *tagname,
1759 GtkScale *scale = GTK_SCALE (buildable);
1760 MarksSubparserData *marks_data;
1762 if (strcmp (tagname, "marks") == 0)
1767 marks_data = (MarksSubparserData *)user_data;
1769 for (m = marks_data->marks; m; m = m->next)
1771 MarkData *mdata = m->data;
1773 if (mdata->translatable && mdata->markup->len)
1774 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1776 mdata->markup->str);
1778 markup = mdata->markup->str;
1780 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1782 mark_data_free (mdata);
1785 g_slist_free (marks_data->marks);
1786 g_slice_free (MarksSubparserData, marks_data);