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