1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GtkSpinButton widget for GTK+
5 * Copyright (C) 1998 Lars Hamann and Stefan Jeske
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
36 #include "gdk/gdkkeysyms.h"
37 #include "gtkbindings.h"
38 #include "gtkspinbutton.h"
40 #include "gtkmarshalers.h"
41 #include "gtksettings.h"
42 #include "gtkprivate.h"
46 #define MIN_SPIN_BUTTON_WIDTH 30
47 #define SPIN_BUTTON_INITIAL_TIMER_DELAY 200
48 #define SPIN_BUTTON_TIMER_DELAY 20
49 #define MAX_TIMER_CALLS 5
52 #define MIN_ARROW_WIDTH 6
76 static void gtk_spin_button_class_init (GtkSpinButtonClass *klass);
77 static void gtk_spin_button_editable_init (GtkEditableClass *iface);
78 static void gtk_spin_button_init (GtkSpinButton *spin_button);
79 static void gtk_spin_button_finalize (GObject *object);
80 static void gtk_spin_button_destroy (GtkObject *object);
81 static void gtk_spin_button_set_property (GObject *object,
85 static void gtk_spin_button_get_property (GObject *object,
89 static void gtk_spin_button_map (GtkWidget *widget);
90 static void gtk_spin_button_unmap (GtkWidget *widget);
91 static void gtk_spin_button_realize (GtkWidget *widget);
92 static void gtk_spin_button_unrealize (GtkWidget *widget);
93 static void gtk_spin_button_size_request (GtkWidget *widget,
94 GtkRequisition *requisition);
95 static void gtk_spin_button_size_allocate (GtkWidget *widget,
96 GtkAllocation *allocation);
97 static gint gtk_spin_button_expose (GtkWidget *widget,
98 GdkEventExpose *event);
99 static gint gtk_spin_button_button_press (GtkWidget *widget,
100 GdkEventButton *event);
101 static gint gtk_spin_button_button_release (GtkWidget *widget,
102 GdkEventButton *event);
103 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
104 GdkEventMotion *event);
105 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
106 GdkEventCrossing *event);
107 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
108 GdkEventCrossing *event);
109 static gint gtk_spin_button_focus_out (GtkWidget *widget,
110 GdkEventFocus *event);
111 static void gtk_spin_button_grab_notify (GtkWidget *widget,
112 gboolean was_grabbed);
113 static void gtk_spin_button_state_changed (GtkWidget *widget,
114 GtkStateType previous_state);
115 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
116 GtkArrowType arrow_type);
117 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
118 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
119 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
120 GtkSpinButton *spin_button);
121 static gint gtk_spin_button_key_release (GtkWidget *widget,
123 static gint gtk_spin_button_scroll (GtkWidget *widget,
124 GdkEventScroll *event);
125 static void gtk_spin_button_activate (GtkEntry *entry);
126 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
128 static void gtk_spin_button_insert_text (GtkEditable *editable,
129 const gchar *new_text,
130 gint new_text_length,
132 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
134 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
135 GtkScrollType scroll);
137 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
139 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
141 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
142 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
143 static void spin_button_redraw (GtkSpinButton *spin_button);
146 static GtkEntryClass *parent_class = NULL;
147 static guint spinbutton_signals[LAST_SIGNAL] = {0};
152 gtk_spin_button_get_type (void)
154 static GType spin_button_type = 0;
156 if (!spin_button_type)
158 static const GTypeInfo spin_button_info =
160 sizeof (GtkSpinButtonClass),
161 NULL, /* base_init */
162 NULL, /* base_finalize */
163 (GClassInitFunc) gtk_spin_button_class_init,
164 NULL, /* class_finalize */
165 NULL, /* class_data */
166 sizeof (GtkSpinButton),
168 (GInstanceInitFunc) gtk_spin_button_init,
171 static const GInterfaceInfo editable_info =
173 (GInterfaceInitFunc) gtk_spin_button_editable_init, /* interface_init */
174 NULL, /* interface_finalize */
175 NULL /* interface_data */
179 g_type_register_static (GTK_TYPE_ENTRY, "GtkSpinButton",
180 &spin_button_info, 0);
182 g_type_add_interface_static (spin_button_type,
186 return spin_button_type;
189 #define add_spin_binding(binding_set, keyval, mask, scroll) \
190 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
192 GTK_TYPE_SCROLL_TYPE, scroll)
195 gtk_spin_button_class_init (GtkSpinButtonClass *class)
197 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
198 GtkObjectClass *object_class;
199 GtkWidgetClass *widget_class;
200 GtkEntryClass *entry_class;
201 GtkBindingSet *binding_set;
203 object_class = (GtkObjectClass*) class;
204 widget_class = (GtkWidgetClass*) class;
205 entry_class = (GtkEntryClass*) class;
207 parent_class = g_type_class_peek_parent (class);
209 gobject_class->finalize = gtk_spin_button_finalize;
211 gobject_class->set_property = gtk_spin_button_set_property;
212 gobject_class->get_property = gtk_spin_button_get_property;
214 object_class->destroy = gtk_spin_button_destroy;
216 widget_class->map = gtk_spin_button_map;
217 widget_class->unmap = gtk_spin_button_unmap;
218 widget_class->realize = gtk_spin_button_realize;
219 widget_class->unrealize = gtk_spin_button_unrealize;
220 widget_class->size_request = gtk_spin_button_size_request;
221 widget_class->size_allocate = gtk_spin_button_size_allocate;
222 widget_class->expose_event = gtk_spin_button_expose;
223 widget_class->scroll_event = gtk_spin_button_scroll;
224 widget_class->button_press_event = gtk_spin_button_button_press;
225 widget_class->button_release_event = gtk_spin_button_button_release;
226 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
227 widget_class->key_release_event = gtk_spin_button_key_release;
228 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
229 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
230 widget_class->focus_out_event = gtk_spin_button_focus_out;
231 widget_class->grab_notify = gtk_spin_button_grab_notify;
232 widget_class->state_changed = gtk_spin_button_state_changed;
234 entry_class->activate = gtk_spin_button_activate;
237 class->output = NULL;
238 class->change_value = gtk_spin_button_real_change_value;
240 g_object_class_install_property (gobject_class,
242 g_param_spec_object ("adjustment",
244 P_("The adjustment that holds the value of the spinbutton"),
246 GTK_PARAM_READWRITE));
248 g_object_class_install_property (gobject_class,
250 g_param_spec_double ("climb-rate",
252 P_("The acceleration rate when you hold down a button"),
256 GTK_PARAM_READWRITE));
258 g_object_class_install_property (gobject_class,
260 g_param_spec_uint ("digits",
262 P_("The number of decimal places to display"),
266 GTK_PARAM_READWRITE));
268 g_object_class_install_property (gobject_class,
270 g_param_spec_boolean ("snap-to-ticks",
272 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
274 GTK_PARAM_READWRITE));
276 g_object_class_install_property (gobject_class,
278 g_param_spec_boolean ("numeric",
280 P_("Whether non-numeric characters should be ignored"),
282 GTK_PARAM_READWRITE));
284 g_object_class_install_property (gobject_class,
286 g_param_spec_boolean ("wrap",
288 P_("Whether a spin button should wrap upon reaching its limits"),
290 GTK_PARAM_READWRITE));
292 g_object_class_install_property (gobject_class,
294 g_param_spec_enum ("update-policy",
296 P_("Whether the spin button should update always, or only when the value is legal"),
297 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
299 GTK_PARAM_READWRITE));
301 g_object_class_install_property (gobject_class,
303 g_param_spec_double ("value",
305 P_("Reads the current value, or sets a new value"),
309 GTK_PARAM_READWRITE));
311 gtk_widget_class_install_style_property_parser (widget_class,
312 g_param_spec_enum ("shadow-type",
314 P_("Style of bevel around the spin button"),
315 GTK_TYPE_SHADOW_TYPE,
318 gtk_rc_property_parse_enum);
319 spinbutton_signals[INPUT] =
320 g_signal_new ("input",
321 G_TYPE_FROM_CLASS (gobject_class),
323 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
325 _gtk_marshal_INT__POINTER,
329 spinbutton_signals[OUTPUT] =
330 g_signal_new ("output",
331 G_TYPE_FROM_CLASS (gobject_class),
333 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
334 _gtk_boolean_handled_accumulator, NULL,
335 _gtk_marshal_BOOLEAN__VOID,
338 spinbutton_signals[VALUE_CHANGED] =
339 g_signal_new ("value_changed",
340 G_TYPE_FROM_CLASS (gobject_class),
342 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
344 _gtk_marshal_VOID__VOID,
348 spinbutton_signals[CHANGE_VALUE] =
349 g_signal_new ("change_value",
350 G_TYPE_FROM_CLASS (gobject_class),
351 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
352 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
354 _gtk_marshal_VOID__ENUM,
356 GTK_TYPE_SCROLL_TYPE);
358 binding_set = gtk_binding_set_by_class (class);
360 add_spin_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
361 add_spin_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
362 add_spin_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
363 add_spin_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
364 add_spin_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
365 add_spin_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
366 add_spin_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
367 add_spin_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
371 gtk_spin_button_editable_init (GtkEditableClass *iface)
373 iface->insert_text = gtk_spin_button_insert_text;
377 gtk_spin_button_set_property (GObject *object,
382 GtkSpinButton *spin_button;
384 spin_button = GTK_SPIN_BUTTON (object);
388 GtkAdjustment *adjustment;
390 case PROP_ADJUSTMENT:
391 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
393 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
394 gtk_spin_button_set_adjustment (spin_button, adjustment);
396 case PROP_CLIMB_RATE:
397 gtk_spin_button_configure (spin_button,
398 spin_button->adjustment,
399 g_value_get_double (value),
400 spin_button->digits);
403 gtk_spin_button_configure (spin_button,
404 spin_button->adjustment,
405 spin_button->climb_rate,
406 g_value_get_uint (value));
408 case PROP_SNAP_TO_TICKS:
409 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
412 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
415 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
417 case PROP_UPDATE_POLICY:
418 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
421 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
429 gtk_spin_button_get_property (GObject *object,
434 GtkSpinButton *spin_button;
436 spin_button = GTK_SPIN_BUTTON (object);
440 case PROP_ADJUSTMENT:
441 g_value_set_object (value, spin_button->adjustment);
443 case PROP_CLIMB_RATE:
444 g_value_set_double (value, spin_button->climb_rate);
447 g_value_set_uint (value, spin_button->digits);
449 case PROP_SNAP_TO_TICKS:
450 g_value_set_boolean (value, spin_button->snap_to_ticks);
453 g_value_set_boolean (value, spin_button->numeric);
456 g_value_set_boolean (value, spin_button->wrap);
458 case PROP_UPDATE_POLICY:
459 g_value_set_enum (value, spin_button->update_policy);
462 g_value_set_double (value, spin_button->adjustment->value);
465 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
471 gtk_spin_button_init (GtkSpinButton *spin_button)
473 spin_button->adjustment = NULL;
474 spin_button->panel = NULL;
475 spin_button->timer = 0;
476 spin_button->climb_rate = 0.0;
477 spin_button->timer_step = 0.0;
478 spin_button->update_policy = GTK_UPDATE_ALWAYS;
479 spin_button->in_child = NO_ARROW;
480 spin_button->click_child = NO_ARROW;
481 spin_button->button = 0;
482 spin_button->need_timer = FALSE;
483 spin_button->timer_calls = 0;
484 spin_button->digits = 0;
485 spin_button->numeric = FALSE;
486 spin_button->wrap = FALSE;
487 spin_button->snap_to_ticks = FALSE;
489 gtk_spin_button_set_adjustment (spin_button,
490 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
494 gtk_spin_button_finalize (GObject *object)
496 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
498 G_OBJECT_CLASS (parent_class)->finalize (object);
502 gtk_spin_button_destroy (GtkObject *object)
504 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
506 GTK_OBJECT_CLASS (parent_class)->destroy (object);
510 gtk_spin_button_map (GtkWidget *widget)
512 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
514 GTK_WIDGET_CLASS (parent_class)->map (widget);
515 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
520 gtk_spin_button_unmap (GtkWidget *widget)
522 if (GTK_WIDGET_MAPPED (widget))
524 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
525 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
530 gtk_spin_button_realize (GtkWidget *widget)
532 GtkSpinButton *spin_button;
533 GdkWindowAttr attributes;
534 gint attributes_mask;
539 spin_button = GTK_SPIN_BUTTON (widget);
540 arrow_size = spin_button_get_arrow_size (spin_button);
542 real_width = widget->allocation.width;
543 widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
544 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
545 GDK_KEY_RELEASE_MASK);
546 GTK_WIDGET_CLASS (parent_class)->realize (widget);
548 widget->allocation.width = real_width;
550 attributes.window_type = GDK_WINDOW_CHILD;
551 attributes.wclass = GDK_INPUT_OUTPUT;
552 attributes.visual = gtk_widget_get_visual (widget);
553 attributes.colormap = gtk_widget_get_colormap (widget);
554 attributes.event_mask = gtk_widget_get_events (widget);
555 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
556 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
557 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
559 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
561 attributes.x = (widget->allocation.x +
562 widget->allocation.width - arrow_size -
563 2 * widget->style->xthickness);
564 attributes.y = widget->allocation.y + (widget->allocation.height -
565 widget->requisition.height) / 2;
566 attributes.width = arrow_size + 2 * widget->style->xthickness;
567 attributes.height = widget->requisition.height;
569 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
570 &attributes, attributes_mask);
571 gdk_window_set_user_data (spin_button->panel, widget);
573 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
576 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
577 if (return_val == FALSE)
578 gtk_spin_button_default_output (spin_button);
580 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
584 gtk_spin_button_unrealize (GtkWidget *widget)
586 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
588 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
592 gdk_window_set_user_data (spin->panel, NULL);
593 gdk_window_destroy (spin->panel);
599 compute_double_length (double val, int digits)
605 if (fabs (val) > 1.0)
606 a = floor (log10 (fabs (val))) + 1;
618 return a + digits + extra;
622 gtk_spin_button_size_request (GtkWidget *widget,
623 GtkRequisition *requisition)
626 GtkSpinButton *spin_button;
629 entry = GTK_ENTRY (widget);
630 spin_button = GTK_SPIN_BUTTON (widget);
631 arrow_size = spin_button_get_arrow_size (spin_button);
633 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
635 if (entry->width_chars < 0)
637 PangoContext *context;
638 PangoFontMetrics *metrics;
644 gboolean interior_focus;
646 gint xborder, yborder;
648 gtk_widget_style_get (widget,
649 "interior-focus", &interior_focus,
650 "focus-line-width", &focus_width,
653 context = gtk_widget_get_pango_context (widget);
654 metrics = pango_context_get_metrics (context,
655 widget->style->font_desc,
656 pango_context_get_language (context));
658 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
659 digit_width = PANGO_SCALE *
660 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
662 pango_font_metrics_unref (metrics);
664 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
666 width = MIN_SPIN_BUTTON_WIDTH;
667 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
668 spin_button->digits));
670 string_len = compute_double_length (spin_button->adjustment->upper,
671 spin_button->digits);
672 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
673 width = MAX (width, w);
674 string_len = compute_double_length (spin_button->adjustment->lower,
675 spin_button->digits);
676 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
677 width = MAX (width, w);
679 _gtk_entry_get_borders (entry, &xborder, &yborder);
681 xborder += 2; /* INNER_BORDER */
683 requisition->width = width + xborder * 2;
686 requisition->width += arrow_size + 2 * widget->style->xthickness;
690 gtk_spin_button_size_allocate (GtkWidget *widget,
691 GtkAllocation *allocation)
694 GtkAllocation entry_allocation;
695 GtkAllocation panel_allocation;
699 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
700 g_return_if_fail (allocation != NULL);
702 spin = GTK_SPIN_BUTTON (widget);
703 arrow_size = spin_button_get_arrow_size (spin);
704 panel_width = arrow_size + 2 * widget->style->xthickness;
706 widget->allocation = *allocation;
708 entry_allocation = *allocation;
709 entry_allocation.width -= panel_width;
711 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
713 entry_allocation.x += panel_width;
714 panel_allocation.x = allocation->x;
718 panel_allocation.x = allocation->x + allocation->width - panel_width;
721 panel_allocation.width = panel_width;
722 panel_allocation.height = MIN (widget->requisition.height, allocation->height);
724 panel_allocation.y = allocation->y + (allocation->height -
725 panel_allocation.height) / 2;
727 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &entry_allocation);
729 if (GTK_WIDGET_REALIZED (widget))
731 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
734 panel_allocation.width,
735 panel_allocation.height);
738 spin_button_redraw (spin);
742 gtk_spin_button_expose (GtkWidget *widget,
743 GdkEventExpose *event)
747 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
748 g_return_val_if_fail (event != NULL, FALSE);
750 spin = GTK_SPIN_BUTTON (widget);
752 if (GTK_WIDGET_DRAWABLE (widget))
754 GtkShadowType shadow_type;
757 if (event->window != spin->panel)
758 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
760 /* we redraw the panel even if it wasn't exposed. This is
761 * because spin->panel is not a child window of widget->window,
762 * so it will not be invalidated by eg. gtk_widget_queue_draw().
767 gdk_drawable_get_size (spin->panel, &rect.width, &rect.height);
769 shadow_type = spin_button_get_shadow_type (spin);
771 gdk_window_begin_paint_rect (spin->panel, &rect);
773 if (shadow_type != GTK_SHADOW_NONE)
775 gtk_paint_box (widget->style, spin->panel,
776 GTK_STATE_NORMAL, shadow_type,
777 NULL, widget, "spinbutton",
778 rect.x, rect.y, rect.width, rect.height);
781 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
782 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
784 gdk_window_end_paint (spin->panel);
791 spin_button_at_limit (GtkSpinButton *spin_button,
794 GtkArrowType effective_arrow;
796 if (spin_button->wrap)
799 if (spin_button->adjustment->step_increment > 0)
800 effective_arrow = arrow;
802 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
804 if (effective_arrow == GTK_ARROW_UP &&
805 (spin_button->adjustment->upper - spin_button->adjustment->value <= EPSILON))
808 if (effective_arrow == GTK_ARROW_DOWN &&
809 (spin_button->adjustment->value - spin_button->adjustment->lower <= EPSILON))
816 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
817 GtkArrowType arrow_type)
819 GtkStateType state_type;
820 GtkShadowType shadow_type;
828 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
829 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
831 widget = GTK_WIDGET (spin_button);
833 if (GTK_WIDGET_DRAWABLE (widget))
835 width = spin_button_get_arrow_size (spin_button) + 2 * widget->style->xthickness;
837 if (arrow_type == GTK_ARROW_UP)
842 height = widget->requisition.height / 2;
847 y = widget->requisition.height / 2;
849 height = (widget->requisition.height + 1) / 2;
852 if (spin_button_at_limit (spin_button, arrow_type))
854 shadow_type = GTK_SHADOW_OUT;
855 state_type = GTK_STATE_INSENSITIVE;
859 if (spin_button->click_child == arrow_type)
861 state_type = GTK_STATE_ACTIVE;
862 shadow_type = GTK_SHADOW_IN;
866 if (spin_button->in_child == arrow_type &&
867 spin_button->click_child == NO_ARROW)
869 state_type = GTK_STATE_PRELIGHT;
873 state_type = GTK_WIDGET_STATE (widget);
876 shadow_type = GTK_SHADOW_OUT;
880 gtk_paint_box (widget->style, spin_button->panel,
881 state_type, shadow_type,
883 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
884 x, y, width, height);
886 height = widget->requisition.height;
888 if (arrow_type == GTK_ARROW_DOWN)
891 height = height - y - 2;
896 height = height / 2 - 2;
901 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
907 w -= w % 2 - 1; /* force odd */
910 x += (width - w) / 2;
911 y += (height - h) / 2;
916 gtk_paint_arrow (widget->style, spin_button->panel,
917 state_type, shadow_type,
918 NULL, widget, "spinbutton",
920 x, y, width, height);
925 gtk_spin_button_enter_notify (GtkWidget *widget,
926 GdkEventCrossing *event)
928 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
930 if (event->window == spin->panel)
935 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
937 if (y <= widget->requisition.height / 2)
938 spin->in_child = GTK_ARROW_UP;
940 spin->in_child = GTK_ARROW_DOWN;
942 spin_button_redraw (spin);
949 gtk_spin_button_leave_notify (GtkWidget *widget,
950 GdkEventCrossing *event)
952 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
954 spin->in_child = NO_ARROW;
955 spin_button_redraw (spin);
961 gtk_spin_button_focus_out (GtkWidget *widget,
962 GdkEventFocus *event)
964 if (GTK_ENTRY (widget)->editable)
965 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
967 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
971 gtk_spin_button_grab_notify (GtkWidget *widget,
972 gboolean was_grabbed)
974 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
978 gtk_spin_button_stop_spinning (spin);
979 spin_button_redraw (spin);
984 gtk_spin_button_state_changed (GtkWidget *widget,
985 GtkStateType previous_state)
987 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
989 if (!GTK_WIDGET_IS_SENSITIVE (widget))
991 gtk_spin_button_stop_spinning (spin);
992 spin_button_redraw (spin);
997 gtk_spin_button_scroll (GtkWidget *widget,
998 GdkEventScroll *event)
1000 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1002 if (event->direction == GDK_SCROLL_UP)
1004 if (!GTK_WIDGET_HAS_FOCUS (widget))
1005 gtk_widget_grab_focus (widget);
1006 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
1008 else if (event->direction == GDK_SCROLL_DOWN)
1010 if (!GTK_WIDGET_HAS_FOCUS (widget))
1011 gtk_widget_grab_focus (widget);
1012 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
1021 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1025 g_source_remove (spin->timer);
1027 spin->timer_calls = 0;
1028 spin->need_timer = FALSE;
1033 spin->timer_step = spin->adjustment->step_increment;
1034 spin->timer_calls = 0;
1036 spin->click_child = NO_ARROW;
1041 start_spinning (GtkSpinButton *spin,
1042 GtkArrowType click_child,
1045 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1047 spin->click_child = click_child;
1051 spin->timer_step = step;
1052 spin->need_timer = TRUE;
1053 spin->timer = g_timeout_add (SPIN_BUTTON_INITIAL_TIMER_DELAY,
1054 (GSourceFunc) gtk_spin_button_timer,
1057 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1059 spin_button_redraw (spin);
1063 gtk_spin_button_button_press (GtkWidget *widget,
1064 GdkEventButton *event)
1066 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1070 if (event->window == spin->panel)
1072 if (!GTK_WIDGET_HAS_FOCUS (widget))
1073 gtk_widget_grab_focus (widget);
1074 spin->button = event->button;
1076 if (GTK_ENTRY (widget)->editable)
1077 gtk_spin_button_update (spin);
1079 if (event->y <= widget->requisition.height / 2)
1081 if (event->button == 1)
1082 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->step_increment);
1083 else if (event->button == 2)
1084 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->page_increment);
1086 spin->click_child = GTK_ARROW_UP;
1090 if (event->button == 1)
1091 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->step_increment);
1092 else if (event->button == 2)
1093 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->page_increment);
1095 spin->click_child = GTK_ARROW_DOWN;
1100 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1106 gtk_spin_button_button_release (GtkWidget *widget,
1107 GdkEventButton *event)
1109 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1112 arrow_size = spin_button_get_arrow_size (spin);
1114 if (event->button == spin->button)
1116 int click_child = spin->click_child;
1118 gtk_spin_button_stop_spinning (spin);
1120 if (event->button == 3)
1122 if (event->y >= 0 && event->x >= 0 &&
1123 event->y <= widget->requisition.height &&
1124 event->x <= arrow_size + 2 * widget->style->xthickness)
1126 if (click_child == GTK_ARROW_UP &&
1127 event->y <= widget->requisition.height / 2)
1131 diff = spin->adjustment->upper - spin->adjustment->value;
1133 gtk_spin_button_real_spin (spin, diff);
1135 else if (click_child == GTK_ARROW_DOWN &&
1136 event->y > widget->requisition.height / 2)
1140 diff = spin->adjustment->value - spin->adjustment->lower;
1142 gtk_spin_button_real_spin (spin, -diff);
1146 spin_button_redraw (spin);
1151 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1155 gtk_spin_button_motion_notify (GtkWidget *widget,
1156 GdkEventMotion *event)
1158 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1163 if (event->window == spin->panel)
1167 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1169 if (y <= widget->requisition.height / 2 &&
1170 spin->in_child == GTK_ARROW_DOWN)
1172 spin->in_child = GTK_ARROW_UP;
1173 spin_button_redraw (spin);
1175 else if (y > widget->requisition.height / 2 &&
1176 spin->in_child == GTK_ARROW_UP)
1178 spin->in_child = GTK_ARROW_DOWN;
1179 spin_button_redraw (spin);
1185 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1189 gtk_spin_button_timer (GtkSpinButton *spin_button)
1191 gboolean retval = FALSE;
1193 GDK_THREADS_ENTER ();
1195 if (spin_button->timer)
1197 if (spin_button->click_child == GTK_ARROW_UP)
1198 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1200 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1202 if (spin_button->need_timer)
1204 spin_button->need_timer = FALSE;
1205 spin_button->timer = g_timeout_add (SPIN_BUTTON_TIMER_DELAY,
1206 (GSourceFunc) gtk_spin_button_timer,
1207 (gpointer) spin_button);
1211 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1212 < spin_button->adjustment->page_increment)
1214 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1215 spin_button->timer_calls++;
1218 spin_button->timer_calls = 0;
1219 spin_button->timer_step += spin_button->climb_rate;
1226 GDK_THREADS_LEAVE ();
1232 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1233 GtkSpinButton *spin_button)
1235 gboolean return_val;
1237 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1240 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1241 if (return_val == FALSE)
1242 gtk_spin_button_default_output (spin_button);
1244 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1246 spin_button_redraw (spin_button);
1248 g_object_notify (G_OBJECT (spin_button), "value");
1252 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1253 GtkScrollType scroll)
1255 /* We don't test whether the entry is editable, since
1256 * this key binding conceptually corresponds to changing
1257 * the value with the buttons using the mouse, which
1258 * we allow for non-editable spin buttons.
1262 case GTK_SCROLL_STEP_BACKWARD:
1263 case GTK_SCROLL_STEP_DOWN:
1264 case GTK_SCROLL_STEP_LEFT:
1265 gtk_spin_button_real_spin (spin, -spin->timer_step);
1267 if (spin->climb_rate > 0.0 && spin->timer_step
1268 < spin->adjustment->page_increment)
1270 if (spin->timer_calls < MAX_TIMER_CALLS)
1271 spin->timer_calls++;
1274 spin->timer_calls = 0;
1275 spin->timer_step += spin->climb_rate;
1280 case GTK_SCROLL_STEP_FORWARD:
1281 case GTK_SCROLL_STEP_UP:
1282 case GTK_SCROLL_STEP_RIGHT:
1283 gtk_spin_button_real_spin (spin, spin->timer_step);
1285 if (spin->climb_rate > 0.0 && spin->timer_step
1286 < spin->adjustment->page_increment)
1288 if (spin->timer_calls < MAX_TIMER_CALLS)
1289 spin->timer_calls++;
1292 spin->timer_calls = 0;
1293 spin->timer_step += spin->climb_rate;
1298 case GTK_SCROLL_PAGE_BACKWARD:
1299 case GTK_SCROLL_PAGE_DOWN:
1300 case GTK_SCROLL_PAGE_LEFT:
1301 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1304 case GTK_SCROLL_PAGE_FORWARD:
1305 case GTK_SCROLL_PAGE_UP:
1306 case GTK_SCROLL_PAGE_RIGHT:
1307 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1310 case GTK_SCROLL_START:
1312 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1314 gtk_spin_button_real_spin (spin, -diff);
1318 case GTK_SCROLL_END:
1320 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1322 gtk_spin_button_real_spin (spin, diff);
1327 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1331 gtk_spin_button_update (spin);
1335 gtk_spin_button_key_release (GtkWidget *widget,
1338 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1340 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1341 spin->timer_step = spin->adjustment->step_increment;
1342 spin->timer_calls = 0;
1348 gtk_spin_button_snap (GtkSpinButton *spin_button,
1354 inc = spin_button->adjustment->step_increment;
1358 tmp = (val - spin_button->adjustment->lower) / inc;
1359 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1360 val = spin_button->adjustment->lower + floor (tmp) * inc;
1362 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1364 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1365 gtk_adjustment_set_value (spin_button->adjustment, val);
1368 gint return_val = FALSE;
1369 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1370 if (return_val == FALSE)
1371 gtk_spin_button_default_output (spin_button);
1376 gtk_spin_button_activate (GtkEntry *entry)
1378 if (entry->editable)
1379 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1381 /* Chain up so that entry->activates_default is honored */
1382 parent_class->activate (entry);
1386 gtk_spin_button_insert_text (GtkEditable *editable,
1387 const gchar *new_text,
1388 gint new_text_length,
1391 GtkEntry *entry = GTK_ENTRY (editable);
1392 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1393 GtkEditableClass *parent_editable_iface = g_type_interface_peek (parent_class, GTK_TYPE_EDITABLE);
1405 entry_length = entry->text_length;
1409 if (*(lc->negative_sign))
1410 neg_sign = *(lc->negative_sign);
1414 if (*(lc->positive_sign))
1415 pos_sign = *(lc->positive_sign);
1419 for (sign=0, i=0; i<entry_length; i++)
1420 if ((entry->text[i] == neg_sign) ||
1421 (entry->text[i] == pos_sign))
1427 if (sign && !(*position))
1430 for (dotpos=-1, i=0; i<entry_length; i++)
1431 if (entry->text[i] == *(lc->decimal_point))
1437 if (dotpos > -1 && *position > dotpos &&
1438 (gint)spin->digits - entry_length
1439 + dotpos - new_text_length + 1 < 0)
1442 for (i = 0; i < new_text_length; i++)
1444 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1446 if (sign || (*position) || i)
1450 else if (new_text[i] == *(lc->decimal_point))
1452 if (!spin->digits || dotpos > -1 ||
1453 (new_text_length - 1 - i + entry_length
1454 - *position > (gint)spin->digits))
1456 dotpos = *position + i;
1458 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1463 parent_editable_iface->insert_text (editable, new_text,
1464 new_text_length, position);
1468 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1472 gdouble new_value = 0.0;
1474 adj = spin_button->adjustment;
1476 new_value = adj->value + increment;
1480 if (spin_button->wrap)
1482 if (fabs (adj->value - adj->upper) < EPSILON)
1483 new_value = adj->lower;
1484 else if (new_value > adj->upper)
1485 new_value = adj->upper;
1488 new_value = MIN (new_value, adj->upper);
1490 else if (increment < 0)
1492 if (spin_button->wrap)
1494 if (fabs (adj->value - adj->lower) < EPSILON)
1495 new_value = adj->upper;
1496 else if (new_value < adj->lower)
1497 new_value = adj->lower;
1500 new_value = MAX (new_value, adj->lower);
1503 if (fabs (new_value - adj->value) > EPSILON)
1504 gtk_adjustment_set_value (adj, new_value);
1506 spin_button_redraw (spin_button);
1510 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1515 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1517 return GTK_INPUT_ERROR;
1523 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1525 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1527 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1528 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1534 /***********************************************************
1535 ***********************************************************
1536 *** Public interface ***
1537 ***********************************************************
1538 ***********************************************************/
1542 gtk_spin_button_configure (GtkSpinButton *spin_button,
1543 GtkAdjustment *adjustment,
1547 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1550 gtk_spin_button_set_adjustment (spin_button, adjustment);
1552 adjustment = spin_button->adjustment;
1554 g_object_freeze_notify (G_OBJECT (spin_button));
1555 if (spin_button->digits != digits)
1557 spin_button->digits = digits;
1558 g_object_notify (G_OBJECT (spin_button), "digits");
1561 if (spin_button->climb_rate != climb_rate)
1563 spin_button->climb_rate = climb_rate;
1564 g_object_notify (G_OBJECT (spin_button), "climb_rate");
1566 g_object_thaw_notify (G_OBJECT (spin_button));
1568 gtk_adjustment_value_changed (adjustment);
1572 gtk_spin_button_new (GtkAdjustment *adjustment,
1576 GtkSpinButton *spin;
1579 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1581 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1583 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1585 return GTK_WIDGET (spin);
1589 * gtk_spin_button_new_with_range:
1590 * @min: Minimum allowable value
1591 * @max: Maximum allowable value
1592 * @step: Increment added or subtracted by spinning the widget
1594 * This is a convenience constructor that allows creation of a numeric
1595 * #GtkSpinButton without manually creating an adjustment. The value is
1596 * initially set to the minimum value and a page increment of 10 * @step
1597 * is the default. The precision of the spin button is equivalent to the
1598 * precision of @step.
1600 * Return value: The new spin button as a #GtkWidget.
1603 gtk_spin_button_new_with_range (gdouble min,
1608 GtkSpinButton *spin;
1611 g_return_val_if_fail (min <= max, NULL);
1612 g_return_val_if_fail (step != 0.0, NULL);
1614 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1616 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1618 if (fabs (step) >= 1.0 || step == 0.0)
1621 digits = abs ((gint) floor (log10 (fabs (step))));
1622 if (digits > MAX_DIGITS)
1623 digits = MAX_DIGITS;
1626 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1628 gtk_spin_button_set_numeric (spin, TRUE);
1630 return GTK_WIDGET (spin);
1633 /* Callback used when the spin button's adjustment changes. We need to redraw
1634 * the arrows when the adjustment's range changes, and reevaluate our size request.
1637 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1639 GtkSpinButton *spin_button;
1641 spin_button = GTK_SPIN_BUTTON (data);
1643 spin_button->timer_step = spin_button->adjustment->step_increment;
1644 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1648 * gtk_spin_button_set_adjustment:
1649 * @spin_button: a #GtkSpinButton
1650 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1652 * Replaces the #GtkAdjustment associated with @spin_button.
1655 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1656 GtkAdjustment *adjustment)
1658 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1660 if (spin_button->adjustment != adjustment)
1662 if (spin_button->adjustment)
1664 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1665 gtk_spin_button_value_changed,
1667 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1668 adjustment_changed_cb,
1670 g_object_unref (spin_button->adjustment);
1672 spin_button->adjustment = adjustment;
1675 g_object_ref (adjustment);
1676 gtk_object_sink (GTK_OBJECT (adjustment));
1677 g_signal_connect (adjustment, "value_changed",
1678 G_CALLBACK (gtk_spin_button_value_changed),
1680 g_signal_connect (adjustment, "changed",
1681 G_CALLBACK (adjustment_changed_cb),
1683 spin_button->timer_step = spin_button->adjustment->step_increment;
1686 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1689 g_object_notify (G_OBJECT (spin_button), "adjustment");
1693 * gtk_spin_button_get_adjustment:
1696 * Get the adjustment associated with a #GtkSpinButton
1698 * Return value: the #GtkAdjustment of @spin_button
1701 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1703 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1705 return spin_button->adjustment;
1709 * gtk_spin_button_set_digits:
1710 * @spin_button: a #GtkSpinButton
1711 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1713 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1717 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1720 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1722 if (spin_button->digits != digits)
1724 spin_button->digits = digits;
1725 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1726 g_object_notify (G_OBJECT (spin_button), "digits");
1728 /* since lower/upper may have changed */
1729 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1734 * gtk_spin_button_get_digits:
1735 * @spin_button: a #GtkSpinButton
1737 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1739 * Returns: the current precision
1742 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1744 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1746 return spin_button->digits;
1750 * gtk_spin_button_set_increments:
1751 * @spin_button: a #GtkSpinButton
1752 * @step: increment applied for a button 1 press.
1753 * @page: increment applied for a button 2 press.
1755 * Sets the step and page increments for spin_button. This affects how
1756 * quickly the value changes when the spin button's arrows are activated.
1759 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1763 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1765 spin_button->adjustment->step_increment = step;
1766 spin_button->adjustment->page_increment = page;
1770 * gtk_spin_button_get_increments:
1771 * @spin_button: a #GtkSpinButton
1772 * @step: location to store step increment, or %NULL
1773 * @page: location to store page increment, or %NULL
1775 * Gets the current step and page the increments used by @spin_button. See
1776 * gtk_spin_button_set_increments().
1779 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1783 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1786 *step = spin_button->adjustment->step_increment;
1788 *page = spin_button->adjustment->page_increment;
1792 * gtk_spin_button_set_range:
1793 * @spin_button: a #GtkSpinButton
1794 * @min: minimum allowable value
1795 * @max: maximum allowable value
1797 * Sets the minimum and maximum allowable values for @spin_button
1800 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1806 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1808 spin_button->adjustment->lower = min;
1809 spin_button->adjustment->upper = max;
1811 value = CLAMP (spin_button->adjustment->value,
1812 spin_button->adjustment->lower,
1813 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1815 if (value != spin_button->adjustment->value)
1816 gtk_spin_button_set_value (spin_button, value);
1818 gtk_adjustment_changed (spin_button->adjustment);
1822 * gtk_spin_button_get_range:
1823 * @spin_button: a #GtkSpinButton
1824 * @min: location to store minimum allowed value, or %NULL
1825 * @max: location to store maximum allowed value, or %NULL
1827 * Gets the range allowed for @spin_button. See
1828 * gtk_spin_button_set_range().
1831 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1835 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1838 *min = spin_button->adjustment->lower;
1840 *max = spin_button->adjustment->upper;
1844 * gtk_spin_button_get_value:
1845 * @spin_button: a #GtkSpinButton
1847 * Get the value in the @spin_button.
1849 * Return value: the value of @spin_button
1852 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1854 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1856 return spin_button->adjustment->value;
1860 * gtk_spin_button_get_value_as_int:
1861 * @spin_button: a #GtkSpinButton
1863 * Get the value @spin_button represented as an integer.
1865 * Return value: the value of @spin_button
1868 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1872 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1874 val = spin_button->adjustment->value;
1875 if (val - floor (val) < ceil (val) - val)
1882 * gtk_spin_button_set_value:
1883 * @spin_button: a #GtkSpinButton
1884 * @value: the new value
1886 * Set the value of @spin_button.
1889 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1892 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1894 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1895 gtk_adjustment_set_value (spin_button->adjustment, value);
1898 gint return_val = FALSE;
1899 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1900 if (return_val == FALSE)
1901 gtk_spin_button_default_output (spin_button);
1906 * gtk_spin_button_set_update_policy:
1907 * @spin_button: a #GtkSpinButton
1908 * @policy: a #GtkSpinButtonUpdatePolicy value
1910 * Sets the update behavior of a spin button. This determines whether the
1911 * spin button is always updated or only when a valid value is set.
1914 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1915 GtkSpinButtonUpdatePolicy policy)
1917 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1919 if (spin_button->update_policy != policy)
1921 spin_button->update_policy = policy;
1922 g_object_notify (G_OBJECT (spin_button), "update_policy");
1927 * gtk_spin_button_get_update_policy:
1928 * @spin_button: a #GtkSpinButton
1930 * Gets the update behavior of a spin button. See
1931 * gtk_spin_button_set_update_policy().
1933 * Return value: the current update policy
1935 GtkSpinButtonUpdatePolicy
1936 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1938 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1940 return spin_button->update_policy;
1944 * gtk_spin_button_set_numeric:
1945 * @spin_button: a #GtkSpinButton
1946 * @numeric: flag indicating if only numeric entry is allowed.
1948 * Sets the flag that determines if non-numeric text can be typed into
1952 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1955 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1957 numeric = numeric != FALSE;
1959 if (spin_button->numeric != numeric)
1961 spin_button->numeric = numeric;
1962 g_object_notify (G_OBJECT (spin_button), "numeric");
1967 * gtk_spin_button_get_numeric:
1968 * @spin_button: a #GtkSpinButton
1970 * Returns whether non-numeric text can be typed into the spin button.
1971 * See gtk_spin_button_set_numeric().
1973 * Return value: %TRUE if only numeric text can be entered
1976 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1978 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1980 return spin_button->numeric;
1984 * gtk_spin_button_set_wrap:
1985 * @spin_button: a #GtkSpinButton
1986 * @wrap: a flag indicating if wrapping behavior is performed.
1988 * Sets the flag that determines if a spin button value wraps around to the
1989 * opposite limit when the upper or lower limit of the range is exceeded.
1992 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1995 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1997 wrap = wrap != FALSE;
1999 if (spin_button->wrap != wrap)
2001 spin_button->wrap = (wrap != 0);
2003 g_object_notify (G_OBJECT (spin_button), "wrap");
2008 * gtk_spin_button_get_wrap:
2009 * @spin_button: a #GtkSpinButton
2011 * Returns whether the spin button's value wraps around to the
2012 * opposite limit when the upper or lower limit of the range is
2013 * exceeded. See gtk_spin_button_set_wrap().
2015 * Return value: %TRUE if the spin button wraps around
2018 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2020 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2022 return spin_button->wrap;
2026 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2028 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
2031 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2033 return arrow_size - arrow_size % 2; /* force even */
2037 * spin_button_get_shadow_type:
2038 * @spin_button: a #GtkSpinButton
2040 * Convenience function to Get the shadow type from the underlying widget's
2043 * Return value: the #GtkShadowType
2046 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2048 GtkShadowType rc_shadow_type;
2050 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
2052 return rc_shadow_type;
2056 * gtk_spin_button_set_snap_to_ticks:
2057 * @spin_button: a #GtkSpinButton
2058 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2060 * Sets the policy as to whether values are corrected to the nearest step
2061 * increment when a spin button is activated after providing an invalid value.
2064 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2065 gboolean snap_to_ticks)
2069 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2071 new_val = (snap_to_ticks != 0);
2073 if (new_val != spin_button->snap_to_ticks)
2075 spin_button->snap_to_ticks = new_val;
2076 if (new_val && GTK_ENTRY (spin_button)->editable)
2077 gtk_spin_button_update (spin_button);
2079 g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
2084 * gtk_spin_button_get_snap_to_ticks:
2085 * @spin_button: a #GtkSpinButton
2087 * Returns whether the values are corrected to the nearest step. See
2088 * gtk_spin_button_set_snap_to_ticks().
2090 * Return value: %TRUE if values are snapped to the nearest step.
2093 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2095 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2097 return spin_button->snap_to_ticks;
2101 * gtk_spin_button_spin:
2102 * @spin_button: a #GtkSpinButton
2103 * @direction: a #GtkSpinType indicating the direction to spin.
2104 * @increment: step increment to apply in the specified direction.
2106 * Increment or decrement a spin button's value in a specified direction
2107 * by a specified amount.
2110 gtk_spin_button_spin (GtkSpinButton *spin_button,
2111 GtkSpinType direction,
2117 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2119 adj = spin_button->adjustment;
2121 /* for compatibility with the 1.0.x version of this function */
2122 if (increment != 0 && increment != adj->step_increment &&
2123 (direction == GTK_SPIN_STEP_FORWARD ||
2124 direction == GTK_SPIN_STEP_BACKWARD))
2126 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2127 increment = -increment;
2128 direction = GTK_SPIN_USER_DEFINED;
2133 case GTK_SPIN_STEP_FORWARD:
2135 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2138 case GTK_SPIN_STEP_BACKWARD:
2140 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2143 case GTK_SPIN_PAGE_FORWARD:
2145 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2148 case GTK_SPIN_PAGE_BACKWARD:
2150 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2155 diff = adj->value - adj->lower;
2157 gtk_spin_button_real_spin (spin_button, -diff);
2162 diff = adj->upper - adj->value;
2164 gtk_spin_button_real_spin (spin_button, diff);
2167 case GTK_SPIN_USER_DEFINED:
2170 gtk_spin_button_real_spin (spin_button, increment);
2179 * gtk_spin_button_update:
2180 * @spin_button: a #GtkSpinButton
2182 * Manually force an update of the spin button.
2185 gtk_spin_button_update (GtkSpinButton *spin_button)
2191 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2194 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2195 if (return_val == FALSE)
2197 return_val = gtk_spin_button_default_input (spin_button, &val);
2198 error = (return_val == GTK_INPUT_ERROR);
2200 else if (return_val == GTK_INPUT_ERROR)
2203 spin_button_redraw (spin_button);
2205 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2207 if (val < spin_button->adjustment->lower)
2208 val = spin_button->adjustment->lower;
2209 else if (val > spin_button->adjustment->upper)
2210 val = spin_button->adjustment->upper;
2212 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2214 val < spin_button->adjustment->lower ||
2215 val > spin_button->adjustment->upper))
2217 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2221 if (spin_button->snap_to_ticks)
2222 gtk_spin_button_snap (spin_button, val);
2225 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2226 gtk_adjustment_set_value (spin_button->adjustment, val);
2230 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0,
2232 if (return_val == FALSE)
2233 gtk_spin_button_default_output (spin_button);
2239 spin_button_redraw (GtkSpinButton *spin_button)
2243 widget = GTK_WIDGET (spin_button);
2245 if (GTK_WIDGET_DRAWABLE (widget))
2247 gtk_widget_queue_draw (widget);
2249 /* We must invalidate the panel window ourselves, because it
2250 * is not a child of widget->window
2252 gdk_window_invalidate_rect (spin_button->panel, NULL, TRUE);
2256 #define __GTK_SPIN_BUTTON_C__
2257 #include "gtkaliasdef.c"