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_expose (GtkWidget *widget,
137 GdkEventExpose *event);
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->expose_event = gtk_scale_expose;
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_expose (GtkWidget *widget,
998 GdkEventExpose *event)
1000 GtkScale *scale = GTK_SCALE (widget);
1001 GtkScalePrivate *priv = scale->priv;
1002 GtkRange *range = GTK_RANGE (scale);
1003 GtkStateType state_type;
1013 style = gtk_widget_get_style (widget);
1014 gtk_widget_style_get (widget,
1015 "focus-padding", &focus_padding,
1016 "slider-width", &slider_width,
1017 "value-spacing", &value_spacing,
1020 /* We need to chain up _first_ so the various geometry members of
1021 * GtkRange struct are updated.
1023 GTK_WIDGET_CLASS (gtk_scale_parent_class)->expose_event (widget, event);
1025 window = gtk_widget_get_window (widget);
1027 state_type = GTK_STATE_NORMAL;
1028 if (!gtk_widget_is_sensitive (widget))
1029 state_type = GTK_STATE_INSENSITIVE;
1033 GtkAllocation allocation;
1034 GtkOrientation orientation;
1035 GdkRectangle range_rect;
1037 gint x1, x2, x3, y1, y2, y3;
1038 PangoLayout *layout;
1039 PangoRectangle logical_rect;
1041 gint min_pos_before, min_pos_after;
1042 gint min_pos, max_pos;
1044 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1045 n_marks = _gtk_range_get_stop_positions (range, &marks);
1046 layout = gtk_widget_create_pango_layout (widget, NULL);
1047 gtk_widget_get_allocation (widget, &allocation);
1048 gtk_range_get_range_rect (range, &range_rect);
1050 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1051 min_pos_before = min_pos_after = allocation.x;
1053 min_pos_before = min_pos_after = allocation.y;
1054 for (m = priv->marks, i = 0; m; m = m->next, i++)
1056 GtkScaleMark *mark = m->data;
1058 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1060 x1 = allocation.x + marks[i];
1061 if (mark->position == GTK_POS_TOP)
1063 y1 = allocation.y + range_rect.y;
1064 y2 = y1 - slider_width / 2;
1065 min_pos = min_pos_before;
1066 max_pos = allocation.x + find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1070 y1 = allocation.y + range_rect.y + range_rect.height;
1071 y2 = y1 + slider_width / 2;
1072 min_pos = min_pos_after;
1073 max_pos = allocation.x + find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep;
1076 gtk_paint_vline (style, window, state_type,
1077 NULL, widget, "scale-mark", y1, y2, x1);
1081 pango_layout_set_markup (layout, mark->markup, -1);
1082 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1084 x3 = x1 - logical_rect.width / 2;
1087 if (x3 + logical_rect.width > max_pos)
1088 x3 = max_pos - logical_rect.width;
1089 if (x3 < allocation.x)
1091 if (mark->position == GTK_POS_TOP)
1093 y3 = y2 - value_spacing - logical_rect.height;
1094 min_pos_before = x3 + logical_rect.width + min_sep;
1098 y3 = y2 + value_spacing;
1099 min_pos_after = x3 + logical_rect.width + min_sep;
1102 gtk_paint_layout (style, window, state_type,
1103 FALSE, NULL, widget, "scale-mark",
1109 if (mark->position == GTK_POS_LEFT)
1111 x1 = allocation.x + range_rect.x;
1112 x2 = allocation.x + range_rect.x - slider_width / 2;
1113 min_pos = min_pos_before;
1114 max_pos = allocation.y + find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep;
1118 x1 = allocation.x + range_rect.x + range_rect.width;
1119 x2 = allocation.x + range_rect.x + range_rect.width + slider_width / 2;
1120 min_pos = min_pos_after;
1121 max_pos = allocation.y + find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep;
1123 y1 = allocation.y + marks[i];
1125 gtk_paint_hline (style, window, state_type,
1126 NULL, widget, "range-mark", x1, x2, y1);
1130 pango_layout_set_markup (layout, mark->markup, -1);
1131 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1133 y3 = y1 - logical_rect.height / 2;
1136 if (y3 + logical_rect.height > max_pos)
1137 y3 = max_pos - logical_rect.height;
1138 if (y3 < allocation.y)
1140 if (mark->position == GTK_POS_LEFT)
1142 x3 = x2 - value_spacing - logical_rect.width;
1143 min_pos_before = y3 + logical_rect.height + min_sep;
1147 x3 = x2 + value_spacing;
1148 min_pos_after = y3 + logical_rect.height + min_sep;
1151 gtk_paint_layout (style, window, state_type,
1152 FALSE, NULL, widget, "scale-mark",
1158 g_object_unref (layout);
1162 if (priv->draw_value)
1164 GtkOrientation orientation;
1165 PangoLayout *layout;
1168 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1169 layout = gtk_scale_get_layout (scale);
1170 gtk_scale_get_layout_offsets (scale, &x, &y);
1172 gtk_paint_layout (style,
1178 orientation == GTK_ORIENTATION_HORIZONTAL ?
1179 "hscale" : "vscale",
1189 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1193 GtkScalePrivate *priv = scale->priv;
1194 GtkAllocation allocation;
1195 GtkWidget *widget = GTK_WIDGET (scale);
1196 GtkRange *range = GTK_RANGE (widget);
1197 GdkRectangle range_rect;
1198 PangoLayout *layout = gtk_scale_get_layout (scale);
1199 PangoRectangle logical_rect;
1200 gint slider_start, slider_end;
1211 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1213 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1215 gtk_widget_get_allocation (widget, &allocation);
1216 gtk_range_get_range_rect (range, &range_rect);
1217 gtk_range_get_slider_range (range,
1221 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1223 switch (priv->value_pos)
1226 *x = range_rect.x - value_spacing - logical_rect.width;
1227 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1231 *x = range_rect.x + range_rect.width + value_spacing;
1232 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1236 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1237 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1238 *y = range_rect.y - logical_rect.height - value_spacing;
1241 case GTK_POS_BOTTOM:
1242 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1243 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1244 *y = range_rect.y + range_rect.height + value_spacing;
1248 g_return_if_reached ();
1254 switch (priv->value_pos)
1257 *x = range_rect.x - logical_rect.width - value_spacing;
1258 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1259 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1263 *x = range_rect.x + range_rect.width + value_spacing;
1264 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1265 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1269 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1270 *y = range_rect.y - logical_rect.height - value_spacing;
1273 case GTK_POS_BOTTOM:
1274 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1275 *y = range_rect.y + range_rect.height + value_spacing;
1279 g_return_if_reached ();
1288 * _gtk_scale_format_value:
1289 * @scale: a #GtkScale
1290 * @value: adjustment value
1292 * Emits #GtkScale::format-value signal to format the value,
1293 * if no user signal handlers, falls back to a default format.
1295 * Return value: formatted value
1298 _gtk_scale_format_value (GtkScale *scale,
1301 GtkScalePrivate *priv = scale->priv;
1304 g_signal_emit (scale,
1305 signals[FORMAT_VALUE],
1313 /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1314 return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value);
1318 gtk_scale_finalize (GObject *object)
1320 GtkScale *scale = GTK_SCALE (object);
1322 _gtk_scale_clear_layout (scale);
1323 gtk_scale_clear_marks (scale);
1325 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1329 * gtk_scale_get_layout:
1330 * @scale: A #GtkScale
1332 * Gets the #PangoLayout used to display the scale. The returned
1333 * object is owned by the scale so does not need to be freed by
1336 * Return value: (transfer none): the #PangoLayout for this scale,
1337 * or %NULL if the #GtkScale:draw-value property is %FALSE.
1342 gtk_scale_get_layout (GtkScale *scale)
1344 GtkScalePrivate *priv;
1347 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1353 if (priv->draw_value)
1354 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1357 if (priv->draw_value)
1359 txt = _gtk_scale_format_value (scale,
1360 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1361 pango_layout_set_text (priv->layout, txt, -1);
1365 return priv->layout;
1369 * gtk_scale_get_layout_offsets:
1370 * @scale: a #GtkScale
1371 * @x: (allow-none): location to store X offset of layout, or %NULL
1372 * @y: (allow-none): location to store Y offset of layout, or %NULL
1374 * Obtains the coordinates where the scale will draw the
1375 * #PangoLayout representing the text in the scale. Remember
1376 * when using the #PangoLayout function you need to convert to
1377 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1379 * If the #GtkScale:draw-value property is %FALSE, the return
1380 * values are undefined.
1385 gtk_scale_get_layout_offsets (GtkScale *scale,
1392 g_return_if_fail (GTK_IS_SCALE (scale));
1394 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1395 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1405 _gtk_scale_clear_layout (GtkScale *scale)
1407 GtkScalePrivate *priv = scale->priv;
1409 g_return_if_fail (GTK_IS_SCALE (scale));
1413 g_object_unref (priv->layout);
1414 priv->layout = NULL;
1419 gtk_scale_mark_free (GtkScaleMark *mark)
1421 g_free (mark->markup);
1426 * gtk_scale_clear_marks:
1427 * @scale: a #GtkScale
1429 * Removes any marks that have been added with gtk_scale_add_mark().
1434 gtk_scale_clear_marks (GtkScale *scale)
1436 GtkScalePrivate *priv;
1438 g_return_if_fail (GTK_IS_SCALE (scale));
1442 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1443 g_slist_free (priv->marks);
1446 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1448 gtk_widget_queue_resize (GTK_WIDGET (scale));
1452 compare_marks (gpointer a, gpointer b)
1454 GtkScaleMark *ma, *mb;
1458 return (gint) (ma->value - mb->value);
1462 * gtk_scale_add_mark:
1463 * @scale: a #GtkScale
1464 * @value: the value at which the mark is placed, must be between
1465 * the lower and upper limits of the scales' adjustment
1466 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1467 * is drawn above the scale, anything else below. For a vertical scale,
1468 * #GTK_POS_LEFT is drawn to the left of the scale, anything else to the
1470 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1473 * Adds a mark at @value.
1475 * A mark is indicated visually by drawing a tick mark next to the scale,
1476 * and GTK+ makes it easy for the user to position the scale exactly at the
1479 * If @markup is not %NULL, text is shown next to the tick mark.
1481 * To remove marks from a scale, use gtk_scale_clear_marks().
1486 gtk_scale_add_mark (GtkScale *scale,
1488 GtkPositionType position,
1489 const gchar *markup)
1491 GtkScalePrivate *priv;
1497 g_return_if_fail (GTK_IS_SCALE (scale));
1501 mark = g_new (GtkScaleMark, 1);
1502 mark->value = value;
1503 mark->markup = g_strdup (markup);
1504 mark->position = position;
1506 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1507 (GCompareFunc) compare_marks);
1509 n = g_slist_length (priv->marks);
1510 values = g_new (gdouble, n);
1511 for (m = priv->marks, i = 0; m; m = m->next, i++)
1514 values[i] = mark->value;
1517 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1521 gtk_widget_queue_resize (GTK_WIDGET (scale));
1524 static GtkBuildableIface *parent_buildable_iface;
1527 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1529 parent_buildable_iface = g_type_interface_peek_parent (iface);
1530 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1531 iface->custom_finished = gtk_scale_buildable_custom_finished;
1537 GtkBuilder *builder;
1539 } MarksSubparserData;
1544 GtkPositionType position;
1547 gboolean translatable;
1551 mark_data_free (MarkData *data)
1553 g_string_free (data->markup, TRUE);
1554 g_free (data->context);
1555 g_slice_free (MarkData, data);
1559 marks_start_element (GMarkupParseContext *context,
1560 const gchar *element_name,
1561 const gchar **names,
1562 const gchar **values,
1566 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1568 gint line_number, char_number;
1570 if (strcmp (element_name, "marks") == 0)
1572 else if (strcmp (element_name, "mark") == 0)
1575 gboolean has_value = FALSE;
1576 GtkPositionType position = GTK_POS_BOTTOM;
1577 const gchar *msg_context = NULL;
1578 gboolean translatable = FALSE;
1581 for (i = 0; names[i]; i++)
1583 if (strcmp (names[i], "translatable") == 0)
1585 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1588 else if (strcmp (names[i], "comments") == 0)
1590 /* do nothing, comments are for translators */
1592 else if (strcmp (names[i], "context") == 0)
1593 msg_context = values[i];
1594 else if (strcmp (names[i], "value") == 0)
1596 GValue gvalue = { 0, };
1598 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1601 value = g_value_get_double (&gvalue);
1604 else if (strcmp (names[i], "position") == 0)
1606 GValue gvalue = { 0, };
1608 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1611 position = g_value_get_enum (&gvalue);
1615 g_markup_parse_context_get_position (context,
1620 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1621 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1623 line_number, char_number, names[i], "mark");
1630 g_markup_parse_context_get_position (context,
1635 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1636 "%s:%d:%d <%s> requires attribute \"%s\"",
1638 line_number, char_number, "mark",
1643 mark = g_slice_new (MarkData);
1644 mark->value = value;
1645 mark->position = position;
1646 mark->markup = g_string_new ("");
1647 mark->context = g_strdup (msg_context);
1648 mark->translatable = translatable;
1650 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1654 g_markup_parse_context_get_position (context,
1659 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1660 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1662 line_number, char_number, element_name);
1668 marks_text (GMarkupParseContext *context,
1674 MarksSubparserData *data = (MarksSubparserData*)user_data;
1676 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1678 MarkData *mark = data->marks->data;
1680 g_string_append_len (mark->markup, text, text_len);
1684 static const GMarkupParser marks_parser =
1686 marks_start_element,
1693 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1694 GtkBuilder *builder,
1696 const gchar *tagname,
1697 GMarkupParser *parser,
1700 MarksSubparserData *parser_data;
1705 if (strcmp (tagname, "marks") == 0)
1707 parser_data = g_slice_new0 (MarksSubparserData);
1708 parser_data->scale = GTK_SCALE (buildable);
1709 parser_data->marks = NULL;
1711 *parser = marks_parser;
1712 *data = parser_data;
1716 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1717 tagname, parser, data);
1721 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1722 GtkBuilder *builder,
1724 const gchar *tagname,
1727 GtkScale *scale = GTK_SCALE (buildable);
1728 MarksSubparserData *marks_data;
1730 if (strcmp (tagname, "marks") == 0)
1735 marks_data = (MarksSubparserData *)user_data;
1737 for (m = marks_data->marks; m; m = m->next)
1739 MarkData *mdata = m->data;
1741 if (mdata->translatable && mdata->markup->len)
1742 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1744 mdata->markup->str);
1746 markup = mdata->markup->str;
1748 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1750 mark_data_free (mdata);
1753 g_slist_free (marks_data->marks);
1754 g_slice_free (MarksSubparserData, marks_data);