]> Pileus Git - ~andy/gtk/blob - gtk/gtkscalebutton.c
gtk/: fully remove gtkalias hacks
[~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 <gdk-pixbuf/gdk-pixbuf.h>
46 #include <gdk/gdkkeysyms.h>
47
48 #include "gtkbindings.h"
49 #include "gtkframe.h"
50 #include "gtkmain.h"
51 #include "gtkmarshalers.h"
52 #include "gtkorientable.h"
53 #include "gtkprivate.h"
54 #include "gtkscale.h"
55 #include "gtkscalebutton.h"
56 #include "gtkstock.h"
57 #include "gtkvbox.h"
58 #include "gtkwindow.h"
59
60 #include "gtkintl.h"
61
62 /**
63  * SECTION:gtkscalebutton
64  * @Short_description: A button which pops up a scale
65  * @Title: GtkScaleButton
66  *
67  * #GtkScaleButton provides a button which pops up a scale widget.
68  * This kind of widget is commonly used for volume controls in multimedia
69  * applications, and GTK+ provides a #GtkVolumeButton subclass that
70  * is tailored for this use case.
71  */
72
73
74 #define SCALE_SIZE 100
75 #define CLICK_TIMEOUT 250
76
77 enum
78 {
79   VALUE_CHANGED,
80   POPUP,
81   POPDOWN,
82
83   LAST_SIGNAL
84 };
85
86 enum
87 {
88   PROP_0,
89
90   PROP_ORIENTATION,
91   PROP_VALUE,
92   PROP_SIZE,
93   PROP_ADJUSTMENT,
94   PROP_ICONS
95 };
96
97 #define GET_PRIVATE(obj)        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SCALE_BUTTON, GtkScaleButtonPrivate))
98
99 struct _GtkScaleButtonPrivate
100 {
101   GtkWidget *dock;
102   GtkWidget *box;
103   GtkWidget *scale;
104   GtkWidget *image;
105
106   GtkIconSize size;
107   GtkOrientation orientation;
108
109   guint click_id;
110   gint click_timeout;
111   guint timeout : 1;
112   gdouble direction;
113   guint32 pop_time;
114
115   gchar **icon_list;
116
117   GdkDevice *grab_pointer;
118   GdkDevice *grab_keyboard;
119
120   GtkAdjustment *adjustment; /* needed because it must be settable in init() */
121 };
122
123 static GObject* gtk_scale_button_constructor    (GType                  type,
124                                                  guint                  n_construct_properties,
125                                                  GObjectConstructParam *construct_params);
126 static void     gtk_scale_button_dispose        (GObject             *object);
127 static void     gtk_scale_button_finalize       (GObject             *object);
128 static void     gtk_scale_button_set_property   (GObject             *object,
129                                                  guint                prop_id,
130                                                  const GValue        *value,
131                                                  GParamSpec          *pspec);
132 static void     gtk_scale_button_get_property   (GObject             *object,
133                                                  guint                prop_id,
134                                                  GValue              *value,
135                                                  GParamSpec          *pspec);
136 static void gtk_scale_button_set_orientation_private (GtkScaleButton *button,
137                                                       GtkOrientation  orientation);
138 static gboolean gtk_scale_button_scroll         (GtkWidget           *widget,
139                                                  GdkEventScroll      *event);
140 static void gtk_scale_button_screen_changed     (GtkWidget           *widget,
141                                                  GdkScreen           *previous_screen);
142 static gboolean gtk_scale_button_press          (GtkWidget           *widget,
143                                                  GdkEventButton      *event);
144 static gboolean gtk_scale_button_key_release    (GtkWidget           *widget,
145                                                  GdkEventKey         *event);
146 static void     gtk_scale_button_popup          (GtkWidget           *widget);
147 static void     gtk_scale_button_popdown        (GtkWidget           *widget);
148 static gboolean cb_dock_button_press            (GtkWidget           *widget,
149                                                  GdkEventButton      *event,
150                                                  gpointer             user_data);
151 static gboolean cb_dock_key_release             (GtkWidget           *widget,
152                                                  GdkEventKey         *event,
153                                                  gpointer             user_data);
154 static gboolean cb_button_press                 (GtkWidget           *widget,
155                                                  GdkEventButton      *event,
156                                                  gpointer             user_data);
157 static gboolean cb_button_release               (GtkWidget           *widget,
158                                                  GdkEventButton      *event,
159                                                  gpointer             user_data);
160 static void cb_dock_grab_notify                 (GtkWidget           *widget,
161                                                  gboolean             was_grabbed,
162                                                  gpointer             user_data);
163 static gboolean cb_dock_grab_broken_event       (GtkWidget           *widget,
164                                                  gboolean             was_grabbed,
165                                                  gpointer             user_data);
166 static void cb_scale_grab_notify                (GtkWidget           *widget,
167                                                  gboolean             was_grabbed,
168                                                  gpointer             user_data);
169 static void gtk_scale_button_update_icon        (GtkScaleButton      *button);
170 static void gtk_scale_button_scale_value_changed(GtkRange            *range);
171
172 /* see below for scale definitions */
173 static GtkWidget *gtk_scale_button_scale_new    (GtkScaleButton      *button);
174
175 G_DEFINE_TYPE_WITH_CODE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON,
176                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
177                                                 NULL))
178
179 static guint signals[LAST_SIGNAL] = { 0, };
180
181 static void
182 gtk_scale_button_class_init (GtkScaleButtonClass *klass)
183 {
184   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
185   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
186   GtkBindingSet *binding_set;
187
188   g_type_class_add_private (klass, sizeof (GtkScaleButtonPrivate));
189
190   gobject_class->constructor = gtk_scale_button_constructor;
191   gobject_class->finalize = gtk_scale_button_finalize;
192   gobject_class->dispose = gtk_scale_button_dispose;
193   gobject_class->set_property = gtk_scale_button_set_property;
194   gobject_class->get_property = gtk_scale_button_get_property;
195
196   widget_class->button_press_event = gtk_scale_button_press;
197   widget_class->key_release_event = gtk_scale_button_key_release;
198   widget_class->scroll_event = gtk_scale_button_scroll;
199   widget_class->screen_changed = gtk_scale_button_screen_changed;
200
201   /**
202    * GtkScaleButton:orientation:
203    *
204    * The orientation of the #GtkScaleButton's popup window.
205    *
206    * Note that since GTK+ 2.16, #GtkScaleButton implements the
207    * #GtkOrientable interface which has its own @orientation
208    * property. However we redefine the property here in order to
209    * override its default horizontal orientation.
210    *
211    * Since: 2.14
212    **/
213   g_object_class_override_property (gobject_class,
214                                     PROP_ORIENTATION,
215                                     "orientation");
216
217   g_object_class_install_property (gobject_class,
218                                    PROP_VALUE,
219                                    g_param_spec_double ("value",
220                                                         P_("Value"),
221                                                         P_("The value of the scale"),
222                                                         -G_MAXDOUBLE,
223                                                         G_MAXDOUBLE,
224                                                         0,
225                                                         GTK_PARAM_READWRITE));
226
227   g_object_class_install_property (gobject_class,
228                                    PROP_SIZE,
229                                    g_param_spec_enum ("size",
230                                                       P_("Icon size"),
231                                                       P_("The icon size"),
232                                                       GTK_TYPE_ICON_SIZE,
233                                                       GTK_ICON_SIZE_SMALL_TOOLBAR,
234                                                       GTK_PARAM_READWRITE));
235
236   g_object_class_install_property (gobject_class,
237                                    PROP_ADJUSTMENT,
238                                    g_param_spec_object ("adjustment",
239                                                         P_("Adjustment"),
240                                                         P_("The GtkAdjustment that contains the current value of this scale button object"),
241                                                         GTK_TYPE_ADJUSTMENT,
242                                                         GTK_PARAM_READWRITE));
243
244   /**
245    * GtkScaleButton:icons:
246    *
247    * The names of the icons to be used by the scale button.
248    * The first item in the array will be used in the button
249    * when the current value is the lowest value, the second
250    * item for the highest value. All the subsequent icons will
251    * be used for all the other values, spread evenly over the
252    * range of values.
253    *
254    * If there's only one icon name in the @icons array, it will
255    * be used for all the values. If only two icon names are in
256    * the @icons array, the first one will be used for the bottom
257    * 50% of the scale, and the second one for the top 50%.
258    *
259    * It is recommended to use at least 3 icons so that the
260    * #GtkScaleButton reflects the current value of the scale
261    * better for the users.
262    *
263    * Since: 2.12
264    */
265   g_object_class_install_property (gobject_class,
266                                    PROP_ICONS,
267                                    g_param_spec_boxed ("icons",
268                                                        P_("Icons"),
269                                                        P_("List of icon names"),
270                                                        G_TYPE_STRV,
271                                                        GTK_PARAM_READWRITE));
272
273   /**
274    * GtkScaleButton::value-changed:
275    * @button: the object which received the signal
276    * @value: the new value
277    *
278    * The ::value-changed signal is emitted when the value field has
279    * changed.
280    *
281    * Since: 2.12
282    */
283   signals[VALUE_CHANGED] =
284     g_signal_new (I_("value-changed"),
285                   G_TYPE_FROM_CLASS (klass),
286                   G_SIGNAL_RUN_LAST,
287                   G_STRUCT_OFFSET (GtkScaleButtonClass, value_changed),
288                   NULL, NULL,
289                   _gtk_marshal_VOID__DOUBLE,
290                   G_TYPE_NONE, 1, G_TYPE_DOUBLE);
291
292   /**
293    * GtkScaleButton::popup:
294    * @button: the object which received the signal
295    *
296    * The ::popup signal is a
297    * <link linkend="keybinding-signals">keybinding signal</link>
298    * which gets emitted to popup the scale widget.
299    *
300    * The default bindings for this signal are Space, Enter and Return.
301    *
302    * Since: 2.12
303    */
304   signals[POPUP] =
305     g_signal_new_class_handler (I_("popup"),
306                                 G_OBJECT_CLASS_TYPE (klass),
307                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
308                                 G_CALLBACK (gtk_scale_button_popup),
309                                 NULL, NULL,
310                                 g_cclosure_marshal_VOID__VOID,
311                                 G_TYPE_NONE, 0);
312
313   /**
314    * GtkScaleButton::popdown:
315    * @button: the object which received the signal
316    *
317    * The ::popdown signal is a
318    * <link linkend="keybinding-signals">keybinding signal</link>
319    * which gets emitted to popdown the scale widget.
320    *
321    * The default binding for this signal is Escape.
322    *
323    * Since: 2.12
324    */
325   signals[POPDOWN] =
326     g_signal_new_class_handler (I_("popdown"),
327                                 G_OBJECT_CLASS_TYPE (klass),
328                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
329                                 G_CALLBACK (gtk_scale_button_popdown),
330                                 NULL, NULL,
331                                 g_cclosure_marshal_VOID__VOID,
332                                 G_TYPE_NONE, 0);
333
334   /* Key bindings */
335   binding_set = gtk_binding_set_by_class (widget_class);
336
337   gtk_binding_entry_add_signal (binding_set, GDK_space, 0,
338                                 "popup", 0);
339   gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, 0,
340                                 "popup", 0);
341   gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
342                                 "popup", 0);
343   gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
344                                 "popup", 0);
345   gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
346                                 "popup", 0);
347   gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
348                                 "popdown", 0);
349 }
350
351 static void
352 gtk_scale_button_init (GtkScaleButton *button)
353 {
354   GtkScaleButtonPrivate *priv;
355   GtkWidget *frame;
356
357   button->priv = priv = GET_PRIVATE (button);
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_vbox_new (FALSE, 0);
392   gtk_container_add (GTK_CONTAINER (frame), priv->box);
393
394   /* + */
395   button->plus_button = gtk_button_new_with_label ("+");
396   gtk_button_set_relief (GTK_BUTTON (button->plus_button), GTK_RELIEF_NONE);
397   g_signal_connect (button->plus_button, "button-press-event",
398                     G_CALLBACK (cb_button_press), button);
399   g_signal_connect (button->plus_button, "button-release-event",
400                     G_CALLBACK (cb_button_release), button);
401   gtk_box_pack_start (GTK_BOX (priv->box), button->plus_button, FALSE, FALSE, 0);
402
403   /* - */
404   button->minus_button = gtk_button_new_with_label ("-");
405   gtk_button_set_relief (GTK_BUTTON (button->minus_button), GTK_RELIEF_NONE);
406   g_signal_connect (button->minus_button, "button-press-event",
407                    G_CALLBACK (cb_button_press), button);
408   g_signal_connect (button->minus_button, "button-release-event",
409                     G_CALLBACK (cb_button_release), button);
410   gtk_box_pack_end (GTK_BOX (priv->box), button->minus_button, FALSE, FALSE, 0);
411
412   priv->adjustment = GTK_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: (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): a %NULL-terminated array of icon names, or %NULL if
564  *         you want to set the list 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   GtkObject *adj;
582
583   adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
584
585   button = g_object_new (GTK_TYPE_SCALE_BUTTON,
586                          "adjustment", adj,
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: 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: 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   if (!adjustment)
706     adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
707   else
708     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
709
710   if (button->priv->adjustment != adjustment)
711     {
712       if (button->priv->adjustment)
713         g_object_unref (button->priv->adjustment);
714       button->priv->adjustment = g_object_ref_sink (adjustment);
715
716       if (button->priv->scale)
717         gtk_range_set_adjustment (GTK_RANGE (button->priv->scale), adjustment);
718
719       g_object_notify (G_OBJECT (button), "adjustment");
720     }
721 }
722
723 /**
724  * gtk_scale_button_get_plus_button:
725  * @button: a #GtkScaleButton
726  *
727  * Retrieves the plus button of the #GtkScaleButton.
728  *
729  * Returns: the plus button of the #GtkScaleButton.
730  *
731  * Since: 2.14
732  */
733 GtkWidget *
734 gtk_scale_button_get_plus_button (GtkScaleButton *button)
735 {
736   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
737
738   return button->plus_button;
739 }
740
741 /**
742  * gtk_scale_button_get_minus_button:
743  * @button: a #GtkScaleButton
744  *
745  * Retrieves the minus button of the #GtkScaleButton.
746  *
747  * Returns: the minus button of the #GtkScaleButton.
748  *
749  * Since: 2.14
750  */
751 GtkWidget *
752 gtk_scale_button_get_minus_button (GtkScaleButton *button)
753 {
754   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
755
756   return button->minus_button;
757 }
758
759 /**
760  * gtk_scale_button_get_popup:
761  * @button: a #GtkScaleButton
762  *
763  * Retrieves the popup of the #GtkScaleButton.
764  *
765  * Returns: the popup of the #GtkScaleButton
766  *
767  * Since: 2.14
768  */
769 GtkWidget *
770 gtk_scale_button_get_popup (GtkScaleButton *button)
771 {
772   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
773
774   return button->priv->dock;
775 }
776
777 static void
778 gtk_scale_button_set_orientation_private (GtkScaleButton *button,
779                                           GtkOrientation  orientation)
780 {
781   GtkScaleButtonPrivate *priv = button->priv;
782
783   if (orientation != priv->orientation)
784     {
785       priv->orientation = orientation;
786
787       gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box),
788                                       orientation);
789       gtk_container_child_set (GTK_CONTAINER (priv->box),
790                                button->plus_button,
791                                "pack-type",
792                                orientation == GTK_ORIENTATION_VERTICAL ?
793                                GTK_PACK_START : GTK_PACK_END,
794                                NULL);
795       gtk_container_child_set (GTK_CONTAINER (priv->box),
796                                button->minus_button,
797                                "pack-type",
798                                orientation == GTK_ORIENTATION_VERTICAL ?
799                                GTK_PACK_END : GTK_PACK_START,
800                                NULL);
801
802       gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->scale),
803                                       orientation);
804
805       if (orientation == GTK_ORIENTATION_VERTICAL)
806         {
807           gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
808                                        -1, SCALE_SIZE);
809           gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
810         }
811       else
812         {
813           gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
814                                        SCALE_SIZE, -1);
815           gtk_range_set_inverted (GTK_RANGE (priv->scale), FALSE);
816         }
817
818       /* FIXME: without this, the popup window appears as a square
819        * after changing the orientation
820        */
821       gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
822
823       g_object_notify (G_OBJECT (button), "orientation");
824     }
825 }
826
827 /*
828  * button callbacks.
829  */
830
831 static gboolean
832 gtk_scale_button_scroll (GtkWidget      *widget,
833                          GdkEventScroll *event)
834 {
835   GtkScaleButton *button;
836   GtkScaleButtonPrivate *priv;
837   GtkAdjustment *adj;
838   gdouble d;
839
840   button = GTK_SCALE_BUTTON (widget);
841   priv = button->priv;
842   adj = priv->adjustment;
843
844   if (event->type != GDK_SCROLL)
845     return FALSE;
846
847   d = gtk_scale_button_get_value (button);
848   if (event->direction == GDK_SCROLL_UP)
849     {
850       d += adj->step_increment;
851       if (d > adj->upper)
852         d = adj->upper;
853     }
854   else
855     {
856       d -= adj->step_increment;
857       if (d < adj->lower)
858         d = adj->lower;
859     }
860   gtk_scale_button_set_value (button, d);
861
862   return TRUE;
863 }
864
865 static void
866 gtk_scale_button_screen_changed (GtkWidget *widget,
867                                  GdkScreen *previous_screen)
868 {
869   GtkScaleButton *button = (GtkScaleButton *) widget;
870   GtkScaleButtonPrivate *priv;
871   GdkScreen *screen;
872   GValue value = { 0, };
873
874   if (gtk_widget_has_screen (widget) == FALSE)
875     return;
876
877   priv = button->priv;
878
879   screen = gtk_widget_get_screen (widget);
880   g_value_init (&value, G_TYPE_INT);
881   if (gdk_screen_get_setting (screen,
882                               "gtk-double-click-time",
883                               &value) == FALSE)
884     {
885       priv->click_timeout = CLICK_TIMEOUT;
886       return;
887     }
888
889   priv->click_timeout = g_value_get_int (&value);
890 }
891
892 static gboolean
893 gtk_scale_popup (GtkWidget *widget,
894                  GdkEvent  *event,
895                  guint32    time)
896 {
897   GtkScaleButton *button;
898   GtkScaleButtonPrivate *priv;
899   GtkAdjustment *adj;
900   gint x, y, m, dx, dy, sx, sy, startoff;
901   gdouble v;
902   GdkDisplay *display;
903   GdkScreen *screen;
904   gboolean is_moved;
905   GdkDevice *device, *keyboard, *pointer;
906
907   is_moved = FALSE;
908   button = GTK_SCALE_BUTTON (widget);
909   priv = button->priv;
910   adj = priv->adjustment;
911
912   display = gtk_widget_get_display (widget);
913   screen = gtk_widget_get_screen (widget);
914
915   /* position roughly */
916   gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
917
918   gdk_window_get_origin (widget->window, &x, &y);
919   x += widget->allocation.x;
920   y += widget->allocation.y;
921
922   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
923     gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
924   else
925     gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
926
927   gtk_widget_show_all (priv->dock);
928
929   gdk_window_get_origin (priv->dock->window, &dx, &dy);
930   dx += priv->dock->allocation.x;
931   dy += priv->dock->allocation.y;
932
933   gdk_window_get_origin (priv->scale->window, &sx, &sy);
934   sx += priv->scale->allocation.x;
935   sy += priv->scale->allocation.y;
936
937   priv->timeout = TRUE;
938
939   /* position (needs widget to be shown already) */
940   v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
941
942   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
943     {
944       startoff = sy - dy;
945
946       x += (widget->allocation.width - priv->dock->allocation.width) / 2;
947       y -= startoff;
948       y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
949       m = priv->scale->allocation.height -
950           GTK_RANGE (priv->scale)->min_slider_size;
951       y -= m * (1.0 - v);
952     }
953   else
954     {
955       startoff = sx - dx;
956
957       x -= startoff;
958       y += (widget->allocation.height - priv->dock->allocation.height) / 2;
959       x -= GTK_RANGE (priv->scale)->min_slider_size / 2;
960       m = priv->scale->allocation.width -
961           GTK_RANGE (priv->scale)->min_slider_size;
962       x -= m * v;
963     }
964
965   /* Make sure the dock stays inside the monitor */
966   if (event->type == GDK_BUTTON_PRESS)
967     {
968       int monitor;
969       GdkEventButton *button_event = (GdkEventButton *) event;
970       GdkRectangle rect;
971       GtkWidget *d;
972
973       d = GTK_WIDGET (priv->dock);
974       monitor = gdk_screen_get_monitor_at_point (screen,
975                                                  button_event->x_root,
976                                                  button_event->y_root);
977       gdk_screen_get_monitor_geometry (screen, monitor, &rect);
978
979       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
980         y += button_event->y;
981       else
982         x += button_event->x;
983
984       /* Move the dock, but set is_moved so we
985        * don't forward the first click later on,
986        * as it could make the scale go to the bottom */
987       if (y < rect.y)
988         {
989           y = rect.y;
990           is_moved = TRUE;
991         }
992       else if (y + d->allocation.height > rect.height + rect.y)
993         {
994           y = rect.y + rect.height - d->allocation.height;
995           is_moved = TRUE;
996         }
997
998       if (x < rect.x)
999         {
1000           x = rect.x;
1001           is_moved = TRUE;
1002         }
1003       else if (x + d->allocation.width > rect.width + rect.x)
1004         {
1005           x = rect.x + rect.width - d->allocation.width;
1006           is_moved = TRUE;
1007         }
1008     }
1009
1010   gtk_window_move (GTK_WINDOW (priv->dock), x, y);
1011
1012   if (event->type == GDK_BUTTON_PRESS)
1013     GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
1014
1015   device = gdk_event_get_device (event);
1016
1017   if (device->source == GDK_SOURCE_KEYBOARD)
1018     {
1019       keyboard = device;
1020       pointer = gdk_device_get_associated_device (device);
1021     }
1022   else
1023     {
1024       pointer = device;
1025       keyboard = gdk_device_get_associated_device (device);
1026     }
1027
1028   /* grab focus */
1029   gtk_device_grab_add (priv->dock, pointer, TRUE);
1030
1031   if (gdk_device_grab (pointer, priv->dock->window,
1032                        GDK_OWNERSHIP_WINDOW, TRUE,
1033                        GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1034                        GDK_POINTER_MOTION_MASK, NULL, time) != GDK_GRAB_SUCCESS)
1035     {
1036       gtk_device_grab_remove (priv->dock, pointer);
1037       gtk_widget_hide (priv->dock);
1038       return FALSE;
1039     }
1040
1041   if (gdk_device_grab (keyboard, priv->dock->window,
1042                        GDK_OWNERSHIP_WINDOW, TRUE,
1043                        GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1044                        NULL, time) != GDK_GRAB_SUCCESS)
1045     {
1046       gdk_device_ungrab (pointer, time);
1047       gtk_device_grab_remove (priv->dock, pointer);
1048       gtk_widget_hide (priv->dock);
1049       return FALSE;
1050     }
1051
1052   gtk_widget_grab_focus (priv->dock);
1053   priv->grab_keyboard = keyboard;
1054   priv->grab_pointer = pointer;
1055
1056   if (event->type == GDK_BUTTON_PRESS && !is_moved)
1057     {
1058       GdkEventButton *e;
1059       GdkEventButton *button_event = (GdkEventButton *) event;
1060
1061       /* forward event to the slider */
1062       e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1063       e->window = priv->scale->window;
1064
1065       /* position: the X position isn't relevant, halfway will work just fine.
1066        * The vertical position should be *exactly* in the middle of the slider
1067        * of the scale; if we don't do that correctly, it'll move from its current
1068        * position, which means a position change on-click, which is bad.
1069        */
1070       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1071         {
1072           e->x = priv->scale->allocation.width / 2;
1073           m = priv->scale->allocation.height -
1074               GTK_RANGE (priv->scale)->min_slider_size;
1075           e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1076         }
1077       else
1078         {
1079           e->y = priv->scale->allocation.height / 2;
1080           m = priv->scale->allocation.width -
1081               GTK_RANGE (priv->scale)->min_slider_size;
1082           e->x = (v * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1083         }
1084
1085       gtk_widget_event (priv->scale, (GdkEvent *) e);
1086       e->window = button_event->window;
1087       gdk_event_free ((GdkEvent *) e);
1088     }
1089
1090   gtk_widget_grab_focus (priv->scale);
1091
1092   priv->pop_time = time;
1093
1094   return TRUE;
1095 }
1096
1097 static gboolean
1098 gtk_scale_button_press (GtkWidget      *widget,
1099                         GdkEventButton *event)
1100 {
1101   return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
1102 }
1103
1104 static void
1105 gtk_scale_button_popup (GtkWidget *widget)
1106 {
1107   GdkEvent *ev;
1108
1109   /* This is a callback for a keybinding signal,
1110    * current event should  be the key event that
1111    * triggered it.
1112    */
1113   ev = gtk_get_current_event ();
1114
1115   if (ev->type != GDK_KEY_PRESS &&
1116       ev->type != GDK_KEY_RELEASE)
1117     {
1118       gdk_event_free (ev);
1119       ev = gdk_event_new (GDK_KEY_RELEASE);
1120       ev->key.time = GDK_CURRENT_TIME;
1121     }
1122
1123   gtk_scale_popup (widget, ev, ev->key.time);
1124   gdk_event_free (ev);
1125 }
1126
1127 static gboolean
1128 gtk_scale_button_key_release (GtkWidget   *widget,
1129                               GdkEventKey *event)
1130 {
1131   return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
1132 }
1133
1134 /* This is called when the grab is broken for
1135  * either the dock, or the scale itself */
1136 static void
1137 gtk_scale_button_grab_notify (GtkScaleButton *button,
1138                               gboolean        was_grabbed)
1139 {
1140   GdkDisplay *display;
1141   GtkScaleButtonPrivate *priv;
1142   GtkWidget *toplevel, *grab_widget;
1143   GtkWindowGroup *group;
1144
1145   priv = button->priv;
1146
1147   if (!priv->grab_pointer ||
1148       !gtk_widget_device_is_shadowed (GTK_WIDGET (button), priv->grab_pointer))
1149     return;
1150
1151   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
1152
1153   if (GTK_IS_WINDOW (toplevel))
1154     group = gtk_window_get_group (GTK_WINDOW (toplevel));
1155   else
1156     group = gtk_window_get_group (NULL);
1157
1158   grab_widget = gtk_window_group_get_current_device_grab (group, priv->grab_pointer);
1159
1160   if (grab_widget &&
1161       gtk_widget_is_ancestor (grab_widget, priv->dock))
1162     return;
1163
1164   display = gtk_widget_get_display (priv->dock);
1165   gdk_device_ungrab (priv->grab_keyboard, GDK_CURRENT_TIME);
1166   gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1167   gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1168
1169   priv->grab_keyboard = NULL;
1170   priv->grab_pointer = NULL;
1171
1172   /* hide again */
1173   gtk_widget_hide (priv->dock);
1174   priv->timeout = FALSE;
1175 }
1176
1177 /*
1178  * +/- button callbacks.
1179  */
1180
1181 static gboolean
1182 cb_button_timeout (gpointer user_data)
1183 {
1184   GtkScaleButton *button;
1185   GtkScaleButtonPrivate *priv;
1186   GtkAdjustment *adj;
1187   gdouble val;
1188   gboolean res = TRUE;
1189
1190   button = GTK_SCALE_BUTTON (user_data);
1191   priv = button->priv;
1192
1193   if (priv->click_id == 0)
1194     return FALSE;
1195
1196   adj = priv->adjustment;
1197
1198   val = gtk_scale_button_get_value (button);
1199   val += priv->direction;
1200   if (val <= adj->lower)
1201     {
1202       res = FALSE;
1203       val = adj->lower;
1204     }
1205   else if (val > adj->upper)
1206     {
1207       res = FALSE;
1208       val = adj->upper;
1209     }
1210   gtk_scale_button_set_value (button, val);
1211
1212   if (!res)
1213     {
1214       g_source_remove (priv->click_id);
1215       priv->click_id = 0;
1216     }
1217
1218   return res;
1219 }
1220
1221 static gboolean
1222 cb_button_press (GtkWidget      *widget,
1223                  GdkEventButton *event,
1224                  gpointer        user_data)
1225 {
1226   GtkScaleButton *button;
1227   GtkScaleButtonPrivate *priv;
1228   GtkAdjustment *adj;
1229
1230   button = GTK_SCALE_BUTTON (user_data);
1231   priv = button->priv;
1232   adj = priv->adjustment;
1233
1234   if (priv->click_id != 0)
1235     g_source_remove (priv->click_id);
1236
1237   if (widget == button->plus_button)
1238     priv->direction = fabs (adj->page_increment);
1239   else
1240     priv->direction = - fabs (adj->page_increment);
1241
1242   priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1243                                             cb_button_timeout,
1244                                             button);
1245   cb_button_timeout (button);
1246
1247   return TRUE;
1248 }
1249
1250 static gboolean
1251 cb_button_release (GtkWidget      *widget,
1252                    GdkEventButton *event,
1253                    gpointer        user_data)
1254 {
1255   GtkScaleButton *button;
1256   GtkScaleButtonPrivate *priv;
1257
1258   button = GTK_SCALE_BUTTON (user_data);
1259   priv = button->priv;
1260
1261   if (priv->click_id != 0)
1262     {
1263       g_source_remove (priv->click_id);
1264       priv->click_id = 0;
1265     }
1266
1267   return TRUE;
1268 }
1269
1270 static void
1271 cb_dock_grab_notify (GtkWidget *widget,
1272                      gboolean   was_grabbed,
1273                      gpointer   user_data)
1274 {
1275   GtkScaleButton *button = (GtkScaleButton *) user_data;
1276
1277   gtk_scale_button_grab_notify (button, was_grabbed);
1278 }
1279
1280 static gboolean
1281 cb_dock_grab_broken_event (GtkWidget *widget,
1282                            gboolean   was_grabbed,
1283                            gpointer   user_data)
1284 {
1285   GtkScaleButton *button = (GtkScaleButton *) user_data;
1286
1287   gtk_scale_button_grab_notify (button, FALSE);
1288
1289   return FALSE;
1290 }
1291
1292 /*
1293  * Scale callbacks.
1294  */
1295
1296 static void
1297 gtk_scale_button_release_grab (GtkScaleButton *button,
1298                                GdkEventButton *event)
1299 {
1300   GdkEventButton *e;
1301   GdkDisplay *display;
1302   GtkScaleButtonPrivate *priv;
1303
1304   priv = button->priv;
1305
1306   /* ungrab focus */
1307   display = gtk_widget_get_display (GTK_WIDGET (button));
1308   gdk_device_ungrab (priv->grab_keyboard, event->time);
1309   gdk_device_ungrab (priv->grab_pointer, event->time);
1310   gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1311
1312   priv->grab_keyboard = NULL;
1313   priv->grab_pointer = NULL;
1314
1315   /* hide again */
1316   gtk_widget_hide (priv->dock);
1317   priv->timeout = FALSE;
1318
1319   e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1320   e->window = GTK_WIDGET (button)->window;
1321   e->type = GDK_BUTTON_RELEASE;
1322   gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1323   e->window = event->window;
1324   gdk_event_free ((GdkEvent *) e);
1325 }
1326
1327 static gboolean
1328 cb_dock_button_press (GtkWidget      *widget,
1329                       GdkEventButton *event,
1330                       gpointer        user_data)
1331 {
1332   GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1333
1334   if (event->type == GDK_BUTTON_PRESS)
1335     {
1336       gtk_scale_button_release_grab (button, event);
1337       return TRUE;
1338     }
1339
1340   return FALSE;
1341 }
1342
1343 static void
1344 gtk_scale_button_popdown (GtkWidget *widget)
1345 {
1346   GtkScaleButton *button;
1347   GtkScaleButtonPrivate *priv;
1348   GdkDisplay *display;
1349
1350   button = GTK_SCALE_BUTTON (widget);
1351   priv = button->priv;
1352
1353   /* ungrab focus */
1354   display = gtk_widget_get_display (widget);
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_Escape)
1373     {
1374       gtk_scale_button_popdown (GTK_WIDGET (user_data));
1375       return TRUE;
1376     }
1377
1378   if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
1379     {
1380       /* The popup hasn't managed the event, pass onto the button */
1381       gtk_bindings_activate_event (GTK_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   GtkRange *range;
1524   GtkAdjustment *adj;
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   range = GTK_RANGE (priv->scale);
1551   adj = priv->adjustment;
1552   value = gtk_scale_button_get_value (button);
1553
1554   /* The 2-icons special case */
1555   if (num_icons == 2)
1556     {
1557       gdouble limit;
1558       limit = (adj->upper - adj->lower) / 2 + adj->lower;
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 == adj->lower)
1572     {
1573       name = priv->icon_list[0];
1574     }
1575   else if (value == adj->upper)
1576     {
1577       name = priv->icon_list[1];
1578     }
1579   else
1580     {
1581       gdouble step;
1582       guint i;
1583
1584       step = (adj->upper - adj->lower) / (num_icons - 2);
1585       i = (guint) ((value - adj->lower) / 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 }