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/.
43 #include <gdk-pixbuf/gdk-pixbuf.h>
44 #include <gdk/gdkkeysyms.h>
48 #include "gtkbindings.h"
49 #include "gtkhscale.h"
50 #include "gtkvscale.h"
54 #include "gtkwindow.h"
55 #include "gtkmarshalers.h"
57 #include "gtkprivate.h"
58 #include "gtkscalebutton.h"
62 #define SCALE_SIZE 100
63 #define CLICK_TIMEOUT 250
85 #define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SCALE_BUTTON, GtkScaleButtonPrivate))
87 struct _GtkScaleButtonPrivate
94 GtkOrientation orientation;
104 GtkAdjustment *adjustment; /* needed because it must be settable in init() */
107 static GObject* gtk_scale_button_constructor (GType type,
108 guint n_construct_properties,
109 GObjectConstructParam *construct_params);
110 static void gtk_scale_button_dispose (GObject *object);
111 static void gtk_scale_button_finalize (GObject *object);
112 static void gtk_scale_button_set_property (GObject *object,
116 static void gtk_scale_button_get_property (GObject *object,
120 static gboolean gtk_scale_button_scroll (GtkWidget *widget,
121 GdkEventScroll *event);
122 static void gtk_scale_button_screen_changed (GtkWidget *widget,
123 GdkScreen *previous_screen);
124 static gboolean gtk_scale_button_press (GtkWidget *widget,
125 GdkEventButton *event);
126 static gboolean gtk_scale_button_key_release (GtkWidget *widget,
128 static void gtk_scale_button_popup (GtkWidget *widget);
129 static void gtk_scale_button_popdown (GtkWidget *widget);
130 static gboolean cb_dock_button_press (GtkWidget *widget,
131 GdkEventButton *event,
133 static gboolean cb_dock_key_release (GtkWidget *widget,
136 static gboolean cb_button_press (GtkWidget *widget,
137 GdkEventButton *event,
139 static gboolean cb_button_release (GtkWidget *widget,
140 GdkEventButton *event,
142 static void cb_dock_grab_notify (GtkWidget *widget,
143 gboolean was_grabbed,
145 static gboolean cb_dock_grab_broken_event (GtkWidget *widget,
146 gboolean was_grabbed,
148 static void cb_scale_grab_notify (GtkWidget *widget,
149 gboolean was_grabbed,
151 static void gtk_scale_button_update_icon (GtkScaleButton *button);
152 static void gtk_scale_button_scale_value_changed(GtkRange *range);
154 /* see below for scale definitions */
155 static GtkWidget *gtk_scale_button_scale_box_new(GtkScaleButton *button);
157 static guint signals[LAST_SIGNAL] = { 0, };
159 G_DEFINE_TYPE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON)
162 gtk_scale_button_class_init (GtkScaleButtonClass *klass)
164 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
165 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
166 GtkBindingSet *binding_set;
168 g_type_class_add_private (klass, sizeof (GtkScaleButtonPrivate));
170 gobject_class->constructor = gtk_scale_button_constructor;
171 gobject_class->finalize = gtk_scale_button_finalize;
172 gobject_class->dispose = gtk_scale_button_dispose;
173 gobject_class->set_property = gtk_scale_button_set_property;
174 gobject_class->get_property = gtk_scale_button_get_property;
176 widget_class->button_press_event = gtk_scale_button_press;
177 widget_class->key_release_event = gtk_scale_button_key_release;
178 widget_class->scroll_event = gtk_scale_button_scroll;
179 widget_class->screen_changed = gtk_scale_button_screen_changed;
182 * GtkScaleButton:orientation:
184 * The orientation of the #GtkScaleButton's popup window.
188 g_object_class_install_property (gobject_class,
190 g_param_spec_enum ("orientation",
192 P_("The orientation of the scale"),
193 GTK_TYPE_ORIENTATION,
194 GTK_ORIENTATION_VERTICAL,
195 GTK_PARAM_READWRITE));
197 g_object_class_install_property (gobject_class,
199 g_param_spec_double ("value",
201 P_("The value of the scale"),
205 GTK_PARAM_READWRITE));
207 g_object_class_install_property (gobject_class,
209 g_param_spec_enum ("size",
213 GTK_ICON_SIZE_SMALL_TOOLBAR,
214 GTK_PARAM_READWRITE));
216 g_object_class_install_property (gobject_class,
218 g_param_spec_object ("adjustment",
220 P_("The GtkAdjustment that contains the current value of this scale button object"),
222 GTK_PARAM_READWRITE));
225 * GtkScaleButton:icons:
227 * The names of the icons to be used by the scale button.
228 * The first item in the array will be used in the button
229 * when the current value is the lowest value, the second
230 * item for the highest value. All the subsequent icons will
231 * be used for all the other values, spread evenly over the
234 * If there's only one icon name in the @icons array, it will
235 * be used for all the values. If only two icon names are in
236 * the @icons array, the first one will be used for the bottom
237 * 50% of the scale, and the second one for the top 50%.
239 * It is recommended to use at least 3 icons so that the
240 * #GtkScaleButton reflects the current value of the scale
241 * better for the users.
245 g_object_class_install_property (gobject_class,
247 g_param_spec_boxed ("icons",
249 P_("List of icon names"),
251 GTK_PARAM_READWRITE));
254 * GtkScaleButton::value-changed:
255 * @button: the object which received the signal
256 * @value: the new value
258 * The ::value-changed signal is emitted when the value field has
263 signals[VALUE_CHANGED] =
264 g_signal_new (I_("value-changed"),
265 G_TYPE_FROM_CLASS (klass),
267 G_STRUCT_OFFSET (GtkScaleButtonClass, value_changed),
269 _gtk_marshal_VOID__DOUBLE,
270 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
273 * GtkScaleButton::popup:
274 * @button: the object which received the signal
276 * The ::popup signal is a
277 * <link linkend="keybinding-signals">keybinding signal</link>
278 * which gets emitted to popup the scale widget.
280 * The default bindings for this signal are Space, Enter and Return.
285 g_signal_new_class_handler (I_("popup"),
286 G_OBJECT_CLASS_TYPE (klass),
287 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
288 G_CALLBACK (gtk_scale_button_popup),
290 g_cclosure_marshal_VOID__VOID,
294 * GtkScaleButton::popdown:
295 * @button: the object which received the signal
297 * The ::popdown signal is a
298 * <link linkend="keybinding-signals">keybinding signal</link>
299 * which gets emitted to popdown the scale widget.
301 * The default binding for this signal is Escape.
306 g_signal_new_class_handler (I_("popdown"),
307 G_OBJECT_CLASS_TYPE (klass),
308 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
309 G_CALLBACK (gtk_scale_button_popdown),
311 g_cclosure_marshal_VOID__VOID,
315 binding_set = gtk_binding_set_by_class (widget_class);
317 gtk_binding_entry_add_signal (binding_set, GDK_space, 0,
319 gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, 0,
321 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
323 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
325 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
327 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
332 gtk_scale_button_init (GtkScaleButton *button)
334 GtkScaleButtonPrivate *priv;
336 button->priv = priv = GET_PRIVATE (button);
338 priv->timeout = FALSE;
340 priv->click_timeout = CLICK_TIMEOUT;
341 priv->orientation = GTK_ORIENTATION_VERTICAL;
343 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
344 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
347 priv->image = gtk_image_new ();
348 gtk_container_add (GTK_CONTAINER (button), priv->image);
349 gtk_widget_show_all (priv->image);
352 priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
353 gtk_widget_set_name (priv->dock, "gtk-scalebutton-popup-window");
354 g_signal_connect (priv->dock, "button-press-event",
355 G_CALLBACK (cb_dock_button_press), button);
356 g_signal_connect (priv->dock, "key-release-event",
357 G_CALLBACK (cb_dock_key_release), button);
358 g_signal_connect (priv->dock, "grab-notify",
359 G_CALLBACK (cb_dock_grab_notify), button);
360 g_signal_connect (priv->dock, "grab-broken-event",
361 G_CALLBACK (cb_dock_grab_broken_event), button);
362 gtk_window_set_decorated (GTK_WINDOW (priv->dock), FALSE);
365 button->plus_button = gtk_button_new_with_label ("+");
366 gtk_button_set_relief (GTK_BUTTON (button->plus_button), GTK_RELIEF_NONE);
367 g_signal_connect (button->plus_button, "button-press-event",
368 G_CALLBACK (cb_button_press), button);
369 g_signal_connect (button->plus_button, "button-release-event",
370 G_CALLBACK (cb_button_release), button);
373 button->minus_button = gtk_button_new_with_label ("-");
374 gtk_button_set_relief (GTK_BUTTON (button->minus_button), GTK_RELIEF_NONE);
375 g_signal_connect (button->minus_button, "button-press-event",
376 G_CALLBACK (cb_button_press), button);
377 g_signal_connect (button->minus_button, "button-release-event",
378 G_CALLBACK (cb_button_release), button);
380 priv->adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 100.0, 2, 20, 0));
381 g_object_ref_sink (priv->adjustment);
385 gtk_scale_button_constructor (GType type,
386 guint n_construct_properties,
387 GObjectConstructParam *construct_params)
390 GtkScaleButton *button;
391 GtkWidget *frame, *box;
392 GtkScaleButtonPrivate *priv;
394 object = G_OBJECT_CLASS (gtk_scale_button_parent_class)->constructor (type, n_construct_properties, construct_params);
396 button = GTK_SCALE_BUTTON (object);
401 frame = gtk_frame_new (NULL);
402 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
403 gtk_container_add (GTK_CONTAINER (priv->dock), frame);
405 /* box with scale and +/- buttons */
406 box = gtk_scale_button_scale_box_new (button);
407 gtk_container_add (GTK_CONTAINER (frame), box);
409 /* set button text and size */
410 priv->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
411 gtk_scale_button_update_icon (button);
417 gtk_scale_button_set_property (GObject *object,
422 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
426 case PROP_ORIENTATION:
427 gtk_scale_button_set_orientation (button, g_value_get_enum (value));
430 gtk_scale_button_set_value (button, g_value_get_double (value));
435 size = g_value_get_enum (value);
436 if (button->priv->size != size)
438 button->priv->size = size;
439 gtk_scale_button_update_icon (button);
443 case PROP_ADJUSTMENT:
444 gtk_scale_button_set_adjustment (button, g_value_get_object (value));
447 gtk_scale_button_set_icons (button,
448 (const gchar **)g_value_get_boxed (value));
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
457 gtk_scale_button_get_property (GObject *object,
462 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
463 GtkScaleButtonPrivate *priv = button->priv;
467 case PROP_ORIENTATION:
468 g_value_set_enum (value, priv->orientation);
471 g_value_set_double (value, gtk_scale_button_get_value (button));
474 g_value_set_enum (value, priv->size);
476 case PROP_ADJUSTMENT:
477 g_value_set_object (value, gtk_scale_button_get_adjustment (button));
480 g_value_set_boxed (value, priv->icon_list);
483 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
489 gtk_scale_button_finalize (GObject *object)
491 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
492 GtkScaleButtonPrivate *priv = button->priv;
496 g_strfreev (priv->icon_list);
497 priv->icon_list = NULL;
500 if (priv->adjustment)
502 g_object_unref (priv->adjustment);
503 priv->adjustment = NULL;
506 G_OBJECT_CLASS (gtk_scale_button_parent_class)->finalize (object);
510 gtk_scale_button_dispose (GObject *object)
512 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
513 GtkScaleButtonPrivate *priv = button->priv;
517 gtk_widget_destroy (priv->dock);
521 if (priv->click_id != 0)
523 g_source_remove (priv->click_id);
527 G_OBJECT_CLASS (gtk_scale_button_parent_class)->dispose (object);
531 * gtk_scale_button_new:
532 * @size: a stock icon size
533 * @min: the minimum value of the scale (usually 0)
534 * @max: the maximum value of the scale (usually 100)
535 * @step: the stepping of value when a scroll-wheel event,
536 * or up/down arrow event occurs (usually 2)
537 * @icons: a %NULL-terminated array of icon names, or %NULL if
538 * you want to set the list later with gtk_scale_button_set_icons()
540 * Creates a #GtkScaleButton, with a range between @min and @max, with
541 * a stepping of @step.
543 * Return value: a new #GtkScaleButton
548 gtk_scale_button_new (GtkIconSize size,
554 GtkScaleButton *button;
557 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
559 button = g_object_new (GTK_TYPE_SCALE_BUTTON,
565 return GTK_WIDGET (button);
569 * gtk_scale_button_get_value:
570 * @button: a #GtkScaleButton
572 * Gets the current value of the scale button.
574 * Return value: current value of the scale button
579 gtk_scale_button_get_value (GtkScaleButton * button)
581 GtkScaleButtonPrivate *priv;
583 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), 0);
587 return gtk_adjustment_get_value (priv->adjustment);
591 * gtk_scale_button_set_value:
592 * @button: a #GtkScaleButton
593 * @value: new value of the scale button
595 * Sets the current value of the scale; if the value is outside
596 * the minimum or maximum range values, it will be clamped to fit
597 * inside them. The scale button emits the #GtkScaleButton::value-changed
598 * signal if the value changes.
603 gtk_scale_button_set_value (GtkScaleButton *button,
606 GtkScaleButtonPrivate *priv;
608 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
612 gtk_range_set_value (GTK_RANGE (priv->scale), value);
616 * gtk_scale_button_set_icons:
617 * @button: a #GtkScaleButton
618 * @icons: a %NULL-terminated array of icon names
620 * Sets the icons to be used by the scale button.
621 * For details, see the #GtkScaleButton:icons property.
626 gtk_scale_button_set_icons (GtkScaleButton *button,
629 GtkScaleButtonPrivate *priv;
632 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
636 tmp = priv->icon_list;
637 priv->icon_list = g_strdupv ((gchar **) icons);
639 gtk_scale_button_update_icon (button);
641 g_object_notify (G_OBJECT (button), "icons");
645 * gtk_scale_button_get_adjustment:
646 * @button: a #GtkScaleButton
648 * Gets the #GtkAdjustment associated with the #GtkScaleButton's scale.
649 * See gtk_range_get_adjustment() for details.
651 * Returns: the adjustment associated with the scale
656 gtk_scale_button_get_adjustment (GtkScaleButton *button)
658 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
660 return button->priv->adjustment;
664 * gtk_scale_button_set_adjustment:
665 * @button: a #GtkScaleButton
666 * @adjustment: a #GtkAdjustment
668 * Sets the #GtkAdjustment to be used as a model
669 * for the #GtkScaleButton's scale.
670 * See gtk_range_set_adjustment() for details.
675 gtk_scale_button_set_adjustment (GtkScaleButton *button,
676 GtkAdjustment *adjustment)
678 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
679 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
681 if (button->priv->adjustment)
682 g_object_unref (button->priv->adjustment);
683 button->priv->adjustment = g_object_ref_sink (adjustment);
685 if (button->priv->scale)
686 gtk_range_set_adjustment (GTK_RANGE (button->priv->scale), adjustment);
688 g_object_notify (G_OBJECT (button), "adjustment");
692 * gtk_scale_button_get_orientation:
693 * @button: a #GtkScaleButton
695 * Gets the orientation of the #GtkScaleButton's popup window.
697 * Returns: the #GtkScaleButton's orientation.
702 gtk_scale_button_get_orientation (GtkScaleButton *button)
704 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), GTK_ORIENTATION_VERTICAL);
706 return button->priv->orientation;
710 * gtk_scale_button_set_orientation:
711 * @button: a #GtkScaleButton
712 * @orientation: the new orientation
714 * Sets the orientation of the #GtkScaleButton's popup window.
719 gtk_scale_button_set_orientation (GtkScaleButton *button,
720 GtkOrientation orientation)
722 GtkScaleButtonPrivate *priv;
724 g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
728 if (orientation != priv->orientation)
730 priv->orientation = orientation;
734 GtkWidget *box = priv->scale->parent;
735 GtkWidget *frame = box->parent;
737 g_object_ref (button->plus_button);
738 g_object_ref (button->minus_button);
740 gtk_container_remove (GTK_CONTAINER (box), button->plus_button);
741 gtk_container_remove (GTK_CONTAINER (box), button->minus_button);
742 gtk_container_remove (GTK_CONTAINER (box), priv->scale);
743 gtk_container_remove (GTK_CONTAINER (frame), box);
745 box = gtk_scale_button_scale_box_new (button);
746 gtk_container_add (GTK_CONTAINER (frame), box);
748 /* FIXME: without this, the popup window appears as a square
749 * after changing the orientation
751 gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
753 g_object_unref (button->plus_button);
754 g_object_unref (button->minus_button);
757 g_object_notify (G_OBJECT (button), "orientation");
762 * gtk_scale_button_get_plus_button:
763 * @button: a #GtkScaleButton
765 * Retrieves the plus button of the #GtkScaleButton.
770 gtk_scale_button_get_plus_button (GtkScaleButton *button)
772 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
774 return button->plus_button;
778 * gtk_scale_button_get_minus_button:
779 * @button: a #GtkScaleButton
781 * Retrieves the minus button of the #GtkScaleButton.
786 gtk_scale_button_get_minus_button (GtkScaleButton *button)
788 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
790 return button->minus_button;
794 * gtk_scale_button_get_popup:
795 * @button: a #GtkScaleButton
797 * Retrieves the popup of the #GtkScaleButton.
799 * Returns: the popup of the #GtkScaleButton
804 gtk_scale_button_get_popup (GtkScaleButton *button)
806 g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
808 return button->priv->dock;
816 gtk_scale_button_scroll (GtkWidget *widget,
817 GdkEventScroll *event)
819 GtkScaleButton *button;
820 GtkScaleButtonPrivate *priv;
824 button = GTK_SCALE_BUTTON (widget);
826 adj = priv->adjustment;
828 if (event->type != GDK_SCROLL)
831 d = gtk_scale_button_get_value (button);
832 if (event->direction == GDK_SCROLL_UP)
834 d += adj->step_increment;
840 d -= adj->step_increment;
844 gtk_scale_button_set_value (button, d);
850 gtk_scale_button_screen_changed (GtkWidget *widget,
851 GdkScreen *previous_screen)
853 GtkScaleButton *button = (GtkScaleButton *) widget;
854 GtkScaleButtonPrivate *priv;
856 GValue value = { 0, };
858 if (gtk_widget_has_screen (widget) == FALSE)
863 screen = gtk_widget_get_screen (widget);
864 g_value_init (&value, G_TYPE_INT);
865 if (gdk_screen_get_setting (screen,
866 "gtk-double-click-time",
869 priv->click_timeout = CLICK_TIMEOUT;
873 priv->click_timeout = g_value_get_int (&value);
877 gtk_scale_popup (GtkWidget *widget,
881 GtkScaleButton *button;
882 GtkScaleButtonPrivate *priv;
884 gint x, y, m, dx, dy, sx, sy, startoff;
889 button = GTK_SCALE_BUTTON (widget);
891 adj = priv->adjustment;
893 display = gtk_widget_get_display (widget);
894 screen = gtk_widget_get_screen (widget);
896 /* position roughly */
897 gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);
899 gdk_window_get_origin (widget->window, &x, &y);
900 x += widget->allocation.x;
901 y += widget->allocation.y;
903 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
904 gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
906 gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);
908 gtk_widget_show_all (priv->dock);
910 gdk_window_get_origin (priv->dock->window, &dx, &dy);
911 dx += priv->dock->allocation.x;
912 dy += priv->dock->allocation.y;
914 gdk_window_get_origin (priv->scale->window, &sx, &sy);
915 sx += priv->scale->allocation.x;
916 sy += priv->scale->allocation.y;
918 priv->timeout = TRUE;
920 /* position (needs widget to be shown already) */
921 v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
923 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
927 x += (widget->allocation.width - priv->dock->allocation.width) / 2;
929 y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
930 m = priv->scale->allocation.height -
931 GTK_RANGE (priv->scale)->min_slider_size;
939 y += (widget->allocation.height - priv->dock->allocation.height) / 2;
940 x -= GTK_RANGE (priv->scale)->min_slider_size / 2;
941 m = priv->scale->allocation.width -
942 GTK_RANGE (priv->scale)->min_slider_size;
946 /* Make sure the dock stays inside the monitor */
947 if (event->type == GDK_BUTTON_PRESS)
950 GdkEventButton *button_event = (GdkEventButton *) event;
954 d = GTK_WIDGET (priv->dock);
955 monitor = gdk_screen_get_monitor_at_point (screen,
956 button_event->x_root,
957 button_event->y_root);
958 gdk_screen_get_monitor_geometry (screen, monitor, &rect);
960 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
961 y += button_event->y;
963 x += button_event->x;
967 else if (y + d->allocation.height > rect.height + rect.y)
968 y = rect.y + rect.height - d->allocation.height;
972 else if (x + d->allocation.width > rect.width + rect.x)
973 x = rect.x + rect.width - d->allocation.width;
976 gtk_window_move (GTK_WINDOW (priv->dock), x, y);
978 if (event->type == GDK_BUTTON_PRESS)
979 GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
982 gtk_grab_add (priv->dock);
984 if (gdk_pointer_grab (priv->dock->window, TRUE,
985 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
986 GDK_POINTER_MOTION_MASK, NULL, NULL, time)
989 gtk_grab_remove (priv->dock);
990 gtk_widget_hide (priv->dock);
994 if (gdk_keyboard_grab (priv->dock->window, TRUE, time) != GDK_GRAB_SUCCESS)
996 gdk_display_pointer_ungrab (display, time);
997 gtk_grab_remove (priv->dock);
998 gtk_widget_hide (priv->dock);
1002 gtk_widget_grab_focus (priv->dock);
1004 if (event->type == GDK_BUTTON_PRESS)
1007 GdkEventButton *button_event = (GdkEventButton *) event;
1009 /* forward event to the slider */
1010 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1011 e->window = priv->scale->window;
1013 /* position: the X position isn't relevant, halfway will work just fine.
1014 * The vertical position should be *exactly* in the middle of the slider
1015 * of the scale; if we don't do that correctly, it'll move from its current
1016 * position, which means a position change on-click, which is bad.
1018 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1020 e->x = priv->scale->allocation.width / 2;
1021 m = priv->scale->allocation.height -
1022 GTK_RANGE (priv->scale)->min_slider_size;
1023 e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1027 e->y = priv->scale->allocation.height / 2;
1028 m = priv->scale->allocation.width -
1029 GTK_RANGE (priv->scale)->min_slider_size;
1030 e->x = (v * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
1033 gtk_widget_event (priv->scale, (GdkEvent *) e);
1034 e->window = button_event->window;
1035 gdk_event_free ((GdkEvent *) e);
1038 gtk_widget_grab_focus (priv->scale);
1040 priv->pop_time = time;
1046 gtk_scale_button_press (GtkWidget *widget,
1047 GdkEventButton *event)
1049 return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
1053 gtk_scale_button_popup (GtkWidget *widget)
1057 ev = gdk_event_new (GDK_KEY_RELEASE);
1058 gtk_scale_popup (widget, ev, GDK_CURRENT_TIME);
1059 gdk_event_free (ev);
1063 gtk_scale_button_key_release (GtkWidget *widget,
1066 return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
1069 /* This is called when the grab is broken for
1070 * either the dock, or the scale itself */
1072 gtk_scale_button_grab_notify (GtkScaleButton *button,
1073 gboolean was_grabbed)
1075 GdkDisplay *display;
1076 GtkScaleButtonPrivate *priv;
1078 if (was_grabbed != FALSE)
1081 priv = button->priv;
1083 if (!GTK_WIDGET_HAS_GRAB (priv->dock))
1086 if (gtk_widget_is_ancestor (gtk_grab_get_current (), priv->dock))
1089 display = gtk_widget_get_display (priv->dock);
1090 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1091 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1092 gtk_grab_remove (priv->dock);
1095 gtk_widget_hide (priv->dock);
1096 priv->timeout = FALSE;
1100 * +/- button callbacks.
1104 cb_button_timeout (gpointer user_data)
1106 GtkScaleButton *button;
1107 GtkScaleButtonPrivate *priv;
1110 gboolean res = TRUE;
1112 button = GTK_SCALE_BUTTON (user_data);
1113 priv = button->priv;
1115 if (priv->click_id == 0)
1118 adj = priv->adjustment;
1120 val = gtk_scale_button_get_value (button);
1121 val += priv->direction;
1122 if (val <= adj->lower)
1127 else if (val > adj->upper)
1132 gtk_scale_button_set_value (button, val);
1136 g_source_remove (priv->click_id);
1144 cb_button_press (GtkWidget *widget,
1145 GdkEventButton *event,
1148 GtkScaleButton *button;
1149 GtkScaleButtonPrivate *priv;
1152 button = GTK_SCALE_BUTTON (user_data);
1153 priv = button->priv;
1154 adj = priv->adjustment;
1156 if (priv->click_id != 0)
1157 g_source_remove (priv->click_id);
1159 if (widget == button->plus_button)
1160 priv->direction = fabs (adj->page_increment);
1162 priv->direction = - fabs (adj->page_increment);
1164 priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
1167 cb_button_timeout (button);
1173 cb_button_release (GtkWidget *widget,
1174 GdkEventButton *event,
1177 GtkScaleButton *button;
1178 GtkScaleButtonPrivate *priv;
1180 button = GTK_SCALE_BUTTON (user_data);
1181 priv = button->priv;
1183 if (priv->click_id != 0)
1185 g_source_remove (priv->click_id);
1193 cb_dock_grab_notify (GtkWidget *widget,
1194 gboolean was_grabbed,
1197 GtkScaleButton *button = (GtkScaleButton *) user_data;
1199 gtk_scale_button_grab_notify (button, was_grabbed);
1203 cb_dock_grab_broken_event (GtkWidget *widget,
1204 gboolean was_grabbed,
1207 GtkScaleButton *button = (GtkScaleButton *) user_data;
1209 gtk_scale_button_grab_notify (button, FALSE);
1219 gtk_scale_button_release_grab (GtkScaleButton *button,
1220 GdkEventButton *event)
1223 GdkDisplay *display;
1224 GtkScaleButtonPrivate *priv;
1226 priv = button->priv;
1229 display = gtk_widget_get_display (GTK_WIDGET (button));
1230 gdk_display_keyboard_ungrab (display, event->time);
1231 gdk_display_pointer_ungrab (display, event->time);
1232 gtk_grab_remove (priv->dock);
1235 gtk_widget_hide (priv->dock);
1236 priv->timeout = FALSE;
1238 e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
1239 e->window = GTK_WIDGET (button)->window;
1240 e->type = GDK_BUTTON_RELEASE;
1241 gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
1242 e->window = event->window;
1243 gdk_event_free ((GdkEvent *) e);
1247 cb_dock_button_press (GtkWidget *widget,
1248 GdkEventButton *event,
1251 GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);
1253 if (event->type == GDK_BUTTON_PRESS)
1255 gtk_scale_button_release_grab (button, event);
1263 gtk_scale_button_popdown (GtkWidget *widget)
1265 GtkScaleButton *button;
1266 GtkScaleButtonPrivate *priv;
1267 GdkDisplay *display;
1269 button = GTK_SCALE_BUTTON (widget);
1270 priv = button->priv;
1273 display = gtk_widget_get_display (widget);
1274 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1275 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1276 gtk_grab_remove (priv->dock);
1279 gtk_widget_hide (priv->dock);
1280 priv->timeout = FALSE;
1284 cb_dock_key_release (GtkWidget *widget,
1288 if (event->keyval == GDK_Escape)
1290 gtk_scale_button_popdown (GTK_WIDGET (user_data));
1294 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
1296 /* The popup hasn't managed the event, pass onto the button */
1297 gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
1304 cb_scale_grab_notify (GtkWidget *widget,
1305 gboolean was_grabbed,
1308 GtkScaleButton *button = (GtkScaleButton *) user_data;
1310 gtk_scale_button_grab_notify (button, was_grabbed);
1317 #define GTK_TYPE_SCALE_BUTTON_VSCALE (_gtk_scale_button_vscale_get_type ())
1318 #define GTK_SCALE_BUTTON_VSCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_VSCALE, GtkScaleButtonVScale))
1319 #define GTK_IS_SCALE_BUTTON_VSCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_VSCALE))
1321 typedef struct _GtkScaleButtonVScale {
1323 GtkScaleButton *button;
1324 } GtkScaleButtonVScale;
1326 typedef struct _GtkScaleButtonVScaleClass {
1327 GtkVScaleClass parent_class;
1328 } GtkScaleButtonVScaleClass;
1330 #define GTK_TYPE_SCALE_BUTTON_HSCALE (_gtk_scale_button_hscale_get_type ())
1331 #define GTK_SCALE_BUTTON_HSCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_HSCALE, GtkScaleButtonHScale))
1332 #define GTK_IS_SCALE_BUTTON_HSCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_HSCALE))
1334 typedef struct _GtkScaleButtonHScale {
1336 GtkScaleButton *button;
1337 } GtkScaleButtonHScale;
1339 typedef struct _GtkScaleButtonHScaleClass {
1340 GtkVScaleClass parent_class;
1341 } GtkScaleButtonHScaleClass;
1343 static gboolean gtk_scale_button_scale_press (GtkWidget *widget,
1344 GdkEventButton *event);
1345 static gboolean gtk_scale_button_scale_release (GtkWidget *widget,
1346 GdkEventButton *event);
1348 G_DEFINE_TYPE (GtkScaleButtonVScale, _gtk_scale_button_vscale, GTK_TYPE_VSCALE)
1350 G_DEFINE_TYPE (GtkScaleButtonHScale, _gtk_scale_button_hscale, GTK_TYPE_HSCALE)
1353 _gtk_scale_button_vscale_class_init (GtkScaleButtonVScaleClass *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_hscale_class_init (GtkScaleButtonHScaleClass *klass)
1366 GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
1367 GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);
1369 gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
1370 gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
1371 gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
1375 _gtk_scale_button_vscale_init (GtkScaleButtonVScale *scale)
1380 _gtk_scale_button_hscale_init (GtkScaleButtonHScale *scale)
1385 gtk_scale_button_scale_box_new (GtkScaleButton *button)
1387 GtkScaleButtonPrivate *priv = button->priv;
1388 GtkScaleButtonVScale *scale;
1391 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1393 box = gtk_vbox_new (FALSE, 0);
1395 scale = g_object_new (GTK_TYPE_SCALE_BUTTON_VSCALE, NULL);
1396 priv->scale = GTK_WIDGET (scale);
1398 gtk_widget_set_size_request (priv->scale, -1, SCALE_SIZE);
1399 gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
1401 gtk_box_pack_start (GTK_BOX (box), button->plus_button, TRUE, FALSE, 0);
1402 gtk_box_pack_start (GTK_BOX (box), priv->scale, TRUE, FALSE, 0);
1403 gtk_box_pack_start (GTK_BOX (box), button->minus_button, TRUE, FALSE, 0);
1407 box = gtk_hbox_new (FALSE, 0);
1409 scale = g_object_new (GTK_TYPE_SCALE_BUTTON_HSCALE, NULL);
1410 priv->scale = GTK_WIDGET (scale);
1412 gtk_widget_set_size_request (priv->scale, SCALE_SIZE, -1);
1414 gtk_box_pack_start (GTK_BOX (box), button->minus_button, TRUE, FALSE, 0);
1415 gtk_box_pack_start (GTK_BOX (box), priv->scale, TRUE, FALSE, 0);
1416 gtk_box_pack_start (GTK_BOX (box), button->plus_button, TRUE, FALSE, 0);
1419 gtk_scale_set_draw_value (GTK_SCALE (priv->scale), FALSE);
1420 gtk_range_set_adjustment (GTK_RANGE (scale),
1421 GTK_ADJUSTMENT (priv->adjustment));
1422 scale->button = button;
1424 g_signal_connect (priv->scale, "grab-notify",
1425 G_CALLBACK (cb_scale_grab_notify), button);
1427 /* FIXME: without this, the popup window appears as a square
1428 * after changing the orientation
1430 gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);
1436 gtk_scale_button_scale_press (GtkWidget *widget,
1437 GdkEventButton *event)
1439 GtkScaleButtonPrivate *priv;
1440 GtkWidgetClass *widget_class;
1442 if (GTK_IS_SCALE_BUTTON_VSCALE (widget))
1444 priv = GTK_SCALE_BUTTON_VSCALE (widget)->button->priv;
1445 widget_class = GTK_WIDGET_CLASS (_gtk_scale_button_vscale_parent_class);
1449 priv = GTK_SCALE_BUTTON_HSCALE (widget)->button->priv;
1450 widget_class = GTK_WIDGET_CLASS (_gtk_scale_button_hscale_parent_class);
1453 /* the scale will grab input; if we have input grabbed, all goes
1454 * horribly wrong, so let's not do that. */
1455 gtk_grab_remove (priv->dock);
1457 return widget_class->button_press_event (widget, event);
1461 gtk_scale_button_scale_release (GtkWidget *widget,
1462 GdkEventButton *event)
1464 GtkScaleButton *button;
1465 GtkWidgetClass *widget_class;
1468 if (GTK_IS_SCALE_BUTTON_VSCALE (widget))
1470 button = GTK_SCALE_BUTTON_VSCALE (widget)->button;
1471 widget_class = GTK_WIDGET_CLASS (_gtk_scale_button_vscale_parent_class);
1475 button = GTK_SCALE_BUTTON_HSCALE (widget)->button;
1476 widget_class = GTK_WIDGET_CLASS (_gtk_scale_button_hscale_parent_class);
1479 if (button->priv->timeout)
1481 /* if we did a quick click, leave the window open; else, hide it */
1482 if (event->time > button->priv->pop_time + button->priv->click_timeout)
1485 gtk_scale_button_release_grab (button, event);
1486 widget_class->button_release_event (widget, event);
1491 button->priv->timeout = FALSE;
1494 res = widget_class->button_release_event (widget, event);
1496 /* the scale will release input; right after that, we *have to* grab
1497 * it back so we can catch out-of-scale clicks and hide the popup,
1498 * so I basically want a g_signal_connect_after_always(), but I can't
1499 * find that, so we do this complex 'first-call-parent-then-do-actual-
1502 gtk_grab_add (button->priv->dock);
1508 gtk_scale_button_update_icon (GtkScaleButton *button)
1510 GtkScaleButtonPrivate *priv;
1517 priv = button->priv;
1519 if (!priv->icon_list || priv->icon_list[0] == '\0')
1521 gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1522 GTK_STOCK_MISSING_IMAGE,
1527 num_icons = g_strv_length (priv->icon_list);
1529 /* The 1-icon special case */
1532 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1538 range = GTK_RANGE (priv->scale);
1539 adj = priv->adjustment;
1540 value = gtk_scale_button_get_value (button);
1542 /* The 2-icons special case */
1546 limit = (adj->upper - adj->lower) / 2 + adj->lower;
1548 name = priv->icon_list[0];
1550 name = priv->icon_list[1];
1552 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1558 /* With 3 or more icons */
1559 if (value == adj->lower)
1561 name = priv->icon_list[0];
1563 else if (value == adj->upper)
1565 name = priv->icon_list[1];
1572 step = (adj->upper - adj->lower) / (num_icons - 2);
1573 i = (guint) ((value - adj->lower) / step) + 2;
1574 g_assert (i < num_icons);
1575 name = priv->icon_list[i];
1578 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1584 gtk_scale_button_scale_value_changed (GtkRange *range)
1586 GtkScaleButton *button;
1589 if (GTK_IS_SCALE_BUTTON_VSCALE (range))
1590 button = GTK_SCALE_BUTTON_VSCALE (range)->button;
1592 button = GTK_SCALE_BUTTON_HSCALE (range)->button;
1594 value = gtk_range_get_value (range);
1596 gtk_scale_button_update_icon (button);
1598 g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
1599 g_object_notify (G_OBJECT (button), "value");
1602 #define __GTK_SCALE_BUTTON_C__
1603 #include "gtkaliasdef.c"