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);
1001 if (arrow_type == GTK_ARROW_UP)
1002 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOP);
1004 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BOTTOM);
1006 priv = spin_button->priv;
1007 widget = GTK_WIDGET (spin_button);
1008 junction = gtk_style_context_get_junction_sides (context);
1010 panel_height = gdk_window_get_height (priv->panel);
1012 if (spin_button_at_limit (spin_button, arrow_type))
1013 state = GTK_STATE_FLAG_INSENSITIVE;
1016 if (priv->click_child == arrow_type)
1017 state = GTK_STATE_ACTIVE;
1020 if (priv->in_child == arrow_type &&
1021 priv->click_child == NO_ARROW)
1022 state = GTK_STATE_FLAG_PRELIGHT;
1024 state = gtk_widget_get_state_flags (widget);
1028 /* first, draw the background and the frame */
1029 if (arrow_type == GTK_ARROW_UP)
1034 junction |= GTK_JUNCTION_BOTTOM;
1036 else if (arrow_type == GTK_ARROW_DOWN)
1039 y = panel_height / 2.0;
1041 junction |= GTK_JUNCTION_TOP;
1044 gtk_style_context_set_junction_sides (context, junction);
1045 gtk_style_context_set_state (context, state);
1047 height = panel_height / 2.0;
1048 width = gdk_window_get_width (priv->panel);
1049 gtk_render_background (context, cr,
1050 x, y, width, height);
1051 gtk_render_frame (context, cr,
1052 x, y, width, height);
1054 /* make the actual rendered arrow smaller than text size */
1055 size = spin_button_get_arrow_size (spin_button);
1056 size = MIN (size, width);
1059 x = (width - size) / 2.0;
1061 if (arrow_type == GTK_ARROW_UP)
1063 y = (height - size / 2.0) / 2.0;
1066 else if (arrow_type == GTK_ARROW_DOWN)
1068 y = height + ((height - size / 2.0) / 2.0) - size / 2.0;
1072 gtk_render_arrow (context, cr,
1075 gtk_style_context_restore (context);
1079 gtk_spin_button_enter_notify (GtkWidget *widget,
1080 GdkEventCrossing *event)
1082 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1083 GtkSpinButtonPrivate *priv = spin->priv;
1084 GtkRequisition requisition;
1087 if (event->window == priv->panel)
1093 device = gdk_event_get_device ((GdkEvent *) event);
1094 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
1096 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1097 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1099 if (y <= req_height / 2)
1100 priv->in_child = GTK_ARROW_UP;
1102 priv->in_child = GTK_ARROW_DOWN;
1104 gtk_widget_queue_draw (GTK_WIDGET (spin));
1107 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1111 gtk_spin_button_leave_notify (GtkWidget *widget,
1112 GdkEventCrossing *event)
1114 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1115 GtkSpinButtonPrivate *priv = spin->priv;
1117 if (priv->in_child != NO_ARROW)
1119 priv->in_child = NO_ARROW;
1120 gtk_widget_queue_draw (GTK_WIDGET (spin));
1123 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1127 gtk_spin_button_focus_out (GtkWidget *widget,
1128 GdkEventFocus *event)
1130 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1131 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1133 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1137 gtk_spin_button_grab_notify (GtkWidget *widget,
1138 gboolean was_grabbed)
1140 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1144 if (gtk_spin_button_stop_spinning (spin))
1145 gtk_widget_queue_draw (GTK_WIDGET (spin));
1150 gtk_spin_button_state_flags_changed (GtkWidget *widget,
1151 GtkStateFlags previous_state)
1153 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1155 if (!gtk_widget_is_sensitive (widget))
1157 if (gtk_spin_button_stop_spinning (spin))
1158 gtk_widget_queue_draw (GTK_WIDGET (spin));
1163 gtk_spin_button_style_updated (GtkWidget *widget)
1165 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1166 GtkSpinButtonPrivate *priv = spin->priv;
1168 if (gtk_widget_get_realized (widget))
1170 GtkStyleContext *context;
1172 context = gtk_widget_get_style_context (widget);
1173 gtk_style_context_set_background (context, priv->panel);
1176 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_updated (widget);
1181 gtk_spin_button_scroll (GtkWidget *widget,
1182 GdkEventScroll *event)
1184 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1185 GtkSpinButtonPrivate *priv = spin->priv;
1187 if (event->direction == GDK_SCROLL_UP)
1189 if (!gtk_widget_has_focus (widget))
1190 gtk_widget_grab_focus (widget);
1191 gtk_spin_button_real_spin (spin, gtk_adjustment_get_step_increment (priv->adjustment));
1193 else if (event->direction == GDK_SCROLL_DOWN)
1195 if (!gtk_widget_has_focus (widget))
1196 gtk_widget_grab_focus (widget);
1197 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_step_increment (priv->adjustment));
1206 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1208 GtkSpinButtonPrivate *priv = spin->priv;
1209 gboolean did_spin = FALSE;
1213 g_source_remove (priv->timer);
1215 priv->need_timer = FALSE;
1221 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1222 priv->timer_calls = 0;
1224 priv->click_child = NO_ARROW;
1230 start_spinning (GtkSpinButton *spin,
1231 GtkArrowType click_child,
1234 GtkSpinButtonPrivate *priv;
1236 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1240 priv->click_child = click_child;
1244 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1247 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1249 priv->timer_step = step;
1250 priv->need_timer = TRUE;
1251 priv->timer = gdk_threads_add_timeout (timeout,
1252 (GSourceFunc) gtk_spin_button_timer,
1255 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1257 gtk_widget_queue_draw (GTK_WIDGET (spin));
1261 gtk_spin_button_button_press (GtkWidget *widget,
1262 GdkEventButton *event)
1264 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1265 GtkSpinButtonPrivate *priv = spin->priv;
1269 if (event->window == priv->panel)
1271 GtkRequisition requisition;
1274 if (!gtk_widget_has_focus (widget))
1275 gtk_widget_grab_focus (widget);
1276 priv->button = event->button;
1278 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1279 gtk_spin_button_update (spin);
1281 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1282 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1284 if (event->y <= req_height / 2)
1286 if (event->button == 1)
1287 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_step_increment (priv->adjustment));
1288 else if (event->button == 2)
1289 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_page_increment (priv->adjustment));
1291 priv->click_child = GTK_ARROW_UP;
1295 if (event->button == 1)
1296 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_step_increment (priv->adjustment));
1297 else if (event->button == 2)
1298 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_page_increment (priv->adjustment));
1300 priv->click_child = GTK_ARROW_DOWN;
1305 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1311 gtk_spin_button_button_release (GtkWidget *widget,
1312 GdkEventButton *event)
1314 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1315 GtkSpinButtonPrivate *priv = spin->priv;
1318 arrow_size = spin_button_get_arrow_size (spin);
1320 if (event->button == priv->button)
1322 int click_child = priv->click_child;
1324 gtk_spin_button_stop_spinning (spin);
1326 if (event->button == 3)
1328 GtkRequisition requisition;
1330 GtkStyleContext *context;
1331 GtkStateFlags state;
1334 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1335 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1337 context = gtk_widget_get_style_context (widget);
1338 state = gtk_widget_get_state_flags (widget);
1339 gtk_style_context_get_padding (context, state, &padding);
1341 if (event->y >= 0 && event->x >= 0 &&
1342 event->y <= req_height &&
1343 event->x <= arrow_size + padding.left + padding.right)
1345 if (click_child == GTK_ARROW_UP &&
1346 event->y <= req_height / 2)
1350 diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1352 gtk_spin_button_real_spin (spin, diff);
1354 else if (click_child == GTK_ARROW_DOWN &&
1355 event->y > req_height / 2)
1359 diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1361 gtk_spin_button_real_spin (spin, -diff);
1365 gtk_widget_queue_draw (GTK_WIDGET (spin));
1370 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1374 gtk_spin_button_motion_notify (GtkWidget *widget,
1375 GdkEventMotion *event)
1377 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1378 GtkSpinButtonPrivate *priv = spin->priv;
1383 if (event->window == priv->panel)
1385 GtkRequisition requisition;
1389 gdk_event_request_motions (event);
1391 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1392 req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1394 if (y <= req_height / 2 &&
1395 priv->in_child == GTK_ARROW_DOWN)
1397 priv->in_child = GTK_ARROW_UP;
1398 gtk_widget_queue_draw (GTK_WIDGET (spin));
1400 else if (y > req_height / 2 &&
1401 priv->in_child == GTK_ARROW_UP)
1403 priv->in_child = GTK_ARROW_DOWN;
1404 gtk_widget_queue_draw (GTK_WIDGET (spin));
1410 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1414 gtk_spin_button_timer (GtkSpinButton *spin_button)
1416 GtkSpinButtonPrivate *priv = spin_button->priv;
1417 gboolean retval = FALSE;
1421 if (priv->click_child == GTK_ARROW_UP)
1422 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1424 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1426 if (priv->need_timer)
1428 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1431 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1433 priv->need_timer = FALSE;
1434 priv->timer = gdk_threads_add_timeout (timeout,
1435 (GSourceFunc) gtk_spin_button_timer,
1436 (gpointer) spin_button);
1440 if (priv->climb_rate > 0.0 && priv->timer_step
1441 < gtk_adjustment_get_page_increment (priv->adjustment))
1443 if (priv->timer_calls < MAX_TIMER_CALLS)
1444 priv->timer_calls++;
1447 priv->timer_calls = 0;
1448 priv->timer_step += priv->climb_rate;
1459 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1460 GtkSpinButton *spin_button)
1462 gboolean return_val;
1464 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1467 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1468 if (return_val == FALSE)
1469 gtk_spin_button_default_output (spin_button);
1471 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1473 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1475 g_object_notify (G_OBJECT (spin_button), "value");
1479 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1480 GtkScrollType scroll)
1482 GtkSpinButtonPrivate *priv = spin->priv;
1485 /* When the key binding is activated, there may be an outstanding
1486 * value, so we first have to commit what is currently written in
1487 * the spin buttons text entry. See #106574
1489 gtk_spin_button_update (spin);
1491 old_value = gtk_adjustment_get_value (priv->adjustment);
1493 /* We don't test whether the entry is editable, since
1494 * this key binding conceptually corresponds to changing
1495 * the value with the buttons using the mouse, which
1496 * we allow for non-editable spin buttons.
1500 case GTK_SCROLL_STEP_BACKWARD:
1501 case GTK_SCROLL_STEP_DOWN:
1502 case GTK_SCROLL_STEP_LEFT:
1503 gtk_spin_button_real_spin (spin, -priv->timer_step);
1505 if (priv->climb_rate > 0.0 && priv->timer_step
1506 < gtk_adjustment_get_page_increment (priv->adjustment))
1508 if (priv->timer_calls < MAX_TIMER_CALLS)
1509 priv->timer_calls++;
1512 priv->timer_calls = 0;
1513 priv->timer_step += priv->climb_rate;
1518 case GTK_SCROLL_STEP_FORWARD:
1519 case GTK_SCROLL_STEP_UP:
1520 case GTK_SCROLL_STEP_RIGHT:
1521 gtk_spin_button_real_spin (spin, priv->timer_step);
1523 if (priv->climb_rate > 0.0 && priv->timer_step
1524 < gtk_adjustment_get_page_increment (priv->adjustment))
1526 if (priv->timer_calls < MAX_TIMER_CALLS)
1527 priv->timer_calls++;
1530 priv->timer_calls = 0;
1531 priv->timer_step += priv->climb_rate;
1536 case GTK_SCROLL_PAGE_BACKWARD:
1537 case GTK_SCROLL_PAGE_DOWN:
1538 case GTK_SCROLL_PAGE_LEFT:
1539 gtk_spin_button_real_spin (spin, -gtk_adjustment_get_page_increment (priv->adjustment));
1542 case GTK_SCROLL_PAGE_FORWARD:
1543 case GTK_SCROLL_PAGE_UP:
1544 case GTK_SCROLL_PAGE_RIGHT:
1545 gtk_spin_button_real_spin (spin, gtk_adjustment_get_page_increment (priv->adjustment));
1548 case GTK_SCROLL_START:
1550 gdouble diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1552 gtk_spin_button_real_spin (spin, -diff);
1556 case GTK_SCROLL_END:
1558 gdouble diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1560 gtk_spin_button_real_spin (spin, diff);
1565 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1569 gtk_spin_button_update (spin);
1571 if (gtk_adjustment_get_value (priv->adjustment) == old_value)
1572 gtk_widget_error_bell (GTK_WIDGET (spin));
1576 gtk_spin_button_key_release (GtkWidget *widget,
1579 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1580 GtkSpinButtonPrivate *priv = spin->priv;
1582 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1583 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1584 priv->timer_calls = 0;
1590 gtk_spin_button_snap (GtkSpinButton *spin_button,
1593 GtkSpinButtonPrivate *priv = spin_button->priv;
1597 inc = gtk_adjustment_get_step_increment (priv->adjustment);
1601 tmp = (val - gtk_adjustment_get_lower (priv->adjustment)) / inc;
1602 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1603 val = gtk_adjustment_get_lower (priv->adjustment) + floor (tmp) * inc;
1605 val = gtk_adjustment_get_lower (priv->adjustment) + ceil (tmp) * inc;
1607 gtk_spin_button_set_value (spin_button, val);
1611 gtk_spin_button_activate (GtkEntry *entry)
1613 if (gtk_editable_get_editable (GTK_EDITABLE (entry)))
1614 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1616 /* Chain up so that entry->activates_default is honored */
1617 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1621 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1627 GtkStyleContext *context;
1628 GtkStateFlags state;
1634 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1636 widget = GTK_WIDGET (entry);
1637 state = gtk_widget_get_state_flags (widget);
1638 context = gtk_widget_get_style_context (widget);
1639 gtk_style_context_get_padding (context, state, &padding);
1641 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1642 panel_width = arrow_size + padding.left + padding.right;
1645 *width -= panel_width;
1647 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1652 gtk_spin_button_insert_text (GtkEditable *editable,
1653 const gchar *new_text,
1654 gint new_text_length,
1657 GtkEntry *entry = GTK_ENTRY (editable);
1658 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1659 GtkSpinButtonPrivate *priv = spin->priv;
1660 GtkEditableInterface *parent_editable_iface;
1662 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1674 const gchar *entry_text;
1676 entry_length = gtk_entry_get_text_length (entry);
1677 entry_text = gtk_entry_get_text (entry);
1681 if (*(lc->negative_sign))
1682 neg_sign = *(lc->negative_sign);
1686 if (*(lc->positive_sign))
1687 pos_sign = *(lc->positive_sign);
1692 /* Workaround for bug caused by some Windows application messing
1693 * up the positive sign of the current locale, more specifically
1694 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1695 * See bug #330743 and for instance
1696 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1698 * I don't know if the positive sign always gets bogusly set to
1699 * a digit when the above Registry value is corrupted as
1700 * described. (In my test case, it got set to "8", and in the
1701 * bug report above it presumably was set ot "0".) Probably it
1702 * might get set to almost anything? So how to distinguish a
1703 * bogus value from some correct one for some locale? That is
1704 * probably hard, but at least we should filter out the
1707 if (pos_sign >= '0' && pos_sign <= '9')
1711 for (sign=0, i=0; i<entry_length; i++)
1712 if ((entry_text[i] == neg_sign) ||
1713 (entry_text[i] == pos_sign))
1719 if (sign && !(*position))
1722 for (dotpos=-1, i=0; i<entry_length; i++)
1723 if (entry_text[i] == *(lc->decimal_point))
1729 if (dotpos > -1 && *position > dotpos &&
1730 (gint)priv->digits - entry_length
1731 + dotpos - new_text_length + 1 < 0)
1734 for (i = 0; i < new_text_length; i++)
1736 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1738 if (sign || (*position) || i)
1742 else if (new_text[i] == *(lc->decimal_point))
1744 if (!priv->digits || dotpos > -1 ||
1745 (new_text_length - 1 - i + entry_length
1746 - *position > (gint)priv->digits))
1748 dotpos = *position + i;
1750 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1755 parent_editable_iface->insert_text (editable, new_text,
1756 new_text_length, position);
1760 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1763 GtkSpinButtonPrivate *priv = spin_button->priv;
1764 GtkAdjustment *adjustment;
1765 gdouble new_value = 0.0;
1766 gboolean wrapped = FALSE;
1768 adjustment = priv->adjustment;
1770 new_value = gtk_adjustment_get_value (adjustment) + increment;
1776 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_upper (adjustment)) < EPSILON)
1778 new_value = gtk_adjustment_get_lower (adjustment);
1781 else if (new_value > gtk_adjustment_get_upper (adjustment))
1782 new_value = gtk_adjustment_get_upper (adjustment);
1785 new_value = MIN (new_value, gtk_adjustment_get_upper (adjustment));
1787 else if (increment < 0)
1791 if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment)) < EPSILON)
1793 new_value = gtk_adjustment_get_upper (adjustment);
1796 else if (new_value < gtk_adjustment_get_lower (adjustment))
1797 new_value = gtk_adjustment_get_lower (adjustment);
1800 new_value = MAX (new_value, gtk_adjustment_get_lower (adjustment));
1803 if (fabs (new_value - gtk_adjustment_get_value (adjustment)) > EPSILON)
1804 gtk_adjustment_set_value (adjustment, new_value);
1807 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1809 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1813 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1818 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1820 return GTK_INPUT_ERROR;
1826 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1828 GtkSpinButtonPrivate *priv = spin_button->priv;
1830 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, gtk_adjustment_get_value (priv->adjustment));
1832 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1833 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1839 /***********************************************************
1840 ***********************************************************
1841 *** Public interface ***
1842 ***********************************************************
1843 ***********************************************************/
1847 * gtk_spin_button_configure:
1848 * @spin_button: a #GtkSpinButton
1849 * @adjustment: (allow-none): a #GtkAdjustment
1850 * @climb_rate: the new climb rate
1851 * @digits: the number of decimal places to display in the spin button
1853 * Changes the properties of an existing spin button. The adjustment,
1854 * climb rate, and number of decimal places are all changed accordingly,
1855 * after this function call.
1858 gtk_spin_button_configure (GtkSpinButton *spin_button,
1859 GtkAdjustment *adjustment,
1863 GtkSpinButtonPrivate *priv;
1865 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1867 priv = spin_button->priv;
1870 gtk_spin_button_set_adjustment (spin_button, adjustment);
1872 adjustment = priv->adjustment;
1874 g_object_freeze_notify (G_OBJECT (spin_button));
1875 if (priv->digits != digits)
1877 priv->digits = digits;
1878 g_object_notify (G_OBJECT (spin_button), "digits");
1881 if (priv->climb_rate != climb_rate)
1883 priv->climb_rate = climb_rate;
1884 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1886 g_object_thaw_notify (G_OBJECT (spin_button));
1888 gtk_adjustment_value_changed (adjustment);
1892 * gtk_spin_button_new:
1893 * @adjustment: (allow-none): the #GtkAdjustment object that this spin
1894 * button should use, or %NULL
1895 * @climb_rate: specifies how much the spin button changes when an arrow
1897 * @digits: the number of decimal places to display
1899 * Creates a new #GtkSpinButton.
1901 * Returns: The new spin button as a #GtkWidget
1904 gtk_spin_button_new (GtkAdjustment *adjustment,
1908 GtkSpinButton *spin;
1911 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1913 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1915 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1917 return GTK_WIDGET (spin);
1921 * gtk_spin_button_new_with_range:
1922 * @min: Minimum allowable value
1923 * @max: Maximum allowable value
1924 * @step: Increment added or subtracted by spinning the widget
1926 * This is a convenience constructor that allows creation of a numeric
1927 * #GtkSpinButton without manually creating an adjustment. The value is
1928 * initially set to the minimum value and a page increment of 10 * @step
1929 * is the default. The precision of the spin button is equivalent to the
1930 * precision of @step.
1932 * Note that the way in which the precision is derived works best if @step
1933 * is a power of ten. If the resulting precision is not suitable for your
1934 * needs, use gtk_spin_button_set_digits() to correct it.
1936 * Return value: The new spin button as a #GtkWidget
1939 gtk_spin_button_new_with_range (gdouble min,
1943 GtkAdjustment *adjustment;
1944 GtkSpinButton *spin;
1947 g_return_val_if_fail (min <= max, NULL);
1948 g_return_val_if_fail (step != 0.0, NULL);
1950 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1952 adjustment = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1954 if (fabs (step) >= 1.0 || step == 0.0)
1957 digits = abs ((gint) floor (log10 (fabs (step))));
1958 if (digits > MAX_DIGITS)
1959 digits = MAX_DIGITS;
1962 gtk_spin_button_configure (spin, adjustment, step, digits);
1964 gtk_spin_button_set_numeric (spin, TRUE);
1966 return GTK_WIDGET (spin);
1969 /* Callback used when the spin button's adjustment changes.
1970 * We need to redraw the arrows when the adjustment's range
1971 * changes, and reevaluate our size request.
1974 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1976 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1977 GtkSpinButtonPrivate *priv = spin_button->priv;
1979 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1980 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1984 * gtk_spin_button_set_adjustment:
1985 * @spin_button: a #GtkSpinButton
1986 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1988 * Replaces the #GtkAdjustment associated with @spin_button.
1991 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1992 GtkAdjustment *adjustment)
1994 GtkSpinButtonPrivate *priv;
1996 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1998 priv = spin_button->priv;
2000 if (priv->adjustment != adjustment)
2002 if (priv->adjustment)
2004 g_signal_handlers_disconnect_by_func (priv->adjustment,
2005 gtk_spin_button_value_changed,
2007 g_signal_handlers_disconnect_by_func (priv->adjustment,
2008 adjustment_changed_cb,
2010 g_object_unref (priv->adjustment);
2012 priv->adjustment = adjustment;
2015 g_object_ref_sink (adjustment);
2016 g_signal_connect (adjustment, "value-changed",
2017 G_CALLBACK (gtk_spin_button_value_changed),
2019 g_signal_connect (adjustment, "changed",
2020 G_CALLBACK (adjustment_changed_cb),
2022 priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
2025 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2028 g_object_notify (G_OBJECT (spin_button), "adjustment");
2032 * gtk_spin_button_get_adjustment:
2033 * @spin_button: a #GtkSpinButton
2035 * Get the adjustment associated with a #GtkSpinButton
2037 * Return value: (transfer none): the #GtkAdjustment of @spin_button
2040 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
2042 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
2044 return spin_button->priv->adjustment;
2048 * gtk_spin_button_set_digits:
2049 * @spin_button: a #GtkSpinButton
2050 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
2052 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
2056 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
2059 GtkSpinButtonPrivate *priv;
2061 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2063 priv = spin_button->priv;
2065 if (priv->digits != digits)
2067 priv->digits = digits;
2068 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2069 g_object_notify (G_OBJECT (spin_button), "digits");
2071 /* since lower/upper may have changed */
2072 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2077 * gtk_spin_button_get_digits:
2078 * @spin_button: a #GtkSpinButton
2080 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
2082 * Returns: the current precision
2085 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
2087 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2089 return spin_button->priv->digits;
2093 * gtk_spin_button_set_increments:
2094 * @spin_button: a #GtkSpinButton
2095 * @step: increment applied for a button 1 press.
2096 * @page: increment applied for a button 2 press.
2098 * Sets the step and page increments for spin_button. This affects how
2099 * quickly the value changes when the spin button's arrows are activated.
2102 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
2106 GtkSpinButtonPrivate *priv;
2108 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2110 priv = spin_button->priv;
2112 gtk_adjustment_configure (priv->adjustment,
2113 gtk_adjustment_get_value (priv->adjustment),
2114 gtk_adjustment_get_lower (priv->adjustment),
2115 gtk_adjustment_get_upper (priv->adjustment),
2118 gtk_adjustment_get_page_size (priv->adjustment));
2122 * gtk_spin_button_get_increments:
2123 * @spin_button: a #GtkSpinButton
2124 * @step: (out) (allow-none): location to store step increment, or %NULL
2125 * @page: (out) (allow-none): location to store page increment, or %NULL
2127 * Gets the current step and page the increments used by @spin_button. See
2128 * gtk_spin_button_set_increments().
2131 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
2135 GtkSpinButtonPrivate *priv;
2137 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2139 priv = spin_button->priv;
2142 *step = gtk_adjustment_get_step_increment (priv->adjustment);
2144 *page = gtk_adjustment_get_page_increment (priv->adjustment);
2148 * gtk_spin_button_set_range:
2149 * @spin_button: a #GtkSpinButton
2150 * @min: minimum allowable value
2151 * @max: maximum allowable value
2153 * Sets the minimum and maximum allowable values for @spin_button.
2155 * If the current value is outside this range, it will be adjusted
2156 * to fit within the range, otherwise it will remain unchanged.
2159 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2163 GtkAdjustment *adjustment;
2165 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2167 adjustment = spin_button->priv->adjustment;
2169 gtk_adjustment_configure (adjustment,
2170 CLAMP (gtk_adjustment_get_value (adjustment), min, max),
2173 gtk_adjustment_get_step_increment (adjustment),
2174 gtk_adjustment_get_page_increment (adjustment),
2175 gtk_adjustment_get_page_size (adjustment));
2179 * gtk_spin_button_get_range:
2180 * @spin_button: a #GtkSpinButton
2181 * @min: (out) (allow-none): location to store minimum allowed value, or %NULL
2182 * @max: (out) (allow-none): location to store maximum allowed value, or %NULL
2184 * Gets the range allowed for @spin_button.
2185 * See gtk_spin_button_set_range().
2188 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2192 GtkSpinButtonPrivate *priv;
2194 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2196 priv = spin_button->priv;
2199 *min = gtk_adjustment_get_lower (priv->adjustment);
2201 *max = gtk_adjustment_get_upper (priv->adjustment);
2205 * gtk_spin_button_get_value:
2206 * @spin_button: a #GtkSpinButton
2208 * Get the value in the @spin_button.
2210 * Return value: the value of @spin_button
2213 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2215 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2217 return gtk_adjustment_get_value (spin_button->priv->adjustment);
2221 * gtk_spin_button_get_value_as_int:
2222 * @spin_button: a #GtkSpinButton
2224 * Get the value @spin_button represented as an integer.
2226 * Return value: the value of @spin_button
2229 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2231 GtkSpinButtonPrivate *priv;
2234 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2236 priv = spin_button->priv;
2238 val = gtk_adjustment_get_value (priv->adjustment);
2239 if (val - floor (val) < ceil (val) - val)
2246 * gtk_spin_button_set_value:
2247 * @spin_button: a #GtkSpinButton
2248 * @value: the new value
2250 * Sets the value of @spin_button.
2253 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2256 GtkSpinButtonPrivate *priv;
2258 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2260 priv = spin_button->priv;
2262 if (fabs (value - gtk_adjustment_get_value (priv->adjustment)) > EPSILON)
2263 gtk_adjustment_set_value (priv->adjustment, value);
2266 gint return_val = FALSE;
2267 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2268 if (return_val == FALSE)
2269 gtk_spin_button_default_output (spin_button);
2274 * gtk_spin_button_set_update_policy:
2275 * @spin_button: a #GtkSpinButton
2276 * @policy: a #GtkSpinButtonUpdatePolicy value
2278 * Sets the update behavior of a spin button.
2279 * This determines wether the spin button is always updated
2280 * or only when a valid value is set.
2283 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2284 GtkSpinButtonUpdatePolicy policy)
2286 GtkSpinButtonPrivate *priv;
2288 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2290 priv = spin_button->priv;
2292 if (priv->update_policy != policy)
2294 priv->update_policy = policy;
2295 g_object_notify (G_OBJECT (spin_button), "update-policy");
2300 * gtk_spin_button_get_update_policy:
2301 * @spin_button: a #GtkSpinButton
2303 * Gets the update behavior of a spin button.
2304 * See gtk_spin_button_set_update_policy().
2306 * Return value: the current update policy
2308 GtkSpinButtonUpdatePolicy
2309 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2311 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2313 return spin_button->priv->update_policy;
2317 * gtk_spin_button_set_numeric:
2318 * @spin_button: a #GtkSpinButton
2319 * @numeric: flag indicating if only numeric entry is allowed
2321 * Sets the flag that determines if non-numeric text can be typed
2322 * into the spin button.
2325 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2328 GtkSpinButtonPrivate *priv;
2330 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2332 priv = spin_button->priv;
2334 numeric = numeric != FALSE;
2336 if (priv->numeric != numeric)
2338 priv->numeric = numeric;
2339 g_object_notify (G_OBJECT (spin_button), "numeric");
2344 * gtk_spin_button_get_numeric:
2345 * @spin_button: a #GtkSpinButton
2347 * Returns whether non-numeric text can be typed into the spin button.
2348 * See gtk_spin_button_set_numeric().
2350 * Return value: %TRUE if only numeric text can be entered
2353 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2355 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2357 return spin_button->priv->numeric;
2361 * gtk_spin_button_set_wrap:
2362 * @spin_button: a #GtkSpinButton
2363 * @wrap: a flag indicating if wrapping behavior is performed
2365 * Sets the flag that determines if a spin button value wraps
2366 * around to the opposite limit when the upper or lower limit
2367 * of the range is exceeded.
2370 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2373 GtkSpinButtonPrivate *priv;
2375 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2377 priv = spin_button->priv;
2379 wrap = wrap != FALSE;
2381 if (priv->wrap != wrap)
2385 g_object_notify (G_OBJECT (spin_button), "wrap");
2390 * gtk_spin_button_get_wrap:
2391 * @spin_button: a #GtkSpinButton
2393 * Returns whether the spin button's value wraps around to the
2394 * opposite limit when the upper or lower limit of the range is
2395 * exceeded. See gtk_spin_button_set_wrap().
2397 * Return value: %TRUE if the spin button wraps around
2400 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2402 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2404 return spin_button->priv->wrap;
2408 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2410 const PangoFontDescription *font_desc;
2411 GtkStyleContext *context;
2415 /* FIXME: use getter */
2416 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
2417 font_desc = gtk_style_context_get_font (context, 0);
2419 size = pango_font_description_get_size (font_desc);
2420 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2422 return arrow_size - arrow_size % 2; /* force even */
2426 * gtk_spin_button_set_snap_to_ticks:
2427 * @spin_button: a #GtkSpinButton
2428 * @snap_to_ticks: a flag indicating if invalid values should be corrected
2430 * Sets the policy as to whether values are corrected to the
2431 * nearest step increment when a spin button is activated after
2432 * providing an invalid value.
2435 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2436 gboolean snap_to_ticks)
2438 GtkSpinButtonPrivate *priv;
2441 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2443 priv = spin_button->priv;
2445 new_val = (snap_to_ticks != 0);
2447 if (new_val != priv->snap_to_ticks)
2449 priv->snap_to_ticks = new_val;
2450 if (new_val && gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
2451 gtk_spin_button_update (spin_button);
2453 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2458 * gtk_spin_button_get_snap_to_ticks:
2459 * @spin_button: a #GtkSpinButton
2461 * Returns whether the values are corrected to the nearest step.
2462 * See gtk_spin_button_set_snap_to_ticks().
2464 * Return value: %TRUE if values are snapped to the nearest step
2467 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2469 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2471 return spin_button->priv->snap_to_ticks;
2475 * gtk_spin_button_spin:
2476 * @spin_button: a #GtkSpinButton
2477 * @direction: a #GtkSpinType indicating the direction to spin
2478 * @increment: step increment to apply in the specified direction
2480 * Increment or decrement a spin button's value in a specified
2481 * direction by a specified amount.
2484 gtk_spin_button_spin (GtkSpinButton *spin_button,
2485 GtkSpinType direction,
2488 GtkSpinButtonPrivate *priv;
2489 GtkAdjustment *adjustment;
2492 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2494 priv = spin_button->priv;
2496 adjustment = priv->adjustment;
2498 /* for compatibility with the 1.0.x version of this function */
2499 if (increment != 0 && increment != gtk_adjustment_get_step_increment (adjustment) &&
2500 (direction == GTK_SPIN_STEP_FORWARD ||
2501 direction == GTK_SPIN_STEP_BACKWARD))
2503 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2504 increment = -increment;
2505 direction = GTK_SPIN_USER_DEFINED;
2510 case GTK_SPIN_STEP_FORWARD:
2512 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_step_increment (adjustment));
2515 case GTK_SPIN_STEP_BACKWARD:
2517 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_step_increment (adjustment));
2520 case GTK_SPIN_PAGE_FORWARD:
2522 gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_page_increment (adjustment));
2525 case GTK_SPIN_PAGE_BACKWARD:
2527 gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_page_increment (adjustment));
2532 diff = gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment);
2534 gtk_spin_button_real_spin (spin_button, -diff);
2539 diff = gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_value (adjustment);
2541 gtk_spin_button_real_spin (spin_button, diff);
2544 case GTK_SPIN_USER_DEFINED:
2547 gtk_spin_button_real_spin (spin_button, increment);
2556 * gtk_spin_button_update:
2557 * @spin_button: a #GtkSpinButton
2559 * Manually force an update of the spin button.
2562 gtk_spin_button_update (GtkSpinButton *spin_button)
2564 GtkSpinButtonPrivate *priv;
2569 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2571 priv = spin_button->priv;
2574 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2575 if (return_val == FALSE)
2577 return_val = gtk_spin_button_default_input (spin_button, &val);
2578 error = (return_val == GTK_INPUT_ERROR);
2580 else if (return_val == GTK_INPUT_ERROR)
2583 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2585 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2587 if (val < gtk_adjustment_get_lower (priv->adjustment))
2588 val = gtk_adjustment_get_lower (priv->adjustment);
2589 else if (val > gtk_adjustment_get_upper (priv->adjustment))
2590 val = gtk_adjustment_get_upper (priv->adjustment);
2592 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2594 val < gtk_adjustment_get_lower (priv->adjustment) ||
2595 val > gtk_adjustment_get_upper (priv->adjustment)))
2597 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2601 if (priv->snap_to_ticks)
2602 gtk_spin_button_snap (spin_button, val);
2604 gtk_spin_button_set_value (spin_button, val);
2608 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2610 return spin_button->priv->panel;