]> Pileus Git - ~andy/gtk/blob - gtk/gtkscalebutton.c
add static function gtk_scale_button_set_orientation_private() and use it
[~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: 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: 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_orientation:
711  * @button: a #GtkScaleButton
712  *
713  * Gets the orientation of the #GtkScaleButton's popup window.
714  *
715  * Returns: the #GtkScaleButton's orientation.
716  *
717  * Since: 2.14
718  *
719  * Deprecated: 2.16: Use gtk_orientable_get_orientation() instead.
720  **/
721 GtkOrientation
722 gtk_scale_button_get_orientation (GtkScaleButton *button)
723 {
724   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), GTK_ORIENTATION_VERTICAL);
725
726   return button->priv->orientation;
727 }
728
729 /**
730  * gtk_scale_button_set_orientation:
731  * @button: a #GtkScaleButton
732  * @orientation: the new orientation
733  *
734  * Sets the orientation of the #GtkScaleButton's popup window.
735  *
736  * Since: 2.14
737  *
738  * Deprecated: 2.16: Use gtk_orientable_set_orientation() instead.
739  **/
740 void
741 gtk_scale_button_set_orientation (GtkScaleButton *button,
742                                   GtkOrientation  orientation)
743 {
744   g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
745
746   gtk_scale_button_set_orientation_private (button, orientation);
747 }
748
749 /**
750  * gtk_scale_button_get_plus_button:
751  * @button: a #GtkScaleButton
752  *
753  * Retrieves the plus button of the #GtkScaleButton.
754  *
755  * Returns: the plus button of the #GtkScaleButton.
756  *
757  * Since: 2.14
758  */
759 GtkWidget *
760 gtk_scale_button_get_plus_button (GtkScaleButton *button)
761 {
762   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
763
764   return button->plus_button;
765 }
766
767 /**
768  * gtk_scale_button_get_minus_button:
769  * @button: a #GtkScaleButton
770  *
771  * Retrieves the minus button of the #GtkScaleButton.
772  *
773  * Returns: the minus button of the #GtkScaleButton.
774  *
775  * Since: 2.14
776  */
777 GtkWidget *
778 gtk_scale_button_get_minus_button (GtkScaleButton *button)
779 {
780   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
781
782   return button->minus_button;
783 }
784
785 /**
786  * gtk_scale_button_get_popup:
787  * @button: a #GtkScaleButton
788  *
789  * Retrieves the popup of the #GtkScaleButton.
790  *
791  * Returns: the popup of the #GtkScaleButton
792  *
793  * Since: 2.14
794  */
795 GtkWidget *
796 gtk_scale_button_get_popup (GtkScaleButton *button)
797 {
798   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
799
800   return button->priv->dock;
801 }
802
803 static void
804 gtk_scale_button_set_orientation_private (GtkScaleButton *button,
805                                           GtkOrientation  orientation)
806 {
807   GtkScaleButtonPrivate *priv = button->priv;
808
809   if (orientation != priv->orientation)
810     {
811       priv->orientation = orientation;
812
813       gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box),
814                                       orientation);
815       gtk_container_child_set (GTK_CONTAINER (priv->box),
816                                button->plus_button,
817                                "pack-type",
818                                orientation == GTK_ORIENTATION_VERTICAL ?
819                                GTK_PACK_START : GTK_PACK_END,
820                                NULL);
821       gtk_container_child_set (GTK_CONTAINER (priv->box),
822                                button->minus_button,
823                                "pack-type",
824                                orientation == GTK_ORIENTATION_VERTICAL ?
825                                GTK_PACK_END : GTK_PACK_START,
826                                NULL);
827
828       gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->scale),
829                                       orientation);
830
831       if (orientation == GTK_ORIENTATION_VERTICAL)
832         {
833           gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
834                                        -1, SCALE_SIZE);
835           gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
836         }
837       else
838         {
839           gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
840                                        SCALE_SIZE, -1);
841           gtk_range_set_inverted (GTK_RANGE (priv->scale), FALSE);
842         }
843
844       /* FIXME: without this, the popup window appears as a square
845        * after changing the orientation
846        */
847       gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
848
849       g_object_notify (G_OBJECT (button), "orientation");
850     }
851 }
852
853 /*
854  * button callbacks.
855  */
856
857 static gboolean
858 gtk_scale_button_scroll (GtkWidget      *widget,
859                          GdkEventScroll *event)
860 {
861   GtkScaleButton *button;
862   GtkScaleButtonPrivate *priv;
863   GtkAdjustment *adj;
864   gdouble d;
865
866   button = GTK_SCALE_BUTTON (widget);
867   priv = button->priv;
868   adj = priv->adjustment;
869
870   if (event->type != GDK_SCROLL)
871     return FALSE;
872
873   d = gtk_scale_button_get_value (button);
874   if (event->direction == GDK_SCROLL_UP)
875     {
876       d += adj->step_increment;
877       if (d > adj->upper)
878         d = adj->upper;
879     }
880   else
881     {
882       d -= adj->step_increment;
883       if (d < adj->lower)
884         d = adj->lower;
885     }
886   gtk_scale_button_set_value (button, d);
887
888   return TRUE;
889 }
890
891 static void
892 gtk_scale_button_screen_changed (GtkWidget *widget,
893                                  GdkScreen *previous_screen)
894 {
895   GtkScaleButton *button = (GtkScaleButton *) widget;
896   GtkScaleButtonPrivate *priv;
897   GdkScreen *screen;
898   GValue value = { 0, };
899
900   if (gtk_widget_has_screen (widget) == FALSE)
901     return;
902
903   priv = button->priv;
904
905   screen = gtk_widget_get_screen (widget);
906   g_value_init (&value, G_TYPE_INT);
907   if (gdk_screen_get_setting (screen,
908                               "gtk-double-click-time",
909                               &value) == FALSE)
910     {
911       priv->click_timeout = CLICK_TIMEOUT;
912       return;
913     }
914
915   priv->click_timeout = g_value_get_int (&value);
916 }
917
918 static gboolean
919 gtk_scale_popup (GtkWidget *widget,
920                  GdkEvent  *event,
921                  guint32    time)
922 {
923   GtkScaleButton *button;
924   GtkScaleButtonPrivate *priv;
925   GtkAdjustment *adj;
926   gint x, y, m, dx, dy, sx, sy, startoff;
927   gdouble v;
928   GdkDisplay *display;
929   GdkScreen *screen;
930
931   button = GTK_SCALE_BUTTON (widget);
932   priv = button->priv;
933   adj = priv->adjustment;
934
935   display = gtk_widget_get_display (widget);
936   screen = gtk_widget_get_screen (widget);
937
938   /* position roughly */
939   gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
940
941   gdk_window_get_origin (widget->window, &x, &y);
942   x += widget->allocation.x;
943   y += widget->allocation.y;
944
945   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
946     gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
947   else
948     gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
949
950   gtk_widget_show_all (priv->dock);
951
952   gdk_window_get_origin (priv->dock->window, &dx, &dy);
953   dx += priv->dock->allocation.x;
954   dy += priv->dock->allocation.y;
955
956   gdk_window_get_origin (priv->scale->window, &sx, &sy);
957   sx += priv->scale->allocation.x;
958   sy += priv->scale->allocation.y;
959
960   priv->timeout = TRUE;
961
962   /* position (needs widget to be shown already) */
963   v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
964
965   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
966     {
967       startoff = sy - dy;
968
969       x += (widget->allocation.width - priv->dock->allocation.width) / 2;
970       y -= startoff;
971       y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
972       m = priv->scale->allocation.height -
973           GTK_RANGE (priv->scale)->min_slider_size;
974       y -= m * (1.0 - v);
975     }
976   else
977     {
978       startoff = sx - dx;
979
980       x -= startoff;
981       y += (widget->allocation.height - priv->dock->allocation.height) / 2;
982       x -= GTK_RANGE (priv->scale)->min_slider_size / 2;
983       m = priv->scale->allocation.width -
984           GTK_RANGE (priv->scale)->min_slider_size;
985       x -= m * v;
986     }
987
988   /* Make sure the dock stays inside the monitor */
989   if (event->type == GDK_BUTTON_PRESS)
990     {
991       int monitor;
992       GdkEventButton *button_event = (GdkEventButton *) event;
993       GdkRectangle rect;
994       GtkWidget *d;
995
996       d = GTK_WIDGET (priv->dock);
997       monitor = gdk_screen_get_monitor_at_point (screen,
998                                                  button_event->x_root,
999                                                  button_event->y_root);
1000       gdk_screen_get_monitor_geometry (screen, monitor, &rect);
1001
1002       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1003         y += button_event->y;
1004       else
1005         x += button_event->x;
1006
1007       if (y < rect.y)
1008         y = rect.y;
1009       else if (y + d->allocation.height > rect.height + rect.y)
1010         y = rect.y + rect.height - d->allocation.height;
1011
1012       if (x < rect.x)
1013         x = rect.x;
1014       else if (x + d->allocation.width > rect.width + rect.x)
1015         x = rect.x + rect.width - d->allocation.width;
1016     }
1017
1018   gtk_window_move (GTK_WINDOW (priv->dock), x, y);
1019
1020   if (event->type == GDK_BUTTON_PRESS)
1021     GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
1022
1023   /* grab focus */
1024   gtk_grab_add (priv->dock);
1025
1026   if (gdk_pointer_grab (priv->dock->window, TRUE,
1027                         GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1028                         GDK_POINTER_MOTION_MASK, NULL, NULL, time)
1029       != GDK_GRAB_SUCCESS)
1030     {
1031       gtk_grab_remove (priv->dock);
1032       gtk_widget_hide (priv->dock);
1033       return FALSE;
1034     }
1035
1036   if (gdk_keyboard_grab (priv->dock->window, TRUE, time) != GDK_GRAB_SUCCESS)
1037     {
1038       gdk_display_pointer_ungrab (display, time);
1039       gtk_grab_remove (priv->dock);
1040       gtk_widget_hide (priv->dock);
1041       return FALSE;
1042     }
1043
1044   gtk_widget_grab_focus (priv->dock);
1045
1046   if (event->type == GDK_BUTTON_PRESS)
1047     {
1048       GdkEventButton *e;
1049       GdkEventButton *button_event = (GdkEventButton *) event;
1050
1051       /* forward event to the slider */
1052       e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1053       e->window = priv->scale->window;
1054
1055       /* position: the X position isn't relevant, halfway will work just fine.
1056        * The vertical position should be *exactly* in the middle of the slider
1057        * of the scale; if we don't do that correctly, it'll move from its current
1058        * position, which means a position change on-click, which is bad.
1059        */
1060       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1061         {
1062           e->x = priv->scale->allocation.width / 2;
1063           m = priv->scale->allocation.height -
1064               GTK_RANGE (priv->scale)->min_slider_size;
1065           e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1066         }
1067       else
1068         {
1069           e->y = priv->scale->allocation.height / 2;
1070           m = priv->scale->allocation.width -
1071               GTK_RANGE (priv->scale)->min_slider_size;
1072           e->x = (v * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1073         }
1074
1075       gtk_widget_event (priv->scale, (GdkEvent *) e);
1076       e->window = button_event->window;
1077       gdk_event_free ((GdkEvent *) e);
1078     }
1079
1080   gtk_widget_grab_focus (priv->scale);
1081
1082   priv->pop_time = time;
1083
1084   return TRUE;
1085 }
1086
1087 static gboolean
1088 gtk_scale_button_press (GtkWidget      *widget,
1089                         GdkEventButton *event)
1090 {
1091   return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
1092 }
1093
1094 static void
1095 gtk_scale_button_popup (GtkWidget *widget)
1096 {
1097   GdkEvent *ev;
1098
1099   ev = gdk_event_new (GDK_KEY_RELEASE);
1100   gtk_scale_popup (widget, ev, GDK_CURRENT_TIME);
1101   gdk_event_free (ev);
1102 }
1103
1104 static gboolean
1105 gtk_scale_button_key_release (GtkWidget   *widget,
1106                               GdkEventKey *event)
1107 {
1108   return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
1109 }
1110
1111 /* This is called when the grab is broken for
1112  * either the dock, or the scale itself */
1113 static void
1114 gtk_scale_button_grab_notify (GtkScaleButton *button,
1115                               gboolean        was_grabbed)
1116 {
1117   GdkDisplay *display;
1118   GtkScaleButtonPrivate *priv;
1119
1120   if (was_grabbed != FALSE)
1121     return;
1122
1123   priv = button->priv;
1124
1125   if (!GTK_WIDGET_HAS_GRAB (priv->dock))
1126     return;
1127
1128   if (gtk_widget_is_ancestor (gtk_grab_get_current (), priv->dock))
1129     return;
1130
1131   display = gtk_widget_get_display (priv->dock);
1132   gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1133   gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1134   gtk_grab_remove (priv->dock);
1135
1136   /* hide again */
1137   gtk_widget_hide (priv->dock);
1138   priv->timeout = FALSE;
1139 }
1140
1141 /*
1142  * +/- button callbacks.
1143  */
1144
1145 static gboolean
1146 cb_button_timeout (gpointer user_data)
1147 {
1148   GtkScaleButton *button;
1149   GtkScaleButtonPrivate *priv;
1150   GtkAdjustment *adj;
1151   gdouble val;
1152   gboolean res = TRUE;
1153
1154   button = GTK_SCALE_BUTTON (user_data);
1155   priv = button->priv;
1156
1157   if (priv->click_id == 0)
1158     return FALSE;
1159
1160   adj = priv->adjustment;
1161
1162   val = gtk_scale_button_get_value (button);
1163   val += priv->direction;
1164   if (val <= adj->lower)
1165     {
1166       res = FALSE;
1167       val = adj->lower;
1168     }
1169   else if (val > adj->upper)
1170     {
1171       res = FALSE;
1172       val = adj->upper;
1173     }
1174   gtk_scale_button_set_value (button, val);
1175
1176   if (!res)
1177     {
1178       g_source_remove (priv->click_id);
1179       priv->click_id = 0;
1180     }
1181
1182   return res;
1183 }
1184
1185 static gboolean
1186 cb_button_press (GtkWidget      *widget,
1187                  GdkEventButton *event,
1188                  gpointer        user_data)
1189 {
1190   GtkScaleButton *button;
1191   GtkScaleButtonPrivate *priv;
1192   GtkAdjustment *adj;
1193
1194   button = GTK_SCALE_BUTTON (user_data);
1195   priv = button->priv;
1196   adj = priv->adjustment;
1197
1198   if (priv->click_id != 0)
1199     g_source_remove (priv->click_id);
1200
1201   if (widget == button->plus_button)
1202     priv->direction = fabs (adj->page_increment);
1203   else
1204     priv->direction = - fabs (adj->page_increment);
1205
1206   priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1207                                             cb_button_timeout,
1208                                             button);
1209   cb_button_timeout (button);
1210
1211   return TRUE;
1212 }
1213
1214 static gboolean
1215 cb_button_release (GtkWidget      *widget,
1216                    GdkEventButton *event,
1217                    gpointer        user_data)
1218 {
1219   GtkScaleButton *button;
1220   GtkScaleButtonPrivate *priv;
1221
1222   button = GTK_SCALE_BUTTON (user_data);
1223   priv = button->priv;
1224
1225   if (priv->click_id != 0)
1226     {
1227       g_source_remove (priv->click_id);
1228       priv->click_id = 0;
1229     }
1230
1231   return TRUE;
1232 }
1233
1234 static void
1235 cb_dock_grab_notify (GtkWidget *widget,
1236                      gboolean   was_grabbed,
1237                      gpointer   user_data)
1238 {
1239   GtkScaleButton *button = (GtkScaleButton *) user_data;
1240
1241   gtk_scale_button_grab_notify (button, was_grabbed);
1242 }
1243
1244 static gboolean
1245 cb_dock_grab_broken_event (GtkWidget *widget,
1246                            gboolean   was_grabbed,
1247                            gpointer   user_data)
1248 {
1249   GtkScaleButton *button = (GtkScaleButton *) user_data;
1250
1251   gtk_scale_button_grab_notify (button, FALSE);
1252
1253   return FALSE;
1254 }
1255
1256 /*
1257  * Scale callbacks.
1258  */
1259
1260 static void
1261 gtk_scale_button_release_grab (GtkScaleButton *button,
1262                                GdkEventButton *event)
1263 {
1264   GdkEventButton *e;
1265   GdkDisplay *display;
1266   GtkScaleButtonPrivate *priv;
1267
1268   priv = button->priv;
1269
1270   /* ungrab focus */
1271   display = gtk_widget_get_display (GTK_WIDGET (button));
1272   gdk_display_keyboard_ungrab (display, event->time);
1273   gdk_display_pointer_ungrab (display, event->time);
1274   gtk_grab_remove (priv->dock);
1275
1276   /* hide again */
1277   gtk_widget_hide (priv->dock);
1278   priv->timeout = FALSE;
1279
1280   e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1281   e->window = GTK_WIDGET (button)->window;
1282   e->type = GDK_BUTTON_RELEASE;
1283   gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1284   e->window = event->window;
1285   gdk_event_free ((GdkEvent *) e);
1286 }
1287
1288 static gboolean
1289 cb_dock_button_press (GtkWidget      *widget,
1290                       GdkEventButton *event,
1291                       gpointer        user_data)
1292 {
1293   GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1294
1295   if (event->type == GDK_BUTTON_PRESS)
1296     {
1297       gtk_scale_button_release_grab (button, event);
1298       return TRUE;
1299     }
1300
1301   return FALSE;
1302 }
1303
1304 static void
1305 gtk_scale_button_popdown (GtkWidget *widget)
1306 {
1307   GtkScaleButton *button;
1308   GtkScaleButtonPrivate *priv;
1309   GdkDisplay *display;
1310
1311   button = GTK_SCALE_BUTTON (widget);
1312   priv = button->priv;
1313
1314   /* ungrab focus */
1315   display = gtk_widget_get_display (widget);
1316   gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1317   gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1318   gtk_grab_remove (priv->dock);
1319
1320   /* hide again */
1321   gtk_widget_hide (priv->dock);
1322   priv->timeout = FALSE;
1323 }
1324
1325 static gboolean
1326 cb_dock_key_release (GtkWidget   *widget,
1327                      GdkEventKey *event,
1328                      gpointer     user_data)
1329 {
1330   if (event->keyval == GDK_Escape)
1331     {
1332       gtk_scale_button_popdown (GTK_WIDGET (user_data));
1333       return TRUE;
1334     }
1335
1336   if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
1337     {
1338       /* The popup hasn't managed the event, pass onto the button */
1339       gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
1340     }
1341
1342   return TRUE;
1343 }
1344
1345 static void
1346 cb_scale_grab_notify (GtkWidget *widget,
1347                       gboolean   was_grabbed,
1348                       gpointer   user_data)
1349 {
1350   GtkScaleButton *button = (GtkScaleButton *) user_data;
1351
1352   gtk_scale_button_grab_notify (button, was_grabbed);
1353 }
1354
1355 /*
1356  * Scale stuff.
1357  */
1358
1359 #define GTK_TYPE_SCALE_BUTTON_SCALE    (_gtk_scale_button_scale_get_type ())
1360 #define GTK_SCALE_BUTTON_SCALE(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, GtkScaleButtonScale))
1361 #define GTK_IS_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_SCALE))
1362
1363 typedef struct _GtkScaleButtonScale
1364 {
1365   GtkScale parent_instance;
1366   GtkScaleButton *button;
1367 } GtkScaleButtonScale;
1368
1369 typedef struct _GtkScaleButtonScaleClass
1370 {
1371   GtkScaleClass parent_class;
1372 } GtkScaleButtonScaleClass;
1373
1374 static gboolean gtk_scale_button_scale_press   (GtkWidget      *widget,
1375                                                 GdkEventButton *event);
1376 static gboolean gtk_scale_button_scale_release (GtkWidget      *widget,
1377                                                 GdkEventButton *event);
1378
1379 G_DEFINE_TYPE (GtkScaleButtonScale, _gtk_scale_button_scale, GTK_TYPE_SCALE)
1380
1381 static void
1382 _gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass)
1383 {
1384   GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1385   GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1386
1387   gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1388   gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1389   gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1390 }
1391
1392 static void
1393 _gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
1394 {
1395 }
1396
1397 static GtkWidget *
1398 gtk_scale_button_scale_new (GtkScaleButton *button)
1399 {
1400   GtkScaleButtonPrivate *priv = button->priv;
1401   GtkScaleButtonScale *scale;
1402
1403   scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE,
1404                         "orientation", priv->orientation,
1405                         "adjustment",  priv->adjustment,
1406                         "draw-value",  FALSE,
1407                         NULL);
1408
1409   scale->button = button;
1410
1411   g_signal_connect (scale, "grab-notify",
1412                     G_CALLBACK (cb_scale_grab_notify), button);
1413
1414   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1415     {
1416       gtk_widget_set_size_request (GTK_WIDGET (scale), -1, SCALE_SIZE);
1417       gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
1418     }
1419   else
1420     {
1421       gtk_widget_set_size_request (GTK_WIDGET (scale), SCALE_SIZE, -1);
1422       gtk_range_set_inverted (GTK_RANGE (scale), FALSE);
1423     }
1424
1425   return GTK_WIDGET (scale);
1426 }
1427
1428 static gboolean
1429 gtk_scale_button_scale_press (GtkWidget      *widget,
1430                               GdkEventButton *event)
1431 {
1432   GtkScaleButtonPrivate *priv = GTK_SCALE_BUTTON_SCALE (widget)->button->priv;
1433
1434   /* the scale will grab input; if we have input grabbed, all goes
1435    * horribly wrong, so let's not do that.
1436    */
1437   gtk_grab_remove (priv->dock);
1438
1439   return GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_press_event (widget, event);
1440 }
1441
1442 static gboolean
1443 gtk_scale_button_scale_release (GtkWidget      *widget,
1444                                 GdkEventButton *event)
1445 {
1446   GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (widget)->button;
1447   gboolean res;
1448
1449   if (button->priv->timeout)
1450     {
1451       /* if we did a quick click, leave the window open; else, hide it */
1452       if (event->time > button->priv->pop_time + button->priv->click_timeout)
1453         {
1454
1455           gtk_scale_button_release_grab (button, event);
1456           GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1457
1458           return TRUE;
1459         }
1460
1461       button->priv->timeout = FALSE;
1462     }
1463
1464   res = GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1465
1466   /* the scale will release input; right after that, we *have to* grab
1467    * it back so we can catch out-of-scale clicks and hide the popup,
1468    * so I basically want a g_signal_connect_after_always(), but I can't
1469    * find that, so we do this complex 'first-call-parent-then-do-actual-
1470    * action' thingy...
1471    */
1472   gtk_grab_add (button->priv->dock);
1473
1474   return res;
1475 }
1476
1477 static void
1478 gtk_scale_button_update_icon (GtkScaleButton *button)
1479 {
1480   GtkScaleButtonPrivate *priv;
1481   GtkRange *range;
1482   GtkAdjustment *adj;
1483   gdouble value;
1484   const gchar *name;
1485   guint num_icons;
1486
1487   priv = button->priv;
1488
1489   if (!priv->icon_list || priv->icon_list[0] == '\0')
1490     {
1491       gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1492                                 GTK_STOCK_MISSING_IMAGE,
1493                                 priv->size);
1494       return;
1495     }
1496
1497   num_icons = g_strv_length (priv->icon_list);
1498
1499   /* The 1-icon special case */
1500   if (num_icons == 1)
1501     {
1502       gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1503                                     priv->icon_list[0],
1504                                     priv->size);
1505       return;
1506     }
1507
1508   range = GTK_RANGE (priv->scale);
1509   adj = priv->adjustment;
1510   value = gtk_scale_button_get_value (button);
1511
1512   /* The 2-icons special case */
1513   if (num_icons == 2)
1514     {
1515       gdouble limit;
1516       limit = (adj->upper - adj->lower) / 2 + adj->lower;
1517       if (value < limit)
1518         name = priv->icon_list[0];
1519       else
1520         name = priv->icon_list[1];
1521
1522       gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1523                                     name,
1524                                     priv->size);
1525       return;
1526     }
1527
1528   /* With 3 or more icons */
1529   if (value == adj->lower)
1530     {
1531       name = priv->icon_list[0];
1532     }
1533   else if (value == adj->upper)
1534     {
1535       name = priv->icon_list[1];
1536     }
1537   else
1538     {
1539       gdouble step;
1540       guint i;
1541
1542       step = (adj->upper - adj->lower) / (num_icons - 2);
1543       i = (guint) ((value - adj->lower) / step) + 2;
1544       g_assert (i < num_icons);
1545       name = priv->icon_list[i];
1546     }
1547
1548   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1549                                 name,
1550                                 priv->size);
1551 }
1552
1553 static void
1554 gtk_scale_button_scale_value_changed (GtkRange *range)
1555 {
1556   GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (range)->button;
1557   gdouble value;
1558
1559   value = gtk_range_get_value (range);
1560
1561   gtk_scale_button_update_icon (button);
1562
1563   g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1564   g_object_notify (G_OBJECT (button), "value");
1565 }
1566
1567 #define __GTK_SCALE_BUTTON_C__
1568 #include "gtkaliasdef.c"