]> Pileus Git - ~andy/gtk/blob - gtk/gtkscale.c
a9a98800066ba4ed8c0185672bb06b8fe920e57d
[~andy/gtk] / gtk / gtkscale.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  * Copyright (C) 2001 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /*
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/.
26  */
27
28 #include "config.h"
29
30 #include <math.h>
31 #include <stdlib.h>
32
33 #include "gtkscaleprivate.h"
34 #include "gtkiconfactory.h"
35 #include "gtkicontheme.h"
36 #include "gtkmarshalers.h"
37 #include "gtkbindings.h"
38 #include "gtkorientable.h"
39 #include "gtktypebuiltins.h"
40 #include "gtkprivate.h"
41 #include "gtkintl.h"
42 #include "gtkbuildable.h"
43 #include "gtkbuilderprivate.h"
44
45
46 /**
47  * SECTION:gtkscale
48  * @Short_description: Base class for GtkHScale and GtkVScale
49  * @Title: GtkScale
50  *
51  * A GtkScale is a slider control used to select a numeric value.
52  * To use it, you'll probably want to investigate the methods on
53  * its base class, #GtkRange, in addition to the methods for GtkScale itself.
54  * To set the value of a scale, you would normally use gtk_range_set_value().
55  * To detect changes to the value, you would normally use the
56  * #GtkRange::value-changed signal.
57  *
58  * Note that using the same upper and lower bounds for the #GtkScale (through
59  * the #GtkRange methods) will hide the slider itself. This is useful for
60  * applications that want to show an undeterminate value on the scale, without
61  * changing the layout of the application (such as movie or music players).
62  *
63  * <refsect2 id="GtkScale-BUILDER-UI"><title>GtkScale as GtkBuildable</title>
64  * GtkScale supports a custom &lt;marks&gt; element, which
65  * can contain multiple &lt;mark&gt; elements. The "value" and "position"
66  * attributes have the same meaning as gtk_scale_add_mark() parameters of the
67  * same name. If the element is not empty, its content is taken as the markup
68  * to show at the mark. It can be translated with the usual "translatable and
69  * "context" attributes.
70  * </refsect2>
71  */
72
73
74 #define MAX_DIGITS      (64)    /* don't change this,
75                                  * a) you don't need to and
76                                  * b) you might cause buffer owerflows in
77                                  *    unrelated code portions otherwise
78                                  */
79
80
81 typedef struct _GtkScaleMark GtkScaleMark;
82
83 struct _GtkScalePrivate
84 {
85   PangoLayout  *layout;
86
87   GSList       *marks;
88
89   gint          digits;
90
91   guint         draw_value : 1;
92   guint         value_pos  : 2;
93 };
94
95 struct _GtkScaleMark
96 {
97   gdouble          value;
98   gchar           *markup;
99   GtkPositionType  position; /* always GTK_POS_TOP or GTK_POS_BOTTOM */
100 };
101
102 enum {
103   PROP_0,
104   PROP_DIGITS,
105   PROP_DRAW_VALUE,
106   PROP_VALUE_POS
107 };
108
109 enum {
110   FORMAT_VALUE,
111   LAST_SIGNAL
112 };
113
114 static guint signals[LAST_SIGNAL];
115
116 static void     gtk_scale_set_property            (GObject        *object,
117                                                    guint           prop_id,
118                                                    const GValue   *value,
119                                                    GParamSpec     *pspec);
120 static void     gtk_scale_get_property            (GObject        *object,
121                                                    guint           prop_id,
122                                                    GValue         *value,
123                                                    GParamSpec     *pspec);
124 static void     gtk_scale_get_preferred_width     (GtkWidget      *widget,
125                                                    gint           *minimum,
126                                                    gint           *natural);
127 static void     gtk_scale_get_preferred_height    (GtkWidget      *widget,
128                                                    gint           *minimum,
129                                                    gint           *natural);
130 static void     gtk_scale_style_updated           (GtkWidget      *widget);
131 static void     gtk_scale_get_range_border        (GtkRange       *range,
132                                                    GtkBorder      *border);
133 static void     gtk_scale_get_mark_label_size     (GtkScale        *scale,
134                                                    GtkPositionType  position,
135                                                    gint            *count1,
136                                                    gint            *width1,
137                                                    gint            *height1,
138                                                    gint            *count2,
139                                                    gint            *width2,
140                                                    gint            *height2);
141 static void     gtk_scale_finalize                (GObject        *object);
142 static void     gtk_scale_screen_changed          (GtkWidget      *widget,
143                                                    GdkScreen      *old_screen);
144 static gboolean gtk_scale_draw                    (GtkWidget      *widget,
145                                                    cairo_t        *cr);
146 static void     gtk_scale_real_get_layout_offsets (GtkScale       *scale,
147                                                    gint           *x,
148                                                    gint           *y);
149 static void     gtk_scale_buildable_interface_init   (GtkBuildableIface *iface);
150 static gboolean gtk_scale_buildable_custom_tag_start (GtkBuildable  *buildable,
151                                                       GtkBuilder    *builder,
152                                                       GObject       *child,
153                                                       const gchar   *tagname,
154                                                       GMarkupParser *parser,
155                                                       gpointer      *data);
156 static void     gtk_scale_buildable_custom_finished  (GtkBuildable  *buildable,
157                                                       GtkBuilder    *builder,
158                                                       GObject       *child,
159                                                       const gchar   *tagname,
160                                                       gpointer       user_data);
161
162
163 G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE,
164                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
165                                                 gtk_scale_buildable_interface_init))
166
167
168 static gboolean
169 single_string_accumulator (GSignalInvocationHint *ihint,
170                            GValue                *return_accu,
171                            const GValue          *handler_return,
172                            gpointer               dummy)
173 {
174   gboolean continue_emission;
175   const gchar *str;
176   
177   str = g_value_get_string (handler_return);
178   g_value_set_string (return_accu, str);
179   continue_emission = str == NULL;
180   
181   return continue_emission;
182 }
183
184
185 #define add_slider_binding(binding_set, keyval, mask, scroll)              \
186   gtk_binding_entry_add_signal (binding_set, keyval, mask,                 \
187                                 I_("move-slider"), 1, \
188                                 GTK_TYPE_SCROLL_TYPE, scroll)
189
190 static void
191 gtk_scale_class_init (GtkScaleClass *class)
192 {
193   GObjectClass   *gobject_class;
194   GtkWidgetClass *widget_class;
195   GtkRangeClass  *range_class;
196   GtkBindingSet  *binding_set;
197   
198   gobject_class = G_OBJECT_CLASS (class);
199   range_class = (GtkRangeClass*) class;
200   widget_class = (GtkWidgetClass*) class;
201   
202   gobject_class->set_property = gtk_scale_set_property;
203   gobject_class->get_property = gtk_scale_get_property;
204   gobject_class->finalize = gtk_scale_finalize;
205
206   widget_class->style_updated = gtk_scale_style_updated;
207   widget_class->screen_changed = gtk_scale_screen_changed;
208   widget_class->draw = gtk_scale_draw;
209   widget_class->get_preferred_width = gtk_scale_get_preferred_width;
210   widget_class->get_preferred_height = gtk_scale_get_preferred_height;
211
212   range_class->slider_detail = "Xscale";
213   range_class->get_range_border = gtk_scale_get_range_border;
214
215   class->get_layout_offsets = gtk_scale_real_get_layout_offsets;
216
217   /**
218    * GtkScale::format-value:
219    * @scale: the object which received the signal
220    * @value: the value to format
221    *
222    * Signal which allows you to change how the scale value is displayed.
223    * Connect a signal handler which returns an allocated string representing 
224    * @value. That string will then be used to display the scale's value.
225    *
226    * Here's an example signal handler which displays a value 1.0 as
227    * with "--&gt;1.0&lt;--".
228    * |[
229    * static gchar*
230    * format_value_callback (GtkScale *scale,
231    *                        gdouble   value)
232    * {
233    *   return g_strdup_printf ("--&gt;&percnt;0.*g&lt;--",
234    *                           gtk_scale_get_digits (scale), value);
235    *  }
236    * ]|
237    *
238    * Return value: allocated string representing @value
239    */
240   signals[FORMAT_VALUE] =
241     g_signal_new (I_("format-value"),
242                   G_TYPE_FROM_CLASS (gobject_class),
243                   G_SIGNAL_RUN_LAST,
244                   G_STRUCT_OFFSET (GtkScaleClass, format_value),
245                   single_string_accumulator, NULL,
246                   _gtk_marshal_STRING__DOUBLE,
247                   G_TYPE_STRING, 1,
248                   G_TYPE_DOUBLE);
249
250   g_object_class_install_property (gobject_class,
251                                    PROP_DIGITS,
252                                    g_param_spec_int ("digits",
253                                                      P_("Digits"),
254                                                      P_("The number of decimal places that are displayed in the value"),
255                                                      -1,
256                                                      MAX_DIGITS,
257                                                      1,
258                                                      GTK_PARAM_READWRITE));
259   
260   g_object_class_install_property (gobject_class,
261                                    PROP_DRAW_VALUE,
262                                    g_param_spec_boolean ("draw-value",
263                                                          P_("Draw Value"),
264                                                          P_("Whether the current value is displayed as a string next to the slider"),
265                                                          TRUE,
266                                                          GTK_PARAM_READWRITE));
267   
268   g_object_class_install_property (gobject_class,
269                                    PROP_VALUE_POS,
270                                    g_param_spec_enum ("value-pos",
271                                                       P_("Value Position"),
272                                                       P_("The position in which the current value is displayed"),
273                                                       GTK_TYPE_POSITION_TYPE,
274                                                       GTK_POS_TOP,
275                                                       GTK_PARAM_READWRITE));
276
277   gtk_widget_class_install_style_property (widget_class,
278                                            g_param_spec_int ("slider-length",
279                                                              P_("Slider Length"),
280                                                              P_("Length of scale's slider"),
281                                                              0,
282                                                              G_MAXINT,
283                                                              31,
284                                                              GTK_PARAM_READABLE));
285
286   gtk_widget_class_install_style_property (widget_class,
287                                            g_param_spec_int ("value-spacing",
288                                                              P_("Value spacing"),
289                                                              P_("Space between value text and the slider/trough area"),
290                                                              0,
291                                                              G_MAXINT,
292                                                              2,
293                                                              GTK_PARAM_READABLE));
294   
295   /* All bindings (even arrow keys) are on both h/v scale, because
296    * blind users etc. don't care about scale orientation.
297    */
298   
299   binding_set = gtk_binding_set_by_class (class);
300
301   add_slider_binding (binding_set, GDK_KEY_Left, 0,
302                       GTK_SCROLL_STEP_LEFT);
303
304   add_slider_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
305                       GTK_SCROLL_PAGE_LEFT);
306
307   add_slider_binding (binding_set, GDK_KEY_KP_Left, 0,
308                       GTK_SCROLL_STEP_LEFT);
309
310   add_slider_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
311                       GTK_SCROLL_PAGE_LEFT);
312
313   add_slider_binding (binding_set, GDK_KEY_Right, 0,
314                       GTK_SCROLL_STEP_RIGHT);
315
316   add_slider_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
317                       GTK_SCROLL_PAGE_RIGHT);
318
319   add_slider_binding (binding_set, GDK_KEY_KP_Right, 0,
320                       GTK_SCROLL_STEP_RIGHT);
321
322   add_slider_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
323                       GTK_SCROLL_PAGE_RIGHT);
324
325   add_slider_binding (binding_set, GDK_KEY_Up, 0,
326                       GTK_SCROLL_STEP_UP);
327
328   add_slider_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
329                       GTK_SCROLL_PAGE_UP);
330
331   add_slider_binding (binding_set, GDK_KEY_KP_Up, 0,
332                       GTK_SCROLL_STEP_UP);
333
334   add_slider_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
335                       GTK_SCROLL_PAGE_UP);
336
337   add_slider_binding (binding_set, GDK_KEY_Down, 0,
338                       GTK_SCROLL_STEP_DOWN);
339
340   add_slider_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
341                       GTK_SCROLL_PAGE_DOWN);
342
343   add_slider_binding (binding_set, GDK_KEY_KP_Down, 0,
344                       GTK_SCROLL_STEP_DOWN);
345
346   add_slider_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
347                       GTK_SCROLL_PAGE_DOWN);
348    
349   add_slider_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
350                       GTK_SCROLL_PAGE_LEFT);
351
352   add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
353                       GTK_SCROLL_PAGE_LEFT);  
354
355   add_slider_binding (binding_set, GDK_KEY_Page_Up, 0,
356                       GTK_SCROLL_PAGE_UP);
357
358   add_slider_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
359                       GTK_SCROLL_PAGE_UP);
360   
361   add_slider_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
362                       GTK_SCROLL_PAGE_RIGHT);
363
364   add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
365                       GTK_SCROLL_PAGE_RIGHT);
366
367   add_slider_binding (binding_set, GDK_KEY_Page_Down, 0,
368                       GTK_SCROLL_PAGE_DOWN);
369
370   add_slider_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
371                       GTK_SCROLL_PAGE_DOWN);
372
373   /* Logical bindings (vs. visual bindings above) */
374
375   add_slider_binding (binding_set, GDK_KEY_plus, 0,
376                       GTK_SCROLL_STEP_FORWARD);  
377
378   add_slider_binding (binding_set, GDK_KEY_minus, 0,
379                       GTK_SCROLL_STEP_BACKWARD);  
380
381   add_slider_binding (binding_set, GDK_KEY_plus, GDK_CONTROL_MASK,
382                       GTK_SCROLL_PAGE_FORWARD);  
383
384   add_slider_binding (binding_set, GDK_KEY_minus, GDK_CONTROL_MASK,
385                       GTK_SCROLL_PAGE_BACKWARD);
386
387
388   add_slider_binding (binding_set, GDK_KEY_KP_Add, 0,
389                       GTK_SCROLL_STEP_FORWARD);  
390
391   add_slider_binding (binding_set, GDK_KEY_KP_Subtract, 0,
392                       GTK_SCROLL_STEP_BACKWARD);  
393
394   add_slider_binding (binding_set, GDK_KEY_KP_Add, GDK_CONTROL_MASK,
395                       GTK_SCROLL_PAGE_FORWARD);  
396
397   add_slider_binding (binding_set, GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
398                       GTK_SCROLL_PAGE_BACKWARD);
399   
400   
401   add_slider_binding (binding_set, GDK_KEY_Home, 0,
402                       GTK_SCROLL_START);
403
404   add_slider_binding (binding_set, GDK_KEY_KP_Home, 0,
405                       GTK_SCROLL_START);
406
407   add_slider_binding (binding_set, GDK_KEY_End, 0,
408                       GTK_SCROLL_END);
409
410   add_slider_binding (binding_set, GDK_KEY_KP_End, 0,
411                       GTK_SCROLL_END);
412
413   g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate));
414 }
415
416 static void
417 gtk_scale_orientation_notify (GtkRange         *range,
418                               const GParamSpec *pspec)
419 {
420   GtkOrientation orientation;
421
422   orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
423   gtk_range_set_flippable (range,
424                            orientation == GTK_ORIENTATION_HORIZONTAL);
425 }
426
427 static void
428 gtk_scale_init (GtkScale *scale)
429 {
430   GtkScalePrivate *priv;
431   GtkRange *range = GTK_RANGE (scale);
432   GtkStyleContext *context;
433
434   scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
435                                              GTK_TYPE_SCALE,
436                                              GtkScalePrivate);
437   priv = scale->priv;
438
439   gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
440
441   gtk_range_set_slider_size_fixed (range, TRUE);
442
443   priv->draw_value = TRUE;
444   priv->value_pos = GTK_POS_TOP;
445   priv->digits = 1;
446   gtk_range_set_round_digits (range, priv->digits);
447
448   gtk_scale_orientation_notify (range, NULL);
449   g_signal_connect (scale, "notify::orientation",
450                     G_CALLBACK (gtk_scale_orientation_notify),
451                     NULL);
452
453   context = gtk_widget_get_style_context (GTK_WIDGET (scale));
454   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE);
455 }
456
457 static void
458 gtk_scale_set_property (GObject      *object,
459                         guint         prop_id,
460                         const GValue *value,
461                         GParamSpec   *pspec)
462 {
463   GtkScale *scale;
464
465   scale = GTK_SCALE (object);
466
467   switch (prop_id)
468     {
469     case PROP_DIGITS:
470       gtk_scale_set_digits (scale, g_value_get_int (value));
471       break;
472     case PROP_DRAW_VALUE:
473       gtk_scale_set_draw_value (scale, g_value_get_boolean (value));
474       break;
475     case PROP_VALUE_POS:
476       gtk_scale_set_value_pos (scale, g_value_get_enum (value));
477       break;
478     default:
479       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
480       break;
481     }
482 }
483
484 static void
485 gtk_scale_get_property (GObject      *object,
486                         guint         prop_id,
487                         GValue       *value,
488                         GParamSpec   *pspec)
489 {
490   GtkScale *scale = GTK_SCALE (object);
491   GtkScalePrivate *priv = scale->priv;
492
493   switch (prop_id)
494     {
495     case PROP_DIGITS:
496       g_value_set_int (value, priv->digits);
497       break;
498     case PROP_DRAW_VALUE:
499       g_value_set_boolean (value, priv->draw_value);
500       break;
501     case PROP_VALUE_POS:
502       g_value_set_enum (value, priv->value_pos);
503       break;
504     default:
505       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
506       break;
507     }
508 }
509
510 /**
511  * gtk_scale_new:
512  * @orientation: the scale's orientation.
513  * @adjustment: (allow-none): the #GtkAdjustment which sets the range
514  *              of the scale, or %NULL to create a new adjustment.
515  *
516  * Creates a new #GtkScale.
517  *
518  * Return value: a new #GtkScale
519  *
520  * Since: 3.0
521  **/
522 GtkWidget *
523 gtk_scale_new (GtkOrientation  orientation,
524                GtkAdjustment  *adjustment)
525 {
526   g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
527                         NULL);
528
529   return g_object_new (GTK_TYPE_SCALE,
530                        "orientation", orientation,
531                        "adjustment",  adjustment,
532                        NULL);
533 }
534
535 /**
536  * gtk_scale_new_with_range:
537  * @orientation: the scale's orientation.
538  * @min: minimum value
539  * @max: maximum value
540  * @step: step increment (tick size) used with keyboard shortcuts
541  *
542  * Creates a new scale widget with the given orientation that lets the
543  * user input a number between @min and @max (including @min and @max)
544  * with the increment @step.  @step must be nonzero; it's the distance
545  * the slider moves when using the arrow keys to adjust the scale
546  * value.
547  *
548  * Note that the way in which the precision is derived works best if @step
549  * is a power of ten. If the resulting precision is not suitable for your
550  * needs, use gtk_scale_set_digits() to correct it.
551  *
552  * Return value: a new #GtkScale
553  *
554  * Since: 3.0
555  */
556 GtkWidget *
557 gtk_scale_new_with_range (GtkOrientation orientation,
558                           gdouble        min,
559                           gdouble        max,
560                           gdouble        step)
561 {
562   GtkAdjustment *adj;
563   gint digits;
564
565   g_return_val_if_fail (min < max, NULL);
566   g_return_val_if_fail (step != 0.0, NULL);
567
568   adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
569
570   if (fabs (step) >= 1.0 || step == 0.0)
571     {
572       digits = 0;
573     }
574   else
575     {
576       digits = abs ((gint) floor (log10 (fabs (step))));
577       if (digits > 5)
578         digits = 5;
579     }
580
581   return g_object_new (GTK_TYPE_SCALE,
582                        "orientation", orientation,
583                        "adjustment",  adj,
584                        "digits",      digits,
585                        NULL);
586 }
587
588 /**
589  * gtk_scale_set_digits:
590  * @scale: a #GtkScale
591  * @digits: the number of decimal places to display,
592  *     e.g. use 1 to display 1.0, 2 to display 1.00, etc
593  *
594  * Sets the number of decimal places that are displayed in the value.
595  * Also causes the value of the adjustment to be rounded off to this
596  * number of digits, so the retrieved value matches the value the user saw.
597  */
598 void
599 gtk_scale_set_digits (GtkScale *scale,
600                       gint      digits)
601 {
602   GtkScalePrivate *priv;
603   GtkRange *range;
604
605   g_return_if_fail (GTK_IS_SCALE (scale));
606
607   priv = scale->priv;
608   range = GTK_RANGE (scale);
609   
610   digits = CLAMP (digits, -1, MAX_DIGITS);
611
612   if (priv->digits != digits)
613     {
614       priv->digits = digits;
615       if (priv->draw_value)
616         gtk_range_set_round_digits (range, digits);
617
618       _gtk_scale_clear_layout (scale);
619       gtk_widget_queue_resize (GTK_WIDGET (scale));
620
621       g_object_notify (G_OBJECT (scale), "digits");
622     }
623 }
624
625 /**
626  * gtk_scale_get_digits:
627  * @scale: a #GtkScale
628  *
629  * Gets the number of decimal places that are displayed in the value.
630  *
631  * Returns: the number of decimal places that are displayed
632  */
633 gint
634 gtk_scale_get_digits (GtkScale *scale)
635 {
636   g_return_val_if_fail (GTK_IS_SCALE (scale), -1);
637
638   return scale->priv->digits;
639 }
640
641 /**
642  * gtk_scale_set_draw_value:
643  * @scale: a #GtkScale
644  * @draw_value: %TRUE to draw the value
645  * 
646  * Specifies whether the current value is displayed as a string next 
647  * to the slider.
648  */
649 void
650 gtk_scale_set_draw_value (GtkScale *scale,
651                           gboolean  draw_value)
652 {
653   GtkScalePrivate *priv;
654
655   g_return_if_fail (GTK_IS_SCALE (scale));
656
657   priv = scale->priv;
658
659   draw_value = draw_value != FALSE;
660
661   if (priv->draw_value != draw_value)
662     {
663       priv->draw_value = draw_value;
664       if (draw_value)
665         gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
666       else
667         gtk_range_set_round_digits (GTK_RANGE (scale), -1);
668
669       _gtk_scale_clear_layout (scale);
670
671       gtk_widget_queue_resize (GTK_WIDGET (scale));
672
673       g_object_notify (G_OBJECT (scale), "draw-value");
674     }
675 }
676
677 /**
678  * gtk_scale_get_draw_value:
679  * @scale: a #GtkScale
680  *
681  * Returns whether the current value is displayed as a string 
682  * next to the slider.
683  *
684  * Returns: whether the current value is displayed as a string
685  */
686 gboolean
687 gtk_scale_get_draw_value (GtkScale *scale)
688 {
689   g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE);
690
691   return scale->priv->draw_value;
692 }
693
694 /**
695  * gtk_scale_set_value_pos:
696  * @scale: a #GtkScale
697  * @pos: the position in which the current value is displayed
698  * 
699  * Sets the position in which the current value is displayed.
700  */
701 void
702 gtk_scale_set_value_pos (GtkScale        *scale,
703                          GtkPositionType  pos)
704 {
705   GtkScalePrivate *priv;
706   GtkWidget *widget;
707
708   g_return_if_fail (GTK_IS_SCALE (scale));
709
710   priv = scale->priv;
711
712   if (priv->value_pos != pos)
713     {
714       priv->value_pos = pos;
715       widget = GTK_WIDGET (scale);
716
717       _gtk_scale_clear_layout (scale);
718       if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget))
719         gtk_widget_queue_resize (widget);
720
721       g_object_notify (G_OBJECT (scale), "value-pos");
722     }
723 }
724
725 /**
726  * gtk_scale_get_value_pos:
727  * @scale: a #GtkScale
728  *
729  * Gets the position in which the current value is displayed.
730  *
731  * Returns: the position in which the current value is displayed
732  */
733 GtkPositionType
734 gtk_scale_get_value_pos (GtkScale *scale)
735 {
736   g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
737
738   return scale->priv->value_pos;
739 }
740
741 static void
742 gtk_scale_get_range_border (GtkRange  *range,
743                             GtkBorder *border)
744 {
745   GtkScalePrivate *priv;
746   GtkWidget *widget;
747   GtkScale *scale;
748   gint w, h;
749   
750   widget = GTK_WIDGET (range);
751   scale = GTK_SCALE (range);
752   priv = scale->priv;
753
754   _gtk_scale_get_value_size (scale, &w, &h);
755
756   border->left = 0;
757   border->right = 0;
758   border->top = 0;
759   border->bottom = 0;
760
761   if (priv->draw_value)
762     {
763       gint value_spacing;
764       gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
765
766       switch (priv->value_pos)
767         {
768         case GTK_POS_LEFT:
769           border->left += w + value_spacing;
770           break;
771         case GTK_POS_RIGHT:
772           border->right += w + value_spacing;
773           break;
774         case GTK_POS_TOP:
775           border->top += h + value_spacing;
776           break;
777         case GTK_POS_BOTTOM:
778           border->bottom += h + value_spacing;
779           break;
780         }
781     }
782
783   if (priv->marks)
784     {
785       gint slider_width;
786       gint value_spacing;
787       gint n1, w1, h1, n2, w2, h2;
788   
789       gtk_widget_style_get (widget, 
790                             "slider-width", &slider_width,
791                             "value-spacing", &value_spacing, 
792                             NULL);
793
794
795       gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
796
797       if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
798         {
799           if (n1 > 0)
800             border->top += h1 + value_spacing + slider_width / 2;
801           if (n2 > 0)
802             border->bottom += h2 + value_spacing + slider_width / 2;
803         }
804       else
805         {
806           if (n1 > 0)
807             border->left += w1 + value_spacing + slider_width / 2;
808           if (n2 > 0)
809             border->right += w2 + value_spacing + slider_width / 2;
810         }
811     }
812 }
813
814 /* FIXME this could actually be static at the moment. */
815 void
816 _gtk_scale_get_value_size (GtkScale *scale,
817                            gint     *width,
818                            gint     *height)
819 {
820   GtkScalePrivate *priv = scale->priv;
821   GtkRange *range;
822
823   if (priv->draw_value)
824     {
825       GtkAdjustment *adjustment;
826       PangoLayout *layout;
827       PangoRectangle logical_rect;
828       gchar *txt;
829       
830       range = GTK_RANGE (scale);
831
832       layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
833       adjustment = gtk_range_get_adjustment (range);
834
835       txt = _gtk_scale_format_value (scale, gtk_adjustment_get_lower (adjustment));
836       pango_layout_set_text (layout, txt, -1);
837       g_free (txt);
838       
839       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
840
841       if (width)
842         *width = logical_rect.width;
843       if (height)
844         *height = logical_rect.height;
845
846       txt = _gtk_scale_format_value (scale, gtk_adjustment_get_upper (adjustment));
847       pango_layout_set_text (layout, txt, -1);
848       g_free (txt);
849       
850       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
851
852       if (width)
853         *width = MAX (*width, logical_rect.width);
854       if (height)
855         *height = MAX (*height, logical_rect.height);
856
857       g_object_unref (layout);
858     }
859   else
860     {
861       if (width)
862         *width = 0;
863       if (height)
864         *height = 0;
865     }
866
867 }
868
869 static void
870 gtk_scale_get_mark_label_size (GtkScale        *scale,
871                                GtkPositionType  position,
872                                gint            *count1,
873                                gint            *width1,
874                                gint            *height1,
875                                gint            *count2,
876                                gint            *width2,
877                                gint            *height2)
878 {
879   GtkScalePrivate *priv = scale->priv;
880   PangoLayout *layout;
881   PangoRectangle logical_rect;
882   GSList *m;
883   gint w, h;
884
885   *count1 = *count2 = 0;
886   *width1 = *width2 = 0;
887   *height1 = *height2 = 0;
888
889   layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
890
891   for (m = priv->marks; m; m = m->next)
892     {
893       GtkScaleMark *mark = m->data;
894
895       if (mark->markup && *mark->markup)
896         {
897           pango_layout_set_markup (layout, mark->markup, -1);
898           pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
899
900           w = logical_rect.width;
901           h = logical_rect.height;
902         }
903       else
904         {
905           w = 0;
906           h = 0;
907         }
908
909       if (mark->position == position)
910         {
911           (*count1)++;
912           *width1 = MAX (*width1, w);
913           *height1 = MAX (*height1, h);
914         }
915       else
916         {
917           (*count2)++;
918           *width2 = MAX (*width2, w);
919           *height2 = MAX (*height2, h);
920         }
921     }
922
923   g_object_unref (layout);
924 }
925
926 static void
927 gtk_scale_style_updated (GtkWidget *widget)
928 {
929   gint slider_length;
930   GtkRange *range;
931
932   range = GTK_RANGE (widget);
933   
934   gtk_widget_style_get (widget,
935                         "slider-length", &slider_length,
936                         NULL);
937
938   gtk_range_set_min_slider_size (range, slider_length);
939
940   _gtk_scale_clear_layout (GTK_SCALE (widget));
941
942   GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_updated (widget);
943 }
944
945 static void
946 gtk_scale_screen_changed (GtkWidget *widget,
947                           GdkScreen *old_screen)
948 {
949   _gtk_scale_clear_layout (GTK_SCALE (widget));
950 }
951
952 static void
953 gtk_scale_get_preferred_width (GtkWidget *widget,
954                                gint      *minimum,
955                                gint      *natural)
956 {
957   GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural);
958   
959   if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
960     {
961       gint n1, w1, h1, n2, w2, h2;
962       gint slider_length;
963       gint w;
964
965       gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
966
967       gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
968
969       w1 = (n1 - 1) * w1 + MAX (w1, slider_length);
970       w2 = (n2 - 1) * w2 + MAX (w2, slider_length);
971       w = MAX (w1, w2);
972
973       *minimum = MAX (*minimum, w);
974       *natural = MAX (*natural, w);
975     }
976 }
977
978 static void
979 gtk_scale_get_preferred_height (GtkWidget *widget,
980                                 gint      *minimum,
981                                 gint      *natural)
982 {
983   GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural);
984
985
986   if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
987     {
988       gint n1, w1, h1, n2, w2, h2;
989       gint slider_length;
990       gint h;
991
992       gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
993
994       gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
995       h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
996       h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
997       h = MAX (h1, h2);
998
999       *minimum = MAX (*minimum, h);
1000       *natural = MAX (*natural, h);
1001     }
1002 }
1003
1004 static gint
1005 find_next_pos (GtkWidget      *widget,
1006                GSList          *list,
1007                gint            *marks,
1008                GtkPositionType  pos,
1009                gint             match)
1010 {
1011   GtkAllocation allocation;
1012   GSList *m;
1013   gint i;
1014
1015   for (m = list->next, i = 1; m; m = m->next, i++)
1016     {
1017       GtkScaleMark *mark = m->data;
1018
1019       if (match == (mark->position == pos))
1020         return marks[i];
1021     }
1022
1023   gtk_widget_get_allocation (widget, &allocation);
1024   if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
1025     return allocation.width;
1026   else
1027     return allocation.height;
1028 }
1029
1030 static gboolean
1031 gtk_scale_draw (GtkWidget *widget,
1032                 cairo_t   *cr)
1033 {
1034   GtkScale *scale = GTK_SCALE (widget);
1035   GtkScalePrivate *priv = scale->priv;
1036   GtkRange *range = GTK_RANGE (scale);
1037   GtkStateFlags state = 0;
1038   GtkStyleContext *context;
1039   gint *marks;
1040   gint focus_padding;
1041   gint slider_width;
1042   gint value_spacing;
1043   gint min_sep = 4;
1044
1045   context = gtk_widget_get_style_context (widget);
1046   gtk_widget_style_get (widget,
1047                         "focus-padding", &focus_padding,
1048                         "slider-width", &slider_width,
1049                         "value-spacing", &value_spacing,
1050                         NULL);
1051
1052   /* We need to chain up _first_ so the various geometry members of
1053    * GtkRange struct are updated.
1054    */
1055   GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1056
1057   if (!gtk_widget_is_sensitive (widget))
1058     state |= GTK_STATE_FLAG_INSENSITIVE;
1059
1060   if (priv->marks)
1061     {
1062       GtkOrientation orientation;
1063       GdkRectangle range_rect;
1064       gint i;
1065       gint x1, x2, x3, y1, y2, y3;
1066       PangoLayout *layout;
1067       PangoRectangle logical_rect;
1068       GSList *m;
1069       gint min_pos_before, min_pos_after;
1070       gint min_pos, max_pos;
1071
1072       orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1073       _gtk_range_get_stop_positions (range, &marks);
1074       layout = gtk_widget_create_pango_layout (widget, NULL);
1075       gtk_range_get_range_rect (range, &range_rect);
1076
1077       min_pos_before = min_pos_after = 0;
1078
1079       for (m = priv->marks, i = 0; m; m = m->next, i++)
1080         {
1081           GtkScaleMark *mark = m->data;
1082
1083           if (orientation == GTK_ORIENTATION_HORIZONTAL)
1084             {
1085               x1 = marks[i];
1086               if (mark->position == GTK_POS_TOP)
1087                 {
1088                   y1 = range_rect.y;
1089                   y2 = y1 - slider_width / 2;
1090                   min_pos = min_pos_before;
1091                   max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1092                 }
1093               else
1094                 {
1095                   y1 = range_rect.y + range_rect.height;
1096                   y2 = y1 + slider_width / 2;
1097                   min_pos = min_pos_after;
1098                   max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM, 1) - min_sep;
1099                 }
1100
1101               gtk_style_context_save (context);
1102               gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1103               gtk_style_context_set_state (context, state);
1104
1105               gtk_render_line (context, cr, x1, y1, x1, y2);
1106
1107               if (mark->markup)
1108                 {
1109                   pango_layout_set_markup (layout, mark->markup, -1);
1110                   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1111
1112                   x3 = x1 - logical_rect.width / 2;
1113                   if (x3 < min_pos)
1114                     x3 = min_pos;
1115                   if (x3 + logical_rect.width > max_pos)
1116                         x3 = max_pos - logical_rect.width;
1117                   if (x3 < 0)
1118                      x3 = 0;
1119                   if (mark->position == GTK_POS_TOP)
1120                     {
1121                       y3 = y2 - value_spacing - logical_rect.height;
1122                       min_pos_before = x3 + logical_rect.width + min_sep;
1123                     }
1124                   else
1125                     {
1126                       y3 = y2 + value_spacing;
1127                       min_pos_after = x3 + logical_rect.width + min_sep;
1128                     }
1129
1130                   gtk_render_layout (context, cr, x3, y3, layout);
1131                 }
1132
1133               gtk_style_context_restore (context);
1134             }
1135           else
1136             {
1137               if (mark->position == GTK_POS_TOP)
1138                 {
1139                   x1 = range_rect.x;
1140                   x2 = range_rect.x - slider_width / 2;
1141                   min_pos = min_pos_before;
1142                   max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1143                 }
1144               else
1145                 {
1146                   x1 = range_rect.x + range_rect.width;
1147                   x2 = range_rect.x + range_rect.width + slider_width / 2;
1148                   min_pos = min_pos_after;
1149                   max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM, 1) - min_sep;
1150                 }
1151               y1 = marks[i];
1152
1153               gtk_style_context_save (context);
1154               gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1155               gtk_style_context_set_state (context, state);
1156
1157               gtk_render_line (context, cr, x1, y1, x2, y1);
1158
1159               if (mark->markup)
1160                 {
1161                   pango_layout_set_markup (layout, mark->markup, -1);
1162                   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1163
1164                   y3 = y1 - logical_rect.height / 2;
1165                   if (y3 < min_pos)
1166                     y3 = min_pos;
1167                   if (y3 + logical_rect.height > max_pos)
1168                     y3 = max_pos - logical_rect.height;
1169                   if (y3 < 0)
1170                     y3 = 0;
1171                   if (mark->position == GTK_POS_TOP)
1172                     {
1173                       x3 = x2 - value_spacing - logical_rect.width;
1174                       min_pos_before = y3 + logical_rect.height + min_sep;
1175                     }
1176                   else
1177                     {
1178                       x3 = x2 + value_spacing;
1179                       min_pos_after = y3 + logical_rect.height + min_sep;
1180                     }
1181
1182                   gtk_render_layout (context, cr, x3, y3, layout);
1183                 }
1184
1185               gtk_style_context_restore (context);
1186             }
1187         }
1188
1189       g_object_unref (layout);
1190       g_free (marks);
1191     }
1192
1193   if (priv->draw_value)
1194     {
1195       GtkAllocation allocation;
1196
1197       PangoLayout *layout;
1198       gint x, y;
1199
1200       layout = gtk_scale_get_layout (scale);
1201       gtk_scale_get_layout_offsets (scale, &x, &y);
1202       gtk_widget_get_allocation (widget, &allocation);
1203
1204       gtk_render_layout (context, cr,
1205                          x - allocation.x,
1206                          y - allocation.y,
1207                          layout);
1208     }
1209
1210   return FALSE;
1211 }
1212
1213 static void
1214 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1215                                    gint     *x,
1216                                    gint     *y)
1217 {
1218   GtkScalePrivate *priv = scale->priv;
1219   GtkAllocation allocation;
1220   GtkWidget *widget = GTK_WIDGET (scale);
1221   GtkRange *range = GTK_RANGE (widget);
1222   GdkRectangle range_rect;
1223   PangoLayout *layout = gtk_scale_get_layout (scale);
1224   PangoRectangle logical_rect;
1225   gint slider_start, slider_end;
1226   gint value_spacing;
1227
1228   if (!layout)
1229     {
1230       *x = 0;
1231       *y = 0;
1232
1233       return;
1234     }
1235
1236   gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1237
1238   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1239
1240   gtk_widget_get_allocation (widget, &allocation);
1241   gtk_range_get_range_rect (range, &range_rect);
1242   gtk_range_get_slider_range (range,
1243                               &slider_start,
1244                               &slider_end);
1245
1246   if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1247     {
1248       switch (priv->value_pos)
1249         {
1250         case GTK_POS_LEFT:
1251           *x = range_rect.x - value_spacing - logical_rect.width;
1252           *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1253           break;
1254
1255         case GTK_POS_RIGHT:
1256           *x = range_rect.x + range_rect.width + value_spacing;
1257           *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1258           break;
1259
1260         case GTK_POS_TOP:
1261           *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1262           *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1263           *y = range_rect.y - logical_rect.height - value_spacing;
1264           break;
1265
1266         case GTK_POS_BOTTOM:
1267           *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1268           *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1269           *y = range_rect.y + range_rect.height + value_spacing;
1270           break;
1271
1272         default:
1273           g_return_if_reached ();
1274           break;
1275         }
1276     }
1277   else
1278     {
1279       switch (priv->value_pos)
1280         {
1281         case GTK_POS_LEFT:
1282           *x = range_rect.x - logical_rect.width - value_spacing;
1283           *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1284           *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1285           break;
1286
1287         case GTK_POS_RIGHT:
1288           *x = range_rect.x + range_rect.width + value_spacing;
1289           *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1290           *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1291           break;
1292
1293         case GTK_POS_TOP:
1294           *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1295           *y = range_rect.y - logical_rect.height - value_spacing;
1296           break;
1297
1298         case GTK_POS_BOTTOM:
1299           *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1300           *y = range_rect.y + range_rect.height + value_spacing;
1301           break;
1302
1303         default:
1304           g_return_if_reached ();
1305         }
1306     }
1307
1308   *x += allocation.x;
1309   *y += allocation.y;
1310 }
1311
1312 /**
1313  * _gtk_scale_format_value:
1314  * @scale: a #GtkScale
1315  * @value: adjustment value
1316  * 
1317  * Emits #GtkScale::format-value signal to format the value, 
1318  * if no user signal handlers, falls back to a default format.
1319  * 
1320  * Return value: formatted value
1321  */
1322 gchar*
1323 _gtk_scale_format_value (GtkScale *scale,
1324                          gdouble   value)
1325 {
1326   GtkScalePrivate *priv = scale->priv;
1327   gchar *fmt = NULL;
1328
1329   g_signal_emit (scale,
1330                  signals[FORMAT_VALUE],
1331                  0,
1332                  value,
1333                  &fmt);
1334
1335   if (fmt)
1336     return fmt;
1337   else
1338     /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1339     return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value);
1340 }
1341
1342 static void
1343 gtk_scale_finalize (GObject *object)
1344 {
1345   GtkScale *scale = GTK_SCALE (object);
1346
1347   _gtk_scale_clear_layout (scale);
1348   gtk_scale_clear_marks (scale);
1349
1350   G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1351 }
1352
1353 /**
1354  * gtk_scale_get_layout:
1355  * @scale: A #GtkScale
1356  *
1357  * Gets the #PangoLayout used to display the scale. The returned
1358  * object is owned by the scale so does not need to be freed by
1359  * the caller.
1360  *
1361  * Return value: (transfer none): the #PangoLayout for this scale,
1362  *     or %NULL if the #GtkScale:draw-value property is %FALSE.
1363  *
1364  * Since: 2.4
1365  */
1366 PangoLayout *
1367 gtk_scale_get_layout (GtkScale *scale)
1368 {
1369   GtkScalePrivate *priv;
1370   gchar *txt;
1371
1372   g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1373
1374   priv = scale->priv;
1375
1376   if (!priv->layout)
1377     {
1378       if (priv->draw_value)
1379         priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1380     }
1381
1382   if (priv->draw_value)
1383     {
1384       txt = _gtk_scale_format_value (scale,
1385                                      gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1386       pango_layout_set_text (priv->layout, txt, -1);
1387       g_free (txt);
1388     }
1389
1390   return priv->layout;
1391 }
1392
1393 /**
1394  * gtk_scale_get_layout_offsets:
1395  * @scale: a #GtkScale
1396  * @x: (out) (allow-none): location to store X offset of layout, or %NULL
1397  * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
1398  *
1399  * Obtains the coordinates where the scale will draw the 
1400  * #PangoLayout representing the text in the scale. Remember
1401  * when using the #PangoLayout function you need to convert to
1402  * and from pixels using PANGO_PIXELS() or #PANGO_SCALE. 
1403  *
1404  * If the #GtkScale:draw-value property is %FALSE, the return 
1405  * values are undefined.
1406  *
1407  * Since: 2.4
1408  */
1409 void 
1410 gtk_scale_get_layout_offsets (GtkScale *scale,
1411                               gint     *x,
1412                               gint     *y)
1413 {
1414   gint local_x = 0; 
1415   gint local_y = 0;
1416
1417   g_return_if_fail (GTK_IS_SCALE (scale));
1418
1419   if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1420     (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1421
1422   if (x)
1423     *x = local_x;
1424   
1425   if (y)
1426     *y = local_y;
1427 }
1428
1429 void
1430 _gtk_scale_clear_layout (GtkScale *scale)
1431 {
1432   GtkScalePrivate *priv = scale->priv;
1433
1434   g_return_if_fail (GTK_IS_SCALE (scale));
1435
1436   if (priv->layout)
1437     {
1438       g_object_unref (priv->layout);
1439       priv->layout = NULL;
1440     }
1441 }
1442
1443 static void
1444 gtk_scale_mark_free (GtkScaleMark *mark)
1445 {
1446   g_free (mark->markup);
1447   g_free (mark);
1448 }
1449
1450 /**
1451  * gtk_scale_clear_marks:
1452  * @scale: a #GtkScale
1453  * 
1454  * Removes any marks that have been added with gtk_scale_add_mark().
1455  *
1456  * Since: 2.16
1457  */
1458 void
1459 gtk_scale_clear_marks (GtkScale *scale)
1460 {
1461   GtkScalePrivate *priv;
1462
1463   g_return_if_fail (GTK_IS_SCALE (scale));
1464
1465   priv = scale->priv;
1466
1467   g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1468   g_slist_free (priv->marks);
1469   priv->marks = NULL;
1470
1471   _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1472
1473   gtk_widget_queue_resize (GTK_WIDGET (scale));
1474 }
1475
1476 static gint
1477 compare_marks (gpointer a, gpointer b)
1478 {
1479   GtkScaleMark *ma, *mb;
1480
1481   ma = a; mb = b;
1482
1483   return (gint) (ma->value - mb->value);
1484 }
1485
1486 /**
1487  * gtk_scale_add_mark:
1488  * @scale: a #GtkScale
1489  * @value: the value at which the mark is placed, must be between
1490  *   the lower and upper limits of the scales' adjustment
1491  * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1492  *   and %GTK_POS_LEFT are drawn above the scale, anything else below.
1493  *   For a vertical scale, #GTK_POS_LEFT and %GTK_POS_TOP are drawn to
1494  *   the left of the scale, anything else to the right.
1495  * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1496  *
1497  *
1498  * Adds a mark at @value.
1499  *
1500  * A mark is indicated visually by drawing a tick mark next to the scale,
1501  * and GTK+ makes it easy for the user to position the scale exactly at the
1502  * marks value.
1503  *
1504  * If @markup is not %NULL, text is shown next to the tick mark.
1505  *
1506  * To remove marks from a scale, use gtk_scale_clear_marks().
1507  *
1508  * Since: 2.16
1509  */
1510 void
1511 gtk_scale_add_mark (GtkScale        *scale,
1512                     gdouble          value,
1513                     GtkPositionType  position,
1514                     const gchar     *markup)
1515 {
1516   GtkScalePrivate *priv;
1517   GtkScaleMark *mark;
1518   GSList *m;
1519   gdouble *values;
1520   gint n, i;
1521
1522   g_return_if_fail (GTK_IS_SCALE (scale));
1523
1524   priv = scale->priv;
1525
1526   mark = g_new (GtkScaleMark, 1);
1527   mark->value = value;
1528   mark->markup = g_strdup (markup);
1529   if (position == GTK_POS_LEFT ||
1530       position == GTK_POS_TOP)
1531     mark->position = GTK_POS_TOP;
1532   else
1533     mark->position = GTK_POS_BOTTOM;
1534  
1535   priv->marks = g_slist_insert_sorted (priv->marks, mark,
1536                                        (GCompareFunc) compare_marks);
1537
1538   n = g_slist_length (priv->marks);
1539   values = g_new (gdouble, n);
1540   for (m = priv->marks, i = 0; m; m = m->next, i++)
1541     {
1542       mark = m->data;
1543       values[i] = mark->value;
1544     }
1545   
1546   _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1547
1548   g_free (values);
1549
1550   gtk_widget_queue_resize (GTK_WIDGET (scale));
1551 }
1552
1553 static GtkBuildableIface *parent_buildable_iface;
1554
1555 static void
1556 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1557 {
1558   parent_buildable_iface = g_type_interface_peek_parent (iface);
1559   iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1560   iface->custom_finished = gtk_scale_buildable_custom_finished;
1561 }
1562
1563 typedef struct
1564 {
1565   GtkScale *scale;
1566   GtkBuilder *builder;
1567   GSList *marks;
1568 } MarksSubparserData;
1569
1570 typedef struct
1571 {
1572   gdouble value;
1573   GtkPositionType position;
1574   GString *markup;
1575   gchar *context;
1576   gboolean translatable;
1577 } MarkData;
1578
1579 static void
1580 mark_data_free (MarkData *data)
1581 {
1582   g_string_free (data->markup, TRUE);
1583   g_free (data->context);
1584   g_slice_free (MarkData, data);
1585 }
1586
1587 static void
1588 marks_start_element (GMarkupParseContext *context,
1589                      const gchar         *element_name,
1590                      const gchar        **names,
1591                      const gchar        **values,
1592                      gpointer             user_data,
1593                      GError             **error)
1594 {
1595   MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1596   guint i;
1597   gint line_number, char_number;
1598
1599   if (strcmp (element_name, "marks") == 0)
1600    ;
1601   else if (strcmp (element_name, "mark") == 0)
1602     {
1603       gdouble value = 0;
1604       gboolean has_value = FALSE;
1605       GtkPositionType position = GTK_POS_BOTTOM;
1606       const gchar *msg_context = NULL;
1607       gboolean translatable = FALSE;
1608       MarkData *mark;
1609
1610       for (i = 0; names[i]; i++)
1611         {
1612           if (strcmp (names[i], "translatable") == 0)
1613             {
1614               if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1615                 return;
1616             }
1617           else if (strcmp (names[i], "comments") == 0)
1618             {
1619               /* do nothing, comments are for translators */
1620             }
1621           else if (strcmp (names[i], "context") == 0)
1622             msg_context = values[i];
1623           else if (strcmp (names[i], "value") == 0)
1624             {
1625               GValue gvalue = { 0, };
1626
1627               if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1628                 return;
1629
1630               value = g_value_get_double (&gvalue);
1631               has_value = TRUE;
1632             }
1633           else if (strcmp (names[i], "position") == 0)
1634             {
1635               GValue gvalue = { 0, };
1636
1637               if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1638                 return;
1639
1640               position = g_value_get_enum (&gvalue);
1641             }
1642           else
1643             {
1644               g_markup_parse_context_get_position (context,
1645                                                    &line_number,
1646                                                    &char_number);
1647               g_set_error (error,
1648                            GTK_BUILDER_ERROR,
1649                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1650                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
1651                            "<input>",
1652                            line_number, char_number, names[i], "mark");
1653               return;
1654             }
1655         }
1656
1657       if (!has_value)
1658         {
1659           g_markup_parse_context_get_position (context,
1660                                                &line_number,
1661                                                &char_number);
1662           g_set_error (error,
1663                        GTK_BUILDER_ERROR,
1664                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1665                        "%s:%d:%d <%s> requires attribute \"%s\"",
1666                        "<input>",
1667                        line_number, char_number, "mark",
1668                        "value");
1669           return;
1670         }
1671
1672       mark = g_slice_new (MarkData);
1673       mark->value = value;
1674       if (position == GTK_POS_LEFT ||
1675           position == GTK_POS_TOP)
1676         mark->position = GTK_POS_TOP;
1677       else
1678         mark->position = GTK_POS_BOTTOM;
1679       mark->markup = g_string_new ("");
1680       mark->context = g_strdup (msg_context);
1681       mark->translatable = translatable;
1682
1683       parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1684     }
1685   else
1686     {
1687       g_markup_parse_context_get_position (context,
1688                                            &line_number,
1689                                            &char_number);
1690       g_set_error (error,
1691                    GTK_BUILDER_ERROR,
1692                    GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1693                    "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1694                    "<input>",
1695                    line_number, char_number, element_name);
1696       return;
1697     }
1698 }
1699
1700 static void
1701 marks_text (GMarkupParseContext  *context,
1702             const gchar          *text,
1703             gsize                 text_len,
1704             gpointer              user_data,
1705             GError              **error)
1706 {
1707   MarksSubparserData *data = (MarksSubparserData*)user_data;
1708
1709   if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1710     {
1711       MarkData *mark = data->marks->data;
1712
1713       g_string_append_len (mark->markup, text, text_len);
1714     }
1715 }
1716
1717 static const GMarkupParser marks_parser =
1718   {
1719     marks_start_element,
1720     NULL,
1721     marks_text,
1722   };
1723
1724
1725 static gboolean
1726 gtk_scale_buildable_custom_tag_start (GtkBuildable  *buildable,
1727                                       GtkBuilder    *builder,
1728                                       GObject       *child,
1729                                       const gchar   *tagname,
1730                                       GMarkupParser *parser,
1731                                       gpointer      *data)
1732 {
1733   MarksSubparserData *parser_data;
1734
1735   if (child)
1736     return FALSE;
1737
1738   if (strcmp (tagname, "marks") == 0)
1739     {
1740       parser_data = g_slice_new0 (MarksSubparserData);
1741       parser_data->scale = GTK_SCALE (buildable);
1742       parser_data->marks = NULL;
1743
1744       *parser = marks_parser;
1745       *data = parser_data;
1746       return TRUE;
1747     }
1748
1749   return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1750                                                    tagname, parser, data);
1751 }
1752
1753 static void
1754 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1755                                      GtkBuilder   *builder,
1756                                      GObject      *child,
1757                                      const gchar  *tagname,
1758                                      gpointer      user_data)
1759 {
1760   GtkScale *scale = GTK_SCALE (buildable);
1761   MarksSubparserData *marks_data;
1762
1763   if (strcmp (tagname, "marks") == 0)
1764     {
1765       GSList *m;
1766       gchar *markup;
1767
1768       marks_data = (MarksSubparserData *)user_data;
1769
1770       for (m = marks_data->marks; m; m = m->next)
1771         {
1772           MarkData *mdata = m->data;
1773
1774           if (mdata->translatable && mdata->markup->len)
1775             markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1776                                                     mdata->context,
1777                                                     mdata->markup->str);
1778           else
1779             markup = mdata->markup->str;
1780
1781           gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1782
1783           mark_data_free (mdata);
1784         }
1785
1786       g_slist_free (marks_data->marks);
1787       g_slice_free (MarksSubparserData, marks_data);
1788     }
1789 }