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"
55 #include <gdk-pixbuf/gdk-pixbuf.h>
56 #include <gdk/gdkkeysyms.h>
57 #include "gtkscalebutton.h"
61 #define SCALE_SIZE 100
62 #define CLICK_TIMEOUT 250
78 struct _GtkScaleButtonPrivate {
79 GtkWidget *dock, *scale, *image;
87 } _GtkScaleButtonPrivate;
89 static void gtk_scale_button_class_init (GtkScaleButtonClass *klass);
90 static void gtk_scale_button_init (GtkScaleButton *button);
91 static void gtk_scale_button_dispose (GObject *object);
92 static void gtk_scale_button_set_property (GObject *object,
96 static void gtk_scale_button_get_property (GObject *object,
100 static gboolean gtk_scale_button_scroll (GtkWidget *widget,
101 GdkEventScroll *event);
102 static void gtk_scale_button_screen_changed (GtkWidget *widget,
103 GdkScreen *previous_screen);
104 static gboolean gtk_scale_button_press (GtkWidget *widget,
105 GdkEventButton *event);
106 static gboolean gtk_scale_button_key_release (GtkWidget *widget,
108 static void gtk_scale_button_popup_from_bindings (GtkWidget *widget);
109 static void gtk_scale_button_popdown_from_bindings (GtkWidget *widget);
110 static gboolean cb_dock_button_press (GtkWidget *widget,
111 GdkEventButton *event,
113 static gboolean cb_dock_key_release (GtkWidget *widget,
116 static gboolean cb_button_press (GtkWidget *widget,
117 GdkEventButton *event,
119 static gboolean cb_button_release (GtkWidget *widget,
120 GdkEventButton *event,
122 static void cb_dock_grab_notify (GtkWidget *widget,
123 gboolean was_grabbed,
125 static gboolean cb_dock_grab_broken_event (GtkWidget *widget,
126 gboolean was_grabbed,
128 static void cb_scale_grab_notify (GtkWidget *widget,
129 gboolean was_grabbed,
131 static void gtk_scale_button_update_icon (GtkScaleButton *button);
132 static void gtk_scale_button_scale_value_changed(GtkRange *range);
134 /* see below for scale definitions */
135 static GtkWidget *gtk_scale_button_scale_new (GtkScaleButton *button,
140 static GtkButtonClass *parent_class = NULL;
141 static guint signals[LAST_SIGNAL] = { 0 };
143 G_DEFINE_TYPE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON)
146 gtk_scale_button_class_init (GtkScaleButtonClass *klass)
148 GtkBindingSet *binding_set;
149 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
150 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
152 parent_class = g_type_class_peek_parent (klass);
154 gobject_class->dispose = gtk_scale_button_dispose;
155 gobject_class->set_property = gtk_scale_button_set_property;
156 gobject_class->get_property = gtk_scale_button_get_property;
157 widget_class->button_press_event = gtk_scale_button_press;
158 widget_class->key_release_event = gtk_scale_button_key_release;
159 widget_class->scroll_event = gtk_scale_button_scroll;
160 widget_class->screen_changed = gtk_scale_button_screen_changed;
162 g_object_class_install_property (gobject_class,
164 g_param_spec_double ("value",
166 P_("The value of the scale"),
172 g_object_class_install_property (gobject_class,
174 g_param_spec_enum ("size",
178 GTK_ICON_SIZE_SMALL_TOOLBAR,
181 g_object_class_install_property (gobject_class,
183 g_param_spec_object ("adjustment",
185 P_("The GtkAdjustment that contains the current value of this scale button object"),
190 * GtkScaleButton::value-changed:
191 * @button: the object that received the signal
193 * Emitted when the value field has changed.
198 signals[VALUE_CHANGED] =
199 g_signal_new (I_("value-changed"),
200 G_TYPE_FROM_CLASS (klass),
202 G_STRUCT_OFFSET (GtkScaleButtonClass, value_changed),
204 _gtk_marshal_VOID__DOUBLE,
205 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
207 _gtk_binding_signal_new (I_("popup"),
208 G_OBJECT_CLASS_TYPE (klass),
209 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
210 G_CALLBACK (gtk_scale_button_popup_from_bindings),
212 g_cclosure_marshal_VOID__VOID,
215 _gtk_binding_signal_new (I_("popdown"),
216 G_OBJECT_CLASS_TYPE (klass),
217 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
218 G_CALLBACK (gtk_scale_button_popdown_from_bindings),
220 g_cclosure_marshal_VOID__VOID,
224 binding_set = gtk_binding_set_by_class (widget_class);
226 gtk_binding_entry_add_signal (binding_set, GDK_space, 0,
228 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
230 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
232 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
234 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
239 gtk_scale_button_init (GtkScaleButton *button)
241 GtkWidget *frame, *box;
242 GtkScaleButtonPrivate *priv;
244 button->priv = g_new0 (GtkScaleButtonPrivate, 1);
247 priv->timeout = FALSE;
249 priv->click_timeout = CLICK_TIMEOUT;
251 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
252 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
255 priv->image = gtk_image_new ();
256 gtk_container_add (GTK_CONTAINER (button), priv->image);
257 gtk_widget_show_all (priv->image);
260 priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
261 g_signal_connect (priv->dock, "button-press-event",
262 G_CALLBACK (cb_dock_button_press), button);
263 g_signal_connect (priv->dock, "key-release-event",
264 G_CALLBACK (cb_dock_key_release), button);
265 g_signal_connect (priv->dock, "grab-notify",
266 G_CALLBACK (cb_dock_grab_notify), button);
267 g_signal_connect (priv->dock, "grab-broken-event",
268 G_CALLBACK (cb_dock_grab_broken_event), button);
269 gtk_window_set_decorated (GTK_WINDOW (priv->dock), FALSE);
272 frame = gtk_frame_new (NULL);
273 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
274 gtk_container_add (GTK_CONTAINER (priv->dock), frame);
275 box = gtk_vbox_new (FALSE, 0);
276 gtk_container_add (GTK_CONTAINER (frame), box);
279 button->plus_button = gtk_button_new_with_label ("+");
280 gtk_button_set_relief (GTK_BUTTON (button->plus_button), GTK_RELIEF_NONE);
281 g_signal_connect (button->plus_button, "button-press-event",
282 G_CALLBACK (cb_button_press), button);
283 g_signal_connect (button->plus_button, "button-release-event",
284 G_CALLBACK (cb_button_release), button);
285 gtk_box_pack_start (GTK_BOX (box), button->plus_button, TRUE, FALSE, 0);
288 priv->scale = gtk_scale_button_scale_new (button, 0., 100., 2.);
289 gtk_widget_set_size_request (priv->scale, -1, SCALE_SIZE);
290 gtk_scale_set_draw_value (GTK_SCALE (priv->scale), FALSE);
291 gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
292 gtk_box_pack_start (GTK_BOX (box), priv->scale, TRUE, FALSE, 0);
293 g_signal_connect (priv->scale, "grab-notify",
294 G_CALLBACK (cb_scale_grab_notify), button);
297 button->minus_button = gtk_button_new_with_label ("-");
298 gtk_button_set_relief (GTK_BUTTON (button->minus_button), GTK_RELIEF_NONE);
299 g_signal_connect (button->minus_button, "button-press-event",
300 G_CALLBACK (cb_button_press), button);
301 g_signal_connect (button->minus_button, "button-release-event",
302 G_CALLBACK (cb_button_release), button);
303 gtk_box_pack_start (GTK_BOX (box), button->minus_button, TRUE, FALSE, 0);
305 /* set button text and size */
306 priv->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
307 gtk_scale_button_update_icon (button);
311 gtk_scale_button_set_property (GObject *object,
316 GtkScaleButton *button;
318 button = GTK_SCALE_BUTTON (object);
323 gtk_scale_button_set_value (button, g_value_get_double (value));
328 size = g_value_get_enum (value);
329 if (button->priv->size != size)
331 button->priv->size = size;
332 gtk_scale_button_update_icon (button);
336 case PROP_ADJUSTMENT:
337 gtk_scale_button_set_adjustment (button, g_value_get_object (value));
340 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 gtk_scale_button_get_property (GObject *object,
351 GtkScaleButton *button;
352 GtkScaleButtonPrivate *priv;
354 button = GTK_SCALE_BUTTON (object);
360 g_value_set_float (value, gtk_scale_button_get_value (button));
363 g_value_set_enum (value, priv->size);
365 case PROP_ADJUSTMENT:
366 g_value_set_object (value, gtk_scale_button_get_adjustment (button));
369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
375 gtk_scale_button_dispose (GObject *object)
377 GtkScaleButton *button;
378 GtkScaleButtonPrivate *priv;
380 button = GTK_SCALE_BUTTON (object);
384 if (priv && priv->dock)
386 gtk_widget_destroy (priv->dock);
390 if (priv && priv->click_id != 0)
392 g_source_remove (priv->click_id);
396 if (priv && priv->icon_list)
398 g_strfreev (priv->icon_list);
399 priv->icon_list = NULL;
405 G_OBJECT_CLASS (parent_class)->dispose (object);
409 * gtk_scale_button_new
410 * @size: a stock icon size
411 * @lower: the minimum value of the scale (usually 0)
412 * @upper: the maximum value of the scale (usually 100)
413 * @step_increment: the stepping of value when a scroll-wheel event,
414 * or up/down arrow event occurs (usually 2)
415 * @icons: a %NULL-terminated array of icon names, or %NULL if
416 * you want to set the list later with gtk_scale_button_set_icons(). See
417 * gtk_scale_button_set_icons() for details.
419 * Creates a #GtkScaleButton, with a range between lower and upper, with
420 * a stepping of step_increment.
422 * Return value: a new #GtkScaleButton
427 gtk_scale_button_new (GtkIconSize size,
430 gdouble step_increment,
433 GtkScaleButton *button;
434 GtkScaleButtonPrivate *priv;
436 button = g_object_new (GTK_TYPE_SCALE_BUTTON, NULL);
440 gtk_scale_button_set_icons (button, icons);
442 gtk_range_set_range (GTK_RANGE (priv->scale), lower, upper);
443 gtk_range_set_increments (GTK_RANGE (priv->scale),
445 10 * step_increment);
447 if (priv->size != size)
450 gtk_scale_button_update_icon (button);
453 return GTK_WIDGET (button);
457 * gtk_scale_button_get_value
458 * @button: a #GtkScaleButton
460 * Gets the current value of the scale button.
462 * Return value: current value of the scale button
467 gtk_scale_button_get_value (GtkScaleButton * button)
469 GtkScaleButtonPrivate *priv;
471 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), 0);
475 return gtk_range_get_value (GTK_RANGE (priv->scale));
479 * gtk_scale_button_set_value
480 * @button: a #GtkScaleButton
481 * @value: new value of the scale button
483 * Sets the current value of the scale; if the value is outside the minimum or
484 * maximum range values, it will be clamped to fit inside them. The scale button
485 * emits the "value_changed" signal if the value changes.
490 gtk_scale_button_set_value (GtkScaleButton *button,
493 GtkScaleButtonPrivate *priv;
495 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
499 gtk_range_set_value (GTK_RANGE (priv->scale), value);
503 * gtk_scale_button_set_icons
504 * @button: a #GtkScaleButton
505 * @icons: a %NULL-terminated array of icon names
507 * Sets the icons to be used by the scale button. The first item in the array
508 * will be used in the button when the current value is the lowest value, the
509 * second item for the highest value. All the subsequent icons will be used for
510 * all the other values, spread evenly over the range of values.
512 * If there's only one icon in the @icons array, it will be used for all the
513 * values. If only two icons are in the @icons array, the first one will be
514 * used for the bottom 50% of the scale, and the second one for the top 50%.
515 * So it is recommended to use at least 3 icons so that the #GtkScaleButton
516 * reflects the current value of the scale better for the users.
521 gtk_scale_button_set_icons (GtkScaleButton *button,
524 GtkScaleButtonPrivate *priv;
526 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
527 g_return_if_fail (icons != NULL);
528 g_return_if_fail (icons[0] != NULL);
532 g_strfreev (priv->icon_list);
533 priv->icon_list = g_strdupv ((gchar **) icons);
534 gtk_scale_button_update_icon (button);
538 * gtk_scale_button_get_adjustment
539 * @button: a #GtkScaleButton
541 * Gets the #GtkAdjustment associated with the #GtkScaleButton's scale.
542 * See gtk_range_get_adjustment() for details.
547 gtk_scale_button_get_adjustment (GtkScaleButton *button)
551 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
553 range = GTK_RANGE (button->priv->scale);
554 g_return_val_if_fail (range != NULL, NULL);
556 return gtk_range_get_adjustment (range);
560 * gtk_scale_button_set_adjustment
561 * @button: a #GtkScaleButton
562 * @adjustment: a #GtkAdjustment
564 * Sets the #GtkAdjustment to be used as a model for the #GtkScaleButton's scale.
565 * See gtk_range_set_adjustment() for details.
570 gtk_scale_button_set_adjustment (GtkScaleButton *button,
571 GtkAdjustment *adjustment)
573 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
574 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
576 gtk_range_set_adjustment (GTK_RANGE (button->priv->scale),
585 gtk_scale_button_scroll (GtkWidget *widget,
586 GdkEventScroll *event)
588 GtkScaleButton *button;
589 GtkScaleButtonPrivate *priv;
593 button = GTK_SCALE_BUTTON (widget);
595 adj = gtk_range_get_adjustment (GTK_RANGE (priv->scale));
597 if (event->type != GDK_SCROLL)
600 d = gtk_scale_button_get_value (button);
601 if (event->direction == GDK_SCROLL_UP)
603 d += adj->step_increment;
609 d -= adj->step_increment;
613 gtk_scale_button_set_value (button, d);
619 gtk_scale_button_screen_changed (GtkWidget *widget,
620 GdkScreen *previous_screen)
622 GtkScaleButton *button = (GtkScaleButton *) widget;
623 GtkScaleButtonPrivate *priv;
625 GValue value = { 0, };
627 if (gtk_widget_has_screen (widget) == FALSE)
632 screen = gtk_widget_get_screen (widget);
633 g_value_init (&value, G_TYPE_INT);
634 if (gdk_screen_get_setting (screen,
635 "gtk-double-click-time",
638 priv->click_timeout = CLICK_TIMEOUT;
642 priv->click_timeout = g_value_get_int (&value);
646 gtk_scale_popup (GtkWidget *widget,
650 GtkScaleButton *button;
651 GtkScaleButtonPrivate *priv;
653 gint x, y, m, dx, dy, sx, sy, ystartoff;
658 button = GTK_SCALE_BUTTON (widget);
660 adj = gtk_range_get_adjustment (GTK_RANGE (priv->scale));
662 display = gtk_widget_get_display (widget);
663 screen = gtk_widget_get_screen (widget);
665 /* position roughly */
666 gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
668 gdk_window_get_origin (widget->window, &x, &y);
669 x += widget->allocation.x;
670 y += widget->allocation.y;
671 gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
672 gtk_widget_show_all (priv->dock);
673 gdk_window_get_origin (priv->dock->window, &dx, &dy);
674 dy += priv->dock->allocation.y;
675 gdk_window_get_origin (priv->scale->window, &sx, &sy);
676 sy += priv->scale->allocation.y;
678 priv->timeout = TRUE;
680 /* position (needs widget to be shown already) */
681 v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
682 x += (widget->allocation.width - priv->dock->allocation.width) / 2;
684 y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
685 m = priv->scale->allocation.height -
686 GTK_RANGE (priv->scale)->min_slider_size;
689 /* Make sure the dock stays inside the monitor */
690 if (event->type == GDK_BUTTON_PRESS)
693 GdkEventButton *button_event = (GdkEventButton *) event;
697 d = GTK_WIDGET (priv->dock);
698 monitor = gdk_screen_get_monitor_at_point (screen,
699 button_event->x_root,
700 button_event->y_root);
701 gdk_screen_get_monitor_geometry (screen, monitor, &rect);
703 y += button_event->y;
706 else if (y + d->allocation.height > rect.height + rect.y)
707 y = rect.y + rect.height - d->allocation.height;
711 else if (x + d->allocation.width > rect.width + rect.x)
712 x = rect.x + rect.width - d->allocation.width;
715 gtk_window_move (GTK_WINDOW (priv->dock), x, y);
717 if (event->type == GDK_BUTTON_PRESS)
718 GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, (GdkEventButton *) event);
721 gtk_grab_add (priv->dock);
723 if (gdk_pointer_grab (priv->dock->window, TRUE,
724 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
725 GDK_POINTER_MOTION_MASK, NULL, NULL, time)
728 gtk_grab_remove (priv->dock);
729 gtk_widget_hide (priv->dock);
733 if (gdk_keyboard_grab (priv->dock->window, TRUE, time) != GDK_GRAB_SUCCESS)
735 gdk_display_pointer_ungrab (display, time);
736 gtk_grab_remove (priv->dock);
737 gtk_widget_hide (priv->dock);
741 gtk_widget_grab_focus (priv->dock);
743 if (event->type == GDK_BUTTON_PRESS)
746 GdkEventButton *button_event = (GdkEventButton *) event;
748 /* forward event to the slider */
749 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
750 e->window = priv->scale->window;
752 /* position: the X position isn't relevant, halfway will work just fine.
753 * The vertical position should be *exactly* in the middle of the slider
754 * of the scale; if we don't do that correctly, it'll move from its current
755 * position, which means a position change on-click, which is bad. */
756 e->x = priv->scale->allocation.width / 2;
757 m = priv->scale->allocation.height -
758 GTK_RANGE (priv->scale)->min_slider_size;
759 e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
760 gtk_widget_event (priv->scale, (GdkEvent *) e);
761 e->window = button_event->window;
762 gdk_event_free ((GdkEvent *) e);
765 gtk_widget_grab_focus (priv->scale);
767 priv->pop_time = time;
773 gtk_scale_button_press (GtkWidget *widget,
774 GdkEventButton *event)
776 return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
780 gtk_scale_button_popup_from_bindings (GtkWidget *widget)
784 ev = gdk_event_new (GDK_KEY_RELEASE);
785 gtk_scale_popup (widget, ev, GDK_CURRENT_TIME);
790 gtk_scale_button_key_release (GtkWidget *widget,
793 return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
796 /* This is called when the grab is broken for
797 * either the dock, or the scale itself */
799 gtk_scale_button_grab_notify (GtkScaleButton *button,
800 gboolean was_grabbed)
803 GtkScaleButtonPrivate *priv;
805 if (was_grabbed != FALSE)
810 if (gtk_widget_is_ancestor (gtk_grab_get_current (), priv->dock))
813 display = gtk_widget_get_display (priv->dock);
814 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
815 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
816 gtk_grab_remove (priv->dock);
819 gtk_widget_hide (priv->dock);
820 priv->timeout = FALSE;
824 * +/- button callbacks.
828 cb_button_timeout (gpointer user_data)
830 GtkScaleButton *button;
831 GtkScaleButtonPrivate *priv;
836 button = GTK_SCALE_BUTTON (user_data);
839 if (priv->click_id == 0)
842 adj = gtk_range_get_adjustment (GTK_RANGE (priv->scale));
844 val = gtk_scale_button_get_value (button);
845 val += priv->direction;
846 if (val <= adj->lower)
851 else if (val > adj->upper)
856 gtk_scale_button_set_value (button, val);
860 g_source_remove (priv->click_id);
868 cb_button_press (GtkWidget *widget,
869 GdkEventButton *event,
872 GtkScaleButton *button;
873 GtkScaleButtonPrivate *priv;
876 button = GTK_SCALE_BUTTON (user_data);
878 adj = gtk_range_get_adjustment (GTK_RANGE (priv->scale));
880 if (priv->click_id != 0)
881 g_source_remove (priv->click_id);
883 if (widget == button->plus_button)
884 priv->direction = fabs (adj->page_increment);
886 priv->direction = - fabs (adj->page_increment);
888 priv->click_id = g_timeout_add (priv->click_timeout,
889 (GSourceFunc) cb_button_timeout, button);
890 cb_button_timeout (button);
896 cb_button_release (GtkWidget *widget,
897 GdkEventButton *event,
900 GtkScaleButton *button;
901 GtkScaleButtonPrivate *priv;
903 button = GTK_SCALE_BUTTON (user_data);
906 if (priv->click_id != 0)
908 g_source_remove (priv->click_id);
916 cb_dock_grab_notify (GtkWidget *widget,
917 gboolean was_grabbed,
920 GtkScaleButton *button = (GtkScaleButton *) user_data;
922 gtk_scale_button_grab_notify (button, was_grabbed);
926 cb_dock_grab_broken_event (GtkWidget *widget,
927 gboolean was_grabbed,
930 GtkScaleButton *button = (GtkScaleButton *) user_data;
932 gtk_scale_button_grab_notify (button, FALSE);
942 gtk_scale_button_release_grab (GtkScaleButton *button,
943 GdkEventButton *event)
947 GtkScaleButtonPrivate *priv;
952 display = gtk_widget_get_display (GTK_WIDGET (button));
953 gdk_display_keyboard_ungrab (display, event->time);
954 gdk_display_pointer_ungrab (display, event->time);
955 gtk_grab_remove (priv->dock);
958 gtk_widget_hide (priv->dock);
959 priv->timeout = FALSE;
961 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
962 e->window = GTK_WIDGET (button)->window;
963 e->type = GDK_BUTTON_RELEASE;
964 gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
965 e->window = event->window;
966 gdk_event_free ((GdkEvent *) e);
970 cb_dock_button_press (GtkWidget *widget,
971 GdkEventButton *event,
974 GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
976 if (event->type == GDK_BUTTON_PRESS)
978 gtk_scale_button_release_grab (button, event);
986 gtk_scale_button_popdown_from_bindings (GtkWidget *widget)
988 GtkScaleButton *button;
989 GtkScaleButtonPrivate *priv;
992 button = GTK_SCALE_BUTTON (widget);
996 display = gtk_widget_get_display (widget);
997 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
998 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
999 gtk_grab_remove (priv->dock);
1002 gtk_widget_hide (priv->dock);
1003 priv->timeout = FALSE;
1007 cb_dock_key_release (GtkWidget *widget,
1011 if (event->keyval == GDK_Escape)
1013 gtk_scale_button_popdown_from_bindings (GTK_WIDGET (user_data));
1017 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
1019 /* The popup hasn't managed the event, pass onto the button */
1020 gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
1027 cb_scale_grab_notify (GtkWidget *widget,
1028 gboolean was_grabbed,
1031 GtkScaleButton *button = (GtkScaleButton *) user_data;
1033 gtk_scale_button_grab_notify (button, was_grabbed);
1040 #define GTK_TYPE_SCALE_BUTTON_SCALE \
1041 (gtk_scale_button_scale_get_type ())
1042 #define GTK_SCALE_BUTTON_SCALE(obj) \
1043 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, \
1044 GtkScaleButtonScale))
1046 typedef struct _GtkScaleButtonScaleClass {
1047 GtkVScaleClass parent_class;
1048 } GtkScaleButtonScaleClass;
1050 typedef struct _GtkScaleButtonScale {
1052 GtkScaleButton *button;
1053 } GtkScaleButtonScale;
1055 static GType gtk_scale_button_scale_get_type (void);
1056 static void gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass);
1057 static gboolean gtk_scale_button_scale_press (GtkWidget *widget,
1058 GdkEventButton *event);
1059 static gboolean gtk_scale_button_scale_release (GtkWidget *widget,
1060 GdkEventButton *event);
1062 static GtkVScaleClass *scale_parent_class = NULL;
1064 G_DEFINE_TYPE (GtkScaleButtonScale, gtk_scale_button_scale, GTK_TYPE_VSCALE)
1067 gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass)
1069 GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1070 GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1072 scale_parent_class = g_type_class_peek_parent (klass);
1074 gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1075 gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1076 gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1080 gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
1085 gtk_scale_button_scale_new (GtkScaleButton *button,
1090 GtkScaleButtonScale *scale;
1091 GtkScaleButtonPrivate *priv;
1094 priv = button->priv;
1095 scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE, NULL);
1096 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1097 gtk_range_set_adjustment (GTK_RANGE (scale), GTK_ADJUSTMENT (adj));
1098 scale->button = button;
1100 return GTK_WIDGET (scale);
1104 gtk_scale_button_scale_press (GtkWidget *widget,
1105 GdkEventButton *event)
1107 GtkScaleButtonScale *scale;
1108 GtkScaleButtonPrivate *priv;
1110 scale = GTK_SCALE_BUTTON_SCALE (widget);
1111 priv = scale->button->priv;
1113 /* the scale will grab input; if we have input grabbed, all goes
1114 * horribly wrong, so let's not do that. */
1115 gtk_grab_remove (priv->dock);
1117 return GTK_WIDGET_CLASS (scale_parent_class)->button_press_event (widget, event);
1121 gtk_scale_button_scale_release (GtkWidget *widget,
1122 GdkEventButton *event)
1124 GtkScaleButtonScale *scale;
1125 GtkScaleButtonPrivate *priv;
1128 scale = GTK_SCALE_BUTTON_SCALE (widget);
1129 priv = scale->button->priv;
1133 /* if we did a quick click, leave the window open; else, hide it */
1134 if (event->time > priv->pop_time + priv->click_timeout)
1136 gtk_scale_button_release_grab (scale->button, event);
1137 GTK_WIDGET_CLASS (scale_parent_class)->button_release_event (widget, event);
1140 priv->timeout = FALSE;
1143 res = GTK_WIDGET_CLASS (scale_parent_class)->button_release_event (widget, event);
1145 /* the scale will release input; right after that, we *have to* grab
1146 * it back so we can catch out-of-scale clicks and hide the popup,
1147 * so I basically want a g_signal_connect_after_always(), but I can't
1148 * find that, so we do this complex 'first-call-parent-then-do-actual-
1149 * action' thingy... */
1150 gtk_grab_add (priv->dock);
1156 gtk_scale_button_update_icon (GtkScaleButton *button)
1158 GtkScaleButtonPrivate *priv;
1165 priv = button->priv;
1167 if (priv->icon_list == NULL || priv->icon_list[0] == NULL)
1169 gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1170 GTK_STOCK_MISSING_IMAGE,
1175 for (num_icons = 0; priv->icon_list[num_icons] != NULL; num_icons++) {}
1177 /* The 1-icon special case */
1180 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1186 range = GTK_RANGE (priv->scale);
1187 adj = gtk_range_get_adjustment (range);
1188 value = gtk_scale_button_get_value (button);
1190 /* The 2-icons special case */
1194 limit = (adj->upper - adj->lower) / 2 + adj->lower;
1196 name = priv->icon_list[0];
1198 name = priv->icon_list[1];
1200 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1206 /* With 3 or more icons */
1207 if (value == adj->lower)
1209 name = priv->icon_list[0];
1211 else if (value == adj->upper)
1213 name = priv->icon_list[1];
1220 step = (adj->upper - adj->lower) / (num_icons - 2);
1221 i = (guint) (value - adj->lower) / step + 2;
1222 g_assert (i < num_icons);
1223 name = priv->icon_list[i];
1226 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1232 gtk_scale_button_scale_value_changed (GtkRange *range)
1234 GtkScaleButtonScale *scale = GTK_SCALE_BUTTON_SCALE (range);
1235 GtkScaleButton *button = scale->button;
1238 value = gtk_range_get_value (range);
1240 gtk_scale_button_update_icon (button);
1242 g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1243 g_object_notify (G_OBJECT (button), "value");
1247 #define __GTK_SCALE_BUTTON_C__
1248 #include "gtkaliasdef.c"