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