1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GtkSpinButton widget for GTK+
5 * Copyright (C) 1998 Lars Hamann and Stefan Jeske
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
35 #include "gdk/gdkkeysyms.h"
36 #include "gtkspinbutton.h"
38 #include "gtksignal.h"
39 #include "gtksettings.h"
42 #define MIN_SPIN_BUTTON_WIDTH 30
44 #define SPIN_BUTTON_INITIAL_TIMER_DELAY 200
45 #define SPIN_BUTTON_TIMER_DELAY 20
46 #define MAX_TIMER_CALLS 5
71 static void gtk_spin_button_class_init (GtkSpinButtonClass *klass);
72 static void gtk_spin_button_init (GtkSpinButton *spin_button);
73 static void gtk_spin_button_finalize (GObject *object);
74 static void gtk_spin_button_set_property (GObject *object,
78 static void gtk_spin_button_get_property (GObject *object,
82 static void gtk_spin_button_map (GtkWidget *widget);
83 static void gtk_spin_button_unmap (GtkWidget *widget);
84 static void gtk_spin_button_realize (GtkWidget *widget);
85 static void gtk_spin_button_unrealize (GtkWidget *widget);
86 static void gtk_spin_button_size_request (GtkWidget *widget,
87 GtkRequisition *requisition);
88 static void gtk_spin_button_size_allocate (GtkWidget *widget,
89 GtkAllocation *allocation);
90 static gint gtk_spin_button_expose (GtkWidget *widget,
91 GdkEventExpose *event);
92 static gint gtk_spin_button_button_press (GtkWidget *widget,
93 GdkEventButton *event);
94 static gint gtk_spin_button_button_release (GtkWidget *widget,
95 GdkEventButton *event);
96 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
97 GdkEventMotion *event);
98 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
99 GdkEventCrossing *event);
100 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
101 GdkEventCrossing *event);
102 static gint gtk_spin_button_focus_out (GtkWidget *widget,
103 GdkEventFocus *event);
104 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
106 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
107 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
108 GtkSpinButton *spin_button);
109 static gint gtk_spin_button_key_press (GtkWidget *widget,
111 static gint gtk_spin_button_key_release (GtkWidget *widget,
113 static gint gtk_spin_button_scroll (GtkWidget *widget,
114 GdkEventScroll *event);
115 static void gtk_spin_button_activate (GtkEntry *entry);
116 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
118 static void gtk_spin_button_insert_text (GtkEntry *entry,
119 const gchar *new_text,
120 gint new_text_length,
122 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
124 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
126 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
127 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
130 static GtkEntryClass *parent_class = NULL;
131 static guint spinbutton_signals[LAST_SIGNAL] = {0};
135 gtk_spin_button_get_type (void)
137 static GtkType spin_button_type = 0;
139 if (!spin_button_type)
141 static const GtkTypeInfo spin_button_info =
144 sizeof (GtkSpinButton),
145 sizeof (GtkSpinButtonClass),
146 (GtkClassInitFunc) gtk_spin_button_class_init,
147 (GtkObjectInitFunc) gtk_spin_button_init,
148 /* reserved_1 */ NULL,
149 /* reserved_2 */ NULL,
150 (GtkClassInitFunc) NULL,
153 spin_button_type = gtk_type_unique (GTK_TYPE_ENTRY, &spin_button_info);
155 return spin_button_type;
159 gtk_spin_button_class_init (GtkSpinButtonClass *class)
161 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
162 GtkObjectClass *object_class;
163 GtkWidgetClass *widget_class;
164 GtkEntryClass *entry_class;
166 object_class = (GtkObjectClass*) class;
167 widget_class = (GtkWidgetClass*) class;
168 entry_class = (GtkEntryClass*) class;
170 parent_class = gtk_type_class (GTK_TYPE_ENTRY);
172 gobject_class->finalize = gtk_spin_button_finalize;
174 gobject_class->set_property = gtk_spin_button_set_property;
175 gobject_class->get_property = gtk_spin_button_get_property;
177 widget_class->map = gtk_spin_button_map;
178 widget_class->unmap = gtk_spin_button_unmap;
179 widget_class->realize = gtk_spin_button_realize;
180 widget_class->unrealize = gtk_spin_button_unrealize;
181 widget_class->size_request = gtk_spin_button_size_request;
182 widget_class->size_allocate = gtk_spin_button_size_allocate;
183 widget_class->expose_event = gtk_spin_button_expose;
184 widget_class->scroll_event = gtk_spin_button_scroll;
185 widget_class->button_press_event = gtk_spin_button_button_press;
186 widget_class->button_release_event = gtk_spin_button_button_release;
187 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
188 widget_class->key_press_event = gtk_spin_button_key_press;
189 widget_class->key_release_event = gtk_spin_button_key_release;
190 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
191 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
192 widget_class->focus_out_event = gtk_spin_button_focus_out;
194 entry_class->insert_text = gtk_spin_button_insert_text;
195 entry_class->activate = gtk_spin_button_activate;
198 class->output = NULL;
200 g_object_class_install_property (gobject_class,
202 g_param_spec_object ("adjustment",
204 _("The adjustment that holds the value of the spinbutton"),
208 g_object_class_install_property (gobject_class,
210 g_param_spec_double ("climb_rate",
212 _("The acceleration rate when you hold down a button"),
218 g_object_class_install_property (gobject_class,
220 g_param_spec_uint ("digits",
222 _("The number of decimal places to display"),
228 g_object_class_install_property (gobject_class,
230 g_param_spec_boolean ("snap_to_ticks",
232 _("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
236 g_object_class_install_property (gobject_class,
238 g_param_spec_boolean ("numeric",
240 _("Whether non-numeric characters should be ignored"),
244 g_object_class_install_property (gobject_class,
246 g_param_spec_boolean ("wrap",
248 _("Whether a spin button should wrap upon reaching its limits"),
252 g_object_class_install_property (gobject_class,
254 g_param_spec_enum ("update_policy",
256 _("Whether the spin button should update always, or only when the value is legal"),
257 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
261 g_object_class_install_property (gobject_class,
263 g_param_spec_double ("value",
265 _("Reads the current value, or sets a new value"),
271 gtk_widget_class_install_style_property_parser (widget_class,
272 g_param_spec_enum ("shadow_type", "Shadow Type", NULL,
273 GTK_TYPE_SHADOW_TYPE,
276 gtk_rc_property_parse_enum);
277 spinbutton_signals[INPUT] =
278 gtk_signal_new ("input",
280 GTK_CLASS_TYPE (object_class),
281 GTK_SIGNAL_OFFSET (GtkSpinButtonClass, input),
282 gtk_marshal_INT__POINTER,
283 GTK_TYPE_INT, 1, GTK_TYPE_POINTER);
285 spinbutton_signals[OUTPUT] =
286 g_signal_new ("output",
287 G_TYPE_FROM_CLASS(object_class),
289 G_STRUCT_OFFSET(GtkSpinButtonClass, output),
290 _gtk_boolean_handled_accumulator, NULL,
291 gtk_marshal_BOOLEAN__VOID,
294 spinbutton_signals[VALUE_CHANGED] =
295 gtk_signal_new ("value_changed",
297 GTK_CLASS_TYPE (object_class),
298 GTK_SIGNAL_OFFSET (GtkSpinButtonClass, value_changed),
299 gtk_marshal_VOID__VOID,
304 gtk_spin_button_set_property (GObject *object,
309 GtkSpinButton *spin_button;
311 spin_button = GTK_SPIN_BUTTON (object);
315 GtkAdjustment *adjustment;
317 case PROP_ADJUSTMENT:
318 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
320 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
321 gtk_spin_button_set_adjustment (spin_button, adjustment);
323 case PROP_CLIMB_RATE:
324 gtk_spin_button_configure (spin_button,
325 spin_button->adjustment,
326 g_value_get_double (value),
327 spin_button->digits);
330 gtk_spin_button_configure (spin_button,
331 spin_button->adjustment,
332 spin_button->climb_rate,
333 g_value_get_uint (value));
335 case PROP_SNAP_TO_TICKS:
336 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
339 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
342 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
344 case PROP_UPDATE_POLICY:
345 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
348 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
356 gtk_spin_button_get_property (GObject *object,
361 GtkSpinButton *spin_button;
363 spin_button = GTK_SPIN_BUTTON (object);
367 case PROP_ADJUSTMENT:
368 g_value_set_object (value, G_OBJECT (spin_button->adjustment));
370 case PROP_CLIMB_RATE:
371 g_value_set_double (value, spin_button->climb_rate);
374 g_value_set_uint (value, spin_button->digits);
376 case PROP_SNAP_TO_TICKS:
377 g_value_set_boolean (value, spin_button->snap_to_ticks);
380 g_value_set_boolean (value, spin_button->numeric);
383 g_value_set_boolean (value, spin_button->wrap);
385 case PROP_UPDATE_POLICY:
386 g_value_set_enum (value, spin_button->update_policy);
389 g_value_set_double (value, spin_button->adjustment->value);
392 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
398 gtk_spin_button_init (GtkSpinButton *spin_button)
400 spin_button->adjustment = NULL;
401 spin_button->panel = NULL;
402 spin_button->timer = 0;
403 spin_button->ev_time = 0;
404 spin_button->climb_rate = 0.0;
405 spin_button->timer_step = 0.0;
406 spin_button->update_policy = GTK_UPDATE_ALWAYS;
407 spin_button->in_child = 2;
408 spin_button->click_child = 2;
409 spin_button->button = 0;
410 spin_button->need_timer = FALSE;
411 spin_button->timer_calls = 0;
412 spin_button->digits = 0;
413 spin_button->numeric = FALSE;
414 spin_button->wrap = FALSE;
415 spin_button->snap_to_ticks = FALSE;
416 gtk_spin_button_set_adjustment (spin_button,
417 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
421 gtk_spin_button_finalize (GObject *object)
423 g_return_if_fail (GTK_IS_SPIN_BUTTON (object));
425 gtk_object_unref (GTK_OBJECT (GTK_SPIN_BUTTON (object)->adjustment));
427 G_OBJECT_CLASS (parent_class)->finalize (object);
431 gtk_spin_button_map (GtkWidget *widget)
433 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
435 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
437 GTK_WIDGET_CLASS (parent_class)->map (widget);
438 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
443 gtk_spin_button_unmap (GtkWidget *widget)
445 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
447 if (GTK_WIDGET_MAPPED (widget))
449 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
450 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
455 gtk_spin_button_realize (GtkWidget *widget)
457 GtkSpinButton *spin_button;
458 GdkWindowAttr attributes;
459 gint attributes_mask;
463 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
465 spin_button = GTK_SPIN_BUTTON (widget);
467 real_width = widget->allocation.width;
468 widget->allocation.width -= ARROW_SIZE + 2 * widget->style->xthickness;
469 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
470 GDK_KEY_RELEASE_MASK);
471 GTK_WIDGET_CLASS (parent_class)->realize (widget);
473 widget->allocation.width = real_width;
475 attributes.window_type = GDK_WINDOW_CHILD;
476 attributes.wclass = GDK_INPUT_OUTPUT;
477 attributes.visual = gtk_widget_get_visual (widget);
478 attributes.colormap = gtk_widget_get_colormap (widget);
479 attributes.event_mask = gtk_widget_get_events (widget);
480 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
481 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
482 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
484 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
486 attributes.x = (widget->allocation.x + widget->allocation.width - ARROW_SIZE -
487 2 * widget->style->xthickness);
488 attributes.y = widget->allocation.y + (widget->allocation.height -
489 widget->requisition.height) / 2;
490 attributes.width = ARROW_SIZE + 2 * widget->style->xthickness;
491 attributes.height = widget->requisition.height;
493 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
494 &attributes, attributes_mask);
495 gdk_window_set_user_data (spin_button->panel, widget);
497 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
500 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
502 if (return_val == FALSE)
503 gtk_spin_button_default_output (spin_button);
507 gtk_spin_button_unrealize (GtkWidget *widget)
511 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
513 spin = GTK_SPIN_BUTTON (widget);
515 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
519 gdk_window_set_user_data (spin->panel, NULL);
520 gdk_window_destroy (spin->panel);
526 compute_double_length (double val, int digits)
532 if (fabs (val) > 1.0)
533 a = floor (log10 (fabs (val))) + 1;
545 return a + digits + extra;
549 gtk_spin_button_size_request (GtkWidget *widget,
550 GtkRequisition *requisition)
553 GtkSpinButton *spin_button;
555 g_return_if_fail (requisition != NULL);
556 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
558 entry = GTK_ENTRY (widget);
559 spin_button = GTK_SPIN_BUTTON (widget);
561 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
563 if (entry->width_chars < 0)
565 PangoContext *context;
566 PangoFontMetrics *metrics;
572 context = gtk_widget_get_pango_context (widget);
573 metrics = pango_context_get_metrics (context,
574 widget->style->font_desc,
575 pango_context_get_language (context));
577 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
578 digit_width = PANGO_PIXELS (digit_width);
580 pango_font_metrics_unref (metrics);
582 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
584 width = MIN_SPIN_BUTTON_WIDTH;
586 string_len = compute_double_length (spin_button->adjustment->upper,
587 spin_button->digits);
588 w = MIN (string_len, 10) * digit_width;
589 width = MAX (width, w);
590 string_len = compute_double_length (spin_button->adjustment->lower,
591 spin_button->digits);
592 w = MIN (string_len, 10) * digit_width;
593 width = MAX (width, w);
595 requisition->width = width + ARROW_SIZE + 2 * widget->style->xthickness;
598 requisition->width += ARROW_SIZE + 2 * widget->style->xthickness;
602 gtk_spin_button_size_allocate (GtkWidget *widget,
603 GtkAllocation *allocation)
605 GtkAllocation child_allocation;
607 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
608 g_return_if_fail (allocation != NULL);
610 child_allocation = *allocation;
611 if (child_allocation.width > ARROW_SIZE + 2 * widget->style->xthickness)
612 child_allocation.width -= ARROW_SIZE + 2 * widget->style->xthickness;
614 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
615 child_allocation.x += ARROW_SIZE + 2 * widget->style->xthickness;
617 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &child_allocation);
619 widget->allocation = *allocation;
621 if (GTK_WIDGET_REALIZED (widget))
623 child_allocation.width = ARROW_SIZE + 2 * widget->style->xthickness;
624 child_allocation.height = widget->requisition.height;
626 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
627 child_allocation.x = (allocation->x + allocation->width - ARROW_SIZE -
628 2 * widget->style->xthickness);
630 child_allocation.x = allocation->x;
632 child_allocation.y = allocation->y + (allocation->height - widget->requisition.height) / 2;
634 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
637 child_allocation.width,
638 child_allocation.height);
643 gtk_spin_button_expose (GtkWidget *widget,
644 GdkEventExpose *event)
648 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
649 g_return_val_if_fail (event != NULL, FALSE);
651 spin = GTK_SPIN_BUTTON (widget);
653 if (GTK_WIDGET_DRAWABLE (widget))
655 GtkShadowType shadow_type;
657 /* FIXME this seems like really broken code -
658 * why aren't we looking at event->window
659 * and acting accordingly?
662 shadow_type = spin_button_get_shadow_type (spin);
663 if (shadow_type != GTK_SHADOW_NONE)
664 gtk_paint_box (widget->style, spin->panel,
665 GTK_STATE_NORMAL, shadow_type,
666 &event->area, widget, "spinbutton",
668 ARROW_SIZE + 2 * widget->style->xthickness,
669 widget->requisition.height);
672 gdk_window_set_back_pixmap (spin->panel, NULL, TRUE);
673 gdk_window_clear_area (spin->panel,
674 event->area.x, event->area.y,
675 event->area.width, event->area.height);
677 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
678 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
680 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
687 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
690 GtkShadowType spin_shadow_type;
691 GtkStateType state_type;
692 GtkShadowType shadow_type;
697 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
699 widget = GTK_WIDGET (spin_button);
700 spin_shadow_type = spin_button_get_shadow_type (spin_button);
702 if (GTK_WIDGET_DRAWABLE (spin_button))
704 if (!spin_button->wrap &&
705 (((arrow == GTK_ARROW_UP &&
706 (spin_button->adjustment->upper - spin_button->adjustment->value
708 ((arrow == GTK_ARROW_DOWN &&
709 (spin_button->adjustment->value - spin_button->adjustment->lower
712 shadow_type = GTK_SHADOW_ETCHED_IN;
713 state_type = GTK_STATE_NORMAL;
717 if (spin_button->in_child == arrow)
719 if (spin_button->click_child == arrow)
720 state_type = GTK_STATE_ACTIVE;
722 state_type = GTK_STATE_PRELIGHT;
725 state_type = GTK_STATE_NORMAL;
727 if (spin_button->click_child == arrow)
728 shadow_type = GTK_SHADOW_IN;
730 shadow_type = GTK_SHADOW_OUT;
732 if (arrow == GTK_ARROW_UP)
734 if (spin_shadow_type != GTK_SHADOW_NONE)
736 x = widget->style->xthickness;
737 y = widget->style->ythickness;
741 x = widget->style->xthickness - 1;
742 y = widget->style->ythickness - 1;
744 gtk_paint_arrow (widget->style, spin_button->panel,
745 state_type, shadow_type,
746 NULL, widget, "spinbutton",
748 x, y, ARROW_SIZE, widget->requisition.height / 2
749 - widget->style->ythickness);
753 if (spin_shadow_type != GTK_SHADOW_NONE)
755 x = widget->style->xthickness;
756 y = widget->requisition.height / 2;
760 x = widget->style->xthickness - 1;
761 y = widget->requisition.height / 2 + 1;
763 gtk_paint_arrow (widget->style, spin_button->panel,
764 state_type, shadow_type,
765 NULL, widget, "spinbutton",
767 x, y, ARROW_SIZE, widget->requisition.height / 2
768 - widget->style->ythickness);
774 gtk_spin_button_enter_notify (GtkWidget *widget,
775 GdkEventCrossing *event)
779 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
780 g_return_val_if_fail (event != NULL, FALSE);
782 spin = GTK_SPIN_BUTTON (widget);
784 if (event->window == spin->panel)
789 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
791 if (y <= widget->requisition.height / 2)
793 spin->in_child = GTK_ARROW_UP;
794 if (spin->click_child == 2)
795 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
799 spin->in_child = GTK_ARROW_DOWN;
800 if (spin->click_child == 2)
801 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
808 gtk_spin_button_leave_notify (GtkWidget *widget,
809 GdkEventCrossing *event)
813 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
814 g_return_val_if_fail (event != NULL, FALSE);
816 spin = GTK_SPIN_BUTTON (widget);
818 if (event->window == spin->panel && spin->click_child == 2)
820 if (spin->in_child == GTK_ARROW_UP)
823 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
828 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
835 gtk_spin_button_focus_out (GtkWidget *widget,
836 GdkEventFocus *event)
838 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
839 g_return_val_if_fail (event != NULL, FALSE);
841 if (GTK_ENTRY (widget)->editable)
842 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
844 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
848 gtk_spin_button_scroll (GtkWidget *widget,
849 GdkEventScroll *event)
853 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
854 g_return_val_if_fail (event != NULL, FALSE);
856 spin = GTK_SPIN_BUTTON (widget);
858 if (event->direction == GDK_SCROLL_UP)
860 if (!GTK_WIDGET_HAS_FOCUS (widget))
861 gtk_widget_grab_focus (widget);
862 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
864 else if (event->direction == GDK_SCROLL_DOWN)
866 if (!GTK_WIDGET_HAS_FOCUS (widget))
867 gtk_widget_grab_focus (widget);
868 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
877 gtk_spin_button_button_press (GtkWidget *widget,
878 GdkEventButton *event)
882 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
883 g_return_val_if_fail (event != NULL, FALSE);
885 spin = GTK_SPIN_BUTTON (widget);
889 if (event->window == spin->panel)
891 if (!GTK_WIDGET_HAS_FOCUS (widget))
892 gtk_widget_grab_focus (widget);
893 gtk_grab_add (widget);
894 spin->button = event->button;
896 if (GTK_ENTRY (widget)->editable)
897 gtk_spin_button_update (spin);
899 if (event->y <= widget->requisition.height / 2)
901 spin->click_child = GTK_ARROW_UP;
902 if (event->button == 1)
904 gtk_spin_button_real_spin (spin,
905 spin->adjustment->step_increment);
908 spin->timer_step = spin->adjustment->step_increment;
909 spin->need_timer = TRUE;
910 spin->timer = gtk_timeout_add
911 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
912 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
915 else if (event->button == 2)
917 gtk_spin_button_real_spin (spin,
918 spin->adjustment->page_increment);
921 spin->timer_step = spin->adjustment->page_increment;
922 spin->need_timer = TRUE;
923 spin->timer = gtk_timeout_add
924 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
925 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
928 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
932 spin->click_child = GTK_ARROW_DOWN;
933 if (event->button == 1)
935 gtk_spin_button_real_spin (spin,
936 -spin->adjustment->step_increment);
939 spin->timer_step = spin->adjustment->step_increment;
940 spin->need_timer = TRUE;
941 spin->timer = gtk_timeout_add
942 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
943 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
946 else if (event->button == 2)
948 gtk_spin_button_real_spin (spin,
949 -spin->adjustment->page_increment);
952 spin->timer_step = spin->adjustment->page_increment;
953 spin->need_timer = TRUE;
954 spin->timer = gtk_timeout_add
955 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
956 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
959 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
964 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
970 gtk_spin_button_button_release (GtkWidget *widget,
971 GdkEventButton *event)
975 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
976 g_return_val_if_fail (event != NULL, FALSE);
978 spin = GTK_SPIN_BUTTON (widget);
980 if (event->button == spin->button)
986 gtk_timeout_remove (spin->timer);
988 spin->timer_calls = 0;
989 spin->need_timer = FALSE;
992 if (event->button == 3)
994 if (event->y >= 0 && event->x >= 0 &&
995 event->y <= widget->requisition.height &&
996 event->x <= ARROW_SIZE + 2 * widget->style->xthickness)
998 if (spin->click_child == GTK_ARROW_UP &&
999 event->y <= widget->requisition.height / 2)
1003 diff = spin->adjustment->upper - spin->adjustment->value;
1005 gtk_spin_button_real_spin (spin, diff);
1007 else if (spin->click_child == GTK_ARROW_DOWN &&
1008 event->y > widget->requisition.height / 2)
1012 diff = spin->adjustment->value - spin->adjustment->lower;
1014 gtk_spin_button_real_spin (spin, -diff);
1018 gtk_grab_remove (widget);
1019 click_child = spin->click_child;
1020 spin->click_child = 2;
1022 gtk_spin_button_draw_arrow (spin, click_child);
1026 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1032 gtk_spin_button_motion_notify (GtkWidget *widget,
1033 GdkEventMotion *event)
1035 GtkSpinButton *spin;
1037 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1038 g_return_val_if_fail (event != NULL, FALSE);
1040 spin = GTK_SPIN_BUTTON (widget);
1045 if (event->window == spin->panel)
1051 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1053 if (y <= widget->requisition.height / 2 &&
1054 spin->in_child == GTK_ARROW_DOWN)
1056 spin->in_child = GTK_ARROW_UP;
1057 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1058 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1060 else if (y > widget->requisition.height / 2 &&
1061 spin->in_child == GTK_ARROW_UP)
1063 spin->in_child = GTK_ARROW_DOWN;
1064 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1065 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1070 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1074 gtk_spin_button_timer (GtkSpinButton *spin_button)
1076 gboolean retval = FALSE;
1078 GDK_THREADS_ENTER ();
1080 if (spin_button->timer)
1082 if (spin_button->click_child == GTK_ARROW_UP)
1083 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1085 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1087 if (spin_button->need_timer)
1089 spin_button->need_timer = FALSE;
1090 spin_button->timer = gtk_timeout_add
1091 (SPIN_BUTTON_TIMER_DELAY, (GtkFunction) gtk_spin_button_timer,
1092 (gpointer) spin_button);
1096 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1097 < spin_button->adjustment->page_increment)
1099 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1100 spin_button->timer_calls++;
1103 spin_button->timer_calls = 0;
1104 spin_button->timer_step += spin_button->climb_rate;
1111 GDK_THREADS_LEAVE ();
1117 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1118 GtkSpinButton *spin_button)
1122 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1125 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1127 if (return_val == FALSE)
1128 gtk_spin_button_default_output (spin_button);
1130 gtk_signal_emit (GTK_OBJECT (spin_button),
1131 spinbutton_signals[VALUE_CHANGED]);
1133 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_UP);
1134 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_DOWN);
1136 g_object_notify (G_OBJECT (spin_button), "value");
1140 gtk_spin_button_key_press (GtkWidget *widget,
1143 GtkSpinButton *spin;
1145 gboolean key_repeat = FALSE;
1146 gboolean retval = FALSE;
1148 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1149 g_return_val_if_fail (event != NULL, FALSE);
1151 spin = GTK_SPIN_BUTTON (widget);
1152 key = event->keyval;
1154 key_repeat = (event->time == spin->ev_time);
1156 if (GTK_ENTRY (widget)->editable)
1163 if (GTK_WIDGET_HAS_FOCUS (widget))
1165 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1168 spin->timer_step = spin->adjustment->step_increment;
1170 gtk_spin_button_real_spin (spin, spin->timer_step);
1174 if (spin->climb_rate > 0.0 && spin->timer_step
1175 < spin->adjustment->page_increment)
1177 if (spin->timer_calls < MAX_TIMER_CALLS)
1178 spin->timer_calls++;
1181 spin->timer_calls = 0;
1182 spin->timer_step += spin->climb_rate;
1193 if (GTK_WIDGET_HAS_FOCUS (widget))
1195 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1198 spin->timer_step = spin->adjustment->step_increment;
1200 gtk_spin_button_real_spin (spin, -spin->timer_step);
1204 if (spin->climb_rate > 0.0 && spin->timer_step
1205 < spin->adjustment->page_increment)
1207 if (spin->timer_calls < MAX_TIMER_CALLS)
1208 spin->timer_calls++;
1211 spin->timer_calls = 0;
1212 spin->timer_step += spin->climb_rate;
1220 case GDK_KP_Page_Up:
1223 if (event->state & GDK_CONTROL_MASK)
1225 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1227 gtk_spin_button_real_spin (spin, diff);
1230 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1235 case GDK_KP_Page_Down:
1238 if (event->state & GDK_CONTROL_MASK)
1240 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1242 gtk_spin_button_real_spin (spin, -diff);
1245 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1257 gtk_spin_button_update (spin);
1261 return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
1265 gtk_spin_button_key_release (GtkWidget *widget,
1268 GtkSpinButton *spin;
1270 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1272 spin = GTK_SPIN_BUTTON (widget);
1274 spin->ev_time = event->time;
1279 gtk_spin_button_snap (GtkSpinButton *spin_button,
1285 inc = spin_button->adjustment->step_increment;
1286 tmp = (val - spin_button->adjustment->lower) / inc;
1287 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1288 val = spin_button->adjustment->lower + floor (tmp) * inc;
1290 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1292 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1293 gtk_adjustment_set_value (spin_button->adjustment, val);
1296 gint return_val = FALSE;
1297 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1299 if (return_val == FALSE)
1300 gtk_spin_button_default_output (spin_button);
1305 gtk_spin_button_activate (GtkEntry *entry)
1307 if (entry->editable)
1308 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1312 gtk_spin_button_insert_text (GtkEntry *entry,
1313 const gchar *new_text,
1314 gint new_text_length,
1317 GtkEditable *editable = GTK_EDITABLE (entry);
1318 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1330 entry_length = entry->text_length;
1334 if (*(lc->negative_sign))
1335 neg_sign = *(lc->negative_sign);
1339 if (*(lc->positive_sign))
1340 pos_sign = *(lc->positive_sign);
1344 for (sign=0, i=0; i<entry_length; i++)
1345 if ((entry->text[i] == neg_sign) ||
1346 (entry->text[i] == pos_sign))
1352 if (sign && !(*position))
1355 for (dotpos=-1, i=0; i<entry_length; i++)
1356 if (entry->text[i] == *(lc->decimal_point))
1362 if (dotpos > -1 && *position > dotpos &&
1363 (gint)spin->digits - entry_length
1364 + dotpos - new_text_length + 1 < 0)
1367 for (i = 0; i < new_text_length; i++)
1369 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1371 if (sign || (*position) || i)
1375 else if (new_text[i] == *(lc->decimal_point))
1377 if (!spin->digits || dotpos > -1 ||
1378 (new_text_length - 1 - i + entry_length
1379 - *position > (gint)spin->digits))
1381 dotpos = *position + i;
1383 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1388 GTK_ENTRY_CLASS (parent_class)->insert_text (entry, new_text,
1389 new_text_length, position);
1393 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1397 gdouble new_value = 0.0;
1399 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1401 adj = spin_button->adjustment;
1403 new_value = adj->value + increment;
1407 if (spin_button->wrap)
1409 if (fabs (adj->value - adj->upper) < EPSILON)
1410 new_value = adj->lower;
1411 else if (new_value > adj->upper)
1412 new_value = adj->upper;
1415 new_value = MIN (new_value, adj->upper);
1417 else if (increment < 0)
1419 if (spin_button->wrap)
1421 if (fabs (adj->value - adj->lower) < EPSILON)
1422 new_value = adj->upper;
1423 else if (new_value < adj->lower)
1424 new_value = adj->lower;
1427 new_value = MAX (new_value, adj->lower);
1430 if (fabs (new_value - adj->value) > EPSILON)
1431 gtk_adjustment_set_value (adj, new_value);
1435 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1440 *new_val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1442 return GTK_INPUT_ERROR;
1448 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1450 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1452 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1453 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1459 /***********************************************************
1460 ***********************************************************
1461 *** Public interface ***
1462 ***********************************************************
1463 ***********************************************************/
1467 gtk_spin_button_configure (GtkSpinButton *spin_button,
1468 GtkAdjustment *adjustment,
1472 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1475 gtk_spin_button_set_adjustment (spin_button, adjustment);
1477 adjustment = spin_button->adjustment;
1479 g_object_freeze_notify (G_OBJECT (spin_button));
1480 if (spin_button->digits != digits)
1482 spin_button->digits = digits;
1483 g_object_notify (G_OBJECT (spin_button), "digits");
1486 if (spin_button->climb_rate != climb_rate)
1488 spin_button->climb_rate = climb_rate;
1489 g_object_notify (G_OBJECT (spin_button), "climb_rate");
1491 g_object_thaw_notify (G_OBJECT (spin_button));
1493 gtk_adjustment_value_changed (adjustment);
1497 gtk_spin_button_new (GtkAdjustment *adjustment,
1501 GtkSpinButton *spin;
1504 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1506 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1508 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1510 return GTK_WIDGET (spin);
1514 * gtk_spin_button_new_with_range:
1515 * @min: Minimum allowable value
1516 * @max: Maximum allowable value
1517 * @step: Increment added or subtracted by spinning the widget
1519 * This is a convenience constructor that allows creation of a numeric
1520 * #GtkSpinButton without manually creating an adjustment. The value is
1521 * initially set to the minimum value and a page increment of 10 * @step
1522 * is the default. The precision of the spin button is equivalent to the
1523 * precision of @step.
1525 * Return value: the newly instantiated spin button
1528 gtk_spin_button_new_with_range (gdouble min,
1533 GtkSpinButton *spin;
1536 g_return_val_if_fail (min < max, NULL);
1537 g_return_val_if_fail (step != 0.0, NULL);
1539 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1541 adj = gtk_adjustment_new (min, min, max, step, 10 * step, step);
1543 if (fabs (step) >= 1.0 || step == 0.0)
1546 digits = abs ((gint) floor (log10 (fabs (step))));
1547 if (digits > MAX_DIGITS)
1548 digits = MAX_DIGITS;
1551 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1553 gtk_spin_button_set_numeric (spin, TRUE);
1555 return GTK_WIDGET (spin);
1558 /* Callback used when the spin button's adjustment changes. We need to redraw
1559 * the arrows when the adjustment's range changes, and reevaluate our size request.
1562 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1564 GtkSpinButton *spin_button;
1566 spin_button = GTK_SPIN_BUTTON (data);
1568 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1572 * gtk_spin_button_set_adjustment:
1573 * @spin_button: a #GtkSpinButton
1574 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1576 * Replaces the #GtkAdjustment associated with @spin_button.
1579 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1580 GtkAdjustment *adjustment)
1582 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1584 if (spin_button->adjustment != adjustment)
1586 if (spin_button->adjustment)
1588 gtk_signal_disconnect_by_data (GTK_OBJECT (spin_button->adjustment),
1589 (gpointer) spin_button);
1590 gtk_object_unref (GTK_OBJECT (spin_button->adjustment));
1592 spin_button->adjustment = adjustment;
1595 gtk_object_ref (GTK_OBJECT (adjustment));
1596 gtk_object_sink (GTK_OBJECT (adjustment));
1597 gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
1598 (GtkSignalFunc) gtk_spin_button_value_changed,
1599 (gpointer) spin_button);
1600 gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
1601 (GtkSignalFunc) adjustment_changed_cb,
1602 (gpointer) spin_button);
1605 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1608 g_object_notify (G_OBJECT (spin_button), "adjustment");
1612 * gtk_spin_button_get_adjustment:
1615 * Get the adjustment associated with a #GtkSpinButton
1617 * Return value: the #GtkAdjustment of @spin_button
1620 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1622 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1624 return spin_button->adjustment;
1628 * gtk_spin_button_set_digits:
1629 * @spin_button: a #GtkSpinButton
1630 * @digits: the number of digits to be displayed for the spin button's value
1632 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1636 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1639 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1641 if (spin_button->digits != digits)
1643 spin_button->digits = digits;
1644 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1645 g_object_notify (G_OBJECT (spin_button), "digits");
1647 /* since lower/upper may have changed */
1648 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1653 * gtk_spin_button_get_digits:
1654 * @spin_button: a #GtkSpinButton
1656 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1658 * Returns: the current precision
1661 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1663 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1665 return spin_button->digits;
1669 * gtk_spin_button_set_increments:
1670 * @spin_button: a #GtkSpinButton
1671 * @step: increment applied for a button 1 press.
1672 * @page: increment applied for a button 2 press.
1674 * Sets the step and page increments for spin_button. This affects how
1675 * quickly the value changes when the spin button's arrows are activated.
1678 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1682 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1684 spin_button->adjustment->step_increment = step;
1685 spin_button->adjustment->page_increment = page;
1689 * gtk_spin_button_get_increments:
1690 * @spin_button: a #GtkSpinButton
1691 * @step: location to store step increment, or %NULL
1692 * @page: location to store page increment, or %NULL
1694 * Gets the current step and page the increments used by @spin_button. See
1695 * gtk_spin_button_set_increments().
1698 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1702 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1705 *step = spin_button->adjustment->step_increment;
1707 *page = spin_button->adjustment->page_increment;
1711 * gtk_spin_button_set_range:
1712 * @spin_button: a #GtkSpinButton
1713 * @min: minimum allowable value
1714 * @max: maximum allowable value
1716 * Sets the minimum and maximum allowable values for @spin_button
1719 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1725 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1727 spin_button->adjustment->lower = min;
1728 spin_button->adjustment->upper = max;
1730 value = CLAMP (spin_button->adjustment->value,
1731 spin_button->adjustment->lower,
1732 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1734 if (value != spin_button->adjustment->value)
1735 gtk_spin_button_set_value (spin_button, value);
1737 gtk_adjustment_changed (spin_button->adjustment);
1741 * gtk_spin_button_get_range:
1742 * @spin_button: a #GtkSpinButton
1743 * @min: location to store minimum allowed value, or %NULL
1744 * @max: location to store maximum allowed value, or %NULL
1746 * Gets the range allowed for @spin_button. See
1747 * gtk_spin_button_set_range().
1750 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1754 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1757 *min = spin_button->adjustment->lower;
1759 *max = spin_button->adjustment->upper;
1763 * gtk_spin_button_get_value:
1764 * @spin_button: a #GtkSpinButton
1766 * Get the value in the @spin_button.
1768 * Return value: the value of @spin_button
1771 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1773 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1775 return spin_button->adjustment->value;
1779 * gtk_spin_button_get_value_as_int:
1780 * @spin_button: a #GtkSpinButton
1782 * Get the value @spin_button represented as an integer.
1784 * Return value: the value of @spin_button
1787 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1791 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1793 val = spin_button->adjustment->value;
1794 if (val - floor (val) < ceil (val) - val)
1801 * gtk_spin_button_set_value:
1802 * @spin_button: a #GtkSpinButton
1803 * @value: the new value
1805 * Set the value of @spin_button.
1808 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1811 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1813 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1814 gtk_adjustment_set_value (spin_button->adjustment, value);
1817 gint return_val = FALSE;
1818 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1820 if (return_val == FALSE)
1821 gtk_spin_button_default_output (spin_button);
1826 * gtk_spin_button_set_update_policy:
1827 * @spin_button: a #GtkSpinButton
1828 * @policy: a #GtkSpinButtonUpdatePolicy value
1830 * Sets the update behavior of a spin button. This determines whether the
1831 * spin button is always updated or only when a valid value is set.
1834 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1835 GtkSpinButtonUpdatePolicy policy)
1837 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1839 if (spin_button->update_policy != policy)
1841 spin_button->update_policy = policy;
1842 g_object_notify (G_OBJECT (spin_button), "update_policy");
1847 * gtk_spin_button_get_update_policy:
1848 * @spin_button: a #GtkSpinButton
1850 * Gets the update behavior of a spin button. See
1851 * gtk_spin_button_set_update_policy().
1853 * Return value: the current update policy
1855 GtkSpinButtonUpdatePolicy
1856 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1858 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1860 return spin_button->update_policy;
1864 * gtk_spin_button_set_numeric:
1865 * @spin_button: a #GtkSpinButton
1866 * @numeric: flag indicating if only numeric entry is allowed.
1868 * Sets the flag that determines if non-numeric text can be typed into
1872 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1875 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1877 spin_button->numeric = (numeric != 0);
1879 g_object_notify (G_OBJECT (spin_button), "numeric");
1883 * gtk_spin_button_get_numeric:
1884 * @spin_button: a #GtkSpinButton
1886 * Returns whether non-numeric text can be typed into the spin button.
1887 * See gtk_spin_button_set_numeric().
1889 * Return value: %TRUE if only numeric text can be entered
1892 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1894 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1896 return spin_button->numeric;
1900 * gtk_spin_button_set_wrap:
1901 * @spin_button: a #GtkSpinButton
1902 * @wrap: a flag indicating if wrapping behavior is performed.
1904 * Sets the flag that determines if a spin button value wraps around to the
1905 * opposite limit when the upper or lower limit of the range is exceeded.
1908 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1911 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1913 spin_button->wrap = (wrap != 0);
1915 g_object_notify (G_OBJECT (spin_button), "wrap");
1919 * gtk_spin_button_get_wrap:
1920 * @spin_button: a #GtkSpinButton
1922 * Returns whether the spin button's value wraps around to the
1923 * opposite limit when the upper or lower limit of the range is
1924 * exceeded. See gtk_spin_button_set_wrap().
1926 * Return value: %TRUE if the spin button wraps around
1929 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
1931 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1933 return spin_button->wrap;
1937 * spin_button_get_shadow_type:
1938 * @spin_button: a #GtkSpinButton
1940 * Convenience function to Get the shadow type from the underlying widget's
1943 * Return value: the #GtkShadowType
1946 spin_button_get_shadow_type (GtkSpinButton *spin_button)
1948 GtkShadowType rc_shadow_type;
1950 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
1952 return rc_shadow_type;
1956 * gtk_spin_button_set_snap_to_ticks:
1957 * @spin_button: a #GtkSpinButton
1958 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
1960 * Sets the policy as to whether values are corrected to the nearest step
1961 * increment when a spin button is activated after providing an invalid value.
1964 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
1965 gboolean snap_to_ticks)
1969 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1971 new_val = (snap_to_ticks != 0);
1973 if (new_val != spin_button->snap_to_ticks)
1975 spin_button->snap_to_ticks = new_val;
1976 if (new_val && GTK_ENTRY (spin_button)->editable)
1977 gtk_spin_button_update (spin_button);
1979 g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
1984 * gtk_spin_button_get_snap_to_ticks:
1985 * @spin_button: a #GtkSpinButton
1987 * Returns whether the values are corrected to the nearest step. See
1988 * gtk_spin_button_set_snap_to_ticks().
1990 * Return value: %TRUE if values are snapped to the nearest step.
1993 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
1995 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1997 return spin_button->snap_to_ticks;
2001 * gtk_spin_button_spin:
2002 * @spin_button: a #GtkSpinButton
2003 * @direction: a #GtkSpinType indicating the direction to spin.
2004 * @increment: step increment to apply in the specified direction.
2006 * Increment or decrement a spin button's value in a specified direction
2007 * by a specified amount.
2010 gtk_spin_button_spin (GtkSpinButton *spin_button,
2011 GtkSpinType direction,
2017 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2019 adj = spin_button->adjustment;
2021 /* for compatibility with the 1.0.x version of this function */
2022 if (increment != 0 && increment != adj->step_increment &&
2023 (direction == GTK_SPIN_STEP_FORWARD ||
2024 direction == GTK_SPIN_STEP_BACKWARD))
2026 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2027 increment = -increment;
2028 direction = GTK_SPIN_USER_DEFINED;
2033 case GTK_SPIN_STEP_FORWARD:
2035 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2038 case GTK_SPIN_STEP_BACKWARD:
2040 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2043 case GTK_SPIN_PAGE_FORWARD:
2045 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2048 case GTK_SPIN_PAGE_BACKWARD:
2050 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2055 diff = adj->value - adj->lower;
2057 gtk_spin_button_real_spin (spin_button, -diff);
2062 diff = adj->upper - adj->value;
2064 gtk_spin_button_real_spin (spin_button, diff);
2067 case GTK_SPIN_USER_DEFINED:
2070 gtk_spin_button_real_spin (spin_button, increment);
2079 * gtk_spin_button_update:
2080 * @spin_button: a #GtkSpinButton
2082 * Manually force an update of the spin button.
2085 gtk_spin_button_update (GtkSpinButton *spin_button)
2091 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2094 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[INPUT],
2096 if (return_val == FALSE)
2098 return_val = gtk_spin_button_default_input (spin_button, &val);
2099 error = (return_val == GTK_INPUT_ERROR);
2101 else if (return_val == GTK_INPUT_ERROR)
2104 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2106 if (val < spin_button->adjustment->lower)
2107 val = spin_button->adjustment->lower;
2108 else if (val > spin_button->adjustment->upper)
2109 val = spin_button->adjustment->upper;
2111 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2113 val < spin_button->adjustment->lower ||
2114 val > spin_button->adjustment->upper))
2116 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2120 if (spin_button->snap_to_ticks)
2121 gtk_spin_button_snap (spin_button, val);
2124 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2125 gtk_adjustment_set_value (spin_button->adjustment, val);
2129 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
2131 if (return_val == FALSE)
2132 gtk_spin_button_default_output (spin_button);