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 guint 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 (widget != NULL);
434 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
436 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
438 GTK_WIDGET_CLASS (parent_class)->map (widget);
439 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
444 gtk_spin_button_unmap (GtkWidget *widget)
446 g_return_if_fail (widget != NULL);
447 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
449 if (GTK_WIDGET_MAPPED (widget))
451 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
452 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
457 gtk_spin_button_realize (GtkWidget *widget)
459 GtkSpinButton *spin_button;
460 GdkWindowAttr attributes;
461 gint attributes_mask;
465 g_return_if_fail (widget != NULL);
466 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
468 spin_button = GTK_SPIN_BUTTON (widget);
470 real_width = widget->allocation.width;
471 widget->allocation.width -= ARROW_SIZE + 2 * widget->style->xthickness;
472 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
473 GDK_KEY_RELEASE_MASK);
474 GTK_WIDGET_CLASS (parent_class)->realize (widget);
476 widget->allocation.width = real_width;
478 attributes.window_type = GDK_WINDOW_CHILD;
479 attributes.wclass = GDK_INPUT_OUTPUT;
480 attributes.visual = gtk_widget_get_visual (widget);
481 attributes.colormap = gtk_widget_get_colormap (widget);
482 attributes.event_mask = gtk_widget_get_events (widget);
483 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
484 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
485 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
487 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
489 attributes.x = (widget->allocation.x + widget->allocation.width - ARROW_SIZE -
490 2 * widget->style->xthickness);
491 attributes.y = widget->allocation.y + (widget->allocation.height -
492 widget->requisition.height) / 2;
493 attributes.width = ARROW_SIZE + 2 * widget->style->xthickness;
494 attributes.height = widget->requisition.height;
496 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
497 &attributes, attributes_mask);
498 gdk_window_set_user_data (spin_button->panel, widget);
500 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
503 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
505 if (return_val == FALSE)
506 gtk_spin_button_default_output (spin_button);
510 gtk_spin_button_unrealize (GtkWidget *widget)
514 g_return_if_fail (widget != NULL);
515 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
517 spin = GTK_SPIN_BUTTON (widget);
519 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
523 gdk_window_set_user_data (spin->panel, NULL);
524 gdk_window_destroy (spin->panel);
530 compute_double_length (double val, int digits)
536 if (fabs (val) > 1.0)
537 a = floor (log10 (fabs (val))) + 1;
549 return a + digits + extra;
553 gtk_spin_button_size_request (GtkWidget *widget,
554 GtkRequisition *requisition)
557 GtkSpinButton *spin_button;
559 g_return_if_fail (widget != NULL);
560 g_return_if_fail (requisition != NULL);
561 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
563 entry = GTK_ENTRY (widget);
564 spin_button = GTK_SPIN_BUTTON (widget);
566 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
568 if (entry->width_chars < 0)
570 PangoContext *context;
571 PangoFontMetrics metrics;
576 context = gtk_widget_get_pango_context (widget);
577 pango_context_get_metrics (context,
578 widget->style->font_desc,
579 pango_context_get_language (context),
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) * PANGO_PIXELS (metrics.approximate_digit_width);
589 width = MAX (width, w);
590 string_len = compute_double_length (spin_button->adjustment->lower,
591 spin_button->adjustment->step_increment);
592 w = MIN (string_len, 10) * PANGO_PIXELS (metrics.approximate_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 (widget != NULL);
608 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
609 g_return_if_fail (allocation != NULL);
611 child_allocation = *allocation;
612 if (child_allocation.width > ARROW_SIZE + 2 * widget->style->xthickness)
613 child_allocation.width -= ARROW_SIZE + 2 * widget->style->xthickness;
615 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
616 child_allocation.x += ARROW_SIZE + 2 * widget->style->xthickness;
618 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &child_allocation);
620 widget->allocation = *allocation;
622 if (GTK_WIDGET_REALIZED (widget))
624 child_allocation.width = ARROW_SIZE + 2 * widget->style->xthickness;
625 child_allocation.height = widget->requisition.height;
627 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
628 child_allocation.x = (allocation->x + allocation->width - ARROW_SIZE -
629 2 * widget->style->xthickness);
631 child_allocation.x = allocation->x;
633 child_allocation.y = allocation->y + (allocation->height - widget->requisition.height) / 2;
635 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
638 child_allocation.width,
639 child_allocation.height);
644 gtk_spin_button_expose (GtkWidget *widget,
645 GdkEventExpose *event)
649 g_return_val_if_fail (widget != NULL, FALSE);
650 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
651 g_return_val_if_fail (event != NULL, FALSE);
653 spin = GTK_SPIN_BUTTON (widget);
655 if (GTK_WIDGET_DRAWABLE (widget))
657 GtkShadowType shadow_type;
659 /* FIXME this seems like really broken code -
660 * why aren't we looking at event->window
661 * and acting accordingly?
664 shadow_type = spin_button_get_shadow_type (spin);
665 if (shadow_type != GTK_SHADOW_NONE)
666 gtk_paint_box (widget->style, spin->panel,
667 GTK_STATE_NORMAL, shadow_type,
668 &event->area, widget, "spinbutton",
670 ARROW_SIZE + 2 * widget->style->xthickness,
671 widget->requisition.height);
674 gdk_window_set_back_pixmap (spin->panel, NULL, TRUE);
675 gdk_window_clear_area (spin->panel,
676 event->area.x, event->area.y,
677 event->area.width, event->area.height);
679 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
680 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
682 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
689 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
692 GtkShadowType spin_shadow_type;
693 GtkStateType state_type;
694 GtkShadowType shadow_type;
699 g_return_if_fail (spin_button != NULL);
700 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
702 widget = GTK_WIDGET (spin_button);
703 spin_shadow_type = spin_button_get_shadow_type (spin_button);
705 if (GTK_WIDGET_DRAWABLE (spin_button))
707 if (!spin_button->wrap &&
708 (((arrow == GTK_ARROW_UP &&
709 (spin_button->adjustment->upper - spin_button->adjustment->value
711 ((arrow == GTK_ARROW_DOWN &&
712 (spin_button->adjustment->value - spin_button->adjustment->lower
715 shadow_type = GTK_SHADOW_ETCHED_IN;
716 state_type = GTK_STATE_NORMAL;
720 if (spin_button->in_child == arrow)
722 if (spin_button->click_child == arrow)
723 state_type = GTK_STATE_ACTIVE;
725 state_type = GTK_STATE_PRELIGHT;
728 state_type = GTK_STATE_NORMAL;
730 if (spin_button->click_child == arrow)
731 shadow_type = GTK_SHADOW_IN;
733 shadow_type = GTK_SHADOW_OUT;
735 if (arrow == GTK_ARROW_UP)
737 if (spin_shadow_type != GTK_SHADOW_NONE)
739 x = widget->style->xthickness;
740 y = widget->style->ythickness;
744 x = widget->style->xthickness - 1;
745 y = widget->style->ythickness - 1;
747 gtk_paint_arrow (widget->style, spin_button->panel,
748 state_type, shadow_type,
749 NULL, widget, "spinbutton",
751 x, y, ARROW_SIZE, widget->requisition.height / 2
752 - widget->style->ythickness);
756 if (spin_shadow_type != GTK_SHADOW_NONE)
758 x = widget->style->xthickness;
759 y = widget->requisition.height / 2;
763 x = widget->style->xthickness - 1;
764 y = widget->requisition.height / 2 + 1;
766 gtk_paint_arrow (widget->style, spin_button->panel,
767 state_type, shadow_type,
768 NULL, widget, "spinbutton",
770 x, y, ARROW_SIZE, widget->requisition.height / 2
771 - widget->style->ythickness);
777 gtk_spin_button_enter_notify (GtkWidget *widget,
778 GdkEventCrossing *event)
782 g_return_val_if_fail (widget != NULL, FALSE);
783 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
784 g_return_val_if_fail (event != NULL, FALSE);
786 spin = GTK_SPIN_BUTTON (widget);
788 if (event->window == spin->panel)
793 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
795 if (y <= widget->requisition.height / 2)
797 spin->in_child = GTK_ARROW_UP;
798 if (spin->click_child == 2)
799 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
803 spin->in_child = GTK_ARROW_DOWN;
804 if (spin->click_child == 2)
805 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
812 gtk_spin_button_leave_notify (GtkWidget *widget,
813 GdkEventCrossing *event)
817 g_return_val_if_fail (widget != NULL, FALSE);
818 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
819 g_return_val_if_fail (event != NULL, FALSE);
821 spin = GTK_SPIN_BUTTON (widget);
823 if (event->window == spin->panel && spin->click_child == 2)
825 if (spin->in_child == GTK_ARROW_UP)
828 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
833 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
840 gtk_spin_button_focus_out (GtkWidget *widget,
841 GdkEventFocus *event)
843 g_return_val_if_fail (widget != NULL, FALSE);
844 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
845 g_return_val_if_fail (event != NULL, FALSE);
847 if (GTK_ENTRY (widget)->editable)
848 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
850 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
854 gtk_spin_button_scroll (GtkWidget *widget,
855 GdkEventScroll *event)
859 g_return_val_if_fail (widget != NULL, FALSE);
860 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
861 g_return_val_if_fail (event != NULL, FALSE);
863 spin = GTK_SPIN_BUTTON (widget);
865 if (event->direction == GDK_SCROLL_UP)
867 if (!GTK_WIDGET_HAS_FOCUS (widget))
868 gtk_widget_grab_focus (widget);
869 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
871 else if (event->direction == GDK_SCROLL_DOWN)
873 if (!GTK_WIDGET_HAS_FOCUS (widget))
874 gtk_widget_grab_focus (widget);
875 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
884 gtk_spin_button_button_press (GtkWidget *widget,
885 GdkEventButton *event)
889 g_return_val_if_fail (widget != NULL, FALSE);
890 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
891 g_return_val_if_fail (event != NULL, FALSE);
893 spin = GTK_SPIN_BUTTON (widget);
897 if (event->window == spin->panel)
899 if (!GTK_WIDGET_HAS_FOCUS (widget))
900 gtk_widget_grab_focus (widget);
901 gtk_grab_add (widget);
902 spin->button = event->button;
904 if (GTK_ENTRY (widget)->editable)
905 gtk_spin_button_update (spin);
907 if (event->y <= widget->requisition.height / 2)
909 spin->click_child = GTK_ARROW_UP;
910 if (event->button == 1)
912 gtk_spin_button_real_spin (spin,
913 spin->adjustment->step_increment);
916 spin->timer_step = spin->adjustment->step_increment;
917 spin->need_timer = TRUE;
918 spin->timer = gtk_timeout_add
919 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
920 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
923 else if (event->button == 2)
925 gtk_spin_button_real_spin (spin,
926 spin->adjustment->page_increment);
929 spin->timer_step = spin->adjustment->page_increment;
930 spin->need_timer = TRUE;
931 spin->timer = gtk_timeout_add
932 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
933 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
936 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
940 spin->click_child = GTK_ARROW_DOWN;
941 if (event->button == 1)
943 gtk_spin_button_real_spin (spin,
944 -spin->adjustment->step_increment);
947 spin->timer_step = spin->adjustment->step_increment;
948 spin->need_timer = TRUE;
949 spin->timer = gtk_timeout_add
950 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
951 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
954 else if (event->button == 2)
956 gtk_spin_button_real_spin (spin,
957 -spin->adjustment->page_increment);
960 spin->timer_step = spin->adjustment->page_increment;
961 spin->need_timer = TRUE;
962 spin->timer = gtk_timeout_add
963 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
964 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
967 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
972 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
978 gtk_spin_button_button_release (GtkWidget *widget,
979 GdkEventButton *event)
983 g_return_val_if_fail (widget != NULL, FALSE);
984 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
985 g_return_val_if_fail (event != NULL, FALSE);
987 spin = GTK_SPIN_BUTTON (widget);
989 if (event->button == spin->button)
995 gtk_timeout_remove (spin->timer);
997 spin->timer_calls = 0;
998 spin->need_timer = FALSE;
1001 if (event->button == 3)
1003 if (event->y >= 0 && event->x >= 0 &&
1004 event->y <= widget->requisition.height &&
1005 event->x <= ARROW_SIZE + 2 * widget->style->xthickness)
1007 if (spin->click_child == GTK_ARROW_UP &&
1008 event->y <= widget->requisition.height / 2)
1012 diff = spin->adjustment->upper - spin->adjustment->value;
1014 gtk_spin_button_real_spin (spin, diff);
1016 else if (spin->click_child == GTK_ARROW_DOWN &&
1017 event->y > widget->requisition.height / 2)
1021 diff = spin->adjustment->value - spin->adjustment->lower;
1023 gtk_spin_button_real_spin (spin, -diff);
1027 gtk_grab_remove (widget);
1028 click_child = spin->click_child;
1029 spin->click_child = 2;
1031 gtk_spin_button_draw_arrow (spin, click_child);
1035 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1041 gtk_spin_button_motion_notify (GtkWidget *widget,
1042 GdkEventMotion *event)
1044 GtkSpinButton *spin;
1046 g_return_val_if_fail (widget != NULL, FALSE);
1047 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1048 g_return_val_if_fail (event != NULL, FALSE);
1050 spin = GTK_SPIN_BUTTON (widget);
1055 if (event->window == spin->panel)
1061 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1063 if (y <= widget->requisition.height / 2 &&
1064 spin->in_child == GTK_ARROW_DOWN)
1066 spin->in_child = GTK_ARROW_UP;
1067 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1068 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1070 else if (y > widget->requisition.height / 2 &&
1071 spin->in_child == GTK_ARROW_UP)
1073 spin->in_child = GTK_ARROW_DOWN;
1074 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1075 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1080 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1084 gtk_spin_button_timer (GtkSpinButton *spin_button)
1086 gboolean retval = FALSE;
1088 GDK_THREADS_ENTER ();
1090 if (spin_button->timer)
1092 if (spin_button->click_child == GTK_ARROW_UP)
1093 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1095 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1097 if (spin_button->need_timer)
1099 spin_button->need_timer = FALSE;
1100 spin_button->timer = gtk_timeout_add
1101 (SPIN_BUTTON_TIMER_DELAY, (GtkFunction) gtk_spin_button_timer,
1102 (gpointer) spin_button);
1106 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1107 < spin_button->adjustment->page_increment)
1109 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1110 spin_button->timer_calls++;
1113 spin_button->timer_calls = 0;
1114 spin_button->timer_step += spin_button->climb_rate;
1121 GDK_THREADS_LEAVE ();
1127 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1128 GtkSpinButton *spin_button)
1132 g_return_if_fail (adjustment != NULL);
1133 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1136 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1138 if (return_val == FALSE)
1139 gtk_spin_button_default_output (spin_button);
1141 gtk_signal_emit (GTK_OBJECT (spin_button),
1142 spinbutton_signals[VALUE_CHANGED]);
1144 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_UP);
1145 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_DOWN);
1147 g_object_notify (G_OBJECT (spin_button), "value");
1151 gtk_spin_button_key_press (GtkWidget *widget,
1154 GtkSpinButton *spin;
1156 gboolean key_repeat = FALSE;
1157 gboolean retval = FALSE;
1159 g_return_val_if_fail (widget != NULL, FALSE);
1160 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1161 g_return_val_if_fail (event != NULL, FALSE);
1163 spin = GTK_SPIN_BUTTON (widget);
1164 key = event->keyval;
1166 key_repeat = (event->time == spin->ev_time);
1168 if (GTK_ENTRY (widget)->editable)
1175 if (GTK_WIDGET_HAS_FOCUS (widget))
1177 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1180 spin->timer_step = spin->adjustment->step_increment;
1182 gtk_spin_button_real_spin (spin, spin->timer_step);
1186 if (spin->climb_rate > 0.0 && spin->timer_step
1187 < spin->adjustment->page_increment)
1189 if (spin->timer_calls < MAX_TIMER_CALLS)
1190 spin->timer_calls++;
1193 spin->timer_calls = 0;
1194 spin->timer_step += spin->climb_rate;
1205 if (GTK_WIDGET_HAS_FOCUS (widget))
1207 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1210 spin->timer_step = spin->adjustment->step_increment;
1212 gtk_spin_button_real_spin (spin, -spin->timer_step);
1216 if (spin->climb_rate > 0.0 && spin->timer_step
1217 < spin->adjustment->page_increment)
1219 if (spin->timer_calls < MAX_TIMER_CALLS)
1220 spin->timer_calls++;
1223 spin->timer_calls = 0;
1224 spin->timer_step += spin->climb_rate;
1232 case GDK_KP_Page_Up:
1235 if (event->state & GDK_CONTROL_MASK)
1237 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1239 gtk_spin_button_real_spin (spin, diff);
1242 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1247 case GDK_KP_Page_Down:
1250 if (event->state & GDK_CONTROL_MASK)
1252 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1254 gtk_spin_button_real_spin (spin, -diff);
1257 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1269 gtk_spin_button_update (spin);
1273 return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
1277 gtk_spin_button_key_release (GtkWidget *widget,
1280 GtkSpinButton *spin;
1282 g_return_val_if_fail (widget != NULL, FALSE);
1283 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1285 spin = GTK_SPIN_BUTTON (widget);
1287 spin->ev_time = event->time;
1292 gtk_spin_button_snap (GtkSpinButton *spin_button,
1298 inc = spin_button->adjustment->step_increment;
1299 tmp = (val - spin_button->adjustment->lower) / inc;
1300 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1301 val = spin_button->adjustment->lower + floor (tmp) * inc;
1303 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1305 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1306 gtk_adjustment_set_value (spin_button->adjustment, val);
1309 gint return_val = FALSE;
1310 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1312 if (return_val == FALSE)
1313 gtk_spin_button_default_output (spin_button);
1318 gtk_spin_button_activate (GtkEntry *entry)
1320 if (entry->editable)
1321 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1325 gtk_spin_button_insert_text (GtkEntry *entry,
1326 const gchar *new_text,
1327 gint new_text_length,
1330 GtkEditable *editable = GTK_EDITABLE (entry);
1331 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1343 entry_length = entry->text_length;
1347 if (*(lc->negative_sign))
1348 neg_sign = *(lc->negative_sign);
1352 if (*(lc->positive_sign))
1353 pos_sign = *(lc->positive_sign);
1357 for (sign=0, i=0; i<entry_length; i++)
1358 if ((entry->text[i] == neg_sign) ||
1359 (entry->text[i] == pos_sign))
1365 if (sign && !(*position))
1368 for (dotpos=-1, i=0; i<entry_length; i++)
1369 if (entry->text[i] == *(lc->decimal_point))
1375 if (dotpos > -1 && *position > dotpos &&
1376 (gint)spin->digits - entry_length
1377 + dotpos - new_text_length + 1 < 0)
1380 for (i = 0; i < new_text_length; i++)
1382 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1384 if (sign || (*position) || i)
1388 else if (new_text[i] == *(lc->decimal_point))
1390 if (!spin->digits || dotpos > -1 ||
1391 (new_text_length - 1 - i + entry_length
1392 - *position > (gint)spin->digits))
1394 dotpos = *position + i;
1396 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1401 GTK_ENTRY_CLASS (parent_class)->insert_text (entry, new_text,
1402 new_text_length, position);
1406 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1410 gdouble new_value = 0.0;
1412 g_return_if_fail (spin_button != NULL);
1413 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1415 adj = spin_button->adjustment;
1417 new_value = adj->value + increment;
1421 if (spin_button->wrap)
1423 if (fabs (adj->value - adj->upper) < EPSILON)
1424 new_value = adj->lower;
1425 else if (new_value > adj->upper)
1426 new_value = adj->upper;
1429 new_value = MIN (new_value, adj->upper);
1431 else if (increment < 0)
1433 if (spin_button->wrap)
1435 if (fabs (adj->value - adj->lower) < EPSILON)
1436 new_value = adj->upper;
1437 else if (new_value < adj->lower)
1438 new_value = adj->lower;
1441 new_value = MAX (new_value, adj->lower);
1444 if (fabs (new_value - adj->value) > EPSILON)
1445 gtk_adjustment_set_value (adj, new_value);
1449 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1454 *new_val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1456 return GTK_INPUT_ERROR;
1462 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1464 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1466 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1467 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1473 /***********************************************************
1474 ***********************************************************
1475 *** Public interface ***
1476 ***********************************************************
1477 ***********************************************************/
1481 gtk_spin_button_configure (GtkSpinButton *spin_button,
1482 GtkAdjustment *adjustment,
1486 g_return_if_fail (spin_button != NULL);
1487 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1490 gtk_spin_button_set_adjustment (spin_button, adjustment);
1492 adjustment = spin_button->adjustment;
1494 if (spin_button->digits != digits)
1496 spin_button->digits = digits;
1497 g_object_notify (G_OBJECT (spin_button), "digits");
1500 if (spin_button->climb_rate != climb_rate)
1502 spin_button->climb_rate = climb_rate;
1503 g_object_notify (G_OBJECT (spin_button), "climb_rate");
1506 gtk_adjustment_value_changed (adjustment);
1510 gtk_spin_button_new (GtkAdjustment *adjustment,
1514 GtkSpinButton *spin;
1517 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1519 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1521 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1523 return GTK_WIDGET (spin);
1527 * gtk_spin_button_new_with_range:
1528 * @min: Minimum allowable value
1529 * @max: Maximum allowable value
1530 * @step: Increment added or subtracted by spinning the widget
1532 * This is a convenience constructor that allows creation of a numeric
1533 * #GtkSpinButton without manually creating an adjustment. The value is
1534 * initially set to the minimum value and a page increment of 10 * @step
1535 * is the default. The precision of the spin button is equivalent to the
1536 * precision of @step.
1538 * Return value: the newly instantiated spin button
1541 gtk_spin_button_new_with_range (gdouble min,
1546 GtkSpinButton *spin;
1549 g_return_val_if_fail (min < max, NULL);
1550 g_return_val_if_fail (step != 0.0, NULL);
1552 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1554 adj = gtk_adjustment_new (min, min, max, step, 10 * step, step);
1556 if (fabs (step) >= 1.0 || step == 0.0)
1559 digits = abs ((gint) floor (log10 (fabs (step))));
1560 if (digits > MAX_DIGITS)
1561 digits = MAX_DIGITS;
1564 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1566 gtk_spin_button_set_numeric (spin, TRUE);
1568 return GTK_WIDGET (spin);
1571 /* Callback used when the spin button's adjustment changes. We need to redraw
1572 * the arrows when the adjustment's range changes, and reevaluate our size request.
1575 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1577 GtkSpinButton *spin_button;
1579 spin_button = GTK_SPIN_BUTTON (data);
1581 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1585 * gtk_spin_button_set_adjustment:
1586 * @spin_button: a #GtkSpinButton
1587 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1589 * Replaces the #GtkAdjustment associated with @spin_button.
1592 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1593 GtkAdjustment *adjustment)
1595 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1597 if (spin_button->adjustment != adjustment)
1599 if (spin_button->adjustment)
1601 gtk_signal_disconnect_by_data (GTK_OBJECT (spin_button->adjustment),
1602 (gpointer) spin_button);
1603 gtk_object_unref (GTK_OBJECT (spin_button->adjustment));
1605 spin_button->adjustment = adjustment;
1608 gtk_object_ref (GTK_OBJECT (adjustment));
1609 gtk_object_sink (GTK_OBJECT (adjustment));
1610 gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
1611 (GtkSignalFunc) gtk_spin_button_value_changed,
1612 (gpointer) spin_button);
1613 gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
1614 (GtkSignalFunc) adjustment_changed_cb,
1615 (gpointer) spin_button);
1618 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1621 g_object_notify (G_OBJECT (spin_button), "adjustment");
1625 * gtk_spin_button_get_adjustment:
1628 * Get the adjustment associated with a #GtkSpinButton
1630 * Return value: the #GtkAdjustment of @spin_button
1633 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1635 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1637 return spin_button->adjustment;
1641 * gtk_spin_button_set_digits:
1642 * @spin_button: a #GtkSpinButton
1643 * @digits: the number of digits to be displayed for the spin button's value
1645 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1649 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1652 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1654 if (spin_button->digits != digits)
1656 spin_button->digits = digits;
1657 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1658 g_object_notify (G_OBJECT (spin_button), "digits");
1660 /* since lower/upper may have changed */
1661 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1666 * gtk_spin_button_get_digits:
1667 * @spin_button: a #GtkSpinButton
1669 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1671 * Returns: the current precision
1674 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1676 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1678 return spin_button->digits;
1682 * gtk_spin_button_set_increments:
1683 * @spin_button: a #GtkSpinButton
1684 * @step: increment applied for a button 1 press.
1685 * @page: increment applied for a button 2 press.
1687 * Sets the step and page increments for spin_button. This affects how
1688 * quickly the value changes when the spin button's arrows are activated.
1691 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1695 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1697 spin_button->adjustment->step_increment = step;
1698 spin_button->adjustment->page_increment = page;
1702 * gtk_spin_button_get_increments:
1703 * @psin_button: a #GtkSpinButton
1704 * @step: location to store step increment, or %NULL
1705 * @page: location to store page increment, or %NULL
1707 * Gets the current step and page the increments used by @spin_button. See
1708 * gtk_spin_button_set_increments().
1711 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1715 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1718 *step = spin_button->adjustment->step_increment;
1720 *page = spin_button->adjustment->page_increment;
1724 * gtk_spin_button_set_range:
1725 * @spin_button: a #GtkSpinButton
1726 * @min: minimum allowable value
1727 * @max: maximum allowable value
1729 * Sets the minimum and maximum allowable values for @spin_button
1732 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1738 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1740 spin_button->adjustment->lower = min;
1741 spin_button->adjustment->upper = max;
1743 value = CLAMP (spin_button->adjustment->value,
1744 spin_button->adjustment->lower,
1745 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1747 if (value != spin_button->adjustment->value)
1748 gtk_spin_button_set_value (spin_button, value);
1750 gtk_adjustment_changed (spin_button->adjustment);
1754 * gtk_spin_button_get_range:
1755 * @spin_button: a #GtkSpinButton
1756 * @min: location to store minimum allowed value, or %NULL
1757 * @max: location to store maximum allowed value, or %NULL
1759 * Gets the range allowed for @spin_button. See
1760 * gtk_spin_button_set_range().
1763 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1767 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1770 *min = spin_button->adjustment->lower;
1772 *max = spin_button->adjustment->upper;
1776 * gtk_spin_button_get_value:
1777 * @spin_button: a #GtkSpinButton
1779 * Get the value in the @spin_button.
1781 * Return value: the value of @spin_button
1784 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1786 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1788 return spin_button->adjustment->value;
1792 * gtk_spin_button_get_value_as_int:
1793 * @spin_button: a #GtkSpinButton
1795 * Get the value @spin_button represented as an integer.
1797 * Return value: the value of @spin_button
1800 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1804 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1806 val = spin_button->adjustment->value;
1807 if (val - floor (val) < ceil (val) - val)
1814 * gtk_spin_button_set_value:
1815 * @spin_button: a #GtkSpinButton
1816 * @value: the new value
1818 * Set the value of @spin_button.
1821 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1824 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1826 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1827 gtk_adjustment_set_value (spin_button->adjustment, value);
1830 gint return_val = FALSE;
1831 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1833 if (return_val == FALSE)
1834 gtk_spin_button_default_output (spin_button);
1839 * gtk_spin_button_set_update_policy:
1840 * @spin_button: a #GtkSpinButton
1841 * @policy: a #GtkSpinButtonUpdatePolicy value
1843 * Sets the update behavior of a spin button. This determines whether the
1844 * spin button is always updated or only when a valid value is set.
1847 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1848 GtkSpinButtonUpdatePolicy policy)
1850 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1852 if (spin_button->update_policy != policy)
1854 spin_button->update_policy = policy;
1855 g_object_notify (G_OBJECT (spin_button), "update_policy");
1860 * gtk_spin_button_get_update_policy:
1861 * @spin_button: a #GtkSpinButton
1863 * Gets the update behavior of a spin button. See
1864 * gtk_spin_button_set_update_policy().
1866 * Return value: the current update policy
1868 GtkSpinButtonUpdatePolicy
1869 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1871 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1873 return spin_button->update_policy;
1877 * gtk_spin_button_set_numeric:
1878 * @spin_button: a #GtkSpinButton
1879 * @numeric: flag indicating if only numeric entry is allowed.
1881 * Sets the flag that determines if non-numeric text can be typed into
1885 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1888 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1890 spin_button->numeric = (numeric != 0);
1892 g_object_notify (G_OBJECT (spin_button), "numeric");
1896 * gtk_spin_button_get_numeric:
1897 * @spin_button: a #GtkSpinButton
1899 * Returns whether non-numeric text can be typed into the spin button.
1900 * See gtk_spin_button_set_numeric().
1902 * Return value: %TRUE if only numeric text can be entered
1905 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1907 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1909 return spin_button->numeric;
1913 * gtk_spin_button_set_wrap:
1914 * @spin_button: a #GtkSpinButton
1915 * @wrap: a flag indicating if wrapping behavior is performed.
1917 * Sets the flag that determines if a spin button value wraps around to the
1918 * opposite limit when the upper or lower limit of the range is exceeded.
1921 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1924 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1926 spin_button->wrap = (wrap != 0);
1928 g_object_notify (G_OBJECT (spin_button), "wrap");
1932 * gtk_spin_button_get_wrap:
1933 * @spin_button: a #GtkSpinButton
1935 * Returns whether the spin button's value wraps around to the
1936 * opposite limit when the upper or lower limit of the range is
1937 * exceeded. See gtk_spin_button_set_wrap().
1939 * Return value: %TRUE if the spin button wraps around
1942 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
1944 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1946 return spin_button->wrap;
1950 * spin_button_get_shadow_type:
1951 * @spin_button: a #GtkSpinButton
1953 * Convenience function to Get the shadow type from the underlying widget's
1956 * Return value: the #GtkShadowType
1959 spin_button_get_shadow_type (GtkSpinButton *spin_button)
1961 GtkShadowType rc_shadow_type;
1963 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
1965 return rc_shadow_type;
1969 * gtk_spin_button_set_snap_to_ticks:
1970 * @spin_button: a #GtkSpinButton
1971 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
1973 * Sets the policy as to whether values are corrected to the nearest step
1974 * increment when a spin button is activated after providing an invalid value.
1977 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
1978 gboolean snap_to_ticks)
1982 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1984 new_val = (snap_to_ticks != 0);
1986 if (new_val != spin_button->snap_to_ticks)
1988 spin_button->snap_to_ticks = new_val;
1989 if (new_val && GTK_ENTRY (spin_button)->editable)
1990 gtk_spin_button_update (spin_button);
1992 g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
1997 * gtk_spin_button_get_snap_to_ticks:
1998 * @spin_button: a #GtkSpinButton
2000 * Returns whether the values are corrected to the nearest step. See
2001 * gtk_spin_button_set_snap_to_ticks().
2003 * Return value: %TRUE if values are snapped to the nearest step.
2006 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2008 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2010 return spin_button->snap_to_ticks;
2014 * gtk_spin_button_spin:
2015 * @spin_button: a #GtkSpinButton
2016 * @direction: a #GtkSpinType indicating the direction to spin.
2017 * @increment: step increment to apply in the specified direction.
2019 * Increment or decrement a spin button's value in a specified direction
2020 * by a specified amount.
2023 gtk_spin_button_spin (GtkSpinButton *spin_button,
2024 GtkSpinType direction,
2030 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2032 adj = spin_button->adjustment;
2034 /* for compatibility with the 1.0.x version of this function */
2035 if (increment != 0 && increment != adj->step_increment &&
2036 (direction == GTK_SPIN_STEP_FORWARD ||
2037 direction == GTK_SPIN_STEP_BACKWARD))
2039 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2040 increment = -increment;
2041 direction = GTK_SPIN_USER_DEFINED;
2046 case GTK_SPIN_STEP_FORWARD:
2048 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2051 case GTK_SPIN_STEP_BACKWARD:
2053 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2056 case GTK_SPIN_PAGE_FORWARD:
2058 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2061 case GTK_SPIN_PAGE_BACKWARD:
2063 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2068 diff = adj->value - adj->lower;
2070 gtk_spin_button_real_spin (spin_button, -diff);
2075 diff = adj->upper - adj->value;
2077 gtk_spin_button_real_spin (spin_button, diff);
2080 case GTK_SPIN_USER_DEFINED:
2083 gtk_spin_button_real_spin (spin_button, increment);
2092 * gtk_spin_button_update:
2093 * @spin_button: a #GtkSpinButton
2095 * Manually force an update of the spin button.
2098 gtk_spin_button_update (GtkSpinButton *spin_button)
2104 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2107 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[INPUT],
2109 if (return_val == FALSE)
2111 return_val = gtk_spin_button_default_input (spin_button, &val);
2112 error = (return_val == GTK_INPUT_ERROR);
2114 else if (return_val == GTK_INPUT_ERROR)
2117 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2119 if (val < spin_button->adjustment->lower)
2120 val = spin_button->adjustment->lower;
2121 else if (val > spin_button->adjustment->upper)
2122 val = spin_button->adjustment->upper;
2124 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2126 val < spin_button->adjustment->lower ||
2127 val > spin_button->adjustment->upper))
2129 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2133 if (spin_button->snap_to_ticks)
2134 gtk_spin_button_snap (spin_button, val);
2137 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2138 gtk_adjustment_set_value (spin_button->adjustment, val);
2142 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
2144 if (return_val == FALSE)
2145 gtk_spin_button_default_output (spin_button);