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 "gtktypebuiltins.h"
41 #include "gtkprivate.h"
43 #include "gtkbuildable.h"
44 #include "gtkbuilderprivate.h"
49 * @Short_description: Base class for GtkHScale and GtkVScale
52 * A GtkScale is a slider control used to select a numeric value.
53 * To use it, you'll probably want to investigate the methods on
54 * its base class, #GtkRange, in addition to the methods for GtkScale itself.
55 * To set the value of a scale, you would normally use gtk_range_set_value().
56 * To detect changes to the value, you would normally use the
57 * #GtkRange::value-changed signal.
59 * Note that using the same upper and lower bounds for the #GtkScale (through
60 * the #GtkRange methods) will hide the slider itself. This is useful for
61 * applications that want to show an undeterminate value on the scale, without
62 * changing the layout of the application (such as movie or music players).
64 * <refsect2 id="GtkScale-BUILDER-UI"><title>GtkScale as GtkBuildable</title>
65 * GtkScale supports a custom <marks> element, which
66 * can contain multiple <mark> elements. The "value" and "position"
67 * attributes have the same meaning as gtk_scale_add_mark() parameters of the
68 * same name. If the element is not empty, its content is taken as the markup
69 * to show at the mark. It can be translated with the usual "translatable and
70 * "context" attributes.
75 #define MAX_DIGITS (64) /* don't change this,
76 * a) you don't need to and
77 * b) you might cause buffer owerflows in
78 * unrelated code portions otherwise
82 typedef struct _GtkScaleMark GtkScaleMark;
84 struct _GtkScalePrivate
100 GtkPositionType position;
115 static guint signals[LAST_SIGNAL];
117 static void gtk_scale_set_property (GObject *object,
121 static void gtk_scale_get_property (GObject *object,
125 static void gtk_scale_get_preferred_width (GtkWidget *widget,
128 static void gtk_scale_get_preferred_height (GtkWidget *widget,
131 static void gtk_scale_style_updated (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_updated = gtk_scale_style_updated;
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);
433 GtkStyleContext *context;
435 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
440 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
442 gtk_range_set_slider_size_fixed (range, TRUE);
444 priv->draw_value = TRUE;
445 priv->value_pos = GTK_POS_TOP;
447 _gtk_range_set_round_digits (range, priv->digits);
449 gtk_scale_orientation_notify (range, NULL);
450 g_signal_connect (scale, "notify::orientation",
451 G_CALLBACK (gtk_scale_orientation_notify),
454 context = gtk_widget_get_style_context (GTK_WIDGET (scale));
455 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE);
459 gtk_scale_set_property (GObject *object,
466 scale = GTK_SCALE (object);
471 gtk_scale_set_digits (scale, g_value_get_int (value));
473 case PROP_DRAW_VALUE:
474 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
477 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486 gtk_scale_get_property (GObject *object,
491 GtkScale *scale = GTK_SCALE (object);
492 GtkScalePrivate *priv = scale->priv;
497 g_value_set_int (value, priv->digits);
499 case PROP_DRAW_VALUE:
500 g_value_set_boolean (value, priv->draw_value);
503 g_value_set_enum (value, priv->value_pos);
506 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
513 * @orientation: the scale's orientation.
514 * @adjustment: the #GtkAdjustment which sets the range of the scale, or
515 * %NULL to create a new adjustment.
517 * Creates a new #GtkScale.
519 * Return value: a new #GtkScale
524 gtk_scale_new (GtkOrientation orientation,
525 GtkAdjustment *adjustment)
527 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
530 return g_object_new (GTK_TYPE_SCALE,
531 "orientation", orientation,
532 "adjustment", adjustment,
537 * gtk_scale_new_with_range:
538 * @orientation: the scale's orientation.
539 * @min: minimum value
540 * @max: maximum value
541 * @step: step increment (tick size) used with keyboard shortcuts
543 * Creates a new scale widget with the given orientation that lets the
544 * user input a number between @min and @max (including @min and @max)
545 * with the increment @step. @step must be nonzero; it's the distance
546 * the slider moves when using the arrow keys to adjust the scale
549 * Note that the way in which the precision is derived works best if @step
550 * is a power of ten. If the resulting precision is not suitable for your
551 * needs, use gtk_scale_set_digits() to correct it.
553 * Return value: a new #GtkScale
558 gtk_scale_new_with_range (GtkOrientation orientation,
566 g_return_val_if_fail (min < max, NULL);
567 g_return_val_if_fail (step != 0.0, NULL);
569 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
571 if (fabs (step) >= 1.0 || step == 0.0)
577 digits = abs ((gint) floor (log10 (fabs (step))));
582 return g_object_new (GTK_TYPE_SCALE,
583 "orientation", orientation,
590 * gtk_scale_set_digits:
591 * @scale: a #GtkScale
592 * @digits: the number of decimal places to display,
593 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
595 * Sets the number of decimal places that are displayed in the value.
596 * Also causes the value of the adjustment to be rounded off to this
597 * number of digits, so the retrieved value matches the value the user saw.
600 gtk_scale_set_digits (GtkScale *scale,
603 GtkScalePrivate *priv;
606 g_return_if_fail (GTK_IS_SCALE (scale));
609 range = GTK_RANGE (scale);
611 digits = CLAMP (digits, -1, MAX_DIGITS);
613 if (priv->digits != digits)
615 priv->digits = digits;
616 if (priv->draw_value)
617 _gtk_range_set_round_digits (range, digits);
619 _gtk_scale_clear_layout (scale);
620 gtk_widget_queue_resize (GTK_WIDGET (scale));
622 g_object_notify (G_OBJECT (scale), "digits");
627 * gtk_scale_get_digits:
628 * @scale: a #GtkScale
630 * Gets the number of decimal places that are displayed in the value.
632 * Returns: the number of decimal places that are displayed
635 gtk_scale_get_digits (GtkScale *scale)
637 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
639 return scale->priv->digits;
643 * gtk_scale_set_draw_value:
644 * @scale: a #GtkScale
645 * @draw_value: %TRUE to draw the value
647 * Specifies whether the current value is displayed as a string next
651 gtk_scale_set_draw_value (GtkScale *scale,
654 GtkScalePrivate *priv;
656 g_return_if_fail (GTK_IS_SCALE (scale));
660 draw_value = draw_value != FALSE;
662 if (priv->draw_value != draw_value)
664 priv->draw_value = draw_value;
666 _gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
668 _gtk_range_set_round_digits (GTK_RANGE (scale), -1);
670 _gtk_scale_clear_layout (scale);
672 gtk_widget_queue_resize (GTK_WIDGET (scale));
674 g_object_notify (G_OBJECT (scale), "draw-value");
679 * gtk_scale_get_draw_value:
680 * @scale: a #GtkScale
682 * Returns whether the current value is displayed as a string
683 * next to the slider.
685 * Returns: whether the current value is displayed as a string
688 gtk_scale_get_draw_value (GtkScale *scale)
690 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
692 return scale->priv->draw_value;
696 * gtk_scale_set_value_pos:
697 * @scale: a #GtkScale
698 * @pos: the position in which the current value is displayed
700 * Sets the position in which the current value is displayed.
703 gtk_scale_set_value_pos (GtkScale *scale,
706 GtkScalePrivate *priv;
709 g_return_if_fail (GTK_IS_SCALE (scale));
713 if (priv->value_pos != pos)
715 priv->value_pos = pos;
716 widget = GTK_WIDGET (scale);
718 _gtk_scale_clear_layout (scale);
719 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
720 gtk_widget_queue_resize (widget);
722 g_object_notify (G_OBJECT (scale), "value-pos");
727 * gtk_scale_get_value_pos:
728 * @scale: a #GtkScale
730 * Gets the position in which the current value is displayed.
732 * Returns: the position in which the current value is displayed
735 gtk_scale_get_value_pos (GtkScale *scale)
737 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
739 return scale->priv->value_pos;
743 gtk_scale_get_range_border (GtkRange *range,
746 GtkScalePrivate *priv;
751 widget = GTK_WIDGET (range);
752 scale = GTK_SCALE (range);
755 _gtk_scale_get_value_size (scale, &w, &h);
762 if (priv->draw_value)
765 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
767 switch (priv->value_pos)
770 border->left += w + value_spacing;
773 border->right += w + value_spacing;
776 border->top += h + value_spacing;
779 border->bottom += h + value_spacing;
788 gint n1, w1, h1, n2, w2, h2;
790 gtk_widget_style_get (widget,
791 "slider-width", &slider_width,
792 "value-spacing", &value_spacing,
796 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
798 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
800 border->top += h1 + value_spacing + slider_width / 2;
802 border->bottom += h2 + value_spacing + slider_width / 2;
806 gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
808 border->left += w1 + value_spacing + slider_width / 2;
810 border->right += w2 + value_spacing + slider_width / 2;
815 /* FIXME this could actually be static at the moment. */
817 _gtk_scale_get_value_size (GtkScale *scale,
821 GtkScalePrivate *priv = scale->priv;
824 if (priv->draw_value)
826 GtkAdjustment *adjustment;
828 PangoRectangle logical_rect;
831 range = GTK_RANGE (scale);
833 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
834 adjustment = gtk_range_get_adjustment (range);
836 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
837 pango_layout_set_text (layout, txt, -1);
840 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
843 *width = logical_rect.width;
845 *height = logical_rect.height;
847 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
848 pango_layout_set_text (layout, txt, -1);
851 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
854 *width = MAX (*width, logical_rect.width);
856 *height = MAX (*height, logical_rect.height);
858 g_object_unref (layout);
871 gtk_scale_get_mark_label_size (GtkScale *scale,
872 GtkPositionType position,
880 GtkScalePrivate *priv = scale->priv;
882 PangoRectangle logical_rect;
886 *count1 = *count2 = 0;
887 *width1 = *width2 = 0;
888 *height1 = *height2 = 0;
890 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
892 for (m = priv->marks; m; m = m->next)
894 GtkScaleMark *mark = m->data;
896 if (mark->markup && *mark->markup)
898 pango_layout_set_markup (layout, mark->markup, -1);
899 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
901 w = logical_rect.width;
902 h = logical_rect.height;
910 if (mark->position == position)
913 *width1 = MAX (*width1, w);
914 *height1 = MAX (*height1, h);
919 *width2 = MAX (*width2, w);
920 *height2 = MAX (*height2, h);
924 g_object_unref (layout);
928 gtk_scale_style_updated (GtkWidget *widget)
933 range = GTK_RANGE (widget);
935 gtk_widget_style_get (widget,
936 "slider-length", &slider_length,
939 gtk_range_set_min_slider_size (range, slider_length);
941 _gtk_scale_clear_layout (GTK_SCALE (widget));
943 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_updated (widget);
947 gtk_scale_screen_changed (GtkWidget *widget,
948 GdkScreen *old_screen)
950 _gtk_scale_clear_layout (GTK_SCALE (widget));
954 gtk_scale_get_preferred_width (GtkWidget *widget,
958 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural);
960 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
962 gint n1, w1, h1, n2, w2, h2;
966 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
968 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
970 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
971 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
974 *minimum = MAX (*minimum, w);
975 *natural = MAX (*natural, w);
980 gtk_scale_get_preferred_height (GtkWidget *widget,
984 GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural);
987 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
989 gint n1, w1, h1, n2, w2, h2;
993 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
995 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
996 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
997 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
1000 *minimum = MAX (*minimum, h);
1001 *natural = MAX (*natural, h);
1006 find_next_pos (GtkWidget *widget,
1009 GtkPositionType pos,
1012 GtkAllocation allocation;
1016 for (m = list->next, i = 1; m; m = m->next, i++)
1018 GtkScaleMark *mark = m->data;
1020 if (match == (mark->position == pos))
1024 gtk_widget_get_allocation (widget, &allocation);
1025 if (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM)
1026 return allocation.width;
1028 return allocation.height;
1032 gtk_scale_draw (GtkWidget *widget,
1035 GtkScale *scale = GTK_SCALE (widget);
1036 GtkScalePrivate *priv = scale->priv;
1037 GtkRange *range = GTK_RANGE (scale);
1038 GtkStateFlags state = 0;
1039 GtkStyleContext *context;
1047 context = gtk_widget_get_style_context (widget);
1048 gtk_widget_style_get (widget,
1049 "focus-padding", &focus_padding,
1050 "slider-width", &slider_width,
1051 "value-spacing", &value_spacing,
1054 /* We need to chain up _first_ so the various geometry members of
1055 * GtkRange struct are updated.
1057 GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1059 if (!gtk_widget_is_sensitive (widget))
1060 state |= GTK_STATE_FLAG_INSENSITIVE;
1064 GtkOrientation orientation;
1065 GdkRectangle range_rect;
1067 gint x1, x2, x3, y1, y2, y3;
1068 PangoLayout *layout;
1069 PangoRectangle logical_rect;
1071 gint min_pos_before, min_pos_after;
1072 gint min_pos, max_pos;
1074 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1075 n_marks = _gtk_range_get_stop_positions (range, &marks);
1076 layout = gtk_widget_create_pango_layout (widget, NULL);
1077 gtk_range_get_range_rect (range, &range_rect);
1079 min_pos_before = min_pos_after = 0;
1081 for (m = priv->marks, i = 0; m; m = m->next, i++)
1083 GtkScaleMark *mark = m->data;
1085 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1088 if (mark->position == GTK_POS_TOP)
1091 y2 = y1 - slider_width / 2;
1092 min_pos = min_pos_before;
1093 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1097 y1 = range_rect.y + range_rect.height;
1098 y2 = y1 + slider_width / 2;
1099 min_pos = min_pos_after;
1100 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep;
1103 gtk_style_context_save (context);
1104 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1105 gtk_style_context_set_state (context, state);
1107 gtk_render_line (context, cr,
1112 pango_layout_set_markup (layout, mark->markup, -1);
1113 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1115 x3 = x1 - logical_rect.width / 2;
1118 if (x3 + logical_rect.width > max_pos)
1119 x3 = max_pos - logical_rect.width;
1122 if (mark->position == GTK_POS_TOP)
1124 y3 = y2 - value_spacing - logical_rect.height;
1125 min_pos_before = x3 + logical_rect.width + min_sep;
1129 y3 = y2 + value_spacing;
1130 min_pos_after = x3 + logical_rect.width + min_sep;
1133 gtk_render_layout (context, cr,
1137 gtk_style_context_restore (context);
1141 if (mark->position == GTK_POS_LEFT)
1144 x2 = range_rect.x - slider_width / 2;
1145 min_pos = min_pos_before;
1146 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep;
1150 x1 = range_rect.x + range_rect.width;
1151 x2 = range_rect.x + range_rect.width + slider_width / 2;
1152 min_pos = min_pos_after;
1153 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep;
1157 gtk_style_context_save (context);
1158 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1159 gtk_style_context_set_state (context, state);
1161 gtk_render_line (context, cr,
1166 pango_layout_set_markup (layout, mark->markup, -1);
1167 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1169 y3 = y1 - logical_rect.height / 2;
1172 if (y3 + logical_rect.height > max_pos)
1173 y3 = max_pos - logical_rect.height;
1176 if (mark->position == GTK_POS_LEFT)
1178 x3 = x2 - value_spacing - logical_rect.width;
1179 min_pos_before = y3 + logical_rect.height + min_sep;
1183 x3 = x2 + value_spacing;
1184 min_pos_after = y3 + logical_rect.height + min_sep;
1187 gtk_render_layout (context, cr,
1191 gtk_style_context_restore (context);
1195 g_object_unref (layout);
1199 if (priv->draw_value)
1201 GtkOrientation orientation;
1202 GtkAllocation allocation;
1204 PangoLayout *layout;
1207 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1208 layout = gtk_scale_get_layout (scale);
1209 gtk_scale_get_layout_offsets (scale, &x, &y);
1210 gtk_widget_get_allocation (widget, &allocation);
1212 gtk_render_layout (context, cr,
1222 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1226 GtkScalePrivate *priv = scale->priv;
1227 GtkAllocation allocation;
1228 GtkWidget *widget = GTK_WIDGET (scale);
1229 GtkRange *range = GTK_RANGE (widget);
1230 GdkRectangle range_rect;
1231 PangoLayout *layout = gtk_scale_get_layout (scale);
1232 PangoRectangle logical_rect;
1233 gint slider_start, slider_end;
1244 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1246 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1248 gtk_widget_get_allocation (widget, &allocation);
1249 gtk_range_get_range_rect (range, &range_rect);
1250 gtk_range_get_slider_range (range,
1254 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1256 switch (priv->value_pos)
1259 *x = range_rect.x - value_spacing - logical_rect.width;
1260 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1264 *x = range_rect.x + range_rect.width + value_spacing;
1265 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1269 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1270 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1271 *y = range_rect.y - logical_rect.height - value_spacing;
1274 case GTK_POS_BOTTOM:
1275 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1276 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1277 *y = range_rect.y + range_rect.height + value_spacing;
1281 g_return_if_reached ();
1287 switch (priv->value_pos)
1290 *x = range_rect.x - logical_rect.width - value_spacing;
1291 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1292 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1296 *x = range_rect.x + range_rect.width + value_spacing;
1297 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1298 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1302 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1303 *y = range_rect.y - logical_rect.height - value_spacing;
1306 case GTK_POS_BOTTOM:
1307 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1308 *y = range_rect.y + range_rect.height + value_spacing;
1312 g_return_if_reached ();
1321 * _gtk_scale_format_value:
1322 * @scale: a #GtkScale
1323 * @value: adjustment value
1325 * Emits #GtkScale::format-value signal to format the value,
1326 * if no user signal handlers, falls back to a default format.
1328 * Return value: formatted value
1331 _gtk_scale_format_value (GtkScale *scale,
1334 GtkScalePrivate *priv = scale->priv;
1337 g_signal_emit (scale,
1338 signals[FORMAT_VALUE],
1346 /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1347 return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value);
1351 gtk_scale_finalize (GObject *object)
1353 GtkScale *scale = GTK_SCALE (object);
1355 _gtk_scale_clear_layout (scale);
1356 gtk_scale_clear_marks (scale);
1358 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1362 * gtk_scale_get_layout:
1363 * @scale: A #GtkScale
1365 * Gets the #PangoLayout used to display the scale. The returned
1366 * object is owned by the scale so does not need to be freed by
1369 * Return value: (transfer none): the #PangoLayout for this scale,
1370 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1375 gtk_scale_get_layout (GtkScale *scale)
1377 GtkScalePrivate *priv;
1380 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1386 if (priv->draw_value)
1387 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1390 if (priv->draw_value)
1392 txt = _gtk_scale_format_value (scale,
1393 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1394 pango_layout_set_text (priv->layout, txt, -1);
1398 return priv->layout;
1402 * gtk_scale_get_layout_offsets:
1403 * @scale: a #GtkScale
1404 * @x: (allow-none): location to store X offset of layout, or %NULL
1405 * @y: (allow-none): location to store Y offset of layout, or %NULL
1407 * Obtains the coordinates where the scale will draw the
1408 * #PangoLayout representing the text in the scale. Remember
1409 * when using the #PangoLayout function you need to convert to
1410 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1412 * If the #GtkScale:draw-value property is %FALSE, the return
1413 * values are undefined.
1418 gtk_scale_get_layout_offsets (GtkScale *scale,
1425 g_return_if_fail (GTK_IS_SCALE (scale));
1427 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1428 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1438 _gtk_scale_clear_layout (GtkScale *scale)
1440 GtkScalePrivate *priv = scale->priv;
1442 g_return_if_fail (GTK_IS_SCALE (scale));
1446 g_object_unref (priv->layout);
1447 priv->layout = NULL;
1452 gtk_scale_mark_free (GtkScaleMark *mark)
1454 g_free (mark->markup);
1459 * gtk_scale_clear_marks:
1460 * @scale: a #GtkScale
1462 * Removes any marks that have been added with gtk_scale_add_mark().
1467 gtk_scale_clear_marks (GtkScale *scale)
1469 GtkScalePrivate *priv;
1471 g_return_if_fail (GTK_IS_SCALE (scale));
1475 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1476 g_slist_free (priv->marks);
1479 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1481 gtk_widget_queue_resize (GTK_WIDGET (scale));
1485 compare_marks (gpointer a, gpointer b)
1487 GtkScaleMark *ma, *mb;
1491 return (gint) (ma->value - mb->value);
1495 * gtk_scale_add_mark:
1496 * @scale: a #GtkScale
1497 * @value: the value at which the mark is placed, must be between
1498 * the lower and upper limits of the scales' adjustment
1499 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1500 * is drawn above the scale, anything else below. For a vertical scale,
1501 * #GTK_POS_LEFT is drawn to the left of the scale, anything else to the
1503 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1506 * Adds a mark at @value.
1508 * A mark is indicated visually by drawing a tick mark next to the scale,
1509 * and GTK+ makes it easy for the user to position the scale exactly at the
1512 * If @markup is not %NULL, text is shown next to the tick mark.
1514 * To remove marks from a scale, use gtk_scale_clear_marks().
1519 gtk_scale_add_mark (GtkScale *scale,
1521 GtkPositionType position,
1522 const gchar *markup)
1524 GtkScalePrivate *priv;
1530 g_return_if_fail (GTK_IS_SCALE (scale));
1534 mark = g_new (GtkScaleMark, 1);
1535 mark->value = value;
1536 mark->markup = g_strdup (markup);
1537 mark->position = position;
1539 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1540 (GCompareFunc) compare_marks);
1542 n = g_slist_length (priv->marks);
1543 values = g_new (gdouble, n);
1544 for (m = priv->marks, i = 0; m; m = m->next, i++)
1547 values[i] = mark->value;
1550 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1554 gtk_widget_queue_resize (GTK_WIDGET (scale));
1557 static GtkBuildableIface *parent_buildable_iface;
1560 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1562 parent_buildable_iface = g_type_interface_peek_parent (iface);
1563 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1564 iface->custom_finished = gtk_scale_buildable_custom_finished;
1570 GtkBuilder *builder;
1572 } MarksSubparserData;
1577 GtkPositionType position;
1580 gboolean translatable;
1584 mark_data_free (MarkData *data)
1586 g_string_free (data->markup, TRUE);
1587 g_free (data->context);
1588 g_slice_free (MarkData, data);
1592 marks_start_element (GMarkupParseContext *context,
1593 const gchar *element_name,
1594 const gchar **names,
1595 const gchar **values,
1599 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1601 gint line_number, char_number;
1603 if (strcmp (element_name, "marks") == 0)
1605 else if (strcmp (element_name, "mark") == 0)
1608 gboolean has_value = FALSE;
1609 GtkPositionType position = GTK_POS_BOTTOM;
1610 const gchar *msg_context = NULL;
1611 gboolean translatable = FALSE;
1614 for (i = 0; names[i]; i++)
1616 if (strcmp (names[i], "translatable") == 0)
1618 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1621 else if (strcmp (names[i], "comments") == 0)
1623 /* do nothing, comments are for translators */
1625 else if (strcmp (names[i], "context") == 0)
1626 msg_context = values[i];
1627 else if (strcmp (names[i], "value") == 0)
1629 GValue gvalue = { 0, };
1631 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1634 value = g_value_get_double (&gvalue);
1637 else if (strcmp (names[i], "position") == 0)
1639 GValue gvalue = { 0, };
1641 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1644 position = g_value_get_enum (&gvalue);
1648 g_markup_parse_context_get_position (context,
1653 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1654 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1656 line_number, char_number, names[i], "mark");
1663 g_markup_parse_context_get_position (context,
1668 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1669 "%s:%d:%d <%s> requires attribute \"%s\"",
1671 line_number, char_number, "mark",
1676 mark = g_slice_new (MarkData);
1677 mark->value = value;
1678 mark->position = position;
1679 mark->markup = g_string_new ("");
1680 mark->context = g_strdup (msg_context);
1681 mark->translatable = translatable;
1683 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1687 g_markup_parse_context_get_position (context,
1692 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1693 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1695 line_number, char_number, element_name);
1701 marks_text (GMarkupParseContext *context,
1707 MarksSubparserData *data = (MarksSubparserData*)user_data;
1709 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1711 MarkData *mark = data->marks->data;
1713 g_string_append_len (mark->markup, text, text_len);
1717 static const GMarkupParser marks_parser =
1719 marks_start_element,
1726 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1727 GtkBuilder *builder,
1729 const gchar *tagname,
1730 GMarkupParser *parser,
1733 MarksSubparserData *parser_data;
1738 if (strcmp (tagname, "marks") == 0)
1740 parser_data = g_slice_new0 (MarksSubparserData);
1741 parser_data->scale = GTK_SCALE (buildable);
1742 parser_data->marks = NULL;
1744 *parser = marks_parser;
1745 *data = parser_data;
1749 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1750 tagname, parser, data);
1754 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1755 GtkBuilder *builder,
1757 const gchar *tagname,
1760 GtkScale *scale = GTK_SCALE (buildable);
1761 MarksSubparserData *marks_data;
1763 if (strcmp (tagname, "marks") == 0)
1768 marks_data = (MarksSubparserData *)user_data;
1770 for (m = marks_data->marks; m; m = m->next)
1772 MarkData *mdata = m->data;
1774 if (mdata->translatable && mdata->markup->len)
1775 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1777 mdata->markup->str);
1779 markup = mdata->markup->str;
1781 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1783 mark_data_free (mdata);
1786 g_slist_free (marks_data->marks);
1787 g_slice_free (MarksSubparserData, marks_data);