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 "gtkmarshalers.h"
39 #include "gtksignal.h"
40 #include "gtksettings.h"
43 #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_editable_init (GtkEditableClass *iface);
73 static void gtk_spin_button_init (GtkSpinButton *spin_button);
74 static void gtk_spin_button_finalize (GObject *object);
75 static void gtk_spin_button_destroy (GtkObject *object);
76 static void gtk_spin_button_set_property (GObject *object,
80 static void gtk_spin_button_get_property (GObject *object,
84 static void gtk_spin_button_map (GtkWidget *widget);
85 static void gtk_spin_button_unmap (GtkWidget *widget);
86 static void gtk_spin_button_realize (GtkWidget *widget);
87 static void gtk_spin_button_unrealize (GtkWidget *widget);
88 static void gtk_spin_button_size_request (GtkWidget *widget,
89 GtkRequisition *requisition);
90 static void gtk_spin_button_size_allocate (GtkWidget *widget,
91 GtkAllocation *allocation);
92 static gint gtk_spin_button_expose (GtkWidget *widget,
93 GdkEventExpose *event);
94 static gint gtk_spin_button_button_press (GtkWidget *widget,
95 GdkEventButton *event);
96 static gint gtk_spin_button_button_release (GtkWidget *widget,
97 GdkEventButton *event);
98 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
99 GdkEventMotion *event);
100 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
101 GdkEventCrossing *event);
102 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
103 GdkEventCrossing *event);
104 static gint gtk_spin_button_focus_out (GtkWidget *widget,
105 GdkEventFocus *event);
106 static void gtk_spin_button_grab_notify (GtkWidget *widget,
107 gboolean was_grabbed);
108 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
110 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
111 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
112 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
113 GtkSpinButton *spin_button);
114 static gint gtk_spin_button_key_press (GtkWidget *widget,
116 static gint gtk_spin_button_key_release (GtkWidget *widget,
118 static gint gtk_spin_button_scroll (GtkWidget *widget,
119 GdkEventScroll *event);
120 static void gtk_spin_button_activate (GtkEntry *entry);
121 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
123 static void gtk_spin_button_insert_text (GtkEditable *editable,
124 const gchar *new_text,
125 gint new_text_length,
127 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
129 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
131 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
132 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
133 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
136 static GtkEntryClass *parent_class = NULL;
137 static guint spinbutton_signals[LAST_SIGNAL] = {0};
141 gtk_spin_button_get_type (void)
143 static GtkType spin_button_type = 0;
145 if (!spin_button_type)
147 static const GtkTypeInfo spin_button_info =
150 sizeof (GtkSpinButton),
151 sizeof (GtkSpinButtonClass),
152 (GtkClassInitFunc) gtk_spin_button_class_init,
153 (GtkObjectInitFunc) gtk_spin_button_init,
154 /* reserved_1 */ NULL,
155 /* reserved_2 */ NULL,
156 (GtkClassInitFunc) NULL,
159 static const GInterfaceInfo editable_info =
161 (GInterfaceInitFunc) gtk_spin_button_editable_init, /* interface_init */
162 NULL, /* interface_finalize */
163 NULL /* interface_data */
166 spin_button_type = gtk_type_unique (GTK_TYPE_ENTRY, &spin_button_info);
167 g_type_add_interface_static (spin_button_type,
171 return spin_button_type;
175 gtk_spin_button_class_init (GtkSpinButtonClass *class)
177 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
178 GtkObjectClass *object_class;
179 GtkWidgetClass *widget_class;
180 GtkEntryClass *entry_class;
182 object_class = (GtkObjectClass*) class;
183 widget_class = (GtkWidgetClass*) class;
184 entry_class = (GtkEntryClass*) class;
186 parent_class = gtk_type_class (GTK_TYPE_ENTRY);
188 gobject_class->finalize = gtk_spin_button_finalize;
190 gobject_class->set_property = gtk_spin_button_set_property;
191 gobject_class->get_property = gtk_spin_button_get_property;
193 object_class->destroy = gtk_spin_button_destroy;
195 widget_class->map = gtk_spin_button_map;
196 widget_class->unmap = gtk_spin_button_unmap;
197 widget_class->realize = gtk_spin_button_realize;
198 widget_class->unrealize = gtk_spin_button_unrealize;
199 widget_class->size_request = gtk_spin_button_size_request;
200 widget_class->size_allocate = gtk_spin_button_size_allocate;
201 widget_class->expose_event = gtk_spin_button_expose;
202 widget_class->scroll_event = gtk_spin_button_scroll;
203 widget_class->button_press_event = gtk_spin_button_button_press;
204 widget_class->button_release_event = gtk_spin_button_button_release;
205 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
206 widget_class->key_press_event = gtk_spin_button_key_press;
207 widget_class->key_release_event = gtk_spin_button_key_release;
208 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
209 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
210 widget_class->focus_out_event = gtk_spin_button_focus_out;
211 widget_class->grab_notify = gtk_spin_button_grab_notify;
213 entry_class->activate = gtk_spin_button_activate;
216 class->output = NULL;
218 g_object_class_install_property (gobject_class,
220 g_param_spec_object ("adjustment",
222 _("The adjustment that holds the value of the spinbutton"),
226 g_object_class_install_property (gobject_class,
228 g_param_spec_double ("climb_rate",
230 _("The acceleration rate when you hold down a button"),
236 g_object_class_install_property (gobject_class,
238 g_param_spec_uint ("digits",
240 _("The number of decimal places to display"),
246 g_object_class_install_property (gobject_class,
248 g_param_spec_boolean ("snap_to_ticks",
250 _("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
254 g_object_class_install_property (gobject_class,
256 g_param_spec_boolean ("numeric",
258 _("Whether non-numeric characters should be ignored"),
262 g_object_class_install_property (gobject_class,
264 g_param_spec_boolean ("wrap",
266 _("Whether a spin button should wrap upon reaching its limits"),
270 g_object_class_install_property (gobject_class,
272 g_param_spec_enum ("update_policy",
274 _("Whether the spin button should update always, or only when the value is legal"),
275 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
279 g_object_class_install_property (gobject_class,
281 g_param_spec_double ("value",
283 _("Reads the current value, or sets a new value"),
289 gtk_widget_class_install_style_property_parser (widget_class,
290 g_param_spec_enum ("shadow_type", "Shadow Type", NULL,
291 GTK_TYPE_SHADOW_TYPE,
294 gtk_rc_property_parse_enum);
295 spinbutton_signals[INPUT] =
296 gtk_signal_new ("input",
298 GTK_CLASS_TYPE (object_class),
299 GTK_SIGNAL_OFFSET (GtkSpinButtonClass, input),
300 _gtk_marshal_INT__POINTER,
301 GTK_TYPE_INT, 1, GTK_TYPE_POINTER);
303 spinbutton_signals[OUTPUT] =
304 g_signal_new ("output",
305 G_TYPE_FROM_CLASS(object_class),
307 G_STRUCT_OFFSET(GtkSpinButtonClass, output),
308 _gtk_boolean_handled_accumulator, NULL,
309 _gtk_marshal_BOOLEAN__VOID,
312 spinbutton_signals[VALUE_CHANGED] =
313 gtk_signal_new ("value_changed",
315 GTK_CLASS_TYPE (object_class),
316 GTK_SIGNAL_OFFSET (GtkSpinButtonClass, value_changed),
317 _gtk_marshal_VOID__VOID,
322 gtk_spin_button_editable_init (GtkEditableClass *iface)
324 iface->insert_text = gtk_spin_button_insert_text;
328 gtk_spin_button_set_property (GObject *object,
333 GtkSpinButton *spin_button;
335 spin_button = GTK_SPIN_BUTTON (object);
339 GtkAdjustment *adjustment;
341 case PROP_ADJUSTMENT:
342 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
344 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
345 gtk_spin_button_set_adjustment (spin_button, adjustment);
347 case PROP_CLIMB_RATE:
348 gtk_spin_button_configure (spin_button,
349 spin_button->adjustment,
350 g_value_get_double (value),
351 spin_button->digits);
354 gtk_spin_button_configure (spin_button,
355 spin_button->adjustment,
356 spin_button->climb_rate,
357 g_value_get_uint (value));
359 case PROP_SNAP_TO_TICKS:
360 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
363 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
366 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
368 case PROP_UPDATE_POLICY:
369 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
372 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
380 gtk_spin_button_get_property (GObject *object,
385 GtkSpinButton *spin_button;
387 spin_button = GTK_SPIN_BUTTON (object);
391 case PROP_ADJUSTMENT:
392 g_value_set_object (value, spin_button->adjustment);
394 case PROP_CLIMB_RATE:
395 g_value_set_double (value, spin_button->climb_rate);
398 g_value_set_uint (value, spin_button->digits);
400 case PROP_SNAP_TO_TICKS:
401 g_value_set_boolean (value, spin_button->snap_to_ticks);
404 g_value_set_boolean (value, spin_button->numeric);
407 g_value_set_boolean (value, spin_button->wrap);
409 case PROP_UPDATE_POLICY:
410 g_value_set_enum (value, spin_button->update_policy);
413 g_value_set_double (value, spin_button->adjustment->value);
416 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
422 gtk_spin_button_init (GtkSpinButton *spin_button)
424 spin_button->adjustment = NULL;
425 spin_button->panel = NULL;
426 spin_button->timer = 0;
427 spin_button->ev_time = 0;
428 spin_button->climb_rate = 0.0;
429 spin_button->timer_step = 0.0;
430 spin_button->update_policy = GTK_UPDATE_ALWAYS;
431 spin_button->in_child = 2;
432 spin_button->click_child = 2;
433 spin_button->button = 0;
434 spin_button->need_timer = FALSE;
435 spin_button->timer_calls = 0;
436 spin_button->digits = 0;
437 spin_button->numeric = FALSE;
438 spin_button->wrap = FALSE;
439 spin_button->snap_to_ticks = FALSE;
440 gtk_spin_button_set_adjustment (spin_button,
441 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
445 gtk_spin_button_finalize (GObject *object)
447 g_return_if_fail (GTK_IS_SPIN_BUTTON (object));
449 gtk_object_unref (GTK_OBJECT (GTK_SPIN_BUTTON (object)->adjustment));
451 G_OBJECT_CLASS (parent_class)->finalize (object);
455 gtk_spin_button_destroy (GtkObject *object)
457 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
459 GTK_OBJECT_CLASS (parent_class)->destroy (object);
463 gtk_spin_button_map (GtkWidget *widget)
465 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
467 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
469 GTK_WIDGET_CLASS (parent_class)->map (widget);
470 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
475 gtk_spin_button_unmap (GtkWidget *widget)
477 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
479 if (GTK_WIDGET_MAPPED (widget))
481 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
482 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
487 gtk_spin_button_realize (GtkWidget *widget)
489 GtkSpinButton *spin_button;
490 GdkWindowAttr attributes;
491 gint attributes_mask;
496 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
498 spin_button = GTK_SPIN_BUTTON (widget);
499 arrow_size = spin_button_get_arrow_size (spin_button);
501 real_width = widget->allocation.width;
502 widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
503 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
504 GDK_KEY_RELEASE_MASK);
505 GTK_WIDGET_CLASS (parent_class)->realize (widget);
507 widget->allocation.width = real_width;
509 attributes.window_type = GDK_WINDOW_CHILD;
510 attributes.wclass = GDK_INPUT_OUTPUT;
511 attributes.visual = gtk_widget_get_visual (widget);
512 attributes.colormap = gtk_widget_get_colormap (widget);
513 attributes.event_mask = gtk_widget_get_events (widget);
514 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
515 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
516 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
518 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
520 attributes.x = (widget->allocation.x +
521 widget->allocation.width - arrow_size -
522 2 * widget->style->xthickness);
523 attributes.y = widget->allocation.y + (widget->allocation.height -
524 widget->requisition.height) / 2;
525 attributes.width = arrow_size + 2 * widget->style->xthickness;
526 attributes.height = widget->requisition.height;
528 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
529 &attributes, attributes_mask);
530 gdk_window_set_user_data (spin_button->panel, widget);
532 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
535 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
537 if (return_val == FALSE)
538 gtk_spin_button_default_output (spin_button);
542 gtk_spin_button_unrealize (GtkWidget *widget)
546 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
548 spin = GTK_SPIN_BUTTON (widget);
550 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
554 gdk_window_set_user_data (spin->panel, NULL);
555 gdk_window_destroy (spin->panel);
561 compute_double_length (double val, int digits)
567 if (fabs (val) > 1.0)
568 a = floor (log10 (fabs (val))) + 1;
580 return a + digits + extra;
584 gtk_spin_button_size_request (GtkWidget *widget,
585 GtkRequisition *requisition)
588 GtkSpinButton *spin_button;
591 g_return_if_fail (requisition != NULL);
592 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
594 entry = GTK_ENTRY (widget);
595 spin_button = GTK_SPIN_BUTTON (widget);
596 arrow_size = spin_button_get_arrow_size (spin_button);
598 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
600 if (entry->width_chars < 0)
602 PangoContext *context;
603 PangoFontMetrics *metrics;
609 gboolean interior_focus;
612 gtk_widget_style_get (widget,
613 "interior-focus", &interior_focus,
614 "focus-line-width", &focus_width,
617 context = gtk_widget_get_pango_context (widget);
618 metrics = pango_context_get_metrics (context,
619 widget->style->font_desc,
620 pango_context_get_language (context));
622 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
623 digit_width = PANGO_PIXELS (digit_width);
625 pango_font_metrics_unref (metrics);
627 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
629 width = MIN_SPIN_BUTTON_WIDTH;
630 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
631 spin_button->digits));
633 string_len = compute_double_length (spin_button->adjustment->upper,
634 spin_button->digits);
635 w = MIN (string_len, max_string_len) * digit_width;
636 width = MAX (width, w);
637 string_len = compute_double_length (spin_button->adjustment->lower,
638 spin_button->digits);
639 w = MIN (string_len, max_string_len) * digit_width;
640 width = MAX (width, w);
642 requisition->width = width + arrow_size + 2 * widget->style->xthickness;
644 requisition->width += 2 * focus_width;
648 requisition->width += arrow_size + 2 * widget->style->xthickness;
653 gtk_spin_button_size_allocate (GtkWidget *widget,
654 GtkAllocation *allocation)
657 GtkAllocation child_allocation;
660 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
661 g_return_if_fail (allocation != NULL);
663 spin = GTK_SPIN_BUTTON (widget);
664 arrow_size = spin_button_get_arrow_size (spin);
666 child_allocation = *allocation;
667 if (child_allocation.width > arrow_size + 2 * widget->style->xthickness)
668 child_allocation.width -= arrow_size + 2 * widget->style->xthickness;
670 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
671 child_allocation.x += arrow_size + 2 * widget->style->xthickness;
673 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &child_allocation);
675 widget->allocation = *allocation;
677 if (GTK_WIDGET_REALIZED (widget))
679 child_allocation.width = arrow_size + 2 * widget->style->xthickness;
680 child_allocation.height = widget->requisition.height;
682 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
683 child_allocation.x = (allocation->x + allocation->width -
684 arrow_size - 2 * widget->style->xthickness);
686 child_allocation.x = allocation->x;
688 child_allocation.y = allocation->y + (allocation->height - widget->requisition.height) / 2;
690 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
693 child_allocation.width,
694 child_allocation.height);
699 gtk_spin_button_expose (GtkWidget *widget,
700 GdkEventExpose *event)
705 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
706 g_return_val_if_fail (event != NULL, FALSE);
708 spin = GTK_SPIN_BUTTON (widget);
709 arrow_size = spin_button_get_arrow_size (spin);
711 if (GTK_WIDGET_DRAWABLE (widget))
713 GtkShadowType shadow_type;
715 /* FIXME this seems like really broken code -
716 * why aren't we looking at event->window
717 * and acting accordingly?
720 shadow_type = spin_button_get_shadow_type (spin);
721 if (shadow_type != GTK_SHADOW_NONE)
722 gtk_paint_box (widget->style, spin->panel,
723 GTK_STATE_NORMAL, shadow_type,
724 &event->area, widget, "spinbutton",
726 arrow_size + 2 * widget->style->xthickness,
727 widget->requisition.height);
730 gdk_window_set_back_pixmap (spin->panel, NULL, TRUE);
731 gdk_window_clear_area (spin->panel,
732 event->area.x, event->area.y,
733 event->area.width, event->area.height);
735 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
736 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
738 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
745 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
748 GtkShadowType spin_shadow_type;
749 GtkStateType state_type;
750 GtkShadowType shadow_type;
756 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
758 widget = GTK_WIDGET (spin_button);
759 spin_shadow_type = spin_button_get_shadow_type (spin_button);
760 arrow_size = spin_button_get_arrow_size (spin_button);
762 if (GTK_WIDGET_DRAWABLE (spin_button))
764 if (!spin_button->wrap &&
765 (((arrow == GTK_ARROW_UP &&
766 (spin_button->adjustment->upper - spin_button->adjustment->value
768 ((arrow == GTK_ARROW_DOWN &&
769 (spin_button->adjustment->value - spin_button->adjustment->lower
772 shadow_type = GTK_SHADOW_ETCHED_IN;
773 state_type = GTK_STATE_NORMAL;
777 if (spin_button->in_child == arrow)
779 if (spin_button->click_child == arrow)
780 state_type = GTK_STATE_ACTIVE;
782 state_type = GTK_STATE_PRELIGHT;
785 state_type = GTK_STATE_NORMAL;
787 if (spin_button->click_child == arrow)
788 shadow_type = GTK_SHADOW_IN;
790 shadow_type = GTK_SHADOW_OUT;
792 if (arrow == GTK_ARROW_UP)
794 if (spin_shadow_type != GTK_SHADOW_NONE)
796 x = widget->style->xthickness;
797 y = widget->style->ythickness;
801 x = widget->style->xthickness - 1;
802 y = widget->style->ythickness - 1;
804 gtk_paint_arrow (widget->style, spin_button->panel,
805 state_type, shadow_type,
806 NULL, widget, "spinbutton",
809 widget->requisition.height / 2
810 - widget->style->ythickness);
814 if (spin_shadow_type != GTK_SHADOW_NONE)
816 x = widget->style->xthickness;
817 y = widget->requisition.height / 2;
821 x = widget->style->xthickness - 1;
822 y = widget->requisition.height / 2 + 1;
824 gtk_paint_arrow (widget->style, spin_button->panel,
825 state_type, shadow_type,
826 NULL, widget, "spinbutton",
829 widget->requisition.height / 2
830 - widget->style->ythickness);
836 gtk_spin_button_enter_notify (GtkWidget *widget,
837 GdkEventCrossing *event)
841 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
842 g_return_val_if_fail (event != NULL, FALSE);
844 spin = GTK_SPIN_BUTTON (widget);
846 if (event->window == spin->panel)
851 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
853 if (y <= widget->requisition.height / 2)
855 spin->in_child = GTK_ARROW_UP;
856 if (spin->click_child == 2)
857 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
861 spin->in_child = GTK_ARROW_DOWN;
862 if (spin->click_child == 2)
863 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
870 gtk_spin_button_leave_notify (GtkWidget *widget,
871 GdkEventCrossing *event)
875 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
876 g_return_val_if_fail (event != NULL, FALSE);
878 spin = GTK_SPIN_BUTTON (widget);
880 if (event->window == spin->panel && spin->click_child == 2)
882 if (spin->in_child == GTK_ARROW_UP)
885 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
890 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
897 gtk_spin_button_focus_out (GtkWidget *widget,
898 GdkEventFocus *event)
900 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
901 g_return_val_if_fail (event != NULL, FALSE);
903 if (GTK_ENTRY (widget)->editable)
904 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
906 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
910 gtk_spin_button_grab_notify (GtkWidget *widget,
911 gboolean was_grabbed)
914 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
918 gtk_spin_button_scroll (GtkWidget *widget,
919 GdkEventScroll *event)
923 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
924 g_return_val_if_fail (event != NULL, FALSE);
926 spin = GTK_SPIN_BUTTON (widget);
928 if (event->direction == GDK_SCROLL_UP)
930 if (!GTK_WIDGET_HAS_FOCUS (widget))
931 gtk_widget_grab_focus (widget);
932 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
934 else if (event->direction == GDK_SCROLL_DOWN)
936 if (!GTK_WIDGET_HAS_FOCUS (widget))
937 gtk_widget_grab_focus (widget);
938 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
947 gtk_spin_button_stop_spinning(GtkSpinButton *spin)
951 gtk_timeout_remove (spin->timer);
953 spin->timer_calls = 0;
954 spin->need_timer = FALSE;
962 start_spinning (GtkSpinButton *spin,
963 GtkArrowType click_child,
966 spin->click_child = click_child;
967 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
971 spin->timer_step = step;
972 spin->need_timer = TRUE;
973 spin->timer = gtk_timeout_add (SPIN_BUTTON_INITIAL_TIMER_DELAY,
974 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
977 gtk_spin_button_draw_arrow (spin, click_child);
981 gtk_spin_button_button_press (GtkWidget *widget,
982 GdkEventButton *event)
986 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
987 g_return_val_if_fail (event != NULL, FALSE);
989 spin = GTK_SPIN_BUTTON (widget);
993 if (event->window == spin->panel)
995 if (!GTK_WIDGET_HAS_FOCUS (widget))
996 gtk_widget_grab_focus (widget);
997 spin->button = event->button;
999 if (GTK_ENTRY (widget)->editable)
1000 gtk_spin_button_update (spin);
1002 if (event->y <= widget->requisition.height / 2)
1004 if (event->button == 1)
1005 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->step_increment);
1006 else if (event->button == 2)
1007 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->page_increment);
1011 if (event->button == 1)
1012 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->step_increment);
1013 else if (event->button == 2)
1014 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->page_increment);
1019 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1025 gtk_spin_button_button_release (GtkWidget *widget,
1026 GdkEventButton *event)
1028 GtkSpinButton *spin;
1031 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1032 g_return_val_if_fail (event != NULL, FALSE);
1034 spin = GTK_SPIN_BUTTON (widget);
1035 arrow_size = spin_button_get_arrow_size (spin);
1037 if (event->button == spin->button)
1041 gtk_spin_button_stop_spinning (spin);
1043 if (event->button == 3)
1045 if (event->y >= 0 && event->x >= 0 &&
1046 event->y <= widget->requisition.height &&
1047 event->x <= arrow_size + 2 * widget->style->xthickness)
1049 if (spin->click_child == GTK_ARROW_UP &&
1050 event->y <= widget->requisition.height / 2)
1054 diff = spin->adjustment->upper - spin->adjustment->value;
1056 gtk_spin_button_real_spin (spin, diff);
1058 else if (spin->click_child == GTK_ARROW_DOWN &&
1059 event->y > widget->requisition.height / 2)
1063 diff = spin->adjustment->value - spin->adjustment->lower;
1065 gtk_spin_button_real_spin (spin, -diff);
1069 click_child = spin->click_child;
1070 spin->click_child = 2;
1071 gtk_spin_button_draw_arrow (spin, click_child);
1075 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1081 gtk_spin_button_motion_notify (GtkWidget *widget,
1082 GdkEventMotion *event)
1084 GtkSpinButton *spin;
1086 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1087 g_return_val_if_fail (event != NULL, FALSE);
1089 spin = GTK_SPIN_BUTTON (widget);
1094 if (event->window == spin->panel)
1100 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1102 if (y <= widget->requisition.height / 2 &&
1103 spin->in_child == GTK_ARROW_DOWN)
1105 spin->in_child = GTK_ARROW_UP;
1106 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1107 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1109 else if (y > widget->requisition.height / 2 &&
1110 spin->in_child == GTK_ARROW_UP)
1112 spin->in_child = GTK_ARROW_DOWN;
1113 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1114 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1119 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1123 gtk_spin_button_timer (GtkSpinButton *spin_button)
1125 gboolean retval = FALSE;
1127 GDK_THREADS_ENTER ();
1129 if (spin_button->timer)
1131 if (spin_button->click_child == GTK_ARROW_UP)
1132 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1134 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1136 if (spin_button->need_timer)
1138 spin_button->need_timer = FALSE;
1139 spin_button->timer = gtk_timeout_add
1140 (SPIN_BUTTON_TIMER_DELAY, (GtkFunction) gtk_spin_button_timer,
1141 (gpointer) spin_button);
1145 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1146 < spin_button->adjustment->page_increment)
1148 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1149 spin_button->timer_calls++;
1152 spin_button->timer_calls = 0;
1153 spin_button->timer_step += spin_button->climb_rate;
1160 GDK_THREADS_LEAVE ();
1166 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1167 GtkSpinButton *spin_button)
1171 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1174 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1176 if (return_val == FALSE)
1177 gtk_spin_button_default_output (spin_button);
1179 gtk_signal_emit (GTK_OBJECT (spin_button),
1180 spinbutton_signals[VALUE_CHANGED]);
1182 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_UP);
1183 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_DOWN);
1185 g_object_notify (G_OBJECT (spin_button), "value");
1189 gtk_spin_button_key_press (GtkWidget *widget,
1192 GtkSpinButton *spin;
1194 gboolean key_repeat = FALSE;
1195 gboolean retval = FALSE;
1197 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1198 g_return_val_if_fail (event != NULL, FALSE);
1200 spin = GTK_SPIN_BUTTON (widget);
1201 key = event->keyval;
1203 key_repeat = (event->time == spin->ev_time);
1205 if (GTK_ENTRY (widget)->editable)
1212 if (GTK_WIDGET_HAS_FOCUS (widget))
1214 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1217 spin->timer_step = spin->adjustment->step_increment;
1219 gtk_spin_button_real_spin (spin, spin->timer_step);
1223 if (spin->climb_rate > 0.0 && spin->timer_step
1224 < spin->adjustment->page_increment)
1226 if (spin->timer_calls < MAX_TIMER_CALLS)
1227 spin->timer_calls++;
1230 spin->timer_calls = 0;
1231 spin->timer_step += spin->climb_rate;
1242 if (GTK_WIDGET_HAS_FOCUS (widget))
1244 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1247 spin->timer_step = spin->adjustment->step_increment;
1249 gtk_spin_button_real_spin (spin, -spin->timer_step);
1253 if (spin->climb_rate > 0.0 && spin->timer_step
1254 < spin->adjustment->page_increment)
1256 if (spin->timer_calls < MAX_TIMER_CALLS)
1257 spin->timer_calls++;
1260 spin->timer_calls = 0;
1261 spin->timer_step += spin->climb_rate;
1269 case GDK_KP_Page_Up:
1272 if (event->state & GDK_CONTROL_MASK)
1274 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1276 gtk_spin_button_real_spin (spin, diff);
1279 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1284 case GDK_KP_Page_Down:
1287 if (event->state & GDK_CONTROL_MASK)
1289 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1291 gtk_spin_button_real_spin (spin, -diff);
1294 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1306 gtk_spin_button_update (spin);
1310 return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
1314 gtk_spin_button_key_release (GtkWidget *widget,
1317 GtkSpinButton *spin;
1319 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1321 spin = GTK_SPIN_BUTTON (widget);
1323 spin->ev_time = event->time;
1328 gtk_spin_button_snap (GtkSpinButton *spin_button,
1334 inc = spin_button->adjustment->step_increment;
1335 tmp = (val - spin_button->adjustment->lower) / inc;
1336 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1337 val = spin_button->adjustment->lower + floor (tmp) * inc;
1339 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1341 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1342 gtk_adjustment_set_value (spin_button->adjustment, val);
1345 gint return_val = FALSE;
1346 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1348 if (return_val == FALSE)
1349 gtk_spin_button_default_output (spin_button);
1354 gtk_spin_button_activate (GtkEntry *entry)
1356 if (entry->editable)
1357 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1361 gtk_spin_button_insert_text (GtkEditable *editable,
1362 const gchar *new_text,
1363 gint new_text_length,
1366 GtkEntry *entry = GTK_ENTRY (editable);
1367 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1368 GtkEditableClass *parent_editable_iface = g_type_interface_peek (parent_class, GTK_TYPE_EDITABLE);
1380 entry_length = entry->text_length;
1384 if (*(lc->negative_sign))
1385 neg_sign = *(lc->negative_sign);
1389 if (*(lc->positive_sign))
1390 pos_sign = *(lc->positive_sign);
1394 for (sign=0, i=0; i<entry_length; i++)
1395 if ((entry->text[i] == neg_sign) ||
1396 (entry->text[i] == pos_sign))
1402 if (sign && !(*position))
1405 for (dotpos=-1, i=0; i<entry_length; i++)
1406 if (entry->text[i] == *(lc->decimal_point))
1412 if (dotpos > -1 && *position > dotpos &&
1413 (gint)spin->digits - entry_length
1414 + dotpos - new_text_length + 1 < 0)
1417 for (i = 0; i < new_text_length; i++)
1419 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1421 if (sign || (*position) || i)
1425 else if (new_text[i] == *(lc->decimal_point))
1427 if (!spin->digits || dotpos > -1 ||
1428 (new_text_length - 1 - i + entry_length
1429 - *position > (gint)spin->digits))
1431 dotpos = *position + i;
1433 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1438 parent_editable_iface->insert_text (editable, new_text,
1439 new_text_length, position);
1443 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1447 gdouble new_value = 0.0;
1449 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1451 adj = spin_button->adjustment;
1453 new_value = adj->value + increment;
1457 if (spin_button->wrap)
1459 if (fabs (adj->value - adj->upper) < EPSILON)
1460 new_value = adj->lower;
1461 else if (new_value > adj->upper)
1462 new_value = adj->upper;
1465 new_value = MIN (new_value, adj->upper);
1467 else if (increment < 0)
1469 if (spin_button->wrap)
1471 if (fabs (adj->value - adj->lower) < EPSILON)
1472 new_value = adj->upper;
1473 else if (new_value < adj->lower)
1474 new_value = adj->lower;
1477 new_value = MAX (new_value, adj->lower);
1480 if (fabs (new_value - adj->value) > EPSILON)
1481 gtk_adjustment_set_value (adj, new_value);
1485 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1490 *new_val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1492 return GTK_INPUT_ERROR;
1498 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1500 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1502 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1503 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1509 /***********************************************************
1510 ***********************************************************
1511 *** Public interface ***
1512 ***********************************************************
1513 ***********************************************************/
1517 gtk_spin_button_configure (GtkSpinButton *spin_button,
1518 GtkAdjustment *adjustment,
1522 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1525 gtk_spin_button_set_adjustment (spin_button, adjustment);
1527 adjustment = spin_button->adjustment;
1529 g_object_freeze_notify (G_OBJECT (spin_button));
1530 if (spin_button->digits != digits)
1532 spin_button->digits = digits;
1533 g_object_notify (G_OBJECT (spin_button), "digits");
1536 if (spin_button->climb_rate != climb_rate)
1538 spin_button->climb_rate = climb_rate;
1539 g_object_notify (G_OBJECT (spin_button), "climb_rate");
1541 g_object_thaw_notify (G_OBJECT (spin_button));
1543 gtk_adjustment_value_changed (adjustment);
1547 gtk_spin_button_new (GtkAdjustment *adjustment,
1551 GtkSpinButton *spin;
1554 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1556 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1558 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1560 return GTK_WIDGET (spin);
1564 * gtk_spin_button_new_with_range:
1565 * @min: Minimum allowable value
1566 * @max: Maximum allowable value
1567 * @step: Increment added or subtracted by spinning the widget
1569 * This is a convenience constructor that allows creation of a numeric
1570 * #GtkSpinButton without manually creating an adjustment. The value is
1571 * initially set to the minimum value and a page increment of 10 * @step
1572 * is the default. The precision of the spin button is equivalent to the
1573 * precision of @step.
1575 * Return value: The new spin button as a #GtkWidget.
1578 gtk_spin_button_new_with_range (gdouble min,
1583 GtkSpinButton *spin;
1586 g_return_val_if_fail (min < max, NULL);
1587 g_return_val_if_fail (step != 0.0, NULL);
1589 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1591 adj = gtk_adjustment_new (min, min, max, step, 10 * step, step);
1593 if (fabs (step) >= 1.0 || step == 0.0)
1596 digits = abs ((gint) floor (log10 (fabs (step))));
1597 if (digits > MAX_DIGITS)
1598 digits = MAX_DIGITS;
1601 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1603 gtk_spin_button_set_numeric (spin, TRUE);
1605 return GTK_WIDGET (spin);
1608 /* Callback used when the spin button's adjustment changes. We need to redraw
1609 * the arrows when the adjustment's range changes, and reevaluate our size request.
1612 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1614 GtkSpinButton *spin_button;
1616 spin_button = GTK_SPIN_BUTTON (data);
1618 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1622 * gtk_spin_button_set_adjustment:
1623 * @spin_button: a #GtkSpinButton
1624 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1626 * Replaces the #GtkAdjustment associated with @spin_button.
1629 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1630 GtkAdjustment *adjustment)
1632 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1634 if (spin_button->adjustment != adjustment)
1636 if (spin_button->adjustment)
1638 gtk_signal_disconnect_by_data (GTK_OBJECT (spin_button->adjustment),
1639 (gpointer) spin_button);
1640 gtk_object_unref (GTK_OBJECT (spin_button->adjustment));
1642 spin_button->adjustment = adjustment;
1645 gtk_object_ref (GTK_OBJECT (adjustment));
1646 gtk_object_sink (GTK_OBJECT (adjustment));
1647 gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
1648 (GtkSignalFunc) gtk_spin_button_value_changed,
1649 (gpointer) spin_button);
1650 gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
1651 (GtkSignalFunc) adjustment_changed_cb,
1652 (gpointer) spin_button);
1655 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1658 g_object_notify (G_OBJECT (spin_button), "adjustment");
1662 * gtk_spin_button_get_adjustment:
1665 * Get the adjustment associated with a #GtkSpinButton
1667 * Return value: the #GtkAdjustment of @spin_button
1670 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1672 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1674 return spin_button->adjustment;
1678 * gtk_spin_button_set_digits:
1679 * @spin_button: a #GtkSpinButton
1680 * @digits: the number of digits to be displayed for the spin button's value
1682 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1686 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1689 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1691 if (spin_button->digits != digits)
1693 spin_button->digits = digits;
1694 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1695 g_object_notify (G_OBJECT (spin_button), "digits");
1697 /* since lower/upper may have changed */
1698 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1703 * gtk_spin_button_get_digits:
1704 * @spin_button: a #GtkSpinButton
1706 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1708 * Returns: the current precision
1711 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1713 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1715 return spin_button->digits;
1719 * gtk_spin_button_set_increments:
1720 * @spin_button: a #GtkSpinButton
1721 * @step: increment applied for a button 1 press.
1722 * @page: increment applied for a button 2 press.
1724 * Sets the step and page increments for spin_button. This affects how
1725 * quickly the value changes when the spin button's arrows are activated.
1728 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1732 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1734 spin_button->adjustment->step_increment = step;
1735 spin_button->adjustment->page_increment = page;
1739 * gtk_spin_button_get_increments:
1740 * @spin_button: a #GtkSpinButton
1741 * @step: location to store step increment, or %NULL
1742 * @page: location to store page increment, or %NULL
1744 * Gets the current step and page the increments used by @spin_button. See
1745 * gtk_spin_button_set_increments().
1748 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1752 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1755 *step = spin_button->adjustment->step_increment;
1757 *page = spin_button->adjustment->page_increment;
1761 * gtk_spin_button_set_range:
1762 * @spin_button: a #GtkSpinButton
1763 * @min: minimum allowable value
1764 * @max: maximum allowable value
1766 * Sets the minimum and maximum allowable values for @spin_button
1769 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1775 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1777 spin_button->adjustment->lower = min;
1778 spin_button->adjustment->upper = max;
1780 value = CLAMP (spin_button->adjustment->value,
1781 spin_button->adjustment->lower,
1782 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1784 if (value != spin_button->adjustment->value)
1785 gtk_spin_button_set_value (spin_button, value);
1787 gtk_adjustment_changed (spin_button->adjustment);
1791 * gtk_spin_button_get_range:
1792 * @spin_button: a #GtkSpinButton
1793 * @min: location to store minimum allowed value, or %NULL
1794 * @max: location to store maximum allowed value, or %NULL
1796 * Gets the range allowed for @spin_button. See
1797 * gtk_spin_button_set_range().
1800 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1804 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1807 *min = spin_button->adjustment->lower;
1809 *max = spin_button->adjustment->upper;
1813 * gtk_spin_button_get_value:
1814 * @spin_button: a #GtkSpinButton
1816 * Get the value in the @spin_button.
1818 * Return value: the value of @spin_button
1821 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1823 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1825 return spin_button->adjustment->value;
1829 * gtk_spin_button_get_value_as_int:
1830 * @spin_button: a #GtkSpinButton
1832 * Get the value @spin_button represented as an integer.
1834 * Return value: the value of @spin_button
1837 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1841 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1843 val = spin_button->adjustment->value;
1844 if (val - floor (val) < ceil (val) - val)
1851 * gtk_spin_button_set_value:
1852 * @spin_button: a #GtkSpinButton
1853 * @value: the new value
1855 * Set the value of @spin_button.
1858 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1861 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1863 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1864 gtk_adjustment_set_value (spin_button->adjustment, value);
1867 gint return_val = FALSE;
1868 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1870 if (return_val == FALSE)
1871 gtk_spin_button_default_output (spin_button);
1876 * gtk_spin_button_set_update_policy:
1877 * @spin_button: a #GtkSpinButton
1878 * @policy: a #GtkSpinButtonUpdatePolicy value
1880 * Sets the update behavior of a spin button. This determines whether the
1881 * spin button is always updated or only when a valid value is set.
1884 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1885 GtkSpinButtonUpdatePolicy policy)
1887 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1889 if (spin_button->update_policy != policy)
1891 spin_button->update_policy = policy;
1892 g_object_notify (G_OBJECT (spin_button), "update_policy");
1897 * gtk_spin_button_get_update_policy:
1898 * @spin_button: a #GtkSpinButton
1900 * Gets the update behavior of a spin button. See
1901 * gtk_spin_button_set_update_policy().
1903 * Return value: the current update policy
1905 GtkSpinButtonUpdatePolicy
1906 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1908 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1910 return spin_button->update_policy;
1914 * gtk_spin_button_set_numeric:
1915 * @spin_button: a #GtkSpinButton
1916 * @numeric: flag indicating if only numeric entry is allowed.
1918 * Sets the flag that determines if non-numeric text can be typed into
1922 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1925 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1927 spin_button->numeric = (numeric != 0);
1929 g_object_notify (G_OBJECT (spin_button), "numeric");
1933 * gtk_spin_button_get_numeric:
1934 * @spin_button: a #GtkSpinButton
1936 * Returns whether non-numeric text can be typed into the spin button.
1937 * See gtk_spin_button_set_numeric().
1939 * Return value: %TRUE if only numeric text can be entered
1942 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1944 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1946 return spin_button->numeric;
1950 * gtk_spin_button_set_wrap:
1951 * @spin_button: a #GtkSpinButton
1952 * @wrap: a flag indicating if wrapping behavior is performed.
1954 * Sets the flag that determines if a spin button value wraps around to the
1955 * opposite limit when the upper or lower limit of the range is exceeded.
1958 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1961 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1963 spin_button->wrap = (wrap != 0);
1965 g_object_notify (G_OBJECT (spin_button), "wrap");
1969 * gtk_spin_button_get_wrap:
1970 * @spin_button: a #GtkSpinButton
1972 * Returns whether the spin button's value wraps around to the
1973 * opposite limit when the upper or lower limit of the range is
1974 * exceeded. See gtk_spin_button_set_wrap().
1976 * Return value: %TRUE if the spin button wraps around
1979 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
1981 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1983 return spin_button->wrap;
1987 spin_button_get_arrow_size (GtkSpinButton *spin_button)
1989 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
1991 return PANGO_PIXELS (size);
1995 * spin_button_get_shadow_type:
1996 * @spin_button: a #GtkSpinButton
1998 * Convenience function to Get the shadow type from the underlying widget's
2001 * Return value: the #GtkShadowType
2004 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2006 GtkShadowType rc_shadow_type;
2008 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
2010 return rc_shadow_type;
2014 * gtk_spin_button_set_snap_to_ticks:
2015 * @spin_button: a #GtkSpinButton
2016 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2018 * Sets the policy as to whether values are corrected to the nearest step
2019 * increment when a spin button is activated after providing an invalid value.
2022 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2023 gboolean snap_to_ticks)
2027 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2029 new_val = (snap_to_ticks != 0);
2031 if (new_val != spin_button->snap_to_ticks)
2033 spin_button->snap_to_ticks = new_val;
2034 if (new_val && GTK_ENTRY (spin_button)->editable)
2035 gtk_spin_button_update (spin_button);
2037 g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
2042 * gtk_spin_button_get_snap_to_ticks:
2043 * @spin_button: a #GtkSpinButton
2045 * Returns whether the values are corrected to the nearest step. See
2046 * gtk_spin_button_set_snap_to_ticks().
2048 * Return value: %TRUE if values are snapped to the nearest step.
2051 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2053 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2055 return spin_button->snap_to_ticks;
2059 * gtk_spin_button_spin:
2060 * @spin_button: a #GtkSpinButton
2061 * @direction: a #GtkSpinType indicating the direction to spin.
2062 * @increment: step increment to apply in the specified direction.
2064 * Increment or decrement a spin button's value in a specified direction
2065 * by a specified amount.
2068 gtk_spin_button_spin (GtkSpinButton *spin_button,
2069 GtkSpinType direction,
2075 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2077 adj = spin_button->adjustment;
2079 /* for compatibility with the 1.0.x version of this function */
2080 if (increment != 0 && increment != adj->step_increment &&
2081 (direction == GTK_SPIN_STEP_FORWARD ||
2082 direction == GTK_SPIN_STEP_BACKWARD))
2084 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2085 increment = -increment;
2086 direction = GTK_SPIN_USER_DEFINED;
2091 case GTK_SPIN_STEP_FORWARD:
2093 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2096 case GTK_SPIN_STEP_BACKWARD:
2098 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2101 case GTK_SPIN_PAGE_FORWARD:
2103 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2106 case GTK_SPIN_PAGE_BACKWARD:
2108 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2113 diff = adj->value - adj->lower;
2115 gtk_spin_button_real_spin (spin_button, -diff);
2120 diff = adj->upper - adj->value;
2122 gtk_spin_button_real_spin (spin_button, diff);
2125 case GTK_SPIN_USER_DEFINED:
2128 gtk_spin_button_real_spin (spin_button, increment);
2137 * gtk_spin_button_update:
2138 * @spin_button: a #GtkSpinButton
2140 * Manually force an update of the spin button.
2143 gtk_spin_button_update (GtkSpinButton *spin_button)
2149 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2152 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[INPUT],
2154 if (return_val == FALSE)
2156 return_val = gtk_spin_button_default_input (spin_button, &val);
2157 error = (return_val == GTK_INPUT_ERROR);
2159 else if (return_val == GTK_INPUT_ERROR)
2162 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2164 if (val < spin_button->adjustment->lower)
2165 val = spin_button->adjustment->lower;
2166 else if (val > spin_button->adjustment->upper)
2167 val = spin_button->adjustment->upper;
2169 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2171 val < spin_button->adjustment->lower ||
2172 val > spin_button->adjustment->upper))
2174 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2178 if (spin_button->snap_to_ticks)
2179 gtk_spin_button_snap (spin_button, val);
2182 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2183 gtk_adjustment_set_value (spin_button->adjustment, val);
2187 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
2189 if (return_val == FALSE)
2190 gtk_spin_button_default_output (spin_button);