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