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 * <refsect2 id="GtkScale-BUILDER-UI"><title>GtkScale as GtkBuildable</title>
59 * GtkScale supports a custom <marks> element, which
60 * can contain multiple <mark> elements. The "value" and "position"
61 * attributes have the same meaning as gtk_scale_add_mark() parameters of the
62 * same name. If the element is not empty, its content is taken as the markup
63 * to show at the mark. It can be translated with the usual "translatable and
64 * "context" attributes.
69 #define MAX_DIGITS (64) /* don't change this,
70 * a) you don't need to and
71 * b) you might cause buffer owerflows in
72 * unrelated code portions otherwise
76 typedef struct _GtkScaleMark GtkScaleMark;
78 struct _GtkScalePrivate
94 GtkPositionType position;
109 static guint signals[LAST_SIGNAL];
111 static void gtk_scale_set_property (GObject *object,
115 static void gtk_scale_get_property (GObject *object,
119 static void gtk_scale_get_preferred_width (GtkWidget *widget,
122 static void gtk_scale_get_preferred_height (GtkWidget *widget,
125 static void gtk_scale_style_set (GtkWidget *widget,
127 static void gtk_scale_get_range_border (GtkRange *range,
129 static void gtk_scale_get_mark_label_size (GtkScale *scale,
130 GtkPositionType position,
137 static void gtk_scale_finalize (GObject *object);
138 static void gtk_scale_screen_changed (GtkWidget *widget,
139 GdkScreen *old_screen);
140 static gboolean gtk_scale_draw (GtkWidget *widget,
142 static void gtk_scale_real_get_layout_offsets (GtkScale *scale,
145 static void gtk_scale_buildable_interface_init (GtkBuildableIface *iface);
146 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
149 const gchar *tagname,
150 GMarkupParser *parser,
152 static void gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
155 const gchar *tagname,
159 G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
160 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
161 gtk_scale_buildable_interface_init))
165 single_string_accumulator (GSignalInvocationHint *ihint,
167 const GValue *handler_return,
170 gboolean continue_emission;
173 str = g_value_get_string (handler_return);
174 g_value_set_string (return_accu, str);
175 continue_emission = str == NULL;
177 return continue_emission;
181 #define add_slider_binding(binding_set, keyval, mask, scroll) \
182 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
183 I_("move-slider"), 1, \
184 GTK_TYPE_SCROLL_TYPE, scroll)
187 gtk_scale_class_init (GtkScaleClass *class)
189 GObjectClass *gobject_class;
190 GtkWidgetClass *widget_class;
191 GtkRangeClass *range_class;
192 GtkBindingSet *binding_set;
194 gobject_class = G_OBJECT_CLASS (class);
195 range_class = (GtkRangeClass*) class;
196 widget_class = (GtkWidgetClass*) class;
198 gobject_class->set_property = gtk_scale_set_property;
199 gobject_class->get_property = gtk_scale_get_property;
200 gobject_class->finalize = gtk_scale_finalize;
202 widget_class->style_set = gtk_scale_style_set;
203 widget_class->screen_changed = gtk_scale_screen_changed;
204 widget_class->draw = gtk_scale_draw;
205 widget_class->get_preferred_width = gtk_scale_get_preferred_width;
206 widget_class->get_preferred_height = gtk_scale_get_preferred_height;
208 range_class->slider_detail = "Xscale";
209 range_class->get_range_border = gtk_scale_get_range_border;
211 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
214 * GtkScale::format-value:
215 * @scale: the object which received the signal
216 * @value: the value to format
218 * Signal which allows you to change how the scale value is displayed.
219 * Connect a signal handler which returns an allocated string representing
220 * @value. That string will then be used to display the scale's value.
222 * Here's an example signal handler which displays a value 1.0 as
223 * with "-->1.0<--".
226 * format_value_callback (GtkScale *scale,
229 * return g_strdup_printf ("-->%0.*g<--",
230 * gtk_scale_get_digits (scale), value);
234 * Return value: allocated string representing @value
236 signals[FORMAT_VALUE] =
237 g_signal_new (I_("format-value"),
238 G_TYPE_FROM_CLASS (gobject_class),
240 G_STRUCT_OFFSET (GtkScaleClass, format_value),
241 single_string_accumulator, NULL,
242 _gtk_marshal_STRING__DOUBLE,
246 g_object_class_install_property (gobject_class,
248 g_param_spec_int ("digits",
250 P_("The number of decimal places that are displayed in the value"),
254 GTK_PARAM_READWRITE));
256 g_object_class_install_property (gobject_class,
258 g_param_spec_boolean ("draw-value",
260 P_("Whether the current value is displayed as a string next to the slider"),
262 GTK_PARAM_READWRITE));
264 g_object_class_install_property (gobject_class,
266 g_param_spec_enum ("value-pos",
267 P_("Value Position"),
268 P_("The position in which the current value is displayed"),
269 GTK_TYPE_POSITION_TYPE,
271 GTK_PARAM_READWRITE));
273 gtk_widget_class_install_style_property (widget_class,
274 g_param_spec_int ("slider-length",
276 P_("Length of scale's slider"),
280 GTK_PARAM_READABLE));
282 gtk_widget_class_install_style_property (widget_class,
283 g_param_spec_int ("value-spacing",
285 P_("Space between value text and the slider/trough area"),
289 GTK_PARAM_READABLE));
291 /* All bindings (even arrow keys) are on both h/v scale, because
292 * blind users etc. don't care about scale orientation.
295 binding_set = gtk_binding_set_by_class (class);
297 add_slider_binding (binding_set, GDK_KEY_Left, 0,
298 GTK_SCROLL_STEP_LEFT);
300 add_slider_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
301 GTK_SCROLL_PAGE_LEFT);
303 add_slider_binding (binding_set, GDK_KEY_KP_Left, 0,
304 GTK_SCROLL_STEP_LEFT);
306 add_slider_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
307 GTK_SCROLL_PAGE_LEFT);
309 add_slider_binding (binding_set, GDK_KEY_Right, 0,
310 GTK_SCROLL_STEP_RIGHT);
312 add_slider_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
313 GTK_SCROLL_PAGE_RIGHT);
315 add_slider_binding (binding_set, GDK_KEY_KP_Right, 0,
316 GTK_SCROLL_STEP_RIGHT);
318 add_slider_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
319 GTK_SCROLL_PAGE_RIGHT);
321 add_slider_binding (binding_set, GDK_KEY_Up, 0,
324 add_slider_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
327 add_slider_binding (binding_set, GDK_KEY_KP_Up, 0,
330 add_slider_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
333 add_slider_binding (binding_set, GDK_KEY_Down, 0,
334 GTK_SCROLL_STEP_DOWN);
336 add_slider_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
337 GTK_SCROLL_PAGE_DOWN);
339 add_slider_binding (binding_set, GDK_KEY_KP_Down, 0,
340 GTK_SCROLL_STEP_DOWN);
342 add_slider_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
343 GTK_SCROLL_PAGE_DOWN);
345 add_slider_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
346 GTK_SCROLL_PAGE_LEFT);
348 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
349 GTK_SCROLL_PAGE_LEFT);
351 add_slider_binding (binding_set, GDK_KEY_Page_Up, 0,
354 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
357 add_slider_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
358 GTK_SCROLL_PAGE_RIGHT);
360 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
361 GTK_SCROLL_PAGE_RIGHT);
363 add_slider_binding (binding_set, GDK_KEY_Page_Down, 0,
364 GTK_SCROLL_PAGE_DOWN);
366 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
367 GTK_SCROLL_PAGE_DOWN);
369 /* Logical bindings (vs. visual bindings above) */
371 add_slider_binding (binding_set, GDK_KEY_plus, 0,
372 GTK_SCROLL_STEP_FORWARD);
374 add_slider_binding (binding_set, GDK_KEY_minus, 0,
375 GTK_SCROLL_STEP_BACKWARD);
377 add_slider_binding (binding_set, GDK_KEY_plus, GDK_CONTROL_MASK,
378 GTK_SCROLL_PAGE_FORWARD);
380 add_slider_binding (binding_set, GDK_KEY_minus, GDK_CONTROL_MASK,
381 GTK_SCROLL_PAGE_BACKWARD);
384 add_slider_binding (binding_set, GDK_KEY_KP_Add, 0,
385 GTK_SCROLL_STEP_FORWARD);
387 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, 0,
388 GTK_SCROLL_STEP_BACKWARD);
390 add_slider_binding (binding_set, GDK_KEY_KP_Add, GDK_CONTROL_MASK,
391 GTK_SCROLL_PAGE_FORWARD);
393 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
394 GTK_SCROLL_PAGE_BACKWARD);
397 add_slider_binding (binding_set, GDK_KEY_Home, 0,
400 add_slider_binding (binding_set, GDK_KEY_KP_Home, 0,
403 add_slider_binding (binding_set, GDK_KEY_End, 0,
406 add_slider_binding (binding_set, GDK_KEY_KP_End, 0,
409 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
413 gtk_scale_orientation_notify (GtkRange *range,
414 const GParamSpec *pspec)
416 GtkOrientation orientation;
418 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
419 gtk_range_set_flippable (range,
420 orientation == GTK_ORIENTATION_HORIZONTAL);
424 gtk_scale_init (GtkScale *scale)
426 GtkScalePrivate *priv;
427 GtkRange *range = GTK_RANGE (scale);
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 priv->draw_value = TRUE;
439 priv->value_pos = GTK_POS_TOP;
441 _gtk_range_set_round_digits (range, priv->digits);
443 gtk_scale_orientation_notify (range, NULL);
444 g_signal_connect (scale, "notify::orientation",
445 G_CALLBACK (gtk_scale_orientation_notify),
450 gtk_scale_set_property (GObject *object,
457 scale = GTK_SCALE (object);
462 gtk_scale_set_digits (scale, g_value_get_int (value));
464 case PROP_DRAW_VALUE:
465 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
468 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
471 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
477 gtk_scale_get_property (GObject *object,
482 GtkScale *scale = GTK_SCALE (object);
483 GtkScalePrivate *priv = scale->priv;
488 g_value_set_int (value, priv->digits);
490 case PROP_DRAW_VALUE:
491 g_value_set_boolean (value, priv->draw_value);
494 g_value_set_enum (value, priv->value_pos);
497 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
504 * @orientation: the scale's orientation.
505 * @adjustment: the #GtkAdjustment which sets the range of the scale, or
506 * %NULL to create a new adjustment.
508 * Creates a new #GtkScale.
510 * Return value: a new #GtkScale
515 gtk_scale_new (GtkOrientation orientation,
516 GtkAdjustment *adjustment)
518 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
521 return g_object_new (GTK_TYPE_SCALE,
522 "orientation", orientation,
523 "adjustment", adjustment,
528 * gtk_scale_new_with_range:
529 * @orientation: the scale's orientation.
530 * @min: minimum value
531 * @max: maximum value
532 * @step: step increment (tick size) used with keyboard shortcuts
534 * Creates a new scale widget with the given orientation that lets the
535 * user input a number between @min and @max (including @min and @max)
536 * with the increment @step. @step must be nonzero; it's the distance
537 * the slider moves when using the arrow keys to adjust the scale
540 * Note that the way in which the precision is derived works best if @step
541 * is a power of ten. If the resulting precision is not suitable for your
542 * needs, use gtk_scale_set_digits() to correct it.
544 * Return value: a new #GtkScale
549 gtk_scale_new_with_range (GtkOrientation orientation,
557 g_return_val_if_fail (min < max, NULL);
558 g_return_val_if_fail (step != 0.0, NULL);
560 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
562 if (fabs (step) >= 1.0 || step == 0.0)
568 digits = abs ((gint) floor (log10 (fabs (step))));
573 return g_object_new (GTK_TYPE_SCALE,
574 "orientation", orientation,
581 * gtk_scale_set_digits:
582 * @scale: a #GtkScale
583 * @digits: the number of decimal places to display,
584 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
586 * Sets the number of decimal places that are displayed in the value.
587 * Also causes the value of the adjustment to be rounded off to this
588 * number of digits, so the retrieved value matches the value the user saw.
591 gtk_scale_set_digits (GtkScale *scale,
594 GtkScalePrivate *priv;
597 g_return_if_fail (GTK_IS_SCALE (scale));
600 range = GTK_RANGE (scale);
602 digits = CLAMP (digits, -1, MAX_DIGITS);
604 if (priv->digits != digits)
606 priv->digits = digits;
607 if (priv->draw_value)
608 _gtk_range_set_round_digits (range, digits);
610 _gtk_scale_clear_layout (scale);
611 gtk_widget_queue_resize (GTK_WIDGET (scale));
613 g_object_notify (G_OBJECT (scale), "digits");
618 * gtk_scale_get_digits:
619 * @scale: a #GtkScale
621 * Gets the number of decimal places that are displayed in the value.
623 * Returns: the number of decimal places that are displayed
626 gtk_scale_get_digits (GtkScale *scale)
628 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
630 return scale->priv->digits;
634 * gtk_scale_set_draw_value:
635 * @scale: a #GtkScale
636 * @draw_value: %TRUE to draw the value
638 * Specifies whether the current value is displayed as a string next
642 gtk_scale_set_draw_value (GtkScale *scale,
645 GtkScalePrivate *priv;
647 g_return_if_fail (GTK_IS_SCALE (scale));
651 draw_value = draw_value != FALSE;
653 if (priv->draw_value != draw_value)
655 priv->draw_value = draw_value;
657 _gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
659 _gtk_range_set_round_digits (GTK_RANGE (scale), -1);
661 _gtk_scale_clear_layout (scale);
663 gtk_widget_queue_resize (GTK_WIDGET (scale));
665 g_object_notify (G_OBJECT (scale), "draw-value");
670 * gtk_scale_get_draw_value:
671 * @scale: a #GtkScale
673 * Returns whether the current value is displayed as a string
674 * next to the slider.
676 * Returns: whether the current value is displayed as a string
679 gtk_scale_get_draw_value (GtkScale *scale)
681 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
683 return scale->priv->draw_value;
687 * gtk_scale_set_value_pos:
688 * @scale: a #GtkScale
689 * @pos: the position in which the current value is displayed
691 * Sets the position in which the current value is displayed.
694 gtk_scale_set_value_pos (GtkScale *scale,
697 GtkScalePrivate *priv;
700 g_return_if_fail (GTK_IS_SCALE (scale));
704 if (priv->value_pos != pos)
706 priv->value_pos = pos;
707 widget = GTK_WIDGET (scale);
709 _gtk_scale_clear_layout (scale);
710 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
711 gtk_widget_queue_resize (widget);
713 g_object_notify (G_OBJECT (scale), "value-pos");
718 * gtk_scale_get_value_pos:
719 * @scale: a #GtkScale
721 * Gets the position in which the current value is displayed.
723 * Returns: the position in which the current value is displayed
726 gtk_scale_get_value_pos (GtkScale *scale)
728 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
730 return scale->priv->value_pos;
734 gtk_scale_get_range_border (GtkRange *range,
737 GtkScalePrivate *priv;
742 widget = GTK_WIDGET (range);
743 scale = GTK_SCALE (range);
746 _gtk_scale_get_value_size (scale, &w, &h);
753 if (priv->draw_value)
756 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
758 switch (priv->value_pos)
761 border->left += w + value_spacing;
764 border->right += w + value_spacing;
767 border->top += h + value_spacing;
770 border->bottom += h + value_spacing;
779 gint n1, w1, h1, n2, w2, h2;
781 gtk_widget_style_get (widget,
782 "slider-width", &slider_width,
783 "value-spacing", &value_spacing,
787 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
789 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
791 border->top += h1 + value_spacing + slider_width / 2;
793 border->bottom += h2 + value_spacing + slider_width / 2;
797 gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
799 border->left += w1 + value_spacing + slider_width / 2;
801 border->right += w2 + value_spacing + slider_width / 2;
806 /* FIXME this could actually be static at the moment. */
808 _gtk_scale_get_value_size (GtkScale *scale,
812 GtkScalePrivate *priv = scale->priv;
815 if (priv->draw_value)
817 GtkAdjustment *adjustment;
819 PangoRectangle logical_rect;
822 range = GTK_RANGE (scale);
824 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
825 adjustment = gtk_range_get_adjustment (range);
827 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
828 pango_layout_set_text (layout, txt, -1);
831 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
834 *width = logical_rect.width;
836 *height = logical_rect.height;
838 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
839 pango_layout_set_text (layout, txt, -1);
842 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
845 *width = MAX (*width, logical_rect.width);
847 *height = MAX (*height, logical_rect.height);
849 g_object_unref (layout);
862 gtk_scale_get_mark_label_size (GtkScale *scale,
863 GtkPositionType position,
871 GtkScalePrivate *priv = scale->priv;
873 PangoRectangle logical_rect;
877 *count1 = *count2 = 0;
878 *width1 = *width2 = 0;
879 *height1 = *height2 = 0;
881 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
883 for (m = priv->marks; m; m = m->next)
885 GtkScaleMark *mark = m->data;
887 if (mark->markup && *mark->markup)
889 pango_layout_set_markup (layout, mark->markup, -1);
890 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
892 w = logical_rect.width;
893 h = logical_rect.height;
901 if (mark->position == position)
904 *width1 = MAX (*width1, w);
905 *height1 = MAX (*height1, h);
910 *width2 = MAX (*width2, w);
911 *height2 = MAX (*height2, h);
915 g_object_unref (layout);
919 gtk_scale_style_set (GtkWidget *widget,
925 range = GTK_RANGE (widget);
927 gtk_widget_style_get (widget,
928 "slider-length", &slider_length,
931 gtk_range_set_min_slider_size (range, slider_length);
933 _gtk_scale_clear_layout (GTK_SCALE (widget));
935 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_set (widget, previous);
939 gtk_scale_screen_changed (GtkWidget *widget,
940 GdkScreen *old_screen)
942 _gtk_scale_clear_layout (GTK_SCALE (widget));
946 gtk_scale_get_preferred_width (GtkWidget *widget,
950 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural);
952 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
954 gint n1, w1, h1, n2, w2, h2;
958 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
960 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
962 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
963 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
966 *minimum = MAX (*minimum, w);
967 *natural = MAX (*natural, w);
972 gtk_scale_get_preferred_height (GtkWidget *widget,
976 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural);
979 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
981 gint n1, w1, h1, n2, w2, h2;
985 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
987 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
988 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
989 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
992 *minimum = MAX (*minimum, h);
993 *natural = MAX (*natural, h);
998 find_next_pos (GtkWidget *widget,
1001 GtkPositionType pos,
1004 GtkAllocation allocation;
1008 for (m = list->next, i = 1; m; m = m->next, i++)
1010 GtkScaleMark *mark = m->data;
1012 if (match == (mark->position == pos))
1016 gtk_widget_get_allocation (widget, &allocation);
1017 if (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM)
1018 return allocation.width;
1020 return allocation.height;
1024 gtk_scale_draw (GtkWidget *widget,
1027 GtkScale *scale = GTK_SCALE (widget);
1028 GtkScalePrivate *priv = scale->priv;
1029 GtkRange *range = GTK_RANGE (scale);
1030 GtkStateType state_type;
1039 style = gtk_widget_get_style (widget);
1040 gtk_widget_style_get (widget,
1041 "focus-padding", &focus_padding,
1042 "slider-width", &slider_width,
1043 "value-spacing", &value_spacing,
1046 /* We need to chain up _first_ so the various geometry members of
1047 * GtkRange struct are updated.
1049 GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1051 state_type = GTK_STATE_NORMAL;
1052 if (!gtk_widget_is_sensitive (widget))
1053 state_type = GTK_STATE_INSENSITIVE;
1057 GtkOrientation orientation;
1058 GdkRectangle range_rect;
1060 gint x1, x2, x3, y1, y2, y3;
1061 PangoLayout *layout;
1062 PangoRectangle logical_rect;
1064 gint min_pos_before, min_pos_after;
1065 gint min_pos, max_pos;
1067 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1068 n_marks = _gtk_range_get_stop_positions (range, &marks);
1069 layout = gtk_widget_create_pango_layout (widget, NULL);
1070 gtk_range_get_range_rect (range, &range_rect);
1072 min_pos_before = min_pos_after = 0;
1074 for (m = priv->marks, i = 0; m; m = m->next, i++)
1076 GtkScaleMark *mark = m->data;
1078 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1081 if (mark->position == GTK_POS_TOP)
1084 y2 = y1 - slider_width / 2;
1085 min_pos = min_pos_before;
1086 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1090 y1 = range_rect.y + range_rect.height;
1091 y2 = y1 + slider_width / 2;
1092 min_pos = min_pos_after;
1093 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep;
1096 gtk_paint_vline (style, cr, state_type,
1097 widget, "scale-mark", y1, y2, x1);
1101 pango_layout_set_markup (layout, mark->markup, -1);
1102 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1104 x3 = x1 - logical_rect.width / 2;
1107 if (x3 + logical_rect.width > max_pos)
1108 x3 = max_pos - logical_rect.width;
1111 if (mark->position == GTK_POS_TOP)
1113 y3 = y2 - value_spacing - logical_rect.height;
1114 min_pos_before = x3 + logical_rect.width + min_sep;
1118 y3 = y2 + value_spacing;
1119 min_pos_after = x3 + logical_rect.width + min_sep;
1122 gtk_paint_layout (style, cr, state_type,
1123 FALSE, widget, "scale-mark",
1129 if (mark->position == GTK_POS_LEFT)
1132 x2 = range_rect.x - slider_width / 2;
1133 min_pos = min_pos_before;
1134 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep;
1138 x1 = range_rect.x + range_rect.width;
1139 x2 = range_rect.x + range_rect.width + slider_width / 2;
1140 min_pos = min_pos_after;
1141 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep;
1145 gtk_paint_hline (style, cr, state_type,
1146 widget, "range-mark", x1, x2, y1);
1150 pango_layout_set_markup (layout, mark->markup, -1);
1151 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1153 y3 = y1 - logical_rect.height / 2;
1156 if (y3 + logical_rect.height > max_pos)
1157 y3 = max_pos - logical_rect.height;
1160 if (mark->position == GTK_POS_LEFT)
1162 x3 = x2 - value_spacing - logical_rect.width;
1163 min_pos_before = y3 + logical_rect.height + min_sep;
1167 x3 = x2 + value_spacing;
1168 min_pos_after = y3 + logical_rect.height + min_sep;
1171 gtk_paint_layout (style, cr, state_type,
1172 FALSE, widget, "scale-mark",
1178 g_object_unref (layout);
1182 if (priv->draw_value)
1184 GtkOrientation orientation;
1185 GtkAllocation allocation;
1187 PangoLayout *layout;
1190 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1191 layout = gtk_scale_get_layout (scale);
1192 gtk_scale_get_layout_offsets (scale, &x, &y);
1193 gtk_widget_get_allocation (widget, &allocation);
1195 gtk_paint_layout (style,
1200 orientation == GTK_ORIENTATION_HORIZONTAL ?
1201 "hscale" : "vscale",
1212 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1216 GtkScalePrivate *priv = scale->priv;
1217 GtkAllocation allocation;
1218 GtkWidget *widget = GTK_WIDGET (scale);
1219 GtkRange *range = GTK_RANGE (widget);
1220 GdkRectangle range_rect;
1221 PangoLayout *layout = gtk_scale_get_layout (scale);
1222 PangoRectangle logical_rect;
1223 gint slider_start, slider_end;
1234 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1236 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1238 gtk_widget_get_allocation (widget, &allocation);
1239 gtk_range_get_range_rect (range, &range_rect);
1240 gtk_range_get_slider_range (range,
1244 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1246 switch (priv->value_pos)
1249 *x = range_rect.x - value_spacing - logical_rect.width;
1250 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1254 *x = range_rect.x + range_rect.width + value_spacing;
1255 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1259 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1260 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1261 *y = range_rect.y - logical_rect.height - value_spacing;
1264 case GTK_POS_BOTTOM:
1265 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1266 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1267 *y = range_rect.y + range_rect.height + value_spacing;
1271 g_return_if_reached ();
1277 switch (priv->value_pos)
1280 *x = range_rect.x - logical_rect.width - value_spacing;
1281 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1282 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1286 *x = range_rect.x + range_rect.width + value_spacing;
1287 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1288 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1292 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1293 *y = range_rect.y - logical_rect.height - value_spacing;
1296 case GTK_POS_BOTTOM:
1297 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1298 *y = range_rect.y + range_rect.height + value_spacing;
1302 g_return_if_reached ();
1311 * _gtk_scale_format_value:
1312 * @scale: a #GtkScale
1313 * @value: adjustment value
1315 * Emits #GtkScale::format-value signal to format the value,
1316 * if no user signal handlers, falls back to a default format.
1318 * Return value: formatted value
1321 _gtk_scale_format_value (GtkScale *scale,
1324 GtkScalePrivate *priv = scale->priv;
1327 g_signal_emit (scale,
1328 signals[FORMAT_VALUE],
1336 /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1337 return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value);
1341 gtk_scale_finalize (GObject *object)
1343 GtkScale *scale = GTK_SCALE (object);
1345 _gtk_scale_clear_layout (scale);
1346 gtk_scale_clear_marks (scale);
1348 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1352 * gtk_scale_get_layout:
1353 * @scale: A #GtkScale
1355 * Gets the #PangoLayout used to display the scale. The returned
1356 * object is owned by the scale so does not need to be freed by
1359 * Return value: (transfer none): the #PangoLayout for this scale,
1360 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1365 gtk_scale_get_layout (GtkScale *scale)
1367 GtkScalePrivate *priv;
1370 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1376 if (priv->draw_value)
1377 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1380 if (priv->draw_value)
1382 txt = _gtk_scale_format_value (scale,
1383 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1384 pango_layout_set_text (priv->layout, txt, -1);
1388 return priv->layout;
1392 * gtk_scale_get_layout_offsets:
1393 * @scale: a #GtkScale
1394 * @x: (allow-none): location to store X offset of layout, or %NULL
1395 * @y: (allow-none): location to store Y offset of layout, or %NULL
1397 * Obtains the coordinates where the scale will draw the
1398 * #PangoLayout representing the text in the scale. Remember
1399 * when using the #PangoLayout function you need to convert to
1400 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1402 * If the #GtkScale:draw-value property is %FALSE, the return
1403 * values are undefined.
1408 gtk_scale_get_layout_offsets (GtkScale *scale,
1415 g_return_if_fail (GTK_IS_SCALE (scale));
1417 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1418 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1428 _gtk_scale_clear_layout (GtkScale *scale)
1430 GtkScalePrivate *priv = scale->priv;
1432 g_return_if_fail (GTK_IS_SCALE (scale));
1436 g_object_unref (priv->layout);
1437 priv->layout = NULL;
1442 gtk_scale_mark_free (GtkScaleMark *mark)
1444 g_free (mark->markup);
1449 * gtk_scale_clear_marks:
1450 * @scale: a #GtkScale
1452 * Removes any marks that have been added with gtk_scale_add_mark().
1457 gtk_scale_clear_marks (GtkScale *scale)
1459 GtkScalePrivate *priv;
1461 g_return_if_fail (GTK_IS_SCALE (scale));
1465 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1466 g_slist_free (priv->marks);
1469 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1471 gtk_widget_queue_resize (GTK_WIDGET (scale));
1475 compare_marks (gpointer a, gpointer b)
1477 GtkScaleMark *ma, *mb;
1481 return (gint) (ma->value - mb->value);
1485 * gtk_scale_add_mark:
1486 * @scale: a #GtkScale
1487 * @value: the value at which the mark is placed, must be between
1488 * the lower and upper limits of the scales' adjustment
1489 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1490 * is drawn above the scale, anything else below. For a vertical scale,
1491 * #GTK_POS_LEFT is drawn to the left of the scale, anything else to the
1493 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1496 * Adds a mark at @value.
1498 * A mark is indicated visually by drawing a tick mark next to the scale,
1499 * and GTK+ makes it easy for the user to position the scale exactly at the
1502 * If @markup is not %NULL, text is shown next to the tick mark.
1504 * To remove marks from a scale, use gtk_scale_clear_marks().
1509 gtk_scale_add_mark (GtkScale *scale,
1511 GtkPositionType position,
1512 const gchar *markup)
1514 GtkScalePrivate *priv;
1520 g_return_if_fail (GTK_IS_SCALE (scale));
1524 mark = g_new (GtkScaleMark, 1);
1525 mark->value = value;
1526 mark->markup = g_strdup (markup);
1527 mark->position = position;
1529 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1530 (GCompareFunc) compare_marks);
1532 n = g_slist_length (priv->marks);
1533 values = g_new (gdouble, n);
1534 for (m = priv->marks, i = 0; m; m = m->next, i++)
1537 values[i] = mark->value;
1540 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1544 gtk_widget_queue_resize (GTK_WIDGET (scale));
1547 static GtkBuildableIface *parent_buildable_iface;
1550 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1552 parent_buildable_iface = g_type_interface_peek_parent (iface);
1553 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1554 iface->custom_finished = gtk_scale_buildable_custom_finished;
1560 GtkBuilder *builder;
1562 } MarksSubparserData;
1567 GtkPositionType position;
1570 gboolean translatable;
1574 mark_data_free (MarkData *data)
1576 g_string_free (data->markup, TRUE);
1577 g_free (data->context);
1578 g_slice_free (MarkData, data);
1582 marks_start_element (GMarkupParseContext *context,
1583 const gchar *element_name,
1584 const gchar **names,
1585 const gchar **values,
1589 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1591 gint line_number, char_number;
1593 if (strcmp (element_name, "marks") == 0)
1595 else if (strcmp (element_name, "mark") == 0)
1598 gboolean has_value = FALSE;
1599 GtkPositionType position = GTK_POS_BOTTOM;
1600 const gchar *msg_context = NULL;
1601 gboolean translatable = FALSE;
1604 for (i = 0; names[i]; i++)
1606 if (strcmp (names[i], "translatable") == 0)
1608 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1611 else if (strcmp (names[i], "comments") == 0)
1613 /* do nothing, comments are for translators */
1615 else if (strcmp (names[i], "context") == 0)
1616 msg_context = values[i];
1617 else if (strcmp (names[i], "value") == 0)
1619 GValue gvalue = { 0, };
1621 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1624 value = g_value_get_double (&gvalue);
1627 else if (strcmp (names[i], "position") == 0)
1629 GValue gvalue = { 0, };
1631 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1634 position = g_value_get_enum (&gvalue);
1638 g_markup_parse_context_get_position (context,
1643 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1644 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1646 line_number, char_number, names[i], "mark");
1653 g_markup_parse_context_get_position (context,
1658 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1659 "%s:%d:%d <%s> requires attribute \"%s\"",
1661 line_number, char_number, "mark",
1666 mark = g_slice_new (MarkData);
1667 mark->value = value;
1668 mark->position = position;
1669 mark->markup = g_string_new ("");
1670 mark->context = g_strdup (msg_context);
1671 mark->translatable = translatable;
1673 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1677 g_markup_parse_context_get_position (context,
1682 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1683 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1685 line_number, char_number, element_name);
1691 marks_text (GMarkupParseContext *context,
1697 MarksSubparserData *data = (MarksSubparserData*)user_data;
1699 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1701 MarkData *mark = data->marks->data;
1703 g_string_append_len (mark->markup, text, text_len);
1707 static const GMarkupParser marks_parser =
1709 marks_start_element,
1716 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1717 GtkBuilder *builder,
1719 const gchar *tagname,
1720 GMarkupParser *parser,
1723 MarksSubparserData *parser_data;
1728 if (strcmp (tagname, "marks") == 0)
1730 parser_data = g_slice_new0 (MarksSubparserData);
1731 parser_data->scale = GTK_SCALE (buildable);
1732 parser_data->marks = NULL;
1734 *parser = marks_parser;
1735 *data = parser_data;
1739 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1740 tagname, parser, data);
1744 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1745 GtkBuilder *builder,
1747 const gchar *tagname,
1750 GtkScale *scale = GTK_SCALE (buildable);
1751 MarksSubparserData *marks_data;
1753 if (strcmp (tagname, "marks") == 0)
1758 marks_data = (MarksSubparserData *)user_data;
1760 for (m = marks_data->marks; m; m = m->next)
1762 MarkData *mdata = m->data;
1764 if (mdata->translatable && mdata->markup->len)
1765 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1767 mdata->markup->str);
1769 markup = mdata->markup->str;
1771 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1773 mark_data_free (mdata);
1776 g_slist_free (marks_data->marks);
1777 g_slice_free (MarksSubparserData, marks_data);