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
43 #define SPIN_BUTTON_INITIAL_TIMER_DELAY 200
44 #define SPIN_BUTTON_TIMER_DELAY 20
45 #define MAX_TIMER_CALLS 5
70 static void gtk_spin_button_class_init (GtkSpinButtonClass *klass);
71 static void gtk_spin_button_editable_init (GtkEditableClass *iface);
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 (GtkEditable *editable,
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_arrow_size (GtkSpinButton *spin_button);
128 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
131 static GtkEntryClass *parent_class = NULL;
132 static guint spinbutton_signals[LAST_SIGNAL] = {0};
136 gtk_spin_button_get_type (void)
138 static GtkType spin_button_type = 0;
140 if (!spin_button_type)
142 static const GtkTypeInfo spin_button_info =
145 sizeof (GtkSpinButton),
146 sizeof (GtkSpinButtonClass),
147 (GtkClassInitFunc) gtk_spin_button_class_init,
148 (GtkObjectInitFunc) gtk_spin_button_init,
149 /* reserved_1 */ NULL,
150 /* reserved_2 */ NULL,
151 (GtkClassInitFunc) NULL,
154 static const GInterfaceInfo editable_info =
156 (GInterfaceInitFunc) gtk_spin_button_editable_init, /* interface_init */
157 NULL, /* interface_finalize */
158 NULL /* interface_data */
161 spin_button_type = gtk_type_unique (GTK_TYPE_ENTRY, &spin_button_info);
162 g_type_add_interface_static (spin_button_type,
166 return spin_button_type;
170 gtk_spin_button_class_init (GtkSpinButtonClass *class)
172 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
173 GtkObjectClass *object_class;
174 GtkWidgetClass *widget_class;
175 GtkEntryClass *entry_class;
177 object_class = (GtkObjectClass*) class;
178 widget_class = (GtkWidgetClass*) class;
179 entry_class = (GtkEntryClass*) class;
181 parent_class = gtk_type_class (GTK_TYPE_ENTRY);
183 gobject_class->finalize = gtk_spin_button_finalize;
185 gobject_class->set_property = gtk_spin_button_set_property;
186 gobject_class->get_property = gtk_spin_button_get_property;
188 widget_class->map = gtk_spin_button_map;
189 widget_class->unmap = gtk_spin_button_unmap;
190 widget_class->realize = gtk_spin_button_realize;
191 widget_class->unrealize = gtk_spin_button_unrealize;
192 widget_class->size_request = gtk_spin_button_size_request;
193 widget_class->size_allocate = gtk_spin_button_size_allocate;
194 widget_class->expose_event = gtk_spin_button_expose;
195 widget_class->scroll_event = gtk_spin_button_scroll;
196 widget_class->button_press_event = gtk_spin_button_button_press;
197 widget_class->button_release_event = gtk_spin_button_button_release;
198 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
199 widget_class->key_press_event = gtk_spin_button_key_press;
200 widget_class->key_release_event = gtk_spin_button_key_release;
201 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
202 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
203 widget_class->focus_out_event = gtk_spin_button_focus_out;
205 entry_class->activate = gtk_spin_button_activate;
208 class->output = NULL;
210 g_object_class_install_property (gobject_class,
212 g_param_spec_object ("adjustment",
214 _("The adjustment that holds the value of the spinbutton"),
218 g_object_class_install_property (gobject_class,
220 g_param_spec_double ("climb_rate",
222 _("The acceleration rate when you hold down a button"),
228 g_object_class_install_property (gobject_class,
230 g_param_spec_uint ("digits",
232 _("The number of decimal places to display"),
238 g_object_class_install_property (gobject_class,
240 g_param_spec_boolean ("snap_to_ticks",
242 _("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
246 g_object_class_install_property (gobject_class,
248 g_param_spec_boolean ("numeric",
250 _("Whether non-numeric characters should be ignored"),
254 g_object_class_install_property (gobject_class,
256 g_param_spec_boolean ("wrap",
258 _("Whether a spin button should wrap upon reaching its limits"),
262 g_object_class_install_property (gobject_class,
264 g_param_spec_enum ("update_policy",
266 _("Whether the spin button should update always, or only when the value is legal"),
267 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
271 g_object_class_install_property (gobject_class,
273 g_param_spec_double ("value",
275 _("Reads the current value, or sets a new value"),
281 gtk_widget_class_install_style_property_parser (widget_class,
282 g_param_spec_enum ("shadow_type", "Shadow Type", NULL,
283 GTK_TYPE_SHADOW_TYPE,
286 gtk_rc_property_parse_enum);
287 spinbutton_signals[INPUT] =
288 gtk_signal_new ("input",
290 GTK_CLASS_TYPE (object_class),
291 GTK_SIGNAL_OFFSET (GtkSpinButtonClass, input),
292 gtk_marshal_INT__POINTER,
293 GTK_TYPE_INT, 1, GTK_TYPE_POINTER);
295 spinbutton_signals[OUTPUT] =
296 g_signal_new ("output",
297 G_TYPE_FROM_CLASS(object_class),
299 G_STRUCT_OFFSET(GtkSpinButtonClass, output),
300 _gtk_boolean_handled_accumulator, NULL,
301 gtk_marshal_BOOLEAN__VOID,
304 spinbutton_signals[VALUE_CHANGED] =
305 gtk_signal_new ("value_changed",
307 GTK_CLASS_TYPE (object_class),
308 GTK_SIGNAL_OFFSET (GtkSpinButtonClass, value_changed),
309 gtk_marshal_VOID__VOID,
314 gtk_spin_button_editable_init (GtkEditableClass *iface)
316 iface->insert_text = gtk_spin_button_insert_text;
320 gtk_spin_button_set_property (GObject *object,
325 GtkSpinButton *spin_button;
327 spin_button = GTK_SPIN_BUTTON (object);
331 GtkAdjustment *adjustment;
333 case PROP_ADJUSTMENT:
334 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
336 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
337 gtk_spin_button_set_adjustment (spin_button, adjustment);
339 case PROP_CLIMB_RATE:
340 gtk_spin_button_configure (spin_button,
341 spin_button->adjustment,
342 g_value_get_double (value),
343 spin_button->digits);
346 gtk_spin_button_configure (spin_button,
347 spin_button->adjustment,
348 spin_button->climb_rate,
349 g_value_get_uint (value));
351 case PROP_SNAP_TO_TICKS:
352 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
355 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
358 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
360 case PROP_UPDATE_POLICY:
361 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
364 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
372 gtk_spin_button_get_property (GObject *object,
377 GtkSpinButton *spin_button;
379 spin_button = GTK_SPIN_BUTTON (object);
383 case PROP_ADJUSTMENT:
384 g_value_set_object (value, G_OBJECT (spin_button->adjustment));
386 case PROP_CLIMB_RATE:
387 g_value_set_double (value, spin_button->climb_rate);
390 g_value_set_uint (value, spin_button->digits);
392 case PROP_SNAP_TO_TICKS:
393 g_value_set_boolean (value, spin_button->snap_to_ticks);
396 g_value_set_boolean (value, spin_button->numeric);
399 g_value_set_boolean (value, spin_button->wrap);
401 case PROP_UPDATE_POLICY:
402 g_value_set_enum (value, spin_button->update_policy);
405 g_value_set_double (value, spin_button->adjustment->value);
408 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
414 gtk_spin_button_init (GtkSpinButton *spin_button)
416 spin_button->adjustment = NULL;
417 spin_button->panel = NULL;
418 spin_button->timer = 0;
419 spin_button->ev_time = 0;
420 spin_button->climb_rate = 0.0;
421 spin_button->timer_step = 0.0;
422 spin_button->update_policy = GTK_UPDATE_ALWAYS;
423 spin_button->in_child = 2;
424 spin_button->click_child = 2;
425 spin_button->button = 0;
426 spin_button->need_timer = FALSE;
427 spin_button->timer_calls = 0;
428 spin_button->digits = 0;
429 spin_button->numeric = FALSE;
430 spin_button->wrap = FALSE;
431 spin_button->snap_to_ticks = FALSE;
432 gtk_spin_button_set_adjustment (spin_button,
433 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
437 gtk_spin_button_finalize (GObject *object)
439 g_return_if_fail (GTK_IS_SPIN_BUTTON (object));
441 gtk_object_unref (GTK_OBJECT (GTK_SPIN_BUTTON (object)->adjustment));
443 G_OBJECT_CLASS (parent_class)->finalize (object);
447 gtk_spin_button_map (GtkWidget *widget)
449 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
451 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
453 GTK_WIDGET_CLASS (parent_class)->map (widget);
454 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
459 gtk_spin_button_unmap (GtkWidget *widget)
461 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
463 if (GTK_WIDGET_MAPPED (widget))
465 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
466 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
471 gtk_spin_button_realize (GtkWidget *widget)
473 GtkSpinButton *spin_button;
474 GdkWindowAttr attributes;
475 gint attributes_mask;
480 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
482 spin_button = GTK_SPIN_BUTTON (widget);
483 arrow_size = spin_button_get_arrow_size (spin_button);
485 real_width = widget->allocation.width;
486 widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
487 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
488 GDK_KEY_RELEASE_MASK);
489 GTK_WIDGET_CLASS (parent_class)->realize (widget);
491 widget->allocation.width = real_width;
493 attributes.window_type = GDK_WINDOW_CHILD;
494 attributes.wclass = GDK_INPUT_OUTPUT;
495 attributes.visual = gtk_widget_get_visual (widget);
496 attributes.colormap = gtk_widget_get_colormap (widget);
497 attributes.event_mask = gtk_widget_get_events (widget);
498 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
499 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
500 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
502 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
504 attributes.x = (widget->allocation.x +
505 widget->allocation.width - arrow_size -
506 2 * widget->style->xthickness);
507 attributes.y = widget->allocation.y + (widget->allocation.height -
508 widget->requisition.height) / 2;
509 attributes.width = arrow_size + 2 * widget->style->xthickness;
510 attributes.height = widget->requisition.height;
512 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
513 &attributes, attributes_mask);
514 gdk_window_set_user_data (spin_button->panel, widget);
516 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
519 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
521 if (return_val == FALSE)
522 gtk_spin_button_default_output (spin_button);
526 gtk_spin_button_unrealize (GtkWidget *widget)
530 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
532 spin = GTK_SPIN_BUTTON (widget);
534 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
538 gdk_window_set_user_data (spin->panel, NULL);
539 gdk_window_destroy (spin->panel);
545 compute_double_length (double val, int digits)
551 if (fabs (val) > 1.0)
552 a = floor (log10 (fabs (val))) + 1;
564 return a + digits + extra;
568 gtk_spin_button_size_request (GtkWidget *widget,
569 GtkRequisition *requisition)
572 GtkSpinButton *spin_button;
575 g_return_if_fail (requisition != NULL);
576 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
578 entry = GTK_ENTRY (widget);
579 spin_button = GTK_SPIN_BUTTON (widget);
580 arrow_size = spin_button_get_arrow_size (spin_button);
582 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
584 if (entry->width_chars < 0)
586 PangoContext *context;
587 PangoFontMetrics *metrics;
594 context = gtk_widget_get_pango_context (widget);
595 metrics = pango_context_get_metrics (context,
596 widget->style->font_desc,
597 pango_context_get_language (context));
599 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
600 digit_width = PANGO_PIXELS (digit_width);
602 pango_font_metrics_unref (metrics);
604 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
606 width = MIN_SPIN_BUTTON_WIDTH;
607 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
608 spin_button->digits));
610 string_len = compute_double_length (spin_button->adjustment->upper,
611 spin_button->digits);
612 w = MIN (string_len, max_string_len) * digit_width;
613 width = MAX (width, w);
614 string_len = compute_double_length (spin_button->adjustment->lower,
615 spin_button->digits);
616 w = MIN (string_len, max_string_len) * digit_width;
617 width = MAX (width, w);
619 requisition->width = (width + arrow_size +
620 2 * widget->style->xthickness);
623 requisition->width += arrow_size + 2 * widget->style->xthickness;
627 gtk_spin_button_size_allocate (GtkWidget *widget,
628 GtkAllocation *allocation)
631 GtkAllocation child_allocation;
634 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
635 g_return_if_fail (allocation != NULL);
637 spin = GTK_SPIN_BUTTON (widget);
638 arrow_size = spin_button_get_arrow_size (spin);
640 child_allocation = *allocation;
641 if (child_allocation.width > arrow_size + 2 * widget->style->xthickness)
642 child_allocation.width -= arrow_size + 2 * widget->style->xthickness;
644 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
645 child_allocation.x += arrow_size + 2 * widget->style->xthickness;
647 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &child_allocation);
649 widget->allocation = *allocation;
651 if (GTK_WIDGET_REALIZED (widget))
653 child_allocation.width = arrow_size + 2 * widget->style->xthickness;
654 child_allocation.height = widget->requisition.height;
656 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
657 child_allocation.x = (allocation->x + allocation->width -
658 arrow_size - 2 * widget->style->xthickness);
660 child_allocation.x = allocation->x;
662 child_allocation.y = allocation->y + (allocation->height - widget->requisition.height) / 2;
664 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
667 child_allocation.width,
668 child_allocation.height);
673 gtk_spin_button_expose (GtkWidget *widget,
674 GdkEventExpose *event)
679 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
680 g_return_val_if_fail (event != NULL, FALSE);
682 spin = GTK_SPIN_BUTTON (widget);
683 arrow_size = spin_button_get_arrow_size (spin);
685 if (GTK_WIDGET_DRAWABLE (widget))
687 GtkShadowType shadow_type;
689 /* FIXME this seems like really broken code -
690 * why aren't we looking at event->window
691 * and acting accordingly?
694 shadow_type = spin_button_get_shadow_type (spin);
695 if (shadow_type != GTK_SHADOW_NONE)
696 gtk_paint_box (widget->style, spin->panel,
697 GTK_STATE_NORMAL, shadow_type,
698 &event->area, widget, "spinbutton",
700 arrow_size + 2 * widget->style->xthickness,
701 widget->requisition.height);
704 gdk_window_set_back_pixmap (spin->panel, NULL, TRUE);
705 gdk_window_clear_area (spin->panel,
706 event->area.x, event->area.y,
707 event->area.width, event->area.height);
709 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
710 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
712 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
719 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
722 GtkShadowType spin_shadow_type;
723 GtkStateType state_type;
724 GtkShadowType shadow_type;
730 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
732 widget = GTK_WIDGET (spin_button);
733 spin_shadow_type = spin_button_get_shadow_type (spin_button);
734 arrow_size = spin_button_get_arrow_size (spin_button);
736 if (GTK_WIDGET_DRAWABLE (spin_button))
738 if (!spin_button->wrap &&
739 (((arrow == GTK_ARROW_UP &&
740 (spin_button->adjustment->upper - spin_button->adjustment->value
742 ((arrow == GTK_ARROW_DOWN &&
743 (spin_button->adjustment->value - spin_button->adjustment->lower
746 shadow_type = GTK_SHADOW_ETCHED_IN;
747 state_type = GTK_STATE_NORMAL;
751 if (spin_button->in_child == arrow)
753 if (spin_button->click_child == arrow)
754 state_type = GTK_STATE_ACTIVE;
756 state_type = GTK_STATE_PRELIGHT;
759 state_type = GTK_STATE_NORMAL;
761 if (spin_button->click_child == arrow)
762 shadow_type = GTK_SHADOW_IN;
764 shadow_type = GTK_SHADOW_OUT;
766 if (arrow == GTK_ARROW_UP)
768 if (spin_shadow_type != GTK_SHADOW_NONE)
770 x = widget->style->xthickness;
771 y = widget->style->ythickness;
775 x = widget->style->xthickness - 1;
776 y = widget->style->ythickness - 1;
778 gtk_paint_arrow (widget->style, spin_button->panel,
779 state_type, shadow_type,
780 NULL, widget, "spinbutton",
783 widget->requisition.height / 2
784 - widget->style->ythickness);
788 if (spin_shadow_type != GTK_SHADOW_NONE)
790 x = widget->style->xthickness;
791 y = widget->requisition.height / 2;
795 x = widget->style->xthickness - 1;
796 y = widget->requisition.height / 2 + 1;
798 gtk_paint_arrow (widget->style, spin_button->panel,
799 state_type, shadow_type,
800 NULL, widget, "spinbutton",
803 widget->requisition.height / 2
804 - widget->style->ythickness);
810 gtk_spin_button_enter_notify (GtkWidget *widget,
811 GdkEventCrossing *event)
815 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
816 g_return_val_if_fail (event != NULL, FALSE);
818 spin = GTK_SPIN_BUTTON (widget);
820 if (event->window == spin->panel)
825 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
827 if (y <= widget->requisition.height / 2)
829 spin->in_child = GTK_ARROW_UP;
830 if (spin->click_child == 2)
831 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
835 spin->in_child = GTK_ARROW_DOWN;
836 if (spin->click_child == 2)
837 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
844 gtk_spin_button_leave_notify (GtkWidget *widget,
845 GdkEventCrossing *event)
849 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
850 g_return_val_if_fail (event != NULL, FALSE);
852 spin = GTK_SPIN_BUTTON (widget);
854 if (event->window == spin->panel && spin->click_child == 2)
856 if (spin->in_child == GTK_ARROW_UP)
859 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
864 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
871 gtk_spin_button_focus_out (GtkWidget *widget,
872 GdkEventFocus *event)
874 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
875 g_return_val_if_fail (event != NULL, FALSE);
877 if (GTK_ENTRY (widget)->editable)
878 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
880 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
884 gtk_spin_button_scroll (GtkWidget *widget,
885 GdkEventScroll *event)
889 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
890 g_return_val_if_fail (event != NULL, FALSE);
892 spin = GTK_SPIN_BUTTON (widget);
894 if (event->direction == GDK_SCROLL_UP)
896 if (!GTK_WIDGET_HAS_FOCUS (widget))
897 gtk_widget_grab_focus (widget);
898 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
900 else if (event->direction == GDK_SCROLL_DOWN)
902 if (!GTK_WIDGET_HAS_FOCUS (widget))
903 gtk_widget_grab_focus (widget);
904 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
913 gtk_spin_button_button_press (GtkWidget *widget,
914 GdkEventButton *event)
918 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
919 g_return_val_if_fail (event != NULL, FALSE);
921 spin = GTK_SPIN_BUTTON (widget);
925 if (event->window == spin->panel)
927 if (!GTK_WIDGET_HAS_FOCUS (widget))
928 gtk_widget_grab_focus (widget);
929 gtk_grab_add (widget);
930 spin->button = event->button;
932 if (GTK_ENTRY (widget)->editable)
933 gtk_spin_button_update (spin);
935 if (event->y <= widget->requisition.height / 2)
937 spin->click_child = GTK_ARROW_UP;
938 if (event->button == 1)
940 gtk_spin_button_real_spin (spin,
941 spin->adjustment->step_increment);
944 spin->timer_step = spin->adjustment->step_increment;
945 spin->need_timer = TRUE;
946 spin->timer = gtk_timeout_add
947 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
948 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
951 else if (event->button == 2)
953 gtk_spin_button_real_spin (spin,
954 spin->adjustment->page_increment);
957 spin->timer_step = spin->adjustment->page_increment;
958 spin->need_timer = TRUE;
959 spin->timer = gtk_timeout_add
960 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
961 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
964 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
968 spin->click_child = GTK_ARROW_DOWN;
969 if (event->button == 1)
971 gtk_spin_button_real_spin (spin,
972 -spin->adjustment->step_increment);
975 spin->timer_step = spin->adjustment->step_increment;
976 spin->need_timer = TRUE;
977 spin->timer = gtk_timeout_add
978 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
979 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
982 else if (event->button == 2)
984 gtk_spin_button_real_spin (spin,
985 -spin->adjustment->page_increment);
988 spin->timer_step = spin->adjustment->page_increment;
989 spin->need_timer = TRUE;
990 spin->timer = gtk_timeout_add
991 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
992 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
995 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1000 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1006 gtk_spin_button_button_release (GtkWidget *widget,
1007 GdkEventButton *event)
1009 GtkSpinButton *spin;
1012 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1013 g_return_val_if_fail (event != NULL, FALSE);
1015 spin = GTK_SPIN_BUTTON (widget);
1016 arrow_size = spin_button_get_arrow_size (spin);
1018 if (event->button == spin->button)
1024 gtk_timeout_remove (spin->timer);
1026 spin->timer_calls = 0;
1027 spin->need_timer = FALSE;
1030 if (event->button == 3)
1032 if (event->y >= 0 && event->x >= 0 &&
1033 event->y <= widget->requisition.height &&
1034 event->x <= arrow_size + 2 * widget->style->xthickness)
1036 if (spin->click_child == GTK_ARROW_UP &&
1037 event->y <= widget->requisition.height / 2)
1041 diff = spin->adjustment->upper - spin->adjustment->value;
1043 gtk_spin_button_real_spin (spin, diff);
1045 else if (spin->click_child == GTK_ARROW_DOWN &&
1046 event->y > widget->requisition.height / 2)
1050 diff = spin->adjustment->value - spin->adjustment->lower;
1052 gtk_spin_button_real_spin (spin, -diff);
1056 gtk_grab_remove (widget);
1057 click_child = spin->click_child;
1058 spin->click_child = 2;
1060 gtk_spin_button_draw_arrow (spin, click_child);
1064 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1070 gtk_spin_button_motion_notify (GtkWidget *widget,
1071 GdkEventMotion *event)
1073 GtkSpinButton *spin;
1075 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1076 g_return_val_if_fail (event != NULL, FALSE);
1078 spin = GTK_SPIN_BUTTON (widget);
1083 if (event->window == spin->panel)
1089 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1091 if (y <= widget->requisition.height / 2 &&
1092 spin->in_child == GTK_ARROW_DOWN)
1094 spin->in_child = GTK_ARROW_UP;
1095 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1096 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1098 else if (y > widget->requisition.height / 2 &&
1099 spin->in_child == GTK_ARROW_UP)
1101 spin->in_child = GTK_ARROW_DOWN;
1102 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1103 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1108 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1112 gtk_spin_button_timer (GtkSpinButton *spin_button)
1114 gboolean retval = FALSE;
1116 GDK_THREADS_ENTER ();
1118 if (spin_button->timer)
1120 if (spin_button->click_child == GTK_ARROW_UP)
1121 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1123 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1125 if (spin_button->need_timer)
1127 spin_button->need_timer = FALSE;
1128 spin_button->timer = gtk_timeout_add
1129 (SPIN_BUTTON_TIMER_DELAY, (GtkFunction) gtk_spin_button_timer,
1130 (gpointer) spin_button);
1134 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1135 < spin_button->adjustment->page_increment)
1137 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1138 spin_button->timer_calls++;
1141 spin_button->timer_calls = 0;
1142 spin_button->timer_step += spin_button->climb_rate;
1149 GDK_THREADS_LEAVE ();
1155 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1156 GtkSpinButton *spin_button)
1160 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1163 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1165 if (return_val == FALSE)
1166 gtk_spin_button_default_output (spin_button);
1168 gtk_signal_emit (GTK_OBJECT (spin_button),
1169 spinbutton_signals[VALUE_CHANGED]);
1171 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_UP);
1172 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_DOWN);
1174 g_object_notify (G_OBJECT (spin_button), "value");
1178 gtk_spin_button_key_press (GtkWidget *widget,
1181 GtkSpinButton *spin;
1183 gboolean key_repeat = FALSE;
1184 gboolean retval = FALSE;
1186 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1187 g_return_val_if_fail (event != NULL, FALSE);
1189 spin = GTK_SPIN_BUTTON (widget);
1190 key = event->keyval;
1192 key_repeat = (event->time == spin->ev_time);
1194 if (GTK_ENTRY (widget)->editable)
1201 if (GTK_WIDGET_HAS_FOCUS (widget))
1203 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1206 spin->timer_step = spin->adjustment->step_increment;
1208 gtk_spin_button_real_spin (spin, spin->timer_step);
1212 if (spin->climb_rate > 0.0 && spin->timer_step
1213 < spin->adjustment->page_increment)
1215 if (spin->timer_calls < MAX_TIMER_CALLS)
1216 spin->timer_calls++;
1219 spin->timer_calls = 0;
1220 spin->timer_step += spin->climb_rate;
1231 if (GTK_WIDGET_HAS_FOCUS (widget))
1233 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1236 spin->timer_step = spin->adjustment->step_increment;
1238 gtk_spin_button_real_spin (spin, -spin->timer_step);
1242 if (spin->climb_rate > 0.0 && spin->timer_step
1243 < spin->adjustment->page_increment)
1245 if (spin->timer_calls < MAX_TIMER_CALLS)
1246 spin->timer_calls++;
1249 spin->timer_calls = 0;
1250 spin->timer_step += spin->climb_rate;
1258 case GDK_KP_Page_Up:
1261 if (event->state & GDK_CONTROL_MASK)
1263 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1265 gtk_spin_button_real_spin (spin, diff);
1268 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1273 case GDK_KP_Page_Down:
1276 if (event->state & GDK_CONTROL_MASK)
1278 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1280 gtk_spin_button_real_spin (spin, -diff);
1283 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1295 gtk_spin_button_update (spin);
1299 return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
1303 gtk_spin_button_key_release (GtkWidget *widget,
1306 GtkSpinButton *spin;
1308 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1310 spin = GTK_SPIN_BUTTON (widget);
1312 spin->ev_time = event->time;
1317 gtk_spin_button_snap (GtkSpinButton *spin_button,
1323 inc = spin_button->adjustment->step_increment;
1324 tmp = (val - spin_button->adjustment->lower) / inc;
1325 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1326 val = spin_button->adjustment->lower + floor (tmp) * inc;
1328 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1330 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1331 gtk_adjustment_set_value (spin_button->adjustment, val);
1334 gint return_val = FALSE;
1335 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1337 if (return_val == FALSE)
1338 gtk_spin_button_default_output (spin_button);
1343 gtk_spin_button_activate (GtkEntry *entry)
1345 if (entry->editable)
1346 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1350 gtk_spin_button_insert_text (GtkEditable *editable,
1351 const gchar *new_text,
1352 gint new_text_length,
1355 GtkEntry *entry = GTK_ENTRY (editable);
1356 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1357 GtkEditableClass *parent_editable_iface = g_type_interface_peek (parent_class, GTK_TYPE_EDITABLE);
1369 entry_length = entry->text_length;
1373 if (*(lc->negative_sign))
1374 neg_sign = *(lc->negative_sign);
1378 if (*(lc->positive_sign))
1379 pos_sign = *(lc->positive_sign);
1383 for (sign=0, i=0; i<entry_length; i++)
1384 if ((entry->text[i] == neg_sign) ||
1385 (entry->text[i] == pos_sign))
1391 if (sign && !(*position))
1394 for (dotpos=-1, i=0; i<entry_length; i++)
1395 if (entry->text[i] == *(lc->decimal_point))
1401 if (dotpos > -1 && *position > dotpos &&
1402 (gint)spin->digits - entry_length
1403 + dotpos - new_text_length + 1 < 0)
1406 for (i = 0; i < new_text_length; i++)
1408 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1410 if (sign || (*position) || i)
1414 else if (new_text[i] == *(lc->decimal_point))
1416 if (!spin->digits || dotpos > -1 ||
1417 (new_text_length - 1 - i + entry_length
1418 - *position > (gint)spin->digits))
1420 dotpos = *position + i;
1422 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1427 parent_editable_iface->insert_text (editable, new_text,
1428 new_text_length, position);
1432 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1436 gdouble new_value = 0.0;
1438 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1440 adj = spin_button->adjustment;
1442 new_value = adj->value + increment;
1446 if (spin_button->wrap)
1448 if (fabs (adj->value - adj->upper) < EPSILON)
1449 new_value = adj->lower;
1450 else if (new_value > adj->upper)
1451 new_value = adj->upper;
1454 new_value = MIN (new_value, adj->upper);
1456 else if (increment < 0)
1458 if (spin_button->wrap)
1460 if (fabs (adj->value - adj->lower) < EPSILON)
1461 new_value = adj->upper;
1462 else if (new_value < adj->lower)
1463 new_value = adj->lower;
1466 new_value = MAX (new_value, adj->lower);
1469 if (fabs (new_value - adj->value) > EPSILON)
1470 gtk_adjustment_set_value (adj, new_value);
1474 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1479 *new_val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1481 return GTK_INPUT_ERROR;
1487 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1489 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1491 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1492 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1498 /***********************************************************
1499 ***********************************************************
1500 *** Public interface ***
1501 ***********************************************************
1502 ***********************************************************/
1506 gtk_spin_button_configure (GtkSpinButton *spin_button,
1507 GtkAdjustment *adjustment,
1511 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1514 gtk_spin_button_set_adjustment (spin_button, adjustment);
1516 adjustment = spin_button->adjustment;
1518 g_object_freeze_notify (G_OBJECT (spin_button));
1519 if (spin_button->digits != digits)
1521 spin_button->digits = digits;
1522 g_object_notify (G_OBJECT (spin_button), "digits");
1525 if (spin_button->climb_rate != climb_rate)
1527 spin_button->climb_rate = climb_rate;
1528 g_object_notify (G_OBJECT (spin_button), "climb_rate");
1530 g_object_thaw_notify (G_OBJECT (spin_button));
1532 gtk_adjustment_value_changed (adjustment);
1536 gtk_spin_button_new (GtkAdjustment *adjustment,
1540 GtkSpinButton *spin;
1543 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1545 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1547 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1549 return GTK_WIDGET (spin);
1553 * gtk_spin_button_new_with_range:
1554 * @min: Minimum allowable value
1555 * @max: Maximum allowable value
1556 * @step: Increment added or subtracted by spinning the widget
1558 * This is a convenience constructor that allows creation of a numeric
1559 * #GtkSpinButton without manually creating an adjustment. The value is
1560 * initially set to the minimum value and a page increment of 10 * @step
1561 * is the default. The precision of the spin button is equivalent to the
1562 * precision of @step.
1564 * Return value: The new spin button as a #GtkWidget.
1567 gtk_spin_button_new_with_range (gdouble min,
1572 GtkSpinButton *spin;
1575 g_return_val_if_fail (min < max, NULL);
1576 g_return_val_if_fail (step != 0.0, NULL);
1578 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1580 adj = gtk_adjustment_new (min, min, max, step, 10 * step, step);
1582 if (fabs (step) >= 1.0 || step == 0.0)
1585 digits = abs ((gint) floor (log10 (fabs (step))));
1586 if (digits > MAX_DIGITS)
1587 digits = MAX_DIGITS;
1590 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1592 gtk_spin_button_set_numeric (spin, TRUE);
1594 return GTK_WIDGET (spin);
1597 /* Callback used when the spin button's adjustment changes. We need to redraw
1598 * the arrows when the adjustment's range changes, and reevaluate our size request.
1601 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1603 GtkSpinButton *spin_button;
1605 spin_button = GTK_SPIN_BUTTON (data);
1607 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1611 * gtk_spin_button_set_adjustment:
1612 * @spin_button: a #GtkSpinButton
1613 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1615 * Replaces the #GtkAdjustment associated with @spin_button.
1618 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1619 GtkAdjustment *adjustment)
1621 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1623 if (spin_button->adjustment != adjustment)
1625 if (spin_button->adjustment)
1627 gtk_signal_disconnect_by_data (GTK_OBJECT (spin_button->adjustment),
1628 (gpointer) spin_button);
1629 gtk_object_unref (GTK_OBJECT (spin_button->adjustment));
1631 spin_button->adjustment = adjustment;
1634 gtk_object_ref (GTK_OBJECT (adjustment));
1635 gtk_object_sink (GTK_OBJECT (adjustment));
1636 gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
1637 (GtkSignalFunc) gtk_spin_button_value_changed,
1638 (gpointer) spin_button);
1639 gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
1640 (GtkSignalFunc) adjustment_changed_cb,
1641 (gpointer) spin_button);
1644 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1647 g_object_notify (G_OBJECT (spin_button), "adjustment");
1651 * gtk_spin_button_get_adjustment:
1654 * Get the adjustment associated with a #GtkSpinButton
1656 * Return value: the #GtkAdjustment of @spin_button
1659 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1661 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1663 return spin_button->adjustment;
1667 * gtk_spin_button_set_digits:
1668 * @spin_button: a #GtkSpinButton
1669 * @digits: the number of digits to be displayed for the spin button's value
1671 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1675 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1678 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1680 if (spin_button->digits != digits)
1682 spin_button->digits = digits;
1683 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1684 g_object_notify (G_OBJECT (spin_button), "digits");
1686 /* since lower/upper may have changed */
1687 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1692 * gtk_spin_button_get_digits:
1693 * @spin_button: a #GtkSpinButton
1695 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1697 * Returns: the current precision
1700 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1702 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1704 return spin_button->digits;
1708 * gtk_spin_button_set_increments:
1709 * @spin_button: a #GtkSpinButton
1710 * @step: increment applied for a button 1 press.
1711 * @page: increment applied for a button 2 press.
1713 * Sets the step and page increments for spin_button. This affects how
1714 * quickly the value changes when the spin button's arrows are activated.
1717 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1721 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1723 spin_button->adjustment->step_increment = step;
1724 spin_button->adjustment->page_increment = page;
1728 * gtk_spin_button_get_increments:
1729 * @spin_button: a #GtkSpinButton
1730 * @step: location to store step increment, or %NULL
1731 * @page: location to store page increment, or %NULL
1733 * Gets the current step and page the increments used by @spin_button. See
1734 * gtk_spin_button_set_increments().
1737 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1741 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1744 *step = spin_button->adjustment->step_increment;
1746 *page = spin_button->adjustment->page_increment;
1750 * gtk_spin_button_set_range:
1751 * @spin_button: a #GtkSpinButton
1752 * @min: minimum allowable value
1753 * @max: maximum allowable value
1755 * Sets the minimum and maximum allowable values for @spin_button
1758 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1764 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1766 spin_button->adjustment->lower = min;
1767 spin_button->adjustment->upper = max;
1769 value = CLAMP (spin_button->adjustment->value,
1770 spin_button->adjustment->lower,
1771 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1773 if (value != spin_button->adjustment->value)
1774 gtk_spin_button_set_value (spin_button, value);
1776 gtk_adjustment_changed (spin_button->adjustment);
1780 * gtk_spin_button_get_range:
1781 * @spin_button: a #GtkSpinButton
1782 * @min: location to store minimum allowed value, or %NULL
1783 * @max: location to store maximum allowed value, or %NULL
1785 * Gets the range allowed for @spin_button. See
1786 * gtk_spin_button_set_range().
1789 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1793 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1796 *min = spin_button->adjustment->lower;
1798 *max = spin_button->adjustment->upper;
1802 * gtk_spin_button_get_value:
1803 * @spin_button: a #GtkSpinButton
1805 * Get the value in the @spin_button.
1807 * Return value: the value of @spin_button
1810 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1812 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1814 return spin_button->adjustment->value;
1818 * gtk_spin_button_get_value_as_int:
1819 * @spin_button: a #GtkSpinButton
1821 * Get the value @spin_button represented as an integer.
1823 * Return value: the value of @spin_button
1826 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1830 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1832 val = spin_button->adjustment->value;
1833 if (val - floor (val) < ceil (val) - val)
1840 * gtk_spin_button_set_value:
1841 * @spin_button: a #GtkSpinButton
1842 * @value: the new value
1844 * Set the value of @spin_button.
1847 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1850 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1852 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1853 gtk_adjustment_set_value (spin_button->adjustment, value);
1856 gint return_val = FALSE;
1857 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1859 if (return_val == FALSE)
1860 gtk_spin_button_default_output (spin_button);
1865 * gtk_spin_button_set_update_policy:
1866 * @spin_button: a #GtkSpinButton
1867 * @policy: a #GtkSpinButtonUpdatePolicy value
1869 * Sets the update behavior of a spin button. This determines whether the
1870 * spin button is always updated or only when a valid value is set.
1873 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1874 GtkSpinButtonUpdatePolicy policy)
1876 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1878 if (spin_button->update_policy != policy)
1880 spin_button->update_policy = policy;
1881 g_object_notify (G_OBJECT (spin_button), "update_policy");
1886 * gtk_spin_button_get_update_policy:
1887 * @spin_button: a #GtkSpinButton
1889 * Gets the update behavior of a spin button. See
1890 * gtk_spin_button_set_update_policy().
1892 * Return value: the current update policy
1894 GtkSpinButtonUpdatePolicy
1895 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1897 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1899 return spin_button->update_policy;
1903 * gtk_spin_button_set_numeric:
1904 * @spin_button: a #GtkSpinButton
1905 * @numeric: flag indicating if only numeric entry is allowed.
1907 * Sets the flag that determines if non-numeric text can be typed into
1911 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1914 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1916 spin_button->numeric = (numeric != 0);
1918 g_object_notify (G_OBJECT (spin_button), "numeric");
1922 * gtk_spin_button_get_numeric:
1923 * @spin_button: a #GtkSpinButton
1925 * Returns whether non-numeric text can be typed into the spin button.
1926 * See gtk_spin_button_set_numeric().
1928 * Return value: %TRUE if only numeric text can be entered
1931 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1933 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1935 return spin_button->numeric;
1939 * gtk_spin_button_set_wrap:
1940 * @spin_button: a #GtkSpinButton
1941 * @wrap: a flag indicating if wrapping behavior is performed.
1943 * Sets the flag that determines if a spin button value wraps around to the
1944 * opposite limit when the upper or lower limit of the range is exceeded.
1947 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1950 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1952 spin_button->wrap = (wrap != 0);
1954 g_object_notify (G_OBJECT (spin_button), "wrap");
1958 * gtk_spin_button_get_wrap:
1959 * @spin_button: a #GtkSpinButton
1961 * Returns whether the spin button's value wraps around to the
1962 * opposite limit when the upper or lower limit of the range is
1963 * exceeded. See gtk_spin_button_set_wrap().
1965 * Return value: %TRUE if the spin button wraps around
1968 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
1970 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1972 return spin_button->wrap;
1976 spin_button_get_arrow_size (GtkSpinButton *spin_button)
1978 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
1980 return PANGO_PIXELS (size);
1984 * spin_button_get_shadow_type:
1985 * @spin_button: a #GtkSpinButton
1987 * Convenience function to Get the shadow type from the underlying widget's
1990 * Return value: the #GtkShadowType
1993 spin_button_get_shadow_type (GtkSpinButton *spin_button)
1995 GtkShadowType rc_shadow_type;
1997 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
1999 return rc_shadow_type;
2003 * gtk_spin_button_set_snap_to_ticks:
2004 * @spin_button: a #GtkSpinButton
2005 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2007 * Sets the policy as to whether values are corrected to the nearest step
2008 * increment when a spin button is activated after providing an invalid value.
2011 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2012 gboolean snap_to_ticks)
2016 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2018 new_val = (snap_to_ticks != 0);
2020 if (new_val != spin_button->snap_to_ticks)
2022 spin_button->snap_to_ticks = new_val;
2023 if (new_val && GTK_ENTRY (spin_button)->editable)
2024 gtk_spin_button_update (spin_button);
2026 g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
2031 * gtk_spin_button_get_snap_to_ticks:
2032 * @spin_button: a #GtkSpinButton
2034 * Returns whether the values are corrected to the nearest step. See
2035 * gtk_spin_button_set_snap_to_ticks().
2037 * Return value: %TRUE if values are snapped to the nearest step.
2040 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2042 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2044 return spin_button->snap_to_ticks;
2048 * gtk_spin_button_spin:
2049 * @spin_button: a #GtkSpinButton
2050 * @direction: a #GtkSpinType indicating the direction to spin.
2051 * @increment: step increment to apply in the specified direction.
2053 * Increment or decrement a spin button's value in a specified direction
2054 * by a specified amount.
2057 gtk_spin_button_spin (GtkSpinButton *spin_button,
2058 GtkSpinType direction,
2064 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2066 adj = spin_button->adjustment;
2068 /* for compatibility with the 1.0.x version of this function */
2069 if (increment != 0 && increment != adj->step_increment &&
2070 (direction == GTK_SPIN_STEP_FORWARD ||
2071 direction == GTK_SPIN_STEP_BACKWARD))
2073 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2074 increment = -increment;
2075 direction = GTK_SPIN_USER_DEFINED;
2080 case GTK_SPIN_STEP_FORWARD:
2082 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2085 case GTK_SPIN_STEP_BACKWARD:
2087 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2090 case GTK_SPIN_PAGE_FORWARD:
2092 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2095 case GTK_SPIN_PAGE_BACKWARD:
2097 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2102 diff = adj->value - adj->lower;
2104 gtk_spin_button_real_spin (spin_button, -diff);
2109 diff = adj->upper - adj->value;
2111 gtk_spin_button_real_spin (spin_button, diff);
2114 case GTK_SPIN_USER_DEFINED:
2117 gtk_spin_button_real_spin (spin_button, increment);
2126 * gtk_spin_button_update:
2127 * @spin_button: a #GtkSpinButton
2129 * Manually force an update of the spin button.
2132 gtk_spin_button_update (GtkSpinButton *spin_button)
2138 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2141 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[INPUT],
2143 if (return_val == FALSE)
2145 return_val = gtk_spin_button_default_input (spin_button, &val);
2146 error = (return_val == GTK_INPUT_ERROR);
2148 else if (return_val == GTK_INPUT_ERROR)
2151 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2153 if (val < spin_button->adjustment->lower)
2154 val = spin_button->adjustment->lower;
2155 else if (val > spin_button->adjustment->upper)
2156 val = spin_button->adjustment->upper;
2158 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2160 val < spin_button->adjustment->lower ||
2161 val > spin_button->adjustment->upper))
2163 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2167 if (spin_button->snap_to_ticks)
2168 gtk_spin_button_snap (spin_button, val);
2171 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2172 gtk_adjustment_set_value (spin_button->adjustment, val);
2176 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
2178 if (return_val == FALSE)
2179 gtk_spin_button_default_output (spin_button);