]> Pileus Git - ~andy/gtk/blob - gtk/gtkscalebutton.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
25  */
26
27 /*
28  * Modified by the GTK+ Team and others 2007.  See the AUTHORS
29  * file for a list of people on the GTK+ Team.  See the ChangeLog
30  * files for a list of changes.  These files are distributed with
31  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32  */
33
34 #include "config.h"
35
36 #include "gtkscalebutton.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 "gtkadjustment.h"
46 #include "gtkbindings.h"
47 #include "gtkframe.h"
48 #include "gtkmain.h"
49 #include "gtkmarshalers.h"
50 #include "gtkorientable.h"
51 #include "gtkprivate.h"
52 #include "gtkscale.h"
53 #include "gtkstock.h"
54 #include "gtkbox.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   gtk_widget_add_events (GTK_WIDGET (button), GDK_SCROLL_MASK);
422 }
423
424 static GObject *
425 gtk_scale_button_constructor (GType                  type,
426                               guint                  n_construct_properties,
427                               GObjectConstructParam *construct_params)
428 {
429   GObject *object;
430   GtkScaleButton *button;
431   GtkScaleButtonPrivate *priv;
432
433   object = G_OBJECT_CLASS (gtk_scale_button_parent_class)->constructor (type, n_construct_properties, construct_params);
434
435   button = GTK_SCALE_BUTTON (object);
436
437   priv = button->priv;
438
439   /* set button text and size */
440   priv->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
441   gtk_scale_button_update_icon (button);
442
443   return object;
444 }
445
446 static void
447 gtk_scale_button_set_property (GObject       *object,
448                                guint          prop_id,
449                                const GValue  *value,
450                                GParamSpec    *pspec)
451 {
452   GtkScaleButton *button = GTK_SCALE_BUTTON (object);
453
454   switch (prop_id)
455     {
456     case PROP_ORIENTATION:
457       gtk_scale_button_set_orientation_private (button, g_value_get_enum (value));
458       break;
459     case PROP_VALUE:
460       gtk_scale_button_set_value (button, g_value_get_double (value));
461       break;
462     case PROP_SIZE:
463       {
464         GtkIconSize size;
465         size = g_value_get_enum (value);
466         if (button->priv->size != size)
467           {
468             button->priv->size = size;
469             gtk_scale_button_update_icon (button);
470           }
471       }
472       break;
473     case PROP_ADJUSTMENT:
474       gtk_scale_button_set_adjustment (button, g_value_get_object (value));
475       break;
476     case PROP_ICONS:
477       gtk_scale_button_set_icons (button,
478                                   (const gchar **)g_value_get_boxed (value));
479       break;
480     default:
481       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482       break;
483     }
484 }
485
486 static void
487 gtk_scale_button_get_property (GObject     *object,
488                                guint        prop_id,
489                                GValue      *value,
490                                GParamSpec  *pspec)
491 {
492   GtkScaleButton *button = GTK_SCALE_BUTTON (object);
493   GtkScaleButtonPrivate *priv = button->priv;
494
495   switch (prop_id)
496     {
497     case PROP_ORIENTATION:
498       g_value_set_enum (value, priv->orientation);
499       break;
500     case PROP_VALUE:
501       g_value_set_double (value, gtk_scale_button_get_value (button));
502       break;
503     case PROP_SIZE:
504       g_value_set_enum (value, priv->size);
505       break;
506     case PROP_ADJUSTMENT:
507       g_value_set_object (value, gtk_scale_button_get_adjustment (button));
508       break;
509     case PROP_ICONS:
510       g_value_set_boxed (value, priv->icon_list);
511       break;
512     default:
513       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
514       break;
515     }
516 }
517
518 static void
519 gtk_scale_button_finalize (GObject *object)
520 {
521   GtkScaleButton *button = GTK_SCALE_BUTTON (object);
522   GtkScaleButtonPrivate *priv = button->priv;
523
524   if (priv->icon_list)
525     {
526       g_strfreev (priv->icon_list);
527       priv->icon_list = NULL;
528     }
529
530   if (priv->adjustment)
531     {
532       g_object_unref (priv->adjustment);
533       priv->adjustment = NULL;
534     }
535
536   G_OBJECT_CLASS (gtk_scale_button_parent_class)->finalize (object);
537 }
538
539 static void
540 gtk_scale_button_dispose (GObject *object)
541 {
542   GtkScaleButton *button = GTK_SCALE_BUTTON (object);
543   GtkScaleButtonPrivate *priv = button->priv;
544
545   if (priv->dock)
546     {
547       gtk_widget_destroy (priv->dock);
548       priv->dock = NULL;
549     }
550
551   if (priv->click_id != 0)
552     {
553       g_source_remove (priv->click_id);
554       priv->click_id = 0;
555     }
556
557   G_OBJECT_CLASS (gtk_scale_button_parent_class)->dispose (object);
558 }
559
560 /**
561  * gtk_scale_button_new:
562  * @size: (type int): a stock icon size
563  * @min: the minimum value of the scale (usually 0)
564  * @max: the maximum value of the scale (usually 100)
565  * @step: the stepping of value when a scroll-wheel event,
566  *        or up/down arrow event occurs (usually 2)
567  * @icons: (allow-none) (array zero-terminated=1): a %NULL-terminated
568  *         array of icon names, or %NULL if you want to set the list
569  *         later with gtk_scale_button_set_icons()
570  *
571  * Creates a #GtkScaleButton, with a range between @min and @max, with
572  * a stepping of @step.
573  *
574  * Return value: a new #GtkScaleButton
575  *
576  * Since: 2.12
577  */
578 GtkWidget *
579 gtk_scale_button_new (GtkIconSize   size,
580                       gdouble       min,
581                       gdouble       max,
582                       gdouble       step,
583                       const gchar **icons)
584 {
585   GtkScaleButton *button;
586   GtkAdjustment *adjustment;
587
588   adjustment = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
589
590   button = g_object_new (GTK_TYPE_SCALE_BUTTON,
591                          "adjustment", adjustment,
592                          "icons", icons,
593                          "size", size,
594                          NULL);
595
596   return GTK_WIDGET (button);
597 }
598
599 /**
600  * gtk_scale_button_get_value:
601  * @button: a #GtkScaleButton
602  *
603  * Gets the current value of the scale button.
604  *
605  * Return value: current value of the scale button
606  *
607  * Since: 2.12
608  */
609 gdouble
610 gtk_scale_button_get_value (GtkScaleButton * button)
611 {
612   GtkScaleButtonPrivate *priv;
613
614   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), 0);
615
616   priv = button->priv;
617
618   return gtk_adjustment_get_value (priv->adjustment);
619 }
620
621 /**
622  * gtk_scale_button_set_value:
623  * @button: a #GtkScaleButton
624  * @value: new value of the scale button
625  *
626  * Sets the current value of the scale; if the value is outside
627  * the minimum or maximum range values, it will be clamped to fit
628  * inside them. The scale button emits the #GtkScaleButton::value-changed
629  * signal if the value changes.
630  *
631  * Since: 2.12
632  */
633 void
634 gtk_scale_button_set_value (GtkScaleButton *button,
635                             gdouble         value)
636 {
637   GtkScaleButtonPrivate *priv;
638
639   g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
640
641   priv = button->priv;
642
643   gtk_range_set_value (GTK_RANGE (priv->scale), value);
644 }
645
646 /**
647  * gtk_scale_button_set_icons:
648  * @button: a #GtkScaleButton
649  * @icons: (array zero-terminated=1): a %NULL-terminated array of icon names
650  *
651  * Sets the icons to be used by the scale button.
652  * For details, see the #GtkScaleButton:icons property.
653  *
654  * Since: 2.12
655  */
656 void
657 gtk_scale_button_set_icons (GtkScaleButton  *button,
658                             const gchar    **icons)
659 {
660   GtkScaleButtonPrivate *priv;
661   gchar **tmp;
662
663   g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
664
665   priv = button->priv;
666
667   tmp = priv->icon_list;
668   priv->icon_list = g_strdupv ((gchar **) icons);
669   g_strfreev (tmp);
670   gtk_scale_button_update_icon (button);
671
672   g_object_notify (G_OBJECT (button), "icons");
673 }
674
675 /**
676  * gtk_scale_button_get_adjustment:
677  * @button: a #GtkScaleButton
678  *
679  * Gets the #GtkAdjustment associated with the #GtkScaleButton's scale.
680  * See gtk_range_get_adjustment() for details.
681  *
682  * Returns: (transfer none): the adjustment associated with the scale
683  *
684  * Since: 2.12
685  */
686 GtkAdjustment*
687 gtk_scale_button_get_adjustment (GtkScaleButton *button)
688 {
689   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
690
691   return button->priv->adjustment;
692 }
693
694 /**
695  * gtk_scale_button_set_adjustment:
696  * @button: a #GtkScaleButton
697  * @adjustment: a #GtkAdjustment
698  *
699  * Sets the #GtkAdjustment to be used as a model
700  * for the #GtkScaleButton's scale.
701  * See gtk_range_set_adjustment() for details.
702  *
703  * Since: 2.12
704  */
705 void
706 gtk_scale_button_set_adjustment (GtkScaleButton *button,
707                                  GtkAdjustment  *adjustment)
708 {
709   g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
710
711   if (!adjustment)
712     adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
713   else
714     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
715
716   if (button->priv->adjustment != adjustment)
717     {
718       if (button->priv->adjustment)
719         g_object_unref (button->priv->adjustment);
720       button->priv->adjustment = g_object_ref_sink (adjustment);
721
722       if (button->priv->scale)
723         gtk_range_set_adjustment (GTK_RANGE (button->priv->scale), adjustment);
724
725       g_object_notify (G_OBJECT (button), "adjustment");
726     }
727 }
728
729 /**
730  * gtk_scale_button_get_plus_button:
731  * @button: a #GtkScaleButton
732  *
733  * Retrieves the plus button of the #GtkScaleButton.
734  *
735  * Returns: (transfer none): the plus button of the #GtkScaleButton
736  *
737  * Since: 2.14
738  */
739 GtkWidget *
740 gtk_scale_button_get_plus_button (GtkScaleButton *button)
741 {
742   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
743
744   return button->priv->plus_button;
745 }
746
747 /**
748  * gtk_scale_button_get_minus_button:
749  * @button: a #GtkScaleButton
750  *
751  * Retrieves the minus button of the #GtkScaleButton.
752  *
753  * Returns: (transfer none): the minus button of the #GtkScaleButton
754  *
755  * Since: 2.14
756  */
757 GtkWidget *
758 gtk_scale_button_get_minus_button (GtkScaleButton *button)
759 {
760   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
761
762   return button->priv->minus_button;
763 }
764
765 /**
766  * gtk_scale_button_get_popup:
767  * @button: a #GtkScaleButton
768  *
769  * Retrieves the popup of the #GtkScaleButton.
770  *
771  * Returns: (transfer none): the popup of the #GtkScaleButton
772  *
773  * Since: 2.14
774  */
775 GtkWidget *
776 gtk_scale_button_get_popup (GtkScaleButton *button)
777 {
778   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
779
780   return button->priv->dock;
781 }
782
783 static void
784 gtk_scale_button_set_orientation_private (GtkScaleButton *button,
785                                           GtkOrientation  orientation)
786 {
787   GtkScaleButtonPrivate *priv = button->priv;
788
789   if (orientation != priv->orientation)
790     {
791       priv->orientation = orientation;
792
793       gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box),
794                                       orientation);
795       gtk_container_child_set (GTK_CONTAINER (priv->box),
796                                priv->plus_button,
797                                "pack-type",
798                                orientation == GTK_ORIENTATION_VERTICAL ?
799                                GTK_PACK_START : GTK_PACK_END,
800                                NULL);
801       gtk_container_child_set (GTK_CONTAINER (priv->box),
802                                priv->minus_button,
803                                "pack-type",
804                                orientation == GTK_ORIENTATION_VERTICAL ?
805                                GTK_PACK_END : GTK_PACK_START,
806                                NULL);
807
808       gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->scale),
809                                       orientation);
810
811       if (orientation == GTK_ORIENTATION_VERTICAL)
812         {
813           gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
814                                        -1, SCALE_SIZE);
815           gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
816         }
817       else
818         {
819           gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
820                                        SCALE_SIZE, -1);
821           gtk_range_set_inverted (GTK_RANGE (priv->scale), FALSE);
822         }
823
824       /* FIXME: without this, the popup window appears as a square
825        * after changing the orientation
826        */
827       gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
828
829       g_object_notify (G_OBJECT (button), "orientation");
830     }
831 }
832
833 /*
834  * button callbacks.
835  */
836
837 static gboolean
838 gtk_scale_button_scroll (GtkWidget      *widget,
839                          GdkEventScroll *event)
840 {
841   GtkScaleButton *button;
842   GtkScaleButtonPrivate *priv;
843   GtkAdjustment *adjustment;
844   gdouble d;
845
846   button = GTK_SCALE_BUTTON (widget);
847   priv = button->priv;
848   adjustment = priv->adjustment;
849
850   if (event->type != GDK_SCROLL)
851     return FALSE;
852
853   d = gtk_scale_button_get_value (button);
854   if (event->direction == GDK_SCROLL_UP)
855     {
856       d += gtk_adjustment_get_step_increment (adjustment);
857       if (d > gtk_adjustment_get_upper (adjustment))
858         d = gtk_adjustment_get_upper (adjustment);
859     }
860   else
861     {
862       d -= gtk_adjustment_get_step_increment (adjustment);
863       if (d < gtk_adjustment_get_lower (adjustment))
864         d = gtk_adjustment_get_lower (adjustment);
865     }
866   gtk_scale_button_set_value (button, d);
867
868   return TRUE;
869 }
870
871 static void
872 gtk_scale_button_screen_changed (GtkWidget *widget,
873                                  GdkScreen *previous_screen)
874 {
875   GtkScaleButton *button = (GtkScaleButton *) widget;
876   GtkScaleButtonPrivate *priv;
877   GdkScreen *screen;
878   GValue value = G_VALUE_INIT;
879
880   if (gtk_widget_has_screen (widget) == FALSE)
881     return;
882
883   priv = button->priv;
884
885   screen = gtk_widget_get_screen (widget);
886   g_value_init (&value, G_TYPE_INT);
887   if (gdk_screen_get_setting (screen,
888                               "gtk-double-click-time",
889                               &value) == FALSE)
890     {
891       priv->click_timeout = CLICK_TIMEOUT;
892       return;
893     }
894
895   priv->click_timeout = g_value_get_int (&value);
896 }
897
898 static gboolean
899 gtk_scale_popup (GtkWidget *widget,
900                  GdkEvent  *event,
901                  guint32    time)
902 {
903   GtkAllocation allocation, dock_allocation, scale_allocation;
904   GtkScaleButton *button;
905   GtkScaleButtonPrivate *priv;
906   GtkAdjustment *adjustment;
907   gint x, y, m, dx, dy, sx, sy, startoff;
908   gint min_slider_size;
909   gdouble v;
910   GdkScreen *screen;
911   gboolean is_moved;
912   GdkDevice *device, *keyboard, *pointer;
913
914   is_moved = FALSE;
915   button = GTK_SCALE_BUTTON (widget);
916   priv = button->priv;
917   adjustment = priv->adjustment;
918
919   screen = gtk_widget_get_screen (widget);
920   gtk_widget_get_allocation (widget, &allocation);
921
922   /* position roughly */
923   gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
924
925   gdk_window_get_origin (gtk_widget_get_window (widget),
926                          &x, &y);
927   x += allocation.x;
928   y += allocation.y;
929
930   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
931     gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
932   else
933     gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
934
935   gtk_widget_show_all (priv->dock);
936
937   gdk_window_get_origin (gtk_widget_get_window (priv->dock),
938                          &dx, &dy);
939   gtk_widget_get_allocation (priv->dock, &dock_allocation);
940   dx += dock_allocation.x;
941   dy += dock_allocation.y;
942
943
944   gdk_window_get_origin (gtk_widget_get_window (priv->scale),
945                          &sx, &sy);
946   gtk_widget_get_allocation (priv->scale, &scale_allocation);
947   sx += scale_allocation.x;
948   sy += scale_allocation.y;
949
950   priv->timeout = TRUE;
951
952   /* position (needs widget to be shown already) */
953   v = gtk_scale_button_get_value (button) / (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment));
954   min_slider_size = gtk_range_get_min_slider_size (GTK_RANGE (priv->scale));
955
956   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
957     {
958       startoff = sy - dy;
959
960       x += (allocation.width - dock_allocation.width) / 2;
961       y -= startoff;
962       y -= min_slider_size / 2;
963       m = scale_allocation.height - min_slider_size;
964       y -= m * (1.0 - v);
965     }
966   else
967     {
968       startoff = sx - dx;
969
970       x -= startoff;
971       y += (allocation.height - dock_allocation.height) / 2;
972       x -= min_slider_size / 2;
973       m = scale_allocation.width - min_slider_size;
974       x -= m * v;
975     }
976
977   /* Make sure the dock stays inside the monitor */
978   if (event->type == GDK_BUTTON_PRESS)
979     {
980       GtkAllocation d_allocation;
981       int monitor;
982       GdkEventButton *button_event = (GdkEventButton *) event;
983       GdkRectangle rect;
984       GtkWidget *d;
985
986       d = GTK_WIDGET (priv->dock);
987       monitor = gdk_screen_get_monitor_at_point (screen,
988                                                  button_event->x_root,
989                                                  button_event->y_root);
990       gdk_screen_get_monitor_workarea (screen, monitor, &rect);
991
992       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
993         y += button_event->y;
994       else
995         x += button_event->x;
996
997       /* Move the dock, but set is_moved so we
998        * don't forward the first click later on,
999        * as it could make the scale go to the bottom */
1000       gtk_widget_get_allocation (d, &d_allocation);
1001       if (y < rect.y)
1002         {
1003           y = rect.y;
1004           is_moved = TRUE;
1005         }
1006       else if (y + d_allocation.height > rect.height + rect.y)
1007         {
1008           y = rect.y + rect.height - d_allocation.height;
1009           is_moved = TRUE;
1010         }
1011
1012       if (x < rect.x)
1013         {
1014           x = rect.x;
1015           is_moved = TRUE;
1016         }
1017       else if (x + d_allocation.width > rect.width + rect.x)
1018         {
1019           x = rect.x + rect.width - d_allocation.width;
1020           is_moved = TRUE;
1021         }
1022     }
1023
1024   gtk_window_move (GTK_WINDOW (priv->dock), x, y);
1025
1026   if (event->type == GDK_BUTTON_PRESS)
1027     GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
1028
1029   device = gdk_event_get_device (event);
1030
1031   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1032     {
1033       keyboard = device;
1034       pointer = gdk_device_get_associated_device (device);
1035     }
1036   else
1037     {
1038       pointer = device;
1039       keyboard = gdk_device_get_associated_device (device);
1040     }
1041
1042   /* grab focus */
1043   gtk_device_grab_add (priv->dock, pointer, TRUE);
1044
1045   if (gdk_device_grab (pointer, gtk_widget_get_window (priv->dock),
1046                        GDK_OWNERSHIP_WINDOW, TRUE,
1047                        GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1048                        GDK_POINTER_MOTION_MASK, NULL, time) != GDK_GRAB_SUCCESS)
1049     {
1050       gtk_device_grab_remove (priv->dock, pointer);
1051       gtk_widget_hide (priv->dock);
1052       return FALSE;
1053     }
1054
1055   if (gdk_device_grab (keyboard, gtk_widget_get_window (priv->dock),
1056                        GDK_OWNERSHIP_WINDOW, TRUE,
1057                        GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1058                        NULL, time) != GDK_GRAB_SUCCESS)
1059     {
1060       gdk_device_ungrab (pointer, time);
1061       gtk_device_grab_remove (priv->dock, pointer);
1062       gtk_widget_hide (priv->dock);
1063       return FALSE;
1064     }
1065
1066   gtk_widget_grab_focus (priv->dock);
1067   priv->grab_keyboard = keyboard;
1068   priv->grab_pointer = pointer;
1069
1070   if (event->type == GDK_BUTTON_PRESS && !is_moved)
1071     {
1072       GdkEventButton *e;
1073       GdkEventButton *button_event = (GdkEventButton *) event;
1074
1075       /* forward event to the slider */
1076       e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1077       e->window = gtk_widget_get_window (priv->scale);
1078
1079       /* position: the X position isn't relevant, halfway will work just fine.
1080        * The vertical position should be *exactly* in the middle of the slider
1081        * of the scale; if we don't do that correctly, it'll move from its current
1082        * position, which means a position change on-click, which is bad.
1083        */
1084       gtk_widget_get_allocation (priv->scale, &scale_allocation);
1085       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1086         {
1087           e->x = scale_allocation.width / 2;
1088           m = scale_allocation.height - min_slider_size;
1089           e->y = ((1.0 - v) * m) + min_slider_size / 2;
1090         }
1091       else
1092         {
1093           e->y = scale_allocation.height / 2;
1094           m = scale_allocation.width - min_slider_size;
1095           e->x = (v * m) + min_slider_size / 2;
1096         }
1097
1098       gtk_widget_event (priv->scale, (GdkEvent *) e);
1099       e->window = button_event->window;
1100       gdk_event_free ((GdkEvent *) e);
1101     }
1102
1103   gtk_widget_grab_focus (priv->scale);
1104
1105   priv->pop_time = time;
1106
1107   return TRUE;
1108 }
1109
1110 static gboolean
1111 gtk_scale_button_press (GtkWidget      *widget,
1112                         GdkEventButton *event)
1113 {
1114   return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
1115 }
1116
1117 static void
1118 gtk_scale_button_popup (GtkWidget *widget)
1119 {
1120   GdkEvent *ev;
1121
1122   /* This is a callback for a keybinding signal,
1123    * current event should  be the key event that
1124    * triggered it.
1125    */
1126   ev = gtk_get_current_event ();
1127
1128   if (ev->type != GDK_KEY_PRESS &&
1129       ev->type != GDK_KEY_RELEASE)
1130     {
1131       gdk_event_free (ev);
1132       ev = gdk_event_new (GDK_KEY_RELEASE);
1133       ev->key.time = GDK_CURRENT_TIME;
1134     }
1135
1136   gtk_scale_popup (widget, ev, ev->key.time);
1137   gdk_event_free (ev);
1138 }
1139
1140 static gboolean
1141 gtk_scale_button_key_release (GtkWidget   *widget,
1142                               GdkEventKey *event)
1143 {
1144   return gtk_bindings_activate_event (G_OBJECT (widget), event);
1145 }
1146
1147 /* This is called when the grab is broken for
1148  * either the dock, or the scale itself */
1149 static void
1150 gtk_scale_button_grab_notify (GtkScaleButton *button,
1151                               gboolean        was_grabbed)
1152 {
1153   GtkScaleButtonPrivate *priv;
1154   GtkWidget *toplevel, *grab_widget;
1155   GtkWindowGroup *group;
1156
1157   priv = button->priv;
1158
1159   if (!priv->grab_pointer ||
1160       !gtk_widget_device_is_shadowed (GTK_WIDGET (button), priv->grab_pointer))
1161     return;
1162
1163   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
1164
1165   if (GTK_IS_WINDOW (toplevel))
1166     group = gtk_window_get_group (GTK_WINDOW (toplevel));
1167   else
1168     group = gtk_window_get_group (NULL);
1169
1170   grab_widget = gtk_window_group_get_current_device_grab (group, priv->grab_pointer);
1171
1172   if (grab_widget &&
1173       gtk_widget_is_ancestor (grab_widget, priv->dock))
1174     return;
1175
1176   gdk_device_ungrab (priv->grab_keyboard, GDK_CURRENT_TIME);
1177   gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1178   gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1179
1180   priv->grab_keyboard = NULL;
1181   priv->grab_pointer = NULL;
1182
1183   /* hide again */
1184   gtk_widget_hide (priv->dock);
1185   priv->timeout = FALSE;
1186 }
1187
1188 /*
1189  * +/- button callbacks.
1190  */
1191
1192 static gboolean
1193 cb_button_timeout (gpointer user_data)
1194 {
1195   GtkScaleButton *button;
1196   GtkScaleButtonPrivate *priv;
1197   GtkAdjustment *adjustment;
1198   gdouble val;
1199   gboolean res = TRUE;
1200
1201   button = GTK_SCALE_BUTTON (user_data);
1202   priv = button->priv;
1203
1204   if (priv->click_id == 0)
1205     return FALSE;
1206
1207   adjustment = priv->adjustment;
1208
1209   val = gtk_scale_button_get_value (button);
1210   val += priv->direction;
1211   if (val <= gtk_adjustment_get_lower (adjustment))
1212     {
1213       res = FALSE;
1214       val = gtk_adjustment_get_lower (adjustment);
1215     }
1216   else if (val > gtk_adjustment_get_upper (adjustment))
1217     {
1218       res = FALSE;
1219       val = gtk_adjustment_get_upper (adjustment);
1220     }
1221   gtk_scale_button_set_value (button, val);
1222
1223   if (!res)
1224     {
1225       g_source_remove (priv->click_id);
1226       priv->click_id = 0;
1227     }
1228
1229   return res;
1230 }
1231
1232 static gboolean
1233 cb_button_press (GtkWidget      *widget,
1234                  GdkEventButton *event,
1235                  gpointer        user_data)
1236 {
1237   GtkScaleButton *button;
1238   GtkScaleButtonPrivate *priv;
1239   GtkAdjustment *adjustment;
1240
1241   button = GTK_SCALE_BUTTON (user_data);
1242   priv = button->priv;
1243   adjustment = priv->adjustment;
1244
1245   if (priv->click_id != 0)
1246     g_source_remove (priv->click_id);
1247
1248   if (widget == priv->plus_button)
1249     priv->direction = fabs (gtk_adjustment_get_page_increment (adjustment));
1250   else
1251     priv->direction = - fabs (gtk_adjustment_get_page_increment (adjustment));
1252
1253   priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1254                                             cb_button_timeout,
1255                                             button);
1256   cb_button_timeout (button);
1257
1258   return TRUE;
1259 }
1260
1261 static gboolean
1262 cb_button_release (GtkWidget      *widget,
1263                    GdkEventButton *event,
1264                    gpointer        user_data)
1265 {
1266   GtkScaleButton *button;
1267   GtkScaleButtonPrivate *priv;
1268
1269   button = GTK_SCALE_BUTTON (user_data);
1270   priv = button->priv;
1271
1272   if (priv->click_id != 0)
1273     {
1274       g_source_remove (priv->click_id);
1275       priv->click_id = 0;
1276     }
1277
1278   return TRUE;
1279 }
1280
1281 static void
1282 cb_dock_grab_notify (GtkWidget *widget,
1283                      gboolean   was_grabbed,
1284                      gpointer   user_data)
1285 {
1286   GtkScaleButton *button = (GtkScaleButton *) user_data;
1287
1288   gtk_scale_button_grab_notify (button, was_grabbed);
1289 }
1290
1291 static gboolean
1292 cb_dock_grab_broken_event (GtkWidget *widget,
1293                            gboolean   was_grabbed,
1294                            gpointer   user_data)
1295 {
1296   GtkScaleButton *button = (GtkScaleButton *) user_data;
1297
1298   gtk_scale_button_grab_notify (button, FALSE);
1299
1300   return FALSE;
1301 }
1302
1303 /* Scale callbacks  */
1304
1305 static void
1306 gtk_scale_button_release_grab (GtkScaleButton *button,
1307                                GdkEventButton *event)
1308 {
1309   GdkEventButton *e;
1310   GtkScaleButtonPrivate *priv;
1311
1312   priv = button->priv;
1313
1314   /* ungrab focus */
1315   gdk_device_ungrab (priv->grab_keyboard, event->time);
1316   gdk_device_ungrab (priv->grab_pointer, event->time);
1317   gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1318
1319   priv->grab_keyboard = NULL;
1320   priv->grab_pointer = NULL;
1321
1322   /* hide again */
1323   gtk_widget_hide (priv->dock);
1324   priv->timeout = FALSE;
1325
1326   e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1327   e->window = gtk_widget_get_window (GTK_WIDGET (button));
1328   e->type = GDK_BUTTON_RELEASE;
1329   gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1330   e->window = event->window;
1331   gdk_event_free ((GdkEvent *) e);
1332 }
1333
1334 static gboolean
1335 cb_dock_button_press (GtkWidget      *widget,
1336                       GdkEventButton *event,
1337                       gpointer        user_data)
1338 {
1339   GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1340
1341   if (event->type == GDK_BUTTON_PRESS)
1342     {
1343       gtk_scale_button_release_grab (button, event);
1344       return TRUE;
1345     }
1346
1347   return FALSE;
1348 }
1349
1350 static void
1351 gtk_scale_button_popdown (GtkWidget *widget)
1352 {
1353   GtkScaleButton *button;
1354   GtkScaleButtonPrivate *priv;
1355
1356   button = GTK_SCALE_BUTTON (widget);
1357   priv = button->priv;
1358
1359   /* ungrab focus */
1360   gdk_device_ungrab (priv->grab_keyboard, GDK_CURRENT_TIME);
1361   gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1362   gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1363
1364   priv->grab_keyboard = NULL;
1365   priv->grab_pointer = NULL;
1366
1367   /* hide again */
1368   gtk_widget_hide (priv->dock);
1369   priv->timeout = FALSE;
1370 }
1371
1372 static gboolean
1373 cb_dock_key_release (GtkWidget   *widget,
1374                      GdkEventKey *event,
1375                      gpointer     user_data)
1376 {
1377   if (event->keyval == GDK_KEY_Escape)
1378     {
1379       gtk_scale_button_popdown (GTK_WIDGET (user_data));
1380       return TRUE;
1381     }
1382
1383   if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
1384     {
1385       /* The popup hasn't managed the event, pass onto the button */
1386       gtk_bindings_activate_event (G_OBJECT (user_data), event);
1387     }
1388
1389   return TRUE;
1390 }
1391
1392 static void
1393 cb_scale_grab_notify (GtkWidget *widget,
1394                       gboolean   was_grabbed,
1395                       gpointer   user_data)
1396 {
1397   GtkScaleButton *button = (GtkScaleButton *) user_data;
1398
1399   gtk_scale_button_grab_notify (button, was_grabbed);
1400 }
1401
1402 /*
1403  * Scale stuff.
1404  */
1405
1406 #define GTK_TYPE_SCALE_BUTTON_SCALE    (_gtk_scale_button_scale_get_type ())
1407 #define GTK_SCALE_BUTTON_SCALE(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, GtkScaleButtonScale))
1408 #define GTK_IS_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_SCALE))
1409
1410 typedef struct _GtkScaleButtonScale
1411 {
1412   GtkScale parent_instance;
1413   GtkScaleButton *button;
1414 } GtkScaleButtonScale;
1415
1416 typedef struct _GtkScaleButtonScaleClass
1417 {
1418   GtkScaleClass parent_class;
1419 } GtkScaleButtonScaleClass;
1420
1421 static gboolean gtk_scale_button_scale_press   (GtkWidget      *widget,
1422                                                 GdkEventButton *event);
1423 static gboolean gtk_scale_button_scale_release (GtkWidget      *widget,
1424                                                 GdkEventButton *event);
1425
1426 GType           _gtk_scale_button_scale_get_type (void);
1427
1428 G_DEFINE_TYPE (GtkScaleButtonScale, _gtk_scale_button_scale, GTK_TYPE_SCALE)
1429
1430 static void
1431 _gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass)
1432 {
1433   GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1434   GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1435
1436   gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1437   gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1438   gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1439 }
1440
1441 static void
1442 _gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
1443 {
1444 }
1445
1446 static GtkWidget *
1447 gtk_scale_button_scale_new (GtkScaleButton *button)
1448 {
1449   GtkScaleButtonPrivate *priv = button->priv;
1450   GtkScaleButtonScale *scale;
1451
1452   scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE,
1453                         "orientation", priv->orientation,
1454                         "adjustment",  priv->adjustment,
1455                         "draw-value",  FALSE,
1456                         NULL);
1457
1458   scale->button = button;
1459
1460   g_signal_connect (scale, "grab-notify",
1461                     G_CALLBACK (cb_scale_grab_notify), button);
1462
1463   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1464     {
1465       gtk_widget_set_size_request (GTK_WIDGET (scale), -1, SCALE_SIZE);
1466       gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
1467     }
1468   else
1469     {
1470       gtk_widget_set_size_request (GTK_WIDGET (scale), SCALE_SIZE, -1);
1471       gtk_range_set_inverted (GTK_RANGE (scale), FALSE);
1472     }
1473
1474   return GTK_WIDGET (scale);
1475 }
1476
1477 static gboolean
1478 gtk_scale_button_scale_press (GtkWidget      *widget,
1479                               GdkEventButton *event)
1480 {
1481   GtkScaleButtonPrivate *priv = GTK_SCALE_BUTTON_SCALE (widget)->button->priv;
1482
1483   /* the scale will grab input; if we have input grabbed, all goes
1484    * horribly wrong, so let's not do that.
1485    */
1486   gtk_device_grab_remove (priv->dock, event->device);
1487
1488   return GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_press_event (widget, event);
1489 }
1490
1491 static gboolean
1492 gtk_scale_button_scale_release (GtkWidget      *widget,
1493                                 GdkEventButton *event)
1494 {
1495   GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (widget)->button;
1496   gboolean res;
1497
1498   if (button->priv->timeout)
1499     {
1500       /* if we did a quick click, leave the window open; else, hide it */
1501       if (event->time > button->priv->pop_time + button->priv->click_timeout)
1502         {
1503
1504           gtk_scale_button_release_grab (button, event);
1505           GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1506
1507           return TRUE;
1508         }
1509
1510       button->priv->timeout = FALSE;
1511     }
1512
1513   res = GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1514
1515   /* the scale will release input; right after that, we *have to* grab
1516    * it back so we can catch out-of-scale clicks and hide the popup,
1517    * so I basically want a g_signal_connect_after_always(), but I can't
1518    * find that, so we do this complex 'first-call-parent-then-do-actual-
1519    * action' thingy...
1520    */
1521   gtk_device_grab_add (button->priv->dock, event->device, TRUE);
1522
1523   return res;
1524 }
1525
1526 static void
1527 gtk_scale_button_update_icon (GtkScaleButton *button)
1528 {
1529   GtkScaleButtonPrivate *priv;
1530   GtkAdjustment *adjustment;
1531   gdouble value;
1532   const gchar *name;
1533   guint num_icons;
1534
1535   priv = button->priv;
1536
1537   if (!priv->icon_list || priv->icon_list[0] == '\0')
1538     {
1539       gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1540                                 GTK_STOCK_MISSING_IMAGE,
1541                                 priv->size);
1542       return;
1543     }
1544
1545   num_icons = g_strv_length (priv->icon_list);
1546
1547   /* The 1-icon special case */
1548   if (num_icons == 1)
1549     {
1550       gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1551                                     priv->icon_list[0],
1552                                     priv->size);
1553       return;
1554     }
1555
1556   adjustment = priv->adjustment;
1557   value = gtk_scale_button_get_value (button);
1558
1559   /* The 2-icons special case */
1560   if (num_icons == 2)
1561     {
1562       gdouble limit;
1563
1564       limit = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment)) / 2 + gtk_adjustment_get_lower (adjustment);
1565       if (value < limit)
1566         name = priv->icon_list[0];
1567       else
1568         name = priv->icon_list[1];
1569
1570       gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1571                                     name,
1572                                     priv->size);
1573       return;
1574     }
1575
1576   /* With 3 or more icons */
1577   if (value == gtk_adjustment_get_lower (adjustment))
1578     {
1579       name = priv->icon_list[0];
1580     }
1581   else if (value == gtk_adjustment_get_upper (adjustment))
1582     {
1583       name = priv->icon_list[1];
1584     }
1585   else
1586     {
1587       gdouble step;
1588       guint i;
1589
1590       step = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment)) / (num_icons - 2);
1591       i = (guint) ((value - gtk_adjustment_get_lower (adjustment)) / step) + 2;
1592       g_assert (i < num_icons);
1593       name = priv->icon_list[i];
1594     }
1595
1596   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1597                                 name,
1598                                 priv->size);
1599 }
1600
1601 static void
1602 gtk_scale_button_scale_value_changed (GtkRange *range)
1603 {
1604   GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (range)->button;
1605   gdouble value;
1606
1607   value = gtk_range_get_value (range);
1608
1609   gtk_scale_button_update_icon (button);
1610
1611   g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1612   g_object_notify (G_OBJECT (button), "value");
1613 }