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