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