]> Pileus Git - ~andy/gtk/blob - gtk/gtkscale.c
Merge branch 'master' into treeview-refactor
[~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 "gdk/gdkkeysyms.h"
34 #include "gtkscale.h"
35 #include "gtkiconfactory.h"
36 #include "gtkicontheme.h"
37 #include "gtkmarshalers.h"
38 #include "gtkbindings.h"
39 #include "gtkorientable.h"
40 #include "gtkprivate.h"
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;
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: the #GtkAdjustment which sets the range of the scale, or
514  *              %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       if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
796         {
797           gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
798           if (n1 > 0)
799             border->top += h1 + value_spacing + slider_width / 2;
800           if (n2 > 0)
801             border->bottom += h2 + value_spacing + slider_width / 2; 
802         }
803       else
804         {
805           gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
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_LEFT, &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 (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM)
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 n_marks;
1040   gint *marks;
1041   gint focus_padding;
1042   gint slider_width;
1043   gint value_spacing;
1044   gint min_sep = 4;
1045
1046   context = gtk_widget_get_style_context (widget);
1047   gtk_widget_style_get (widget,
1048                         "focus-padding", &focus_padding,
1049                         "slider-width", &slider_width, 
1050                         "value-spacing", &value_spacing, 
1051                         NULL);
1052
1053   /* We need to chain up _first_ so the various geometry members of
1054    * GtkRange struct are updated.
1055    */
1056   GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr);
1057
1058   if (!gtk_widget_is_sensitive (widget))
1059     state |= GTK_STATE_FLAG_INSENSITIVE;
1060
1061   if (priv->marks)
1062     {
1063       GtkOrientation orientation;
1064       GdkRectangle range_rect;
1065       gint i;
1066       gint x1, x2, x3, y1, y2, y3;
1067       PangoLayout *layout;
1068       PangoRectangle logical_rect;
1069       GSList *m;
1070       gint min_pos_before, min_pos_after;
1071       gint min_pos, max_pos;
1072
1073       orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1074       n_marks = _gtk_range_get_stop_positions (range, &marks);
1075       layout = gtk_widget_create_pango_layout (widget, NULL);
1076       gtk_range_get_range_rect (range, &range_rect);
1077
1078       min_pos_before = min_pos_after = 0;
1079
1080       for (m = priv->marks, i = 0; m; m = m->next, i++)
1081         {
1082           GtkScaleMark *mark = m->data;
1083
1084           if (orientation == GTK_ORIENTATION_HORIZONTAL)
1085             {
1086               x1 = marks[i];
1087               if (mark->position == GTK_POS_TOP)
1088                 {
1089                   y1 = range_rect.y;
1090                   y2 = y1 - slider_width / 2;
1091                   min_pos = min_pos_before;
1092                   max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
1093                 }
1094               else
1095                 {
1096                   y1 = range_rect.y + range_rect.height;
1097                   y2 = y1 + slider_width / 2;
1098                   min_pos = min_pos_after;
1099                   max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep;
1100                 }
1101
1102               gtk_style_context_save (context);
1103               gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1104               gtk_style_context_set_state (context, state);
1105
1106               gtk_render_line (context, cr,
1107                                x1, y1, x1, y2);
1108
1109               if (mark->markup)
1110                 {
1111                   pango_layout_set_markup (layout, mark->markup, -1);
1112                   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1113
1114                   x3 = x1 - logical_rect.width / 2;
1115                   if (x3 < min_pos)
1116                     x3 = min_pos;
1117                   if (x3 + logical_rect.width > max_pos)
1118                         x3 = max_pos - logical_rect.width;
1119                   if (x3 < 0)
1120                      x3 = 0;
1121                   if (mark->position == GTK_POS_TOP)
1122                     {
1123                       y3 = y2 - value_spacing - logical_rect.height;
1124                       min_pos_before = x3 + logical_rect.width + min_sep;
1125                     }
1126                   else
1127                     {
1128                       y3 = y2 + value_spacing;
1129                       min_pos_after = x3 + logical_rect.width + min_sep;
1130                     }
1131
1132                   gtk_render_layout (context, cr,
1133                                      x3, y3, layout);
1134                 }
1135
1136               gtk_style_context_restore (context);
1137             }
1138           else
1139             {
1140               if (mark->position == GTK_POS_LEFT)
1141                 {
1142                   x1 = range_rect.x;
1143                   x2 = range_rect.x - slider_width / 2;
1144                   min_pos = min_pos_before;
1145                   max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep;
1146                 }
1147               else
1148                 {
1149                   x1 = range_rect.x + range_rect.width;
1150                   x2 = range_rect.x + range_rect.width + slider_width / 2;
1151                   min_pos = min_pos_after;
1152                   max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep;
1153                 }
1154               y1 = marks[i];
1155
1156               gtk_style_context_save (context);
1157               gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
1158               gtk_style_context_set_state (context, state);
1159
1160               gtk_render_line (context, cr,
1161                                x1, y1, x2, y1);
1162
1163               if (mark->markup)
1164                 {
1165                   pango_layout_set_markup (layout, mark->markup, -1);
1166                   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1167
1168                   y3 = y1 - logical_rect.height / 2;
1169                   if (y3 < min_pos)
1170                     y3 = min_pos;
1171                   if (y3 + logical_rect.height > max_pos)
1172                     y3 = max_pos - logical_rect.height;
1173                   if (y3 < 0)
1174                     y3 = 0;
1175                   if (mark->position == GTK_POS_LEFT)
1176                     {
1177                       x3 = x2 - value_spacing - logical_rect.width;
1178                       min_pos_before = y3 + logical_rect.height + min_sep;
1179                     }
1180                   else
1181                     {
1182                       x3 = x2 + value_spacing;
1183                       min_pos_after = y3 + logical_rect.height + min_sep;
1184                     }
1185
1186                   gtk_render_layout (context, cr,
1187                                      x3, y3, layout);
1188                 }
1189
1190               gtk_style_context_restore (context);
1191             }
1192         } 
1193
1194       g_object_unref (layout);
1195       g_free (marks);
1196     }
1197
1198   if (priv->draw_value)
1199     {
1200       GtkOrientation orientation;
1201       GtkAllocation allocation;
1202
1203       PangoLayout *layout;
1204       gint x, y;
1205
1206       orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range));
1207       layout = gtk_scale_get_layout (scale);
1208       gtk_scale_get_layout_offsets (scale, &x, &y);
1209       gtk_widget_get_allocation (widget, &allocation);
1210
1211       gtk_render_layout (context, cr,
1212                          x - allocation.x,
1213                          y - allocation.y,
1214                          layout);
1215     }
1216
1217   return FALSE;
1218 }
1219
1220 static void
1221 gtk_scale_real_get_layout_offsets (GtkScale *scale,
1222                                    gint     *x,
1223                                    gint     *y)
1224 {
1225   GtkScalePrivate *priv = scale->priv;
1226   GtkAllocation allocation;
1227   GtkWidget *widget = GTK_WIDGET (scale);
1228   GtkRange *range = GTK_RANGE (widget);
1229   GdkRectangle range_rect;
1230   PangoLayout *layout = gtk_scale_get_layout (scale);
1231   PangoRectangle logical_rect;
1232   gint slider_start, slider_end;
1233   gint value_spacing;
1234
1235   if (!layout)
1236     {
1237       *x = 0;
1238       *y = 0;
1239
1240       return;
1241     }
1242
1243   gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
1244
1245   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1246
1247   gtk_widget_get_allocation (widget, &allocation);
1248   gtk_range_get_range_rect (range, &range_rect);
1249   gtk_range_get_slider_range (range,
1250                               &slider_start,
1251                               &slider_end);
1252
1253   if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL)
1254     {
1255       switch (priv->value_pos)
1256         {
1257         case GTK_POS_LEFT:
1258           *x = range_rect.x - value_spacing - logical_rect.width;
1259           *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1260           break;
1261
1262         case GTK_POS_RIGHT:
1263           *x = range_rect.x + range_rect.width + value_spacing;
1264           *y = range_rect.y + (range_rect.height - logical_rect.height) / 2;
1265           break;
1266
1267         case GTK_POS_TOP:
1268           *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1269           *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1270           *y = range_rect.y - logical_rect.height - value_spacing;
1271           break;
1272
1273         case GTK_POS_BOTTOM:
1274           *x = slider_start + (slider_end - slider_start - logical_rect.width) / 2;
1275           *x = CLAMP (*x, 0, allocation.width - logical_rect.width);
1276           *y = range_rect.y + range_rect.height + value_spacing;
1277           break;
1278
1279         default:
1280           g_return_if_reached ();
1281           break;
1282         }
1283     }
1284   else
1285     {
1286       switch (priv->value_pos)
1287         {
1288         case GTK_POS_LEFT:
1289           *x = range_rect.x - logical_rect.width - value_spacing;
1290           *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1291           *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1292           break;
1293
1294         case GTK_POS_RIGHT:
1295           *x = range_rect.x + range_rect.width + value_spacing;
1296           *y = slider_start + (slider_end - slider_start - logical_rect.height) / 2;
1297           *y = CLAMP (*y, 0, allocation.height - logical_rect.height);
1298           break;
1299
1300         case GTK_POS_TOP:
1301           *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1302           *y = range_rect.y - logical_rect.height - value_spacing;
1303           break;
1304
1305         case GTK_POS_BOTTOM:
1306           *x = range_rect.x + (range_rect.width - logical_rect.width) / 2;
1307           *y = range_rect.y + range_rect.height + value_spacing;
1308           break;
1309
1310         default:
1311           g_return_if_reached ();
1312         }
1313     }
1314
1315   *x += allocation.x;
1316   *y += allocation.y;
1317 }
1318
1319 /**
1320  * _gtk_scale_format_value:
1321  * @scale: a #GtkScale
1322  * @value: adjustment value
1323  * 
1324  * Emits #GtkScale::format-value signal to format the value, 
1325  * if no user signal handlers, falls back to a default format.
1326  * 
1327  * Return value: formatted value
1328  */
1329 gchar*
1330 _gtk_scale_format_value (GtkScale *scale,
1331                          gdouble   value)
1332 {
1333   GtkScalePrivate *priv = scale->priv;
1334   gchar *fmt = NULL;
1335
1336   g_signal_emit (scale,
1337                  signals[FORMAT_VALUE],
1338                  0,
1339                  value,
1340                  &fmt);
1341
1342   if (fmt)
1343     return fmt;
1344   else
1345     /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */
1346     return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value);
1347 }
1348
1349 static void
1350 gtk_scale_finalize (GObject *object)
1351 {
1352   GtkScale *scale = GTK_SCALE (object);
1353
1354   _gtk_scale_clear_layout (scale);
1355   gtk_scale_clear_marks (scale);
1356
1357   G_OBJECT_CLASS (gtk_scale_parent_class)->finalize (object);
1358 }
1359
1360 /**
1361  * gtk_scale_get_layout:
1362  * @scale: A #GtkScale
1363  *
1364  * Gets the #PangoLayout used to display the scale. The returned
1365  * object is owned by the scale so does not need to be freed by
1366  * the caller.
1367  *
1368  * Return value: (transfer none): the #PangoLayout for this scale,
1369  *     or %NULL if the #GtkScale:draw-value property is %FALSE.
1370  *
1371  * Since: 2.4
1372  */
1373 PangoLayout *
1374 gtk_scale_get_layout (GtkScale *scale)
1375 {
1376   GtkScalePrivate *priv;
1377   gchar *txt;
1378
1379   g_return_val_if_fail (GTK_IS_SCALE (scale), NULL);
1380
1381   priv = scale->priv;
1382
1383   if (!priv->layout)
1384     {
1385       if (priv->draw_value)
1386         priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (scale), NULL);
1387     }
1388
1389   if (priv->draw_value)
1390     {
1391       txt = _gtk_scale_format_value (scale,
1392                                      gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
1393       pango_layout_set_text (priv->layout, txt, -1);
1394       g_free (txt);
1395     }
1396
1397   return priv->layout;
1398 }
1399
1400 /**
1401  * gtk_scale_get_layout_offsets:
1402  * @scale: a #GtkScale
1403  * @x: (allow-none): location to store X offset of layout, or %NULL
1404  * @y: (allow-none): location to store Y offset of layout, or %NULL
1405  *
1406  * Obtains the coordinates where the scale will draw the 
1407  * #PangoLayout representing the text in the scale. Remember
1408  * when using the #PangoLayout function you need to convert to
1409  * and from pixels using PANGO_PIXELS() or #PANGO_SCALE. 
1410  *
1411  * If the #GtkScale:draw-value property is %FALSE, the return 
1412  * values are undefined.
1413  *
1414  * Since: 2.4
1415  */
1416 void 
1417 gtk_scale_get_layout_offsets (GtkScale *scale,
1418                               gint     *x,
1419                               gint     *y)
1420 {
1421   gint local_x = 0; 
1422   gint local_y = 0;
1423
1424   g_return_if_fail (GTK_IS_SCALE (scale));
1425
1426   if (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets)
1427     (GTK_SCALE_GET_CLASS (scale)->get_layout_offsets) (scale, &local_x, &local_y);
1428
1429   if (x)
1430     *x = local_x;
1431   
1432   if (y)
1433     *y = local_y;
1434 }
1435
1436 void
1437 _gtk_scale_clear_layout (GtkScale *scale)
1438 {
1439   GtkScalePrivate *priv = scale->priv;
1440
1441   g_return_if_fail (GTK_IS_SCALE (scale));
1442
1443   if (priv->layout)
1444     {
1445       g_object_unref (priv->layout);
1446       priv->layout = NULL;
1447     }
1448 }
1449
1450 static void
1451 gtk_scale_mark_free (GtkScaleMark *mark)
1452 {
1453   g_free (mark->markup);
1454   g_free (mark);
1455 }
1456
1457 /**
1458  * gtk_scale_clear_marks:
1459  * @scale: a #GtkScale
1460  * 
1461  * Removes any marks that have been added with gtk_scale_add_mark().
1462  *
1463  * Since: 2.16
1464  */
1465 void
1466 gtk_scale_clear_marks (GtkScale *scale)
1467 {
1468   GtkScalePrivate *priv;
1469
1470   g_return_if_fail (GTK_IS_SCALE (scale));
1471
1472   priv = scale->priv;
1473
1474   g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL);
1475   g_slist_free (priv->marks);
1476   priv->marks = NULL;
1477
1478   _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0);
1479
1480   gtk_widget_queue_resize (GTK_WIDGET (scale));
1481 }
1482
1483 static gint
1484 compare_marks (gpointer a, gpointer b)
1485 {
1486   GtkScaleMark *ma, *mb;
1487
1488   ma = a; mb = b;
1489
1490   return (gint) (ma->value - mb->value);
1491 }
1492
1493 /**
1494  * gtk_scale_add_mark:
1495  * @scale: a #GtkScale
1496  * @value: the value at which the mark is placed, must be between 
1497  *   the lower and upper limits of the scales' adjustment
1498  * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
1499  *   is drawn above the scale, anything else below. For a vertical scale,
1500  *   #GTK_POS_LEFT is drawn to the left of the scale, anything else to the
1501  *   right.
1502  * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
1503  *
1504  *
1505  * Adds a mark at @value. 
1506  *
1507  * A mark is indicated visually by drawing a tick mark next to the scale, 
1508  * and GTK+ makes it easy for the user to position the scale exactly at the 
1509  * marks value.
1510  *
1511  * If @markup is not %NULL, text is shown next to the tick mark. 
1512  *
1513  * To remove marks from a scale, use gtk_scale_clear_marks().
1514  *
1515  * Since: 2.16
1516  */
1517 void
1518 gtk_scale_add_mark (GtkScale        *scale,
1519                     gdouble          value,
1520                     GtkPositionType  position,
1521                     const gchar     *markup)
1522 {
1523   GtkScalePrivate *priv;
1524   GtkScaleMark *mark;
1525   GSList *m;
1526   gdouble *values;
1527   gint n, i;
1528
1529   g_return_if_fail (GTK_IS_SCALE (scale));
1530
1531   priv = scale->priv;
1532
1533   mark = g_new (GtkScaleMark, 1);
1534   mark->value = value;
1535   mark->markup = g_strdup (markup);
1536   mark->position = position;
1537  
1538   priv->marks = g_slist_insert_sorted (priv->marks, mark,
1539                                        (GCompareFunc) compare_marks);
1540
1541   n = g_slist_length (priv->marks);
1542   values = g_new (gdouble, n);
1543   for (m = priv->marks, i = 0; m; m = m->next, i++)
1544     {
1545       mark = m->data;
1546       values[i] = mark->value;
1547     }
1548   
1549   _gtk_range_set_stop_values (GTK_RANGE (scale), values, n);
1550
1551   g_free (values);
1552
1553   gtk_widget_queue_resize (GTK_WIDGET (scale));
1554 }
1555
1556 static GtkBuildableIface *parent_buildable_iface;
1557
1558 static void
1559 gtk_scale_buildable_interface_init (GtkBuildableIface *iface)
1560 {
1561   parent_buildable_iface = g_type_interface_peek_parent (iface);
1562   iface->custom_tag_start = gtk_scale_buildable_custom_tag_start;
1563   iface->custom_finished = gtk_scale_buildable_custom_finished;
1564 }
1565
1566 typedef struct
1567 {
1568   GtkScale *scale;
1569   GtkBuilder *builder;
1570   GSList *marks;
1571 } MarksSubparserData;
1572
1573 typedef struct
1574 {
1575   gdouble value;
1576   GtkPositionType position;
1577   GString *markup;
1578   gchar *context;
1579   gboolean translatable;
1580 } MarkData;
1581
1582 static void
1583 mark_data_free (MarkData *data)
1584 {
1585   g_string_free (data->markup, TRUE);
1586   g_free (data->context);
1587   g_slice_free (MarkData, data);
1588 }
1589
1590 static void
1591 marks_start_element (GMarkupParseContext *context,
1592                      const gchar         *element_name,
1593                      const gchar        **names,
1594                      const gchar        **values,
1595                      gpointer             user_data,
1596                      GError             **error)
1597 {
1598   MarksSubparserData *parser_data = (MarksSubparserData*)user_data;
1599   guint i;
1600   gint line_number, char_number;
1601
1602   if (strcmp (element_name, "marks") == 0)
1603    ;
1604   else if (strcmp (element_name, "mark") == 0)
1605     {
1606       gdouble value = 0;
1607       gboolean has_value = FALSE;
1608       GtkPositionType position = GTK_POS_BOTTOM;
1609       const gchar *msg_context = NULL;
1610       gboolean translatable = FALSE;
1611       MarkData *mark;
1612
1613       for (i = 0; names[i]; i++)
1614         {
1615           if (strcmp (names[i], "translatable") == 0)
1616             {
1617               if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
1618                 return;
1619             }
1620           else if (strcmp (names[i], "comments") == 0)
1621             {
1622               /* do nothing, comments are for translators */
1623             }
1624           else if (strcmp (names[i], "context") == 0)
1625             msg_context = values[i];
1626           else if (strcmp (names[i], "value") == 0)
1627             {
1628               GValue gvalue = { 0, };
1629
1630               if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error))
1631                 return;
1632
1633               value = g_value_get_double (&gvalue);
1634               has_value = TRUE;
1635             }
1636           else if (strcmp (names[i], "position") == 0)
1637             {
1638               GValue gvalue = { 0, };
1639
1640               if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error))
1641                 return;
1642
1643               position = g_value_get_enum (&gvalue);
1644             }
1645           else
1646             {
1647               g_markup_parse_context_get_position (context,
1648                                                    &line_number,
1649                                                    &char_number);
1650               g_set_error (error,
1651                            GTK_BUILDER_ERROR,
1652                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1653                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
1654                            "<input>",
1655                            line_number, char_number, names[i], "mark");
1656               return;
1657             }
1658         }
1659
1660       if (!has_value)
1661         {
1662           g_markup_parse_context_get_position (context,
1663                                                &line_number,
1664                                                &char_number);
1665           g_set_error (error,
1666                        GTK_BUILDER_ERROR,
1667                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1668                        "%s:%d:%d <%s> requires attribute \"%s\"",
1669                        "<input>",
1670                        line_number, char_number, "mark",
1671                        "value");
1672           return;
1673         }
1674
1675       mark = g_slice_new (MarkData);
1676       mark->value = value;
1677       mark->position = position;
1678       mark->markup = g_string_new ("");
1679       mark->context = g_strdup (msg_context);
1680       mark->translatable = translatable;
1681
1682       parser_data->marks = g_slist_prepend (parser_data->marks, mark);
1683     }
1684   else
1685     {
1686       g_markup_parse_context_get_position (context,
1687                                            &line_number,
1688                                            &char_number);
1689       g_set_error (error,
1690                    GTK_BUILDER_ERROR,
1691                    GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1692                    "%s:%d:%d unsupported tag for GtkScale: \"%s\"",
1693                    "<input>",
1694                    line_number, char_number, element_name);
1695       return;
1696     }
1697 }
1698
1699 static void
1700 marks_text (GMarkupParseContext  *context,
1701             const gchar          *text,
1702             gsize                 text_len,
1703             gpointer              user_data,
1704             GError              **error)
1705 {
1706   MarksSubparserData *data = (MarksSubparserData*)user_data;
1707
1708   if (strcmp (g_markup_parse_context_get_element (context), "mark") == 0)
1709     {
1710       MarkData *mark = data->marks->data;
1711
1712       g_string_append_len (mark->markup, text, text_len);
1713     }
1714 }
1715
1716 static const GMarkupParser marks_parser =
1717   {
1718     marks_start_element,
1719     NULL,
1720     marks_text,
1721   };
1722
1723
1724 static gboolean
1725 gtk_scale_buildable_custom_tag_start (GtkBuildable  *buildable,
1726                                       GtkBuilder    *builder,
1727                                       GObject       *child,
1728                                       const gchar   *tagname,
1729                                       GMarkupParser *parser,
1730                                       gpointer      *data)
1731 {
1732   MarksSubparserData *parser_data;
1733
1734   if (child)
1735     return FALSE;
1736
1737   if (strcmp (tagname, "marks") == 0)
1738     {
1739       parser_data = g_slice_new0 (MarksSubparserData);
1740       parser_data->scale = GTK_SCALE (buildable);
1741       parser_data->marks = NULL;
1742
1743       *parser = marks_parser;
1744       *data = parser_data;
1745       return TRUE;
1746     }
1747
1748   return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1749                                                    tagname, parser, data);
1750 }
1751
1752 static void
1753 gtk_scale_buildable_custom_finished (GtkBuildable *buildable,
1754                                      GtkBuilder   *builder,
1755                                      GObject      *child,
1756                                      const gchar  *tagname,
1757                                      gpointer      user_data)
1758 {
1759   GtkScale *scale = GTK_SCALE (buildable);
1760   MarksSubparserData *marks_data;
1761
1762   if (strcmp (tagname, "marks") == 0)
1763     {
1764       GSList *m;
1765       gchar *markup;
1766
1767       marks_data = (MarksSubparserData *)user_data;
1768
1769       for (m = marks_data->marks; m; m = m->next)
1770         {
1771           MarkData *mdata = m->data;
1772
1773           if (mdata->translatable && mdata->markup->len)
1774             markup = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
1775                                                     mdata->context,
1776                                                     mdata->markup->str);
1777           else
1778             markup = mdata->markup->str;
1779
1780           gtk_scale_add_mark (scale, mdata->value, mdata->position, markup);
1781
1782           mark_data_free (mdata);
1783         }
1784
1785       g_slist_free (marks_data->marks);
1786       g_slice_free (MarksSubparserData, marks_data);
1787     }
1788 }