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 MAX_TIMER_CALLS 5
50 #define MIN_ARROW_WIDTH 6
75 static void gtk_spin_button_editable_init (GtkEditableClass *iface);
76 static void gtk_spin_button_finalize (GObject *object);
77 static void gtk_spin_button_destroy (GtkObject *object);
78 static void gtk_spin_button_set_property (GObject *object,
82 static void gtk_spin_button_get_property (GObject *object,
86 static void gtk_spin_button_map (GtkWidget *widget);
87 static void gtk_spin_button_unmap (GtkWidget *widget);
88 static void gtk_spin_button_realize (GtkWidget *widget);
89 static void gtk_spin_button_unrealize (GtkWidget *widget);
90 static void gtk_spin_button_size_request (GtkWidget *widget,
91 GtkRequisition *requisition);
92 static void gtk_spin_button_size_allocate (GtkWidget *widget,
93 GtkAllocation *allocation);
94 static gint gtk_spin_button_expose (GtkWidget *widget,
95 GdkEventExpose *event);
96 static gint gtk_spin_button_button_press (GtkWidget *widget,
97 GdkEventButton *event);
98 static gint gtk_spin_button_button_release (GtkWidget *widget,
99 GdkEventButton *event);
100 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
101 GdkEventMotion *event);
102 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
103 GdkEventCrossing *event);
104 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
105 GdkEventCrossing *event);
106 static gint gtk_spin_button_focus_out (GtkWidget *widget,
107 GdkEventFocus *event);
108 static void gtk_spin_button_grab_notify (GtkWidget *widget,
109 gboolean was_grabbed);
110 static void gtk_spin_button_state_changed (GtkWidget *widget,
111 GtkStateType previous_state);
112 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
113 GtkArrowType arrow_type);
114 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
115 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
116 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
117 GtkSpinButton *spin_button);
118 static gint gtk_spin_button_key_release (GtkWidget *widget,
120 static gint gtk_spin_button_scroll (GtkWidget *widget,
121 GdkEventScroll *event);
122 static void gtk_spin_button_activate (GtkEntry *entry);
123 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
125 static void gtk_spin_button_insert_text (GtkEditable *editable,
126 const gchar *new_text,
127 gint new_text_length,
129 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
131 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
132 GtkScrollType scroll);
134 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
136 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
138 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
139 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
140 static void spin_button_redraw (GtkSpinButton *spin_button);
143 static guint spinbutton_signals[LAST_SIGNAL] = {0};
147 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
148 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
149 gtk_spin_button_editable_init));
151 #define add_spin_binding(binding_set, keyval, mask, scroll) \
152 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
154 GTK_TYPE_SCROLL_TYPE, scroll)
157 gtk_spin_button_class_init (GtkSpinButtonClass *class)
159 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
160 GtkObjectClass *object_class;
161 GtkWidgetClass *widget_class;
162 GtkEntryClass *entry_class;
163 GtkBindingSet *binding_set;
165 object_class = (GtkObjectClass*) class;
166 widget_class = (GtkWidgetClass*) class;
167 entry_class = (GtkEntryClass*) class;
169 gobject_class->finalize = gtk_spin_button_finalize;
171 gobject_class->set_property = gtk_spin_button_set_property;
172 gobject_class->get_property = gtk_spin_button_get_property;
174 object_class->destroy = gtk_spin_button_destroy;
176 widget_class->map = gtk_spin_button_map;
177 widget_class->unmap = gtk_spin_button_unmap;
178 widget_class->realize = gtk_spin_button_realize;
179 widget_class->unrealize = gtk_spin_button_unrealize;
180 widget_class->size_request = gtk_spin_button_size_request;
181 widget_class->size_allocate = gtk_spin_button_size_allocate;
182 widget_class->expose_event = gtk_spin_button_expose;
183 widget_class->scroll_event = gtk_spin_button_scroll;
184 widget_class->button_press_event = gtk_spin_button_button_press;
185 widget_class->button_release_event = gtk_spin_button_button_release;
186 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
187 widget_class->key_release_event = gtk_spin_button_key_release;
188 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
189 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
190 widget_class->focus_out_event = gtk_spin_button_focus_out;
191 widget_class->grab_notify = gtk_spin_button_grab_notify;
192 widget_class->state_changed = gtk_spin_button_state_changed;
194 entry_class->activate = gtk_spin_button_activate;
197 class->output = NULL;
198 class->change_value = gtk_spin_button_real_change_value;
200 g_object_class_install_property (gobject_class,
202 g_param_spec_object ("adjustment",
204 P_("The adjustment that holds the value of the spinbutton"),
206 GTK_PARAM_READWRITE));
208 g_object_class_install_property (gobject_class,
210 g_param_spec_double ("climb-rate",
212 P_("The acceleration rate when you hold down a button"),
216 GTK_PARAM_READWRITE));
218 g_object_class_install_property (gobject_class,
220 g_param_spec_uint ("digits",
222 P_("The number of decimal places to display"),
226 GTK_PARAM_READWRITE));
228 g_object_class_install_property (gobject_class,
230 g_param_spec_boolean ("snap-to-ticks",
232 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
234 GTK_PARAM_READWRITE));
236 g_object_class_install_property (gobject_class,
238 g_param_spec_boolean ("numeric",
240 P_("Whether non-numeric characters should be ignored"),
242 GTK_PARAM_READWRITE));
244 g_object_class_install_property (gobject_class,
246 g_param_spec_boolean ("wrap",
248 P_("Whether a spin button should wrap upon reaching its limits"),
250 GTK_PARAM_READWRITE));
252 g_object_class_install_property (gobject_class,
254 g_param_spec_enum ("update-policy",
256 P_("Whether the spin button should update always, or only when the value is legal"),
257 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
259 GTK_PARAM_READWRITE));
261 g_object_class_install_property (gobject_class,
263 g_param_spec_double ("value",
265 P_("Reads the current value, or sets a new value"),
269 GTK_PARAM_READWRITE));
271 gtk_widget_class_install_style_property_parser (widget_class,
272 g_param_spec_enum ("shadow-type",
274 P_("Style of bevel around the spin button"),
275 GTK_TYPE_SHADOW_TYPE,
278 gtk_rc_property_parse_enum);
279 spinbutton_signals[INPUT] =
280 g_signal_new (I_("input"),
281 G_TYPE_FROM_CLASS (gobject_class),
283 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
285 _gtk_marshal_INT__POINTER,
289 spinbutton_signals[OUTPUT] =
290 g_signal_new (I_("output"),
291 G_TYPE_FROM_CLASS (gobject_class),
293 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
294 _gtk_boolean_handled_accumulator, NULL,
295 _gtk_marshal_BOOLEAN__VOID,
298 spinbutton_signals[VALUE_CHANGED] =
299 g_signal_new (I_("value_changed"),
300 G_TYPE_FROM_CLASS (gobject_class),
302 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
304 _gtk_marshal_VOID__VOID,
308 * GtkSpinButton::wrapped:
309 * @spinbutton: the object which received the signal
311 * The wrapped signal is emitted right after the spinbutton wraps
312 * from its maximum to minimum value or vice-versa.
316 spinbutton_signals[WRAPPED] =
317 g_signal_new (I_("wrapped"),
318 G_TYPE_FROM_CLASS (gobject_class),
320 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
322 _gtk_marshal_VOID__VOID,
326 spinbutton_signals[CHANGE_VALUE] =
327 g_signal_new (I_("change_value"),
328 G_TYPE_FROM_CLASS (gobject_class),
329 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
330 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
332 _gtk_marshal_VOID__ENUM,
334 GTK_TYPE_SCROLL_TYPE);
336 binding_set = gtk_binding_set_by_class (class);
338 add_spin_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
339 add_spin_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
340 add_spin_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
341 add_spin_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
342 add_spin_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
343 add_spin_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
344 add_spin_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
345 add_spin_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
349 gtk_spin_button_editable_init (GtkEditableClass *iface)
351 iface->insert_text = gtk_spin_button_insert_text;
355 gtk_spin_button_set_property (GObject *object,
360 GtkSpinButton *spin_button;
362 spin_button = GTK_SPIN_BUTTON (object);
366 GtkAdjustment *adjustment;
368 case PROP_ADJUSTMENT:
369 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
371 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
372 gtk_spin_button_set_adjustment (spin_button, adjustment);
374 case PROP_CLIMB_RATE:
375 gtk_spin_button_configure (spin_button,
376 spin_button->adjustment,
377 g_value_get_double (value),
378 spin_button->digits);
381 gtk_spin_button_configure (spin_button,
382 spin_button->adjustment,
383 spin_button->climb_rate,
384 g_value_get_uint (value));
386 case PROP_SNAP_TO_TICKS:
387 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
390 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
393 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
395 case PROP_UPDATE_POLICY:
396 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
399 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
407 gtk_spin_button_get_property (GObject *object,
412 GtkSpinButton *spin_button;
414 spin_button = GTK_SPIN_BUTTON (object);
418 case PROP_ADJUSTMENT:
419 g_value_set_object (value, spin_button->adjustment);
421 case PROP_CLIMB_RATE:
422 g_value_set_double (value, spin_button->climb_rate);
425 g_value_set_uint (value, spin_button->digits);
427 case PROP_SNAP_TO_TICKS:
428 g_value_set_boolean (value, spin_button->snap_to_ticks);
431 g_value_set_boolean (value, spin_button->numeric);
434 g_value_set_boolean (value, spin_button->wrap);
436 case PROP_UPDATE_POLICY:
437 g_value_set_enum (value, spin_button->update_policy);
440 g_value_set_double (value, spin_button->adjustment->value);
443 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
449 gtk_spin_button_init (GtkSpinButton *spin_button)
451 spin_button->adjustment = NULL;
452 spin_button->panel = NULL;
453 spin_button->timer = 0;
454 spin_button->climb_rate = 0.0;
455 spin_button->timer_step = 0.0;
456 spin_button->update_policy = GTK_UPDATE_ALWAYS;
457 spin_button->in_child = NO_ARROW;
458 spin_button->click_child = NO_ARROW;
459 spin_button->button = 0;
460 spin_button->need_timer = FALSE;
461 spin_button->timer_calls = 0;
462 spin_button->digits = 0;
463 spin_button->numeric = FALSE;
464 spin_button->wrap = FALSE;
465 spin_button->snap_to_ticks = FALSE;
467 gtk_spin_button_set_adjustment (spin_button,
468 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
472 gtk_spin_button_finalize (GObject *object)
474 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
476 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
480 gtk_spin_button_destroy (GtkObject *object)
482 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
484 GTK_OBJECT_CLASS (gtk_spin_button_parent_class)->destroy (object);
488 gtk_spin_button_map (GtkWidget *widget)
490 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
492 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
493 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
498 gtk_spin_button_unmap (GtkWidget *widget)
500 if (GTK_WIDGET_MAPPED (widget))
502 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
504 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
505 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
510 gtk_spin_button_realize (GtkWidget *widget)
512 GtkSpinButton *spin_button;
513 GdkWindowAttr attributes;
514 gint attributes_mask;
519 spin_button = GTK_SPIN_BUTTON (widget);
520 arrow_size = spin_button_get_arrow_size (spin_button);
522 real_width = widget->allocation.width;
523 widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
524 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
525 GDK_KEY_RELEASE_MASK);
526 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
528 widget->allocation.width = real_width;
530 attributes.window_type = GDK_WINDOW_CHILD;
531 attributes.wclass = GDK_INPUT_OUTPUT;
532 attributes.visual = gtk_widget_get_visual (widget);
533 attributes.colormap = gtk_widget_get_colormap (widget);
534 attributes.event_mask = gtk_widget_get_events (widget);
535 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
536 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
537 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
539 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
541 attributes.x = (widget->allocation.x +
542 widget->allocation.width - arrow_size -
543 2 * widget->style->xthickness);
544 attributes.y = widget->allocation.y + (widget->allocation.height -
545 widget->requisition.height) / 2;
546 attributes.width = arrow_size + 2 * widget->style->xthickness;
547 attributes.height = widget->requisition.height;
549 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
550 &attributes, attributes_mask);
551 gdk_window_set_user_data (spin_button->panel, widget);
553 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
556 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
557 if (return_val == FALSE)
558 gtk_spin_button_default_output (spin_button);
560 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
564 gtk_spin_button_unrealize (GtkWidget *widget)
566 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
568 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
572 gdk_window_set_user_data (spin->panel, NULL);
573 gdk_window_destroy (spin->panel);
579 compute_double_length (double val, int digits)
585 if (fabs (val) > 1.0)
586 a = floor (log10 (fabs (val))) + 1;
598 return a + digits + extra;
602 gtk_spin_button_size_request (GtkWidget *widget,
603 GtkRequisition *requisition)
606 GtkSpinButton *spin_button;
609 entry = GTK_ENTRY (widget);
610 spin_button = GTK_SPIN_BUTTON (widget);
611 arrow_size = spin_button_get_arrow_size (spin_button);
613 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_request (widget, requisition);
615 if (entry->width_chars < 0)
617 PangoContext *context;
618 PangoFontMetrics *metrics;
624 gboolean interior_focus;
626 gint xborder, yborder;
628 gtk_widget_style_get (widget,
629 "interior-focus", &interior_focus,
630 "focus-line-width", &focus_width,
633 context = gtk_widget_get_pango_context (widget);
634 metrics = pango_context_get_metrics (context,
635 widget->style->font_desc,
636 pango_context_get_language (context));
638 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
639 digit_width = PANGO_SCALE *
640 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
642 pango_font_metrics_unref (metrics);
644 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
646 width = MIN_SPIN_BUTTON_WIDTH;
647 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
648 spin_button->digits));
650 string_len = compute_double_length (spin_button->adjustment->upper,
651 spin_button->digits);
652 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
653 width = MAX (width, w);
654 string_len = compute_double_length (spin_button->adjustment->lower,
655 spin_button->digits);
656 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
657 width = MAX (width, w);
659 _gtk_entry_get_borders (entry, &xborder, &yborder);
661 xborder += 2; /* INNER_BORDER */
663 requisition->width = width + xborder * 2;
666 requisition->width += arrow_size + 2 * widget->style->xthickness;
670 gtk_spin_button_size_allocate (GtkWidget *widget,
671 GtkAllocation *allocation)
674 GtkAllocation entry_allocation;
675 GtkAllocation panel_allocation;
679 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
680 g_return_if_fail (allocation != NULL);
682 spin = GTK_SPIN_BUTTON (widget);
683 arrow_size = spin_button_get_arrow_size (spin);
684 panel_width = arrow_size + 2 * widget->style->xthickness;
686 widget->allocation = *allocation;
688 entry_allocation = *allocation;
689 entry_allocation.width -= panel_width;
691 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
693 entry_allocation.x += panel_width;
694 panel_allocation.x = allocation->x;
698 panel_allocation.x = allocation->x + allocation->width - panel_width;
701 panel_allocation.width = panel_width;
702 panel_allocation.height = MIN (widget->requisition.height, allocation->height);
704 panel_allocation.y = allocation->y + (allocation->height -
705 panel_allocation.height) / 2;
707 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, &entry_allocation);
709 if (GTK_WIDGET_REALIZED (widget))
711 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
714 panel_allocation.width,
715 panel_allocation.height);
718 spin_button_redraw (spin);
722 gtk_spin_button_expose (GtkWidget *widget,
723 GdkEventExpose *event)
727 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
728 g_return_val_if_fail (event != NULL, FALSE);
730 spin = GTK_SPIN_BUTTON (widget);
732 if (GTK_WIDGET_DRAWABLE (widget))
734 GtkShadowType shadow_type;
737 if (event->window != spin->panel)
738 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->expose_event (widget, event);
740 /* we redraw the panel even if it wasn't exposed. This is
741 * because spin->panel is not a child window of widget->window,
742 * so it will not be invalidated by eg. gtk_widget_queue_draw().
747 gdk_drawable_get_size (spin->panel, &rect.width, &rect.height);
749 shadow_type = spin_button_get_shadow_type (spin);
751 gdk_window_begin_paint_rect (spin->panel, &rect);
753 if (shadow_type != GTK_SHADOW_NONE)
755 gtk_paint_box (widget->style, spin->panel,
756 GTK_STATE_NORMAL, shadow_type,
757 NULL, widget, "spinbutton",
758 rect.x, rect.y, rect.width, rect.height);
761 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
762 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
764 gdk_window_end_paint (spin->panel);
771 spin_button_at_limit (GtkSpinButton *spin_button,
774 GtkArrowType effective_arrow;
776 if (spin_button->wrap)
779 if (spin_button->adjustment->step_increment > 0)
780 effective_arrow = arrow;
782 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
784 if (effective_arrow == GTK_ARROW_UP &&
785 (spin_button->adjustment->upper - spin_button->adjustment->value <= EPSILON))
788 if (effective_arrow == GTK_ARROW_DOWN &&
789 (spin_button->adjustment->value - spin_button->adjustment->lower <= EPSILON))
796 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
797 GtkArrowType arrow_type)
799 GtkStateType state_type;
800 GtkShadowType shadow_type;
808 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
809 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
811 widget = GTK_WIDGET (spin_button);
813 if (GTK_WIDGET_DRAWABLE (widget))
815 width = spin_button_get_arrow_size (spin_button) + 2 * widget->style->xthickness;
817 if (arrow_type == GTK_ARROW_UP)
822 height = widget->requisition.height / 2;
827 y = widget->requisition.height / 2;
829 height = (widget->requisition.height + 1) / 2;
832 if (spin_button_at_limit (spin_button, arrow_type))
834 shadow_type = GTK_SHADOW_OUT;
835 state_type = GTK_STATE_INSENSITIVE;
839 if (spin_button->click_child == arrow_type)
841 state_type = GTK_STATE_ACTIVE;
842 shadow_type = GTK_SHADOW_IN;
846 if (spin_button->in_child == arrow_type &&
847 spin_button->click_child == NO_ARROW)
849 state_type = GTK_STATE_PRELIGHT;
853 state_type = GTK_WIDGET_STATE (widget);
856 shadow_type = GTK_SHADOW_OUT;
860 gtk_paint_box (widget->style, spin_button->panel,
861 state_type, shadow_type,
863 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
864 x, y, width, height);
866 height = widget->requisition.height;
868 if (arrow_type == GTK_ARROW_DOWN)
871 height = height - y - 2;
876 height = height / 2 - 2;
881 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
887 w -= w % 2 - 1; /* force odd */
890 x += (width - w) / 2;
891 y += (height - h) / 2;
896 gtk_paint_arrow (widget->style, spin_button->panel,
897 state_type, shadow_type,
898 NULL, widget, "spinbutton",
900 x, y, width, height);
905 gtk_spin_button_enter_notify (GtkWidget *widget,
906 GdkEventCrossing *event)
908 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
910 if (event->window == spin->panel)
915 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
917 if (y <= widget->requisition.height / 2)
918 spin->in_child = GTK_ARROW_UP;
920 spin->in_child = GTK_ARROW_DOWN;
922 spin_button_redraw (spin);
929 gtk_spin_button_leave_notify (GtkWidget *widget,
930 GdkEventCrossing *event)
932 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
934 spin->in_child = NO_ARROW;
935 spin_button_redraw (spin);
941 gtk_spin_button_focus_out (GtkWidget *widget,
942 GdkEventFocus *event)
944 if (GTK_ENTRY (widget)->editable)
945 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
947 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
951 gtk_spin_button_grab_notify (GtkWidget *widget,
952 gboolean was_grabbed)
954 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
958 gtk_spin_button_stop_spinning (spin);
959 spin_button_redraw (spin);
964 gtk_spin_button_state_changed (GtkWidget *widget,
965 GtkStateType previous_state)
967 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
969 if (!GTK_WIDGET_IS_SENSITIVE (widget))
971 gtk_spin_button_stop_spinning (spin);
972 spin_button_redraw (spin);
977 gtk_spin_button_scroll (GtkWidget *widget,
978 GdkEventScroll *event)
980 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
982 if (event->direction == GDK_SCROLL_UP)
984 if (!GTK_WIDGET_HAS_FOCUS (widget))
985 gtk_widget_grab_focus (widget);
986 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
988 else if (event->direction == GDK_SCROLL_DOWN)
990 if (!GTK_WIDGET_HAS_FOCUS (widget))
991 gtk_widget_grab_focus (widget);
992 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
1001 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1005 g_source_remove (spin->timer);
1007 spin->timer_calls = 0;
1008 spin->need_timer = FALSE;
1013 spin->timer_step = spin->adjustment->step_increment;
1014 spin->timer_calls = 0;
1016 spin->click_child = NO_ARROW;
1021 start_spinning (GtkSpinButton *spin,
1022 GtkArrowType click_child,
1025 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1027 spin->click_child = click_child;
1031 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1034 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1036 spin->timer_step = step;
1037 spin->need_timer = TRUE;
1038 spin->timer = g_timeout_add (timeout,
1039 (GSourceFunc) gtk_spin_button_timer,
1042 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1044 spin_button_redraw (spin);
1048 gtk_spin_button_button_press (GtkWidget *widget,
1049 GdkEventButton *event)
1051 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1055 if (event->window == spin->panel)
1057 if (!GTK_WIDGET_HAS_FOCUS (widget))
1058 gtk_widget_grab_focus (widget);
1059 spin->button = event->button;
1061 if (GTK_ENTRY (widget)->editable)
1062 gtk_spin_button_update (spin);
1064 if (event->y <= widget->requisition.height / 2)
1066 if (event->button == 1)
1067 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->step_increment);
1068 else if (event->button == 2)
1069 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->page_increment);
1071 spin->click_child = GTK_ARROW_UP;
1075 if (event->button == 1)
1076 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->step_increment);
1077 else if (event->button == 2)
1078 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->page_increment);
1080 spin->click_child = GTK_ARROW_DOWN;
1085 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1091 gtk_spin_button_button_release (GtkWidget *widget,
1092 GdkEventButton *event)
1094 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1097 arrow_size = spin_button_get_arrow_size (spin);
1099 if (event->button == spin->button)
1101 int click_child = spin->click_child;
1103 gtk_spin_button_stop_spinning (spin);
1105 if (event->button == 3)
1107 if (event->y >= 0 && event->x >= 0 &&
1108 event->y <= widget->requisition.height &&
1109 event->x <= arrow_size + 2 * widget->style->xthickness)
1111 if (click_child == GTK_ARROW_UP &&
1112 event->y <= widget->requisition.height / 2)
1116 diff = spin->adjustment->upper - spin->adjustment->value;
1118 gtk_spin_button_real_spin (spin, diff);
1120 else if (click_child == GTK_ARROW_DOWN &&
1121 event->y > widget->requisition.height / 2)
1125 diff = spin->adjustment->value - spin->adjustment->lower;
1127 gtk_spin_button_real_spin (spin, -diff);
1131 spin_button_redraw (spin);
1136 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1140 gtk_spin_button_motion_notify (GtkWidget *widget,
1141 GdkEventMotion *event)
1143 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1148 if (event->window == spin->panel)
1152 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1154 if (y <= widget->requisition.height / 2 &&
1155 spin->in_child == GTK_ARROW_DOWN)
1157 spin->in_child = GTK_ARROW_UP;
1158 spin_button_redraw (spin);
1160 else if (y > widget->requisition.height / 2 &&
1161 spin->in_child == GTK_ARROW_UP)
1163 spin->in_child = GTK_ARROW_DOWN;
1164 spin_button_redraw (spin);
1170 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1174 gtk_spin_button_timer (GtkSpinButton *spin_button)
1176 gboolean retval = FALSE;
1178 GDK_THREADS_ENTER ();
1180 if (spin_button->timer)
1182 if (spin_button->click_child == GTK_ARROW_UP)
1183 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1185 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1187 if (spin_button->need_timer)
1189 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1192 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1194 spin_button->need_timer = FALSE;
1195 spin_button->timer = g_timeout_add (timeout,
1196 (GSourceFunc) gtk_spin_button_timer,
1197 (gpointer) spin_button);
1201 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1202 < spin_button->adjustment->page_increment)
1204 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1205 spin_button->timer_calls++;
1208 spin_button->timer_calls = 0;
1209 spin_button->timer_step += spin_button->climb_rate;
1216 GDK_THREADS_LEAVE ();
1222 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1223 GtkSpinButton *spin_button)
1225 gboolean return_val;
1227 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1230 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1231 if (return_val == FALSE)
1232 gtk_spin_button_default_output (spin_button);
1234 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1236 spin_button_redraw (spin_button);
1238 g_object_notify (G_OBJECT (spin_button), "value");
1242 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1243 GtkScrollType scroll)
1245 /* We don't test whether the entry is editable, since
1246 * this key binding conceptually corresponds to changing
1247 * the value with the buttons using the mouse, which
1248 * we allow for non-editable spin buttons.
1252 case GTK_SCROLL_STEP_BACKWARD:
1253 case GTK_SCROLL_STEP_DOWN:
1254 case GTK_SCROLL_STEP_LEFT:
1255 gtk_spin_button_real_spin (spin, -spin->timer_step);
1257 if (spin->climb_rate > 0.0 && spin->timer_step
1258 < spin->adjustment->page_increment)
1260 if (spin->timer_calls < MAX_TIMER_CALLS)
1261 spin->timer_calls++;
1264 spin->timer_calls = 0;
1265 spin->timer_step += spin->climb_rate;
1270 case GTK_SCROLL_STEP_FORWARD:
1271 case GTK_SCROLL_STEP_UP:
1272 case GTK_SCROLL_STEP_RIGHT:
1273 gtk_spin_button_real_spin (spin, spin->timer_step);
1275 if (spin->climb_rate > 0.0 && spin->timer_step
1276 < spin->adjustment->page_increment)
1278 if (spin->timer_calls < MAX_TIMER_CALLS)
1279 spin->timer_calls++;
1282 spin->timer_calls = 0;
1283 spin->timer_step += spin->climb_rate;
1288 case GTK_SCROLL_PAGE_BACKWARD:
1289 case GTK_SCROLL_PAGE_DOWN:
1290 case GTK_SCROLL_PAGE_LEFT:
1291 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1294 case GTK_SCROLL_PAGE_FORWARD:
1295 case GTK_SCROLL_PAGE_UP:
1296 case GTK_SCROLL_PAGE_RIGHT:
1297 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1300 case GTK_SCROLL_START:
1302 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1304 gtk_spin_button_real_spin (spin, -diff);
1308 case GTK_SCROLL_END:
1310 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1312 gtk_spin_button_real_spin (spin, diff);
1317 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1321 gtk_spin_button_update (spin);
1325 gtk_spin_button_key_release (GtkWidget *widget,
1328 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1330 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1331 spin->timer_step = spin->adjustment->step_increment;
1332 spin->timer_calls = 0;
1338 gtk_spin_button_snap (GtkSpinButton *spin_button,
1344 inc = spin_button->adjustment->step_increment;
1348 tmp = (val - spin_button->adjustment->lower) / inc;
1349 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1350 val = spin_button->adjustment->lower + floor (tmp) * inc;
1352 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1354 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1355 gtk_adjustment_set_value (spin_button->adjustment, val);
1358 gint return_val = FALSE;
1359 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1360 if (return_val == FALSE)
1361 gtk_spin_button_default_output (spin_button);
1366 gtk_spin_button_activate (GtkEntry *entry)
1368 if (entry->editable)
1369 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1371 /* Chain up so that entry->activates_default is honored */
1372 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1376 gtk_spin_button_insert_text (GtkEditable *editable,
1377 const gchar *new_text,
1378 gint new_text_length,
1381 GtkEntry *entry = GTK_ENTRY (editable);
1382 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1383 GtkEditableClass *parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class, GTK_TYPE_EDITABLE);
1395 entry_length = entry->text_length;
1399 if (*(lc->negative_sign))
1400 neg_sign = *(lc->negative_sign);
1404 if (*(lc->positive_sign))
1405 pos_sign = *(lc->positive_sign);
1409 for (sign=0, i=0; i<entry_length; i++)
1410 if ((entry->text[i] == neg_sign) ||
1411 (entry->text[i] == pos_sign))
1417 if (sign && !(*position))
1420 for (dotpos=-1, i=0; i<entry_length; i++)
1421 if (entry->text[i] == *(lc->decimal_point))
1427 if (dotpos > -1 && *position > dotpos &&
1428 (gint)spin->digits - entry_length
1429 + dotpos - new_text_length + 1 < 0)
1432 for (i = 0; i < new_text_length; i++)
1434 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1436 if (sign || (*position) || i)
1440 else if (new_text[i] == *(lc->decimal_point))
1442 if (!spin->digits || dotpos > -1 ||
1443 (new_text_length - 1 - i + entry_length
1444 - *position > (gint)spin->digits))
1446 dotpos = *position + i;
1448 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1453 parent_editable_iface->insert_text (editable, new_text,
1454 new_text_length, position);
1458 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1462 gdouble new_value = 0.0;
1463 gboolean wrapped = FALSE;
1465 adj = spin_button->adjustment;
1467 new_value = adj->value + increment;
1471 if (spin_button->wrap)
1473 if (fabs (adj->value - adj->upper) < EPSILON)
1475 new_value = adj->lower;
1478 else if (new_value > adj->upper)
1479 new_value = adj->upper;
1482 new_value = MIN (new_value, adj->upper);
1484 else if (increment < 0)
1486 if (spin_button->wrap)
1488 if (fabs (adj->value - adj->lower) < EPSILON)
1490 new_value = adj->upper;
1493 else if (new_value < adj->lower)
1494 new_value = adj->lower;
1497 new_value = MAX (new_value, adj->lower);
1500 if (fabs (new_value - adj->value) > EPSILON)
1501 gtk_adjustment_set_value (adj, new_value);
1504 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
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 * Note that the way in which the precision is derived works best if @step
1601 * is a power of ten. If the resulting precision is not suitable for your
1602 * needs, use gtk_spin_button_set_digits() to correct it.
1604 * Return value: The new spin button as a #GtkWidget.
1607 gtk_spin_button_new_with_range (gdouble min,
1612 GtkSpinButton *spin;
1615 g_return_val_if_fail (min <= max, NULL);
1616 g_return_val_if_fail (step != 0.0, NULL);
1618 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1620 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1622 if (fabs (step) >= 1.0 || step == 0.0)
1625 digits = abs ((gint) floor (log10 (fabs (step))));
1626 if (digits > MAX_DIGITS)
1627 digits = MAX_DIGITS;
1630 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1632 gtk_spin_button_set_numeric (spin, TRUE);
1634 return GTK_WIDGET (spin);
1637 /* Callback used when the spin button's adjustment changes. We need to redraw
1638 * the arrows when the adjustment's range changes, and reevaluate our size request.
1641 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1643 GtkSpinButton *spin_button;
1645 spin_button = GTK_SPIN_BUTTON (data);
1647 spin_button->timer_step = spin_button->adjustment->step_increment;
1648 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1652 * gtk_spin_button_set_adjustment:
1653 * @spin_button: a #GtkSpinButton
1654 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1656 * Replaces the #GtkAdjustment associated with @spin_button.
1659 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1660 GtkAdjustment *adjustment)
1662 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1664 if (spin_button->adjustment != adjustment)
1666 if (spin_button->adjustment)
1668 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1669 gtk_spin_button_value_changed,
1671 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1672 adjustment_changed_cb,
1674 g_object_unref (spin_button->adjustment);
1676 spin_button->adjustment = adjustment;
1679 g_object_ref_sink (adjustment);
1680 g_signal_connect (adjustment, "value_changed",
1681 G_CALLBACK (gtk_spin_button_value_changed),
1683 g_signal_connect (adjustment, "changed",
1684 G_CALLBACK (adjustment_changed_cb),
1686 spin_button->timer_step = spin_button->adjustment->step_increment;
1689 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1692 g_object_notify (G_OBJECT (spin_button), "adjustment");
1696 * gtk_spin_button_get_adjustment:
1697 * @spin_button: a #GtkSpinButton
1699 * Get the adjustment associated with a #GtkSpinButton
1701 * Return value: the #GtkAdjustment of @spin_button
1704 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1706 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1708 return spin_button->adjustment;
1712 * gtk_spin_button_set_digits:
1713 * @spin_button: a #GtkSpinButton
1714 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1716 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1720 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1723 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1725 if (spin_button->digits != digits)
1727 spin_button->digits = digits;
1728 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1729 g_object_notify (G_OBJECT (spin_button), "digits");
1731 /* since lower/upper may have changed */
1732 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1737 * gtk_spin_button_get_digits:
1738 * @spin_button: a #GtkSpinButton
1740 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1742 * Returns: the current precision
1745 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1747 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1749 return spin_button->digits;
1753 * gtk_spin_button_set_increments:
1754 * @spin_button: a #GtkSpinButton
1755 * @step: increment applied for a button 1 press.
1756 * @page: increment applied for a button 2 press.
1758 * Sets the step and page increments for spin_button. This affects how
1759 * quickly the value changes when the spin button's arrows are activated.
1762 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1766 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1768 spin_button->adjustment->step_increment = step;
1769 spin_button->adjustment->page_increment = page;
1773 * gtk_spin_button_get_increments:
1774 * @spin_button: a #GtkSpinButton
1775 * @step: location to store step increment, or %NULL
1776 * @page: location to store page increment, or %NULL
1778 * Gets the current step and page the increments used by @spin_button. See
1779 * gtk_spin_button_set_increments().
1782 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1786 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1789 *step = spin_button->adjustment->step_increment;
1791 *page = spin_button->adjustment->page_increment;
1795 * gtk_spin_button_set_range:
1796 * @spin_button: a #GtkSpinButton
1797 * @min: minimum allowable value
1798 * @max: maximum allowable value
1800 * Sets the minimum and maximum allowable values for @spin_button
1803 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1809 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1811 spin_button->adjustment->lower = min;
1812 spin_button->adjustment->upper = max;
1814 value = CLAMP (spin_button->adjustment->value,
1815 spin_button->adjustment->lower,
1816 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1818 if (value != spin_button->adjustment->value)
1819 gtk_spin_button_set_value (spin_button, value);
1821 gtk_adjustment_changed (spin_button->adjustment);
1825 * gtk_spin_button_get_range:
1826 * @spin_button: a #GtkSpinButton
1827 * @min: location to store minimum allowed value, or %NULL
1828 * @max: location to store maximum allowed value, or %NULL
1830 * Gets the range allowed for @spin_button. See
1831 * gtk_spin_button_set_range().
1834 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1838 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1841 *min = spin_button->adjustment->lower;
1843 *max = spin_button->adjustment->upper;
1847 * gtk_spin_button_get_value:
1848 * @spin_button: a #GtkSpinButton
1850 * Get the value in the @spin_button.
1852 * Return value: the value of @spin_button
1855 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1857 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1859 return spin_button->adjustment->value;
1863 * gtk_spin_button_get_value_as_int:
1864 * @spin_button: a #GtkSpinButton
1866 * Get the value @spin_button represented as an integer.
1868 * Return value: the value of @spin_button
1871 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1875 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1877 val = spin_button->adjustment->value;
1878 if (val - floor (val) < ceil (val) - val)
1885 * gtk_spin_button_set_value:
1886 * @spin_button: a #GtkSpinButton
1887 * @value: the new value
1889 * Set the value of @spin_button.
1892 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1895 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1897 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1898 gtk_adjustment_set_value (spin_button->adjustment, value);
1901 gint return_val = FALSE;
1902 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1903 if (return_val == FALSE)
1904 gtk_spin_button_default_output (spin_button);
1909 * gtk_spin_button_set_update_policy:
1910 * @spin_button: a #GtkSpinButton
1911 * @policy: a #GtkSpinButtonUpdatePolicy value
1913 * Sets the update behavior of a spin button. This determines whether the
1914 * spin button is always updated or only when a valid value is set.
1917 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1918 GtkSpinButtonUpdatePolicy policy)
1920 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1922 if (spin_button->update_policy != policy)
1924 spin_button->update_policy = policy;
1925 g_object_notify (G_OBJECT (spin_button), "update-policy");
1930 * gtk_spin_button_get_update_policy:
1931 * @spin_button: a #GtkSpinButton
1933 * Gets the update behavior of a spin button. See
1934 * gtk_spin_button_set_update_policy().
1936 * Return value: the current update policy
1938 GtkSpinButtonUpdatePolicy
1939 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1941 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1943 return spin_button->update_policy;
1947 * gtk_spin_button_set_numeric:
1948 * @spin_button: a #GtkSpinButton
1949 * @numeric: flag indicating if only numeric entry is allowed.
1951 * Sets the flag that determines if non-numeric text can be typed into
1955 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1958 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1960 numeric = numeric != FALSE;
1962 if (spin_button->numeric != numeric)
1964 spin_button->numeric = numeric;
1965 g_object_notify (G_OBJECT (spin_button), "numeric");
1970 * gtk_spin_button_get_numeric:
1971 * @spin_button: a #GtkSpinButton
1973 * Returns whether non-numeric text can be typed into the spin button.
1974 * See gtk_spin_button_set_numeric().
1976 * Return value: %TRUE if only numeric text can be entered
1979 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1981 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1983 return spin_button->numeric;
1987 * gtk_spin_button_set_wrap:
1988 * @spin_button: a #GtkSpinButton
1989 * @wrap: a flag indicating if wrapping behavior is performed.
1991 * Sets the flag that determines if a spin button value wraps around to the
1992 * opposite limit when the upper or lower limit of the range is exceeded.
1995 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1998 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2000 wrap = wrap != FALSE;
2002 if (spin_button->wrap != wrap)
2004 spin_button->wrap = (wrap != 0);
2006 g_object_notify (G_OBJECT (spin_button), "wrap");
2011 * gtk_spin_button_get_wrap:
2012 * @spin_button: a #GtkSpinButton
2014 * Returns whether the spin button's value wraps around to the
2015 * opposite limit when the upper or lower limit of the range is
2016 * exceeded. See gtk_spin_button_set_wrap().
2018 * Return value: %TRUE if the spin button wraps around
2021 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2023 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2025 return spin_button->wrap;
2029 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2031 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
2034 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2036 return arrow_size - arrow_size % 2; /* force even */
2040 * spin_button_get_shadow_type:
2041 * @spin_button: a #GtkSpinButton
2043 * Convenience function to Get the shadow type from the underlying widget's
2046 * Return value: the #GtkShadowType
2049 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2051 GtkShadowType rc_shadow_type;
2053 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow-type", &rc_shadow_type, NULL);
2055 return rc_shadow_type;
2059 * gtk_spin_button_set_snap_to_ticks:
2060 * @spin_button: a #GtkSpinButton
2061 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2063 * Sets the policy as to whether values are corrected to the nearest step
2064 * increment when a spin button is activated after providing an invalid value.
2067 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2068 gboolean snap_to_ticks)
2072 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2074 new_val = (snap_to_ticks != 0);
2076 if (new_val != spin_button->snap_to_ticks)
2078 spin_button->snap_to_ticks = new_val;
2079 if (new_val && GTK_ENTRY (spin_button)->editable)
2080 gtk_spin_button_update (spin_button);
2082 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2087 * gtk_spin_button_get_snap_to_ticks:
2088 * @spin_button: a #GtkSpinButton
2090 * Returns whether the values are corrected to the nearest step. See
2091 * gtk_spin_button_set_snap_to_ticks().
2093 * Return value: %TRUE if values are snapped to the nearest step.
2096 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2098 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2100 return spin_button->snap_to_ticks;
2104 * gtk_spin_button_spin:
2105 * @spin_button: a #GtkSpinButton
2106 * @direction: a #GtkSpinType indicating the direction to spin.
2107 * @increment: step increment to apply in the specified direction.
2109 * Increment or decrement a spin button's value in a specified direction
2110 * by a specified amount.
2113 gtk_spin_button_spin (GtkSpinButton *spin_button,
2114 GtkSpinType direction,
2120 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2122 adj = spin_button->adjustment;
2124 /* for compatibility with the 1.0.x version of this function */
2125 if (increment != 0 && increment != adj->step_increment &&
2126 (direction == GTK_SPIN_STEP_FORWARD ||
2127 direction == GTK_SPIN_STEP_BACKWARD))
2129 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2130 increment = -increment;
2131 direction = GTK_SPIN_USER_DEFINED;
2136 case GTK_SPIN_STEP_FORWARD:
2138 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2141 case GTK_SPIN_STEP_BACKWARD:
2143 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2146 case GTK_SPIN_PAGE_FORWARD:
2148 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2151 case GTK_SPIN_PAGE_BACKWARD:
2153 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2158 diff = adj->value - adj->lower;
2160 gtk_spin_button_real_spin (spin_button, -diff);
2165 diff = adj->upper - adj->value;
2167 gtk_spin_button_real_spin (spin_button, diff);
2170 case GTK_SPIN_USER_DEFINED:
2173 gtk_spin_button_real_spin (spin_button, increment);
2182 * gtk_spin_button_update:
2183 * @spin_button: a #GtkSpinButton
2185 * Manually force an update of the spin button.
2188 gtk_spin_button_update (GtkSpinButton *spin_button)
2194 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2197 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2198 if (return_val == FALSE)
2200 return_val = gtk_spin_button_default_input (spin_button, &val);
2201 error = (return_val == GTK_INPUT_ERROR);
2203 else if (return_val == GTK_INPUT_ERROR)
2206 spin_button_redraw (spin_button);
2208 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2210 if (val < spin_button->adjustment->lower)
2211 val = spin_button->adjustment->lower;
2212 else if (val > spin_button->adjustment->upper)
2213 val = spin_button->adjustment->upper;
2215 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2217 val < spin_button->adjustment->lower ||
2218 val > spin_button->adjustment->upper))
2220 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2224 if (spin_button->snap_to_ticks)
2225 gtk_spin_button_snap (spin_button, val);
2228 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2229 gtk_adjustment_set_value (spin_button->adjustment, val);
2233 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0,
2235 if (return_val == FALSE)
2236 gtk_spin_button_default_output (spin_button);
2242 spin_button_redraw (GtkSpinButton *spin_button)
2246 widget = GTK_WIDGET (spin_button);
2248 if (GTK_WIDGET_DRAWABLE (widget))
2250 gtk_widget_queue_draw (widget);
2252 /* We must invalidate the panel window ourselves, because it
2253 * is not a child of widget->window
2255 gdk_window_invalidate_rect (spin_button->panel, NULL, TRUE);
2259 #define __GTK_SPIN_BUTTON_C__
2260 #include "gtkaliasdef.c"