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