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"
48 * @Short_description: A slider widget for selecting a value from a range
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
80 typedef struct _GtkScaleMark GtkScaleMark;
82 struct _GtkScalePrivate
98 GtkPositionType position; /* always GTK_POS_TOP or GTK_POS_BOTTOM */
113 static guint signals[LAST_SIGNAL];
115 static void gtk_scale_set_property (GObject *object,
119 static void gtk_scale_get_property (GObject *object,
123 static void gtk_scale_get_preferred_width (GtkWidget *widget,
126 static void gtk_scale_get_preferred_height (GtkWidget *widget,
129 static void gtk_scale_style_updated (GtkWidget *widget);
130 static void gtk_scale_get_range_border (GtkRange *range,
132 static void gtk_scale_get_mark_label_size (GtkScale *scale,
133 GtkPositionType position,
140 static void gtk_scale_finalize (GObject *object);
141 static void gtk_scale_screen_changed (GtkWidget *widget,
142 GdkScreen *old_screen);
143 static gboolean gtk_scale_draw (GtkWidget *widget,
145 static void gtk_scale_real_get_layout_offsets (GtkScale *scale,
148 static void gtk_scale_buildable_interface_init (GtkBuildableIface *iface);
149 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
152 const gchar *tagname,
153 GMarkupParser *parser,
155 static void gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
158 const gchar *tagname,
162 G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
163 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
164 gtk_scale_buildable_interface_init))
168 single_string_accumulator (GSignalInvocationHint *ihint,
170 const GValue *handler_return,
173 gboolean continue_emission;
176 str = g_value_get_string (handler_return);
177 g_value_set_string (return_accu, str);
178 continue_emission = str == NULL;
180 return continue_emission;
184 #define add_slider_binding(binding_set, keyval, mask, scroll) \
185 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
186 I_("move-slider"), 1, \
187 GTK_TYPE_SCROLL_TYPE, scroll)
190 gtk_scale_class_init (GtkScaleClass *class)
192 GObjectClass *gobject_class;
193 GtkWidgetClass *widget_class;
194 GtkRangeClass *range_class;
195 GtkBindingSet *binding_set;
197 gobject_class = G_OBJECT_CLASS (class);
198 range_class = (GtkRangeClass*) class;
199 widget_class = (GtkWidgetClass*) class;
201 gobject_class->set_property = gtk_scale_set_property;
202 gobject_class->get_property = gtk_scale_get_property;
203 gobject_class->finalize = gtk_scale_finalize;
205 widget_class->style_updated = gtk_scale_style_updated;
206 widget_class->screen_changed = gtk_scale_screen_changed;
207 widget_class->draw = gtk_scale_draw;
208 widget_class->get_preferred_width = gtk_scale_get_preferred_width;
209 widget_class->get_preferred_height = gtk_scale_get_preferred_height;
211 range_class->slider_detail = "Xscale";
212 range_class->get_range_border = gtk_scale_get_range_border;
214 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
217 * GtkScale::format-value:
218 * @scale: the object which received the signal
219 * @value: the value to format
221 * Signal which allows you to change how the scale value is displayed.
222 * Connect a signal handler which returns an allocated string representing
223 * @value. That string will then be used to display the scale's value.
225 * Here's an example signal handler which displays a value 1.0 as
226 * with "-->1.0<--".
229 * format_value_callback (GtkScale *scale,
232 * return g_strdup_printf ("-->%0.*g<--",
233 * gtk_scale_get_digits (scale), value);
237 * Return value: allocated string representing @value
239 signals[FORMAT_VALUE] =
240 g_signal_new (I_("format-value"),
241 G_TYPE_FROM_CLASS (gobject_class),
243 G_STRUCT_OFFSET (GtkScaleClass, format_value),
244 single_string_accumulator, NULL,
245 _gtk_marshal_STRING__DOUBLE,
249 g_object_class_install_property (gobject_class,
251 g_param_spec_int ("digits",
253 P_("The number of decimal places that are displayed in the value"),
257 GTK_PARAM_READWRITE));
259 g_object_class_install_property (gobject_class,
261 g_param_spec_boolean ("draw-value",
263 P_("Whether the current value is displayed as a string next to the slider"),
265 GTK_PARAM_READWRITE));
267 g_object_class_install_property (gobject_class,
269 g_param_spec_enum ("value-pos",
270 P_("Value Position"),
271 P_("The position in which the current value is displayed"),
272 GTK_TYPE_POSITION_TYPE,
274 GTK_PARAM_READWRITE));
276 gtk_widget_class_install_style_property (widget_class,
277 g_param_spec_int ("slider-length",
279 P_("Length of scale's slider"),
283 GTK_PARAM_READABLE));
285 gtk_widget_class_install_style_property (widget_class,
286 g_param_spec_int ("value-spacing",
288 P_("Space between value text and the slider/trough area"),
292 GTK_PARAM_READABLE));
294 /* All bindings (even arrow keys) are on both h/v scale, because
295 * blind users etc. don't care about scale orientation.
298 binding_set = gtk_binding_set_by_class (class);
300 add_slider_binding (binding_set, GDK_KEY_Left, 0,
301 GTK_SCROLL_STEP_LEFT);
303 add_slider_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
304 GTK_SCROLL_PAGE_LEFT);
306 add_slider_binding (binding_set, GDK_KEY_KP_Left, 0,
307 GTK_SCROLL_STEP_LEFT);
309 add_slider_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
310 GTK_SCROLL_PAGE_LEFT);
312 add_slider_binding (binding_set, GDK_KEY_Right, 0,
313 GTK_SCROLL_STEP_RIGHT);
315 add_slider_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
316 GTK_SCROLL_PAGE_RIGHT);
318 add_slider_binding (binding_set, GDK_KEY_KP_Right, 0,
319 GTK_SCROLL_STEP_RIGHT);
321 add_slider_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
322 GTK_SCROLL_PAGE_RIGHT);
324 add_slider_binding (binding_set, GDK_KEY_Up, 0,
327 add_slider_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
330 add_slider_binding (binding_set, GDK_KEY_KP_Up, 0,
333 add_slider_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
336 add_slider_binding (binding_set, GDK_KEY_Down, 0,
337 GTK_SCROLL_STEP_DOWN);
339 add_slider_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
340 GTK_SCROLL_PAGE_DOWN);
342 add_slider_binding (binding_set, GDK_KEY_KP_Down, 0,
343 GTK_SCROLL_STEP_DOWN);
345 add_slider_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
346 GTK_SCROLL_PAGE_DOWN);
348 add_slider_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
349 GTK_SCROLL_PAGE_LEFT);
351 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
352 GTK_SCROLL_PAGE_LEFT);
354 add_slider_binding (binding_set, GDK_KEY_Page_Up, 0,
357 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
360 add_slider_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
361 GTK_SCROLL_PAGE_RIGHT);
363 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
364 GTK_SCROLL_PAGE_RIGHT);
366 add_slider_binding (binding_set, GDK_KEY_Page_Down, 0,
367 GTK_SCROLL_PAGE_DOWN);
369 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
370 GTK_SCROLL_PAGE_DOWN);
372 /* Logical bindings (vs. visual bindings above) */
374 add_slider_binding (binding_set, GDK_KEY_plus, 0,
375 GTK_SCROLL_STEP_FORWARD);
377 add_slider_binding (binding_set, GDK_KEY_minus, 0,
378 GTK_SCROLL_STEP_BACKWARD);
380 add_slider_binding (binding_set, GDK_KEY_plus, GDK_CONTROL_MASK,
381 GTK_SCROLL_PAGE_FORWARD);
383 add_slider_binding (binding_set, GDK_KEY_minus, GDK_CONTROL_MASK,
384 GTK_SCROLL_PAGE_BACKWARD);
387 add_slider_binding (binding_set, GDK_KEY_KP_Add, 0,
388 GTK_SCROLL_STEP_FORWARD);
390 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, 0,
391 GTK_SCROLL_STEP_BACKWARD);
393 add_slider_binding (binding_set, GDK_KEY_KP_Add, GDK_CONTROL_MASK,
394 GTK_SCROLL_PAGE_FORWARD);
396 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
397 GTK_SCROLL_PAGE_BACKWARD);
400 add_slider_binding (binding_set, GDK_KEY_Home, 0,
403 add_slider_binding (binding_set, GDK_KEY_KP_Home, 0,
406 add_slider_binding (binding_set, GDK_KEY_End, 0,
409 add_slider_binding (binding_set, GDK_KEY_KP_End, 0,
412 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
416 gtk_scale_orientation_notify (GtkRange *range,
417 const GParamSpec *pspec)
419 GtkOrientation orientation;
421 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
422 gtk_range_set_flippable (range,
423 orientation == GTK_ORIENTATION_HORIZONTAL);
427 gtk_scale_init (GtkScale *scale)
429 GtkScalePrivate *priv;
430 GtkRange *range = GTK_RANGE (scale);
431 GtkStyleContext *context;
433 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
438 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
440 gtk_range_set_slider_size_fixed (range, TRUE);
442 priv->draw_value = TRUE;
443 priv->value_pos = GTK_POS_TOP;
445 gtk_range_set_round_digits (range, priv->digits);
447 gtk_scale_orientation_notify (range, NULL);
448 g_signal_connect (scale, "notify::orientation",
449 G_CALLBACK (gtk_scale_orientation_notify),
452 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
453 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE);
457 gtk_scale_set_property (GObject *object,
464 scale = GTK_SCALE (object);
469 gtk_scale_set_digits (scale, g_value_get_int (value));
471 case PROP_DRAW_VALUE:
472 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
475 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
478 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
484 gtk_scale_get_property (GObject *object,
489 GtkScale *scale = GTK_SCALE (object);
490 GtkScalePrivate *priv = scale->priv;
495 g_value_set_int (value, priv->digits);
497 case PROP_DRAW_VALUE:
498 g_value_set_boolean (value, priv->draw_value);
501 g_value_set_enum (value, priv->value_pos);
504 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
511 * @orientation: the scale's orientation.
512 * @adjustment: (allow-none): the #GtkAdjustment which sets the range
513 * of the scale, or %NULL to create a new adjustment.
515 * Creates a new #GtkScale.
517 * Return value: a new #GtkScale
522 gtk_scale_new (GtkOrientation orientation,
523 GtkAdjustment *adjustment)
525 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
528 return g_object_new (GTK_TYPE_SCALE,
529 "orientation", orientation,
530 "adjustment", adjustment,
535 * gtk_scale_new_with_range:
536 * @orientation: the scale's orientation.
537 * @min: minimum value
538 * @max: maximum value
539 * @step: step increment (tick size) used with keyboard shortcuts
541 * Creates a new scale widget with the given orientation that lets the
542 * user input a number between @min and @max (including @min and @max)
543 * with the increment @step. @step must be nonzero; it's the distance
544 * the slider moves when using the arrow keys to adjust the scale
547 * Note that the way in which the precision is derived works best if @step
548 * is a power of ten. If the resulting precision is not suitable for your
549 * needs, use gtk_scale_set_digits() to correct it.
551 * Return value: a new #GtkScale
556 gtk_scale_new_with_range (GtkOrientation orientation,
564 g_return_val_if_fail (min < max, NULL);
565 g_return_val_if_fail (step != 0.0, NULL);
567 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
569 if (fabs (step) >= 1.0 || step == 0.0)
575 digits = abs ((gint) floor (log10 (fabs (step))));
580 return g_object_new (GTK_TYPE_SCALE,
581 "orientation", orientation,
588 * gtk_scale_set_digits:
589 * @scale: a #GtkScale
590 * @digits: the number of decimal places to display,
591 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
593 * Sets the number of decimal places that are displayed in the value.
594 * Also causes the value of the adjustment to be rounded off to this
595 * number of digits, so the retrieved value matches the value the user saw.
598 gtk_scale_set_digits (GtkScale *scale,
601 GtkScalePrivate *priv;
604 g_return_if_fail (GTK_IS_SCALE (scale));
607 range = GTK_RANGE (scale);
609 digits = CLAMP (digits, -1, MAX_DIGITS);
611 if (priv->digits != digits)
613 priv->digits = digits;
614 if (priv->draw_value)
615 gtk_range_set_round_digits (range, digits);
617 _gtk_scale_clear_layout (scale);
618 gtk_widget_queue_resize (GTK_WIDGET (scale));
620 g_object_notify (G_OBJECT (scale), "digits");
625 * gtk_scale_get_digits:
626 * @scale: a #GtkScale
628 * Gets the number of decimal places that are displayed in the value.
630 * Returns: the number of decimal places that are displayed
633 gtk_scale_get_digits (GtkScale *scale)
635 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
637 return scale->priv->digits;
641 * gtk_scale_set_draw_value:
642 * @scale: a #GtkScale
643 * @draw_value: %TRUE to draw the value
645 * Specifies whether the current value is displayed as a string next
649 gtk_scale_set_draw_value (GtkScale *scale,
652 GtkScalePrivate *priv;
654 g_return_if_fail (GTK_IS_SCALE (scale));
658 draw_value = draw_value != FALSE;
660 if (priv->draw_value != draw_value)
662 priv->draw_value = draw_value;
664 gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
666 gtk_range_set_round_digits (GTK_RANGE (scale), -1);
668 _gtk_scale_clear_layout (scale);
670 gtk_widget_queue_resize (GTK_WIDGET (scale));
672 g_object_notify (G_OBJECT (scale), "draw-value");
677 * gtk_scale_get_draw_value:
678 * @scale: a #GtkScale
680 * Returns whether the current value is displayed as a string
681 * next to the slider.
683 * Returns: whether the current value is displayed as a string
686 gtk_scale_get_draw_value (GtkScale *scale)
688 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
690 return scale->priv->draw_value;
694 * gtk_scale_set_value_pos:
695 * @scale: a #GtkScale
696 * @pos: the position in which the current value is displayed
698 * Sets the position in which the current value is displayed.
701 gtk_scale_set_value_pos (GtkScale *scale,
704 GtkScalePrivate *priv;
707 g_return_if_fail (GTK_IS_SCALE (scale));
711 if (priv->value_pos != pos)
713 priv->value_pos = pos;
714 widget = GTK_WIDGET (scale);
716 _gtk_scale_clear_layout (scale);
717 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
718 gtk_widget_queue_resize (widget);
720 g_object_notify (G_OBJECT (scale), "value-pos");
725 * gtk_scale_get_value_pos:
726 * @scale: a #GtkScale
728 * Gets the position in which the current value is displayed.
730 * Returns: the position in which the current value is displayed
733 gtk_scale_get_value_pos (GtkScale *scale)
735 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
737 return scale->priv->value_pos;
741 gtk_scale_get_range_border (GtkRange *range,
744 GtkScalePrivate *priv;
749 widget = GTK_WIDGET (range);
750 scale = GTK_SCALE (range);
753 _gtk_scale_get_value_size (scale, &w, &h);
760 if (priv->draw_value)
763 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
765 switch (priv->value_pos)
768 border->left += w + value_spacing;
771 border->right += w + value_spacing;
774 border->top += h + value_spacing;
777 border->bottom += h + value_spacing;
786 gint n1, w1, h1, n2, w2, h2;
788 gtk_widget_style_get (widget,
789 "slider-width", &slider_width,
790 "value-spacing", &value_spacing,
794 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
796 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
799 border->top += h1 + value_spacing + slider_width / 2;
801 border->bottom += h2 + value_spacing + slider_width / 2;
806 border->left += w1 + value_spacing + slider_width / 2;
808 border->right += w2 + value_spacing + slider_width / 2;
813 /* FIXME this could actually be static at the moment. */
815 _gtk_scale_get_value_size (GtkScale *scale,
819 GtkScalePrivate *priv = scale->priv;
822 if (priv->draw_value)
824 GtkAdjustment *adjustment;
826 PangoRectangle logical_rect;
829 range = GTK_RANGE (scale);
831 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
832 adjustment = gtk_range_get_adjustment (range);
834 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
835 pango_layout_set_text (layout, txt, -1);
838 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
841 *width = logical_rect.width;
843 *height = logical_rect.height;
845 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
846 pango_layout_set_text (layout, txt, -1);
849 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
852 *width = MAX (*width, logical_rect.width);
854 *height = MAX (*height, logical_rect.height);
856 g_object_unref (layout);
869 gtk_scale_get_mark_label_size (GtkScale *scale,
870 GtkPositionType position,
878 GtkScalePrivate *priv = scale->priv;
880 PangoRectangle logical_rect;
884 *count1 = *count2 = 0;
885 *width1 = *width2 = 0;
886 *height1 = *height2 = 0;
888 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
890 for (m = priv->marks; m; m = m->next)
892 GtkScaleMark *mark = m->data;
894 if (mark->markup && *mark->markup)
896 pango_layout_set_markup (layout, mark->markup, -1);
897 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
899 w = logical_rect.width;
900 h = logical_rect.height;
908 if (mark->position == position)
911 *width1 = MAX (*width1, w);
912 *height1 = MAX (*height1, h);
917 *width2 = MAX (*width2, w);
918 *height2 = MAX (*height2, h);
922 g_object_unref (layout);
926 gtk_scale_style_updated (GtkWidget *widget)
931 range = GTK_RANGE (widget);
933 gtk_widget_style_get (widget,
934 "slider-length", &slider_length,
937 gtk_range_set_min_slider_size (range, slider_length);
939 _gtk_scale_clear_layout (GTK_SCALE (widget));
941 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_updated (widget);
945 gtk_scale_screen_changed (GtkWidget *widget,
946 GdkScreen *old_screen)
948 _gtk_scale_clear_layout (GTK_SCALE (widget));
952 gtk_scale_get_preferred_width (GtkWidget *widget,
956 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural);
958 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
960 gint n1, w1, h1, n2, w2, h2;
964 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
966 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
968 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
969 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
972 *minimum = MAX (*minimum, w);
973 *natural = MAX (*natural, w);
978 gtk_scale_get_preferred_height (GtkWidget *widget,
982 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural);
985 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
987 gint n1, w1, h1, n2, w2, h2;
991 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
993 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
994 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
995 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
998 *minimum = MAX (*minimum, h);
999 *natural = MAX (*natural, h);
1004 find_next_pos (GtkWidget *widget,
1007 GtkPositionType pos,
1010 GtkAllocation allocation;
1014 for (m = list->next, i = 1; m; m = m->next, i++)
1016 GtkScaleMark *mark = m->data;
1018 if (match == (mark->position == pos))
1022 gtk_widget_get_allocation (widget, &allocation);
1023 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
1024 return allocation.width;
1026 return allocation.height;
1030 gtk_scale_draw (GtkWidget *widget,
1033 GtkScale *scale = GTK_SCALE (widget);
1034 GtkScalePrivate *priv = scale->priv;
1035 GtkRange *range = GTK_RANGE (scale);
1036 GtkStateFlags state = 0;
1037 GtkStyleContext *context;
1044 context = gtk_widget_get_style_context (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 if (!gtk_widget_is_sensitive (widget))
1057 state |= GTK_STATE_FLAG_INSENSITIVE;
1061 GtkOrientation orientation;
1062 GdkRectangle range_rect;
1064 gint x1, x2, x3, y1, y2, y3;
1065 PangoLayout *layout;
1066 PangoRectangle logical_rect;
1068 gint min_pos_before, min_pos_after;
1069 gint min_pos, max_pos;
1071 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1072 _gtk_range_get_stop_positions (range, &marks);
1073 layout = gtk_widget_create_pango_layout (widget, NULL);
1074 gtk_range_get_range_rect (range, &range_rect);
1076 min_pos_before = min_pos_after = 0;
1078 for (m = priv->marks, i = 0; m; m = m->next, i++)
1080 GtkScaleMark *mark = m->data;
1082 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1085 if (mark->position == GTK_POS_TOP)
1088 y2 = y1 - slider_width / 2;
1089 min_pos = min_pos_before;
1090 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1094 y1 = range_rect.y + range_rect.height;
1095 y2 = y1 + slider_width / 2;
1096 min_pos = min_pos_after;
1097 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM, 1) - min_sep;
1100 gtk_style_context_save (context);
1101 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1102 gtk_style_context_set_state (context, state);
1104 gtk_render_line (context, cr, x1, y1, x1, y2);
1108 pango_layout_set_markup (layout, mark->markup, -1);
1109 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1111 x3 = x1 - logical_rect.width / 2;
1114 if (x3 + logical_rect.width > max_pos)
1115 x3 = max_pos - logical_rect.width;
1118 if (mark->position == GTK_POS_TOP)
1120 y3 = y2 - value_spacing - logical_rect.height;
1121 min_pos_before = x3 + logical_rect.width + min_sep;
1125 y3 = y2 + value_spacing;
1126 min_pos_after = x3 + logical_rect.width + min_sep;
1129 gtk_render_layout (context, cr, x3, y3, layout);
1132 gtk_style_context_restore (context);
1136 if (mark->position == GTK_POS_TOP)
1139 x2 = range_rect.x - slider_width / 2;
1140 min_pos = min_pos_before;
1141 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1145 x1 = range_rect.x + range_rect.width;
1146 x2 = range_rect.x + range_rect.width + slider_width / 2;
1147 min_pos = min_pos_after;
1148 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM, 1) - min_sep;
1152 gtk_style_context_save (context);
1153 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1154 gtk_style_context_set_state (context, state);
1156 gtk_render_line (context, cr, x1, y1, x2, y1);
1160 pango_layout_set_markup (layout, mark->markup, -1);
1161 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1163 y3 = y1 - logical_rect.height / 2;
1166 if (y3 + logical_rect.height > max_pos)
1167 y3 = max_pos - logical_rect.height;
1170 if (mark->position == GTK_POS_TOP)
1172 x3 = x2 - value_spacing - logical_rect.width;
1173 min_pos_before = y3 + logical_rect.height + min_sep;
1177 x3 = x2 + value_spacing;
1178 min_pos_after = y3 + logical_rect.height + min_sep;
1181 gtk_render_layout (context, cr, x3, y3, layout);
1184 gtk_style_context_restore (context);
1188 g_object_unref (layout);
1192 if (priv->draw_value)
1194 GtkAllocation allocation;
1196 PangoLayout *layout;
1199 layout = gtk_scale_get_layout (scale);
1200 gtk_scale_get_layout_offsets (scale, &x, &y);
1201 gtk_widget_get_allocation (widget, &allocation);
1203 gtk_render_layout (context, cr,
1213 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1217 GtkScalePrivate *priv = scale->priv;
1218 GtkAllocation allocation;
1219 GtkWidget *widget = GTK_WIDGET (scale);
1220 GtkRange *range = GTK_RANGE (widget);
1221 GdkRectangle range_rect;
1222 PangoLayout *layout = gtk_scale_get_layout (scale);
1223 PangoRectangle logical_rect;
1224 gint slider_start, slider_end;
1235 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1237 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1239 gtk_widget_get_allocation (widget, &allocation);
1240 gtk_range_get_range_rect (range, &range_rect);
1241 gtk_range_get_slider_range (range,
1245 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1247 switch (priv->value_pos)
1250 *x = range_rect.x - value_spacing - logical_rect.width;
1251 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1255 *x = range_rect.x + range_rect.width + value_spacing;
1256 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1260 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1261 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1262 *y = range_rect.y - logical_rect.height - value_spacing;
1265 case GTK_POS_BOTTOM:
1266 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1267 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1268 *y = range_rect.y + range_rect.height + value_spacing;
1272 g_return_if_reached ();
1278 switch (priv->value_pos)
1281 *x = range_rect.x - logical_rect.width - value_spacing;
1282 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1283 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1287 *x = range_rect.x + range_rect.width + value_spacing;
1288 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1289 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1293 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1294 *y = range_rect.y - logical_rect.height - value_spacing;
1297 case GTK_POS_BOTTOM:
1298 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1299 *y = range_rect.y + range_rect.height + value_spacing;
1303 g_return_if_reached ();
1312 * _gtk_scale_format_value:
1313 * @scale: a #GtkScale
1314 * @value: adjustment value
1316 * Emits #GtkScale::format-value signal to format the value,
1317 * if no user signal handlers, falls back to a default format.
1319 * Return value: formatted value
1322 _gtk_scale_format_value (GtkScale *scale,
1325 GtkScalePrivate *priv = scale->priv;
1328 g_signal_emit (scale,
1329 signals[FORMAT_VALUE],
1337 /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1338 return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value);
1342 gtk_scale_finalize (GObject *object)
1344 GtkScale *scale = GTK_SCALE (object);
1346 _gtk_scale_clear_layout (scale);
1347 gtk_scale_clear_marks (scale);
1349 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1353 * gtk_scale_get_layout:
1354 * @scale: A #GtkScale
1356 * Gets the #PangoLayout used to display the scale. The returned
1357 * object is owned by the scale so does not need to be freed by
1360 * Return value: (transfer none): the #PangoLayout for this scale,
1361 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1366 gtk_scale_get_layout (GtkScale *scale)
1368 GtkScalePrivate *priv;
1371 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1377 if (priv->draw_value)
1378 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1381 if (priv->draw_value)
1383 txt = _gtk_scale_format_value (scale,
1384 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1385 pango_layout_set_text (priv->layout, txt, -1);
1389 return priv->layout;
1393 * gtk_scale_get_layout_offsets:
1394 * @scale: a #GtkScale
1395 * @x: (out) (allow-none): location to store X offset of layout, or %NULL
1396 * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
1398 * Obtains the coordinates where the scale will draw the
1399 * #PangoLayout representing the text in the scale. Remember
1400 * when using the #PangoLayout function you need to convert to
1401 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1403 * If the #GtkScale:draw-value property is %FALSE, the return
1404 * values are undefined.
1409 gtk_scale_get_layout_offsets (GtkScale *scale,
1416 g_return_if_fail (GTK_IS_SCALE (scale));
1418 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1419 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1429 _gtk_scale_clear_layout (GtkScale *scale)
1431 GtkScalePrivate *priv = scale->priv;
1433 g_return_if_fail (GTK_IS_SCALE (scale));
1437 g_object_unref (priv->layout);
1438 priv->layout = NULL;
1443 gtk_scale_mark_free (GtkScaleMark *mark)
1445 g_free (mark->markup);
1450 * gtk_scale_clear_marks:
1451 * @scale: a #GtkScale
1453 * Removes any marks that have been added with gtk_scale_add_mark().
1458 gtk_scale_clear_marks (GtkScale *scale)
1460 GtkScalePrivate *priv;
1461 GtkStyleContext *context;
1463 g_return_if_fail (GTK_IS_SCALE (scale));
1467 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1468 g_slist_free (priv->marks);
1471 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1472 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1473 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1475 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1477 gtk_widget_queue_resize (GTK_WIDGET (scale));
1481 compare_marks (gpointer a, gpointer b)
1483 GtkScaleMark *ma, *mb;
1487 return (gint) (ma->value - mb->value);
1491 * gtk_scale_add_mark:
1492 * @scale: a #GtkScale
1493 * @value: the value at which the mark is placed, must be between
1494 * the lower and upper limits of the scales' adjustment
1495 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1496 * and %GTK_POS_LEFT are drawn above the scale, anything else below.
1497 * For a vertical scale, #GTK_POS_LEFT and %GTK_POS_TOP are drawn to
1498 * the left of the scale, anything else to the right.
1499 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1502 * Adds a mark at @value.
1504 * A mark is indicated visually by drawing a tick mark next to the scale,
1505 * and GTK+ makes it easy for the user to position the scale exactly at the
1508 * If @markup is not %NULL, text is shown next to the tick mark.
1510 * To remove marks from a scale, use gtk_scale_clear_marks().
1515 gtk_scale_add_mark (GtkScale *scale,
1517 GtkPositionType position,
1518 const gchar *markup)
1520 GtkScalePrivate *priv;
1525 GtkStyleContext *context;
1528 g_return_if_fail (GTK_IS_SCALE (scale));
1532 mark = g_new (GtkScaleMark, 1);
1533 mark->value = value;
1534 mark->markup = g_strdup (markup);
1535 if (position == GTK_POS_LEFT ||
1536 position == GTK_POS_TOP)
1537 mark->position = GTK_POS_TOP;
1539 mark->position = GTK_POS_BOTTOM;
1541 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1542 (GCompareFunc) compare_marks);
1544 #define MARKS_ABOVE 1
1545 #define MARKS_BELOW 2
1548 n = g_slist_length (priv->marks);
1549 values = g_new (gdouble, n);
1550 for (m = priv->marks, i = 0; m; m = m->next, i++)
1553 values[i] = mark->value;
1554 if (mark->position == GTK_POS_TOP)
1555 all_pos |= MARKS_ABOVE;
1557 all_pos |= MARKS_BELOW;
1560 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1564 /* Set the style classes for the slider, so it could
1565 * point to the right direction when marks are present
1567 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
1569 if (all_pos & MARKS_ABOVE)
1570 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1572 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
1573 if (all_pos & MARKS_BELOW)
1574 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1576 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
1578 gtk_widget_queue_resize (GTK_WIDGET (scale));
1581 static GtkBuildableIface *parent_buildable_iface;
1584 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1586 parent_buildable_iface = g_type_interface_peek_parent (iface);
1587 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1588 iface->custom_finished = gtk_scale_buildable_custom_finished;
1594 GtkBuilder *builder;
1596 } MarksSubparserData;
1601 GtkPositionType position;
1604 gboolean translatable;
1608 mark_data_free (MarkData *data)
1610 g_string_free (data->markup, TRUE);
1611 g_free (data->context);
1612 g_slice_free (MarkData, data);
1616 marks_start_element (GMarkupParseContext *context,
1617 const gchar *element_name,
1618 const gchar **names,
1619 const gchar **values,
1623 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1625 gint line_number, char_number;
1627 if (strcmp (element_name, "marks") == 0)
1629 else if (strcmp (element_name, "mark") == 0)
1632 gboolean has_value = FALSE;
1633 GtkPositionType position = GTK_POS_BOTTOM;
1634 const gchar *msg_context = NULL;
1635 gboolean translatable = FALSE;
1638 for (i = 0; names[i]; i++)
1640 if (strcmp (names[i], "translatable") == 0)
1642 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1645 else if (strcmp (names[i], "comments") == 0)
1647 /* do nothing, comments are for translators */
1649 else if (strcmp (names[i], "context") == 0)
1650 msg_context = values[i];
1651 else if (strcmp (names[i], "value") == 0)
1653 GValue gvalue = { 0, };
1655 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1658 value = g_value_get_double (&gvalue);
1661 else if (strcmp (names[i], "position") == 0)
1663 GValue gvalue = { 0, };
1665 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1668 position = g_value_get_enum (&gvalue);
1672 g_markup_parse_context_get_position (context,
1677 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1678 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1680 line_number, char_number, names[i], "mark");
1687 g_markup_parse_context_get_position (context,
1692 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1693 "%s:%d:%d <%s> requires attribute \"%s\"",
1695 line_number, char_number, "mark",
1700 mark = g_slice_new (MarkData);
1701 mark->value = value;
1702 if (position == GTK_POS_LEFT ||
1703 position == GTK_POS_TOP)
1704 mark->position = GTK_POS_TOP;
1706 mark->position = GTK_POS_BOTTOM;
1707 mark->markup = g_string_new ("");
1708 mark->context = g_strdup (msg_context);
1709 mark->translatable = translatable;
1711 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1715 g_markup_parse_context_get_position (context,
1720 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1721 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1723 line_number, char_number, element_name);
1729 marks_text (GMarkupParseContext *context,
1735 MarksSubparserData *data = (MarksSubparserData*)user_data;
1737 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1739 MarkData *mark = data->marks->data;
1741 g_string_append_len (mark->markup, text, text_len);
1745 static const GMarkupParser marks_parser =
1747 marks_start_element,
1754 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1755 GtkBuilder *builder,
1757 const gchar *tagname,
1758 GMarkupParser *parser,
1761 MarksSubparserData *parser_data;
1766 if (strcmp (tagname, "marks") == 0)
1768 parser_data = g_slice_new0 (MarksSubparserData);
1769 parser_data->scale = GTK_SCALE (buildable);
1770 parser_data->marks = NULL;
1772 *parser = marks_parser;
1773 *data = parser_data;
1777 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1778 tagname, parser, data);
1782 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1783 GtkBuilder *builder,
1785 const gchar *tagname,
1788 GtkScale *scale = GTK_SCALE (buildable);
1789 MarksSubparserData *marks_data;
1791 if (strcmp (tagname, "marks") == 0)
1796 marks_data = (MarksSubparserData *)user_data;
1798 for (m = marks_data->marks; m; m = m->next)
1800 MarkData *mdata = m->data;
1802 if (mdata->translatable && mdata->markup->len)
1803 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1805 mdata->markup->str);
1807 markup = mdata->markup->str;
1809 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1811 mark_data_free (mdata);
1814 g_slist_free (marks_data->marks);
1815 g_slice_free (MarksSubparserData, marks_data);