]> Pileus Git - ~andy/gtk/blob - gtk/gtkscalebutton.c
Bug 566532 – GtkScaleButton implementation of GtkOrientable
[~andy/gtk] / gtk / gtkscalebutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2005 Ronald S. Bultje
3  * Copyright (C) 2006, 2007 Christian Persch
4  * Copyright (C) 2006 Jan Arne Petersen
5  * Copyright (C) 2005-2007 Red Hat, Inc.
6  *
7  * Authors:
8  * - Ronald S. Bultje <rbultje@ronald.bitfreak.net>
9  * - Bastien Nocera <bnocera@redhat.com>
10  * - Jan Arne Petersen <jpetersen@jpetersen.org>
11  * - Christian Persch <chpe@svn.gnome.org>
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this library; if not, write to the
25  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26  * Boston, MA 02111-1307, USA.
27  */
28
29 /*
30  * Modified by the GTK+ Team and others 2007.  See the AUTHORS
31  * file for a list of people on the GTK+ Team.  See the ChangeLog
32  * files for a list of changes.  These files are distributed with
33  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34  */
35
36 #include "config.h"
37
38 #ifndef _WIN32
39 #define _GNU_SOURCE
40 #endif
41 #include <math.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include <gdk-pixbuf/gdk-pixbuf.h>
46 #include <gdk/gdkkeysyms.h>
47
48 #include "gtkmain.h"
49 #include "gtkintl.h"
50 #include "gtkbindings.h"
51 #include "gtkhscale.h"
52 #include "gtkvscale.h"
53 #include "gtkframe.h"
54 #include "gtkorientable.h"
55 #include "gtkhbox.h"
56 #include "gtkvbox.h"
57 #include "gtkwindow.h"
58 #include "gtkmarshalers.h"
59 #include "gtkstock.h"
60 #include "gtkprivate.h"
61 #include "gtkscalebutton.h"
62
63 #include "gtkalias.h"
64
65 #define SCALE_SIZE 100
66 #define CLICK_TIMEOUT 250
67
68 enum
69 {
70   VALUE_CHANGED,
71   POPUP,
72   POPDOWN,
73
74   LAST_SIGNAL
75 };
76
77 enum
78 {
79   PROP_0,
80
81   PROP_ORIENTATION,
82   PROP_VALUE,
83   PROP_SIZE,
84   PROP_ADJUSTMENT,
85   PROP_ICONS
86 };
87
88 #define GET_PRIVATE(obj)        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SCALE_BUTTON, GtkScaleButtonPrivate))
89
90 struct _GtkScaleButtonPrivate
91 {
92   GtkWidget *dock;
93   GtkWidget *scale;
94   GtkWidget *image;
95
96   GtkIconSize size;
97   GtkOrientation orientation;
98
99   guint click_id;
100   gint click_timeout;
101   guint timeout : 1;
102   gdouble direction;
103   guint32 pop_time;
104
105   gchar **icon_list;
106
107   GtkAdjustment *adjustment; /* needed because it must be settable in init() */
108 };
109
110 static GObject* gtk_scale_button_constructor    (GType                  type,
111                                                  guint                  n_construct_properties,
112                                                  GObjectConstructParam *construct_params);
113 static void     gtk_scale_button_dispose        (GObject             *object);
114 static void     gtk_scale_button_finalize       (GObject             *object);
115 static void     gtk_scale_button_set_property   (GObject             *object,
116                                                  guint                prop_id,
117                                                  const GValue        *value,
118                                                  GParamSpec          *pspec);
119 static void     gtk_scale_button_get_property   (GObject             *object,
120                                                  guint                prop_id,
121                                                  GValue              *value,
122                                                  GParamSpec          *pspec);
123 static gboolean gtk_scale_button_scroll         (GtkWidget           *widget,
124                                                  GdkEventScroll      *event);
125 static void gtk_scale_button_screen_changed     (GtkWidget           *widget,
126                                                  GdkScreen           *previous_screen);
127 static gboolean gtk_scale_button_press          (GtkWidget           *widget,
128                                                  GdkEventButton      *event);
129 static gboolean gtk_scale_button_key_release    (GtkWidget           *widget,
130                                                  GdkEventKey         *event);
131 static void     gtk_scale_button_popup          (GtkWidget           *widget);
132 static void     gtk_scale_button_popdown        (GtkWidget           *widget);
133 static gboolean cb_dock_button_press            (GtkWidget           *widget,
134                                                  GdkEventButton      *event,
135                                                  gpointer             user_data);
136 static gboolean cb_dock_key_release             (GtkWidget           *widget,
137                                                  GdkEventKey         *event,
138                                                  gpointer             user_data);
139 static gboolean cb_button_press                 (GtkWidget           *widget,
140                                                  GdkEventButton      *event,
141                                                  gpointer             user_data);
142 static gboolean cb_button_release               (GtkWidget           *widget,
143                                                  GdkEventButton      *event,
144                                                  gpointer             user_data);
145 static void cb_dock_grab_notify                 (GtkWidget           *widget,
146                                                  gboolean             was_grabbed,
147                                                  gpointer             user_data);
148 static gboolean cb_dock_grab_broken_event       (GtkWidget           *widget,
149                                                  gboolean             was_grabbed,
150                                                  gpointer             user_data);
151 static void cb_scale_grab_notify                (GtkWidget           *widget,
152                                                  gboolean             was_grabbed,
153                                                  gpointer             user_data);
154 static void gtk_scale_button_update_icon        (GtkScaleButton      *button);
155 static void gtk_scale_button_scale_value_changed(GtkRange            *range);
156
157 /* see below for scale definitions */
158 static GtkWidget *gtk_scale_button_scale_box_new(GtkScaleButton      *button);
159
160 G_DEFINE_TYPE_WITH_CODE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON,
161                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
162                                                 NULL))
163
164 static guint signals[LAST_SIGNAL] = { 0, };
165
166 static void
167 gtk_scale_button_class_init (GtkScaleButtonClass *klass)
168 {
169   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
170   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
171   GtkBindingSet *binding_set;
172
173   g_type_class_add_private (klass, sizeof (GtkScaleButtonPrivate));
174
175   gobject_class->constructor = gtk_scale_button_constructor;
176   gobject_class->finalize = gtk_scale_button_finalize;
177   gobject_class->dispose = gtk_scale_button_dispose;
178   gobject_class->set_property = gtk_scale_button_set_property;
179   gobject_class->get_property = gtk_scale_button_get_property;
180
181   widget_class->button_press_event = gtk_scale_button_press;
182   widget_class->key_release_event = gtk_scale_button_key_release;
183   widget_class->scroll_event = gtk_scale_button_scroll;
184   widget_class->screen_changed = gtk_scale_button_screen_changed;
185
186   /**
187    * GtkScaleButton:orientation:
188    *
189    * The orientation of the #GtkScaleButton's popup window.
190    *
191    * Since: 2.14
192    **/
193   g_object_class_override_property (gobject_class,
194                                     PROP_ORIENTATION,
195                                     "orientation");
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_orientable_set_orientation (GTK_ORIENTABLE (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   if (!adjustment)
680     adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
681   else
682     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
683
684   if (button->priv->adjustment != adjustment)
685     {
686       if (button->priv->adjustment)
687         g_object_unref (button->priv->adjustment);
688       button->priv->adjustment = g_object_ref_sink (adjustment);
689
690       if (button->priv->scale)
691         gtk_range_set_adjustment (GTK_RANGE (button->priv->scale), adjustment);
692
693       g_object_notify (G_OBJECT (button), "adjustment");
694     }
695 }
696
697 /**
698  * gtk_scale_button_get_orientation:
699  * @button: a #GtkScaleButton
700  *
701  * Gets the orientation of the #GtkScaleButton's popup window.
702  *
703  * Returns: the #GtkScaleButton's orientation.
704  *
705  * Since: 2.14
706  *
707  * Deprecated: 2.16: Use gtk_orientable_get_orientation() instead.
708  **/
709 GtkOrientation
710 gtk_scale_button_get_orientation (GtkScaleButton *button)
711 {
712   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), GTK_ORIENTATION_VERTICAL);
713
714   return button->priv->orientation;
715 }
716
717 /**
718  * gtk_scale_button_set_orientation:
719  * @button: a #GtkScaleButton
720  * @orientation: the new orientation
721  *
722  * Sets the orientation of the #GtkScaleButton's popup window.
723  *
724  * Since: 2.14
725  *
726  * Deprecated: 2.16: Use gtk_orientable_set_orientation() instead.
727  **/
728 void
729 gtk_scale_button_set_orientation (GtkScaleButton *button,
730                                   GtkOrientation  orientation)
731 {
732   GtkScaleButtonPrivate *priv;
733
734   g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
735
736   priv = button->priv;
737
738   if (orientation != priv->orientation)
739     {
740       priv->orientation = orientation;
741
742       if (priv->scale)
743         {
744           GtkWidget *box = priv->scale->parent;
745           GtkWidget *frame = box->parent;
746
747           g_object_ref (button->plus_button);
748           g_object_ref (button->minus_button);
749
750           gtk_container_remove (GTK_CONTAINER (box), button->plus_button);
751           gtk_container_remove (GTK_CONTAINER (box), button->minus_button);
752           gtk_container_remove (GTK_CONTAINER (box), priv->scale);
753           gtk_container_remove (GTK_CONTAINER (frame), box);
754
755           box = gtk_scale_button_scale_box_new (button);
756           gtk_container_add (GTK_CONTAINER (frame), box);
757
758           /* FIXME: without this, the popup window appears as a square
759            * after changing the orientation
760            */
761           gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
762
763           g_object_unref (button->plus_button);
764           g_object_unref (button->minus_button);
765         }
766
767       g_object_notify (G_OBJECT (button), "orientation");
768     }
769 }
770
771 /**
772  * gtk_scale_button_get_plus_button:
773  * @button: a #GtkScaleButton
774  *
775  * Retrieves the plus button of the #GtkScaleButton.
776  *
777  * Returns: the plus button of the #GtkScaleButton.
778  *
779  * Since: 2.14
780  */
781 GtkWidget *
782 gtk_scale_button_get_plus_button (GtkScaleButton *button)
783 {
784   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
785
786   return button->plus_button;
787 }
788
789 /**
790  * gtk_scale_button_get_minus_button:
791  * @button: a #GtkScaleButton
792  *
793  * Retrieves the minus button of the #GtkScaleButton.
794  *
795  * Returns: the minus button of the #GtkScaleButton.
796  *
797  * Since: 2.14
798  */
799 GtkWidget *
800 gtk_scale_button_get_minus_button (GtkScaleButton *button)
801 {
802   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
803
804   return button->minus_button;
805 }
806
807 /**
808  * gtk_scale_button_get_popup:
809  * @button: a #GtkScaleButton
810  *
811  * Retrieves the popup of the #GtkScaleButton.
812  *
813  * Returns: the popup of the #GtkScaleButton
814  *
815  * Since: 2.14
816  */
817 GtkWidget *
818 gtk_scale_button_get_popup (GtkScaleButton *button)
819 {
820   g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
821
822   return button->priv->dock;
823 }
824
825 /*
826  * button callbacks.
827  */
828
829 static gboolean
830 gtk_scale_button_scroll (GtkWidget      *widget,
831                          GdkEventScroll *event)
832 {
833   GtkScaleButton *button;
834   GtkScaleButtonPrivate *priv;
835   GtkAdjustment *adj;
836   gdouble d;
837
838   button = GTK_SCALE_BUTTON (widget);
839   priv = button->priv;
840   adj = priv->adjustment;
841
842   if (event->type != GDK_SCROLL)
843     return FALSE;
844
845   d = gtk_scale_button_get_value (button);
846   if (event->direction == GDK_SCROLL_UP)
847     {
848       d += adj->step_increment;
849       if (d > adj->upper)
850         d = adj->upper;
851     }
852   else
853     {
854       d -= adj->step_increment;
855       if (d < adj->lower)
856         d = adj->lower;
857     }
858   gtk_scale_button_set_value (button, d);
859
860   return TRUE;
861 }
862
863 static void
864 gtk_scale_button_screen_changed (GtkWidget *widget,
865                                  GdkScreen *previous_screen)
866 {
867   GtkScaleButton *button = (GtkScaleButton *) widget;
868   GtkScaleButtonPrivate *priv;
869   GdkScreen *screen;
870   GValue value = { 0, };
871
872   if (gtk_widget_has_screen (widget) == FALSE)
873     return;
874
875   priv = button->priv;
876
877   screen = gtk_widget_get_screen (widget);
878   g_value_init (&value, G_TYPE_INT);
879   if (gdk_screen_get_setting (screen,
880                               "gtk-double-click-time",
881                               &value) == FALSE)
882     {
883       priv->click_timeout = CLICK_TIMEOUT;
884       return;
885     }
886
887   priv->click_timeout = g_value_get_int (&value);
888 }
889
890 static gboolean
891 gtk_scale_popup (GtkWidget *widget,
892                  GdkEvent  *event,
893                  guint32    time)
894 {
895   GtkScaleButton *button;
896   GtkScaleButtonPrivate *priv;
897   GtkAdjustment *adj;
898   gint x, y, m, dx, dy, sx, sy, startoff;
899   gdouble v;
900   GdkDisplay *display;
901   GdkScreen *screen;
902
903   button = GTK_SCALE_BUTTON (widget);
904   priv = button->priv;
905   adj = priv->adjustment;
906
907   display = gtk_widget_get_display (widget);
908   screen = gtk_widget_get_screen (widget);
909
910   /* position roughly */
911   gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
912
913   gdk_window_get_origin (widget->window, &x, &y);
914   x += widget->allocation.x;
915   y += widget->allocation.y;
916
917   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
918     gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
919   else
920     gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
921
922   gtk_widget_show_all (priv->dock);
923
924   gdk_window_get_origin (priv->dock->window, &dx, &dy);
925   dx += priv->dock->allocation.x;
926   dy += priv->dock->allocation.y;
927
928   gdk_window_get_origin (priv->scale->window, &sx, &sy);
929   sx += priv->scale->allocation.x;
930   sy += priv->scale->allocation.y;
931
932   priv->timeout = TRUE;
933
934   /* position (needs widget to be shown already) */
935   v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
936
937   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
938     {
939       startoff = sy - dy;
940
941       x += (widget->allocation.width - priv->dock->allocation.width) / 2;
942       y -= startoff;
943       y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
944       m = priv->scale->allocation.height -
945           GTK_RANGE (priv->scale)->min_slider_size;
946       y -= m * (1.0 - v);
947     }
948   else
949     {
950       startoff = sx - dx;
951
952       x -= startoff;
953       y += (widget->allocation.height - priv->dock->allocation.height) / 2;
954       x -= GTK_RANGE (priv->scale)->min_slider_size / 2;
955       m = priv->scale->allocation.width -
956           GTK_RANGE (priv->scale)->min_slider_size;
957       x -= m * v;
958     }
959
960   /* Make sure the dock stays inside the monitor */
961   if (event->type == GDK_BUTTON_PRESS)
962     {
963       int monitor;
964       GdkEventButton *button_event = (GdkEventButton *) event;
965       GdkRectangle rect;
966       GtkWidget *d;
967
968       d = GTK_WIDGET (priv->dock);
969       monitor = gdk_screen_get_monitor_at_point (screen,
970                                                  button_event->x_root,
971                                                  button_event->y_root);
972       gdk_screen_get_monitor_geometry (screen, monitor, &rect);
973
974       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
975         y += button_event->y;
976       else
977         x += button_event->x;
978
979       if (y < rect.y)
980         y = rect.y;
981       else if (y + d->allocation.height > rect.height + rect.y)
982         y = rect.y + rect.height - d->allocation.height;
983
984       if (x < rect.x)
985         x = rect.x;
986       else if (x + d->allocation.width > rect.width + rect.x)
987         x = rect.x + rect.width - d->allocation.width;
988     }
989
990   gtk_window_move (GTK_WINDOW (priv->dock), x, y);
991
992   if (event->type == GDK_BUTTON_PRESS)
993     GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
994
995   /* grab focus */
996   gtk_grab_add (priv->dock);
997
998   if (gdk_pointer_grab (priv->dock->window, TRUE,
999                         GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1000                         GDK_POINTER_MOTION_MASK, NULL, NULL, time)
1001       != GDK_GRAB_SUCCESS)
1002     {
1003       gtk_grab_remove (priv->dock);
1004       gtk_widget_hide (priv->dock);
1005       return FALSE;
1006     }
1007
1008   if (gdk_keyboard_grab (priv->dock->window, TRUE, time) != GDK_GRAB_SUCCESS)
1009     {
1010       gdk_display_pointer_ungrab (display, time);
1011       gtk_grab_remove (priv->dock);
1012       gtk_widget_hide (priv->dock);
1013       return FALSE;
1014     }
1015
1016   gtk_widget_grab_focus (priv->dock);
1017
1018   if (event->type == GDK_BUTTON_PRESS)
1019     {
1020       GdkEventButton *e;
1021       GdkEventButton *button_event = (GdkEventButton *) event;
1022
1023       /* forward event to the slider */
1024       e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1025       e->window = priv->scale->window;
1026
1027       /* position: the X position isn't relevant, halfway will work just fine.
1028        * The vertical position should be *exactly* in the middle of the slider
1029        * of the scale; if we don't do that correctly, it'll move from its current
1030        * position, which means a position change on-click, which is bad.
1031        */
1032       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1033         {
1034           e->x = priv->scale->allocation.width / 2;
1035           m = priv->scale->allocation.height -
1036               GTK_RANGE (priv->scale)->min_slider_size;
1037           e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1038         }
1039       else
1040         {
1041           e->y = priv->scale->allocation.height / 2;
1042           m = priv->scale->allocation.width -
1043               GTK_RANGE (priv->scale)->min_slider_size;
1044           e->x = (v * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1045         }
1046
1047       gtk_widget_event (priv->scale, (GdkEvent *) e);
1048       e->window = button_event->window;
1049       gdk_event_free ((GdkEvent *) e);
1050     }
1051
1052   gtk_widget_grab_focus (priv->scale);
1053
1054   priv->pop_time = time;
1055
1056   return TRUE;
1057 }
1058
1059 static gboolean
1060 gtk_scale_button_press (GtkWidget      *widget,
1061                         GdkEventButton *event)
1062 {
1063   return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
1064 }
1065
1066 static void
1067 gtk_scale_button_popup (GtkWidget *widget)
1068 {
1069   GdkEvent *ev;
1070
1071   ev = gdk_event_new (GDK_KEY_RELEASE);
1072   gtk_scale_popup (widget, ev, GDK_CURRENT_TIME);
1073   gdk_event_free (ev);
1074 }
1075
1076 static gboolean
1077 gtk_scale_button_key_release (GtkWidget   *widget,
1078                               GdkEventKey *event)
1079 {
1080   return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
1081 }
1082
1083 /* This is called when the grab is broken for
1084  * either the dock, or the scale itself */
1085 static void
1086 gtk_scale_button_grab_notify (GtkScaleButton *button,
1087                               gboolean        was_grabbed)
1088 {
1089   GdkDisplay *display;
1090   GtkScaleButtonPrivate *priv;
1091
1092   if (was_grabbed != FALSE)
1093     return;
1094
1095   priv = button->priv;
1096
1097   if (!GTK_WIDGET_HAS_GRAB (priv->dock))
1098     return;
1099
1100   if (gtk_widget_is_ancestor (gtk_grab_get_current (), priv->dock))
1101     return;
1102
1103   display = gtk_widget_get_display (priv->dock);
1104   gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1105   gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1106   gtk_grab_remove (priv->dock);
1107
1108   /* hide again */
1109   gtk_widget_hide (priv->dock);
1110   priv->timeout = FALSE;
1111 }
1112
1113 /*
1114  * +/- button callbacks.
1115  */
1116
1117 static gboolean
1118 cb_button_timeout (gpointer user_data)
1119 {
1120   GtkScaleButton *button;
1121   GtkScaleButtonPrivate *priv;
1122   GtkAdjustment *adj;
1123   gdouble val;
1124   gboolean res = TRUE;
1125
1126   button = GTK_SCALE_BUTTON (user_data);
1127   priv = button->priv;
1128
1129   if (priv->click_id == 0)
1130     return FALSE;
1131
1132   adj = priv->adjustment;
1133
1134   val = gtk_scale_button_get_value (button);
1135   val += priv->direction;
1136   if (val <= adj->lower)
1137     {
1138       res = FALSE;
1139       val = adj->lower;
1140     }
1141   else if (val > adj->upper)
1142     {
1143       res = FALSE;
1144       val = adj->upper;
1145     }
1146   gtk_scale_button_set_value (button, val);
1147
1148   if (!res)
1149     {
1150       g_source_remove (priv->click_id);
1151       priv->click_id = 0;
1152     }
1153
1154   return res;
1155 }
1156
1157 static gboolean
1158 cb_button_press (GtkWidget      *widget,
1159                  GdkEventButton *event,
1160                  gpointer        user_data)
1161 {
1162   GtkScaleButton *button;
1163   GtkScaleButtonPrivate *priv;
1164   GtkAdjustment *adj;
1165
1166   button = GTK_SCALE_BUTTON (user_data);
1167   priv = button->priv;
1168   adj = priv->adjustment;
1169
1170   if (priv->click_id != 0)
1171     g_source_remove (priv->click_id);
1172
1173   if (widget == button->plus_button)
1174     priv->direction = fabs (adj->page_increment);
1175   else
1176     priv->direction = - fabs (adj->page_increment);
1177
1178   priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1179                                             cb_button_timeout,
1180                                             button);
1181   cb_button_timeout (button);
1182
1183   return TRUE;
1184 }
1185
1186 static gboolean
1187 cb_button_release (GtkWidget      *widget,
1188                    GdkEventButton *event,
1189                    gpointer        user_data)
1190 {
1191   GtkScaleButton *button;
1192   GtkScaleButtonPrivate *priv;
1193
1194   button = GTK_SCALE_BUTTON (user_data);
1195   priv = button->priv;
1196
1197   if (priv->click_id != 0)
1198     {
1199       g_source_remove (priv->click_id);
1200       priv->click_id = 0;
1201     }
1202
1203   return TRUE;
1204 }
1205
1206 static void
1207 cb_dock_grab_notify (GtkWidget *widget,
1208                      gboolean   was_grabbed,
1209                      gpointer   user_data)
1210 {
1211   GtkScaleButton *button = (GtkScaleButton *) user_data;
1212
1213   gtk_scale_button_grab_notify (button, was_grabbed);
1214 }
1215
1216 static gboolean
1217 cb_dock_grab_broken_event (GtkWidget *widget,
1218                            gboolean   was_grabbed,
1219                            gpointer   user_data)
1220 {
1221   GtkScaleButton *button = (GtkScaleButton *) user_data;
1222
1223   gtk_scale_button_grab_notify (button, FALSE);
1224
1225   return FALSE;
1226 }
1227
1228 /*
1229  * Scale callbacks.
1230  */
1231
1232 static void
1233 gtk_scale_button_release_grab (GtkScaleButton *button,
1234                                GdkEventButton *event)
1235 {
1236   GdkEventButton *e;
1237   GdkDisplay *display;
1238   GtkScaleButtonPrivate *priv;
1239
1240   priv = button->priv;
1241
1242   /* ungrab focus */
1243   display = gtk_widget_get_display (GTK_WIDGET (button));
1244   gdk_display_keyboard_ungrab (display, event->time);
1245   gdk_display_pointer_ungrab (display, event->time);
1246   gtk_grab_remove (priv->dock);
1247
1248   /* hide again */
1249   gtk_widget_hide (priv->dock);
1250   priv->timeout = FALSE;
1251
1252   e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1253   e->window = GTK_WIDGET (button)->window;
1254   e->type = GDK_BUTTON_RELEASE;
1255   gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1256   e->window = event->window;
1257   gdk_event_free ((GdkEvent *) e);
1258 }
1259
1260 static gboolean
1261 cb_dock_button_press (GtkWidget      *widget,
1262                       GdkEventButton *event,
1263                       gpointer        user_data)
1264 {
1265   GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1266
1267   if (event->type == GDK_BUTTON_PRESS)
1268     {
1269       gtk_scale_button_release_grab (button, event);
1270       return TRUE;
1271     }
1272
1273   return FALSE;
1274 }
1275
1276 static void
1277 gtk_scale_button_popdown (GtkWidget *widget)
1278 {
1279   GtkScaleButton *button;
1280   GtkScaleButtonPrivate *priv;
1281   GdkDisplay *display;
1282
1283   button = GTK_SCALE_BUTTON (widget);
1284   priv = button->priv;
1285
1286   /* ungrab focus */
1287   display = gtk_widget_get_display (widget);
1288   gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1289   gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1290   gtk_grab_remove (priv->dock);
1291
1292   /* hide again */
1293   gtk_widget_hide (priv->dock);
1294   priv->timeout = FALSE;
1295 }
1296
1297 static gboolean
1298 cb_dock_key_release (GtkWidget   *widget,
1299                      GdkEventKey *event,
1300                      gpointer     user_data)
1301 {
1302   if (event->keyval == GDK_Escape)
1303     {
1304       gtk_scale_button_popdown (GTK_WIDGET (user_data));
1305       return TRUE;
1306     }
1307
1308   if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
1309     {
1310       /* The popup hasn't managed the event, pass onto the button */
1311       gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
1312     }
1313
1314   return TRUE;
1315 }
1316
1317 static void
1318 cb_scale_grab_notify (GtkWidget *widget,
1319                       gboolean   was_grabbed,
1320                       gpointer   user_data)
1321 {
1322   GtkScaleButton *button = (GtkScaleButton *) user_data;
1323
1324   gtk_scale_button_grab_notify (button, was_grabbed);
1325 }
1326
1327 /*
1328  * Scale stuff.
1329  */
1330
1331 #define GTK_TYPE_SCALE_BUTTON_VSCALE    (_gtk_scale_button_vscale_get_type ())
1332 #define GTK_SCALE_BUTTON_VSCALE(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_VSCALE, GtkScaleButtonVScale))
1333 #define GTK_IS_SCALE_BUTTON_VSCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_VSCALE))
1334
1335 typedef struct _GtkScaleButtonVScale {
1336   GtkVScale parent;
1337   GtkScaleButton *button;
1338 } GtkScaleButtonVScale;
1339
1340 typedef struct _GtkScaleButtonVScaleClass {
1341   GtkVScaleClass parent_class;
1342 } GtkScaleButtonVScaleClass;
1343
1344 #define GTK_TYPE_SCALE_BUTTON_HSCALE    (_gtk_scale_button_hscale_get_type ())
1345 #define GTK_SCALE_BUTTON_HSCALE(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_HSCALE, GtkScaleButtonHScale))
1346 #define GTK_IS_SCALE_BUTTON_HSCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_HSCALE))
1347
1348 typedef struct _GtkScaleButtonHScale {
1349   GtkVScale parent;
1350   GtkScaleButton *button;
1351 } GtkScaleButtonHScale;
1352
1353 typedef struct _GtkScaleButtonHScaleClass {
1354   GtkVScaleClass parent_class;
1355 } GtkScaleButtonHScaleClass;
1356
1357 static gboolean gtk_scale_button_scale_press   (GtkWidget      *widget,
1358                                                 GdkEventButton *event);
1359 static gboolean gtk_scale_button_scale_release (GtkWidget      *widget,
1360                                                 GdkEventButton *event);
1361
1362 G_DEFINE_TYPE (GtkScaleButtonVScale, _gtk_scale_button_vscale, GTK_TYPE_VSCALE)
1363
1364 G_DEFINE_TYPE (GtkScaleButtonHScale, _gtk_scale_button_hscale, GTK_TYPE_HSCALE)
1365
1366 static void
1367 _gtk_scale_button_vscale_class_init (GtkScaleButtonVScaleClass *klass)
1368 {
1369   GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1370   GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1371
1372   gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1373   gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1374   gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1375 }
1376
1377 static void
1378 _gtk_scale_button_hscale_class_init (GtkScaleButtonHScaleClass *klass)
1379 {
1380   GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1381   GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1382
1383   gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1384   gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1385   gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1386 }
1387
1388 static void
1389 _gtk_scale_button_vscale_init (GtkScaleButtonVScale *scale)
1390 {
1391 }
1392
1393 static void
1394 _gtk_scale_button_hscale_init (GtkScaleButtonHScale *scale)
1395 {
1396 }
1397
1398 static GtkWidget *
1399 gtk_scale_button_scale_box_new (GtkScaleButton *button)
1400 {
1401   GtkScaleButtonPrivate *priv = button->priv;
1402   GtkScaleButtonVScale *scale;
1403   GtkWidget *box;
1404
1405   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1406     {
1407       box = gtk_vbox_new (FALSE, 0);
1408
1409       scale = g_object_new (GTK_TYPE_SCALE_BUTTON_VSCALE, NULL);
1410       priv->scale = GTK_WIDGET (scale);
1411
1412       gtk_widget_set_size_request (priv->scale, -1, SCALE_SIZE);
1413       gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
1414
1415       gtk_box_pack_start (GTK_BOX (box), button->plus_button, TRUE, FALSE, 0);
1416       gtk_box_pack_start (GTK_BOX (box), priv->scale, TRUE, FALSE, 0);
1417       gtk_box_pack_start (GTK_BOX (box), button->minus_button, TRUE, FALSE, 0);
1418     }
1419   else
1420     {
1421       box = gtk_hbox_new (FALSE, 0);
1422
1423       scale = g_object_new (GTK_TYPE_SCALE_BUTTON_HSCALE, NULL);
1424       priv->scale = GTK_WIDGET (scale);
1425
1426       gtk_widget_set_size_request (priv->scale, SCALE_SIZE, -1);
1427
1428       gtk_box_pack_start (GTK_BOX (box), button->minus_button, TRUE, FALSE, 0);
1429       gtk_box_pack_start (GTK_BOX (box), priv->scale, TRUE, FALSE, 0);
1430       gtk_box_pack_start (GTK_BOX (box), button->plus_button, TRUE, FALSE, 0);
1431     }
1432
1433   gtk_scale_set_draw_value (GTK_SCALE (priv->scale), FALSE);
1434   gtk_range_set_adjustment (GTK_RANGE (scale),
1435                             GTK_ADJUSTMENT (priv->adjustment));
1436   scale->button = button;
1437
1438   g_signal_connect (priv->scale, "grab-notify",
1439                     G_CALLBACK (cb_scale_grab_notify), button);
1440
1441   /* FIXME: without this, the popup window appears as a square
1442    * after changing the orientation
1443    */
1444   gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
1445
1446   return box;
1447 }
1448
1449 static gboolean
1450 gtk_scale_button_scale_press (GtkWidget      *widget,
1451                               GdkEventButton *event)
1452 {
1453   GtkScaleButtonPrivate *priv;
1454   GtkWidgetClass *widget_class;
1455
1456   if (GTK_IS_SCALE_BUTTON_VSCALE (widget))
1457     {
1458       priv = GTK_SCALE_BUTTON_VSCALE (widget)->button->priv;
1459       widget_class = GTK_WIDGET_CLASS (_gtk_scale_button_vscale_parent_class);
1460     }
1461   else
1462     {
1463       priv = GTK_SCALE_BUTTON_HSCALE (widget)->button->priv;
1464       widget_class = GTK_WIDGET_CLASS (_gtk_scale_button_hscale_parent_class);
1465     }
1466
1467   /* the scale will grab input; if we have input grabbed, all goes
1468    * horribly wrong, so let's not do that. */
1469   gtk_grab_remove (priv->dock);
1470
1471   return widget_class->button_press_event (widget, event);
1472 }
1473
1474 static gboolean
1475 gtk_scale_button_scale_release (GtkWidget      *widget,
1476                                 GdkEventButton *event)
1477 {
1478   GtkScaleButton *button;
1479   GtkWidgetClass *widget_class;
1480   gboolean res;
1481
1482   if (GTK_IS_SCALE_BUTTON_VSCALE (widget))
1483     {
1484       button = GTK_SCALE_BUTTON_VSCALE (widget)->button;
1485       widget_class = GTK_WIDGET_CLASS (_gtk_scale_button_vscale_parent_class);
1486     }
1487   else
1488     {
1489       button = GTK_SCALE_BUTTON_HSCALE (widget)->button;
1490       widget_class = GTK_WIDGET_CLASS (_gtk_scale_button_hscale_parent_class);
1491     }
1492
1493   if (button->priv->timeout)
1494     {
1495       /* if we did a quick click, leave the window open; else, hide it */
1496       if (event->time > button->priv->pop_time + button->priv->click_timeout)
1497         {
1498
1499           gtk_scale_button_release_grab (button, event);
1500           widget_class->button_release_event (widget, event);
1501
1502           return TRUE;
1503         }
1504
1505       button->priv->timeout = FALSE;
1506     }
1507
1508   res = widget_class->button_release_event (widget, event);
1509
1510   /* the scale will release input; right after that, we *have to* grab
1511    * it back so we can catch out-of-scale clicks and hide the popup,
1512    * so I basically want a g_signal_connect_after_always(), but I can't
1513    * find that, so we do this complex 'first-call-parent-then-do-actual-
1514    * action' thingy...
1515    */
1516   gtk_grab_add (button->priv->dock);
1517
1518   return res;
1519 }
1520
1521 static void
1522 gtk_scale_button_update_icon (GtkScaleButton *button)
1523 {
1524   GtkScaleButtonPrivate *priv;
1525   GtkRange *range;
1526   GtkAdjustment *adj;
1527   gdouble value;
1528   const gchar *name;
1529   guint num_icons;
1530
1531   priv = button->priv;
1532
1533   if (!priv->icon_list || priv->icon_list[0] == '\0')
1534     {
1535       gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1536                                 GTK_STOCK_MISSING_IMAGE,
1537                                 priv->size);
1538       return;
1539     }
1540
1541   num_icons = g_strv_length (priv->icon_list);
1542
1543   /* The 1-icon special case */
1544   if (num_icons == 1)
1545     {
1546       gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1547                                     priv->icon_list[0],
1548                                     priv->size);
1549       return;
1550     }
1551
1552   range = GTK_RANGE (priv->scale);
1553   adj = priv->adjustment;
1554   value = gtk_scale_button_get_value (button);
1555
1556   /* The 2-icons special case */
1557   if (num_icons == 2)
1558     {
1559       gdouble limit;
1560       limit = (adj->upper - adj->lower) / 2 + adj->lower;
1561       if (value < limit)
1562         name = priv->icon_list[0];
1563       else
1564         name = priv->icon_list[1];
1565
1566       gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1567                                     name,
1568                                     priv->size);
1569       return;
1570     }
1571
1572   /* With 3 or more icons */
1573   if (value == adj->lower)
1574     {
1575       name = priv->icon_list[0];
1576     }
1577   else if (value == adj->upper)
1578     {
1579       name = priv->icon_list[1];
1580     }
1581   else
1582     {
1583       gdouble step;
1584       guint i;
1585
1586       step = (adj->upper - adj->lower) / (num_icons - 2);
1587       i = (guint) ((value - adj->lower) / step) + 2;
1588       g_assert (i < num_icons);
1589       name = priv->icon_list[i];
1590     }
1591
1592   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1593                                 name,
1594                                 priv->size);
1595 }
1596
1597 static void
1598 gtk_scale_button_scale_value_changed (GtkRange *range)
1599 {
1600   GtkScaleButton *button;
1601   gdouble value;
1602
1603   if (GTK_IS_SCALE_BUTTON_VSCALE (range))
1604     button = GTK_SCALE_BUTTON_VSCALE (range)->button;
1605   else
1606     button = GTK_SCALE_BUTTON_HSCALE (range)->button;
1607
1608   value = gtk_range_get_value (range);
1609
1610   gtk_scale_button_update_icon (button);
1611
1612   g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1613   g_object_notify (G_OBJECT (button), "value");
1614 }
1615
1616 #define __GTK_SCALE_BUTTON_C__
1617 #include "gtkaliasdef.c"