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 struct _GtkScaleButtonPrivate
99 GtkWidget *plus_button;
100 GtkWidget *minus_button;
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_KEY_space, 0,
339 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, 0,
341 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
343 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
345 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
347 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
352 gtk_scale_button_init (GtkScaleButton *button)
354 GtkScaleButtonPrivate *priv;
357 button->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
358 GTK_TYPE_SCALE_BUTTON,
359 GtkScaleButtonPrivate);
361 priv->timeout = FALSE;
363 priv->click_timeout = CLICK_TIMEOUT;
364 priv->orientation = GTK_ORIENTATION_VERTICAL;
366 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
367 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
370 priv->image = gtk_image_new ();
371 gtk_container_add (GTK_CONTAINER (button), priv->image);
372 gtk_widget_show_all (priv->image);
375 priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
376 gtk_widget_set_name (priv->dock, "gtk-scalebutton-popup-window");
377 g_signal_connect (priv->dock, "button-press-event",
378 G_CALLBACK (cb_dock_button_press), button);
379 g_signal_connect (priv->dock, "key-release-event",
380 G_CALLBACK (cb_dock_key_release), button);
381 g_signal_connect (priv->dock, "grab-notify",
382 G_CALLBACK (cb_dock_grab_notify), button);
383 g_signal_connect (priv->dock, "grab-broken-event",
384 G_CALLBACK (cb_dock_grab_broken_event), button);
385 gtk_window_set_decorated (GTK_WINDOW (priv->dock), FALSE);
388 frame = gtk_frame_new (NULL);
389 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
390 gtk_container_add (GTK_CONTAINER (priv->dock), frame);
392 /* box for scale and +/- buttons */
393 priv->box = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 0);
394 gtk_container_add (GTK_CONTAINER (frame), priv->box);
397 priv->plus_button = gtk_button_new_with_label ("+");
398 gtk_button_set_relief (GTK_BUTTON (priv->plus_button), GTK_RELIEF_NONE);
399 g_signal_connect (priv->plus_button, "button-press-event",
400 G_CALLBACK (cb_button_press), button);
401 g_signal_connect (priv->plus_button, "button-release-event",
402 G_CALLBACK (cb_button_release), button);
403 gtk_box_pack_start (GTK_BOX (priv->box), priv->plus_button, FALSE, FALSE, 0);
406 priv->minus_button = gtk_button_new_with_label ("-");
407 gtk_button_set_relief (GTK_BUTTON (priv->minus_button), GTK_RELIEF_NONE);
408 g_signal_connect (priv->minus_button, "button-press-event",
409 G_CALLBACK (cb_button_press), button);
410 g_signal_connect (priv->minus_button, "button-release-event",
411 G_CALLBACK (cb_button_release), button);
412 gtk_box_pack_end (GTK_BOX (priv->box), priv->minus_button, FALSE, FALSE, 0);
414 priv->adjustment = gtk_adjustment_new (0.0, 0.0, 100.0, 2, 20, 0);
415 g_object_ref_sink (priv->adjustment);
418 priv->scale = gtk_scale_button_scale_new (button);
419 gtk_container_add (GTK_CONTAINER (priv->box), priv->scale);
423 gtk_scale_button_constructor (GType type,
424 guint n_construct_properties,
425 GObjectConstructParam *construct_params)
428 GtkScaleButton *button;
429 GtkScaleButtonPrivate *priv;
431 object = G_OBJECT_CLASS (gtk_scale_button_parent_class)->constructor (type, n_construct_properties, construct_params);
433 button = GTK_SCALE_BUTTON (object);
437 /* set button text and size */
438 priv->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
439 gtk_scale_button_update_icon (button);
445 gtk_scale_button_set_property (GObject *object,
450 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
454 case PROP_ORIENTATION:
455 gtk_scale_button_set_orientation_private (button, g_value_get_enum (value));
458 gtk_scale_button_set_value (button, g_value_get_double (value));
463 size = g_value_get_enum (value);
464 if (button->priv->size != size)
466 button->priv->size = size;
467 gtk_scale_button_update_icon (button);
471 case PROP_ADJUSTMENT:
472 gtk_scale_button_set_adjustment (button, g_value_get_object (value));
475 gtk_scale_button_set_icons (button,
476 (const gchar **)g_value_get_boxed (value));
479 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
485 gtk_scale_button_get_property (GObject *object,
490 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
491 GtkScaleButtonPrivate *priv = button->priv;
495 case PROP_ORIENTATION:
496 g_value_set_enum (value, priv->orientation);
499 g_value_set_double (value, gtk_scale_button_get_value (button));
502 g_value_set_enum (value, priv->size);
504 case PROP_ADJUSTMENT:
505 g_value_set_object (value, gtk_scale_button_get_adjustment (button));
508 g_value_set_boxed (value, priv->icon_list);
511 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
517 gtk_scale_button_finalize (GObject *object)
519 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
520 GtkScaleButtonPrivate *priv = button->priv;
524 g_strfreev (priv->icon_list);
525 priv->icon_list = NULL;
528 if (priv->adjustment)
530 g_object_unref (priv->adjustment);
531 priv->adjustment = NULL;
534 G_OBJECT_CLASS (gtk_scale_button_parent_class)->finalize (object);
538 gtk_scale_button_dispose (GObject *object)
540 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
541 GtkScaleButtonPrivate *priv = button->priv;
545 gtk_widget_destroy (priv->dock);
549 if (priv->click_id != 0)
551 g_source_remove (priv->click_id);
555 G_OBJECT_CLASS (gtk_scale_button_parent_class)->dispose (object);
559 * gtk_scale_button_new:
560 * @size: (type int): a stock icon size
561 * @min: the minimum value of the scale (usually 0)
562 * @max: the maximum value of the scale (usually 100)
563 * @step: the stepping of value when a scroll-wheel event,
564 * or up/down arrow event occurs (usually 2)
565 * @icons: (allow-none): a %NULL-terminated array of icon names, or %NULL if
566 * you want to set the list later with gtk_scale_button_set_icons()
568 * Creates a #GtkScaleButton, with a range between @min and @max, with
569 * a stepping of @step.
571 * Return value: a new #GtkScaleButton
576 gtk_scale_button_new (GtkIconSize size,
582 GtkScaleButton *button;
585 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
587 button = g_object_new (GTK_TYPE_SCALE_BUTTON,
593 return GTK_WIDGET (button);
597 * gtk_scale_button_get_value:
598 * @button: a #GtkScaleButton
600 * Gets the current value of the scale button.
602 * Return value: current value of the scale button
607 gtk_scale_button_get_value (GtkScaleButton * button)
609 GtkScaleButtonPrivate *priv;
611 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), 0);
615 return gtk_adjustment_get_value (priv->adjustment);
619 * gtk_scale_button_set_value:
620 * @button: a #GtkScaleButton
621 * @value: new value of the scale button
623 * Sets the current value of the scale; if the value is outside
624 * the minimum or maximum range values, it will be clamped to fit
625 * inside them. The scale button emits the #GtkScaleButton::value-changed
626 * signal if the value changes.
631 gtk_scale_button_set_value (GtkScaleButton *button,
634 GtkScaleButtonPrivate *priv;
636 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
640 gtk_range_set_value (GTK_RANGE (priv->scale), value);
644 * gtk_scale_button_set_icons:
645 * @button: a #GtkScaleButton
646 * @icons: a %NULL-terminated array of icon names
648 * Sets the icons to be used by the scale button.
649 * For details, see the #GtkScaleButton:icons property.
654 gtk_scale_button_set_icons (GtkScaleButton *button,
657 GtkScaleButtonPrivate *priv;
660 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
664 tmp = priv->icon_list;
665 priv->icon_list = g_strdupv ((gchar **) icons);
667 gtk_scale_button_update_icon (button);
669 g_object_notify (G_OBJECT (button), "icons");
673 * gtk_scale_button_get_adjustment:
674 * @button: a #GtkScaleButton
676 * Gets the #GtkAdjustment associated with the #GtkScaleButton's scale.
677 * See gtk_range_get_adjustment() for details.
679 * Returns: (transfer none): the adjustment associated with the scale
684 gtk_scale_button_get_adjustment (GtkScaleButton *button)
686 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
688 return button->priv->adjustment;
692 * gtk_scale_button_set_adjustment:
693 * @button: a #GtkScaleButton
694 * @adjustment: a #GtkAdjustment
696 * Sets the #GtkAdjustment to be used as a model
697 * for the #GtkScaleButton's scale.
698 * See gtk_range_set_adjustment() for details.
703 gtk_scale_button_set_adjustment (GtkScaleButton *button,
704 GtkAdjustment *adjustment)
706 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
709 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
711 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
713 if (button->priv->adjustment != adjustment)
715 if (button->priv->adjustment)
716 g_object_unref (button->priv->adjustment);
717 button->priv->adjustment = g_object_ref_sink (adjustment);
719 if (button->priv->scale)
720 gtk_range_set_adjustment (GTK_RANGE (button->priv->scale), adjustment);
722 g_object_notify (G_OBJECT (button), "adjustment");
727 * gtk_scale_button_get_plus_button:
728 * @button: a #GtkScaleButton
730 * Retrieves the plus button of the #GtkScaleButton.
732 * Returns: (transfer none): the plus button of the #GtkScaleButton
737 gtk_scale_button_get_plus_button (GtkScaleButton *button)
739 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
741 return button->priv->plus_button;
745 * gtk_scale_button_get_minus_button:
746 * @button: a #GtkScaleButton
748 * Retrieves the minus button of the #GtkScaleButton.
750 * Returns: (transfer none): the minus button of the #GtkScaleButton
755 gtk_scale_button_get_minus_button (GtkScaleButton *button)
757 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
759 return button->priv->minus_button;
763 * gtk_scale_button_get_popup:
764 * @button: a #GtkScaleButton
766 * Retrieves the popup of the #GtkScaleButton.
768 * Returns: (transfer none): the popup of the #GtkScaleButton
773 gtk_scale_button_get_popup (GtkScaleButton *button)
775 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
777 return button->priv->dock;
781 gtk_scale_button_set_orientation_private (GtkScaleButton *button,
782 GtkOrientation orientation)
784 GtkScaleButtonPrivate *priv = button->priv;
786 if (orientation != priv->orientation)
788 priv->orientation = orientation;
790 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box),
792 gtk_container_child_set (GTK_CONTAINER (priv->box),
795 orientation == GTK_ORIENTATION_VERTICAL ?
796 GTK_PACK_START : GTK_PACK_END,
798 gtk_container_child_set (GTK_CONTAINER (priv->box),
801 orientation == GTK_ORIENTATION_VERTICAL ?
802 GTK_PACK_END : GTK_PACK_START,
805 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->scale),
808 if (orientation == GTK_ORIENTATION_VERTICAL)
810 gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
812 gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
816 gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
818 gtk_range_set_inverted (GTK_RANGE (priv->scale), FALSE);
821 /* FIXME: without this, the popup window appears as a square
822 * after changing the orientation
824 gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
826 g_object_notify (G_OBJECT (button), "orientation");
835 gtk_scale_button_scroll (GtkWidget *widget,
836 GdkEventScroll *event)
838 GtkScaleButton *button;
839 GtkScaleButtonPrivate *priv;
843 button = GTK_SCALE_BUTTON (widget);
845 adj = priv->adjustment;
847 if (event->type != GDK_SCROLL)
850 d = gtk_scale_button_get_value (button);
851 if (event->direction == GDK_SCROLL_UP)
853 d += adj->step_increment;
859 d -= adj->step_increment;
863 gtk_scale_button_set_value (button, d);
869 gtk_scale_button_screen_changed (GtkWidget *widget,
870 GdkScreen *previous_screen)
872 GtkScaleButton *button = (GtkScaleButton *) widget;
873 GtkScaleButtonPrivate *priv;
875 GValue value = { 0, };
877 if (gtk_widget_has_screen (widget) == FALSE)
882 screen = gtk_widget_get_screen (widget);
883 g_value_init (&value, G_TYPE_INT);
884 if (gdk_screen_get_setting (screen,
885 "gtk-double-click-time",
888 priv->click_timeout = CLICK_TIMEOUT;
892 priv->click_timeout = g_value_get_int (&value);
896 gtk_scale_popup (GtkWidget *widget,
900 GtkAllocation allocation, dock_allocation, scale_allocation;
901 GtkScaleButton *button;
902 GtkScaleButtonPrivate *priv;
904 gint x, y, m, dx, dy, sx, sy, startoff;
905 gint min_slider_size;
910 GdkDevice *device, *keyboard, *pointer;
913 button = GTK_SCALE_BUTTON (widget);
915 adj = priv->adjustment;
917 display = gtk_widget_get_display (widget);
918 screen = gtk_widget_get_screen (widget);
919 gtk_widget_get_allocation (widget, &allocation);
921 /* position roughly */
922 gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
924 gdk_window_get_origin (gtk_widget_get_window (widget),
929 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
930 gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
932 gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
934 gtk_widget_show_all (priv->dock);
936 gdk_window_get_origin (gtk_widget_get_window (priv->dock),
938 gtk_widget_get_allocation (priv->dock, &dock_allocation);
939 dx += dock_allocation.x;
940 dy += dock_allocation.y;
943 gdk_window_get_origin (gtk_widget_get_window (priv->scale),
945 gtk_widget_get_allocation (priv->scale, &scale_allocation);
946 sx += scale_allocation.x;
947 sy += scale_allocation.y;
949 priv->timeout = TRUE;
951 /* position (needs widget to be shown already) */
952 v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
953 min_slider_size = gtk_range_get_min_slider_size (GTK_RANGE (priv->scale));
955 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
959 x += (allocation.width - dock_allocation.width) / 2;
961 y -= min_slider_size / 2;
962 m = scale_allocation.height - min_slider_size;
970 y += (allocation.height - dock_allocation.height) / 2;
971 x -= min_slider_size / 2;
972 m = scale_allocation.width - min_slider_size;
976 /* Make sure the dock stays inside the monitor */
977 if (event->type == GDK_BUTTON_PRESS)
979 GtkAllocation d_allocation;
981 GdkEventButton *button_event = (GdkEventButton *) event;
985 d = GTK_WIDGET (priv->dock);
986 monitor = gdk_screen_get_monitor_at_point (screen,
987 button_event->x_root,
988 button_event->y_root);
989 gdk_screen_get_monitor_geometry (screen, monitor, &rect);
991 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
992 y += button_event->y;
994 x += button_event->x;
996 /* Move the dock, but set is_moved so we
997 * don't forward the first click later on,
998 * as it could make the scale go to the bottom */
999 gtk_widget_get_allocation (d, &d_allocation);
1005 else if (y + d_allocation.height > rect.height + rect.y)
1007 y = rect.y + rect.height - d_allocation.height;
1016 else if (x + d_allocation.width > rect.width + rect.x)
1018 x = rect.x + rect.width - d_allocation.width;
1023 gtk_window_move (GTK_WINDOW (priv->dock), x, y);
1025 if (event->type == GDK_BUTTON_PRESS)
1026 GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
1028 device = gdk_event_get_device (event);
1030 if (device->source == GDK_SOURCE_KEYBOARD)
1033 pointer = gdk_device_get_associated_device (device);
1038 keyboard = gdk_device_get_associated_device (device);
1042 gtk_device_grab_add (priv->dock, pointer, TRUE);
1044 if (gdk_device_grab (pointer, gtk_widget_get_window (priv->dock),
1045 GDK_OWNERSHIP_WINDOW, TRUE,
1046 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1047 GDK_POINTER_MOTION_MASK, NULL, time) != GDK_GRAB_SUCCESS)
1049 gtk_device_grab_remove (priv->dock, pointer);
1050 gtk_widget_hide (priv->dock);
1054 if (gdk_device_grab (keyboard, gtk_widget_get_window (priv->dock),
1055 GDK_OWNERSHIP_WINDOW, TRUE,
1056 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1057 NULL, time) != GDK_GRAB_SUCCESS)
1059 gdk_device_ungrab (pointer, time);
1060 gtk_device_grab_remove (priv->dock, pointer);
1061 gtk_widget_hide (priv->dock);
1065 gtk_widget_grab_focus (priv->dock);
1066 priv->grab_keyboard = keyboard;
1067 priv->grab_pointer = pointer;
1069 if (event->type == GDK_BUTTON_PRESS && !is_moved)
1072 GdkEventButton *button_event = (GdkEventButton *) event;
1074 /* forward event to the slider */
1075 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1076 e->window = gtk_widget_get_window (priv->scale);
1078 /* position: the X position isn't relevant, halfway will work just fine.
1079 * The vertical position should be *exactly* in the middle of the slider
1080 * of the scale; if we don't do that correctly, it'll move from its current
1081 * position, which means a position change on-click, which is bad.
1083 gtk_widget_get_allocation (priv->scale, &scale_allocation);
1084 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1086 e->x = scale_allocation.width / 2;
1087 m = scale_allocation.height - min_slider_size;
1088 e->y = ((1.0 - v) * m) + min_slider_size / 2;
1092 e->y = scale_allocation.height / 2;
1093 m = scale_allocation.width - min_slider_size;
1094 e->x = (v * m) + min_slider_size / 2;
1097 gtk_widget_event (priv->scale, (GdkEvent *) e);
1098 e->window = button_event->window;
1099 gdk_event_free ((GdkEvent *) e);
1102 gtk_widget_grab_focus (priv->scale);
1104 priv->pop_time = time;
1110 gtk_scale_button_press (GtkWidget *widget,
1111 GdkEventButton *event)
1113 return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
1117 gtk_scale_button_popup (GtkWidget *widget)
1121 /* This is a callback for a keybinding signal,
1122 * current event should be the key event that
1125 ev = gtk_get_current_event ();
1127 if (ev->type != GDK_KEY_PRESS &&
1128 ev->type != GDK_KEY_RELEASE)
1130 gdk_event_free (ev);
1131 ev = gdk_event_new (GDK_KEY_RELEASE);
1132 ev->key.time = GDK_CURRENT_TIME;
1135 gtk_scale_popup (widget, ev, ev->key.time);
1136 gdk_event_free (ev);
1140 gtk_scale_button_key_release (GtkWidget *widget,
1143 return gtk_bindings_activate_event (G_OBJECT (widget), event);
1146 /* This is called when the grab is broken for
1147 * either the dock, or the scale itself */
1149 gtk_scale_button_grab_notify (GtkScaleButton *button,
1150 gboolean was_grabbed)
1152 GdkDisplay *display;
1153 GtkScaleButtonPrivate *priv;
1154 GtkWidget *toplevel, *grab_widget;
1155 GtkWindowGroup *group;
1157 priv = button->priv;
1159 if (!priv->grab_pointer ||
1160 !gtk_widget_device_is_shadowed (GTK_WIDGET (button), priv->grab_pointer))
1163 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
1165 if (GTK_IS_WINDOW (toplevel))
1166 group = gtk_window_get_group (GTK_WINDOW (toplevel));
1168 group = gtk_window_get_group (NULL);
1170 grab_widget = gtk_window_group_get_current_device_grab (group, priv->grab_pointer);
1173 gtk_widget_is_ancestor (grab_widget, priv->dock))
1176 display = gtk_widget_get_display (priv->dock);
1177 gdk_device_ungrab (priv->grab_keyboard, GDK_CURRENT_TIME);
1178 gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1179 gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1181 priv->grab_keyboard = NULL;
1182 priv->grab_pointer = NULL;
1185 gtk_widget_hide (priv->dock);
1186 priv->timeout = FALSE;
1190 * +/- button callbacks.
1194 cb_button_timeout (gpointer user_data)
1196 GtkScaleButton *button;
1197 GtkScaleButtonPrivate *priv;
1200 gboolean res = TRUE;
1202 button = GTK_SCALE_BUTTON (user_data);
1203 priv = button->priv;
1205 if (priv->click_id == 0)
1208 adj = priv->adjustment;
1210 val = gtk_scale_button_get_value (button);
1211 val += priv->direction;
1212 if (val <= adj->lower)
1217 else if (val > adj->upper)
1222 gtk_scale_button_set_value (button, val);
1226 g_source_remove (priv->click_id);
1234 cb_button_press (GtkWidget *widget,
1235 GdkEventButton *event,
1238 GtkScaleButton *button;
1239 GtkScaleButtonPrivate *priv;
1242 button = GTK_SCALE_BUTTON (user_data);
1243 priv = button->priv;
1244 adj = priv->adjustment;
1246 if (priv->click_id != 0)
1247 g_source_remove (priv->click_id);
1249 if (widget == priv->plus_button)
1250 priv->direction = fabs (adj->page_increment);
1252 priv->direction = - fabs (adj->page_increment);
1254 priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1257 cb_button_timeout (button);
1263 cb_button_release (GtkWidget *widget,
1264 GdkEventButton *event,
1267 GtkScaleButton *button;
1268 GtkScaleButtonPrivate *priv;
1270 button = GTK_SCALE_BUTTON (user_data);
1271 priv = button->priv;
1273 if (priv->click_id != 0)
1275 g_source_remove (priv->click_id);
1283 cb_dock_grab_notify (GtkWidget *widget,
1284 gboolean was_grabbed,
1287 GtkScaleButton *button = (GtkScaleButton *) user_data;
1289 gtk_scale_button_grab_notify (button, was_grabbed);
1293 cb_dock_grab_broken_event (GtkWidget *widget,
1294 gboolean was_grabbed,
1297 GtkScaleButton *button = (GtkScaleButton *) user_data;
1299 gtk_scale_button_grab_notify (button, FALSE);
1309 gtk_scale_button_release_grab (GtkScaleButton *button,
1310 GdkEventButton *event)
1313 GdkDisplay *display;
1314 GtkScaleButtonPrivate *priv;
1316 priv = button->priv;
1319 display = gtk_widget_get_display (GTK_WIDGET (button));
1320 gdk_device_ungrab (priv->grab_keyboard, event->time);
1321 gdk_device_ungrab (priv->grab_pointer, event->time);
1322 gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1324 priv->grab_keyboard = NULL;
1325 priv->grab_pointer = NULL;
1328 gtk_widget_hide (priv->dock);
1329 priv->timeout = FALSE;
1331 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1332 e->window = gtk_widget_get_window (GTK_WIDGET (button));
1333 e->type = GDK_BUTTON_RELEASE;
1334 gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1335 e->window = event->window;
1336 gdk_event_free ((GdkEvent *) e);
1340 cb_dock_button_press (GtkWidget *widget,
1341 GdkEventButton *event,
1344 GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1346 if (event->type == GDK_BUTTON_PRESS)
1348 gtk_scale_button_release_grab (button, event);
1356 gtk_scale_button_popdown (GtkWidget *widget)
1358 GtkScaleButton *button;
1359 GtkScaleButtonPrivate *priv;
1360 GdkDisplay *display;
1362 button = GTK_SCALE_BUTTON (widget);
1363 priv = button->priv;
1366 display = gtk_widget_get_display (widget);
1367 gdk_device_ungrab (priv->grab_keyboard, GDK_CURRENT_TIME);
1368 gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1369 gtk_device_grab_remove (priv->dock, priv->grab_pointer);
1371 priv->grab_keyboard = NULL;
1372 priv->grab_pointer = NULL;
1375 gtk_widget_hide (priv->dock);
1376 priv->timeout = FALSE;
1380 cb_dock_key_release (GtkWidget *widget,
1384 if (event->keyval == GDK_KEY_Escape)
1386 gtk_scale_button_popdown (GTK_WIDGET (user_data));
1390 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
1392 /* The popup hasn't managed the event, pass onto the button */
1393 gtk_bindings_activate_event (G_OBJECT (user_data), event);
1400 cb_scale_grab_notify (GtkWidget *widget,
1401 gboolean was_grabbed,
1404 GtkScaleButton *button = (GtkScaleButton *) user_data;
1406 gtk_scale_button_grab_notify (button, was_grabbed);
1413 #define GTK_TYPE_SCALE_BUTTON_SCALE (_gtk_scale_button_scale_get_type ())
1414 #define GTK_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, GtkScaleButtonScale))
1415 #define GTK_IS_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_SCALE))
1417 typedef struct _GtkScaleButtonScale
1419 GtkScale parent_instance;
1420 GtkScaleButton *button;
1421 } GtkScaleButtonScale;
1423 typedef struct _GtkScaleButtonScaleClass
1425 GtkScaleClass parent_class;
1426 } GtkScaleButtonScaleClass;
1428 static gboolean gtk_scale_button_scale_press (GtkWidget *widget,
1429 GdkEventButton *event);
1430 static gboolean gtk_scale_button_scale_release (GtkWidget *widget,
1431 GdkEventButton *event);
1433 G_DEFINE_TYPE (GtkScaleButtonScale, _gtk_scale_button_scale, GTK_TYPE_SCALE)
1436 _gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass)
1438 GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1439 GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1441 gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1442 gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1443 gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1447 _gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
1452 gtk_scale_button_scale_new (GtkScaleButton *button)
1454 GtkScaleButtonPrivate *priv = button->priv;
1455 GtkScaleButtonScale *scale;
1457 scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE,
1458 "orientation", priv->orientation,
1459 "adjustment", priv->adjustment,
1460 "draw-value", FALSE,
1463 scale->button = button;
1465 g_signal_connect (scale, "grab-notify",
1466 G_CALLBACK (cb_scale_grab_notify), button);
1468 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1470 gtk_widget_set_size_request (GTK_WIDGET (scale), -1, SCALE_SIZE);
1471 gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
1475 gtk_widget_set_size_request (GTK_WIDGET (scale), SCALE_SIZE, -1);
1476 gtk_range_set_inverted (GTK_RANGE (scale), FALSE);
1479 return GTK_WIDGET (scale);
1483 gtk_scale_button_scale_press (GtkWidget *widget,
1484 GdkEventButton *event)
1486 GtkScaleButtonPrivate *priv = GTK_SCALE_BUTTON_SCALE (widget)->button->priv;
1488 /* the scale will grab input; if we have input grabbed, all goes
1489 * horribly wrong, so let's not do that.
1491 gtk_device_grab_remove (priv->dock, event->device);
1493 return GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_press_event (widget, event);
1497 gtk_scale_button_scale_release (GtkWidget *widget,
1498 GdkEventButton *event)
1500 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (widget)->button;
1503 if (button->priv->timeout)
1505 /* if we did a quick click, leave the window open; else, hide it */
1506 if (event->time > button->priv->pop_time + button->priv->click_timeout)
1509 gtk_scale_button_release_grab (button, event);
1510 GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1515 button->priv->timeout = FALSE;
1518 res = GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1520 /* the scale will release input; right after that, we *have to* grab
1521 * it back so we can catch out-of-scale clicks and hide the popup,
1522 * so I basically want a g_signal_connect_after_always(), but I can't
1523 * find that, so we do this complex 'first-call-parent-then-do-actual-
1526 gtk_device_grab_add (button->priv->dock, event->device, TRUE);
1532 gtk_scale_button_update_icon (GtkScaleButton *button)
1534 GtkScaleButtonPrivate *priv;
1541 priv = button->priv;
1543 if (!priv->icon_list || priv->icon_list[0] == '\0')
1545 gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1546 GTK_STOCK_MISSING_IMAGE,
1551 num_icons = g_strv_length (priv->icon_list);
1553 /* The 1-icon special case */
1556 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1562 range = GTK_RANGE (priv->scale);
1563 adj = priv->adjustment;
1564 value = gtk_scale_button_get_value (button);
1566 /* The 2-icons special case */
1570 limit = (adj->upper - adj->lower) / 2 + adj->lower;
1572 name = priv->icon_list[0];
1574 name = priv->icon_list[1];
1576 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1582 /* With 3 or more icons */
1583 if (value == adj->lower)
1585 name = priv->icon_list[0];
1587 else if (value == adj->upper)
1589 name = priv->icon_list[1];
1596 step = (adj->upper - adj->lower) / (num_icons - 2);
1597 i = (guint) ((value - adj->lower) / step) + 2;
1598 g_assert (i < num_icons);
1599 name = priv->icon_list[i];
1602 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1608 gtk_scale_button_scale_value_changed (GtkRange *range)
1610 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (range)->button;
1613 value = gtk_range_get_value (range);
1615 gtk_scale_button_update_icon (button);
1617 g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1618 g_object_notify (G_OBJECT (button), "value");