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