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.
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>
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.
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.
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.
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/.
45 #include <gdk-pixbuf/gdk-pixbuf.h>
46 #include <gdk/gdkkeysyms.h>
48 #include "gtkbindings.h"
51 #include "gtkmarshalers.h"
52 #include "gtkorientable.h"
53 #include "gtkprivate.h"
55 #include "gtkscalebutton.h"
58 #include "gtkwindow.h"
63 * SECTION:gtkscalebutton
64 * @Short_description: A button which pops up a scale
65 * @Title: GtkScaleButton
67 * #GtkScaleButton provides a button which pops up a scale widget.
68 * This kind of widget is commonly used for volume controls in multimedia
69 * applications, and GTK+ provides a #GtkVolumeButton subclass that
70 * is tailored for this use case.
74 #define SCALE_SIZE 100
75 #define CLICK_TIMEOUT 250
97 #define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SCALE_BUTTON, GtkScaleButtonPrivate))
99 struct _GtkScaleButtonPrivate
107 GtkOrientation orientation;
117 GdkDevice *grab_pointer;
118 GdkDevice *grab_keyboard;
120 GtkAdjustment *adjustment; /* needed because it must be settable in init() */
123 static GObject* gtk_scale_button_constructor (GType type,
124 guint n_construct_properties,
125 GObjectConstructParam *construct_params);
126 static void gtk_scale_button_dispose (GObject *object);
127 static void gtk_scale_button_finalize (GObject *object);
128 static void gtk_scale_button_set_property (GObject *object,
132 static void gtk_scale_button_get_property (GObject *object,
136 static void gtk_scale_button_set_orientation_private (GtkScaleButton *button,
137 GtkOrientation orientation);
138 static gboolean gtk_scale_button_scroll (GtkWidget *widget,
139 GdkEventScroll *event);
140 static void gtk_scale_button_screen_changed (GtkWidget *widget,
141 GdkScreen *previous_screen);
142 static gboolean gtk_scale_button_press (GtkWidget *widget,
143 GdkEventButton *event);
144 static gboolean gtk_scale_button_key_release (GtkWidget *widget,
146 static void gtk_scale_button_popup (GtkWidget *widget);
147 static void gtk_scale_button_popdown (GtkWidget *widget);
148 static gboolean cb_dock_button_press (GtkWidget *widget,
149 GdkEventButton *event,
151 static gboolean cb_dock_key_release (GtkWidget *widget,
154 static gboolean cb_button_press (GtkWidget *widget,
155 GdkEventButton *event,
157 static gboolean cb_button_release (GtkWidget *widget,
158 GdkEventButton *event,
160 static void cb_dock_grab_notify (GtkWidget *widget,
161 gboolean was_grabbed,
163 static gboolean cb_dock_grab_broken_event (GtkWidget *widget,
164 gboolean was_grabbed,
166 static void cb_scale_grab_notify (GtkWidget *widget,
167 gboolean was_grabbed,
169 static void gtk_scale_button_update_icon (GtkScaleButton *button);
170 static void gtk_scale_button_scale_value_changed(GtkRange *range);
172 /* see below for scale definitions */
173 static GtkWidget *gtk_scale_button_scale_new (GtkScaleButton *button);
175 G_DEFINE_TYPE_WITH_CODE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON,
176 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
179 static guint signals[LAST_SIGNAL] = { 0, };
182 gtk_scale_button_class_init (GtkScaleButtonClass *klass)
184 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
185 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
186 GtkBindingSet *binding_set;
188 g_type_class_add_private (klass, sizeof (GtkScaleButtonPrivate));
190 gobject_class->constructor = gtk_scale_button_constructor;
191 gobject_class->finalize = gtk_scale_button_finalize;
192 gobject_class->dispose = gtk_scale_button_dispose;
193 gobject_class->set_property = gtk_scale_button_set_property;
194 gobject_class->get_property = gtk_scale_button_get_property;
196 widget_class->button_press_event = gtk_scale_button_press;
197 widget_class->key_release_event = gtk_scale_button_key_release;
198 widget_class->scroll_event = gtk_scale_button_scroll;
199 widget_class->screen_changed = gtk_scale_button_screen_changed;
202 * GtkScaleButton:orientation:
204 * The orientation of the #GtkScaleButton's popup window.
206 * Note that since GTK+ 2.16, #GtkScaleButton implements the
207 * #GtkOrientable interface which has its own @orientation
208 * property. However we redefine the property here in order to
209 * override its default horizontal orientation.
213 g_object_class_override_property (gobject_class,
217 g_object_class_install_property (gobject_class,
219 g_param_spec_double ("value",
221 P_("The value of the scale"),
225 GTK_PARAM_READWRITE));
227 g_object_class_install_property (gobject_class,
229 g_param_spec_enum ("size",
233 GTK_ICON_SIZE_SMALL_TOOLBAR,
234 GTK_PARAM_READWRITE));
236 g_object_class_install_property (gobject_class,
238 g_param_spec_object ("adjustment",
240 P_("The GtkAdjustment that contains the current value of this scale button object"),
242 GTK_PARAM_READWRITE));
245 * GtkScaleButton:icons:
247 * The names of the icons to be used by the scale button.
248 * The first item in the array will be used in the button
249 * when the current value is the lowest value, the second
250 * item for the highest value. All the subsequent icons will
251 * be used for all the other values, spread evenly over the
254 * If there's only one icon name in the @icons array, it will
255 * be used for all the values. If only two icon names are in
256 * the @icons array, the first one will be used for the bottom
257 * 50% of the scale, and the second one for the top 50%.
259 * It is recommended to use at least 3 icons so that the
260 * #GtkScaleButton reflects the current value of the scale
261 * better for the users.
265 g_object_class_install_property (gobject_class,
267 g_param_spec_boxed ("icons",
269 P_("List of icon names"),
271 GTK_PARAM_READWRITE));
274 * GtkScaleButton::value-changed:
275 * @button: the object which received the signal
276 * @value: the new value
278 * The ::value-changed signal is emitted when the value field has
283 signals[VALUE_CHANGED] =
284 g_signal_new (I_("value-changed"),
285 G_TYPE_FROM_CLASS (klass),
287 G_STRUCT_OFFSET (GtkScaleButtonClass, value_changed),
289 _gtk_marshal_VOID__DOUBLE,
290 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
293 * GtkScaleButton::popup:
294 * @button: the object which received the signal
296 * The ::popup signal is a
297 * <link linkend="keybinding-signals">keybinding signal</link>
298 * which gets emitted to popup the scale widget.
300 * The default bindings for this signal are Space, Enter and Return.
305 g_signal_new_class_handler (I_("popup"),
306 G_OBJECT_CLASS_TYPE (klass),
307 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
308 G_CALLBACK (gtk_scale_button_popup),
310 g_cclosure_marshal_VOID__VOID,
314 * GtkScaleButton::popdown:
315 * @button: the object which received the signal
317 * The ::popdown signal is a
318 * <link linkend="keybinding-signals">keybinding signal</link>
319 * which gets emitted to popdown the scale widget.
321 * The default binding for this signal is Escape.
326 g_signal_new_class_handler (I_("popdown"),
327 G_OBJECT_CLASS_TYPE (klass),
328 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
329 G_CALLBACK (gtk_scale_button_popdown),
331 g_cclosure_marshal_VOID__VOID,
335 binding_set = gtk_binding_set_by_class (widget_class);
337 gtk_binding_entry_add_signal (binding_set, GDK_space, 0,
339 gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, 0,
341 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
343 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
345 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
347 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
352 gtk_scale_button_init (GtkScaleButton *button)
354 GtkScaleButtonPrivate *priv;
357 button->priv = priv = GET_PRIVATE (button);
359 priv->timeout = FALSE;
361 priv->click_timeout = CLICK_TIMEOUT;
362 priv->orientation = GTK_ORIENTATION_VERTICAL;
364 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
365 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
368 priv->image = gtk_image_new ();
369 gtk_container_add (GTK_CONTAINER (button), priv->image);
370 gtk_widget_show_all (priv->image);
373 priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
374 gtk_widget_set_name (priv->dock, "gtk-scalebutton-popup-window");
375 g_signal_connect (priv->dock, "button-press-event",
376 G_CALLBACK (cb_dock_button_press), button);
377 g_signal_connect (priv->dock, "key-release-event",
378 G_CALLBACK (cb_dock_key_release), button);
379 g_signal_connect (priv->dock, "grab-notify",
380 G_CALLBACK (cb_dock_grab_notify), button);
381 g_signal_connect (priv->dock, "grab-broken-event",
382 G_CALLBACK (cb_dock_grab_broken_event), button);
383 gtk_window_set_decorated (GTK_WINDOW (priv->dock), FALSE);
386 frame = gtk_frame_new (NULL);
387 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
388 gtk_container_add (GTK_CONTAINER (priv->dock), frame);
390 /* box for scale and +/- buttons */
391 priv->box = gtk_vbox_new (FALSE, 0);
392 gtk_container_add (GTK_CONTAINER (frame), priv->box);
395 button->plus_button = gtk_button_new_with_label ("+");
396 gtk_button_set_relief (GTK_BUTTON (button->plus_button), GTK_RELIEF_NONE);
397 g_signal_connect (button->plus_button, "button-press-event",
398 G_CALLBACK (cb_button_press), button);
399 g_signal_connect (button->plus_button, "button-release-event",
400 G_CALLBACK (cb_button_release), button);
401 gtk_box_pack_start (GTK_BOX (priv->box), button->plus_button, FALSE, FALSE, 0);
404 button->minus_button = gtk_button_new_with_label ("-");
405 gtk_button_set_relief (GTK_BUTTON (button->minus_button), GTK_RELIEF_NONE);
406 g_signal_connect (button->minus_button, "button-press-event",
407 G_CALLBACK (cb_button_press), button);
408 g_signal_connect (button->minus_button, "button-release-event",
409 G_CALLBACK (cb_button_release), button);
410 gtk_box_pack_end (GTK_BOX (priv->box), button->minus_button, FALSE, FALSE, 0);
412 priv->adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 100.0, 2, 20, 0));
413 g_object_ref_sink (priv->adjustment);
416 priv->scale = gtk_scale_button_scale_new (button);
417 gtk_container_add (GTK_CONTAINER (priv->box), priv->scale);
421 gtk_scale_button_constructor (GType type,
422 guint n_construct_properties,
423 GObjectConstructParam *construct_params)
426 GtkScaleButton *button;
427 GtkScaleButtonPrivate *priv;
429 object = G_OBJECT_CLASS (gtk_scale_button_parent_class)->constructor (type, n_construct_properties, construct_params);
431 button = GTK_SCALE_BUTTON (object);
435 /* set button text and size */
436 priv->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
437 gtk_scale_button_update_icon (button);
443 gtk_scale_button_set_property (GObject *object,
448 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
452 case PROP_ORIENTATION:
453 gtk_scale_button_set_orientation_private (button, g_value_get_enum (value));
456 gtk_scale_button_set_value (button, g_value_get_double (value));
461 size = g_value_get_enum (value);
462 if (button->priv->size != size)
464 button->priv->size = size;
465 gtk_scale_button_update_icon (button);
469 case PROP_ADJUSTMENT:
470 gtk_scale_button_set_adjustment (button, g_value_get_object (value));
473 gtk_scale_button_set_icons (button,
474 (const gchar **)g_value_get_boxed (value));
477 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
483 gtk_scale_button_get_property (GObject *object,
488 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
489 GtkScaleButtonPrivate *priv = button->priv;
493 case PROP_ORIENTATION:
494 g_value_set_enum (value, priv->orientation);
497 g_value_set_double (value, gtk_scale_button_get_value (button));
500 g_value_set_enum (value, priv->size);
502 case PROP_ADJUSTMENT:
503 g_value_set_object (value, gtk_scale_button_get_adjustment (button));
506 g_value_set_boxed (value, priv->icon_list);
509 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
515 gtk_scale_button_finalize (GObject *object)
517 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
518 GtkScaleButtonPrivate *priv = button->priv;
522 g_strfreev (priv->icon_list);
523 priv->icon_list = NULL;
526 if (priv->adjustment)
528 g_object_unref (priv->adjustment);
529 priv->adjustment = NULL;
532 G_OBJECT_CLASS (gtk_scale_button_parent_class)->finalize (object);
536 gtk_scale_button_dispose (GObject *object)
538 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
539 GtkScaleButtonPrivate *priv = button->priv;
543 gtk_widget_destroy (priv->dock);
547 if (priv->click_id != 0)
549 g_source_remove (priv->click_id);
553 G_OBJECT_CLASS (gtk_scale_button_parent_class)->dispose (object);
557 * gtk_scale_button_new:
558 * @size: (int): a stock icon size
559 * @min: the minimum value of the scale (usually 0)
560 * @max: the maximum value of the scale (usually 100)
561 * @step: the stepping of value when a scroll-wheel event,
562 * or up/down arrow event occurs (usually 2)
563 * @icons: (allow-none): a %NULL-terminated array of icon names, or %NULL if
564 * you want to set the list later with gtk_scale_button_set_icons()
566 * Creates a #GtkScaleButton, with a range between @min and @max, with
567 * a stepping of @step.
569 * Return value: a new #GtkScaleButton
574 gtk_scale_button_new (GtkIconSize size,
580 GtkScaleButton *button;
583 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
585 button = g_object_new (GTK_TYPE_SCALE_BUTTON,
591 return GTK_WIDGET (button);
595 * gtk_scale_button_get_value:
596 * @button: a #GtkScaleButton
598 * Gets the current value of the scale button.
600 * Return value: current value of the scale button
605 gtk_scale_button_get_value (GtkScaleButton * button)
607 GtkScaleButtonPrivate *priv;
609 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), 0);
613 return gtk_adjustment_get_value (priv->adjustment);
617 * gtk_scale_button_set_value:
618 * @button: a #GtkScaleButton
619 * @value: new value of the scale button
621 * Sets the current value of the scale; if the value is outside
622 * the minimum or maximum range values, it will be clamped to fit
623 * inside them. The scale button emits the #GtkScaleButton::value-changed
624 * signal if the value changes.
629 gtk_scale_button_set_value (GtkScaleButton *button,
632 GtkScaleButtonPrivate *priv;
634 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
638 gtk_range_set_value (GTK_RANGE (priv->scale), value);
642 * gtk_scale_button_set_icons:
643 * @button: a #GtkScaleButton
644 * @icons: a %NULL-terminated array of icon names
646 * Sets the icons to be used by the scale button.
647 * For details, see the #GtkScaleButton:icons property.
652 gtk_scale_button_set_icons (GtkScaleButton *button,
655 GtkScaleButtonPrivate *priv;
658 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
662 tmp = priv->icon_list;
663 priv->icon_list = g_strdupv ((gchar **) icons);
665 gtk_scale_button_update_icon (button);
667 g_object_notify (G_OBJECT (button), "icons");
671 * gtk_scale_button_get_adjustment:
672 * @button: a #GtkScaleButton
674 * Gets the #GtkAdjustment associated with the #GtkScaleButton's scale.
675 * See gtk_range_get_adjustment() for details.
677 * Returns: the adjustment associated with the scale
682 gtk_scale_button_get_adjustment (GtkScaleButton *button)
684 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
686 return button->priv->adjustment;
690 * gtk_scale_button_set_adjustment:
691 * @button: a #GtkScaleButton
692 * @adjustment: a #GtkAdjustment
694 * Sets the #GtkAdjustment to be used as a model
695 * for the #GtkScaleButton's scale.
696 * See gtk_range_set_adjustment() for details.
701 gtk_scale_button_set_adjustment (GtkScaleButton *button,
702 GtkAdjustment *adjustment)
704 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
706 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
708 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
710 if (button->priv->adjustment != adjustment)
712 if (button->priv->adjustment)
713 g_object_unref (button->priv->adjustment);
714 button->priv->adjustment = g_object_ref_sink (adjustment);
716 if (button->priv->scale)
717 gtk_range_set_adjustment (GTK_RANGE (button->priv->scale), adjustment);
719 g_object_notify (G_OBJECT (button), "adjustment");
724 * gtk_scale_button_get_plus_button:
725 * @button: a #GtkScaleButton
727 * Retrieves the plus button of the #GtkScaleButton.
729 * Returns: the plus button of the #GtkScaleButton.
734 gtk_scale_button_get_plus_button (GtkScaleButton *button)
736 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
738 return button->plus_button;
742 * gtk_scale_button_get_minus_button:
743 * @button: a #GtkScaleButton
745 * Retrieves the minus button of the #GtkScaleButton.
747 * Returns: the minus button of the #GtkScaleButton.
752 gtk_scale_button_get_minus_button (GtkScaleButton *button)
754 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
756 return button->minus_button;
760 * gtk_scale_button_get_popup:
761 * @button: a #GtkScaleButton
763 * Retrieves the popup of the #GtkScaleButton.
765 * Returns: the popup of the #GtkScaleButton
770 gtk_scale_button_get_popup (GtkScaleButton *button)
772 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
774 return button->priv->dock;
778 gtk_scale_button_set_orientation_private (GtkScaleButton *button,
779 GtkOrientation orientation)
781 GtkScaleButtonPrivate *priv = button->priv;
783 if (orientation != priv->orientation)
785 priv->orientation = orientation;
787 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box),
789 gtk_container_child_set (GTK_CONTAINER (priv->box),
792 orientation == GTK_ORIENTATION_VERTICAL ?
793 GTK_PACK_START : GTK_PACK_END,
795 gtk_container_child_set (GTK_CONTAINER (priv->box),
796 button->minus_button,
798 orientation == GTK_ORIENTATION_VERTICAL ?
799 GTK_PACK_END : GTK_PACK_START,
802 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->scale),
805 if (orientation == GTK_ORIENTATION_VERTICAL)
807 gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
809 gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
813 gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
815 gtk_range_set_inverted (GTK_RANGE (priv->scale), FALSE);
818 /* FIXME: without this, the popup window appears as a square
819 * after changing the orientation
821 gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
823 g_object_notify (G_OBJECT (button), "orientation");
832 gtk_scale_button_scroll (GtkWidget *widget,
833 GdkEventScroll *event)
835 GtkScaleButton *button;
836 GtkScaleButtonPrivate *priv;
840 button = GTK_SCALE_BUTTON (widget);
842 adj = priv->adjustment;
844 if (event->type != GDK_SCROLL)
847 d = gtk_scale_button_get_value (button);
848 if (event->direction == GDK_SCROLL_UP)
850 d += adj->step_increment;
856 d -= adj->step_increment;
860 gtk_scale_button_set_value (button, d);
866 gtk_scale_button_screen_changed (GtkWidget *widget,
867 GdkScreen *previous_screen)
869 GtkScaleButton *button = (GtkScaleButton *) widget;
870 GtkScaleButtonPrivate *priv;
872 GValue value = { 0, };
874 if (gtk_widget_has_screen (widget) == FALSE)
879 screen = gtk_widget_get_screen (widget);
880 g_value_init (&value, G_TYPE_INT);
881 if (gdk_screen_get_setting (screen,
882 "gtk-double-click-time",
885 priv->click_timeout = CLICK_TIMEOUT;
889 priv->click_timeout = g_value_get_int (&value);
893 gtk_scale_popup (GtkWidget *widget,
897 GtkScaleButton *button;
898 GtkScaleButtonPrivate *priv;
900 gint x, y, m, dx, dy, sx, sy, startoff;
905 GdkDevice *device, *keyboard, *pointer;
908 button = GTK_SCALE_BUTTON (widget);
910 adj = priv->adjustment;
912 display = gtk_widget_get_display (widget);
913 screen = gtk_widget_get_screen (widget);
915 /* position roughly */
916 gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
918 gdk_window_get_origin (widget->window, &x, &y);
919 x += widget->allocation.x;
920 y += widget->allocation.y;
922 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
923 gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
925 gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
927 gtk_widget_show_all (priv->dock);
929 gdk_window_get_origin (priv->dock->window, &dx, &dy);
930 dx += priv->dock->allocation.x;
931 dy += priv->dock->allocation.y;
933 gdk_window_get_origin (priv->scale->window, &sx, &sy);
934 sx += priv->scale->allocation.x;
935 sy += priv->scale->allocation.y;
937 priv->timeout = TRUE;
939 /* position (needs widget to be shown already) */
940 v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
942 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
946 x += (widget->allocation.width - priv->dock->allocation.width) / 2;
948 y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
949 m = priv->scale->allocation.height -
950 GTK_RANGE (priv->scale)->min_slider_size;
958 y += (widget->allocation.height - priv->dock->allocation.height) / 2;
959 x -= GTK_RANGE (priv->scale)->min_slider_size / 2;
960 m = priv->scale->allocation.width -
961 GTK_RANGE (priv->scale)->min_slider_size;
965 /* Make sure the dock stays inside the monitor */
966 if (event->type == GDK_BUTTON_PRESS)
969 GdkEventButton *button_event = (GdkEventButton *) event;
973 d = GTK_WIDGET (priv->dock);
974 monitor = gdk_screen_get_monitor_at_point (screen,
975 button_event->x_root,
976 button_event->y_root);
977 gdk_screen_get_monitor_geometry (screen, monitor, &rect);
979 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
980 y += button_event->y;
982 x += button_event->x;
984 /* Move the dock, but set is_moved so we
985 * don't forward the first click later on,
986 * as it could make the scale go to the bottom */
992 else if (y + d->allocation.height > rect.height + rect.y)
994 y = rect.y + rect.height - d->allocation.height;
1003 else if (x + d->allocation.width > rect.width + rect.x)
1005 x = rect.x + rect.width - d->allocation.width;
1010 gtk_window_move (GTK_WINDOW (priv->dock), x, y);
1012 if (event->type == GDK_BUTTON_PRESS)
1013 GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
1015 device = gdk_event_get_device (event);
1017 if (device->source == GDK_SOURCE_KEYBOARD)
1020 pointer = gdk_device_get_associated_device (device);
1025 keyboard = gdk_device_get_associated_device (device);
1029 gtk_device_grab_add (priv->dock, pointer, TRUE);
1031 if (gdk_device_grab (pointer, priv->dock->window,
1032 GDK_OWNERSHIP_WINDOW, TRUE,
1033 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1034 GDK_POINTER_MOTION_MASK, NULL, time) != GDK_GRAB_SUCCESS)
1036 gtk_device_grab_remove (priv->dock, pointer);
1037 gtk_widget_hide (priv->dock);
1041 if (gdk_device_grab (keyboard, priv->dock->window,
1042 GDK_OWNERSHIP_WINDOW, TRUE,
1043 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1044 NULL, time) != GDK_GRAB_SUCCESS)
1046 gdk_device_ungrab (pointer, time);
1047 gtk_device_grab_remove (priv->dock, pointer);
1048 gtk_widget_hide (priv->dock);
1052 gtk_widget_grab_focus (priv->dock);
1053 priv->grab_keyboard = keyboard;
1054 priv->grab_pointer = pointer;
1056 if (event->type == GDK_BUTTON_PRESS && !is_moved)
1059 GdkEventButton *button_event = (GdkEventButton *) event;
1061 /* forward event to the slider */
1062 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1063 e->window = priv->scale->window;
1065 /* position: the X position isn't relevant, halfway will work just fine.
1066 * The vertical position should be *exactly* in the middle of the slider
1067 * of the scale; if we don't do that correctly, it'll move from its current
1068 * position, which means a position change on-click, which is bad.
1070 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1072 e->x = priv->scale->allocation.width / 2;
1073 m = priv->scale->allocation.height -
1074 GTK_RANGE (priv->scale)->min_slider_size;
1075 e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1079 e->y = priv->scale->allocation.height / 2;
1080 m = priv->scale->allocation.width -
1081 GTK_RANGE (priv->scale)->min_slider_size;
1082 e->x = (v * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1085 gtk_widget_event (priv->scale, (GdkEvent *) e);
1086 e->window = button_event->window;
1087 gdk_event_free ((GdkEvent *) e);
1090 gtk_widget_grab_focus (priv->scale);
1092 priv->pop_time = time;
1098 gtk_scale_button_press (GtkWidget *widget,
1099 GdkEventButton *event)
1101 return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
1105 gtk_scale_button_popup (GtkWidget *widget)
1109 /* This is a callback for a keybinding signal,
1110 * current event should be the key event that
1113 ev = gtk_get_current_event ();
1115 if (ev->type != GDK_KEY_PRESS &&
1116 ev->type != GDK_KEY_RELEASE)
1118 gdk_event_free (ev);
1119 ev = gdk_event_new (GDK_KEY_RELEASE);
1120 ev->key.time = GDK_CURRENT_TIME;
1123 gtk_scale_popup (widget, ev, ev->key.time);
1124 gdk_event_free (ev);
1128 gtk_scale_button_key_release (GtkWidget *widget,
1131 return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
1134 /* This is called when the grab is broken for
1135 * either the dock, or the scale itself */
1137 gtk_scale_button_grab_notify (GtkScaleButton *button,
1138 gboolean was_grabbed)
1140 GdkDisplay *display;
1141 GtkScaleButtonPrivate *priv;
1142 GtkWidget *toplevel, *grab_widget;
1143 GtkWindowGroup *group;
1145 priv = button->priv;
1147 if (!priv->grab_pointer ||
1148 !gtk_widget_device_is_shadowed (GTK_WIDGET (button), priv->grab_pointer))
1151 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
1153 if (GTK_IS_WINDOW (toplevel))
1154 group = gtk_window_get_group (GTK_WINDOW (toplevel));
1156 group = gtk_window_get_group (NULL);
1158 grab_widget = gtk_window_group_get_current_device_grab (group, priv->grab_pointer);
1161 gtk_widget_is_ancestor (grab_widget, priv->dock))
1164 display = gtk_widget_get_display (priv->dock);
1165 gdk_device_ungrab (priv->grab_keyboard, GDK_CURRENT_TIME);
1166 gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1167 gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1169 priv->grab_keyboard = NULL;
1170 priv->grab_pointer = NULL;
1173 gtk_widget_hide (priv->dock);
1174 priv->timeout = FALSE;
1178 * +/- button callbacks.
1182 cb_button_timeout (gpointer user_data)
1184 GtkScaleButton *button;
1185 GtkScaleButtonPrivate *priv;
1188 gboolean res = TRUE;
1190 button = GTK_SCALE_BUTTON (user_data);
1191 priv = button->priv;
1193 if (priv->click_id == 0)
1196 adj = priv->adjustment;
1198 val = gtk_scale_button_get_value (button);
1199 val += priv->direction;
1200 if (val <= adj->lower)
1205 else if (val > adj->upper)
1210 gtk_scale_button_set_value (button, val);
1214 g_source_remove (priv->click_id);
1222 cb_button_press (GtkWidget *widget,
1223 GdkEventButton *event,
1226 GtkScaleButton *button;
1227 GtkScaleButtonPrivate *priv;
1230 button = GTK_SCALE_BUTTON (user_data);
1231 priv = button->priv;
1232 adj = priv->adjustment;
1234 if (priv->click_id != 0)
1235 g_source_remove (priv->click_id);
1237 if (widget == button->plus_button)
1238 priv->direction = fabs (adj->page_increment);
1240 priv->direction = - fabs (adj->page_increment);
1242 priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1245 cb_button_timeout (button);
1251 cb_button_release (GtkWidget *widget,
1252 GdkEventButton *event,
1255 GtkScaleButton *button;
1256 GtkScaleButtonPrivate *priv;
1258 button = GTK_SCALE_BUTTON (user_data);
1259 priv = button->priv;
1261 if (priv->click_id != 0)
1263 g_source_remove (priv->click_id);
1271 cb_dock_grab_notify (GtkWidget *widget,
1272 gboolean was_grabbed,
1275 GtkScaleButton *button = (GtkScaleButton *) user_data;
1277 gtk_scale_button_grab_notify (button, was_grabbed);
1281 cb_dock_grab_broken_event (GtkWidget *widget,
1282 gboolean was_grabbed,
1285 GtkScaleButton *button = (GtkScaleButton *) user_data;
1287 gtk_scale_button_grab_notify (button, FALSE);
1297 gtk_scale_button_release_grab (GtkScaleButton *button,
1298 GdkEventButton *event)
1301 GdkDisplay *display;
1302 GtkScaleButtonPrivate *priv;
1304 priv = button->priv;
1307 display = gtk_widget_get_display (GTK_WIDGET (button));
1308 gdk_device_ungrab (priv->grab_keyboard, event->time);
1309 gdk_device_ungrab (priv->grab_pointer, event->time);
1310 gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1312 priv->grab_keyboard = NULL;
1313 priv->grab_pointer = NULL;
1316 gtk_widget_hide (priv->dock);
1317 priv->timeout = FALSE;
1319 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1320 e->window = GTK_WIDGET (button)->window;
1321 e->type = GDK_BUTTON_RELEASE;
1322 gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1323 e->window = event->window;
1324 gdk_event_free ((GdkEvent *) e);
1328 cb_dock_button_press (GtkWidget *widget,
1329 GdkEventButton *event,
1332 GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1334 if (event->type == GDK_BUTTON_PRESS)
1336 gtk_scale_button_release_grab (button, event);
1344 gtk_scale_button_popdown (GtkWidget *widget)
1346 GtkScaleButton *button;
1347 GtkScaleButtonPrivate *priv;
1348 GdkDisplay *display;
1350 button = GTK_SCALE_BUTTON (widget);
1351 priv = button->priv;
1354 display = gtk_widget_get_display (widget);
1355 gdk_device_ungrab (priv->grab_keyboard, GDK_CURRENT_TIME);
1356 gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1357 gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1359 priv->grab_keyboard = NULL;
1360 priv->grab_pointer = NULL;
1363 gtk_widget_hide (priv->dock);
1364 priv->timeout = FALSE;
1368 cb_dock_key_release (GtkWidget *widget,
1372 if (event->keyval == GDK_Escape)
1374 gtk_scale_button_popdown (GTK_WIDGET (user_data));
1378 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
1380 /* The popup hasn't managed the event, pass onto the button */
1381 gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
1388 cb_scale_grab_notify (GtkWidget *widget,
1389 gboolean was_grabbed,
1392 GtkScaleButton *button = (GtkScaleButton *) user_data;
1394 gtk_scale_button_grab_notify (button, was_grabbed);
1401 #define GTK_TYPE_SCALE_BUTTON_SCALE (_gtk_scale_button_scale_get_type ())
1402 #define GTK_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, GtkScaleButtonScale))
1403 #define GTK_IS_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_SCALE))
1405 typedef struct _GtkScaleButtonScale
1407 GtkScale parent_instance;
1408 GtkScaleButton *button;
1409 } GtkScaleButtonScale;
1411 typedef struct _GtkScaleButtonScaleClass
1413 GtkScaleClass parent_class;
1414 } GtkScaleButtonScaleClass;
1416 static gboolean gtk_scale_button_scale_press (GtkWidget *widget,
1417 GdkEventButton *event);
1418 static gboolean gtk_scale_button_scale_release (GtkWidget *widget,
1419 GdkEventButton *event);
1421 G_DEFINE_TYPE (GtkScaleButtonScale, _gtk_scale_button_scale, GTK_TYPE_SCALE)
1424 _gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass)
1426 GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1427 GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1429 gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1430 gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1431 gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1435 _gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
1440 gtk_scale_button_scale_new (GtkScaleButton *button)
1442 GtkScaleButtonPrivate *priv = button->priv;
1443 GtkScaleButtonScale *scale;
1445 scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE,
1446 "orientation", priv->orientation,
1447 "adjustment", priv->adjustment,
1448 "draw-value", FALSE,
1451 scale->button = button;
1453 g_signal_connect (scale, "grab-notify",
1454 G_CALLBACK (cb_scale_grab_notify), button);
1456 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1458 gtk_widget_set_size_request (GTK_WIDGET (scale), -1, SCALE_SIZE);
1459 gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
1463 gtk_widget_set_size_request (GTK_WIDGET (scale), SCALE_SIZE, -1);
1464 gtk_range_set_inverted (GTK_RANGE (scale), FALSE);
1467 return GTK_WIDGET (scale);
1471 gtk_scale_button_scale_press (GtkWidget *widget,
1472 GdkEventButton *event)
1474 GtkScaleButtonPrivate *priv = GTK_SCALE_BUTTON_SCALE (widget)->button->priv;
1476 /* the scale will grab input; if we have input grabbed, all goes
1477 * horribly wrong, so let's not do that.
1479 gtk_device_grab_remove (priv->dock, event->device);
1481 return GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_press_event (widget, event);
1485 gtk_scale_button_scale_release (GtkWidget *widget,
1486 GdkEventButton *event)
1488 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (widget)->button;
1491 if (button->priv->timeout)
1493 /* if we did a quick click, leave the window open; else, hide it */
1494 if (event->time > button->priv->pop_time + button->priv->click_timeout)
1497 gtk_scale_button_release_grab (button, event);
1498 GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1503 button->priv->timeout = FALSE;
1506 res = GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1508 /* the scale will release input; right after that, we *have to* grab
1509 * it back so we can catch out-of-scale clicks and hide the popup,
1510 * so I basically want a g_signal_connect_after_always(), but I can't
1511 * find that, so we do this complex 'first-call-parent-then-do-actual-
1514 gtk_device_grab_add (button->priv->dock, event->device, TRUE);
1520 gtk_scale_button_update_icon (GtkScaleButton *button)
1522 GtkScaleButtonPrivate *priv;
1529 priv = button->priv;
1531 if (!priv->icon_list || priv->icon_list[0] == '\0')
1533 gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1534 GTK_STOCK_MISSING_IMAGE,
1539 num_icons = g_strv_length (priv->icon_list);
1541 /* The 1-icon special case */
1544 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1550 range = GTK_RANGE (priv->scale);
1551 adj = priv->adjustment;
1552 value = gtk_scale_button_get_value (button);
1554 /* The 2-icons special case */
1558 limit = (adj->upper - adj->lower) / 2 + adj->lower;
1560 name = priv->icon_list[0];
1562 name = priv->icon_list[1];
1564 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1570 /* With 3 or more icons */
1571 if (value == adj->lower)
1573 name = priv->icon_list[0];
1575 else if (value == adj->upper)
1577 name = priv->icon_list[1];
1584 step = (adj->upper - adj->lower) / (num_icons - 2);
1585 i = (guint) ((value - adj->lower) / step) + 2;
1586 g_assert (i < num_icons);
1587 name = priv->icon_list[i];
1590 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1596 gtk_scale_button_scale_value_changed (GtkRange *range)
1598 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (range)->button;
1601 value = gtk_range_get_value (range);
1603 gtk_scale_button_update_icon (button);
1605 g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1606 g_object_notify (G_OBJECT (button), "value");