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 "gtkprivate.h"
41 #include "gtkbuildable.h"
42 #include "gtkbuilderprivate.h"
44 #define MAX_DIGITS (64) /* don't change this,
45 * a) you don't need to and
46 * b) you might cause buffer owerflows in
47 * unrelated code portions otherwise
50 #define GTK_SCALE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SCALE, GtkScalePrivate))
52 typedef struct _GtkScalePrivate GtkScalePrivate;
54 typedef struct _GtkScaleMark GtkScaleMark;
60 GtkPositionType position;
63 struct _GtkScalePrivate
81 static guint signals[LAST_SIGNAL];
83 static void gtk_scale_set_property (GObject *object,
87 static void gtk_scale_get_property (GObject *object,
91 static void gtk_scale_size_request (GtkWidget *widget,
92 GtkRequisition *requisition);
93 static void gtk_scale_style_set (GtkWidget *widget,
95 static void gtk_scale_get_range_border (GtkRange *range,
97 static void gtk_scale_get_mark_label_size (GtkScale *scale,
98 GtkPositionType position,
105 static void gtk_scale_finalize (GObject *object);
106 static void gtk_scale_screen_changed (GtkWidget *widget,
107 GdkScreen *old_screen);
108 static gboolean gtk_scale_expose (GtkWidget *widget,
109 GdkEventExpose *event);
110 static void gtk_scale_real_get_layout_offsets (GtkScale *scale,
113 static void gtk_scale_buildable_interface_init (GtkBuildableIface *iface);
114 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
117 const gchar *tagname,
118 GMarkupParser *parser,
120 static void gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
123 const gchar *tagname,
127 G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
128 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
129 gtk_scale_buildable_interface_init))
133 single_string_accumulator (GSignalInvocationHint *ihint,
135 const GValue *handler_return,
138 gboolean continue_emission;
141 str = g_value_get_string (handler_return);
142 g_value_set_string (return_accu, str);
143 continue_emission = str == NULL;
145 return continue_emission;
149 #define add_slider_binding(binding_set, keyval, mask, scroll) \
150 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
151 I_("move-slider"), 1, \
152 GTK_TYPE_SCROLL_TYPE, scroll)
155 gtk_scale_class_init (GtkScaleClass *class)
157 GObjectClass *gobject_class;
158 GtkWidgetClass *widget_class;
159 GtkRangeClass *range_class;
160 GtkBindingSet *binding_set;
162 gobject_class = G_OBJECT_CLASS (class);
163 range_class = (GtkRangeClass*) class;
164 widget_class = (GtkWidgetClass*) class;
166 gobject_class->set_property = gtk_scale_set_property;
167 gobject_class->get_property = gtk_scale_get_property;
168 gobject_class->finalize = gtk_scale_finalize;
170 widget_class->style_set = gtk_scale_style_set;
171 widget_class->screen_changed = gtk_scale_screen_changed;
172 widget_class->expose_event = gtk_scale_expose;
173 widget_class->size_request = gtk_scale_size_request;
175 range_class->slider_detail = "Xscale";
176 range_class->get_range_border = gtk_scale_get_range_border;
178 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
181 * GtkScale::format-value:
182 * @scale: the object which received the signal
183 * @value: the value to format
185 * Signal which allows you to change how the scale value is displayed.
186 * Connect a signal handler which returns an allocated string representing
187 * @value. That string will then be used to display the scale's value.
189 * Here's an example signal handler which displays a value 1.0 as
190 * with "-->1.0<--".
193 * format_value_callback (GtkScale *scale,
196 * return g_strdup_printf ("-->%0.*g<--",
197 * gtk_scale_get_digits (scale), value);
201 * Return value: allocated string representing @value
203 signals[FORMAT_VALUE] =
204 g_signal_new (I_("format-value"),
205 G_TYPE_FROM_CLASS (gobject_class),
207 G_STRUCT_OFFSET (GtkScaleClass, format_value),
208 single_string_accumulator, NULL,
209 _gtk_marshal_STRING__DOUBLE,
213 g_object_class_install_property (gobject_class,
215 g_param_spec_int ("digits",
217 P_("The number of decimal places that are displayed in the value"),
221 GTK_PARAM_READWRITE));
223 g_object_class_install_property (gobject_class,
225 g_param_spec_boolean ("draw-value",
227 P_("Whether the current value is displayed as a string next to the slider"),
229 GTK_PARAM_READWRITE));
231 g_object_class_install_property (gobject_class,
233 g_param_spec_enum ("value-pos",
234 P_("Value Position"),
235 P_("The position in which the current value is displayed"),
236 GTK_TYPE_POSITION_TYPE,
238 GTK_PARAM_READWRITE));
240 gtk_widget_class_install_style_property (widget_class,
241 g_param_spec_int ("slider-length",
243 P_("Length of scale's slider"),
247 GTK_PARAM_READABLE));
249 gtk_widget_class_install_style_property (widget_class,
250 g_param_spec_int ("value-spacing",
252 P_("Space between value text and the slider/trough area"),
256 GTK_PARAM_READABLE));
258 /* All bindings (even arrow keys) are on both h/v scale, because
259 * blind users etc. don't care about scale orientation.
262 binding_set = gtk_binding_set_by_class (class);
264 add_slider_binding (binding_set, GDK_Left, 0,
265 GTK_SCROLL_STEP_LEFT);
267 add_slider_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
268 GTK_SCROLL_PAGE_LEFT);
270 add_slider_binding (binding_set, GDK_KP_Left, 0,
271 GTK_SCROLL_STEP_LEFT);
273 add_slider_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
274 GTK_SCROLL_PAGE_LEFT);
276 add_slider_binding (binding_set, GDK_Right, 0,
277 GTK_SCROLL_STEP_RIGHT);
279 add_slider_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
280 GTK_SCROLL_PAGE_RIGHT);
282 add_slider_binding (binding_set, GDK_KP_Right, 0,
283 GTK_SCROLL_STEP_RIGHT);
285 add_slider_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
286 GTK_SCROLL_PAGE_RIGHT);
288 add_slider_binding (binding_set, GDK_Up, 0,
291 add_slider_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
294 add_slider_binding (binding_set, GDK_KP_Up, 0,
297 add_slider_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
300 add_slider_binding (binding_set, GDK_Down, 0,
301 GTK_SCROLL_STEP_DOWN);
303 add_slider_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
304 GTK_SCROLL_PAGE_DOWN);
306 add_slider_binding (binding_set, GDK_KP_Down, 0,
307 GTK_SCROLL_STEP_DOWN);
309 add_slider_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
310 GTK_SCROLL_PAGE_DOWN);
312 add_slider_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
313 GTK_SCROLL_PAGE_LEFT);
315 add_slider_binding (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
316 GTK_SCROLL_PAGE_LEFT);
318 add_slider_binding (binding_set, GDK_Page_Up, 0,
321 add_slider_binding (binding_set, GDK_KP_Page_Up, 0,
324 add_slider_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
325 GTK_SCROLL_PAGE_RIGHT);
327 add_slider_binding (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
328 GTK_SCROLL_PAGE_RIGHT);
330 add_slider_binding (binding_set, GDK_Page_Down, 0,
331 GTK_SCROLL_PAGE_DOWN);
333 add_slider_binding (binding_set, GDK_KP_Page_Down, 0,
334 GTK_SCROLL_PAGE_DOWN);
336 /* Logical bindings (vs. visual bindings above) */
338 add_slider_binding (binding_set, GDK_plus, 0,
339 GTK_SCROLL_STEP_FORWARD);
341 add_slider_binding (binding_set, GDK_minus, 0,
342 GTK_SCROLL_STEP_BACKWARD);
344 add_slider_binding (binding_set, GDK_plus, GDK_CONTROL_MASK,
345 GTK_SCROLL_PAGE_FORWARD);
347 add_slider_binding (binding_set, GDK_minus, GDK_CONTROL_MASK,
348 GTK_SCROLL_PAGE_BACKWARD);
351 add_slider_binding (binding_set, GDK_KP_Add, 0,
352 GTK_SCROLL_STEP_FORWARD);
354 add_slider_binding (binding_set, GDK_KP_Subtract, 0,
355 GTK_SCROLL_STEP_BACKWARD);
357 add_slider_binding (binding_set, GDK_KP_Add, GDK_CONTROL_MASK,
358 GTK_SCROLL_PAGE_FORWARD);
360 add_slider_binding (binding_set, GDK_KP_Subtract, GDK_CONTROL_MASK,
361 GTK_SCROLL_PAGE_BACKWARD);
364 add_slider_binding (binding_set, GDK_Home, 0,
367 add_slider_binding (binding_set, GDK_KP_Home, 0,
370 add_slider_binding (binding_set, GDK_End, 0,
373 add_slider_binding (binding_set, GDK_KP_End, 0,
376 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
380 gtk_scale_orientation_notify (GtkRange *range,
381 const GParamSpec *pspec)
383 range->flippable = (range->orientation == GTK_ORIENTATION_HORIZONTAL);
387 gtk_scale_init (GtkScale *scale)
389 GtkRange *range = GTK_RANGE (scale);
391 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
393 range->slider_size_fixed = TRUE;
394 range->has_stepper_a = FALSE;
395 range->has_stepper_b = FALSE;
396 range->has_stepper_c = FALSE;
397 range->has_stepper_d = FALSE;
399 scale->draw_value = TRUE;
400 scale->value_pos = GTK_POS_TOP;
402 range->round_digits = scale->digits;
404 gtk_scale_orientation_notify (range, NULL);
405 g_signal_connect (scale, "notify::orientation",
406 G_CALLBACK (gtk_scale_orientation_notify),
411 gtk_scale_set_property (GObject *object,
418 scale = GTK_SCALE (object);
423 gtk_scale_set_digits (scale, g_value_get_int (value));
425 case PROP_DRAW_VALUE:
426 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
429 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
432 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
438 gtk_scale_get_property (GObject *object,
445 scale = GTK_SCALE (object);
450 g_value_set_int (value, scale->digits);
452 case PROP_DRAW_VALUE:
453 g_value_set_boolean (value, scale->draw_value);
456 g_value_set_enum (value, scale->value_pos);
459 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
466 * @orientation: the scale's orientation.
467 * @adjustment: the #GtkAdjustment which sets the range of the scale, or
468 * %NULL to create a new adjustment.
470 * Creates a new #GtkScale.
472 * Return value: a new #GtkScale
477 gtk_scale_new (GtkOrientation orientation,
478 GtkAdjustment *adjustment)
480 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
483 return g_object_new (GTK_TYPE_SCALE,
484 "orientation", orientation,
485 "adjustment", adjustment,
490 * gtk_scale_new_with_range:
491 * @orientation: the scale's orientation.
492 * @min: minimum value
493 * @max: maximum value
494 * @step: step increment (tick size) used with keyboard shortcuts
496 * Creates a new scale widget with the given orientation that lets the
497 * user input a number between @min and @max (including @min and @max)
498 * with the increment @step. @step must be nonzero; it's the distance
499 * the slider moves when using the arrow keys to adjust the scale
502 * Note that the way in which the precision is derived works best if @step
503 * is a power of ten. If the resulting precision is not suitable for your
504 * needs, use gtk_scale_set_digits() to correct it.
506 * Return value: a new #GtkScale
511 gtk_scale_new_with_range (GtkOrientation orientation,
519 g_return_val_if_fail (min < max, NULL);
520 g_return_val_if_fail (step != 0.0, NULL);
522 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
524 if (fabs (step) >= 1.0 || step == 0.0)
530 digits = abs ((gint) floor (log10 (fabs (step))));
535 return g_object_new (GTK_TYPE_SCALE,
536 "orientation", orientation,
543 * gtk_scale_set_digits:
544 * @scale: a #GtkScale
545 * @digits: the number of decimal places to display,
546 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
548 * Sets the number of decimal places that are displayed in the value.
549 * Also causes the value of the adjustment to be rounded off to this
550 * number of digits, so the retrieved value matches the value the user saw.
553 gtk_scale_set_digits (GtkScale *scale,
558 g_return_if_fail (GTK_IS_SCALE (scale));
560 range = GTK_RANGE (scale);
562 digits = CLAMP (digits, -1, MAX_DIGITS);
564 if (scale->digits != digits)
566 scale->digits = digits;
567 if (scale->draw_value)
568 range->round_digits = digits;
570 _gtk_scale_clear_layout (scale);
571 gtk_widget_queue_resize (GTK_WIDGET (scale));
573 g_object_notify (G_OBJECT (scale), "digits");
578 * gtk_scale_get_digits:
579 * @scale: a #GtkScale
581 * Gets the number of decimal places that are displayed in the value.
583 * Returns: the number of decimal places that are displayed
586 gtk_scale_get_digits (GtkScale *scale)
588 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
590 return scale->digits;
594 * gtk_scale_set_draw_value:
595 * @scale: a #GtkScale
596 * @draw_value: %TRUE to draw the value
598 * Specifies whether the current value is displayed as a string next
602 gtk_scale_set_draw_value (GtkScale *scale,
605 g_return_if_fail (GTK_IS_SCALE (scale));
607 draw_value = draw_value != FALSE;
609 if (scale->draw_value != draw_value)
611 scale->draw_value = draw_value;
613 GTK_RANGE (scale)->round_digits = scale->digits;
615 GTK_RANGE (scale)->round_digits = -1;
617 _gtk_scale_clear_layout (scale);
619 gtk_widget_queue_resize (GTK_WIDGET (scale));
621 g_object_notify (G_OBJECT (scale), "draw-value");
626 * gtk_scale_get_draw_value:
627 * @scale: a #GtkScale
629 * Returns whether the current value is displayed as a string
630 * next to the slider.
632 * Returns: whether the current value is displayed as a string
635 gtk_scale_get_draw_value (GtkScale *scale)
637 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
639 return scale->draw_value;
643 * gtk_scale_set_value_pos:
644 * @scale: a #GtkScale
645 * @pos: the position in which the current value is displayed
647 * Sets the position in which the current value is displayed.
650 gtk_scale_set_value_pos (GtkScale *scale,
655 g_return_if_fail (GTK_IS_SCALE (scale));
657 if (scale->value_pos != pos)
659 scale->value_pos = pos;
660 widget = GTK_WIDGET (scale);
662 _gtk_scale_clear_layout (scale);
663 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
664 gtk_widget_queue_resize (widget);
666 g_object_notify (G_OBJECT (scale), "value-pos");
671 * gtk_scale_get_value_pos:
672 * @scale: a #GtkScale
674 * Gets the position in which the current value is displayed.
676 * Returns: the position in which the current value is displayed
679 gtk_scale_get_value_pos (GtkScale *scale)
681 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
683 return scale->value_pos;
687 gtk_scale_get_range_border (GtkRange *range,
690 GtkScalePrivate *priv;
695 widget = GTK_WIDGET (range);
696 scale = GTK_SCALE (range);
697 priv = GTK_SCALE_GET_PRIVATE (scale);
699 _gtk_scale_get_value_size (scale, &w, &h);
706 if (scale->draw_value)
709 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
711 switch (scale->value_pos)
714 border->left += w + value_spacing;
717 border->right += w + value_spacing;
720 border->top += h + value_spacing;
723 border->bottom += h + value_spacing;
732 gint n1, w1, h1, n2, w2, h2;
734 gtk_widget_style_get (widget,
735 "slider-width", &slider_width,
736 "value-spacing", &value_spacing,
740 if (GTK_RANGE (scale)->orientation == GTK_ORIENTATION_HORIZONTAL)
742 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
744 border->top += h1 + value_spacing + slider_width / 2;
746 border->bottom += h2 + value_spacing + slider_width / 2;
750 gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
752 border->left += w1 + value_spacing + slider_width / 2;
754 border->right += w2 + value_spacing + slider_width / 2;
759 /* FIXME this could actually be static at the moment. */
761 _gtk_scale_get_value_size (GtkScale *scale,
767 g_return_if_fail (GTK_IS_SCALE (scale));
769 if (scale->draw_value)
772 PangoRectangle logical_rect;
775 range = GTK_RANGE (scale);
777 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
779 txt = _gtk_scale_format_value (scale, range->adjustment->lower);
780 pango_layout_set_text (layout, txt, -1);
783 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
786 *width = logical_rect.width;
788 *height = logical_rect.height;
790 txt = _gtk_scale_format_value (scale, range->adjustment->upper);
791 pango_layout_set_text (layout, txt, -1);
794 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
797 *width = MAX (*width, logical_rect.width);
799 *height = MAX (*height, logical_rect.height);
801 g_object_unref (layout);
814 gtk_scale_get_mark_label_size (GtkScale *scale,
815 GtkPositionType position,
823 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
825 PangoRectangle logical_rect;
829 *count1 = *count2 = 0;
830 *width1 = *width2 = 0;
831 *height1 = *height2 = 0;
833 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
835 for (m = priv->marks; m; m = m->next)
837 GtkScaleMark *mark = m->data;
841 pango_layout_set_markup (layout, mark->markup, -1);
842 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
844 w = logical_rect.width;
845 h = logical_rect.height;
853 if (mark->position == position)
856 *width1 = MAX (*width1, w);
857 *height1 = MAX (*height1, h);
862 *width2 = MAX (*width2, w);
863 *height2 = MAX (*height2, h);
867 g_object_unref (layout);
871 gtk_scale_style_set (GtkWidget *widget,
877 range = GTK_RANGE (widget);
879 gtk_widget_style_get (widget,
880 "slider-length", &slider_length,
883 range->min_slider_size = slider_length;
885 _gtk_scale_clear_layout (GTK_SCALE (widget));
887 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_set (widget, previous);
891 gtk_scale_screen_changed (GtkWidget *widget,
892 GdkScreen *old_screen)
894 _gtk_scale_clear_layout (GTK_SCALE (widget));
898 gtk_scale_size_request (GtkWidget *widget,
899 GtkRequisition *requisition)
901 GtkRange *range = GTK_RANGE (widget);
902 gint n1, w1, h1, n2, w2, h2;
905 GTK_WIDGET_CLASS (gtk_scale_parent_class)->size_request (widget, requisition);
907 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
910 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
912 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
914 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
915 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
916 requisition->width = MAX (requisition->width, MAX (w1, w2));
920 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
921 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
922 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
923 requisition->height = MAX (requisition->height, MAX (h1, h2));
928 find_next_pos (GtkWidget *widget,
937 for (m = list->next, i = 1; m; m = m->next, i++)
939 GtkScaleMark *mark = m->data;
941 if (match == (mark->position == pos))
945 if (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM)
946 return widget->allocation.width;
948 return widget->allocation.height;
952 gtk_scale_expose (GtkWidget *widget,
953 GdkEventExpose *event)
955 GtkScale *scale = GTK_SCALE (widget);
956 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
957 GtkRange *range = GTK_RANGE (scale);
958 GtkStateType state_type;
966 gtk_widget_style_get (widget,
967 "focus-padding", &focus_padding,
968 "slider-width", &slider_width,
969 "value-spacing", &value_spacing,
972 /* We need to chain up _first_ so the various geometry members of
973 * GtkRange struct are updated.
975 GTK_WIDGET_CLASS (gtk_scale_parent_class)->expose_event (widget, event);
977 state_type = GTK_STATE_NORMAL;
978 if (!gtk_widget_is_sensitive (widget))
979 state_type = GTK_STATE_INSENSITIVE;
984 gint x1, x2, x3, y1, y2, y3;
986 PangoRectangle logical_rect;
988 gint min_pos_before, min_pos_after;
989 gint min_pos, max_pos;
991 n_marks = _gtk_range_get_stop_positions (range, &marks);
992 layout = gtk_widget_create_pango_layout (widget, NULL);
994 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
995 min_pos_before = min_pos_after = widget->allocation.x;
997 min_pos_before = min_pos_after = widget->allocation.y;
998 for (m = priv->marks, i = 0; m; m = m->next, i++)
1000 GtkScaleMark *mark = m->data;
1002 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
1004 x1 = widget->allocation.x + marks[i];
1005 if (mark->position == GTK_POS_TOP)
1007 y1 = widget->allocation.y + range->range_rect.y;
1008 y2 = y1 - slider_width / 2;
1009 min_pos = min_pos_before;
1010 max_pos = widget->allocation.x + find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1014 y1 = widget->allocation.y + range->range_rect.y + range->range_rect.height;
1015 y2 = y1 + slider_width / 2;
1016 min_pos = min_pos_after;
1017 max_pos = widget->allocation.x + find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep;
1020 gtk_paint_vline (widget->style, widget->window, state_type,
1021 NULL, widget, "scale-mark", y1, y2, x1);
1025 pango_layout_set_markup (layout, mark->markup, -1);
1026 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1028 x3 = x1 - logical_rect.width / 2;
1031 if (x3 + logical_rect.width > max_pos)
1032 x3 = max_pos - logical_rect.width;
1033 if (x3 < widget->allocation.x)
1034 x3 = widget->allocation.x;
1035 if (mark->position == GTK_POS_TOP)
1037 y3 = y2 - value_spacing - logical_rect.height;
1038 min_pos_before = x3 + logical_rect.width + min_sep;
1042 y3 = y2 + value_spacing;
1043 min_pos_after = x3 + logical_rect.width + min_sep;
1046 gtk_paint_layout (widget->style, widget->window, state_type,
1047 FALSE, NULL, widget, "scale-mark",
1053 if (mark->position == GTK_POS_LEFT)
1055 x1 = widget->allocation.x + range->range_rect.x;
1056 x2 = widget->allocation.x + range->range_rect.x - slider_width / 2;
1057 min_pos = min_pos_before;
1058 max_pos = widget->allocation.y + find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep;
1062 x1 = widget->allocation.x + range->range_rect.x + range->range_rect.width;
1063 x2 = widget->allocation.x + range->range_rect.x + range->range_rect.width + slider_width / 2;
1064 min_pos = min_pos_after;
1065 max_pos = widget->allocation.y + find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep;
1067 y1 = widget->allocation.y + marks[i];
1069 gtk_paint_hline (widget->style, widget->window, state_type,
1070 NULL, widget, "range-mark", x1, x2, y1);
1074 pango_layout_set_markup (layout, mark->markup, -1);
1075 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1077 y3 = y1 - logical_rect.height / 2;
1080 if (y3 + logical_rect.height > max_pos)
1081 y3 = max_pos - logical_rect.height;
1082 if (y3 < widget->allocation.y)
1083 y3 = widget->allocation.y;
1084 if (mark->position == GTK_POS_LEFT)
1086 x3 = x2 - value_spacing - logical_rect.width;
1087 min_pos_before = y3 + logical_rect.height + min_sep;
1091 x3 = x2 + value_spacing;
1092 min_pos_after = y3 + logical_rect.height + min_sep;
1095 gtk_paint_layout (widget->style, widget->window, state_type,
1096 FALSE, NULL, widget, "scale-mark",
1102 g_object_unref (layout);
1106 if (scale->draw_value)
1108 PangoLayout *layout;
1111 layout = gtk_scale_get_layout (scale);
1112 gtk_scale_get_layout_offsets (scale, &x, &y);
1114 gtk_paint_layout (widget->style,
1120 range->orientation == GTK_ORIENTATION_HORIZONTAL ?
1121 "hscale" : "vscale",
1131 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1135 GtkWidget *widget = GTK_WIDGET (scale);
1136 GtkRange *range = GTK_RANGE (widget);
1137 PangoLayout *layout = gtk_scale_get_layout (scale);
1138 PangoRectangle logical_rect;
1149 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1151 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1153 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
1155 switch (scale->value_pos)
1158 *x = range->range_rect.x - value_spacing - logical_rect.width;
1159 *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
1163 *x = range->range_rect.x + range->range_rect.width + value_spacing;
1164 *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
1168 *x = range->slider_start +
1169 (range->slider_end - range->slider_start - logical_rect.width) / 2;
1170 *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
1171 *y = range->range_rect.y - logical_rect.height - value_spacing;
1174 case GTK_POS_BOTTOM:
1175 *x = range->slider_start +
1176 (range->slider_end - range->slider_start - logical_rect.width) / 2;
1177 *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
1178 *y = range->range_rect.y + range->range_rect.height + value_spacing;
1182 g_return_if_reached ();
1188 switch (scale->value_pos)
1191 *x = range->range_rect.x - logical_rect.width - value_spacing;
1192 *y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
1193 *y = CLAMP (*y, 0, widget->allocation.height - logical_rect.height);
1197 *x = range->range_rect.x + range->range_rect.width + value_spacing;
1198 *y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
1199 *y = CLAMP (*y, 0, widget->allocation.height - logical_rect.height);
1203 *x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
1204 *y = range->range_rect.y - logical_rect.height - value_spacing;
1207 case GTK_POS_BOTTOM:
1208 *x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
1209 *y = range->range_rect.y + range->range_rect.height + value_spacing;
1213 g_return_if_reached ();
1217 *x += widget->allocation.x;
1218 *y += widget->allocation.y;
1222 * _gtk_scale_format_value:
1223 * @scale: a #GtkScale
1224 * @value: adjustment value
1226 * Emits #GtkScale::format-value signal to format the value,
1227 * if no user signal handlers, falls back to a default format.
1229 * Return value: formatted value
1232 _gtk_scale_format_value (GtkScale *scale,
1237 g_signal_emit (scale,
1238 signals[FORMAT_VALUE],
1246 /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1247 return g_strdup_printf ("\342\200\216%0.*f", scale->digits, value);
1251 gtk_scale_finalize (GObject *object)
1253 GtkScale *scale = GTK_SCALE (object);
1255 _gtk_scale_clear_layout (scale);
1256 gtk_scale_clear_marks (scale);
1258 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1262 * gtk_scale_get_layout:
1263 * @scale: A #GtkScale
1265 * Gets the #PangoLayout used to display the scale.
1266 * The returned object is owned by the scale so does
1267 * not need to be freed by the caller.
1269 * Return value: the #PangoLayout for this scale, or %NULL
1270 * if the #GtkScale:draw-value property is %FALSE.
1275 gtk_scale_get_layout (GtkScale *scale)
1277 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
1280 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1284 if (scale->draw_value)
1285 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1288 if (scale->draw_value)
1290 txt = _gtk_scale_format_value (scale,
1291 GTK_RANGE (scale)->adjustment->value);
1292 pango_layout_set_text (priv->layout, txt, -1);
1296 return priv->layout;
1300 * gtk_scale_get_layout_offsets:
1301 * @scale: a #GtkScale
1302 * @x: (allow-none): location to store X offset of layout, or %NULL
1303 * @y: (allow-none): location to store Y offset of layout, or %NULL
1305 * Obtains the coordinates where the scale will draw the
1306 * #PangoLayout representing the text in the scale. Remember
1307 * when using the #PangoLayout function you need to convert to
1308 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1310 * If the #GtkScale:draw-value property is %FALSE, the return
1311 * values are undefined.
1316 gtk_scale_get_layout_offsets (GtkScale *scale,
1323 g_return_if_fail (GTK_IS_SCALE (scale));
1325 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1326 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1336 _gtk_scale_clear_layout (GtkScale *scale)
1338 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
1340 g_return_if_fail (GTK_IS_SCALE (scale));
1344 g_object_unref (priv->layout);
1345 priv->layout = NULL;
1350 gtk_scale_mark_free (GtkScaleMark *mark)
1352 g_free (mark->markup);
1357 * gtk_scale_clear_marks:
1358 * @scale: a #GtkScale
1360 * Removes any marks that have been added with gtk_scale_add_mark().
1365 gtk_scale_clear_marks (GtkScale *scale)
1367 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
1369 g_return_if_fail (GTK_IS_SCALE (scale));
1371 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1372 g_slist_free (priv->marks);
1375 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1377 gtk_widget_queue_resize (GTK_WIDGET (scale));
1381 compare_marks (gpointer a, gpointer b)
1383 GtkScaleMark *ma, *mb;
1387 return (gint) (ma->value - mb->value);
1391 * gtk_scale_add_mark:
1392 * @scale: a #GtkScale
1393 * @value: the value at which the mark is placed, must be between
1394 * the lower and upper limits of the scales' adjustment
1395 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1396 * is drawn above the scale, anything else below. For a vertical scale,
1397 * #GTK_POS_LEFT is drawn to the left of the scale, anything else to the
1399 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1402 * Adds a mark at @value.
1404 * A mark is indicated visually by drawing a tick mark next to the scale,
1405 * and GTK+ makes it easy for the user to position the scale exactly at the
1408 * If @markup is not %NULL, text is shown next to the tick mark.
1410 * To remove marks from a scale, use gtk_scale_clear_marks().
1415 gtk_scale_add_mark (GtkScale *scale,
1417 GtkPositionType position,
1418 const gchar *markup)
1420 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
1426 mark = g_new (GtkScaleMark, 1);
1427 mark->value = value;
1428 mark->markup = g_strdup (markup);
1429 mark->position = position;
1431 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1432 (GCompareFunc) compare_marks);
1434 n = g_slist_length (priv->marks);
1435 values = g_new (gdouble, n);
1436 for (m = priv->marks, i = 0; m; m = m->next, i++)
1439 values[i] = mark->value;
1442 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1446 gtk_widget_queue_resize (GTK_WIDGET (scale));
1449 static GtkBuildableIface *parent_buildable_iface;
1452 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1454 parent_buildable_iface = g_type_interface_peek_parent (iface);
1455 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1456 iface->custom_finished = gtk_scale_buildable_custom_finished;
1462 GtkBuilder *builder;
1464 } MarksSubparserData;
1469 GtkPositionType position;
1472 gboolean translatable;
1476 mark_data_free (MarkData *data)
1478 g_string_free (data->markup, TRUE);
1479 g_free (data->context);
1480 g_slice_free (MarkData, data);
1484 marks_start_element (GMarkupParseContext *context,
1485 const gchar *element_name,
1486 const gchar **names,
1487 const gchar **values,
1491 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1493 gint line_number, char_number;
1495 if (strcmp (element_name, "marks") == 0)
1497 else if (strcmp (element_name, "mark") == 0)
1500 gboolean has_value = FALSE;
1501 GtkPositionType position = GTK_POS_BOTTOM;
1502 const gchar *msg_context = NULL;
1503 gboolean translatable = FALSE;
1506 for (i = 0; names[i]; i++)
1508 if (strcmp (names[i], "translatable") == 0)
1510 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1513 else if (strcmp (names[i], "comments") == 0)
1515 /* do nothing, comments are for translators */
1517 else if (strcmp (names[i], "context") == 0)
1518 msg_context = values[i];
1519 else if (strcmp (names[i], "value") == 0)
1521 GValue gvalue = { 0, };
1523 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1526 value = g_value_get_double (&gvalue);
1529 else if (strcmp (names[i], "position") == 0)
1531 GValue gvalue = { 0, };
1533 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1536 position = g_value_get_enum (&gvalue);
1540 g_markup_parse_context_get_position (context,
1545 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1546 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1548 line_number, char_number, names[i], "mark");
1555 g_markup_parse_context_get_position (context,
1560 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1561 "%s:%d:%d <%s> requires attribute \"%s\"",
1563 line_number, char_number, "mark",
1568 mark = g_slice_new (MarkData);
1569 mark->value = value;
1570 mark->position = position;
1571 mark->markup = g_string_new ("");
1572 mark->context = g_strdup (msg_context);
1573 mark->translatable = translatable;
1575 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1579 g_markup_parse_context_get_position (context,
1584 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1585 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1587 line_number, char_number, element_name);
1593 marks_text (GMarkupParseContext *context,
1599 MarksSubparserData *data = (MarksSubparserData*)user_data;
1601 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1603 MarkData *mark = data->marks->data;
1605 g_string_append_len (mark->markup, text, text_len);
1609 static const GMarkupParser marks_parser =
1611 marks_start_element,
1618 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1619 GtkBuilder *builder,
1621 const gchar *tagname,
1622 GMarkupParser *parser,
1625 MarksSubparserData *parser_data;
1630 if (strcmp (tagname, "marks") == 0)
1632 parser_data = g_slice_new0 (MarksSubparserData);
1633 parser_data->scale = GTK_SCALE (buildable);
1634 parser_data->marks = NULL;
1636 *parser = marks_parser;
1637 *data = parser_data;
1641 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1642 tagname, parser, data);
1646 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1647 GtkBuilder *builder,
1649 const gchar *tagname,
1652 GtkScale *scale = GTK_SCALE (buildable);
1653 MarksSubparserData *marks_data;
1655 if (strcmp (tagname, "marks") == 0)
1660 marks_data = (MarksSubparserData *)user_data;
1662 for (m = marks_data->marks; m; m = m->next)
1664 MarkData *mdata = m->data;
1666 if (mdata->translatable && mdata->markup->len)
1667 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1669 mdata->markup->str);
1671 markup = mdata->markup->str;
1673 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1675 mark_data_free (mdata);
1678 g_slist_free (marks_data->marks);
1679 g_slice_free (MarksSubparserData, marks_data);