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/.
35 #include "gdk/gdkkeysyms.h"
36 #include "gtkbindings.h"
37 #include "gtkspinbutton.h"
39 #include "gtkmarshalers.h"
40 #include "gtksettings.h"
43 #define MIN_SPIN_BUTTON_WIDTH 30
44 #define SPIN_BUTTON_INITIAL_TIMER_DELAY 200
45 #define SPIN_BUTTON_TIMER_DELAY 20
46 #define MAX_TIMER_CALLS 5
49 #define MIN_ARROW_WIDTH 6
73 static void gtk_spin_button_class_init (GtkSpinButtonClass *klass);
74 static void gtk_spin_button_editable_init (GtkEditableClass *iface);
75 static void gtk_spin_button_init (GtkSpinButton *spin_button);
76 static void gtk_spin_button_finalize (GObject *object);
77 static void gtk_spin_button_destroy (GtkObject *object);
78 static void gtk_spin_button_set_property (GObject *object,
82 static void gtk_spin_button_get_property (GObject *object,
86 static void gtk_spin_button_map (GtkWidget *widget);
87 static void gtk_spin_button_unmap (GtkWidget *widget);
88 static void gtk_spin_button_realize (GtkWidget *widget);
89 static void gtk_spin_button_unrealize (GtkWidget *widget);
90 static void gtk_spin_button_size_request (GtkWidget *widget,
91 GtkRequisition *requisition);
92 static void gtk_spin_button_size_allocate (GtkWidget *widget,
93 GtkAllocation *allocation);
94 static gint gtk_spin_button_expose (GtkWidget *widget,
95 GdkEventExpose *event);
96 static gint gtk_spin_button_button_press (GtkWidget *widget,
97 GdkEventButton *event);
98 static gint gtk_spin_button_button_release (GtkWidget *widget,
99 GdkEventButton *event);
100 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
101 GdkEventMotion *event);
102 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
103 GdkEventCrossing *event);
104 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
105 GdkEventCrossing *event);
106 static gint gtk_spin_button_focus_out (GtkWidget *widget,
107 GdkEventFocus *event);
108 static void gtk_spin_button_grab_notify (GtkWidget *widget,
109 gboolean was_grabbed);
110 static void gtk_spin_button_state_changed (GtkWidget *widget,
111 GtkStateType previous_state);
112 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
113 GtkArrowType arrow_type);
114 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
115 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
116 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
117 GtkSpinButton *spin_button);
118 static gint gtk_spin_button_key_release (GtkWidget *widget,
120 static gint gtk_spin_button_scroll (GtkWidget *widget,
121 GdkEventScroll *event);
122 static void gtk_spin_button_activate (GtkEntry *entry);
123 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
125 static void gtk_spin_button_insert_text (GtkEditable *editable,
126 const gchar *new_text,
127 gint new_text_length,
129 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
131 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
132 GtkScrollType scroll);
134 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
136 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
138 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
139 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
140 static void spin_button_redraw (GtkSpinButton *spin_button);
143 static GtkEntryClass *parent_class = NULL;
144 static guint spinbutton_signals[LAST_SIGNAL] = {0};
149 gtk_spin_button_get_type (void)
151 static GType spin_button_type = 0;
153 if (!spin_button_type)
155 static const GTypeInfo spin_button_info =
157 sizeof (GtkSpinButtonClass),
158 NULL, /* base_init */
159 NULL, /* base_finalize */
160 (GClassInitFunc) gtk_spin_button_class_init,
161 NULL, /* class_finalize */
162 NULL, /* class_data */
163 sizeof (GtkSpinButton),
165 (GInstanceInitFunc) gtk_spin_button_init,
168 static const GInterfaceInfo editable_info =
170 (GInterfaceInitFunc) gtk_spin_button_editable_init, /* interface_init */
171 NULL, /* interface_finalize */
172 NULL /* interface_data */
176 g_type_register_static (GTK_TYPE_ENTRY, "GtkSpinButton",
177 &spin_button_info, 0);
179 g_type_add_interface_static (spin_button_type,
183 return spin_button_type;
186 #define add_spin_binding(binding_set, keyval, mask, scroll) \
187 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
189 GTK_TYPE_SCROLL_TYPE, scroll)
192 gtk_spin_button_class_init (GtkSpinButtonClass *class)
194 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
195 GtkObjectClass *object_class;
196 GtkWidgetClass *widget_class;
197 GtkEntryClass *entry_class;
198 GtkBindingSet *binding_set;
200 object_class = (GtkObjectClass*) class;
201 widget_class = (GtkWidgetClass*) class;
202 entry_class = (GtkEntryClass*) class;
204 parent_class = g_type_class_peek_parent (class);
206 gobject_class->finalize = gtk_spin_button_finalize;
208 gobject_class->set_property = gtk_spin_button_set_property;
209 gobject_class->get_property = gtk_spin_button_get_property;
211 object_class->destroy = gtk_spin_button_destroy;
213 widget_class->map = gtk_spin_button_map;
214 widget_class->unmap = gtk_spin_button_unmap;
215 widget_class->realize = gtk_spin_button_realize;
216 widget_class->unrealize = gtk_spin_button_unrealize;
217 widget_class->size_request = gtk_spin_button_size_request;
218 widget_class->size_allocate = gtk_spin_button_size_allocate;
219 widget_class->expose_event = gtk_spin_button_expose;
220 widget_class->scroll_event = gtk_spin_button_scroll;
221 widget_class->button_press_event = gtk_spin_button_button_press;
222 widget_class->button_release_event = gtk_spin_button_button_release;
223 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
224 widget_class->key_release_event = gtk_spin_button_key_release;
225 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
226 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
227 widget_class->focus_out_event = gtk_spin_button_focus_out;
228 widget_class->grab_notify = gtk_spin_button_grab_notify;
229 widget_class->state_changed = gtk_spin_button_state_changed;
231 entry_class->activate = gtk_spin_button_activate;
234 class->output = NULL;
235 class->change_value = gtk_spin_button_real_change_value;
237 g_object_class_install_property (gobject_class,
239 g_param_spec_object ("adjustment",
241 _("The adjustment that holds the value of the spinbutton"),
245 g_object_class_install_property (gobject_class,
247 g_param_spec_double ("climb_rate",
249 _("The acceleration rate when you hold down a button"),
255 g_object_class_install_property (gobject_class,
257 g_param_spec_uint ("digits",
259 _("The number of decimal places to display"),
265 g_object_class_install_property (gobject_class,
267 g_param_spec_boolean ("snap_to_ticks",
269 _("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
273 g_object_class_install_property (gobject_class,
275 g_param_spec_boolean ("numeric",
277 _("Whether non-numeric characters should be ignored"),
281 g_object_class_install_property (gobject_class,
283 g_param_spec_boolean ("wrap",
285 _("Whether a spin button should wrap upon reaching its limits"),
289 g_object_class_install_property (gobject_class,
291 g_param_spec_enum ("update_policy",
293 _("Whether the spin button should update always, or only when the value is legal"),
294 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
298 g_object_class_install_property (gobject_class,
300 g_param_spec_double ("value",
302 _("Reads the current value, or sets a new value"),
308 gtk_widget_class_install_style_property_parser (widget_class,
309 g_param_spec_enum ("shadow_type", "Shadow Type", NULL,
310 GTK_TYPE_SHADOW_TYPE,
313 gtk_rc_property_parse_enum);
314 spinbutton_signals[INPUT] =
315 g_signal_new ("input",
316 G_TYPE_FROM_CLASS (gobject_class),
318 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
320 _gtk_marshal_INT__POINTER,
324 spinbutton_signals[OUTPUT] =
325 g_signal_new ("output",
326 G_TYPE_FROM_CLASS (gobject_class),
328 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
329 _gtk_boolean_handled_accumulator, NULL,
330 _gtk_marshal_BOOLEAN__VOID,
333 spinbutton_signals[VALUE_CHANGED] =
334 g_signal_new ("value_changed",
335 G_TYPE_FROM_CLASS (gobject_class),
337 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
339 _gtk_marshal_VOID__VOID,
343 spinbutton_signals[CHANGE_VALUE] =
344 g_signal_new ("change_value",
345 G_TYPE_FROM_CLASS (gobject_class),
346 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
347 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
349 _gtk_marshal_VOID__ENUM,
351 GTK_TYPE_SCROLL_TYPE);
353 binding_set = gtk_binding_set_by_class (class);
355 add_spin_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
356 add_spin_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
357 add_spin_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
358 add_spin_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
359 add_spin_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
360 add_spin_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
361 add_spin_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
362 add_spin_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
366 gtk_spin_button_editable_init (GtkEditableClass *iface)
368 iface->insert_text = gtk_spin_button_insert_text;
372 gtk_spin_button_set_property (GObject *object,
377 GtkSpinButton *spin_button;
379 spin_button = GTK_SPIN_BUTTON (object);
383 GtkAdjustment *adjustment;
385 case PROP_ADJUSTMENT:
386 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
388 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
389 gtk_spin_button_set_adjustment (spin_button, adjustment);
391 case PROP_CLIMB_RATE:
392 gtk_spin_button_configure (spin_button,
393 spin_button->adjustment,
394 g_value_get_double (value),
395 spin_button->digits);
398 gtk_spin_button_configure (spin_button,
399 spin_button->adjustment,
400 spin_button->climb_rate,
401 g_value_get_uint (value));
403 case PROP_SNAP_TO_TICKS:
404 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
407 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
410 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
412 case PROP_UPDATE_POLICY:
413 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
416 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
424 gtk_spin_button_get_property (GObject *object,
429 GtkSpinButton *spin_button;
431 spin_button = GTK_SPIN_BUTTON (object);
435 case PROP_ADJUSTMENT:
436 g_value_set_object (value, spin_button->adjustment);
438 case PROP_CLIMB_RATE:
439 g_value_set_double (value, spin_button->climb_rate);
442 g_value_set_uint (value, spin_button->digits);
444 case PROP_SNAP_TO_TICKS:
445 g_value_set_boolean (value, spin_button->snap_to_ticks);
448 g_value_set_boolean (value, spin_button->numeric);
451 g_value_set_boolean (value, spin_button->wrap);
453 case PROP_UPDATE_POLICY:
454 g_value_set_enum (value, spin_button->update_policy);
457 g_value_set_double (value, spin_button->adjustment->value);
460 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
466 gtk_spin_button_init (GtkSpinButton *spin_button)
468 spin_button->adjustment = NULL;
469 spin_button->panel = NULL;
470 spin_button->timer = 0;
471 spin_button->climb_rate = 0.0;
472 spin_button->timer_step = 0.0;
473 spin_button->update_policy = GTK_UPDATE_ALWAYS;
474 spin_button->in_child = NO_ARROW;
475 spin_button->click_child = NO_ARROW;
476 spin_button->button = 0;
477 spin_button->need_timer = FALSE;
478 spin_button->timer_calls = 0;
479 spin_button->digits = 0;
480 spin_button->numeric = FALSE;
481 spin_button->wrap = FALSE;
482 spin_button->snap_to_ticks = FALSE;
484 gtk_spin_button_set_adjustment (spin_button,
485 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
489 gtk_spin_button_finalize (GObject *object)
491 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
493 G_OBJECT_CLASS (parent_class)->finalize (object);
497 gtk_spin_button_destroy (GtkObject *object)
499 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
501 GTK_OBJECT_CLASS (parent_class)->destroy (object);
505 gtk_spin_button_map (GtkWidget *widget)
507 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
509 GTK_WIDGET_CLASS (parent_class)->map (widget);
510 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
515 gtk_spin_button_unmap (GtkWidget *widget)
517 if (GTK_WIDGET_MAPPED (widget))
519 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
520 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
525 gtk_spin_button_realize (GtkWidget *widget)
527 GtkSpinButton *spin_button;
528 GdkWindowAttr attributes;
529 gint attributes_mask;
534 spin_button = GTK_SPIN_BUTTON (widget);
535 arrow_size = spin_button_get_arrow_size (spin_button);
537 real_width = widget->allocation.width;
538 widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
539 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
540 GDK_KEY_RELEASE_MASK);
541 GTK_WIDGET_CLASS (parent_class)->realize (widget);
543 widget->allocation.width = real_width;
545 attributes.window_type = GDK_WINDOW_CHILD;
546 attributes.wclass = GDK_INPUT_OUTPUT;
547 attributes.visual = gtk_widget_get_visual (widget);
548 attributes.colormap = gtk_widget_get_colormap (widget);
549 attributes.event_mask = gtk_widget_get_events (widget);
550 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
551 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
552 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
554 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
556 attributes.x = (widget->allocation.x +
557 widget->allocation.width - arrow_size -
558 2 * widget->style->xthickness);
559 attributes.y = widget->allocation.y + (widget->allocation.height -
560 widget->requisition.height) / 2;
561 attributes.width = arrow_size + 2 * widget->style->xthickness;
562 attributes.height = widget->requisition.height;
564 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
565 &attributes, attributes_mask);
566 gdk_window_set_user_data (spin_button->panel, widget);
568 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
571 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
572 if (return_val == FALSE)
573 gtk_spin_button_default_output (spin_button);
575 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
579 gtk_spin_button_unrealize (GtkWidget *widget)
581 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
583 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
587 gdk_window_set_user_data (spin->panel, NULL);
588 gdk_window_destroy (spin->panel);
594 compute_double_length (double val, int digits)
600 if (fabs (val) > 1.0)
601 a = floor (log10 (fabs (val))) + 1;
613 return a + digits + extra;
617 gtk_spin_button_size_request (GtkWidget *widget,
618 GtkRequisition *requisition)
621 GtkSpinButton *spin_button;
624 entry = GTK_ENTRY (widget);
625 spin_button = GTK_SPIN_BUTTON (widget);
626 arrow_size = spin_button_get_arrow_size (spin_button);
628 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
630 if (entry->width_chars < 0)
632 PangoContext *context;
633 PangoFontMetrics *metrics;
639 gboolean interior_focus;
642 gtk_widget_style_get (widget,
643 "interior-focus", &interior_focus,
644 "focus-line-width", &focus_width,
647 context = gtk_widget_get_pango_context (widget);
648 metrics = pango_context_get_metrics (context,
649 widget->style->font_desc,
650 pango_context_get_language (context));
652 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
653 digit_width = PANGO_PIXELS (digit_width);
655 pango_font_metrics_unref (metrics);
657 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
659 width = MIN_SPIN_BUTTON_WIDTH;
660 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
661 spin_button->digits));
663 string_len = compute_double_length (spin_button->adjustment->upper,
664 spin_button->digits);
665 w = MIN (string_len, max_string_len) * digit_width;
666 width = MAX (width, w);
667 string_len = compute_double_length (spin_button->adjustment->lower,
668 spin_button->digits);
669 w = MIN (string_len, max_string_len) * digit_width;
670 width = MAX (width, w);
672 requisition->width = width;
674 requisition->width += 2 * focus_width;
677 requisition->width += arrow_size + 2 * widget->style->xthickness;
681 gtk_spin_button_size_allocate (GtkWidget *widget,
682 GtkAllocation *allocation)
685 GtkAllocation entry_allocation;
686 GtkAllocation panel_allocation;
690 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
691 g_return_if_fail (allocation != NULL);
693 spin = GTK_SPIN_BUTTON (widget);
694 arrow_size = spin_button_get_arrow_size (spin);
695 panel_width = arrow_size + 2 * widget->style->xthickness;
697 widget->allocation = *allocation;
699 entry_allocation = *allocation;
700 entry_allocation.width -= panel_width;
702 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
704 entry_allocation.x += panel_width;
705 panel_allocation.x = allocation->x;
709 panel_allocation.x = allocation->x + allocation->width - panel_width;
712 panel_allocation.width = panel_width;
713 panel_allocation.height = MIN (widget->requisition.height, allocation->height);
715 panel_allocation.y = allocation->y + (allocation->height -
716 panel_allocation.height) / 2;
718 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &entry_allocation);
720 if (GTK_WIDGET_REALIZED (widget))
722 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
725 panel_allocation.width,
726 panel_allocation.height);
729 spin_button_redraw (spin);
733 gtk_spin_button_expose (GtkWidget *widget,
734 GdkEventExpose *event)
738 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
739 g_return_val_if_fail (event != NULL, FALSE);
741 spin = GTK_SPIN_BUTTON (widget);
743 if (GTK_WIDGET_DRAWABLE (widget))
745 GtkShadowType shadow_type;
748 if (event->window != spin->panel)
749 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
751 /* we redraw the panel even if it wasn't exposed. This is
752 * because spin->panel is not a child window of widget->window,
753 * so it will not be invalidated by eg. gtk_widget_queue_draw().
758 gdk_drawable_get_size (spin->panel, &rect.width, &rect.height);
760 shadow_type = spin_button_get_shadow_type (spin);
762 gdk_window_begin_paint_rect (spin->panel, &rect);
764 if (shadow_type != GTK_SHADOW_NONE)
766 gtk_paint_box (widget->style, spin->panel,
767 GTK_STATE_NORMAL, shadow_type,
768 NULL, widget, "spinbutton",
769 rect.x, rect.y, rect.width, rect.height);
772 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
773 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
775 gdk_window_end_paint (spin->panel);
782 spin_button_at_limit (GtkSpinButton *spin_button,
785 if (spin_button->wrap)
788 if (arrow == GTK_ARROW_UP &&
789 (spin_button->adjustment->upper - spin_button->adjustment->value <= EPSILON))
792 if (arrow == GTK_ARROW_DOWN &&
793 (spin_button->adjustment->value - spin_button->adjustment->lower <= EPSILON))
800 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
801 GtkArrowType arrow_type)
803 GtkStateType state_type;
804 GtkShadowType shadow_type;
812 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
813 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
815 widget = GTK_WIDGET (spin_button);
817 if (GTK_WIDGET_DRAWABLE (widget))
819 width = spin_button_get_arrow_size (spin_button) + 2 * widget->style->xthickness;
821 if (arrow_type == GTK_ARROW_UP)
826 height = widget->requisition.height / 2;
831 y = widget->requisition.height / 2;
833 height = (widget->requisition.height + 1) / 2;
836 if (spin_button_at_limit (spin_button, arrow_type))
838 shadow_type = GTK_SHADOW_OUT;
839 state_type = GTK_STATE_INSENSITIVE;
843 if (spin_button->click_child == arrow_type)
845 state_type = GTK_STATE_ACTIVE;
846 shadow_type = GTK_SHADOW_IN;
850 if (spin_button->in_child == arrow_type &&
851 spin_button->click_child == NO_ARROW)
853 state_type = GTK_STATE_PRELIGHT;
857 state_type = GTK_WIDGET_STATE (widget);
860 shadow_type = GTK_SHADOW_OUT;
864 gtk_paint_box (widget->style, spin_button->panel,
865 state_type, shadow_type,
867 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
868 x, y, width, height);
870 height = widget->requisition.height;
872 if (arrow_type == GTK_ARROW_DOWN)
875 height = height - y - 2;
880 height = height / 2 - 2;
885 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
891 w -= w % 2 - 1; /* force odd */
894 x += (width - w) / 2;
895 y += (height - h) / 2;
900 gtk_paint_arrow (widget->style, spin_button->panel,
901 state_type, shadow_type,
902 NULL, widget, "spinbutton",
904 x, y, width, height);
909 gtk_spin_button_enter_notify (GtkWidget *widget,
910 GdkEventCrossing *event)
912 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
914 if (event->window == spin->panel)
919 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
921 if (y <= widget->requisition.height / 2)
922 spin->in_child = GTK_ARROW_UP;
924 spin->in_child = GTK_ARROW_DOWN;
926 spin_button_redraw (spin);
933 gtk_spin_button_leave_notify (GtkWidget *widget,
934 GdkEventCrossing *event)
936 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
938 spin->in_child = NO_ARROW;
939 spin_button_redraw (spin);
945 gtk_spin_button_focus_out (GtkWidget *widget,
946 GdkEventFocus *event)
948 if (GTK_ENTRY (widget)->editable)
949 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
951 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
955 gtk_spin_button_grab_notify (GtkWidget *widget,
956 gboolean was_grabbed)
959 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
963 gtk_spin_button_state_changed (GtkWidget *widget,
964 GtkStateType previous_state)
966 if (!GTK_WIDGET_IS_SENSITIVE (widget))
967 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
971 gtk_spin_button_scroll (GtkWidget *widget,
972 GdkEventScroll *event)
974 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
976 if (event->direction == GDK_SCROLL_UP)
978 if (!GTK_WIDGET_HAS_FOCUS (widget))
979 gtk_widget_grab_focus (widget);
980 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
982 else if (event->direction == GDK_SCROLL_DOWN)
984 if (!GTK_WIDGET_HAS_FOCUS (widget))
985 gtk_widget_grab_focus (widget);
986 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
995 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
999 gtk_timeout_remove (spin->timer);
1001 spin->timer_calls = 0;
1002 spin->need_timer = FALSE;
1007 spin->timer_step = spin->adjustment->step_increment;
1008 spin->timer_calls = 0;
1012 start_spinning (GtkSpinButton *spin,
1013 GtkArrowType click_child,
1016 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1018 spin->click_child = click_child;
1019 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1023 spin->timer_step = step;
1024 spin->need_timer = TRUE;
1025 spin->timer = gtk_timeout_add (SPIN_BUTTON_INITIAL_TIMER_DELAY,
1026 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
1029 spin_button_redraw (spin);
1033 gtk_spin_button_button_press (GtkWidget *widget,
1034 GdkEventButton *event)
1036 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1040 if (event->window == spin->panel)
1042 if (!GTK_WIDGET_HAS_FOCUS (widget))
1043 gtk_widget_grab_focus (widget);
1044 spin->button = event->button;
1046 if (GTK_ENTRY (widget)->editable)
1047 gtk_spin_button_update (spin);
1049 if (event->y <= widget->requisition.height / 2)
1051 if (event->button == 1)
1052 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->step_increment);
1053 else if (event->button == 2)
1054 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->page_increment);
1056 spin->click_child = GTK_ARROW_UP;
1060 if (event->button == 1)
1061 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->step_increment);
1062 else if (event->button == 2)
1063 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->page_increment);
1065 spin->click_child = GTK_ARROW_DOWN;
1070 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1076 gtk_spin_button_button_release (GtkWidget *widget,
1077 GdkEventButton *event)
1079 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1082 arrow_size = spin_button_get_arrow_size (spin);
1084 if (event->button == spin->button)
1088 gtk_spin_button_stop_spinning (spin);
1090 if (event->button == 3)
1092 if (event->y >= 0 && event->x >= 0 &&
1093 event->y <= widget->requisition.height &&
1094 event->x <= arrow_size + 2 * widget->style->xthickness)
1096 if (spin->click_child == GTK_ARROW_UP &&
1097 event->y <= widget->requisition.height / 2)
1101 diff = spin->adjustment->upper - spin->adjustment->value;
1103 gtk_spin_button_real_spin (spin, diff);
1105 else if (spin->click_child == GTK_ARROW_DOWN &&
1106 event->y > widget->requisition.height / 2)
1110 diff = spin->adjustment->value - spin->adjustment->lower;
1112 gtk_spin_button_real_spin (spin, -diff);
1116 click_child = spin->click_child;
1117 spin->click_child = NO_ARROW;
1119 spin_button_redraw (spin);
1124 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1128 gtk_spin_button_motion_notify (GtkWidget *widget,
1129 GdkEventMotion *event)
1131 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1136 if (event->window == spin->panel)
1140 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1142 if (y <= widget->requisition.height / 2 &&
1143 spin->in_child == GTK_ARROW_DOWN)
1145 spin->in_child = GTK_ARROW_UP;
1146 spin_button_redraw (spin);
1148 else if (y > widget->requisition.height / 2 &&
1149 spin->in_child == GTK_ARROW_UP)
1151 spin->in_child = GTK_ARROW_DOWN;
1152 spin_button_redraw (spin);
1158 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1162 gtk_spin_button_timer (GtkSpinButton *spin_button)
1164 gboolean retval = FALSE;
1166 GDK_THREADS_ENTER ();
1168 if (spin_button->timer)
1170 if (spin_button->click_child == GTK_ARROW_UP)
1171 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1173 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1175 if (spin_button->need_timer)
1177 spin_button->need_timer = FALSE;
1178 spin_button->timer = gtk_timeout_add
1179 (SPIN_BUTTON_TIMER_DELAY, (GtkFunction) gtk_spin_button_timer,
1180 (gpointer) spin_button);
1184 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1185 < spin_button->adjustment->page_increment)
1187 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1188 spin_button->timer_calls++;
1191 spin_button->timer_calls = 0;
1192 spin_button->timer_step += spin_button->climb_rate;
1199 GDK_THREADS_LEAVE ();
1205 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1206 GtkSpinButton *spin_button)
1208 gboolean return_val;
1210 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1213 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1214 if (return_val == FALSE)
1215 gtk_spin_button_default_output (spin_button);
1217 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1219 spin_button_redraw (spin_button);
1221 g_object_notify (G_OBJECT (spin_button), "value");
1225 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1226 GtkScrollType scroll)
1228 /* We don't test whether the entry is editable, since
1229 * this key binding conceptually corresponds to changing
1230 * the value with the buttons using the mouse, which
1231 * we allow for non-editable spin buttons.
1235 case GTK_SCROLL_STEP_BACKWARD:
1236 case GTK_SCROLL_STEP_DOWN:
1237 case GTK_SCROLL_STEP_LEFT:
1238 gtk_spin_button_real_spin (spin, -spin->timer_step);
1240 if (spin->climb_rate > 0.0 && spin->timer_step
1241 < spin->adjustment->page_increment)
1243 if (spin->timer_calls < MAX_TIMER_CALLS)
1244 spin->timer_calls++;
1247 spin->timer_calls = 0;
1248 spin->timer_step += spin->climb_rate;
1253 case GTK_SCROLL_STEP_FORWARD:
1254 case GTK_SCROLL_STEP_UP:
1255 case GTK_SCROLL_STEP_RIGHT:
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_PAGE_BACKWARD:
1272 case GTK_SCROLL_PAGE_DOWN:
1273 case GTK_SCROLL_PAGE_LEFT:
1274 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1277 case GTK_SCROLL_PAGE_FORWARD:
1278 case GTK_SCROLL_PAGE_UP:
1279 case GTK_SCROLL_PAGE_RIGHT:
1280 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1283 case GTK_SCROLL_START:
1285 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1287 gtk_spin_button_real_spin (spin, -diff);
1291 case GTK_SCROLL_END:
1293 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1295 gtk_spin_button_real_spin (spin, diff);
1300 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1304 gtk_spin_button_update (spin);
1308 gtk_spin_button_key_release (GtkWidget *widget,
1311 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1313 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1314 spin->timer_step = spin->adjustment->step_increment;
1315 spin->timer_calls = 0;
1321 gtk_spin_button_snap (GtkSpinButton *spin_button,
1327 inc = spin_button->adjustment->step_increment;
1331 tmp = (val - spin_button->adjustment->lower) / inc;
1332 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1333 val = spin_button->adjustment->lower + floor (tmp) * inc;
1335 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1337 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1338 gtk_adjustment_set_value (spin_button->adjustment, val);
1341 gint return_val = FALSE;
1342 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1343 if (return_val == FALSE)
1344 gtk_spin_button_default_output (spin_button);
1349 gtk_spin_button_activate (GtkEntry *entry)
1351 if (entry->editable)
1352 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1354 /* Chain up so that entry->activates_default is honored */
1355 parent_class->activate (entry);
1359 gtk_spin_button_insert_text (GtkEditable *editable,
1360 const gchar *new_text,
1361 gint new_text_length,
1364 GtkEntry *entry = GTK_ENTRY (editable);
1365 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1366 GtkEditableClass *parent_editable_iface = g_type_interface_peek (parent_class, GTK_TYPE_EDITABLE);
1378 entry_length = entry->text_length;
1382 if (*(lc->negative_sign))
1383 neg_sign = *(lc->negative_sign);
1387 if (*(lc->positive_sign))
1388 pos_sign = *(lc->positive_sign);
1392 for (sign=0, i=0; i<entry_length; i++)
1393 if ((entry->text[i] == neg_sign) ||
1394 (entry->text[i] == pos_sign))
1400 if (sign && !(*position))
1403 for (dotpos=-1, i=0; i<entry_length; i++)
1404 if (entry->text[i] == *(lc->decimal_point))
1410 if (dotpos > -1 && *position > dotpos &&
1411 (gint)spin->digits - entry_length
1412 + dotpos - new_text_length + 1 < 0)
1415 for (i = 0; i < new_text_length; i++)
1417 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1419 if (sign || (*position) || i)
1423 else if (new_text[i] == *(lc->decimal_point))
1425 if (!spin->digits || dotpos > -1 ||
1426 (new_text_length - 1 - i + entry_length
1427 - *position > (gint)spin->digits))
1429 dotpos = *position + i;
1431 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1436 parent_editable_iface->insert_text (editable, new_text,
1437 new_text_length, position);
1441 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1445 gdouble new_value = 0.0;
1447 adj = spin_button->adjustment;
1449 new_value = adj->value + increment;
1453 if (spin_button->wrap)
1455 if (fabs (adj->value - adj->upper) < EPSILON)
1456 new_value = adj->lower;
1457 else if (new_value > adj->upper)
1458 new_value = adj->upper;
1461 new_value = MIN (new_value, adj->upper);
1463 else if (increment < 0)
1465 if (spin_button->wrap)
1467 if (fabs (adj->value - adj->lower) < EPSILON)
1468 new_value = adj->upper;
1469 else if (new_value < adj->lower)
1470 new_value = adj->lower;
1473 new_value = MAX (new_value, adj->lower);
1476 if (fabs (new_value - adj->value) > EPSILON)
1477 gtk_adjustment_set_value (adj, new_value);
1479 spin_button_redraw (spin_button);
1483 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1488 *new_val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1490 return GTK_INPUT_ERROR;
1496 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1498 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1500 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1501 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1507 /***********************************************************
1508 ***********************************************************
1509 *** Public interface ***
1510 ***********************************************************
1511 ***********************************************************/
1515 gtk_spin_button_configure (GtkSpinButton *spin_button,
1516 GtkAdjustment *adjustment,
1520 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1523 gtk_spin_button_set_adjustment (spin_button, adjustment);
1525 adjustment = spin_button->adjustment;
1527 g_object_freeze_notify (G_OBJECT (spin_button));
1528 if (spin_button->digits != digits)
1530 spin_button->digits = digits;
1531 g_object_notify (G_OBJECT (spin_button), "digits");
1534 if (spin_button->climb_rate != climb_rate)
1536 spin_button->climb_rate = climb_rate;
1537 g_object_notify (G_OBJECT (spin_button), "climb_rate");
1539 g_object_thaw_notify (G_OBJECT (spin_button));
1541 gtk_adjustment_value_changed (adjustment);
1545 gtk_spin_button_new (GtkAdjustment *adjustment,
1549 GtkSpinButton *spin;
1552 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1554 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1556 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1558 return GTK_WIDGET (spin);
1562 * gtk_spin_button_new_with_range:
1563 * @min: Minimum allowable value
1564 * @max: Maximum allowable value
1565 * @step: Increment added or subtracted by spinning the widget
1567 * This is a convenience constructor that allows creation of a numeric
1568 * #GtkSpinButton without manually creating an adjustment. The value is
1569 * initially set to the minimum value and a page increment of 10 * @step
1570 * is the default. The precision of the spin button is equivalent to the
1571 * precision of @step.
1573 * Return value: The new spin button as a #GtkWidget.
1576 gtk_spin_button_new_with_range (gdouble min,
1581 GtkSpinButton *spin;
1584 g_return_val_if_fail (min < max, NULL);
1585 g_return_val_if_fail (step != 0.0, NULL);
1587 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1589 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1591 if (fabs (step) >= 1.0 || step == 0.0)
1594 digits = abs ((gint) floor (log10 (fabs (step))));
1595 if (digits > MAX_DIGITS)
1596 digits = MAX_DIGITS;
1599 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1601 gtk_spin_button_set_numeric (spin, TRUE);
1603 return GTK_WIDGET (spin);
1606 /* Callback used when the spin button's adjustment changes. We need to redraw
1607 * the arrows when the adjustment's range changes, and reevaluate our size request.
1610 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1612 GtkSpinButton *spin_button;
1614 spin_button = GTK_SPIN_BUTTON (data);
1616 spin_button->timer_step = spin_button->adjustment->step_increment;
1617 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1621 * gtk_spin_button_set_adjustment:
1622 * @spin_button: a #GtkSpinButton
1623 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1625 * Replaces the #GtkAdjustment associated with @spin_button.
1628 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1629 GtkAdjustment *adjustment)
1631 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1633 if (spin_button->adjustment != adjustment)
1635 if (spin_button->adjustment)
1637 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1638 gtk_spin_button_value_changed,
1640 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1641 adjustment_changed_cb,
1643 g_object_unref (spin_button->adjustment);
1645 spin_button->adjustment = adjustment;
1648 g_object_ref (adjustment);
1649 gtk_object_sink (GTK_OBJECT (adjustment));
1650 g_signal_connect (adjustment, "value_changed",
1651 G_CALLBACK (gtk_spin_button_value_changed),
1653 g_signal_connect (adjustment, "changed",
1654 G_CALLBACK (adjustment_changed_cb),
1656 spin_button->timer_step = spin_button->adjustment->step_increment;
1659 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1662 g_object_notify (G_OBJECT (spin_button), "adjustment");
1666 * gtk_spin_button_get_adjustment:
1669 * Get the adjustment associated with a #GtkSpinButton
1671 * Return value: the #GtkAdjustment of @spin_button
1674 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1676 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1678 return spin_button->adjustment;
1682 * gtk_spin_button_set_digits:
1683 * @spin_button: a #GtkSpinButton
1684 * @digits: the number of digits to be displayed for the spin button's value
1686 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1690 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1693 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1695 if (spin_button->digits != digits)
1697 spin_button->digits = digits;
1698 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1699 g_object_notify (G_OBJECT (spin_button), "digits");
1701 /* since lower/upper may have changed */
1702 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1707 * gtk_spin_button_get_digits:
1708 * @spin_button: a #GtkSpinButton
1710 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1712 * Returns: the current precision
1715 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1717 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1719 return spin_button->digits;
1723 * gtk_spin_button_set_increments:
1724 * @spin_button: a #GtkSpinButton
1725 * @step: increment applied for a button 1 press.
1726 * @page: increment applied for a button 2 press.
1728 * Sets the step and page increments for spin_button. This affects how
1729 * quickly the value changes when the spin button's arrows are activated.
1732 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1736 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1738 spin_button->adjustment->step_increment = step;
1739 spin_button->adjustment->page_increment = page;
1743 * gtk_spin_button_get_increments:
1744 * @spin_button: a #GtkSpinButton
1745 * @step: location to store step increment, or %NULL
1746 * @page: location to store page increment, or %NULL
1748 * Gets the current step and page the increments used by @spin_button. See
1749 * gtk_spin_button_set_increments().
1752 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1756 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1759 *step = spin_button->adjustment->step_increment;
1761 *page = spin_button->adjustment->page_increment;
1765 * gtk_spin_button_set_range:
1766 * @spin_button: a #GtkSpinButton
1767 * @min: minimum allowable value
1768 * @max: maximum allowable value
1770 * Sets the minimum and maximum allowable values for @spin_button
1773 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1779 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1781 spin_button->adjustment->lower = min;
1782 spin_button->adjustment->upper = max;
1784 value = CLAMP (spin_button->adjustment->value,
1785 spin_button->adjustment->lower,
1786 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1788 if (value != spin_button->adjustment->value)
1789 gtk_spin_button_set_value (spin_button, value);
1791 gtk_adjustment_changed (spin_button->adjustment);
1795 * gtk_spin_button_get_range:
1796 * @spin_button: a #GtkSpinButton
1797 * @min: location to store minimum allowed value, or %NULL
1798 * @max: location to store maximum allowed value, or %NULL
1800 * Gets the range allowed for @spin_button. See
1801 * gtk_spin_button_set_range().
1804 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1808 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1811 *min = spin_button->adjustment->lower;
1813 *max = spin_button->adjustment->upper;
1817 * gtk_spin_button_get_value:
1818 * @spin_button: a #GtkSpinButton
1820 * Get the value in the @spin_button.
1822 * Return value: the value of @spin_button
1825 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1827 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1829 return spin_button->adjustment->value;
1833 * gtk_spin_button_get_value_as_int:
1834 * @spin_button: a #GtkSpinButton
1836 * Get the value @spin_button represented as an integer.
1838 * Return value: the value of @spin_button
1841 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1845 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1847 val = spin_button->adjustment->value;
1848 if (val - floor (val) < ceil (val) - val)
1855 * gtk_spin_button_set_value:
1856 * @spin_button: a #GtkSpinButton
1857 * @value: the new value
1859 * Set the value of @spin_button.
1862 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1865 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1867 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1868 gtk_adjustment_set_value (spin_button->adjustment, value);
1871 gint return_val = FALSE;
1872 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1873 if (return_val == FALSE)
1874 gtk_spin_button_default_output (spin_button);
1879 * gtk_spin_button_set_update_policy:
1880 * @spin_button: a #GtkSpinButton
1881 * @policy: a #GtkSpinButtonUpdatePolicy value
1883 * Sets the update behavior of a spin button. This determines whether the
1884 * spin button is always updated or only when a valid value is set.
1887 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1888 GtkSpinButtonUpdatePolicy policy)
1890 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1892 if (spin_button->update_policy != policy)
1894 spin_button->update_policy = policy;
1895 g_object_notify (G_OBJECT (spin_button), "update_policy");
1900 * gtk_spin_button_get_update_policy:
1901 * @spin_button: a #GtkSpinButton
1903 * Gets the update behavior of a spin button. See
1904 * gtk_spin_button_set_update_policy().
1906 * Return value: the current update policy
1908 GtkSpinButtonUpdatePolicy
1909 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1911 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1913 return spin_button->update_policy;
1917 * gtk_spin_button_set_numeric:
1918 * @spin_button: a #GtkSpinButton
1919 * @numeric: flag indicating if only numeric entry is allowed.
1921 * Sets the flag that determines if non-numeric text can be typed into
1925 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1928 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1930 numeric = numeric != FALSE;
1932 if (spin_button->numeric != numeric)
1934 spin_button->numeric = numeric;
1935 g_object_notify (G_OBJECT (spin_button), "numeric");
1940 * gtk_spin_button_get_numeric:
1941 * @spin_button: a #GtkSpinButton
1943 * Returns whether non-numeric text can be typed into the spin button.
1944 * See gtk_spin_button_set_numeric().
1946 * Return value: %TRUE if only numeric text can be entered
1949 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1951 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1953 return spin_button->numeric;
1957 * gtk_spin_button_set_wrap:
1958 * @spin_button: a #GtkSpinButton
1959 * @wrap: a flag indicating if wrapping behavior is performed.
1961 * Sets the flag that determines if a spin button value wraps around to the
1962 * opposite limit when the upper or lower limit of the range is exceeded.
1965 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1968 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1970 wrap = wrap != FALSE;
1972 if (spin_button->wrap != wrap)
1974 spin_button->wrap = (wrap != 0);
1976 g_object_notify (G_OBJECT (spin_button), "wrap");
1981 * gtk_spin_button_get_wrap:
1982 * @spin_button: a #GtkSpinButton
1984 * Returns whether the spin button's value wraps around to the
1985 * opposite limit when the upper or lower limit of the range is
1986 * exceeded. See gtk_spin_button_set_wrap().
1988 * Return value: %TRUE if the spin button wraps around
1991 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
1993 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1995 return spin_button->wrap;
1999 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2001 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
2004 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2006 return arrow_size - arrow_size % 2; /* force even */
2010 * spin_button_get_shadow_type:
2011 * @spin_button: a #GtkSpinButton
2013 * Convenience function to Get the shadow type from the underlying widget's
2016 * Return value: the #GtkShadowType
2019 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2021 GtkShadowType rc_shadow_type;
2023 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
2025 return rc_shadow_type;
2029 * gtk_spin_button_set_snap_to_ticks:
2030 * @spin_button: a #GtkSpinButton
2031 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2033 * Sets the policy as to whether values are corrected to the nearest step
2034 * increment when a spin button is activated after providing an invalid value.
2037 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2038 gboolean snap_to_ticks)
2042 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2044 new_val = (snap_to_ticks != 0);
2046 if (new_val != spin_button->snap_to_ticks)
2048 spin_button->snap_to_ticks = new_val;
2049 if (new_val && GTK_ENTRY (spin_button)->editable)
2050 gtk_spin_button_update (spin_button);
2052 g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
2057 * gtk_spin_button_get_snap_to_ticks:
2058 * @spin_button: a #GtkSpinButton
2060 * Returns whether the values are corrected to the nearest step. See
2061 * gtk_spin_button_set_snap_to_ticks().
2063 * Return value: %TRUE if values are snapped to the nearest step.
2066 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2068 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2070 return spin_button->snap_to_ticks;
2074 * gtk_spin_button_spin:
2075 * @spin_button: a #GtkSpinButton
2076 * @direction: a #GtkSpinType indicating the direction to spin.
2077 * @increment: step increment to apply in the specified direction.
2079 * Increment or decrement a spin button's value in a specified direction
2080 * by a specified amount.
2083 gtk_spin_button_spin (GtkSpinButton *spin_button,
2084 GtkSpinType direction,
2090 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2092 adj = spin_button->adjustment;
2094 /* for compatibility with the 1.0.x version of this function */
2095 if (increment != 0 && increment != adj->step_increment &&
2096 (direction == GTK_SPIN_STEP_FORWARD ||
2097 direction == GTK_SPIN_STEP_BACKWARD))
2099 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2100 increment = -increment;
2101 direction = GTK_SPIN_USER_DEFINED;
2106 case GTK_SPIN_STEP_FORWARD:
2108 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2111 case GTK_SPIN_STEP_BACKWARD:
2113 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2116 case GTK_SPIN_PAGE_FORWARD:
2118 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2121 case GTK_SPIN_PAGE_BACKWARD:
2123 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2128 diff = adj->value - adj->lower;
2130 gtk_spin_button_real_spin (spin_button, -diff);
2135 diff = adj->upper - adj->value;
2137 gtk_spin_button_real_spin (spin_button, diff);
2140 case GTK_SPIN_USER_DEFINED:
2143 gtk_spin_button_real_spin (spin_button, increment);
2152 * gtk_spin_button_update:
2153 * @spin_button: a #GtkSpinButton
2155 * Manually force an update of the spin button.
2158 gtk_spin_button_update (GtkSpinButton *spin_button)
2164 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2167 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2168 if (return_val == FALSE)
2170 return_val = gtk_spin_button_default_input (spin_button, &val);
2171 error = (return_val == GTK_INPUT_ERROR);
2173 else if (return_val == GTK_INPUT_ERROR)
2176 spin_button_redraw (spin_button);
2178 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2180 if (val < spin_button->adjustment->lower)
2181 val = spin_button->adjustment->lower;
2182 else if (val > spin_button->adjustment->upper)
2183 val = spin_button->adjustment->upper;
2185 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2187 val < spin_button->adjustment->lower ||
2188 val > spin_button->adjustment->upper))
2190 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2194 if (spin_button->snap_to_ticks)
2195 gtk_spin_button_snap (spin_button, val);
2198 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2199 gtk_adjustment_set_value (spin_button->adjustment, val);
2203 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0,
2205 if (return_val == FALSE)
2206 gtk_spin_button_default_output (spin_button);
2212 spin_button_redraw (GtkSpinButton *spin_button)
2216 widget = GTK_WIDGET (spin_button);
2218 if (GTK_WIDGET_DRAWABLE (widget))
2220 gtk_widget_queue_draw (widget);
2222 /* We must invalidate the panel window ourselves, because it
2223 * is not a child of widget->window
2225 gdk_window_invalidate_rect (spin_button->panel, NULL, TRUE);