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 #define SCALE_SIZE 100
64 #define CLICK_TIMEOUT 250
86 #define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SCALE_BUTTON, GtkScaleButtonPrivate))
88 struct _GtkScaleButtonPrivate
96 GtkOrientation orientation;
106 GtkAdjustment *adjustment; /* needed because it must be settable in init() */
109 static GObject* gtk_scale_button_constructor (GType type,
110 guint n_construct_properties,
111 GObjectConstructParam *construct_params);
112 static void gtk_scale_button_dispose (GObject *object);
113 static void gtk_scale_button_finalize (GObject *object);
114 static void gtk_scale_button_set_property (GObject *object,
118 static void gtk_scale_button_get_property (GObject *object,
122 static void gtk_scale_button_set_orientation_private (GtkScaleButton *button,
123 GtkOrientation orientation);
124 static gboolean gtk_scale_button_scroll (GtkWidget *widget,
125 GdkEventScroll *event);
126 static void gtk_scale_button_screen_changed (GtkWidget *widget,
127 GdkScreen *previous_screen);
128 static gboolean gtk_scale_button_press (GtkWidget *widget,
129 GdkEventButton *event);
130 static gboolean gtk_scale_button_key_release (GtkWidget *widget,
132 static void gtk_scale_button_popup (GtkWidget *widget);
133 static void gtk_scale_button_popdown (GtkWidget *widget);
134 static gboolean cb_dock_button_press (GtkWidget *widget,
135 GdkEventButton *event,
137 static gboolean cb_dock_key_release (GtkWidget *widget,
140 static gboolean cb_button_press (GtkWidget *widget,
141 GdkEventButton *event,
143 static gboolean cb_button_release (GtkWidget *widget,
144 GdkEventButton *event,
146 static void cb_dock_grab_notify (GtkWidget *widget,
147 gboolean was_grabbed,
149 static gboolean cb_dock_grab_broken_event (GtkWidget *widget,
150 gboolean was_grabbed,
152 static void cb_scale_grab_notify (GtkWidget *widget,
153 gboolean was_grabbed,
155 static void gtk_scale_button_update_icon (GtkScaleButton *button);
156 static void gtk_scale_button_scale_value_changed(GtkRange *range);
158 /* see below for scale definitions */
159 static GtkWidget *gtk_scale_button_scale_new (GtkScaleButton *button);
161 G_DEFINE_TYPE_WITH_CODE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON,
162 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
165 static guint signals[LAST_SIGNAL] = { 0, };
168 gtk_scale_button_class_init (GtkScaleButtonClass *klass)
170 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
171 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
172 GtkBindingSet *binding_set;
174 g_type_class_add_private (klass, sizeof (GtkScaleButtonPrivate));
176 gobject_class->constructor = gtk_scale_button_constructor;
177 gobject_class->finalize = gtk_scale_button_finalize;
178 gobject_class->dispose = gtk_scale_button_dispose;
179 gobject_class->set_property = gtk_scale_button_set_property;
180 gobject_class->get_property = gtk_scale_button_get_property;
182 widget_class->button_press_event = gtk_scale_button_press;
183 widget_class->key_release_event = gtk_scale_button_key_release;
184 widget_class->scroll_event = gtk_scale_button_scroll;
185 widget_class->screen_changed = gtk_scale_button_screen_changed;
188 * GtkScaleButton:orientation:
190 * The orientation of the #GtkScaleButton's popup window.
192 * Note that since GTK+ 2.16, #GtkScaleButton implements the
193 * #GtkOrientable interface which has its own @orientation
194 * property. However we redefine the property here in order to
195 * override its default horizontal orientation.
199 g_object_class_override_property (gobject_class,
203 g_object_class_install_property (gobject_class,
205 g_param_spec_double ("value",
207 P_("The value of the scale"),
211 GTK_PARAM_READWRITE));
213 g_object_class_install_property (gobject_class,
215 g_param_spec_enum ("size",
219 GTK_ICON_SIZE_SMALL_TOOLBAR,
220 GTK_PARAM_READWRITE));
222 g_object_class_install_property (gobject_class,
224 g_param_spec_object ("adjustment",
226 P_("The GtkAdjustment that contains the current value of this scale button object"),
228 GTK_PARAM_READWRITE));
231 * GtkScaleButton:icons:
233 * The names of the icons to be used by the scale button.
234 * The first item in the array will be used in the button
235 * when the current value is the lowest value, the second
236 * item for the highest value. All the subsequent icons will
237 * be used for all the other values, spread evenly over the
240 * If there's only one icon name in the @icons array, it will
241 * be used for all the values. If only two icon names are in
242 * the @icons array, the first one will be used for the bottom
243 * 50% of the scale, and the second one for the top 50%.
245 * It is recommended to use at least 3 icons so that the
246 * #GtkScaleButton reflects the current value of the scale
247 * better for the users.
251 g_object_class_install_property (gobject_class,
253 g_param_spec_boxed ("icons",
255 P_("List of icon names"),
257 GTK_PARAM_READWRITE));
260 * GtkScaleButton::value-changed:
261 * @button: the object which received the signal
262 * @value: the new value
264 * The ::value-changed signal is emitted when the value field has
269 signals[VALUE_CHANGED] =
270 g_signal_new (I_("value-changed"),
271 G_TYPE_FROM_CLASS (klass),
273 G_STRUCT_OFFSET (GtkScaleButtonClass, value_changed),
275 _gtk_marshal_VOID__DOUBLE,
276 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
279 * GtkScaleButton::popup:
280 * @button: the object which received the signal
282 * The ::popup signal is a
283 * <link linkend="keybinding-signals">keybinding signal</link>
284 * which gets emitted to popup the scale widget.
286 * The default bindings for this signal are Space, Enter and Return.
291 g_signal_new_class_handler (I_("popup"),
292 G_OBJECT_CLASS_TYPE (klass),
293 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
294 G_CALLBACK (gtk_scale_button_popup),
296 g_cclosure_marshal_VOID__VOID,
300 * GtkScaleButton::popdown:
301 * @button: the object which received the signal
303 * The ::popdown signal is a
304 * <link linkend="keybinding-signals">keybinding signal</link>
305 * which gets emitted to popdown the scale widget.
307 * The default binding for this signal is Escape.
312 g_signal_new_class_handler (I_("popdown"),
313 G_OBJECT_CLASS_TYPE (klass),
314 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
315 G_CALLBACK (gtk_scale_button_popdown),
317 g_cclosure_marshal_VOID__VOID,
321 binding_set = gtk_binding_set_by_class (widget_class);
323 gtk_binding_entry_add_signal (binding_set, GDK_space, 0,
325 gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, 0,
327 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
329 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
331 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
333 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
338 gtk_scale_button_init (GtkScaleButton *button)
340 GtkScaleButtonPrivate *priv;
343 button->priv = priv = GET_PRIVATE (button);
345 priv->timeout = FALSE;
347 priv->click_timeout = CLICK_TIMEOUT;
348 priv->orientation = GTK_ORIENTATION_VERTICAL;
350 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
351 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
354 priv->image = gtk_image_new ();
355 gtk_container_add (GTK_CONTAINER (button), priv->image);
356 gtk_widget_show_all (priv->image);
359 priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
360 gtk_widget_set_name (priv->dock, "gtk-scalebutton-popup-window");
361 g_signal_connect (priv->dock, "button-press-event",
362 G_CALLBACK (cb_dock_button_press), button);
363 g_signal_connect (priv->dock, "key-release-event",
364 G_CALLBACK (cb_dock_key_release), button);
365 g_signal_connect (priv->dock, "grab-notify",
366 G_CALLBACK (cb_dock_grab_notify), button);
367 g_signal_connect (priv->dock, "grab-broken-event",
368 G_CALLBACK (cb_dock_grab_broken_event), button);
369 gtk_window_set_decorated (GTK_WINDOW (priv->dock), FALSE);
372 frame = gtk_frame_new (NULL);
373 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
374 gtk_container_add (GTK_CONTAINER (priv->dock), frame);
376 /* box for scale and +/- buttons */
377 priv->box = gtk_vbox_new (FALSE, 0);
378 gtk_container_add (GTK_CONTAINER (frame), priv->box);
381 button->plus_button = gtk_button_new_with_label ("+");
382 gtk_button_set_relief (GTK_BUTTON (button->plus_button), GTK_RELIEF_NONE);
383 g_signal_connect (button->plus_button, "button-press-event",
384 G_CALLBACK (cb_button_press), button);
385 g_signal_connect (button->plus_button, "button-release-event",
386 G_CALLBACK (cb_button_release), button);
387 gtk_box_pack_start (GTK_BOX (priv->box), button->plus_button, FALSE, FALSE, 0);
390 button->minus_button = gtk_button_new_with_label ("-");
391 gtk_button_set_relief (GTK_BUTTON (button->minus_button), GTK_RELIEF_NONE);
392 g_signal_connect (button->minus_button, "button-press-event",
393 G_CALLBACK (cb_button_press), button);
394 g_signal_connect (button->minus_button, "button-release-event",
395 G_CALLBACK (cb_button_release), button);
396 gtk_box_pack_end (GTK_BOX (priv->box), button->minus_button, FALSE, FALSE, 0);
398 priv->adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 100.0, 2, 20, 0));
399 g_object_ref_sink (priv->adjustment);
402 priv->scale = gtk_scale_button_scale_new (button);
403 gtk_container_add (GTK_CONTAINER (priv->box), priv->scale);
407 gtk_scale_button_constructor (GType type,
408 guint n_construct_properties,
409 GObjectConstructParam *construct_params)
412 GtkScaleButton *button;
413 GtkScaleButtonPrivate *priv;
415 object = G_OBJECT_CLASS (gtk_scale_button_parent_class)->constructor (type, n_construct_properties, construct_params);
417 button = GTK_SCALE_BUTTON (object);
421 /* set button text and size */
422 priv->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
423 gtk_scale_button_update_icon (button);
429 gtk_scale_button_set_property (GObject *object,
434 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
438 case PROP_ORIENTATION:
439 gtk_scale_button_set_orientation_private (button, g_value_get_enum (value));
442 gtk_scale_button_set_value (button, g_value_get_double (value));
447 size = g_value_get_enum (value);
448 if (button->priv->size != size)
450 button->priv->size = size;
451 gtk_scale_button_update_icon (button);
455 case PROP_ADJUSTMENT:
456 gtk_scale_button_set_adjustment (button, g_value_get_object (value));
459 gtk_scale_button_set_icons (button,
460 (const gchar **)g_value_get_boxed (value));
463 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
469 gtk_scale_button_get_property (GObject *object,
474 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
475 GtkScaleButtonPrivate *priv = button->priv;
479 case PROP_ORIENTATION:
480 g_value_set_enum (value, priv->orientation);
483 g_value_set_double (value, gtk_scale_button_get_value (button));
486 g_value_set_enum (value, priv->size);
488 case PROP_ADJUSTMENT:
489 g_value_set_object (value, gtk_scale_button_get_adjustment (button));
492 g_value_set_boxed (value, priv->icon_list);
495 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
501 gtk_scale_button_finalize (GObject *object)
503 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
504 GtkScaleButtonPrivate *priv = button->priv;
508 g_strfreev (priv->icon_list);
509 priv->icon_list = NULL;
512 if (priv->adjustment)
514 g_object_unref (priv->adjustment);
515 priv->adjustment = NULL;
518 G_OBJECT_CLASS (gtk_scale_button_parent_class)->finalize (object);
522 gtk_scale_button_dispose (GObject *object)
524 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
525 GtkScaleButtonPrivate *priv = button->priv;
529 gtk_widget_destroy (priv->dock);
533 if (priv->click_id != 0)
535 g_source_remove (priv->click_id);
539 G_OBJECT_CLASS (gtk_scale_button_parent_class)->dispose (object);
543 * gtk_scale_button_new:
544 * @size: a stock icon size
545 * @min: the minimum value of the scale (usually 0)
546 * @max: the maximum value of the scale (usually 100)
547 * @step: the stepping of value when a scroll-wheel event,
548 * or up/down arrow event occurs (usually 2)
549 * @icons: a %NULL-terminated array of icon names, or %NULL if
550 * you want to set the list later with gtk_scale_button_set_icons()
552 * Creates a #GtkScaleButton, with a range between @min and @max, with
553 * a stepping of @step.
555 * Return value: a new #GtkScaleButton
560 gtk_scale_button_new (GtkIconSize size,
566 GtkScaleButton *button;
569 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
571 button = g_object_new (GTK_TYPE_SCALE_BUTTON,
577 return GTK_WIDGET (button);
581 * gtk_scale_button_get_value:
582 * @button: a #GtkScaleButton
584 * Gets the current value of the scale button.
586 * Return value: current value of the scale button
591 gtk_scale_button_get_value (GtkScaleButton * button)
593 GtkScaleButtonPrivate *priv;
595 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), 0);
599 return gtk_adjustment_get_value (priv->adjustment);
603 * gtk_scale_button_set_value:
604 * @button: a #GtkScaleButton
605 * @value: new value of the scale button
607 * Sets the current value of the scale; if the value is outside
608 * the minimum or maximum range values, it will be clamped to fit
609 * inside them. The scale button emits the #GtkScaleButton::value-changed
610 * signal if the value changes.
615 gtk_scale_button_set_value (GtkScaleButton *button,
618 GtkScaleButtonPrivate *priv;
620 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
624 gtk_range_set_value (GTK_RANGE (priv->scale), value);
628 * gtk_scale_button_set_icons:
629 * @button: a #GtkScaleButton
630 * @icons: a %NULL-terminated array of icon names
632 * Sets the icons to be used by the scale button.
633 * For details, see the #GtkScaleButton:icons property.
638 gtk_scale_button_set_icons (GtkScaleButton *button,
641 GtkScaleButtonPrivate *priv;
644 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
648 tmp = priv->icon_list;
649 priv->icon_list = g_strdupv ((gchar **) icons);
651 gtk_scale_button_update_icon (button);
653 g_object_notify (G_OBJECT (button), "icons");
657 * gtk_scale_button_get_adjustment:
658 * @button: a #GtkScaleButton
660 * Gets the #GtkAdjustment associated with the #GtkScaleButton's scale.
661 * See gtk_range_get_adjustment() for details.
663 * Returns: the adjustment associated with the scale
668 gtk_scale_button_get_adjustment (GtkScaleButton *button)
670 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
672 return button->priv->adjustment;
676 * gtk_scale_button_set_adjustment:
677 * @button: a #GtkScaleButton
678 * @adjustment: a #GtkAdjustment
680 * Sets the #GtkAdjustment to be used as a model
681 * for the #GtkScaleButton's scale.
682 * See gtk_range_set_adjustment() for details.
687 gtk_scale_button_set_adjustment (GtkScaleButton *button,
688 GtkAdjustment *adjustment)
690 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
692 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
694 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
696 if (button->priv->adjustment != adjustment)
698 if (button->priv->adjustment)
699 g_object_unref (button->priv->adjustment);
700 button->priv->adjustment = g_object_ref_sink (adjustment);
702 if (button->priv->scale)
703 gtk_range_set_adjustment (GTK_RANGE (button->priv->scale), adjustment);
705 g_object_notify (G_OBJECT (button), "adjustment");
710 * gtk_scale_button_get_orientation:
711 * @button: a #GtkScaleButton
713 * Gets the orientation of the #GtkScaleButton's popup window.
715 * Returns: the #GtkScaleButton's orientation.
719 * Deprecated: 2.16: Use gtk_orientable_get_orientation() instead.
722 gtk_scale_button_get_orientation (GtkScaleButton *button)
724 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), GTK_ORIENTATION_VERTICAL);
726 return button->priv->orientation;
730 * gtk_scale_button_set_orientation:
731 * @button: a #GtkScaleButton
732 * @orientation: the new orientation
734 * Sets the orientation of the #GtkScaleButton's popup window.
738 * Deprecated: 2.16: Use gtk_orientable_set_orientation() instead.
741 gtk_scale_button_set_orientation (GtkScaleButton *button,
742 GtkOrientation orientation)
744 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
746 gtk_scale_button_set_orientation_private (button, orientation);
750 * gtk_scale_button_get_plus_button:
751 * @button: a #GtkScaleButton
753 * Retrieves the plus button of the #GtkScaleButton.
755 * Returns: the plus button of the #GtkScaleButton.
760 gtk_scale_button_get_plus_button (GtkScaleButton *button)
762 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
764 return button->plus_button;
768 * gtk_scale_button_get_minus_button:
769 * @button: a #GtkScaleButton
771 * Retrieves the minus button of the #GtkScaleButton.
773 * Returns: the minus button of the #GtkScaleButton.
778 gtk_scale_button_get_minus_button (GtkScaleButton *button)
780 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
782 return button->minus_button;
786 * gtk_scale_button_get_popup:
787 * @button: a #GtkScaleButton
789 * Retrieves the popup of the #GtkScaleButton.
791 * Returns: the popup of the #GtkScaleButton
796 gtk_scale_button_get_popup (GtkScaleButton *button)
798 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
800 return button->priv->dock;
804 gtk_scale_button_set_orientation_private (GtkScaleButton *button,
805 GtkOrientation orientation)
807 GtkScaleButtonPrivate *priv = button->priv;
809 if (orientation != priv->orientation)
811 priv->orientation = orientation;
813 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box),
815 gtk_container_child_set (GTK_CONTAINER (priv->box),
818 orientation == GTK_ORIENTATION_VERTICAL ?
819 GTK_PACK_START : GTK_PACK_END,
821 gtk_container_child_set (GTK_CONTAINER (priv->box),
822 button->minus_button,
824 orientation == GTK_ORIENTATION_VERTICAL ?
825 GTK_PACK_END : GTK_PACK_START,
828 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->scale),
831 if (orientation == GTK_ORIENTATION_VERTICAL)
833 gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
835 gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
839 gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
841 gtk_range_set_inverted (GTK_RANGE (priv->scale), FALSE);
844 /* FIXME: without this, the popup window appears as a square
845 * after changing the orientation
847 gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
849 g_object_notify (G_OBJECT (button), "orientation");
858 gtk_scale_button_scroll (GtkWidget *widget,
859 GdkEventScroll *event)
861 GtkScaleButton *button;
862 GtkScaleButtonPrivate *priv;
866 button = GTK_SCALE_BUTTON (widget);
868 adj = priv->adjustment;
870 if (event->type != GDK_SCROLL)
873 d = gtk_scale_button_get_value (button);
874 if (event->direction == GDK_SCROLL_UP)
876 d += adj->step_increment;
882 d -= adj->step_increment;
886 gtk_scale_button_set_value (button, d);
892 gtk_scale_button_screen_changed (GtkWidget *widget,
893 GdkScreen *previous_screen)
895 GtkScaleButton *button = (GtkScaleButton *) widget;
896 GtkScaleButtonPrivate *priv;
898 GValue value = { 0, };
900 if (gtk_widget_has_screen (widget) == FALSE)
905 screen = gtk_widget_get_screen (widget);
906 g_value_init (&value, G_TYPE_INT);
907 if (gdk_screen_get_setting (screen,
908 "gtk-double-click-time",
911 priv->click_timeout = CLICK_TIMEOUT;
915 priv->click_timeout = g_value_get_int (&value);
919 gtk_scale_popup (GtkWidget *widget,
923 GtkScaleButton *button;
924 GtkScaleButtonPrivate *priv;
926 gint x, y, m, dx, dy, sx, sy, startoff;
931 button = GTK_SCALE_BUTTON (widget);
933 adj = priv->adjustment;
935 display = gtk_widget_get_display (widget);
936 screen = gtk_widget_get_screen (widget);
938 /* position roughly */
939 gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
941 gdk_window_get_origin (widget->window, &x, &y);
942 x += widget->allocation.x;
943 y += widget->allocation.y;
945 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
946 gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
948 gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
950 gtk_widget_show_all (priv->dock);
952 gdk_window_get_origin (priv->dock->window, &dx, &dy);
953 dx += priv->dock->allocation.x;
954 dy += priv->dock->allocation.y;
956 gdk_window_get_origin (priv->scale->window, &sx, &sy);
957 sx += priv->scale->allocation.x;
958 sy += priv->scale->allocation.y;
960 priv->timeout = TRUE;
962 /* position (needs widget to be shown already) */
963 v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
965 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
969 x += (widget->allocation.width - priv->dock->allocation.width) / 2;
971 y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
972 m = priv->scale->allocation.height -
973 GTK_RANGE (priv->scale)->min_slider_size;
981 y += (widget->allocation.height - priv->dock->allocation.height) / 2;
982 x -= GTK_RANGE (priv->scale)->min_slider_size / 2;
983 m = priv->scale->allocation.width -
984 GTK_RANGE (priv->scale)->min_slider_size;
988 /* Make sure the dock stays inside the monitor */
989 if (event->type == GDK_BUTTON_PRESS)
992 GdkEventButton *button_event = (GdkEventButton *) event;
996 d = GTK_WIDGET (priv->dock);
997 monitor = gdk_screen_get_monitor_at_point (screen,
998 button_event->x_root,
999 button_event->y_root);
1000 gdk_screen_get_monitor_geometry (screen, monitor, &rect);
1002 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1003 y += button_event->y;
1005 x += button_event->x;
1009 else if (y + d->allocation.height > rect.height + rect.y)
1010 y = rect.y + rect.height - d->allocation.height;
1014 else if (x + d->allocation.width > rect.width + rect.x)
1015 x = rect.x + rect.width - d->allocation.width;
1018 gtk_window_move (GTK_WINDOW (priv->dock), x, y);
1020 if (event->type == GDK_BUTTON_PRESS)
1021 GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
1024 gtk_grab_add (priv->dock);
1026 if (gdk_pointer_grab (priv->dock->window, TRUE,
1027 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1028 GDK_POINTER_MOTION_MASK, NULL, NULL, time)
1029 != GDK_GRAB_SUCCESS)
1031 gtk_grab_remove (priv->dock);
1032 gtk_widget_hide (priv->dock);
1036 if (gdk_keyboard_grab (priv->dock->window, TRUE, time) != GDK_GRAB_SUCCESS)
1038 gdk_display_pointer_ungrab (display, time);
1039 gtk_grab_remove (priv->dock);
1040 gtk_widget_hide (priv->dock);
1044 gtk_widget_grab_focus (priv->dock);
1046 if (event->type == GDK_BUTTON_PRESS)
1049 GdkEventButton *button_event = (GdkEventButton *) event;
1051 /* forward event to the slider */
1052 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1053 e->window = priv->scale->window;
1055 /* position: the X position isn't relevant, halfway will work just fine.
1056 * The vertical position should be *exactly* in the middle of the slider
1057 * of the scale; if we don't do that correctly, it'll move from its current
1058 * position, which means a position change on-click, which is bad.
1060 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1062 e->x = priv->scale->allocation.width / 2;
1063 m = priv->scale->allocation.height -
1064 GTK_RANGE (priv->scale)->min_slider_size;
1065 e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1069 e->y = priv->scale->allocation.height / 2;
1070 m = priv->scale->allocation.width -
1071 GTK_RANGE (priv->scale)->min_slider_size;
1072 e->x = (v * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1075 gtk_widget_event (priv->scale, (GdkEvent *) e);
1076 e->window = button_event->window;
1077 gdk_event_free ((GdkEvent *) e);
1080 gtk_widget_grab_focus (priv->scale);
1082 priv->pop_time = time;
1088 gtk_scale_button_press (GtkWidget *widget,
1089 GdkEventButton *event)
1091 return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
1095 gtk_scale_button_popup (GtkWidget *widget)
1099 ev = gdk_event_new (GDK_KEY_RELEASE);
1100 gtk_scale_popup (widget, ev, GDK_CURRENT_TIME);
1101 gdk_event_free (ev);
1105 gtk_scale_button_key_release (GtkWidget *widget,
1108 return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
1111 /* This is called when the grab is broken for
1112 * either the dock, or the scale itself */
1114 gtk_scale_button_grab_notify (GtkScaleButton *button,
1115 gboolean was_grabbed)
1117 GdkDisplay *display;
1118 GtkScaleButtonPrivate *priv;
1120 if (was_grabbed != FALSE)
1123 priv = button->priv;
1125 if (!GTK_WIDGET_HAS_GRAB (priv->dock))
1128 if (gtk_widget_is_ancestor (gtk_grab_get_current (), priv->dock))
1131 display = gtk_widget_get_display (priv->dock);
1132 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1133 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1134 gtk_grab_remove (priv->dock);
1137 gtk_widget_hide (priv->dock);
1138 priv->timeout = FALSE;
1142 * +/- button callbacks.
1146 cb_button_timeout (gpointer user_data)
1148 GtkScaleButton *button;
1149 GtkScaleButtonPrivate *priv;
1152 gboolean res = TRUE;
1154 button = GTK_SCALE_BUTTON (user_data);
1155 priv = button->priv;
1157 if (priv->click_id == 0)
1160 adj = priv->adjustment;
1162 val = gtk_scale_button_get_value (button);
1163 val += priv->direction;
1164 if (val <= adj->lower)
1169 else if (val > adj->upper)
1174 gtk_scale_button_set_value (button, val);
1178 g_source_remove (priv->click_id);
1186 cb_button_press (GtkWidget *widget,
1187 GdkEventButton *event,
1190 GtkScaleButton *button;
1191 GtkScaleButtonPrivate *priv;
1194 button = GTK_SCALE_BUTTON (user_data);
1195 priv = button->priv;
1196 adj = priv->adjustment;
1198 if (priv->click_id != 0)
1199 g_source_remove (priv->click_id);
1201 if (widget == button->plus_button)
1202 priv->direction = fabs (adj->page_increment);
1204 priv->direction = - fabs (adj->page_increment);
1206 priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1209 cb_button_timeout (button);
1215 cb_button_release (GtkWidget *widget,
1216 GdkEventButton *event,
1219 GtkScaleButton *button;
1220 GtkScaleButtonPrivate *priv;
1222 button = GTK_SCALE_BUTTON (user_data);
1223 priv = button->priv;
1225 if (priv->click_id != 0)
1227 g_source_remove (priv->click_id);
1235 cb_dock_grab_notify (GtkWidget *widget,
1236 gboolean was_grabbed,
1239 GtkScaleButton *button = (GtkScaleButton *) user_data;
1241 gtk_scale_button_grab_notify (button, was_grabbed);
1245 cb_dock_grab_broken_event (GtkWidget *widget,
1246 gboolean was_grabbed,
1249 GtkScaleButton *button = (GtkScaleButton *) user_data;
1251 gtk_scale_button_grab_notify (button, FALSE);
1261 gtk_scale_button_release_grab (GtkScaleButton *button,
1262 GdkEventButton *event)
1265 GdkDisplay *display;
1266 GtkScaleButtonPrivate *priv;
1268 priv = button->priv;
1271 display = gtk_widget_get_display (GTK_WIDGET (button));
1272 gdk_display_keyboard_ungrab (display, event->time);
1273 gdk_display_pointer_ungrab (display, event->time);
1274 gtk_grab_remove (priv->dock);
1277 gtk_widget_hide (priv->dock);
1278 priv->timeout = FALSE;
1280 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1281 e->window = GTK_WIDGET (button)->window;
1282 e->type = GDK_BUTTON_RELEASE;
1283 gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1284 e->window = event->window;
1285 gdk_event_free ((GdkEvent *) e);
1289 cb_dock_button_press (GtkWidget *widget,
1290 GdkEventButton *event,
1293 GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1295 if (event->type == GDK_BUTTON_PRESS)
1297 gtk_scale_button_release_grab (button, event);
1305 gtk_scale_button_popdown (GtkWidget *widget)
1307 GtkScaleButton *button;
1308 GtkScaleButtonPrivate *priv;
1309 GdkDisplay *display;
1311 button = GTK_SCALE_BUTTON (widget);
1312 priv = button->priv;
1315 display = gtk_widget_get_display (widget);
1316 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1317 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1318 gtk_grab_remove (priv->dock);
1321 gtk_widget_hide (priv->dock);
1322 priv->timeout = FALSE;
1326 cb_dock_key_release (GtkWidget *widget,
1330 if (event->keyval == GDK_Escape)
1332 gtk_scale_button_popdown (GTK_WIDGET (user_data));
1336 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
1338 /* The popup hasn't managed the event, pass onto the button */
1339 gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
1346 cb_scale_grab_notify (GtkWidget *widget,
1347 gboolean was_grabbed,
1350 GtkScaleButton *button = (GtkScaleButton *) user_data;
1352 gtk_scale_button_grab_notify (button, was_grabbed);
1359 #define GTK_TYPE_SCALE_BUTTON_SCALE (_gtk_scale_button_scale_get_type ())
1360 #define GTK_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, GtkScaleButtonScale))
1361 #define GTK_IS_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_SCALE))
1363 typedef struct _GtkScaleButtonScale
1365 GtkScale parent_instance;
1366 GtkScaleButton *button;
1367 } GtkScaleButtonScale;
1369 typedef struct _GtkScaleButtonScaleClass
1371 GtkScaleClass parent_class;
1372 } GtkScaleButtonScaleClass;
1374 static gboolean gtk_scale_button_scale_press (GtkWidget *widget,
1375 GdkEventButton *event);
1376 static gboolean gtk_scale_button_scale_release (GtkWidget *widget,
1377 GdkEventButton *event);
1379 G_DEFINE_TYPE (GtkScaleButtonScale, _gtk_scale_button_scale, GTK_TYPE_SCALE)
1382 _gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass)
1384 GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1385 GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1387 gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1388 gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1389 gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1393 _gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
1398 gtk_scale_button_scale_new (GtkScaleButton *button)
1400 GtkScaleButtonPrivate *priv = button->priv;
1401 GtkScaleButtonScale *scale;
1403 scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE,
1404 "orientation", priv->orientation,
1405 "adjustment", priv->adjustment,
1406 "draw-value", FALSE,
1409 scale->button = button;
1411 g_signal_connect (scale, "grab-notify",
1412 G_CALLBACK (cb_scale_grab_notify), button);
1414 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1416 gtk_widget_set_size_request (GTK_WIDGET (scale), -1, SCALE_SIZE);
1417 gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
1421 gtk_widget_set_size_request (GTK_WIDGET (scale), SCALE_SIZE, -1);
1422 gtk_range_set_inverted (GTK_RANGE (scale), FALSE);
1425 return GTK_WIDGET (scale);
1429 gtk_scale_button_scale_press (GtkWidget *widget,
1430 GdkEventButton *event)
1432 GtkScaleButtonPrivate *priv = GTK_SCALE_BUTTON_SCALE (widget)->button->priv;
1434 /* the scale will grab input; if we have input grabbed, all goes
1435 * horribly wrong, so let's not do that.
1437 gtk_grab_remove (priv->dock);
1439 return GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_press_event (widget, event);
1443 gtk_scale_button_scale_release (GtkWidget *widget,
1444 GdkEventButton *event)
1446 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (widget)->button;
1449 if (button->priv->timeout)
1451 /* if we did a quick click, leave the window open; else, hide it */
1452 if (event->time > button->priv->pop_time + button->priv->click_timeout)
1455 gtk_scale_button_release_grab (button, event);
1456 GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1461 button->priv->timeout = FALSE;
1464 res = GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1466 /* the scale will release input; right after that, we *have to* grab
1467 * it back so we can catch out-of-scale clicks and hide the popup,
1468 * so I basically want a g_signal_connect_after_always(), but I can't
1469 * find that, so we do this complex 'first-call-parent-then-do-actual-
1472 gtk_grab_add (button->priv->dock);
1478 gtk_scale_button_update_icon (GtkScaleButton *button)
1480 GtkScaleButtonPrivate *priv;
1487 priv = button->priv;
1489 if (!priv->icon_list || priv->icon_list[0] == '\0')
1491 gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1492 GTK_STOCK_MISSING_IMAGE,
1497 num_icons = g_strv_length (priv->icon_list);
1499 /* The 1-icon special case */
1502 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1508 range = GTK_RANGE (priv->scale);
1509 adj = priv->adjustment;
1510 value = gtk_scale_button_get_value (button);
1512 /* The 2-icons special case */
1516 limit = (adj->upper - adj->lower) / 2 + adj->lower;
1518 name = priv->icon_list[0];
1520 name = priv->icon_list[1];
1522 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1528 /* With 3 or more icons */
1529 if (value == adj->lower)
1531 name = priv->icon_list[0];
1533 else if (value == adj->upper)
1535 name = priv->icon_list[1];
1542 step = (adj->upper - adj->lower) / (num_icons - 2);
1543 i = (guint) ((value - adj->lower) / step) + 2;
1544 g_assert (i < num_icons);
1545 name = priv->icon_list[i];
1548 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1554 gtk_scale_button_scale_value_changed (GtkRange *range)
1556 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (range)->button;
1559 value = gtk_range_get_value (range);
1561 gtk_scale_button_update_icon (button);
1563 g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1564 g_object_notify (G_OBJECT (button), "value");
1567 #define __GTK_SCALE_BUTTON_C__
1568 #include "gtkaliasdef.c"