1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 2001 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
33 #include "gdk/gdkkeysyms.h"
35 #include "gtkiconfactory.h"
36 #include "gtkicontheme.h"
37 #include "gtkmarshalers.h"
38 #include "gtkbindings.h"
39 #include "gtkorientable.h"
40 #include "gtkprivate.h"
42 #include "gtkbuildable.h"
43 #include "gtkbuilderprivate.h"
48 * @Short_description: Base class for GtkHScale and GtkVScale
51 * A GtkScale is a slider control used to select a numeric value.
52 * To use it, you'll probably want to investigate the methods on
53 * its base class, #GtkRange, in addition to the methods for GtkScale itself.
54 * To set the value of a scale, you would normally use gtk_range_set_value().
55 * To detect changes to the value, you would normally use the
56 * #GtkRange::value-changed signal.
58 * <refsect2 id="GtkScale-BUILDER-UI"><title>GtkScale as GtkBuildable</title>
59 * GtkScale supports a custom <marks> element, which
60 * can contain multiple <mark> elements. The "value" and "position"
61 * attributes have the same meaning as gtk_scale_add_mark() parameters of the
62 * same name. If the element is not empty, its content is taken as the markup
63 * to show at the mark. It can be translated with the usual "translatable and
64 * "context" attributes.
69 #define MAX_DIGITS (64) /* don't change this,
70 * a) you don't need to and
71 * b) you might cause buffer owerflows in
72 * unrelated code portions otherwise
76 typedef struct _GtkScaleMark GtkScaleMark;
78 struct _GtkScalePrivate
94 GtkPositionType position;
109 static guint signals[LAST_SIGNAL];
111 static void gtk_scale_set_property (GObject *object,
115 static void gtk_scale_get_property (GObject *object,
119 static void gtk_scale_size_request (GtkWidget *widget,
120 GtkRequisition *requisition);
121 static void gtk_scale_style_set (GtkWidget *widget,
123 static void gtk_scale_get_range_border (GtkRange *range,
125 static void gtk_scale_get_mark_label_size (GtkScale *scale,
126 GtkPositionType position,
133 static void gtk_scale_finalize (GObject *object);
134 static void gtk_scale_screen_changed (GtkWidget *widget,
135 GdkScreen *old_screen);
136 static gboolean gtk_scale_draw (GtkWidget *widget,
138 static void gtk_scale_real_get_layout_offsets (GtkScale *scale,
141 static void gtk_scale_buildable_interface_init (GtkBuildableIface *iface);
142 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
145 const gchar *tagname,
146 GMarkupParser *parser,
148 static void gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
151 const gchar *tagname,
155 G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
156 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
157 gtk_scale_buildable_interface_init))
161 single_string_accumulator (GSignalInvocationHint *ihint,
163 const GValue *handler_return,
166 gboolean continue_emission;
169 str = g_value_get_string (handler_return);
170 g_value_set_string (return_accu, str);
171 continue_emission = str == NULL;
173 return continue_emission;
177 #define add_slider_binding(binding_set, keyval, mask, scroll) \
178 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
179 I_("move-slider"), 1, \
180 GTK_TYPE_SCROLL_TYPE, scroll)
183 gtk_scale_class_init (GtkScaleClass *class)
185 GObjectClass *gobject_class;
186 GtkWidgetClass *widget_class;
187 GtkRangeClass *range_class;
188 GtkBindingSet *binding_set;
190 gobject_class = G_OBJECT_CLASS (class);
191 range_class = (GtkRangeClass*) class;
192 widget_class = (GtkWidgetClass*) class;
194 gobject_class->set_property = gtk_scale_set_property;
195 gobject_class->get_property = gtk_scale_get_property;
196 gobject_class->finalize = gtk_scale_finalize;
198 widget_class->style_set = gtk_scale_style_set;
199 widget_class->screen_changed = gtk_scale_screen_changed;
200 widget_class->draw = gtk_scale_draw;
201 widget_class->size_request = gtk_scale_size_request;
203 range_class->slider_detail = "Xscale";
204 range_class->get_range_border = gtk_scale_get_range_border;
206 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
209 * GtkScale::format-value:
210 * @scale: the object which received the signal
211 * @value: the value to format
213 * Signal which allows you to change how the scale value is displayed.
214 * Connect a signal handler which returns an allocated string representing
215 * @value. That string will then be used to display the scale's value.
217 * Here's an example signal handler which displays a value 1.0 as
218 * with "-->1.0<--".
221 * format_value_callback (GtkScale *scale,
224 * return g_strdup_printf ("-->%0.*g<--",
225 * gtk_scale_get_digits (scale), value);
229 * Return value: allocated string representing @value
231 signals[FORMAT_VALUE] =
232 g_signal_new (I_("format-value"),
233 G_TYPE_FROM_CLASS (gobject_class),
235 G_STRUCT_OFFSET (GtkScaleClass, format_value),
236 single_string_accumulator, NULL,
237 _gtk_marshal_STRING__DOUBLE,
241 g_object_class_install_property (gobject_class,
243 g_param_spec_int ("digits",
245 P_("The number of decimal places that are displayed in the value"),
249 GTK_PARAM_READWRITE));
251 g_object_class_install_property (gobject_class,
253 g_param_spec_boolean ("draw-value",
255 P_("Whether the current value is displayed as a string next to the slider"),
257 GTK_PARAM_READWRITE));
259 g_object_class_install_property (gobject_class,
261 g_param_spec_enum ("value-pos",
262 P_("Value Position"),
263 P_("The position in which the current value is displayed"),
264 GTK_TYPE_POSITION_TYPE,
266 GTK_PARAM_READWRITE));
268 gtk_widget_class_install_style_property (widget_class,
269 g_param_spec_int ("slider-length",
271 P_("Length of scale's slider"),
275 GTK_PARAM_READABLE));
277 gtk_widget_class_install_style_property (widget_class,
278 g_param_spec_int ("value-spacing",
280 P_("Space between value text and the slider/trough area"),
284 GTK_PARAM_READABLE));
286 /* All bindings (even arrow keys) are on both h/v scale, because
287 * blind users etc. don't care about scale orientation.
290 binding_set = gtk_binding_set_by_class (class);
292 add_slider_binding (binding_set, GDK_KEY_Left, 0,
293 GTK_SCROLL_STEP_LEFT);
295 add_slider_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
296 GTK_SCROLL_PAGE_LEFT);
298 add_slider_binding (binding_set, GDK_KEY_KP_Left, 0,
299 GTK_SCROLL_STEP_LEFT);
301 add_slider_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
302 GTK_SCROLL_PAGE_LEFT);
304 add_slider_binding (binding_set, GDK_KEY_Right, 0,
305 GTK_SCROLL_STEP_RIGHT);
307 add_slider_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
308 GTK_SCROLL_PAGE_RIGHT);
310 add_slider_binding (binding_set, GDK_KEY_KP_Right, 0,
311 GTK_SCROLL_STEP_RIGHT);
313 add_slider_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
314 GTK_SCROLL_PAGE_RIGHT);
316 add_slider_binding (binding_set, GDK_KEY_Up, 0,
319 add_slider_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
322 add_slider_binding (binding_set, GDK_KEY_KP_Up, 0,
325 add_slider_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
328 add_slider_binding (binding_set, GDK_KEY_Down, 0,
329 GTK_SCROLL_STEP_DOWN);
331 add_slider_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
332 GTK_SCROLL_PAGE_DOWN);
334 add_slider_binding (binding_set, GDK_KEY_KP_Down, 0,
335 GTK_SCROLL_STEP_DOWN);
337 add_slider_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
338 GTK_SCROLL_PAGE_DOWN);
340 add_slider_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
341 GTK_SCROLL_PAGE_LEFT);
343 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
344 GTK_SCROLL_PAGE_LEFT);
346 add_slider_binding (binding_set, GDK_KEY_Page_Up, 0,
349 add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
352 add_slider_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
353 GTK_SCROLL_PAGE_RIGHT);
355 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
356 GTK_SCROLL_PAGE_RIGHT);
358 add_slider_binding (binding_set, GDK_KEY_Page_Down, 0,
359 GTK_SCROLL_PAGE_DOWN);
361 add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
362 GTK_SCROLL_PAGE_DOWN);
364 /* Logical bindings (vs. visual bindings above) */
366 add_slider_binding (binding_set, GDK_KEY_plus, 0,
367 GTK_SCROLL_STEP_FORWARD);
369 add_slider_binding (binding_set, GDK_KEY_minus, 0,
370 GTK_SCROLL_STEP_BACKWARD);
372 add_slider_binding (binding_set, GDK_KEY_plus, GDK_CONTROL_MASK,
373 GTK_SCROLL_PAGE_FORWARD);
375 add_slider_binding (binding_set, GDK_KEY_minus, GDK_CONTROL_MASK,
376 GTK_SCROLL_PAGE_BACKWARD);
379 add_slider_binding (binding_set, GDK_KEY_KP_Add, 0,
380 GTK_SCROLL_STEP_FORWARD);
382 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, 0,
383 GTK_SCROLL_STEP_BACKWARD);
385 add_slider_binding (binding_set, GDK_KEY_KP_Add, GDK_CONTROL_MASK,
386 GTK_SCROLL_PAGE_FORWARD);
388 add_slider_binding (binding_set, GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
389 GTK_SCROLL_PAGE_BACKWARD);
392 add_slider_binding (binding_set, GDK_KEY_Home, 0,
395 add_slider_binding (binding_set, GDK_KEY_KP_Home, 0,
398 add_slider_binding (binding_set, GDK_KEY_End, 0,
401 add_slider_binding (binding_set, GDK_KEY_KP_End, 0,
404 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
408 gtk_scale_orientation_notify (GtkRange *range,
409 const GParamSpec *pspec)
411 GtkOrientation orientation;
413 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
414 gtk_range_set_flippable (range,
415 orientation == GTK_ORIENTATION_HORIZONTAL);
419 gtk_scale_init (GtkScale *scale)
421 GtkScalePrivate *priv;
422 GtkRange *range = GTK_RANGE (scale);
424 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
429 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
431 gtk_range_set_slider_size_fixed (range, TRUE);
433 priv->draw_value = TRUE;
434 priv->value_pos = GTK_POS_TOP;
436 _gtk_range_set_round_digits (range, priv->digits);
438 gtk_scale_orientation_notify (range, NULL);
439 g_signal_connect (scale, "notify::orientation",
440 G_CALLBACK (gtk_scale_orientation_notify),
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: the #GtkAdjustment which sets the range of the scale, or
501 * %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 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
784 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
786 border->top += h1 + value_spacing + slider_width / 2;
788 border->bottom += h2 + value_spacing + slider_width / 2;
792 gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
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_set (GtkWidget *widget,
920 range = GTK_RANGE (widget);
922 gtk_widget_style_get (widget,
923 "slider-length", &slider_length,
926 gtk_range_set_min_slider_size (range, slider_length);
928 _gtk_scale_clear_layout (GTK_SCALE (widget));
930 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_set (widget, previous);
934 gtk_scale_screen_changed (GtkWidget *widget,
935 GdkScreen *old_screen)
937 _gtk_scale_clear_layout (GTK_SCALE (widget));
941 gtk_scale_size_request (GtkWidget *widget,
942 GtkRequisition *requisition)
944 GtkRange *range = GTK_RANGE (widget);
945 gint n1, w1, h1, n2, w2, h2;
948 GTK_WIDGET_CLASS (gtk_scale_parent_class)->size_request (widget, requisition);
950 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
953 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
955 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
957 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
958 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
959 requisition->width = MAX (requisition->width, MAX (w1, w2));
963 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
964 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
965 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
966 requisition->height = MAX (requisition->height, MAX (h1, h2));
971 find_next_pos (GtkWidget *widget,
977 GtkAllocation allocation;
981 for (m = list->next, i = 1; m; m = m->next, i++)
983 GtkScaleMark *mark = m->data;
985 if (match == (mark->position == pos))
989 gtk_widget_get_allocation (widget, &allocation);
990 if (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM)
991 return allocation.width;
993 return allocation.height;
997 gtk_scale_draw (GtkWidget *widget,
1000 GtkScale *scale = GTK_SCALE (widget);
1001 GtkScalePrivate *priv = scale->priv;
1002 GtkRange *range = GTK_RANGE (scale);
1003 GtkStateType state_type;
1012 style = gtk_widget_get_style (widget);
1013 gtk_widget_style_get (widget,
1014 "focus-padding", &focus_padding,
1015 "slider-width", &slider_width,
1016 "value-spacing", &value_spacing,
1019 /* We need to chain up _first_ so the various geometry members of
1020 * GtkRange struct are updated.
1022 GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1024 state_type = GTK_STATE_NORMAL;
1025 if (!gtk_widget_is_sensitive (widget))
1026 state_type = GTK_STATE_INSENSITIVE;
1030 GtkOrientation orientation;
1031 GdkRectangle range_rect;
1033 gint x1, x2, x3, y1, y2, y3;
1034 PangoLayout *layout;
1035 PangoRectangle logical_rect;
1037 gint min_pos_before, min_pos_after;
1038 gint min_pos, max_pos;
1040 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1041 n_marks = _gtk_range_get_stop_positions (range, &marks);
1042 layout = gtk_widget_create_pango_layout (widget, NULL);
1043 gtk_range_get_range_rect (range, &range_rect);
1045 min_pos_before = min_pos_after = 0;
1047 for (m = priv->marks, i = 0; m; m = m->next, i++)
1049 GtkScaleMark *mark = m->data;
1051 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1054 if (mark->position == GTK_POS_TOP)
1057 y2 = y1 - slider_width / 2;
1058 min_pos = min_pos_before;
1059 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1063 y1 = range_rect.y + range_rect.height;
1064 y2 = y1 + slider_width / 2;
1065 min_pos = min_pos_after;
1066 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep;
1069 gtk_paint_vline (style, cr, state_type,
1070 widget, "scale-mark", y1, y2, x1);
1074 pango_layout_set_markup (layout, mark->markup, -1);
1075 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1077 x3 = x1 - logical_rect.width / 2;
1080 if (x3 + logical_rect.width > max_pos)
1081 x3 = max_pos - logical_rect.width;
1084 if (mark->position == GTK_POS_TOP)
1086 y3 = y2 - value_spacing - logical_rect.height;
1087 min_pos_before = x3 + logical_rect.width + min_sep;
1091 y3 = y2 + value_spacing;
1092 min_pos_after = x3 + logical_rect.width + min_sep;
1095 gtk_paint_layout (style, cr, state_type,
1096 FALSE, widget, "scale-mark",
1102 if (mark->position == GTK_POS_LEFT)
1105 x2 = range_rect.x - slider_width / 2;
1106 min_pos = min_pos_before;
1107 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep;
1111 x1 = range_rect.x + range_rect.width;
1112 x2 = range_rect.x + range_rect.width + slider_width / 2;
1113 min_pos = min_pos_after;
1114 max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep;
1118 gtk_paint_hline (style, cr, state_type,
1119 widget, "range-mark", x1, x2, y1);
1123 pango_layout_set_markup (layout, mark->markup, -1);
1124 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1126 y3 = y1 - logical_rect.height / 2;
1129 if (y3 + logical_rect.height > max_pos)
1130 y3 = max_pos - logical_rect.height;
1133 if (mark->position == GTK_POS_LEFT)
1135 x3 = x2 - value_spacing - logical_rect.width;
1136 min_pos_before = y3 + logical_rect.height + min_sep;
1140 x3 = x2 + value_spacing;
1141 min_pos_after = y3 + logical_rect.height + min_sep;
1144 gtk_paint_layout (style, cr, state_type,
1145 FALSE, widget, "scale-mark",
1151 g_object_unref (layout);
1155 if (priv->draw_value)
1157 GtkOrientation orientation;
1158 GtkAllocation allocation;
1160 PangoLayout *layout;
1163 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1164 layout = gtk_scale_get_layout (scale);
1165 gtk_scale_get_layout_offsets (scale, &x, &y);
1166 gtk_widget_get_allocation (widget, &allocation);
1168 gtk_paint_layout (style,
1173 orientation == GTK_ORIENTATION_HORIZONTAL ?
1174 "hscale" : "vscale",
1185 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1189 GtkScalePrivate *priv = scale->priv;
1190 GtkAllocation allocation;
1191 GtkWidget *widget = GTK_WIDGET (scale);
1192 GtkRange *range = GTK_RANGE (widget);
1193 GdkRectangle range_rect;
1194 PangoLayout *layout = gtk_scale_get_layout (scale);
1195 PangoRectangle logical_rect;
1196 gint slider_start, slider_end;
1207 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1209 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1211 gtk_widget_get_allocation (widget, &allocation);
1212 gtk_range_get_range_rect (range, &range_rect);
1213 gtk_range_get_slider_range (range,
1217 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1219 switch (priv->value_pos)
1222 *x = range_rect.x - value_spacing - logical_rect.width;
1223 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1227 *x = range_rect.x + range_rect.width + value_spacing;
1228 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1232 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1233 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1234 *y = range_rect.y - logical_rect.height - value_spacing;
1237 case GTK_POS_BOTTOM:
1238 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1239 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1240 *y = range_rect.y + range_rect.height + value_spacing;
1244 g_return_if_reached ();
1250 switch (priv->value_pos)
1253 *x = range_rect.x - logical_rect.width - value_spacing;
1254 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1255 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1259 *x = range_rect.x + range_rect.width + value_spacing;
1260 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1261 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1265 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1266 *y = range_rect.y - logical_rect.height - value_spacing;
1269 case GTK_POS_BOTTOM:
1270 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1271 *y = range_rect.y + range_rect.height + value_spacing;
1275 g_return_if_reached ();
1284 * _gtk_scale_format_value:
1285 * @scale: a #GtkScale
1286 * @value: adjustment value
1288 * Emits #GtkScale::format-value signal to format the value,
1289 * if no user signal handlers, falls back to a default format.
1291 * Return value: formatted value
1294 _gtk_scale_format_value (GtkScale *scale,
1297 GtkScalePrivate *priv = scale->priv;
1300 g_signal_emit (scale,
1301 signals[FORMAT_VALUE],
1309 /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1310 return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value);
1314 gtk_scale_finalize (GObject *object)
1316 GtkScale *scale = GTK_SCALE (object);
1318 _gtk_scale_clear_layout (scale);
1319 gtk_scale_clear_marks (scale);
1321 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1325 * gtk_scale_get_layout:
1326 * @scale: A #GtkScale
1328 * Gets the #PangoLayout used to display the scale. The returned
1329 * object is owned by the scale so does not need to be freed by
1332 * Return value: (transfer none): the #PangoLayout for this scale,
1333 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1338 gtk_scale_get_layout (GtkScale *scale)
1340 GtkScalePrivate *priv;
1343 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1349 if (priv->draw_value)
1350 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1353 if (priv->draw_value)
1355 txt = _gtk_scale_format_value (scale,
1356 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1357 pango_layout_set_text (priv->layout, txt, -1);
1361 return priv->layout;
1365 * gtk_scale_get_layout_offsets:
1366 * @scale: a #GtkScale
1367 * @x: (allow-none): location to store X offset of layout, or %NULL
1368 * @y: (allow-none): location to store Y offset of layout, or %NULL
1370 * Obtains the coordinates where the scale will draw the
1371 * #PangoLayout representing the text in the scale. Remember
1372 * when using the #PangoLayout function you need to convert to
1373 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1375 * If the #GtkScale:draw-value property is %FALSE, the return
1376 * values are undefined.
1381 gtk_scale_get_layout_offsets (GtkScale *scale,
1388 g_return_if_fail (GTK_IS_SCALE (scale));
1390 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1391 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1401 _gtk_scale_clear_layout (GtkScale *scale)
1403 GtkScalePrivate *priv = scale->priv;
1405 g_return_if_fail (GTK_IS_SCALE (scale));
1409 g_object_unref (priv->layout);
1410 priv->layout = NULL;
1415 gtk_scale_mark_free (GtkScaleMark *mark)
1417 g_free (mark->markup);
1422 * gtk_scale_clear_marks:
1423 * @scale: a #GtkScale
1425 * Removes any marks that have been added with gtk_scale_add_mark().
1430 gtk_scale_clear_marks (GtkScale *scale)
1432 GtkScalePrivate *priv;
1434 g_return_if_fail (GTK_IS_SCALE (scale));
1438 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1439 g_slist_free (priv->marks);
1442 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1444 gtk_widget_queue_resize (GTK_WIDGET (scale));
1448 compare_marks (gpointer a, gpointer b)
1450 GtkScaleMark *ma, *mb;
1454 return (gint) (ma->value - mb->value);
1458 * gtk_scale_add_mark:
1459 * @scale: a #GtkScale
1460 * @value: the value at which the mark is placed, must be between
1461 * the lower and upper limits of the scales' adjustment
1462 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1463 * is drawn above the scale, anything else below. For a vertical scale,
1464 * #GTK_POS_LEFT is drawn to the left of the scale, anything else to the
1466 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1469 * Adds a mark at @value.
1471 * A mark is indicated visually by drawing a tick mark next to the scale,
1472 * and GTK+ makes it easy for the user to position the scale exactly at the
1475 * If @markup is not %NULL, text is shown next to the tick mark.
1477 * To remove marks from a scale, use gtk_scale_clear_marks().
1482 gtk_scale_add_mark (GtkScale *scale,
1484 GtkPositionType position,
1485 const gchar *markup)
1487 GtkScalePrivate *priv;
1493 g_return_if_fail (GTK_IS_SCALE (scale));
1497 mark = g_new (GtkScaleMark, 1);
1498 mark->value = value;
1499 mark->markup = g_strdup (markup);
1500 mark->position = position;
1502 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1503 (GCompareFunc) compare_marks);
1505 n = g_slist_length (priv->marks);
1506 values = g_new (gdouble, n);
1507 for (m = priv->marks, i = 0; m; m = m->next, i++)
1510 values[i] = mark->value;
1513 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1517 gtk_widget_queue_resize (GTK_WIDGET (scale));
1520 static GtkBuildableIface *parent_buildable_iface;
1523 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1525 parent_buildable_iface = g_type_interface_peek_parent (iface);
1526 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1527 iface->custom_finished = gtk_scale_buildable_custom_finished;
1533 GtkBuilder *builder;
1535 } MarksSubparserData;
1540 GtkPositionType position;
1543 gboolean translatable;
1547 mark_data_free (MarkData *data)
1549 g_string_free (data->markup, TRUE);
1550 g_free (data->context);
1551 g_slice_free (MarkData, data);
1555 marks_start_element (GMarkupParseContext *context,
1556 const gchar *element_name,
1557 const gchar **names,
1558 const gchar **values,
1562 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1564 gint line_number, char_number;
1566 if (strcmp (element_name, "marks") == 0)
1568 else if (strcmp (element_name, "mark") == 0)
1571 gboolean has_value = FALSE;
1572 GtkPositionType position = GTK_POS_BOTTOM;
1573 const gchar *msg_context = NULL;
1574 gboolean translatable = FALSE;
1577 for (i = 0; names[i]; i++)
1579 if (strcmp (names[i], "translatable") == 0)
1581 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1584 else if (strcmp (names[i], "comments") == 0)
1586 /* do nothing, comments are for translators */
1588 else if (strcmp (names[i], "context") == 0)
1589 msg_context = values[i];
1590 else if (strcmp (names[i], "value") == 0)
1592 GValue gvalue = { 0, };
1594 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1597 value = g_value_get_double (&gvalue);
1600 else if (strcmp (names[i], "position") == 0)
1602 GValue gvalue = { 0, };
1604 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1607 position = g_value_get_enum (&gvalue);
1611 g_markup_parse_context_get_position (context,
1616 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1617 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1619 line_number, char_number, names[i], "mark");
1626 g_markup_parse_context_get_position (context,
1631 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1632 "%s:%d:%d <%s> requires attribute \"%s\"",
1634 line_number, char_number, "mark",
1639 mark = g_slice_new (MarkData);
1640 mark->value = value;
1641 mark->position = position;
1642 mark->markup = g_string_new ("");
1643 mark->context = g_strdup (msg_context);
1644 mark->translatable = translatable;
1646 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1650 g_markup_parse_context_get_position (context,
1655 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1656 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1658 line_number, char_number, element_name);
1664 marks_text (GMarkupParseContext *context,
1670 MarksSubparserData *data = (MarksSubparserData*)user_data;
1672 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1674 MarkData *mark = data->marks->data;
1676 g_string_append_len (mark->markup, text, text_len);
1680 static const GMarkupParser marks_parser =
1682 marks_start_element,
1689 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1690 GtkBuilder *builder,
1692 const gchar *tagname,
1693 GMarkupParser *parser,
1696 MarksSubparserData *parser_data;
1701 if (strcmp (tagname, "marks") == 0)
1703 parser_data = g_slice_new0 (MarksSubparserData);
1704 parser_data->scale = GTK_SCALE (buildable);
1705 parser_data->marks = NULL;
1707 *parser = marks_parser;
1708 *data = parser_data;
1712 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1713 tagname, parser, data);
1717 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1718 GtkBuilder *builder,
1720 const gchar *tagname,
1723 GtkScale *scale = GTK_SCALE (buildable);
1724 MarksSubparserData *marks_data;
1726 if (strcmp (tagname, "marks") == 0)
1731 marks_data = (MarksSubparserData *)user_data;
1733 for (m = marks_data->marks; m; m = m->next)
1735 MarkData *mdata = m->data;
1737 if (mdata->translatable && mdata->markup->len)
1738 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1740 mdata->markup->str);
1742 markup = mdata->markup->str;
1744 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1746 mark_data_free (mdata);
1749 g_slist_free (marks_data->marks);
1750 g_slice_free (MarksSubparserData, marks_data);