]> Pileus Git - ~andy/gtk/blob - gtk/gtkscalebutton.c
Bug 442042 – GtkScaleButton is too limited
[~andy/gtk] / gtk / gtkscalebutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2005 Ronald S. Bultje
3  * Copyright (C) 2006, 2007 Christian Persch
4  * Copyright (C) 2006 Jan Arne Petersen
5  * Copyright (C) 2005-2007 Red Hat, Inc.
6  *
7  * Authors:
8  * - Ronald S. Bultje <rbultje@ronald.bitfreak.net>
9  * - Bastien Nocera <bnocera@redhat.com>
10  * - Jan Arne Petersen <jpetersen@jpetersen.org>
11  * - Christian Persch <chpe@svn.gnome.org>
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this library; if not, write to the
25  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26  * Boston, MA 02111-1307, USA.
27  */
28
29 /*
30  * Modified by the GTK+ Team and others 2007.  See the AUTHORS
31  * file for a list of people on the GTK+ Team.  See the ChangeLog
32  * files for a list of changes.  These files are distributed with
33  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34  */
35
36 #include "config.h"
37
38 #define _GNU_SOURCE
39 #include <math.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include <gdk-pixbuf/gdk-pixbuf.h>
44 #include <gdk/gdkkeysyms.h>
45
46 #include "gtkmain.h"
47 #include "gtkintl.h"
48 #include "gtkbindings.h"
49 #include "gtkhscale.h"
50 #include "gtkvscale.h"
51 #include "gtkframe.h"
52 #include "gtkvbox.h"
53 #include "gtkwindow.h"
54 #include "gtkmarshalers.h"
55 #include "gtkstock.h"
56 #include "gtkprivate.h"
57 #include "gtkscalebutton.h"
58
59 #include "gtkalias.h"
60
61 #define SCALE_SIZE 100
62 #define CLICK_TIMEOUT 250
63
64 enum
65 {
66   VALUE_CHANGED,
67   POPUP,
68   POPDOWN,
69
70   LAST_SIGNAL
71 };
72
73 enum
74 {
75   PROP_0,
76
77   PROP_ORIENTATION,
78   PROP_VALUE,
79   PROP_SIZE,
80   PROP_ADJUSTMENT,
81   PROP_ICONS
82 };
83
84 #define GET_PRIVATE(obj)        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SCALE_BUTTON, GtkScaleButtonPrivate))
85
86 struct _GtkScaleButtonPrivate
87 {
88   GtkWidget *dock;
89   GtkWidget *scale;
90   GtkWidget *image;
91
92   GtkIconSize size;
93   GtkOrientation orientation;
94
95   guint click_id;
96   gint click_timeout;
97   guint timeout : 1;
98   gdouble direction;
99   guint32 pop_time;
100
101   gchar **icon_list;
102
103   GtkAdjustment *adjustment; /* needed because it must be settable in init() */
104 };
105
106 static GObject* gtk_scale_button_constructor    (GType                  type,
107                                                  guint                  n_construct_properties,
108                                                  GObjectConstructParam *construct_params);
109 static void     gtk_scale_button_dispose        (GObject             *object);
110 static void     gtk_scale_button_finalize       (GObject             *object);
111 static void     gtk_scale_button_set_property   (GObject             *object,
112                                                  guint                prop_id,
113                                                  const GValue        *value,
114                                                  GParamSpec          *pspec);
115 static void     gtk_scale_button_get_property   (GObject             *object,
116                                                  guint                prop_id,
117                                                  GValue              *value,
118                                                  GParamSpec          *pspec);
119 static gboolean gtk_scale_button_scroll         (GtkWidget           *widget,
120                                                  GdkEventScroll      *event);
121 static void gtk_scale_button_screen_changed     (GtkWidget           *widget,
122                                                  GdkScreen           *previous_screen);
123 static gboolean gtk_scale_button_press          (GtkWidget           *widget,
124                                                  GdkEventButton      *event);
125 static gboolean gtk_scale_button_key_release    (GtkWidget           *widget,
126                                                  GdkEventKey         *event);
127 static void gtk_scale_button_popup_from_bindings (GtkWidget *widget);
128 static void gtk_scale_button_popdown_from_bindings (GtkWidget *widget);
129 static gboolean cb_dock_button_press            (GtkWidget           *widget,
130                                                  GdkEventButton      *event,
131                                                  gpointer             user_data);
132 static gboolean cb_dock_key_release             (GtkWidget           *widget,
133                                                  GdkEventKey         *event,
134                                                  gpointer             user_data);
135 static gboolean cb_button_press                 (GtkWidget           *widget,
136                                                  GdkEventButton      *event,
137                                                  gpointer             user_data);
138 static gboolean cb_button_release               (GtkWidget           *widget,
139                                                  GdkEventButton      *event,
140                                                  gpointer             user_data);
141 static void cb_dock_grab_notify                 (GtkWidget           *widget,
142                                                  gboolean             was_grabbed,
143                                                  gpointer             user_data);
144 static gboolean cb_dock_grab_broken_event       (GtkWidget           *widget,
145                                                  gboolean             was_grabbed,
146                                                  gpointer             user_data);
147 static void cb_scale_grab_notify                (GtkWidget           *widget,
148                                                  gboolean             was_grabbed,
149                                                  gpointer             user_data);
150 static void gtk_scale_button_update_icon        (GtkScaleButton      *button);
151 static void gtk_scale_button_scale_value_changed(GtkRange            *range);
152
153 /* see below for scale definitions */
154 static GtkWidget *gtk_scale_button_scale_new    (GtkScaleButton      *button,
155                                                  gdouble              min,
156                                                  gdouble              max,
157                                                  gdouble              step);
158
159 static guint signals[LAST_SIGNAL] = { 0, };
160
161 G_DEFINE_TYPE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON)
162
163 static void
164 gtk_scale_button_class_init (GtkScaleButtonClass *klass)
165 {
166   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
167   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
168   GtkBindingSet *binding_set;
169
170   g_type_class_add_private (klass, sizeof (GtkScaleButtonPrivate));
171
172   gobject_class->constructor = gtk_scale_button_constructor;
173   gobject_class->finalize = gtk_scale_button_finalize;
174   gobject_class->dispose = gtk_scale_button_dispose;
175   gobject_class->set_property = gtk_scale_button_set_property;
176   gobject_class->get_property = gtk_scale_button_get_property;
177
178   widget_class->button_press_event = gtk_scale_button_press;
179   widget_class->key_release_event = gtk_scale_button_key_release;
180   widget_class->scroll_event = gtk_scale_button_scroll;
181   widget_class->screen_changed = gtk_scale_button_screen_changed;
182
183   g_object_class_install_property (gobject_class,
184                                    PROP_ORIENTATION,
185                                    g_param_spec_enum ("orientation",
186                                                       P_("Orientation"),
187                                                       P_("The orientation of the scale"),
188                                                       GTK_TYPE_ORIENTATION,
189                                                       GTK_ORIENTATION_VERTICAL,
190                                                       GTK_PARAM_READWRITE |
191                                                       G_PARAM_CONSTRUCT_ONLY));
192
193   g_object_class_install_property (gobject_class,
194                                    PROP_VALUE,
195                                    g_param_spec_double ("value",
196                                                         P_("Value"),
197                                                         P_("The value of the scale"),
198                                                         -G_MAXDOUBLE,
199                                                         G_MAXDOUBLE,
200                                                         0,
201                                                         GTK_PARAM_READWRITE));
202
203   g_object_class_install_property (gobject_class,
204                                    PROP_SIZE,
205                                    g_param_spec_enum ("size",
206                                                       P_("Icon size"),
207                                                       P_("The icon size"),
208                                                       GTK_TYPE_ICON_SIZE,
209                                                       GTK_ICON_SIZE_SMALL_TOOLBAR,
210                                                       GTK_PARAM_READWRITE));
211
212   g_object_class_install_property (gobject_class,
213                                    PROP_ADJUSTMENT,
214                                    g_param_spec_object ("adjustment",
215                                                         P_("Adjustment"),
216                                                         P_("The GtkAdjustment that contains the current value of this scale button object"),
217                                                         GTK_TYPE_ADJUSTMENT,
218                                                         GTK_PARAM_READWRITE));
219
220   /**
221    * GtkScaleButton:icons:
222    *
223    * The names of the icons to be used by the scale button.
224    * The first item in the array will be used in the button
225    * when the current value is the lowest value, the second
226    * item for the highest value. All the subsequent icons will
227    * be used for all the other values, spread evenly over the
228    * range of values.
229    *
230    * If there's only one icon name in the @icons array, it will
231    * be used for all the values. If only two icon names are in
232    * the @icons array, the first one will be used for the bottom
233    * 50% of the scale, and the second one for the top 50%.
234    *
235    * It is recommended to use at least 3 icons so that the
236    * #GtkScaleButton reflects the current value of the scale
237    * better for the users.
238    *
239    * Since: 2.12
240    */
241   g_object_class_install_property (gobject_class,
242                                    PROP_ICONS,
243                                    g_param_spec_boxed ("icons",
244                                                        P_("Icons"),
245                                                        P_("List of icon names"),
246                                                        G_TYPE_STRV,
247                                                        GTK_PARAM_READWRITE));
248
249   /**
250    * GtkScaleButton::value-changed:
251    * @button: the object which received the signal
252    * @value: the new value
253    *
254    * The ::value-changed signal is emitted when the value field has
255    * changed.
256    *
257    * Since: 2.12
258    */
259   signals[VALUE_CHANGED] =
260     g_signal_new (I_("value-changed"),
261                   G_TYPE_FROM_CLASS (klass),
262                   G_SIGNAL_RUN_LAST,
263                   G_STRUCT_OFFSET (GtkScaleButtonClass, value_changed),
264                   NULL, NULL,
265                   _gtk_marshal_VOID__DOUBLE,
266                   G_TYPE_NONE, 1, G_TYPE_DOUBLE);
267
268   /**
269    * GtkScaleButton::popup:
270    * @button: the object which received the signal
271    *
272    * The ::popup signal is a
273    * <link linkend="keybinding-signals">keybinding signal</link>
274    * which gets emitted to popup the scale widget.
275    *
276    * The default bindings for this signal are Space, Enter and Return.
277    *
278    * Since: 2.12
279    */
280   signals[POPUP] =
281     _gtk_binding_signal_new (I_("popup"),
282                              G_OBJECT_CLASS_TYPE (klass),
283                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
284                              G_CALLBACK (gtk_scale_button_popup_from_bindings),
285                              NULL, NULL,
286                              g_cclosure_marshal_VOID__VOID,
287                              G_TYPE_NONE, 0);
288
289   /**
290    * GtkScaleButton::popdown:
291    * @button: the object which received the signal
292    *
293    * The ::popdown signal is a
294    * <link linkend="keybinding-signals">keybinding signal</link>
295    * which gets emitted to popdown the scale widget.
296    *
297    * The default binding for this signal is Escape.
298    *
299    * Since: 2.12
300    */
301   signals[POPDOWN] =
302     _gtk_binding_signal_new (I_("popdown"),
303                              G_OBJECT_CLASS_TYPE (klass),
304                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
305                              G_CALLBACK (gtk_scale_button_popdown_from_bindings),
306                              NULL, NULL,
307                              g_cclosure_marshal_VOID__VOID,
308                              G_TYPE_NONE, 0);
309
310   /* Key bindings */
311   binding_set = gtk_binding_set_by_class (widget_class);
312
313   gtk_binding_entry_add_signal (binding_set, GDK_space, 0,
314                                 "popup", 0);
315   gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, 0,
316                                 "popup", 0);
317   gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
318                                 "popup", 0);
319   gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
320                                 "popup", 0);
321   gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
322                                 "popup", 0);
323   gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
324                                 "popdown", 0);
325 }
326
327 static void
328 gtk_scale_button_init (GtkScaleButton *button)
329 {
330   GtkScaleButtonPrivate *priv;
331
332   button->priv = priv = GET_PRIVATE (button);
333
334   priv->timeout = FALSE;
335   priv->click_id = 0;
336   priv->click_timeout = CLICK_TIMEOUT;
337
338   gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
339   gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
340
341   /* image */
342   priv->image = gtk_image_new ();
343   gtk_container_add (GTK_CONTAINER (button), priv->image);
344   gtk_widget_show_all (priv->image);
345
346   /* window */
347   priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
348   g_signal_connect (priv->dock, "button-press-event",
349                     G_CALLBACK (cb_dock_button_press), button);
350   g_signal_connect (priv->dock, "key-release-event",
351                     G_CALLBACK (cb_dock_key_release), button);
352   g_signal_connect (priv->dock, "grab-notify",
353                     G_CALLBACK (cb_dock_grab_notify), button);
354   g_signal_connect (priv->dock, "grab-broken-event",
355                     G_CALLBACK (cb_dock_grab_broken_event), button);
356   gtk_window_set_decorated (GTK_WINDOW (priv->dock), FALSE);
357
358   /* + */
359   button->plus_button = gtk_button_new_with_label ("+");
360   gtk_button_set_relief (GTK_BUTTON (button->plus_button), GTK_RELIEF_NONE);
361   g_signal_connect (button->plus_button, "button-press-event",
362                     G_CALLBACK (cb_button_press), button);
363   g_signal_connect (button->plus_button, "button-release-event",
364                     G_CALLBACK (cb_button_release), button);
365
366   /* - */
367   button->minus_button = gtk_button_new_with_label ("-");
368   gtk_button_set_relief (GTK_BUTTON (button->minus_button), GTK_RELIEF_NONE);
369   g_signal_connect (button->minus_button, "button-press-event",
370                    G_CALLBACK (cb_button_press), button);
371   g_signal_connect (button->minus_button, "button-release-event",
372                     G_CALLBACK (cb_button_release), button);
373
374   priv->adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 100.0, 2, 20, 0));
375   g_object_ref_sink (priv->adjustment);
376 }
377
378 static GObject *
379 gtk_scale_button_constructor (GType                  type,
380                               guint                  n_construct_properties,
381                               GObjectConstructParam *construct_params)
382 {
383   GObject *object;
384   GtkScaleButton *button;
385   GtkWidget *frame, *box;
386   GtkScaleButtonPrivate *priv;
387
388   object = G_OBJECT_CLASS (gtk_scale_button_parent_class)->constructor (type, n_construct_properties, construct_params);
389
390   button = GTK_SCALE_BUTTON (object);
391
392   priv = button->priv;
393
394   /* frame */
395   frame = gtk_frame_new (NULL);
396   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
397   gtk_container_add (GTK_CONTAINER (priv->dock), frame);
398   box = gtk_vbox_new (FALSE, 0);
399   gtk_container_add (GTK_CONTAINER (frame), box);
400
401   /* + */
402   gtk_box_pack_start (GTK_BOX (box), button->plus_button, TRUE, FALSE, 0);
403
404   /* scale */
405   priv->scale = gtk_scale_button_scale_new (button, 0., 100., 2.);
406   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
407     {
408       gtk_widget_set_size_request (priv->scale, -1, SCALE_SIZE);
409       gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
410     }
411   else
412     {
413       gtk_widget_set_size_request (priv->scale, SCALE_SIZE, -1);
414     }
415
416   gtk_scale_set_draw_value (GTK_SCALE (priv->scale), FALSE);
417   gtk_box_pack_start (GTK_BOX (box), priv->scale, TRUE, FALSE, 0);
418   g_signal_connect (priv->scale, "grab-notify",
419                     G_CALLBACK (cb_scale_grab_notify), button);
420
421   /* - */
422   gtk_box_pack_start (GTK_BOX (box), button->minus_button, TRUE, FALSE, 0);
423
424   /* set button text and size */
425   priv->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
426   gtk_scale_button_update_icon (button);
427
428   return object;
429 }
430
431 static void
432 gtk_scale_button_set_property (GObject       *object,
433                                guint          prop_id,
434                                const GValue  *value,
435                                GParamSpec    *pspec)
436 {
437   GtkScaleButton *button;
438
439   button = GTK_SCALE_BUTTON (object);
440
441   switch (prop_id)
442     {
443     case PROP_ORIENTATION:
444       button->priv->orientation = g_value_get_enum (value);
445       break;
446     case PROP_VALUE:
447       gtk_scale_button_set_value (button, g_value_get_double (value));
448       break;
449     case PROP_SIZE:
450       {
451         GtkIconSize size;
452         size = g_value_get_enum (value);
453         if (button->priv->size != size)
454           {
455             button->priv->size = size;
456             gtk_scale_button_update_icon (button);
457           }
458       }
459       break;
460     case PROP_ADJUSTMENT:
461       gtk_scale_button_set_adjustment (button, g_value_get_object (value));
462       break;
463     case PROP_ICONS:
464       gtk_scale_button_set_icons (button,
465                                   (const gchar **)g_value_get_boxed (value));
466       break;
467     default:
468       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
469       break;
470     }
471 }
472
473 static void
474 gtk_scale_button_get_property (GObject     *object,
475                                guint        prop_id,
476                                GValue      *value,
477                                GParamSpec  *pspec)
478 {
479   GtkScaleButton *button;
480   GtkScaleButtonPrivate *priv;
481
482   button = GTK_SCALE_BUTTON (object);
483   priv = button->priv;
484
485   switch (prop_id)
486     {
487     case PROP_ORIENTATION:
488       g_value_set_enum (value, priv->orientation);
489       break;
490     case PROP_VALUE:
491       g_value_set_double (value, gtk_scale_button_get_value (button));
492       break;
493     case PROP_SIZE:
494       g_value_set_enum (value, priv->size);
495       break;
496     case PROP_ADJUSTMENT:
497       g_value_set_object (value, gtk_scale_button_get_adjustment (button));
498       break;
499     case PROP_ICONS:
500       g_value_set_boxed (value, priv->icon_list);
501       break;
502     default:
503       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
504       break;
505     }
506 }
507
508 static void
509 gtk_scale_button_finalize (GObject *object)
510 {
511   GtkScaleButton *button = GTK_SCALE_BUTTON (object);
512   GtkScaleButtonPrivate *priv = button->priv;
513
514   if (priv->icon_list)
515     {
516       g_strfreev (priv->icon_list);
517       priv->icon_list = NULL;
518     }
519
520   if (priv->adjustment)
521     {
522       g_object_unref (priv->adjustment);
523       priv->adjustment = NULL;
524     }
525
526   G_OBJECT_CLASS (gtk_scale_button_parent_class)->finalize (object);
527 }
528
529 static void
530 gtk_scale_button_dispose (GObject *object)
531 {
532   GtkScaleButton *button = GTK_SCALE_BUTTON (object);
533   GtkScaleButtonPrivate *priv = button->priv;
534
535   if (priv->dock)
536     {
537       gtk_widget_destroy (priv->dock);
538       priv->dock = NULL;
539     }
540
541   if (priv->click_id != 0)
542     {
543       g_source_remove (priv->click_id);
544       priv->click_id = 0;
545     }
546
547   G_OBJECT_CLASS (gtk_scale_button_parent_class)->dispose (object);
548 }
549
550 /**
551  * gtk_scale_button_new:
552  * @size: a stock icon size
553  * @min: the minimum value of the scale (usually 0)
554  * @max: the maximum value of the scale (usually 100)
555  * @step: the stepping of value when a scroll-wheel event,
556  *        or up/down arrow event occurs (usually 2)
557  * @icons: a %NULL-terminated array of icon names, or %NULL if
558  *         you want to set the list later with gtk_scale_button_set_icons()
559  *
560  * Creates a #GtkScaleButton, with a range between @min and @max, with
561  * a stepping of @step.
562  *
563  * Return value: a new #GtkScaleButton
564  *
565  * Since: 2.12
566  */
567 GtkWidget *
568 gtk_scale_button_new (GtkIconSize   size,
569                       gdouble       min,
570                       gdouble       max,
571                       gdouble       step,
572                       const gchar **icons)
573 {
574   GtkScaleButton *button;
575   GtkObject *adj;
576
577   adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
578
579   button = g_object_new (GTK_TYPE_SCALE_BUTTON,
580                          "adjustment", adj,
581                          "icons", icons,
582                          "size", size,
583                          NULL);
584
585   return GTK_WIDGET (button);
586 }
587
588 /**
589  * gtk_scale_button_get_value:
590  * @button: a #GtkScaleButton
591  *
592  * Gets the current value of the scale button.
593  *
594  * Return value: current value of the scale button
595  *
596  * Since: 2.12
597  */
598 gdouble
599 gtk_scale_button_get_value (GtkScaleButton * button)
600 {
601   GtkScaleButtonPrivate *priv;
602
603   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), 0);
604
605   priv = button->priv;
606
607   return gtk_adjustment_get_value (priv->adjustment);
608 }
609
610 /**
611  * gtk_scale_button_set_value:
612  * @button: a #GtkScaleButton
613  * @value: new value of the scale button
614  *
615  * Sets the current value of the scale; if the value is outside
616  * the minimum or maximum range values, it will be clamped to fit
617  * inside them. The scale button emits the #GtkScaleButton::value-changed
618  * signal if the value changes.
619  *
620  * Since: 2.12
621  */
622 void
623 gtk_scale_button_set_value (GtkScaleButton *button,
624                             gdouble         value)
625 {
626   GtkScaleButtonPrivate *priv;
627
628   g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
629
630   priv = button->priv;
631
632   gtk_range_set_value (GTK_RANGE (priv->scale), value);
633 }
634
635 /**
636  * gtk_scale_button_set_icons:
637  * @button: a #GtkScaleButton
638  * @icons: a %NULL-terminated array of icon names
639  *
640  * Sets the icons to be used by the scale button.
641  * For details, see the #GtkScaleButton:icons property.
642  *
643  * Since: 2.12
644  */
645 void
646 gtk_scale_button_set_icons (GtkScaleButton  *button,
647                             const gchar    **icons)
648 {
649   GtkScaleButtonPrivate *priv;
650   gchar **tmp;
651
652   g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
653
654   priv = button->priv;
655
656   tmp = priv->icon_list;
657   priv->icon_list = g_strdupv ((gchar **) icons);
658   g_strfreev (tmp);
659   gtk_scale_button_update_icon (button);
660
661   g_object_notify (G_OBJECT (button), "icons");
662 }
663
664 /**
665  * gtk_scale_button_get_adjustment:
666  * @button: a #GtkScaleButton
667  *
668  * Gets the #GtkAdjustment associated with the #GtkScaleButton's scale.
669  * See gtk_range_get_adjustment() for details.
670  *
671  * Returns: the adjustment associated with the scale
672  *
673  * Since: 2.12
674  */
675 GtkAdjustment*
676 gtk_scale_button_get_adjustment (GtkScaleButton *button)
677 {
678   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
679
680   return button->priv->adjustment;
681 }
682
683 /**
684  * gtk_scale_button_set_adjustment:
685  * @button: a #GtkScaleButton
686  * @adjustment: a #GtkAdjustment
687  *
688  * Sets the #GtkAdjustment to be used as a model
689  * for the #GtkScaleButton's scale.
690  * See gtk_range_set_adjustment() for details.
691  *
692  * Since: 2.12
693  */
694 void
695 gtk_scale_button_set_adjustment (GtkScaleButton *button,
696                                  GtkAdjustment  *adjustment)
697 {
698   g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
699   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
700
701   if (button->priv->adjustment)
702     g_object_unref (button->priv->adjustment);
703   button->priv->adjustment = g_object_ref_sink (adjustment);
704
705   if (button->priv->scale)
706     gtk_range_set_adjustment (GTK_RANGE (button->priv->scale), adjustment);
707
708   g_object_notify (G_OBJECT (button), "adjustment");
709 }
710
711 /*
712  * button callbacks.
713  */
714
715 static gboolean
716 gtk_scale_button_scroll (GtkWidget      *widget,
717                          GdkEventScroll *event)
718 {
719   GtkScaleButton *button;
720   GtkScaleButtonPrivate *priv;
721   GtkAdjustment *adj;
722   gdouble d;
723
724   button = GTK_SCALE_BUTTON (widget);
725   priv = button->priv;
726   adj = priv->adjustment;
727
728   if (event->type != GDK_SCROLL)
729     return FALSE;
730
731   d = gtk_scale_button_get_value (button);
732   if (event->direction == GDK_SCROLL_UP)
733     {
734       d += adj->step_increment;
735       if (d > adj->upper)
736         d = adj->upper;
737     }
738   else
739     {
740       d -= adj->step_increment;
741       if (d < adj->lower)
742         d = adj->lower;
743     }
744   gtk_scale_button_set_value (button, d);
745
746   return TRUE;
747 }
748
749 static void
750 gtk_scale_button_screen_changed (GtkWidget *widget,
751                                  GdkScreen *previous_screen)
752 {
753   GtkScaleButton *button = (GtkScaleButton *) widget;
754   GtkScaleButtonPrivate *priv;
755   GdkScreen *screen;
756   GValue value = { 0, };
757
758   if (gtk_widget_has_screen (widget) == FALSE)
759     return;
760
761   priv = button->priv;
762
763   screen = gtk_widget_get_screen (widget);
764   g_value_init (&value, G_TYPE_INT);
765   if (gdk_screen_get_setting (screen,
766                               "gtk-double-click-time",
767                               &value) == FALSE)
768     {
769       priv->click_timeout = CLICK_TIMEOUT;
770       return;
771     }
772
773   priv->click_timeout = g_value_get_int (&value);
774 }
775
776 static gboolean
777 gtk_scale_popup (GtkWidget *widget,
778                  GdkEvent  *event,
779                  guint32    time)
780 {
781   GtkScaleButton *button;
782   GtkScaleButtonPrivate *priv;
783   GtkAdjustment *adj;
784   gint x, y, m, dx, dy, sx, sy, startoff;
785   gdouble v;
786   GdkDisplay *display;
787   GdkScreen *screen;
788
789   button = GTK_SCALE_BUTTON (widget);
790   priv = button->priv;
791   adj = priv->adjustment;
792
793   display = gtk_widget_get_display (widget);
794   screen = gtk_widget_get_screen (widget);
795
796   /* position roughly */
797   gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
798
799   gdk_window_get_origin (widget->window, &x, &y);
800   x += widget->allocation.x;
801   y += widget->allocation.y;
802
803   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
804     gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
805   else
806     gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
807
808   gtk_widget_show_all (priv->dock);
809
810   gdk_window_get_origin (priv->dock->window, &dx, &dy);
811   dx += priv->dock->allocation.x;
812   dy += priv->dock->allocation.y;
813
814   gdk_window_get_origin (priv->scale->window, &sx, &sy);
815   sx += priv->scale->allocation.x;
816   sy += priv->scale->allocation.y;
817
818   priv->timeout = TRUE;
819
820   /* position (needs widget to be shown already) */
821   v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
822
823   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
824     {
825       startoff = sy - dy;
826
827       x += (widget->allocation.width - priv->dock->allocation.width) / 2;
828       y -= startoff;
829       y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
830       m = priv->scale->allocation.height -
831           GTK_RANGE (priv->scale)->min_slider_size;
832       y -= m * (1.0 - v);
833     }
834   else
835     {
836       startoff = sx - dx;
837
838       x -= startoff;
839       y += (widget->allocation.height - priv->dock->allocation.height) / 2;
840       x -= GTK_RANGE (priv->scale)->min_slider_size / 2;
841       m = priv->scale->allocation.width -
842           GTK_RANGE (priv->scale)->min_slider_size;
843       x -= m * v;
844     }
845
846   /* Make sure the dock stays inside the monitor */
847   if (event->type == GDK_BUTTON_PRESS)
848     {
849       int monitor;
850       GdkEventButton *button_event = (GdkEventButton *) event;
851       GdkRectangle rect;
852       GtkWidget *d;
853
854       d = GTK_WIDGET (priv->dock);
855       monitor = gdk_screen_get_monitor_at_point (screen,
856                                                  button_event->x_root,
857                                                  button_event->y_root);
858       gdk_screen_get_monitor_geometry (screen, monitor, &rect);
859
860       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
861         y += button_event->y;
862       else
863         x += button_event->x;
864
865       if (y < rect.y)
866         y = rect.y;
867       else if (y + d->allocation.height > rect.height + rect.y)
868         y = rect.y + rect.height - d->allocation.height;
869
870       if (x < rect.x)
871         x = rect.x;
872       else if (x + d->allocation.width > rect.width + rect.x)
873         x = rect.x + rect.width - d->allocation.width;
874     }
875
876   gtk_window_move (GTK_WINDOW (priv->dock), x, y);
877
878   if (event->type == GDK_BUTTON_PRESS)
879     GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
880
881   /* grab focus */
882   gtk_grab_add (priv->dock);
883
884   if (gdk_pointer_grab (priv->dock->window, TRUE,
885                         GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
886                         GDK_POINTER_MOTION_MASK, NULL, NULL, time)
887       != GDK_GRAB_SUCCESS)
888     {
889       gtk_grab_remove (priv->dock);
890       gtk_widget_hide (priv->dock);
891       return FALSE;
892     }
893
894   if (gdk_keyboard_grab (priv->dock->window, TRUE, time) != GDK_GRAB_SUCCESS)
895     {
896       gdk_display_pointer_ungrab (display, time);
897       gtk_grab_remove (priv->dock);
898       gtk_widget_hide (priv->dock);
899       return FALSE;
900     }
901
902   gtk_widget_grab_focus (priv->dock);
903
904   if (event->type == GDK_BUTTON_PRESS)
905     {
906       GdkEventButton *e;
907       GdkEventButton *button_event = (GdkEventButton *) event;
908
909       /* forward event to the slider */
910       e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
911       e->window = priv->scale->window;
912
913       /* position: the X position isn't relevant, halfway will work just fine.
914        * The vertical position should be *exactly* in the middle of the slider
915        * of the scale; if we don't do that correctly, it'll move from its current
916        * position, which means a position change on-click, which is bad.
917        */
918       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
919         {
920           e->x = priv->scale->allocation.width / 2;
921           m = priv->scale->allocation.height -
922               GTK_RANGE (priv->scale)->min_slider_size;
923           e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
924         }
925       else
926         {
927           e->y = priv->scale->allocation.height / 2;
928           m = priv->scale->allocation.width -
929               GTK_RANGE (priv->scale)->min_slider_size;
930           e->x = (v * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
931         }
932
933       gtk_widget_event (priv->scale, (GdkEvent *) e);
934       e->window = button_event->window;
935       gdk_event_free ((GdkEvent *) e);
936     }
937
938   gtk_widget_grab_focus (priv->scale);
939
940   priv->pop_time = time;
941
942   return TRUE;
943 }
944
945 static gboolean
946 gtk_scale_button_press (GtkWidget      *widget,
947                         GdkEventButton *event)
948 {
949   return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
950 }
951
952 static void
953 gtk_scale_button_popup_from_bindings (GtkWidget *widget)
954 {
955   GdkEvent *ev;
956
957   ev = gdk_event_new (GDK_KEY_RELEASE);
958   gtk_scale_popup (widget, ev, GDK_CURRENT_TIME);
959   gdk_event_free (ev);
960 }
961
962 static gboolean
963 gtk_scale_button_key_release (GtkWidget   *widget,
964                               GdkEventKey *event)
965 {
966   return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
967 }
968
969 /* This is called when the grab is broken for
970  * either the dock, or the scale itself */
971 static void
972 gtk_scale_button_grab_notify (GtkScaleButton *button,
973                               gboolean        was_grabbed)
974 {
975   GdkDisplay *display;
976   GtkScaleButtonPrivate *priv;
977
978   if (was_grabbed != FALSE)
979     return;
980
981   priv = button->priv;
982
983   if (!GTK_WIDGET_HAS_GRAB (priv->dock))
984     return;
985
986   if (gtk_widget_is_ancestor (gtk_grab_get_current (), priv->dock))
987     return;
988
989   display = gtk_widget_get_display (priv->dock);
990   gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
991   gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
992   gtk_grab_remove (priv->dock);
993
994   /* hide again */
995   gtk_widget_hide (priv->dock);
996   priv->timeout = FALSE;
997 }
998
999 /*
1000  * +/- button callbacks.
1001  */
1002
1003 static gboolean
1004 cb_button_timeout (gpointer user_data)
1005 {
1006   GtkScaleButton *button;
1007   GtkScaleButtonPrivate *priv;
1008   GtkAdjustment *adj;
1009   gdouble val;
1010   gboolean res = TRUE;
1011
1012   button = GTK_SCALE_BUTTON (user_data);
1013   priv = button->priv;
1014
1015   if (priv->click_id == 0)
1016     return FALSE;
1017
1018   adj = priv->adjustment;
1019
1020   val = gtk_scale_button_get_value (button);
1021   val += priv->direction;
1022   if (val <= adj->lower)
1023     {
1024       res = FALSE;
1025       val = adj->lower;
1026     }
1027   else if (val > adj->upper)
1028     {
1029       res = FALSE;
1030       val = adj->upper;
1031     }
1032   gtk_scale_button_set_value (button, val);
1033
1034   if (!res)
1035     {
1036       g_source_remove (priv->click_id);
1037       priv->click_id = 0;
1038     }
1039
1040   return res;
1041 }
1042
1043 static gboolean
1044 cb_button_press (GtkWidget      *widget,
1045                  GdkEventButton *event,
1046                  gpointer        user_data)
1047 {
1048   GtkScaleButton *button;
1049   GtkScaleButtonPrivate *priv;
1050   GtkAdjustment *adj;
1051
1052   button = GTK_SCALE_BUTTON (user_data);
1053   priv = button->priv;
1054   adj = priv->adjustment;
1055
1056   if (priv->click_id != 0)
1057     g_source_remove (priv->click_id);
1058
1059   if (widget == button->plus_button)
1060     priv->direction = fabs (adj->page_increment);
1061   else
1062     priv->direction = - fabs (adj->page_increment);
1063
1064   priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1065                                             cb_button_timeout,
1066                                             button);
1067   cb_button_timeout (button);
1068
1069   return TRUE;
1070 }
1071
1072 static gboolean
1073 cb_button_release (GtkWidget      *widget,
1074                    GdkEventButton *event,
1075                    gpointer        user_data)
1076 {
1077   GtkScaleButton *button;
1078   GtkScaleButtonPrivate *priv;
1079
1080   button = GTK_SCALE_BUTTON (user_data);
1081   priv = button->priv;
1082
1083   if (priv->click_id != 0)
1084     {
1085       g_source_remove (priv->click_id);
1086       priv->click_id = 0;
1087     }
1088
1089   return TRUE;
1090 }
1091
1092 static void
1093 cb_dock_grab_notify (GtkWidget *widget,
1094                      gboolean   was_grabbed,
1095                      gpointer   user_data)
1096 {
1097   GtkScaleButton *button = (GtkScaleButton *) user_data;
1098
1099   gtk_scale_button_grab_notify (button, was_grabbed);
1100 }
1101
1102 static gboolean
1103 cb_dock_grab_broken_event (GtkWidget *widget,
1104                            gboolean   was_grabbed,
1105                            gpointer   user_data)
1106 {
1107   GtkScaleButton *button = (GtkScaleButton *) user_data;
1108
1109   gtk_scale_button_grab_notify (button, FALSE);
1110
1111   return FALSE;
1112 }
1113
1114 /*
1115  * Scale callbacks.
1116  */
1117
1118 static void
1119 gtk_scale_button_release_grab (GtkScaleButton *button,
1120                                GdkEventButton *event)
1121 {
1122   GdkEventButton *e;
1123   GdkDisplay *display;
1124   GtkScaleButtonPrivate *priv;
1125
1126   priv = button->priv;
1127
1128   /* ungrab focus */
1129   display = gtk_widget_get_display (GTK_WIDGET (button));
1130   gdk_display_keyboard_ungrab (display, event->time);
1131   gdk_display_pointer_ungrab (display, event->time);
1132   gtk_grab_remove (priv->dock);
1133
1134   /* hide again */
1135   gtk_widget_hide (priv->dock);
1136   priv->timeout = FALSE;
1137
1138   e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1139   e->window = GTK_WIDGET (button)->window;
1140   e->type = GDK_BUTTON_RELEASE;
1141   gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1142   e->window = event->window;
1143   gdk_event_free ((GdkEvent *) e);
1144 }
1145
1146 static gboolean
1147 cb_dock_button_press (GtkWidget      *widget,
1148                       GdkEventButton *event,
1149                       gpointer        user_data)
1150 {
1151   GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1152
1153   if (event->type == GDK_BUTTON_PRESS)
1154     {
1155       gtk_scale_button_release_grab (button, event);
1156       return TRUE;
1157     }
1158
1159   return FALSE;
1160 }
1161
1162 static void
1163 gtk_scale_button_popdown_from_bindings (GtkWidget *widget)
1164 {
1165   GtkScaleButton *button;
1166   GtkScaleButtonPrivate *priv;
1167   GdkDisplay *display;
1168
1169   button = GTK_SCALE_BUTTON (widget);
1170   priv = button->priv;
1171
1172   /* ungrab focus */
1173   display = gtk_widget_get_display (widget);
1174   gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1175   gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1176   gtk_grab_remove (priv->dock);
1177
1178   /* hide again */
1179   gtk_widget_hide (priv->dock);
1180   priv->timeout = FALSE;
1181 }
1182
1183 static gboolean
1184 cb_dock_key_release (GtkWidget   *widget,
1185                      GdkEventKey *event,
1186                      gpointer     user_data)
1187 {
1188   if (event->keyval == GDK_Escape)
1189     {
1190       gtk_scale_button_popdown_from_bindings (GTK_WIDGET (user_data));
1191       return TRUE;
1192     }
1193
1194   if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
1195     {
1196       /* The popup hasn't managed the event, pass onto the button */
1197       gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
1198     }
1199
1200   return TRUE;
1201 }
1202
1203 static void
1204 cb_scale_grab_notify (GtkWidget *widget,
1205                       gboolean   was_grabbed,
1206                       gpointer   user_data)
1207 {
1208   GtkScaleButton *button = (GtkScaleButton *) user_data;
1209
1210   gtk_scale_button_grab_notify (button, was_grabbed);
1211 }
1212
1213 /*
1214  * Scale stuff.
1215  */
1216
1217 #define GTK_TYPE_SCALE_BUTTON_VSCALE    (gtk_scale_button_vscale_get_type ())
1218 #define GTK_SCALE_BUTTON_VSCALE(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_VSCALE, GtkScaleButtonVScale))
1219 #define GTK_IS_SCALE_BUTTON_VSCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_VSCALE))
1220
1221 typedef struct _GtkScaleButtonVScale {
1222   GtkVScale parent;
1223   GtkScaleButton *button;
1224 } GtkScaleButtonVScale;
1225
1226 typedef struct _GtkScaleButtonVScaleClass {
1227   GtkVScaleClass parent_class;
1228 } GtkScaleButtonVScaleClass;
1229
1230 #define GTK_TYPE_SCALE_BUTTON_HSCALE    (gtk_scale_button_hscale_get_type ())
1231 #define GTK_SCALE_BUTTON_HSCALE(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_HSCALE, GtkScaleButtonHScale))
1232 #define GTK_IS_SCALE_BUTTON_HSCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_HSCALE))
1233
1234 typedef struct _GtkScaleButtonHScale {
1235   GtkVScale parent;
1236   GtkScaleButton *button;
1237 } GtkScaleButtonHScale;
1238
1239 typedef struct _GtkScaleButtonHScaleClass {
1240   GtkVScaleClass parent_class;
1241 } GtkScaleButtonHScaleClass;
1242
1243 static gboolean gtk_scale_button_scale_press   (GtkWidget      *widget,
1244                                                 GdkEventButton *event);
1245 static gboolean gtk_scale_button_scale_release (GtkWidget      *widget,
1246                                                 GdkEventButton *event);
1247
1248 G_DEFINE_TYPE (GtkScaleButtonVScale, gtk_scale_button_vscale, GTK_TYPE_VSCALE)
1249
1250 G_DEFINE_TYPE (GtkScaleButtonHScale, gtk_scale_button_hscale, GTK_TYPE_HSCALE)
1251
1252 static void
1253 gtk_scale_button_vscale_class_init (GtkScaleButtonVScaleClass *klass)
1254 {
1255   GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1256   GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1257
1258   gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1259   gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1260   gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1261 }
1262
1263 static void
1264 gtk_scale_button_hscale_class_init (GtkScaleButtonHScaleClass *klass)
1265 {
1266   GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1267   GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1268
1269   gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1270   gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1271   gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1272 }
1273
1274 static void
1275 gtk_scale_button_vscale_init (GtkScaleButtonVScale *scale)
1276 {
1277 }
1278
1279 static void
1280 gtk_scale_button_hscale_init (GtkScaleButtonHScale *scale)
1281 {
1282 }
1283
1284 static GtkWidget *
1285 gtk_scale_button_scale_new (GtkScaleButton *button,
1286                             gdouble         min,
1287                             gdouble         max,
1288                             gdouble         step)
1289 {
1290   GtkScaleButtonPrivate *priv = button->priv;
1291   GtkScaleButtonVScale *scale;
1292
1293   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1294     scale = g_object_new (GTK_TYPE_SCALE_BUTTON_VSCALE, NULL);
1295   else
1296     scale = g_object_new (GTK_TYPE_SCALE_BUTTON_HSCALE, NULL);
1297
1298   gtk_range_set_adjustment (GTK_RANGE (scale),
1299                             GTK_ADJUSTMENT (priv->adjustment));
1300
1301   scale->button = button;
1302
1303   return GTK_WIDGET (scale);
1304 }
1305
1306 static gboolean
1307 gtk_scale_button_scale_press (GtkWidget      *widget,
1308                               GdkEventButton *event)
1309 {
1310   GtkScaleButtonPrivate *priv;
1311   GtkWidgetClass *widget_class;
1312
1313   if (GTK_IS_SCALE_BUTTON_VSCALE (widget))
1314     {
1315       priv = GTK_SCALE_BUTTON_VSCALE (widget)->button->priv;
1316       widget_class = GTK_WIDGET_CLASS (gtk_scale_button_vscale_parent_class);
1317     }
1318   else
1319     {
1320       priv = GTK_SCALE_BUTTON_HSCALE (widget)->button->priv;
1321       widget_class = GTK_WIDGET_CLASS (gtk_scale_button_hscale_parent_class);
1322     }
1323
1324   /* the scale will grab input; if we have input grabbed, all goes
1325    * horribly wrong, so let's not do that. */
1326   gtk_grab_remove (priv->dock);
1327
1328   return widget_class->button_press_event (widget, event);
1329 }
1330
1331 static gboolean
1332 gtk_scale_button_scale_release (GtkWidget      *widget,
1333                                 GdkEventButton *event)
1334 {
1335   GtkScaleButton *button;
1336   GtkWidgetClass *widget_class;
1337   gboolean res;
1338
1339   if (GTK_IS_SCALE_BUTTON_VSCALE (widget))
1340     {
1341       button = GTK_SCALE_BUTTON_VSCALE (widget)->button;
1342       widget_class = GTK_WIDGET_CLASS (gtk_scale_button_vscale_parent_class);
1343     }
1344   else
1345     {
1346       button = GTK_SCALE_BUTTON_HSCALE (widget)->button;
1347       widget_class = GTK_WIDGET_CLASS (gtk_scale_button_hscale_parent_class);
1348     }
1349
1350   if (button->priv->timeout)
1351     {
1352       /* if we did a quick click, leave the window open; else, hide it */
1353       if (event->time > button->priv->pop_time + button->priv->click_timeout)
1354         {
1355
1356           gtk_scale_button_release_grab (button, event);
1357           widget_class->button_release_event (widget, event);
1358
1359           return TRUE;
1360         }
1361
1362       button->priv->timeout = FALSE;
1363     }
1364
1365   res = widget_class->button_release_event (widget, event);
1366
1367   /* the scale will release input; right after that, we *have to* grab
1368    * it back so we can catch out-of-scale clicks and hide the popup,
1369    * so I basically want a g_signal_connect_after_always(), but I can't
1370    * find that, so we do this complex 'first-call-parent-then-do-actual-
1371    * action' thingy...
1372    */
1373   gtk_grab_add (button->priv->dock);
1374
1375   return res;
1376 }
1377
1378 static void
1379 gtk_scale_button_update_icon (GtkScaleButton *button)
1380 {
1381   GtkScaleButtonPrivate *priv;
1382   GtkRange *range;
1383   GtkAdjustment *adj;
1384   gdouble value;
1385   const gchar *name;
1386   guint num_icons;
1387
1388   priv = button->priv;
1389
1390   if (!priv->icon_list || priv->icon_list[0] == '\0')
1391     {
1392       gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1393                                 GTK_STOCK_MISSING_IMAGE,
1394                                 priv->size);
1395       return;
1396     }
1397
1398   num_icons = g_strv_length (priv->icon_list);
1399
1400   /* The 1-icon special case */
1401   if (num_icons == 1)
1402     {
1403       gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1404                                     priv->icon_list[0],
1405                                     priv->size);
1406       return;
1407     }
1408
1409   range = GTK_RANGE (priv->scale);
1410   adj = priv->adjustment;
1411   value = gtk_scale_button_get_value (button);
1412
1413   /* The 2-icons special case */
1414   if (num_icons == 2)
1415     {
1416       gdouble limit;
1417       limit = (adj->upper - adj->lower) / 2 + adj->lower;
1418       if (value < limit)
1419         name = priv->icon_list[0];
1420       else
1421         name = priv->icon_list[1];
1422
1423       gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1424                                     name,
1425                                     priv->size);
1426       return;
1427     }
1428
1429   /* With 3 or more icons */
1430   if (value == adj->lower)
1431     {
1432       name = priv->icon_list[0];
1433     }
1434   else if (value == adj->upper)
1435     {
1436       name = priv->icon_list[1];
1437     }
1438   else
1439     {
1440       gdouble step;
1441       guint i;
1442
1443       step = (adj->upper - adj->lower) / (num_icons - 2);
1444       i = (guint) ((value - adj->lower) / step) + 2;
1445       g_assert (i < num_icons);
1446       name = priv->icon_list[i];
1447     }
1448
1449   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1450                                 name,
1451                                 priv->size);
1452 }
1453
1454 static void
1455 gtk_scale_button_scale_value_changed (GtkRange *range)
1456 {
1457   GtkScaleButton *button;
1458   gdouble value;
1459
1460   if (GTK_IS_SCALE_BUTTON_VSCALE (range))
1461     button = GTK_SCALE_BUTTON_VSCALE (range)->button;
1462   else
1463     button = GTK_SCALE_BUTTON_HSCALE (range)->button;
1464
1465   value = gtk_range_get_value (range);
1466
1467   gtk_scale_button_update_icon (button);
1468
1469   g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1470   g_object_notify (G_OBJECT (button), "value");
1471 }
1472
1473 #define __GTK_SCALE_BUTTON_C__
1474 #include "gtkaliasdef.c"