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"
45 #define MAX_DIGITS (64) /* don't change this,
46 * a) you don't need to and
47 * b) you might cause buffer owerflows in
48 * unrelated code portions otherwise
52 typedef struct _GtkScaleMark GtkScaleMark;
54 struct _GtkScalePrivate
70 GtkPositionType position;
85 static guint signals[LAST_SIGNAL];
87 static void gtk_scale_set_property (GObject *object,
91 static void gtk_scale_get_property (GObject *object,
95 static void gtk_scale_size_request (GtkWidget *widget,
96 GtkRequisition *requisition);
97 static void gtk_scale_style_set (GtkWidget *widget,
99 static void gtk_scale_get_range_border (GtkRange *range,
101 static void gtk_scale_get_mark_label_size (GtkScale *scale,
102 GtkPositionType position,
109 static void gtk_scale_finalize (GObject *object);
110 static void gtk_scale_screen_changed (GtkWidget *widget,
111 GdkScreen *old_screen);
112 static gboolean gtk_scale_expose (GtkWidget *widget,
113 GdkEventExpose *event);
114 static void gtk_scale_real_get_layout_offsets (GtkScale *scale,
117 static void gtk_scale_buildable_interface_init (GtkBuildableIface *iface);
118 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
121 const gchar *tagname,
122 GMarkupParser *parser,
124 static void gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
127 const gchar *tagname,
131 G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
132 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
133 gtk_scale_buildable_interface_init))
137 single_string_accumulator (GSignalInvocationHint *ihint,
139 const GValue *handler_return,
142 gboolean continue_emission;
145 str = g_value_get_string (handler_return);
146 g_value_set_string (return_accu, str);
147 continue_emission = str == NULL;
149 return continue_emission;
153 #define add_slider_binding(binding_set, keyval, mask, scroll) \
154 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
155 I_("move-slider"), 1, \
156 GTK_TYPE_SCROLL_TYPE, scroll)
159 gtk_scale_class_init (GtkScaleClass *class)
161 GObjectClass *gobject_class;
162 GtkWidgetClass *widget_class;
163 GtkRangeClass *range_class;
164 GtkBindingSet *binding_set;
166 gobject_class = G_OBJECT_CLASS (class);
167 range_class = (GtkRangeClass*) class;
168 widget_class = (GtkWidgetClass*) class;
170 gobject_class->set_property = gtk_scale_set_property;
171 gobject_class->get_property = gtk_scale_get_property;
172 gobject_class->finalize = gtk_scale_finalize;
174 widget_class->style_set = gtk_scale_style_set;
175 widget_class->screen_changed = gtk_scale_screen_changed;
176 widget_class->expose_event = gtk_scale_expose;
177 widget_class->size_request = gtk_scale_size_request;
179 range_class->slider_detail = "Xscale";
180 range_class->get_range_border = gtk_scale_get_range_border;
182 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
185 * GtkScale::format-value:
186 * @scale: the object which received the signal
187 * @value: the value to format
189 * Signal which allows you to change how the scale value is displayed.
190 * Connect a signal handler which returns an allocated string representing
191 * @value. That string will then be used to display the scale's value.
193 * Here's an example signal handler which displays a value 1.0 as
194 * with "-->1.0<--".
197 * format_value_callback (GtkScale *scale,
200 * return g_strdup_printf ("-->%0.*g<--",
201 * gtk_scale_get_digits (scale), value);
205 * Return value: allocated string representing @value
207 signals[FORMAT_VALUE] =
208 g_signal_new (I_("format-value"),
209 G_TYPE_FROM_CLASS (gobject_class),
211 G_STRUCT_OFFSET (GtkScaleClass, format_value),
212 single_string_accumulator, NULL,
213 _gtk_marshal_STRING__DOUBLE,
217 g_object_class_install_property (gobject_class,
219 g_param_spec_int ("digits",
221 P_("The number of decimal places that are displayed in the value"),
225 GTK_PARAM_READWRITE));
227 g_object_class_install_property (gobject_class,
229 g_param_spec_boolean ("draw-value",
231 P_("Whether the current value is displayed as a string next to the slider"),
233 GTK_PARAM_READWRITE));
235 g_object_class_install_property (gobject_class,
237 g_param_spec_enum ("value-pos",
238 P_("Value Position"),
239 P_("The position in which the current value is displayed"),
240 GTK_TYPE_POSITION_TYPE,
242 GTK_PARAM_READWRITE));
244 gtk_widget_class_install_style_property (widget_class,
245 g_param_spec_int ("slider-length",
247 P_("Length of scale's slider"),
251 GTK_PARAM_READABLE));
253 gtk_widget_class_install_style_property (widget_class,
254 g_param_spec_int ("value-spacing",
256 P_("Space between value text and the slider/trough area"),
260 GTK_PARAM_READABLE));
262 /* All bindings (even arrow keys) are on both h/v scale, because
263 * blind users etc. don't care about scale orientation.
266 binding_set = gtk_binding_set_by_class (class);
268 add_slider_binding (binding_set, GDK_Left, 0,
269 GTK_SCROLL_STEP_LEFT);
271 add_slider_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
272 GTK_SCROLL_PAGE_LEFT);
274 add_slider_binding (binding_set, GDK_KP_Left, 0,
275 GTK_SCROLL_STEP_LEFT);
277 add_slider_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
278 GTK_SCROLL_PAGE_LEFT);
280 add_slider_binding (binding_set, GDK_Right, 0,
281 GTK_SCROLL_STEP_RIGHT);
283 add_slider_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
284 GTK_SCROLL_PAGE_RIGHT);
286 add_slider_binding (binding_set, GDK_KP_Right, 0,
287 GTK_SCROLL_STEP_RIGHT);
289 add_slider_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
290 GTK_SCROLL_PAGE_RIGHT);
292 add_slider_binding (binding_set, GDK_Up, 0,
295 add_slider_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
298 add_slider_binding (binding_set, GDK_KP_Up, 0,
301 add_slider_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
304 add_slider_binding (binding_set, GDK_Down, 0,
305 GTK_SCROLL_STEP_DOWN);
307 add_slider_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
308 GTK_SCROLL_PAGE_DOWN);
310 add_slider_binding (binding_set, GDK_KP_Down, 0,
311 GTK_SCROLL_STEP_DOWN);
313 add_slider_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
314 GTK_SCROLL_PAGE_DOWN);
316 add_slider_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
317 GTK_SCROLL_PAGE_LEFT);
319 add_slider_binding (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
320 GTK_SCROLL_PAGE_LEFT);
322 add_slider_binding (binding_set, GDK_Page_Up, 0,
325 add_slider_binding (binding_set, GDK_KP_Page_Up, 0,
328 add_slider_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
329 GTK_SCROLL_PAGE_RIGHT);
331 add_slider_binding (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
332 GTK_SCROLL_PAGE_RIGHT);
334 add_slider_binding (binding_set, GDK_Page_Down, 0,
335 GTK_SCROLL_PAGE_DOWN);
337 add_slider_binding (binding_set, GDK_KP_Page_Down, 0,
338 GTK_SCROLL_PAGE_DOWN);
340 /* Logical bindings (vs. visual bindings above) */
342 add_slider_binding (binding_set, GDK_plus, 0,
343 GTK_SCROLL_STEP_FORWARD);
345 add_slider_binding (binding_set, GDK_minus, 0,
346 GTK_SCROLL_STEP_BACKWARD);
348 add_slider_binding (binding_set, GDK_plus, GDK_CONTROL_MASK,
349 GTK_SCROLL_PAGE_FORWARD);
351 add_slider_binding (binding_set, GDK_minus, GDK_CONTROL_MASK,
352 GTK_SCROLL_PAGE_BACKWARD);
355 add_slider_binding (binding_set, GDK_KP_Add, 0,
356 GTK_SCROLL_STEP_FORWARD);
358 add_slider_binding (binding_set, GDK_KP_Subtract, 0,
359 GTK_SCROLL_STEP_BACKWARD);
361 add_slider_binding (binding_set, GDK_KP_Add, GDK_CONTROL_MASK,
362 GTK_SCROLL_PAGE_FORWARD);
364 add_slider_binding (binding_set, GDK_KP_Subtract, GDK_CONTROL_MASK,
365 GTK_SCROLL_PAGE_BACKWARD);
368 add_slider_binding (binding_set, GDK_Home, 0,
371 add_slider_binding (binding_set, GDK_KP_Home, 0,
374 add_slider_binding (binding_set, GDK_End, 0,
377 add_slider_binding (binding_set, GDK_KP_End, 0,
380 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
384 gtk_scale_orientation_notify (GtkRange *range,
385 const GParamSpec *pspec)
387 GtkOrientation orientation;
389 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
390 gtk_range_set_flippable (range,
391 orientation == GTK_ORIENTATION_HORIZONTAL);
395 gtk_scale_init (GtkScale *scale)
397 GtkScalePrivate *priv;
398 GtkRange *range = GTK_RANGE (scale);
400 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
405 gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
407 gtk_range_set_slider_size_fixed (range, TRUE);
409 priv->draw_value = TRUE;
410 priv->value_pos = GTK_POS_TOP;
412 _gtk_range_set_round_digits (range, priv->digits);
414 gtk_scale_orientation_notify (range, NULL);
415 g_signal_connect (scale, "notify::orientation",
416 G_CALLBACK (gtk_scale_orientation_notify),
421 gtk_scale_set_property (GObject *object,
428 scale = GTK_SCALE (object);
433 gtk_scale_set_digits (scale, g_value_get_int (value));
435 case PROP_DRAW_VALUE:
436 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
439 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
442 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
448 gtk_scale_get_property (GObject *object,
453 GtkScale *scale = GTK_SCALE (object);
454 GtkScalePrivate *priv = scale->priv;
459 g_value_set_int (value, priv->digits);
461 case PROP_DRAW_VALUE:
462 g_value_set_boolean (value, priv->draw_value);
465 g_value_set_enum (value, priv->value_pos);
468 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
475 * @orientation: the scale's orientation.
476 * @adjustment: the #GtkAdjustment which sets the range of the scale, or
477 * %NULL to create a new adjustment.
479 * Creates a new #GtkScale.
481 * Return value: a new #GtkScale
486 gtk_scale_new (GtkOrientation orientation,
487 GtkAdjustment *adjustment)
489 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
492 return g_object_new (GTK_TYPE_SCALE,
493 "orientation", orientation,
494 "adjustment", adjustment,
499 * gtk_scale_new_with_range:
500 * @orientation: the scale's orientation.
501 * @min: minimum value
502 * @max: maximum value
503 * @step: step increment (tick size) used with keyboard shortcuts
505 * Creates a new scale widget with the given orientation that lets the
506 * user input a number between @min and @max (including @min and @max)
507 * with the increment @step. @step must be nonzero; it's the distance
508 * the slider moves when using the arrow keys to adjust the scale
511 * Note that the way in which the precision is derived works best if @step
512 * is a power of ten. If the resulting precision is not suitable for your
513 * needs, use gtk_scale_set_digits() to correct it.
515 * Return value: a new #GtkScale
520 gtk_scale_new_with_range (GtkOrientation orientation,
528 g_return_val_if_fail (min < max, NULL);
529 g_return_val_if_fail (step != 0.0, NULL);
531 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
533 if (fabs (step) >= 1.0 || step == 0.0)
539 digits = abs ((gint) floor (log10 (fabs (step))));
544 return g_object_new (GTK_TYPE_SCALE,
545 "orientation", orientation,
552 * gtk_scale_set_digits:
553 * @scale: a #GtkScale
554 * @digits: the number of decimal places to display,
555 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
557 * Sets the number of decimal places that are displayed in the value.
558 * Also causes the value of the adjustment to be rounded off to this
559 * number of digits, so the retrieved value matches the value the user saw.
562 gtk_scale_set_digits (GtkScale *scale,
565 GtkScalePrivate *priv;
568 g_return_if_fail (GTK_IS_SCALE (scale));
571 range = GTK_RANGE (scale);
573 digits = CLAMP (digits, -1, MAX_DIGITS);
575 if (priv->digits != digits)
577 priv->digits = digits;
578 if (priv->draw_value)
579 _gtk_range_set_round_digits (range, digits);
581 _gtk_scale_clear_layout (scale);
582 gtk_widget_queue_resize (GTK_WIDGET (scale));
584 g_object_notify (G_OBJECT (scale), "digits");
589 * gtk_scale_get_digits:
590 * @scale: a #GtkScale
592 * Gets the number of decimal places that are displayed in the value.
594 * Returns: the number of decimal places that are displayed
597 gtk_scale_get_digits (GtkScale *scale)
599 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
601 return scale->priv->digits;
605 * gtk_scale_set_draw_value:
606 * @scale: a #GtkScale
607 * @draw_value: %TRUE to draw the value
609 * Specifies whether the current value is displayed as a string next
613 gtk_scale_set_draw_value (GtkScale *scale,
616 GtkScalePrivate *priv;
618 g_return_if_fail (GTK_IS_SCALE (scale));
622 draw_value = draw_value != FALSE;
624 if (priv->draw_value != draw_value)
626 priv->draw_value = draw_value;
628 _gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
630 _gtk_range_set_round_digits (GTK_RANGE (scale), -1);
632 _gtk_scale_clear_layout (scale);
634 gtk_widget_queue_resize (GTK_WIDGET (scale));
636 g_object_notify (G_OBJECT (scale), "draw-value");
641 * gtk_scale_get_draw_value:
642 * @scale: a #GtkScale
644 * Returns whether the current value is displayed as a string
645 * next to the slider.
647 * Returns: whether the current value is displayed as a string
650 gtk_scale_get_draw_value (GtkScale *scale)
652 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
654 return scale->priv->draw_value;
658 * gtk_scale_set_value_pos:
659 * @scale: a #GtkScale
660 * @pos: the position in which the current value is displayed
662 * Sets the position in which the current value is displayed.
665 gtk_scale_set_value_pos (GtkScale *scale,
668 GtkScalePrivate *priv;
671 g_return_if_fail (GTK_IS_SCALE (scale));
675 if (priv->value_pos != pos)
677 priv->value_pos = pos;
678 widget = GTK_WIDGET (scale);
680 _gtk_scale_clear_layout (scale);
681 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
682 gtk_widget_queue_resize (widget);
684 g_object_notify (G_OBJECT (scale), "value-pos");
689 * gtk_scale_get_value_pos:
690 * @scale: a #GtkScale
692 * Gets the position in which the current value is displayed.
694 * Returns: the position in which the current value is displayed
697 gtk_scale_get_value_pos (GtkScale *scale)
699 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
701 return scale->priv->value_pos;
705 gtk_scale_get_range_border (GtkRange *range,
708 GtkScalePrivate *priv;
713 widget = GTK_WIDGET (range);
714 scale = GTK_SCALE (range);
717 _gtk_scale_get_value_size (scale, &w, &h);
724 if (priv->draw_value)
727 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
729 switch (priv->value_pos)
732 border->left += w + value_spacing;
735 border->right += w + value_spacing;
738 border->top += h + value_spacing;
741 border->bottom += h + value_spacing;
750 gint n1, w1, h1, n2, w2, h2;
752 gtk_widget_style_get (widget,
753 "slider-width", &slider_width,
754 "value-spacing", &value_spacing,
758 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
760 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
762 border->top += h1 + value_spacing + slider_width / 2;
764 border->bottom += h2 + value_spacing + slider_width / 2;
768 gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
770 border->left += w1 + value_spacing + slider_width / 2;
772 border->right += w2 + value_spacing + slider_width / 2;
777 /* FIXME this could actually be static at the moment. */
779 _gtk_scale_get_value_size (GtkScale *scale,
783 GtkScalePrivate *priv = scale->priv;
786 if (priv->draw_value)
788 GtkAdjustment *adjustment;
790 PangoRectangle logical_rect;
793 range = GTK_RANGE (scale);
795 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
796 adjustment = gtk_range_get_adjustment (range);
798 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
799 pango_layout_set_text (layout, txt, -1);
802 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
805 *width = logical_rect.width;
807 *height = logical_rect.height;
809 txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
810 pango_layout_set_text (layout, txt, -1);
813 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
816 *width = MAX (*width, logical_rect.width);
818 *height = MAX (*height, logical_rect.height);
820 g_object_unref (layout);
833 gtk_scale_get_mark_label_size (GtkScale *scale,
834 GtkPositionType position,
842 GtkScalePrivate *priv = scale->priv;
844 PangoRectangle logical_rect;
848 *count1 = *count2 = 0;
849 *width1 = *width2 = 0;
850 *height1 = *height2 = 0;
852 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
854 for (m = priv->marks; m; m = m->next)
856 GtkScaleMark *mark = m->data;
858 if (mark->markup && *mark->markup)
860 pango_layout_set_markup (layout, mark->markup, -1);
861 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
863 w = logical_rect.width;
864 h = logical_rect.height;
872 if (mark->position == position)
875 *width1 = MAX (*width1, w);
876 *height1 = MAX (*height1, h);
881 *width2 = MAX (*width2, w);
882 *height2 = MAX (*height2, h);
886 g_object_unref (layout);
890 gtk_scale_style_set (GtkWidget *widget,
896 range = GTK_RANGE (widget);
898 gtk_widget_style_get (widget,
899 "slider-length", &slider_length,
902 gtk_range_set_min_slider_size (range, slider_length);
904 _gtk_scale_clear_layout (GTK_SCALE (widget));
906 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_set (widget, previous);
910 gtk_scale_screen_changed (GtkWidget *widget,
911 GdkScreen *old_screen)
913 _gtk_scale_clear_layout (GTK_SCALE (widget));
917 gtk_scale_size_request (GtkWidget *widget,
918 GtkRequisition *requisition)
920 GtkRange *range = GTK_RANGE (widget);
921 gint n1, w1, h1, n2, w2, h2;
924 GTK_WIDGET_CLASS (gtk_scale_parent_class)->size_request (widget, requisition);
926 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
929 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
931 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
933 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
934 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
935 requisition->width = MAX (requisition->width, MAX (w1, w2));
939 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
940 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
941 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
942 requisition->height = MAX (requisition->height, MAX (h1, h2));
947 find_next_pos (GtkWidget *widget,
953 GtkAllocation allocation;
957 for (m = list->next, i = 1; m; m = m->next, i++)
959 GtkScaleMark *mark = m->data;
961 if (match == (mark->position == pos))
965 gtk_widget_get_allocation (widget, &allocation);
966 if (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM)
967 return allocation.width;
969 return allocation.height;
973 gtk_scale_expose (GtkWidget *widget,
974 GdkEventExpose *event)
976 GtkScale *scale = GTK_SCALE (widget);
977 GtkScalePrivate *priv = scale->priv;
978 GtkRange *range = GTK_RANGE (scale);
979 GtkStateType state_type;
989 style = gtk_widget_get_style (widget);
990 gtk_widget_style_get (widget,
991 "focus-padding", &focus_padding,
992 "slider-width", &slider_width,
993 "value-spacing", &value_spacing,
996 /* We need to chain up _first_ so the various geometry members of
997 * GtkRange struct are updated.
999 GTK_WIDGET_CLASS (gtk_scale_parent_class)->expose_event (widget, event);
1001 window = gtk_widget_get_window (widget);
1003 state_type = GTK_STATE_NORMAL;
1004 if (!gtk_widget_is_sensitive (widget))
1005 state_type = GTK_STATE_INSENSITIVE;
1009 GtkAllocation allocation;
1010 GtkOrientation orientation;
1011 GdkRectangle range_rect;
1013 gint x1, x2, x3, y1, y2, y3;
1014 PangoLayout *layout;
1015 PangoRectangle logical_rect;
1017 gint min_pos_before, min_pos_after;
1018 gint min_pos, max_pos;
1020 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1021 n_marks = _gtk_range_get_stop_positions (range, &marks);
1022 layout = gtk_widget_create_pango_layout (widget, NULL);
1023 gtk_widget_get_allocation (widget, &allocation);
1024 gtk_range_get_range_rect (range, &range_rect);
1026 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1027 min_pos_before = min_pos_after = allocation.x;
1029 min_pos_before = min_pos_after = allocation.y;
1030 for (m = priv->marks, i = 0; m; m = m->next, i++)
1032 GtkScaleMark *mark = m->data;
1034 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1036 x1 = allocation.x + marks[i];
1037 if (mark->position == GTK_POS_TOP)
1039 y1 = allocation.y + range_rect.y;
1040 y2 = y1 - slider_width / 2;
1041 min_pos = min_pos_before;
1042 max_pos = allocation.x + find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1046 y1 = allocation.y + range_rect.y + range_rect.height;
1047 y2 = y1 + slider_width / 2;
1048 min_pos = min_pos_after;
1049 max_pos = allocation.x + find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep;
1052 gtk_paint_vline (style, window, state_type,
1053 NULL, widget, "scale-mark", y1, y2, x1);
1057 pango_layout_set_markup (layout, mark->markup, -1);
1058 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1060 x3 = x1 - logical_rect.width / 2;
1063 if (x3 + logical_rect.width > max_pos)
1064 x3 = max_pos - logical_rect.width;
1065 if (x3 < allocation.x)
1067 if (mark->position == GTK_POS_TOP)
1069 y3 = y2 - value_spacing - logical_rect.height;
1070 min_pos_before = x3 + logical_rect.width + min_sep;
1074 y3 = y2 + value_spacing;
1075 min_pos_after = x3 + logical_rect.width + min_sep;
1078 gtk_paint_layout (style, window, state_type,
1079 FALSE, NULL, widget, "scale-mark",
1085 if (mark->position == GTK_POS_LEFT)
1087 x1 = allocation.x + range_rect.x;
1088 x2 = allocation.x + range_rect.x - slider_width / 2;
1089 min_pos = min_pos_before;
1090 max_pos = allocation.y + find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep;
1094 x1 = allocation.x + range_rect.x + range_rect.width;
1095 x2 = allocation.x + range_rect.x + range_rect.width + slider_width / 2;
1096 min_pos = min_pos_after;
1097 max_pos = allocation.y + find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep;
1099 y1 = allocation.y + marks[i];
1101 gtk_paint_hline (style, window, state_type,
1102 NULL, widget, "range-mark", x1, x2, y1);
1106 pango_layout_set_markup (layout, mark->markup, -1);
1107 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1109 y3 = y1 - logical_rect.height / 2;
1112 if (y3 + logical_rect.height > max_pos)
1113 y3 = max_pos - logical_rect.height;
1114 if (y3 < allocation.y)
1116 if (mark->position == GTK_POS_LEFT)
1118 x3 = x2 - value_spacing - logical_rect.width;
1119 min_pos_before = y3 + logical_rect.height + min_sep;
1123 x3 = x2 + value_spacing;
1124 min_pos_after = y3 + logical_rect.height + min_sep;
1127 gtk_paint_layout (style, window, state_type,
1128 FALSE, NULL, widget, "scale-mark",
1134 g_object_unref (layout);
1138 if (priv->draw_value)
1140 GtkOrientation orientation;
1141 PangoLayout *layout;
1144 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1145 layout = gtk_scale_get_layout (scale);
1146 gtk_scale_get_layout_offsets (scale, &x, &y);
1148 gtk_paint_layout (style,
1154 orientation == GTK_ORIENTATION_HORIZONTAL ?
1155 "hscale" : "vscale",
1165 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1169 GtkScalePrivate *priv = scale->priv;
1170 GtkAllocation allocation;
1171 GtkWidget *widget = GTK_WIDGET (scale);
1172 GtkRange *range = GTK_RANGE (widget);
1173 GdkRectangle range_rect;
1174 PangoLayout *layout = gtk_scale_get_layout (scale);
1175 PangoRectangle logical_rect;
1176 gint slider_start, slider_end;
1187 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1189 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1191 gtk_widget_get_allocation (widget, &allocation);
1192 gtk_range_get_range_rect (range, &range_rect);
1193 gtk_range_get_slider_range (range,
1197 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1199 switch (priv->value_pos)
1202 *x = range_rect.x - value_spacing - logical_rect.width;
1203 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1207 *x = range_rect.x + range_rect.width + value_spacing;
1208 *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1212 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1213 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1214 *y = range_rect.y - logical_rect.height - value_spacing;
1217 case GTK_POS_BOTTOM:
1218 *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1219 *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1220 *y = range_rect.y + range_rect.height + value_spacing;
1224 g_return_if_reached ();
1230 switch (priv->value_pos)
1233 *x = range_rect.x - logical_rect.width - value_spacing;
1234 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1235 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1239 *x = range_rect.x + range_rect.width + value_spacing;
1240 *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1241 *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1245 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1246 *y = range_rect.y - logical_rect.height - value_spacing;
1249 case GTK_POS_BOTTOM:
1250 *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1251 *y = range_rect.y + range_rect.height + value_spacing;
1255 g_return_if_reached ();
1264 * _gtk_scale_format_value:
1265 * @scale: a #GtkScale
1266 * @value: adjustment value
1268 * Emits #GtkScale::format-value signal to format the value,
1269 * if no user signal handlers, falls back to a default format.
1271 * Return value: formatted value
1274 _gtk_scale_format_value (GtkScale *scale,
1277 GtkScalePrivate *priv = scale->priv;
1280 g_signal_emit (scale,
1281 signals[FORMAT_VALUE],
1289 /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1290 return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value);
1294 gtk_scale_finalize (GObject *object)
1296 GtkScale *scale = GTK_SCALE (object);
1298 _gtk_scale_clear_layout (scale);
1299 gtk_scale_clear_marks (scale);
1301 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1305 * gtk_scale_get_layout:
1306 * @scale: A #GtkScale
1308 * Gets the #PangoLayout used to display the scale.
1309 * The returned object is owned by the scale so does
1310 * not need to be freed by the caller.
1312 * Return value: the #PangoLayout for this scale, or %NULL
1313 * if the #GtkScale:draw-value property is %FALSE.
1318 gtk_scale_get_layout (GtkScale *scale)
1320 GtkScalePrivate *priv;
1323 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1329 if (priv->draw_value)
1330 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1333 if (priv->draw_value)
1335 txt = _gtk_scale_format_value (scale,
1336 gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1337 pango_layout_set_text (priv->layout, txt, -1);
1341 return priv->layout;
1345 * gtk_scale_get_layout_offsets:
1346 * @scale: a #GtkScale
1347 * @x: (allow-none): location to store X offset of layout, or %NULL
1348 * @y: (allow-none): location to store Y offset of layout, or %NULL
1350 * Obtains the coordinates where the scale will draw the
1351 * #PangoLayout representing the text in the scale. Remember
1352 * when using the #PangoLayout function you need to convert to
1353 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1355 * If the #GtkScale:draw-value property is %FALSE, the return
1356 * values are undefined.
1361 gtk_scale_get_layout_offsets (GtkScale *scale,
1368 g_return_if_fail (GTK_IS_SCALE (scale));
1370 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1371 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1381 _gtk_scale_clear_layout (GtkScale *scale)
1383 GtkScalePrivate *priv = scale->priv;
1385 g_return_if_fail (GTK_IS_SCALE (scale));
1389 g_object_unref (priv->layout);
1390 priv->layout = NULL;
1395 gtk_scale_mark_free (GtkScaleMark *mark)
1397 g_free (mark->markup);
1402 * gtk_scale_clear_marks:
1403 * @scale: a #GtkScale
1405 * Removes any marks that have been added with gtk_scale_add_mark().
1410 gtk_scale_clear_marks (GtkScale *scale)
1412 GtkScalePrivate *priv;
1414 g_return_if_fail (GTK_IS_SCALE (scale));
1418 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1419 g_slist_free (priv->marks);
1422 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1424 gtk_widget_queue_resize (GTK_WIDGET (scale));
1428 compare_marks (gpointer a, gpointer b)
1430 GtkScaleMark *ma, *mb;
1434 return (gint) (ma->value - mb->value);
1438 * gtk_scale_add_mark:
1439 * @scale: a #GtkScale
1440 * @value: the value at which the mark is placed, must be between
1441 * the lower and upper limits of the scales' adjustment
1442 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1443 * is drawn above the scale, anything else below. For a vertical scale,
1444 * #GTK_POS_LEFT is drawn to the left of the scale, anything else to the
1446 * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1449 * Adds a mark at @value.
1451 * A mark is indicated visually by drawing a tick mark next to the scale,
1452 * and GTK+ makes it easy for the user to position the scale exactly at the
1455 * If @markup is not %NULL, text is shown next to the tick mark.
1457 * To remove marks from a scale, use gtk_scale_clear_marks().
1462 gtk_scale_add_mark (GtkScale *scale,
1464 GtkPositionType position,
1465 const gchar *markup)
1467 GtkScalePrivate *priv;
1473 g_return_if_fail (GTK_IS_SCALE (scale));
1477 mark = g_new (GtkScaleMark, 1);
1478 mark->value = value;
1479 mark->markup = g_strdup (markup);
1480 mark->position = position;
1482 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1483 (GCompareFunc) compare_marks);
1485 n = g_slist_length (priv->marks);
1486 values = g_new (gdouble, n);
1487 for (m = priv->marks, i = 0; m; m = m->next, i++)
1490 values[i] = mark->value;
1493 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1497 gtk_widget_queue_resize (GTK_WIDGET (scale));
1500 static GtkBuildableIface *parent_buildable_iface;
1503 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1505 parent_buildable_iface = g_type_interface_peek_parent (iface);
1506 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1507 iface->custom_finished = gtk_scale_buildable_custom_finished;
1513 GtkBuilder *builder;
1515 } MarksSubparserData;
1520 GtkPositionType position;
1523 gboolean translatable;
1527 mark_data_free (MarkData *data)
1529 g_string_free (data->markup, TRUE);
1530 g_free (data->context);
1531 g_slice_free (MarkData, data);
1535 marks_start_element (GMarkupParseContext *context,
1536 const gchar *element_name,
1537 const gchar **names,
1538 const gchar **values,
1542 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1544 gint line_number, char_number;
1546 if (strcmp (element_name, "marks") == 0)
1548 else if (strcmp (element_name, "mark") == 0)
1551 gboolean has_value = FALSE;
1552 GtkPositionType position = GTK_POS_BOTTOM;
1553 const gchar *msg_context = NULL;
1554 gboolean translatable = FALSE;
1557 for (i = 0; names[i]; i++)
1559 if (strcmp (names[i], "translatable") == 0)
1561 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1564 else if (strcmp (names[i], "comments") == 0)
1566 /* do nothing, comments are for translators */
1568 else if (strcmp (names[i], "context") == 0)
1569 msg_context = values[i];
1570 else if (strcmp (names[i], "value") == 0)
1572 GValue gvalue = { 0, };
1574 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1577 value = g_value_get_double (&gvalue);
1580 else if (strcmp (names[i], "position") == 0)
1582 GValue gvalue = { 0, };
1584 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1587 position = g_value_get_enum (&gvalue);
1591 g_markup_parse_context_get_position (context,
1596 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1597 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1599 line_number, char_number, names[i], "mark");
1606 g_markup_parse_context_get_position (context,
1611 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1612 "%s:%d:%d <%s> requires attribute \"%s\"",
1614 line_number, char_number, "mark",
1619 mark = g_slice_new (MarkData);
1620 mark->value = value;
1621 mark->position = position;
1622 mark->markup = g_string_new ("");
1623 mark->context = g_strdup (msg_context);
1624 mark->translatable = translatable;
1626 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1630 g_markup_parse_context_get_position (context,
1635 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1636 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1638 line_number, char_number, element_name);
1644 marks_text (GMarkupParseContext *context,
1650 MarksSubparserData *data = (MarksSubparserData*)user_data;
1652 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1654 MarkData *mark = data->marks->data;
1656 g_string_append_len (mark->markup, text, text_len);
1660 static const GMarkupParser marks_parser =
1662 marks_start_element,
1669 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1670 GtkBuilder *builder,
1672 const gchar *tagname,
1673 GMarkupParser *parser,
1676 MarksSubparserData *parser_data;
1681 if (strcmp (tagname, "marks") == 0)
1683 parser_data = g_slice_new0 (MarksSubparserData);
1684 parser_data->scale = GTK_SCALE (buildable);
1685 parser_data->marks = NULL;
1687 *parser = marks_parser;
1688 *data = parser_data;
1692 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1693 tagname, parser, data);
1697 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1698 GtkBuilder *builder,
1700 const gchar *tagname,
1703 GtkScale *scale = GTK_SCALE (buildable);
1704 MarksSubparserData *marks_data;
1706 if (strcmp (tagname, "marks") == 0)
1711 marks_data = (MarksSubparserData *)user_data;
1713 for (m = marks_data->marks; m; m = m->next)
1715 MarkData *mdata = m->data;
1717 if (mdata->translatable && mdata->markup->len)
1718 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1720 mdata->markup->str);
1722 markup = mdata->markup->str;
1724 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1726 mark_data_free (mdata);
1729 g_slist_free (marks_data->marks);
1730 g_slice_free (MarksSubparserData, marks_data);