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