1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GtkSpinButton widget for GTK+
5 * Copyright (C) 1998 Lars Hamann and Stefan Jeske
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
38 #include "gtkbindings.h"
39 #include "gtkspinbutton.h"
40 #include "gtkentryprivate.h"
41 #include "gtkmainprivate.h"
42 #include "gtkmarshalers.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
46 #include "gtktypebuiltins.h"
48 #define MIN_SPIN_BUTTON_WIDTH 30
49 #define MAX_TIMER_CALLS 5
52 #define MIN_ARROW_WIDTH 6
56 * SECTION:gtkspinbutton
57 * @Title: GtkSpinButton
58 * @Short_description: Retrieve an integer or floating-point number from
60 * @See_also: #GtkEntry
62 * A #GtkSpinButton is an ideal way to allow the user to set the value of
63 * some attribute. Rather than having to directly type a number into a
64 * #GtkEntry, GtkSpinButton allows the user to click on one of two arrows
65 * to increment or decrement the displayed value. A value can still be
66 * typed in, with the bonus that it can be checked to ensure it is in a
69 * The main properties of a GtkSpinButton are through an adjustment.
70 * See the #GtkAdjustment section for more details about an adjustment's
74 * <title>Using a GtkSpinButton to get an integer</title>
76 * /* Provides a function to retrieve an integer value from a
77 * * GtkSpinButton and creates a spin button to model percentage
82 * grab_int_value (GtkSpinButton *button,
85 * return gtk_spin_button_get_value_as_int (button);
89 * create_integer_spin_button (void)
92 * GtkWidget *window, *button;
93 * GtkAdjustment *adjustment;
95 * adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0);
97 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
98 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
100 * /* creates the spinbutton, with no decimal places */
101 * button = gtk_spin_button_new (adjustment, 1.0, 0);
102 * gtk_container_add (GTK_CONTAINER (window), button);
104 * gtk_widget_show_all (window);
110 * <title>Using a GtkSpinButton to get a floating point value</title>
112 * /* Provides a function to retrieve a floating point value from a
113 * * GtkSpinButton, and creates a high precision spin button.
117 * grab_float_value (GtkSpinButton *button,
118 * gpointer user_data)
120 * return gtk_spin_button_get_value (button);
124 * create_floating_spin_button (void)
126 * GtkWidget *window, *button;
127 * GtkAdjustment *adjustment;
129 * adjustment = gtk_adjustment_new (2.500, 0.0, 5.0, 0.001, 0.1, 0.0);
131 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
132 * gtk_container_set_border_width (GTK_CONTAINER (window), 5);
134 * /* creates the spinbutton, with three decimal places */
135 * button = gtk_spin_button_new (adjustment, 0.001, 3);
136 * gtk_container_add (GTK_CONTAINER (window), button);
138 * gtk_widget_show_all (window);
144 struct _GtkSpinButtonPrivate
146 GtkAdjustment *adjustment;
152 GtkSpinButtonUpdatePolicy update_policy;
158 guint click_child : 2; /* valid: GTK_ARROW_UP=0, GTK_ARROW_DOWN=1 or 2=NONE/BOTH */
161 guint need_timer : 1;
163 guint snap_to_ticks : 1;
164 guint timer_calls : 3;
191 static void gtk_spin_button_editable_init (GtkEditableInterface *iface);
192 static void gtk_spin_button_finalize (GObject *object);
193 static void gtk_spin_button_set_property (GObject *object,
197 static void gtk_spin_button_get_property (GObject *object,
201 static void gtk_spin_button_destroy (GtkWidget *widget);
202 static void gtk_spin_button_map (GtkWidget *widget);
203 static void gtk_spin_button_unmap (GtkWidget *widget);
204 static void gtk_spin_button_realize (GtkWidget *widget);
205 static void gtk_spin_button_unrealize (GtkWidget *widget);
206 static void gtk_spin_button_get_preferred_width (GtkWidget *widget,
210 static void gtk_spin_button_size_allocate (GtkWidget *widget,
211 GtkAllocation *allocation);
212 static gint gtk_spin_button_draw (GtkWidget *widget,
214 static gint gtk_spin_button_button_press (GtkWidget *widget,
215 GdkEventButton *event);
216 static gint gtk_spin_button_button_release (GtkWidget *widget,
217 GdkEventButton *event);
218 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
219 GdkEventMotion *event);
220 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
221 GdkEventCrossing *event);
222 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
223 GdkEventCrossing *event);
224 static gint gtk_spin_button_focus_out (GtkWidget *widget,
225 GdkEventFocus *event);
226 static void gtk_spin_button_grab_notify (GtkWidget *widget,
227 gboolean was_grabbed);
228 static void gtk_spin_button_state_flags_changed (GtkWidget *widget,
229 GtkStateFlags previous_state);
230 static void gtk_spin_button_style_updated (GtkWidget *widget);
231 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
232 GtkStyleContext *context,
234 GtkArrowType arrow_type);
235 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
236 static gboolean gtk_spin_button_stop_spinning (GtkSpinButton *spin);
237 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
238 GtkSpinButton *spin_button);
239 static gint gtk_spin_button_key_release (GtkWidget *widget,
241 static gint gtk_spin_button_scroll (GtkWidget *widget,
242 GdkEventScroll *event);
243 static void gtk_spin_button_activate (GtkEntry *entry);
244 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
249 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
251 static void gtk_spin_button_insert_text (GtkEditable *editable,
252 const gchar *new_text,
253 gint new_text_length,
255 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
257 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
258 GtkScrollType scroll);
260 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
262 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
264 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
266 static guint spinbutton_signals[LAST_SIGNAL] = {0};
270 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
271 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
272 gtk_spin_button_editable_init))
274 #define add_spin_binding(binding_set, keyval, mask, scroll) \
275 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
277 GTK_TYPE_SCROLL_TYPE, scroll)
280 gtk_spin_button_class_init (GtkSpinButtonClass *class)
282 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
283 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
284 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
285 GtkBindingSet *binding_set;
287 gobject_class->finalize = gtk_spin_button_finalize;
288 gobject_class->set_property = gtk_spin_button_set_property;
289 gobject_class->get_property = gtk_spin_button_get_property;
291 widget_class->destroy = gtk_spin_button_destroy;
292 widget_class->map = gtk_spin_button_map;
293 widget_class->unmap = gtk_spin_button_unmap;
294 widget_class->realize = gtk_spin_button_realize;
295 widget_class->unrealize = gtk_spin_button_unrealize;
296 widget_class->get_preferred_width = gtk_spin_button_get_preferred_width;
297 widget_class->size_allocate = gtk_spin_button_size_allocate;
298 widget_class->draw = gtk_spin_button_draw;
299 widget_class->scroll_event = gtk_spin_button_scroll;
300 widget_class->button_press_event = gtk_spin_button_button_press;
301 widget_class->button_release_event = gtk_spin_button_button_release;
302 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
303 widget_class->key_release_event = gtk_spin_button_key_release;
304 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
305 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
306 widget_class->focus_out_event = gtk_spin_button_focus_out;
307 widget_class->grab_notify = gtk_spin_button_grab_notify;
308 widget_class->state_flags_changed = gtk_spin_button_state_flags_changed;
309 widget_class->style_updated = gtk_spin_button_style_updated;
311 entry_class->activate = gtk_spin_button_activate;
312 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
315 class->output = NULL;
316 class->change_value = gtk_spin_button_real_change_value;
318 g_object_class_install_property (gobject_class,
320 g_param_spec_object ("adjustment",
322 P_("The adjustment that holds the value of the spin button"),
324 GTK_PARAM_READWRITE));
326 g_object_class_install_property (gobject_class,
328 g_param_spec_double ("climb-rate",
330 P_("The acceleration rate when you hold down a button"),
334 GTK_PARAM_READWRITE));
336 g_object_class_install_property (gobject_class,
338 g_param_spec_uint ("digits",
340 P_("The number of decimal places to display"),
344 GTK_PARAM_READWRITE));
346 g_object_class_install_property (gobject_class,
348 g_param_spec_boolean ("snap-to-ticks",
350 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
352 GTK_PARAM_READWRITE));
354 g_object_class_install_property (gobject_class,
356 g_param_spec_boolean ("numeric",
358 P_("Whether non-numeric characters should be ignored"),
360 GTK_PARAM_READWRITE));
362 g_object_class_install_property (gobject_class,
364 g_param_spec_boolean ("wrap",
366 P_("Whether a spin button should wrap upon reaching its limits"),
368 GTK_PARAM_READWRITE));
370 g_object_class_install_property (gobject_class,
372 g_param_spec_enum ("update-policy",
374 P_("Whether the spin button should update always, or only when the value is legal"),
375 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
377 GTK_PARAM_READWRITE));
379 g_object_class_install_property (gobject_class,
381 g_param_spec_double ("value",
383 P_("Reads the current value, or sets a new value"),
387 GTK_PARAM_READWRITE));
389 gtk_widget_class_install_style_property_parser (widget_class,
390 g_param_spec_enum ("shadow-type",
392 P_("Style of bevel around the spin button"),
393 GTK_TYPE_SHADOW_TYPE,
396 gtk_rc_property_parse_enum);
399 * GtkSpinButton::input:
400 * @spin_button: the object on which the signal was emitted
401 * @new_value: (out) (type double): return location for the new value
403 * The ::input signal can be used to influence the conversion of
404 * the users input into a double value. The signal handler is
405 * expected to use gtk_entry_get_text() to retrieve the text of
406 * the entry and set @new_value to the new value.
408 * The default conversion uses g_strtod().
410 * Returns: %TRUE for a successful conversion, %FALSE if the input
411 * was not handled, and %GTK_INPUT_ERROR if the conversion failed.
413 spinbutton_signals[INPUT] =
414 g_signal_new (I_("input"),
415 G_TYPE_FROM_CLASS (gobject_class),
417 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
419 _gtk_marshal_INT__POINTER,
424 * GtkSpinButton::output:
425 * @spin_button: the object which received the signal
427 * The ::output signal can be used to change to formatting
428 * of the value that is displayed in the spin buttons entry.
430 * /* show leading zeros */
432 * on_output (GtkSpinButton *spin,
435 * GtkAdjustment *adjustment;
439 * adjustment = gtk_spin_button_get_adjustment (spin);
440 * value = (int)gtk_adjustment_get_value (adjustment);
441 * text = g_strdup_printf ("%02d", value);
442 * gtk_entry_set_text (GTK_ENTRY (spin), text);
449 * Returns: %TRUE if the value has been displayed
451 spinbutton_signals[OUTPUT] =
452 g_signal_new (I_("output"),
453 G_TYPE_FROM_CLASS (gobject_class),
455 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
456 _gtk_boolean_handled_accumulator, NULL,
457 _gtk_marshal_BOOLEAN__VOID,
460 spinbutton_signals[VALUE_CHANGED] =
461 g_signal_new (I_("value-changed"),
462 G_TYPE_FROM_CLASS (gobject_class),
464 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
466 _gtk_marshal_VOID__VOID,
470 * GtkSpinButton::wrapped:
471 * @spinbutton: the object which received the signal
473 * The wrapped signal is emitted right after the spinbutton wraps
474 * from its maximum to minimum value or vice-versa.
478 spinbutton_signals[WRAPPED] =
479 g_signal_new (I_("wrapped"),
480 G_TYPE_FROM_CLASS (gobject_class),
482 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
484 _gtk_marshal_VOID__VOID,
488 spinbutton_signals[CHANGE_VALUE] =
489 g_signal_new (I_("change-value"),
490 G_TYPE_FROM_CLASS (gobject_class),
491 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
492 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
494 _gtk_marshal_VOID__ENUM,
496 GTK_TYPE_SCROLL_TYPE);
498 binding_set = gtk_binding_set_by_class (class);
500 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
501 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
502 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
503 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
504 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
505 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
506 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
507 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
509 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
513 gtk_spin_button_editable_init (GtkEditableInterface *iface)
515 iface->insert_text = gtk_spin_button_insert_text;
519 gtk_spin_button_set_property (GObject *object,
524 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
525 GtkSpinButtonPrivate *priv = spin_button->priv;
529 GtkAdjustment *adjustment;
531 case PROP_ADJUSTMENT:
532 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
534 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
535 gtk_spin_button_set_adjustment (spin_button, adjustment);
537 case PROP_CLIMB_RATE:
538 gtk_spin_button_configure (spin_button,
540 g_value_get_double (value),
544 gtk_spin_button_configure (spin_button,
547 g_value_get_uint (value));
549 case PROP_SNAP_TO_TICKS:
550 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
553 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
556 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
558 case PROP_UPDATE_POLICY:
559 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
562 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
565 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
571 gtk_spin_button_get_property (GObject *object,
576 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
577 GtkSpinButtonPrivate *priv = spin_button->priv;
581 case PROP_ADJUSTMENT:
582 g_value_set_object (value, priv->adjustment);
584 case PROP_CLIMB_RATE:
585 g_value_set_double (value, priv->climb_rate);
588 g_value_set_uint (value, priv->digits);
590 case PROP_SNAP_TO_TICKS:
591 g_value_set_boolean (value, priv->snap_to_ticks);
594 g_value_set_boolean (value, priv->numeric);
597 g_value_set_boolean (value, priv->wrap);
599 case PROP_UPDATE_POLICY:
600 g_value_set_enum (value, priv->update_policy);
603 g_value_set_double (value, gtk_adjustment_get_value (priv->adjustment));
606 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
612 gtk_spin_button_init (GtkSpinButton *spin_button)
614 GtkSpinButtonPrivate *priv;
615 GtkStyleContext *context;
617 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
618 GTK_TYPE_SPIN_BUTTON,
619 GtkSpinButtonPrivate);
620 priv = spin_button->priv;
622 priv->adjustment = NULL;
625 priv->climb_rate = 0.0;
626 priv->timer_step = 0.0;
627 priv->update_policy = GTK_UPDATE_ALWAYS;
628 priv->in_child = NO_ARROW;
629 priv->click_child = NO_ARROW;
631 priv->need_timer = FALSE;
632 priv->timer_calls = 0;
634 priv->numeric = FALSE;
636 priv->snap_to_ticks = FALSE;
638 gtk_spin_button_set_adjustment (spin_button,
639 gtk_adjustment_new (0, 0, 0, 0, 0, 0));
641 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
642 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINBUTTON);
646 gtk_spin_button_finalize (GObject *object)
648 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
650 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
654 gtk_spin_button_destroy (GtkWidget *widget)
656 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
658 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->destroy (widget);
662 gtk_spin_button_map (GtkWidget *widget)
664 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
665 GtkSpinButtonPrivate *priv = spin_button->priv;
667 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
669 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
670 gdk_window_show (priv->panel);
675 gtk_spin_button_unmap (GtkWidget *widget)
677 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
678 GtkSpinButtonPrivate *priv = spin_button->priv;
680 if (gtk_widget_get_mapped (widget))
682 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
684 gdk_window_hide (priv->panel);
685 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
690 gtk_spin_button_realize (GtkWidget *widget)
692 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
693 GtkSpinButtonPrivate *priv = spin_button->priv;
694 GtkStyleContext *context;
696 GtkAllocation allocation;
697 GtkRequisition requisition;
698 GdkWindowAttr attributes;
699 gint attributes_mask;
704 arrow_size = spin_button_get_arrow_size (spin_button);
706 gtk_widget_get_preferred_size (widget, &requisition, NULL);
707 gtk_widget_get_allocation (widget, &allocation);
709 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
710 GDK_KEY_RELEASE_MASK);
711 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
713 attributes.window_type = GDK_WINDOW_CHILD;
714 attributes.wclass = GDK_INPUT_ONLY;
715 attributes.visual = gtk_widget_get_visual (widget);
716 attributes.event_mask = gtk_widget_get_events (widget);
717 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
718 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
719 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
721 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
723 state = gtk_widget_get_state_flags (widget);
724 context = gtk_widget_get_style_context (widget);
725 gtk_style_context_get_padding (context, state, &padding);
727 attributes.x = allocation.x + allocation.width - arrow_size - (padding.left + padding.right);
728 attributes.y = allocation.y + (allocation.height - requisition.height) / 2;
729 attributes.width = arrow_size + padding.left + padding.right;
730 attributes.height = requisition.height;
732 priv->panel = gdk_window_new (gtk_widget_get_window (widget),
733 &attributes, attributes_mask);
734 gdk_window_set_user_data (priv->panel, widget);
737 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
738 if (return_val == FALSE)
739 gtk_spin_button_default_output (spin_button);
741 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
745 gtk_spin_button_unrealize (GtkWidget *widget)
747 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
748 GtkSpinButtonPrivate *priv = spin->priv;
750 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
754 gdk_window_set_user_data (priv->panel, NULL);
755 gdk_window_destroy (priv->panel);
761 compute_double_length (double val, int digits)
767 if (fabs (val) > 1.0)
768 a = floor (log10 (fabs (val))) + 1;
780 return a + digits + extra;
784 gtk_spin_button_get_preferred_width (GtkWidget *widget,
788 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
789 GtkSpinButtonPrivate *priv = spin_button->priv;
790 GtkEntry *entry = GTK_ENTRY (widget);
791 GtkStyleContext *style_context;
795 style_context = gtk_widget_get_style_context (widget);
797 arrow_size = spin_button_get_arrow_size (spin_button);
799 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->get_preferred_width (widget, minimum, natural);
801 if (gtk_entry_get_width_chars (entry) < 0)
803 PangoContext *context;
804 const PangoFontDescription *font_desc;
805 PangoFontMetrics *metrics;
811 gboolean interior_focus;
813 gint xborder, yborder;
814 GtkBorder inner_border;
816 gtk_widget_style_get (widget,
817 "interior-focus", &interior_focus,
818 "focus-line-width", &focus_width,
821 font_desc = gtk_style_context_get_font (style_context, 0);
823 context = gtk_widget_get_pango_context (widget);
824 metrics = pango_context_get_metrics (context, font_desc,
825 pango_context_get_language (context));
827 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
828 digit_width = PANGO_SCALE *
829 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
831 pango_font_metrics_unref (metrics);
833 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
834 width = MIN_SPIN_BUTTON_WIDTH;
835 max_string_len = MAX (10, compute_double_length (1e9 * gtk_adjustment_get_step_increment (priv->adjustment),
838 string_len = compute_double_length (gtk_adjustment_get_upper (priv->adjustment),
840 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
841 width = MAX (width, w);
842 string_len = compute_double_length (gtk_adjustment_get_lower (priv->adjustment), priv->digits);
843 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
844 width = MAX (width, w);
846 _gtk_entry_get_borders (entry, &xborder, &yborder);
847 _gtk_entry_effective_inner_border (entry, &inner_border);
849 width += xborder * 2 + inner_border.left + inner_border.right;
855 gtk_style_context_get_padding (style_context,
856 gtk_widget_get_state_flags (widget),
859 *minimum += arrow_size + padding.left + padding.right;
860 *natural += arrow_size + padding.left + padding.right;
864 gtk_spin_button_size_allocate (GtkWidget *widget,
865 GtkAllocation *allocation)
867 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
868 GtkSpinButtonPrivate *priv = spin->priv;
869 GtkAllocation panel_allocation;
870 GtkRequisition requisition;
871 GtkStyleContext *context;
877 arrow_size = spin_button_get_arrow_size (spin);
878 context = gtk_widget_get_style_context (widget);
879 state = gtk_widget_get_state_flags (widget);
881 gtk_style_context_get_padding (context, state, &padding);
882 panel_width = arrow_size + padding.left + padding.right;
884 gtk_widget_get_preferred_size (widget, &requisition, NULL);
886 gtk_widget_set_allocation (widget, allocation);
888 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
889 panel_allocation.x = allocation->x;
891 panel_allocation.x = allocation->x + allocation->width - panel_width;
893 panel_allocation.width = panel_width;
894 panel_allocation.height = MIN (requisition.height, allocation->height);
896 panel_allocation.y = allocation->y +
897 (allocation->height - requisition.height) / 2;
899 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
901 if (gtk_widget_get_realized (widget))
903 gdk_window_move_resize (priv->panel,
906 panel_allocation.width,
907 panel_allocation.height);
910 gtk_widget_queue_draw (GTK_WIDGET (spin));
914 gtk_spin_button_draw (GtkWidget *widget,
917 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
918 GtkSpinButtonPrivate *priv = spin->priv;
919 GtkStyleContext *context;
920 GtkStateFlags state = 0;
923 is_rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
924 context = gtk_widget_get_style_context (widget);
927 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
930 state = gtk_widget_get_state_flags (widget);
932 gtk_style_context_save (context);
933 gtk_style_context_set_state (context, state);
936 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_RIGHT);
938 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_LEFT);
940 gtk_cairo_transform_to_window (cr, widget, priv->panel);
942 gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_UP);
943 gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_DOWN);
945 gtk_style_context_restore (context);
951 spin_button_at_limit (GtkSpinButton *spin_button,
954 GtkSpinButtonPrivate *priv = spin_button->priv;
955 GtkArrowType effective_arrow;
960 if (gtk_adjustment_get_step_increment (priv->adjustment) > 0)
961 effective_arrow = arrow;
963 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
965 if (effective_arrow == GTK_ARROW_UP &&
966 (gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment) <= EPSILON))
969 if (effective_arrow == GTK_ARROW_DOWN &&
970 (gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment) <= EPSILON))
977 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
978 GtkStyleContext *context,
980 GtkArrowType arrow_type)
982 GtkSpinButtonPrivate *priv;
983 GtkJunctionSides junction;
988 gdouble size, width, height, x, y;
990 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
992 gtk_style_context_save (context);
993 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
995 priv = spin_button->priv;
996 widget = GTK_WIDGET (spin_button);
997 junction = gtk_style_context_get_junction_sides (context);
999 panel_height = gdk_window_get_height (priv->panel);
1001 if (spin_button_at_limit (spin_button, arrow_type))
1002 state = GTK_STATE_FLAG_INSENSITIVE;
1005 if (priv->click_child == arrow_type)
1006 state = GTK_STATE_ACTIVE;
1009 if (priv->in_child == arrow_type &&
1010 priv->click_child == NO_ARROW)
1011 state = GTK_STATE_FLAG_PRELIGHT;
1013 state = gtk_widget_get_state_flags (widget);
1017 /* first, draw the background and the frame */
1018 if (arrow_type == GTK_ARROW_UP)
1023 junction |= GTK_JUNCTION_BOTTOM;
1025 else if (arrow_type == GTK_ARROW_DOWN)
1028 y = panel_height / 2.0;
1030 junction |= GTK_JUNCTION_TOP;
1033 gtk_style_context_set_junction_sides (context, junction);
1034 gtk_style_context_set_state (context, state);
1036 height = panel_height / 2.0;
1037 width = gdk_window_get_width (priv->panel);
1038 gtk_render_background (context, cr,
1039 x, y, width, height);
1040 gtk_render_frame (context, cr,
1041 x, y, width, height);
1043 /* make the actual rendered arrow smaller than text size */
1044 size = spin_button_get_arrow_size (spin_button);
1045 size = MIN (size, width);
1048 x = (width - size) / 2.0;
1050 if (arrow_type == GTK_ARROW_UP)
1052 y = (height - size / 2.0) / 2.0;
1055 else if (arrow_type == GTK_ARROW_DOWN)
1057 y = height + ((height - size / 2.0) / 2.0) - size / 2.0;
1061 gtk_render_arrow (context, cr,
1064 gtk_style_context_restore (context);
1068 gtk_spin_button_enter_notify (GtkWidget *widget,
1069 GdkEventCrossing *event)
1071 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1072 GtkSpinButtonPrivate *priv = spin->priv;
1073 GtkRequisition requisition;
1075 if (event->window == priv->panel)
1081 device = gdk_event_get_device ((GdkEvent *) event);
1082 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
1084 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1086 if (y <= requisition.height / 2)
1087 priv->in_child = GTK_ARROW_UP;
1089 priv->in_child = GTK_ARROW_DOWN;
1091 gtk_widget_queue_draw (GTK_WIDGET (spin));
1094 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1098 gtk_spin_button_leave_notify (GtkWidget *widget,
1099 GdkEventCrossing *event)
1101 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1102 GtkSpinButtonPrivate *priv = spin->priv;
1104 if (priv->in_child != NO_ARROW)
1106 priv->in_child = NO_ARROW;
1107 gtk_widget_queue_draw (GTK_WIDGET (spin));
1110 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1114 gtk_spin_button_focus_out (GtkWidget *widget,
1115 GdkEventFocus *event)
1117 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1118 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1120 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1124 gtk_spin_button_grab_notify (GtkWidget *widget,
1125 gboolean was_grabbed)
1127 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1131 if (gtk_spin_button_stop_spinning (spin))
1132 gtk_widget_queue_draw (GTK_WIDGET (spin));
1137 gtk_spin_button_state_flags_changed (GtkWidget *widget,
1138 GtkStateFlags previous_state)
1140 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1142 if (!gtk_widget_is_sensitive (widget))
1144 if (gtk_spin_button_stop_spinning (spin))
1145 gtk_widget_queue_draw (GTK_WIDGET (spin));
1150 gtk_spin_button_style_updated (GtkWidget *widget)
1152 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1153 GtkSpinButtonPrivate *priv = spin->priv;
1155 if (gtk_widget_get_realized (widget))
1157 GtkStyleContext *context;
1159 context = gtk_widget_get_style_context (widget);
1160 gtk_style_context_set_background (context, priv->panel);
1163 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_updated (widget);
1168 gtk_spin_button_scroll (GtkWidget *widget,
1169 GdkEventScroll *event)
1171 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1172 GtkSpinButtonPrivate *priv = spin->priv;
1174 if (event->direction == GDK_SCROLL_UP)
1176 if (!gtk_widget_has_focus (widget))
1177 gtk_widget_grab_focus (widget);
1178 gtk_spin_button_real_spin (spin, gtk_adjustment_get_step_increment (priv->adjustment));
1180 else if (event->direction == GDK_SCROLL_DOWN)
1182 if (!gtk_widget_has_focus (widget))
1183 gtk_widget_grab_focus (widget);
1184 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_step_increment (priv->adjustment));
1193 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1195 GtkSpinButtonPrivate *priv = spin->priv;
1196 gboolean did_spin = FALSE;
1200 g_source_remove (priv->timer);
1202 priv->need_timer = FALSE;
1208 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1209 priv->timer_calls = 0;
1211 priv->click_child = NO_ARROW;
1217 start_spinning (GtkSpinButton *spin,
1218 GtkArrowType click_child,
1221 GtkSpinButtonPrivate *priv;
1223 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1227 priv->click_child = click_child;
1231 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1234 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1236 priv->timer_step = step;
1237 priv->need_timer = TRUE;
1238 priv->timer = gdk_threads_add_timeout (timeout,
1239 (GSourceFunc) gtk_spin_button_timer,
1242 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1244 gtk_widget_queue_draw (GTK_WIDGET (spin));
1248 gtk_spin_button_button_press (GtkWidget *widget,
1249 GdkEventButton *event)
1251 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1252 GtkSpinButtonPrivate *priv = spin->priv;
1256 if (event->window == priv->panel)
1258 GtkRequisition requisition;
1260 if (!gtk_widget_has_focus (widget))
1261 gtk_widget_grab_focus (widget);
1262 priv->button = event->button;
1264 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1265 gtk_spin_button_update (spin);
1267 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1269 if (event->y <= requisition.height / 2)
1271 if (event->button == 1)
1272 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_step_increment (priv->adjustment));
1273 else if (event->button == 2)
1274 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_page_increment (priv->adjustment));
1276 priv->click_child = GTK_ARROW_UP;
1280 if (event->button == 1)
1281 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_step_increment (priv->adjustment));
1282 else if (event->button == 2)
1283 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_page_increment (priv->adjustment));
1285 priv->click_child = GTK_ARROW_DOWN;
1290 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1296 gtk_spin_button_button_release (GtkWidget *widget,
1297 GdkEventButton *event)
1299 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1300 GtkSpinButtonPrivate *priv = spin->priv;
1303 arrow_size = spin_button_get_arrow_size (spin);
1305 if (event->button == priv->button)
1307 int click_child = priv->click_child;
1309 gtk_spin_button_stop_spinning (spin);
1311 if (event->button == 3)
1313 GtkRequisition requisition;
1314 GtkStyleContext *context;
1315 GtkStateFlags state;
1318 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1320 context = gtk_widget_get_style_context (widget);
1321 state = gtk_widget_get_state_flags (widget);
1322 gtk_style_context_get_padding (context, state, &padding);
1324 if (event->y >= 0 && event->x >= 0 &&
1325 event->y <= requisition.height &&
1326 event->x <= arrow_size + padding.left + padding.right)
1328 if (click_child == GTK_ARROW_UP &&
1329 event->y <= requisition.height / 2)
1333 diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1335 gtk_spin_button_real_spin (spin, diff);
1337 else if (click_child == GTK_ARROW_DOWN &&
1338 event->y > requisition.height / 2)
1342 diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1344 gtk_spin_button_real_spin (spin, -diff);
1348 gtk_widget_queue_draw (GTK_WIDGET (spin));
1353 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1357 gtk_spin_button_motion_notify (GtkWidget *widget,
1358 GdkEventMotion *event)
1360 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1361 GtkSpinButtonPrivate *priv = spin->priv;
1366 if (event->window == priv->panel)
1368 GtkRequisition requisition;
1371 gdk_event_request_motions (event);
1373 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1375 if (y <= requisition.height / 2 &&
1376 priv->in_child == GTK_ARROW_DOWN)
1378 priv->in_child = GTK_ARROW_UP;
1379 gtk_widget_queue_draw (GTK_WIDGET (spin));
1381 else if (y > requisition.height / 2 &&
1382 priv->in_child == GTK_ARROW_UP)
1384 priv->in_child = GTK_ARROW_DOWN;
1385 gtk_widget_queue_draw (GTK_WIDGET (spin));
1391 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1395 gtk_spin_button_timer (GtkSpinButton *spin_button)
1397 GtkSpinButtonPrivate *priv = spin_button->priv;
1398 gboolean retval = FALSE;
1402 if (priv->click_child == GTK_ARROW_UP)
1403 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1405 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1407 if (priv->need_timer)
1409 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1412 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1414 priv->need_timer = FALSE;
1415 priv->timer = gdk_threads_add_timeout (timeout,
1416 (GSourceFunc) gtk_spin_button_timer,
1417 (gpointer) spin_button);
1421 if (priv->climb_rate > 0.0 && priv->timer_step
1422 < gtk_adjustment_get_page_increment (priv->adjustment))
1424 if (priv->timer_calls < MAX_TIMER_CALLS)
1425 priv->timer_calls++;
1428 priv->timer_calls = 0;
1429 priv->timer_step += priv->climb_rate;
1440 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1441 GtkSpinButton *spin_button)
1443 gboolean return_val;
1445 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1448 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1449 if (return_val == FALSE)
1450 gtk_spin_button_default_output (spin_button);
1452 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1454 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1456 g_object_notify (G_OBJECT (spin_button), "value");
1460 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1461 GtkScrollType scroll)
1463 GtkSpinButtonPrivate *priv = spin->priv;
1466 /* When the key binding is activated, there may be an outstanding
1467 * value, so we first have to commit what is currently written in
1468 * the spin buttons text entry. See #106574
1470 gtk_spin_button_update (spin);
1472 old_value = gtk_adjustment_get_value (priv->adjustment);
1474 /* We don't test whether the entry is editable, since
1475 * this key binding conceptually corresponds to changing
1476 * the value with the buttons using the mouse, which
1477 * we allow for non-editable spin buttons.
1481 case GTK_SCROLL_STEP_BACKWARD:
1482 case GTK_SCROLL_STEP_DOWN:
1483 case GTK_SCROLL_STEP_LEFT:
1484 gtk_spin_button_real_spin (spin, -priv->timer_step);
1486 if (priv->climb_rate > 0.0 && priv->timer_step
1487 < gtk_adjustment_get_page_increment (priv->adjustment))
1489 if (priv->timer_calls < MAX_TIMER_CALLS)
1490 priv->timer_calls++;
1493 priv->timer_calls = 0;
1494 priv->timer_step += priv->climb_rate;
1499 case GTK_SCROLL_STEP_FORWARD:
1500 case GTK_SCROLL_STEP_UP:
1501 case GTK_SCROLL_STEP_RIGHT:
1502 gtk_spin_button_real_spin (spin, priv->timer_step);
1504 if (priv->climb_rate > 0.0 && priv->timer_step
1505 < gtk_adjustment_get_page_increment (priv->adjustment))
1507 if (priv->timer_calls < MAX_TIMER_CALLS)
1508 priv->timer_calls++;
1511 priv->timer_calls = 0;
1512 priv->timer_step += priv->climb_rate;
1517 case GTK_SCROLL_PAGE_BACKWARD:
1518 case GTK_SCROLL_PAGE_DOWN:
1519 case GTK_SCROLL_PAGE_LEFT:
1520 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_page_increment (priv->adjustment));
1523 case GTK_SCROLL_PAGE_FORWARD:
1524 case GTK_SCROLL_PAGE_UP:
1525 case GTK_SCROLL_PAGE_RIGHT:
1526 gtk_spin_button_real_spin (spin, gtk_adjustment_get_page_increment (priv->adjustment));
1529 case GTK_SCROLL_START:
1531 gdouble diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1533 gtk_spin_button_real_spin (spin, -diff);
1537 case GTK_SCROLL_END:
1539 gdouble diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1541 gtk_spin_button_real_spin (spin, diff);
1546 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1550 gtk_spin_button_update (spin);
1552 if (gtk_adjustment_get_value (priv->adjustment) == old_value)
1553 gtk_widget_error_bell (GTK_WIDGET (spin));
1557 gtk_spin_button_key_release (GtkWidget *widget,
1560 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1561 GtkSpinButtonPrivate *priv = spin->priv;
1563 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1564 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1565 priv->timer_calls = 0;
1571 gtk_spin_button_snap (GtkSpinButton *spin_button,
1574 GtkSpinButtonPrivate *priv = spin_button->priv;
1578 inc = gtk_adjustment_get_step_increment (priv->adjustment);
1582 tmp = (val - gtk_adjustment_get_lower (priv->adjustment)) / inc;
1583 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1584 val = gtk_adjustment_get_lower (priv->adjustment) + floor (tmp) * inc;
1586 val = gtk_adjustment_get_lower (priv->adjustment) + ceil (tmp) * inc;
1588 gtk_spin_button_set_value (spin_button, val);
1592 gtk_spin_button_activate (GtkEntry *entry)
1594 if (gtk_editable_get_editable (GTK_EDITABLE (entry)))
1595 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1597 /* Chain up so that entry->activates_default is honored */
1598 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1602 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1608 GtkStyleContext *context;
1609 GtkStateFlags state;
1615 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1617 widget = GTK_WIDGET (entry);
1618 state = gtk_widget_get_state_flags (widget);
1619 context = gtk_widget_get_style_context (widget);
1620 gtk_style_context_get_padding (context, state, &padding);
1622 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1623 panel_width = arrow_size + padding.left + padding.right;
1626 *width -= panel_width;
1628 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1633 gtk_spin_button_insert_text (GtkEditable *editable,
1634 const gchar *new_text,
1635 gint new_text_length,
1638 GtkEntry *entry = GTK_ENTRY (editable);
1639 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1640 GtkSpinButtonPrivate *priv = spin->priv;
1641 GtkEditableInterface *parent_editable_iface;
1643 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1655 const gchar *entry_text;
1657 entry_length = gtk_entry_get_text_length (entry);
1658 entry_text = gtk_entry_get_text (entry);
1662 if (*(lc->negative_sign))
1663 neg_sign = *(lc->negative_sign);
1667 if (*(lc->positive_sign))
1668 pos_sign = *(lc->positive_sign);
1673 /* Workaround for bug caused by some Windows application messing
1674 * up the positive sign of the current locale, more specifically
1675 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1676 * See bug #330743 and for instance
1677 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1679 * I don't know if the positive sign always gets bogusly set to
1680 * a digit when the above Registry value is corrupted as
1681 * described. (In my test case, it got set to "8", and in the
1682 * bug report above it presumably was set ot "0".) Probably it
1683 * might get set to almost anything? So how to distinguish a
1684 * bogus value from some correct one for some locale? That is
1685 * probably hard, but at least we should filter out the
1688 if (pos_sign >= '0' && pos_sign <= '9')
1692 for (sign=0, i=0; i<entry_length; i++)
1693 if ((entry_text[i] == neg_sign) ||
1694 (entry_text[i] == pos_sign))
1700 if (sign && !(*position))
1703 for (dotpos=-1, i=0; i<entry_length; i++)
1704 if (entry_text[i] == *(lc->decimal_point))
1710 if (dotpos > -1 && *position > dotpos &&
1711 (gint)priv->digits - entry_length
1712 + dotpos - new_text_length + 1 < 0)
1715 for (i = 0; i < new_text_length; i++)
1717 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1719 if (sign || (*position) || i)
1723 else if (new_text[i] == *(lc->decimal_point))
1725 if (!priv->digits || dotpos > -1 ||
1726 (new_text_length - 1 - i + entry_length
1727 - *position > (gint)priv->digits))
1729 dotpos = *position + i;
1731 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1736 parent_editable_iface->insert_text (editable, new_text,
1737 new_text_length, position);
1741 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1744 GtkSpinButtonPrivate *priv = spin_button->priv;
1745 GtkAdjustment *adjustment;
1746 gdouble new_value = 0.0;
1747 gboolean wrapped = FALSE;
1749 adjustment = priv->adjustment;
1751 new_value = gtk_adjustment_get_value (adjustment) + increment;
1757 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_upper (adjustment)) < EPSILON)
1759 new_value = gtk_adjustment_get_lower (adjustment);
1762 else if (new_value > gtk_adjustment_get_upper (adjustment))
1763 new_value = gtk_adjustment_get_upper (adjustment);
1766 new_value = MIN (new_value, gtk_adjustment_get_upper (adjustment));
1768 else if (increment < 0)
1772 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment)) < EPSILON)
1774 new_value = gtk_adjustment_get_upper (adjustment);
1777 else if (new_value < gtk_adjustment_get_lower (adjustment))
1778 new_value = gtk_adjustment_get_lower (adjustment);
1781 new_value = MAX (new_value, gtk_adjustment_get_lower (adjustment));
1784 if (fabs (new_value - gtk_adjustment_get_value (adjustment)) > EPSILON)
1785 gtk_adjustment_set_value (adjustment, new_value);
1788 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1790 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1794 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1799 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1801 return GTK_INPUT_ERROR;
1807 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1809 GtkSpinButtonPrivate *priv = spin_button->priv;
1811 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, gtk_adjustment_get_value (priv->adjustment));
1813 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1814 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1820 /***********************************************************
1821 ***********************************************************
1822 *** Public interface ***
1823 ***********************************************************
1824 ***********************************************************/
1828 * gtk_spin_button_configure:
1829 * @spin_button: a #GtkSpinButton
1830 * @adjustment: (allow-none): a #GtkAdjustment
1831 * @climb_rate: the new climb rate
1832 * @digits: the number of decimal places to display in the spin button
1834 * Changes the properties of an existing spin button. The adjustment,
1835 * climb rate, and number of decimal places are all changed accordingly,
1836 * after this function call.
1839 gtk_spin_button_configure (GtkSpinButton *spin_button,
1840 GtkAdjustment *adjustment,
1844 GtkSpinButtonPrivate *priv;
1846 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1848 priv = spin_button->priv;
1851 gtk_spin_button_set_adjustment (spin_button, adjustment);
1853 adjustment = priv->adjustment;
1855 g_object_freeze_notify (G_OBJECT (spin_button));
1856 if (priv->digits != digits)
1858 priv->digits = digits;
1859 g_object_notify (G_OBJECT (spin_button), "digits");
1862 if (priv->climb_rate != climb_rate)
1864 priv->climb_rate = climb_rate;
1865 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1867 g_object_thaw_notify (G_OBJECT (spin_button));
1869 gtk_adjustment_value_changed (adjustment);
1873 * gtk_spin_button_new:
1874 * @adjustment: (allow-none): the #GtkAdjustment object that this spin
1875 * button should use, or %NULL
1876 * @climb_rate: specifies how much the spin button changes when an arrow
1878 * @digits: the number of decimal places to display
1880 * Creates a new #GtkSpinButton.
1882 * Returns: The new spin button as a #GtkWidget
1885 gtk_spin_button_new (GtkAdjustment *adjustment,
1889 GtkSpinButton *spin;
1892 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1894 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1896 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1898 return GTK_WIDGET (spin);
1902 * gtk_spin_button_new_with_range:
1903 * @min: Minimum allowable value
1904 * @max: Maximum allowable value
1905 * @step: Increment added or subtracted by spinning the widget
1907 * This is a convenience constructor that allows creation of a numeric
1908 * #GtkSpinButton without manually creating an adjustment. The value is
1909 * initially set to the minimum value and a page increment of 10 * @step
1910 * is the default. The precision of the spin button is equivalent to the
1911 * precision of @step.
1913 * Note that the way in which the precision is derived works best if @step
1914 * is a power of ten. If the resulting precision is not suitable for your
1915 * needs, use gtk_spin_button_set_digits() to correct it.
1917 * Return value: The new spin button as a #GtkWidget
1920 gtk_spin_button_new_with_range (gdouble min,
1924 GtkAdjustment *adjustment;
1925 GtkSpinButton *spin;
1928 g_return_val_if_fail (min <= max, NULL);
1929 g_return_val_if_fail (step != 0.0, NULL);
1931 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1933 adjustment = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1935 if (fabs (step) >= 1.0 || step == 0.0)
1938 digits = abs ((gint) floor (log10 (fabs (step))));
1939 if (digits > MAX_DIGITS)
1940 digits = MAX_DIGITS;
1943 gtk_spin_button_configure (spin, adjustment, step, digits);
1945 gtk_spin_button_set_numeric (spin, TRUE);
1947 return GTK_WIDGET (spin);
1950 /* Callback used when the spin button's adjustment changes.
1951 * We need to redraw the arrows when the adjustment's range
1952 * changes, and reevaluate our size request.
1955 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1957 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1958 GtkSpinButtonPrivate *priv = spin_button->priv;
1960 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1961 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1965 * gtk_spin_button_set_adjustment:
1966 * @spin_button: a #GtkSpinButton
1967 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1969 * Replaces the #GtkAdjustment associated with @spin_button.
1972 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1973 GtkAdjustment *adjustment)
1975 GtkSpinButtonPrivate *priv;
1977 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1979 priv = spin_button->priv;
1981 if (priv->adjustment != adjustment)
1983 if (priv->adjustment)
1985 g_signal_handlers_disconnect_by_func (priv->adjustment,
1986 gtk_spin_button_value_changed,
1988 g_signal_handlers_disconnect_by_func (priv->adjustment,
1989 adjustment_changed_cb,
1991 g_object_unref (priv->adjustment);
1993 priv->adjustment = adjustment;
1996 g_object_ref_sink (adjustment);
1997 g_signal_connect (adjustment, "value-changed",
1998 G_CALLBACK (gtk_spin_button_value_changed),
2000 g_signal_connect (adjustment, "changed",
2001 G_CALLBACK (adjustment_changed_cb),
2003 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
2006 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2009 g_object_notify (G_OBJECT (spin_button), "adjustment");
2013 * gtk_spin_button_get_adjustment:
2014 * @spin_button: a #GtkSpinButton
2016 * Get the adjustment associated with a #GtkSpinButton
2018 * Return value: (transfer none): the #GtkAdjustment of @spin_button
2021 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
2023 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
2025 return spin_button->priv->adjustment;
2029 * gtk_spin_button_set_digits:
2030 * @spin_button: a #GtkSpinButton
2031 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
2033 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
2037 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
2040 GtkSpinButtonPrivate *priv;
2042 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2044 priv = spin_button->priv;
2046 if (priv->digits != digits)
2048 priv->digits = digits;
2049 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2050 g_object_notify (G_OBJECT (spin_button), "digits");
2052 /* since lower/upper may have changed */
2053 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2058 * gtk_spin_button_get_digits:
2059 * @spin_button: a #GtkSpinButton
2061 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
2063 * Returns: the current precision
2066 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
2068 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2070 return spin_button->priv->digits;
2074 * gtk_spin_button_set_increments:
2075 * @spin_button: a #GtkSpinButton
2076 * @step: increment applied for a button 1 press.
2077 * @page: increment applied for a button 2 press.
2079 * Sets the step and page increments for spin_button. This affects how
2080 * quickly the value changes when the spin button's arrows are activated.
2083 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
2087 GtkSpinButtonPrivate *priv;
2089 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2091 priv = spin_button->priv;
2093 gtk_adjustment_configure (priv->adjustment,
2094 gtk_adjustment_get_value (priv->adjustment),
2095 gtk_adjustment_get_lower (priv->adjustment),
2096 gtk_adjustment_get_upper (priv->adjustment),
2099 gtk_adjustment_get_page_size (priv->adjustment));
2103 * gtk_spin_button_get_increments:
2104 * @spin_button: a #GtkSpinButton
2105 * @step: (out) (allow-none): location to store step increment, or %NULL
2106 * @page: (out) (allow-none): location to store page increment, or %NULL
2108 * Gets the current step and page the increments used by @spin_button. See
2109 * gtk_spin_button_set_increments().
2112 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
2116 GtkSpinButtonPrivate *priv;
2118 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2120 priv = spin_button->priv;
2123 *step = gtk_adjustment_get_step_increment (priv->adjustment);
2125 *page = gtk_adjustment_get_page_increment (priv->adjustment);
2129 * gtk_spin_button_set_range:
2130 * @spin_button: a #GtkSpinButton
2131 * @min: minimum allowable value
2132 * @max: maximum allowable value
2134 * Sets the minimum and maximum allowable values for @spin_button.
2137 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2141 GtkAdjustment *adjustment;
2143 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2145 adjustment = spin_button->priv->adjustment;
2147 gtk_adjustment_configure (adjustment,
2148 CLAMP (gtk_adjustment_get_value (adjustment), min, max),
2151 gtk_adjustment_get_step_increment (adjustment),
2152 gtk_adjustment_get_page_increment (adjustment),
2153 gtk_adjustment_get_page_size (adjustment));
2157 * gtk_spin_button_get_range:
2158 * @spin_button: a #GtkSpinButton
2159 * @min: (out) (allow-none): location to store minimum allowed value, or %NULL
2160 * @max: (out) (allow-none): location to store maximum allowed value, or %NULL
2162 * Gets the range allowed for @spin_button.
2163 * See gtk_spin_button_set_range().
2166 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2170 GtkSpinButtonPrivate *priv;
2172 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2174 priv = spin_button->priv;
2177 *min = gtk_adjustment_get_lower (priv->adjustment);
2179 *max = gtk_adjustment_get_upper (priv->adjustment);
2183 * gtk_spin_button_get_value:
2184 * @spin_button: a #GtkSpinButton
2186 * Get the value in the @spin_button.
2188 * Return value: the value of @spin_button
2191 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2193 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2195 return gtk_adjustment_get_value (spin_button->priv->adjustment);
2199 * gtk_spin_button_get_value_as_int:
2200 * @spin_button: a #GtkSpinButton
2202 * Get the value @spin_button represented as an integer.
2204 * Return value: the value of @spin_button
2207 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2209 GtkSpinButtonPrivate *priv;
2212 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2214 priv = spin_button->priv;
2216 val = gtk_adjustment_get_value (priv->adjustment);
2217 if (val - floor (val) < ceil (val) - val)
2224 * gtk_spin_button_set_value:
2225 * @spin_button: a #GtkSpinButton
2226 * @value: the new value
2228 * Sets the value of @spin_button.
2231 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2234 GtkSpinButtonPrivate *priv;
2236 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2238 priv = spin_button->priv;
2240 if (fabs (value - gtk_adjustment_get_value (priv->adjustment)) > EPSILON)
2241 gtk_adjustment_set_value (priv->adjustment, value);
2244 gint return_val = FALSE;
2245 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2246 if (return_val == FALSE)
2247 gtk_spin_button_default_output (spin_button);
2252 * gtk_spin_button_set_update_policy:
2253 * @spin_button: a #GtkSpinButton
2254 * @policy: a #GtkSpinButtonUpdatePolicy value
2256 * Sets the update behavior of a spin button.
2257 * This determines wether the spin button is always updated
2258 * or only when a valid value is set.
2261 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2262 GtkSpinButtonUpdatePolicy policy)
2264 GtkSpinButtonPrivate *priv;
2266 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2268 priv = spin_button->priv;
2270 if (priv->update_policy != policy)
2272 priv->update_policy = policy;
2273 g_object_notify (G_OBJECT (spin_button), "update-policy");
2278 * gtk_spin_button_get_update_policy:
2279 * @spin_button: a #GtkSpinButton
2281 * Gets the update behavior of a spin button.
2282 * See gtk_spin_button_set_update_policy().
2284 * Return value: the current update policy
2286 GtkSpinButtonUpdatePolicy
2287 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2289 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2291 return spin_button->priv->update_policy;
2295 * gtk_spin_button_set_numeric:
2296 * @spin_button: a #GtkSpinButton
2297 * @numeric: flag indicating if only numeric entry is allowed
2299 * Sets the flag that determines if non-numeric text can be typed
2300 * into the spin button.
2303 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2306 GtkSpinButtonPrivate *priv;
2308 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2310 priv = spin_button->priv;
2312 numeric = numeric != FALSE;
2314 if (priv->numeric != numeric)
2316 priv->numeric = numeric;
2317 g_object_notify (G_OBJECT (spin_button), "numeric");
2322 * gtk_spin_button_get_numeric:
2323 * @spin_button: a #GtkSpinButton
2325 * Returns whether non-numeric text can be typed into the spin button.
2326 * See gtk_spin_button_set_numeric().
2328 * Return value: %TRUE if only numeric text can be entered
2331 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2333 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2335 return spin_button->priv->numeric;
2339 * gtk_spin_button_set_wrap:
2340 * @spin_button: a #GtkSpinButton
2341 * @wrap: a flag indicating if wrapping behavior is performed
2343 * Sets the flag that determines if a spin button value wraps
2344 * around to the opposite limit when the upper or lower limit
2345 * of the range is exceeded.
2348 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2351 GtkSpinButtonPrivate *priv;
2353 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2355 priv = spin_button->priv;
2357 wrap = wrap != FALSE;
2359 if (priv->wrap != wrap)
2363 g_object_notify (G_OBJECT (spin_button), "wrap");
2368 * gtk_spin_button_get_wrap:
2369 * @spin_button: a #GtkSpinButton
2371 * Returns whether the spin button's value wraps around to the
2372 * opposite limit when the upper or lower limit of the range is
2373 * exceeded. See gtk_spin_button_set_wrap().
2375 * Return value: %TRUE if the spin button wraps around
2378 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2380 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2382 return spin_button->priv->wrap;
2386 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2388 const PangoFontDescription *font_desc;
2389 GtkStyleContext *context;
2393 /* FIXME: use getter */
2394 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
2395 font_desc = gtk_style_context_get_font (context, 0);
2397 size = pango_font_description_get_size (font_desc);
2398 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2400 return arrow_size - arrow_size % 2; /* force even */
2404 * gtk_spin_button_set_snap_to_ticks:
2405 * @spin_button: a #GtkSpinButton
2406 * @snap_to_ticks: a flag indicating if invalid values should be corrected
2408 * Sets the policy as to whether values are corrected to the
2409 * nearest step increment when a spin button is activated after
2410 * providing an invalid value.
2413 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2414 gboolean snap_to_ticks)
2416 GtkSpinButtonPrivate *priv;
2419 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2421 priv = spin_button->priv;
2423 new_val = (snap_to_ticks != 0);
2425 if (new_val != priv->snap_to_ticks)
2427 priv->snap_to_ticks = new_val;
2428 if (new_val && gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
2429 gtk_spin_button_update (spin_button);
2431 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2436 * gtk_spin_button_get_snap_to_ticks:
2437 * @spin_button: a #GtkSpinButton
2439 * Returns whether the values are corrected to the nearest step.
2440 * See gtk_spin_button_set_snap_to_ticks().
2442 * Return value: %TRUE if values are snapped to the nearest step
2445 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2447 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2449 return spin_button->priv->snap_to_ticks;
2453 * gtk_spin_button_spin:
2454 * @spin_button: a #GtkSpinButton
2455 * @direction: a #GtkSpinType indicating the direction to spin
2456 * @increment: step increment to apply in the specified direction
2458 * Increment or decrement a spin button's value in a specified
2459 * direction by a specified amount.
2462 gtk_spin_button_spin (GtkSpinButton *spin_button,
2463 GtkSpinType direction,
2466 GtkSpinButtonPrivate *priv;
2467 GtkAdjustment *adjustment;
2470 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2472 priv = spin_button->priv;
2474 adjustment = priv->adjustment;
2476 /* for compatibility with the 1.0.x version of this function */
2477 if (increment != 0 && increment != gtk_adjustment_get_step_increment (adjustment) &&
2478 (direction == GTK_SPIN_STEP_FORWARD ||
2479 direction == GTK_SPIN_STEP_BACKWARD))
2481 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2482 increment = -increment;
2483 direction = GTK_SPIN_USER_DEFINED;
2488 case GTK_SPIN_STEP_FORWARD:
2490 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_step_increment (adjustment));
2493 case GTK_SPIN_STEP_BACKWARD:
2495 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_step_increment (adjustment));
2498 case GTK_SPIN_PAGE_FORWARD:
2500 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_page_increment (adjustment));
2503 case GTK_SPIN_PAGE_BACKWARD:
2505 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_page_increment (adjustment));
2510 diff = gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment);
2512 gtk_spin_button_real_spin (spin_button, -diff);
2517 diff = gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_value (adjustment);
2519 gtk_spin_button_real_spin (spin_button, diff);
2522 case GTK_SPIN_USER_DEFINED:
2525 gtk_spin_button_real_spin (spin_button, increment);
2534 * gtk_spin_button_update:
2535 * @spin_button: a #GtkSpinButton
2537 * Manually force an update of the spin button.
2540 gtk_spin_button_update (GtkSpinButton *spin_button)
2542 GtkSpinButtonPrivate *priv;
2547 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2549 priv = spin_button->priv;
2552 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2553 if (return_val == FALSE)
2555 return_val = gtk_spin_button_default_input (spin_button, &val);
2556 error = (return_val == GTK_INPUT_ERROR);
2558 else if (return_val == GTK_INPUT_ERROR)
2561 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2563 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2565 if (val < gtk_adjustment_get_lower (priv->adjustment))
2566 val = gtk_adjustment_get_lower (priv->adjustment);
2567 else if (val > gtk_adjustment_get_upper (priv->adjustment))
2568 val = gtk_adjustment_get_upper (priv->adjustment);
2570 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2572 val < gtk_adjustment_get_lower (priv->adjustment) ||
2573 val > gtk_adjustment_get_upper (priv->adjustment)))
2575 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2579 if (priv->snap_to_ticks)
2580 gtk_spin_button_snap (spin_button, val);
2582 gtk_spin_button_set_value (spin_button, val);
2586 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2588 return spin_button->priv->panel;