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