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