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"
38 #include "gtkbindings.h"
39 #include "gtkspinbutton.h"
41 #include "gtkmarshalers.h"
42 #include "gtksettings.h"
45 #define MIN_SPIN_BUTTON_WIDTH 30
46 #define SPIN_BUTTON_INITIAL_TIMER_DELAY 200
47 #define SPIN_BUTTON_TIMER_DELAY 20
48 #define MAX_TIMER_CALLS 5
51 #define MIN_ARROW_WIDTH 6
75 static void gtk_spin_button_class_init (GtkSpinButtonClass *klass);
76 static void gtk_spin_button_editable_init (GtkEditableClass *iface);
77 static void gtk_spin_button_init (GtkSpinButton *spin_button);
78 static void gtk_spin_button_finalize (GObject *object);
79 static void gtk_spin_button_destroy (GtkObject *object);
80 static void gtk_spin_button_set_property (GObject *object,
84 static void gtk_spin_button_get_property (GObject *object,
88 static void gtk_spin_button_map (GtkWidget *widget);
89 static void gtk_spin_button_unmap (GtkWidget *widget);
90 static void gtk_spin_button_realize (GtkWidget *widget);
91 static void gtk_spin_button_unrealize (GtkWidget *widget);
92 static void gtk_spin_button_size_request (GtkWidget *widget,
93 GtkRequisition *requisition);
94 static void gtk_spin_button_size_allocate (GtkWidget *widget,
95 GtkAllocation *allocation);
96 static gint gtk_spin_button_expose (GtkWidget *widget,
97 GdkEventExpose *event);
98 static gint gtk_spin_button_button_press (GtkWidget *widget,
99 GdkEventButton *event);
100 static gint gtk_spin_button_button_release (GtkWidget *widget,
101 GdkEventButton *event);
102 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
103 GdkEventMotion *event);
104 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
105 GdkEventCrossing *event);
106 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
107 GdkEventCrossing *event);
108 static gint gtk_spin_button_focus_out (GtkWidget *widget,
109 GdkEventFocus *event);
110 static void gtk_spin_button_grab_notify (GtkWidget *widget,
111 gboolean was_grabbed);
112 static void gtk_spin_button_state_changed (GtkWidget *widget,
113 GtkStateType previous_state);
114 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
115 GtkArrowType arrow_type);
116 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
117 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
118 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
119 GtkSpinButton *spin_button);
120 static gint gtk_spin_button_key_release (GtkWidget *widget,
122 static gint gtk_spin_button_scroll (GtkWidget *widget,
123 GdkEventScroll *event);
124 static void gtk_spin_button_activate (GtkEntry *entry);
125 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
127 static void gtk_spin_button_insert_text (GtkEditable *editable,
128 const gchar *new_text,
129 gint new_text_length,
131 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
133 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
134 GtkScrollType scroll);
136 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
138 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
140 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
141 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
142 static void spin_button_redraw (GtkSpinButton *spin_button);
145 static GtkEntryClass *parent_class = NULL;
146 static guint spinbutton_signals[LAST_SIGNAL] = {0};
151 gtk_spin_button_get_type (void)
153 static GType spin_button_type = 0;
155 if (!spin_button_type)
157 static const GTypeInfo spin_button_info =
159 sizeof (GtkSpinButtonClass),
160 NULL, /* base_init */
161 NULL, /* base_finalize */
162 (GClassInitFunc) gtk_spin_button_class_init,
163 NULL, /* class_finalize */
164 NULL, /* class_data */
165 sizeof (GtkSpinButton),
167 (GInstanceInitFunc) gtk_spin_button_init,
170 static const GInterfaceInfo editable_info =
172 (GInterfaceInitFunc) gtk_spin_button_editable_init, /* interface_init */
173 NULL, /* interface_finalize */
174 NULL /* interface_data */
178 g_type_register_static (GTK_TYPE_ENTRY, "GtkSpinButton",
179 &spin_button_info, 0);
181 g_type_add_interface_static (spin_button_type,
185 return spin_button_type;
188 #define add_spin_binding(binding_set, keyval, mask, scroll) \
189 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
191 GTK_TYPE_SCROLL_TYPE, scroll)
194 gtk_spin_button_class_init (GtkSpinButtonClass *class)
196 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
197 GtkObjectClass *object_class;
198 GtkWidgetClass *widget_class;
199 GtkEntryClass *entry_class;
200 GtkBindingSet *binding_set;
202 object_class = (GtkObjectClass*) class;
203 widget_class = (GtkWidgetClass*) class;
204 entry_class = (GtkEntryClass*) class;
206 parent_class = g_type_class_peek_parent (class);
208 gobject_class->finalize = gtk_spin_button_finalize;
210 gobject_class->set_property = gtk_spin_button_set_property;
211 gobject_class->get_property = gtk_spin_button_get_property;
213 object_class->destroy = gtk_spin_button_destroy;
215 widget_class->map = gtk_spin_button_map;
216 widget_class->unmap = gtk_spin_button_unmap;
217 widget_class->realize = gtk_spin_button_realize;
218 widget_class->unrealize = gtk_spin_button_unrealize;
219 widget_class->size_request = gtk_spin_button_size_request;
220 widget_class->size_allocate = gtk_spin_button_size_allocate;
221 widget_class->expose_event = gtk_spin_button_expose;
222 widget_class->scroll_event = gtk_spin_button_scroll;
223 widget_class->button_press_event = gtk_spin_button_button_press;
224 widget_class->button_release_event = gtk_spin_button_button_release;
225 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
226 widget_class->key_release_event = gtk_spin_button_key_release;
227 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
228 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
229 widget_class->focus_out_event = gtk_spin_button_focus_out;
230 widget_class->grab_notify = gtk_spin_button_grab_notify;
231 widget_class->state_changed = gtk_spin_button_state_changed;
233 entry_class->activate = gtk_spin_button_activate;
236 class->output = NULL;
237 class->change_value = gtk_spin_button_real_change_value;
239 g_object_class_install_property (gobject_class,
241 g_param_spec_object ("adjustment",
243 P_("The adjustment that holds the value of the spinbutton"),
247 g_object_class_install_property (gobject_class,
249 g_param_spec_double ("climb_rate",
251 P_("The acceleration rate when you hold down a button"),
257 g_object_class_install_property (gobject_class,
259 g_param_spec_uint ("digits",
261 P_("The number of decimal places to display"),
267 g_object_class_install_property (gobject_class,
269 g_param_spec_boolean ("snap_to_ticks",
271 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
275 g_object_class_install_property (gobject_class,
277 g_param_spec_boolean ("numeric",
279 P_("Whether non-numeric characters should be ignored"),
283 g_object_class_install_property (gobject_class,
285 g_param_spec_boolean ("wrap",
287 P_("Whether a spin button should wrap upon reaching its limits"),
291 g_object_class_install_property (gobject_class,
293 g_param_spec_enum ("update_policy",
295 P_("Whether the spin button should update always, or only when the value is legal"),
296 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
300 g_object_class_install_property (gobject_class,
302 g_param_spec_double ("value",
304 P_("Reads the current value, or sets a new value"),
310 gtk_widget_class_install_style_property_parser (widget_class,
311 g_param_spec_enum ("shadow_type",
313 P_("Style of bevel around the spin button"),
314 GTK_TYPE_SHADOW_TYPE,
317 gtk_rc_property_parse_enum);
318 spinbutton_signals[INPUT] =
319 g_signal_new ("input",
320 G_TYPE_FROM_CLASS (gobject_class),
322 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
324 _gtk_marshal_INT__POINTER,
328 spinbutton_signals[OUTPUT] =
329 g_signal_new ("output",
330 G_TYPE_FROM_CLASS (gobject_class),
332 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
333 _gtk_boolean_handled_accumulator, NULL,
334 _gtk_marshal_BOOLEAN__VOID,
337 spinbutton_signals[VALUE_CHANGED] =
338 g_signal_new ("value_changed",
339 G_TYPE_FROM_CLASS (gobject_class),
341 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
343 _gtk_marshal_VOID__VOID,
347 spinbutton_signals[CHANGE_VALUE] =
348 g_signal_new ("change_value",
349 G_TYPE_FROM_CLASS (gobject_class),
350 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
351 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
353 _gtk_marshal_VOID__ENUM,
355 GTK_TYPE_SCROLL_TYPE);
357 binding_set = gtk_binding_set_by_class (class);
359 add_spin_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
360 add_spin_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
361 add_spin_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
362 add_spin_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
363 add_spin_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
364 add_spin_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
365 add_spin_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
366 add_spin_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
370 gtk_spin_button_editable_init (GtkEditableClass *iface)
372 iface->insert_text = gtk_spin_button_insert_text;
376 gtk_spin_button_set_property (GObject *object,
381 GtkSpinButton *spin_button;
383 spin_button = GTK_SPIN_BUTTON (object);
387 GtkAdjustment *adjustment;
389 case PROP_ADJUSTMENT:
390 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
392 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
393 gtk_spin_button_set_adjustment (spin_button, adjustment);
395 case PROP_CLIMB_RATE:
396 gtk_spin_button_configure (spin_button,
397 spin_button->adjustment,
398 g_value_get_double (value),
399 spin_button->digits);
402 gtk_spin_button_configure (spin_button,
403 spin_button->adjustment,
404 spin_button->climb_rate,
405 g_value_get_uint (value));
407 case PROP_SNAP_TO_TICKS:
408 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
411 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
414 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
416 case PROP_UPDATE_POLICY:
417 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
420 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
428 gtk_spin_button_get_property (GObject *object,
433 GtkSpinButton *spin_button;
435 spin_button = GTK_SPIN_BUTTON (object);
439 case PROP_ADJUSTMENT:
440 g_value_set_object (value, spin_button->adjustment);
442 case PROP_CLIMB_RATE:
443 g_value_set_double (value, spin_button->climb_rate);
446 g_value_set_uint (value, spin_button->digits);
448 case PROP_SNAP_TO_TICKS:
449 g_value_set_boolean (value, spin_button->snap_to_ticks);
452 g_value_set_boolean (value, spin_button->numeric);
455 g_value_set_boolean (value, spin_button->wrap);
457 case PROP_UPDATE_POLICY:
458 g_value_set_enum (value, spin_button->update_policy);
461 g_value_set_double (value, spin_button->adjustment->value);
464 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
470 gtk_spin_button_init (GtkSpinButton *spin_button)
472 spin_button->adjustment = NULL;
473 spin_button->panel = NULL;
474 spin_button->timer = 0;
475 spin_button->climb_rate = 0.0;
476 spin_button->timer_step = 0.0;
477 spin_button->update_policy = GTK_UPDATE_ALWAYS;
478 spin_button->in_child = NO_ARROW;
479 spin_button->click_child = NO_ARROW;
480 spin_button->button = 0;
481 spin_button->need_timer = FALSE;
482 spin_button->timer_calls = 0;
483 spin_button->digits = 0;
484 spin_button->numeric = FALSE;
485 spin_button->wrap = FALSE;
486 spin_button->snap_to_ticks = FALSE;
488 gtk_spin_button_set_adjustment (spin_button,
489 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
493 gtk_spin_button_finalize (GObject *object)
495 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
497 G_OBJECT_CLASS (parent_class)->finalize (object);
501 gtk_spin_button_destroy (GtkObject *object)
503 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
505 GTK_OBJECT_CLASS (parent_class)->destroy (object);
509 gtk_spin_button_map (GtkWidget *widget)
511 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
513 GTK_WIDGET_CLASS (parent_class)->map (widget);
514 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
519 gtk_spin_button_unmap (GtkWidget *widget)
521 if (GTK_WIDGET_MAPPED (widget))
523 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
524 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
529 gtk_spin_button_realize (GtkWidget *widget)
531 GtkSpinButton *spin_button;
532 GdkWindowAttr attributes;
533 gint attributes_mask;
538 spin_button = GTK_SPIN_BUTTON (widget);
539 arrow_size = spin_button_get_arrow_size (spin_button);
541 real_width = widget->allocation.width;
542 widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
543 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
544 GDK_KEY_RELEASE_MASK);
545 GTK_WIDGET_CLASS (parent_class)->realize (widget);
547 widget->allocation.width = real_width;
549 attributes.window_type = GDK_WINDOW_CHILD;
550 attributes.wclass = GDK_INPUT_OUTPUT;
551 attributes.visual = gtk_widget_get_visual (widget);
552 attributes.colormap = gtk_widget_get_colormap (widget);
553 attributes.event_mask = gtk_widget_get_events (widget);
554 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
555 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
556 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
558 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
560 attributes.x = (widget->allocation.x +
561 widget->allocation.width - arrow_size -
562 2 * widget->style->xthickness);
563 attributes.y = widget->allocation.y + (widget->allocation.height -
564 widget->requisition.height) / 2;
565 attributes.width = arrow_size + 2 * widget->style->xthickness;
566 attributes.height = widget->requisition.height;
568 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
569 &attributes, attributes_mask);
570 gdk_window_set_user_data (spin_button->panel, widget);
572 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
575 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
576 if (return_val == FALSE)
577 gtk_spin_button_default_output (spin_button);
579 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
583 gtk_spin_button_unrealize (GtkWidget *widget)
585 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
587 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
591 gdk_window_set_user_data (spin->panel, NULL);
592 gdk_window_destroy (spin->panel);
598 compute_double_length (double val, int digits)
604 if (fabs (val) > 1.0)
605 a = floor (log10 (fabs (val))) + 1;
617 return a + digits + extra;
620 /* Keep in sync with gtkentry.c !
623 get_borders (GtkEntry *entry,
627 GtkWidget *widget = GTK_WIDGET (entry);
629 gboolean interior_focus;
631 gtk_widget_style_get (widget,
632 "interior-focus", &interior_focus,
633 "focus-line-width", &focus_width,
636 if (entry->has_frame)
638 *xborder = widget->style->xthickness;
639 *yborder = widget->style->ythickness;
649 *xborder += focus_width;
650 *yborder += focus_width;
655 gtk_spin_button_size_request (GtkWidget *widget,
656 GtkRequisition *requisition)
659 GtkSpinButton *spin_button;
662 entry = GTK_ENTRY (widget);
663 spin_button = GTK_SPIN_BUTTON (widget);
664 arrow_size = spin_button_get_arrow_size (spin_button);
666 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
668 if (entry->width_chars < 0)
670 PangoContext *context;
671 PangoFontMetrics *metrics;
677 gboolean interior_focus;
679 gint xborder, yborder;
681 gtk_widget_style_get (widget,
682 "interior-focus", &interior_focus,
683 "focus-line-width", &focus_width,
686 context = gtk_widget_get_pango_context (widget);
687 metrics = pango_context_get_metrics (context,
688 widget->style->font_desc,
689 pango_context_get_language (context));
691 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
692 digit_width = PANGO_SCALE *
693 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
695 pango_font_metrics_unref (metrics);
697 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
699 width = MIN_SPIN_BUTTON_WIDTH;
700 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
701 spin_button->digits));
703 string_len = compute_double_length (spin_button->adjustment->upper,
704 spin_button->digits);
705 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
706 width = MAX (width, w);
707 string_len = compute_double_length (spin_button->adjustment->lower,
708 spin_button->digits);
709 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
710 width = MAX (width, w);
712 get_borders (entry, &xborder, &yborder);
714 xborder += 2; /* INNER_BORDER */
716 requisition->width = width + xborder * 2;
719 requisition->width += arrow_size + 2 * widget->style->xthickness;
723 gtk_spin_button_size_allocate (GtkWidget *widget,
724 GtkAllocation *allocation)
727 GtkAllocation entry_allocation;
728 GtkAllocation panel_allocation;
732 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
733 g_return_if_fail (allocation != NULL);
735 spin = GTK_SPIN_BUTTON (widget);
736 arrow_size = spin_button_get_arrow_size (spin);
737 panel_width = arrow_size + 2 * widget->style->xthickness;
739 widget->allocation = *allocation;
741 entry_allocation = *allocation;
742 entry_allocation.width -= panel_width;
744 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
746 entry_allocation.x += panel_width;
747 panel_allocation.x = allocation->x;
751 panel_allocation.x = allocation->x + allocation->width - panel_width;
754 panel_allocation.width = panel_width;
755 panel_allocation.height = MIN (widget->requisition.height, allocation->height);
757 panel_allocation.y = allocation->y + (allocation->height -
758 panel_allocation.height) / 2;
760 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &entry_allocation);
762 if (GTK_WIDGET_REALIZED (widget))
764 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
767 panel_allocation.width,
768 panel_allocation.height);
771 spin_button_redraw (spin);
775 gtk_spin_button_expose (GtkWidget *widget,
776 GdkEventExpose *event)
780 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
781 g_return_val_if_fail (event != NULL, FALSE);
783 spin = GTK_SPIN_BUTTON (widget);
785 if (GTK_WIDGET_DRAWABLE (widget))
787 GtkShadowType shadow_type;
790 if (event->window != spin->panel)
791 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
793 /* we redraw the panel even if it wasn't exposed. This is
794 * because spin->panel is not a child window of widget->window,
795 * so it will not be invalidated by eg. gtk_widget_queue_draw().
800 gdk_drawable_get_size (spin->panel, &rect.width, &rect.height);
802 shadow_type = spin_button_get_shadow_type (spin);
804 gdk_window_begin_paint_rect (spin->panel, &rect);
806 if (shadow_type != GTK_SHADOW_NONE)
808 gtk_paint_box (widget->style, spin->panel,
809 GTK_STATE_NORMAL, shadow_type,
810 NULL, widget, "spinbutton",
811 rect.x, rect.y, rect.width, rect.height);
814 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
815 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
817 gdk_window_end_paint (spin->panel);
824 spin_button_at_limit (GtkSpinButton *spin_button,
827 GtkArrowType effective_arrow;
829 if (spin_button->wrap)
832 if (spin_button->adjustment->step_increment > 0)
833 effective_arrow = arrow;
835 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
837 if (effective_arrow == GTK_ARROW_UP &&
838 (spin_button->adjustment->upper - spin_button->adjustment->value <= EPSILON))
841 if (effective_arrow == GTK_ARROW_DOWN &&
842 (spin_button->adjustment->value - spin_button->adjustment->lower <= EPSILON))
849 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
850 GtkArrowType arrow_type)
852 GtkStateType state_type;
853 GtkShadowType shadow_type;
861 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
862 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
864 widget = GTK_WIDGET (spin_button);
866 if (GTK_WIDGET_DRAWABLE (widget))
868 width = spin_button_get_arrow_size (spin_button) + 2 * widget->style->xthickness;
870 if (arrow_type == GTK_ARROW_UP)
875 height = widget->requisition.height / 2;
880 y = widget->requisition.height / 2;
882 height = (widget->requisition.height + 1) / 2;
885 if (spin_button_at_limit (spin_button, arrow_type))
887 shadow_type = GTK_SHADOW_OUT;
888 state_type = GTK_STATE_INSENSITIVE;
892 if (spin_button->click_child == arrow_type)
894 state_type = GTK_STATE_ACTIVE;
895 shadow_type = GTK_SHADOW_IN;
899 if (spin_button->in_child == arrow_type &&
900 spin_button->click_child == NO_ARROW)
902 state_type = GTK_STATE_PRELIGHT;
906 state_type = GTK_WIDGET_STATE (widget);
909 shadow_type = GTK_SHADOW_OUT;
913 gtk_paint_box (widget->style, spin_button->panel,
914 state_type, shadow_type,
916 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
917 x, y, width, height);
919 height = widget->requisition.height;
921 if (arrow_type == GTK_ARROW_DOWN)
924 height = height - y - 2;
929 height = height / 2 - 2;
934 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
940 w -= w % 2 - 1; /* force odd */
943 x += (width - w) / 2;
944 y += (height - h) / 2;
949 gtk_paint_arrow (widget->style, spin_button->panel,
950 state_type, shadow_type,
951 NULL, widget, "spinbutton",
953 x, y, width, height);
958 gtk_spin_button_enter_notify (GtkWidget *widget,
959 GdkEventCrossing *event)
961 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
963 if (event->window == spin->panel)
968 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
970 if (y <= widget->requisition.height / 2)
971 spin->in_child = GTK_ARROW_UP;
973 spin->in_child = GTK_ARROW_DOWN;
975 spin_button_redraw (spin);
982 gtk_spin_button_leave_notify (GtkWidget *widget,
983 GdkEventCrossing *event)
985 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
987 spin->in_child = NO_ARROW;
988 spin_button_redraw (spin);
994 gtk_spin_button_focus_out (GtkWidget *widget,
995 GdkEventFocus *event)
997 if (GTK_ENTRY (widget)->editable)
998 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1000 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
1004 gtk_spin_button_grab_notify (GtkWidget *widget,
1005 gboolean was_grabbed)
1007 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1011 gtk_spin_button_stop_spinning (spin);
1012 spin_button_redraw (spin);
1017 gtk_spin_button_state_changed (GtkWidget *widget,
1018 GtkStateType previous_state)
1020 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1022 if (!GTK_WIDGET_IS_SENSITIVE (widget))
1024 gtk_spin_button_stop_spinning (spin);
1025 spin_button_redraw (spin);
1030 gtk_spin_button_scroll (GtkWidget *widget,
1031 GdkEventScroll *event)
1033 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1035 if (event->direction == GDK_SCROLL_UP)
1037 if (!GTK_WIDGET_HAS_FOCUS (widget))
1038 gtk_widget_grab_focus (widget);
1039 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
1041 else if (event->direction == GDK_SCROLL_DOWN)
1043 if (!GTK_WIDGET_HAS_FOCUS (widget))
1044 gtk_widget_grab_focus (widget);
1045 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
1054 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1058 g_source_remove (spin->timer);
1060 spin->timer_calls = 0;
1061 spin->need_timer = FALSE;
1066 spin->timer_step = spin->adjustment->step_increment;
1067 spin->timer_calls = 0;
1069 spin->click_child = NO_ARROW;
1074 start_spinning (GtkSpinButton *spin,
1075 GtkArrowType click_child,
1078 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1080 spin->click_child = click_child;
1084 spin->timer_step = step;
1085 spin->need_timer = TRUE;
1086 spin->timer = g_timeout_add (SPIN_BUTTON_INITIAL_TIMER_DELAY,
1087 (GSourceFunc) gtk_spin_button_timer,
1090 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1092 spin_button_redraw (spin);
1096 gtk_spin_button_button_press (GtkWidget *widget,
1097 GdkEventButton *event)
1099 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1103 if (event->window == spin->panel)
1105 if (!GTK_WIDGET_HAS_FOCUS (widget))
1106 gtk_widget_grab_focus (widget);
1107 spin->button = event->button;
1109 if (GTK_ENTRY (widget)->editable)
1110 gtk_spin_button_update (spin);
1112 if (event->y <= widget->requisition.height / 2)
1114 if (event->button == 1)
1115 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->step_increment);
1116 else if (event->button == 2)
1117 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->page_increment);
1119 spin->click_child = GTK_ARROW_UP;
1123 if (event->button == 1)
1124 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->step_increment);
1125 else if (event->button == 2)
1126 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->page_increment);
1128 spin->click_child = GTK_ARROW_DOWN;
1133 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1139 gtk_spin_button_button_release (GtkWidget *widget,
1140 GdkEventButton *event)
1142 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1145 arrow_size = spin_button_get_arrow_size (spin);
1147 if (event->button == spin->button)
1149 int click_child = spin->click_child;
1151 gtk_spin_button_stop_spinning (spin);
1153 if (event->button == 3)
1155 if (event->y >= 0 && event->x >= 0 &&
1156 event->y <= widget->requisition.height &&
1157 event->x <= arrow_size + 2 * widget->style->xthickness)
1159 if (click_child == GTK_ARROW_UP &&
1160 event->y <= widget->requisition.height / 2)
1164 diff = spin->adjustment->upper - spin->adjustment->value;
1166 gtk_spin_button_real_spin (spin, diff);
1168 else if (click_child == GTK_ARROW_DOWN &&
1169 event->y > widget->requisition.height / 2)
1173 diff = spin->adjustment->value - spin->adjustment->lower;
1175 gtk_spin_button_real_spin (spin, -diff);
1179 spin_button_redraw (spin);
1184 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1188 gtk_spin_button_motion_notify (GtkWidget *widget,
1189 GdkEventMotion *event)
1191 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1196 if (event->window == spin->panel)
1200 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1202 if (y <= widget->requisition.height / 2 &&
1203 spin->in_child == GTK_ARROW_DOWN)
1205 spin->in_child = GTK_ARROW_UP;
1206 spin_button_redraw (spin);
1208 else if (y > widget->requisition.height / 2 &&
1209 spin->in_child == GTK_ARROW_UP)
1211 spin->in_child = GTK_ARROW_DOWN;
1212 spin_button_redraw (spin);
1218 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1222 gtk_spin_button_timer (GtkSpinButton *spin_button)
1224 gboolean retval = FALSE;
1226 GDK_THREADS_ENTER ();
1228 if (spin_button->timer)
1230 if (spin_button->click_child == GTK_ARROW_UP)
1231 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1233 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1235 if (spin_button->need_timer)
1237 spin_button->need_timer = FALSE;
1238 spin_button->timer = g_timeout_add (SPIN_BUTTON_TIMER_DELAY,
1239 (GSourceFunc) gtk_spin_button_timer,
1240 (gpointer) spin_button);
1244 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1245 < spin_button->adjustment->page_increment)
1247 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1248 spin_button->timer_calls++;
1251 spin_button->timer_calls = 0;
1252 spin_button->timer_step += spin_button->climb_rate;
1259 GDK_THREADS_LEAVE ();
1265 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1266 GtkSpinButton *spin_button)
1268 gboolean return_val;
1270 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1273 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1274 if (return_val == FALSE)
1275 gtk_spin_button_default_output (spin_button);
1277 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1279 spin_button_redraw (spin_button);
1281 g_object_notify (G_OBJECT (spin_button), "value");
1285 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1286 GtkScrollType scroll)
1288 /* We don't test whether the entry is editable, since
1289 * this key binding conceptually corresponds to changing
1290 * the value with the buttons using the mouse, which
1291 * we allow for non-editable spin buttons.
1295 case GTK_SCROLL_STEP_BACKWARD:
1296 case GTK_SCROLL_STEP_DOWN:
1297 case GTK_SCROLL_STEP_LEFT:
1298 gtk_spin_button_real_spin (spin, -spin->timer_step);
1300 if (spin->climb_rate > 0.0 && spin->timer_step
1301 < spin->adjustment->page_increment)
1303 if (spin->timer_calls < MAX_TIMER_CALLS)
1304 spin->timer_calls++;
1307 spin->timer_calls = 0;
1308 spin->timer_step += spin->climb_rate;
1313 case GTK_SCROLL_STEP_FORWARD:
1314 case GTK_SCROLL_STEP_UP:
1315 case GTK_SCROLL_STEP_RIGHT:
1316 gtk_spin_button_real_spin (spin, spin->timer_step);
1318 if (spin->climb_rate > 0.0 && spin->timer_step
1319 < spin->adjustment->page_increment)
1321 if (spin->timer_calls < MAX_TIMER_CALLS)
1322 spin->timer_calls++;
1325 spin->timer_calls = 0;
1326 spin->timer_step += spin->climb_rate;
1331 case GTK_SCROLL_PAGE_BACKWARD:
1332 case GTK_SCROLL_PAGE_DOWN:
1333 case GTK_SCROLL_PAGE_LEFT:
1334 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1337 case GTK_SCROLL_PAGE_FORWARD:
1338 case GTK_SCROLL_PAGE_UP:
1339 case GTK_SCROLL_PAGE_RIGHT:
1340 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1343 case GTK_SCROLL_START:
1345 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1347 gtk_spin_button_real_spin (spin, -diff);
1351 case GTK_SCROLL_END:
1353 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1355 gtk_spin_button_real_spin (spin, diff);
1360 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1364 gtk_spin_button_update (spin);
1368 gtk_spin_button_key_release (GtkWidget *widget,
1371 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1373 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1374 spin->timer_step = spin->adjustment->step_increment;
1375 spin->timer_calls = 0;
1381 gtk_spin_button_snap (GtkSpinButton *spin_button,
1387 inc = spin_button->adjustment->step_increment;
1391 tmp = (val - spin_button->adjustment->lower) / inc;
1392 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1393 val = spin_button->adjustment->lower + floor (tmp) * inc;
1395 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1397 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1398 gtk_adjustment_set_value (spin_button->adjustment, val);
1401 gint return_val = FALSE;
1402 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1403 if (return_val == FALSE)
1404 gtk_spin_button_default_output (spin_button);
1409 gtk_spin_button_activate (GtkEntry *entry)
1411 if (entry->editable)
1412 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1414 /* Chain up so that entry->activates_default is honored */
1415 parent_class->activate (entry);
1419 gtk_spin_button_insert_text (GtkEditable *editable,
1420 const gchar *new_text,
1421 gint new_text_length,
1424 GtkEntry *entry = GTK_ENTRY (editable);
1425 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1426 GtkEditableClass *parent_editable_iface = g_type_interface_peek (parent_class, GTK_TYPE_EDITABLE);
1438 entry_length = entry->text_length;
1442 if (*(lc->negative_sign))
1443 neg_sign = *(lc->negative_sign);
1447 if (*(lc->positive_sign))
1448 pos_sign = *(lc->positive_sign);
1452 for (sign=0, i=0; i<entry_length; i++)
1453 if ((entry->text[i] == neg_sign) ||
1454 (entry->text[i] == pos_sign))
1460 if (sign && !(*position))
1463 for (dotpos=-1, i=0; i<entry_length; i++)
1464 if (entry->text[i] == *(lc->decimal_point))
1470 if (dotpos > -1 && *position > dotpos &&
1471 (gint)spin->digits - entry_length
1472 + dotpos - new_text_length + 1 < 0)
1475 for (i = 0; i < new_text_length; i++)
1477 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1479 if (sign || (*position) || i)
1483 else if (new_text[i] == *(lc->decimal_point))
1485 if (!spin->digits || dotpos > -1 ||
1486 (new_text_length - 1 - i + entry_length
1487 - *position > (gint)spin->digits))
1489 dotpos = *position + i;
1491 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1496 parent_editable_iface->insert_text (editable, new_text,
1497 new_text_length, position);
1501 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1505 gdouble new_value = 0.0;
1507 adj = spin_button->adjustment;
1509 new_value = adj->value + increment;
1513 if (spin_button->wrap)
1515 if (fabs (adj->value - adj->upper) < EPSILON)
1516 new_value = adj->lower;
1517 else if (new_value > adj->upper)
1518 new_value = adj->upper;
1521 new_value = MIN (new_value, adj->upper);
1523 else if (increment < 0)
1525 if (spin_button->wrap)
1527 if (fabs (adj->value - adj->lower) < EPSILON)
1528 new_value = adj->upper;
1529 else if (new_value < adj->lower)
1530 new_value = adj->lower;
1533 new_value = MAX (new_value, adj->lower);
1536 if (fabs (new_value - adj->value) > EPSILON)
1537 gtk_adjustment_set_value (adj, new_value);
1539 spin_button_redraw (spin_button);
1543 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1548 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1550 return GTK_INPUT_ERROR;
1556 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1558 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1560 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1561 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1567 /***********************************************************
1568 ***********************************************************
1569 *** Public interface ***
1570 ***********************************************************
1571 ***********************************************************/
1575 gtk_spin_button_configure (GtkSpinButton *spin_button,
1576 GtkAdjustment *adjustment,
1580 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1583 gtk_spin_button_set_adjustment (spin_button, adjustment);
1585 adjustment = spin_button->adjustment;
1587 g_object_freeze_notify (G_OBJECT (spin_button));
1588 if (spin_button->digits != digits)
1590 spin_button->digits = digits;
1591 g_object_notify (G_OBJECT (spin_button), "digits");
1594 if (spin_button->climb_rate != climb_rate)
1596 spin_button->climb_rate = climb_rate;
1597 g_object_notify (G_OBJECT (spin_button), "climb_rate");
1599 g_object_thaw_notify (G_OBJECT (spin_button));
1601 gtk_adjustment_value_changed (adjustment);
1605 gtk_spin_button_new (GtkAdjustment *adjustment,
1609 GtkSpinButton *spin;
1612 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1614 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1616 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1618 return GTK_WIDGET (spin);
1622 * gtk_spin_button_new_with_range:
1623 * @min: Minimum allowable value
1624 * @max: Maximum allowable value
1625 * @step: Increment added or subtracted by spinning the widget
1627 * This is a convenience constructor that allows creation of a numeric
1628 * #GtkSpinButton without manually creating an adjustment. The value is
1629 * initially set to the minimum value and a page increment of 10 * @step
1630 * is the default. The precision of the spin button is equivalent to the
1631 * precision of @step.
1633 * Return value: The new spin button as a #GtkWidget.
1636 gtk_spin_button_new_with_range (gdouble min,
1641 GtkSpinButton *spin;
1644 g_return_val_if_fail (min < max, NULL);
1645 g_return_val_if_fail (step != 0.0, NULL);
1647 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1649 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1651 if (fabs (step) >= 1.0 || step == 0.0)
1654 digits = abs ((gint) floor (log10 (fabs (step))));
1655 if (digits > MAX_DIGITS)
1656 digits = MAX_DIGITS;
1659 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1661 gtk_spin_button_set_numeric (spin, TRUE);
1663 return GTK_WIDGET (spin);
1666 /* Callback used when the spin button's adjustment changes. We need to redraw
1667 * the arrows when the adjustment's range changes, and reevaluate our size request.
1670 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1672 GtkSpinButton *spin_button;
1674 spin_button = GTK_SPIN_BUTTON (data);
1676 spin_button->timer_step = spin_button->adjustment->step_increment;
1677 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1681 * gtk_spin_button_set_adjustment:
1682 * @spin_button: a #GtkSpinButton
1683 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1685 * Replaces the #GtkAdjustment associated with @spin_button.
1688 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1689 GtkAdjustment *adjustment)
1691 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1693 if (spin_button->adjustment != adjustment)
1695 if (spin_button->adjustment)
1697 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1698 gtk_spin_button_value_changed,
1700 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1701 adjustment_changed_cb,
1703 g_object_unref (spin_button->adjustment);
1705 spin_button->adjustment = adjustment;
1708 g_object_ref (adjustment);
1709 gtk_object_sink (GTK_OBJECT (adjustment));
1710 g_signal_connect (adjustment, "value_changed",
1711 G_CALLBACK (gtk_spin_button_value_changed),
1713 g_signal_connect (adjustment, "changed",
1714 G_CALLBACK (adjustment_changed_cb),
1716 spin_button->timer_step = spin_button->adjustment->step_increment;
1719 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1722 g_object_notify (G_OBJECT (spin_button), "adjustment");
1726 * gtk_spin_button_get_adjustment:
1729 * Get the adjustment associated with a #GtkSpinButton
1731 * Return value: the #GtkAdjustment of @spin_button
1734 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1736 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1738 return spin_button->adjustment;
1742 * gtk_spin_button_set_digits:
1743 * @spin_button: a #GtkSpinButton
1744 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1746 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1750 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1753 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1755 if (spin_button->digits != digits)
1757 spin_button->digits = digits;
1758 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1759 g_object_notify (G_OBJECT (spin_button), "digits");
1761 /* since lower/upper may have changed */
1762 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1767 * gtk_spin_button_get_digits:
1768 * @spin_button: a #GtkSpinButton
1770 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1772 * Returns: the current precision
1775 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1777 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1779 return spin_button->digits;
1783 * gtk_spin_button_set_increments:
1784 * @spin_button: a #GtkSpinButton
1785 * @step: increment applied for a button 1 press.
1786 * @page: increment applied for a button 2 press.
1788 * Sets the step and page increments for spin_button. This affects how
1789 * quickly the value changes when the spin button's arrows are activated.
1792 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1796 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1798 spin_button->adjustment->step_increment = step;
1799 spin_button->adjustment->page_increment = page;
1803 * gtk_spin_button_get_increments:
1804 * @spin_button: a #GtkSpinButton
1805 * @step: location to store step increment, or %NULL
1806 * @page: location to store page increment, or %NULL
1808 * Gets the current step and page the increments used by @spin_button. See
1809 * gtk_spin_button_set_increments().
1812 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1816 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1819 *step = spin_button->adjustment->step_increment;
1821 *page = spin_button->adjustment->page_increment;
1825 * gtk_spin_button_set_range:
1826 * @spin_button: a #GtkSpinButton
1827 * @min: minimum allowable value
1828 * @max: maximum allowable value
1830 * Sets the minimum and maximum allowable values for @spin_button
1833 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1839 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1841 spin_button->adjustment->lower = min;
1842 spin_button->adjustment->upper = max;
1844 value = CLAMP (spin_button->adjustment->value,
1845 spin_button->adjustment->lower,
1846 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1848 if (value != spin_button->adjustment->value)
1849 gtk_spin_button_set_value (spin_button, value);
1851 gtk_adjustment_changed (spin_button->adjustment);
1855 * gtk_spin_button_get_range:
1856 * @spin_button: a #GtkSpinButton
1857 * @min: location to store minimum allowed value, or %NULL
1858 * @max: location to store maximum allowed value, or %NULL
1860 * Gets the range allowed for @spin_button. See
1861 * gtk_spin_button_set_range().
1864 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1868 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1871 *min = spin_button->adjustment->lower;
1873 *max = spin_button->adjustment->upper;
1877 * gtk_spin_button_get_value:
1878 * @spin_button: a #GtkSpinButton
1880 * Get the value in the @spin_button.
1882 * Return value: the value of @spin_button
1885 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1887 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1889 return spin_button->adjustment->value;
1893 * gtk_spin_button_get_value_as_int:
1894 * @spin_button: a #GtkSpinButton
1896 * Get the value @spin_button represented as an integer.
1898 * Return value: the value of @spin_button
1901 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1905 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1907 val = spin_button->adjustment->value;
1908 if (val - floor (val) < ceil (val) - val)
1915 * gtk_spin_button_set_value:
1916 * @spin_button: a #GtkSpinButton
1917 * @value: the new value
1919 * Set the value of @spin_button.
1922 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1925 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1927 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1928 gtk_adjustment_set_value (spin_button->adjustment, value);
1931 gint return_val = FALSE;
1932 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1933 if (return_val == FALSE)
1934 gtk_spin_button_default_output (spin_button);
1939 * gtk_spin_button_set_update_policy:
1940 * @spin_button: a #GtkSpinButton
1941 * @policy: a #GtkSpinButtonUpdatePolicy value
1943 * Sets the update behavior of a spin button. This determines whether the
1944 * spin button is always updated or only when a valid value is set.
1947 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1948 GtkSpinButtonUpdatePolicy policy)
1950 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1952 if (spin_button->update_policy != policy)
1954 spin_button->update_policy = policy;
1955 g_object_notify (G_OBJECT (spin_button), "update_policy");
1960 * gtk_spin_button_get_update_policy:
1961 * @spin_button: a #GtkSpinButton
1963 * Gets the update behavior of a spin button. See
1964 * gtk_spin_button_set_update_policy().
1966 * Return value: the current update policy
1968 GtkSpinButtonUpdatePolicy
1969 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1971 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1973 return spin_button->update_policy;
1977 * gtk_spin_button_set_numeric:
1978 * @spin_button: a #GtkSpinButton
1979 * @numeric: flag indicating if only numeric entry is allowed.
1981 * Sets the flag that determines if non-numeric text can be typed into
1985 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1988 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1990 numeric = numeric != FALSE;
1992 if (spin_button->numeric != numeric)
1994 spin_button->numeric = numeric;
1995 g_object_notify (G_OBJECT (spin_button), "numeric");
2000 * gtk_spin_button_get_numeric:
2001 * @spin_button: a #GtkSpinButton
2003 * Returns whether non-numeric text can be typed into the spin button.
2004 * See gtk_spin_button_set_numeric().
2006 * Return value: %TRUE if only numeric text can be entered
2009 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2011 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2013 return spin_button->numeric;
2017 * gtk_spin_button_set_wrap:
2018 * @spin_button: a #GtkSpinButton
2019 * @wrap: a flag indicating if wrapping behavior is performed.
2021 * Sets the flag that determines if a spin button value wraps around to the
2022 * opposite limit when the upper or lower limit of the range is exceeded.
2025 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2028 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2030 wrap = wrap != FALSE;
2032 if (spin_button->wrap != wrap)
2034 spin_button->wrap = (wrap != 0);
2036 g_object_notify (G_OBJECT (spin_button), "wrap");
2041 * gtk_spin_button_get_wrap:
2042 * @spin_button: a #GtkSpinButton
2044 * Returns whether the spin button's value wraps around to the
2045 * opposite limit when the upper or lower limit of the range is
2046 * exceeded. See gtk_spin_button_set_wrap().
2048 * Return value: %TRUE if the spin button wraps around
2051 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2053 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2055 return spin_button->wrap;
2059 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2061 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
2064 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2066 return arrow_size - arrow_size % 2; /* force even */
2070 * spin_button_get_shadow_type:
2071 * @spin_button: a #GtkSpinButton
2073 * Convenience function to Get the shadow type from the underlying widget's
2076 * Return value: the #GtkShadowType
2079 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2081 GtkShadowType rc_shadow_type;
2083 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
2085 return rc_shadow_type;
2089 * gtk_spin_button_set_snap_to_ticks:
2090 * @spin_button: a #GtkSpinButton
2091 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2093 * Sets the policy as to whether values are corrected to the nearest step
2094 * increment when a spin button is activated after providing an invalid value.
2097 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2098 gboolean snap_to_ticks)
2102 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2104 new_val = (snap_to_ticks != 0);
2106 if (new_val != spin_button->snap_to_ticks)
2108 spin_button->snap_to_ticks = new_val;
2109 if (new_val && GTK_ENTRY (spin_button)->editable)
2110 gtk_spin_button_update (spin_button);
2112 g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
2117 * gtk_spin_button_get_snap_to_ticks:
2118 * @spin_button: a #GtkSpinButton
2120 * Returns whether the values are corrected to the nearest step. See
2121 * gtk_spin_button_set_snap_to_ticks().
2123 * Return value: %TRUE if values are snapped to the nearest step.
2126 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2128 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2130 return spin_button->snap_to_ticks;
2134 * gtk_spin_button_spin:
2135 * @spin_button: a #GtkSpinButton
2136 * @direction: a #GtkSpinType indicating the direction to spin.
2137 * @increment: step increment to apply in the specified direction.
2139 * Increment or decrement a spin button's value in a specified direction
2140 * by a specified amount.
2143 gtk_spin_button_spin (GtkSpinButton *spin_button,
2144 GtkSpinType direction,
2150 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2152 adj = spin_button->adjustment;
2154 /* for compatibility with the 1.0.x version of this function */
2155 if (increment != 0 && increment != adj->step_increment &&
2156 (direction == GTK_SPIN_STEP_FORWARD ||
2157 direction == GTK_SPIN_STEP_BACKWARD))
2159 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2160 increment = -increment;
2161 direction = GTK_SPIN_USER_DEFINED;
2166 case GTK_SPIN_STEP_FORWARD:
2168 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2171 case GTK_SPIN_STEP_BACKWARD:
2173 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2176 case GTK_SPIN_PAGE_FORWARD:
2178 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2181 case GTK_SPIN_PAGE_BACKWARD:
2183 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2188 diff = adj->value - adj->lower;
2190 gtk_spin_button_real_spin (spin_button, -diff);
2195 diff = adj->upper - adj->value;
2197 gtk_spin_button_real_spin (spin_button, diff);
2200 case GTK_SPIN_USER_DEFINED:
2203 gtk_spin_button_real_spin (spin_button, increment);
2212 * gtk_spin_button_update:
2213 * @spin_button: a #GtkSpinButton
2215 * Manually force an update of the spin button.
2218 gtk_spin_button_update (GtkSpinButton *spin_button)
2224 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2227 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2228 if (return_val == FALSE)
2230 return_val = gtk_spin_button_default_input (spin_button, &val);
2231 error = (return_val == GTK_INPUT_ERROR);
2233 else if (return_val == GTK_INPUT_ERROR)
2236 spin_button_redraw (spin_button);
2238 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2240 if (val < spin_button->adjustment->lower)
2241 val = spin_button->adjustment->lower;
2242 else if (val > spin_button->adjustment->upper)
2243 val = spin_button->adjustment->upper;
2245 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2247 val < spin_button->adjustment->lower ||
2248 val > spin_button->adjustment->upper))
2250 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2254 if (spin_button->snap_to_ticks)
2255 gtk_spin_button_snap (spin_button, val);
2258 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2259 gtk_adjustment_set_value (spin_button->adjustment, val);
2263 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0,
2265 if (return_val == FALSE)
2266 gtk_spin_button_default_output (spin_button);
2272 spin_button_redraw (GtkSpinButton *spin_button)
2276 widget = GTK_WIDGET (spin_button);
2278 if (GTK_WIDGET_DRAWABLE (widget))
2280 gtk_widget_queue_draw (widget);
2282 /* We must invalidate the panel window ourselves, because it
2283 * is not a child of widget->window
2285 gdk_window_invalidate_rect (spin_button->panel, NULL, TRUE);