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 gboolean gtk_scale_button_scroll (GtkWidget *widget,
123 GdkEventScroll *event);
124 static void gtk_scale_button_screen_changed (GtkWidget *widget,
125 GdkScreen *previous_screen);
126 static gboolean gtk_scale_button_press (GtkWidget *widget,
127 GdkEventButton *event);
128 static gboolean gtk_scale_button_key_release (GtkWidget *widget,
130 static void gtk_scale_button_popup (GtkWidget *widget);
131 static void gtk_scale_button_popdown (GtkWidget *widget);
132 static gboolean cb_dock_button_press (GtkWidget *widget,
133 GdkEventButton *event,
135 static gboolean cb_dock_key_release (GtkWidget *widget,
138 static gboolean cb_button_press (GtkWidget *widget,
139 GdkEventButton *event,
141 static gboolean cb_button_release (GtkWidget *widget,
142 GdkEventButton *event,
144 static void cb_dock_grab_notify (GtkWidget *widget,
145 gboolean was_grabbed,
147 static gboolean cb_dock_grab_broken_event (GtkWidget *widget,
148 gboolean was_grabbed,
150 static void cb_scale_grab_notify (GtkWidget *widget,
151 gboolean was_grabbed,
153 static void gtk_scale_button_update_icon (GtkScaleButton *button);
154 static void gtk_scale_button_scale_value_changed(GtkRange *range);
156 /* see below for scale definitions */
157 static GtkWidget *gtk_scale_button_scale_new (GtkScaleButton *button);
159 G_DEFINE_TYPE_WITH_CODE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON,
160 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
163 static guint signals[LAST_SIGNAL] = { 0, };
166 gtk_scale_button_class_init (GtkScaleButtonClass *klass)
168 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
169 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
170 GtkBindingSet *binding_set;
172 g_type_class_add_private (klass, sizeof (GtkScaleButtonPrivate));
174 gobject_class->constructor = gtk_scale_button_constructor;
175 gobject_class->finalize = gtk_scale_button_finalize;
176 gobject_class->dispose = gtk_scale_button_dispose;
177 gobject_class->set_property = gtk_scale_button_set_property;
178 gobject_class->get_property = gtk_scale_button_get_property;
180 widget_class->button_press_event = gtk_scale_button_press;
181 widget_class->key_release_event = gtk_scale_button_key_release;
182 widget_class->scroll_event = gtk_scale_button_scroll;
183 widget_class->screen_changed = gtk_scale_button_screen_changed;
186 * GtkScaleButton:orientation:
188 * The orientation of the #GtkScaleButton's popup window.
190 * Note that since GTK+ 2.16, #GtkScaleButton implements the
191 * #GtkOrientable interface which has its own @orientation
192 * property. However we redefine the property here in order to
193 * override its default horizontal orientation.
197 g_object_class_override_property (gobject_class,
201 g_object_class_install_property (gobject_class,
203 g_param_spec_double ("value",
205 P_("The value of the scale"),
209 GTK_PARAM_READWRITE));
211 g_object_class_install_property (gobject_class,
213 g_param_spec_enum ("size",
217 GTK_ICON_SIZE_SMALL_TOOLBAR,
218 GTK_PARAM_READWRITE));
220 g_object_class_install_property (gobject_class,
222 g_param_spec_object ("adjustment",
224 P_("The GtkAdjustment that contains the current value of this scale button object"),
226 GTK_PARAM_READWRITE));
229 * GtkScaleButton:icons:
231 * The names of the icons to be used by the scale button.
232 * The first item in the array will be used in the button
233 * when the current value is the lowest value, the second
234 * item for the highest value. All the subsequent icons will
235 * be used for all the other values, spread evenly over the
238 * If there's only one icon name in the @icons array, it will
239 * be used for all the values. If only two icon names are in
240 * the @icons array, the first one will be used for the bottom
241 * 50% of the scale, and the second one for the top 50%.
243 * It is recommended to use at least 3 icons so that the
244 * #GtkScaleButton reflects the current value of the scale
245 * better for the users.
249 g_object_class_install_property (gobject_class,
251 g_param_spec_boxed ("icons",
253 P_("List of icon names"),
255 GTK_PARAM_READWRITE));
258 * GtkScaleButton::value-changed:
259 * @button: the object which received the signal
260 * @value: the new value
262 * The ::value-changed signal is emitted when the value field has
267 signals[VALUE_CHANGED] =
268 g_signal_new (I_("value-changed"),
269 G_TYPE_FROM_CLASS (klass),
271 G_STRUCT_OFFSET (GtkScaleButtonClass, value_changed),
273 _gtk_marshal_VOID__DOUBLE,
274 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
277 * GtkScaleButton::popup:
278 * @button: the object which received the signal
280 * The ::popup signal is a
281 * <link linkend="keybinding-signals">keybinding signal</link>
282 * which gets emitted to popup the scale widget.
284 * The default bindings for this signal are Space, Enter and Return.
289 g_signal_new_class_handler (I_("popup"),
290 G_OBJECT_CLASS_TYPE (klass),
291 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
292 G_CALLBACK (gtk_scale_button_popup),
294 g_cclosure_marshal_VOID__VOID,
298 * GtkScaleButton::popdown:
299 * @button: the object which received the signal
301 * The ::popdown signal is a
302 * <link linkend="keybinding-signals">keybinding signal</link>
303 * which gets emitted to popdown the scale widget.
305 * The default binding for this signal is Escape.
310 g_signal_new_class_handler (I_("popdown"),
311 G_OBJECT_CLASS_TYPE (klass),
312 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
313 G_CALLBACK (gtk_scale_button_popdown),
315 g_cclosure_marshal_VOID__VOID,
319 binding_set = gtk_binding_set_by_class (widget_class);
321 gtk_binding_entry_add_signal (binding_set, GDK_space, 0,
323 gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, 0,
325 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
327 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
329 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
331 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
336 gtk_scale_button_init (GtkScaleButton *button)
338 GtkScaleButtonPrivate *priv;
341 button->priv = priv = GET_PRIVATE (button);
343 priv->timeout = FALSE;
345 priv->click_timeout = CLICK_TIMEOUT;
346 priv->orientation = GTK_ORIENTATION_VERTICAL;
348 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
349 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
352 priv->image = gtk_image_new ();
353 gtk_container_add (GTK_CONTAINER (button), priv->image);
354 gtk_widget_show_all (priv->image);
357 priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
358 gtk_widget_set_name (priv->dock, "gtk-scalebutton-popup-window");
359 g_signal_connect (priv->dock, "button-press-event",
360 G_CALLBACK (cb_dock_button_press), button);
361 g_signal_connect (priv->dock, "key-release-event",
362 G_CALLBACK (cb_dock_key_release), button);
363 g_signal_connect (priv->dock, "grab-notify",
364 G_CALLBACK (cb_dock_grab_notify), button);
365 g_signal_connect (priv->dock, "grab-broken-event",
366 G_CALLBACK (cb_dock_grab_broken_event), button);
367 gtk_window_set_decorated (GTK_WINDOW (priv->dock), FALSE);
370 frame = gtk_frame_new (NULL);
371 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
372 gtk_container_add (GTK_CONTAINER (priv->dock), frame);
374 /* box for scale and +/- buttons */
375 priv->box = gtk_vbox_new (FALSE, 0);
376 gtk_container_add (GTK_CONTAINER (frame), priv->box);
379 button->plus_button = gtk_button_new_with_label ("+");
380 gtk_button_set_relief (GTK_BUTTON (button->plus_button), GTK_RELIEF_NONE);
381 g_signal_connect (button->plus_button, "button-press-event",
382 G_CALLBACK (cb_button_press), button);
383 g_signal_connect (button->plus_button, "button-release-event",
384 G_CALLBACK (cb_button_release), button);
385 gtk_box_pack_start (GTK_BOX (priv->box), button->plus_button, FALSE, FALSE, 0);
388 button->minus_button = gtk_button_new_with_label ("-");
389 gtk_button_set_relief (GTK_BUTTON (button->minus_button), GTK_RELIEF_NONE);
390 g_signal_connect (button->minus_button, "button-press-event",
391 G_CALLBACK (cb_button_press), button);
392 g_signal_connect (button->minus_button, "button-release-event",
393 G_CALLBACK (cb_button_release), button);
394 gtk_box_pack_end (GTK_BOX (priv->box), button->minus_button, FALSE, FALSE, 0);
396 priv->adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 100.0, 2, 20, 0));
397 g_object_ref_sink (priv->adjustment);
400 priv->scale = gtk_scale_button_scale_new (button);
401 gtk_container_add (GTK_CONTAINER (priv->box), priv->scale);
405 gtk_scale_button_constructor (GType type,
406 guint n_construct_properties,
407 GObjectConstructParam *construct_params)
410 GtkScaleButton *button;
411 GtkScaleButtonPrivate *priv;
413 object = G_OBJECT_CLASS (gtk_scale_button_parent_class)->constructor (type, n_construct_properties, construct_params);
415 button = GTK_SCALE_BUTTON (object);
419 /* set button text and size */
420 priv->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
421 gtk_scale_button_update_icon (button);
427 gtk_scale_button_set_property (GObject *object,
432 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
436 case PROP_ORIENTATION:
437 gtk_scale_button_set_orientation (button, g_value_get_enum (value));
440 gtk_scale_button_set_value (button, g_value_get_double (value));
445 size = g_value_get_enum (value);
446 if (button->priv->size != size)
448 button->priv->size = size;
449 gtk_scale_button_update_icon (button);
453 case PROP_ADJUSTMENT:
454 gtk_scale_button_set_adjustment (button, g_value_get_object (value));
457 gtk_scale_button_set_icons (button,
458 (const gchar **)g_value_get_boxed (value));
461 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
467 gtk_scale_button_get_property (GObject *object,
472 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
473 GtkScaleButtonPrivate *priv = button->priv;
477 case PROP_ORIENTATION:
478 g_value_set_enum (value, priv->orientation);
481 g_value_set_double (value, gtk_scale_button_get_value (button));
484 g_value_set_enum (value, priv->size);
486 case PROP_ADJUSTMENT:
487 g_value_set_object (value, gtk_scale_button_get_adjustment (button));
490 g_value_set_boxed (value, priv->icon_list);
493 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
499 gtk_scale_button_finalize (GObject *object)
501 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
502 GtkScaleButtonPrivate *priv = button->priv;
506 g_strfreev (priv->icon_list);
507 priv->icon_list = NULL;
510 if (priv->adjustment)
512 g_object_unref (priv->adjustment);
513 priv->adjustment = NULL;
516 G_OBJECT_CLASS (gtk_scale_button_parent_class)->finalize (object);
520 gtk_scale_button_dispose (GObject *object)
522 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
523 GtkScaleButtonPrivate *priv = button->priv;
527 gtk_widget_destroy (priv->dock);
531 if (priv->click_id != 0)
533 g_source_remove (priv->click_id);
537 G_OBJECT_CLASS (gtk_scale_button_parent_class)->dispose (object);
541 * gtk_scale_button_new:
542 * @size: a stock icon size
543 * @min: the minimum value of the scale (usually 0)
544 * @max: the maximum value of the scale (usually 100)
545 * @step: the stepping of value when a scroll-wheel event,
546 * or up/down arrow event occurs (usually 2)
547 * @icons: a %NULL-terminated array of icon names, or %NULL if
548 * you want to set the list later with gtk_scale_button_set_icons()
550 * Creates a #GtkScaleButton, with a range between @min and @max, with
551 * a stepping of @step.
553 * Return value: a new #GtkScaleButton
558 gtk_scale_button_new (GtkIconSize size,
564 GtkScaleButton *button;
567 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
569 button = g_object_new (GTK_TYPE_SCALE_BUTTON,
575 return GTK_WIDGET (button);
579 * gtk_scale_button_get_value:
580 * @button: a #GtkScaleButton
582 * Gets the current value of the scale button.
584 * Return value: current value of the scale button
589 gtk_scale_button_get_value (GtkScaleButton * button)
591 GtkScaleButtonPrivate *priv;
593 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), 0);
597 return gtk_adjustment_get_value (priv->adjustment);
601 * gtk_scale_button_set_value:
602 * @button: a #GtkScaleButton
603 * @value: new value of the scale button
605 * Sets the current value of the scale; if the value is outside
606 * the minimum or maximum range values, it will be clamped to fit
607 * inside them. The scale button emits the #GtkScaleButton::value-changed
608 * signal if the value changes.
613 gtk_scale_button_set_value (GtkScaleButton *button,
616 GtkScaleButtonPrivate *priv;
618 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
622 gtk_range_set_value (GTK_RANGE (priv->scale), value);
626 * gtk_scale_button_set_icons:
627 * @button: a #GtkScaleButton
628 * @icons: a %NULL-terminated array of icon names
630 * Sets the icons to be used by the scale button.
631 * For details, see the #GtkScaleButton:icons property.
636 gtk_scale_button_set_icons (GtkScaleButton *button,
639 GtkScaleButtonPrivate *priv;
642 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
646 tmp = priv->icon_list;
647 priv->icon_list = g_strdupv ((gchar **) icons);
649 gtk_scale_button_update_icon (button);
651 g_object_notify (G_OBJECT (button), "icons");
655 * gtk_scale_button_get_adjustment:
656 * @button: a #GtkScaleButton
658 * Gets the #GtkAdjustment associated with the #GtkScaleButton's scale.
659 * See gtk_range_get_adjustment() for details.
661 * Returns: the adjustment associated with the scale
666 gtk_scale_button_get_adjustment (GtkScaleButton *button)
668 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
670 return button->priv->adjustment;
674 * gtk_scale_button_set_adjustment:
675 * @button: a #GtkScaleButton
676 * @adjustment: a #GtkAdjustment
678 * Sets the #GtkAdjustment to be used as a model
679 * for the #GtkScaleButton's scale.
680 * See gtk_range_set_adjustment() for details.
685 gtk_scale_button_set_adjustment (GtkScaleButton *button,
686 GtkAdjustment *adjustment)
688 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
690 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
692 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
694 if (button->priv->adjustment != adjustment)
696 if (button->priv->adjustment)
697 g_object_unref (button->priv->adjustment);
698 button->priv->adjustment = g_object_ref_sink (adjustment);
700 if (button->priv->scale)
701 gtk_range_set_adjustment (GTK_RANGE (button->priv->scale), adjustment);
703 g_object_notify (G_OBJECT (button), "adjustment");
708 * gtk_scale_button_get_orientation:
709 * @button: a #GtkScaleButton
711 * Gets the orientation of the #GtkScaleButton's popup window.
713 * Returns: the #GtkScaleButton's orientation.
717 * Deprecated: 2.16: Use gtk_orientable_get_orientation() instead.
720 gtk_scale_button_get_orientation (GtkScaleButton *button)
722 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), GTK_ORIENTATION_VERTICAL);
724 return button->priv->orientation;
728 * gtk_scale_button_set_orientation:
729 * @button: a #GtkScaleButton
730 * @orientation: the new orientation
732 * Sets the orientation of the #GtkScaleButton's popup window.
736 * Deprecated: 2.16: Use gtk_orientable_set_orientation() instead.
739 gtk_scale_button_set_orientation (GtkScaleButton *button,
740 GtkOrientation orientation)
742 GtkScaleButtonPrivate *priv;
744 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
748 if (orientation != priv->orientation)
750 priv->orientation = orientation;
752 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box),
754 gtk_container_child_set (GTK_CONTAINER (priv->box),
757 orientation == GTK_ORIENTATION_VERTICAL ?
758 GTK_PACK_START : GTK_PACK_END,
760 gtk_container_child_set (GTK_CONTAINER (priv->box),
761 button->minus_button,
763 orientation == GTK_ORIENTATION_VERTICAL ?
764 GTK_PACK_END : GTK_PACK_START,
767 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->scale),
770 if (orientation == GTK_ORIENTATION_VERTICAL)
772 gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
774 gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
778 gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
780 gtk_range_set_inverted (GTK_RANGE (priv->scale), FALSE);
783 /* FIXME: without this, the popup window appears as a square
784 * after changing the orientation
786 gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
788 g_object_notify (G_OBJECT (button), "orientation");
793 * gtk_scale_button_get_plus_button:
794 * @button: a #GtkScaleButton
796 * Retrieves the plus button of the #GtkScaleButton.
798 * Returns: the plus button of the #GtkScaleButton.
803 gtk_scale_button_get_plus_button (GtkScaleButton *button)
805 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
807 return button->plus_button;
811 * gtk_scale_button_get_minus_button:
812 * @button: a #GtkScaleButton
814 * Retrieves the minus button of the #GtkScaleButton.
816 * Returns: the minus button of the #GtkScaleButton.
821 gtk_scale_button_get_minus_button (GtkScaleButton *button)
823 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
825 return button->minus_button;
829 * gtk_scale_button_get_popup:
830 * @button: a #GtkScaleButton
832 * Retrieves the popup of the #GtkScaleButton.
834 * Returns: the popup of the #GtkScaleButton
839 gtk_scale_button_get_popup (GtkScaleButton *button)
841 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
843 return button->priv->dock;
851 gtk_scale_button_scroll (GtkWidget *widget,
852 GdkEventScroll *event)
854 GtkScaleButton *button;
855 GtkScaleButtonPrivate *priv;
859 button = GTK_SCALE_BUTTON (widget);
861 adj = priv->adjustment;
863 if (event->type != GDK_SCROLL)
866 d = gtk_scale_button_get_value (button);
867 if (event->direction == GDK_SCROLL_UP)
869 d += adj->step_increment;
875 d -= adj->step_increment;
879 gtk_scale_button_set_value (button, d);
885 gtk_scale_button_screen_changed (GtkWidget *widget,
886 GdkScreen *previous_screen)
888 GtkScaleButton *button = (GtkScaleButton *) widget;
889 GtkScaleButtonPrivate *priv;
891 GValue value = { 0, };
893 if (gtk_widget_has_screen (widget) == FALSE)
898 screen = gtk_widget_get_screen (widget);
899 g_value_init (&value, G_TYPE_INT);
900 if (gdk_screen_get_setting (screen,
901 "gtk-double-click-time",
904 priv->click_timeout = CLICK_TIMEOUT;
908 priv->click_timeout = g_value_get_int (&value);
912 gtk_scale_popup (GtkWidget *widget,
916 GtkScaleButton *button;
917 GtkScaleButtonPrivate *priv;
919 gint x, y, m, dx, dy, sx, sy, startoff;
924 button = GTK_SCALE_BUTTON (widget);
926 adj = priv->adjustment;
928 display = gtk_widget_get_display (widget);
929 screen = gtk_widget_get_screen (widget);
931 /* position roughly */
932 gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
934 gdk_window_get_origin (widget->window, &x, &y);
935 x += widget->allocation.x;
936 y += widget->allocation.y;
938 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
939 gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
941 gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
943 gtk_widget_show_all (priv->dock);
945 gdk_window_get_origin (priv->dock->window, &dx, &dy);
946 dx += priv->dock->allocation.x;
947 dy += priv->dock->allocation.y;
949 gdk_window_get_origin (priv->scale->window, &sx, &sy);
950 sx += priv->scale->allocation.x;
951 sy += priv->scale->allocation.y;
953 priv->timeout = TRUE;
955 /* position (needs widget to be shown already) */
956 v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
958 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
962 x += (widget->allocation.width - priv->dock->allocation.width) / 2;
964 y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
965 m = priv->scale->allocation.height -
966 GTK_RANGE (priv->scale)->min_slider_size;
974 y += (widget->allocation.height - priv->dock->allocation.height) / 2;
975 x -= GTK_RANGE (priv->scale)->min_slider_size / 2;
976 m = priv->scale->allocation.width -
977 GTK_RANGE (priv->scale)->min_slider_size;
981 /* Make sure the dock stays inside the monitor */
982 if (event->type == GDK_BUTTON_PRESS)
985 GdkEventButton *button_event = (GdkEventButton *) event;
989 d = GTK_WIDGET (priv->dock);
990 monitor = gdk_screen_get_monitor_at_point (screen,
991 button_event->x_root,
992 button_event->y_root);
993 gdk_screen_get_monitor_geometry (screen, monitor, &rect);
995 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
996 y += button_event->y;
998 x += button_event->x;
1002 else if (y + d->allocation.height > rect.height + rect.y)
1003 y = rect.y + rect.height - d->allocation.height;
1007 else if (x + d->allocation.width > rect.width + rect.x)
1008 x = rect.x + rect.width - d->allocation.width;
1011 gtk_window_move (GTK_WINDOW (priv->dock), x, y);
1013 if (event->type == GDK_BUTTON_PRESS)
1014 GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
1017 gtk_grab_add (priv->dock);
1019 if (gdk_pointer_grab (priv->dock->window, TRUE,
1020 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1021 GDK_POINTER_MOTION_MASK, NULL, NULL, time)
1022 != GDK_GRAB_SUCCESS)
1024 gtk_grab_remove (priv->dock);
1025 gtk_widget_hide (priv->dock);
1029 if (gdk_keyboard_grab (priv->dock->window, TRUE, time) != GDK_GRAB_SUCCESS)
1031 gdk_display_pointer_ungrab (display, time);
1032 gtk_grab_remove (priv->dock);
1033 gtk_widget_hide (priv->dock);
1037 gtk_widget_grab_focus (priv->dock);
1039 if (event->type == GDK_BUTTON_PRESS)
1042 GdkEventButton *button_event = (GdkEventButton *) event;
1044 /* forward event to the slider */
1045 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1046 e->window = priv->scale->window;
1048 /* position: the X position isn't relevant, halfway will work just fine.
1049 * The vertical position should be *exactly* in the middle of the slider
1050 * of the scale; if we don't do that correctly, it'll move from its current
1051 * position, which means a position change on-click, which is bad.
1053 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1055 e->x = priv->scale->allocation.width / 2;
1056 m = priv->scale->allocation.height -
1057 GTK_RANGE (priv->scale)->min_slider_size;
1058 e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1062 e->y = priv->scale->allocation.height / 2;
1063 m = priv->scale->allocation.width -
1064 GTK_RANGE (priv->scale)->min_slider_size;
1065 e->x = (v * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1068 gtk_widget_event (priv->scale, (GdkEvent *) e);
1069 e->window = button_event->window;
1070 gdk_event_free ((GdkEvent *) e);
1073 gtk_widget_grab_focus (priv->scale);
1075 priv->pop_time = time;
1081 gtk_scale_button_press (GtkWidget *widget,
1082 GdkEventButton *event)
1084 return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
1088 gtk_scale_button_popup (GtkWidget *widget)
1092 ev = gdk_event_new (GDK_KEY_RELEASE);
1093 gtk_scale_popup (widget, ev, GDK_CURRENT_TIME);
1094 gdk_event_free (ev);
1098 gtk_scale_button_key_release (GtkWidget *widget,
1101 return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
1104 /* This is called when the grab is broken for
1105 * either the dock, or the scale itself */
1107 gtk_scale_button_grab_notify (GtkScaleButton *button,
1108 gboolean was_grabbed)
1110 GdkDisplay *display;
1111 GtkScaleButtonPrivate *priv;
1113 if (was_grabbed != FALSE)
1116 priv = button->priv;
1118 if (!GTK_WIDGET_HAS_GRAB (priv->dock))
1121 if (gtk_widget_is_ancestor (gtk_grab_get_current (), priv->dock))
1124 display = gtk_widget_get_display (priv->dock);
1125 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1126 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1127 gtk_grab_remove (priv->dock);
1130 gtk_widget_hide (priv->dock);
1131 priv->timeout = FALSE;
1135 * +/- button callbacks.
1139 cb_button_timeout (gpointer user_data)
1141 GtkScaleButton *button;
1142 GtkScaleButtonPrivate *priv;
1145 gboolean res = TRUE;
1147 button = GTK_SCALE_BUTTON (user_data);
1148 priv = button->priv;
1150 if (priv->click_id == 0)
1153 adj = priv->adjustment;
1155 val = gtk_scale_button_get_value (button);
1156 val += priv->direction;
1157 if (val <= adj->lower)
1162 else if (val > adj->upper)
1167 gtk_scale_button_set_value (button, val);
1171 g_source_remove (priv->click_id);
1179 cb_button_press (GtkWidget *widget,
1180 GdkEventButton *event,
1183 GtkScaleButton *button;
1184 GtkScaleButtonPrivate *priv;
1187 button = GTK_SCALE_BUTTON (user_data);
1188 priv = button->priv;
1189 adj = priv->adjustment;
1191 if (priv->click_id != 0)
1192 g_source_remove (priv->click_id);
1194 if (widget == button->plus_button)
1195 priv->direction = fabs (adj->page_increment);
1197 priv->direction = - fabs (adj->page_increment);
1199 priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1202 cb_button_timeout (button);
1208 cb_button_release (GtkWidget *widget,
1209 GdkEventButton *event,
1212 GtkScaleButton *button;
1213 GtkScaleButtonPrivate *priv;
1215 button = GTK_SCALE_BUTTON (user_data);
1216 priv = button->priv;
1218 if (priv->click_id != 0)
1220 g_source_remove (priv->click_id);
1228 cb_dock_grab_notify (GtkWidget *widget,
1229 gboolean was_grabbed,
1232 GtkScaleButton *button = (GtkScaleButton *) user_data;
1234 gtk_scale_button_grab_notify (button, was_grabbed);
1238 cb_dock_grab_broken_event (GtkWidget *widget,
1239 gboolean was_grabbed,
1242 GtkScaleButton *button = (GtkScaleButton *) user_data;
1244 gtk_scale_button_grab_notify (button, FALSE);
1254 gtk_scale_button_release_grab (GtkScaleButton *button,
1255 GdkEventButton *event)
1258 GdkDisplay *display;
1259 GtkScaleButtonPrivate *priv;
1261 priv = button->priv;
1264 display = gtk_widget_get_display (GTK_WIDGET (button));
1265 gdk_display_keyboard_ungrab (display, event->time);
1266 gdk_display_pointer_ungrab (display, event->time);
1267 gtk_grab_remove (priv->dock);
1270 gtk_widget_hide (priv->dock);
1271 priv->timeout = FALSE;
1273 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1274 e->window = GTK_WIDGET (button)->window;
1275 e->type = GDK_BUTTON_RELEASE;
1276 gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1277 e->window = event->window;
1278 gdk_event_free ((GdkEvent *) e);
1282 cb_dock_button_press (GtkWidget *widget,
1283 GdkEventButton *event,
1286 GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1288 if (event->type == GDK_BUTTON_PRESS)
1290 gtk_scale_button_release_grab (button, event);
1298 gtk_scale_button_popdown (GtkWidget *widget)
1300 GtkScaleButton *button;
1301 GtkScaleButtonPrivate *priv;
1302 GdkDisplay *display;
1304 button = GTK_SCALE_BUTTON (widget);
1305 priv = button->priv;
1308 display = gtk_widget_get_display (widget);
1309 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1310 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1311 gtk_grab_remove (priv->dock);
1314 gtk_widget_hide (priv->dock);
1315 priv->timeout = FALSE;
1319 cb_dock_key_release (GtkWidget *widget,
1323 if (event->keyval == GDK_Escape)
1325 gtk_scale_button_popdown (GTK_WIDGET (user_data));
1329 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
1331 /* The popup hasn't managed the event, pass onto the button */
1332 gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
1339 cb_scale_grab_notify (GtkWidget *widget,
1340 gboolean was_grabbed,
1343 GtkScaleButton *button = (GtkScaleButton *) user_data;
1345 gtk_scale_button_grab_notify (button, was_grabbed);
1352 #define GTK_TYPE_SCALE_BUTTON_SCALE (_gtk_scale_button_scale_get_type ())
1353 #define GTK_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, GtkScaleButtonScale))
1354 #define GTK_IS_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_SCALE))
1356 typedef struct _GtkScaleButtonScale
1358 GtkScale parent_instance;
1359 GtkScaleButton *button;
1360 } GtkScaleButtonScale;
1362 typedef struct _GtkScaleButtonScaleClass
1364 GtkScaleClass parent_class;
1365 } GtkScaleButtonScaleClass;
1367 static gboolean gtk_scale_button_scale_press (GtkWidget *widget,
1368 GdkEventButton *event);
1369 static gboolean gtk_scale_button_scale_release (GtkWidget *widget,
1370 GdkEventButton *event);
1372 G_DEFINE_TYPE (GtkScaleButtonScale, _gtk_scale_button_scale, GTK_TYPE_SCALE)
1375 _gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass)
1377 GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1378 GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1380 gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1381 gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1382 gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1386 _gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
1391 gtk_scale_button_scale_new (GtkScaleButton *button)
1393 GtkScaleButtonPrivate *priv = button->priv;
1394 GtkScaleButtonScale *scale;
1396 scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE,
1397 "orientation", priv->orientation,
1398 "adjustment", priv->adjustment,
1399 "draw-value", FALSE,
1402 scale->button = button;
1404 g_signal_connect (scale, "grab-notify",
1405 G_CALLBACK (cb_scale_grab_notify), button);
1407 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1409 gtk_widget_set_size_request (GTK_WIDGET (scale), -1, SCALE_SIZE);
1410 gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
1414 gtk_widget_set_size_request (GTK_WIDGET (scale), SCALE_SIZE, -1);
1415 gtk_range_set_inverted (GTK_RANGE (scale), FALSE);
1418 return GTK_WIDGET (scale);
1422 gtk_scale_button_scale_press (GtkWidget *widget,
1423 GdkEventButton *event)
1425 GtkScaleButtonPrivate *priv = GTK_SCALE_BUTTON_SCALE (widget)->button->priv;
1427 /* the scale will grab input; if we have input grabbed, all goes
1428 * horribly wrong, so let's not do that.
1430 gtk_grab_remove (priv->dock);
1432 return GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_press_event (widget, event);
1436 gtk_scale_button_scale_release (GtkWidget *widget,
1437 GdkEventButton *event)
1439 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (widget)->button;
1442 if (button->priv->timeout)
1444 /* if we did a quick click, leave the window open; else, hide it */
1445 if (event->time > button->priv->pop_time + button->priv->click_timeout)
1448 gtk_scale_button_release_grab (button, event);
1449 GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1454 button->priv->timeout = FALSE;
1457 res = GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1459 /* the scale will release input; right after that, we *have to* grab
1460 * it back so we can catch out-of-scale clicks and hide the popup,
1461 * so I basically want a g_signal_connect_after_always(), but I can't
1462 * find that, so we do this complex 'first-call-parent-then-do-actual-
1465 gtk_grab_add (button->priv->dock);
1471 gtk_scale_button_update_icon (GtkScaleButton *button)
1473 GtkScaleButtonPrivate *priv;
1480 priv = button->priv;
1482 if (!priv->icon_list || priv->icon_list[0] == '\0')
1484 gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1485 GTK_STOCK_MISSING_IMAGE,
1490 num_icons = g_strv_length (priv->icon_list);
1492 /* The 1-icon special case */
1495 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1501 range = GTK_RANGE (priv->scale);
1502 adj = priv->adjustment;
1503 value = gtk_scale_button_get_value (button);
1505 /* The 2-icons special case */
1509 limit = (adj->upper - adj->lower) / 2 + adj->lower;
1511 name = priv->icon_list[0];
1513 name = priv->icon_list[1];
1515 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1521 /* With 3 or more icons */
1522 if (value == adj->lower)
1524 name = priv->icon_list[0];
1526 else if (value == adj->upper)
1528 name = priv->icon_list[1];
1535 step = (adj->upper - adj->lower) / (num_icons - 2);
1536 i = (guint) ((value - adj->lower) / step) + 2;
1537 g_assert (i < num_icons);
1538 name = priv->icon_list[i];
1541 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1547 gtk_scale_button_scale_value_changed (GtkRange *range)
1549 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (range)->button;
1552 value = gtk_range_get_value (range);
1554 gtk_scale_button_update_icon (button);
1556 g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1557 g_object_notify (G_OBJECT (button), "value");
1560 #define __GTK_SCALE_BUTTON_C__
1561 #include "gtkaliasdef.c"