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"
46 #define MAX_DIGITS (64) /* don't change this,
47 * a) you don't need to and
48 * b) you might cause buffer owerflows in
49 * unrelated code portions otherwise
52 #define GTK_SCALE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SCALE, GtkScalePrivate))
54 typedef struct _GtkScalePrivate GtkScalePrivate;
56 typedef struct _GtkScaleMark GtkScaleMark;
62 GtkPositionType position;
65 struct _GtkScalePrivate
83 static guint signals[LAST_SIGNAL];
85 static void gtk_scale_set_property (GObject *object,
89 static void gtk_scale_get_property (GObject *object,
93 static void gtk_scale_size_request (GtkWidget *widget,
94 GtkRequisition *requisition);
95 static void gtk_scale_style_set (GtkWidget *widget,
97 static void gtk_scale_get_range_border (GtkRange *range,
99 static void gtk_scale_get_mark_label_size (GtkScale *scale,
100 GtkPositionType position,
107 static void gtk_scale_finalize (GObject *object);
108 static void gtk_scale_screen_changed (GtkWidget *widget,
109 GdkScreen *old_screen);
110 static gboolean gtk_scale_expose (GtkWidget *widget,
111 GdkEventExpose *event);
112 static void gtk_scale_real_get_layout_offsets (GtkScale *scale,
115 static void gtk_scale_buildable_interface_init (GtkBuildableIface *iface);
116 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
119 const gchar *tagname,
120 GMarkupParser *parser,
122 static void gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
125 const gchar *tagname,
129 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
130 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
131 gtk_scale_buildable_interface_init))
135 single_string_accumulator (GSignalInvocationHint *ihint,
137 const GValue *handler_return,
140 gboolean continue_emission;
143 str = g_value_get_string (handler_return);
144 g_value_set_string (return_accu, str);
145 continue_emission = str == NULL;
147 return continue_emission;
151 #define add_slider_binding(binding_set, keyval, mask, scroll) \
152 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
153 I_("move-slider"), 1, \
154 GTK_TYPE_SCROLL_TYPE, scroll)
157 gtk_scale_class_init (GtkScaleClass *class)
159 GObjectClass *gobject_class;
160 GtkWidgetClass *widget_class;
161 GtkRangeClass *range_class;
162 GtkBindingSet *binding_set;
164 gobject_class = G_OBJECT_CLASS (class);
165 range_class = (GtkRangeClass*) class;
166 widget_class = (GtkWidgetClass*) class;
168 gobject_class->set_property = gtk_scale_set_property;
169 gobject_class->get_property = gtk_scale_get_property;
170 gobject_class->finalize = gtk_scale_finalize;
172 widget_class->style_set = gtk_scale_style_set;
173 widget_class->screen_changed = gtk_scale_screen_changed;
174 widget_class->expose_event = gtk_scale_expose;
175 widget_class->size_request = gtk_scale_size_request;
177 range_class->slider_detail = "Xscale";
178 range_class->get_range_border = gtk_scale_get_range_border;
180 class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
183 * GtkScale::format-value:
184 * @scale: the object which received the signal
185 * @value: the value to format
187 * Signal which allows you to change how the scale value is displayed.
188 * Connect a signal handler which returns an allocated string representing
189 * @value. That string will then be used to display the scale's value.
191 * Here's an example signal handler which displays a value 1.0 as
192 * with "-->1.0<--".
195 * format_value_callback (GtkScale *scale,
198 * return g_strdup_printf ("-->%0.*g<--",
199 * gtk_scale_get_digits (scale), value);
203 * Return value: allocated string representing @value
205 signals[FORMAT_VALUE] =
206 g_signal_new (I_("format-value"),
207 G_TYPE_FROM_CLASS (gobject_class),
209 G_STRUCT_OFFSET (GtkScaleClass, format_value),
210 single_string_accumulator, NULL,
211 _gtk_marshal_STRING__DOUBLE,
215 g_object_class_install_property (gobject_class,
217 g_param_spec_int ("digits",
219 P_("The number of decimal places that are displayed in the value"),
223 GTK_PARAM_READWRITE));
225 g_object_class_install_property (gobject_class,
227 g_param_spec_boolean ("draw-value",
229 P_("Whether the current value is displayed as a string next to the slider"),
231 GTK_PARAM_READWRITE));
233 g_object_class_install_property (gobject_class,
235 g_param_spec_enum ("value-pos",
236 P_("Value Position"),
237 P_("The position in which the current value is displayed"),
238 GTK_TYPE_POSITION_TYPE,
240 GTK_PARAM_READWRITE));
242 gtk_widget_class_install_style_property (widget_class,
243 g_param_spec_int ("slider-length",
245 P_("Length of scale's slider"),
249 GTK_PARAM_READABLE));
251 gtk_widget_class_install_style_property (widget_class,
252 g_param_spec_int ("value-spacing",
254 P_("Space between value text and the slider/trough area"),
258 GTK_PARAM_READABLE));
260 /* All bindings (even arrow keys) are on both h/v scale, because
261 * blind users etc. don't care about scale orientation.
264 binding_set = gtk_binding_set_by_class (class);
266 add_slider_binding (binding_set, GDK_Left, 0,
267 GTK_SCROLL_STEP_LEFT);
269 add_slider_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
270 GTK_SCROLL_PAGE_LEFT);
272 add_slider_binding (binding_set, GDK_KP_Left, 0,
273 GTK_SCROLL_STEP_LEFT);
275 add_slider_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
276 GTK_SCROLL_PAGE_LEFT);
278 add_slider_binding (binding_set, GDK_Right, 0,
279 GTK_SCROLL_STEP_RIGHT);
281 add_slider_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
282 GTK_SCROLL_PAGE_RIGHT);
284 add_slider_binding (binding_set, GDK_KP_Right, 0,
285 GTK_SCROLL_STEP_RIGHT);
287 add_slider_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
288 GTK_SCROLL_PAGE_RIGHT);
290 add_slider_binding (binding_set, GDK_Up, 0,
293 add_slider_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
296 add_slider_binding (binding_set, GDK_KP_Up, 0,
299 add_slider_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
302 add_slider_binding (binding_set, GDK_Down, 0,
303 GTK_SCROLL_STEP_DOWN);
305 add_slider_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
306 GTK_SCROLL_PAGE_DOWN);
308 add_slider_binding (binding_set, GDK_KP_Down, 0,
309 GTK_SCROLL_STEP_DOWN);
311 add_slider_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
312 GTK_SCROLL_PAGE_DOWN);
314 add_slider_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
315 GTK_SCROLL_PAGE_LEFT);
317 add_slider_binding (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
318 GTK_SCROLL_PAGE_LEFT);
320 add_slider_binding (binding_set, GDK_Page_Up, 0,
323 add_slider_binding (binding_set, GDK_KP_Page_Up, 0,
326 add_slider_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
327 GTK_SCROLL_PAGE_RIGHT);
329 add_slider_binding (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
330 GTK_SCROLL_PAGE_RIGHT);
332 add_slider_binding (binding_set, GDK_Page_Down, 0,
333 GTK_SCROLL_PAGE_DOWN);
335 add_slider_binding (binding_set, GDK_KP_Page_Down, 0,
336 GTK_SCROLL_PAGE_DOWN);
338 /* Logical bindings (vs. visual bindings above) */
340 add_slider_binding (binding_set, GDK_plus, 0,
341 GTK_SCROLL_STEP_FORWARD);
343 add_slider_binding (binding_set, GDK_minus, 0,
344 GTK_SCROLL_STEP_BACKWARD);
346 add_slider_binding (binding_set, GDK_plus, GDK_CONTROL_MASK,
347 GTK_SCROLL_PAGE_FORWARD);
349 add_slider_binding (binding_set, GDK_minus, GDK_CONTROL_MASK,
350 GTK_SCROLL_PAGE_BACKWARD);
353 add_slider_binding (binding_set, GDK_KP_Add, 0,
354 GTK_SCROLL_STEP_FORWARD);
356 add_slider_binding (binding_set, GDK_KP_Subtract, 0,
357 GTK_SCROLL_STEP_BACKWARD);
359 add_slider_binding (binding_set, GDK_KP_Add, GDK_CONTROL_MASK,
360 GTK_SCROLL_PAGE_FORWARD);
362 add_slider_binding (binding_set, GDK_KP_Subtract, GDK_CONTROL_MASK,
363 GTK_SCROLL_PAGE_BACKWARD);
366 add_slider_binding (binding_set, GDK_Home, 0,
369 add_slider_binding (binding_set, GDK_KP_Home, 0,
372 add_slider_binding (binding_set, GDK_End, 0,
375 add_slider_binding (binding_set, GDK_KP_End, 0,
378 g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
382 gtk_scale_orientation_notify (GtkRange *range,
383 const GParamSpec *pspec)
385 range->flippable = (range->orientation == GTK_ORIENTATION_HORIZONTAL);
389 gtk_scale_init (GtkScale *scale)
391 GtkRange *range = GTK_RANGE (scale);
393 GTK_WIDGET_SET_FLAGS (scale, GTK_CAN_FOCUS);
395 range->slider_size_fixed = TRUE;
396 range->has_stepper_a = FALSE;
397 range->has_stepper_b = FALSE;
398 range->has_stepper_c = FALSE;
399 range->has_stepper_d = FALSE;
401 scale->draw_value = TRUE;
402 scale->value_pos = GTK_POS_TOP;
404 range->round_digits = scale->digits;
406 gtk_scale_orientation_notify (range, NULL);
407 g_signal_connect (scale, "notify::orientation",
408 G_CALLBACK (gtk_scale_orientation_notify),
413 gtk_scale_set_property (GObject *object,
420 scale = GTK_SCALE (object);
425 gtk_scale_set_digits (scale, g_value_get_int (value));
427 case PROP_DRAW_VALUE:
428 gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
431 gtk_scale_set_value_pos (scale, g_value_get_enum (value));
434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
440 gtk_scale_get_property (GObject *object,
447 scale = GTK_SCALE (object);
452 g_value_set_int (value, scale->digits);
454 case PROP_DRAW_VALUE:
455 g_value_set_boolean (value, scale->draw_value);
458 g_value_set_enum (value, scale->value_pos);
461 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
469 * @orientation: the scale's orientation.
470 * @adjustment: the #GtkAdjustment which sets the range of the scale, or
471 * %NULL to create a new adjustment.
473 * Creates a new #GtkScale.
475 * Return value: a new #GtkScale
480 gtk_scale_new (GtkOrientation orientation,
481 GtkAdjustment *adjustment)
483 g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
486 return g_object_new (GTK_TYPE_SCALE,
487 "orientation", orientation,
488 "adjustment", adjustment,
493 * gtk_scale_new_with_range:
494 * @orientation: the scale's orientation.
495 * @min: minimum value
496 * @max: maximum value
497 * @step: step increment (tick size) used with keyboard shortcuts
499 * Creates a new scale widget with the given orientation that lets the
500 * user input a number between @min and @max (including @min and @max)
501 * with the increment @step. @step must be nonzero; it's the distance
502 * the slider moves when using the arrow keys to adjust the scale
505 * Note that the way in which the precision is derived works best if @step
506 * is a power of ten. If the resulting precision is not suitable for your
507 * needs, use gtk_scale_set_digits() to correct it.
509 * Return value: a new #GtkScale
514 gtk_scale_new_with_range (GtkOrientation orientation,
522 g_return_val_if_fail (min < max, NULL);
523 g_return_val_if_fail (step != 0.0, NULL);
525 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
527 if (fabs (step) >= 1.0 || step == 0.0)
533 digits = abs ((gint) floor (log10 (fabs (step))));
538 return g_object_new (GTK_TYPE_SCALE,
539 "orientation", orientation,
547 * gtk_scale_set_digits:
548 * @scale: a #GtkScale
549 * @digits: the number of decimal places to display,
550 * e.g. use 1 to display 1.0, 2 to display 1.00, etc
552 * Sets the number of decimal places that are displayed in the value.
553 * Also causes the value of the adjustment to be rounded off to this
554 * number of digits, so the retrieved value matches the value the user saw.
557 gtk_scale_set_digits (GtkScale *scale,
562 g_return_if_fail (GTK_IS_SCALE (scale));
564 range = GTK_RANGE (scale);
566 digits = CLAMP (digits, -1, MAX_DIGITS);
568 if (scale->digits != digits)
570 scale->digits = digits;
571 if (scale->draw_value)
572 range->round_digits = digits;
574 _gtk_scale_clear_layout (scale);
575 gtk_widget_queue_resize (GTK_WIDGET (scale));
577 g_object_notify (G_OBJECT (scale), "digits");
582 * gtk_scale_get_digits:
583 * @scale: a #GtkScale
585 * Gets the number of decimal places that are displayed in the value.
587 * Returns: the number of decimal places that are displayed
590 gtk_scale_get_digits (GtkScale *scale)
592 g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
594 return scale->digits;
598 * gtk_scale_set_draw_value:
599 * @scale: a #GtkScale
600 * @draw_value: %TRUE to draw the value
602 * Specifies whether the current value is displayed as a string next
606 gtk_scale_set_draw_value (GtkScale *scale,
609 g_return_if_fail (GTK_IS_SCALE (scale));
611 draw_value = draw_value != FALSE;
613 if (scale->draw_value != draw_value)
615 scale->draw_value = draw_value;
617 GTK_RANGE (scale)->round_digits = scale->digits;
619 GTK_RANGE (scale)->round_digits = -1;
621 _gtk_scale_clear_layout (scale);
623 gtk_widget_queue_resize (GTK_WIDGET (scale));
625 g_object_notify (G_OBJECT (scale), "draw-value");
630 * gtk_scale_get_draw_value:
631 * @scale: a #GtkScale
633 * Returns whether the current value is displayed as a string
634 * next to the slider.
636 * Returns: whether the current value is displayed as a string
639 gtk_scale_get_draw_value (GtkScale *scale)
641 g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
643 return scale->draw_value;
647 * gtk_scale_set_value_pos:
648 * @scale: a #GtkScale
649 * @pos: the position in which the current value is displayed
651 * Sets the position in which the current value is displayed.
654 gtk_scale_set_value_pos (GtkScale *scale,
657 g_return_if_fail (GTK_IS_SCALE (scale));
659 if (scale->value_pos != pos)
661 scale->value_pos = pos;
663 _gtk_scale_clear_layout (scale);
664 if (GTK_WIDGET_VISIBLE (scale) && GTK_WIDGET_MAPPED (scale))
665 gtk_widget_queue_resize (GTK_WIDGET (scale));
667 g_object_notify (G_OBJECT (scale), "value-pos");
672 * gtk_scale_get_value_pos:
673 * @scale: a #GtkScale
675 * Gets the position in which the current value is displayed.
677 * Returns: the position in which the current value is displayed
680 gtk_scale_get_value_pos (GtkScale *scale)
682 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
684 return scale->value_pos;
688 gtk_scale_get_range_border (GtkRange *range,
691 GtkScalePrivate *priv;
696 widget = GTK_WIDGET (range);
697 scale = GTK_SCALE (range);
698 priv = GTK_SCALE_GET_PRIVATE (scale);
700 _gtk_scale_get_value_size (scale, &w, &h);
707 if (scale->draw_value)
710 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
712 switch (scale->value_pos)
715 border->left += w + value_spacing;
718 border->right += w + value_spacing;
721 border->top += h + value_spacing;
724 border->bottom += h + value_spacing;
733 gint n1, w1, h1, n2, w2, h2;
735 gtk_widget_style_get (widget,
736 "slider-width", &slider_width,
737 "value-spacing", &value_spacing,
741 if (GTK_RANGE (scale)->orientation == GTK_ORIENTATION_HORIZONTAL)
743 gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
745 border->top += h1 + value_spacing + slider_width / 2;
747 border->bottom += h2 + value_spacing + slider_width / 2;
751 gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
753 border->left += w1 + value_spacing + slider_width / 2;
755 border->right += w2 + value_spacing + slider_width / 2;
760 /* FIXME this could actually be static at the moment. */
762 _gtk_scale_get_value_size (GtkScale *scale,
768 g_return_if_fail (GTK_IS_SCALE (scale));
770 if (scale->draw_value)
773 PangoRectangle logical_rect;
776 range = GTK_RANGE (scale);
778 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
780 txt = _gtk_scale_format_value (scale, range->adjustment->lower);
781 pango_layout_set_text (layout, txt, -1);
784 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
787 *width = logical_rect.width;
789 *height = logical_rect.height;
791 txt = _gtk_scale_format_value (scale, range->adjustment->upper);
792 pango_layout_set_text (layout, txt, -1);
795 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
798 *width = MAX (*width, logical_rect.width);
800 *height = MAX (*height, logical_rect.height);
802 g_object_unref (layout);
815 gtk_scale_get_mark_label_size (GtkScale *scale,
816 GtkPositionType position,
824 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
826 PangoRectangle logical_rect;
830 *count1 = *count2 = 0;
831 *width1 = *width2 = 0;
832 *height1 = *height2 = 0;
834 layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
836 for (m = priv->marks; m; m = m->next)
838 GtkScaleMark *mark = m->data;
842 pango_layout_set_markup (layout, mark->markup, -1);
843 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
845 w = logical_rect.width;
846 h = logical_rect.height;
854 if (mark->position == position)
857 *width1 = MAX (*width1, w);
858 *height1 = MAX (*height1, h);
863 *width2 = MAX (*width2, w);
864 *height2 = MAX (*height2, h);
868 g_object_unref (layout);
872 gtk_scale_style_set (GtkWidget *widget,
878 range = GTK_RANGE (widget);
880 gtk_widget_style_get (widget,
881 "slider-length", &slider_length,
884 range->min_slider_size = slider_length;
886 _gtk_scale_clear_layout (GTK_SCALE (widget));
888 GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_set (widget, previous);
892 gtk_scale_screen_changed (GtkWidget *widget,
893 GdkScreen *old_screen)
895 _gtk_scale_clear_layout (GTK_SCALE (widget));
899 gtk_scale_size_request (GtkWidget *widget,
900 GtkRequisition *requisition)
902 GtkRange *range = GTK_RANGE (widget);
903 gint n1, w1, h1, n2, w2, h2;
906 GTK_WIDGET_CLASS (gtk_scale_parent_class)->size_request (widget, requisition);
908 gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
911 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
913 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
915 w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
916 w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
917 requisition->width = MAX (requisition->width, MAX (w1, w2));
921 gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
922 h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
923 h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
924 requisition->height = MAX (requisition->height, MAX (h1, h2));
929 find_next_pos (GtkWidget *widget,
938 for (m = list->next, i = 1; m; m = m->next, i++)
940 GtkScaleMark *mark = m->data;
942 if (match == (mark->position == pos))
946 return widget->allocation.width;
950 gtk_scale_expose (GtkWidget *widget,
951 GdkEventExpose *event)
953 GtkScale *scale = GTK_SCALE (widget);
954 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
955 GtkRange *range = GTK_RANGE (scale);
956 GtkStateType state_type;
964 gtk_widget_style_get (widget,
965 "focus-padding", &focus_padding,
966 "slider-width", &slider_width,
967 "value-spacing", &value_spacing,
970 /* We need to chain up _first_ so the various geometry members of
971 * GtkRange struct are updated.
973 GTK_WIDGET_CLASS (gtk_scale_parent_class)->expose_event (widget, event);
975 state_type = GTK_STATE_NORMAL;
976 if (!GTK_WIDGET_IS_SENSITIVE (widget))
977 state_type = GTK_STATE_INSENSITIVE;
982 gint x1, x2, x3, y1, y2, y3;
984 PangoRectangle logical_rect;
986 gint min_pos_before, min_pos_after;
987 gint min_pos, max_pos;
989 n_marks = _gtk_range_get_stop_positions (range, &marks);
990 layout = gtk_widget_create_pango_layout (widget, NULL);
992 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
993 min_pos_before = min_pos_after = widget->allocation.x;
995 min_pos_before = min_pos_after = widget->allocation.y;
996 for (m = priv->marks, i = 0; m; m = m->next, i++)
998 GtkScaleMark *mark = m->data;
1000 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
1002 x1 = widget->allocation.x + marks[i];
1003 if (mark->position == GTK_POS_TOP)
1005 y1 = widget->allocation.y + range->range_rect.y;
1006 y2 = y1 - slider_width / 2;
1007 min_pos = min_pos_before;
1008 max_pos = widget->allocation.x + find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1012 y1 = widget->allocation.y + range->range_rect.y + range->range_rect.height;
1013 y2 = y1 + slider_width / 2;
1014 min_pos = min_pos_after;
1015 max_pos = widget->allocation.x + find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep;
1018 gtk_paint_vline (widget->style, widget->window, state_type,
1019 NULL, widget, "scale-mark", y1, y2, x1);
1023 pango_layout_set_markup (layout, mark->markup, -1);
1024 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1026 x3 = x1 - logical_rect.width / 2;
1029 if (x3 + logical_rect.width > max_pos)
1030 x3 = max_pos - logical_rect.width;
1031 if (x3 < widget->allocation.x)
1032 x3 = widget->allocation.x;
1033 if (mark->position == GTK_POS_TOP)
1035 y3 = y2 - value_spacing - logical_rect.height;
1036 min_pos_before = x3 + logical_rect.width + min_sep;
1040 y3 = y2 + value_spacing;
1041 min_pos_after = x3 + logical_rect.width + min_sep;
1044 gtk_paint_layout (widget->style, widget->window, state_type,
1045 FALSE, NULL, widget, "scale-mark",
1051 if (mark->position == GTK_POS_LEFT)
1053 x1 = widget->allocation.x + range->range_rect.x;
1054 x2 = widget->allocation.x + range->range_rect.x - slider_width / 2;
1055 min_pos = min_pos_before;
1056 max_pos = widget->allocation.y + find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep;
1060 x1 = widget->allocation.x + range->range_rect.x + range->range_rect.width;
1061 x2 = widget->allocation.x + range->range_rect.x + range->range_rect.width + slider_width / 2;
1062 min_pos = min_pos_after;
1063 max_pos = widget->allocation.y + find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep;
1065 y1 = widget->allocation.y + marks[i];
1067 gtk_paint_hline (widget->style, widget->window, state_type,
1068 NULL, widget, "range-mark", x1, x2, y1);
1072 pango_layout_set_markup (layout, mark->markup, -1);
1073 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1075 y3 = y1 - logical_rect.height / 2;
1078 if (y3 + logical_rect.height > max_pos)
1079 y3 = max_pos - logical_rect.height;
1080 if (y3 < widget->allocation.y)
1081 y3 = widget->allocation.y;
1082 if (mark->position == GTK_POS_LEFT)
1084 x3 = x2 - value_spacing - logical_rect.width;
1085 min_pos_before = y3 + logical_rect.height + min_sep;
1089 x3 = x2 + value_spacing;
1090 min_pos_after = y3 + logical_rect.height + min_sep;
1093 gtk_paint_layout (widget->style, widget->window, state_type,
1094 FALSE, NULL, widget, "scale-mark",
1100 g_object_unref (layout);
1104 if (scale->draw_value)
1106 PangoLayout *layout;
1109 layout = gtk_scale_get_layout (scale);
1110 gtk_scale_get_layout_offsets (scale, &x, &y);
1112 gtk_paint_layout (widget->style,
1118 range->orientation == GTK_ORIENTATION_HORIZONTAL ?
1119 "hscale" : "vscale",
1129 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1133 GtkWidget *widget = GTK_WIDGET (scale);
1134 GtkRange *range = GTK_RANGE (widget);
1135 PangoLayout *layout = gtk_scale_get_layout (scale);
1136 PangoRectangle logical_rect;
1147 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1149 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1151 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
1153 switch (scale->value_pos)
1156 *x = range->range_rect.x - value_spacing - logical_rect.width;
1157 *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
1161 *x = range->range_rect.x + range->range_rect.width + value_spacing;
1162 *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
1166 *x = range->slider_start +
1167 (range->slider_end - range->slider_start - logical_rect.width) / 2;
1168 *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
1169 *y = range->range_rect.y - logical_rect.height - value_spacing;
1172 case GTK_POS_BOTTOM:
1173 *x = range->slider_start +
1174 (range->slider_end - range->slider_start - logical_rect.width) / 2;
1175 *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
1176 *y = range->range_rect.y + range->range_rect.height + value_spacing;
1180 g_return_if_reached ();
1186 switch (scale->value_pos)
1189 *x = range->range_rect.x - logical_rect.width - value_spacing;
1190 *y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
1191 *y = CLAMP (*y, 0, widget->allocation.height - logical_rect.height);
1195 *x = range->range_rect.x + range->range_rect.width + value_spacing;
1196 *y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
1197 *y = CLAMP (*y, 0, widget->allocation.height - logical_rect.height);
1201 *x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
1202 *y = range->range_rect.y - logical_rect.height - value_spacing;
1205 case GTK_POS_BOTTOM:
1206 *x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
1207 *y = range->range_rect.y + range->range_rect.height + value_spacing;
1211 g_return_if_reached ();
1215 *x += widget->allocation.x;
1216 *y += widget->allocation.y;
1220 * _gtk_scale_format_value:
1221 * @scale: a #GtkScale
1222 * @value: adjustment value
1224 * Emits #GtkScale::format-value signal to format the value,
1225 * if no user signal handlers, falls back to a default format.
1227 * Return value: formatted value
1230 _gtk_scale_format_value (GtkScale *scale,
1235 g_signal_emit (scale,
1236 signals[FORMAT_VALUE],
1244 /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1245 return g_strdup_printf ("\342\200\216%0.*f", scale->digits, value);
1249 gtk_scale_finalize (GObject *object)
1251 GtkScale *scale = GTK_SCALE (object);
1253 _gtk_scale_clear_layout (scale);
1254 gtk_scale_clear_marks (scale);
1256 G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1260 * gtk_scale_get_layout:
1261 * @scale: A #GtkScale
1263 * Gets the #PangoLayout used to display the scale.
1264 * The returned object is owned by the scale so does
1265 * not need to be freed by the caller.
1267 * Return value: the #PangoLayout for this scale, or %NULL
1268 * if the #GtkScale:draw-value property is %FALSE.
1273 gtk_scale_get_layout (GtkScale *scale)
1275 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
1278 g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1282 if (scale->draw_value)
1283 priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1286 if (scale->draw_value)
1288 txt = _gtk_scale_format_value (scale,
1289 GTK_RANGE (scale)->adjustment->value);
1290 pango_layout_set_text (priv->layout, txt, -1);
1294 return priv->layout;
1298 * gtk_scale_get_layout_offsets:
1299 * @scale: a #GtkScale
1300 * @x: location to store X offset of layout, or %NULL
1301 * @y: location to store Y offset of layout, or %NULL
1303 * Obtains the coordinates where the scale will draw the
1304 * #PangoLayout representing the text in the scale. Remember
1305 * when using the #PangoLayout function you need to convert to
1306 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
1308 * If the #GtkScale:draw-value property is %FALSE, the return
1309 * values are undefined.
1314 gtk_scale_get_layout_offsets (GtkScale *scale,
1321 g_return_if_fail (GTK_IS_SCALE (scale));
1323 if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1324 (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1334 _gtk_scale_clear_layout (GtkScale *scale)
1336 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
1338 g_return_if_fail (GTK_IS_SCALE (scale));
1342 g_object_unref (priv->layout);
1343 priv->layout = NULL;
1348 gtk_scale_mark_free (GtkScaleMark *mark)
1350 g_free (mark->markup);
1355 * gtk_scale_clear_marks:
1356 * @scale: a #GtkScale
1358 * Removes any marks that have been added with gtk_scale_add_mark().
1363 gtk_scale_clear_marks (GtkScale *scale)
1365 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
1367 g_return_if_fail (GTK_IS_SCALE (scale));
1369 g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1370 g_slist_free (priv->marks);
1373 _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1375 gtk_widget_queue_resize (GTK_WIDGET (scale));
1379 compare_marks (gpointer a, gpointer b)
1381 GtkScaleMark *ma, *mb;
1385 return (gint) (ma->value - mb->value);
1389 * gtk_scale_add_mark:
1390 * @scale: a #GtkScale
1391 * @value: the value at which the mark is placed, must be between
1392 * the lower and upper limits of the scales' adjustment
1393 * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1394 * is drawn above the scale, anything else below. For a vertical scale,
1395 * #GTK_POS_LEFT is drawn to the left of the scale, anything else to the
1397 * @markup: Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1400 * Adds a mark at @value.
1402 * A mark is indicated visually by drawing a tick mark next to the scale,
1403 * and GTK+ makes it easy for the user to position the scale exactly at the
1406 * If @markup is not %NULL, text is shown next to the tick mark.
1408 * To remove marks from a scale, use gtk_scale_clear_marks().
1413 gtk_scale_add_mark (GtkScale *scale,
1415 GtkPositionType position,
1416 const gchar *markup)
1418 GtkScalePrivate *priv = GTK_SCALE_GET_PRIVATE (scale);
1424 mark = g_new (GtkScaleMark, 1);
1425 mark->value = value;
1426 mark->markup = g_strdup (markup);
1427 mark->position = position;
1429 priv->marks = g_slist_insert_sorted (priv->marks, mark,
1430 (GCompareFunc) compare_marks);
1432 n = g_slist_length (priv->marks);
1433 values = g_new (gdouble, n);
1434 for (m = priv->marks, i = 0; m; m = m->next, i++)
1437 values[i] = mark->value;
1440 _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1444 gtk_widget_queue_resize (GTK_WIDGET (scale));
1447 static GtkBuildableIface *parent_buildable_iface;
1450 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1452 parent_buildable_iface = g_type_interface_peek_parent (iface);
1453 iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1454 iface->custom_finished = gtk_scale_buildable_custom_finished;
1460 GtkBuilder *builder;
1462 } MarksSubparserData;
1467 GtkPositionType position;
1470 gboolean translatable;
1474 mark_data_free (MarkData *data)
1476 g_string_free (data->markup, TRUE);
1477 g_free (data->context);
1478 g_slice_free (MarkData, data);
1482 marks_start_element (GMarkupParseContext *context,
1483 const gchar *element_name,
1484 const gchar **names,
1485 const gchar **values,
1489 MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1491 gint line_number, char_number;
1493 if (strcmp (element_name, "marks") == 0)
1495 else if (strcmp (element_name, "mark") == 0)
1498 gboolean has_value = FALSE;
1499 GtkPositionType position = GTK_POS_BOTTOM;
1500 const gchar *msg_context = NULL;
1501 gboolean translatable = FALSE;
1504 for (i = 0; names[i]; i++)
1506 if (strcmp (names[i], "translatable") == 0)
1508 if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1511 else if (strcmp (names[i], "comments") == 0)
1513 /* do nothing, comments are for translators */
1515 else if (strcmp (names[i], "context") == 0)
1516 msg_context = values[i];
1517 else if (strcmp (names[i], "value") == 0)
1519 GValue gvalue = { 0, };
1521 if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1524 value = g_value_get_double (&gvalue);
1527 else if (strcmp (names[i], "position") == 0)
1529 GValue gvalue = { 0, };
1531 if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1534 position = g_value_get_enum (&gvalue);
1538 g_markup_parse_context_get_position (context,
1543 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1544 "%s:%d:%d '%s' is not a valid attribute of <%s>",
1546 line_number, char_number, names[i], "mark");
1553 g_markup_parse_context_get_position (context,
1558 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1559 "%s:%d:%d <%s> requires attribute \"%s\"",
1561 line_number, char_number, "mark",
1566 mark = g_slice_new (MarkData);
1567 mark->value = value;
1568 mark->position = position;
1569 mark->markup = g_string_new ("");
1570 mark->context = g_strdup (msg_context);
1571 mark->translatable = translatable;
1573 parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1577 g_markup_parse_context_get_position (context,
1582 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1583 "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1585 line_number, char_number, element_name);
1591 marks_text (GMarkupParseContext *context,
1597 MarksSubparserData *data = (MarksSubparserData*)user_data;
1599 if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1601 MarkData *mark = data->marks->data;
1603 g_string_append_len (mark->markup, text, text_len);
1607 static const GMarkupParser marks_parser =
1609 marks_start_element,
1616 gtk_scale_buildable_custom_tag_start (GtkBuildable *buildable,
1617 GtkBuilder *builder,
1619 const gchar *tagname,
1620 GMarkupParser *parser,
1623 MarksSubparserData *parser_data;
1628 if (strcmp (tagname, "marks") == 0)
1630 parser_data = g_slice_new0 (MarksSubparserData);
1631 parser_data->scale = GTK_SCALE (buildable);
1632 parser_data->marks = NULL;
1634 *parser = marks_parser;
1635 *data = parser_data;
1639 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1640 tagname, parser, data);
1644 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1645 GtkBuilder *builder,
1647 const gchar *tagname,
1650 GtkScale *scale = GTK_SCALE (buildable);
1651 MarksSubparserData *marks_data;
1653 if (strcmp (tagname, "marks") == 0)
1658 marks_data = (MarksSubparserData *)user_data;
1660 for (m = marks_data->marks; m; m = m->next)
1662 MarkData *mdata = m->data;
1664 if (mdata->translatable && mdata->markup->len)
1665 markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1667 mdata->markup->str);
1669 markup = mdata->markup->str;
1671 gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1673 mark_data_free (mdata);
1676 g_slist_free (marks_data->marks);
1677 g_slice_free (MarksSubparserData, marks_data);
1681 #define __GTK_SCALE_C__
1682 #include "gtkaliasdef.c"