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/.
36 #include "gdk/gdkkeysyms.h"
37 #include "gtkbindings.h"
38 #include "gtkspinbutton.h"
39 #include "gtkentryprivate.h"
41 #include "gtkmarshalers.h"
42 #include "gtksettings.h"
43 #include "gtkprivate.h"
47 #define MIN_SPIN_BUTTON_WIDTH 30
48 #define MAX_TIMER_CALLS 5
51 #define MIN_ARROW_WIDTH 6
76 static void gtk_spin_button_editable_init (GtkEditableClass *iface);
77 static void gtk_spin_button_finalize (GObject *object);
78 static void gtk_spin_button_destroy (GtkObject *object);
79 static void gtk_spin_button_set_property (GObject *object,
83 static void gtk_spin_button_get_property (GObject *object,
87 static void gtk_spin_button_map (GtkWidget *widget);
88 static void gtk_spin_button_unmap (GtkWidget *widget);
89 static void gtk_spin_button_realize (GtkWidget *widget);
90 static void gtk_spin_button_unrealize (GtkWidget *widget);
91 static void gtk_spin_button_size_request (GtkWidget *widget,
92 GtkRequisition *requisition);
93 static void gtk_spin_button_size_allocate (GtkWidget *widget,
94 GtkAllocation *allocation);
95 static gint gtk_spin_button_expose (GtkWidget *widget,
96 GdkEventExpose *event);
97 static gint gtk_spin_button_button_press (GtkWidget *widget,
98 GdkEventButton *event);
99 static gint gtk_spin_button_button_release (GtkWidget *widget,
100 GdkEventButton *event);
101 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
102 GdkEventMotion *event);
103 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
104 GdkEventCrossing *event);
105 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
106 GdkEventCrossing *event);
107 static gint gtk_spin_button_focus_out (GtkWidget *widget,
108 GdkEventFocus *event);
109 static void gtk_spin_button_grab_notify (GtkWidget *widget,
110 gboolean was_grabbed);
111 static void gtk_spin_button_state_changed (GtkWidget *widget,
112 GtkStateType previous_state);
113 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
114 GtkArrowType arrow_type);
115 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
116 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
117 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
118 GtkSpinButton *spin_button);
119 static gint gtk_spin_button_key_release (GtkWidget *widget,
121 static gint gtk_spin_button_scroll (GtkWidget *widget,
122 GdkEventScroll *event);
123 static void gtk_spin_button_activate (GtkEntry *entry);
124 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
126 static void gtk_spin_button_insert_text (GtkEditable *editable,
127 const gchar *new_text,
128 gint new_text_length,
130 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
132 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
133 GtkScrollType scroll);
135 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
137 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
139 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
140 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
141 static void spin_button_redraw (GtkSpinButton *spin_button);
144 static guint spinbutton_signals[LAST_SIGNAL] = {0};
148 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
149 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
150 gtk_spin_button_editable_init))
152 #define add_spin_binding(binding_set, keyval, mask, scroll) \
153 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
155 GTK_TYPE_SCROLL_TYPE, scroll)
158 gtk_spin_button_class_init (GtkSpinButtonClass *class)
160 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
161 GtkObjectClass *object_class;
162 GtkWidgetClass *widget_class;
163 GtkEntryClass *entry_class;
164 GtkBindingSet *binding_set;
166 object_class = (GtkObjectClass*) class;
167 widget_class = (GtkWidgetClass*) class;
168 entry_class = (GtkEntryClass*) class;
170 gobject_class->finalize = gtk_spin_button_finalize;
172 gobject_class->set_property = gtk_spin_button_set_property;
173 gobject_class->get_property = gtk_spin_button_get_property;
175 object_class->destroy = gtk_spin_button_destroy;
177 widget_class->map = gtk_spin_button_map;
178 widget_class->unmap = gtk_spin_button_unmap;
179 widget_class->realize = gtk_spin_button_realize;
180 widget_class->unrealize = gtk_spin_button_unrealize;
181 widget_class->size_request = gtk_spin_button_size_request;
182 widget_class->size_allocate = gtk_spin_button_size_allocate;
183 widget_class->expose_event = gtk_spin_button_expose;
184 widget_class->scroll_event = gtk_spin_button_scroll;
185 widget_class->button_press_event = gtk_spin_button_button_press;
186 widget_class->button_release_event = gtk_spin_button_button_release;
187 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
188 widget_class->key_release_event = gtk_spin_button_key_release;
189 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
190 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
191 widget_class->focus_out_event = gtk_spin_button_focus_out;
192 widget_class->grab_notify = gtk_spin_button_grab_notify;
193 widget_class->state_changed = gtk_spin_button_state_changed;
195 entry_class->activate = gtk_spin_button_activate;
198 class->output = NULL;
199 class->change_value = gtk_spin_button_real_change_value;
201 g_object_class_install_property (gobject_class,
203 g_param_spec_object ("adjustment",
205 P_("The adjustment that holds the value of the spinbutton"),
207 GTK_PARAM_READWRITE));
209 g_object_class_install_property (gobject_class,
211 g_param_spec_double ("climb-rate",
213 P_("The acceleration rate when you hold down a button"),
217 GTK_PARAM_READWRITE));
219 g_object_class_install_property (gobject_class,
221 g_param_spec_uint ("digits",
223 P_("The number of decimal places to display"),
227 GTK_PARAM_READWRITE));
229 g_object_class_install_property (gobject_class,
231 g_param_spec_boolean ("snap-to-ticks",
233 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
235 GTK_PARAM_READWRITE));
237 g_object_class_install_property (gobject_class,
239 g_param_spec_boolean ("numeric",
241 P_("Whether non-numeric characters should be ignored"),
243 GTK_PARAM_READWRITE));
245 g_object_class_install_property (gobject_class,
247 g_param_spec_boolean ("wrap",
249 P_("Whether a spin button should wrap upon reaching its limits"),
251 GTK_PARAM_READWRITE));
253 g_object_class_install_property (gobject_class,
255 g_param_spec_enum ("update-policy",
257 P_("Whether the spin button should update always, or only when the value is legal"),
258 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
260 GTK_PARAM_READWRITE));
262 g_object_class_install_property (gobject_class,
264 g_param_spec_double ("value",
266 P_("Reads the current value, or sets a new value"),
270 GTK_PARAM_READWRITE));
272 gtk_widget_class_install_style_property_parser (widget_class,
273 g_param_spec_enum ("shadow-type",
275 P_("Style of bevel around the spin button"),
276 GTK_TYPE_SHADOW_TYPE,
279 gtk_rc_property_parse_enum);
280 spinbutton_signals[INPUT] =
281 g_signal_new (I_("input"),
282 G_TYPE_FROM_CLASS (gobject_class),
284 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
286 _gtk_marshal_INT__POINTER,
290 spinbutton_signals[OUTPUT] =
291 g_signal_new (I_("output"),
292 G_TYPE_FROM_CLASS (gobject_class),
294 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
295 _gtk_boolean_handled_accumulator, NULL,
296 _gtk_marshal_BOOLEAN__VOID,
299 spinbutton_signals[VALUE_CHANGED] =
300 g_signal_new (I_("value_changed"),
301 G_TYPE_FROM_CLASS (gobject_class),
303 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
305 _gtk_marshal_VOID__VOID,
309 * GtkSpinButton::wrapped:
310 * @spinbutton: the object which received the signal
312 * The wrapped signal is emitted right after the spinbutton wraps
313 * from its maximum to minimum value or vice-versa.
317 spinbutton_signals[WRAPPED] =
318 g_signal_new (I_("wrapped"),
319 G_TYPE_FROM_CLASS (gobject_class),
321 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
323 _gtk_marshal_VOID__VOID,
327 spinbutton_signals[CHANGE_VALUE] =
328 g_signal_new (I_("change_value"),
329 G_TYPE_FROM_CLASS (gobject_class),
330 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
331 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
333 _gtk_marshal_VOID__ENUM,
335 GTK_TYPE_SCROLL_TYPE);
337 binding_set = gtk_binding_set_by_class (class);
339 add_spin_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
340 add_spin_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
341 add_spin_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
342 add_spin_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
343 add_spin_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
344 add_spin_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
345 add_spin_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
346 add_spin_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
350 gtk_spin_button_editable_init (GtkEditableClass *iface)
352 iface->insert_text = gtk_spin_button_insert_text;
356 gtk_spin_button_set_property (GObject *object,
361 GtkSpinButton *spin_button;
363 spin_button = GTK_SPIN_BUTTON (object);
367 GtkAdjustment *adjustment;
369 case PROP_ADJUSTMENT:
370 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
372 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
373 gtk_spin_button_set_adjustment (spin_button, adjustment);
375 case PROP_CLIMB_RATE:
376 gtk_spin_button_configure (spin_button,
377 spin_button->adjustment,
378 g_value_get_double (value),
379 spin_button->digits);
382 gtk_spin_button_configure (spin_button,
383 spin_button->adjustment,
384 spin_button->climb_rate,
385 g_value_get_uint (value));
387 case PROP_SNAP_TO_TICKS:
388 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
391 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
394 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
396 case PROP_UPDATE_POLICY:
397 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
400 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
408 gtk_spin_button_get_property (GObject *object,
413 GtkSpinButton *spin_button;
415 spin_button = GTK_SPIN_BUTTON (object);
419 case PROP_ADJUSTMENT:
420 g_value_set_object (value, spin_button->adjustment);
422 case PROP_CLIMB_RATE:
423 g_value_set_double (value, spin_button->climb_rate);
426 g_value_set_uint (value, spin_button->digits);
428 case PROP_SNAP_TO_TICKS:
429 g_value_set_boolean (value, spin_button->snap_to_ticks);
432 g_value_set_boolean (value, spin_button->numeric);
435 g_value_set_boolean (value, spin_button->wrap);
437 case PROP_UPDATE_POLICY:
438 g_value_set_enum (value, spin_button->update_policy);
441 g_value_set_double (value, spin_button->adjustment->value);
444 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
450 gtk_spin_button_init (GtkSpinButton *spin_button)
452 spin_button->adjustment = NULL;
453 spin_button->panel = NULL;
454 spin_button->timer = 0;
455 spin_button->climb_rate = 0.0;
456 spin_button->timer_step = 0.0;
457 spin_button->update_policy = GTK_UPDATE_ALWAYS;
458 spin_button->in_child = NO_ARROW;
459 spin_button->click_child = NO_ARROW;
460 spin_button->button = 0;
461 spin_button->need_timer = FALSE;
462 spin_button->timer_calls = 0;
463 spin_button->digits = 0;
464 spin_button->numeric = FALSE;
465 spin_button->wrap = FALSE;
466 spin_button->snap_to_ticks = FALSE;
468 gtk_spin_button_set_adjustment (spin_button,
469 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
473 gtk_spin_button_finalize (GObject *object)
475 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
477 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
481 gtk_spin_button_destroy (GtkObject *object)
483 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
485 GTK_OBJECT_CLASS (gtk_spin_button_parent_class)->destroy (object);
489 gtk_spin_button_map (GtkWidget *widget)
491 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
493 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
494 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
499 gtk_spin_button_unmap (GtkWidget *widget)
501 if (GTK_WIDGET_MAPPED (widget))
503 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
505 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
506 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
511 gtk_spin_button_realize (GtkWidget *widget)
513 GtkSpinButton *spin_button;
514 GdkWindowAttr attributes;
515 gint attributes_mask;
520 spin_button = GTK_SPIN_BUTTON (widget);
521 arrow_size = spin_button_get_arrow_size (spin_button);
523 real_width = widget->allocation.width;
524 widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
525 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
526 GDK_KEY_RELEASE_MASK);
527 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
529 widget->allocation.width = real_width;
531 attributes.window_type = GDK_WINDOW_CHILD;
532 attributes.wclass = GDK_INPUT_OUTPUT;
533 attributes.visual = gtk_widget_get_visual (widget);
534 attributes.colormap = gtk_widget_get_colormap (widget);
535 attributes.event_mask = gtk_widget_get_events (widget);
536 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
537 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
538 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
540 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
542 attributes.x = (widget->allocation.x +
543 widget->allocation.width - arrow_size -
544 2 * widget->style->xthickness);
545 attributes.y = widget->allocation.y + (widget->allocation.height -
546 widget->requisition.height) / 2;
547 attributes.width = arrow_size + 2 * widget->style->xthickness;
548 attributes.height = widget->requisition.height;
550 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
551 &attributes, attributes_mask);
552 gdk_window_set_user_data (spin_button->panel, widget);
554 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
557 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
558 if (return_val == FALSE)
559 gtk_spin_button_default_output (spin_button);
561 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
565 gtk_spin_button_unrealize (GtkWidget *widget)
567 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
569 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
573 gdk_window_set_user_data (spin->panel, NULL);
574 gdk_window_destroy (spin->panel);
580 compute_double_length (double val, int digits)
586 if (fabs (val) > 1.0)
587 a = floor (log10 (fabs (val))) + 1;
599 return a + digits + extra;
603 gtk_spin_button_size_request (GtkWidget *widget,
604 GtkRequisition *requisition)
607 GtkSpinButton *spin_button;
610 entry = GTK_ENTRY (widget);
611 spin_button = GTK_SPIN_BUTTON (widget);
612 arrow_size = spin_button_get_arrow_size (spin_button);
614 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_request (widget, requisition);
616 if (entry->width_chars < 0)
618 PangoContext *context;
619 PangoFontMetrics *metrics;
625 gboolean interior_focus;
627 gint xborder, yborder;
628 GtkBorder inner_border;
630 gtk_widget_style_get (widget,
631 "interior-focus", &interior_focus,
632 "focus-line-width", &focus_width,
635 context = gtk_widget_get_pango_context (widget);
636 metrics = pango_context_get_metrics (context,
637 widget->style->font_desc,
638 pango_context_get_language (context));
640 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
641 digit_width = PANGO_SCALE *
642 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
644 pango_font_metrics_unref (metrics);
646 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
648 width = MIN_SPIN_BUTTON_WIDTH;
649 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
650 spin_button->digits));
652 string_len = compute_double_length (spin_button->adjustment->upper,
653 spin_button->digits);
654 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
655 width = MAX (width, w);
656 string_len = compute_double_length (spin_button->adjustment->lower,
657 spin_button->digits);
658 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
659 width = MAX (width, w);
661 _gtk_entry_get_borders (entry, &xborder, &yborder);
662 _gtk_entry_effective_inner_border (entry, &inner_border);
664 requisition->width = width + xborder * 2 + inner_border.left + inner_border.right;
667 requisition->width += arrow_size + 2 * widget->style->xthickness;
671 gtk_spin_button_size_allocate (GtkWidget *widget,
672 GtkAllocation *allocation)
675 GtkAllocation entry_allocation;
676 GtkAllocation panel_allocation;
680 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
681 g_return_if_fail (allocation != NULL);
683 spin = GTK_SPIN_BUTTON (widget);
684 arrow_size = spin_button_get_arrow_size (spin);
685 panel_width = arrow_size + 2 * widget->style->xthickness;
687 widget->allocation = *allocation;
689 entry_allocation = *allocation;
690 entry_allocation.width -= panel_width;
692 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
694 entry_allocation.x += panel_width;
695 panel_allocation.x = allocation->x;
699 panel_allocation.x = allocation->x + allocation->width - panel_width;
702 panel_allocation.width = panel_width;
703 panel_allocation.height = MIN (widget->requisition.height, allocation->height);
705 panel_allocation.y = allocation->y + (allocation->height -
706 panel_allocation.height) / 2;
708 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, &entry_allocation);
710 if (GTK_WIDGET_REALIZED (widget))
712 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
715 panel_allocation.width,
716 panel_allocation.height);
719 spin_button_redraw (spin);
723 gtk_spin_button_expose (GtkWidget *widget,
724 GdkEventExpose *event)
728 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
729 g_return_val_if_fail (event != NULL, FALSE);
731 spin = GTK_SPIN_BUTTON (widget);
733 if (GTK_WIDGET_DRAWABLE (widget))
735 GtkShadowType shadow_type;
738 if (event->window != spin->panel)
739 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->expose_event (widget, event);
741 /* we redraw the panel even if it wasn't exposed. This is
742 * because spin->panel is not a child window of widget->window,
743 * so it will not be invalidated by eg. gtk_widget_queue_draw().
748 gdk_drawable_get_size (spin->panel, &rect.width, &rect.height);
750 shadow_type = spin_button_get_shadow_type (spin);
752 gdk_window_begin_paint_rect (spin->panel, &rect);
754 if (shadow_type != GTK_SHADOW_NONE)
756 gtk_paint_box (widget->style, spin->panel,
757 GTK_STATE_NORMAL, shadow_type,
758 NULL, widget, "spinbutton",
759 rect.x, rect.y, rect.width, rect.height);
762 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
763 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
765 gdk_window_end_paint (spin->panel);
772 spin_button_at_limit (GtkSpinButton *spin_button,
775 GtkArrowType effective_arrow;
777 if (spin_button->wrap)
780 if (spin_button->adjustment->step_increment > 0)
781 effective_arrow = arrow;
783 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
785 if (effective_arrow == GTK_ARROW_UP &&
786 (spin_button->adjustment->upper - spin_button->adjustment->value <= EPSILON))
789 if (effective_arrow == GTK_ARROW_DOWN &&
790 (spin_button->adjustment->value - spin_button->adjustment->lower <= EPSILON))
797 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
798 GtkArrowType arrow_type)
800 GtkStateType state_type;
801 GtkShadowType shadow_type;
809 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
810 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
812 widget = GTK_WIDGET (spin_button);
814 if (GTK_WIDGET_DRAWABLE (widget))
816 width = spin_button_get_arrow_size (spin_button) + 2 * widget->style->xthickness;
818 if (arrow_type == GTK_ARROW_UP)
823 height = widget->requisition.height / 2;
828 y = widget->requisition.height / 2;
830 height = (widget->requisition.height + 1) / 2;
833 if (spin_button_at_limit (spin_button, arrow_type))
835 shadow_type = GTK_SHADOW_OUT;
836 state_type = GTK_STATE_INSENSITIVE;
840 if (spin_button->click_child == arrow_type)
842 state_type = GTK_STATE_ACTIVE;
843 shadow_type = GTK_SHADOW_IN;
847 if (spin_button->in_child == arrow_type &&
848 spin_button->click_child == NO_ARROW)
850 state_type = GTK_STATE_PRELIGHT;
854 state_type = GTK_WIDGET_STATE (widget);
857 shadow_type = GTK_SHADOW_OUT;
861 gtk_paint_box (widget->style, spin_button->panel,
862 state_type, shadow_type,
864 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
865 x, y, width, height);
867 height = widget->requisition.height;
869 if (arrow_type == GTK_ARROW_DOWN)
872 height = height - y - 2;
877 height = height / 2 - 2;
882 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
888 w -= w % 2 - 1; /* force odd */
891 x += (width - w) / 2;
892 y += (height - h) / 2;
897 gtk_paint_arrow (widget->style, spin_button->panel,
898 state_type, shadow_type,
899 NULL, widget, "spinbutton",
901 x, y, width, height);
906 gtk_spin_button_enter_notify (GtkWidget *widget,
907 GdkEventCrossing *event)
909 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
911 if (event->window == spin->panel)
916 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
918 if (y <= widget->requisition.height / 2)
919 spin->in_child = GTK_ARROW_UP;
921 spin->in_child = GTK_ARROW_DOWN;
923 spin_button_redraw (spin);
930 gtk_spin_button_leave_notify (GtkWidget *widget,
931 GdkEventCrossing *event)
933 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
935 spin->in_child = NO_ARROW;
936 spin_button_redraw (spin);
942 gtk_spin_button_focus_out (GtkWidget *widget,
943 GdkEventFocus *event)
945 if (GTK_ENTRY (widget)->editable)
946 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
948 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
952 gtk_spin_button_grab_notify (GtkWidget *widget,
953 gboolean was_grabbed)
955 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
959 gtk_spin_button_stop_spinning (spin);
960 spin_button_redraw (spin);
965 gtk_spin_button_state_changed (GtkWidget *widget,
966 GtkStateType previous_state)
968 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
970 if (!GTK_WIDGET_IS_SENSITIVE (widget))
972 gtk_spin_button_stop_spinning (spin);
973 spin_button_redraw (spin);
978 gtk_spin_button_scroll (GtkWidget *widget,
979 GdkEventScroll *event)
981 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
983 if (event->direction == GDK_SCROLL_UP)
985 if (!GTK_WIDGET_HAS_FOCUS (widget))
986 gtk_widget_grab_focus (widget);
987 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
989 else if (event->direction == GDK_SCROLL_DOWN)
991 if (!GTK_WIDGET_HAS_FOCUS (widget))
992 gtk_widget_grab_focus (widget);
993 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
1002 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1006 g_source_remove (spin->timer);
1008 spin->timer_calls = 0;
1009 spin->need_timer = FALSE;
1014 spin->timer_step = spin->adjustment->step_increment;
1015 spin->timer_calls = 0;
1017 spin->click_child = NO_ARROW;
1022 start_spinning (GtkSpinButton *spin,
1023 GtkArrowType click_child,
1026 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1028 spin->click_child = click_child;
1032 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1035 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1037 spin->timer_step = step;
1038 spin->need_timer = TRUE;
1039 spin->timer = g_timeout_add (timeout,
1040 (GSourceFunc) gtk_spin_button_timer,
1043 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1045 spin_button_redraw (spin);
1049 gtk_spin_button_button_press (GtkWidget *widget,
1050 GdkEventButton *event)
1052 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1056 if (event->window == spin->panel)
1058 if (!GTK_WIDGET_HAS_FOCUS (widget))
1059 gtk_widget_grab_focus (widget);
1060 spin->button = event->button;
1062 if (GTK_ENTRY (widget)->editable)
1063 gtk_spin_button_update (spin);
1065 if (event->y <= widget->requisition.height / 2)
1067 if (event->button == 1)
1068 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->step_increment);
1069 else if (event->button == 2)
1070 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->page_increment);
1072 spin->click_child = GTK_ARROW_UP;
1076 if (event->button == 1)
1077 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->step_increment);
1078 else if (event->button == 2)
1079 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->page_increment);
1081 spin->click_child = GTK_ARROW_DOWN;
1086 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1092 gtk_spin_button_button_release (GtkWidget *widget,
1093 GdkEventButton *event)
1095 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1098 arrow_size = spin_button_get_arrow_size (spin);
1100 if (event->button == spin->button)
1102 int click_child = spin->click_child;
1104 gtk_spin_button_stop_spinning (spin);
1106 if (event->button == 3)
1108 if (event->y >= 0 && event->x >= 0 &&
1109 event->y <= widget->requisition.height &&
1110 event->x <= arrow_size + 2 * widget->style->xthickness)
1112 if (click_child == GTK_ARROW_UP &&
1113 event->y <= widget->requisition.height / 2)
1117 diff = spin->adjustment->upper - spin->adjustment->value;
1119 gtk_spin_button_real_spin (spin, diff);
1121 else if (click_child == GTK_ARROW_DOWN &&
1122 event->y > widget->requisition.height / 2)
1126 diff = spin->adjustment->value - spin->adjustment->lower;
1128 gtk_spin_button_real_spin (spin, -diff);
1132 spin_button_redraw (spin);
1137 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1141 gtk_spin_button_motion_notify (GtkWidget *widget,
1142 GdkEventMotion *event)
1144 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1149 if (event->window == spin->panel)
1153 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1155 if (y <= widget->requisition.height / 2 &&
1156 spin->in_child == GTK_ARROW_DOWN)
1158 spin->in_child = GTK_ARROW_UP;
1159 spin_button_redraw (spin);
1161 else if (y > widget->requisition.height / 2 &&
1162 spin->in_child == GTK_ARROW_UP)
1164 spin->in_child = GTK_ARROW_DOWN;
1165 spin_button_redraw (spin);
1171 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1175 gtk_spin_button_timer (GtkSpinButton *spin_button)
1177 gboolean retval = FALSE;
1179 GDK_THREADS_ENTER ();
1181 if (spin_button->timer)
1183 if (spin_button->click_child == GTK_ARROW_UP)
1184 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1186 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1188 if (spin_button->need_timer)
1190 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1193 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1195 spin_button->need_timer = FALSE;
1196 spin_button->timer = g_timeout_add (timeout,
1197 (GSourceFunc) gtk_spin_button_timer,
1198 (gpointer) spin_button);
1202 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1203 < spin_button->adjustment->page_increment)
1205 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1206 spin_button->timer_calls++;
1209 spin_button->timer_calls = 0;
1210 spin_button->timer_step += spin_button->climb_rate;
1217 GDK_THREADS_LEAVE ();
1223 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1224 GtkSpinButton *spin_button)
1226 gboolean return_val;
1228 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1231 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1232 if (return_val == FALSE)
1233 gtk_spin_button_default_output (spin_button);
1235 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1237 spin_button_redraw (spin_button);
1239 g_object_notify (G_OBJECT (spin_button), "value");
1243 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1244 GtkScrollType scroll)
1246 /* We don't test whether the entry is editable, since
1247 * this key binding conceptually corresponds to changing
1248 * the value with the buttons using the mouse, which
1249 * we allow for non-editable spin buttons.
1253 case GTK_SCROLL_STEP_BACKWARD:
1254 case GTK_SCROLL_STEP_DOWN:
1255 case GTK_SCROLL_STEP_LEFT:
1256 gtk_spin_button_real_spin (spin, -spin->timer_step);
1258 if (spin->climb_rate > 0.0 && spin->timer_step
1259 < spin->adjustment->page_increment)
1261 if (spin->timer_calls < MAX_TIMER_CALLS)
1262 spin->timer_calls++;
1265 spin->timer_calls = 0;
1266 spin->timer_step += spin->climb_rate;
1271 case GTK_SCROLL_STEP_FORWARD:
1272 case GTK_SCROLL_STEP_UP:
1273 case GTK_SCROLL_STEP_RIGHT:
1274 gtk_spin_button_real_spin (spin, spin->timer_step);
1276 if (spin->climb_rate > 0.0 && spin->timer_step
1277 < spin->adjustment->page_increment)
1279 if (spin->timer_calls < MAX_TIMER_CALLS)
1280 spin->timer_calls++;
1283 spin->timer_calls = 0;
1284 spin->timer_step += spin->climb_rate;
1289 case GTK_SCROLL_PAGE_BACKWARD:
1290 case GTK_SCROLL_PAGE_DOWN:
1291 case GTK_SCROLL_PAGE_LEFT:
1292 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1295 case GTK_SCROLL_PAGE_FORWARD:
1296 case GTK_SCROLL_PAGE_UP:
1297 case GTK_SCROLL_PAGE_RIGHT:
1298 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1301 case GTK_SCROLL_START:
1303 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1305 gtk_spin_button_real_spin (spin, -diff);
1309 case GTK_SCROLL_END:
1311 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1313 gtk_spin_button_real_spin (spin, diff);
1318 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1322 gtk_spin_button_update (spin);
1326 gtk_spin_button_key_release (GtkWidget *widget,
1329 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1331 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1332 spin->timer_step = spin->adjustment->step_increment;
1333 spin->timer_calls = 0;
1339 gtk_spin_button_snap (GtkSpinButton *spin_button,
1345 inc = spin_button->adjustment->step_increment;
1349 tmp = (val - spin_button->adjustment->lower) / inc;
1350 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1351 val = spin_button->adjustment->lower + floor (tmp) * inc;
1353 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1355 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1356 gtk_adjustment_set_value (spin_button->adjustment, val);
1359 gint return_val = FALSE;
1360 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1361 if (return_val == FALSE)
1362 gtk_spin_button_default_output (spin_button);
1367 gtk_spin_button_activate (GtkEntry *entry)
1369 if (entry->editable)
1370 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1372 /* Chain up so that entry->activates_default is honored */
1373 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1377 gtk_spin_button_insert_text (GtkEditable *editable,
1378 const gchar *new_text,
1379 gint new_text_length,
1382 GtkEntry *entry = GTK_ENTRY (editable);
1383 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1384 GtkEditableClass *parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class, GTK_TYPE_EDITABLE);
1396 entry_length = entry->text_length;
1400 if (*(lc->negative_sign))
1401 neg_sign = *(lc->negative_sign);
1405 if (*(lc->positive_sign))
1406 pos_sign = *(lc->positive_sign);
1410 for (sign=0, i=0; i<entry_length; i++)
1411 if ((entry->text[i] == neg_sign) ||
1412 (entry->text[i] == pos_sign))
1418 if (sign && !(*position))
1421 for (dotpos=-1, i=0; i<entry_length; i++)
1422 if (entry->text[i] == *(lc->decimal_point))
1428 if (dotpos > -1 && *position > dotpos &&
1429 (gint)spin->digits - entry_length
1430 + dotpos - new_text_length + 1 < 0)
1433 for (i = 0; i < new_text_length; i++)
1435 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1437 if (sign || (*position) || i)
1441 else if (new_text[i] == *(lc->decimal_point))
1443 if (!spin->digits || dotpos > -1 ||
1444 (new_text_length - 1 - i + entry_length
1445 - *position > (gint)spin->digits))
1447 dotpos = *position + i;
1449 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1454 parent_editable_iface->insert_text (editable, new_text,
1455 new_text_length, position);
1459 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1463 gdouble new_value = 0.0;
1464 gboolean wrapped = FALSE;
1466 adj = spin_button->adjustment;
1468 new_value = adj->value + increment;
1472 if (spin_button->wrap)
1474 if (fabs (adj->value - adj->upper) < EPSILON)
1476 new_value = adj->lower;
1479 else if (new_value > adj->upper)
1480 new_value = adj->upper;
1483 new_value = MIN (new_value, adj->upper);
1485 else if (increment < 0)
1487 if (spin_button->wrap)
1489 if (fabs (adj->value - adj->lower) < EPSILON)
1491 new_value = adj->upper;
1494 else if (new_value < adj->lower)
1495 new_value = adj->lower;
1498 new_value = MAX (new_value, adj->lower);
1501 if (fabs (new_value - adj->value) > EPSILON)
1502 gtk_adjustment_set_value (adj, new_value);
1505 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1507 spin_button_redraw (spin_button);
1511 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1516 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1518 return GTK_INPUT_ERROR;
1524 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1526 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1528 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1529 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1535 /***********************************************************
1536 ***********************************************************
1537 *** Public interface ***
1538 ***********************************************************
1539 ***********************************************************/
1543 gtk_spin_button_configure (GtkSpinButton *spin_button,
1544 GtkAdjustment *adjustment,
1548 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1551 gtk_spin_button_set_adjustment (spin_button, adjustment);
1553 adjustment = spin_button->adjustment;
1555 g_object_freeze_notify (G_OBJECT (spin_button));
1556 if (spin_button->digits != digits)
1558 spin_button->digits = digits;
1559 g_object_notify (G_OBJECT (spin_button), "digits");
1562 if (spin_button->climb_rate != climb_rate)
1564 spin_button->climb_rate = climb_rate;
1565 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1567 g_object_thaw_notify (G_OBJECT (spin_button));
1569 gtk_adjustment_value_changed (adjustment);
1573 gtk_spin_button_new (GtkAdjustment *adjustment,
1577 GtkSpinButton *spin;
1580 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1582 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1584 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1586 return GTK_WIDGET (spin);
1590 * gtk_spin_button_new_with_range:
1591 * @min: Minimum allowable value
1592 * @max: Maximum allowable value
1593 * @step: Increment added or subtracted by spinning the widget
1595 * This is a convenience constructor that allows creation of a numeric
1596 * #GtkSpinButton without manually creating an adjustment. The value is
1597 * initially set to the minimum value and a page increment of 10 * @step
1598 * is the default. The precision of the spin button is equivalent to the
1599 * precision of @step.
1601 * Note that the way in which the precision is derived works best if @step
1602 * is a power of ten. If the resulting precision is not suitable for your
1603 * needs, use gtk_spin_button_set_digits() to correct it.
1605 * Return value: The new spin button as a #GtkWidget.
1608 gtk_spin_button_new_with_range (gdouble min,
1613 GtkSpinButton *spin;
1616 g_return_val_if_fail (min <= max, NULL);
1617 g_return_val_if_fail (step != 0.0, NULL);
1619 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1621 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1623 if (fabs (step) >= 1.0 || step == 0.0)
1626 digits = abs ((gint) floor (log10 (fabs (step))));
1627 if (digits > MAX_DIGITS)
1628 digits = MAX_DIGITS;
1631 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1633 gtk_spin_button_set_numeric (spin, TRUE);
1635 return GTK_WIDGET (spin);
1638 /* Callback used when the spin button's adjustment changes. We need to redraw
1639 * the arrows when the adjustment's range changes, and reevaluate our size request.
1642 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1644 GtkSpinButton *spin_button;
1646 spin_button = GTK_SPIN_BUTTON (data);
1648 spin_button->timer_step = spin_button->adjustment->step_increment;
1649 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1653 * gtk_spin_button_set_adjustment:
1654 * @spin_button: a #GtkSpinButton
1655 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1657 * Replaces the #GtkAdjustment associated with @spin_button.
1660 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1661 GtkAdjustment *adjustment)
1663 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1665 if (spin_button->adjustment != adjustment)
1667 if (spin_button->adjustment)
1669 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1670 gtk_spin_button_value_changed,
1672 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1673 adjustment_changed_cb,
1675 g_object_unref (spin_button->adjustment);
1677 spin_button->adjustment = adjustment;
1680 g_object_ref_sink (adjustment);
1681 g_signal_connect (adjustment, "value_changed",
1682 G_CALLBACK (gtk_spin_button_value_changed),
1684 g_signal_connect (adjustment, "changed",
1685 G_CALLBACK (adjustment_changed_cb),
1687 spin_button->timer_step = spin_button->adjustment->step_increment;
1690 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1693 g_object_notify (G_OBJECT (spin_button), "adjustment");
1697 * gtk_spin_button_get_adjustment:
1698 * @spin_button: a #GtkSpinButton
1700 * Get the adjustment associated with a #GtkSpinButton
1702 * Return value: the #GtkAdjustment of @spin_button
1705 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1707 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1709 return spin_button->adjustment;
1713 * gtk_spin_button_set_digits:
1714 * @spin_button: a #GtkSpinButton
1715 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1717 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1721 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1724 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1726 if (spin_button->digits != digits)
1728 spin_button->digits = digits;
1729 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1730 g_object_notify (G_OBJECT (spin_button), "digits");
1732 /* since lower/upper may have changed */
1733 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1738 * gtk_spin_button_get_digits:
1739 * @spin_button: a #GtkSpinButton
1741 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1743 * Returns: the current precision
1746 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1748 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1750 return spin_button->digits;
1754 * gtk_spin_button_set_increments:
1755 * @spin_button: a #GtkSpinButton
1756 * @step: increment applied for a button 1 press.
1757 * @page: increment applied for a button 2 press.
1759 * Sets the step and page increments for spin_button. This affects how
1760 * quickly the value changes when the spin button's arrows are activated.
1763 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1767 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1769 spin_button->adjustment->step_increment = step;
1770 spin_button->adjustment->page_increment = page;
1774 * gtk_spin_button_get_increments:
1775 * @spin_button: a #GtkSpinButton
1776 * @step: location to store step increment, or %NULL
1777 * @page: location to store page increment, or %NULL
1779 * Gets the current step and page the increments used by @spin_button. See
1780 * gtk_spin_button_set_increments().
1783 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1787 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1790 *step = spin_button->adjustment->step_increment;
1792 *page = spin_button->adjustment->page_increment;
1796 * gtk_spin_button_set_range:
1797 * @spin_button: a #GtkSpinButton
1798 * @min: minimum allowable value
1799 * @max: maximum allowable value
1801 * Sets the minimum and maximum allowable values for @spin_button
1804 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1810 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1812 spin_button->adjustment->lower = min;
1813 spin_button->adjustment->upper = max;
1815 value = CLAMP (spin_button->adjustment->value,
1816 spin_button->adjustment->lower,
1817 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1819 if (value != spin_button->adjustment->value)
1820 gtk_spin_button_set_value (spin_button, value);
1822 gtk_adjustment_changed (spin_button->adjustment);
1826 * gtk_spin_button_get_range:
1827 * @spin_button: a #GtkSpinButton
1828 * @min: location to store minimum allowed value, or %NULL
1829 * @max: location to store maximum allowed value, or %NULL
1831 * Gets the range allowed for @spin_button. See
1832 * gtk_spin_button_set_range().
1835 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1839 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1842 *min = spin_button->adjustment->lower;
1844 *max = spin_button->adjustment->upper;
1848 * gtk_spin_button_get_value:
1849 * @spin_button: a #GtkSpinButton
1851 * Get the value in the @spin_button.
1853 * Return value: the value of @spin_button
1856 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1858 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1860 return spin_button->adjustment->value;
1864 * gtk_spin_button_get_value_as_int:
1865 * @spin_button: a #GtkSpinButton
1867 * Get the value @spin_button represented as an integer.
1869 * Return value: the value of @spin_button
1872 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1876 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1878 val = spin_button->adjustment->value;
1879 if (val - floor (val) < ceil (val) - val)
1886 * gtk_spin_button_set_value:
1887 * @spin_button: a #GtkSpinButton
1888 * @value: the new value
1890 * Set the value of @spin_button.
1893 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1896 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1898 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1899 gtk_adjustment_set_value (spin_button->adjustment, value);
1902 gint return_val = FALSE;
1903 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1904 if (return_val == FALSE)
1905 gtk_spin_button_default_output (spin_button);
1910 * gtk_spin_button_set_update_policy:
1911 * @spin_button: a #GtkSpinButton
1912 * @policy: a #GtkSpinButtonUpdatePolicy value
1914 * Sets the update behavior of a spin button. This determines whether the
1915 * spin button is always updated or only when a valid value is set.
1918 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1919 GtkSpinButtonUpdatePolicy policy)
1921 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1923 if (spin_button->update_policy != policy)
1925 spin_button->update_policy = policy;
1926 g_object_notify (G_OBJECT (spin_button), "update-policy");
1931 * gtk_spin_button_get_update_policy:
1932 * @spin_button: a #GtkSpinButton
1934 * Gets the update behavior of a spin button. See
1935 * gtk_spin_button_set_update_policy().
1937 * Return value: the current update policy
1939 GtkSpinButtonUpdatePolicy
1940 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1942 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1944 return spin_button->update_policy;
1948 * gtk_spin_button_set_numeric:
1949 * @spin_button: a #GtkSpinButton
1950 * @numeric: flag indicating if only numeric entry is allowed.
1952 * Sets the flag that determines if non-numeric text can be typed into
1956 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1959 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1961 numeric = numeric != FALSE;
1963 if (spin_button->numeric != numeric)
1965 spin_button->numeric = numeric;
1966 g_object_notify (G_OBJECT (spin_button), "numeric");
1971 * gtk_spin_button_get_numeric:
1972 * @spin_button: a #GtkSpinButton
1974 * Returns whether non-numeric text can be typed into the spin button.
1975 * See gtk_spin_button_set_numeric().
1977 * Return value: %TRUE if only numeric text can be entered
1980 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1982 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1984 return spin_button->numeric;
1988 * gtk_spin_button_set_wrap:
1989 * @spin_button: a #GtkSpinButton
1990 * @wrap: a flag indicating if wrapping behavior is performed.
1992 * Sets the flag that determines if a spin button value wraps around to the
1993 * opposite limit when the upper or lower limit of the range is exceeded.
1996 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1999 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2001 wrap = wrap != FALSE;
2003 if (spin_button->wrap != wrap)
2005 spin_button->wrap = (wrap != 0);
2007 g_object_notify (G_OBJECT (spin_button), "wrap");
2012 * gtk_spin_button_get_wrap:
2013 * @spin_button: a #GtkSpinButton
2015 * Returns whether the spin button's value wraps around to the
2016 * opposite limit when the upper or lower limit of the range is
2017 * exceeded. See gtk_spin_button_set_wrap().
2019 * Return value: %TRUE if the spin button wraps around
2022 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2024 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2026 return spin_button->wrap;
2030 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2032 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
2035 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2037 return arrow_size - arrow_size % 2; /* force even */
2041 * spin_button_get_shadow_type:
2042 * @spin_button: a #GtkSpinButton
2044 * Convenience function to Get the shadow type from the underlying widget's
2047 * Return value: the #GtkShadowType
2050 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2052 GtkShadowType rc_shadow_type;
2054 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow-type", &rc_shadow_type, NULL);
2056 return rc_shadow_type;
2060 * gtk_spin_button_set_snap_to_ticks:
2061 * @spin_button: a #GtkSpinButton
2062 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2064 * Sets the policy as to whether values are corrected to the nearest step
2065 * increment when a spin button is activated after providing an invalid value.
2068 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2069 gboolean snap_to_ticks)
2073 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2075 new_val = (snap_to_ticks != 0);
2077 if (new_val != spin_button->snap_to_ticks)
2079 spin_button->snap_to_ticks = new_val;
2080 if (new_val && GTK_ENTRY (spin_button)->editable)
2081 gtk_spin_button_update (spin_button);
2083 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2088 * gtk_spin_button_get_snap_to_ticks:
2089 * @spin_button: a #GtkSpinButton
2091 * Returns whether the values are corrected to the nearest step. See
2092 * gtk_spin_button_set_snap_to_ticks().
2094 * Return value: %TRUE if values are snapped to the nearest step.
2097 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2099 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2101 return spin_button->snap_to_ticks;
2105 * gtk_spin_button_spin:
2106 * @spin_button: a #GtkSpinButton
2107 * @direction: a #GtkSpinType indicating the direction to spin.
2108 * @increment: step increment to apply in the specified direction.
2110 * Increment or decrement a spin button's value in a specified direction
2111 * by a specified amount.
2114 gtk_spin_button_spin (GtkSpinButton *spin_button,
2115 GtkSpinType direction,
2121 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2123 adj = spin_button->adjustment;
2125 /* for compatibility with the 1.0.x version of this function */
2126 if (increment != 0 && increment != adj->step_increment &&
2127 (direction == GTK_SPIN_STEP_FORWARD ||
2128 direction == GTK_SPIN_STEP_BACKWARD))
2130 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2131 increment = -increment;
2132 direction = GTK_SPIN_USER_DEFINED;
2137 case GTK_SPIN_STEP_FORWARD:
2139 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2142 case GTK_SPIN_STEP_BACKWARD:
2144 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2147 case GTK_SPIN_PAGE_FORWARD:
2149 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2152 case GTK_SPIN_PAGE_BACKWARD:
2154 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2159 diff = adj->value - adj->lower;
2161 gtk_spin_button_real_spin (spin_button, -diff);
2166 diff = adj->upper - adj->value;
2168 gtk_spin_button_real_spin (spin_button, diff);
2171 case GTK_SPIN_USER_DEFINED:
2174 gtk_spin_button_real_spin (spin_button, increment);
2183 * gtk_spin_button_update:
2184 * @spin_button: a #GtkSpinButton
2186 * Manually force an update of the spin button.
2189 gtk_spin_button_update (GtkSpinButton *spin_button)
2195 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2198 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2199 if (return_val == FALSE)
2201 return_val = gtk_spin_button_default_input (spin_button, &val);
2202 error = (return_val == GTK_INPUT_ERROR);
2204 else if (return_val == GTK_INPUT_ERROR)
2207 spin_button_redraw (spin_button);
2209 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2211 if (val < spin_button->adjustment->lower)
2212 val = spin_button->adjustment->lower;
2213 else if (val > spin_button->adjustment->upper)
2214 val = spin_button->adjustment->upper;
2216 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2218 val < spin_button->adjustment->lower ||
2219 val > spin_button->adjustment->upper))
2221 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2225 if (spin_button->snap_to_ticks)
2226 gtk_spin_button_snap (spin_button, val);
2229 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2230 gtk_adjustment_set_value (spin_button->adjustment, val);
2234 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0,
2236 if (return_val == FALSE)
2237 gtk_spin_button_default_output (spin_button);
2243 spin_button_redraw (GtkSpinButton *spin_button)
2247 widget = GTK_WIDGET (spin_button);
2249 if (GTK_WIDGET_DRAWABLE (widget))
2251 gtk_widget_queue_draw (widget);
2253 /* We must invalidate the panel window ourselves, because it
2254 * is not a child of widget->window
2256 gdk_window_invalidate_rect (spin_button->panel, NULL, TRUE);
2260 #define __GTK_SPIN_BUTTON_C__
2261 #include "gtkaliasdef.c"