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 "gtkbindings.h"
48 #include "gtkmarshalers.h"
49 #include "gtkorientable.h"
50 #include "gtkprivate.h"
52 #include "gtkscalebutton.h"
55 #include "gtkwindow.h"
60 * SECTION:gtkscalebutton
61 * @Short_description: A button which pops up a scale
62 * @Title: GtkScaleButton
64 * #GtkScaleButton provides a button which pops up a scale widget.
65 * This kind of widget is commonly used for volume controls in multimedia
66 * applications, and GTK+ provides a #GtkVolumeButton subclass that
67 * is tailored for this use case.
71 #define SCALE_SIZE 100
72 #define CLICK_TIMEOUT 250
94 struct _GtkScaleButtonPrivate
96 GtkWidget *plus_button;
97 GtkWidget *minus_button;
104 GtkOrientation orientation;
114 GdkDevice *grab_pointer;
115 GdkDevice *grab_keyboard;
117 GtkAdjustment *adjustment; /* needed because it must be settable in init() */
120 static GObject* gtk_scale_button_constructor (GType type,
121 guint n_construct_properties,
122 GObjectConstructParam *construct_params);
123 static void gtk_scale_button_dispose (GObject *object);
124 static void gtk_scale_button_finalize (GObject *object);
125 static void gtk_scale_button_set_property (GObject *object,
129 static void gtk_scale_button_get_property (GObject *object,
133 static void gtk_scale_button_set_orientation_private (GtkScaleButton *button,
134 GtkOrientation orientation);
135 static gboolean gtk_scale_button_scroll (GtkWidget *widget,
136 GdkEventScroll *event);
137 static void gtk_scale_button_screen_changed (GtkWidget *widget,
138 GdkScreen *previous_screen);
139 static gboolean gtk_scale_button_press (GtkWidget *widget,
140 GdkEventButton *event);
141 static gboolean gtk_scale_button_key_release (GtkWidget *widget,
143 static void gtk_scale_button_popup (GtkWidget *widget);
144 static void gtk_scale_button_popdown (GtkWidget *widget);
145 static gboolean cb_dock_button_press (GtkWidget *widget,
146 GdkEventButton *event,
148 static gboolean cb_dock_key_release (GtkWidget *widget,
151 static gboolean cb_button_press (GtkWidget *widget,
152 GdkEventButton *event,
154 static gboolean cb_button_release (GtkWidget *widget,
155 GdkEventButton *event,
157 static void cb_dock_grab_notify (GtkWidget *widget,
158 gboolean was_grabbed,
160 static gboolean cb_dock_grab_broken_event (GtkWidget *widget,
161 gboolean was_grabbed,
163 static void cb_scale_grab_notify (GtkWidget *widget,
164 gboolean was_grabbed,
166 static void gtk_scale_button_update_icon (GtkScaleButton *button);
167 static void gtk_scale_button_scale_value_changed(GtkRange *range);
169 /* see below for scale definitions */
170 static GtkWidget *gtk_scale_button_scale_new (GtkScaleButton *button);
172 G_DEFINE_TYPE_WITH_CODE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON,
173 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
176 static guint signals[LAST_SIGNAL] = { 0, };
179 gtk_scale_button_class_init (GtkScaleButtonClass *klass)
181 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
182 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
183 GtkBindingSet *binding_set;
185 g_type_class_add_private (klass, sizeof (GtkScaleButtonPrivate));
187 gobject_class->constructor = gtk_scale_button_constructor;
188 gobject_class->finalize = gtk_scale_button_finalize;
189 gobject_class->dispose = gtk_scale_button_dispose;
190 gobject_class->set_property = gtk_scale_button_set_property;
191 gobject_class->get_property = gtk_scale_button_get_property;
193 widget_class->button_press_event = gtk_scale_button_press;
194 widget_class->key_release_event = gtk_scale_button_key_release;
195 widget_class->scroll_event = gtk_scale_button_scroll;
196 widget_class->screen_changed = gtk_scale_button_screen_changed;
199 * GtkScaleButton:orientation:
201 * The orientation of the #GtkScaleButton's popup window.
203 * Note that since GTK+ 2.16, #GtkScaleButton implements the
204 * #GtkOrientable interface which has its own @orientation
205 * property. However we redefine the property here in order to
206 * override its default horizontal orientation.
210 g_object_class_override_property (gobject_class,
214 g_object_class_install_property (gobject_class,
216 g_param_spec_double ("value",
218 P_("The value of the scale"),
222 GTK_PARAM_READWRITE));
224 g_object_class_install_property (gobject_class,
226 g_param_spec_enum ("size",
230 GTK_ICON_SIZE_SMALL_TOOLBAR,
231 GTK_PARAM_READWRITE));
233 g_object_class_install_property (gobject_class,
235 g_param_spec_object ("adjustment",
237 P_("The GtkAdjustment that contains the current value of this scale button object"),
239 GTK_PARAM_READWRITE));
242 * GtkScaleButton:icons:
244 * The names of the icons to be used by the scale button.
245 * The first item in the array will be used in the button
246 * when the current value is the lowest value, the second
247 * item for the highest value. All the subsequent icons will
248 * be used for all the other values, spread evenly over the
251 * If there's only one icon name in the @icons array, it will
252 * be used for all the values. If only two icon names are in
253 * the @icons array, the first one will be used for the bottom
254 * 50% of the scale, and the second one for the top 50%.
256 * It is recommended to use at least 3 icons so that the
257 * #GtkScaleButton reflects the current value of the scale
258 * better for the users.
262 g_object_class_install_property (gobject_class,
264 g_param_spec_boxed ("icons",
266 P_("List of icon names"),
268 GTK_PARAM_READWRITE));
271 * GtkScaleButton::value-changed:
272 * @button: the object which received the signal
273 * @value: the new value
275 * The ::value-changed signal is emitted when the value field has
280 signals[VALUE_CHANGED] =
281 g_signal_new (I_("value-changed"),
282 G_TYPE_FROM_CLASS (klass),
284 G_STRUCT_OFFSET (GtkScaleButtonClass, value_changed),
286 _gtk_marshal_VOID__DOUBLE,
287 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
290 * GtkScaleButton::popup:
291 * @button: the object which received the signal
293 * The ::popup signal is a
294 * <link linkend="keybinding-signals">keybinding signal</link>
295 * which gets emitted to popup the scale widget.
297 * The default bindings for this signal are Space, Enter and Return.
302 g_signal_new_class_handler (I_("popup"),
303 G_OBJECT_CLASS_TYPE (klass),
304 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
305 G_CALLBACK (gtk_scale_button_popup),
307 g_cclosure_marshal_VOID__VOID,
311 * GtkScaleButton::popdown:
312 * @button: the object which received the signal
314 * The ::popdown signal is a
315 * <link linkend="keybinding-signals">keybinding signal</link>
316 * which gets emitted to popdown the scale widget.
318 * The default binding for this signal is Escape.
323 g_signal_new_class_handler (I_("popdown"),
324 G_OBJECT_CLASS_TYPE (klass),
325 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
326 G_CALLBACK (gtk_scale_button_popdown),
328 g_cclosure_marshal_VOID__VOID,
332 binding_set = gtk_binding_set_by_class (widget_class);
334 gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, 0,
336 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, 0,
338 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
340 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
342 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
344 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
349 gtk_scale_button_init (GtkScaleButton *button)
351 GtkScaleButtonPrivate *priv;
354 button->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
355 GTK_TYPE_SCALE_BUTTON,
356 GtkScaleButtonPrivate);
358 priv->timeout = FALSE;
360 priv->click_timeout = CLICK_TIMEOUT;
361 priv->orientation = GTK_ORIENTATION_VERTICAL;
363 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
364 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
367 priv->image = gtk_image_new ();
368 gtk_container_add (GTK_CONTAINER (button), priv->image);
369 gtk_widget_show_all (priv->image);
372 priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
373 gtk_widget_set_name (priv->dock, "gtk-scalebutton-popup-window");
374 g_signal_connect (priv->dock, "button-press-event",
375 G_CALLBACK (cb_dock_button_press), button);
376 g_signal_connect (priv->dock, "key-release-event",
377 G_CALLBACK (cb_dock_key_release), button);
378 g_signal_connect (priv->dock, "grab-notify",
379 G_CALLBACK (cb_dock_grab_notify), button);
380 g_signal_connect (priv->dock, "grab-broken-event",
381 G_CALLBACK (cb_dock_grab_broken_event), button);
382 gtk_window_set_decorated (GTK_WINDOW (priv->dock), FALSE);
385 frame = gtk_frame_new (NULL);
386 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
387 gtk_container_add (GTK_CONTAINER (priv->dock), frame);
389 /* box for scale and +/- buttons */
390 priv->box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
391 gtk_container_add (GTK_CONTAINER (frame), priv->box);
394 priv->plus_button = gtk_button_new_with_label ("+");
395 gtk_button_set_relief (GTK_BUTTON (priv->plus_button), GTK_RELIEF_NONE);
396 g_signal_connect (priv->plus_button, "button-press-event",
397 G_CALLBACK (cb_button_press), button);
398 g_signal_connect (priv->plus_button, "button-release-event",
399 G_CALLBACK (cb_button_release), button);
400 gtk_box_pack_start (GTK_BOX (priv->box), priv->plus_button, FALSE, FALSE, 0);
403 priv->minus_button = gtk_button_new_with_label ("-");
404 gtk_button_set_relief (GTK_BUTTON (priv->minus_button), GTK_RELIEF_NONE);
405 g_signal_connect (priv->minus_button, "button-press-event",
406 G_CALLBACK (cb_button_press), button);
407 g_signal_connect (priv->minus_button, "button-release-event",
408 G_CALLBACK (cb_button_release), button);
409 gtk_box_pack_end (GTK_BOX (priv->box), priv->minus_button, FALSE, FALSE, 0);
411 priv->adjustment = gtk_adjustment_new (0.0, 0.0, 100.0, 2, 20, 0);
412 g_object_ref_sink (priv->adjustment);
415 priv->scale = gtk_scale_button_scale_new (button);
416 gtk_container_add (GTK_CONTAINER (priv->box), priv->scale);
420 gtk_scale_button_constructor (GType type,
421 guint n_construct_properties,
422 GObjectConstructParam *construct_params)
425 GtkScaleButton *button;
426 GtkScaleButtonPrivate *priv;
428 object = G_OBJECT_CLASS (gtk_scale_button_parent_class)->constructor (type, n_construct_properties, construct_params);
430 button = GTK_SCALE_BUTTON (object);
434 /* set button text and size */
435 priv->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
436 gtk_scale_button_update_icon (button);
442 gtk_scale_button_set_property (GObject *object,
447 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
451 case PROP_ORIENTATION:
452 gtk_scale_button_set_orientation_private (button, g_value_get_enum (value));
455 gtk_scale_button_set_value (button, g_value_get_double (value));
460 size = g_value_get_enum (value);
461 if (button->priv->size != size)
463 button->priv->size = size;
464 gtk_scale_button_update_icon (button);
468 case PROP_ADJUSTMENT:
469 gtk_scale_button_set_adjustment (button, g_value_get_object (value));
472 gtk_scale_button_set_icons (button,
473 (const gchar **)g_value_get_boxed (value));
476 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482 gtk_scale_button_get_property (GObject *object,
487 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
488 GtkScaleButtonPrivate *priv = button->priv;
492 case PROP_ORIENTATION:
493 g_value_set_enum (value, priv->orientation);
496 g_value_set_double (value, gtk_scale_button_get_value (button));
499 g_value_set_enum (value, priv->size);
501 case PROP_ADJUSTMENT:
502 g_value_set_object (value, gtk_scale_button_get_adjustment (button));
505 g_value_set_boxed (value, priv->icon_list);
508 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
514 gtk_scale_button_finalize (GObject *object)
516 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
517 GtkScaleButtonPrivate *priv = button->priv;
521 g_strfreev (priv->icon_list);
522 priv->icon_list = NULL;
525 if (priv->adjustment)
527 g_object_unref (priv->adjustment);
528 priv->adjustment = NULL;
531 G_OBJECT_CLASS (gtk_scale_button_parent_class)->finalize (object);
535 gtk_scale_button_dispose (GObject *object)
537 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
538 GtkScaleButtonPrivate *priv = button->priv;
542 gtk_widget_destroy (priv->dock);
546 if (priv->click_id != 0)
548 g_source_remove (priv->click_id);
552 G_OBJECT_CLASS (gtk_scale_button_parent_class)->dispose (object);
556 * gtk_scale_button_new:
557 * @size: (type int): a stock icon size
558 * @min: the minimum value of the scale (usually 0)
559 * @max: the maximum value of the scale (usually 100)
560 * @step: the stepping of value when a scroll-wheel event,
561 * or up/down arrow event occurs (usually 2)
562 * @icons: (allow-none): a %NULL-terminated array of icon names, or %NULL if
563 * you want to set the list later with gtk_scale_button_set_icons()
565 * Creates a #GtkScaleButton, with a range between @min and @max, with
566 * a stepping of @step.
568 * Return value: a new #GtkScaleButton
573 gtk_scale_button_new (GtkIconSize size,
579 GtkScaleButton *button;
582 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
584 button = g_object_new (GTK_TYPE_SCALE_BUTTON,
590 return GTK_WIDGET (button);
594 * gtk_scale_button_get_value:
595 * @button: a #GtkScaleButton
597 * Gets the current value of the scale button.
599 * Return value: current value of the scale button
604 gtk_scale_button_get_value (GtkScaleButton * button)
606 GtkScaleButtonPrivate *priv;
608 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), 0);
612 return gtk_adjustment_get_value (priv->adjustment);
616 * gtk_scale_button_set_value:
617 * @button: a #GtkScaleButton
618 * @value: new value of the scale button
620 * Sets the current value of the scale; if the value is outside
621 * the minimum or maximum range values, it will be clamped to fit
622 * inside them. The scale button emits the #GtkScaleButton::value-changed
623 * signal if the value changes.
628 gtk_scale_button_set_value (GtkScaleButton *button,
631 GtkScaleButtonPrivate *priv;
633 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
637 gtk_range_set_value (GTK_RANGE (priv->scale), value);
641 * gtk_scale_button_set_icons:
642 * @button: a #GtkScaleButton
643 * @icons: a %NULL-terminated array of icon names
645 * Sets the icons to be used by the scale button.
646 * For details, see the #GtkScaleButton:icons property.
651 gtk_scale_button_set_icons (GtkScaleButton *button,
654 GtkScaleButtonPrivate *priv;
657 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
661 tmp = priv->icon_list;
662 priv->icon_list = g_strdupv ((gchar **) icons);
664 gtk_scale_button_update_icon (button);
666 g_object_notify (G_OBJECT (button), "icons");
670 * gtk_scale_button_get_adjustment:
671 * @button: a #GtkScaleButton
673 * Gets the #GtkAdjustment associated with the #GtkScaleButton's scale.
674 * See gtk_range_get_adjustment() for details.
676 * Returns: (transfer none): the adjustment associated with the scale
681 gtk_scale_button_get_adjustment (GtkScaleButton *button)
683 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
685 return button->priv->adjustment;
689 * gtk_scale_button_set_adjustment:
690 * @button: a #GtkScaleButton
691 * @adjustment: a #GtkAdjustment
693 * Sets the #GtkAdjustment to be used as a model
694 * for the #GtkScaleButton's scale.
695 * See gtk_range_set_adjustment() for details.
700 gtk_scale_button_set_adjustment (GtkScaleButton *button,
701 GtkAdjustment *adjustment)
703 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
706 adjustment = 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: (transfer none): 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->priv->plus_button;
742 * gtk_scale_button_get_minus_button:
743 * @button: a #GtkScaleButton
745 * Retrieves the minus button of the #GtkScaleButton.
747 * Returns: (transfer none): 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->priv->minus_button;
760 * gtk_scale_button_get_popup:
761 * @button: a #GtkScaleButton
763 * Retrieves the popup of the #GtkScaleButton.
765 * Returns: (transfer none): 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),
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 GtkAllocation allocation, dock_allocation, scale_allocation;
898 GtkScaleButton *button;
899 GtkScaleButtonPrivate *priv;
901 gint x, y, m, dx, dy, sx, sy, startoff;
902 gint min_slider_size;
907 GdkDevice *device, *keyboard, *pointer;
910 button = GTK_SCALE_BUTTON (widget);
912 adj = priv->adjustment;
914 display = gtk_widget_get_display (widget);
915 screen = gtk_widget_get_screen (widget);
916 gtk_widget_get_allocation (widget, &allocation);
918 /* position roughly */
919 gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
921 gdk_window_get_origin (gtk_widget_get_window (widget),
926 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
927 gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
929 gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
931 gtk_widget_show_all (priv->dock);
933 gdk_window_get_origin (gtk_widget_get_window (priv->dock),
935 gtk_widget_get_allocation (priv->dock, &dock_allocation);
936 dx += dock_allocation.x;
937 dy += dock_allocation.y;
940 gdk_window_get_origin (gtk_widget_get_window (priv->scale),
942 gtk_widget_get_allocation (priv->scale, &scale_allocation);
943 sx += scale_allocation.x;
944 sy += scale_allocation.y;
946 priv->timeout = TRUE;
948 /* position (needs widget to be shown already) */
949 v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
950 min_slider_size = gtk_range_get_min_slider_size (GTK_RANGE (priv->scale));
952 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
956 x += (allocation.width - dock_allocation.width) / 2;
958 y -= min_slider_size / 2;
959 m = scale_allocation.height - min_slider_size;
967 y += (allocation.height - dock_allocation.height) / 2;
968 x -= min_slider_size / 2;
969 m = scale_allocation.width - min_slider_size;
973 /* Make sure the dock stays inside the monitor */
974 if (event->type == GDK_BUTTON_PRESS)
976 GtkAllocation d_allocation;
978 GdkEventButton *button_event = (GdkEventButton *) event;
982 d = GTK_WIDGET (priv->dock);
983 monitor = gdk_screen_get_monitor_at_point (screen,
984 button_event->x_root,
985 button_event->y_root);
986 gdk_screen_get_monitor_geometry (screen, monitor, &rect);
988 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
989 y += button_event->y;
991 x += button_event->x;
993 /* Move the dock, but set is_moved so we
994 * don't forward the first click later on,
995 * as it could make the scale go to the bottom */
996 gtk_widget_get_allocation (d, &d_allocation);
1002 else if (y + d_allocation.height > rect.height + rect.y)
1004 y = rect.y + rect.height - d_allocation.height;
1013 else if (x + d_allocation.width > rect.width + rect.x)
1015 x = rect.x + rect.width - d_allocation.width;
1020 gtk_window_move (GTK_WINDOW (priv->dock), x, y);
1022 if (event->type == GDK_BUTTON_PRESS)
1023 GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
1025 device = gdk_event_get_device (event);
1027 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1030 pointer = gdk_device_get_associated_device (device);
1035 keyboard = gdk_device_get_associated_device (device);
1039 gtk_device_grab_add (priv->dock, pointer, TRUE);
1041 if (gdk_device_grab (pointer, gtk_widget_get_window (priv->dock),
1042 GDK_OWNERSHIP_WINDOW, TRUE,
1043 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1044 GDK_POINTER_MOTION_MASK, NULL, time) != GDK_GRAB_SUCCESS)
1046 gtk_device_grab_remove (priv->dock, pointer);
1047 gtk_widget_hide (priv->dock);
1051 if (gdk_device_grab (keyboard, gtk_widget_get_window (priv->dock),
1052 GDK_OWNERSHIP_WINDOW, TRUE,
1053 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1054 NULL, time) != GDK_GRAB_SUCCESS)
1056 gdk_device_ungrab (pointer, time);
1057 gtk_device_grab_remove (priv->dock, pointer);
1058 gtk_widget_hide (priv->dock);
1062 gtk_widget_grab_focus (priv->dock);
1063 priv->grab_keyboard = keyboard;
1064 priv->grab_pointer = pointer;
1066 if (event->type == GDK_BUTTON_PRESS && !is_moved)
1069 GdkEventButton *button_event = (GdkEventButton *) event;
1071 /* forward event to the slider */
1072 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1073 e->window = gtk_widget_get_window (priv->scale);
1075 /* position: the X position isn't relevant, halfway will work just fine.
1076 * The vertical position should be *exactly* in the middle of the slider
1077 * of the scale; if we don't do that correctly, it'll move from its current
1078 * position, which means a position change on-click, which is bad.
1080 gtk_widget_get_allocation (priv->scale, &scale_allocation);
1081 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1083 e->x = scale_allocation.width / 2;
1084 m = scale_allocation.height - min_slider_size;
1085 e->y = ((1.0 - v) * m) + min_slider_size / 2;
1089 e->y = scale_allocation.height / 2;
1090 m = scale_allocation.width - min_slider_size;
1091 e->x = (v * m) + min_slider_size / 2;
1094 gtk_widget_event (priv->scale, (GdkEvent *) e);
1095 e->window = button_event->window;
1096 gdk_event_free ((GdkEvent *) e);
1099 gtk_widget_grab_focus (priv->scale);
1101 priv->pop_time = time;
1107 gtk_scale_button_press (GtkWidget *widget,
1108 GdkEventButton *event)
1110 return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
1114 gtk_scale_button_popup (GtkWidget *widget)
1118 /* This is a callback for a keybinding signal,
1119 * current event should be the key event that
1122 ev = gtk_get_current_event ();
1124 if (ev->type != GDK_KEY_PRESS &&
1125 ev->type != GDK_KEY_RELEASE)
1127 gdk_event_free (ev);
1128 ev = gdk_event_new (GDK_KEY_RELEASE);
1129 ev->key.time = GDK_CURRENT_TIME;
1132 gtk_scale_popup (widget, ev, ev->key.time);
1133 gdk_event_free (ev);
1137 gtk_scale_button_key_release (GtkWidget *widget,
1140 return gtk_bindings_activate_event (G_OBJECT (widget), event);
1143 /* This is called when the grab is broken for
1144 * either the dock, or the scale itself */
1146 gtk_scale_button_grab_notify (GtkScaleButton *button,
1147 gboolean was_grabbed)
1149 GdkDisplay *display;
1150 GtkScaleButtonPrivate *priv;
1151 GtkWidget *toplevel, *grab_widget;
1152 GtkWindowGroup *group;
1154 priv = button->priv;
1156 if (!priv->grab_pointer ||
1157 !gtk_widget_device_is_shadowed (GTK_WIDGET (button), priv->grab_pointer))
1160 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
1162 if (GTK_IS_WINDOW (toplevel))
1163 group = gtk_window_get_group (GTK_WINDOW (toplevel));
1165 group = gtk_window_get_group (NULL);
1167 grab_widget = gtk_window_group_get_current_device_grab (group, priv->grab_pointer);
1170 gtk_widget_is_ancestor (grab_widget, priv->dock))
1173 display = gtk_widget_get_display (priv->dock);
1174 gdk_device_ungrab (priv->grab_keyboard, GDK_CURRENT_TIME);
1175 gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1176 gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1178 priv->grab_keyboard = NULL;
1179 priv->grab_pointer = NULL;
1182 gtk_widget_hide (priv->dock);
1183 priv->timeout = FALSE;
1187 * +/- button callbacks.
1191 cb_button_timeout (gpointer user_data)
1193 GtkScaleButton *button;
1194 GtkScaleButtonPrivate *priv;
1197 gboolean res = TRUE;
1199 button = GTK_SCALE_BUTTON (user_data);
1200 priv = button->priv;
1202 if (priv->click_id == 0)
1205 adj = priv->adjustment;
1207 val = gtk_scale_button_get_value (button);
1208 val += priv->direction;
1209 if (val <= adj->lower)
1214 else if (val > adj->upper)
1219 gtk_scale_button_set_value (button, val);
1223 g_source_remove (priv->click_id);
1231 cb_button_press (GtkWidget *widget,
1232 GdkEventButton *event,
1235 GtkScaleButton *button;
1236 GtkScaleButtonPrivate *priv;
1239 button = GTK_SCALE_BUTTON (user_data);
1240 priv = button->priv;
1241 adj = priv->adjustment;
1243 if (priv->click_id != 0)
1244 g_source_remove (priv->click_id);
1246 if (widget == priv->plus_button)
1247 priv->direction = fabs (adj->page_increment);
1249 priv->direction = - fabs (adj->page_increment);
1251 priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1254 cb_button_timeout (button);
1260 cb_button_release (GtkWidget *widget,
1261 GdkEventButton *event,
1264 GtkScaleButton *button;
1265 GtkScaleButtonPrivate *priv;
1267 button = GTK_SCALE_BUTTON (user_data);
1268 priv = button->priv;
1270 if (priv->click_id != 0)
1272 g_source_remove (priv->click_id);
1280 cb_dock_grab_notify (GtkWidget *widget,
1281 gboolean was_grabbed,
1284 GtkScaleButton *button = (GtkScaleButton *) user_data;
1286 gtk_scale_button_grab_notify (button, was_grabbed);
1290 cb_dock_grab_broken_event (GtkWidget *widget,
1291 gboolean was_grabbed,
1294 GtkScaleButton *button = (GtkScaleButton *) user_data;
1296 gtk_scale_button_grab_notify (button, FALSE);
1306 gtk_scale_button_release_grab (GtkScaleButton *button,
1307 GdkEventButton *event)
1310 GdkDisplay *display;
1311 GtkScaleButtonPrivate *priv;
1313 priv = button->priv;
1316 display = gtk_widget_get_display (GTK_WIDGET (button));
1317 gdk_device_ungrab (priv->grab_keyboard, event->time);
1318 gdk_device_ungrab (priv->grab_pointer, event->time);
1319 gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1321 priv->grab_keyboard = NULL;
1322 priv->grab_pointer = NULL;
1325 gtk_widget_hide (priv->dock);
1326 priv->timeout = FALSE;
1328 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1329 e->window = gtk_widget_get_window (GTK_WIDGET (button));
1330 e->type = GDK_BUTTON_RELEASE;
1331 gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1332 e->window = event->window;
1333 gdk_event_free ((GdkEvent *) e);
1337 cb_dock_button_press (GtkWidget *widget,
1338 GdkEventButton *event,
1341 GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1343 if (event->type == GDK_BUTTON_PRESS)
1345 gtk_scale_button_release_grab (button, event);
1353 gtk_scale_button_popdown (GtkWidget *widget)
1355 GtkScaleButton *button;
1356 GtkScaleButtonPrivate *priv;
1357 GdkDisplay *display;
1359 button = GTK_SCALE_BUTTON (widget);
1360 priv = button->priv;
1363 display = gtk_widget_get_display (widget);
1364 gdk_device_ungrab (priv->grab_keyboard, GDK_CURRENT_TIME);
1365 gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1366 gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1368 priv->grab_keyboard = NULL;
1369 priv->grab_pointer = NULL;
1372 gtk_widget_hide (priv->dock);
1373 priv->timeout = FALSE;
1377 cb_dock_key_release (GtkWidget *widget,
1381 if (event->keyval == GDK_KEY_Escape)
1383 gtk_scale_button_popdown (GTK_WIDGET (user_data));
1387 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
1389 /* The popup hasn't managed the event, pass onto the button */
1390 gtk_bindings_activate_event (G_OBJECT (user_data), event);
1397 cb_scale_grab_notify (GtkWidget *widget,
1398 gboolean was_grabbed,
1401 GtkScaleButton *button = (GtkScaleButton *) user_data;
1403 gtk_scale_button_grab_notify (button, was_grabbed);
1410 #define GTK_TYPE_SCALE_BUTTON_SCALE (_gtk_scale_button_scale_get_type ())
1411 #define GTK_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, GtkScaleButtonScale))
1412 #define GTK_IS_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_SCALE))
1414 typedef struct _GtkScaleButtonScale
1416 GtkScale parent_instance;
1417 GtkScaleButton *button;
1418 } GtkScaleButtonScale;
1420 typedef struct _GtkScaleButtonScaleClass
1422 GtkScaleClass parent_class;
1423 } GtkScaleButtonScaleClass;
1425 static gboolean gtk_scale_button_scale_press (GtkWidget *widget,
1426 GdkEventButton *event);
1427 static gboolean gtk_scale_button_scale_release (GtkWidget *widget,
1428 GdkEventButton *event);
1430 G_DEFINE_TYPE (GtkScaleButtonScale, _gtk_scale_button_scale, GTK_TYPE_SCALE)
1433 _gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass)
1435 GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1436 GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1438 gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1439 gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1440 gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1444 _gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
1449 gtk_scale_button_scale_new (GtkScaleButton *button)
1451 GtkScaleButtonPrivate *priv = button->priv;
1452 GtkScaleButtonScale *scale;
1454 scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE,
1455 "orientation", priv->orientation,
1456 "adjustment", priv->adjustment,
1457 "draw-value", FALSE,
1460 scale->button = button;
1462 g_signal_connect (scale, "grab-notify",
1463 G_CALLBACK (cb_scale_grab_notify), button);
1465 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1467 gtk_widget_set_size_request (GTK_WIDGET (scale), -1, SCALE_SIZE);
1468 gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
1472 gtk_widget_set_size_request (GTK_WIDGET (scale), SCALE_SIZE, -1);
1473 gtk_range_set_inverted (GTK_RANGE (scale), FALSE);
1476 return GTK_WIDGET (scale);
1480 gtk_scale_button_scale_press (GtkWidget *widget,
1481 GdkEventButton *event)
1483 GtkScaleButtonPrivate *priv = GTK_SCALE_BUTTON_SCALE (widget)->button->priv;
1485 /* the scale will grab input; if we have input grabbed, all goes
1486 * horribly wrong, so let's not do that.
1488 gtk_device_grab_remove (priv->dock, event->device);
1490 return GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_press_event (widget, event);
1494 gtk_scale_button_scale_release (GtkWidget *widget,
1495 GdkEventButton *event)
1497 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (widget)->button;
1500 if (button->priv->timeout)
1502 /* if we did a quick click, leave the window open; else, hide it */
1503 if (event->time > button->priv->pop_time + button->priv->click_timeout)
1506 gtk_scale_button_release_grab (button, event);
1507 GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1512 button->priv->timeout = FALSE;
1515 res = GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1517 /* the scale will release input; right after that, we *have to* grab
1518 * it back so we can catch out-of-scale clicks and hide the popup,
1519 * so I basically want a g_signal_connect_after_always(), but I can't
1520 * find that, so we do this complex 'first-call-parent-then-do-actual-
1523 gtk_device_grab_add (button->priv->dock, event->device, TRUE);
1529 gtk_scale_button_update_icon (GtkScaleButton *button)
1531 GtkScaleButtonPrivate *priv;
1538 priv = button->priv;
1540 if (!priv->icon_list || priv->icon_list[0] == '\0')
1542 gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1543 GTK_STOCK_MISSING_IMAGE,
1548 num_icons = g_strv_length (priv->icon_list);
1550 /* The 1-icon special case */
1553 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1559 range = GTK_RANGE (priv->scale);
1560 adj = priv->adjustment;
1561 value = gtk_scale_button_get_value (button);
1563 /* The 2-icons special case */
1567 limit = (adj->upper - adj->lower) / 2 + adj->lower;
1569 name = priv->icon_list[0];
1571 name = priv->icon_list[1];
1573 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1579 /* With 3 or more icons */
1580 if (value == adj->lower)
1582 name = priv->icon_list[0];
1584 else if (value == adj->upper)
1586 name = priv->icon_list[1];
1593 step = (adj->upper - adj->lower) / (num_icons - 2);
1594 i = (guint) ((value - adj->lower) / step) + 2;
1595 g_assert (i < num_icons);
1596 name = priv->icon_list[i];
1599 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1605 gtk_scale_button_scale_value_changed (GtkRange *range)
1607 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (range)->button;
1610 value = gtk_range_get_value (range);
1612 gtk_scale_button_update_icon (button);
1614 g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1615 g_object_notify (G_OBJECT (button), "value");