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_set (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_set = gtk_scale_style_set;
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));
418 gtk_scale_orientation_notify (GtkRange *range,
419 const GParamSpec *pspec)
421 GtkOrientation orientation;
423 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
424 gtk_range_set_flippable (range,
425 orientation == GTK_ORIENTATION_HORIZONTAL);
429 gtk_scale_init (GtkScale *scale)
431 GtkScalePrivate *priv;
432 GtkRange *range = GTK_RANGE (scale);
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),
455 gtk_scale_set_property (GObject *object,
462 scale = GTK_SCALE (object);
467 gtk_scale_set_digits (scale, g_value_get_int (value));
469 case PROP_DRAW_VALUE:
470 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
473 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
476 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482 gtk_scale_get_property (GObject *object,
487 GtkScale *scale = GTK_SCALE (object);
488 GtkScalePrivate *priv = scale->priv;
493 g_value_set_int (value, priv->digits);
495 case PROP_DRAW_VALUE:
496 g_value_set_boolean (value, priv->draw_value);
499 g_value_set_enum (value, priv->value_pos);
502 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
509 * @orientation: the scale's orientation.
510 * @adjustment: the #GtkAdjustment which sets the range of the scale, or
511 * %NULL to create a new adjustment.
513 * Creates a new #GtkScale.
515 * Return value: a new #GtkScale
520 gtk_scale_new (GtkOrientation orientation,
521 GtkAdjustment *adjustment)
523 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
526 return g_object_new (GTK_TYPE_SCALE,
527 "orientation", orientation,
528 "adjustment", adjustment,
533 * gtk_scale_new_with_range:
534 * @orientation: the scale's orientation.
535 * @min: minimum value
536 * @max: maximum value
537 * @step: step increment (tick size) used with keyboard shortcuts
539 * Creates a new scale widget with the given orientation that lets the
540 * user input a number between @min and @max (including @min and @max)
541 * with the increment @step. @step must be nonzero; it's the distance
542 * the slider moves when using the arrow keys to adjust the scale
545 * Note that the way in which the precision is derived works best if @step
546 * is a power of ten. If the resulting precision is not suitable for your
547 * needs, use gtk_scale_set_digits() to correct it.
549 * Return value: a new #GtkScale
554 gtk_scale_new_with_range (GtkOrientation orientation,
562 g_return_val_if_fail (min < max, NULL);
563 g_return_val_if_fail (step != 0.0, NULL);
565 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
567 if (fabs (step) >= 1.0 || step == 0.0)
573 digits = abs ((gint) floor (log10 (fabs (step))));
578 return g_object_new (GTK_TYPE_SCALE,
579 "orientation", orientation,
586 * gtk_scale_set_digits:
587 * @scale: a #GtkScale
588 * @digits: the number of decimal places to display,
589 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
591 * Sets the number of decimal places that are displayed in the value.
592 * Also causes the value of the adjustment to be rounded off to this
593 * number of digits, so the retrieved value matches the value the user saw.
596 gtk_scale_set_digits (GtkScale *scale,
599 GtkScalePrivate *priv;
602 g_return_if_fail (GTK_IS_SCALE (scale));
605 range = GTK_RANGE (scale);
607 digits = CLAMP (digits, -1, MAX_DIGITS);
609 if (priv->digits != digits)
611 priv->digits = digits;
612 if (priv->draw_value)
613 _gtk_range_set_round_digits (range, digits);
615 _gtk_scale_clear_layout (scale);
616 gtk_widget_queue_resize (GTK_WIDGET (scale));
618 g_object_notify (G_OBJECT (scale), "digits");
623 * gtk_scale_get_digits:
624 * @scale: a #GtkScale
626 * Gets the number of decimal places that are displayed in the value.
628 * Returns: the number of decimal places that are displayed
631 gtk_scale_get_digits (GtkScale *scale)
633 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
635 return scale->priv->digits;
639 * gtk_scale_set_draw_value:
640 * @scale: a #GtkScale
641 * @draw_value: %TRUE to draw the value
643 * Specifies whether the current value is displayed as a string next
647 gtk_scale_set_draw_value (GtkScale *scale,
650 GtkScalePrivate *priv;
652 g_return_if_fail (GTK_IS_SCALE (scale));
656 draw_value = draw_value != FALSE;
658 if (priv->draw_value != draw_value)
660 priv->draw_value = draw_value;
662 _gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
664 _gtk_range_set_round_digits (GTK_RANGE (scale), -1);
666 _gtk_scale_clear_layout (scale);
668 gtk_widget_queue_resize (GTK_WIDGET (scale));
670 g_object_notify (G_OBJECT (scale), "draw-value");
675 * gtk_scale_get_draw_value:
676 * @scale: a #GtkScale
678 * Returns whether the current value is displayed as a string
679 * next to the slider.
681 * Returns: whether the current value is displayed as a string
684 gtk_scale_get_draw_value (GtkScale *scale)
686 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
688 return scale->priv->draw_value;
692 * gtk_scale_set_value_pos:
693 * @scale: a #GtkScale
694 * @pos: the position in which the current value is displayed
696 * Sets the position in which the current value is displayed.
699 gtk_scale_set_value_pos (GtkScale *scale,
702 GtkScalePrivate *priv;
705 g_return_if_fail (GTK_IS_SCALE (scale));
709 if (priv->value_pos != pos)
711 priv->value_pos = pos;
712 widget = GTK_WIDGET (scale);
714 _gtk_scale_clear_layout (scale);
715 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
716 gtk_widget_queue_resize (widget);
718 g_object_notify (G_OBJECT (scale), "value-pos");
723 * gtk_scale_get_value_pos:
724 * @scale: a #GtkScale
726 * Gets the position in which the current value is displayed.
728 * Returns: the position in which the current value is displayed
731 gtk_scale_get_value_pos (GtkScale *scale)
733 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
735 return scale->priv->value_pos;
739 gtk_scale_get_range_border (GtkRange *range,
742 GtkScalePrivate *priv;
747 widget = GTK_WIDGET (range);
748 scale = GTK_SCALE (range);
751 _gtk_scale_get_value_size (scale, &w, &h);
758 if (priv->draw_value)
761 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
763 switch (priv->value_pos)
766 border->left += w + value_spacing;
769 border->right += w + value_spacing;
772 border->top += h + value_spacing;
775 border->bottom += h + value_spacing;
784 gint n1, w1, h1, n2, w2, h2;
786 gtk_widget_style_get (widget,
787 "slider-width", &slider_width,
788 "value-spacing", &value_spacing,
792 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
794 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
796 border->top += h1 + value_spacing + slider_width / 2;
798 border->bottom += h2 + value_spacing + slider_width / 2;
802 gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
804 border->left += w1 + value_spacing + slider_width / 2;
806 border->right += w2 + value_spacing + slider_width / 2;
811 /* FIXME this could actually be static at the moment. */
813 _gtk_scale_get_value_size (GtkScale *scale,
817 GtkScalePrivate *priv = scale->priv;
820 if (priv->draw_value)
822 GtkAdjustment *adjustment;
824 PangoRectangle logical_rect;
827 range = GTK_RANGE (scale);
829 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
830 adjustment = gtk_range_get_adjustment (range);
832 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
833 pango_layout_set_text (layout, txt, -1);
836 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
839 *width = logical_rect.width;
841 *height = logical_rect.height;
843 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
844 pango_layout_set_text (layout, txt, -1);
847 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
850 *width = MAX (*width, logical_rect.width);
852 *height = MAX (*height, logical_rect.height);
854 g_object_unref (layout);
867 gtk_scale_get_mark_label_size (GtkScale *scale,
868 GtkPositionType position,
876 GtkScalePrivate *priv = scale->priv;
878 PangoRectangle logical_rect;
882 *count1 = *count2 = 0;
883 *width1 = *width2 = 0;
884 *height1 = *height2 = 0;
886 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
888 for (m = priv->marks; m; m = m->next)
890 GtkScaleMark *mark = m->data;
892 if (mark->markup && *mark->markup)
894 pango_layout_set_markup (layout, mark->markup, -1);
895 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
897 w = logical_rect.width;
898 h = logical_rect.height;
906 if (mark->position == position)
909 *width1 = MAX (*width1, w);
910 *height1 = MAX (*height1, h);
915 *width2 = MAX (*width2, w);
916 *height2 = MAX (*height2, h);
920 g_object_unref (layout);
924 gtk_scale_style_set (GtkWidget *widget,
930 range = GTK_RANGE (widget);
932 gtk_widget_style_get (widget,
933 "slider-length", &slider_length,
936 gtk_range_set_min_slider_size (range, slider_length);
938 _gtk_scale_clear_layout (GTK_SCALE (widget));
940 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_set (widget, previous);
944 gtk_scale_screen_changed (GtkWidget *widget,
945 GdkScreen *old_screen)
947 _gtk_scale_clear_layout (GTK_SCALE (widget));
951 gtk_scale_get_preferred_width (GtkWidget *widget,
955 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural);
957 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
959 gint n1, w1, h1, n2, w2, h2;
963 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
965 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
967 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
968 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
971 *minimum = MAX (*minimum, w);
972 *natural = MAX (*natural, w);
977 gtk_scale_get_preferred_height (GtkWidget *widget,
981 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural);
984 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
986 gint n1, w1, h1, n2, w2, h2;
990 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
992 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
993 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
994 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
997 *minimum = MAX (*minimum, h);
998 *natural = MAX (*natural, h);
1003 find_next_pos (GtkWidget *widget,
1006 GtkPositionType pos,
1009 GtkAllocation allocation;
1013 for (m = list->next, i = 1; m; m = m->next, i++)
1015 GtkScaleMark *mark = m->data;
1017 if (match == (mark->position == pos))
1021 gtk_widget_get_allocation (widget, &allocation);
1022 if (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM)
1023 return allocation.width;
1025 return allocation.height;
1029 gtk_scale_draw (GtkWidget *widget,
1032 GtkScale *scale = GTK_SCALE (widget);
1033 GtkScalePrivate *priv = scale->priv;
1034 GtkRange *range = GTK_RANGE (scale);
1035 GtkStateType state_type;
1044 style = gtk_widget_get_style (widget);
1045 gtk_widget_style_get (widget,
1046 "focus-padding", &focus_padding,
1047 "slider-width", &slider_width,
1048 "value-spacing", &value_spacing,
1051 /* We need to chain up _first_ so the various geometry members of
1052 * GtkRange struct are updated.
1054 GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1056 state_type = GTK_STATE_NORMAL;
1057 if (!gtk_widget_is_sensitive (widget))
1058 state_type = GTK_STATE_INSENSITIVE;
1062 GtkOrientation orientation;
1063 GdkRectangle range_rect;
1065 gint x1, x2, x3, y1, y2, y3;
1066 PangoLayout *layout;
1067 PangoRectangle logical_rect;
1069 gint min_pos_before, min_pos_after;
1070 gint min_pos, max_pos;
1072 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1073 n_marks = _gtk_range_get_stop_positions (range, &marks);
1074 layout = gtk_widget_create_pango_layout (widget, NULL);
1075 gtk_range_get_range_rect (range, &range_rect);
1077 min_pos_before = min_pos_after = 0;
1079 for (m = priv->marks, i = 0; m; m = m->next, i++)
1081 GtkScaleMark *mark = m->data;
1083 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1086 if (mark->position == GTK_POS_TOP)
1089 y2 = y1 - slider_width / 2;
1090 min_pos = min_pos_before;
1091 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1095 y1 = range_rect.y + range_rect.height;
1096 y2 = y1 + slider_width / 2;
1097 min_pos = min_pos_after;
1098 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep;
1101 gtk_paint_vline (style, cr, state_type,
1102 widget, "scale-mark", y1, y2, x1);
1106 pango_layout_set_markup (layout, mark->markup, -1);
1107 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1109 x3 = x1 - logical_rect.width / 2;
1112 if (x3 + logical_rect.width > max_pos)
1113 x3 = max_pos - logical_rect.width;
1116 if (mark->position == GTK_POS_TOP)
1118 y3 = y2 - value_spacing - logical_rect.height;
1119 min_pos_before = x3 + logical_rect.width + min_sep;
1123 y3 = y2 + value_spacing;
1124 min_pos_after = x3 + logical_rect.width + min_sep;
1127 gtk_paint_layout (style, cr, state_type,
1128 FALSE, widget, "scale-mark",
1134 if (mark->position == GTK_POS_LEFT)
1137 x2 = range_rect.x - slider_width / 2;
1138 min_pos = min_pos_before;
1139 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep;
1143 x1 = range_rect.x + range_rect.width;
1144 x2 = range_rect.x + range_rect.width + slider_width / 2;
1145 min_pos = min_pos_after;
1146 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep;
1150 gtk_paint_hline (style, cr, state_type,
1151 widget, "range-mark", x1, x2, y1);
1155 pango_layout_set_markup (layout, mark->markup, -1);
1156 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1158 y3 = y1 - logical_rect.height / 2;
1161 if (y3 + logical_rect.height > max_pos)
1162 y3 = max_pos - logical_rect.height;
1165 if (mark->position == GTK_POS_LEFT)
1167 x3 = x2 - value_spacing - logical_rect.width;
1168 min_pos_before = y3 + logical_rect.height + min_sep;
1172 x3 = x2 + value_spacing;
1173 min_pos_after = y3 + logical_rect.height + min_sep;
1176 gtk_paint_layout (style, cr, state_type,
1177 FALSE, widget, "scale-mark",
1183 g_object_unref (layout);
1187 if (priv->draw_value)
1189 GtkOrientation orientation;
1190 GtkAllocation allocation;
1192 PangoLayout *layout;
1195 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1196 layout = gtk_scale_get_layout (scale);
1197 gtk_scale_get_layout_offsets (scale, &x, &y);
1198 gtk_widget_get_allocation (widget, &allocation);
1200 gtk_paint_layout (style,
1205 orientation == GTK_ORIENTATION_HORIZONTAL ?
1206 "hscale" : "vscale",
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 /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1342 return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value);
1346 gtk_scale_finalize (GObject *object)
1348 GtkScale *scale = GTK_SCALE (object);
1350 _gtk_scale_clear_layout (scale);
1351 gtk_scale_clear_marks (scale);
1353 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1357 * gtk_scale_get_layout:
1358 * @scale: A #GtkScale
1360 * Gets the #PangoLayout used to display the scale. The returned
1361 * object is owned by the scale so does not need to be freed by
1364 * Return value: (transfer none): the #PangoLayout for this scale,
1365 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1370 gtk_scale_get_layout (GtkScale *scale)
1372 GtkScalePrivate *priv;
1375 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1381 if (priv->draw_value)
1382 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1385 if (priv->draw_value)
1387 txt = _gtk_scale_format_value (scale,
1388 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1389 pango_layout_set_text (priv->layout, txt, -1);
1393 return priv->layout;
1397 * gtk_scale_get_layout_offsets:
1398 * @scale: a #GtkScale
1399 * @x: (allow-none): location to store X offset of layout, or %NULL
1400 * @y: (allow-none): location to store Y offset of layout, or %NULL
1402 * Obtains the coordinates where the scale will draw the
1403 * #PangoLayout representing the text in the scale. Remember
1404 * when using the #PangoLayout function you need to convert to
1405 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1407 * If the #GtkScale:draw-value property is %FALSE, the return
1408 * values are undefined.
1413 gtk_scale_get_layout_offsets (GtkScale *scale,
1420 g_return_if_fail (GTK_IS_SCALE (scale));
1422 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1423 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1433 _gtk_scale_clear_layout (GtkScale *scale)
1435 GtkScalePrivate *priv = scale->priv;
1437 g_return_if_fail (GTK_IS_SCALE (scale));
1441 g_object_unref (priv->layout);
1442 priv->layout = NULL;
1447 gtk_scale_mark_free (GtkScaleMark *mark)
1449 g_free (mark->markup);
1454 * gtk_scale_clear_marks:
1455 * @scale: a #GtkScale
1457 * Removes any marks that have been added with gtk_scale_add_mark().
1462 gtk_scale_clear_marks (GtkScale *scale)
1464 GtkScalePrivate *priv;
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 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1476 gtk_widget_queue_resize (GTK_WIDGET (scale));
1480 compare_marks (gpointer a, gpointer b)
1482 GtkScaleMark *ma, *mb;
1486 return (gint) (ma->value - mb->value);
1490 * gtk_scale_add_mark:
1491 * @scale: a #GtkScale
1492 * @value: the value at which the mark is placed, must be between
1493 * the lower and upper limits of the scales' adjustment
1494 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1495 * is drawn above the scale, anything else below. For a vertical scale,
1496 * #GTK_POS_LEFT is drawn to the left of the scale, anything else to the
1498 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1501 * Adds a mark at @value.
1503 * A mark is indicated visually by drawing a tick mark next to the scale,
1504 * and GTK+ makes it easy for the user to position the scale exactly at the
1507 * If @markup is not %NULL, text is shown next to the tick mark.
1509 * To remove marks from a scale, use gtk_scale_clear_marks().
1514 gtk_scale_add_mark (GtkScale *scale,
1516 GtkPositionType position,
1517 const gchar *markup)
1519 GtkScalePrivate *priv;
1525 g_return_if_fail (GTK_IS_SCALE (scale));
1529 mark = g_new (GtkScaleMark, 1);
1530 mark->value = value;
1531 mark->markup = g_strdup (markup);
1532 mark->position = position;
1534 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1535 (GCompareFunc) compare_marks);
1537 n = g_slist_length (priv->marks);
1538 values = g_new (gdouble, n);
1539 for (m = priv->marks, i = 0; m; m = m->next, i++)
1542 values[i] = mark->value;
1545 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1549 gtk_widget_queue_resize (GTK_WIDGET (scale));
1552 static GtkBuildableIface *parent_buildable_iface;
1555 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1557 parent_buildable_iface = g_type_interface_peek_parent (iface);
1558 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1559 iface->custom_finished = gtk_scale_buildable_custom_finished;
1565 GtkBuilder *builder;
1567 } MarksSubparserData;
1572 GtkPositionType position;
1575 gboolean translatable;
1579 mark_data_free (MarkData *data)
1581 g_string_free (data->markup, TRUE);
1582 g_free (data->context);
1583 g_slice_free (MarkData, data);
1587 marks_start_element (GMarkupParseContext *context,
1588 const gchar *element_name,
1589 const gchar **names,
1590 const gchar **values,
1594 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1596 gint line_number, char_number;
1598 if (strcmp (element_name, "marks") == 0)
1600 else if (strcmp (element_name, "mark") == 0)
1603 gboolean has_value = FALSE;
1604 GtkPositionType position = GTK_POS_BOTTOM;
1605 const gchar *msg_context = NULL;
1606 gboolean translatable = FALSE;
1609 for (i = 0; names[i]; i++)
1611 if (strcmp (names[i], "translatable") == 0)
1613 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1616 else if (strcmp (names[i], "comments") == 0)
1618 /* do nothing, comments are for translators */
1620 else if (strcmp (names[i], "context") == 0)
1621 msg_context = values[i];
1622 else if (strcmp (names[i], "value") == 0)
1624 GValue gvalue = { 0, };
1626 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1629 value = g_value_get_double (&gvalue);
1632 else if (strcmp (names[i], "position") == 0)
1634 GValue gvalue = { 0, };
1636 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1639 position = g_value_get_enum (&gvalue);
1643 g_markup_parse_context_get_position (context,
1648 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1649 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1651 line_number, char_number, names[i], "mark");
1658 g_markup_parse_context_get_position (context,
1663 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1664 "%s:%d:%d <%s> requires attribute \"%s\"",
1666 line_number, char_number, "mark",
1671 mark = g_slice_new (MarkData);
1672 mark->value = value;
1673 mark->position = position;
1674 mark->markup = g_string_new ("");
1675 mark->context = g_strdup (msg_context);
1676 mark->translatable = translatable;
1678 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1682 g_markup_parse_context_get_position (context,
1687 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1688 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1690 line_number, char_number, element_name);
1696 marks_text (GMarkupParseContext *context,
1702 MarksSubparserData *data = (MarksSubparserData*)user_data;
1704 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1706 MarkData *mark = data->marks->data;
1708 g_string_append_len (mark->markup, text, text_len);
1712 static const GMarkupParser marks_parser =
1714 marks_start_element,
1721 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1722 GtkBuilder *builder,
1724 const gchar *tagname,
1725 GMarkupParser *parser,
1728 MarksSubparserData *parser_data;
1733 if (strcmp (tagname, "marks") == 0)
1735 parser_data = g_slice_new0 (MarksSubparserData);
1736 parser_data->scale = GTK_SCALE (buildable);
1737 parser_data->marks = NULL;
1739 *parser = marks_parser;
1740 *data = parser_data;
1744 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1745 tagname, parser, data);
1749 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1750 GtkBuilder *builder,
1752 const gchar *tagname,
1755 GtkScale *scale = GTK_SCALE (buildable);
1756 MarksSubparserData *marks_data;
1758 if (strcmp (tagname, "marks") == 0)
1763 marks_data = (MarksSubparserData *)user_data;
1765 for (m = marks_data->marks; m; m = m->next)
1767 MarkData *mdata = m->data;
1769 if (mdata->translatable && mdata->markup->len)
1770 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1772 mdata->markup->str);
1774 markup = mdata->markup->str;
1776 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1778 mark_data_free (mdata);
1781 g_slist_free (marks_data->marks);
1782 g_slice_free (MarksSubparserData, marks_data);