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: (int): 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: (allow-none): 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_plus_button:
711 * @button: a #GtkScaleButton
713 * Retrieves the plus button of the #GtkScaleButton.
715 * Returns: the plus button of the #GtkScaleButton.
720 gtk_scale_button_get_plus_button (GtkScaleButton *button)
722 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
724 return button->plus_button;
728 * gtk_scale_button_get_minus_button:
729 * @button: a #GtkScaleButton
731 * Retrieves the minus button of the #GtkScaleButton.
733 * Returns: the minus button of the #GtkScaleButton.
738 gtk_scale_button_get_minus_button (GtkScaleButton *button)
740 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
742 return button->minus_button;
746 * gtk_scale_button_get_popup:
747 * @button: a #GtkScaleButton
749 * Retrieves the popup of the #GtkScaleButton.
751 * Returns: the popup of the #GtkScaleButton
756 gtk_scale_button_get_popup (GtkScaleButton *button)
758 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
760 return button->priv->dock;
764 gtk_scale_button_set_orientation_private (GtkScaleButton *button,
765 GtkOrientation orientation)
767 GtkScaleButtonPrivate *priv = button->priv;
769 if (orientation != priv->orientation)
771 priv->orientation = orientation;
773 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box),
775 gtk_container_child_set (GTK_CONTAINER (priv->box),
778 orientation == GTK_ORIENTATION_VERTICAL ?
779 GTK_PACK_START : GTK_PACK_END,
781 gtk_container_child_set (GTK_CONTAINER (priv->box),
782 button->minus_button,
784 orientation == GTK_ORIENTATION_VERTICAL ?
785 GTK_PACK_END : GTK_PACK_START,
788 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->scale),
791 if (orientation == GTK_ORIENTATION_VERTICAL)
793 gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
795 gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
799 gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
801 gtk_range_set_inverted (GTK_RANGE (priv->scale), FALSE);
804 /* FIXME: without this, the popup window appears as a square
805 * after changing the orientation
807 gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
809 g_object_notify (G_OBJECT (button), "orientation");
818 gtk_scale_button_scroll (GtkWidget *widget,
819 GdkEventScroll *event)
821 GtkScaleButton *button;
822 GtkScaleButtonPrivate *priv;
826 button = GTK_SCALE_BUTTON (widget);
828 adj = priv->adjustment;
830 if (event->type != GDK_SCROLL)
833 d = gtk_scale_button_get_value (button);
834 if (event->direction == GDK_SCROLL_UP)
836 d += adj->step_increment;
842 d -= adj->step_increment;
846 gtk_scale_button_set_value (button, d);
852 gtk_scale_button_screen_changed (GtkWidget *widget,
853 GdkScreen *previous_screen)
855 GtkScaleButton *button = (GtkScaleButton *) widget;
856 GtkScaleButtonPrivate *priv;
858 GValue value = { 0, };
860 if (gtk_widget_has_screen (widget) == FALSE)
865 screen = gtk_widget_get_screen (widget);
866 g_value_init (&value, G_TYPE_INT);
867 if (gdk_screen_get_setting (screen,
868 "gtk-double-click-time",
871 priv->click_timeout = CLICK_TIMEOUT;
875 priv->click_timeout = g_value_get_int (&value);
879 gtk_scale_popup (GtkWidget *widget,
883 GtkScaleButton *button;
884 GtkScaleButtonPrivate *priv;
886 gint x, y, m, dx, dy, sx, sy, startoff;
893 button = GTK_SCALE_BUTTON (widget);
895 adj = priv->adjustment;
897 display = gtk_widget_get_display (widget);
898 screen = gtk_widget_get_screen (widget);
900 /* position roughly */
901 gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
903 gdk_window_get_origin (widget->window, &x, &y);
904 x += widget->allocation.x;
905 y += widget->allocation.y;
907 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
908 gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
910 gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
912 gtk_widget_show_all (priv->dock);
914 gdk_window_get_origin (priv->dock->window, &dx, &dy);
915 dx += priv->dock->allocation.x;
916 dy += priv->dock->allocation.y;
918 gdk_window_get_origin (priv->scale->window, &sx, &sy);
919 sx += priv->scale->allocation.x;
920 sy += priv->scale->allocation.y;
922 priv->timeout = TRUE;
924 /* position (needs widget to be shown already) */
925 v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
927 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
931 x += (widget->allocation.width - priv->dock->allocation.width) / 2;
933 y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
934 m = priv->scale->allocation.height -
935 GTK_RANGE (priv->scale)->min_slider_size;
943 y += (widget->allocation.height - priv->dock->allocation.height) / 2;
944 x -= GTK_RANGE (priv->scale)->min_slider_size / 2;
945 m = priv->scale->allocation.width -
946 GTK_RANGE (priv->scale)->min_slider_size;
950 /* Make sure the dock stays inside the monitor */
951 if (event->type == GDK_BUTTON_PRESS)
954 GdkEventButton *button_event = (GdkEventButton *) event;
958 d = GTK_WIDGET (priv->dock);
959 monitor = gdk_screen_get_monitor_at_point (screen,
960 button_event->x_root,
961 button_event->y_root);
962 gdk_screen_get_monitor_geometry (screen, monitor, &rect);
964 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
965 y += button_event->y;
967 x += button_event->x;
969 /* Move the dock, but set is_moved so we
970 * don't forward the first click later on,
971 * as it could make the scale go to the bottom */
975 } else if (y + d->allocation.height > rect.height + rect.y) {
976 y = rect.y + rect.height - d->allocation.height;
983 } else if (x + d->allocation.width > rect.width + rect.x) {
984 x = rect.x + rect.width - d->allocation.width;
989 gtk_window_move (GTK_WINDOW (priv->dock), x, y);
991 if (event->type == GDK_BUTTON_PRESS)
992 GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
995 gtk_grab_add (priv->dock);
997 if (gdk_pointer_grab (priv->dock->window, TRUE,
998 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
999 GDK_POINTER_MOTION_MASK, NULL, NULL, time)
1000 != GDK_GRAB_SUCCESS)
1002 gtk_grab_remove (priv->dock);
1003 gtk_widget_hide (priv->dock);
1007 if (gdk_keyboard_grab (priv->dock->window, TRUE, time) != GDK_GRAB_SUCCESS)
1009 gdk_display_pointer_ungrab (display, time);
1010 gtk_grab_remove (priv->dock);
1011 gtk_widget_hide (priv->dock);
1015 gtk_widget_grab_focus (priv->dock);
1017 if (event->type == GDK_BUTTON_PRESS && !is_moved)
1020 GdkEventButton *button_event = (GdkEventButton *) event;
1022 /* forward event to the slider */
1023 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1024 e->window = priv->scale->window;
1026 /* position: the X position isn't relevant, halfway will work just fine.
1027 * The vertical position should be *exactly* in the middle of the slider
1028 * of the scale; if we don't do that correctly, it'll move from its current
1029 * position, which means a position change on-click, which is bad.
1031 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1033 e->x = priv->scale->allocation.width / 2;
1034 m = priv->scale->allocation.height -
1035 GTK_RANGE (priv->scale)->min_slider_size;
1036 e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1040 e->y = priv->scale->allocation.height / 2;
1041 m = priv->scale->allocation.width -
1042 GTK_RANGE (priv->scale)->min_slider_size;
1043 e->x = (v * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1046 gtk_widget_event (priv->scale, (GdkEvent *) e);
1047 e->window = button_event->window;
1048 gdk_event_free ((GdkEvent *) e);
1051 gtk_widget_grab_focus (priv->scale);
1053 priv->pop_time = time;
1059 gtk_scale_button_press (GtkWidget *widget,
1060 GdkEventButton *event)
1062 return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
1066 gtk_scale_button_popup (GtkWidget *widget)
1070 ev = gdk_event_new (GDK_KEY_RELEASE);
1071 gtk_scale_popup (widget, ev, GDK_CURRENT_TIME);
1072 gdk_event_free (ev);
1076 gtk_scale_button_key_release (GtkWidget *widget,
1079 return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
1082 /* This is called when the grab is broken for
1083 * either the dock, or the scale itself */
1085 gtk_scale_button_grab_notify (GtkScaleButton *button,
1086 gboolean was_grabbed)
1088 GdkDisplay *display;
1089 GtkScaleButtonPrivate *priv;
1091 if (was_grabbed != FALSE)
1094 priv = button->priv;
1096 if (!gtk_widget_has_grab (priv->dock))
1099 if (gtk_widget_is_ancestor (gtk_grab_get_current (), priv->dock))
1102 display = gtk_widget_get_display (priv->dock);
1103 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1104 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1105 gtk_grab_remove (priv->dock);
1108 gtk_widget_hide (priv->dock);
1109 priv->timeout = FALSE;
1113 * +/- button callbacks.
1117 cb_button_timeout (gpointer user_data)
1119 GtkScaleButton *button;
1120 GtkScaleButtonPrivate *priv;
1123 gboolean res = TRUE;
1125 button = GTK_SCALE_BUTTON (user_data);
1126 priv = button->priv;
1128 if (priv->click_id == 0)
1131 adj = priv->adjustment;
1133 val = gtk_scale_button_get_value (button);
1134 val += priv->direction;
1135 if (val <= adj->lower)
1140 else if (val > adj->upper)
1145 gtk_scale_button_set_value (button, val);
1149 g_source_remove (priv->click_id);
1157 cb_button_press (GtkWidget *widget,
1158 GdkEventButton *event,
1161 GtkScaleButton *button;
1162 GtkScaleButtonPrivate *priv;
1165 button = GTK_SCALE_BUTTON (user_data);
1166 priv = button->priv;
1167 adj = priv->adjustment;
1169 if (priv->click_id != 0)
1170 g_source_remove (priv->click_id);
1172 if (widget == button->plus_button)
1173 priv->direction = fabs (adj->page_increment);
1175 priv->direction = - fabs (adj->page_increment);
1177 priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1180 cb_button_timeout (button);
1186 cb_button_release (GtkWidget *widget,
1187 GdkEventButton *event,
1190 GtkScaleButton *button;
1191 GtkScaleButtonPrivate *priv;
1193 button = GTK_SCALE_BUTTON (user_data);
1194 priv = button->priv;
1196 if (priv->click_id != 0)
1198 g_source_remove (priv->click_id);
1206 cb_dock_grab_notify (GtkWidget *widget,
1207 gboolean was_grabbed,
1210 GtkScaleButton *button = (GtkScaleButton *) user_data;
1212 gtk_scale_button_grab_notify (button, was_grabbed);
1216 cb_dock_grab_broken_event (GtkWidget *widget,
1217 gboolean was_grabbed,
1220 GtkScaleButton *button = (GtkScaleButton *) user_data;
1222 gtk_scale_button_grab_notify (button, FALSE);
1232 gtk_scale_button_release_grab (GtkScaleButton *button,
1233 GdkEventButton *event)
1236 GdkDisplay *display;
1237 GtkScaleButtonPrivate *priv;
1239 priv = button->priv;
1242 display = gtk_widget_get_display (GTK_WIDGET (button));
1243 gdk_display_keyboard_ungrab (display, event->time);
1244 gdk_display_pointer_ungrab (display, event->time);
1245 gtk_grab_remove (priv->dock);
1248 gtk_widget_hide (priv->dock);
1249 priv->timeout = FALSE;
1251 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1252 e->window = GTK_WIDGET (button)->window;
1253 e->type = GDK_BUTTON_RELEASE;
1254 gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1255 e->window = event->window;
1256 gdk_event_free ((GdkEvent *) e);
1260 cb_dock_button_press (GtkWidget *widget,
1261 GdkEventButton *event,
1264 GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1266 if (event->type == GDK_BUTTON_PRESS)
1268 gtk_scale_button_release_grab (button, event);
1276 gtk_scale_button_popdown (GtkWidget *widget)
1278 GtkScaleButton *button;
1279 GtkScaleButtonPrivate *priv;
1280 GdkDisplay *display;
1282 button = GTK_SCALE_BUTTON (widget);
1283 priv = button->priv;
1286 display = gtk_widget_get_display (widget);
1287 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1288 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1289 gtk_grab_remove (priv->dock);
1292 gtk_widget_hide (priv->dock);
1293 priv->timeout = FALSE;
1297 cb_dock_key_release (GtkWidget *widget,
1301 if (event->keyval == GDK_Escape)
1303 gtk_scale_button_popdown (GTK_WIDGET (user_data));
1307 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
1309 /* The popup hasn't managed the event, pass onto the button */
1310 gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
1317 cb_scale_grab_notify (GtkWidget *widget,
1318 gboolean was_grabbed,
1321 GtkScaleButton *button = (GtkScaleButton *) user_data;
1323 gtk_scale_button_grab_notify (button, was_grabbed);
1330 #define GTK_TYPE_SCALE_BUTTON_SCALE (_gtk_scale_button_scale_get_type ())
1331 #define GTK_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, GtkScaleButtonScale))
1332 #define GTK_IS_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_SCALE))
1334 typedef struct _GtkScaleButtonScale
1336 GtkScale parent_instance;
1337 GtkScaleButton *button;
1338 } GtkScaleButtonScale;
1340 typedef struct _GtkScaleButtonScaleClass
1342 GtkScaleClass parent_class;
1343 } GtkScaleButtonScaleClass;
1345 static gboolean gtk_scale_button_scale_press (GtkWidget *widget,
1346 GdkEventButton *event);
1347 static gboolean gtk_scale_button_scale_release (GtkWidget *widget,
1348 GdkEventButton *event);
1350 G_DEFINE_TYPE (GtkScaleButtonScale, _gtk_scale_button_scale, GTK_TYPE_SCALE)
1353 _gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass)
1355 GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1356 GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1358 gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1359 gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1360 gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1364 _gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
1369 gtk_scale_button_scale_new (GtkScaleButton *button)
1371 GtkScaleButtonPrivate *priv = button->priv;
1372 GtkScaleButtonScale *scale;
1374 scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE,
1375 "orientation", priv->orientation,
1376 "adjustment", priv->adjustment,
1377 "draw-value", FALSE,
1380 scale->button = button;
1382 g_signal_connect (scale, "grab-notify",
1383 G_CALLBACK (cb_scale_grab_notify), button);
1385 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1387 gtk_widget_set_size_request (GTK_WIDGET (scale), -1, SCALE_SIZE);
1388 gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
1392 gtk_widget_set_size_request (GTK_WIDGET (scale), SCALE_SIZE, -1);
1393 gtk_range_set_inverted (GTK_RANGE (scale), FALSE);
1396 return GTK_WIDGET (scale);
1400 gtk_scale_button_scale_press (GtkWidget *widget,
1401 GdkEventButton *event)
1403 GtkScaleButtonPrivate *priv = GTK_SCALE_BUTTON_SCALE (widget)->button->priv;
1405 /* the scale will grab input; if we have input grabbed, all goes
1406 * horribly wrong, so let's not do that.
1408 gtk_grab_remove (priv->dock);
1410 return GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_press_event (widget, event);
1414 gtk_scale_button_scale_release (GtkWidget *widget,
1415 GdkEventButton *event)
1417 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (widget)->button;
1420 if (button->priv->timeout)
1422 /* if we did a quick click, leave the window open; else, hide it */
1423 if (event->time > button->priv->pop_time + button->priv->click_timeout)
1426 gtk_scale_button_release_grab (button, event);
1427 GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1432 button->priv->timeout = FALSE;
1435 res = GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1437 /* the scale will release input; right after that, we *have to* grab
1438 * it back so we can catch out-of-scale clicks and hide the popup,
1439 * so I basically want a g_signal_connect_after_always(), but I can't
1440 * find that, so we do this complex 'first-call-parent-then-do-actual-
1443 gtk_grab_add (button->priv->dock);
1449 gtk_scale_button_update_icon (GtkScaleButton *button)
1451 GtkScaleButtonPrivate *priv;
1458 priv = button->priv;
1460 if (!priv->icon_list || priv->icon_list[0] == '\0')
1462 gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1463 GTK_STOCK_MISSING_IMAGE,
1468 num_icons = g_strv_length (priv->icon_list);
1470 /* The 1-icon special case */
1473 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1479 range = GTK_RANGE (priv->scale);
1480 adj = priv->adjustment;
1481 value = gtk_scale_button_get_value (button);
1483 /* The 2-icons special case */
1487 limit = (adj->upper - adj->lower) / 2 + adj->lower;
1489 name = priv->icon_list[0];
1491 name = priv->icon_list[1];
1493 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1499 /* With 3 or more icons */
1500 if (value == adj->lower)
1502 name = priv->icon_list[0];
1504 else if (value == adj->upper)
1506 name = priv->icon_list[1];
1513 step = (adj->upper - adj->lower) / (num_icons - 2);
1514 i = (guint) ((value - adj->lower) / step) + 2;
1515 g_assert (i < num_icons);
1516 name = priv->icon_list[i];
1519 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1525 gtk_scale_button_scale_value_changed (GtkRange *range)
1527 GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (range)->button;
1530 value = gtk_range_get_value (range);
1532 gtk_scale_button_update_icon (button);
1534 g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1535 g_object_notify (G_OBJECT (button), "value");
1538 #define __GTK_SCALE_BUTTON_C__
1539 #include "gtkaliasdef.c"