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