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
51 typedef struct _GtkScaleMark GtkScaleMark;
69 GtkPositionType position;
84 static guint signals[LAST_SIGNAL];
86 static void gtk_scale_set_property (GObject *object,
90 static void gtk_scale_get_property (GObject *object,
94 static void gtk_scale_size_request (GtkWidget *widget,
95 GtkRequisition *requisition);
96 static void gtk_scale_style_set (GtkWidget *widget,
98 static void gtk_scale_get_range_border (GtkRange *range,
100 static void gtk_scale_get_mark_label_size (GtkScale *scale,
101 GtkPositionType position,
108 static void gtk_scale_finalize (GObject *object);
109 static void gtk_scale_screen_changed (GtkWidget *widget,
110 GdkScreen *old_screen);
111 static gboolean gtk_scale_expose (GtkWidget *widget,
112 GdkEventExpose *event);
113 static void gtk_scale_real_get_layout_offsets (GtkScale *scale,
116 static void gtk_scale_buildable_interface_init (GtkBuildableIface *iface);
117 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
120 const gchar *tagname,
121 GMarkupParser *parser,
123 static void gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
126 const gchar *tagname,
130 G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
131 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
132 gtk_scale_buildable_interface_init))
136 single_string_accumulator (GSignalInvocationHint *ihint,
138 const GValue *handler_return,
141 gboolean continue_emission;
144 str = g_value_get_string (handler_return);
145 g_value_set_string (return_accu, str);
146 continue_emission = str == NULL;
148 return continue_emission;
152 #define add_slider_binding(binding_set, keyval, mask, scroll) \
153 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
154 I_("move-slider"), 1, \
155 GTK_TYPE_SCROLL_TYPE, scroll)
158 gtk_scale_class_init (GtkScaleClass *class)
160 GObjectClass *gobject_class;
161 GtkWidgetClass *widget_class;
162 GtkRangeClass *range_class;
163 GtkBindingSet *binding_set;
165 gobject_class = G_OBJECT_CLASS (class);
166 range_class = (GtkRangeClass*) class;
167 widget_class = (GtkWidgetClass*) class;
169 gobject_class->set_property = gtk_scale_set_property;
170 gobject_class->get_property = gtk_scale_get_property;
171 gobject_class->finalize = gtk_scale_finalize;
173 widget_class->style_set = gtk_scale_style_set;
174 widget_class->screen_changed = gtk_scale_screen_changed;
175 widget_class->expose_event = gtk_scale_expose;
176 widget_class->size_request = gtk_scale_size_request;
178 range_class->slider_detail = "Xscale";
179 range_class->get_range_border = gtk_scale_get_range_border;
181 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
184 * GtkScale::format-value:
185 * @scale: the object which received the signal
186 * @value: the value to format
188 * Signal which allows you to change how the scale value is displayed.
189 * Connect a signal handler which returns an allocated string representing
190 * @value. That string will then be used to display the scale's value.
192 * Here's an example signal handler which displays a value 1.0 as
193 * with "-->1.0<--".
196 * format_value_callback (GtkScale *scale,
199 * return g_strdup_printf ("-->%0.*g<--",
200 * gtk_scale_get_digits (scale), value);
204 * Return value: allocated string representing @value
206 signals[FORMAT_VALUE] =
207 g_signal_new (I_("format-value"),
208 G_TYPE_FROM_CLASS (gobject_class),
210 G_STRUCT_OFFSET (GtkScaleClass, format_value),
211 single_string_accumulator, NULL,
212 _gtk_marshal_STRING__DOUBLE,
216 g_object_class_install_property (gobject_class,
218 g_param_spec_int ("digits",
220 P_("The number of decimal places that are displayed in the value"),
224 GTK_PARAM_READWRITE));
226 g_object_class_install_property (gobject_class,
228 g_param_spec_boolean ("draw-value",
230 P_("Whether the current value is displayed as a string next to the slider"),
232 GTK_PARAM_READWRITE));
234 g_object_class_install_property (gobject_class,
236 g_param_spec_enum ("value-pos",
237 P_("Value Position"),
238 P_("The position in which the current value is displayed"),
239 GTK_TYPE_POSITION_TYPE,
241 GTK_PARAM_READWRITE));
243 gtk_widget_class_install_style_property (widget_class,
244 g_param_spec_int ("slider-length",
246 P_("Length of scale's slider"),
250 GTK_PARAM_READABLE));
252 gtk_widget_class_install_style_property (widget_class,
253 g_param_spec_int ("value-spacing",
255 P_("Space between value text and the slider/trough area"),
259 GTK_PARAM_READABLE));
261 /* All bindings (even arrow keys) are on both h/v scale, because
262 * blind users etc. don't care about scale orientation.
265 binding_set = gtk_binding_set_by_class (class);
267 add_slider_binding (binding_set, GDK_Left, 0,
268 GTK_SCROLL_STEP_LEFT);
270 add_slider_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
271 GTK_SCROLL_PAGE_LEFT);
273 add_slider_binding (binding_set, GDK_KP_Left, 0,
274 GTK_SCROLL_STEP_LEFT);
276 add_slider_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
277 GTK_SCROLL_PAGE_LEFT);
279 add_slider_binding (binding_set, GDK_Right, 0,
280 GTK_SCROLL_STEP_RIGHT);
282 add_slider_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
283 GTK_SCROLL_PAGE_RIGHT);
285 add_slider_binding (binding_set, GDK_KP_Right, 0,
286 GTK_SCROLL_STEP_RIGHT);
288 add_slider_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
289 GTK_SCROLL_PAGE_RIGHT);
291 add_slider_binding (binding_set, GDK_Up, 0,
294 add_slider_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
297 add_slider_binding (binding_set, GDK_KP_Up, 0,
300 add_slider_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
303 add_slider_binding (binding_set, GDK_Down, 0,
304 GTK_SCROLL_STEP_DOWN);
306 add_slider_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
307 GTK_SCROLL_PAGE_DOWN);
309 add_slider_binding (binding_set, GDK_KP_Down, 0,
310 GTK_SCROLL_STEP_DOWN);
312 add_slider_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
313 GTK_SCROLL_PAGE_DOWN);
315 add_slider_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
316 GTK_SCROLL_PAGE_LEFT);
318 add_slider_binding (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
319 GTK_SCROLL_PAGE_LEFT);
321 add_slider_binding (binding_set, GDK_Page_Up, 0,
324 add_slider_binding (binding_set, GDK_KP_Page_Up, 0,
327 add_slider_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
328 GTK_SCROLL_PAGE_RIGHT);
330 add_slider_binding (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
331 GTK_SCROLL_PAGE_RIGHT);
333 add_slider_binding (binding_set, GDK_Page_Down, 0,
334 GTK_SCROLL_PAGE_DOWN);
336 add_slider_binding (binding_set, GDK_KP_Page_Down, 0,
337 GTK_SCROLL_PAGE_DOWN);
339 /* Logical bindings (vs. visual bindings above) */
341 add_slider_binding (binding_set, GDK_plus, 0,
342 GTK_SCROLL_STEP_FORWARD);
344 add_slider_binding (binding_set, GDK_minus, 0,
345 GTK_SCROLL_STEP_BACKWARD);
347 add_slider_binding (binding_set, GDK_plus, GDK_CONTROL_MASK,
348 GTK_SCROLL_PAGE_FORWARD);
350 add_slider_binding (binding_set, GDK_minus, GDK_CONTROL_MASK,
351 GTK_SCROLL_PAGE_BACKWARD);
354 add_slider_binding (binding_set, GDK_KP_Add, 0,
355 GTK_SCROLL_STEP_FORWARD);
357 add_slider_binding (binding_set, GDK_KP_Subtract, 0,
358 GTK_SCROLL_STEP_BACKWARD);
360 add_slider_binding (binding_set, GDK_KP_Add, GDK_CONTROL_MASK,
361 GTK_SCROLL_PAGE_FORWARD);
363 add_slider_binding (binding_set, GDK_KP_Subtract, GDK_CONTROL_MASK,
364 GTK_SCROLL_PAGE_BACKWARD);
367 add_slider_binding (binding_set, GDK_Home, 0,
370 add_slider_binding (binding_set, GDK_KP_Home, 0,
373 add_slider_binding (binding_set, GDK_End, 0,
376 add_slider_binding (binding_set, GDK_KP_End, 0,
379 g_type_class_add_private (gobject_class, sizeof (GtkScalePriv));
383 gtk_scale_orientation_notify (GtkRange *range,
384 const GParamSpec *pspec)
386 range->flippable = (range->orientation == GTK_ORIENTATION_HORIZONTAL);
390 gtk_scale_init (GtkScale *scale)
393 GtkRange *range = GTK_RANGE (scale);
395 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
400 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
402 range->slider_size_fixed = TRUE;
403 range->has_stepper_a = FALSE;
404 range->has_stepper_b = FALSE;
405 range->has_stepper_c = FALSE;
406 range->has_stepper_d = FALSE;
408 priv->draw_value = TRUE;
409 priv->value_pos = GTK_POS_TOP;
411 _gtk_range_set_round_digits (range, priv->digits);
413 gtk_scale_orientation_notify (range, NULL);
414 g_signal_connect (scale, "notify::orientation",
415 G_CALLBACK (gtk_scale_orientation_notify),
420 gtk_scale_set_property (GObject *object,
427 scale = GTK_SCALE (object);
432 gtk_scale_set_digits (scale, g_value_get_int (value));
434 case PROP_DRAW_VALUE:
435 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
438 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
441 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
447 gtk_scale_get_property (GObject *object,
452 GtkScale *scale = GTK_SCALE (object);
453 GtkScalePriv *priv = scale->priv;
458 g_value_set_int (value, priv->digits);
460 case PROP_DRAW_VALUE:
461 g_value_set_boolean (value, priv->draw_value);
464 g_value_set_enum (value, priv->value_pos);
467 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
474 * @orientation: the scale's orientation.
475 * @adjustment: the #GtkAdjustment which sets the range of the scale, or
476 * %NULL to create a new adjustment.
478 * Creates a new #GtkScale.
480 * Return value: a new #GtkScale
485 gtk_scale_new (GtkOrientation orientation,
486 GtkAdjustment *adjustment)
488 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
491 return g_object_new (GTK_TYPE_SCALE,
492 "orientation", orientation,
493 "adjustment", adjustment,
498 * gtk_scale_new_with_range:
499 * @orientation: the scale's orientation.
500 * @min: minimum value
501 * @max: maximum value
502 * @step: step increment (tick size) used with keyboard shortcuts
504 * Creates a new scale widget with the given orientation that lets the
505 * user input a number between @min and @max (including @min and @max)
506 * with the increment @step. @step must be nonzero; it's the distance
507 * the slider moves when using the arrow keys to adjust the scale
510 * Note that the way in which the precision is derived works best if @step
511 * is a power of ten. If the resulting precision is not suitable for your
512 * needs, use gtk_scale_set_digits() to correct it.
514 * Return value: a new #GtkScale
519 gtk_scale_new_with_range (GtkOrientation orientation,
527 g_return_val_if_fail (min < max, NULL);
528 g_return_val_if_fail (step != 0.0, NULL);
530 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
532 if (fabs (step) >= 1.0 || step == 0.0)
538 digits = abs ((gint) floor (log10 (fabs (step))));
543 return g_object_new (GTK_TYPE_SCALE,
544 "orientation", orientation,
551 * gtk_scale_set_digits:
552 * @scale: a #GtkScale
553 * @digits: the number of decimal places to display,
554 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
556 * Sets the number of decimal places that are displayed in the value.
557 * Also causes the value of the adjustment to be rounded off to this
558 * number of digits, so the retrieved value matches the value the user saw.
561 gtk_scale_set_digits (GtkScale *scale,
567 g_return_if_fail (GTK_IS_SCALE (scale));
570 range = GTK_RANGE (scale);
572 digits = CLAMP (digits, -1, MAX_DIGITS);
574 if (priv->digits != digits)
576 priv->digits = digits;
577 if (priv->draw_value)
578 _gtk_range_set_round_digits (range, digits);
580 _gtk_scale_clear_layout (scale);
581 gtk_widget_queue_resize (GTK_WIDGET (scale));
583 g_object_notify (G_OBJECT (scale), "digits");
588 * gtk_scale_get_digits:
589 * @scale: a #GtkScale
591 * Gets the number of decimal places that are displayed in the value.
593 * Returns: the number of decimal places that are displayed
596 gtk_scale_get_digits (GtkScale *scale)
598 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
600 return scale->priv->digits;
604 * gtk_scale_set_draw_value:
605 * @scale: a #GtkScale
606 * @draw_value: %TRUE to draw the value
608 * Specifies whether the current value is displayed as a string next
612 gtk_scale_set_draw_value (GtkScale *scale,
617 g_return_if_fail (GTK_IS_SCALE (scale));
621 draw_value = draw_value != FALSE;
623 if (priv->draw_value != draw_value)
625 priv->draw_value = draw_value;
627 _gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
629 _gtk_range_set_round_digits (GTK_RANGE (scale), -1);
631 _gtk_scale_clear_layout (scale);
633 gtk_widget_queue_resize (GTK_WIDGET (scale));
635 g_object_notify (G_OBJECT (scale), "draw-value");
640 * gtk_scale_get_draw_value:
641 * @scale: a #GtkScale
643 * Returns whether the current value is displayed as a string
644 * next to the slider.
646 * Returns: whether the current value is displayed as a string
649 gtk_scale_get_draw_value (GtkScale *scale)
651 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
653 return scale->priv->draw_value;
657 * gtk_scale_set_value_pos:
658 * @scale: a #GtkScale
659 * @pos: the position in which the current value is displayed
661 * Sets the position in which the current value is displayed.
664 gtk_scale_set_value_pos (GtkScale *scale,
670 g_return_if_fail (GTK_IS_SCALE (scale));
674 if (priv->value_pos != pos)
676 priv->value_pos = pos;
677 widget = GTK_WIDGET (scale);
679 _gtk_scale_clear_layout (scale);
680 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
681 gtk_widget_queue_resize (widget);
683 g_object_notify (G_OBJECT (scale), "value-pos");
688 * gtk_scale_get_value_pos:
689 * @scale: a #GtkScale
691 * Gets the position in which the current value is displayed.
693 * Returns: the position in which the current value is displayed
696 gtk_scale_get_value_pos (GtkScale *scale)
698 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
700 return scale->priv->value_pos;
704 gtk_scale_get_range_border (GtkRange *range,
712 widget = GTK_WIDGET (range);
713 scale = GTK_SCALE (range);
716 _gtk_scale_get_value_size (scale, &w, &h);
723 if (priv->draw_value)
726 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
728 switch (priv->value_pos)
731 border->left += w + value_spacing;
734 border->right += w + value_spacing;
737 border->top += h + value_spacing;
740 border->bottom += h + value_spacing;
749 gint n1, w1, h1, n2, w2, h2;
751 gtk_widget_style_get (widget,
752 "slider-width", &slider_width,
753 "value-spacing", &value_spacing,
757 if (GTK_RANGE (scale)->orientation == GTK_ORIENTATION_HORIZONTAL)
759 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
761 border->top += h1 + value_spacing + slider_width / 2;
763 border->bottom += h2 + value_spacing + slider_width / 2;
767 gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
769 border->left += w1 + value_spacing + slider_width / 2;
771 border->right += w2 + value_spacing + slider_width / 2;
776 /* FIXME this could actually be static at the moment. */
778 _gtk_scale_get_value_size (GtkScale *scale,
782 GtkScalePriv *priv = scale->priv;
785 if (priv->draw_value)
788 PangoRectangle logical_rect;
791 range = GTK_RANGE (scale);
793 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
795 txt = _gtk_scale_format_value (scale, range->adjustment->lower);
796 pango_layout_set_text (layout, txt, -1);
799 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
802 *width = logical_rect.width;
804 *height = logical_rect.height;
806 txt = _gtk_scale_format_value (scale, range->adjustment->upper);
807 pango_layout_set_text (layout, txt, -1);
810 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
813 *width = MAX (*width, logical_rect.width);
815 *height = MAX (*height, logical_rect.height);
817 g_object_unref (layout);
830 gtk_scale_get_mark_label_size (GtkScale *scale,
831 GtkPositionType position,
839 GtkScalePriv *priv = scale->priv;
841 PangoRectangle logical_rect;
845 *count1 = *count2 = 0;
846 *width1 = *width2 = 0;
847 *height1 = *height2 = 0;
849 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
851 for (m = priv->marks; m; m = m->next)
853 GtkScaleMark *mark = m->data;
855 if (mark->markup && *mark->markup)
857 pango_layout_set_markup (layout, mark->markup, -1);
858 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
860 w = logical_rect.width;
861 h = logical_rect.height;
869 if (mark->position == position)
872 *width1 = MAX (*width1, w);
873 *height1 = MAX (*height1, h);
878 *width2 = MAX (*width2, w);
879 *height2 = MAX (*height2, h);
883 g_object_unref (layout);
887 gtk_scale_style_set (GtkWidget *widget,
893 range = GTK_RANGE (widget);
895 gtk_widget_style_get (widget,
896 "slider-length", &slider_length,
899 range->min_slider_size = slider_length;
901 _gtk_scale_clear_layout (GTK_SCALE (widget));
903 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_set (widget, previous);
907 gtk_scale_screen_changed (GtkWidget *widget,
908 GdkScreen *old_screen)
910 _gtk_scale_clear_layout (GTK_SCALE (widget));
914 gtk_scale_size_request (GtkWidget *widget,
915 GtkRequisition *requisition)
917 GtkRange *range = GTK_RANGE (widget);
918 gint n1, w1, h1, n2, w2, h2;
921 GTK_WIDGET_CLASS (gtk_scale_parent_class)->size_request (widget, requisition);
923 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
926 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
928 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
930 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
931 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
932 requisition->width = MAX (requisition->width, MAX (w1, w2));
936 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
937 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
938 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
939 requisition->height = MAX (requisition->height, MAX (h1, h2));
944 find_next_pos (GtkWidget *widget,
950 GtkAllocation allocation;
954 for (m = list->next, i = 1; m; m = m->next, i++)
956 GtkScaleMark *mark = m->data;
958 if (match == (mark->position == pos))
962 gtk_widget_get_allocation (widget, &allocation);
963 if (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM)
964 return allocation.width;
966 return allocation.height;
970 gtk_scale_expose (GtkWidget *widget,
971 GdkEventExpose *event)
973 GtkScale *scale = GTK_SCALE (widget);
974 GtkScalePriv *priv = scale->priv;
975 GtkRange *range = GTK_RANGE (scale);
976 GtkStateType state_type;
986 style = gtk_widget_get_style (widget);
987 gtk_widget_style_get (widget,
988 "focus-padding", &focus_padding,
989 "slider-width", &slider_width,
990 "value-spacing", &value_spacing,
993 /* We need to chain up _first_ so the various geometry members of
994 * GtkRange struct are updated.
996 GTK_WIDGET_CLASS (gtk_scale_parent_class)->expose_event (widget, event);
998 window = gtk_widget_get_window (widget);
1000 state_type = GTK_STATE_NORMAL;
1001 if (!gtk_widget_is_sensitive (widget))
1002 state_type = GTK_STATE_INSENSITIVE;
1006 GtkAllocation allocation;
1008 gint x1, x2, x3, y1, y2, y3;
1009 PangoLayout *layout;
1010 PangoRectangle logical_rect;
1012 gint min_pos_before, min_pos_after;
1013 gint min_pos, max_pos;
1015 n_marks = _gtk_range_get_stop_positions (range, &marks);
1016 layout = gtk_widget_create_pango_layout (widget, NULL);
1017 gtk_widget_get_allocation (widget, &allocation);
1019 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
1020 min_pos_before = min_pos_after = allocation.x;
1022 min_pos_before = min_pos_after = allocation.y;
1023 for (m = priv->marks, i = 0; m; m = m->next, i++)
1025 GtkScaleMark *mark = m->data;
1027 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
1029 x1 = allocation.x + marks[i];
1030 if (mark->position == GTK_POS_TOP)
1032 y1 = allocation.y + range->range_rect.y;
1033 y2 = y1 - slider_width / 2;
1034 min_pos = min_pos_before;
1035 max_pos = allocation.x + find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1039 y1 = allocation.y + range->range_rect.y + range->range_rect.height;
1040 y2 = y1 + slider_width / 2;
1041 min_pos = min_pos_after;
1042 max_pos = allocation.x + find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep;
1045 gtk_paint_vline (style, window, state_type,
1046 NULL, widget, "scale-mark", y1, y2, x1);
1050 pango_layout_set_markup (layout, mark->markup, -1);
1051 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1053 x3 = x1 - logical_rect.width / 2;
1056 if (x3 + logical_rect.width > max_pos)
1057 x3 = max_pos - logical_rect.width;
1058 if (x3 < allocation.x)
1060 if (mark->position == GTK_POS_TOP)
1062 y3 = y2 - value_spacing - logical_rect.height;
1063 min_pos_before = x3 + logical_rect.width + min_sep;
1067 y3 = y2 + value_spacing;
1068 min_pos_after = x3 + logical_rect.width + min_sep;
1071 gtk_paint_layout (style, window, state_type,
1072 FALSE, NULL, widget, "scale-mark",
1078 if (mark->position == GTK_POS_LEFT)
1080 x1 = allocation.x + range->range_rect.x;
1081 x2 = allocation.x + range->range_rect.x - slider_width / 2;
1082 min_pos = min_pos_before;
1083 max_pos = allocation.y + find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep;
1087 x1 = allocation.x + range->range_rect.x + range->range_rect.width;
1088 x2 = allocation.x + range->range_rect.x + range->range_rect.width + slider_width / 2;
1089 min_pos = min_pos_after;
1090 max_pos = allocation.y + find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep;
1092 y1 = allocation.y + marks[i];
1094 gtk_paint_hline (style, window, state_type,
1095 NULL, widget, "range-mark", x1, x2, y1);
1099 pango_layout_set_markup (layout, mark->markup, -1);
1100 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1102 y3 = y1 - logical_rect.height / 2;
1105 if (y3 + logical_rect.height > max_pos)
1106 y3 = max_pos - logical_rect.height;
1107 if (y3 < allocation.y)
1109 if (mark->position == GTK_POS_LEFT)
1111 x3 = x2 - value_spacing - logical_rect.width;
1112 min_pos_before = y3 + logical_rect.height + min_sep;
1116 x3 = x2 + value_spacing;
1117 min_pos_after = y3 + logical_rect.height + min_sep;
1120 gtk_paint_layout (style, window, state_type,
1121 FALSE, NULL, widget, "scale-mark",
1127 g_object_unref (layout);
1131 if (priv->draw_value)
1133 PangoLayout *layout;
1136 layout = gtk_scale_get_layout (scale);
1137 gtk_scale_get_layout_offsets (scale, &x, &y);
1139 gtk_paint_layout (style,
1145 range->orientation == GTK_ORIENTATION_HORIZONTAL ?
1146 "hscale" : "vscale",
1156 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1160 GtkScalePriv *priv = scale->priv;
1161 GtkAllocation allocation;
1162 GtkWidget *widget = GTK_WIDGET (scale);
1163 GtkRange *range = GTK_RANGE (widget);
1164 PangoLayout *layout = gtk_scale_get_layout (scale);
1165 PangoRectangle logical_rect;
1176 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1178 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1180 gtk_widget_get_allocation (widget, &allocation);
1182 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
1184 switch (priv->value_pos)
1187 *x = range->range_rect.x - value_spacing - logical_rect.width;
1188 *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
1192 *x = range->range_rect.x + range->range_rect.width + value_spacing;
1193 *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
1197 *x = range->slider_start +
1198 (range->slider_end - range->slider_start - logical_rect.width) / 2;
1199 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1200 *y = range->range_rect.y - logical_rect.height - value_spacing;
1203 case GTK_POS_BOTTOM:
1204 *x = range->slider_start +
1205 (range->slider_end - range->slider_start - logical_rect.width) / 2;
1206 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1207 *y = range->range_rect.y + range->range_rect.height + value_spacing;
1211 g_return_if_reached ();
1217 switch (priv->value_pos)
1220 *x = range->range_rect.x - logical_rect.width - value_spacing;
1221 *y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
1222 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1226 *x = range->range_rect.x + range->range_rect.width + value_spacing;
1227 *y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
1228 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1232 *x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
1233 *y = range->range_rect.y - logical_rect.height - value_spacing;
1236 case GTK_POS_BOTTOM:
1237 *x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
1238 *y = range->range_rect.y + range->range_rect.height + value_spacing;
1242 g_return_if_reached ();
1251 * _gtk_scale_format_value:
1252 * @scale: a #GtkScale
1253 * @value: adjustment value
1255 * Emits #GtkScale::format-value signal to format the value,
1256 * if no user signal handlers, falls back to a default format.
1258 * Return value: formatted value
1261 _gtk_scale_format_value (GtkScale *scale,
1264 GtkScalePriv *priv = scale->priv;
1267 g_signal_emit (scale,
1268 signals[FORMAT_VALUE],
1276 /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1277 return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value);
1281 gtk_scale_finalize (GObject *object)
1283 GtkScale *scale = GTK_SCALE (object);
1285 _gtk_scale_clear_layout (scale);
1286 gtk_scale_clear_marks (scale);
1288 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1292 * gtk_scale_get_layout:
1293 * @scale: A #GtkScale
1295 * Gets the #PangoLayout used to display the scale.
1296 * The returned object is owned by the scale so does
1297 * not need to be freed by the caller.
1299 * Return value: the #PangoLayout for this scale, or %NULL
1300 * if the #GtkScale:draw-value property is %FALSE.
1305 gtk_scale_get_layout (GtkScale *scale)
1310 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1316 if (priv->draw_value)
1317 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1320 if (priv->draw_value)
1322 txt = _gtk_scale_format_value (scale,
1323 GTK_RANGE (scale)->adjustment->value);
1324 pango_layout_set_text (priv->layout, txt, -1);
1328 return priv->layout;
1332 * gtk_scale_get_layout_offsets:
1333 * @scale: a #GtkScale
1334 * @x: (allow-none): location to store X offset of layout, or %NULL
1335 * @y: (allow-none): location to store Y offset of layout, or %NULL
1337 * Obtains the coordinates where the scale will draw the
1338 * #PangoLayout representing the text in the scale. Remember
1339 * when using the #PangoLayout function you need to convert to
1340 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1342 * If the #GtkScale:draw-value property is %FALSE, the return
1343 * values are undefined.
1348 gtk_scale_get_layout_offsets (GtkScale *scale,
1355 g_return_if_fail (GTK_IS_SCALE (scale));
1357 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1358 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1368 _gtk_scale_clear_layout (GtkScale *scale)
1370 GtkScalePriv *priv = scale->priv;
1372 g_return_if_fail (GTK_IS_SCALE (scale));
1376 g_object_unref (priv->layout);
1377 priv->layout = NULL;
1382 gtk_scale_mark_free (GtkScaleMark *mark)
1384 g_free (mark->markup);
1389 * gtk_scale_clear_marks:
1390 * @scale: a #GtkScale
1392 * Removes any marks that have been added with gtk_scale_add_mark().
1397 gtk_scale_clear_marks (GtkScale *scale)
1401 g_return_if_fail (GTK_IS_SCALE (scale));
1405 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1406 g_slist_free (priv->marks);
1409 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1411 gtk_widget_queue_resize (GTK_WIDGET (scale));
1415 compare_marks (gpointer a, gpointer b)
1417 GtkScaleMark *ma, *mb;
1421 return (gint) (ma->value - mb->value);
1425 * gtk_scale_add_mark:
1426 * @scale: a #GtkScale
1427 * @value: the value at which the mark is placed, must be between
1428 * the lower and upper limits of the scales' adjustment
1429 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1430 * is drawn above the scale, anything else below. For a vertical scale,
1431 * #GTK_POS_LEFT is drawn to the left of the scale, anything else to the
1433 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1436 * Adds a mark at @value.
1438 * A mark is indicated visually by drawing a tick mark next to the scale,
1439 * and GTK+ makes it easy for the user to position the scale exactly at the
1442 * If @markup is not %NULL, text is shown next to the tick mark.
1444 * To remove marks from a scale, use gtk_scale_clear_marks().
1449 gtk_scale_add_mark (GtkScale *scale,
1451 GtkPositionType position,
1452 const gchar *markup)
1460 g_return_if_fail (GTK_IS_SCALE (scale));
1464 mark = g_new (GtkScaleMark, 1);
1465 mark->value = value;
1466 mark->markup = g_strdup (markup);
1467 mark->position = position;
1469 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1470 (GCompareFunc) compare_marks);
1472 n = g_slist_length (priv->marks);
1473 values = g_new (gdouble, n);
1474 for (m = priv->marks, i = 0; m; m = m->next, i++)
1477 values[i] = mark->value;
1480 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1484 gtk_widget_queue_resize (GTK_WIDGET (scale));
1487 static GtkBuildableIface *parent_buildable_iface;
1490 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1492 parent_buildable_iface = g_type_interface_peek_parent (iface);
1493 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1494 iface->custom_finished = gtk_scale_buildable_custom_finished;
1500 GtkBuilder *builder;
1502 } MarksSubparserData;
1507 GtkPositionType position;
1510 gboolean translatable;
1514 mark_data_free (MarkData *data)
1516 g_string_free (data->markup, TRUE);
1517 g_free (data->context);
1518 g_slice_free (MarkData, data);
1522 marks_start_element (GMarkupParseContext *context,
1523 const gchar *element_name,
1524 const gchar **names,
1525 const gchar **values,
1529 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1531 gint line_number, char_number;
1533 if (strcmp (element_name, "marks") == 0)
1535 else if (strcmp (element_name, "mark") == 0)
1538 gboolean has_value = FALSE;
1539 GtkPositionType position = GTK_POS_BOTTOM;
1540 const gchar *msg_context = NULL;
1541 gboolean translatable = FALSE;
1544 for (i = 0; names[i]; i++)
1546 if (strcmp (names[i], "translatable") == 0)
1548 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1551 else if (strcmp (names[i], "comments") == 0)
1553 /* do nothing, comments are for translators */
1555 else if (strcmp (names[i], "context") == 0)
1556 msg_context = values[i];
1557 else if (strcmp (names[i], "value") == 0)
1559 GValue gvalue = { 0, };
1561 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1564 value = g_value_get_double (&gvalue);
1567 else if (strcmp (names[i], "position") == 0)
1569 GValue gvalue = { 0, };
1571 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1574 position = g_value_get_enum (&gvalue);
1578 g_markup_parse_context_get_position (context,
1583 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1584 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1586 line_number, char_number, names[i], "mark");
1593 g_markup_parse_context_get_position (context,
1598 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1599 "%s:%d:%d <%s> requires attribute \"%s\"",
1601 line_number, char_number, "mark",
1606 mark = g_slice_new (MarkData);
1607 mark->value = value;
1608 mark->position = position;
1609 mark->markup = g_string_new ("");
1610 mark->context = g_strdup (msg_context);
1611 mark->translatable = translatable;
1613 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1617 g_markup_parse_context_get_position (context,
1622 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1623 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1625 line_number, char_number, element_name);
1631 marks_text (GMarkupParseContext *context,
1637 MarksSubparserData *data = (MarksSubparserData*)user_data;
1639 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1641 MarkData *mark = data->marks->data;
1643 g_string_append_len (mark->markup, text, text_len);
1647 static const GMarkupParser marks_parser =
1649 marks_start_element,
1656 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1657 GtkBuilder *builder,
1659 const gchar *tagname,
1660 GMarkupParser *parser,
1663 MarksSubparserData *parser_data;
1668 if (strcmp (tagname, "marks") == 0)
1670 parser_data = g_slice_new0 (MarksSubparserData);
1671 parser_data->scale = GTK_SCALE (buildable);
1672 parser_data->marks = NULL;
1674 *parser = marks_parser;
1675 *data = parser_data;
1679 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1680 tagname, parser, data);
1684 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1685 GtkBuilder *builder,
1687 const gchar *tagname,
1690 GtkScale *scale = GTK_SCALE (buildable);
1691 MarksSubparserData *marks_data;
1693 if (strcmp (tagname, "marks") == 0)
1698 marks_data = (MarksSubparserData *)user_data;
1700 for (m = marks_data->marks; m; m = m->next)
1702 MarkData *mdata = m->data;
1704 if (mdata->translatable && mdata->markup->len)
1705 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1707 mdata->markup->str);
1709 markup = mdata->markup->str;
1711 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1713 mark_data_free (mdata);
1716 g_slist_free (marks_data->marks);
1717 g_slice_free (MarksSubparserData, marks_data);