1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GtkSpinButton widget for GTK+
5 * Copyright (C) 1998 Lars Hamann and Stefan Jeske
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include "gtkspinbutton.h"
38 #include "gtkadjustment.h"
39 #include "gtkbindings.h"
40 #include "gtkentryprivate.h"
41 #include "gtkicontheme.h"
43 #include "gtkmarshalers.h"
44 #include "gtkprivate.h"
45 #include "gtksettings.h"
47 #include "gtktypebuiltins.h"
48 #include "gtkwidgetpath.h"
49 #include "gtkwidgetprivate.h"
51 #include "a11y/gtkspinbuttonaccessible.h"
53 #define MIN_SPIN_BUTTON_WIDTH 30
54 #define MAX_TIMER_CALLS 5
57 #define MIN_ARROW_WIDTH 6
61 * SECTION:gtkspinbutton
62 * @Title: GtkSpinButton
63 * @Short_description: Retrieve an integer or floating-point number from
65 * @See_also: #GtkEntry
67 * A #GtkSpinButton is an ideal way to allow the user to set the value of
68 * some attribute. Rather than having to directly type a number into a
69 * #GtkEntry, GtkSpinButton allows the user to click on one of two arrows
70 * to increment or decrement the displayed value. A value can still be
71 * typed in, with the bonus that it can be checked to ensure it is in a
74 * The main properties of a GtkSpinButton are through an adjustment.
75 * See the #GtkAdjustment section for more details about an adjustment's
79 * <title>Using a GtkSpinButton to get an integer</title>
81 * /* Provides a function to retrieve an integer value from a
82 * * GtkSpinButton and creates a spin button to model percentage
87 * grab_int_value (GtkSpinButton *button,
90 * return gtk_spin_button_get_value_as_int (button);
94 * create_integer_spin_button (void)
97 * GtkWidget *window, *button;
98 * GtkAdjustment *adjustment;
100 * adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0);
102 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
103 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
105 * /* creates the spinbutton, with no decimal places */
106 * button = gtk_spin_button_new (adjustment, 1.0, 0);
107 * gtk_container_add (GTK_CONTAINER (window), button);
109 * gtk_widget_show_all (window);
115 * <title>Using a GtkSpinButton to get a floating point value</title>
117 * /* Provides a function to retrieve a floating point value from a
118 * * GtkSpinButton, and creates a high precision spin button.
122 * grab_float_value (GtkSpinButton *button,
123 * gpointer user_data)
125 * return gtk_spin_button_get_value (button);
129 * create_floating_spin_button (void)
131 * GtkWidget *window, *button;
132 * GtkAdjustment *adjustment;
134 * adjustment = gtk_adjustment_new (2.500, 0.0, 5.0, 0.001, 0.1, 0.0);
136 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
137 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
139 * /* creates the spinbutton, with three decimal places */
140 * button = gtk_spin_button_new (adjustment, 0.001, 3);
141 * gtk_container_add (GTK_CONTAINER (window), button);
143 * gtk_widget_show_all (window);
149 struct _GtkSpinButtonPrivate
151 GtkAdjustment *adjustment;
153 GdkWindow *down_panel;
156 GdkWindow *click_child;
161 GtkSpinButtonUpdatePolicy update_policy;
168 guint need_timer : 1;
170 guint snap_to_ticks : 1;
171 guint timer_calls : 3;
198 static void gtk_spin_button_editable_init (GtkEditableInterface *iface);
199 static void gtk_spin_button_finalize (GObject *object);
200 static void gtk_spin_button_set_property (GObject *object,
204 static void gtk_spin_button_get_property (GObject *object,
208 static void gtk_spin_button_destroy (GtkWidget *widget);
209 static void gtk_spin_button_map (GtkWidget *widget);
210 static void gtk_spin_button_unmap (GtkWidget *widget);
211 static void gtk_spin_button_realize (GtkWidget *widget);
212 static void gtk_spin_button_unrealize (GtkWidget *widget);
213 static void gtk_spin_button_get_preferred_width (GtkWidget *widget,
217 static void gtk_spin_button_size_allocate (GtkWidget *widget,
218 GtkAllocation *allocation);
219 static gint gtk_spin_button_draw (GtkWidget *widget,
221 static gint gtk_spin_button_button_press (GtkWidget *widget,
222 GdkEventButton *event);
223 static gint gtk_spin_button_button_release (GtkWidget *widget,
224 GdkEventButton *event);
225 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
226 GdkEventMotion *event);
227 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
228 GdkEventCrossing *event);
229 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
230 GdkEventCrossing *event);
231 static gint gtk_spin_button_focus_out (GtkWidget *widget,
232 GdkEventFocus *event);
233 static void gtk_spin_button_grab_notify (GtkWidget *widget,
234 gboolean was_grabbed);
235 static void gtk_spin_button_state_flags_changed (GtkWidget *widget,
236 GtkStateFlags previous_state);
237 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
238 static gboolean gtk_spin_button_stop_spinning (GtkSpinButton *spin);
239 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
240 GtkSpinButton *spin_button);
241 static gint gtk_spin_button_key_release (GtkWidget *widget,
243 static gint gtk_spin_button_scroll (GtkWidget *widget,
244 GdkEventScroll *event);
245 static void gtk_spin_button_activate (GtkEntry *entry);
246 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
251 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
253 static void gtk_spin_button_insert_text (GtkEditable *editable,
254 const gchar *new_text,
255 gint new_text_length,
257 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
259 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
260 GtkScrollType scroll);
262 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
264 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
266 static guint spinbutton_signals[LAST_SIGNAL] = {0};
268 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
269 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
270 gtk_spin_button_editable_init))
272 #define add_spin_binding(binding_set, keyval, mask, scroll) \
273 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
275 GTK_TYPE_SCROLL_TYPE, scroll)
278 gtk_spin_button_class_init (GtkSpinButtonClass *class)
280 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
281 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
282 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
283 GtkBindingSet *binding_set;
285 gobject_class->finalize = gtk_spin_button_finalize;
286 gobject_class->set_property = gtk_spin_button_set_property;
287 gobject_class->get_property = gtk_spin_button_get_property;
289 widget_class->destroy = gtk_spin_button_destroy;
290 widget_class->map = gtk_spin_button_map;
291 widget_class->unmap = gtk_spin_button_unmap;
292 widget_class->realize = gtk_spin_button_realize;
293 widget_class->unrealize = gtk_spin_button_unrealize;
294 widget_class->get_preferred_width = gtk_spin_button_get_preferred_width;
295 widget_class->size_allocate = gtk_spin_button_size_allocate;
296 widget_class->draw = gtk_spin_button_draw;
297 widget_class->scroll_event = gtk_spin_button_scroll;
298 widget_class->button_press_event = gtk_spin_button_button_press;
299 widget_class->button_release_event = gtk_spin_button_button_release;
300 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
301 widget_class->key_release_event = gtk_spin_button_key_release;
302 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
303 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
304 widget_class->focus_out_event = gtk_spin_button_focus_out;
305 widget_class->grab_notify = gtk_spin_button_grab_notify;
306 widget_class->state_flags_changed = gtk_spin_button_state_flags_changed;
308 entry_class->activate = gtk_spin_button_activate;
309 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
312 class->output = NULL;
313 class->change_value = gtk_spin_button_real_change_value;
315 g_object_class_install_property (gobject_class,
317 g_param_spec_object ("adjustment",
319 P_("The adjustment that holds the value of the spin button"),
321 GTK_PARAM_READWRITE));
323 g_object_class_install_property (gobject_class,
325 g_param_spec_double ("climb-rate",
327 P_("The acceleration rate when you hold down a button"),
331 GTK_PARAM_READWRITE));
333 g_object_class_install_property (gobject_class,
335 g_param_spec_uint ("digits",
337 P_("The number of decimal places to display"),
341 GTK_PARAM_READWRITE));
343 g_object_class_install_property (gobject_class,
345 g_param_spec_boolean ("snap-to-ticks",
347 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
349 GTK_PARAM_READWRITE));
351 g_object_class_install_property (gobject_class,
353 g_param_spec_boolean ("numeric",
355 P_("Whether non-numeric characters should be ignored"),
357 GTK_PARAM_READWRITE));
359 g_object_class_install_property (gobject_class,
361 g_param_spec_boolean ("wrap",
363 P_("Whether a spin button should wrap upon reaching its limits"),
365 GTK_PARAM_READWRITE));
367 g_object_class_install_property (gobject_class,
369 g_param_spec_enum ("update-policy",
371 P_("Whether the spin button should update always, or only when the value is legal"),
372 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
374 GTK_PARAM_READWRITE));
376 g_object_class_install_property (gobject_class,
378 g_param_spec_double ("value",
380 P_("Reads the current value, or sets a new value"),
384 GTK_PARAM_READWRITE));
386 gtk_widget_class_install_style_property_parser (widget_class,
387 g_param_spec_enum ("shadow-type",
389 P_("Style of bevel around the spin button"),
390 GTK_TYPE_SHADOW_TYPE,
393 gtk_rc_property_parse_enum);
396 * GtkSpinButton::input:
397 * @spin_button: the object on which the signal was emitted
398 * @new_value: (out) (type double): return location for the new value
400 * The ::input signal can be used to influence the conversion of
401 * the users input into a double value. The signal handler is
402 * expected to use gtk_entry_get_text() to retrieve the text of
403 * the entry and set @new_value to the new value.
405 * The default conversion uses g_strtod().
407 * Returns: %TRUE for a successful conversion, %FALSE if the input
408 * was not handled, and %GTK_INPUT_ERROR if the conversion failed.
410 spinbutton_signals[INPUT] =
411 g_signal_new (I_("input"),
412 G_TYPE_FROM_CLASS (gobject_class),
414 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
416 _gtk_marshal_INT__POINTER,
421 * GtkSpinButton::output:
422 * @spin_button: the object which received the signal
424 * The ::output signal can be used to change to formatting
425 * of the value that is displayed in the spin buttons entry.
427 * /* show leading zeros */
429 * on_output (GtkSpinButton *spin,
432 * GtkAdjustment *adjustment;
436 * adjustment = gtk_spin_button_get_adjustment (spin);
437 * value = (int)gtk_adjustment_get_value (adjustment);
438 * text = g_strdup_printf ("%02d", value);
439 * gtk_entry_set_text (GTK_ENTRY (spin), text);
446 * Returns: %TRUE if the value has been displayed
448 spinbutton_signals[OUTPUT] =
449 g_signal_new (I_("output"),
450 G_TYPE_FROM_CLASS (gobject_class),
452 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
453 _gtk_boolean_handled_accumulator, NULL,
454 _gtk_marshal_BOOLEAN__VOID,
457 spinbutton_signals[VALUE_CHANGED] =
458 g_signal_new (I_("value-changed"),
459 G_TYPE_FROM_CLASS (gobject_class),
461 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
463 _gtk_marshal_VOID__VOID,
467 * GtkSpinButton::wrapped:
468 * @spinbutton: the object which received the signal
470 * The wrapped signal is emitted right after the spinbutton wraps
471 * from its maximum to minimum value or vice-versa.
475 spinbutton_signals[WRAPPED] =
476 g_signal_new (I_("wrapped"),
477 G_TYPE_FROM_CLASS (gobject_class),
479 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
481 _gtk_marshal_VOID__VOID,
485 spinbutton_signals[CHANGE_VALUE] =
486 g_signal_new (I_("change-value"),
487 G_TYPE_FROM_CLASS (gobject_class),
488 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
489 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
491 _gtk_marshal_VOID__ENUM,
493 GTK_TYPE_SCROLL_TYPE);
495 binding_set = gtk_binding_set_by_class (class);
497 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
498 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
499 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
500 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
501 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
502 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
503 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
504 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
506 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
508 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SPIN_BUTTON_ACCESSIBLE);
512 gtk_spin_button_editable_init (GtkEditableInterface *iface)
514 iface->insert_text = gtk_spin_button_insert_text;
518 gtk_spin_button_set_property (GObject *object,
523 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
524 GtkSpinButtonPrivate *priv = spin_button->priv;
528 GtkAdjustment *adjustment;
530 case PROP_ADJUSTMENT:
531 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
533 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
534 gtk_spin_button_set_adjustment (spin_button, adjustment);
536 case PROP_CLIMB_RATE:
537 gtk_spin_button_configure (spin_button,
539 g_value_get_double (value),
543 gtk_spin_button_configure (spin_button,
546 g_value_get_uint (value));
548 case PROP_SNAP_TO_TICKS:
549 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
552 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
555 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
557 case PROP_UPDATE_POLICY:
558 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
561 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
564 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
570 gtk_spin_button_get_property (GObject *object,
575 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
576 GtkSpinButtonPrivate *priv = spin_button->priv;
580 case PROP_ADJUSTMENT:
581 g_value_set_object (value, priv->adjustment);
583 case PROP_CLIMB_RATE:
584 g_value_set_double (value, priv->climb_rate);
587 g_value_set_uint (value, priv->digits);
589 case PROP_SNAP_TO_TICKS:
590 g_value_set_boolean (value, priv->snap_to_ticks);
593 g_value_set_boolean (value, priv->numeric);
596 g_value_set_boolean (value, priv->wrap);
598 case PROP_UPDATE_POLICY:
599 g_value_set_enum (value, priv->update_policy);
602 g_value_set_double (value, gtk_adjustment_get_value (priv->adjustment));
605 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
613 gint width, height, icon_size;
615 gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
616 icon_size = MAX (width, height);
622 create_one_pixbuf (GtkStyleContext *context,
623 const gchar *icon_name)
625 GtkIconInfo *icon_info;
627 gint size = get_icon_size ();
629 icon_info = gtk_icon_theme_lookup_icon (gtk_icon_theme_get_default (),
631 GTK_ICON_LOOKUP_GENERIC_FALLBACK |
632 GTK_ICON_LOOKUP_USE_BUILTIN);
634 if (icon_info != NULL)
636 pix = gtk_icon_info_load_symbolic_for_context (icon_info, context,
638 gtk_icon_info_free (icon_info);
642 GtkIconSet *icon_set;
644 icon_set = gtk_style_context_lookup_icon_set (context, GTK_STOCK_MISSING_IMAGE);
645 pix = gtk_icon_set_render_icon_pixbuf (icon_set, context, GTK_ICON_SIZE_MENU);
647 g_warning ("Unable to fetch icon %s from the icon theme", icon_name);
654 gtk_spin_button_init (GtkSpinButton *spin_button)
656 GtkSpinButtonPrivate *priv;
657 GtkStyleContext *context;
659 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
660 GTK_TYPE_SPIN_BUTTON,
661 GtkSpinButtonPrivate);
662 priv = spin_button->priv;
664 priv->adjustment = NULL;
665 priv->down_panel = NULL;
666 priv->up_panel = NULL;
668 priv->climb_rate = 0.0;
669 priv->timer_step = 0.0;
670 priv->update_policy = GTK_UPDATE_ALWAYS;
671 priv->in_child = NULL;
672 priv->click_child = NULL;
674 priv->need_timer = FALSE;
675 priv->timer_calls = 0;
677 priv->numeric = FALSE;
679 priv->snap_to_ticks = FALSE;
681 gtk_spin_button_set_adjustment (spin_button,
682 gtk_adjustment_new (0, 0, 0, 0, 0, 0));
684 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
685 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINBUTTON);
687 gtk_widget_add_events (GTK_WIDGET (spin_button), GDK_SCROLL_MASK);
691 gtk_spin_button_finalize (GObject *object)
693 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
695 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
699 gtk_spin_button_destroy (GtkWidget *widget)
701 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
703 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->destroy (widget);
707 gtk_spin_button_map (GtkWidget *widget)
709 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
710 GtkSpinButtonPrivate *priv = spin_button->priv;
712 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
714 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
715 gdk_window_show (priv->down_panel);
716 gdk_window_show (priv->up_panel);
721 gtk_spin_button_unmap (GtkWidget *widget)
723 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
724 GtkSpinButtonPrivate *priv = spin_button->priv;
726 if (gtk_widget_get_mapped (widget))
728 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
730 gdk_window_hide (priv->down_panel);
731 gdk_window_hide (priv->up_panel);
732 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
737 gtk_spin_button_panel_nthchildize_context (GtkSpinButton *spin_button,
738 GtkStyleContext *context,
741 GtkWidget *widget = GTK_WIDGET (spin_button);
742 GtkWidgetPath *path, *siblings_path;
743 GtkTextDirection direction;
744 gint up_pos, down_pos;
746 /* We are a subclass of GtkEntry, which is not a GtkContainer, so we
747 * have to emulate what gtk_container_get_path_for_child() would do
748 * for the button panels
750 path = _gtk_widget_create_path (widget);
751 direction = gtk_widget_get_direction (widget);
752 siblings_path = gtk_widget_path_new ();
754 /* flip children order for RTL */
755 if (direction == GTK_TEXT_DIR_RTL)
757 up_pos = gtk_widget_path_append_type (siblings_path, GTK_TYPE_SPIN_BUTTON);
758 down_pos = gtk_widget_path_append_type (siblings_path, GTK_TYPE_SPIN_BUTTON);
759 gtk_widget_path_append_type (siblings_path, GTK_TYPE_ENTRY);
763 gtk_widget_path_append_type (siblings_path, GTK_TYPE_ENTRY);
764 down_pos = gtk_widget_path_append_type (siblings_path, GTK_TYPE_SPIN_BUTTON);
765 up_pos = gtk_widget_path_append_type (siblings_path, GTK_TYPE_SPIN_BUTTON);
768 gtk_widget_path_iter_add_class (siblings_path, up_pos, GTK_STYLE_CLASS_BUTTON);
769 gtk_widget_path_iter_add_class (siblings_path, down_pos, GTK_STYLE_CLASS_BUTTON);
771 /* this allows compatibility for themes that use .spinbutton.button */
772 gtk_widget_path_iter_add_class (siblings_path, up_pos, GTK_STYLE_CLASS_SPINBUTTON);
773 gtk_widget_path_iter_add_class (siblings_path, down_pos, GTK_STYLE_CLASS_SPINBUTTON);
775 if (panel == spin_button->priv->down_panel)
777 gtk_widget_path_append_with_siblings (path, siblings_path, up_pos);
778 gtk_widget_path_append_with_siblings (path, siblings_path, down_pos);
782 gtk_widget_path_append_with_siblings (path, siblings_path, down_pos);
783 gtk_widget_path_append_with_siblings (path, siblings_path, up_pos);
786 gtk_style_context_set_path (context, path);
787 gtk_widget_path_unref (path);
788 gtk_widget_path_unref (siblings_path);
792 gtk_spin_button_panel_at_limit (GtkSpinButton *spin_button,
795 GtkSpinButtonPrivate *priv = spin_button->priv;
796 GdkWindow *effective_panel;
801 if (gtk_adjustment_get_step_increment (priv->adjustment) > 0)
802 effective_panel = panel;
804 effective_panel = panel == priv->up_panel ? priv->down_panel : priv->up_panel;
806 if (effective_panel == priv->up_panel &&
807 (gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment) <= EPSILON))
810 if (effective_panel == priv->down_panel &&
811 (gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment) <= EPSILON))
818 gtk_spin_button_panel_get_state (GtkSpinButton *spin_button,
822 GtkSpinButtonPrivate *priv = spin_button->priv;
824 state = gtk_widget_get_state_flags (GTK_WIDGET (spin_button));
826 state &= ~(GTK_STATE_FLAG_ACTIVE | GTK_STATE_FLAG_PRELIGHT);
828 if ((state & GTK_STATE_FLAG_INSENSITIVE) ||
829 gtk_spin_button_panel_at_limit (spin_button, panel) ||
830 !gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
832 state |= GTK_STATE_FLAG_INSENSITIVE;
836 if (priv->click_child == panel)
837 state |= GTK_STATE_ACTIVE;
838 else if (priv->in_child == panel &&
839 priv->click_child == NULL)
840 state |= GTK_STATE_FLAG_PRELIGHT;
846 static GtkStyleContext *
847 gtk_spin_button_panel_get_context (GtkSpinButton *spin_button,
850 GtkStyleContext *context;
852 context = gtk_style_context_new ();
853 gtk_spin_button_panel_nthchildize_context (spin_button, context, panel);
859 gtk_spin_button_panel_get_width (GtkSpinButton *spin_button,
862 GtkBorder button_padding, button_border;
863 GtkStyleContext *context;
866 context = gtk_spin_button_panel_get_context (spin_button, panel);
867 state = gtk_spin_button_panel_get_state (spin_button, panel);
869 gtk_style_context_get_padding (context, state, &button_padding);
870 gtk_style_context_get_border (context, state, &button_border);
872 g_object_unref (context);
874 return get_icon_size () + button_padding.left + button_padding.right +
875 button_border.left + button_border.right;
879 gtk_spin_button_panel_get_allocations (GtkSpinButton *spin_button,
880 GtkAllocation *down_allocation_out,
881 GtkAllocation *up_allocation_out)
883 GtkWidget *widget = GTK_WIDGET (spin_button);
884 GtkSpinButtonPrivate *priv = spin_button->priv;
885 GtkAllocation spin_allocation, down_allocation, up_allocation, allocation;
886 GtkRequisition requisition;
888 gint up_panel_width, down_panel_width;
889 GtkStyleContext *context;
892 gtk_widget_get_allocation (widget, &spin_allocation);
893 gtk_widget_get_preferred_size (widget, &requisition, NULL);
895 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
896 gtk_style_context_get_border (context, GTK_STATE_NORMAL, &space);
898 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
899 down_panel_width = gtk_spin_button_panel_get_width (spin_button, priv->down_panel);
900 up_panel_width = gtk_spin_button_panel_get_width (spin_button, priv->up_panel);
902 /* both panels have the same size, and they're as big as the entry allocation,
905 allocation.height = MIN (req_height, spin_allocation.height) - space.top - space.bottom;
906 allocation.y = spin_allocation.y + space.top + (spin_allocation.height - req_height) / 2;
907 down_allocation = up_allocation = allocation;
909 down_allocation.width = down_panel_width;
910 up_allocation.width = up_panel_width;
912 /* invert x axis allocation for RTL */
913 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
915 up_allocation.x = spin_allocation.x + space.left;
916 down_allocation.x = up_allocation.x + up_panel_width;
920 up_allocation.x = spin_allocation.x + spin_allocation.width - up_panel_width - space.right;
921 down_allocation.x = up_allocation.x - down_panel_width;
924 if (down_allocation_out)
925 *down_allocation_out = down_allocation;
926 if (up_allocation_out)
927 *up_allocation_out = up_allocation;
931 gtk_spin_button_panel_draw (GtkSpinButton *spin_button,
935 GtkSpinButtonPrivate *priv = spin_button->priv;
936 GtkStyleContext *context;
939 gdouble width, height, x, y;
942 widget = GTK_WIDGET (spin_button);
945 gtk_cairo_transform_to_window (cr, widget, panel);
947 context = gtk_spin_button_panel_get_context (spin_button, panel);
948 state = gtk_spin_button_panel_get_state (spin_button, panel);
949 gtk_style_context_set_state (context, state);
951 height = gdk_window_get_height (panel);
952 width = gdk_window_get_width (panel);
954 if (panel == priv->down_panel)
955 pix = create_one_pixbuf (context, "list-remove-symbolic");
957 pix = create_one_pixbuf (context, "list-add-symbolic");
959 gtk_render_background (context, cr,
960 0, 0, width, height);
961 gtk_render_frame (context, cr,
962 0, 0, width, height);
964 x = floor ((width - gdk_pixbuf_get_width (pix)) / 2.0);
965 y = floor ((height - gdk_pixbuf_get_height (pix)) / 2.0);
967 gtk_render_icon (context, cr, pix,
971 g_object_unref (pix);
972 g_object_unref (context);
976 gtk_spin_button_realize (GtkWidget *widget)
978 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
979 GtkSpinButtonPrivate *priv = spin_button->priv;
980 GtkAllocation down_allocation, up_allocation;
981 GdkWindowAttr attributes;
982 gint attributes_mask;
985 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
986 GDK_KEY_RELEASE_MASK);
987 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
989 attributes.window_type = GDK_WINDOW_CHILD;
990 attributes.wclass = GDK_INPUT_ONLY;
991 attributes.visual = gtk_widget_get_visual (widget);
992 attributes.event_mask = gtk_widget_get_events (widget);
993 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
994 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
995 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
997 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
999 gtk_spin_button_panel_get_allocations (spin_button, &down_allocation, &up_allocation);
1001 /* create the left panel window */
1002 attributes.x = down_allocation.x;
1003 attributes.y = down_allocation.y;
1004 attributes.width = down_allocation.width;
1005 attributes.height = down_allocation.height;
1007 priv->down_panel = gdk_window_new (gtk_widget_get_window (widget),
1008 &attributes, attributes_mask);
1009 gdk_window_set_user_data (priv->down_panel, widget);
1011 /* create the right panel window */
1012 attributes.x = up_allocation.x;
1013 attributes.y = up_allocation.y;
1014 attributes.width = up_allocation.width;
1015 attributes.height = up_allocation.height;
1017 priv->up_panel = gdk_window_new (gtk_widget_get_window (widget),
1018 &attributes, attributes_mask);
1019 gdk_window_set_user_data (priv->up_panel, widget);
1022 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1023 if (return_val == FALSE)
1024 gtk_spin_button_default_output (spin_button);
1026 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1030 gtk_spin_button_unrealize (GtkWidget *widget)
1032 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1033 GtkSpinButtonPrivate *priv = spin->priv;
1035 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
1037 if (priv->down_panel)
1039 gdk_window_set_user_data (priv->down_panel, NULL);
1040 gdk_window_destroy (priv->down_panel);
1041 priv->down_panel = NULL;
1046 gdk_window_set_user_data (priv->up_panel, NULL);
1047 gdk_window_destroy (priv->up_panel);
1048 priv->up_panel = NULL;
1053 compute_double_length (double val, int digits)
1059 if (fabs (val) > 1.0)
1060 a = floor (log10 (fabs (val))) + 1;
1072 return a + digits + extra;
1076 gtk_spin_button_get_preferred_width (GtkWidget *widget,
1080 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
1081 GtkSpinButtonPrivate *priv = spin_button->priv;
1082 GtkEntry *entry = GTK_ENTRY (widget);
1083 GtkStyleContext *style_context;
1084 gint up_panel_width, down_panel_width;
1086 style_context = gtk_widget_get_style_context (widget);
1087 up_panel_width = gtk_spin_button_panel_get_width (spin_button, priv->up_panel);
1088 down_panel_width = gtk_spin_button_panel_get_width (spin_button, priv->down_panel);
1090 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->get_preferred_width (widget, minimum, natural);
1092 if (gtk_entry_get_width_chars (entry) < 0)
1094 PangoContext *context;
1095 const PangoFontDescription *font_desc;
1096 PangoFontMetrics *metrics;
1100 gint max_string_len;
1102 gboolean interior_focus;
1106 gtk_style_context_get_style (style_context,
1107 "interior-focus", &interior_focus,
1108 "focus-line-width", &focus_width,
1110 font_desc = gtk_style_context_get_font (style_context, GTK_STATE_FLAG_NORMAL);
1112 context = gtk_widget_get_pango_context (widget);
1113 metrics = pango_context_get_metrics (context, font_desc,
1114 pango_context_get_language (context));
1116 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
1117 digit_width = PANGO_SCALE *
1118 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
1120 pango_font_metrics_unref (metrics);
1122 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
1123 width = MIN_SPIN_BUTTON_WIDTH;
1124 max_string_len = MAX (10, compute_double_length (1e9 * gtk_adjustment_get_step_increment (priv->adjustment),
1127 string_len = compute_double_length (gtk_adjustment_get_upper (priv->adjustment),
1129 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
1130 width = MAX (width, w);
1131 string_len = compute_double_length (gtk_adjustment_get_lower (priv->adjustment), priv->digits);
1132 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
1133 width = MAX (width, w);
1135 _gtk_entry_get_borders (entry, &borders);
1136 width += borders.left + borders.right;
1142 *minimum += up_panel_width + down_panel_width;
1143 *natural += up_panel_width + down_panel_width;
1147 gtk_spin_button_size_allocate (GtkWidget *widget,
1148 GtkAllocation *allocation)
1150 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1151 GtkSpinButtonPrivate *priv = spin->priv;
1152 GtkAllocation down_panel_allocation, up_panel_allocation;
1154 gtk_widget_set_allocation (widget, allocation);
1155 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
1157 gtk_spin_button_panel_get_allocations (spin, &down_panel_allocation, &up_panel_allocation);
1159 if (gtk_widget_get_realized (widget))
1161 gdk_window_move_resize (priv->down_panel,
1162 down_panel_allocation.x,
1163 down_panel_allocation.y,
1164 down_panel_allocation.width,
1165 down_panel_allocation.height);
1167 gdk_window_move_resize (priv->up_panel,
1168 up_panel_allocation.x,
1169 up_panel_allocation.y,
1170 up_panel_allocation.width,
1171 up_panel_allocation.height);
1174 gtk_widget_queue_draw (GTK_WIDGET (spin));
1178 gtk_spin_button_draw (GtkWidget *widget,
1181 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1182 GtkSpinButtonPrivate *priv = spin->priv;
1184 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
1186 /* Draw the buttons */
1187 gtk_spin_button_panel_draw (spin, cr, priv->down_panel);
1188 gtk_spin_button_panel_draw (spin, cr, priv->up_panel);
1194 gtk_spin_button_enter_notify (GtkWidget *widget,
1195 GdkEventCrossing *event)
1197 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1198 GtkSpinButtonPrivate *priv = spin->priv;
1200 if ((event->window == priv->down_panel) ||
1201 (event->window == priv->up_panel))
1203 priv->in_child = event->window;
1204 gtk_widget_queue_draw (GTK_WIDGET (spin));
1207 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1211 gtk_spin_button_leave_notify (GtkWidget *widget,
1212 GdkEventCrossing *event)
1214 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1215 GtkSpinButtonPrivate *priv = spin->priv;
1217 if (priv->in_child != NULL)
1219 priv->in_child = NULL;
1220 gtk_widget_queue_draw (GTK_WIDGET (spin));
1223 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1227 gtk_spin_button_focus_out (GtkWidget *widget,
1228 GdkEventFocus *event)
1230 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1231 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1233 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1237 gtk_spin_button_grab_notify (GtkWidget *widget,
1238 gboolean was_grabbed)
1240 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1244 if (gtk_spin_button_stop_spinning (spin))
1245 gtk_widget_queue_draw (GTK_WIDGET (spin));
1250 gtk_spin_button_state_flags_changed (GtkWidget *widget,
1251 GtkStateFlags previous_state)
1253 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1255 if (!gtk_widget_is_sensitive (widget))
1257 if (gtk_spin_button_stop_spinning (spin))
1258 gtk_widget_queue_draw (GTK_WIDGET (spin));
1263 gtk_spin_button_scroll (GtkWidget *widget,
1264 GdkEventScroll *event)
1266 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1267 GtkSpinButtonPrivate *priv = spin->priv;
1269 if (event->direction == GDK_SCROLL_UP)
1271 if (!gtk_widget_has_focus (widget))
1272 gtk_widget_grab_focus (widget);
1273 gtk_spin_button_real_spin (spin, gtk_adjustment_get_step_increment (priv->adjustment));
1275 else if (event->direction == GDK_SCROLL_DOWN)
1277 if (!gtk_widget_has_focus (widget))
1278 gtk_widget_grab_focus (widget);
1279 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_step_increment (priv->adjustment));
1288 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1290 GtkSpinButtonPrivate *priv = spin->priv;
1291 gboolean did_spin = FALSE;
1295 g_source_remove (priv->timer);
1297 priv->need_timer = FALSE;
1303 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1304 priv->timer_calls = 0;
1306 priv->click_child = NULL;
1312 start_spinning (GtkSpinButton *spin,
1313 GdkWindow *click_child,
1316 GtkSpinButtonPrivate *priv;
1320 priv->click_child = click_child;
1324 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1327 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1329 priv->timer_step = step;
1330 priv->need_timer = TRUE;
1331 priv->timer = gdk_threads_add_timeout (timeout,
1332 (GSourceFunc) gtk_spin_button_timer,
1335 gtk_spin_button_real_spin (spin, click_child == priv->up_panel ? step : -step);
1337 gtk_widget_queue_draw (GTK_WIDGET (spin));
1341 gtk_spin_button_button_press (GtkWidget *widget,
1342 GdkEventButton *event)
1344 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1345 GtkSpinButtonPrivate *priv = spin->priv;
1349 if ((event->window == priv->down_panel) ||
1350 (event->window == priv->up_panel))
1352 if (!gtk_widget_has_focus (widget))
1353 gtk_widget_grab_focus (widget);
1354 priv->button = event->button;
1356 if (gtk_editable_get_editable (GTK_EDITABLE (widget))) {
1357 gtk_spin_button_update (spin);
1359 if (event->button == GDK_BUTTON_PRIMARY)
1360 start_spinning (spin, event->window, gtk_adjustment_get_step_increment (priv->adjustment));
1361 else if (event->button == GDK_BUTTON_MIDDLE)
1362 start_spinning (spin, event->window, gtk_adjustment_get_page_increment (priv->adjustment));
1364 priv->click_child = event->window;
1366 gtk_widget_error_bell (widget);
1371 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1377 gtk_spin_button_button_release (GtkWidget *widget,
1378 GdkEventButton *event)
1380 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1381 GtkSpinButtonPrivate *priv = spin->priv;
1383 if (event->button == priv->button)
1385 GdkWindow *click_child = priv->click_child;
1387 gtk_spin_button_stop_spinning (spin);
1389 if (event->button == GDK_BUTTON_SECONDARY)
1393 if (event->window == priv->down_panel &&
1394 click_child == event->window)
1396 diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1398 gtk_spin_button_real_spin (spin, -diff);
1400 else if (event->window == priv->up_panel &&
1401 click_child == event->window)
1403 diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1405 gtk_spin_button_real_spin (spin, diff);
1408 gtk_widget_queue_draw (GTK_WIDGET (spin));
1413 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1417 gtk_spin_button_motion_notify (GtkWidget *widget,
1418 GdkEventMotion *event)
1420 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1421 GtkSpinButtonPrivate *priv = spin->priv;
1426 if (event->window == priv->down_panel ||
1427 event->window == priv->up_panel)
1429 gdk_event_request_motions (event);
1431 priv->in_child = event->window;
1432 gtk_widget_queue_draw (widget);
1437 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1441 gtk_spin_button_timer (GtkSpinButton *spin_button)
1443 GtkSpinButtonPrivate *priv = spin_button->priv;
1444 gboolean retval = FALSE;
1448 if (priv->click_child == priv->up_panel)
1449 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1451 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1453 if (priv->need_timer)
1455 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1458 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1460 priv->need_timer = FALSE;
1461 priv->timer = gdk_threads_add_timeout (timeout,
1462 (GSourceFunc) gtk_spin_button_timer,
1463 (gpointer) spin_button);
1467 if (priv->climb_rate > 0.0 && priv->timer_step
1468 < gtk_adjustment_get_page_increment (priv->adjustment))
1470 if (priv->timer_calls < MAX_TIMER_CALLS)
1471 priv->timer_calls++;
1474 priv->timer_calls = 0;
1475 priv->timer_step += priv->climb_rate;
1486 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1487 GtkSpinButton *spin_button)
1489 gboolean return_val;
1491 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1494 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1495 if (return_val == FALSE)
1496 gtk_spin_button_default_output (spin_button);
1498 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1500 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1502 g_object_notify (G_OBJECT (spin_button), "value");
1506 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1507 GtkScrollType scroll)
1509 GtkSpinButtonPrivate *priv = spin->priv;
1512 if (!gtk_editable_get_editable (GTK_EDITABLE (spin)))
1514 gtk_widget_error_bell (GTK_WIDGET (spin));
1518 /* When the key binding is activated, there may be an outstanding
1519 * value, so we first have to commit what is currently written in
1520 * the spin buttons text entry. See #106574
1522 gtk_spin_button_update (spin);
1524 old_value = gtk_adjustment_get_value (priv->adjustment);
1528 case GTK_SCROLL_STEP_BACKWARD:
1529 case GTK_SCROLL_STEP_DOWN:
1530 case GTK_SCROLL_STEP_LEFT:
1531 gtk_spin_button_real_spin (spin, -priv->timer_step);
1533 if (priv->climb_rate > 0.0 && priv->timer_step
1534 < gtk_adjustment_get_page_increment (priv->adjustment))
1536 if (priv->timer_calls < MAX_TIMER_CALLS)
1537 priv->timer_calls++;
1540 priv->timer_calls = 0;
1541 priv->timer_step += priv->climb_rate;
1546 case GTK_SCROLL_STEP_FORWARD:
1547 case GTK_SCROLL_STEP_UP:
1548 case GTK_SCROLL_STEP_RIGHT:
1549 gtk_spin_button_real_spin (spin, priv->timer_step);
1551 if (priv->climb_rate > 0.0 && priv->timer_step
1552 < gtk_adjustment_get_page_increment (priv->adjustment))
1554 if (priv->timer_calls < MAX_TIMER_CALLS)
1555 priv->timer_calls++;
1558 priv->timer_calls = 0;
1559 priv->timer_step += priv->climb_rate;
1564 case GTK_SCROLL_PAGE_BACKWARD:
1565 case GTK_SCROLL_PAGE_DOWN:
1566 case GTK_SCROLL_PAGE_LEFT:
1567 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_page_increment (priv->adjustment));
1570 case GTK_SCROLL_PAGE_FORWARD:
1571 case GTK_SCROLL_PAGE_UP:
1572 case GTK_SCROLL_PAGE_RIGHT:
1573 gtk_spin_button_real_spin (spin, gtk_adjustment_get_page_increment (priv->adjustment));
1576 case GTK_SCROLL_START:
1578 gdouble diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1580 gtk_spin_button_real_spin (spin, -diff);
1584 case GTK_SCROLL_END:
1586 gdouble diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1588 gtk_spin_button_real_spin (spin, diff);
1593 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1597 gtk_spin_button_update (spin);
1599 if (gtk_adjustment_get_value (priv->adjustment) == old_value)
1600 gtk_widget_error_bell (GTK_WIDGET (spin));
1604 gtk_spin_button_key_release (GtkWidget *widget,
1607 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1608 GtkSpinButtonPrivate *priv = spin->priv;
1610 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1611 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1612 priv->timer_calls = 0;
1618 gtk_spin_button_snap (GtkSpinButton *spin_button,
1621 GtkSpinButtonPrivate *priv = spin_button->priv;
1625 inc = gtk_adjustment_get_step_increment (priv->adjustment);
1629 tmp = (val - gtk_adjustment_get_lower (priv->adjustment)) / inc;
1630 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1631 val = gtk_adjustment_get_lower (priv->adjustment) + floor (tmp) * inc;
1633 val = gtk_adjustment_get_lower (priv->adjustment) + ceil (tmp) * inc;
1635 gtk_spin_button_set_value (spin_button, val);
1639 gtk_spin_button_activate (GtkEntry *entry)
1641 if (gtk_editable_get_editable (GTK_EDITABLE (entry)))
1642 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1644 /* Chain up so that entry->activates_default is honored */
1645 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1649 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1655 GtkSpinButtonPrivate *priv = GTK_SPIN_BUTTON (entry)->priv;
1656 gint up_panel_width, down_panel_width;
1658 up_panel_width = gtk_spin_button_panel_get_width (GTK_SPIN_BUTTON (entry), priv->up_panel);
1659 down_panel_width = gtk_spin_button_panel_get_width (GTK_SPIN_BUTTON (entry), priv->down_panel);
1661 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1663 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
1666 *x += up_panel_width + down_panel_width;
1670 *width -= up_panel_width + down_panel_width;
1674 gtk_spin_button_insert_text (GtkEditable *editable,
1675 const gchar *new_text,
1676 gint new_text_length,
1679 GtkEntry *entry = GTK_ENTRY (editable);
1680 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1681 GtkSpinButtonPrivate *priv = spin->priv;
1682 GtkEditableInterface *parent_editable_iface;
1684 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1696 const gchar *entry_text;
1698 entry_length = gtk_entry_get_text_length (entry);
1699 entry_text = gtk_entry_get_text (entry);
1703 if (*(lc->negative_sign))
1704 neg_sign = *(lc->negative_sign);
1708 if (*(lc->positive_sign))
1709 pos_sign = *(lc->positive_sign);
1714 /* Workaround for bug caused by some Windows application messing
1715 * up the positive sign of the current locale, more specifically
1716 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1717 * See bug #330743 and for instance
1718 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1720 * I don't know if the positive sign always gets bogusly set to
1721 * a digit when the above Registry value is corrupted as
1722 * described. (In my test case, it got set to "8", and in the
1723 * bug report above it presumably was set ot "0".) Probably it
1724 * might get set to almost anything? So how to distinguish a
1725 * bogus value from some correct one for some locale? That is
1726 * probably hard, but at least we should filter out the
1729 if (pos_sign >= '0' && pos_sign <= '9')
1733 for (sign=0, i=0; i<entry_length; i++)
1734 if ((entry_text[i] == neg_sign) ||
1735 (entry_text[i] == pos_sign))
1741 if (sign && !(*position))
1744 for (dotpos=-1, i=0; i<entry_length; i++)
1745 if (entry_text[i] == *(lc->decimal_point))
1751 if (dotpos > -1 && *position > dotpos &&
1752 (gint)priv->digits - entry_length
1753 + dotpos - new_text_length + 1 < 0)
1756 for (i = 0; i < new_text_length; i++)
1758 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1760 if (sign || (*position) || i)
1764 else if (new_text[i] == *(lc->decimal_point))
1766 if (!priv->digits || dotpos > -1 ||
1767 (new_text_length - 1 - i + entry_length
1768 - *position > (gint)priv->digits))
1770 dotpos = *position + i;
1772 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1777 parent_editable_iface->insert_text (editable, new_text,
1778 new_text_length, position);
1782 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1785 GtkSpinButtonPrivate *priv = spin_button->priv;
1786 GtkAdjustment *adjustment;
1787 gdouble new_value = 0.0;
1788 gboolean wrapped = FALSE;
1790 adjustment = priv->adjustment;
1792 new_value = gtk_adjustment_get_value (adjustment) + increment;
1798 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_upper (adjustment)) < EPSILON)
1800 new_value = gtk_adjustment_get_lower (adjustment);
1803 else if (new_value > gtk_adjustment_get_upper (adjustment))
1804 new_value = gtk_adjustment_get_upper (adjustment);
1807 new_value = MIN (new_value, gtk_adjustment_get_upper (adjustment));
1809 else if (increment < 0)
1813 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment)) < EPSILON)
1815 new_value = gtk_adjustment_get_upper (adjustment);
1818 else if (new_value < gtk_adjustment_get_lower (adjustment))
1819 new_value = gtk_adjustment_get_lower (adjustment);
1822 new_value = MAX (new_value, gtk_adjustment_get_lower (adjustment));
1825 if (fabs (new_value - gtk_adjustment_get_value (adjustment)) > EPSILON)
1826 gtk_adjustment_set_value (adjustment, new_value);
1829 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1831 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1835 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1840 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1842 return GTK_INPUT_ERROR;
1848 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1850 GtkSpinButtonPrivate *priv = spin_button->priv;
1852 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, gtk_adjustment_get_value (priv->adjustment));
1854 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1855 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1861 /***********************************************************
1862 ***********************************************************
1863 *** Public interface ***
1864 ***********************************************************
1865 ***********************************************************/
1869 * gtk_spin_button_configure:
1870 * @spin_button: a #GtkSpinButton
1871 * @adjustment: (allow-none): a #GtkAdjustment
1872 * @climb_rate: the new climb rate
1873 * @digits: the number of decimal places to display in the spin button
1875 * Changes the properties of an existing spin button. The adjustment,
1876 * climb rate, and number of decimal places are all changed accordingly,
1877 * after this function call.
1880 gtk_spin_button_configure (GtkSpinButton *spin_button,
1881 GtkAdjustment *adjustment,
1885 GtkSpinButtonPrivate *priv;
1887 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1889 priv = spin_button->priv;
1892 gtk_spin_button_set_adjustment (spin_button, adjustment);
1894 adjustment = priv->adjustment;
1896 g_object_freeze_notify (G_OBJECT (spin_button));
1897 if (priv->digits != digits)
1899 priv->digits = digits;
1900 g_object_notify (G_OBJECT (spin_button), "digits");
1903 if (priv->climb_rate != climb_rate)
1905 priv->climb_rate = climb_rate;
1906 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1908 g_object_thaw_notify (G_OBJECT (spin_button));
1910 gtk_adjustment_value_changed (adjustment);
1914 * gtk_spin_button_new:
1915 * @adjustment: (allow-none): the #GtkAdjustment object that this spin
1916 * button should use, or %NULL
1917 * @climb_rate: specifies how much the spin button changes when an arrow
1919 * @digits: the number of decimal places to display
1921 * Creates a new #GtkSpinButton.
1923 * Returns: The new spin button as a #GtkWidget
1926 gtk_spin_button_new (GtkAdjustment *adjustment,
1930 GtkSpinButton *spin;
1933 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1935 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1937 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1939 return GTK_WIDGET (spin);
1943 * gtk_spin_button_new_with_range:
1944 * @min: Minimum allowable value
1945 * @max: Maximum allowable value
1946 * @step: Increment added or subtracted by spinning the widget
1948 * This is a convenience constructor that allows creation of a numeric
1949 * #GtkSpinButton without manually creating an adjustment. The value is
1950 * initially set to the minimum value and a page increment of 10 * @step
1951 * is the default. The precision of the spin button is equivalent to the
1952 * precision of @step.
1954 * Note that the way in which the precision is derived works best if @step
1955 * is a power of ten. If the resulting precision is not suitable for your
1956 * needs, use gtk_spin_button_set_digits() to correct it.
1958 * Return value: The new spin button as a #GtkWidget
1961 gtk_spin_button_new_with_range (gdouble min,
1965 GtkAdjustment *adjustment;
1966 GtkSpinButton *spin;
1969 g_return_val_if_fail (min <= max, NULL);
1970 g_return_val_if_fail (step != 0.0, NULL);
1972 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1974 adjustment = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1976 if (fabs (step) >= 1.0 || step == 0.0)
1979 digits = abs ((gint) floor (log10 (fabs (step))));
1980 if (digits > MAX_DIGITS)
1981 digits = MAX_DIGITS;
1984 gtk_spin_button_configure (spin, adjustment, step, digits);
1986 gtk_spin_button_set_numeric (spin, TRUE);
1988 return GTK_WIDGET (spin);
1991 /* Callback used when the spin button's adjustment changes.
1992 * We need to redraw the arrows when the adjustment's range
1993 * changes, and reevaluate our size request.
1996 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1998 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1999 GtkSpinButtonPrivate *priv = spin_button->priv;
2001 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
2002 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2006 * gtk_spin_button_set_adjustment:
2007 * @spin_button: a #GtkSpinButton
2008 * @adjustment: a #GtkAdjustment to replace the existing adjustment
2010 * Replaces the #GtkAdjustment associated with @spin_button.
2013 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
2014 GtkAdjustment *adjustment)
2016 GtkSpinButtonPrivate *priv;
2018 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2020 priv = spin_button->priv;
2022 if (priv->adjustment != adjustment)
2024 if (priv->adjustment)
2026 g_signal_handlers_disconnect_by_func (priv->adjustment,
2027 gtk_spin_button_value_changed,
2029 g_signal_handlers_disconnect_by_func (priv->adjustment,
2030 adjustment_changed_cb,
2032 g_object_unref (priv->adjustment);
2034 priv->adjustment = adjustment;
2037 g_object_ref_sink (adjustment);
2038 g_signal_connect (adjustment, "value-changed",
2039 G_CALLBACK (gtk_spin_button_value_changed),
2041 g_signal_connect (adjustment, "changed",
2042 G_CALLBACK (adjustment_changed_cb),
2044 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
2047 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2050 g_object_notify (G_OBJECT (spin_button), "adjustment");
2054 * gtk_spin_button_get_adjustment:
2055 * @spin_button: a #GtkSpinButton
2057 * Get the adjustment associated with a #GtkSpinButton
2059 * Return value: (transfer none): the #GtkAdjustment of @spin_button
2062 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
2064 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
2066 return spin_button->priv->adjustment;
2070 * gtk_spin_button_set_digits:
2071 * @spin_button: a #GtkSpinButton
2072 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
2074 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
2078 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
2081 GtkSpinButtonPrivate *priv;
2083 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2085 priv = spin_button->priv;
2087 if (priv->digits != digits)
2089 priv->digits = digits;
2090 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2091 g_object_notify (G_OBJECT (spin_button), "digits");
2093 /* since lower/upper may have changed */
2094 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2099 * gtk_spin_button_get_digits:
2100 * @spin_button: a #GtkSpinButton
2102 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
2104 * Returns: the current precision
2107 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
2109 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2111 return spin_button->priv->digits;
2115 * gtk_spin_button_set_increments:
2116 * @spin_button: a #GtkSpinButton
2117 * @step: increment applied for a button 1 press.
2118 * @page: increment applied for a button 2 press.
2120 * Sets the step and page increments for spin_button. This affects how
2121 * quickly the value changes when the spin button's arrows are activated.
2124 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
2128 GtkSpinButtonPrivate *priv;
2130 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2132 priv = spin_button->priv;
2134 gtk_adjustment_configure (priv->adjustment,
2135 gtk_adjustment_get_value (priv->adjustment),
2136 gtk_adjustment_get_lower (priv->adjustment),
2137 gtk_adjustment_get_upper (priv->adjustment),
2140 gtk_adjustment_get_page_size (priv->adjustment));
2144 * gtk_spin_button_get_increments:
2145 * @spin_button: a #GtkSpinButton
2146 * @step: (out) (allow-none): location to store step increment, or %NULL
2147 * @page: (out) (allow-none): location to store page increment, or %NULL
2149 * Gets the current step and page the increments used by @spin_button. See
2150 * gtk_spin_button_set_increments().
2153 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
2157 GtkSpinButtonPrivate *priv;
2159 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2161 priv = spin_button->priv;
2164 *step = gtk_adjustment_get_step_increment (priv->adjustment);
2166 *page = gtk_adjustment_get_page_increment (priv->adjustment);
2170 * gtk_spin_button_set_range:
2171 * @spin_button: a #GtkSpinButton
2172 * @min: minimum allowable value
2173 * @max: maximum allowable value
2175 * Sets the minimum and maximum allowable values for @spin_button.
2177 * If the current value is outside this range, it will be adjusted
2178 * to fit within the range, otherwise it will remain unchanged.
2181 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2185 GtkAdjustment *adjustment;
2187 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2189 adjustment = spin_button->priv->adjustment;
2191 gtk_adjustment_configure (adjustment,
2192 CLAMP (gtk_adjustment_get_value (adjustment), min, max),
2195 gtk_adjustment_get_step_increment (adjustment),
2196 gtk_adjustment_get_page_increment (adjustment),
2197 gtk_adjustment_get_page_size (adjustment));
2201 * gtk_spin_button_get_range:
2202 * @spin_button: a #GtkSpinButton
2203 * @min: (out) (allow-none): location to store minimum allowed value, or %NULL
2204 * @max: (out) (allow-none): location to store maximum allowed value, or %NULL
2206 * Gets the range allowed for @spin_button.
2207 * See gtk_spin_button_set_range().
2210 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2214 GtkSpinButtonPrivate *priv;
2216 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2218 priv = spin_button->priv;
2221 *min = gtk_adjustment_get_lower (priv->adjustment);
2223 *max = gtk_adjustment_get_upper (priv->adjustment);
2227 * gtk_spin_button_get_value:
2228 * @spin_button: a #GtkSpinButton
2230 * Get the value in the @spin_button.
2232 * Return value: the value of @spin_button
2235 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2237 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2239 return gtk_adjustment_get_value (spin_button->priv->adjustment);
2243 * gtk_spin_button_get_value_as_int:
2244 * @spin_button: a #GtkSpinButton
2246 * Get the value @spin_button represented as an integer.
2248 * Return value: the value of @spin_button
2251 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2253 GtkSpinButtonPrivate *priv;
2256 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2258 priv = spin_button->priv;
2260 val = gtk_adjustment_get_value (priv->adjustment);
2261 if (val - floor (val) < ceil (val) - val)
2268 * gtk_spin_button_set_value:
2269 * @spin_button: a #GtkSpinButton
2270 * @value: the new value
2272 * Sets the value of @spin_button.
2275 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2278 GtkSpinButtonPrivate *priv;
2280 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2282 priv = spin_button->priv;
2284 if (fabs (value - gtk_adjustment_get_value (priv->adjustment)) > EPSILON)
2285 gtk_adjustment_set_value (priv->adjustment, value);
2288 gint return_val = FALSE;
2289 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2290 if (return_val == FALSE)
2291 gtk_spin_button_default_output (spin_button);
2296 * gtk_spin_button_set_update_policy:
2297 * @spin_button: a #GtkSpinButton
2298 * @policy: a #GtkSpinButtonUpdatePolicy value
2300 * Sets the update behavior of a spin button.
2301 * This determines wether the spin button is always updated
2302 * or only when a valid value is set.
2305 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2306 GtkSpinButtonUpdatePolicy policy)
2308 GtkSpinButtonPrivate *priv;
2310 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2312 priv = spin_button->priv;
2314 if (priv->update_policy != policy)
2316 priv->update_policy = policy;
2317 g_object_notify (G_OBJECT (spin_button), "update-policy");
2322 * gtk_spin_button_get_update_policy:
2323 * @spin_button: a #GtkSpinButton
2325 * Gets the update behavior of a spin button.
2326 * See gtk_spin_button_set_update_policy().
2328 * Return value: the current update policy
2330 GtkSpinButtonUpdatePolicy
2331 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2333 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2335 return spin_button->priv->update_policy;
2339 * gtk_spin_button_set_numeric:
2340 * @spin_button: a #GtkSpinButton
2341 * @numeric: flag indicating if only numeric entry is allowed
2343 * Sets the flag that determines if non-numeric text can be typed
2344 * into the spin button.
2347 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2350 GtkSpinButtonPrivate *priv;
2352 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2354 priv = spin_button->priv;
2356 numeric = numeric != FALSE;
2358 if (priv->numeric != numeric)
2360 priv->numeric = numeric;
2361 g_object_notify (G_OBJECT (spin_button), "numeric");
2366 * gtk_spin_button_get_numeric:
2367 * @spin_button: a #GtkSpinButton
2369 * Returns whether non-numeric text can be typed into the spin button.
2370 * See gtk_spin_button_set_numeric().
2372 * Return value: %TRUE if only numeric text can be entered
2375 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2377 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2379 return spin_button->priv->numeric;
2383 * gtk_spin_button_set_wrap:
2384 * @spin_button: a #GtkSpinButton
2385 * @wrap: a flag indicating if wrapping behavior is performed
2387 * Sets the flag that determines if a spin button value wraps
2388 * around to the opposite limit when the upper or lower limit
2389 * of the range is exceeded.
2392 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2395 GtkSpinButtonPrivate *priv;
2397 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2399 priv = spin_button->priv;
2401 wrap = wrap != FALSE;
2403 if (priv->wrap != wrap)
2407 g_object_notify (G_OBJECT (spin_button), "wrap");
2412 * gtk_spin_button_get_wrap:
2413 * @spin_button: a #GtkSpinButton
2415 * Returns whether the spin button's value wraps around to the
2416 * opposite limit when the upper or lower limit of the range is
2417 * exceeded. See gtk_spin_button_set_wrap().
2419 * Return value: %TRUE if the spin button wraps around
2422 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2424 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2426 return spin_button->priv->wrap;
2430 * gtk_spin_button_set_snap_to_ticks:
2431 * @spin_button: a #GtkSpinButton
2432 * @snap_to_ticks: a flag indicating if invalid values should be corrected
2434 * Sets the policy as to whether values are corrected to the
2435 * nearest step increment when a spin button is activated after
2436 * providing an invalid value.
2439 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2440 gboolean snap_to_ticks)
2442 GtkSpinButtonPrivate *priv;
2445 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2447 priv = spin_button->priv;
2449 new_val = (snap_to_ticks != 0);
2451 if (new_val != priv->snap_to_ticks)
2453 priv->snap_to_ticks = new_val;
2454 if (new_val && gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
2455 gtk_spin_button_update (spin_button);
2457 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2462 * gtk_spin_button_get_snap_to_ticks:
2463 * @spin_button: a #GtkSpinButton
2465 * Returns whether the values are corrected to the nearest step.
2466 * See gtk_spin_button_set_snap_to_ticks().
2468 * Return value: %TRUE if values are snapped to the nearest step
2471 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2473 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2475 return spin_button->priv->snap_to_ticks;
2479 * gtk_spin_button_spin:
2480 * @spin_button: a #GtkSpinButton
2481 * @direction: a #GtkSpinType indicating the direction to spin
2482 * @increment: step increment to apply in the specified direction
2484 * Increment or decrement a spin button's value in a specified
2485 * direction by a specified amount.
2488 gtk_spin_button_spin (GtkSpinButton *spin_button,
2489 GtkSpinType direction,
2492 GtkSpinButtonPrivate *priv;
2493 GtkAdjustment *adjustment;
2496 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2498 priv = spin_button->priv;
2500 adjustment = priv->adjustment;
2502 /* for compatibility with the 1.0.x version of this function */
2503 if (increment != 0 && increment != gtk_adjustment_get_step_increment (adjustment) &&
2504 (direction == GTK_SPIN_STEP_FORWARD ||
2505 direction == GTK_SPIN_STEP_BACKWARD))
2507 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2508 increment = -increment;
2509 direction = GTK_SPIN_USER_DEFINED;
2514 case GTK_SPIN_STEP_FORWARD:
2516 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_step_increment (adjustment));
2519 case GTK_SPIN_STEP_BACKWARD:
2521 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_step_increment (adjustment));
2524 case GTK_SPIN_PAGE_FORWARD:
2526 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_page_increment (adjustment));
2529 case GTK_SPIN_PAGE_BACKWARD:
2531 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_page_increment (adjustment));
2536 diff = gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment);
2538 gtk_spin_button_real_spin (spin_button, -diff);
2543 diff = gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_value (adjustment);
2545 gtk_spin_button_real_spin (spin_button, diff);
2548 case GTK_SPIN_USER_DEFINED:
2551 gtk_spin_button_real_spin (spin_button, increment);
2560 * gtk_spin_button_update:
2561 * @spin_button: a #GtkSpinButton
2563 * Manually force an update of the spin button.
2566 gtk_spin_button_update (GtkSpinButton *spin_button)
2568 GtkSpinButtonPrivate *priv;
2573 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2575 priv = spin_button->priv;
2578 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2579 if (return_val == FALSE)
2581 return_val = gtk_spin_button_default_input (spin_button, &val);
2582 error = (return_val == GTK_INPUT_ERROR);
2584 else if (return_val == GTK_INPUT_ERROR)
2587 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2589 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2591 if (val < gtk_adjustment_get_lower (priv->adjustment))
2592 val = gtk_adjustment_get_lower (priv->adjustment);
2593 else if (val > gtk_adjustment_get_upper (priv->adjustment))
2594 val = gtk_adjustment_get_upper (priv->adjustment);
2596 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2598 val < gtk_adjustment_get_lower (priv->adjustment) ||
2599 val > gtk_adjustment_get_upper (priv->adjustment)))
2601 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2605 if (priv->snap_to_ticks)
2606 gtk_spin_button_snap (spin_button, val);
2608 gtk_spin_button_set_value (spin_button, val);
2612 _gtk_spin_button_get_panels (GtkSpinButton *spin_button,
2613 GdkWindow **down_panel,
2614 GdkWindow **up_panel)
2616 if (down_panel != NULL)
2617 *down_panel = spin_button->priv->down_panel;
2619 if (up_panel != NULL)
2620 *up_panel = spin_button->priv->up_panel;