1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 2001 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
33 #include "gtkscaleprivate.h"
34 #include "gtkiconfactory.h"
35 #include "gtkicontheme.h"
36 #include "gtkmarshalers.h"
37 #include "gtkbindings.h"
38 #include "gtkorientable.h"
39 #include "gtktypebuiltins.h"
40 #include "gtkprivate.h"
42 #include "gtkbuildable.h"
43 #include "gtkbuilderprivate.h"
44 #include "gtkmainprivate.h"
46 #include "a11y/gtkscaleaccessible.h"
51 * @Short_description: A slider widget for selecting a value from a range
54 * A GtkScale is a slider control used to select a numeric value.
55 * To use it, you'll probably want to investigate the methods on
56 * its base class, #GtkRange, in addition to the methods for GtkScale itself.
57 * To set the value of a scale, you would normally use gtk_range_set_value().
58 * To detect changes to the value, you would normally use the
59 * #GtkRange::value-changed signal.
61 * Note that using the same upper and lower bounds for the #GtkScale (through
62 * the #GtkRange methods) will hide the slider itself. This is useful for
63 * applications that want to show an undeterminate value on the scale, without
64 * changing the layout of the application (such as movie or music players).
66 * <refsect2 id="GtkScale-BUILDER-UI"><title>GtkScale as GtkBuildable</title>
67 * GtkScale supports a custom <marks> element, which
68 * can contain multiple <mark> elements. The "value" and "position"
69 * attributes have the same meaning as gtk_scale_add_mark() parameters of the
70 * same name. If the element is not empty, its content is taken as the markup
71 * to show at the mark. It can be translated with the usual "translatable and
72 * "context" attributes.
77 #define MAX_DIGITS (64) /* don't change this,
78 * a) you don't need to and
79 * b) you might cause buffer owerflows in
80 * unrelated code portions otherwise
83 typedef struct _GtkScaleMark GtkScaleMark;
85 struct _GtkScalePrivate
101 GtkPositionType position; /* always GTK_POS_TOP or GTK_POS_BOTTOM */
116 static guint signals[LAST_SIGNAL];
118 static void gtk_scale_set_property (GObject *object,
122 static void gtk_scale_get_property (GObject *object,
126 static void gtk_scale_get_preferred_width (GtkWidget *widget,
129 static void gtk_scale_get_preferred_height (GtkWidget *widget,
132 static void gtk_scale_style_updated (GtkWidget *widget);
133 static void gtk_scale_get_range_border (GtkRange *range,
135 static void gtk_scale_get_mark_label_size (GtkScale *scale,
136 GtkPositionType position,
143 static void gtk_scale_finalize (GObject *object);
144 static void gtk_scale_screen_changed (GtkWidget *widget,
145 GdkScreen *old_screen);
146 static gboolean gtk_scale_draw (GtkWidget *widget,
148 static void gtk_scale_real_get_layout_offsets (GtkScale *scale,
151 static void gtk_scale_buildable_interface_init (GtkBuildableIface *iface);
152 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
155 const gchar *tagname,
156 GMarkupParser *parser,
158 static void gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
161 const gchar *tagname,
165 G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
166 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
167 gtk_scale_buildable_interface_init))
170 #define add_slider_binding(binding_set, keyval, mask, scroll) \
171 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
172 I_("move-slider"), 1, \
173 GTK_TYPE_SCROLL_TYPE, scroll)
176 gtk_scale_class_init (GtkScaleClass *class)
178 GObjectClass *gobject_class;
179 GtkWidgetClass *widget_class;
180 GtkRangeClass *range_class;
181 GtkBindingSet *binding_set;
183 gobject_class = G_OBJECT_CLASS (class);
184 range_class = (GtkRangeClass*) class;
185 widget_class = (GtkWidgetClass*) class;
187 gobject_class->set_property = gtk_scale_set_property;
188 gobject_class->get_property = gtk_scale_get_property;
189 gobject_class->finalize = gtk_scale_finalize;
191 widget_class->style_updated = gtk_scale_style_updated;
192 widget_class->screen_changed = gtk_scale_screen_changed;
193 widget_class->draw = gtk_scale_draw;
194 widget_class->get_preferred_width = gtk_scale_get_preferred_width;
195 widget_class->get_preferred_height = gtk_scale_get_preferred_height;
197 range_class->slider_detail = "Xscale";
198 range_class->get_range_border = gtk_scale_get_range_border;
200 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
203 * GtkScale::format-value:
204 * @scale: the object which received the signal
205 * @value: the value to format
207 * Signal which allows you to change how the scale value is displayed.
208 * Connect a signal handler which returns an allocated string representing
209 * @value. That string will then be used to display the scale's value.
211 * Here's an example signal handler which displays a value 1.0 as
212 * with "-->1.0<--".
215 * format_value_callback (GtkScale *scale,
218 * return g_strdup_printf ("-->%0.*g<--",
219 * gtk_scale_get_digits (scale), value);
223 * Return value: allocated string representing @value
225 signals[FORMAT_VALUE] =
226 g_signal_new (I_("format-value"),
227 G_TYPE_FROM_CLASS (gobject_class),
229 G_STRUCT_OFFSET (GtkScaleClass, format_value),
230 _gtk_single_string_accumulator, NULL,
231 _gtk_marshal_STRING__DOUBLE,
235 g_object_class_install_property (gobject_class,
237 g_param_spec_int ("digits",
239 P_("The number of decimal places that are displayed in the value"),
243 GTK_PARAM_READWRITE));
245 g_object_class_install_property (gobject_class,
247 g_param_spec_boolean ("draw-value",
249 P_("Whether the current value is displayed as a string next to the slider"),
251 GTK_PARAM_READWRITE));
253 g_object_class_install_property (gobject_class,
255 g_param_spec_enum ("value-pos",
256 P_("Value Position"),
257 P_("The position in which the current value is displayed"),
258 GTK_TYPE_POSITION_TYPE,
260 GTK_PARAM_READWRITE));
262 gtk_widget_class_install_style_property (widget_class,
263 g_param_spec_int ("slider-length",
265 P_("Length of scale's slider"),
269 GTK_PARAM_READABLE));
271 gtk_widget_class_install_style_property (widget_class,
272 g_param_spec_int ("value-spacing",
274 P_("Space between value text and the slider/trough area"),
278 GTK_PARAM_READABLE));
280 /* All bindings (even arrow keys) are on both h/v scale, because
281 * blind users etc. don't care about scale orientation.
284 binding_set = gtk_binding_set_by_class (class);
286 add_slider_binding (binding_set, GDK_KEY_Left, 0,
287 GTK_SCROLL_STEP_LEFT);
289 add_slider_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
290 GTK_SCROLL_PAGE_LEFT);
292 add_slider_binding (binding_set, GDK_KEY_KP_Left, 0,
293 GTK_SCROLL_STEP_LEFT);
295 add_slider_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
296 GTK_SCROLL_PAGE_LEFT);
298 add_slider_binding (binding_set, GDK_KEY_Right, 0,
299 GTK_SCROLL_STEP_RIGHT);
301 add_slider_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
302 GTK_SCROLL_PAGE_RIGHT);
304 add_slider_binding (binding_set, GDK_KEY_KP_Right, 0,
305 GTK_SCROLL_STEP_RIGHT);
307 add_slider_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
308 GTK_SCROLL_PAGE_RIGHT);
310 add_slider_binding (binding_set, GDK_KEY_Up, 0,
313 add_slider_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
316 add_slider_binding (binding_set, GDK_KEY_KP_Up, 0,
319 add_slider_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
322 add_slider_binding (binding_set, GDK_KEY_Down, 0,
323 GTK_SCROLL_STEP_DOWN);
325 add_slider_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
326 GTK_SCROLL_PAGE_DOWN);
328 add_slider_binding (binding_set, GDK_KEY_KP_Down, 0,
329 GTK_SCROLL_STEP_DOWN);
331 add_slider_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
332 GTK_SCROLL_PAGE_DOWN);
334 add_slider_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
335 GTK_SCROLL_PAGE_LEFT);
337 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
338 GTK_SCROLL_PAGE_LEFT);
340 add_slider_binding (binding_set, GDK_KEY_Page_Up, 0,
343 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
346 add_slider_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
347 GTK_SCROLL_PAGE_RIGHT);
349 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
350 GTK_SCROLL_PAGE_RIGHT);
352 add_slider_binding (binding_set, GDK_KEY_Page_Down, 0,
353 GTK_SCROLL_PAGE_DOWN);
355 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
356 GTK_SCROLL_PAGE_DOWN);
358 /* Logical bindings (vs. visual bindings above) */
360 add_slider_binding (binding_set, GDK_KEY_plus, 0,
361 GTK_SCROLL_STEP_FORWARD);
363 add_slider_binding (binding_set, GDK_KEY_minus, 0,
364 GTK_SCROLL_STEP_BACKWARD);
366 add_slider_binding (binding_set, GDK_KEY_plus, GDK_CONTROL_MASK,
367 GTK_SCROLL_PAGE_FORWARD);
369 add_slider_binding (binding_set, GDK_KEY_minus, GDK_CONTROL_MASK,
370 GTK_SCROLL_PAGE_BACKWARD);
373 add_slider_binding (binding_set, GDK_KEY_KP_Add, 0,
374 GTK_SCROLL_STEP_FORWARD);
376 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, 0,
377 GTK_SCROLL_STEP_BACKWARD);
379 add_slider_binding (binding_set, GDK_KEY_KP_Add, GDK_CONTROL_MASK,
380 GTK_SCROLL_PAGE_FORWARD);
382 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
383 GTK_SCROLL_PAGE_BACKWARD);
386 add_slider_binding (binding_set, GDK_KEY_Home, 0,
389 add_slider_binding (binding_set, GDK_KEY_KP_Home, 0,
392 add_slider_binding (binding_set, GDK_KEY_End, 0,
395 add_slider_binding (binding_set, GDK_KEY_KP_End, 0,
398 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
400 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCALE_ACCESSIBLE);
404 gtk_scale_orientation_notify (GtkRange *range,
405 const GParamSpec *pspec)
407 GtkOrientation orientation;
409 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
410 gtk_range_set_flippable (range,
411 orientation == GTK_ORIENTATION_HORIZONTAL);
415 gtk_scale_init (GtkScale *scale)
417 GtkScalePrivate *priv;
418 GtkRange *range = GTK_RANGE (scale);
419 GtkStyleContext *context;
421 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
426 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
428 gtk_range_set_slider_size_fixed (range, TRUE);
430 priv->draw_value = TRUE;
431 priv->value_pos = GTK_POS_TOP;
433 gtk_range_set_round_digits (range, priv->digits);
435 gtk_scale_orientation_notify (range, NULL);
436 g_signal_connect (scale, "notify::orientation",
437 G_CALLBACK (gtk_scale_orientation_notify),
440 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
441 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE);
445 gtk_scale_set_property (GObject *object,
452 scale = GTK_SCALE (object);
457 gtk_scale_set_digits (scale, g_value_get_int (value));
459 case PROP_DRAW_VALUE:
460 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
463 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
466 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
472 gtk_scale_get_property (GObject *object,
477 GtkScale *scale = GTK_SCALE (object);
478 GtkScalePrivate *priv = scale->priv;
483 g_value_set_int (value, priv->digits);
485 case PROP_DRAW_VALUE:
486 g_value_set_boolean (value, priv->draw_value);
489 g_value_set_enum (value, priv->value_pos);
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
499 * @orientation: the scale's orientation.
500 * @adjustment: (allow-none): the #GtkAdjustment which sets the range
501 * of the scale, or %NULL to create a new adjustment.
503 * Creates a new #GtkScale.
505 * Return value: a new #GtkScale
510 gtk_scale_new (GtkOrientation orientation,
511 GtkAdjustment *adjustment)
513 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
516 return g_object_new (GTK_TYPE_SCALE,
517 "orientation", orientation,
518 "adjustment", adjustment,
523 * gtk_scale_new_with_range:
524 * @orientation: the scale's orientation.
525 * @min: minimum value
526 * @max: maximum value
527 * @step: step increment (tick size) used with keyboard shortcuts
529 * Creates a new scale widget with the given orientation that lets the
530 * user input a number between @min and @max (including @min and @max)
531 * with the increment @step. @step must be nonzero; it's the distance
532 * the slider moves when using the arrow keys to adjust the scale
535 * Note that the way in which the precision is derived works best if @step
536 * is a power of ten. If the resulting precision is not suitable for your
537 * needs, use gtk_scale_set_digits() to correct it.
539 * Return value: a new #GtkScale
544 gtk_scale_new_with_range (GtkOrientation orientation,
552 g_return_val_if_fail (min < max, NULL);
553 g_return_val_if_fail (step != 0.0, NULL);
555 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
557 if (fabs (step) >= 1.0 || step == 0.0)
563 digits = abs ((gint) floor (log10 (fabs (step))));
568 return g_object_new (GTK_TYPE_SCALE,
569 "orientation", orientation,
576 * gtk_scale_set_digits:
577 * @scale: a #GtkScale
578 * @digits: the number of decimal places to display,
579 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
581 * Sets the number of decimal places that are displayed in the value.
582 * Also causes the value of the adjustment to be rounded off to this
583 * number of digits, so the retrieved value matches the value the user saw.
586 gtk_scale_set_digits (GtkScale *scale,
589 GtkScalePrivate *priv;
592 g_return_if_fail (GTK_IS_SCALE (scale));
595 range = GTK_RANGE (scale);
597 digits = CLAMP (digits, -1, MAX_DIGITS);
599 if (priv->digits != digits)
601 priv->digits = digits;
602 if (priv->draw_value)
603 gtk_range_set_round_digits (range, digits);
605 _gtk_scale_clear_layout (scale);
606 gtk_widget_queue_resize (GTK_WIDGET (scale));
608 g_object_notify (G_OBJECT (scale), "digits");
613 * gtk_scale_get_digits:
614 * @scale: a #GtkScale
616 * Gets the number of decimal places that are displayed in the value.
618 * Returns: the number of decimal places that are displayed
621 gtk_scale_get_digits (GtkScale *scale)
623 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
625 return scale->priv->digits;
629 * gtk_scale_set_draw_value:
630 * @scale: a #GtkScale
631 * @draw_value: %TRUE to draw the value
633 * Specifies whether the current value is displayed as a string next
637 gtk_scale_set_draw_value (GtkScale *scale,
640 GtkScalePrivate *priv;
642 g_return_if_fail (GTK_IS_SCALE (scale));
646 draw_value = draw_value != FALSE;
648 if (priv->draw_value != draw_value)
650 priv->draw_value = draw_value;
652 gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
654 gtk_range_set_round_digits (GTK_RANGE (scale), -1);
656 _gtk_scale_clear_layout (scale);
658 gtk_widget_queue_resize (GTK_WIDGET (scale));
660 g_object_notify (G_OBJECT (scale), "draw-value");
665 * gtk_scale_get_draw_value:
666 * @scale: a #GtkScale
668 * Returns whether the current value is displayed as a string
669 * next to the slider.
671 * Returns: whether the current value is displayed as a string
674 gtk_scale_get_draw_value (GtkScale *scale)
676 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
678 return scale->priv->draw_value;
682 * gtk_scale_set_value_pos:
683 * @scale: a #GtkScale
684 * @pos: the position in which the current value is displayed
686 * Sets the position in which the current value is displayed.
689 gtk_scale_set_value_pos (GtkScale *scale,
692 GtkScalePrivate *priv;
695 g_return_if_fail (GTK_IS_SCALE (scale));
699 if (priv->value_pos != pos)
701 priv->value_pos = pos;
702 widget = GTK_WIDGET (scale);
704 _gtk_scale_clear_layout (scale);
705 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
706 gtk_widget_queue_resize (widget);
708 g_object_notify (G_OBJECT (scale), "value-pos");
713 * gtk_scale_get_value_pos:
714 * @scale: a #GtkScale
716 * Gets the position in which the current value is displayed.
718 * Returns: the position in which the current value is displayed
721 gtk_scale_get_value_pos (GtkScale *scale)
723 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
725 return scale->priv->value_pos;
729 gtk_scale_get_range_border (GtkRange *range,
732 GtkScalePrivate *priv;
737 widget = GTK_WIDGET (range);
738 scale = GTK_SCALE (range);
741 _gtk_scale_get_value_size (scale, &w, &h);
748 if (priv->draw_value)
751 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
753 switch (priv->value_pos)
756 border->left += w + value_spacing;
759 border->right += w + value_spacing;
762 border->top += h + value_spacing;
765 border->bottom += h + value_spacing;
774 gint n1, w1, h1, n2, w2, h2;
776 gtk_widget_style_get (widget,
777 "slider-width", &slider_width,
778 "value-spacing", &value_spacing,
782 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
784 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
787 border->top += h1 + value_spacing + slider_width / 2;
789 border->bottom += h2 + value_spacing + slider_width / 2;
794 border->left += w1 + value_spacing + slider_width / 2;
796 border->right += w2 + value_spacing + slider_width / 2;
801 /* FIXME this could actually be static at the moment. */
803 _gtk_scale_get_value_size (GtkScale *scale,
807 GtkScalePrivate *priv = scale->priv;
810 if (priv->draw_value)
812 GtkAdjustment *adjustment;
814 PangoRectangle logical_rect;
817 range = GTK_RANGE (scale);
819 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
820 adjustment = gtk_range_get_adjustment (range);
822 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
823 pango_layout_set_text (layout, txt, -1);
826 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
829 *width = logical_rect.width;
831 *height = logical_rect.height;
833 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
834 pango_layout_set_text (layout, txt, -1);
837 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
840 *width = MAX (*width, logical_rect.width);
842 *height = MAX (*height, logical_rect.height);
844 g_object_unref (layout);
857 gtk_scale_get_mark_label_size (GtkScale *scale,
858 GtkPositionType position,
866 GtkScalePrivate *priv = scale->priv;
868 PangoRectangle logical_rect;
872 *count1 = *count2 = 0;
873 *width1 = *width2 = 0;
874 *height1 = *height2 = 0;
876 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
878 for (m = priv->marks; m; m = m->next)
880 GtkScaleMark *mark = m->data;
882 if (mark->markup && *mark->markup)
884 pango_layout_set_markup (layout, mark->markup, -1);
885 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
887 w = logical_rect.width;
888 h = logical_rect.height;
896 if (mark->position == position)
899 *width1 = MAX (*width1, w);
900 *height1 = MAX (*height1, h);
905 *width2 = MAX (*width2, w);
906 *height2 = MAX (*height2, h);
910 g_object_unref (layout);
914 gtk_scale_style_updated (GtkWidget *widget)
919 range = GTK_RANGE (widget);
921 gtk_widget_style_get (widget,
922 "slider-length", &slider_length,
925 gtk_range_set_min_slider_size (range, slider_length);
927 _gtk_scale_clear_layout (GTK_SCALE (widget));
929 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_updated (widget);
933 gtk_scale_screen_changed (GtkWidget *widget,
934 GdkScreen *old_screen)
936 _gtk_scale_clear_layout (GTK_SCALE (widget));
940 gtk_scale_get_preferred_width (GtkWidget *widget,
944 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural);
946 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
948 gint n1, w1, h1, n2, w2, h2;
952 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
954 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
956 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
957 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
960 *minimum = MAX (*minimum, w);
961 *natural = MAX (*natural, w);
966 gtk_scale_get_preferred_height (GtkWidget *widget,
970 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural);
973 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
975 gint n1, w1, h1, n2, w2, h2;
979 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
981 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
982 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
983 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
986 *minimum = MAX (*minimum, h);
987 *natural = MAX (*natural, h);
992 find_next_pos (GtkWidget *widget,
998 GtkAllocation allocation;
1002 for (m = list->next, i = 1; m; m = m->next, i++)
1004 GtkScaleMark *mark = m->data;
1006 if (match == (mark->position == pos))
1010 gtk_widget_get_allocation (widget, &allocation);
1011 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
1012 return allocation.width;
1014 return allocation.height;
1018 gtk_scale_draw (GtkWidget *widget,
1021 GtkScale *scale = GTK_SCALE (widget);
1022 GtkScalePrivate *priv = scale->priv;
1023 GtkRange *range = GTK_RANGE (scale);
1024 GtkStateFlags state = 0;
1025 GtkStyleContext *context;
1032 context = gtk_widget_get_style_context (widget);
1033 gtk_widget_style_get (widget,
1034 "focus-padding", &focus_padding,
1035 "slider-width", &slider_width,
1036 "value-spacing", &value_spacing,
1039 /* We need to chain up _first_ so the various geometry members of
1040 * GtkRange struct are updated.
1042 GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1044 if (!gtk_widget_is_sensitive (widget))
1045 state |= GTK_STATE_FLAG_INSENSITIVE;
1049 GtkOrientation orientation;
1050 GdkRectangle range_rect;
1052 gint x1, x2, x3, y1, y2, y3;
1053 PangoLayout *layout;
1054 PangoRectangle logical_rect;
1056 gint min_pos_before, min_pos_after;
1057 gint min_pos, max_pos;
1059 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1060 _gtk_range_get_stop_positions (range, &marks);
1061 layout = gtk_widget_create_pango_layout (widget, NULL);
1062 gtk_range_get_range_rect (range, &range_rect);
1064 min_pos_before = min_pos_after = 0;
1066 for (m = priv->marks, i = 0; m; m = m->next, i++)
1068 GtkScaleMark *mark = m->data;
1070 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1073 if (mark->position == GTK_POS_TOP)
1076 y2 = y1 - slider_width / 2;
1077 min_pos = min_pos_before;
1078 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1082 y1 = range_rect.y + range_rect.height;
1083 y2 = y1 + slider_width / 2;
1084 min_pos = min_pos_after;
1085 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM, 1) - min_sep;
1088 gtk_style_context_save (context);
1089 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1090 gtk_style_context_set_state (context, state);
1092 gtk_render_line (context, cr, x1, y1, x1, y2);
1096 pango_layout_set_markup (layout, mark->markup, -1);
1097 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1099 x3 = x1 - logical_rect.width / 2;
1102 if (x3 + logical_rect.width > max_pos)
1103 x3 = max_pos - logical_rect.width;
1106 if (mark->position == GTK_POS_TOP)
1108 y3 = y2 - value_spacing - logical_rect.height;
1109 min_pos_before = x3 + logical_rect.width + min_sep;
1113 y3 = y2 + value_spacing;
1114 min_pos_after = x3 + logical_rect.width + min_sep;
1117 gtk_render_layout (context, cr, x3, y3, layout);
1120 gtk_style_context_restore (context);
1124 if (mark->position == GTK_POS_TOP)
1127 x2 = range_rect.x - slider_width / 2;
1128 min_pos = min_pos_before;
1129 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1133 x1 = range_rect.x + range_rect.width;
1134 x2 = range_rect.x + range_rect.width + slider_width / 2;
1135 min_pos = min_pos_after;
1136 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM, 1) - min_sep;
1140 gtk_style_context_save (context);
1141 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1142 gtk_style_context_set_state (context, state);
1144 gtk_render_line (context, cr, x1, y1, x2, y1);
1148 pango_layout_set_markup (layout, mark->markup, -1);
1149 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1151 y3 = y1 - logical_rect.height / 2;
1154 if (y3 + logical_rect.height > max_pos)
1155 y3 = max_pos - logical_rect.height;
1158 if (mark->position == GTK_POS_TOP)
1160 x3 = x2 - value_spacing - logical_rect.width;
1161 min_pos_before = y3 + logical_rect.height + min_sep;
1165 x3 = x2 + value_spacing;
1166 min_pos_after = y3 + logical_rect.height + min_sep;
1169 gtk_render_layout (context, cr, x3, y3, layout);
1172 gtk_style_context_restore (context);
1176 g_object_unref (layout);
1180 if (priv->draw_value)
1182 GtkAllocation allocation;
1184 PangoLayout *layout;
1187 layout = gtk_scale_get_layout (scale);
1188 gtk_scale_get_layout_offsets (scale, &x, &y);
1189 gtk_widget_get_allocation (widget, &allocation);
1191 gtk_render_layout (context, cr,
1201 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1205 GtkScalePrivate *priv = scale->priv;
1206 GtkAllocation allocation;
1207 GtkWidget *widget = GTK_WIDGET (scale);
1208 GtkRange *range = GTK_RANGE (widget);
1209 GdkRectangle range_rect;
1210 PangoLayout *layout = gtk_scale_get_layout (scale);
1211 PangoRectangle logical_rect;
1212 gint slider_start, slider_end;
1223 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1225 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1227 gtk_widget_get_allocation (widget, &allocation);
1228 gtk_range_get_range_rect (range, &range_rect);
1229 gtk_range_get_slider_range (range,
1233 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1235 switch (priv->value_pos)
1238 *x = range_rect.x - value_spacing - logical_rect.width;
1239 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1243 *x = range_rect.x + range_rect.width + value_spacing;
1244 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1248 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1249 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1250 *y = range_rect.y - logical_rect.height - value_spacing;
1253 case GTK_POS_BOTTOM:
1254 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1255 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1256 *y = range_rect.y + range_rect.height + value_spacing;
1260 g_return_if_reached ();
1266 switch (priv->value_pos)
1269 *x = range_rect.x - logical_rect.width - value_spacing;
1270 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1271 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1275 *x = range_rect.x + range_rect.width + value_spacing;
1276 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1277 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1281 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1282 *y = range_rect.y - logical_rect.height - value_spacing;
1285 case GTK_POS_BOTTOM:
1286 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1287 *y = range_rect.y + range_rect.height + value_spacing;
1291 g_return_if_reached ();
1300 * _gtk_scale_format_value:
1301 * @scale: a #GtkScale
1302 * @value: adjustment value
1304 * Emits #GtkScale::format-value signal to format the value,
1305 * if no user signal handlers, falls back to a default format.
1307 * Return value: formatted value
1310 _gtk_scale_format_value (GtkScale *scale,
1313 GtkScalePrivate *priv = scale->priv;
1316 g_signal_emit (scale,
1317 signals[FORMAT_VALUE],
1325 return g_strdup_printf ("%0.*f", priv->digits, value);
1329 gtk_scale_finalize (GObject *object)
1331 GtkScale *scale = GTK_SCALE (object);
1333 _gtk_scale_clear_layout (scale);
1334 gtk_scale_clear_marks (scale);
1336 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1340 * gtk_scale_get_layout:
1341 * @scale: A #GtkScale
1343 * Gets the #PangoLayout used to display the scale. The returned
1344 * object is owned by the scale so does not need to be freed by
1347 * Return value: (transfer none): the #PangoLayout for this scale,
1348 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1353 gtk_scale_get_layout (GtkScale *scale)
1355 GtkScalePrivate *priv;
1358 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1364 if (priv->draw_value)
1365 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1368 if (priv->draw_value)
1370 txt = _gtk_scale_format_value (scale,
1371 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1372 pango_layout_set_text (priv->layout, txt, -1);
1376 return priv->layout;
1380 * gtk_scale_get_layout_offsets:
1381 * @scale: a #GtkScale
1382 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
1383 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
1385 * Obtains the coordinates where the scale will draw the
1386 * #PangoLayout representing the text in the scale. Remember
1387 * when using the #PangoLayout function you need to convert to
1388 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1390 * If the #GtkScale:draw-value property is %FALSE, the return
1391 * values are undefined.
1396 gtk_scale_get_layout_offsets (GtkScale *scale,
1403 g_return_if_fail (GTK_IS_SCALE (scale));
1405 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1406 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1416 _gtk_scale_clear_layout (GtkScale *scale)
1418 GtkScalePrivate *priv = scale->priv;
1420 g_return_if_fail (GTK_IS_SCALE (scale));
1424 g_object_unref (priv->layout);
1425 priv->layout = NULL;
1430 gtk_scale_mark_free (GtkScaleMark *mark)
1432 g_free (mark->markup);
1437 * gtk_scale_clear_marks:
1438 * @scale: a #GtkScale
1440 * Removes any marks that have been added with gtk_scale_add_mark().
1445 gtk_scale_clear_marks (GtkScale *scale)
1447 GtkScalePrivate *priv;
1448 GtkStyleContext *context;
1450 g_return_if_fail (GTK_IS_SCALE (scale));
1454 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1455 g_slist_free (priv->marks);
1458 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1459 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1460 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1462 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1464 gtk_widget_queue_resize (GTK_WIDGET (scale));
1468 compare_marks (gpointer a, gpointer b)
1470 GtkScaleMark *ma, *mb;
1474 return (gint) (ma->value - mb->value);
1478 * gtk_scale_add_mark:
1479 * @scale: a #GtkScale
1480 * @value: the value at which the mark is placed, must be between
1481 * the lower and upper limits of the scales' adjustment
1482 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1483 * and %GTK_POS_LEFT are drawn above the scale, anything else below.
1484 * For a vertical scale, #GTK_POS_LEFT and %GTK_POS_TOP are drawn to
1485 * the left of the scale, anything else to the right.
1486 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1489 * Adds a mark at @value.
1491 * A mark is indicated visually by drawing a tick mark next to the scale,
1492 * and GTK+ makes it easy for the user to position the scale exactly at the
1495 * If @markup is not %NULL, text is shown next to the tick mark.
1497 * To remove marks from a scale, use gtk_scale_clear_marks().
1502 gtk_scale_add_mark (GtkScale *scale,
1504 GtkPositionType position,
1505 const gchar *markup)
1507 GtkScalePrivate *priv;
1512 GtkStyleContext *context;
1515 g_return_if_fail (GTK_IS_SCALE (scale));
1519 mark = g_new (GtkScaleMark, 1);
1520 mark->value = value;
1521 mark->markup = g_strdup (markup);
1522 if (position == GTK_POS_LEFT ||
1523 position == GTK_POS_TOP)
1524 mark->position = GTK_POS_TOP;
1526 mark->position = GTK_POS_BOTTOM;
1528 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1529 (GCompareFunc) compare_marks);
1531 #define MARKS_ABOVE 1
1532 #define MARKS_BELOW 2
1535 n = g_slist_length (priv->marks);
1536 values = g_new (gdouble, n);
1537 for (m = priv->marks, i = 0; m; m = m->next, i++)
1540 values[i] = mark->value;
1541 if (mark->position == GTK_POS_TOP)
1542 all_pos |= MARKS_ABOVE;
1544 all_pos |= MARKS_BELOW;
1547 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1551 /* Set the style classes for the slider, so it could
1552 * point to the right direction when marks are present
1554 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1556 if (all_pos & MARKS_ABOVE)
1557 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1559 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1560 if (all_pos & MARKS_BELOW)
1561 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1563 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1565 gtk_widget_queue_resize (GTK_WIDGET (scale));
1568 static GtkBuildableIface *parent_buildable_iface;
1571 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1573 parent_buildable_iface = g_type_interface_peek_parent (iface);
1574 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1575 iface->custom_finished = gtk_scale_buildable_custom_finished;
1581 GtkBuilder *builder;
1583 } MarksSubparserData;
1588 GtkPositionType position;
1591 gboolean translatable;
1595 mark_data_free (MarkData *data)
1597 g_string_free (data->markup, TRUE);
1598 g_free (data->context);
1599 g_slice_free (MarkData, data);
1603 marks_start_element (GMarkupParseContext *context,
1604 const gchar *element_name,
1605 const gchar **names,
1606 const gchar **values,
1610 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1612 gint line_number, char_number;
1614 if (strcmp (element_name, "marks") == 0)
1616 else if (strcmp (element_name, "mark") == 0)
1619 gboolean has_value = FALSE;
1620 GtkPositionType position = GTK_POS_BOTTOM;
1621 const gchar *msg_context = NULL;
1622 gboolean translatable = FALSE;
1625 for (i = 0; names[i]; i++)
1627 if (strcmp (names[i], "translatable") == 0)
1629 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1632 else if (strcmp (names[i], "comments") == 0)
1634 /* do nothing, comments are for translators */
1636 else if (strcmp (names[i], "context") == 0)
1637 msg_context = values[i];
1638 else if (strcmp (names[i], "value") == 0)
1640 GValue gvalue = { 0, };
1642 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1645 value = g_value_get_double (&gvalue);
1648 else if (strcmp (names[i], "position") == 0)
1650 GValue gvalue = { 0, };
1652 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1655 position = g_value_get_enum (&gvalue);
1659 g_markup_parse_context_get_position (context,
1664 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1665 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1667 line_number, char_number, names[i], "mark");
1674 g_markup_parse_context_get_position (context,
1679 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1680 "%s:%d:%d <%s> requires attribute \"%s\"",
1682 line_number, char_number, "mark",
1687 mark = g_slice_new (MarkData);
1688 mark->value = value;
1689 if (position == GTK_POS_LEFT ||
1690 position == GTK_POS_TOP)
1691 mark->position = GTK_POS_TOP;
1693 mark->position = GTK_POS_BOTTOM;
1694 mark->markup = g_string_new ("");
1695 mark->context = g_strdup (msg_context);
1696 mark->translatable = translatable;
1698 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1702 g_markup_parse_context_get_position (context,
1707 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1708 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1710 line_number, char_number, element_name);
1716 marks_text (GMarkupParseContext *context,
1722 MarksSubparserData *data = (MarksSubparserData*)user_data;
1724 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1726 MarkData *mark = data->marks->data;
1728 g_string_append_len (mark->markup, text, text_len);
1732 static const GMarkupParser marks_parser =
1734 marks_start_element,
1741 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1742 GtkBuilder *builder,
1744 const gchar *tagname,
1745 GMarkupParser *parser,
1748 MarksSubparserData *parser_data;
1753 if (strcmp (tagname, "marks") == 0)
1755 parser_data = g_slice_new0 (MarksSubparserData);
1756 parser_data->scale = GTK_SCALE (buildable);
1757 parser_data->marks = NULL;
1759 *parser = marks_parser;
1760 *data = parser_data;
1764 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1765 tagname, parser, data);
1769 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1770 GtkBuilder *builder,
1772 const gchar *tagname,
1775 GtkScale *scale = GTK_SCALE (buildable);
1776 MarksSubparserData *marks_data;
1778 if (strcmp (tagname, "marks") == 0)
1783 marks_data = (MarksSubparserData *)user_data;
1785 for (m = marks_data->marks; m; m = m->next)
1787 MarkData *mdata = m->data;
1789 if (mdata->translatable && mdata->markup->len)
1790 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1792 mdata->markup->str);
1794 markup = mdata->markup->str;
1796 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1798 mark_data_free (mdata);
1801 g_slist_free (marks_data->marks);
1802 g_slice_free (MarksSubparserData, marks_data);