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 "gtkiconhelperprivate.h"
42 #include "gtkicontheme.h"
44 #include "gtkmarshalers.h"
45 #include "gtkprivate.h"
46 #include "gtksettings.h"
48 #include "gtktypebuiltins.h"
49 #include "gtkwidgetpath.h"
50 #include "gtkwidgetprivate.h"
52 #include "a11y/gtkspinbuttonaccessible.h"
54 #define MIN_SPIN_BUTTON_WIDTH 30
55 #define MAX_TIMER_CALLS 5
58 #define MIN_ARROW_WIDTH 6
62 * SECTION:gtkspinbutton
63 * @Title: GtkSpinButton
64 * @Short_description: Retrieve an integer or floating-point number from
66 * @See_also: #GtkEntry
68 * A #GtkSpinButton is an ideal way to allow the user to set the value of
69 * some attribute. Rather than having to directly type a number into a
70 * #GtkEntry, GtkSpinButton allows the user to click on one of two arrows
71 * to increment or decrement the displayed value. A value can still be
72 * typed in, with the bonus that it can be checked to ensure it is in a
75 * The main properties of a GtkSpinButton are through an adjustment.
76 * See the #GtkAdjustment section for more details about an adjustment's
80 * <title>Using a GtkSpinButton to get an integer</title>
82 * /* Provides a function to retrieve an integer value from a
83 * * GtkSpinButton and creates a spin button to model percentage
88 * grab_int_value (GtkSpinButton *button,
91 * return gtk_spin_button_get_value_as_int (button);
95 * create_integer_spin_button (void)
98 * GtkWidget *window, *button;
99 * GtkAdjustment *adjustment;
101 * adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0);
103 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
104 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
106 * /* creates the spinbutton, with no decimal places */
107 * button = gtk_spin_button_new (adjustment, 1.0, 0);
108 * gtk_container_add (GTK_CONTAINER (window), button);
110 * gtk_widget_show_all (window);
116 * <title>Using a GtkSpinButton to get a floating point value</title>
118 * /* Provides a function to retrieve a floating point value from a
119 * * GtkSpinButton, and creates a high precision spin button.
123 * grab_float_value (GtkSpinButton *button,
124 * gpointer user_data)
126 * return gtk_spin_button_get_value (button);
130 * create_floating_spin_button (void)
132 * GtkWidget *window, *button;
133 * GtkAdjustment *adjustment;
135 * adjustment = gtk_adjustment_new (2.500, 0.0, 5.0, 0.001, 0.1, 0.0);
137 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
138 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
140 * /* creates the spinbutton, with three decimal places */
141 * button = gtk_spin_button_new (adjustment, 0.001, 3);
142 * gtk_container_add (GTK_CONTAINER (window), button);
144 * gtk_widget_show_all (window);
150 struct _GtkSpinButtonPrivate
152 GtkAdjustment *adjustment;
154 GdkWindow *down_panel;
157 GdkWindow *click_child;
162 GtkSpinButtonUpdatePolicy update_policy;
169 guint need_timer : 1;
171 guint snap_to_ticks : 1;
172 guint timer_calls : 3;
199 static void gtk_spin_button_editable_init (GtkEditableInterface *iface);
200 static void gtk_spin_button_finalize (GObject *object);
201 static void gtk_spin_button_set_property (GObject *object,
205 static void gtk_spin_button_get_property (GObject *object,
209 static void gtk_spin_button_destroy (GtkWidget *widget);
210 static void gtk_spin_button_map (GtkWidget *widget);
211 static void gtk_spin_button_unmap (GtkWidget *widget);
212 static void gtk_spin_button_realize (GtkWidget *widget);
213 static void gtk_spin_button_unrealize (GtkWidget *widget);
214 static void gtk_spin_button_get_preferred_width (GtkWidget *widget,
218 static void gtk_spin_button_size_allocate (GtkWidget *widget,
219 GtkAllocation *allocation);
220 static gint gtk_spin_button_draw (GtkWidget *widget,
222 static gint gtk_spin_button_button_press (GtkWidget *widget,
223 GdkEventButton *event);
224 static gint gtk_spin_button_button_release (GtkWidget *widget,
225 GdkEventButton *event);
226 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
227 GdkEventMotion *event);
228 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
229 GdkEventCrossing *event);
230 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
231 GdkEventCrossing *event);
232 static gint gtk_spin_button_focus_out (GtkWidget *widget,
233 GdkEventFocus *event);
234 static void gtk_spin_button_grab_notify (GtkWidget *widget,
235 gboolean was_grabbed);
236 static void gtk_spin_button_state_flags_changed (GtkWidget *widget,
237 GtkStateFlags previous_state);
238 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
239 static gboolean gtk_spin_button_stop_spinning (GtkSpinButton *spin);
240 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
241 GtkSpinButton *spin_button);
242 static gint gtk_spin_button_key_release (GtkWidget *widget,
244 static gint gtk_spin_button_scroll (GtkWidget *widget,
245 GdkEventScroll *event);
246 static void gtk_spin_button_activate (GtkEntry *entry);
247 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
252 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
254 static void gtk_spin_button_insert_text (GtkEditable *editable,
255 const gchar *new_text,
256 gint new_text_length,
258 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
260 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
261 GtkScrollType scroll);
263 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
265 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
267 static guint spinbutton_signals[LAST_SIGNAL] = {0};
269 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
270 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
271 gtk_spin_button_editable_init))
273 #define add_spin_binding(binding_set, keyval, mask, scroll) \
274 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
276 GTK_TYPE_SCROLL_TYPE, scroll)
279 gtk_spin_button_class_init (GtkSpinButtonClass *class)
281 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
282 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
283 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
284 GtkBindingSet *binding_set;
286 gobject_class->finalize = gtk_spin_button_finalize;
287 gobject_class->set_property = gtk_spin_button_set_property;
288 gobject_class->get_property = gtk_spin_button_get_property;
290 widget_class->destroy = gtk_spin_button_destroy;
291 widget_class->map = gtk_spin_button_map;
292 widget_class->unmap = gtk_spin_button_unmap;
293 widget_class->realize = gtk_spin_button_realize;
294 widget_class->unrealize = gtk_spin_button_unrealize;
295 widget_class->get_preferred_width = gtk_spin_button_get_preferred_width;
296 widget_class->size_allocate = gtk_spin_button_size_allocate;
297 widget_class->draw = gtk_spin_button_draw;
298 widget_class->scroll_event = gtk_spin_button_scroll;
299 widget_class->button_press_event = gtk_spin_button_button_press;
300 widget_class->button_release_event = gtk_spin_button_button_release;
301 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
302 widget_class->key_release_event = gtk_spin_button_key_release;
303 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
304 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
305 widget_class->focus_out_event = gtk_spin_button_focus_out;
306 widget_class->grab_notify = gtk_spin_button_grab_notify;
307 widget_class->state_flags_changed = gtk_spin_button_state_flags_changed;
309 entry_class->activate = gtk_spin_button_activate;
310 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
313 class->output = NULL;
314 class->change_value = gtk_spin_button_real_change_value;
316 g_object_class_install_property (gobject_class,
318 g_param_spec_object ("adjustment",
320 P_("The adjustment that holds the value of the spin button"),
322 GTK_PARAM_READWRITE));
324 g_object_class_install_property (gobject_class,
326 g_param_spec_double ("climb-rate",
328 P_("The acceleration rate when you hold down a button"),
332 GTK_PARAM_READWRITE));
334 g_object_class_install_property (gobject_class,
336 g_param_spec_uint ("digits",
338 P_("The number of decimal places to display"),
342 GTK_PARAM_READWRITE));
344 g_object_class_install_property (gobject_class,
346 g_param_spec_boolean ("snap-to-ticks",
348 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
350 GTK_PARAM_READWRITE));
352 g_object_class_install_property (gobject_class,
354 g_param_spec_boolean ("numeric",
356 P_("Whether non-numeric characters should be ignored"),
358 GTK_PARAM_READWRITE));
360 g_object_class_install_property (gobject_class,
362 g_param_spec_boolean ("wrap",
364 P_("Whether a spin button should wrap upon reaching its limits"),
366 GTK_PARAM_READWRITE));
368 g_object_class_install_property (gobject_class,
370 g_param_spec_enum ("update-policy",
372 P_("Whether the spin button should update always, or only when the value is legal"),
373 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
375 GTK_PARAM_READWRITE));
377 g_object_class_install_property (gobject_class,
379 g_param_spec_double ("value",
381 P_("Reads the current value, or sets a new value"),
385 GTK_PARAM_READWRITE));
387 gtk_widget_class_install_style_property_parser (widget_class,
388 g_param_spec_enum ("shadow-type",
390 P_("Style of bevel around the spin button"),
391 GTK_TYPE_SHADOW_TYPE,
394 gtk_rc_property_parse_enum);
397 * GtkSpinButton::input:
398 * @spin_button: the object on which the signal was emitted
399 * @new_value: (out) (type double): return location for the new value
401 * The ::input signal can be used to influence the conversion of
402 * the users input into a double value. The signal handler is
403 * expected to use gtk_entry_get_text() to retrieve the text of
404 * the entry and set @new_value to the new value.
406 * The default conversion uses g_strtod().
408 * Returns: %TRUE for a successful conversion, %FALSE if the input
409 * was not handled, and %GTK_INPUT_ERROR if the conversion failed.
411 spinbutton_signals[INPUT] =
412 g_signal_new (I_("input"),
413 G_TYPE_FROM_CLASS (gobject_class),
415 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
417 _gtk_marshal_INT__POINTER,
422 * GtkSpinButton::output:
423 * @spin_button: the object which received the signal
425 * The ::output signal can be used to change to formatting
426 * of the value that is displayed in the spin buttons entry.
428 * /* show leading zeros */
430 * on_output (GtkSpinButton *spin,
433 * GtkAdjustment *adjustment;
437 * adjustment = gtk_spin_button_get_adjustment (spin);
438 * value = (int)gtk_adjustment_get_value (adjustment);
439 * text = g_strdup_printf ("%02d", value);
440 * gtk_entry_set_text (GTK_ENTRY (spin), text);
447 * Returns: %TRUE if the value has been displayed
449 spinbutton_signals[OUTPUT] =
450 g_signal_new (I_("output"),
451 G_TYPE_FROM_CLASS (gobject_class),
453 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
454 _gtk_boolean_handled_accumulator, NULL,
455 _gtk_marshal_BOOLEAN__VOID,
458 spinbutton_signals[VALUE_CHANGED] =
459 g_signal_new (I_("value-changed"),
460 G_TYPE_FROM_CLASS (gobject_class),
462 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
464 _gtk_marshal_VOID__VOID,
468 * GtkSpinButton::wrapped:
469 * @spinbutton: the object which received the signal
471 * The wrapped signal is emitted right after the spinbutton wraps
472 * from its maximum to minimum value or vice-versa.
476 spinbutton_signals[WRAPPED] =
477 g_signal_new (I_("wrapped"),
478 G_TYPE_FROM_CLASS (gobject_class),
480 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
482 _gtk_marshal_VOID__VOID,
486 spinbutton_signals[CHANGE_VALUE] =
487 g_signal_new (I_("change-value"),
488 G_TYPE_FROM_CLASS (gobject_class),
489 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
490 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
492 _gtk_marshal_VOID__ENUM,
494 GTK_TYPE_SCROLL_TYPE);
496 binding_set = gtk_binding_set_by_class (class);
498 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
499 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
500 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
501 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
502 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
503 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
504 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
505 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
507 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
509 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SPIN_BUTTON_ACCESSIBLE);
513 gtk_spin_button_editable_init (GtkEditableInterface *iface)
515 iface->insert_text = gtk_spin_button_insert_text;
519 gtk_spin_button_set_property (GObject *object,
524 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
525 GtkSpinButtonPrivate *priv = spin_button->priv;
529 GtkAdjustment *adjustment;
531 case PROP_ADJUSTMENT:
532 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
534 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
535 gtk_spin_button_set_adjustment (spin_button, adjustment);
537 case PROP_CLIMB_RATE:
538 gtk_spin_button_configure (spin_button,
540 g_value_get_double (value),
544 gtk_spin_button_configure (spin_button,
547 g_value_get_uint (value));
549 case PROP_SNAP_TO_TICKS:
550 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
553 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
556 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
558 case PROP_UPDATE_POLICY:
559 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
562 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
565 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
571 gtk_spin_button_get_property (GObject *object,
576 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
577 GtkSpinButtonPrivate *priv = spin_button->priv;
581 case PROP_ADJUSTMENT:
582 g_value_set_object (value, priv->adjustment);
584 case PROP_CLIMB_RATE:
585 g_value_set_double (value, priv->climb_rate);
588 g_value_set_uint (value, priv->digits);
590 case PROP_SNAP_TO_TICKS:
591 g_value_set_boolean (value, priv->snap_to_ticks);
594 g_value_set_boolean (value, priv->numeric);
597 g_value_set_boolean (value, priv->wrap);
599 case PROP_UPDATE_POLICY:
600 g_value_set_enum (value, priv->update_policy);
603 g_value_set_double (value, gtk_adjustment_get_value (priv->adjustment));
606 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
612 gtk_spin_button_init (GtkSpinButton *spin_button)
614 GtkSpinButtonPrivate *priv;
615 GtkStyleContext *context;
617 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
618 GTK_TYPE_SPIN_BUTTON,
619 GtkSpinButtonPrivate);
620 priv = spin_button->priv;
622 priv->adjustment = NULL;
623 priv->down_panel = NULL;
624 priv->up_panel = NULL;
626 priv->climb_rate = 0.0;
627 priv->timer_step = 0.0;
628 priv->update_policy = GTK_UPDATE_ALWAYS;
629 priv->in_child = NULL;
630 priv->click_child = NULL;
632 priv->need_timer = FALSE;
633 priv->timer_calls = 0;
635 priv->numeric = FALSE;
637 priv->snap_to_ticks = FALSE;
639 gtk_spin_button_set_adjustment (spin_button,
640 gtk_adjustment_new (0, 0, 0, 0, 0, 0));
642 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
643 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINBUTTON);
645 gtk_widget_add_events (GTK_WIDGET (spin_button), GDK_SCROLL_MASK);
649 gtk_spin_button_finalize (GObject *object)
651 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
653 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
657 gtk_spin_button_destroy (GtkWidget *widget)
659 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
661 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->destroy (widget);
665 gtk_spin_button_map (GtkWidget *widget)
667 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
668 GtkSpinButtonPrivate *priv = spin_button->priv;
670 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
672 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
673 gdk_window_show (priv->down_panel);
674 gdk_window_show (priv->up_panel);
679 gtk_spin_button_unmap (GtkWidget *widget)
681 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
682 GtkSpinButtonPrivate *priv = spin_button->priv;
684 if (gtk_widget_get_mapped (widget))
686 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
688 gdk_window_hide (priv->down_panel);
689 gdk_window_hide (priv->up_panel);
690 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
695 gtk_spin_button_panel_nthchildize_context (GtkSpinButton *spin_button,
696 GtkStyleContext *context,
699 GtkWidget *widget = GTK_WIDGET (spin_button);
700 GtkWidgetPath *path, *siblings_path;
701 GtkTextDirection direction;
702 gint up_pos, down_pos;
704 /* We are a subclass of GtkEntry, which is not a GtkContainer, so we
705 * have to emulate what gtk_container_get_path_for_child() would do
706 * for the button panels
708 path = _gtk_widget_create_path (widget);
709 direction = gtk_widget_get_direction (widget);
710 siblings_path = gtk_widget_path_new ();
712 /* flip children order for RTL */
713 if (direction == GTK_TEXT_DIR_RTL)
715 up_pos = gtk_widget_path_append_type (siblings_path, GTK_TYPE_SPIN_BUTTON);
716 down_pos = gtk_widget_path_append_type (siblings_path, GTK_TYPE_SPIN_BUTTON);
717 gtk_widget_path_append_type (siblings_path, GTK_TYPE_ENTRY);
721 gtk_widget_path_append_type (siblings_path, GTK_TYPE_ENTRY);
722 down_pos = gtk_widget_path_append_type (siblings_path, GTK_TYPE_SPIN_BUTTON);
723 up_pos = gtk_widget_path_append_type (siblings_path, GTK_TYPE_SPIN_BUTTON);
726 gtk_widget_path_iter_add_class (siblings_path, up_pos, GTK_STYLE_CLASS_BUTTON);
727 gtk_widget_path_iter_add_class (siblings_path, down_pos, GTK_STYLE_CLASS_BUTTON);
729 /* this allows compatibility for themes that use .spinbutton.button */
730 gtk_widget_path_iter_add_class (siblings_path, up_pos, GTK_STYLE_CLASS_SPINBUTTON);
731 gtk_widget_path_iter_add_class (siblings_path, down_pos, GTK_STYLE_CLASS_SPINBUTTON);
733 if (panel == spin_button->priv->down_panel)
735 gtk_widget_path_append_with_siblings (path, siblings_path, up_pos);
736 gtk_widget_path_append_with_siblings (path, siblings_path, down_pos);
740 gtk_widget_path_append_with_siblings (path, siblings_path, down_pos);
741 gtk_widget_path_append_with_siblings (path, siblings_path, up_pos);
744 gtk_style_context_set_path (context, path);
745 gtk_widget_path_unref (path);
746 gtk_widget_path_unref (siblings_path);
750 gtk_spin_button_panel_at_limit (GtkSpinButton *spin_button,
753 GtkSpinButtonPrivate *priv = spin_button->priv;
754 GdkWindow *effective_panel;
759 if (gtk_adjustment_get_step_increment (priv->adjustment) > 0)
760 effective_panel = panel;
762 effective_panel = panel == priv->up_panel ? priv->down_panel : priv->up_panel;
764 if (effective_panel == priv->up_panel &&
765 (gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment) <= EPSILON))
768 if (effective_panel == priv->down_panel &&
769 (gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment) <= EPSILON))
776 gtk_spin_button_panel_get_state (GtkSpinButton *spin_button,
780 GtkSpinButtonPrivate *priv = spin_button->priv;
782 state = gtk_widget_get_state_flags (GTK_WIDGET (spin_button));
784 state &= ~(GTK_STATE_FLAG_ACTIVE | GTK_STATE_FLAG_PRELIGHT);
786 if ((state & GTK_STATE_FLAG_INSENSITIVE) ||
787 gtk_spin_button_panel_at_limit (spin_button, panel) ||
788 !gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
790 state |= GTK_STATE_FLAG_INSENSITIVE;
794 if (priv->click_child == panel)
795 state |= GTK_STATE_FLAG_ACTIVE;
796 else if (priv->in_child == panel &&
797 priv->click_child == NULL)
798 state |= GTK_STATE_FLAG_PRELIGHT;
804 static GtkStyleContext *
805 gtk_spin_button_panel_get_context (GtkSpinButton *spin_button,
808 GtkStyleContext *context;
810 context = gtk_style_context_new ();
811 gtk_spin_button_panel_nthchildize_context (spin_button, context, panel);
817 gtk_spin_button_panel_get_width (GtkSpinButton *spin_button,
820 GtkBorder button_padding, button_border;
821 GtkStyleContext *context;
823 gint icon_size, width, height;
825 gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
826 icon_size = MAX (width, height);
828 context = gtk_spin_button_panel_get_context (spin_button, panel);
829 state = gtk_spin_button_panel_get_state (spin_button, panel);
831 gtk_style_context_get_padding (context, state, &button_padding);
832 gtk_style_context_get_border (context, state, &button_border);
834 g_object_unref (context);
836 return icon_size + button_padding.left + button_padding.right +
837 button_border.left + button_border.right;
841 gtk_spin_button_panel_get_allocations (GtkSpinButton *spin_button,
842 GtkAllocation *down_allocation_out,
843 GtkAllocation *up_allocation_out)
845 GtkWidget *widget = GTK_WIDGET (spin_button);
846 GtkSpinButtonPrivate *priv = spin_button->priv;
847 GtkAllocation spin_allocation, down_allocation, up_allocation, allocation;
848 GtkRequisition requisition;
850 gint up_panel_width, down_panel_width;
851 GtkStyleContext *context;
854 gtk_widget_get_allocation (widget, &spin_allocation);
855 gtk_widget_get_preferred_size (widget, &requisition, NULL);
857 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
858 gtk_style_context_get_border (context, GTK_STATE_FLAG_NORMAL, &space);
860 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
861 down_panel_width = gtk_spin_button_panel_get_width (spin_button, priv->down_panel);
862 up_panel_width = gtk_spin_button_panel_get_width (spin_button, priv->up_panel);
864 /* both panels have the same size, and they're as big as the entry allocation,
867 allocation.height = MIN (req_height, spin_allocation.height) - space.top - space.bottom;
868 allocation.y = spin_allocation.y + space.top + (spin_allocation.height - req_height) / 2;
869 down_allocation = up_allocation = allocation;
871 down_allocation.width = down_panel_width;
872 up_allocation.width = up_panel_width;
874 /* invert x axis allocation for RTL */
875 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
877 up_allocation.x = spin_allocation.x + space.left;
878 down_allocation.x = up_allocation.x + up_panel_width;
882 up_allocation.x = spin_allocation.x + spin_allocation.width - up_panel_width - space.right;
883 down_allocation.x = up_allocation.x - down_panel_width;
886 if (down_allocation_out)
887 *down_allocation_out = down_allocation;
888 if (up_allocation_out)
889 *up_allocation_out = up_allocation;
893 gtk_spin_button_panel_draw (GtkSpinButton *spin_button,
897 GtkSpinButtonPrivate *priv = spin_button->priv;
898 GtkStyleContext *context;
901 gdouble width, height, x, y;
902 gint icon_width, icon_height;
903 GtkIconHelper *icon_helper;
905 widget = GTK_WIDGET (spin_button);
908 gtk_cairo_transform_to_window (cr, widget, panel);
910 context = gtk_spin_button_panel_get_context (spin_button, panel);
911 state = gtk_spin_button_panel_get_state (spin_button, panel);
912 gtk_style_context_set_state (context, state);
914 height = gdk_window_get_height (panel);
915 width = gdk_window_get_width (panel);
917 icon_helper = _gtk_icon_helper_new ();
918 _gtk_icon_helper_set_use_fallback (icon_helper, TRUE);
920 if (panel == priv->down_panel)
921 _gtk_icon_helper_set_icon_name (icon_helper, "list-remove-symbolic", GTK_ICON_SIZE_MENU);
923 _gtk_icon_helper_set_icon_name (icon_helper, "list-add-symbolic", GTK_ICON_SIZE_MENU);
925 _gtk_icon_helper_get_size (icon_helper, context,
926 &icon_width, &icon_height);
928 gtk_render_background (context, cr,
929 0, 0, width, height);
930 gtk_render_frame (context, cr,
931 0, 0, width, height);
933 x = floor ((width - icon_width) / 2.0);
934 y = floor ((height - icon_height) / 2.0);
936 _gtk_icon_helper_draw (icon_helper, context, cr,
940 g_object_unref (icon_helper);
941 g_object_unref (context);
945 gtk_spin_button_realize (GtkWidget *widget)
947 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
948 GtkSpinButtonPrivate *priv = spin_button->priv;
949 GtkAllocation down_allocation, up_allocation;
950 GdkWindowAttr attributes;
951 gint attributes_mask;
954 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
955 GDK_KEY_RELEASE_MASK);
956 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
958 attributes.window_type = GDK_WINDOW_CHILD;
959 attributes.wclass = GDK_INPUT_ONLY;
960 attributes.visual = gtk_widget_get_visual (widget);
961 attributes.event_mask = gtk_widget_get_events (widget);
962 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
963 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
964 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
966 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
968 gtk_spin_button_panel_get_allocations (spin_button, &down_allocation, &up_allocation);
970 /* create the left panel window */
971 attributes.x = down_allocation.x;
972 attributes.y = down_allocation.y;
973 attributes.width = down_allocation.width;
974 attributes.height = down_allocation.height;
976 priv->down_panel = gdk_window_new (gtk_widget_get_window (widget),
977 &attributes, attributes_mask);
978 gdk_window_set_user_data (priv->down_panel, widget);
980 /* create the right panel window */
981 attributes.x = up_allocation.x;
982 attributes.y = up_allocation.y;
983 attributes.width = up_allocation.width;
984 attributes.height = up_allocation.height;
986 priv->up_panel = gdk_window_new (gtk_widget_get_window (widget),
987 &attributes, attributes_mask);
988 gdk_window_set_user_data (priv->up_panel, widget);
991 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
992 if (return_val == FALSE)
993 gtk_spin_button_default_output (spin_button);
995 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
999 gtk_spin_button_unrealize (GtkWidget *widget)
1001 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1002 GtkSpinButtonPrivate *priv = spin->priv;
1004 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
1006 if (priv->down_panel)
1008 gdk_window_set_user_data (priv->down_panel, NULL);
1009 gdk_window_destroy (priv->down_panel);
1010 priv->down_panel = NULL;
1015 gdk_window_set_user_data (priv->up_panel, NULL);
1016 gdk_window_destroy (priv->up_panel);
1017 priv->up_panel = NULL;
1022 compute_double_length (double val, int digits)
1028 if (fabs (val) > 1.0)
1029 a = floor (log10 (fabs (val))) + 1;
1041 return a + digits + extra;
1045 gtk_spin_button_get_preferred_width (GtkWidget *widget,
1049 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
1050 GtkSpinButtonPrivate *priv = spin_button->priv;
1051 GtkEntry *entry = GTK_ENTRY (widget);
1052 GtkStyleContext *style_context;
1053 gint up_panel_width, down_panel_width;
1055 style_context = gtk_widget_get_style_context (widget);
1056 up_panel_width = gtk_spin_button_panel_get_width (spin_button, priv->up_panel);
1057 down_panel_width = gtk_spin_button_panel_get_width (spin_button, priv->down_panel);
1059 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->get_preferred_width (widget, minimum, natural);
1061 if (gtk_entry_get_width_chars (entry) < 0)
1063 PangoContext *context;
1064 const PangoFontDescription *font_desc;
1065 PangoFontMetrics *metrics;
1069 gint max_string_len;
1071 gboolean interior_focus;
1075 gtk_style_context_get_style (style_context,
1076 "interior-focus", &interior_focus,
1077 "focus-line-width", &focus_width,
1079 font_desc = gtk_style_context_get_font (style_context, GTK_STATE_FLAG_NORMAL);
1081 context = gtk_widget_get_pango_context (widget);
1082 metrics = pango_context_get_metrics (context, font_desc,
1083 pango_context_get_language (context));
1085 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
1086 digit_width = PANGO_SCALE *
1087 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
1089 pango_font_metrics_unref (metrics);
1091 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
1092 width = MIN_SPIN_BUTTON_WIDTH;
1093 max_string_len = MAX (10, compute_double_length (1e9 * gtk_adjustment_get_step_increment (priv->adjustment),
1096 string_len = compute_double_length (gtk_adjustment_get_upper (priv->adjustment),
1098 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
1099 width = MAX (width, w);
1100 string_len = compute_double_length (gtk_adjustment_get_lower (priv->adjustment), priv->digits);
1101 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
1102 width = MAX (width, w);
1104 _gtk_entry_get_borders (entry, &borders);
1105 width += borders.left + borders.right;
1111 *minimum += up_panel_width + down_panel_width;
1112 *natural += up_panel_width + down_panel_width;
1116 gtk_spin_button_size_allocate (GtkWidget *widget,
1117 GtkAllocation *allocation)
1119 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1120 GtkSpinButtonPrivate *priv = spin->priv;
1121 GtkAllocation down_panel_allocation, up_panel_allocation;
1123 gtk_widget_set_allocation (widget, allocation);
1124 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
1126 gtk_spin_button_panel_get_allocations (spin, &down_panel_allocation, &up_panel_allocation);
1128 if (gtk_widget_get_realized (widget))
1130 gdk_window_move_resize (priv->down_panel,
1131 down_panel_allocation.x,
1132 down_panel_allocation.y,
1133 down_panel_allocation.width,
1134 down_panel_allocation.height);
1136 gdk_window_move_resize (priv->up_panel,
1137 up_panel_allocation.x,
1138 up_panel_allocation.y,
1139 up_panel_allocation.width,
1140 up_panel_allocation.height);
1143 gtk_widget_queue_draw (GTK_WIDGET (spin));
1147 gtk_spin_button_draw (GtkWidget *widget,
1150 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1151 GtkSpinButtonPrivate *priv = spin->priv;
1153 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
1155 /* Draw the buttons */
1156 gtk_spin_button_panel_draw (spin, cr, priv->down_panel);
1157 gtk_spin_button_panel_draw (spin, cr, priv->up_panel);
1163 gtk_spin_button_enter_notify (GtkWidget *widget,
1164 GdkEventCrossing *event)
1166 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1167 GtkSpinButtonPrivate *priv = spin->priv;
1169 if ((event->window == priv->down_panel) ||
1170 (event->window == priv->up_panel))
1172 priv->in_child = event->window;
1173 gtk_widget_queue_draw (GTK_WIDGET (spin));
1176 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1180 gtk_spin_button_leave_notify (GtkWidget *widget,
1181 GdkEventCrossing *event)
1183 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1184 GtkSpinButtonPrivate *priv = spin->priv;
1186 if (priv->in_child != NULL)
1188 priv->in_child = NULL;
1189 gtk_widget_queue_draw (GTK_WIDGET (spin));
1192 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1196 gtk_spin_button_focus_out (GtkWidget *widget,
1197 GdkEventFocus *event)
1199 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1200 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1202 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1206 gtk_spin_button_grab_notify (GtkWidget *widget,
1207 gboolean was_grabbed)
1209 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1213 if (gtk_spin_button_stop_spinning (spin))
1214 gtk_widget_queue_draw (GTK_WIDGET (spin));
1219 gtk_spin_button_state_flags_changed (GtkWidget *widget,
1220 GtkStateFlags previous_state)
1222 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1224 if (!gtk_widget_is_sensitive (widget))
1226 if (gtk_spin_button_stop_spinning (spin))
1227 gtk_widget_queue_draw (GTK_WIDGET (spin));
1232 gtk_spin_button_scroll (GtkWidget *widget,
1233 GdkEventScroll *event)
1235 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1236 GtkSpinButtonPrivate *priv = spin->priv;
1238 if (event->direction == GDK_SCROLL_UP)
1240 if (!gtk_widget_has_focus (widget))
1241 gtk_widget_grab_focus (widget);
1242 gtk_spin_button_real_spin (spin, gtk_adjustment_get_step_increment (priv->adjustment));
1244 else if (event->direction == GDK_SCROLL_DOWN)
1246 if (!gtk_widget_has_focus (widget))
1247 gtk_widget_grab_focus (widget);
1248 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_step_increment (priv->adjustment));
1257 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1259 GtkSpinButtonPrivate *priv = spin->priv;
1260 gboolean did_spin = FALSE;
1264 g_source_remove (priv->timer);
1266 priv->need_timer = FALSE;
1272 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1273 priv->timer_calls = 0;
1275 priv->click_child = NULL;
1281 start_spinning (GtkSpinButton *spin,
1282 GdkWindow *click_child,
1285 GtkSpinButtonPrivate *priv;
1289 priv->click_child = click_child;
1293 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1296 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1298 priv->timer_step = step;
1299 priv->need_timer = TRUE;
1300 priv->timer = gdk_threads_add_timeout (timeout,
1301 (GSourceFunc) gtk_spin_button_timer,
1304 gtk_spin_button_real_spin (spin, click_child == priv->up_panel ? step : -step);
1306 gtk_widget_queue_draw (GTK_WIDGET (spin));
1310 gtk_spin_button_button_press (GtkWidget *widget,
1311 GdkEventButton *event)
1313 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1314 GtkSpinButtonPrivate *priv = spin->priv;
1318 if ((event->window == priv->down_panel) ||
1319 (event->window == priv->up_panel))
1321 if (!gtk_widget_has_focus (widget))
1322 gtk_widget_grab_focus (widget);
1323 priv->button = event->button;
1325 if (gtk_editable_get_editable (GTK_EDITABLE (widget))) {
1326 gtk_spin_button_update (spin);
1328 if (event->button == GDK_BUTTON_PRIMARY)
1329 start_spinning (spin, event->window, gtk_adjustment_get_step_increment (priv->adjustment));
1330 else if (event->button == GDK_BUTTON_MIDDLE)
1331 start_spinning (spin, event->window, gtk_adjustment_get_page_increment (priv->adjustment));
1333 priv->click_child = event->window;
1335 gtk_widget_error_bell (widget);
1340 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1346 gtk_spin_button_button_release (GtkWidget *widget,
1347 GdkEventButton *event)
1349 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1350 GtkSpinButtonPrivate *priv = spin->priv;
1352 if (event->button == priv->button)
1354 GdkWindow *click_child = priv->click_child;
1356 gtk_spin_button_stop_spinning (spin);
1358 if (event->button == GDK_BUTTON_SECONDARY)
1362 if (event->window == priv->down_panel &&
1363 click_child == event->window)
1365 diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1367 gtk_spin_button_real_spin (spin, -diff);
1369 else if (event->window == priv->up_panel &&
1370 click_child == event->window)
1372 diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1374 gtk_spin_button_real_spin (spin, diff);
1377 gtk_widget_queue_draw (GTK_WIDGET (spin));
1382 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1386 gtk_spin_button_motion_notify (GtkWidget *widget,
1387 GdkEventMotion *event)
1389 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1390 GtkSpinButtonPrivate *priv = spin->priv;
1395 if (event->window == priv->down_panel ||
1396 event->window == priv->up_panel)
1398 gdk_event_request_motions (event);
1400 priv->in_child = event->window;
1401 gtk_widget_queue_draw (widget);
1406 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1410 gtk_spin_button_timer (GtkSpinButton *spin_button)
1412 GtkSpinButtonPrivate *priv = spin_button->priv;
1413 gboolean retval = FALSE;
1417 if (priv->click_child == priv->up_panel)
1418 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1420 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1422 if (priv->need_timer)
1424 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1427 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1429 priv->need_timer = FALSE;
1430 priv->timer = gdk_threads_add_timeout (timeout,
1431 (GSourceFunc) gtk_spin_button_timer,
1432 (gpointer) spin_button);
1436 if (priv->climb_rate > 0.0 && priv->timer_step
1437 < gtk_adjustment_get_page_increment (priv->adjustment))
1439 if (priv->timer_calls < MAX_TIMER_CALLS)
1440 priv->timer_calls++;
1443 priv->timer_calls = 0;
1444 priv->timer_step += priv->climb_rate;
1455 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1456 GtkSpinButton *spin_button)
1458 gboolean return_val;
1460 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1463 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1464 if (return_val == FALSE)
1465 gtk_spin_button_default_output (spin_button);
1467 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1469 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1471 g_object_notify (G_OBJECT (spin_button), "value");
1475 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1476 GtkScrollType scroll)
1478 GtkSpinButtonPrivate *priv = spin->priv;
1481 if (!gtk_editable_get_editable (GTK_EDITABLE (spin)))
1483 gtk_widget_error_bell (GTK_WIDGET (spin));
1487 /* When the key binding is activated, there may be an outstanding
1488 * value, so we first have to commit what is currently written in
1489 * the spin buttons text entry. See #106574
1491 gtk_spin_button_update (spin);
1493 old_value = gtk_adjustment_get_value (priv->adjustment);
1497 case GTK_SCROLL_STEP_BACKWARD:
1498 case GTK_SCROLL_STEP_DOWN:
1499 case GTK_SCROLL_STEP_LEFT:
1500 gtk_spin_button_real_spin (spin, -priv->timer_step);
1502 if (priv->climb_rate > 0.0 && priv->timer_step
1503 < gtk_adjustment_get_page_increment (priv->adjustment))
1505 if (priv->timer_calls < MAX_TIMER_CALLS)
1506 priv->timer_calls++;
1509 priv->timer_calls = 0;
1510 priv->timer_step += priv->climb_rate;
1515 case GTK_SCROLL_STEP_FORWARD:
1516 case GTK_SCROLL_STEP_UP:
1517 case GTK_SCROLL_STEP_RIGHT:
1518 gtk_spin_button_real_spin (spin, priv->timer_step);
1520 if (priv->climb_rate > 0.0 && priv->timer_step
1521 < gtk_adjustment_get_page_increment (priv->adjustment))
1523 if (priv->timer_calls < MAX_TIMER_CALLS)
1524 priv->timer_calls++;
1527 priv->timer_calls = 0;
1528 priv->timer_step += priv->climb_rate;
1533 case GTK_SCROLL_PAGE_BACKWARD:
1534 case GTK_SCROLL_PAGE_DOWN:
1535 case GTK_SCROLL_PAGE_LEFT:
1536 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_page_increment (priv->adjustment));
1539 case GTK_SCROLL_PAGE_FORWARD:
1540 case GTK_SCROLL_PAGE_UP:
1541 case GTK_SCROLL_PAGE_RIGHT:
1542 gtk_spin_button_real_spin (spin, gtk_adjustment_get_page_increment (priv->adjustment));
1545 case GTK_SCROLL_START:
1547 gdouble diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1549 gtk_spin_button_real_spin (spin, -diff);
1553 case GTK_SCROLL_END:
1555 gdouble diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1557 gtk_spin_button_real_spin (spin, diff);
1562 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1566 gtk_spin_button_update (spin);
1568 if (gtk_adjustment_get_value (priv->adjustment) == old_value)
1569 gtk_widget_error_bell (GTK_WIDGET (spin));
1573 gtk_spin_button_key_release (GtkWidget *widget,
1576 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1577 GtkSpinButtonPrivate *priv = spin->priv;
1579 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1580 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1581 priv->timer_calls = 0;
1587 gtk_spin_button_snap (GtkSpinButton *spin_button,
1590 GtkSpinButtonPrivate *priv = spin_button->priv;
1594 inc = gtk_adjustment_get_step_increment (priv->adjustment);
1598 tmp = (val - gtk_adjustment_get_lower (priv->adjustment)) / inc;
1599 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1600 val = gtk_adjustment_get_lower (priv->adjustment) + floor (tmp) * inc;
1602 val = gtk_adjustment_get_lower (priv->adjustment) + ceil (tmp) * inc;
1604 gtk_spin_button_set_value (spin_button, val);
1608 gtk_spin_button_activate (GtkEntry *entry)
1610 if (gtk_editable_get_editable (GTK_EDITABLE (entry)))
1611 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1613 /* Chain up so that entry->activates_default is honored */
1614 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1618 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1624 GtkSpinButtonPrivate *priv = GTK_SPIN_BUTTON (entry)->priv;
1625 gint up_panel_width, down_panel_width;
1627 up_panel_width = gtk_spin_button_panel_get_width (GTK_SPIN_BUTTON (entry), priv->up_panel);
1628 down_panel_width = gtk_spin_button_panel_get_width (GTK_SPIN_BUTTON (entry), priv->down_panel);
1630 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1632 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
1635 *x += up_panel_width + down_panel_width;
1639 *width -= up_panel_width + down_panel_width;
1643 gtk_spin_button_insert_text (GtkEditable *editable,
1644 const gchar *new_text,
1645 gint new_text_length,
1648 GtkEntry *entry = GTK_ENTRY (editable);
1649 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1650 GtkSpinButtonPrivate *priv = spin->priv;
1651 GtkEditableInterface *parent_editable_iface;
1653 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1665 const gchar *entry_text;
1667 entry_length = gtk_entry_get_text_length (entry);
1668 entry_text = gtk_entry_get_text (entry);
1672 if (*(lc->negative_sign))
1673 neg_sign = *(lc->negative_sign);
1677 if (*(lc->positive_sign))
1678 pos_sign = *(lc->positive_sign);
1683 /* Workaround for bug caused by some Windows application messing
1684 * up the positive sign of the current locale, more specifically
1685 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1686 * See bug #330743 and for instance
1687 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1689 * I don't know if the positive sign always gets bogusly set to
1690 * a digit when the above Registry value is corrupted as
1691 * described. (In my test case, it got set to "8", and in the
1692 * bug report above it presumably was set ot "0".) Probably it
1693 * might get set to almost anything? So how to distinguish a
1694 * bogus value from some correct one for some locale? That is
1695 * probably hard, but at least we should filter out the
1698 if (pos_sign >= '0' && pos_sign <= '9')
1702 for (sign=0, i=0; i<entry_length; i++)
1703 if ((entry_text[i] == neg_sign) ||
1704 (entry_text[i] == pos_sign))
1710 if (sign && !(*position))
1713 for (dotpos=-1, i=0; i<entry_length; i++)
1714 if (entry_text[i] == *(lc->decimal_point))
1720 if (dotpos > -1 && *position > dotpos &&
1721 (gint)priv->digits - entry_length
1722 + dotpos - new_text_length + 1 < 0)
1725 for (i = 0; i < new_text_length; i++)
1727 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1729 if (sign || (*position) || i)
1733 else if (new_text[i] == *(lc->decimal_point))
1735 if (!priv->digits || dotpos > -1 ||
1736 (new_text_length - 1 - i + entry_length
1737 - *position > (gint)priv->digits))
1739 dotpos = *position + i;
1741 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1746 parent_editable_iface->insert_text (editable, new_text,
1747 new_text_length, position);
1751 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1754 GtkSpinButtonPrivate *priv = spin_button->priv;
1755 GtkAdjustment *adjustment;
1756 gdouble new_value = 0.0;
1757 gboolean wrapped = FALSE;
1759 adjustment = priv->adjustment;
1761 new_value = gtk_adjustment_get_value (adjustment) + increment;
1767 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_upper (adjustment)) < EPSILON)
1769 new_value = gtk_adjustment_get_lower (adjustment);
1772 else if (new_value > gtk_adjustment_get_upper (adjustment))
1773 new_value = gtk_adjustment_get_upper (adjustment);
1776 new_value = MIN (new_value, gtk_adjustment_get_upper (adjustment));
1778 else if (increment < 0)
1782 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment)) < EPSILON)
1784 new_value = gtk_adjustment_get_upper (adjustment);
1787 else if (new_value < gtk_adjustment_get_lower (adjustment))
1788 new_value = gtk_adjustment_get_lower (adjustment);
1791 new_value = MAX (new_value, gtk_adjustment_get_lower (adjustment));
1794 if (fabs (new_value - gtk_adjustment_get_value (adjustment)) > EPSILON)
1795 gtk_adjustment_set_value (adjustment, new_value);
1798 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1800 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1804 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1809 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1811 return GTK_INPUT_ERROR;
1817 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1819 GtkSpinButtonPrivate *priv = spin_button->priv;
1821 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, gtk_adjustment_get_value (priv->adjustment));
1823 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1824 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1830 /***********************************************************
1831 ***********************************************************
1832 *** Public interface ***
1833 ***********************************************************
1834 ***********************************************************/
1838 * gtk_spin_button_configure:
1839 * @spin_button: a #GtkSpinButton
1840 * @adjustment: (allow-none): a #GtkAdjustment
1841 * @climb_rate: the new climb rate
1842 * @digits: the number of decimal places to display in the spin button
1844 * Changes the properties of an existing spin button. The adjustment,
1845 * climb rate, and number of decimal places are all changed accordingly,
1846 * after this function call.
1849 gtk_spin_button_configure (GtkSpinButton *spin_button,
1850 GtkAdjustment *adjustment,
1854 GtkSpinButtonPrivate *priv;
1856 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1858 priv = spin_button->priv;
1861 gtk_spin_button_set_adjustment (spin_button, adjustment);
1863 adjustment = priv->adjustment;
1865 g_object_freeze_notify (G_OBJECT (spin_button));
1866 if (priv->digits != digits)
1868 priv->digits = digits;
1869 g_object_notify (G_OBJECT (spin_button), "digits");
1872 if (priv->climb_rate != climb_rate)
1874 priv->climb_rate = climb_rate;
1875 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1877 g_object_thaw_notify (G_OBJECT (spin_button));
1879 gtk_adjustment_value_changed (adjustment);
1883 * gtk_spin_button_new:
1884 * @adjustment: (allow-none): the #GtkAdjustment object that this spin
1885 * button should use, or %NULL
1886 * @climb_rate: specifies how much the spin button changes when an arrow
1888 * @digits: the number of decimal places to display
1890 * Creates a new #GtkSpinButton.
1892 * Returns: The new spin button as a #GtkWidget
1895 gtk_spin_button_new (GtkAdjustment *adjustment,
1899 GtkSpinButton *spin;
1902 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1904 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1906 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1908 return GTK_WIDGET (spin);
1912 * gtk_spin_button_new_with_range:
1913 * @min: Minimum allowable value
1914 * @max: Maximum allowable value
1915 * @step: Increment added or subtracted by spinning the widget
1917 * This is a convenience constructor that allows creation of a numeric
1918 * #GtkSpinButton without manually creating an adjustment. The value is
1919 * initially set to the minimum value and a page increment of 10 * @step
1920 * is the default. The precision of the spin button is equivalent to the
1921 * precision of @step.
1923 * Note that the way in which the precision is derived works best if @step
1924 * is a power of ten. If the resulting precision is not suitable for your
1925 * needs, use gtk_spin_button_set_digits() to correct it.
1927 * Return value: The new spin button as a #GtkWidget
1930 gtk_spin_button_new_with_range (gdouble min,
1934 GtkAdjustment *adjustment;
1935 GtkSpinButton *spin;
1938 g_return_val_if_fail (min <= max, NULL);
1939 g_return_val_if_fail (step != 0.0, NULL);
1941 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1943 adjustment = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1945 if (fabs (step) >= 1.0 || step == 0.0)
1948 digits = abs ((gint) floor (log10 (fabs (step))));
1949 if (digits > MAX_DIGITS)
1950 digits = MAX_DIGITS;
1953 gtk_spin_button_configure (spin, adjustment, step, digits);
1955 gtk_spin_button_set_numeric (spin, TRUE);
1957 return GTK_WIDGET (spin);
1960 /* Callback used when the spin button's adjustment changes.
1961 * We need to redraw the arrows when the adjustment's range
1962 * changes, and reevaluate our size request.
1965 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1967 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1968 GtkSpinButtonPrivate *priv = spin_button->priv;
1970 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1971 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1975 * gtk_spin_button_set_adjustment:
1976 * @spin_button: a #GtkSpinButton
1977 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1979 * Replaces the #GtkAdjustment associated with @spin_button.
1982 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1983 GtkAdjustment *adjustment)
1985 GtkSpinButtonPrivate *priv;
1987 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1989 priv = spin_button->priv;
1991 if (priv->adjustment != adjustment)
1993 if (priv->adjustment)
1995 g_signal_handlers_disconnect_by_func (priv->adjustment,
1996 gtk_spin_button_value_changed,
1998 g_signal_handlers_disconnect_by_func (priv->adjustment,
1999 adjustment_changed_cb,
2001 g_object_unref (priv->adjustment);
2003 priv->adjustment = adjustment;
2006 g_object_ref_sink (adjustment);
2007 g_signal_connect (adjustment, "value-changed",
2008 G_CALLBACK (gtk_spin_button_value_changed),
2010 g_signal_connect (adjustment, "changed",
2011 G_CALLBACK (adjustment_changed_cb),
2013 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
2016 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2019 g_object_notify (G_OBJECT (spin_button), "adjustment");
2023 * gtk_spin_button_get_adjustment:
2024 * @spin_button: a #GtkSpinButton
2026 * Get the adjustment associated with a #GtkSpinButton
2028 * Return value: (transfer none): the #GtkAdjustment of @spin_button
2031 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
2033 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
2035 return spin_button->priv->adjustment;
2039 * gtk_spin_button_set_digits:
2040 * @spin_button: a #GtkSpinButton
2041 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
2043 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
2047 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
2050 GtkSpinButtonPrivate *priv;
2052 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2054 priv = spin_button->priv;
2056 if (priv->digits != digits)
2058 priv->digits = digits;
2059 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2060 g_object_notify (G_OBJECT (spin_button), "digits");
2062 /* since lower/upper may have changed */
2063 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2068 * gtk_spin_button_get_digits:
2069 * @spin_button: a #GtkSpinButton
2071 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
2073 * Returns: the current precision
2076 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
2078 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2080 return spin_button->priv->digits;
2084 * gtk_spin_button_set_increments:
2085 * @spin_button: a #GtkSpinButton
2086 * @step: increment applied for a button 1 press.
2087 * @page: increment applied for a button 2 press.
2089 * Sets the step and page increments for spin_button. This affects how
2090 * quickly the value changes when the spin button's arrows are activated.
2093 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
2097 GtkSpinButtonPrivate *priv;
2099 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2101 priv = spin_button->priv;
2103 gtk_adjustment_configure (priv->adjustment,
2104 gtk_adjustment_get_value (priv->adjustment),
2105 gtk_adjustment_get_lower (priv->adjustment),
2106 gtk_adjustment_get_upper (priv->adjustment),
2109 gtk_adjustment_get_page_size (priv->adjustment));
2113 * gtk_spin_button_get_increments:
2114 * @spin_button: a #GtkSpinButton
2115 * @step: (out) (allow-none): location to store step increment, or %NULL
2116 * @page: (out) (allow-none): location to store page increment, or %NULL
2118 * Gets the current step and page the increments used by @spin_button. See
2119 * gtk_spin_button_set_increments().
2122 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
2126 GtkSpinButtonPrivate *priv;
2128 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2130 priv = spin_button->priv;
2133 *step = gtk_adjustment_get_step_increment (priv->adjustment);
2135 *page = gtk_adjustment_get_page_increment (priv->adjustment);
2139 * gtk_spin_button_set_range:
2140 * @spin_button: a #GtkSpinButton
2141 * @min: minimum allowable value
2142 * @max: maximum allowable value
2144 * Sets the minimum and maximum allowable values for @spin_button.
2146 * If the current value is outside this range, it will be adjusted
2147 * to fit within the range, otherwise it will remain unchanged.
2150 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2154 GtkAdjustment *adjustment;
2156 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2158 adjustment = spin_button->priv->adjustment;
2160 gtk_adjustment_configure (adjustment,
2161 CLAMP (gtk_adjustment_get_value (adjustment), min, max),
2164 gtk_adjustment_get_step_increment (adjustment),
2165 gtk_adjustment_get_page_increment (adjustment),
2166 gtk_adjustment_get_page_size (adjustment));
2170 * gtk_spin_button_get_range:
2171 * @spin_button: a #GtkSpinButton
2172 * @min: (out) (allow-none): location to store minimum allowed value, or %NULL
2173 * @max: (out) (allow-none): location to store maximum allowed value, or %NULL
2175 * Gets the range allowed for @spin_button.
2176 * See gtk_spin_button_set_range().
2179 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2183 GtkSpinButtonPrivate *priv;
2185 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2187 priv = spin_button->priv;
2190 *min = gtk_adjustment_get_lower (priv->adjustment);
2192 *max = gtk_adjustment_get_upper (priv->adjustment);
2196 * gtk_spin_button_get_value:
2197 * @spin_button: a #GtkSpinButton
2199 * Get the value in the @spin_button.
2201 * Return value: the value of @spin_button
2204 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2206 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2208 return gtk_adjustment_get_value (spin_button->priv->adjustment);
2212 * gtk_spin_button_get_value_as_int:
2213 * @spin_button: a #GtkSpinButton
2215 * Get the value @spin_button represented as an integer.
2217 * Return value: the value of @spin_button
2220 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2222 GtkSpinButtonPrivate *priv;
2225 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2227 priv = spin_button->priv;
2229 val = gtk_adjustment_get_value (priv->adjustment);
2230 if (val - floor (val) < ceil (val) - val)
2237 * gtk_spin_button_set_value:
2238 * @spin_button: a #GtkSpinButton
2239 * @value: the new value
2241 * Sets the value of @spin_button.
2244 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2247 GtkSpinButtonPrivate *priv;
2249 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2251 priv = spin_button->priv;
2253 if (fabs (value - gtk_adjustment_get_value (priv->adjustment)) > EPSILON)
2254 gtk_adjustment_set_value (priv->adjustment, value);
2257 gint return_val = FALSE;
2258 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2259 if (return_val == FALSE)
2260 gtk_spin_button_default_output (spin_button);
2265 * gtk_spin_button_set_update_policy:
2266 * @spin_button: a #GtkSpinButton
2267 * @policy: a #GtkSpinButtonUpdatePolicy value
2269 * Sets the update behavior of a spin button.
2270 * This determines wether the spin button is always updated
2271 * or only when a valid value is set.
2274 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2275 GtkSpinButtonUpdatePolicy policy)
2277 GtkSpinButtonPrivate *priv;
2279 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2281 priv = spin_button->priv;
2283 if (priv->update_policy != policy)
2285 priv->update_policy = policy;
2286 g_object_notify (G_OBJECT (spin_button), "update-policy");
2291 * gtk_spin_button_get_update_policy:
2292 * @spin_button: a #GtkSpinButton
2294 * Gets the update behavior of a spin button.
2295 * See gtk_spin_button_set_update_policy().
2297 * Return value: the current update policy
2299 GtkSpinButtonUpdatePolicy
2300 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2302 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2304 return spin_button->priv->update_policy;
2308 * gtk_spin_button_set_numeric:
2309 * @spin_button: a #GtkSpinButton
2310 * @numeric: flag indicating if only numeric entry is allowed
2312 * Sets the flag that determines if non-numeric text can be typed
2313 * into the spin button.
2316 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2319 GtkSpinButtonPrivate *priv;
2321 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2323 priv = spin_button->priv;
2325 numeric = numeric != FALSE;
2327 if (priv->numeric != numeric)
2329 priv->numeric = numeric;
2330 g_object_notify (G_OBJECT (spin_button), "numeric");
2335 * gtk_spin_button_get_numeric:
2336 * @spin_button: a #GtkSpinButton
2338 * Returns whether non-numeric text can be typed into the spin button.
2339 * See gtk_spin_button_set_numeric().
2341 * Return value: %TRUE if only numeric text can be entered
2344 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2346 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2348 return spin_button->priv->numeric;
2352 * gtk_spin_button_set_wrap:
2353 * @spin_button: a #GtkSpinButton
2354 * @wrap: a flag indicating if wrapping behavior is performed
2356 * Sets the flag that determines if a spin button value wraps
2357 * around to the opposite limit when the upper or lower limit
2358 * of the range is exceeded.
2361 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2364 GtkSpinButtonPrivate *priv;
2366 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2368 priv = spin_button->priv;
2370 wrap = wrap != FALSE;
2372 if (priv->wrap != wrap)
2376 g_object_notify (G_OBJECT (spin_button), "wrap");
2381 * gtk_spin_button_get_wrap:
2382 * @spin_button: a #GtkSpinButton
2384 * Returns whether the spin button's value wraps around to the
2385 * opposite limit when the upper or lower limit of the range is
2386 * exceeded. See gtk_spin_button_set_wrap().
2388 * Return value: %TRUE if the spin button wraps around
2391 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2393 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2395 return spin_button->priv->wrap;
2399 * gtk_spin_button_set_snap_to_ticks:
2400 * @spin_button: a #GtkSpinButton
2401 * @snap_to_ticks: a flag indicating if invalid values should be corrected
2403 * Sets the policy as to whether values are corrected to the
2404 * nearest step increment when a spin button is activated after
2405 * providing an invalid value.
2408 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2409 gboolean snap_to_ticks)
2411 GtkSpinButtonPrivate *priv;
2414 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2416 priv = spin_button->priv;
2418 new_val = (snap_to_ticks != 0);
2420 if (new_val != priv->snap_to_ticks)
2422 priv->snap_to_ticks = new_val;
2423 if (new_val && gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
2424 gtk_spin_button_update (spin_button);
2426 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2431 * gtk_spin_button_get_snap_to_ticks:
2432 * @spin_button: a #GtkSpinButton
2434 * Returns whether the values are corrected to the nearest step.
2435 * See gtk_spin_button_set_snap_to_ticks().
2437 * Return value: %TRUE if values are snapped to the nearest step
2440 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2442 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2444 return spin_button->priv->snap_to_ticks;
2448 * gtk_spin_button_spin:
2449 * @spin_button: a #GtkSpinButton
2450 * @direction: a #GtkSpinType indicating the direction to spin
2451 * @increment: step increment to apply in the specified direction
2453 * Increment or decrement a spin button's value in a specified
2454 * direction by a specified amount.
2457 gtk_spin_button_spin (GtkSpinButton *spin_button,
2458 GtkSpinType direction,
2461 GtkSpinButtonPrivate *priv;
2462 GtkAdjustment *adjustment;
2465 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2467 priv = spin_button->priv;
2469 adjustment = priv->adjustment;
2471 /* for compatibility with the 1.0.x version of this function */
2472 if (increment != 0 && increment != gtk_adjustment_get_step_increment (adjustment) &&
2473 (direction == GTK_SPIN_STEP_FORWARD ||
2474 direction == GTK_SPIN_STEP_BACKWARD))
2476 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2477 increment = -increment;
2478 direction = GTK_SPIN_USER_DEFINED;
2483 case GTK_SPIN_STEP_FORWARD:
2485 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_step_increment (adjustment));
2488 case GTK_SPIN_STEP_BACKWARD:
2490 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_step_increment (adjustment));
2493 case GTK_SPIN_PAGE_FORWARD:
2495 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_page_increment (adjustment));
2498 case GTK_SPIN_PAGE_BACKWARD:
2500 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_page_increment (adjustment));
2505 diff = gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment);
2507 gtk_spin_button_real_spin (spin_button, -diff);
2512 diff = gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_value (adjustment);
2514 gtk_spin_button_real_spin (spin_button, diff);
2517 case GTK_SPIN_USER_DEFINED:
2520 gtk_spin_button_real_spin (spin_button, increment);
2529 * gtk_spin_button_update:
2530 * @spin_button: a #GtkSpinButton
2532 * Manually force an update of the spin button.
2535 gtk_spin_button_update (GtkSpinButton *spin_button)
2537 GtkSpinButtonPrivate *priv;
2542 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2544 priv = spin_button->priv;
2547 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2548 if (return_val == FALSE)
2550 return_val = gtk_spin_button_default_input (spin_button, &val);
2551 error = (return_val == GTK_INPUT_ERROR);
2553 else if (return_val == GTK_INPUT_ERROR)
2556 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2558 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2560 if (val < gtk_adjustment_get_lower (priv->adjustment))
2561 val = gtk_adjustment_get_lower (priv->adjustment);
2562 else if (val > gtk_adjustment_get_upper (priv->adjustment))
2563 val = gtk_adjustment_get_upper (priv->adjustment);
2565 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2567 val < gtk_adjustment_get_lower (priv->adjustment) ||
2568 val > gtk_adjustment_get_upper (priv->adjustment)))
2570 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2574 if (priv->snap_to_ticks)
2575 gtk_spin_button_snap (spin_button, val);
2577 gtk_spin_button_set_value (spin_button, val);
2581 _gtk_spin_button_get_panels (GtkSpinButton *spin_button,
2582 GdkWindow **down_panel,
2583 GdkWindow **up_panel)
2585 if (down_panel != NULL)
2586 *down_panel = spin_button->priv->down_panel;
2588 if (up_panel != NULL)
2589 *up_panel = spin_button->priv->up_panel;