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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
38 #include "gtkbindings.h"
39 #include "gtkspinbutton.h"
40 #include "gtkentryprivate.h"
41 #include "gtkmainprivate.h"
42 #include "gtkmarshalers.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
46 #include "gtktypebuiltins.h"
48 #define MIN_SPIN_BUTTON_WIDTH 30
49 #define MAX_TIMER_CALLS 5
52 #define MIN_ARROW_WIDTH 6
56 * SECTION:gtkspinbutton
57 * @Title: GtkSpinButton
58 * @Short_description: Retrieve an integer or floating-point number from
60 * @See_also: #GtkEntry
62 * A #GtkSpinButton is an ideal way to allow the user to set the value of
63 * some attribute. Rather than having to directly type a number into a
64 * #GtkEntry, GtkSpinButton allows the user to click on one of two arrows
65 * to increment or decrement the displayed value. A value can still be
66 * typed in, with the bonus that it can be checked to ensure it is in a
69 * The main properties of a GtkSpinButton are through an adjustment.
70 * See the #GtkAdjustment section for more details about an adjustment's
74 * <title>Using a GtkSpinButton to get an integer</title>
76 * /* Provides a function to retrieve an integer value from a
77 * * GtkSpinButton and creates a spin button to model percentage
82 * grab_int_value (GtkSpinButton *button,
85 * return gtk_spin_button_get_value_as_int (button);
89 * create_integer_spin_button (void)
92 * GtkWidget *window, *button;
93 * GtkAdjustment *adjustment;
95 * adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0);
97 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
98 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
100 * /* creates the spinbutton, with no decimal places */
101 * button = gtk_spin_button_new (adjustment, 1.0, 0);
102 * gtk_container_add (GTK_CONTAINER (window), button);
104 * gtk_widget_show_all (window);
110 * <title>Using a GtkSpinButton to get a floating point value</title>
112 * /* Provides a function to retrieve a floating point value from a
113 * * GtkSpinButton, and creates a high precision spin button.
117 * grab_float_value (GtkSpinButton *button,
118 * gpointer user_data)
120 * return gtk_spin_button_get_value (button);
124 * create_floating_spin_button (void)
126 * GtkWidget *window, *button;
127 * GtkAdjustment *adjustment;
129 * adjustment = gtk_adjustment_new (2.500, 0.0, 5.0, 0.001, 0.1, 0.0);
131 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
132 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
134 * /* creates the spinbutton, with three decimal places */
135 * button = gtk_spin_button_new (adjustment, 0.001, 3);
136 * gtk_container_add (GTK_CONTAINER (window), button);
138 * gtk_widget_show_all (window);
144 struct _GtkSpinButtonPrivate
146 GtkSpinButtonUpdatePolicy update_policy;
147 GtkAdjustment *adjustment;
157 guint click_child : 2; /* valid: GTK_ARROW_UP=0, GTK_ARROW_DOWN=1 or 2=NONE/BOTH */
160 guint need_timer : 1;
162 guint snap_to_ticks : 1;
163 guint timer_calls : 3;
190 static void gtk_spin_button_editable_init (GtkEditableInterface *iface);
191 static void gtk_spin_button_finalize (GObject *object);
192 static void gtk_spin_button_set_property (GObject *object,
196 static void gtk_spin_button_get_property (GObject *object,
200 static void gtk_spin_button_destroy (GtkWidget *widget);
201 static void gtk_spin_button_map (GtkWidget *widget);
202 static void gtk_spin_button_unmap (GtkWidget *widget);
203 static void gtk_spin_button_realize (GtkWidget *widget);
204 static void gtk_spin_button_unrealize (GtkWidget *widget);
205 static void gtk_spin_button_get_preferred_width (GtkWidget *widget,
209 static void gtk_spin_button_size_allocate (GtkWidget *widget,
210 GtkAllocation *allocation);
211 static gint gtk_spin_button_draw (GtkWidget *widget,
213 static gint gtk_spin_button_button_press (GtkWidget *widget,
214 GdkEventButton *event);
215 static gint gtk_spin_button_button_release (GtkWidget *widget,
216 GdkEventButton *event);
217 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
218 GdkEventMotion *event);
219 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
220 GdkEventCrossing *event);
221 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
222 GdkEventCrossing *event);
223 static gint gtk_spin_button_focus_out (GtkWidget *widget,
224 GdkEventFocus *event);
225 static void gtk_spin_button_grab_notify (GtkWidget *widget,
226 gboolean was_grabbed);
227 static void gtk_spin_button_state_flags_changed (GtkWidget *widget,
228 GtkStateFlags previous_state);
229 static void gtk_spin_button_style_updated (GtkWidget *widget);
230 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
231 GtkStyleContext *context,
233 GtkArrowType arrow_type);
234 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
235 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
236 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
237 GtkSpinButton *spin_button);
238 static gint gtk_spin_button_key_release (GtkWidget *widget,
240 static gint gtk_spin_button_scroll (GtkWidget *widget,
241 GdkEventScroll *event);
242 static void gtk_spin_button_activate (GtkEntry *entry);
243 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
248 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
250 static void gtk_spin_button_insert_text (GtkEditable *editable,
251 const gchar *new_text,
252 gint new_text_length,
254 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
256 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
257 GtkScrollType scroll);
259 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
261 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
263 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
265 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;
308 widget_class->style_updated = gtk_spin_button_style_updated;
310 entry_class->activate = gtk_spin_button_activate;
311 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
314 class->output = NULL;
315 class->change_value = gtk_spin_button_real_change_value;
317 g_object_class_install_property (gobject_class,
319 g_param_spec_object ("adjustment",
321 P_("The adjustment that holds the value of the spin button"),
323 GTK_PARAM_READWRITE));
325 g_object_class_install_property (gobject_class,
327 g_param_spec_double ("climb-rate",
329 P_("The acceleration rate when you hold down a button"),
333 GTK_PARAM_READWRITE));
335 g_object_class_install_property (gobject_class,
337 g_param_spec_uint ("digits",
339 P_("The number of decimal places to display"),
343 GTK_PARAM_READWRITE));
345 g_object_class_install_property (gobject_class,
347 g_param_spec_boolean ("snap-to-ticks",
349 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
351 GTK_PARAM_READWRITE));
353 g_object_class_install_property (gobject_class,
355 g_param_spec_boolean ("numeric",
357 P_("Whether non-numeric characters should be ignored"),
359 GTK_PARAM_READWRITE));
361 g_object_class_install_property (gobject_class,
363 g_param_spec_boolean ("wrap",
365 P_("Whether a spin button should wrap upon reaching its limits"),
367 GTK_PARAM_READWRITE));
369 g_object_class_install_property (gobject_class,
371 g_param_spec_enum ("update-policy",
373 P_("Whether the spin button should update always, or only when the value is legal"),
374 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
376 GTK_PARAM_READWRITE));
378 g_object_class_install_property (gobject_class,
380 g_param_spec_double ("value",
382 P_("Reads the current value, or sets a new value"),
386 GTK_PARAM_READWRITE));
388 gtk_widget_class_install_style_property_parser (widget_class,
389 g_param_spec_enum ("shadow-type",
391 P_("Style of bevel around the spin button"),
392 GTK_TYPE_SHADOW_TYPE,
395 gtk_rc_property_parse_enum);
398 * GtkSpinButton::input:
399 * @spin_button: the object on which the signal was emitted
400 * @new_value: return location for the new value
402 * The ::input signal can be used to influence the conversion of
403 * the users input into a double value. The signal handler is
404 * expected to use gtk_entry_get_text() to retrieve the text of
405 * the entry and set @new_value to the new value.
407 * The default conversion uses g_strtod().
409 * Returns: %TRUE for a successful conversion, %FALSE if the input
410 * was not handled, and %GTK_INPUT_ERROR if the conversion failed.
412 spinbutton_signals[INPUT] =
413 g_signal_new (I_("input"),
414 G_TYPE_FROM_CLASS (gobject_class),
416 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
418 _gtk_marshal_INT__POINTER,
423 * GtkSpinButton::output:
424 * @spin_button: the object which received the signal
426 * The ::output signal can be used to change to formatting
427 * of the value that is displayed in the spin buttons entry.
429 * /* show leading zeros */
431 * on_output (GtkSpinButton *spin,
434 * GtkAdjustment *adjustment;
438 * adjustment = gtk_spin_button_get_adjustment (spin);
439 * value = (int)gtk_adjustment_get_value (adjustment);
440 * text = g_strdup_printf ("%02d", value);
441 * gtk_entry_set_text (GTK_ENTRY (spin), text);
448 * Returns: %TRUE if the value has been displayed
450 spinbutton_signals[OUTPUT] =
451 g_signal_new (I_("output"),
452 G_TYPE_FROM_CLASS (gobject_class),
454 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
455 _gtk_boolean_handled_accumulator, NULL,
456 _gtk_marshal_BOOLEAN__VOID,
459 spinbutton_signals[VALUE_CHANGED] =
460 g_signal_new (I_("value-changed"),
461 G_TYPE_FROM_CLASS (gobject_class),
463 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
465 _gtk_marshal_VOID__VOID,
469 * GtkSpinButton::wrapped:
470 * @spinbutton: the object which received the signal
472 * The wrapped signal is emitted right after the spinbutton wraps
473 * from its maximum to minimum value or vice-versa.
477 spinbutton_signals[WRAPPED] =
478 g_signal_new (I_("wrapped"),
479 G_TYPE_FROM_CLASS (gobject_class),
481 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
483 _gtk_marshal_VOID__VOID,
487 spinbutton_signals[CHANGE_VALUE] =
488 g_signal_new (I_("change-value"),
489 G_TYPE_FROM_CLASS (gobject_class),
490 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
491 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
493 _gtk_marshal_VOID__ENUM,
495 GTK_TYPE_SCROLL_TYPE);
497 binding_set = gtk_binding_set_by_class (class);
499 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
500 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
501 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
502 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
503 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
504 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
505 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
506 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
508 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
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);
611 gtk_spin_button_init (GtkSpinButton *spin_button)
613 GtkSpinButtonPrivate *priv;
614 GtkStyleContext *context;
616 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
617 GTK_TYPE_SPIN_BUTTON,
618 GtkSpinButtonPrivate);
619 priv = spin_button->priv;
621 priv->adjustment = NULL;
624 priv->climb_rate = 0.0;
625 priv->timer_step = 0.0;
626 priv->update_policy = GTK_UPDATE_ALWAYS;
627 priv->in_child = NO_ARROW;
628 priv->click_child = NO_ARROW;
630 priv->need_timer = FALSE;
631 priv->timer_calls = 0;
633 priv->numeric = FALSE;
635 priv->snap_to_ticks = FALSE;
637 gtk_spin_button_set_adjustment (spin_button,
638 gtk_adjustment_new (0, 0, 0, 0, 0, 0));
640 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
641 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINBUTTON);
645 gtk_spin_button_finalize (GObject *object)
647 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
649 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
653 gtk_spin_button_destroy (GtkWidget *widget)
655 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
657 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->destroy (widget);
661 gtk_spin_button_map (GtkWidget *widget)
663 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
664 GtkSpinButtonPrivate *priv = spin_button->priv;
666 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
668 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
669 gdk_window_show (priv->panel);
674 gtk_spin_button_unmap (GtkWidget *widget)
676 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
677 GtkSpinButtonPrivate *priv = spin_button->priv;
679 if (gtk_widget_get_mapped (widget))
681 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
683 gdk_window_hide (priv->panel);
684 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
689 gtk_spin_button_realize (GtkWidget *widget)
691 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
692 GtkSpinButtonPrivate *priv = spin_button->priv;
693 GtkStyleContext *context;
695 GtkAllocation allocation;
696 GtkRequisition requisition;
697 GdkWindowAttr attributes;
698 gint attributes_mask;
703 arrow_size = spin_button_get_arrow_size (spin_button);
705 gtk_widget_get_preferred_size (widget, &requisition, NULL);
706 gtk_widget_get_allocation (widget, &allocation);
708 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
709 GDK_KEY_RELEASE_MASK);
710 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
712 attributes.window_type = GDK_WINDOW_CHILD;
713 attributes.wclass = GDK_INPUT_ONLY;
714 attributes.visual = gtk_widget_get_visual (widget);
715 attributes.event_mask = gtk_widget_get_events (widget);
716 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
717 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
718 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
720 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
722 state = gtk_widget_get_state_flags (widget);
723 context = gtk_widget_get_style_context (widget);
724 gtk_style_context_get_padding (context, state, &padding);
726 attributes.x = allocation.x + allocation.width - arrow_size - (padding.left + padding.right);
727 attributes.y = allocation.y + (allocation.height - requisition.height) / 2;
728 attributes.width = arrow_size + padding.left + padding.right;
729 attributes.height = requisition.height;
731 priv->panel = gdk_window_new (gtk_widget_get_window (widget),
732 &attributes, attributes_mask);
733 gdk_window_set_user_data (priv->panel, widget);
736 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
737 if (return_val == FALSE)
738 gtk_spin_button_default_output (spin_button);
740 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
744 gtk_spin_button_unrealize (GtkWidget *widget)
746 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
747 GtkSpinButtonPrivate *priv = spin->priv;
749 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
753 gdk_window_set_user_data (priv->panel, NULL);
754 gdk_window_destroy (priv->panel);
760 compute_double_length (double val, int digits)
766 if (fabs (val) > 1.0)
767 a = floor (log10 (fabs (val))) + 1;
779 return a + digits + extra;
783 gtk_spin_button_get_preferred_width (GtkWidget *widget,
787 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
788 GtkSpinButtonPrivate *priv = spin_button->priv;
789 GtkEntry *entry = GTK_ENTRY (widget);
790 GtkStyleContext *style_context;
794 style_context = gtk_widget_get_style_context (widget);
796 arrow_size = spin_button_get_arrow_size (spin_button);
798 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->get_preferred_width (widget, minimum, natural);
800 if (gtk_entry_get_width_chars (entry) < 0)
802 PangoContext *context;
803 const PangoFontDescription *font_desc;
804 PangoFontMetrics *metrics;
810 gboolean interior_focus;
812 gint xborder, yborder;
813 GtkBorder inner_border;
815 gtk_widget_style_get (widget,
816 "interior-focus", &interior_focus,
817 "focus-line-width", &focus_width,
820 font_desc = gtk_style_context_get_font (style_context, 0);
822 context = gtk_widget_get_pango_context (widget);
823 metrics = pango_context_get_metrics (context, font_desc,
824 pango_context_get_language (context));
826 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
827 digit_width = PANGO_SCALE *
828 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
830 pango_font_metrics_unref (metrics);
832 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
833 width = MIN_SPIN_BUTTON_WIDTH;
834 max_string_len = MAX (10, compute_double_length (1e9 * gtk_adjustment_get_step_increment (priv->adjustment),
837 string_len = compute_double_length (gtk_adjustment_get_upper (priv->adjustment),
839 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
840 width = MAX (width, w);
841 string_len = compute_double_length (gtk_adjustment_get_lower (priv->adjustment), priv->digits);
842 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
843 width = MAX (width, w);
845 _gtk_entry_get_borders (entry, &xborder, &yborder);
846 _gtk_entry_effective_inner_border (entry, &inner_border);
848 width += xborder * 2 + inner_border.left + inner_border.right;
854 gtk_style_context_get_padding (style_context,
855 gtk_widget_get_state_flags (widget),
858 *minimum += arrow_size + padding.left + padding.right;
859 *natural += arrow_size + padding.left + padding.right;
863 gtk_spin_button_size_allocate (GtkWidget *widget,
864 GtkAllocation *allocation)
866 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
867 GtkSpinButtonPrivate *priv = spin->priv;
868 GtkAllocation panel_allocation;
869 GtkRequisition requisition;
870 GtkStyleContext *context;
876 arrow_size = spin_button_get_arrow_size (spin);
877 context = gtk_widget_get_style_context (widget);
878 state = gtk_widget_get_state_flags (widget);
880 gtk_style_context_get_padding (context, state, &padding);
881 panel_width = arrow_size + padding.left + padding.right;
883 gtk_widget_get_preferred_size (widget, &requisition, NULL);
885 gtk_widget_set_allocation (widget, allocation);
887 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
888 panel_allocation.x = allocation->x;
890 panel_allocation.x = allocation->x + allocation->width - panel_width;
892 panel_allocation.width = panel_width;
893 panel_allocation.height = MIN (requisition.height, allocation->height);
895 panel_allocation.y = allocation->y +
896 (allocation->height - requisition.height) / 2;
898 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
900 if (gtk_widget_get_realized (widget))
902 gdk_window_move_resize (priv->panel,
905 panel_allocation.width,
906 panel_allocation.height);
909 gtk_widget_queue_draw (GTK_WIDGET (spin));
913 gtk_spin_button_draw (GtkWidget *widget,
916 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
917 GtkSpinButtonPrivate *priv = spin->priv;
918 GtkStyleContext *context;
919 GtkStateFlags state = 0;
922 is_rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
923 context = gtk_widget_get_style_context (widget);
924 gtk_style_context_save (context);
927 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_LEFT);
929 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_RIGHT);
931 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
933 gtk_style_context_restore (context);
936 state = gtk_widget_get_state_flags (widget);
938 if (gtk_widget_has_focus (widget))
939 state |= GTK_STATE_FLAG_FOCUSED;
941 gtk_style_context_save (context);
942 gtk_style_context_set_state (context, state);
943 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_ENTRY);
946 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_RIGHT);
948 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_LEFT);
950 gtk_cairo_transform_to_window (cr, widget, priv->panel);
952 if (gtk_entry_get_has_frame (GTK_ENTRY (widget)))
953 gtk_render_background (context, cr, 0, 0,
954 gdk_window_get_width (priv->panel),
955 gdk_window_get_height (priv->panel));
957 gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_UP);
958 gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_DOWN);
960 gtk_style_context_restore (context);
967 spin_button_at_limit (GtkSpinButton *spin_button,
970 GtkSpinButtonPrivate *priv = spin_button->priv;
971 GtkArrowType effective_arrow;
976 if (gtk_adjustment_get_step_increment (priv->adjustment) > 0)
977 effective_arrow = arrow;
979 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
981 if (effective_arrow == GTK_ARROW_UP &&
982 (gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment) <= EPSILON))
985 if (effective_arrow == GTK_ARROW_DOWN &&
986 (gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment) <= EPSILON))
993 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
994 GtkStyleContext *context,
996 GtkArrowType arrow_type)
998 GtkSpinButtonPrivate *priv;
999 GtkJunctionSides junction;
1000 GtkStateFlags state;
1011 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
1013 gtk_style_context_save (context);
1014 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
1016 priv = spin_button->priv;
1017 widget = GTK_WIDGET (spin_button);
1018 junction = gtk_style_context_get_junction_sides (context);
1020 panel_height = gdk_window_get_height (priv->panel);
1022 if (arrow_type == GTK_ARROW_UP)
1027 height = panel_height / 2;
1029 junction |= GTK_JUNCTION_BOTTOM;
1034 y = panel_height / 2;
1036 height = (panel_height + 1) / 2;
1038 junction |= GTK_JUNCTION_TOP;
1041 if (spin_button_at_limit (spin_button, arrow_type))
1042 state = GTK_STATE_FLAG_INSENSITIVE;
1045 if (priv->click_child == arrow_type)
1046 state = GTK_STATE_ACTIVE;
1049 if (priv->in_child == arrow_type &&
1050 priv->click_child == NO_ARROW)
1051 state = GTK_STATE_FLAG_PRELIGHT;
1053 state = gtk_widget_get_state_flags (widget);
1057 gtk_style_context_get_padding (context, state, &padding);
1058 gtk_style_context_set_junction_sides (context, junction);
1059 gtk_style_context_set_state (context, state);
1061 width = spin_button_get_arrow_size (spin_button) + padding.left + padding.right;
1063 gtk_render_background (context, cr, x, y, width, height);
1064 gtk_render_frame (context, cr, x, y, width, height);
1066 height = panel_height;
1068 if (arrow_type == GTK_ARROW_DOWN)
1071 height = height - y - 2;
1076 height = height / 2 - 2;
1081 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1087 w -= w % 2 - 1; /* force odd */
1090 x += (width - w) / 2;
1091 y += (height - h) / 2;
1096 gtk_render_arrow (context, cr,
1097 angle, x, y, width);
1099 gtk_style_context_restore (context);
1103 gtk_spin_button_enter_notify (GtkWidget *widget,
1104 GdkEventCrossing *event)
1106 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1107 GtkSpinButtonPrivate *priv = spin->priv;
1108 GtkRequisition requisition;
1110 if (event->window == priv->panel)
1116 device = gdk_event_get_device ((GdkEvent *) event);
1117 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
1119 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1121 if (y <= requisition.height / 2)
1122 priv->in_child = GTK_ARROW_UP;
1124 priv->in_child = GTK_ARROW_DOWN;
1126 gtk_widget_queue_draw (GTK_WIDGET (spin));
1129 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event)
1130 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1136 gtk_spin_button_leave_notify (GtkWidget *widget,
1137 GdkEventCrossing *event)
1139 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1140 GtkSpinButtonPrivate *priv = spin->priv;
1142 priv->in_child = NO_ARROW;
1143 gtk_widget_queue_draw (GTK_WIDGET (spin));
1145 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event)
1146 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1152 gtk_spin_button_focus_out (GtkWidget *widget,
1153 GdkEventFocus *event)
1155 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1156 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1158 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1162 gtk_spin_button_grab_notify (GtkWidget *widget,
1163 gboolean was_grabbed)
1165 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1169 gtk_spin_button_stop_spinning (spin);
1170 gtk_widget_queue_draw (GTK_WIDGET (spin));
1175 gtk_spin_button_state_flags_changed (GtkWidget *widget,
1176 GtkStateFlags previous_state)
1178 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1180 if (!gtk_widget_is_sensitive (widget))
1182 gtk_spin_button_stop_spinning (spin);
1183 gtk_widget_queue_draw (GTK_WIDGET (spin));
1188 gtk_spin_button_style_updated (GtkWidget *widget)
1190 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1191 GtkSpinButtonPrivate *priv = spin->priv;
1193 if (gtk_widget_get_realized (widget))
1195 GtkStyleContext *context;
1197 context = gtk_widget_get_style_context (widget);
1198 gtk_style_context_set_background (context, priv->panel);
1201 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_updated (widget);
1206 gtk_spin_button_scroll (GtkWidget *widget,
1207 GdkEventScroll *event)
1209 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1210 GtkSpinButtonPrivate *priv = spin->priv;
1212 if (event->direction == GDK_SCROLL_UP)
1214 if (!gtk_widget_has_focus (widget))
1215 gtk_widget_grab_focus (widget);
1216 gtk_spin_button_real_spin (spin, gtk_adjustment_get_step_increment (priv->adjustment));
1218 else if (event->direction == GDK_SCROLL_DOWN)
1220 if (!gtk_widget_has_focus (widget))
1221 gtk_widget_grab_focus (widget);
1222 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_step_increment (priv->adjustment));
1231 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1233 GtkSpinButtonPrivate *priv = spin->priv;
1237 g_source_remove (priv->timer);
1239 priv->timer_calls = 0;
1240 priv->need_timer = FALSE;
1245 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1246 priv->timer_calls = 0;
1248 priv->click_child = NO_ARROW;
1253 start_spinning (GtkSpinButton *spin,
1254 GtkArrowType click_child,
1257 GtkSpinButtonPrivate *priv;
1259 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1263 priv->click_child = click_child;
1267 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1270 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1272 priv->timer_step = step;
1273 priv->need_timer = TRUE;
1274 priv->timer = gdk_threads_add_timeout (timeout,
1275 (GSourceFunc) gtk_spin_button_timer,
1278 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1280 gtk_widget_queue_draw (GTK_WIDGET (spin));
1284 gtk_spin_button_button_press (GtkWidget *widget,
1285 GdkEventButton *event)
1287 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1288 GtkSpinButtonPrivate *priv = spin->priv;
1292 if (event->window == priv->panel)
1294 GtkRequisition requisition;
1296 if (!gtk_widget_has_focus (widget))
1297 gtk_widget_grab_focus (widget);
1298 priv->button = event->button;
1300 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1301 gtk_spin_button_update (spin);
1303 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1305 if (event->y <= requisition.height / 2)
1307 if (event->button == 1)
1308 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_step_increment (priv->adjustment));
1309 else if (event->button == 2)
1310 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_page_increment (priv->adjustment));
1312 priv->click_child = GTK_ARROW_UP;
1316 if (event->button == 1)
1317 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_step_increment (priv->adjustment));
1318 else if (event->button == 2)
1319 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_page_increment (priv->adjustment));
1321 priv->click_child = GTK_ARROW_DOWN;
1326 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1332 gtk_spin_button_button_release (GtkWidget *widget,
1333 GdkEventButton *event)
1335 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1336 GtkSpinButtonPrivate *priv = spin->priv;
1339 arrow_size = spin_button_get_arrow_size (spin);
1341 if (event->button == priv->button)
1343 int click_child = priv->click_child;
1345 gtk_spin_button_stop_spinning (spin);
1347 if (event->button == 3)
1349 GtkRequisition requisition;
1350 GtkStyleContext *context;
1351 GtkStateFlags state;
1354 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1356 context = gtk_widget_get_style_context (widget);
1357 state = gtk_widget_get_state_flags (widget);
1358 gtk_style_context_get_padding (context, state, &padding);
1360 if (event->y >= 0 && event->x >= 0 &&
1361 event->y <= requisition.height &&
1362 event->x <= arrow_size + padding.left + padding.right)
1364 if (click_child == GTK_ARROW_UP &&
1365 event->y <= requisition.height / 2)
1369 diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1371 gtk_spin_button_real_spin (spin, diff);
1373 else if (click_child == GTK_ARROW_DOWN &&
1374 event->y > requisition.height / 2)
1378 diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1380 gtk_spin_button_real_spin (spin, -diff);
1384 gtk_widget_queue_draw (GTK_WIDGET (spin));
1389 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1393 gtk_spin_button_motion_notify (GtkWidget *widget,
1394 GdkEventMotion *event)
1396 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1397 GtkSpinButtonPrivate *priv = spin->priv;
1402 if (event->window == priv->panel)
1404 GtkRequisition requisition;
1407 gdk_event_request_motions (event);
1409 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1411 if (y <= requisition.height / 2 &&
1412 priv->in_child == GTK_ARROW_DOWN)
1414 priv->in_child = GTK_ARROW_UP;
1415 gtk_widget_queue_draw (GTK_WIDGET (spin));
1417 else if (y > requisition.height / 2 &&
1418 priv->in_child == GTK_ARROW_UP)
1420 priv->in_child = GTK_ARROW_DOWN;
1421 gtk_widget_queue_draw (GTK_WIDGET (spin));
1427 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1431 gtk_spin_button_timer (GtkSpinButton *spin_button)
1433 GtkSpinButtonPrivate *priv = spin_button->priv;
1434 gboolean retval = FALSE;
1438 if (priv->click_child == GTK_ARROW_UP)
1439 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1441 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1443 if (priv->need_timer)
1445 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1448 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1450 priv->need_timer = FALSE;
1451 priv->timer = gdk_threads_add_timeout (timeout,
1452 (GSourceFunc) gtk_spin_button_timer,
1453 (gpointer) spin_button);
1457 if (priv->climb_rate > 0.0 && priv->timer_step
1458 < gtk_adjustment_get_page_increment (priv->adjustment))
1460 if (priv->timer_calls < MAX_TIMER_CALLS)
1461 priv->timer_calls++;
1464 priv->timer_calls = 0;
1465 priv->timer_step += priv->climb_rate;
1476 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1477 GtkSpinButton *spin_button)
1479 gboolean return_val;
1481 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1484 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1485 if (return_val == FALSE)
1486 gtk_spin_button_default_output (spin_button);
1488 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1490 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1492 g_object_notify (G_OBJECT (spin_button), "value");
1496 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1497 GtkScrollType scroll)
1499 GtkSpinButtonPrivate *priv = spin->priv;
1502 /* When the key binding is activated, there may be an outstanding
1503 * value, so we first have to commit what is currently written in
1504 * the spin buttons text entry. See #106574
1506 gtk_spin_button_update (spin);
1508 old_value = gtk_adjustment_get_value (priv->adjustment);
1510 /* We don't test whether the entry is editable, since
1511 * this key binding conceptually corresponds to changing
1512 * the value with the buttons using the mouse, which
1513 * we allow for non-editable spin buttons.
1517 case GTK_SCROLL_STEP_BACKWARD:
1518 case GTK_SCROLL_STEP_DOWN:
1519 case GTK_SCROLL_STEP_LEFT:
1520 gtk_spin_button_real_spin (spin, -priv->timer_step);
1522 if (priv->climb_rate > 0.0 && priv->timer_step
1523 < gtk_adjustment_get_page_increment (priv->adjustment))
1525 if (priv->timer_calls < MAX_TIMER_CALLS)
1526 priv->timer_calls++;
1529 priv->timer_calls = 0;
1530 priv->timer_step += priv->climb_rate;
1535 case GTK_SCROLL_STEP_FORWARD:
1536 case GTK_SCROLL_STEP_UP:
1537 case GTK_SCROLL_STEP_RIGHT:
1538 gtk_spin_button_real_spin (spin, priv->timer_step);
1540 if (priv->climb_rate > 0.0 && priv->timer_step
1541 < gtk_adjustment_get_page_increment (priv->adjustment))
1543 if (priv->timer_calls < MAX_TIMER_CALLS)
1544 priv->timer_calls++;
1547 priv->timer_calls = 0;
1548 priv->timer_step += priv->climb_rate;
1553 case GTK_SCROLL_PAGE_BACKWARD:
1554 case GTK_SCROLL_PAGE_DOWN:
1555 case GTK_SCROLL_PAGE_LEFT:
1556 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_page_increment (priv->adjustment));
1559 case GTK_SCROLL_PAGE_FORWARD:
1560 case GTK_SCROLL_PAGE_UP:
1561 case GTK_SCROLL_PAGE_RIGHT:
1562 gtk_spin_button_real_spin (spin, gtk_adjustment_get_page_increment (priv->adjustment));
1565 case GTK_SCROLL_START:
1567 gdouble diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1569 gtk_spin_button_real_spin (spin, -diff);
1573 case GTK_SCROLL_END:
1575 gdouble diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1577 gtk_spin_button_real_spin (spin, diff);
1582 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1586 gtk_spin_button_update (spin);
1588 if (gtk_adjustment_get_value (priv->adjustment) == old_value)
1589 gtk_widget_error_bell (GTK_WIDGET (spin));
1593 gtk_spin_button_key_release (GtkWidget *widget,
1596 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1597 GtkSpinButtonPrivate *priv = spin->priv;
1599 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1600 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1601 priv->timer_calls = 0;
1607 gtk_spin_button_snap (GtkSpinButton *spin_button,
1610 GtkSpinButtonPrivate *priv = spin_button->priv;
1614 inc = gtk_adjustment_get_step_increment (priv->adjustment);
1618 tmp = (val - gtk_adjustment_get_lower (priv->adjustment)) / inc;
1619 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1620 val = gtk_adjustment_get_lower (priv->adjustment) + floor (tmp) * inc;
1622 val = gtk_adjustment_get_lower (priv->adjustment) + ceil (tmp) * inc;
1624 gtk_spin_button_set_value (spin_button, val);
1628 gtk_spin_button_activate (GtkEntry *entry)
1630 if (gtk_editable_get_editable (GTK_EDITABLE (entry)))
1631 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1633 /* Chain up so that entry->activates_default is honored */
1634 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1638 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1644 GtkStyleContext *context;
1645 GtkStateFlags state;
1651 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1653 widget = GTK_WIDGET (entry);
1654 state = gtk_widget_get_state_flags (widget);
1655 context = gtk_widget_get_style_context (widget);
1656 gtk_style_context_get_padding (context, state, &padding);
1658 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1659 panel_width = arrow_size + padding.left + padding.right;
1662 *width -= panel_width;
1664 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1669 gtk_spin_button_insert_text (GtkEditable *editable,
1670 const gchar *new_text,
1671 gint new_text_length,
1674 GtkEntry *entry = GTK_ENTRY (editable);
1675 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1676 GtkSpinButtonPrivate *priv = spin->priv;
1677 GtkEditableInterface *parent_editable_iface;
1679 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1691 const gchar *entry_text;
1693 entry_length = gtk_entry_get_text_length (entry);
1694 entry_text = gtk_entry_get_text (entry);
1698 if (*(lc->negative_sign))
1699 neg_sign = *(lc->negative_sign);
1703 if (*(lc->positive_sign))
1704 pos_sign = *(lc->positive_sign);
1709 /* Workaround for bug caused by some Windows application messing
1710 * up the positive sign of the current locale, more specifically
1711 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1712 * See bug #330743 and for instance
1713 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1715 * I don't know if the positive sign always gets bogusly set to
1716 * a digit when the above Registry value is corrupted as
1717 * described. (In my test case, it got set to "8", and in the
1718 * bug report above it presumably was set ot "0".) Probably it
1719 * might get set to almost anything? So how to distinguish a
1720 * bogus value from some correct one for some locale? That is
1721 * probably hard, but at least we should filter out the
1724 if (pos_sign >= '0' && pos_sign <= '9')
1728 for (sign=0, i=0; i<entry_length; i++)
1729 if ((entry_text[i] == neg_sign) ||
1730 (entry_text[i] == pos_sign))
1736 if (sign && !(*position))
1739 for (dotpos=-1, i=0; i<entry_length; i++)
1740 if (entry_text[i] == *(lc->decimal_point))
1746 if (dotpos > -1 && *position > dotpos &&
1747 (gint)priv->digits - entry_length
1748 + dotpos - new_text_length + 1 < 0)
1751 for (i = 0; i < new_text_length; i++)
1753 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1755 if (sign || (*position) || i)
1759 else if (new_text[i] == *(lc->decimal_point))
1761 if (!priv->digits || dotpos > -1 ||
1762 (new_text_length - 1 - i + entry_length
1763 - *position > (gint)priv->digits))
1765 dotpos = *position + i;
1767 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1772 parent_editable_iface->insert_text (editable, new_text,
1773 new_text_length, position);
1777 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1780 GtkSpinButtonPrivate *priv = spin_button->priv;
1781 GtkAdjustment *adjustment;
1782 gdouble new_value = 0.0;
1783 gboolean wrapped = FALSE;
1785 adjustment = priv->adjustment;
1787 new_value = gtk_adjustment_get_value (adjustment) + increment;
1793 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_upper (adjustment)) < EPSILON)
1795 new_value = gtk_adjustment_get_lower (adjustment);
1798 else if (new_value > gtk_adjustment_get_upper (adjustment))
1799 new_value = gtk_adjustment_get_upper (adjustment);
1802 new_value = MIN (new_value, gtk_adjustment_get_upper (adjustment));
1804 else if (increment < 0)
1808 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment)) < EPSILON)
1810 new_value = gtk_adjustment_get_upper (adjustment);
1813 else if (new_value < gtk_adjustment_get_lower (adjustment))
1814 new_value = gtk_adjustment_get_lower (adjustment);
1817 new_value = MAX (new_value, gtk_adjustment_get_lower (adjustment));
1820 if (fabs (new_value - gtk_adjustment_get_value (adjustment)) > EPSILON)
1821 gtk_adjustment_set_value (adjustment, new_value);
1824 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1826 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1830 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1835 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1837 return GTK_INPUT_ERROR;
1843 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1845 GtkSpinButtonPrivate *priv = spin_button->priv;
1847 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, gtk_adjustment_get_value (priv->adjustment));
1849 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1850 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1856 /***********************************************************
1857 ***********************************************************
1858 *** Public interface ***
1859 ***********************************************************
1860 ***********************************************************/
1864 * gtk_spin_button_configure:
1865 * @spin_button: a #GtkSpinButton
1866 * @adjustment: (allow-none): a #GtkAdjustment
1867 * @climb_rate: the new climb rate
1868 * @digits: the number of decimal places to display in the spin button
1870 * Changes the properties of an existing spin button. The adjustment,
1871 * climb rate, and number of decimal places are all changed accordingly,
1872 * after this function call.
1875 gtk_spin_button_configure (GtkSpinButton *spin_button,
1876 GtkAdjustment *adjustment,
1880 GtkSpinButtonPrivate *priv;
1882 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1884 priv = spin_button->priv;
1887 gtk_spin_button_set_adjustment (spin_button, adjustment);
1889 adjustment = priv->adjustment;
1891 g_object_freeze_notify (G_OBJECT (spin_button));
1892 if (priv->digits != digits)
1894 priv->digits = digits;
1895 g_object_notify (G_OBJECT (spin_button), "digits");
1898 if (priv->climb_rate != climb_rate)
1900 priv->climb_rate = climb_rate;
1901 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1903 g_object_thaw_notify (G_OBJECT (spin_button));
1905 gtk_adjustment_value_changed (adjustment);
1909 * gtk_spin_button_new:
1910 * @adjustment: (allow-none): the #GtkAdjustment object that this spin
1911 * button should use, or %NULL
1912 * @climb_rate: specifies how much the spin button changes when an arrow
1914 * @digits: the number of decimal places to display
1916 * Creates a new #GtkSpinButton.
1918 * Returns: The new spin button as a #GtkWidget
1921 gtk_spin_button_new (GtkAdjustment *adjustment,
1925 GtkSpinButton *spin;
1928 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1930 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1932 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1934 return GTK_WIDGET (spin);
1938 * gtk_spin_button_new_with_range:
1939 * @min: Minimum allowable value
1940 * @max: Maximum allowable value
1941 * @step: Increment added or subtracted by spinning the widget
1943 * This is a convenience constructor that allows creation of a numeric
1944 * #GtkSpinButton without manually creating an adjustment. The value is
1945 * initially set to the minimum value and a page increment of 10 * @step
1946 * is the default. The precision of the spin button is equivalent to the
1947 * precision of @step.
1949 * Note that the way in which the precision is derived works best if @step
1950 * is a power of ten. If the resulting precision is not suitable for your
1951 * needs, use gtk_spin_button_set_digits() to correct it.
1953 * Return value: The new spin button as a #GtkWidget
1956 gtk_spin_button_new_with_range (gdouble min,
1960 GtkAdjustment *adjustment;
1961 GtkSpinButton *spin;
1964 g_return_val_if_fail (min <= max, NULL);
1965 g_return_val_if_fail (step != 0.0, NULL);
1967 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1969 adjustment = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1971 if (fabs (step) >= 1.0 || step == 0.0)
1974 digits = abs ((gint) floor (log10 (fabs (step))));
1975 if (digits > MAX_DIGITS)
1976 digits = MAX_DIGITS;
1979 gtk_spin_button_configure (spin, adjustment, step, digits);
1981 gtk_spin_button_set_numeric (spin, TRUE);
1983 return GTK_WIDGET (spin);
1986 /* Callback used when the spin button's adjustment changes.
1987 * We need to redraw the arrows when the adjustment's range
1988 * changes, and reevaluate our size request.
1991 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1993 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1994 GtkSpinButtonPrivate *priv = spin_button->priv;
1996 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1997 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2001 * gtk_spin_button_set_adjustment:
2002 * @spin_button: a #GtkSpinButton
2003 * @adjustment: a #GtkAdjustment to replace the existing adjustment
2005 * Replaces the #GtkAdjustment associated with @spin_button.
2008 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
2009 GtkAdjustment *adjustment)
2011 GtkSpinButtonPrivate *priv;
2013 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2015 priv = spin_button->priv;
2017 if (priv->adjustment != adjustment)
2019 if (priv->adjustment)
2021 g_signal_handlers_disconnect_by_func (priv->adjustment,
2022 gtk_spin_button_value_changed,
2024 g_signal_handlers_disconnect_by_func (priv->adjustment,
2025 adjustment_changed_cb,
2027 g_object_unref (priv->adjustment);
2029 priv->adjustment = adjustment;
2032 g_object_ref_sink (adjustment);
2033 g_signal_connect (adjustment, "value-changed",
2034 G_CALLBACK (gtk_spin_button_value_changed),
2036 g_signal_connect (adjustment, "changed",
2037 G_CALLBACK (adjustment_changed_cb),
2039 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
2042 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2045 g_object_notify (G_OBJECT (spin_button), "adjustment");
2049 * gtk_spin_button_get_adjustment:
2050 * @spin_button: a #GtkSpinButton
2052 * Get the adjustment associated with a #GtkSpinButton
2054 * Return value: (transfer none): the #GtkAdjustment of @spin_button
2057 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
2059 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
2061 return spin_button->priv->adjustment;
2065 * gtk_spin_button_set_digits:
2066 * @spin_button: a #GtkSpinButton
2067 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
2069 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
2073 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
2076 GtkSpinButtonPrivate *priv;
2078 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2080 priv = spin_button->priv;
2082 if (priv->digits != digits)
2084 priv->digits = digits;
2085 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2086 g_object_notify (G_OBJECT (spin_button), "digits");
2088 /* since lower/upper may have changed */
2089 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2094 * gtk_spin_button_get_digits:
2095 * @spin_button: a #GtkSpinButton
2097 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
2099 * Returns: the current precision
2102 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
2104 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2106 return spin_button->priv->digits;
2110 * gtk_spin_button_set_increments:
2111 * @spin_button: a #GtkSpinButton
2112 * @step: increment applied for a button 1 press.
2113 * @page: increment applied for a button 2 press.
2115 * Sets the step and page increments for spin_button. This affects how
2116 * quickly the value changes when the spin button's arrows are activated.
2119 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
2123 GtkSpinButtonPrivate *priv;
2125 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2127 priv = spin_button->priv;
2129 gtk_adjustment_configure (priv->adjustment,
2130 gtk_adjustment_get_value (priv->adjustment),
2131 gtk_adjustment_get_lower (priv->adjustment),
2132 gtk_adjustment_get_upper (priv->adjustment),
2135 gtk_adjustment_get_page_size (priv->adjustment));
2139 * gtk_spin_button_get_increments:
2140 * @spin_button: a #GtkSpinButton
2141 * @step: (allow-none): location to store step increment, or %NULL
2142 * @page: (allow-none): location to store page increment, or %NULL
2144 * Gets the current step and page the increments used by @spin_button. See
2145 * gtk_spin_button_set_increments().
2148 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
2152 GtkSpinButtonPrivate *priv;
2154 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2156 priv = spin_button->priv;
2159 *step = gtk_adjustment_get_step_increment (priv->adjustment);
2161 *page = gtk_adjustment_get_page_increment (priv->adjustment);
2165 * gtk_spin_button_set_range:
2166 * @spin_button: a #GtkSpinButton
2167 * @min: minimum allowable value
2168 * @max: maximum allowable value
2170 * Sets the minimum and maximum allowable values for @spin_button.
2173 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2177 GtkAdjustment *adjustment;
2179 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2181 adjustment = spin_button->priv->adjustment;
2183 gtk_adjustment_configure (adjustment,
2184 CLAMP (gtk_adjustment_get_value (adjustment), min, max),
2187 gtk_adjustment_get_step_increment (adjustment),
2188 gtk_adjustment_get_page_increment (adjustment),
2189 gtk_adjustment_get_page_size (adjustment));
2193 * gtk_spin_button_get_range:
2194 * @spin_button: a #GtkSpinButton
2195 * @min: (allow-none): location to store minimum allowed value, or %NULL
2196 * @max: (allow-none): location to store maximum allowed value, or %NULL
2198 * Gets the range allowed for @spin_button.
2199 * See gtk_spin_button_set_range().
2202 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2206 GtkSpinButtonPrivate *priv;
2208 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2210 priv = spin_button->priv;
2213 *min = gtk_adjustment_get_lower (priv->adjustment);
2215 *max = gtk_adjustment_get_upper (priv->adjustment);
2219 * gtk_spin_button_get_value:
2220 * @spin_button: a #GtkSpinButton
2222 * Get the value in the @spin_button.
2224 * Return value: the value of @spin_button
2227 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2229 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2231 return gtk_adjustment_get_value (spin_button->priv->adjustment);
2235 * gtk_spin_button_get_value_as_int:
2236 * @spin_button: a #GtkSpinButton
2238 * Get the value @spin_button represented as an integer.
2240 * Return value: the value of @spin_button
2243 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2245 GtkSpinButtonPrivate *priv;
2248 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2250 priv = spin_button->priv;
2252 val = gtk_adjustment_get_value (priv->adjustment);
2253 if (val - floor (val) < ceil (val) - val)
2260 * gtk_spin_button_set_value:
2261 * @spin_button: a #GtkSpinButton
2262 * @value: the new value
2264 * Sets the value of @spin_button.
2267 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2270 GtkSpinButtonPrivate *priv;
2272 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2274 priv = spin_button->priv;
2276 if (fabs (value - gtk_adjustment_get_value (priv->adjustment)) > EPSILON)
2277 gtk_adjustment_set_value (priv->adjustment, value);
2280 gint return_val = FALSE;
2281 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2282 if (return_val == FALSE)
2283 gtk_spin_button_default_output (spin_button);
2288 * gtk_spin_button_set_update_policy:
2289 * @spin_button: a #GtkSpinButton
2290 * @policy: a #GtkSpinButtonUpdatePolicy value
2292 * Sets the update behavior of a spin button.
2293 * This determines wether the spin button is always updated
2294 * or only when a valid value is set.
2297 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2298 GtkSpinButtonUpdatePolicy policy)
2300 GtkSpinButtonPrivate *priv;
2302 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2304 priv = spin_button->priv;
2306 if (priv->update_policy != policy)
2308 priv->update_policy = policy;
2309 g_object_notify (G_OBJECT (spin_button), "update-policy");
2314 * gtk_spin_button_get_update_policy:
2315 * @spin_button: a #GtkSpinButton
2317 * Gets the update behavior of a spin button.
2318 * See gtk_spin_button_set_update_policy().
2320 * Return value: the current update policy
2322 GtkSpinButtonUpdatePolicy
2323 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2325 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2327 return spin_button->priv->update_policy;
2331 * gtk_spin_button_set_numeric:
2332 * @spin_button: a #GtkSpinButton
2333 * @numeric: flag indicating if only numeric entry is allowed
2335 * Sets the flag that determines if non-numeric text can be typed
2336 * into the spin button.
2339 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2342 GtkSpinButtonPrivate *priv;
2344 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2346 priv = spin_button->priv;
2348 numeric = numeric != FALSE;
2350 if (priv->numeric != numeric)
2352 priv->numeric = numeric;
2353 g_object_notify (G_OBJECT (spin_button), "numeric");
2358 * gtk_spin_button_get_numeric:
2359 * @spin_button: a #GtkSpinButton
2361 * Returns whether non-numeric text can be typed into the spin button.
2362 * See gtk_spin_button_set_numeric().
2364 * Return value: %TRUE if only numeric text can be entered
2367 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2369 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2371 return spin_button->priv->numeric;
2375 * gtk_spin_button_set_wrap:
2376 * @spin_button: a #GtkSpinButton
2377 * @wrap: a flag indicating if wrapping behavior is performed
2379 * Sets the flag that determines if a spin button value wraps
2380 * around to the opposite limit when the upper or lower limit
2381 * of the range is exceeded.
2384 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2387 GtkSpinButtonPrivate *priv;
2389 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2391 priv = spin_button->priv;
2393 wrap = wrap != FALSE;
2395 if (priv->wrap != wrap)
2399 g_object_notify (G_OBJECT (spin_button), "wrap");
2404 * gtk_spin_button_get_wrap:
2405 * @spin_button: a #GtkSpinButton
2407 * Returns whether the spin button's value wraps around to the
2408 * opposite limit when the upper or lower limit of the range is
2409 * exceeded. See gtk_spin_button_set_wrap().
2411 * Return value: %TRUE if the spin button wraps around
2414 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2416 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2418 return spin_button->priv->wrap;
2422 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2424 const PangoFontDescription *font_desc;
2425 GtkStyleContext *context;
2429 /* FIXME: use getter */
2430 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
2431 font_desc = gtk_style_context_get_font (context, 0);
2433 size = pango_font_description_get_size (font_desc);
2434 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2436 return arrow_size - arrow_size % 2; /* force even */
2440 * gtk_spin_button_set_snap_to_ticks:
2441 * @spin_button: a #GtkSpinButton
2442 * @snap_to_ticks: a flag indicating if invalid values should be corrected
2444 * Sets the policy as to whether values are corrected to the
2445 * nearest step increment when a spin button is activated after
2446 * providing an invalid value.
2449 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2450 gboolean snap_to_ticks)
2452 GtkSpinButtonPrivate *priv;
2455 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2457 priv = spin_button->priv;
2459 new_val = (snap_to_ticks != 0);
2461 if (new_val != priv->snap_to_ticks)
2463 priv->snap_to_ticks = new_val;
2464 if (new_val && gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
2465 gtk_spin_button_update (spin_button);
2467 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2472 * gtk_spin_button_get_snap_to_ticks:
2473 * @spin_button: a #GtkSpinButton
2475 * Returns whether the values are corrected to the nearest step.
2476 * See gtk_spin_button_set_snap_to_ticks().
2478 * Return value: %TRUE if values are snapped to the nearest step
2481 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2483 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2485 return spin_button->priv->snap_to_ticks;
2489 * gtk_spin_button_spin:
2490 * @spin_button: a #GtkSpinButton
2491 * @direction: a #GtkSpinType indicating the direction to spin
2492 * @increment: step increment to apply in the specified direction
2494 * Increment or decrement a spin button's value in a specified
2495 * direction by a specified amount.
2498 gtk_spin_button_spin (GtkSpinButton *spin_button,
2499 GtkSpinType direction,
2502 GtkSpinButtonPrivate *priv;
2503 GtkAdjustment *adjustment;
2506 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2508 priv = spin_button->priv;
2510 adjustment = priv->adjustment;
2512 /* for compatibility with the 1.0.x version of this function */
2513 if (increment != 0 && increment != gtk_adjustment_get_step_increment (adjustment) &&
2514 (direction == GTK_SPIN_STEP_FORWARD ||
2515 direction == GTK_SPIN_STEP_BACKWARD))
2517 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2518 increment = -increment;
2519 direction = GTK_SPIN_USER_DEFINED;
2524 case GTK_SPIN_STEP_FORWARD:
2526 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_step_increment (adjustment));
2529 case GTK_SPIN_STEP_BACKWARD:
2531 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_step_increment (adjustment));
2534 case GTK_SPIN_PAGE_FORWARD:
2536 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_page_increment (adjustment));
2539 case GTK_SPIN_PAGE_BACKWARD:
2541 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_page_increment (adjustment));
2546 diff = gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment);
2548 gtk_spin_button_real_spin (spin_button, -diff);
2553 diff = gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_value (adjustment);
2555 gtk_spin_button_real_spin (spin_button, diff);
2558 case GTK_SPIN_USER_DEFINED:
2561 gtk_spin_button_real_spin (spin_button, increment);
2570 * gtk_spin_button_update:
2571 * @spin_button: a #GtkSpinButton
2573 * Manually force an update of the spin button.
2576 gtk_spin_button_update (GtkSpinButton *spin_button)
2578 GtkSpinButtonPrivate *priv;
2583 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2585 priv = spin_button->priv;
2588 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2589 if (return_val == FALSE)
2591 return_val = gtk_spin_button_default_input (spin_button, &val);
2592 error = (return_val == GTK_INPUT_ERROR);
2594 else if (return_val == GTK_INPUT_ERROR)
2597 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2599 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2601 if (val < gtk_adjustment_get_lower (priv->adjustment))
2602 val = gtk_adjustment_get_lower (priv->adjustment);
2603 else if (val > gtk_adjustment_get_upper (priv->adjustment))
2604 val = gtk_adjustment_get_upper (priv->adjustment);
2606 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2608 val < gtk_adjustment_get_lower (priv->adjustment) ||
2609 val > gtk_adjustment_get_upper (priv->adjustment)))
2611 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2615 if (priv->snap_to_ticks)
2616 gtk_spin_button_snap (spin_button, val);
2618 gtk_spin_button_set_value (spin_button, val);
2622 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2624 return spin_button->priv->panel;