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