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)
958 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
962 gtk_spin_button_stop_spinning (spin);
963 spin_button_redraw (spin);
968 gtk_spin_button_state_changed (GtkWidget *widget,
969 GtkStateType previous_state)
971 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
973 if (!GTK_WIDGET_IS_SENSITIVE (widget))
975 gtk_spin_button_stop_spinning (spin);
976 spin_button_redraw (spin);
981 gtk_spin_button_scroll (GtkWidget *widget,
982 GdkEventScroll *event)
984 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
986 if (event->direction == GDK_SCROLL_UP)
988 if (!GTK_WIDGET_HAS_FOCUS (widget))
989 gtk_widget_grab_focus (widget);
990 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
992 else if (event->direction == GDK_SCROLL_DOWN)
994 if (!GTK_WIDGET_HAS_FOCUS (widget))
995 gtk_widget_grab_focus (widget);
996 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
1005 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1009 g_source_remove (spin->timer);
1011 spin->timer_calls = 0;
1012 spin->need_timer = FALSE;
1017 spin->timer_step = spin->adjustment->step_increment;
1018 spin->timer_calls = 0;
1020 spin->click_child = NO_ARROW;
1025 start_spinning (GtkSpinButton *spin,
1026 GtkArrowType click_child,
1029 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1031 spin->click_child = click_child;
1032 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1036 spin->timer_step = step;
1037 spin->need_timer = TRUE;
1038 spin->timer = g_timeout_add (SPIN_BUTTON_INITIAL_TIMER_DELAY,
1039 (GtkFunction) gtk_spin_button_timer,
1043 spin_button_redraw (spin);
1047 gtk_spin_button_button_press (GtkWidget *widget,
1048 GdkEventButton *event)
1050 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1054 if (event->window == spin->panel)
1056 if (!GTK_WIDGET_HAS_FOCUS (widget))
1057 gtk_widget_grab_focus (widget);
1058 spin->button = event->button;
1060 if (GTK_ENTRY (widget)->editable)
1061 gtk_spin_button_update (spin);
1063 if (event->y <= widget->requisition.height / 2)
1065 if (event->button == 1)
1066 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->step_increment);
1067 else if (event->button == 2)
1068 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->page_increment);
1070 spin->click_child = GTK_ARROW_UP;
1074 if (event->button == 1)
1075 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->step_increment);
1076 else if (event->button == 2)
1077 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->page_increment);
1079 spin->click_child = GTK_ARROW_DOWN;
1084 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1090 gtk_spin_button_button_release (GtkWidget *widget,
1091 GdkEventButton *event)
1093 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1096 arrow_size = spin_button_get_arrow_size (spin);
1098 if (event->button == spin->button)
1100 int click_child = spin->click_child;
1102 gtk_spin_button_stop_spinning (spin);
1104 if (event->button == 3)
1106 if (event->y >= 0 && event->x >= 0 &&
1107 event->y <= widget->requisition.height &&
1108 event->x <= arrow_size + 2 * widget->style->xthickness)
1110 if (click_child == GTK_ARROW_UP &&
1111 event->y <= widget->requisition.height / 2)
1115 diff = spin->adjustment->upper - spin->adjustment->value;
1117 gtk_spin_button_real_spin (spin, diff);
1119 else if (click_child == GTK_ARROW_DOWN &&
1120 event->y > widget->requisition.height / 2)
1124 diff = spin->adjustment->value - spin->adjustment->lower;
1126 gtk_spin_button_real_spin (spin, -diff);
1130 spin_button_redraw (spin);
1135 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1139 gtk_spin_button_motion_notify (GtkWidget *widget,
1140 GdkEventMotion *event)
1142 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1147 if (event->window == spin->panel)
1151 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1153 if (y <= widget->requisition.height / 2 &&
1154 spin->in_child == GTK_ARROW_DOWN)
1156 spin->in_child = GTK_ARROW_UP;
1157 spin_button_redraw (spin);
1159 else if (y > widget->requisition.height / 2 &&
1160 spin->in_child == GTK_ARROW_UP)
1162 spin->in_child = GTK_ARROW_DOWN;
1163 spin_button_redraw (spin);
1169 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1173 gtk_spin_button_timer (GtkSpinButton *spin_button)
1175 gboolean retval = FALSE;
1177 GDK_THREADS_ENTER ();
1179 if (spin_button->timer)
1181 if (spin_button->click_child == GTK_ARROW_UP)
1182 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1184 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1186 if (spin_button->need_timer)
1188 spin_button->need_timer = FALSE;
1189 spin_button->timer = g_timeout_add (SPIN_BUTTON_TIMER_DELAY,
1190 (GtkFunction) gtk_spin_button_timer,
1191 (gpointer) spin_button);
1195 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1196 < spin_button->adjustment->page_increment)
1198 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1199 spin_button->timer_calls++;
1202 spin_button->timer_calls = 0;
1203 spin_button->timer_step += spin_button->climb_rate;
1210 GDK_THREADS_LEAVE ();
1216 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1217 GtkSpinButton *spin_button)
1219 gboolean return_val;
1221 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1224 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1225 if (return_val == FALSE)
1226 gtk_spin_button_default_output (spin_button);
1228 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1230 spin_button_redraw (spin_button);
1232 g_object_notify (G_OBJECT (spin_button), "value");
1236 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1237 GtkScrollType scroll)
1239 /* We don't test whether the entry is editable, since
1240 * this key binding conceptually corresponds to changing
1241 * the value with the buttons using the mouse, which
1242 * we allow for non-editable spin buttons.
1246 case GTK_SCROLL_STEP_BACKWARD:
1247 case GTK_SCROLL_STEP_DOWN:
1248 case GTK_SCROLL_STEP_LEFT:
1249 gtk_spin_button_real_spin (spin, -spin->timer_step);
1251 if (spin->climb_rate > 0.0 && spin->timer_step
1252 < spin->adjustment->page_increment)
1254 if (spin->timer_calls < MAX_TIMER_CALLS)
1255 spin->timer_calls++;
1258 spin->timer_calls = 0;
1259 spin->timer_step += spin->climb_rate;
1264 case GTK_SCROLL_STEP_FORWARD:
1265 case GTK_SCROLL_STEP_UP:
1266 case GTK_SCROLL_STEP_RIGHT:
1267 gtk_spin_button_real_spin (spin, spin->timer_step);
1269 if (spin->climb_rate > 0.0 && spin->timer_step
1270 < spin->adjustment->page_increment)
1272 if (spin->timer_calls < MAX_TIMER_CALLS)
1273 spin->timer_calls++;
1276 spin->timer_calls = 0;
1277 spin->timer_step += spin->climb_rate;
1282 case GTK_SCROLL_PAGE_BACKWARD:
1283 case GTK_SCROLL_PAGE_DOWN:
1284 case GTK_SCROLL_PAGE_LEFT:
1285 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1288 case GTK_SCROLL_PAGE_FORWARD:
1289 case GTK_SCROLL_PAGE_UP:
1290 case GTK_SCROLL_PAGE_RIGHT:
1291 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1294 case GTK_SCROLL_START:
1296 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1298 gtk_spin_button_real_spin (spin, -diff);
1302 case GTK_SCROLL_END:
1304 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1306 gtk_spin_button_real_spin (spin, diff);
1311 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1315 gtk_spin_button_update (spin);
1319 gtk_spin_button_key_release (GtkWidget *widget,
1322 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1324 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1325 spin->timer_step = spin->adjustment->step_increment;
1326 spin->timer_calls = 0;
1332 gtk_spin_button_snap (GtkSpinButton *spin_button,
1338 inc = spin_button->adjustment->step_increment;
1342 tmp = (val - spin_button->adjustment->lower) / inc;
1343 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1344 val = spin_button->adjustment->lower + floor (tmp) * inc;
1346 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1348 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1349 gtk_adjustment_set_value (spin_button->adjustment, val);
1352 gint return_val = FALSE;
1353 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1354 if (return_val == FALSE)
1355 gtk_spin_button_default_output (spin_button);
1360 gtk_spin_button_activate (GtkEntry *entry)
1362 if (entry->editable)
1363 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1365 /* Chain up so that entry->activates_default is honored */
1366 parent_class->activate (entry);
1370 gtk_spin_button_insert_text (GtkEditable *editable,
1371 const gchar *new_text,
1372 gint new_text_length,
1375 GtkEntry *entry = GTK_ENTRY (editable);
1376 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1377 GtkEditableClass *parent_editable_iface = g_type_interface_peek (parent_class, GTK_TYPE_EDITABLE);
1389 entry_length = entry->text_length;
1393 if (*(lc->negative_sign))
1394 neg_sign = *(lc->negative_sign);
1398 if (*(lc->positive_sign))
1399 pos_sign = *(lc->positive_sign);
1403 for (sign=0, i=0; i<entry_length; i++)
1404 if ((entry->text[i] == neg_sign) ||
1405 (entry->text[i] == pos_sign))
1411 if (sign && !(*position))
1414 for (dotpos=-1, i=0; i<entry_length; i++)
1415 if (entry->text[i] == *(lc->decimal_point))
1421 if (dotpos > -1 && *position > dotpos &&
1422 (gint)spin->digits - entry_length
1423 + dotpos - new_text_length + 1 < 0)
1426 for (i = 0; i < new_text_length; i++)
1428 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1430 if (sign || (*position) || i)
1434 else if (new_text[i] == *(lc->decimal_point))
1436 if (!spin->digits || dotpos > -1 ||
1437 (new_text_length - 1 - i + entry_length
1438 - *position > (gint)spin->digits))
1440 dotpos = *position + i;
1442 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1447 parent_editable_iface->insert_text (editable, new_text,
1448 new_text_length, position);
1452 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1456 gdouble new_value = 0.0;
1458 adj = spin_button->adjustment;
1460 new_value = adj->value + increment;
1464 if (spin_button->wrap)
1466 if (fabs (adj->value - adj->upper) < EPSILON)
1467 new_value = adj->lower;
1468 else if (new_value > adj->upper)
1469 new_value = adj->upper;
1472 new_value = MIN (new_value, adj->upper);
1474 else if (increment < 0)
1476 if (spin_button->wrap)
1478 if (fabs (adj->value - adj->lower) < EPSILON)
1479 new_value = adj->upper;
1480 else if (new_value < adj->lower)
1481 new_value = adj->lower;
1484 new_value = MAX (new_value, adj->lower);
1487 if (fabs (new_value - adj->value) > EPSILON)
1488 gtk_adjustment_set_value (adj, new_value);
1490 spin_button_redraw (spin_button);
1494 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1499 *new_val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1501 return GTK_INPUT_ERROR;
1507 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1509 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1511 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1512 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1518 /***********************************************************
1519 ***********************************************************
1520 *** Public interface ***
1521 ***********************************************************
1522 ***********************************************************/
1526 gtk_spin_button_configure (GtkSpinButton *spin_button,
1527 GtkAdjustment *adjustment,
1531 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1534 gtk_spin_button_set_adjustment (spin_button, adjustment);
1536 adjustment = spin_button->adjustment;
1538 g_object_freeze_notify (G_OBJECT (spin_button));
1539 if (spin_button->digits != digits)
1541 spin_button->digits = digits;
1542 g_object_notify (G_OBJECT (spin_button), "digits");
1545 if (spin_button->climb_rate != climb_rate)
1547 spin_button->climb_rate = climb_rate;
1548 g_object_notify (G_OBJECT (spin_button), "climb_rate");
1550 g_object_thaw_notify (G_OBJECT (spin_button));
1552 gtk_adjustment_value_changed (adjustment);
1556 gtk_spin_button_new (GtkAdjustment *adjustment,
1560 GtkSpinButton *spin;
1563 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1565 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1567 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1569 return GTK_WIDGET (spin);
1573 * gtk_spin_button_new_with_range:
1574 * @min: Minimum allowable value
1575 * @max: Maximum allowable value
1576 * @step: Increment added or subtracted by spinning the widget
1578 * This is a convenience constructor that allows creation of a numeric
1579 * #GtkSpinButton without manually creating an adjustment. The value is
1580 * initially set to the minimum value and a page increment of 10 * @step
1581 * is the default. The precision of the spin button is equivalent to the
1582 * precision of @step.
1584 * Return value: The new spin button as a #GtkWidget.
1587 gtk_spin_button_new_with_range (gdouble min,
1592 GtkSpinButton *spin;
1595 g_return_val_if_fail (min < max, NULL);
1596 g_return_val_if_fail (step != 0.0, NULL);
1598 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1600 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1602 if (fabs (step) >= 1.0 || step == 0.0)
1605 digits = abs ((gint) floor (log10 (fabs (step))));
1606 if (digits > MAX_DIGITS)
1607 digits = MAX_DIGITS;
1610 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1612 gtk_spin_button_set_numeric (spin, TRUE);
1614 return GTK_WIDGET (spin);
1617 /* Callback used when the spin button's adjustment changes. We need to redraw
1618 * the arrows when the adjustment's range changes, and reevaluate our size request.
1621 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1623 GtkSpinButton *spin_button;
1625 spin_button = GTK_SPIN_BUTTON (data);
1627 spin_button->timer_step = spin_button->adjustment->step_increment;
1628 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1632 * gtk_spin_button_set_adjustment:
1633 * @spin_button: a #GtkSpinButton
1634 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1636 * Replaces the #GtkAdjustment associated with @spin_button.
1639 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1640 GtkAdjustment *adjustment)
1642 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1644 if (spin_button->adjustment != adjustment)
1646 if (spin_button->adjustment)
1648 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1649 gtk_spin_button_value_changed,
1651 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1652 adjustment_changed_cb,
1654 g_object_unref (spin_button->adjustment);
1656 spin_button->adjustment = adjustment;
1659 g_object_ref (adjustment);
1660 gtk_object_sink (GTK_OBJECT (adjustment));
1661 g_signal_connect (adjustment, "value_changed",
1662 G_CALLBACK (gtk_spin_button_value_changed),
1664 g_signal_connect (adjustment, "changed",
1665 G_CALLBACK (adjustment_changed_cb),
1667 spin_button->timer_step = spin_button->adjustment->step_increment;
1670 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1673 g_object_notify (G_OBJECT (spin_button), "adjustment");
1677 * gtk_spin_button_get_adjustment:
1680 * Get the adjustment associated with a #GtkSpinButton
1682 * Return value: the #GtkAdjustment of @spin_button
1685 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1687 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1689 return spin_button->adjustment;
1693 * gtk_spin_button_set_digits:
1694 * @spin_button: a #GtkSpinButton
1695 * @digits: the number of digits to be displayed for the spin button's value
1697 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1701 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1704 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1706 if (spin_button->digits != digits)
1708 spin_button->digits = digits;
1709 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1710 g_object_notify (G_OBJECT (spin_button), "digits");
1712 /* since lower/upper may have changed */
1713 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1718 * gtk_spin_button_get_digits:
1719 * @spin_button: a #GtkSpinButton
1721 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1723 * Returns: the current precision
1726 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1728 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1730 return spin_button->digits;
1734 * gtk_spin_button_set_increments:
1735 * @spin_button: a #GtkSpinButton
1736 * @step: increment applied for a button 1 press.
1737 * @page: increment applied for a button 2 press.
1739 * Sets the step and page increments for spin_button. This affects how
1740 * quickly the value changes when the spin button's arrows are activated.
1743 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1747 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1749 spin_button->adjustment->step_increment = step;
1750 spin_button->adjustment->page_increment = page;
1754 * gtk_spin_button_get_increments:
1755 * @spin_button: a #GtkSpinButton
1756 * @step: location to store step increment, or %NULL
1757 * @page: location to store page increment, or %NULL
1759 * Gets the current step and page the increments used by @spin_button. See
1760 * gtk_spin_button_set_increments().
1763 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1767 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1770 *step = spin_button->adjustment->step_increment;
1772 *page = spin_button->adjustment->page_increment;
1776 * gtk_spin_button_set_range:
1777 * @spin_button: a #GtkSpinButton
1778 * @min: minimum allowable value
1779 * @max: maximum allowable value
1781 * Sets the minimum and maximum allowable values for @spin_button
1784 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1790 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1792 spin_button->adjustment->lower = min;
1793 spin_button->adjustment->upper = max;
1795 value = CLAMP (spin_button->adjustment->value,
1796 spin_button->adjustment->lower,
1797 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1799 if (value != spin_button->adjustment->value)
1800 gtk_spin_button_set_value (spin_button, value);
1802 gtk_adjustment_changed (spin_button->adjustment);
1806 * gtk_spin_button_get_range:
1807 * @spin_button: a #GtkSpinButton
1808 * @min: location to store minimum allowed value, or %NULL
1809 * @max: location to store maximum allowed value, or %NULL
1811 * Gets the range allowed for @spin_button. See
1812 * gtk_spin_button_set_range().
1815 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1819 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1822 *min = spin_button->adjustment->lower;
1824 *max = spin_button->adjustment->upper;
1828 * gtk_spin_button_get_value:
1829 * @spin_button: a #GtkSpinButton
1831 * Get the value in the @spin_button.
1833 * Return value: the value of @spin_button
1836 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1838 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1840 return spin_button->adjustment->value;
1844 * gtk_spin_button_get_value_as_int:
1845 * @spin_button: a #GtkSpinButton
1847 * Get the value @spin_button represented as an integer.
1849 * Return value: the value of @spin_button
1852 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1856 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1858 val = spin_button->adjustment->value;
1859 if (val - floor (val) < ceil (val) - val)
1866 * gtk_spin_button_set_value:
1867 * @spin_button: a #GtkSpinButton
1868 * @value: the new value
1870 * Set the value of @spin_button.
1873 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1876 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1878 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1879 gtk_adjustment_set_value (spin_button->adjustment, value);
1882 gint return_val = FALSE;
1883 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1884 if (return_val == FALSE)
1885 gtk_spin_button_default_output (spin_button);
1890 * gtk_spin_button_set_update_policy:
1891 * @spin_button: a #GtkSpinButton
1892 * @policy: a #GtkSpinButtonUpdatePolicy value
1894 * Sets the update behavior of a spin button. This determines whether the
1895 * spin button is always updated or only when a valid value is set.
1898 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1899 GtkSpinButtonUpdatePolicy policy)
1901 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1903 if (spin_button->update_policy != policy)
1905 spin_button->update_policy = policy;
1906 g_object_notify (G_OBJECT (spin_button), "update_policy");
1911 * gtk_spin_button_get_update_policy:
1912 * @spin_button: a #GtkSpinButton
1914 * Gets the update behavior of a spin button. See
1915 * gtk_spin_button_set_update_policy().
1917 * Return value: the current update policy
1919 GtkSpinButtonUpdatePolicy
1920 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1922 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1924 return spin_button->update_policy;
1928 * gtk_spin_button_set_numeric:
1929 * @spin_button: a #GtkSpinButton
1930 * @numeric: flag indicating if only numeric entry is allowed.
1932 * Sets the flag that determines if non-numeric text can be typed into
1936 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1939 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1941 numeric = numeric != FALSE;
1943 if (spin_button->numeric != numeric)
1945 spin_button->numeric = numeric;
1946 g_object_notify (G_OBJECT (spin_button), "numeric");
1951 * gtk_spin_button_get_numeric:
1952 * @spin_button: a #GtkSpinButton
1954 * Returns whether non-numeric text can be typed into the spin button.
1955 * See gtk_spin_button_set_numeric().
1957 * Return value: %TRUE if only numeric text can be entered
1960 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1962 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1964 return spin_button->numeric;
1968 * gtk_spin_button_set_wrap:
1969 * @spin_button: a #GtkSpinButton
1970 * @wrap: a flag indicating if wrapping behavior is performed.
1972 * Sets the flag that determines if a spin button value wraps around to the
1973 * opposite limit when the upper or lower limit of the range is exceeded.
1976 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1979 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1981 wrap = wrap != FALSE;
1983 if (spin_button->wrap != wrap)
1985 spin_button->wrap = (wrap != 0);
1987 g_object_notify (G_OBJECT (spin_button), "wrap");
1992 * gtk_spin_button_get_wrap:
1993 * @spin_button: a #GtkSpinButton
1995 * Returns whether the spin button's value wraps around to the
1996 * opposite limit when the upper or lower limit of the range is
1997 * exceeded. See gtk_spin_button_set_wrap().
1999 * Return value: %TRUE if the spin button wraps around
2002 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2004 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2006 return spin_button->wrap;
2010 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2012 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
2015 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2017 return arrow_size - arrow_size % 2; /* force even */
2021 * spin_button_get_shadow_type:
2022 * @spin_button: a #GtkSpinButton
2024 * Convenience function to Get the shadow type from the underlying widget's
2027 * Return value: the #GtkShadowType
2030 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2032 GtkShadowType rc_shadow_type;
2034 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
2036 return rc_shadow_type;
2040 * gtk_spin_button_set_snap_to_ticks:
2041 * @spin_button: a #GtkSpinButton
2042 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2044 * Sets the policy as to whether values are corrected to the nearest step
2045 * increment when a spin button is activated after providing an invalid value.
2048 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2049 gboolean snap_to_ticks)
2053 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2055 new_val = (snap_to_ticks != 0);
2057 if (new_val != spin_button->snap_to_ticks)
2059 spin_button->snap_to_ticks = new_val;
2060 if (new_val && GTK_ENTRY (spin_button)->editable)
2061 gtk_spin_button_update (spin_button);
2063 g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
2068 * gtk_spin_button_get_snap_to_ticks:
2069 * @spin_button: a #GtkSpinButton
2071 * Returns whether the values are corrected to the nearest step. See
2072 * gtk_spin_button_set_snap_to_ticks().
2074 * Return value: %TRUE if values are snapped to the nearest step.
2077 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2079 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2081 return spin_button->snap_to_ticks;
2085 * gtk_spin_button_spin:
2086 * @spin_button: a #GtkSpinButton
2087 * @direction: a #GtkSpinType indicating the direction to spin.
2088 * @increment: step increment to apply in the specified direction.
2090 * Increment or decrement a spin button's value in a specified direction
2091 * by a specified amount.
2094 gtk_spin_button_spin (GtkSpinButton *spin_button,
2095 GtkSpinType direction,
2101 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2103 adj = spin_button->adjustment;
2105 /* for compatibility with the 1.0.x version of this function */
2106 if (increment != 0 && increment != adj->step_increment &&
2107 (direction == GTK_SPIN_STEP_FORWARD ||
2108 direction == GTK_SPIN_STEP_BACKWARD))
2110 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2111 increment = -increment;
2112 direction = GTK_SPIN_USER_DEFINED;
2117 case GTK_SPIN_STEP_FORWARD:
2119 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2122 case GTK_SPIN_STEP_BACKWARD:
2124 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2127 case GTK_SPIN_PAGE_FORWARD:
2129 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2132 case GTK_SPIN_PAGE_BACKWARD:
2134 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2139 diff = adj->value - adj->lower;
2141 gtk_spin_button_real_spin (spin_button, -diff);
2146 diff = adj->upper - adj->value;
2148 gtk_spin_button_real_spin (spin_button, diff);
2151 case GTK_SPIN_USER_DEFINED:
2154 gtk_spin_button_real_spin (spin_button, increment);
2163 * gtk_spin_button_update:
2164 * @spin_button: a #GtkSpinButton
2166 * Manually force an update of the spin button.
2169 gtk_spin_button_update (GtkSpinButton *spin_button)
2175 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2178 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2179 if (return_val == FALSE)
2181 return_val = gtk_spin_button_default_input (spin_button, &val);
2182 error = (return_val == GTK_INPUT_ERROR);
2184 else if (return_val == GTK_INPUT_ERROR)
2187 spin_button_redraw (spin_button);
2189 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2191 if (val < spin_button->adjustment->lower)
2192 val = spin_button->adjustment->lower;
2193 else if (val > spin_button->adjustment->upper)
2194 val = spin_button->adjustment->upper;
2196 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2198 val < spin_button->adjustment->lower ||
2199 val > spin_button->adjustment->upper))
2201 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2205 if (spin_button->snap_to_ticks)
2206 gtk_spin_button_snap (spin_button, val);
2209 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2210 gtk_adjustment_set_value (spin_button->adjustment, val);
2214 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0,
2216 if (return_val == FALSE)
2217 gtk_spin_button_default_output (spin_button);
2223 spin_button_redraw (GtkSpinButton *spin_button)
2227 widget = GTK_WIDGET (spin_button);
2229 if (GTK_WIDGET_DRAWABLE (widget))
2231 gtk_widget_queue_draw (widget);
2233 /* We must invalidate the panel window ourselves, because it
2234 * is not a child of widget->window
2236 gdk_window_invalidate_rect (spin_button->panel, NULL, TRUE);