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 #include "a11y/gtkspinbuttonaccessible.h"
50 #define MIN_SPIN_BUTTON_WIDTH 30
51 #define MAX_TIMER_CALLS 5
54 #define MIN_ARROW_WIDTH 6
58 * SECTION:gtkspinbutton
59 * @Title: GtkSpinButton
60 * @Short_description: Retrieve an integer or floating-point number from
62 * @See_also: #GtkEntry
64 * A #GtkSpinButton is an ideal way to allow the user to set the value of
65 * some attribute. Rather than having to directly type a number into a
66 * #GtkEntry, GtkSpinButton allows the user to click on one of two arrows
67 * to increment or decrement the displayed value. A value can still be
68 * typed in, with the bonus that it can be checked to ensure it is in a
71 * The main properties of a GtkSpinButton are through an adjustment.
72 * See the #GtkAdjustment section for more details about an adjustment's
76 * <title>Using a GtkSpinButton to get an integer</title>
78 * /* Provides a function to retrieve an integer value from a
79 * * GtkSpinButton and creates a spin button to model percentage
84 * grab_int_value (GtkSpinButton *button,
87 * return gtk_spin_button_get_value_as_int (button);
91 * create_integer_spin_button (void)
94 * GtkWidget *window, *button;
95 * GtkAdjustment *adjustment;
97 * adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0);
99 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
100 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
102 * /* creates the spinbutton, with no decimal places */
103 * button = gtk_spin_button_new (adjustment, 1.0, 0);
104 * gtk_container_add (GTK_CONTAINER (window), button);
106 * gtk_widget_show_all (window);
112 * <title>Using a GtkSpinButton to get a floating point value</title>
114 * /* Provides a function to retrieve a floating point value from a
115 * * GtkSpinButton, and creates a high precision spin button.
119 * grab_float_value (GtkSpinButton *button,
120 * gpointer user_data)
122 * return gtk_spin_button_get_value (button);
126 * create_floating_spin_button (void)
128 * GtkWidget *window, *button;
129 * GtkAdjustment *adjustment;
131 * adjustment = gtk_adjustment_new (2.500, 0.0, 5.0, 0.001, 0.1, 0.0);
133 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
134 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
136 * /* creates the spinbutton, with three decimal places */
137 * button = gtk_spin_button_new (adjustment, 0.001, 3);
138 * gtk_container_add (GTK_CONTAINER (window), button);
140 * gtk_widget_show_all (window);
146 struct _GtkSpinButtonPrivate
148 GtkAdjustment *adjustment;
154 GtkSpinButtonUpdatePolicy update_policy;
160 guint click_child : 2; /* valid: GTK_ARROW_UP=0, GTK_ARROW_DOWN=1 or 2=NONE/BOTH */
163 guint need_timer : 1;
165 guint snap_to_ticks : 1;
166 guint timer_calls : 3;
193 static void gtk_spin_button_editable_init (GtkEditableInterface *iface);
194 static void gtk_spin_button_finalize (GObject *object);
195 static void gtk_spin_button_set_property (GObject *object,
199 static void gtk_spin_button_get_property (GObject *object,
203 static void gtk_spin_button_destroy (GtkWidget *widget);
204 static void gtk_spin_button_map (GtkWidget *widget);
205 static void gtk_spin_button_unmap (GtkWidget *widget);
206 static void gtk_spin_button_realize (GtkWidget *widget);
207 static void gtk_spin_button_unrealize (GtkWidget *widget);
208 static void gtk_spin_button_get_preferred_width (GtkWidget *widget,
212 static void gtk_spin_button_size_allocate (GtkWidget *widget,
213 GtkAllocation *allocation);
214 static gint gtk_spin_button_draw (GtkWidget *widget,
216 static gint gtk_spin_button_button_press (GtkWidget *widget,
217 GdkEventButton *event);
218 static gint gtk_spin_button_button_release (GtkWidget *widget,
219 GdkEventButton *event);
220 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
221 GdkEventMotion *event);
222 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
223 GdkEventCrossing *event);
224 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
225 GdkEventCrossing *event);
226 static gint gtk_spin_button_focus_out (GtkWidget *widget,
227 GdkEventFocus *event);
228 static void gtk_spin_button_grab_notify (GtkWidget *widget,
229 gboolean was_grabbed);
230 static void gtk_spin_button_state_flags_changed (GtkWidget *widget,
231 GtkStateFlags previous_state);
232 static void gtk_spin_button_style_updated (GtkWidget *widget);
233 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
234 GtkStyleContext *context,
236 GtkArrowType arrow_type);
237 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
238 static gboolean gtk_spin_button_stop_spinning (GtkSpinButton *spin);
239 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
240 GtkSpinButton *spin_button);
241 static gint gtk_spin_button_key_release (GtkWidget *widget,
243 static gint gtk_spin_button_scroll (GtkWidget *widget,
244 GdkEventScroll *event);
245 static void gtk_spin_button_activate (GtkEntry *entry);
246 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
251 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
253 static void gtk_spin_button_insert_text (GtkEditable *editable,
254 const gchar *new_text,
255 gint new_text_length,
257 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
259 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
260 GtkScrollType scroll);
262 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
264 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
266 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
268 static guint spinbutton_signals[LAST_SIGNAL] = {0};
272 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
273 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
274 gtk_spin_button_editable_init))
276 #define add_spin_binding(binding_set, keyval, mask, scroll) \
277 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
279 GTK_TYPE_SCROLL_TYPE, scroll)
282 gtk_spin_button_class_init (GtkSpinButtonClass *class)
284 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
285 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
286 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
287 GtkBindingSet *binding_set;
289 gobject_class->finalize = gtk_spin_button_finalize;
290 gobject_class->set_property = gtk_spin_button_set_property;
291 gobject_class->get_property = gtk_spin_button_get_property;
293 widget_class->destroy = gtk_spin_button_destroy;
294 widget_class->map = gtk_spin_button_map;
295 widget_class->unmap = gtk_spin_button_unmap;
296 widget_class->realize = gtk_spin_button_realize;
297 widget_class->unrealize = gtk_spin_button_unrealize;
298 widget_class->get_preferred_width = gtk_spin_button_get_preferred_width;
299 widget_class->size_allocate = gtk_spin_button_size_allocate;
300 widget_class->draw = gtk_spin_button_draw;
301 widget_class->scroll_event = gtk_spin_button_scroll;
302 widget_class->button_press_event = gtk_spin_button_button_press;
303 widget_class->button_release_event = gtk_spin_button_button_release;
304 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
305 widget_class->key_release_event = gtk_spin_button_key_release;
306 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
307 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
308 widget_class->focus_out_event = gtk_spin_button_focus_out;
309 widget_class->grab_notify = gtk_spin_button_grab_notify;
310 widget_class->state_flags_changed = gtk_spin_button_state_flags_changed;
311 widget_class->style_updated = gtk_spin_button_style_updated;
313 entry_class->activate = gtk_spin_button_activate;
314 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
317 class->output = NULL;
318 class->change_value = gtk_spin_button_real_change_value;
320 g_object_class_install_property (gobject_class,
322 g_param_spec_object ("adjustment",
324 P_("The adjustment that holds the value of the spin button"),
326 GTK_PARAM_READWRITE));
328 g_object_class_install_property (gobject_class,
330 g_param_spec_double ("climb-rate",
332 P_("The acceleration rate when you hold down a button"),
336 GTK_PARAM_READWRITE));
338 g_object_class_install_property (gobject_class,
340 g_param_spec_uint ("digits",
342 P_("The number of decimal places to display"),
346 GTK_PARAM_READWRITE));
348 g_object_class_install_property (gobject_class,
350 g_param_spec_boolean ("snap-to-ticks",
352 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
354 GTK_PARAM_READWRITE));
356 g_object_class_install_property (gobject_class,
358 g_param_spec_boolean ("numeric",
360 P_("Whether non-numeric characters should be ignored"),
362 GTK_PARAM_READWRITE));
364 g_object_class_install_property (gobject_class,
366 g_param_spec_boolean ("wrap",
368 P_("Whether a spin button should wrap upon reaching its limits"),
370 GTK_PARAM_READWRITE));
372 g_object_class_install_property (gobject_class,
374 g_param_spec_enum ("update-policy",
376 P_("Whether the spin button should update always, or only when the value is legal"),
377 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
379 GTK_PARAM_READWRITE));
381 g_object_class_install_property (gobject_class,
383 g_param_spec_double ("value",
385 P_("Reads the current value, or sets a new value"),
389 GTK_PARAM_READWRITE));
391 gtk_widget_class_install_style_property_parser (widget_class,
392 g_param_spec_enum ("shadow-type",
394 P_("Style of bevel around the spin button"),
395 GTK_TYPE_SHADOW_TYPE,
398 gtk_rc_property_parse_enum);
401 * GtkSpinButton::input:
402 * @spin_button: the object on which the signal was emitted
403 * @new_value: (out) (type double): return location for the new value
405 * The ::input signal can be used to influence the conversion of
406 * the users input into a double value. The signal handler is
407 * expected to use gtk_entry_get_text() to retrieve the text of
408 * the entry and set @new_value to the new value.
410 * The default conversion uses g_strtod().
412 * Returns: %TRUE for a successful conversion, %FALSE if the input
413 * was not handled, and %GTK_INPUT_ERROR if the conversion failed.
415 spinbutton_signals[INPUT] =
416 g_signal_new (I_("input"),
417 G_TYPE_FROM_CLASS (gobject_class),
419 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
421 _gtk_marshal_INT__POINTER,
426 * GtkSpinButton::output:
427 * @spin_button: the object which received the signal
429 * The ::output signal can be used to change to formatting
430 * of the value that is displayed in the spin buttons entry.
432 * /* show leading zeros */
434 * on_output (GtkSpinButton *spin,
437 * GtkAdjustment *adjustment;
441 * adjustment = gtk_spin_button_get_adjustment (spin);
442 * value = (int)gtk_adjustment_get_value (adjustment);
443 * text = g_strdup_printf ("%02d", value);
444 * gtk_entry_set_text (GTK_ENTRY (spin), text);
451 * Returns: %TRUE if the value has been displayed
453 spinbutton_signals[OUTPUT] =
454 g_signal_new (I_("output"),
455 G_TYPE_FROM_CLASS (gobject_class),
457 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
458 _gtk_boolean_handled_accumulator, NULL,
459 _gtk_marshal_BOOLEAN__VOID,
462 spinbutton_signals[VALUE_CHANGED] =
463 g_signal_new (I_("value-changed"),
464 G_TYPE_FROM_CLASS (gobject_class),
466 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
468 _gtk_marshal_VOID__VOID,
472 * GtkSpinButton::wrapped:
473 * @spinbutton: the object which received the signal
475 * The wrapped signal is emitted right after the spinbutton wraps
476 * from its maximum to minimum value or vice-versa.
480 spinbutton_signals[WRAPPED] =
481 g_signal_new (I_("wrapped"),
482 G_TYPE_FROM_CLASS (gobject_class),
484 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
486 _gtk_marshal_VOID__VOID,
490 spinbutton_signals[CHANGE_VALUE] =
491 g_signal_new (I_("change-value"),
492 G_TYPE_FROM_CLASS (gobject_class),
493 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
494 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
496 _gtk_marshal_VOID__ENUM,
498 GTK_TYPE_SCROLL_TYPE);
500 binding_set = gtk_binding_set_by_class (class);
502 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
503 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
504 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
505 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
506 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
507 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
508 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
509 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
511 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
513 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SPIN_BUTTON_ACCESSIBLE);
517 gtk_spin_button_editable_init (GtkEditableInterface *iface)
519 iface->insert_text = gtk_spin_button_insert_text;
523 gtk_spin_button_set_property (GObject *object,
528 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
529 GtkSpinButtonPrivate *priv = spin_button->priv;
533 GtkAdjustment *adjustment;
535 case PROP_ADJUSTMENT:
536 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
538 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
539 gtk_spin_button_set_adjustment (spin_button, adjustment);
541 case PROP_CLIMB_RATE:
542 gtk_spin_button_configure (spin_button,
544 g_value_get_double (value),
548 gtk_spin_button_configure (spin_button,
551 g_value_get_uint (value));
553 case PROP_SNAP_TO_TICKS:
554 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
557 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
560 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
562 case PROP_UPDATE_POLICY:
563 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
566 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
569 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
575 gtk_spin_button_get_property (GObject *object,
580 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
581 GtkSpinButtonPrivate *priv = spin_button->priv;
585 case PROP_ADJUSTMENT:
586 g_value_set_object (value, priv->adjustment);
588 case PROP_CLIMB_RATE:
589 g_value_set_double (value, priv->climb_rate);
592 g_value_set_uint (value, priv->digits);
594 case PROP_SNAP_TO_TICKS:
595 g_value_set_boolean (value, priv->snap_to_ticks);
598 g_value_set_boolean (value, priv->numeric);
601 g_value_set_boolean (value, priv->wrap);
603 case PROP_UPDATE_POLICY:
604 g_value_set_enum (value, priv->update_policy);
607 g_value_set_double (value, gtk_adjustment_get_value (priv->adjustment));
610 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
616 gtk_spin_button_init (GtkSpinButton *spin_button)
618 GtkSpinButtonPrivate *priv;
619 GtkStyleContext *context;
621 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
622 GTK_TYPE_SPIN_BUTTON,
623 GtkSpinButtonPrivate);
624 priv = spin_button->priv;
626 priv->adjustment = NULL;
629 priv->climb_rate = 0.0;
630 priv->timer_step = 0.0;
631 priv->update_policy = GTK_UPDATE_ALWAYS;
632 priv->in_child = NO_ARROW;
633 priv->click_child = NO_ARROW;
635 priv->need_timer = FALSE;
636 priv->timer_calls = 0;
638 priv->numeric = FALSE;
640 priv->snap_to_ticks = FALSE;
642 gtk_spin_button_set_adjustment (spin_button,
643 gtk_adjustment_new (0, 0, 0, 0, 0, 0));
645 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
646 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINBUTTON);
650 gtk_spin_button_finalize (GObject *object)
652 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
654 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
658 gtk_spin_button_destroy (GtkWidget *widget)
660 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
662 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->destroy (widget);
666 gtk_spin_button_map (GtkWidget *widget)
668 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
669 GtkSpinButtonPrivate *priv = spin_button->priv;
671 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
673 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
674 gdk_window_show (priv->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->panel);
689 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
694 gtk_spin_button_realize (GtkWidget *widget)
696 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
697 GtkSpinButtonPrivate *priv = spin_button->priv;
698 GtkStyleContext *context;
700 GtkAllocation allocation;
701 GtkRequisition requisition;
702 GdkWindowAttr attributes;
703 gint attributes_mask;
709 arrow_size = spin_button_get_arrow_size (spin_button);
711 gtk_widget_get_preferred_size (widget, &requisition, NULL);
712 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
713 gtk_widget_get_allocation (widget, &allocation);
715 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
716 GDK_KEY_RELEASE_MASK);
717 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
719 attributes.window_type = GDK_WINDOW_CHILD;
720 attributes.wclass = GDK_INPUT_ONLY;
721 attributes.visual = gtk_widget_get_visual (widget);
722 attributes.event_mask = gtk_widget_get_events (widget);
723 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
724 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
725 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
727 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
729 state = gtk_widget_get_state_flags (widget);
730 context = gtk_widget_get_style_context (widget);
731 gtk_style_context_get_padding (context, state, &padding);
733 attributes.x = allocation.x + allocation.width - arrow_size - (padding.left + padding.right);
734 attributes.y = allocation.y + (allocation.height - req_height) / 2;
735 attributes.width = arrow_size + padding.left + padding.right;
736 attributes.height = req_height;
738 priv->panel = gdk_window_new (gtk_widget_get_window (widget),
739 &attributes, attributes_mask);
740 gdk_window_set_user_data (priv->panel, widget);
743 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
744 if (return_val == FALSE)
745 gtk_spin_button_default_output (spin_button);
747 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
751 gtk_spin_button_unrealize (GtkWidget *widget)
753 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
754 GtkSpinButtonPrivate *priv = spin->priv;
756 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
760 gdk_window_set_user_data (priv->panel, NULL);
761 gdk_window_destroy (priv->panel);
767 compute_double_length (double val, int digits)
773 if (fabs (val) > 1.0)
774 a = floor (log10 (fabs (val))) + 1;
786 return a + digits + extra;
790 gtk_spin_button_get_preferred_width (GtkWidget *widget,
794 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
795 GtkSpinButtonPrivate *priv = spin_button->priv;
796 GtkEntry *entry = GTK_ENTRY (widget);
797 GtkStyleContext *style_context;
801 style_context = gtk_widget_get_style_context (widget);
803 arrow_size = spin_button_get_arrow_size (spin_button);
805 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->get_preferred_width (widget, minimum, natural);
807 if (gtk_entry_get_width_chars (entry) < 0)
809 PangoContext *context;
810 const PangoFontDescription *font_desc;
811 PangoFontMetrics *metrics;
817 gboolean interior_focus;
819 gint xborder, yborder;
820 GtkBorder inner_border;
822 gtk_widget_style_get (widget,
823 "interior-focus", &interior_focus,
824 "focus-line-width", &focus_width,
827 font_desc = gtk_style_context_get_font (style_context, 0);
829 context = gtk_widget_get_pango_context (widget);
830 metrics = pango_context_get_metrics (context, font_desc,
831 pango_context_get_language (context));
833 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
834 digit_width = PANGO_SCALE *
835 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
837 pango_font_metrics_unref (metrics);
839 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
840 width = MIN_SPIN_BUTTON_WIDTH;
841 max_string_len = MAX (10, compute_double_length (1e9 * gtk_adjustment_get_step_increment (priv->adjustment),
844 string_len = compute_double_length (gtk_adjustment_get_upper (priv->adjustment),
846 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
847 width = MAX (width, w);
848 string_len = compute_double_length (gtk_adjustment_get_lower (priv->adjustment), priv->digits);
849 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
850 width = MAX (width, w);
852 _gtk_entry_get_borders (entry, &xborder, &yborder);
853 _gtk_entry_effective_inner_border (entry, &inner_border);
855 width += xborder * 2 + inner_border.left + inner_border.right;
861 gtk_style_context_get_padding (style_context,
862 gtk_widget_get_state_flags (widget),
865 *minimum += arrow_size + padding.left + padding.right;
866 *natural += arrow_size + padding.left + padding.right;
870 gtk_spin_button_size_allocate (GtkWidget *widget,
871 GtkAllocation *allocation)
873 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
874 GtkSpinButtonPrivate *priv = spin->priv;
875 GtkAllocation panel_allocation;
876 GtkRequisition requisition;
877 GtkStyleContext *context;
884 arrow_size = spin_button_get_arrow_size (spin);
885 context = gtk_widget_get_style_context (widget);
886 state = gtk_widget_get_state_flags (widget);
888 gtk_style_context_get_padding (context, state, &padding);
889 panel_width = arrow_size + padding.left + padding.right;
891 gtk_widget_get_preferred_size (widget, &requisition, NULL);
892 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
894 gtk_widget_set_allocation (widget, allocation);
896 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
897 panel_allocation.x = allocation->x;
899 panel_allocation.x = allocation->x + allocation->width - panel_width;
901 panel_allocation.width = panel_width;
902 panel_allocation.height = MIN (req_height, allocation->height);
904 panel_allocation.y = allocation->y +
905 (allocation->height - req_height) / 2;
907 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
909 if (gtk_widget_get_realized (widget))
911 gdk_window_move_resize (priv->panel,
914 panel_allocation.width,
915 panel_allocation.height);
918 gtk_widget_queue_draw (GTK_WIDGET (spin));
922 gtk_spin_button_draw (GtkWidget *widget,
925 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
926 GtkSpinButtonPrivate *priv = spin->priv;
927 GtkStyleContext *context;
928 GtkStateFlags state = 0;
931 is_rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
932 context = gtk_widget_get_style_context (widget);
935 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
938 state = gtk_widget_get_state_flags (widget);
940 gtk_style_context_save (context);
941 gtk_style_context_set_state (context, state);
944 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_RIGHT);
946 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_LEFT);
948 gtk_cairo_transform_to_window (cr, widget, priv->panel);
950 gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_UP);
951 gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_DOWN);
953 gtk_style_context_restore (context);
959 spin_button_at_limit (GtkSpinButton *spin_button,
962 GtkSpinButtonPrivate *priv = spin_button->priv;
963 GtkArrowType effective_arrow;
968 if (gtk_adjustment_get_step_increment (priv->adjustment) > 0)
969 effective_arrow = arrow;
971 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
973 if (effective_arrow == GTK_ARROW_UP &&
974 (gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment) <= EPSILON))
977 if (effective_arrow == GTK_ARROW_DOWN &&
978 (gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment) <= EPSILON))
985 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
986 GtkStyleContext *context,
988 GtkArrowType arrow_type)
990 GtkSpinButtonPrivate *priv;
991 GtkJunctionSides junction;
996 gdouble size, width, height, x, y;
998 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
1000 gtk_style_context_save (context);
1001 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
1003 priv = spin_button->priv;
1004 widget = GTK_WIDGET (spin_button);
1005 junction = gtk_style_context_get_junction_sides (context);
1007 panel_height = gdk_window_get_height (priv->panel);
1009 if (spin_button_at_limit (spin_button, arrow_type))
1010 state = GTK_STATE_FLAG_INSENSITIVE;
1013 if (priv->click_child == arrow_type)
1014 state = GTK_STATE_ACTIVE;
1017 if (priv->in_child == arrow_type &&
1018 priv->click_child == NO_ARROW)
1019 state = GTK_STATE_FLAG_PRELIGHT;
1021 state = gtk_widget_get_state_flags (widget);
1025 /* first, draw the background and the frame */
1026 if (arrow_type == GTK_ARROW_UP)
1031 junction |= GTK_JUNCTION_BOTTOM;
1033 else if (arrow_type == GTK_ARROW_DOWN)
1036 y = panel_height / 2.0;
1038 junction |= GTK_JUNCTION_TOP;
1041 gtk_style_context_set_junction_sides (context, junction);
1042 gtk_style_context_set_state (context, state);
1044 height = panel_height / 2.0;
1045 width = gdk_window_get_width (priv->panel);
1046 gtk_render_background (context, cr,
1047 x, y, width, height);
1048 gtk_render_frame (context, cr,
1049 x, y, width, height);
1051 /* make the actual rendered arrow smaller than text size */
1052 size = spin_button_get_arrow_size (spin_button);
1053 size = MIN (size, width);
1056 x = (width - size) / 2.0;
1058 if (arrow_type == GTK_ARROW_UP)
1060 y = (height - size / 2.0) / 2.0;
1063 else if (arrow_type == GTK_ARROW_DOWN)
1065 y = height + ((height - size / 2.0) / 2.0) - size / 2.0;
1069 gtk_render_arrow (context, cr,
1072 gtk_style_context_restore (context);
1076 gtk_spin_button_enter_notify (GtkWidget *widget,
1077 GdkEventCrossing *event)
1079 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1080 GtkSpinButtonPrivate *priv = spin->priv;
1081 GtkRequisition requisition;
1084 if (event->window == priv->panel)
1090 device = gdk_event_get_device ((GdkEvent *) event);
1091 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
1093 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1094 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1096 if (y <= req_height / 2)
1097 priv->in_child = GTK_ARROW_UP;
1099 priv->in_child = GTK_ARROW_DOWN;
1101 gtk_widget_queue_draw (GTK_WIDGET (spin));
1104 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1108 gtk_spin_button_leave_notify (GtkWidget *widget,
1109 GdkEventCrossing *event)
1111 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1112 GtkSpinButtonPrivate *priv = spin->priv;
1114 if (priv->in_child != NO_ARROW)
1116 priv->in_child = NO_ARROW;
1117 gtk_widget_queue_draw (GTK_WIDGET (spin));
1120 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1124 gtk_spin_button_focus_out (GtkWidget *widget,
1125 GdkEventFocus *event)
1127 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1128 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1130 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1134 gtk_spin_button_grab_notify (GtkWidget *widget,
1135 gboolean was_grabbed)
1137 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1141 if (gtk_spin_button_stop_spinning (spin))
1142 gtk_widget_queue_draw (GTK_WIDGET (spin));
1147 gtk_spin_button_state_flags_changed (GtkWidget *widget,
1148 GtkStateFlags previous_state)
1150 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1152 if (!gtk_widget_is_sensitive (widget))
1154 if (gtk_spin_button_stop_spinning (spin))
1155 gtk_widget_queue_draw (GTK_WIDGET (spin));
1160 gtk_spin_button_style_updated (GtkWidget *widget)
1162 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1163 GtkSpinButtonPrivate *priv = spin->priv;
1165 if (gtk_widget_get_realized (widget))
1167 GtkStyleContext *context;
1169 context = gtk_widget_get_style_context (widget);
1170 gtk_style_context_set_background (context, priv->panel);
1173 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_updated (widget);
1178 gtk_spin_button_scroll (GtkWidget *widget,
1179 GdkEventScroll *event)
1181 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1182 GtkSpinButtonPrivate *priv = spin->priv;
1184 if (event->direction == GDK_SCROLL_UP)
1186 if (!gtk_widget_has_focus (widget))
1187 gtk_widget_grab_focus (widget);
1188 gtk_spin_button_real_spin (spin, gtk_adjustment_get_step_increment (priv->adjustment));
1190 else if (event->direction == GDK_SCROLL_DOWN)
1192 if (!gtk_widget_has_focus (widget))
1193 gtk_widget_grab_focus (widget);
1194 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_step_increment (priv->adjustment));
1203 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1205 GtkSpinButtonPrivate *priv = spin->priv;
1206 gboolean did_spin = FALSE;
1210 g_source_remove (priv->timer);
1212 priv->need_timer = FALSE;
1218 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1219 priv->timer_calls = 0;
1221 priv->click_child = NO_ARROW;
1227 start_spinning (GtkSpinButton *spin,
1228 GtkArrowType click_child,
1231 GtkSpinButtonPrivate *priv;
1233 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1237 priv->click_child = click_child;
1241 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1244 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1246 priv->timer_step = step;
1247 priv->need_timer = TRUE;
1248 priv->timer = gdk_threads_add_timeout (timeout,
1249 (GSourceFunc) gtk_spin_button_timer,
1252 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1254 gtk_widget_queue_draw (GTK_WIDGET (spin));
1258 gtk_spin_button_button_press (GtkWidget *widget,
1259 GdkEventButton *event)
1261 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1262 GtkSpinButtonPrivate *priv = spin->priv;
1266 if (event->window == priv->panel)
1268 GtkRequisition requisition;
1271 if (!gtk_widget_has_focus (widget))
1272 gtk_widget_grab_focus (widget);
1273 priv->button = event->button;
1275 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1276 gtk_spin_button_update (spin);
1278 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1279 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1281 if (event->y <= req_height / 2)
1283 if (event->button == 1)
1284 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_step_increment (priv->adjustment));
1285 else if (event->button == 2)
1286 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_page_increment (priv->adjustment));
1288 priv->click_child = GTK_ARROW_UP;
1292 if (event->button == 1)
1293 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_step_increment (priv->adjustment));
1294 else if (event->button == 2)
1295 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_page_increment (priv->adjustment));
1297 priv->click_child = GTK_ARROW_DOWN;
1302 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1308 gtk_spin_button_button_release (GtkWidget *widget,
1309 GdkEventButton *event)
1311 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1312 GtkSpinButtonPrivate *priv = spin->priv;
1315 arrow_size = spin_button_get_arrow_size (spin);
1317 if (event->button == priv->button)
1319 int click_child = priv->click_child;
1321 gtk_spin_button_stop_spinning (spin);
1323 if (event->button == 3)
1325 GtkRequisition requisition;
1327 GtkStyleContext *context;
1328 GtkStateFlags state;
1331 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1332 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1334 context = gtk_widget_get_style_context (widget);
1335 state = gtk_widget_get_state_flags (widget);
1336 gtk_style_context_get_padding (context, state, &padding);
1338 if (event->y >= 0 && event->x >= 0 &&
1339 event->y <= req_height &&
1340 event->x <= arrow_size + padding.left + padding.right)
1342 if (click_child == GTK_ARROW_UP &&
1343 event->y <= req_height / 2)
1347 diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1349 gtk_spin_button_real_spin (spin, diff);
1351 else if (click_child == GTK_ARROW_DOWN &&
1352 event->y > req_height / 2)
1356 diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1358 gtk_spin_button_real_spin (spin, -diff);
1362 gtk_widget_queue_draw (GTK_WIDGET (spin));
1367 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1371 gtk_spin_button_motion_notify (GtkWidget *widget,
1372 GdkEventMotion *event)
1374 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1375 GtkSpinButtonPrivate *priv = spin->priv;
1380 if (event->window == priv->panel)
1382 GtkRequisition requisition;
1386 gdk_event_request_motions (event);
1388 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1389 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1391 if (y <= req_height / 2 &&
1392 priv->in_child == GTK_ARROW_DOWN)
1394 priv->in_child = GTK_ARROW_UP;
1395 gtk_widget_queue_draw (GTK_WIDGET (spin));
1397 else if (y > req_height / 2 &&
1398 priv->in_child == GTK_ARROW_UP)
1400 priv->in_child = GTK_ARROW_DOWN;
1401 gtk_widget_queue_draw (GTK_WIDGET (spin));
1407 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1411 gtk_spin_button_timer (GtkSpinButton *spin_button)
1413 GtkSpinButtonPrivate *priv = spin_button->priv;
1414 gboolean retval = FALSE;
1418 if (priv->click_child == GTK_ARROW_UP)
1419 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1421 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1423 if (priv->need_timer)
1425 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1428 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1430 priv->need_timer = FALSE;
1431 priv->timer = gdk_threads_add_timeout (timeout,
1432 (GSourceFunc) gtk_spin_button_timer,
1433 (gpointer) spin_button);
1437 if (priv->climb_rate > 0.0 && priv->timer_step
1438 < gtk_adjustment_get_page_increment (priv->adjustment))
1440 if (priv->timer_calls < MAX_TIMER_CALLS)
1441 priv->timer_calls++;
1444 priv->timer_calls = 0;
1445 priv->timer_step += priv->climb_rate;
1456 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1457 GtkSpinButton *spin_button)
1459 gboolean return_val;
1461 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1464 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1465 if (return_val == FALSE)
1466 gtk_spin_button_default_output (spin_button);
1468 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1470 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1472 g_object_notify (G_OBJECT (spin_button), "value");
1476 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1477 GtkScrollType scroll)
1479 GtkSpinButtonPrivate *priv = spin->priv;
1482 /* When the key binding is activated, there may be an outstanding
1483 * value, so we first have to commit what is currently written in
1484 * the spin buttons text entry. See #106574
1486 gtk_spin_button_update (spin);
1488 old_value = gtk_adjustment_get_value (priv->adjustment);
1490 /* We don't test whether the entry is editable, since
1491 * this key binding conceptually corresponds to changing
1492 * the value with the buttons using the mouse, which
1493 * we allow for non-editable spin buttons.
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 GtkStyleContext *context;
1625 GtkStateFlags state;
1631 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1633 widget = GTK_WIDGET (entry);
1634 state = gtk_widget_get_state_flags (widget);
1635 context = gtk_widget_get_style_context (widget);
1636 gtk_style_context_get_padding (context, state, &padding);
1638 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1639 panel_width = arrow_size + padding.left + padding.right;
1642 *width -= panel_width;
1644 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1649 gtk_spin_button_insert_text (GtkEditable *editable,
1650 const gchar *new_text,
1651 gint new_text_length,
1654 GtkEntry *entry = GTK_ENTRY (editable);
1655 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1656 GtkSpinButtonPrivate *priv = spin->priv;
1657 GtkEditableInterface *parent_editable_iface;
1659 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1671 const gchar *entry_text;
1673 entry_length = gtk_entry_get_text_length (entry);
1674 entry_text = gtk_entry_get_text (entry);
1678 if (*(lc->negative_sign))
1679 neg_sign = *(lc->negative_sign);
1683 if (*(lc->positive_sign))
1684 pos_sign = *(lc->positive_sign);
1689 /* Workaround for bug caused by some Windows application messing
1690 * up the positive sign of the current locale, more specifically
1691 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1692 * See bug #330743 and for instance
1693 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1695 * I don't know if the positive sign always gets bogusly set to
1696 * a digit when the above Registry value is corrupted as
1697 * described. (In my test case, it got set to "8", and in the
1698 * bug report above it presumably was set ot "0".) Probably it
1699 * might get set to almost anything? So how to distinguish a
1700 * bogus value from some correct one for some locale? That is
1701 * probably hard, but at least we should filter out the
1704 if (pos_sign >= '0' && pos_sign <= '9')
1708 for (sign=0, i=0; i<entry_length; i++)
1709 if ((entry_text[i] == neg_sign) ||
1710 (entry_text[i] == pos_sign))
1716 if (sign && !(*position))
1719 for (dotpos=-1, i=0; i<entry_length; i++)
1720 if (entry_text[i] == *(lc->decimal_point))
1726 if (dotpos > -1 && *position > dotpos &&
1727 (gint)priv->digits - entry_length
1728 + dotpos - new_text_length + 1 < 0)
1731 for (i = 0; i < new_text_length; i++)
1733 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1735 if (sign || (*position) || i)
1739 else if (new_text[i] == *(lc->decimal_point))
1741 if (!priv->digits || dotpos > -1 ||
1742 (new_text_length - 1 - i + entry_length
1743 - *position > (gint)priv->digits))
1745 dotpos = *position + i;
1747 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1752 parent_editable_iface->insert_text (editable, new_text,
1753 new_text_length, position);
1757 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1760 GtkSpinButtonPrivate *priv = spin_button->priv;
1761 GtkAdjustment *adjustment;
1762 gdouble new_value = 0.0;
1763 gboolean wrapped = FALSE;
1765 adjustment = priv->adjustment;
1767 new_value = gtk_adjustment_get_value (adjustment) + increment;
1773 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_upper (adjustment)) < EPSILON)
1775 new_value = gtk_adjustment_get_lower (adjustment);
1778 else if (new_value > gtk_adjustment_get_upper (adjustment))
1779 new_value = gtk_adjustment_get_upper (adjustment);
1782 new_value = MIN (new_value, gtk_adjustment_get_upper (adjustment));
1784 else if (increment < 0)
1788 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment)) < EPSILON)
1790 new_value = gtk_adjustment_get_upper (adjustment);
1793 else if (new_value < gtk_adjustment_get_lower (adjustment))
1794 new_value = gtk_adjustment_get_lower (adjustment);
1797 new_value = MAX (new_value, gtk_adjustment_get_lower (adjustment));
1800 if (fabs (new_value - gtk_adjustment_get_value (adjustment)) > EPSILON)
1801 gtk_adjustment_set_value (adjustment, new_value);
1804 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1806 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1810 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1815 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1817 return GTK_INPUT_ERROR;
1823 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1825 GtkSpinButtonPrivate *priv = spin_button->priv;
1827 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, gtk_adjustment_get_value (priv->adjustment));
1829 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1830 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1836 /***********************************************************
1837 ***********************************************************
1838 *** Public interface ***
1839 ***********************************************************
1840 ***********************************************************/
1844 * gtk_spin_button_configure:
1845 * @spin_button: a #GtkSpinButton
1846 * @adjustment: (allow-none): a #GtkAdjustment
1847 * @climb_rate: the new climb rate
1848 * @digits: the number of decimal places to display in the spin button
1850 * Changes the properties of an existing spin button. The adjustment,
1851 * climb rate, and number of decimal places are all changed accordingly,
1852 * after this function call.
1855 gtk_spin_button_configure (GtkSpinButton *spin_button,
1856 GtkAdjustment *adjustment,
1860 GtkSpinButtonPrivate *priv;
1862 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1864 priv = spin_button->priv;
1867 gtk_spin_button_set_adjustment (spin_button, adjustment);
1869 adjustment = priv->adjustment;
1871 g_object_freeze_notify (G_OBJECT (spin_button));
1872 if (priv->digits != digits)
1874 priv->digits = digits;
1875 g_object_notify (G_OBJECT (spin_button), "digits");
1878 if (priv->climb_rate != climb_rate)
1880 priv->climb_rate = climb_rate;
1881 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1883 g_object_thaw_notify (G_OBJECT (spin_button));
1885 gtk_adjustment_value_changed (adjustment);
1889 * gtk_spin_button_new:
1890 * @adjustment: (allow-none): the #GtkAdjustment object that this spin
1891 * button should use, or %NULL
1892 * @climb_rate: specifies how much the spin button changes when an arrow
1894 * @digits: the number of decimal places to display
1896 * Creates a new #GtkSpinButton.
1898 * Returns: The new spin button as a #GtkWidget
1901 gtk_spin_button_new (GtkAdjustment *adjustment,
1905 GtkSpinButton *spin;
1908 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1910 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1912 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1914 return GTK_WIDGET (spin);
1918 * gtk_spin_button_new_with_range:
1919 * @min: Minimum allowable value
1920 * @max: Maximum allowable value
1921 * @step: Increment added or subtracted by spinning the widget
1923 * This is a convenience constructor that allows creation of a numeric
1924 * #GtkSpinButton without manually creating an adjustment. The value is
1925 * initially set to the minimum value and a page increment of 10 * @step
1926 * is the default. The precision of the spin button is equivalent to the
1927 * precision of @step.
1929 * Note that the way in which the precision is derived works best if @step
1930 * is a power of ten. If the resulting precision is not suitable for your
1931 * needs, use gtk_spin_button_set_digits() to correct it.
1933 * Return value: The new spin button as a #GtkWidget
1936 gtk_spin_button_new_with_range (gdouble min,
1940 GtkAdjustment *adjustment;
1941 GtkSpinButton *spin;
1944 g_return_val_if_fail (min <= max, NULL);
1945 g_return_val_if_fail (step != 0.0, NULL);
1947 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1949 adjustment = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1951 if (fabs (step) >= 1.0 || step == 0.0)
1954 digits = abs ((gint) floor (log10 (fabs (step))));
1955 if (digits > MAX_DIGITS)
1956 digits = MAX_DIGITS;
1959 gtk_spin_button_configure (spin, adjustment, step, digits);
1961 gtk_spin_button_set_numeric (spin, TRUE);
1963 return GTK_WIDGET (spin);
1966 /* Callback used when the spin button's adjustment changes.
1967 * We need to redraw the arrows when the adjustment's range
1968 * changes, and reevaluate our size request.
1971 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1973 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1974 GtkSpinButtonPrivate *priv = spin_button->priv;
1976 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1977 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1981 * gtk_spin_button_set_adjustment:
1982 * @spin_button: a #GtkSpinButton
1983 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1985 * Replaces the #GtkAdjustment associated with @spin_button.
1988 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1989 GtkAdjustment *adjustment)
1991 GtkSpinButtonPrivate *priv;
1993 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1995 priv = spin_button->priv;
1997 if (priv->adjustment != adjustment)
1999 if (priv->adjustment)
2001 g_signal_handlers_disconnect_by_func (priv->adjustment,
2002 gtk_spin_button_value_changed,
2004 g_signal_handlers_disconnect_by_func (priv->adjustment,
2005 adjustment_changed_cb,
2007 g_object_unref (priv->adjustment);
2009 priv->adjustment = adjustment;
2012 g_object_ref_sink (adjustment);
2013 g_signal_connect (adjustment, "value-changed",
2014 G_CALLBACK (gtk_spin_button_value_changed),
2016 g_signal_connect (adjustment, "changed",
2017 G_CALLBACK (adjustment_changed_cb),
2019 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
2022 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2025 g_object_notify (G_OBJECT (spin_button), "adjustment");
2029 * gtk_spin_button_get_adjustment:
2030 * @spin_button: a #GtkSpinButton
2032 * Get the adjustment associated with a #GtkSpinButton
2034 * Return value: (transfer none): the #GtkAdjustment of @spin_button
2037 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
2039 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
2041 return spin_button->priv->adjustment;
2045 * gtk_spin_button_set_digits:
2046 * @spin_button: a #GtkSpinButton
2047 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
2049 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
2053 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
2056 GtkSpinButtonPrivate *priv;
2058 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2060 priv = spin_button->priv;
2062 if (priv->digits != digits)
2064 priv->digits = digits;
2065 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2066 g_object_notify (G_OBJECT (spin_button), "digits");
2068 /* since lower/upper may have changed */
2069 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2074 * gtk_spin_button_get_digits:
2075 * @spin_button: a #GtkSpinButton
2077 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
2079 * Returns: the current precision
2082 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
2084 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2086 return spin_button->priv->digits;
2090 * gtk_spin_button_set_increments:
2091 * @spin_button: a #GtkSpinButton
2092 * @step: increment applied for a button 1 press.
2093 * @page: increment applied for a button 2 press.
2095 * Sets the step and page increments for spin_button. This affects how
2096 * quickly the value changes when the spin button's arrows are activated.
2099 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
2103 GtkSpinButtonPrivate *priv;
2105 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2107 priv = spin_button->priv;
2109 gtk_adjustment_configure (priv->adjustment,
2110 gtk_adjustment_get_value (priv->adjustment),
2111 gtk_adjustment_get_lower (priv->adjustment),
2112 gtk_adjustment_get_upper (priv->adjustment),
2115 gtk_adjustment_get_page_size (priv->adjustment));
2119 * gtk_spin_button_get_increments:
2120 * @spin_button: a #GtkSpinButton
2121 * @step: (out) (allow-none): location to store step increment, or %NULL
2122 * @page: (out) (allow-none): location to store page increment, or %NULL
2124 * Gets the current step and page the increments used by @spin_button. See
2125 * gtk_spin_button_set_increments().
2128 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
2132 GtkSpinButtonPrivate *priv;
2134 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2136 priv = spin_button->priv;
2139 *step = gtk_adjustment_get_step_increment (priv->adjustment);
2141 *page = gtk_adjustment_get_page_increment (priv->adjustment);
2145 * gtk_spin_button_set_range:
2146 * @spin_button: a #GtkSpinButton
2147 * @min: minimum allowable value
2148 * @max: maximum allowable value
2150 * Sets the minimum and maximum allowable values for @spin_button.
2153 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2157 GtkAdjustment *adjustment;
2159 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2161 adjustment = spin_button->priv->adjustment;
2163 gtk_adjustment_configure (adjustment,
2164 CLAMP (gtk_adjustment_get_value (adjustment), min, max),
2167 gtk_adjustment_get_step_increment (adjustment),
2168 gtk_adjustment_get_page_increment (adjustment),
2169 gtk_adjustment_get_page_size (adjustment));
2173 * gtk_spin_button_get_range:
2174 * @spin_button: a #GtkSpinButton
2175 * @min: (out) (allow-none): location to store minimum allowed value, or %NULL
2176 * @max: (out) (allow-none): location to store maximum allowed value, or %NULL
2178 * Gets the range allowed for @spin_button.
2179 * See gtk_spin_button_set_range().
2182 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2186 GtkSpinButtonPrivate *priv;
2188 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2190 priv = spin_button->priv;
2193 *min = gtk_adjustment_get_lower (priv->adjustment);
2195 *max = gtk_adjustment_get_upper (priv->adjustment);
2199 * gtk_spin_button_get_value:
2200 * @spin_button: a #GtkSpinButton
2202 * Get the value in the @spin_button.
2204 * Return value: the value of @spin_button
2207 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2209 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2211 return gtk_adjustment_get_value (spin_button->priv->adjustment);
2215 * gtk_spin_button_get_value_as_int:
2216 * @spin_button: a #GtkSpinButton
2218 * Get the value @spin_button represented as an integer.
2220 * Return value: the value of @spin_button
2223 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2225 GtkSpinButtonPrivate *priv;
2228 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2230 priv = spin_button->priv;
2232 val = gtk_adjustment_get_value (priv->adjustment);
2233 if (val - floor (val) < ceil (val) - val)
2240 * gtk_spin_button_set_value:
2241 * @spin_button: a #GtkSpinButton
2242 * @value: the new value
2244 * Sets the value of @spin_button.
2247 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2250 GtkSpinButtonPrivate *priv;
2252 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2254 priv = spin_button->priv;
2256 if (fabs (value - gtk_adjustment_get_value (priv->adjustment)) > EPSILON)
2257 gtk_adjustment_set_value (priv->adjustment, value);
2260 gint return_val = FALSE;
2261 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2262 if (return_val == FALSE)
2263 gtk_spin_button_default_output (spin_button);
2268 * gtk_spin_button_set_update_policy:
2269 * @spin_button: a #GtkSpinButton
2270 * @policy: a #GtkSpinButtonUpdatePolicy value
2272 * Sets the update behavior of a spin button.
2273 * This determines wether the spin button is always updated
2274 * or only when a valid value is set.
2277 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2278 GtkSpinButtonUpdatePolicy policy)
2280 GtkSpinButtonPrivate *priv;
2282 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2284 priv = spin_button->priv;
2286 if (priv->update_policy != policy)
2288 priv->update_policy = policy;
2289 g_object_notify (G_OBJECT (spin_button), "update-policy");
2294 * gtk_spin_button_get_update_policy:
2295 * @spin_button: a #GtkSpinButton
2297 * Gets the update behavior of a spin button.
2298 * See gtk_spin_button_set_update_policy().
2300 * Return value: the current update policy
2302 GtkSpinButtonUpdatePolicy
2303 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2305 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2307 return spin_button->priv->update_policy;
2311 * gtk_spin_button_set_numeric:
2312 * @spin_button: a #GtkSpinButton
2313 * @numeric: flag indicating if only numeric entry is allowed
2315 * Sets the flag that determines if non-numeric text can be typed
2316 * into the spin button.
2319 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2322 GtkSpinButtonPrivate *priv;
2324 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2326 priv = spin_button->priv;
2328 numeric = numeric != FALSE;
2330 if (priv->numeric != numeric)
2332 priv->numeric = numeric;
2333 g_object_notify (G_OBJECT (spin_button), "numeric");
2338 * gtk_spin_button_get_numeric:
2339 * @spin_button: a #GtkSpinButton
2341 * Returns whether non-numeric text can be typed into the spin button.
2342 * See gtk_spin_button_set_numeric().
2344 * Return value: %TRUE if only numeric text can be entered
2347 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2349 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2351 return spin_button->priv->numeric;
2355 * gtk_spin_button_set_wrap:
2356 * @spin_button: a #GtkSpinButton
2357 * @wrap: a flag indicating if wrapping behavior is performed
2359 * Sets the flag that determines if a spin button value wraps
2360 * around to the opposite limit when the upper or lower limit
2361 * of the range is exceeded.
2364 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2367 GtkSpinButtonPrivate *priv;
2369 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2371 priv = spin_button->priv;
2373 wrap = wrap != FALSE;
2375 if (priv->wrap != wrap)
2379 g_object_notify (G_OBJECT (spin_button), "wrap");
2384 * gtk_spin_button_get_wrap:
2385 * @spin_button: a #GtkSpinButton
2387 * Returns whether the spin button's value wraps around to the
2388 * opposite limit when the upper or lower limit of the range is
2389 * exceeded. See gtk_spin_button_set_wrap().
2391 * Return value: %TRUE if the spin button wraps around
2394 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2396 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2398 return spin_button->priv->wrap;
2402 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2404 const PangoFontDescription *font_desc;
2405 GtkStyleContext *context;
2409 /* FIXME: use getter */
2410 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
2411 font_desc = gtk_style_context_get_font (context, 0);
2413 size = pango_font_description_get_size (font_desc);
2414 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2416 return arrow_size - arrow_size % 2; /* force even */
2420 * gtk_spin_button_set_snap_to_ticks:
2421 * @spin_button: a #GtkSpinButton
2422 * @snap_to_ticks: a flag indicating if invalid values should be corrected
2424 * Sets the policy as to whether values are corrected to the
2425 * nearest step increment when a spin button is activated after
2426 * providing an invalid value.
2429 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2430 gboolean snap_to_ticks)
2432 GtkSpinButtonPrivate *priv;
2435 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2437 priv = spin_button->priv;
2439 new_val = (snap_to_ticks != 0);
2441 if (new_val != priv->snap_to_ticks)
2443 priv->snap_to_ticks = new_val;
2444 if (new_val && gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
2445 gtk_spin_button_update (spin_button);
2447 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2452 * gtk_spin_button_get_snap_to_ticks:
2453 * @spin_button: a #GtkSpinButton
2455 * Returns whether the values are corrected to the nearest step.
2456 * See gtk_spin_button_set_snap_to_ticks().
2458 * Return value: %TRUE if values are snapped to the nearest step
2461 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2463 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2465 return spin_button->priv->snap_to_ticks;
2469 * gtk_spin_button_spin:
2470 * @spin_button: a #GtkSpinButton
2471 * @direction: a #GtkSpinType indicating the direction to spin
2472 * @increment: step increment to apply in the specified direction
2474 * Increment or decrement a spin button's value in a specified
2475 * direction by a specified amount.
2478 gtk_spin_button_spin (GtkSpinButton *spin_button,
2479 GtkSpinType direction,
2482 GtkSpinButtonPrivate *priv;
2483 GtkAdjustment *adjustment;
2486 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2488 priv = spin_button->priv;
2490 adjustment = priv->adjustment;
2492 /* for compatibility with the 1.0.x version of this function */
2493 if (increment != 0 && increment != gtk_adjustment_get_step_increment (adjustment) &&
2494 (direction == GTK_SPIN_STEP_FORWARD ||
2495 direction == GTK_SPIN_STEP_BACKWARD))
2497 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2498 increment = -increment;
2499 direction = GTK_SPIN_USER_DEFINED;
2504 case GTK_SPIN_STEP_FORWARD:
2506 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_step_increment (adjustment));
2509 case GTK_SPIN_STEP_BACKWARD:
2511 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_step_increment (adjustment));
2514 case GTK_SPIN_PAGE_FORWARD:
2516 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_page_increment (adjustment));
2519 case GTK_SPIN_PAGE_BACKWARD:
2521 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_page_increment (adjustment));
2526 diff = gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment);
2528 gtk_spin_button_real_spin (spin_button, -diff);
2533 diff = gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_value (adjustment);
2535 gtk_spin_button_real_spin (spin_button, diff);
2538 case GTK_SPIN_USER_DEFINED:
2541 gtk_spin_button_real_spin (spin_button, increment);
2550 * gtk_spin_button_update:
2551 * @spin_button: a #GtkSpinButton
2553 * Manually force an update of the spin button.
2556 gtk_spin_button_update (GtkSpinButton *spin_button)
2558 GtkSpinButtonPrivate *priv;
2563 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2565 priv = spin_button->priv;
2568 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2569 if (return_val == FALSE)
2571 return_val = gtk_spin_button_default_input (spin_button, &val);
2572 error = (return_val == GTK_INPUT_ERROR);
2574 else if (return_val == GTK_INPUT_ERROR)
2577 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2579 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2581 if (val < gtk_adjustment_get_lower (priv->adjustment))
2582 val = gtk_adjustment_get_lower (priv->adjustment);
2583 else if (val > gtk_adjustment_get_upper (priv->adjustment))
2584 val = gtk_adjustment_get_upper (priv->adjustment);
2586 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2588 val < gtk_adjustment_get_lower (priv->adjustment) ||
2589 val > gtk_adjustment_get_upper (priv->adjustment)))
2591 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2595 if (priv->snap_to_ticks)
2596 gtk_spin_button_snap (spin_button, val);
2598 gtk_spin_button_set_value (spin_button, val);
2602 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2604 return spin_button->priv->panel;