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/.
46 #include "gtkbindings.h"
48 #include "gtkvscale.h"
51 #include "gtkwindow.h"
52 #include "gtkmarshalers.h"
54 #include "gtkprivate.h"
56 #include <gdk-pixbuf/gdk-pixbuf.h>
57 #include <gdk/gdkkeysyms.h>
58 #include "gtkscalebutton.h"
62 #define SCALE_SIZE 100
63 #define CLICK_TIMEOUT 250
80 struct _GtkScaleButtonPrivate {
81 GtkWidget *dock, *scale, *image;
89 } _GtkScaleButtonPrivate;
91 static void gtk_scale_button_class_init (GtkScaleButtonClass *klass);
92 static void gtk_scale_button_init (GtkScaleButton *button);
93 static void gtk_scale_button_dispose (GObject *object);
94 static void gtk_scale_button_set_property (GObject *object,
98 static void gtk_scale_button_get_property (GObject *object,
102 static gboolean gtk_scale_button_scroll (GtkWidget *widget,
103 GdkEventScroll *event);
104 static void gtk_scale_button_screen_changed (GtkWidget *widget,
105 GdkScreen *previous_screen);
106 static gboolean gtk_scale_button_press (GtkWidget *widget,
107 GdkEventButton *event);
108 static gboolean gtk_scale_button_key_release (GtkWidget *widget,
110 static void gtk_scale_button_popup_from_bindings (GtkWidget *widget);
111 static void gtk_scale_button_popdown_from_bindings (GtkWidget *widget);
112 static gboolean cb_dock_button_press (GtkWidget *widget,
113 GdkEventButton *event,
115 static gboolean cb_dock_key_release (GtkWidget *widget,
118 static gboolean cb_button_press (GtkWidget *widget,
119 GdkEventButton *event,
121 static gboolean cb_button_release (GtkWidget *widget,
122 GdkEventButton *event,
124 static void cb_dock_grab_notify (GtkWidget *widget,
125 gboolean was_grabbed,
127 static gboolean cb_dock_grab_broken_event (GtkWidget *widget,
128 gboolean was_grabbed,
130 static void cb_scale_grab_notify (GtkWidget *widget,
131 gboolean was_grabbed,
133 static void gtk_scale_button_update_icon (GtkScaleButton *button);
134 static void gtk_scale_button_scale_value_changed(GtkRange *range);
136 /* see below for scale definitions */
137 static GtkWidget *gtk_scale_button_scale_new (GtkScaleButton *button,
142 static GtkButtonClass *parent_class = NULL;
143 static guint signals[LAST_SIGNAL] = { 0 };
145 G_DEFINE_TYPE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON)
148 gtk_scale_button_class_init (GtkScaleButtonClass *klass)
150 GtkBindingSet *binding_set;
151 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
152 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
154 parent_class = g_type_class_peek_parent (klass);
156 gobject_class->dispose = gtk_scale_button_dispose;
157 gobject_class->set_property = gtk_scale_button_set_property;
158 gobject_class->get_property = gtk_scale_button_get_property;
159 widget_class->button_press_event = gtk_scale_button_press;
160 widget_class->key_release_event = gtk_scale_button_key_release;
161 widget_class->scroll_event = gtk_scale_button_scroll;
162 widget_class->screen_changed = gtk_scale_button_screen_changed;
164 g_object_class_install_property (gobject_class,
166 g_param_spec_double ("value",
168 P_("The value of the scale"),
172 GTK_PARAM_READWRITE));
174 g_object_class_install_property (gobject_class,
176 g_param_spec_enum ("size",
180 GTK_ICON_SIZE_SMALL_TOOLBAR,
181 GTK_PARAM_READWRITE));
183 g_object_class_install_property (gobject_class,
185 g_param_spec_object ("adjustment",
187 P_("The GtkAdjustment that contains the current value of this scale button object"),
189 GTK_PARAM_READWRITE));
192 * GtkScaleButton:icons:
194 * The names of the icons to be used by the scale button.
195 * The first item in the array will be used in the button
196 * when the current value is the lowest value, the second
197 * item for the highest value. All the subsequent icons will
198 * be used for all the other values, spread evenly over the
201 * If there's only one icon name in the @icons array, it will
202 * be used for all the values. If only two icon names are in
203 * the @icons array, the first one will be used for the bottom
204 * 50% of the scale, and the second one for the top 50%.
206 * It is recommended to use at least 3 icons so that the
207 * #GtkScaleButton reflects the current value of the scale
208 * better for the users.
212 g_object_class_install_property (gobject_class,
214 g_param_spec_boxed ("icons",
216 P_("List of icon names"),
218 GTK_PARAM_READWRITE));
220 * GtkScaleButton::value-changed:
221 * @button: the object that received the signal
223 * Emitted when the value field has changed.
228 signals[VALUE_CHANGED] =
229 g_signal_new (I_("value-changed"),
230 G_TYPE_FROM_CLASS (klass),
232 G_STRUCT_OFFSET (GtkScaleButtonClass, value_changed),
234 _gtk_marshal_VOID__DOUBLE,
235 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
237 _gtk_binding_signal_new (I_("popup"),
238 G_OBJECT_CLASS_TYPE (klass),
239 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
240 G_CALLBACK (gtk_scale_button_popup_from_bindings),
242 g_cclosure_marshal_VOID__VOID,
245 _gtk_binding_signal_new (I_("popdown"),
246 G_OBJECT_CLASS_TYPE (klass),
247 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
248 G_CALLBACK (gtk_scale_button_popdown_from_bindings),
250 g_cclosure_marshal_VOID__VOID,
254 binding_set = gtk_binding_set_by_class (widget_class);
256 gtk_binding_entry_add_signal (binding_set, GDK_space, 0,
258 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
260 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
262 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
264 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
269 gtk_scale_button_init (GtkScaleButton *button)
271 GtkWidget *frame, *box;
272 GtkScaleButtonPrivate *priv;
274 button->priv = g_new0 (GtkScaleButtonPrivate, 1);
277 priv->timeout = FALSE;
279 priv->click_timeout = CLICK_TIMEOUT;
281 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
282 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
285 priv->image = gtk_image_new ();
286 gtk_container_add (GTK_CONTAINER (button), priv->image);
287 gtk_widget_show_all (priv->image);
290 priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
291 g_signal_connect (priv->dock, "button-press-event",
292 G_CALLBACK (cb_dock_button_press), button);
293 g_signal_connect (priv->dock, "key-release-event",
294 G_CALLBACK (cb_dock_key_release), button);
295 g_signal_connect (priv->dock, "grab-notify",
296 G_CALLBACK (cb_dock_grab_notify), button);
297 g_signal_connect (priv->dock, "grab-broken-event",
298 G_CALLBACK (cb_dock_grab_broken_event), button);
299 gtk_window_set_decorated (GTK_WINDOW (priv->dock), FALSE);
302 frame = gtk_frame_new (NULL);
303 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
304 gtk_container_add (GTK_CONTAINER (priv->dock), frame);
305 box = gtk_vbox_new (FALSE, 0);
306 gtk_container_add (GTK_CONTAINER (frame), box);
309 button->plus_button = gtk_button_new_with_label ("+");
310 gtk_button_set_relief (GTK_BUTTON (button->plus_button), GTK_RELIEF_NONE);
311 g_signal_connect (button->plus_button, "button-press-event",
312 G_CALLBACK (cb_button_press), button);
313 g_signal_connect (button->plus_button, "button-release-event",
314 G_CALLBACK (cb_button_release), button);
315 gtk_box_pack_start (GTK_BOX (box), button->plus_button, TRUE, FALSE, 0);
318 priv->scale = gtk_scale_button_scale_new (button, 0., 100., 2.);
319 gtk_widget_set_size_request (priv->scale, -1, SCALE_SIZE);
320 gtk_scale_set_draw_value (GTK_SCALE (priv->scale), FALSE);
321 gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
322 gtk_box_pack_start (GTK_BOX (box), priv->scale, TRUE, FALSE, 0);
323 g_signal_connect (priv->scale, "grab-notify",
324 G_CALLBACK (cb_scale_grab_notify), button);
327 button->minus_button = gtk_button_new_with_label ("-");
328 gtk_button_set_relief (GTK_BUTTON (button->minus_button), GTK_RELIEF_NONE);
329 g_signal_connect (button->minus_button, "button-press-event",
330 G_CALLBACK (cb_button_press), button);
331 g_signal_connect (button->minus_button, "button-release-event",
332 G_CALLBACK (cb_button_release), button);
333 gtk_box_pack_start (GTK_BOX (box), button->minus_button, TRUE, FALSE, 0);
335 /* set button text and size */
336 priv->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
337 gtk_scale_button_update_icon (button);
341 gtk_scale_button_set_property (GObject *object,
346 GtkScaleButton *button;
348 button = GTK_SCALE_BUTTON (object);
353 gtk_scale_button_set_value (button, g_value_get_double (value));
358 size = g_value_get_enum (value);
359 if (button->priv->size != size)
361 button->priv->size = size;
362 gtk_scale_button_update_icon (button);
366 case PROP_ADJUSTMENT:
367 gtk_scale_button_set_adjustment (button, g_value_get_object (value));
370 gtk_scale_button_set_icons (button,
371 (const gchar **)g_value_get_boxed (value));
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
380 gtk_scale_button_get_property (GObject *object,
385 GtkScaleButton *button;
386 GtkScaleButtonPrivate *priv;
388 button = GTK_SCALE_BUTTON (object);
394 g_value_set_float (value, gtk_scale_button_get_value (button));
397 g_value_set_enum (value, priv->size);
399 case PROP_ADJUSTMENT:
400 g_value_set_object (value, gtk_scale_button_get_adjustment (button));
403 g_value_set_boxed (value, priv->icon_list);
406 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412 gtk_scale_button_dispose (GObject *object)
414 GtkScaleButton *button;
415 GtkScaleButtonPrivate *priv;
417 button = GTK_SCALE_BUTTON (object);
421 if (priv && priv->dock)
423 gtk_widget_destroy (priv->dock);
427 if (priv && priv->click_id != 0)
429 g_source_remove (priv->click_id);
433 if (priv && priv->icon_list)
435 g_strfreev (priv->icon_list);
436 priv->icon_list = NULL;
442 G_OBJECT_CLASS (parent_class)->dispose (object);
446 * gtk_scale_button_new
447 * @size: a stock icon size
448 * @min: the minimum value of the scale (usually 0)
449 * @max: the maximum value of the scale (usually 100)
450 * @step: the stepping of value when a scroll-wheel event,
451 * or up/down arrow event occurs (usually 2)
452 * @icons: a %NULL-terminated array of icon names, or %NULL if
453 * you want to set the list later with gtk_scale_button_set_icons()
455 * Creates a #GtkScaleButton, with a range between @min and @max, with
456 * a stepping of @step.
458 * Return value: a new #GtkScaleButton
463 gtk_scale_button_new (GtkIconSize size,
469 GtkScaleButton *button;
472 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
474 button = g_object_new (GTK_TYPE_SCALE_BUTTON,
480 return GTK_WIDGET (button);
484 * gtk_scale_button_get_value
485 * @button: a #GtkScaleButton
487 * Gets the current value of the scale button.
489 * Return value: current value of the scale button
494 gtk_scale_button_get_value (GtkScaleButton * button)
496 GtkScaleButtonPrivate *priv;
498 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), 0);
502 return gtk_range_get_value (GTK_RANGE (priv->scale));
506 * gtk_scale_button_set_value
507 * @button: a #GtkScaleButton
508 * @value: new value of the scale button
510 * Sets the current value of the scale; if the value is outside
511 * the minimum or maximum range values, it will be clamped to fit
512 * inside them. The scale button emits the #GtkScaleButton::value-changed
513 * signal if the value changes.
518 gtk_scale_button_set_value (GtkScaleButton *button,
521 GtkScaleButtonPrivate *priv;
523 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
527 gtk_range_set_value (GTK_RANGE (priv->scale), value);
531 * gtk_scale_button_set_icons
532 * @button: a #GtkScaleButton
533 * @icons: a %NULL-terminated array of icon names
535 * Sets the icons to be used by the scale button.
536 * For details, see the #GtkScaleButton:icons property.
541 gtk_scale_button_set_icons (GtkScaleButton *button,
544 GtkScaleButtonPrivate *priv;
547 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
551 tmp = priv->icon_list;
552 priv->icon_list = g_strdupv ((gchar **) icons);
554 gtk_scale_button_update_icon (button);
556 g_object_notify (G_OBJECT (button), "icons");
560 * gtk_scale_button_get_adjustment:
561 * @button: a #GtkScaleButton
563 * Gets the #GtkAdjustment associated with the #GtkScaleButton's scale.
564 * See gtk_range_get_adjustment() for details.
566 * Returns: the adjustment associated with the scale
571 gtk_scale_button_get_adjustment (GtkScaleButton *button)
575 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
577 range = GTK_RANGE (button->priv->scale);
578 g_return_val_if_fail (range != NULL, NULL);
580 return gtk_range_get_adjustment (range);
584 * gtk_scale_button_set_adjustment
585 * @button: a #GtkScaleButton
586 * @adjustment: a #GtkAdjustment
588 * Sets the #GtkAdjustment to be used as a model
589 * for the #GtkScaleButton's scale.
590 * See gtk_range_set_adjustment() for details.
595 gtk_scale_button_set_adjustment (GtkScaleButton *button,
596 GtkAdjustment *adjustment)
598 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
599 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
601 gtk_range_set_adjustment (GTK_RANGE (button->priv->scale),
604 g_object_notify (G_OBJECT (button), "adjustment");
612 gtk_scale_button_scroll (GtkWidget *widget,
613 GdkEventScroll *event)
615 GtkScaleButton *button;
616 GtkScaleButtonPrivate *priv;
620 button = GTK_SCALE_BUTTON (widget);
622 adj = gtk_range_get_adjustment (GTK_RANGE (priv->scale));
624 if (event->type != GDK_SCROLL)
627 d = gtk_scale_button_get_value (button);
628 if (event->direction == GDK_SCROLL_UP)
630 d += adj->step_increment;
636 d -= adj->step_increment;
640 gtk_scale_button_set_value (button, d);
646 gtk_scale_button_screen_changed (GtkWidget *widget,
647 GdkScreen *previous_screen)
649 GtkScaleButton *button = (GtkScaleButton *) widget;
650 GtkScaleButtonPrivate *priv;
652 GValue value = { 0, };
654 if (gtk_widget_has_screen (widget) == FALSE)
659 screen = gtk_widget_get_screen (widget);
660 g_value_init (&value, G_TYPE_INT);
661 if (gdk_screen_get_setting (screen,
662 "gtk-double-click-time",
665 priv->click_timeout = CLICK_TIMEOUT;
669 priv->click_timeout = g_value_get_int (&value);
673 gtk_scale_popup (GtkWidget *widget,
677 GtkScaleButton *button;
678 GtkScaleButtonPrivate *priv;
680 gint x, y, m, dx, dy, sx, sy, ystartoff;
685 button = GTK_SCALE_BUTTON (widget);
687 adj = gtk_range_get_adjustment (GTK_RANGE (priv->scale));
689 display = gtk_widget_get_display (widget);
690 screen = gtk_widget_get_screen (widget);
692 /* position roughly */
693 gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
695 gdk_window_get_origin (widget->window, &x, &y);
696 x += widget->allocation.x;
697 y += widget->allocation.y;
698 gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
699 gtk_widget_show_all (priv->dock);
700 gdk_window_get_origin (priv->dock->window, &dx, &dy);
701 dy += priv->dock->allocation.y;
702 gdk_window_get_origin (priv->scale->window, &sx, &sy);
703 sy += priv->scale->allocation.y;
705 priv->timeout = TRUE;
707 /* position (needs widget to be shown already) */
708 v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
709 x += (widget->allocation.width - priv->dock->allocation.width) / 2;
711 y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
712 m = priv->scale->allocation.height -
713 GTK_RANGE (priv->scale)->min_slider_size;
716 /* Make sure the dock stays inside the monitor */
717 if (event->type == GDK_BUTTON_PRESS)
720 GdkEventButton *button_event = (GdkEventButton *) event;
724 d = GTK_WIDGET (priv->dock);
725 monitor = gdk_screen_get_monitor_at_point (screen,
726 button_event->x_root,
727 button_event->y_root);
728 gdk_screen_get_monitor_geometry (screen, monitor, &rect);
730 y += button_event->y;
733 else if (y + d->allocation.height > rect.height + rect.y)
734 y = rect.y + rect.height - d->allocation.height;
738 else if (x + d->allocation.width > rect.width + rect.x)
739 x = rect.x + rect.width - d->allocation.width;
742 gtk_window_move (GTK_WINDOW (priv->dock), x, y);
744 if (event->type == GDK_BUTTON_PRESS)
745 GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, (GdkEventButton *) event);
748 gtk_grab_add (priv->dock);
750 if (gdk_pointer_grab (priv->dock->window, TRUE,
751 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
752 GDK_POINTER_MOTION_MASK, NULL, NULL, time)
755 gtk_grab_remove (priv->dock);
756 gtk_widget_hide (priv->dock);
760 if (gdk_keyboard_grab (priv->dock->window, TRUE, time) != GDK_GRAB_SUCCESS)
762 gdk_display_pointer_ungrab (display, time);
763 gtk_grab_remove (priv->dock);
764 gtk_widget_hide (priv->dock);
768 gtk_widget_grab_focus (priv->dock);
770 if (event->type == GDK_BUTTON_PRESS)
773 GdkEventButton *button_event = (GdkEventButton *) event;
775 /* forward event to the slider */
776 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
777 e->window = priv->scale->window;
779 /* position: the X position isn't relevant, halfway will work just fine.
780 * The vertical position should be *exactly* in the middle of the slider
781 * of the scale; if we don't do that correctly, it'll move from its current
782 * position, which means a position change on-click, which is bad. */
783 e->x = priv->scale->allocation.width / 2;
784 m = priv->scale->allocation.height -
785 GTK_RANGE (priv->scale)->min_slider_size;
786 e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
787 gtk_widget_event (priv->scale, (GdkEvent *) e);
788 e->window = button_event->window;
789 gdk_event_free ((GdkEvent *) e);
792 gtk_widget_grab_focus (priv->scale);
794 priv->pop_time = time;
800 gtk_scale_button_press (GtkWidget *widget,
801 GdkEventButton *event)
803 return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
807 gtk_scale_button_popup_from_bindings (GtkWidget *widget)
811 ev = gdk_event_new (GDK_KEY_RELEASE);
812 gtk_scale_popup (widget, ev, GDK_CURRENT_TIME);
817 gtk_scale_button_key_release (GtkWidget *widget,
820 return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
823 /* This is called when the grab is broken for
824 * either the dock, or the scale itself */
826 gtk_scale_button_grab_notify (GtkScaleButton *button,
827 gboolean was_grabbed)
830 GtkScaleButtonPrivate *priv;
832 if (was_grabbed != FALSE)
837 if (gtk_widget_is_ancestor (gtk_grab_get_current (), priv->dock))
840 display = gtk_widget_get_display (priv->dock);
841 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
842 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
843 gtk_grab_remove (priv->dock);
846 gtk_widget_hide (priv->dock);
847 priv->timeout = FALSE;
851 * +/- button callbacks.
855 cb_button_timeout (gpointer user_data)
857 GtkScaleButton *button;
858 GtkScaleButtonPrivate *priv;
863 button = GTK_SCALE_BUTTON (user_data);
866 if (priv->click_id == 0)
869 adj = gtk_range_get_adjustment (GTK_RANGE (priv->scale));
871 val = gtk_scale_button_get_value (button);
872 val += priv->direction;
873 if (val <= adj->lower)
878 else if (val > adj->upper)
883 gtk_scale_button_set_value (button, val);
887 g_source_remove (priv->click_id);
895 cb_button_press (GtkWidget *widget,
896 GdkEventButton *event,
899 GtkScaleButton *button;
900 GtkScaleButtonPrivate *priv;
903 button = GTK_SCALE_BUTTON (user_data);
905 adj = gtk_range_get_adjustment (GTK_RANGE (priv->scale));
907 if (priv->click_id != 0)
908 g_source_remove (priv->click_id);
910 if (widget == button->plus_button)
911 priv->direction = fabs (adj->page_increment);
913 priv->direction = - fabs (adj->page_increment);
915 priv->click_id = g_timeout_add (priv->click_timeout,
916 (GSourceFunc) cb_button_timeout, button);
917 cb_button_timeout (button);
923 cb_button_release (GtkWidget *widget,
924 GdkEventButton *event,
927 GtkScaleButton *button;
928 GtkScaleButtonPrivate *priv;
930 button = GTK_SCALE_BUTTON (user_data);
933 if (priv->click_id != 0)
935 g_source_remove (priv->click_id);
943 cb_dock_grab_notify (GtkWidget *widget,
944 gboolean was_grabbed,
947 GtkScaleButton *button = (GtkScaleButton *) user_data;
949 gtk_scale_button_grab_notify (button, was_grabbed);
953 cb_dock_grab_broken_event (GtkWidget *widget,
954 gboolean was_grabbed,
957 GtkScaleButton *button = (GtkScaleButton *) user_data;
959 gtk_scale_button_grab_notify (button, FALSE);
969 gtk_scale_button_release_grab (GtkScaleButton *button,
970 GdkEventButton *event)
974 GtkScaleButtonPrivate *priv;
979 display = gtk_widget_get_display (GTK_WIDGET (button));
980 gdk_display_keyboard_ungrab (display, event->time);
981 gdk_display_pointer_ungrab (display, event->time);
982 gtk_grab_remove (priv->dock);
985 gtk_widget_hide (priv->dock);
986 priv->timeout = FALSE;
988 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
989 e->window = GTK_WIDGET (button)->window;
990 e->type = GDK_BUTTON_RELEASE;
991 gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
992 e->window = event->window;
993 gdk_event_free ((GdkEvent *) e);
997 cb_dock_button_press (GtkWidget *widget,
998 GdkEventButton *event,
1001 GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1003 if (event->type == GDK_BUTTON_PRESS)
1005 gtk_scale_button_release_grab (button, event);
1013 gtk_scale_button_popdown_from_bindings (GtkWidget *widget)
1015 GtkScaleButton *button;
1016 GtkScaleButtonPrivate *priv;
1017 GdkDisplay *display;
1019 button = GTK_SCALE_BUTTON (widget);
1020 priv = button->priv;
1023 display = gtk_widget_get_display (widget);
1024 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1025 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1026 gtk_grab_remove (priv->dock);
1029 gtk_widget_hide (priv->dock);
1030 priv->timeout = FALSE;
1034 cb_dock_key_release (GtkWidget *widget,
1038 if (event->keyval == GDK_Escape)
1040 gtk_scale_button_popdown_from_bindings (GTK_WIDGET (user_data));
1044 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
1046 /* The popup hasn't managed the event, pass onto the button */
1047 gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
1054 cb_scale_grab_notify (GtkWidget *widget,
1055 gboolean was_grabbed,
1058 GtkScaleButton *button = (GtkScaleButton *) user_data;
1060 gtk_scale_button_grab_notify (button, was_grabbed);
1067 #define GTK_TYPE_SCALE_BUTTON_SCALE \
1068 (gtk_scale_button_scale_get_type ())
1069 #define GTK_SCALE_BUTTON_SCALE(obj) \
1070 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, \
1071 GtkScaleButtonScale))
1073 typedef struct _GtkScaleButtonScaleClass {
1074 GtkVScaleClass parent_class;
1075 } GtkScaleButtonScaleClass;
1077 typedef struct _GtkScaleButtonScale {
1079 GtkScaleButton *button;
1080 } GtkScaleButtonScale;
1082 static GType gtk_scale_button_scale_get_type (void);
1083 static void gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass);
1084 static gboolean gtk_scale_button_scale_press (GtkWidget *widget,
1085 GdkEventButton *event);
1086 static gboolean gtk_scale_button_scale_release (GtkWidget *widget,
1087 GdkEventButton *event);
1089 static GtkVScaleClass *scale_parent_class = NULL;
1091 G_DEFINE_TYPE (GtkScaleButtonScale, gtk_scale_button_scale, GTK_TYPE_VSCALE)
1094 gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass)
1096 GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1097 GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1099 scale_parent_class = g_type_class_peek_parent (klass);
1101 gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1102 gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1103 gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1107 gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
1112 gtk_scale_button_scale_new (GtkScaleButton *button,
1117 GtkScaleButtonScale *scale;
1118 GtkScaleButtonPrivate *priv;
1121 priv = button->priv;
1122 scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE, NULL);
1123 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1124 gtk_range_set_adjustment (GTK_RANGE (scale), GTK_ADJUSTMENT (adj));
1125 scale->button = button;
1127 return GTK_WIDGET (scale);
1131 gtk_scale_button_scale_press (GtkWidget *widget,
1132 GdkEventButton *event)
1134 GtkScaleButtonScale *scale;
1135 GtkScaleButtonPrivate *priv;
1137 scale = GTK_SCALE_BUTTON_SCALE (widget);
1138 priv = scale->button->priv;
1140 /* the scale will grab input; if we have input grabbed, all goes
1141 * horribly wrong, so let's not do that. */
1142 gtk_grab_remove (priv->dock);
1144 return GTK_WIDGET_CLASS (scale_parent_class)->button_press_event (widget, event);
1148 gtk_scale_button_scale_release (GtkWidget *widget,
1149 GdkEventButton *event)
1151 GtkScaleButtonScale *scale;
1152 GtkScaleButtonPrivate *priv;
1155 scale = GTK_SCALE_BUTTON_SCALE (widget);
1156 priv = scale->button->priv;
1160 /* if we did a quick click, leave the window open; else, hide it */
1161 if (event->time > priv->pop_time + priv->click_timeout)
1163 gtk_scale_button_release_grab (scale->button, event);
1164 GTK_WIDGET_CLASS (scale_parent_class)->button_release_event (widget, event);
1167 priv->timeout = FALSE;
1170 res = GTK_WIDGET_CLASS (scale_parent_class)->button_release_event (widget, event);
1172 /* the scale will release input; right after that, we *have to* grab
1173 * it back so we can catch out-of-scale clicks and hide the popup,
1174 * so I basically want a g_signal_connect_after_always(), but I can't
1175 * find that, so we do this complex 'first-call-parent-then-do-actual-
1176 * action' thingy... */
1177 gtk_grab_add (priv->dock);
1183 gtk_scale_button_update_icon (GtkScaleButton *button)
1185 GtkScaleButtonPrivate *priv;
1192 priv = button->priv;
1194 if (priv->icon_list == NULL || priv->icon_list[0] == NULL)
1196 gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1197 GTK_STOCK_MISSING_IMAGE,
1202 for (num_icons = 0; priv->icon_list[num_icons] != NULL; num_icons++) {}
1204 /* The 1-icon special case */
1207 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1213 range = GTK_RANGE (priv->scale);
1214 adj = gtk_range_get_adjustment (range);
1215 value = gtk_scale_button_get_value (button);
1217 /* The 2-icons special case */
1221 limit = (adj->upper - adj->lower) / 2 + adj->lower;
1223 name = priv->icon_list[0];
1225 name = priv->icon_list[1];
1227 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1233 /* With 3 or more icons */
1234 if (value == adj->lower)
1236 name = priv->icon_list[0];
1238 else if (value == adj->upper)
1240 name = priv->icon_list[1];
1247 step = (adj->upper - adj->lower) / (num_icons - 2);
1248 i = (guint) (value - adj->lower) / step + 2;
1249 g_assert (i < num_icons);
1250 name = priv->icon_list[i];
1253 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1259 gtk_scale_button_scale_value_changed (GtkRange *range)
1261 GtkScaleButtonScale *scale = GTK_SCALE_BUTTON_SCALE (range);
1262 GtkScaleButton *button = scale->button;
1265 value = gtk_range_get_value (range);
1267 gtk_scale_button_update_icon (button);
1269 g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1270 g_object_notify (G_OBJECT (button), "value");
1274 #define __GTK_SCALE_BUTTON_C__
1275 #include "gtkaliasdef.c"