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 "gtkmarshalers.h"
42 #include "gtksettings.h"
43 #include "gtkprivate.h"
45 #include "gtktypebuiltins.h"
47 #include "a11y/gtkspinbuttonaccessible.h"
49 #define MIN_SPIN_BUTTON_WIDTH 30
50 #define MAX_TIMER_CALLS 5
53 #define MIN_ARROW_WIDTH 6
57 * SECTION:gtkspinbutton
58 * @Title: GtkSpinButton
59 * @Short_description: Retrieve an integer or floating-point number from
61 * @See_also: #GtkEntry
63 * A #GtkSpinButton is an ideal way to allow the user to set the value of
64 * some attribute. Rather than having to directly type a number into a
65 * #GtkEntry, GtkSpinButton allows the user to click on one of two arrows
66 * to increment or decrement the displayed value. A value can still be
67 * typed in, with the bonus that it can be checked to ensure it is in a
70 * The main properties of a GtkSpinButton are through an adjustment.
71 * See the #GtkAdjustment section for more details about an adjustment's
75 * <title>Using a GtkSpinButton to get an integer</title>
77 * /* Provides a function to retrieve an integer value from a
78 * * GtkSpinButton and creates a spin button to model percentage
83 * grab_int_value (GtkSpinButton *button,
86 * return gtk_spin_button_get_value_as_int (button);
90 * create_integer_spin_button (void)
93 * GtkWidget *window, *button;
94 * GtkAdjustment *adjustment;
96 * adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0);
98 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
99 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
101 * /* creates the spinbutton, with no decimal places */
102 * button = gtk_spin_button_new (adjustment, 1.0, 0);
103 * gtk_container_add (GTK_CONTAINER (window), button);
105 * gtk_widget_show_all (window);
111 * <title>Using a GtkSpinButton to get a floating point value</title>
113 * /* Provides a function to retrieve a floating point value from a
114 * * GtkSpinButton, and creates a high precision spin button.
118 * grab_float_value (GtkSpinButton *button,
119 * gpointer user_data)
121 * return gtk_spin_button_get_value (button);
125 * create_floating_spin_button (void)
127 * GtkWidget *window, *button;
128 * GtkAdjustment *adjustment;
130 * adjustment = gtk_adjustment_new (2.500, 0.0, 5.0, 0.001, 0.1, 0.0);
132 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
133 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
135 * /* creates the spinbutton, with three decimal places */
136 * button = gtk_spin_button_new (adjustment, 0.001, 3);
137 * gtk_container_add (GTK_CONTAINER (window), button);
139 * gtk_widget_show_all (window);
145 struct _GtkSpinButtonPrivate
147 GtkAdjustment *adjustment;
153 GtkSpinButtonUpdatePolicy update_policy;
159 guint click_child : 2; /* valid: GTK_ARROW_UP=0, GTK_ARROW_DOWN=1 or 2=NONE/BOTH */
162 guint need_timer : 1;
164 guint snap_to_ticks : 1;
165 guint timer_calls : 3;
192 static void gtk_spin_button_editable_init (GtkEditableInterface *iface);
193 static void gtk_spin_button_finalize (GObject *object);
194 static void gtk_spin_button_set_property (GObject *object,
198 static void gtk_spin_button_get_property (GObject *object,
202 static void gtk_spin_button_destroy (GtkWidget *widget);
203 static void gtk_spin_button_map (GtkWidget *widget);
204 static void gtk_spin_button_unmap (GtkWidget *widget);
205 static void gtk_spin_button_realize (GtkWidget *widget);
206 static void gtk_spin_button_unrealize (GtkWidget *widget);
207 static void gtk_spin_button_get_preferred_width (GtkWidget *widget,
211 static void gtk_spin_button_size_allocate (GtkWidget *widget,
212 GtkAllocation *allocation);
213 static gint gtk_spin_button_draw (GtkWidget *widget,
215 static gint gtk_spin_button_button_press (GtkWidget *widget,
216 GdkEventButton *event);
217 static gint gtk_spin_button_button_release (GtkWidget *widget,
218 GdkEventButton *event);
219 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
220 GdkEventMotion *event);
221 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
222 GdkEventCrossing *event);
223 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
224 GdkEventCrossing *event);
225 static gint gtk_spin_button_focus_out (GtkWidget *widget,
226 GdkEventFocus *event);
227 static void gtk_spin_button_grab_notify (GtkWidget *widget,
228 gboolean was_grabbed);
229 static void gtk_spin_button_state_flags_changed (GtkWidget *widget,
230 GtkStateFlags previous_state);
231 static void gtk_spin_button_style_updated (GtkWidget *widget);
232 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
233 GtkStyleContext *context,
235 GtkArrowType arrow_type);
236 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
237 static gboolean gtk_spin_button_stop_spinning (GtkSpinButton *spin);
238 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
239 GtkSpinButton *spin_button);
240 static gint gtk_spin_button_key_release (GtkWidget *widget,
242 static gint gtk_spin_button_scroll (GtkWidget *widget,
243 GdkEventScroll *event);
244 static void gtk_spin_button_activate (GtkEntry *entry);
245 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
250 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
252 static void gtk_spin_button_insert_text (GtkEditable *editable,
253 const gchar *new_text,
254 gint new_text_length,
256 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
258 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
259 GtkScrollType scroll);
261 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
263 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
265 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
267 static guint spinbutton_signals[LAST_SIGNAL] = {0};
271 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
272 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
273 gtk_spin_button_editable_init))
275 #define add_spin_binding(binding_set, keyval, mask, scroll) \
276 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
278 GTK_TYPE_SCROLL_TYPE, scroll)
281 gtk_spin_button_class_init (GtkSpinButtonClass *class)
283 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
284 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
285 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
286 GtkBindingSet *binding_set;
288 gobject_class->finalize = gtk_spin_button_finalize;
289 gobject_class->set_property = gtk_spin_button_set_property;
290 gobject_class->get_property = gtk_spin_button_get_property;
292 widget_class->destroy = gtk_spin_button_destroy;
293 widget_class->map = gtk_spin_button_map;
294 widget_class->unmap = gtk_spin_button_unmap;
295 widget_class->realize = gtk_spin_button_realize;
296 widget_class->unrealize = gtk_spin_button_unrealize;
297 widget_class->get_preferred_width = gtk_spin_button_get_preferred_width;
298 widget_class->size_allocate = gtk_spin_button_size_allocate;
299 widget_class->draw = gtk_spin_button_draw;
300 widget_class->scroll_event = gtk_spin_button_scroll;
301 widget_class->button_press_event = gtk_spin_button_button_press;
302 widget_class->button_release_event = gtk_spin_button_button_release;
303 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
304 widget_class->key_release_event = gtk_spin_button_key_release;
305 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
306 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
307 widget_class->focus_out_event = gtk_spin_button_focus_out;
308 widget_class->grab_notify = gtk_spin_button_grab_notify;
309 widget_class->state_flags_changed = gtk_spin_button_state_flags_changed;
310 widget_class->style_updated = gtk_spin_button_style_updated;
312 entry_class->activate = gtk_spin_button_activate;
313 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
316 class->output = NULL;
317 class->change_value = gtk_spin_button_real_change_value;
319 g_object_class_install_property (gobject_class,
321 g_param_spec_object ("adjustment",
323 P_("The adjustment that holds the value of the spin button"),
325 GTK_PARAM_READWRITE));
327 g_object_class_install_property (gobject_class,
329 g_param_spec_double ("climb-rate",
331 P_("The acceleration rate when you hold down a button"),
335 GTK_PARAM_READWRITE));
337 g_object_class_install_property (gobject_class,
339 g_param_spec_uint ("digits",
341 P_("The number of decimal places to display"),
345 GTK_PARAM_READWRITE));
347 g_object_class_install_property (gobject_class,
349 g_param_spec_boolean ("snap-to-ticks",
351 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
353 GTK_PARAM_READWRITE));
355 g_object_class_install_property (gobject_class,
357 g_param_spec_boolean ("numeric",
359 P_("Whether non-numeric characters should be ignored"),
361 GTK_PARAM_READWRITE));
363 g_object_class_install_property (gobject_class,
365 g_param_spec_boolean ("wrap",
367 P_("Whether a spin button should wrap upon reaching its limits"),
369 GTK_PARAM_READWRITE));
371 g_object_class_install_property (gobject_class,
373 g_param_spec_enum ("update-policy",
375 P_("Whether the spin button should update always, or only when the value is legal"),
376 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
378 GTK_PARAM_READWRITE));
380 g_object_class_install_property (gobject_class,
382 g_param_spec_double ("value",
384 P_("Reads the current value, or sets a new value"),
388 GTK_PARAM_READWRITE));
390 gtk_widget_class_install_style_property_parser (widget_class,
391 g_param_spec_enum ("shadow-type",
393 P_("Style of bevel around the spin button"),
394 GTK_TYPE_SHADOW_TYPE,
397 gtk_rc_property_parse_enum);
400 * GtkSpinButton::input:
401 * @spin_button: the object on which the signal was emitted
402 * @new_value: (out) (type double): return location for the new value
404 * The ::input signal can be used to influence the conversion of
405 * the users input into a double value. The signal handler is
406 * expected to use gtk_entry_get_text() to retrieve the text of
407 * the entry and set @new_value to the new value.
409 * The default conversion uses g_strtod().
411 * Returns: %TRUE for a successful conversion, %FALSE if the input
412 * was not handled, and %GTK_INPUT_ERROR if the conversion failed.
414 spinbutton_signals[INPUT] =
415 g_signal_new (I_("input"),
416 G_TYPE_FROM_CLASS (gobject_class),
418 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
420 _gtk_marshal_INT__POINTER,
425 * GtkSpinButton::output:
426 * @spin_button: the object which received the signal
428 * The ::output signal can be used to change to formatting
429 * of the value that is displayed in the spin buttons entry.
431 * /* show leading zeros */
433 * on_output (GtkSpinButton *spin,
436 * GtkAdjustment *adjustment;
440 * adjustment = gtk_spin_button_get_adjustment (spin);
441 * value = (int)gtk_adjustment_get_value (adjustment);
442 * text = g_strdup_printf ("%02d", value);
443 * gtk_entry_set_text (GTK_ENTRY (spin), text);
450 * Returns: %TRUE if the value has been displayed
452 spinbutton_signals[OUTPUT] =
453 g_signal_new (I_("output"),
454 G_TYPE_FROM_CLASS (gobject_class),
456 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
457 _gtk_boolean_handled_accumulator, NULL,
458 _gtk_marshal_BOOLEAN__VOID,
461 spinbutton_signals[VALUE_CHANGED] =
462 g_signal_new (I_("value-changed"),
463 G_TYPE_FROM_CLASS (gobject_class),
465 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
467 _gtk_marshal_VOID__VOID,
471 * GtkSpinButton::wrapped:
472 * @spinbutton: the object which received the signal
474 * The wrapped signal is emitted right after the spinbutton wraps
475 * from its maximum to minimum value or vice-versa.
479 spinbutton_signals[WRAPPED] =
480 g_signal_new (I_("wrapped"),
481 G_TYPE_FROM_CLASS (gobject_class),
483 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
485 _gtk_marshal_VOID__VOID,
489 spinbutton_signals[CHANGE_VALUE] =
490 g_signal_new (I_("change-value"),
491 G_TYPE_FROM_CLASS (gobject_class),
492 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
493 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
495 _gtk_marshal_VOID__ENUM,
497 GTK_TYPE_SCROLL_TYPE);
499 binding_set = gtk_binding_set_by_class (class);
501 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
502 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
503 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
504 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
505 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
506 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
507 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
508 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
510 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
512 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SPIN_BUTTON_ACCESSIBLE);
516 gtk_spin_button_editable_init (GtkEditableInterface *iface)
518 iface->insert_text = gtk_spin_button_insert_text;
522 gtk_spin_button_set_property (GObject *object,
527 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
528 GtkSpinButtonPrivate *priv = spin_button->priv;
532 GtkAdjustment *adjustment;
534 case PROP_ADJUSTMENT:
535 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
537 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
538 gtk_spin_button_set_adjustment (spin_button, adjustment);
540 case PROP_CLIMB_RATE:
541 gtk_spin_button_configure (spin_button,
543 g_value_get_double (value),
547 gtk_spin_button_configure (spin_button,
550 g_value_get_uint (value));
552 case PROP_SNAP_TO_TICKS:
553 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
556 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
559 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
561 case PROP_UPDATE_POLICY:
562 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
565 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
568 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
574 gtk_spin_button_get_property (GObject *object,
579 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
580 GtkSpinButtonPrivate *priv = spin_button->priv;
584 case PROP_ADJUSTMENT:
585 g_value_set_object (value, priv->adjustment);
587 case PROP_CLIMB_RATE:
588 g_value_set_double (value, priv->climb_rate);
591 g_value_set_uint (value, priv->digits);
593 case PROP_SNAP_TO_TICKS:
594 g_value_set_boolean (value, priv->snap_to_ticks);
597 g_value_set_boolean (value, priv->numeric);
600 g_value_set_boolean (value, priv->wrap);
602 case PROP_UPDATE_POLICY:
603 g_value_set_enum (value, priv->update_policy);
606 g_value_set_double (value, gtk_adjustment_get_value (priv->adjustment));
609 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
615 gtk_spin_button_init (GtkSpinButton *spin_button)
617 GtkSpinButtonPrivate *priv;
618 GtkStyleContext *context;
620 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
621 GTK_TYPE_SPIN_BUTTON,
622 GtkSpinButtonPrivate);
623 priv = spin_button->priv;
625 priv->adjustment = NULL;
628 priv->climb_rate = 0.0;
629 priv->timer_step = 0.0;
630 priv->update_policy = GTK_UPDATE_ALWAYS;
631 priv->in_child = NO_ARROW;
632 priv->click_child = NO_ARROW;
634 priv->need_timer = FALSE;
635 priv->timer_calls = 0;
637 priv->numeric = FALSE;
639 priv->snap_to_ticks = FALSE;
641 gtk_spin_button_set_adjustment (spin_button,
642 gtk_adjustment_new (0, 0, 0, 0, 0, 0));
644 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
645 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINBUTTON);
649 gtk_spin_button_finalize (GObject *object)
651 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
653 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
657 gtk_spin_button_destroy (GtkWidget *widget)
659 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
661 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->destroy (widget);
665 gtk_spin_button_map (GtkWidget *widget)
667 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
668 GtkSpinButtonPrivate *priv = spin_button->priv;
670 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
672 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
673 gdk_window_show (priv->panel);
678 gtk_spin_button_unmap (GtkWidget *widget)
680 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
681 GtkSpinButtonPrivate *priv = spin_button->priv;
683 if (gtk_widget_get_mapped (widget))
685 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
687 gdk_window_hide (priv->panel);
688 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
693 gtk_spin_button_realize (GtkWidget *widget)
695 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
696 GtkSpinButtonPrivate *priv = spin_button->priv;
697 GtkStyleContext *context;
699 GtkAllocation allocation;
700 GtkRequisition requisition;
701 GdkWindowAttr attributes;
702 gint attributes_mask;
708 arrow_size = spin_button_get_arrow_size (spin_button);
710 gtk_widget_get_preferred_size (widget, &requisition, NULL);
711 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
712 gtk_widget_get_allocation (widget, &allocation);
714 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
715 GDK_KEY_RELEASE_MASK);
716 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
718 attributes.window_type = GDK_WINDOW_CHILD;
719 attributes.wclass = GDK_INPUT_ONLY;
720 attributes.visual = gtk_widget_get_visual (widget);
721 attributes.event_mask = gtk_widget_get_events (widget);
722 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
723 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
724 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
726 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
728 state = gtk_widget_get_state_flags (widget);
729 context = gtk_widget_get_style_context (widget);
730 gtk_style_context_get_padding (context, state, &padding);
732 attributes.x = allocation.x + allocation.width - arrow_size - (padding.left + padding.right);
733 attributes.y = allocation.y + (allocation.height - req_height) / 2;
734 attributes.width = arrow_size + padding.left + padding.right;
735 attributes.height = req_height;
737 priv->panel = gdk_window_new (gtk_widget_get_window (widget),
738 &attributes, attributes_mask);
739 gdk_window_set_user_data (priv->panel, widget);
742 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
743 if (return_val == FALSE)
744 gtk_spin_button_default_output (spin_button);
746 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
750 gtk_spin_button_unrealize (GtkWidget *widget)
752 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
753 GtkSpinButtonPrivate *priv = spin->priv;
755 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
759 gdk_window_set_user_data (priv->panel, NULL);
760 gdk_window_destroy (priv->panel);
766 compute_double_length (double val, int digits)
772 if (fabs (val) > 1.0)
773 a = floor (log10 (fabs (val))) + 1;
785 return a + digits + extra;
789 gtk_spin_button_get_preferred_width (GtkWidget *widget,
793 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
794 GtkSpinButtonPrivate *priv = spin_button->priv;
795 GtkEntry *entry = GTK_ENTRY (widget);
796 GtkStyleContext *style_context;
800 style_context = gtk_widget_get_style_context (widget);
802 arrow_size = spin_button_get_arrow_size (spin_button);
804 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->get_preferred_width (widget, minimum, natural);
806 if (gtk_entry_get_width_chars (entry) < 0)
808 PangoContext *context;
809 const PangoFontDescription *font_desc;
810 PangoFontMetrics *metrics;
816 gboolean interior_focus;
818 gint xborder, yborder;
819 GtkBorder inner_border;
821 gtk_widget_style_get (widget,
822 "interior-focus", &interior_focus,
823 "focus-line-width", &focus_width,
826 font_desc = gtk_style_context_get_font (style_context, 0);
828 context = gtk_widget_get_pango_context (widget);
829 metrics = pango_context_get_metrics (context, font_desc,
830 pango_context_get_language (context));
832 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
833 digit_width = PANGO_SCALE *
834 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
836 pango_font_metrics_unref (metrics);
838 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
839 width = MIN_SPIN_BUTTON_WIDTH;
840 max_string_len = MAX (10, compute_double_length (1e9 * gtk_adjustment_get_step_increment (priv->adjustment),
843 string_len = compute_double_length (gtk_adjustment_get_upper (priv->adjustment),
845 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
846 width = MAX (width, w);
847 string_len = compute_double_length (gtk_adjustment_get_lower (priv->adjustment), priv->digits);
848 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
849 width = MAX (width, w);
851 _gtk_entry_get_borders (entry, &xborder, &yborder);
852 _gtk_entry_effective_inner_border (entry, &inner_border);
854 width += xborder * 2 + inner_border.left + inner_border.right;
860 gtk_style_context_get_padding (style_context,
861 gtk_widget_get_state_flags (widget),
864 *minimum += arrow_size + padding.left + padding.right;
865 *natural += arrow_size + padding.left + padding.right;
869 gtk_spin_button_size_allocate (GtkWidget *widget,
870 GtkAllocation *allocation)
872 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
873 GtkSpinButtonPrivate *priv = spin->priv;
874 GtkAllocation panel_allocation;
875 GtkRequisition requisition;
876 GtkStyleContext *context;
883 arrow_size = spin_button_get_arrow_size (spin);
884 context = gtk_widget_get_style_context (widget);
885 state = gtk_widget_get_state_flags (widget);
887 gtk_style_context_get_padding (context, state, &padding);
888 panel_width = arrow_size + padding.left + padding.right;
890 gtk_widget_get_preferred_size (widget, &requisition, NULL);
891 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
893 gtk_widget_set_allocation (widget, allocation);
895 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
896 panel_allocation.x = allocation->x;
898 panel_allocation.x = allocation->x + allocation->width - panel_width;
900 panel_allocation.width = panel_width;
901 panel_allocation.height = MIN (req_height, allocation->height);
903 panel_allocation.y = allocation->y +
904 (allocation->height - req_height) / 2;
906 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
908 if (gtk_widget_get_realized (widget))
910 gdk_window_move_resize (priv->panel,
913 panel_allocation.width,
914 panel_allocation.height);
917 gtk_widget_queue_draw (GTK_WIDGET (spin));
921 gtk_spin_button_draw (GtkWidget *widget,
924 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
925 GtkSpinButtonPrivate *priv = spin->priv;
926 GtkStyleContext *context;
927 GtkStateFlags state = 0;
930 is_rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
931 context = gtk_widget_get_style_context (widget);
934 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
937 state = gtk_widget_get_state_flags (widget);
939 gtk_style_context_save (context);
940 gtk_style_context_set_state (context, state);
943 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_RIGHT);
945 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_LEFT);
947 gtk_cairo_transform_to_window (cr, widget, priv->panel);
949 gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_UP);
950 gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_DOWN);
952 gtk_style_context_restore (context);
958 spin_button_at_limit (GtkSpinButton *spin_button,
961 GtkSpinButtonPrivate *priv = spin_button->priv;
962 GtkArrowType effective_arrow;
967 if (gtk_adjustment_get_step_increment (priv->adjustment) > 0)
968 effective_arrow = arrow;
970 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
972 if (effective_arrow == GTK_ARROW_UP &&
973 (gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment) <= EPSILON))
976 if (effective_arrow == GTK_ARROW_DOWN &&
977 (gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment) <= EPSILON))
984 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
985 GtkStyleContext *context,
987 GtkArrowType arrow_type)
989 GtkSpinButtonPrivate *priv;
990 GtkJunctionSides junction;
995 gdouble size, width, height, x, y;
997 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
999 gtk_style_context_save (context);
1000 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
1002 priv = spin_button->priv;
1003 widget = GTK_WIDGET (spin_button);
1004 junction = gtk_style_context_get_junction_sides (context);
1006 panel_height = gdk_window_get_height (priv->panel);
1008 if (spin_button_at_limit (spin_button, arrow_type))
1009 state = GTK_STATE_FLAG_INSENSITIVE;
1012 if (priv->click_child == arrow_type)
1013 state = GTK_STATE_ACTIVE;
1016 if (priv->in_child == arrow_type &&
1017 priv->click_child == NO_ARROW)
1018 state = GTK_STATE_FLAG_PRELIGHT;
1020 state = gtk_widget_get_state_flags (widget);
1024 /* first, draw the background and the frame */
1025 if (arrow_type == GTK_ARROW_UP)
1030 junction |= GTK_JUNCTION_BOTTOM;
1032 else if (arrow_type == GTK_ARROW_DOWN)
1035 y = panel_height / 2.0;
1037 junction |= GTK_JUNCTION_TOP;
1040 gtk_style_context_set_junction_sides (context, junction);
1041 gtk_style_context_set_state (context, state);
1043 height = panel_height / 2.0;
1044 width = gdk_window_get_width (priv->panel);
1045 gtk_render_background (context, cr,
1046 x, y, width, height);
1047 gtk_render_frame (context, cr,
1048 x, y, width, height);
1050 /* make the actual rendered arrow smaller than text size */
1051 size = spin_button_get_arrow_size (spin_button);
1052 size = MIN (size, width);
1055 x = (width - size) / 2.0;
1057 if (arrow_type == GTK_ARROW_UP)
1059 y = (height - size / 2.0) / 2.0;
1062 else if (arrow_type == GTK_ARROW_DOWN)
1064 y = height + ((height - size / 2.0) / 2.0) - size / 2.0;
1068 gtk_render_arrow (context, cr,
1071 gtk_style_context_restore (context);
1075 gtk_spin_button_enter_notify (GtkWidget *widget,
1076 GdkEventCrossing *event)
1078 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1079 GtkSpinButtonPrivate *priv = spin->priv;
1080 GtkRequisition requisition;
1083 if (event->window == priv->panel)
1089 device = gdk_event_get_device ((GdkEvent *) event);
1090 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
1092 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1093 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1095 if (y <= req_height / 2)
1096 priv->in_child = GTK_ARROW_UP;
1098 priv->in_child = GTK_ARROW_DOWN;
1100 gtk_widget_queue_draw (GTK_WIDGET (spin));
1103 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1107 gtk_spin_button_leave_notify (GtkWidget *widget,
1108 GdkEventCrossing *event)
1110 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1111 GtkSpinButtonPrivate *priv = spin->priv;
1113 if (priv->in_child != NO_ARROW)
1115 priv->in_child = NO_ARROW;
1116 gtk_widget_queue_draw (GTK_WIDGET (spin));
1119 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1123 gtk_spin_button_focus_out (GtkWidget *widget,
1124 GdkEventFocus *event)
1126 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1127 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1129 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1133 gtk_spin_button_grab_notify (GtkWidget *widget,
1134 gboolean was_grabbed)
1136 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1140 if (gtk_spin_button_stop_spinning (spin))
1141 gtk_widget_queue_draw (GTK_WIDGET (spin));
1146 gtk_spin_button_state_flags_changed (GtkWidget *widget,
1147 GtkStateFlags previous_state)
1149 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1151 if (!gtk_widget_is_sensitive (widget))
1153 if (gtk_spin_button_stop_spinning (spin))
1154 gtk_widget_queue_draw (GTK_WIDGET (spin));
1159 gtk_spin_button_style_updated (GtkWidget *widget)
1161 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1162 GtkSpinButtonPrivate *priv = spin->priv;
1164 if (gtk_widget_get_realized (widget))
1166 GtkStyleContext *context;
1168 context = gtk_widget_get_style_context (widget);
1169 gtk_style_context_set_background (context, priv->panel);
1172 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_updated (widget);
1177 gtk_spin_button_scroll (GtkWidget *widget,
1178 GdkEventScroll *event)
1180 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1181 GtkSpinButtonPrivate *priv = spin->priv;
1183 if (event->direction == GDK_SCROLL_UP)
1185 if (!gtk_widget_has_focus (widget))
1186 gtk_widget_grab_focus (widget);
1187 gtk_spin_button_real_spin (spin, gtk_adjustment_get_step_increment (priv->adjustment));
1189 else if (event->direction == GDK_SCROLL_DOWN)
1191 if (!gtk_widget_has_focus (widget))
1192 gtk_widget_grab_focus (widget);
1193 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_step_increment (priv->adjustment));
1202 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1204 GtkSpinButtonPrivate *priv = spin->priv;
1205 gboolean did_spin = FALSE;
1209 g_source_remove (priv->timer);
1211 priv->need_timer = FALSE;
1217 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1218 priv->timer_calls = 0;
1220 priv->click_child = NO_ARROW;
1226 start_spinning (GtkSpinButton *spin,
1227 GtkArrowType click_child,
1230 GtkSpinButtonPrivate *priv;
1232 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1236 priv->click_child = click_child;
1240 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1243 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1245 priv->timer_step = step;
1246 priv->need_timer = TRUE;
1247 priv->timer = gdk_threads_add_timeout (timeout,
1248 (GSourceFunc) gtk_spin_button_timer,
1251 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1253 gtk_widget_queue_draw (GTK_WIDGET (spin));
1257 gtk_spin_button_button_press (GtkWidget *widget,
1258 GdkEventButton *event)
1260 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1261 GtkSpinButtonPrivate *priv = spin->priv;
1265 if (event->window == priv->panel)
1267 GtkRequisition requisition;
1270 if (!gtk_widget_has_focus (widget))
1271 gtk_widget_grab_focus (widget);
1272 priv->button = event->button;
1274 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1275 gtk_spin_button_update (spin);
1277 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1278 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1280 if (event->y <= req_height / 2)
1282 if (event->button == 1)
1283 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_step_increment (priv->adjustment));
1284 else if (event->button == 2)
1285 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_page_increment (priv->adjustment));
1287 priv->click_child = GTK_ARROW_UP;
1291 if (event->button == 1)
1292 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_step_increment (priv->adjustment));
1293 else if (event->button == 2)
1294 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_page_increment (priv->adjustment));
1296 priv->click_child = GTK_ARROW_DOWN;
1301 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1307 gtk_spin_button_button_release (GtkWidget *widget,
1308 GdkEventButton *event)
1310 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1311 GtkSpinButtonPrivate *priv = spin->priv;
1314 arrow_size = spin_button_get_arrow_size (spin);
1316 if (event->button == priv->button)
1318 int click_child = priv->click_child;
1320 gtk_spin_button_stop_spinning (spin);
1322 if (event->button == 3)
1324 GtkRequisition requisition;
1326 GtkStyleContext *context;
1327 GtkStateFlags state;
1330 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1331 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1333 context = gtk_widget_get_style_context (widget);
1334 state = gtk_widget_get_state_flags (widget);
1335 gtk_style_context_get_padding (context, state, &padding);
1337 if (event->y >= 0 && event->x >= 0 &&
1338 event->y <= req_height &&
1339 event->x <= arrow_size + padding.left + padding.right)
1341 if (click_child == GTK_ARROW_UP &&
1342 event->y <= req_height / 2)
1346 diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1348 gtk_spin_button_real_spin (spin, diff);
1350 else if (click_child == GTK_ARROW_DOWN &&
1351 event->y > req_height / 2)
1355 diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1357 gtk_spin_button_real_spin (spin, -diff);
1361 gtk_widget_queue_draw (GTK_WIDGET (spin));
1366 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1370 gtk_spin_button_motion_notify (GtkWidget *widget,
1371 GdkEventMotion *event)
1373 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1374 GtkSpinButtonPrivate *priv = spin->priv;
1379 if (event->window == priv->panel)
1381 GtkRequisition requisition;
1385 gdk_event_request_motions (event);
1387 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1388 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1390 if (y <= req_height / 2 &&
1391 priv->in_child == GTK_ARROW_DOWN)
1393 priv->in_child = GTK_ARROW_UP;
1394 gtk_widget_queue_draw (GTK_WIDGET (spin));
1396 else if (y > req_height / 2 &&
1397 priv->in_child == GTK_ARROW_UP)
1399 priv->in_child = GTK_ARROW_DOWN;
1400 gtk_widget_queue_draw (GTK_WIDGET (spin));
1406 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1410 gtk_spin_button_timer (GtkSpinButton *spin_button)
1412 GtkSpinButtonPrivate *priv = spin_button->priv;
1413 gboolean retval = FALSE;
1417 if (priv->click_child == GTK_ARROW_UP)
1418 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1420 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1422 if (priv->need_timer)
1424 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1427 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1429 priv->need_timer = FALSE;
1430 priv->timer = gdk_threads_add_timeout (timeout,
1431 (GSourceFunc) gtk_spin_button_timer,
1432 (gpointer) spin_button);
1436 if (priv->climb_rate > 0.0 && priv->timer_step
1437 < gtk_adjustment_get_page_increment (priv->adjustment))
1439 if (priv->timer_calls < MAX_TIMER_CALLS)
1440 priv->timer_calls++;
1443 priv->timer_calls = 0;
1444 priv->timer_step += priv->climb_rate;
1455 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1456 GtkSpinButton *spin_button)
1458 gboolean return_val;
1460 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1463 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1464 if (return_val == FALSE)
1465 gtk_spin_button_default_output (spin_button);
1467 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1469 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1471 g_object_notify (G_OBJECT (spin_button), "value");
1475 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1476 GtkScrollType scroll)
1478 GtkSpinButtonPrivate *priv = spin->priv;
1481 /* When the key binding is activated, there may be an outstanding
1482 * value, so we first have to commit what is currently written in
1483 * the spin buttons text entry. See #106574
1485 gtk_spin_button_update (spin);
1487 old_value = gtk_adjustment_get_value (priv->adjustment);
1489 /* We don't test whether the entry is editable, since
1490 * this key binding conceptually corresponds to changing
1491 * the value with the buttons using the mouse, which
1492 * we allow for non-editable spin buttons.
1496 case GTK_SCROLL_STEP_BACKWARD:
1497 case GTK_SCROLL_STEP_DOWN:
1498 case GTK_SCROLL_STEP_LEFT:
1499 gtk_spin_button_real_spin (spin, -priv->timer_step);
1501 if (priv->climb_rate > 0.0 && priv->timer_step
1502 < gtk_adjustment_get_page_increment (priv->adjustment))
1504 if (priv->timer_calls < MAX_TIMER_CALLS)
1505 priv->timer_calls++;
1508 priv->timer_calls = 0;
1509 priv->timer_step += priv->climb_rate;
1514 case GTK_SCROLL_STEP_FORWARD:
1515 case GTK_SCROLL_STEP_UP:
1516 case GTK_SCROLL_STEP_RIGHT:
1517 gtk_spin_button_real_spin (spin, priv->timer_step);
1519 if (priv->climb_rate > 0.0 && priv->timer_step
1520 < gtk_adjustment_get_page_increment (priv->adjustment))
1522 if (priv->timer_calls < MAX_TIMER_CALLS)
1523 priv->timer_calls++;
1526 priv->timer_calls = 0;
1527 priv->timer_step += priv->climb_rate;
1532 case GTK_SCROLL_PAGE_BACKWARD:
1533 case GTK_SCROLL_PAGE_DOWN:
1534 case GTK_SCROLL_PAGE_LEFT:
1535 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_page_increment (priv->adjustment));
1538 case GTK_SCROLL_PAGE_FORWARD:
1539 case GTK_SCROLL_PAGE_UP:
1540 case GTK_SCROLL_PAGE_RIGHT:
1541 gtk_spin_button_real_spin (spin, gtk_adjustment_get_page_increment (priv->adjustment));
1544 case GTK_SCROLL_START:
1546 gdouble diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1548 gtk_spin_button_real_spin (spin, -diff);
1552 case GTK_SCROLL_END:
1554 gdouble diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1556 gtk_spin_button_real_spin (spin, diff);
1561 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1565 gtk_spin_button_update (spin);
1567 if (gtk_adjustment_get_value (priv->adjustment) == old_value)
1568 gtk_widget_error_bell (GTK_WIDGET (spin));
1572 gtk_spin_button_key_release (GtkWidget *widget,
1575 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1576 GtkSpinButtonPrivate *priv = spin->priv;
1578 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1579 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1580 priv->timer_calls = 0;
1586 gtk_spin_button_snap (GtkSpinButton *spin_button,
1589 GtkSpinButtonPrivate *priv = spin_button->priv;
1593 inc = gtk_adjustment_get_step_increment (priv->adjustment);
1597 tmp = (val - gtk_adjustment_get_lower (priv->adjustment)) / inc;
1598 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1599 val = gtk_adjustment_get_lower (priv->adjustment) + floor (tmp) * inc;
1601 val = gtk_adjustment_get_lower (priv->adjustment) + ceil (tmp) * inc;
1603 gtk_spin_button_set_value (spin_button, val);
1607 gtk_spin_button_activate (GtkEntry *entry)
1609 if (gtk_editable_get_editable (GTK_EDITABLE (entry)))
1610 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1612 /* Chain up so that entry->activates_default is honored */
1613 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1617 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1623 GtkStyleContext *context;
1624 GtkStateFlags state;
1630 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1632 widget = GTK_WIDGET (entry);
1633 state = gtk_widget_get_state_flags (widget);
1634 context = gtk_widget_get_style_context (widget);
1635 gtk_style_context_get_padding (context, state, &padding);
1637 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1638 panel_width = arrow_size + padding.left + padding.right;
1641 *width -= panel_width;
1643 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1648 gtk_spin_button_insert_text (GtkEditable *editable,
1649 const gchar *new_text,
1650 gint new_text_length,
1653 GtkEntry *entry = GTK_ENTRY (editable);
1654 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1655 GtkSpinButtonPrivate *priv = spin->priv;
1656 GtkEditableInterface *parent_editable_iface;
1658 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1670 const gchar *entry_text;
1672 entry_length = gtk_entry_get_text_length (entry);
1673 entry_text = gtk_entry_get_text (entry);
1677 if (*(lc->negative_sign))
1678 neg_sign = *(lc->negative_sign);
1682 if (*(lc->positive_sign))
1683 pos_sign = *(lc->positive_sign);
1688 /* Workaround for bug caused by some Windows application messing
1689 * up the positive sign of the current locale, more specifically
1690 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1691 * See bug #330743 and for instance
1692 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1694 * I don't know if the positive sign always gets bogusly set to
1695 * a digit when the above Registry value is corrupted as
1696 * described. (In my test case, it got set to "8", and in the
1697 * bug report above it presumably was set ot "0".) Probably it
1698 * might get set to almost anything? So how to distinguish a
1699 * bogus value from some correct one for some locale? That is
1700 * probably hard, but at least we should filter out the
1703 if (pos_sign >= '0' && pos_sign <= '9')
1707 for (sign=0, i=0; i<entry_length; i++)
1708 if ((entry_text[i] == neg_sign) ||
1709 (entry_text[i] == pos_sign))
1715 if (sign && !(*position))
1718 for (dotpos=-1, i=0; i<entry_length; i++)
1719 if (entry_text[i] == *(lc->decimal_point))
1725 if (dotpos > -1 && *position > dotpos &&
1726 (gint)priv->digits - entry_length
1727 + dotpos - new_text_length + 1 < 0)
1730 for (i = 0; i < new_text_length; i++)
1732 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1734 if (sign || (*position) || i)
1738 else if (new_text[i] == *(lc->decimal_point))
1740 if (!priv->digits || dotpos > -1 ||
1741 (new_text_length - 1 - i + entry_length
1742 - *position > (gint)priv->digits))
1744 dotpos = *position + i;
1746 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1751 parent_editable_iface->insert_text (editable, new_text,
1752 new_text_length, position);
1756 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1759 GtkSpinButtonPrivate *priv = spin_button->priv;
1760 GtkAdjustment *adjustment;
1761 gdouble new_value = 0.0;
1762 gboolean wrapped = FALSE;
1764 adjustment = priv->adjustment;
1766 new_value = gtk_adjustment_get_value (adjustment) + increment;
1772 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_upper (adjustment)) < EPSILON)
1774 new_value = gtk_adjustment_get_lower (adjustment);
1777 else if (new_value > gtk_adjustment_get_upper (adjustment))
1778 new_value = gtk_adjustment_get_upper (adjustment);
1781 new_value = MIN (new_value, gtk_adjustment_get_upper (adjustment));
1783 else if (increment < 0)
1787 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment)) < EPSILON)
1789 new_value = gtk_adjustment_get_upper (adjustment);
1792 else if (new_value < gtk_adjustment_get_lower (adjustment))
1793 new_value = gtk_adjustment_get_lower (adjustment);
1796 new_value = MAX (new_value, gtk_adjustment_get_lower (adjustment));
1799 if (fabs (new_value - gtk_adjustment_get_value (adjustment)) > EPSILON)
1800 gtk_adjustment_set_value (adjustment, new_value);
1803 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1805 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1809 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1814 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1816 return GTK_INPUT_ERROR;
1822 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1824 GtkSpinButtonPrivate *priv = spin_button->priv;
1826 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, gtk_adjustment_get_value (priv->adjustment));
1828 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1829 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1835 /***********************************************************
1836 ***********************************************************
1837 *** Public interface ***
1838 ***********************************************************
1839 ***********************************************************/
1843 * gtk_spin_button_configure:
1844 * @spin_button: a #GtkSpinButton
1845 * @adjustment: (allow-none): a #GtkAdjustment
1846 * @climb_rate: the new climb rate
1847 * @digits: the number of decimal places to display in the spin button
1849 * Changes the properties of an existing spin button. The adjustment,
1850 * climb rate, and number of decimal places are all changed accordingly,
1851 * after this function call.
1854 gtk_spin_button_configure (GtkSpinButton *spin_button,
1855 GtkAdjustment *adjustment,
1859 GtkSpinButtonPrivate *priv;
1861 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1863 priv = spin_button->priv;
1866 gtk_spin_button_set_adjustment (spin_button, adjustment);
1868 adjustment = priv->adjustment;
1870 g_object_freeze_notify (G_OBJECT (spin_button));
1871 if (priv->digits != digits)
1873 priv->digits = digits;
1874 g_object_notify (G_OBJECT (spin_button), "digits");
1877 if (priv->climb_rate != climb_rate)
1879 priv->climb_rate = climb_rate;
1880 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1882 g_object_thaw_notify (G_OBJECT (spin_button));
1884 gtk_adjustment_value_changed (adjustment);
1888 * gtk_spin_button_new:
1889 * @adjustment: (allow-none): the #GtkAdjustment object that this spin
1890 * button should use, or %NULL
1891 * @climb_rate: specifies how much the spin button changes when an arrow
1893 * @digits: the number of decimal places to display
1895 * Creates a new #GtkSpinButton.
1897 * Returns: The new spin button as a #GtkWidget
1900 gtk_spin_button_new (GtkAdjustment *adjustment,
1904 GtkSpinButton *spin;
1907 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1909 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1911 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1913 return GTK_WIDGET (spin);
1917 * gtk_spin_button_new_with_range:
1918 * @min: Minimum allowable value
1919 * @max: Maximum allowable value
1920 * @step: Increment added or subtracted by spinning the widget
1922 * This is a convenience constructor that allows creation of a numeric
1923 * #GtkSpinButton without manually creating an adjustment. The value is
1924 * initially set to the minimum value and a page increment of 10 * @step
1925 * is the default. The precision of the spin button is equivalent to the
1926 * precision of @step.
1928 * Note that the way in which the precision is derived works best if @step
1929 * is a power of ten. If the resulting precision is not suitable for your
1930 * needs, use gtk_spin_button_set_digits() to correct it.
1932 * Return value: The new spin button as a #GtkWidget
1935 gtk_spin_button_new_with_range (gdouble min,
1939 GtkAdjustment *adjustment;
1940 GtkSpinButton *spin;
1943 g_return_val_if_fail (min <= max, NULL);
1944 g_return_val_if_fail (step != 0.0, NULL);
1946 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1948 adjustment = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1950 if (fabs (step) >= 1.0 || step == 0.0)
1953 digits = abs ((gint) floor (log10 (fabs (step))));
1954 if (digits > MAX_DIGITS)
1955 digits = MAX_DIGITS;
1958 gtk_spin_button_configure (spin, adjustment, step, digits);
1960 gtk_spin_button_set_numeric (spin, TRUE);
1962 return GTK_WIDGET (spin);
1965 /* Callback used when the spin button's adjustment changes.
1966 * We need to redraw the arrows when the adjustment's range
1967 * changes, and reevaluate our size request.
1970 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1972 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1973 GtkSpinButtonPrivate *priv = spin_button->priv;
1975 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1976 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1980 * gtk_spin_button_set_adjustment:
1981 * @spin_button: a #GtkSpinButton
1982 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1984 * Replaces the #GtkAdjustment associated with @spin_button.
1987 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1988 GtkAdjustment *adjustment)
1990 GtkSpinButtonPrivate *priv;
1992 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1994 priv = spin_button->priv;
1996 if (priv->adjustment != adjustment)
1998 if (priv->adjustment)
2000 g_signal_handlers_disconnect_by_func (priv->adjustment,
2001 gtk_spin_button_value_changed,
2003 g_signal_handlers_disconnect_by_func (priv->adjustment,
2004 adjustment_changed_cb,
2006 g_object_unref (priv->adjustment);
2008 priv->adjustment = adjustment;
2011 g_object_ref_sink (adjustment);
2012 g_signal_connect (adjustment, "value-changed",
2013 G_CALLBACK (gtk_spin_button_value_changed),
2015 g_signal_connect (adjustment, "changed",
2016 G_CALLBACK (adjustment_changed_cb),
2018 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
2021 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2024 g_object_notify (G_OBJECT (spin_button), "adjustment");
2028 * gtk_spin_button_get_adjustment:
2029 * @spin_button: a #GtkSpinButton
2031 * Get the adjustment associated with a #GtkSpinButton
2033 * Return value: (transfer none): the #GtkAdjustment of @spin_button
2036 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
2038 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
2040 return spin_button->priv->adjustment;
2044 * gtk_spin_button_set_digits:
2045 * @spin_button: a #GtkSpinButton
2046 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
2048 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
2052 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
2055 GtkSpinButtonPrivate *priv;
2057 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2059 priv = spin_button->priv;
2061 if (priv->digits != digits)
2063 priv->digits = digits;
2064 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2065 g_object_notify (G_OBJECT (spin_button), "digits");
2067 /* since lower/upper may have changed */
2068 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2073 * gtk_spin_button_get_digits:
2074 * @spin_button: a #GtkSpinButton
2076 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
2078 * Returns: the current precision
2081 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
2083 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2085 return spin_button->priv->digits;
2089 * gtk_spin_button_set_increments:
2090 * @spin_button: a #GtkSpinButton
2091 * @step: increment applied for a button 1 press.
2092 * @page: increment applied for a button 2 press.
2094 * Sets the step and page increments for spin_button. This affects how
2095 * quickly the value changes when the spin button's arrows are activated.
2098 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
2102 GtkSpinButtonPrivate *priv;
2104 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2106 priv = spin_button->priv;
2108 gtk_adjustment_configure (priv->adjustment,
2109 gtk_adjustment_get_value (priv->adjustment),
2110 gtk_adjustment_get_lower (priv->adjustment),
2111 gtk_adjustment_get_upper (priv->adjustment),
2114 gtk_adjustment_get_page_size (priv->adjustment));
2118 * gtk_spin_button_get_increments:
2119 * @spin_button: a #GtkSpinButton
2120 * @step: (out) (allow-none): location to store step increment, or %NULL
2121 * @page: (out) (allow-none): location to store page increment, or %NULL
2123 * Gets the current step and page the increments used by @spin_button. See
2124 * gtk_spin_button_set_increments().
2127 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
2131 GtkSpinButtonPrivate *priv;
2133 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2135 priv = spin_button->priv;
2138 *step = gtk_adjustment_get_step_increment (priv->adjustment);
2140 *page = gtk_adjustment_get_page_increment (priv->adjustment);
2144 * gtk_spin_button_set_range:
2145 * @spin_button: a #GtkSpinButton
2146 * @min: minimum allowable value
2147 * @max: maximum allowable value
2149 * Sets the minimum and maximum allowable values for @spin_button.
2151 * If the current value is outside this range, it will be adjusted
2152 * to fit within the range, otherwise it will remain unchanged.
2155 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2159 GtkAdjustment *adjustment;
2161 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2163 adjustment = spin_button->priv->adjustment;
2165 gtk_adjustment_configure (adjustment,
2166 CLAMP (gtk_adjustment_get_value (adjustment), min, max),
2169 gtk_adjustment_get_step_increment (adjustment),
2170 gtk_adjustment_get_page_increment (adjustment),
2171 gtk_adjustment_get_page_size (adjustment));
2175 * gtk_spin_button_get_range:
2176 * @spin_button: a #GtkSpinButton
2177 * @min: (out) (allow-none): location to store minimum allowed value, or %NULL
2178 * @max: (out) (allow-none): location to store maximum allowed value, or %NULL
2180 * Gets the range allowed for @spin_button.
2181 * See gtk_spin_button_set_range().
2184 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2188 GtkSpinButtonPrivate *priv;
2190 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2192 priv = spin_button->priv;
2195 *min = gtk_adjustment_get_lower (priv->adjustment);
2197 *max = gtk_adjustment_get_upper (priv->adjustment);
2201 * gtk_spin_button_get_value:
2202 * @spin_button: a #GtkSpinButton
2204 * Get the value in the @spin_button.
2206 * Return value: the value of @spin_button
2209 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2211 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2213 return gtk_adjustment_get_value (spin_button->priv->adjustment);
2217 * gtk_spin_button_get_value_as_int:
2218 * @spin_button: a #GtkSpinButton
2220 * Get the value @spin_button represented as an integer.
2222 * Return value: the value of @spin_button
2225 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2227 GtkSpinButtonPrivate *priv;
2230 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2232 priv = spin_button->priv;
2234 val = gtk_adjustment_get_value (priv->adjustment);
2235 if (val - floor (val) < ceil (val) - val)
2242 * gtk_spin_button_set_value:
2243 * @spin_button: a #GtkSpinButton
2244 * @value: the new value
2246 * Sets the value of @spin_button.
2249 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2252 GtkSpinButtonPrivate *priv;
2254 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2256 priv = spin_button->priv;
2258 if (fabs (value - gtk_adjustment_get_value (priv->adjustment)) > EPSILON)
2259 gtk_adjustment_set_value (priv->adjustment, value);
2262 gint return_val = FALSE;
2263 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2264 if (return_val == FALSE)
2265 gtk_spin_button_default_output (spin_button);
2270 * gtk_spin_button_set_update_policy:
2271 * @spin_button: a #GtkSpinButton
2272 * @policy: a #GtkSpinButtonUpdatePolicy value
2274 * Sets the update behavior of a spin button.
2275 * This determines wether the spin button is always updated
2276 * or only when a valid value is set.
2279 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2280 GtkSpinButtonUpdatePolicy policy)
2282 GtkSpinButtonPrivate *priv;
2284 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2286 priv = spin_button->priv;
2288 if (priv->update_policy != policy)
2290 priv->update_policy = policy;
2291 g_object_notify (G_OBJECT (spin_button), "update-policy");
2296 * gtk_spin_button_get_update_policy:
2297 * @spin_button: a #GtkSpinButton
2299 * Gets the update behavior of a spin button.
2300 * See gtk_spin_button_set_update_policy().
2302 * Return value: the current update policy
2304 GtkSpinButtonUpdatePolicy
2305 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2307 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2309 return spin_button->priv->update_policy;
2313 * gtk_spin_button_set_numeric:
2314 * @spin_button: a #GtkSpinButton
2315 * @numeric: flag indicating if only numeric entry is allowed
2317 * Sets the flag that determines if non-numeric text can be typed
2318 * into the spin button.
2321 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2324 GtkSpinButtonPrivate *priv;
2326 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2328 priv = spin_button->priv;
2330 numeric = numeric != FALSE;
2332 if (priv->numeric != numeric)
2334 priv->numeric = numeric;
2335 g_object_notify (G_OBJECT (spin_button), "numeric");
2340 * gtk_spin_button_get_numeric:
2341 * @spin_button: a #GtkSpinButton
2343 * Returns whether non-numeric text can be typed into the spin button.
2344 * See gtk_spin_button_set_numeric().
2346 * Return value: %TRUE if only numeric text can be entered
2349 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2351 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2353 return spin_button->priv->numeric;
2357 * gtk_spin_button_set_wrap:
2358 * @spin_button: a #GtkSpinButton
2359 * @wrap: a flag indicating if wrapping behavior is performed
2361 * Sets the flag that determines if a spin button value wraps
2362 * around to the opposite limit when the upper or lower limit
2363 * of the range is exceeded.
2366 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2369 GtkSpinButtonPrivate *priv;
2371 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2373 priv = spin_button->priv;
2375 wrap = wrap != FALSE;
2377 if (priv->wrap != wrap)
2381 g_object_notify (G_OBJECT (spin_button), "wrap");
2386 * gtk_spin_button_get_wrap:
2387 * @spin_button: a #GtkSpinButton
2389 * Returns whether the spin button's value wraps around to the
2390 * opposite limit when the upper or lower limit of the range is
2391 * exceeded. See gtk_spin_button_set_wrap().
2393 * Return value: %TRUE if the spin button wraps around
2396 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2398 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2400 return spin_button->priv->wrap;
2404 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2406 const PangoFontDescription *font_desc;
2407 GtkStyleContext *context;
2411 /* FIXME: use getter */
2412 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
2413 font_desc = gtk_style_context_get_font (context, 0);
2415 size = pango_font_description_get_size (font_desc);
2416 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2418 return arrow_size - arrow_size % 2; /* force even */
2422 * gtk_spin_button_set_snap_to_ticks:
2423 * @spin_button: a #GtkSpinButton
2424 * @snap_to_ticks: a flag indicating if invalid values should be corrected
2426 * Sets the policy as to whether values are corrected to the
2427 * nearest step increment when a spin button is activated after
2428 * providing an invalid value.
2431 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2432 gboolean snap_to_ticks)
2434 GtkSpinButtonPrivate *priv;
2437 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2439 priv = spin_button->priv;
2441 new_val = (snap_to_ticks != 0);
2443 if (new_val != priv->snap_to_ticks)
2445 priv->snap_to_ticks = new_val;
2446 if (new_val && gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
2447 gtk_spin_button_update (spin_button);
2449 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2454 * gtk_spin_button_get_snap_to_ticks:
2455 * @spin_button: a #GtkSpinButton
2457 * Returns whether the values are corrected to the nearest step.
2458 * See gtk_spin_button_set_snap_to_ticks().
2460 * Return value: %TRUE if values are snapped to the nearest step
2463 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2465 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2467 return spin_button->priv->snap_to_ticks;
2471 * gtk_spin_button_spin:
2472 * @spin_button: a #GtkSpinButton
2473 * @direction: a #GtkSpinType indicating the direction to spin
2474 * @increment: step increment to apply in the specified direction
2476 * Increment or decrement a spin button's value in a specified
2477 * direction by a specified amount.
2480 gtk_spin_button_spin (GtkSpinButton *spin_button,
2481 GtkSpinType direction,
2484 GtkSpinButtonPrivate *priv;
2485 GtkAdjustment *adjustment;
2488 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2490 priv = spin_button->priv;
2492 adjustment = priv->adjustment;
2494 /* for compatibility with the 1.0.x version of this function */
2495 if (increment != 0 && increment != gtk_adjustment_get_step_increment (adjustment) &&
2496 (direction == GTK_SPIN_STEP_FORWARD ||
2497 direction == GTK_SPIN_STEP_BACKWARD))
2499 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2500 increment = -increment;
2501 direction = GTK_SPIN_USER_DEFINED;
2506 case GTK_SPIN_STEP_FORWARD:
2508 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_step_increment (adjustment));
2511 case GTK_SPIN_STEP_BACKWARD:
2513 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_step_increment (adjustment));
2516 case GTK_SPIN_PAGE_FORWARD:
2518 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_page_increment (adjustment));
2521 case GTK_SPIN_PAGE_BACKWARD:
2523 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_page_increment (adjustment));
2528 diff = gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment);
2530 gtk_spin_button_real_spin (spin_button, -diff);
2535 diff = gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_value (adjustment);
2537 gtk_spin_button_real_spin (spin_button, diff);
2540 case GTK_SPIN_USER_DEFINED:
2543 gtk_spin_button_real_spin (spin_button, increment);
2552 * gtk_spin_button_update:
2553 * @spin_button: a #GtkSpinButton
2555 * Manually force an update of the spin button.
2558 gtk_spin_button_update (GtkSpinButton *spin_button)
2560 GtkSpinButtonPrivate *priv;
2565 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2567 priv = spin_button->priv;
2570 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2571 if (return_val == FALSE)
2573 return_val = gtk_spin_button_default_input (spin_button, &val);
2574 error = (return_val == GTK_INPUT_ERROR);
2576 else if (return_val == GTK_INPUT_ERROR)
2579 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2581 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2583 if (val < gtk_adjustment_get_lower (priv->adjustment))
2584 val = gtk_adjustment_get_lower (priv->adjustment);
2585 else if (val > gtk_adjustment_get_upper (priv->adjustment))
2586 val = gtk_adjustment_get_upper (priv->adjustment);
2588 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2590 val < gtk_adjustment_get_lower (priv->adjustment) ||
2591 val > gtk_adjustment_get_upper (priv->adjustment)))
2593 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2597 if (priv->snap_to_ticks)
2598 gtk_spin_button_snap (spin_button, val);
2600 gtk_spin_button_set_value (spin_button, val);
2604 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2606 return spin_button->priv->panel;