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_set_property (GObject *object,
79 static void gtk_spin_button_get_property (GObject *object,
83 static void gtk_spin_button_map (GtkWidget *widget);
84 static void gtk_spin_button_unmap (GtkWidget *widget);
85 static void gtk_spin_button_realize (GtkWidget *widget);
86 static void gtk_spin_button_unrealize (GtkWidget *widget);
87 static void gtk_spin_button_size_request (GtkWidget *widget,
88 GtkRequisition *requisition);
89 static void gtk_spin_button_size_allocate (GtkWidget *widget,
90 GtkAllocation *allocation);
91 static gint gtk_spin_button_expose (GtkWidget *widget,
92 GdkEventExpose *event);
93 static gint gtk_spin_button_button_press (GtkWidget *widget,
94 GdkEventButton *event);
95 static gint gtk_spin_button_button_release (GtkWidget *widget,
96 GdkEventButton *event);
97 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
98 GdkEventMotion *event);
99 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
100 GdkEventCrossing *event);
101 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
102 GdkEventCrossing *event);
103 static gint gtk_spin_button_focus_out (GtkWidget *widget,
104 GdkEventFocus *event);
105 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
107 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
108 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
109 GtkSpinButton *spin_button);
110 static gint gtk_spin_button_key_press (GtkWidget *widget,
112 static gint gtk_spin_button_key_release (GtkWidget *widget,
114 static gint gtk_spin_button_scroll (GtkWidget *widget,
115 GdkEventScroll *event);
116 static void gtk_spin_button_activate (GtkEntry *entry);
117 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
119 static void gtk_spin_button_insert_text (GtkEditable *editable,
120 const gchar *new_text,
121 gint new_text_length,
123 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
125 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
127 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
128 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
129 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
132 static GtkEntryClass *parent_class = NULL;
133 static guint spinbutton_signals[LAST_SIGNAL] = {0};
137 gtk_spin_button_get_type (void)
139 static GtkType spin_button_type = 0;
141 if (!spin_button_type)
143 static const GtkTypeInfo spin_button_info =
146 sizeof (GtkSpinButton),
147 sizeof (GtkSpinButtonClass),
148 (GtkClassInitFunc) gtk_spin_button_class_init,
149 (GtkObjectInitFunc) gtk_spin_button_init,
150 /* reserved_1 */ NULL,
151 /* reserved_2 */ NULL,
152 (GtkClassInitFunc) NULL,
155 static const GInterfaceInfo editable_info =
157 (GInterfaceInitFunc) gtk_spin_button_editable_init, /* interface_init */
158 NULL, /* interface_finalize */
159 NULL /* interface_data */
162 spin_button_type = gtk_type_unique (GTK_TYPE_ENTRY, &spin_button_info);
163 g_type_add_interface_static (spin_button_type,
167 return spin_button_type;
171 gtk_spin_button_class_init (GtkSpinButtonClass *class)
173 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
174 GtkObjectClass *object_class;
175 GtkWidgetClass *widget_class;
176 GtkEntryClass *entry_class;
178 object_class = (GtkObjectClass*) class;
179 widget_class = (GtkWidgetClass*) class;
180 entry_class = (GtkEntryClass*) class;
182 parent_class = gtk_type_class (GTK_TYPE_ENTRY);
184 gobject_class->finalize = gtk_spin_button_finalize;
186 gobject_class->set_property = gtk_spin_button_set_property;
187 gobject_class->get_property = gtk_spin_button_get_property;
189 widget_class->map = gtk_spin_button_map;
190 widget_class->unmap = gtk_spin_button_unmap;
191 widget_class->realize = gtk_spin_button_realize;
192 widget_class->unrealize = gtk_spin_button_unrealize;
193 widget_class->size_request = gtk_spin_button_size_request;
194 widget_class->size_allocate = gtk_spin_button_size_allocate;
195 widget_class->expose_event = gtk_spin_button_expose;
196 widget_class->scroll_event = gtk_spin_button_scroll;
197 widget_class->button_press_event = gtk_spin_button_button_press;
198 widget_class->button_release_event = gtk_spin_button_button_release;
199 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
200 widget_class->key_press_event = gtk_spin_button_key_press;
201 widget_class->key_release_event = gtk_spin_button_key_release;
202 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
203 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
204 widget_class->focus_out_event = gtk_spin_button_focus_out;
206 entry_class->activate = gtk_spin_button_activate;
209 class->output = NULL;
211 g_object_class_install_property (gobject_class,
213 g_param_spec_object ("adjustment",
215 _("The adjustment that holds the value of the spinbutton"),
219 g_object_class_install_property (gobject_class,
221 g_param_spec_double ("climb_rate",
223 _("The acceleration rate when you hold down a button"),
229 g_object_class_install_property (gobject_class,
231 g_param_spec_uint ("digits",
233 _("The number of decimal places to display"),
239 g_object_class_install_property (gobject_class,
241 g_param_spec_boolean ("snap_to_ticks",
243 _("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
247 g_object_class_install_property (gobject_class,
249 g_param_spec_boolean ("numeric",
251 _("Whether non-numeric characters should be ignored"),
255 g_object_class_install_property (gobject_class,
257 g_param_spec_boolean ("wrap",
259 _("Whether a spin button should wrap upon reaching its limits"),
263 g_object_class_install_property (gobject_class,
265 g_param_spec_enum ("update_policy",
267 _("Whether the spin button should update always, or only when the value is legal"),
268 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
272 g_object_class_install_property (gobject_class,
274 g_param_spec_double ("value",
276 _("Reads the current value, or sets a new value"),
282 gtk_widget_class_install_style_property_parser (widget_class,
283 g_param_spec_enum ("shadow_type", "Shadow Type", NULL,
284 GTK_TYPE_SHADOW_TYPE,
287 gtk_rc_property_parse_enum);
288 spinbutton_signals[INPUT] =
289 gtk_signal_new ("input",
291 GTK_CLASS_TYPE (object_class),
292 GTK_SIGNAL_OFFSET (GtkSpinButtonClass, input),
293 _gtk_marshal_INT__POINTER,
294 GTK_TYPE_INT, 1, GTK_TYPE_POINTER);
296 spinbutton_signals[OUTPUT] =
297 g_signal_new ("output",
298 G_TYPE_FROM_CLASS(object_class),
300 G_STRUCT_OFFSET(GtkSpinButtonClass, output),
301 _gtk_boolean_handled_accumulator, NULL,
302 _gtk_marshal_BOOLEAN__VOID,
305 spinbutton_signals[VALUE_CHANGED] =
306 gtk_signal_new ("value_changed",
308 GTK_CLASS_TYPE (object_class),
309 GTK_SIGNAL_OFFSET (GtkSpinButtonClass, value_changed),
310 _gtk_marshal_VOID__VOID,
315 gtk_spin_button_editable_init (GtkEditableClass *iface)
317 iface->insert_text = gtk_spin_button_insert_text;
321 gtk_spin_button_set_property (GObject *object,
326 GtkSpinButton *spin_button;
328 spin_button = GTK_SPIN_BUTTON (object);
332 GtkAdjustment *adjustment;
334 case PROP_ADJUSTMENT:
335 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
337 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
338 gtk_spin_button_set_adjustment (spin_button, adjustment);
340 case PROP_CLIMB_RATE:
341 gtk_spin_button_configure (spin_button,
342 spin_button->adjustment,
343 g_value_get_double (value),
344 spin_button->digits);
347 gtk_spin_button_configure (spin_button,
348 spin_button->adjustment,
349 spin_button->climb_rate,
350 g_value_get_uint (value));
352 case PROP_SNAP_TO_TICKS:
353 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
356 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
359 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
361 case PROP_UPDATE_POLICY:
362 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
365 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
373 gtk_spin_button_get_property (GObject *object,
378 GtkSpinButton *spin_button;
380 spin_button = GTK_SPIN_BUTTON (object);
384 case PROP_ADJUSTMENT:
385 g_value_set_object (value, G_OBJECT (spin_button->adjustment));
387 case PROP_CLIMB_RATE:
388 g_value_set_double (value, spin_button->climb_rate);
391 g_value_set_uint (value, spin_button->digits);
393 case PROP_SNAP_TO_TICKS:
394 g_value_set_boolean (value, spin_button->snap_to_ticks);
397 g_value_set_boolean (value, spin_button->numeric);
400 g_value_set_boolean (value, spin_button->wrap);
402 case PROP_UPDATE_POLICY:
403 g_value_set_enum (value, spin_button->update_policy);
406 g_value_set_double (value, spin_button->adjustment->value);
409 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
415 gtk_spin_button_init (GtkSpinButton *spin_button)
417 spin_button->adjustment = NULL;
418 spin_button->panel = NULL;
419 spin_button->timer = 0;
420 spin_button->ev_time = 0;
421 spin_button->climb_rate = 0.0;
422 spin_button->timer_step = 0.0;
423 spin_button->update_policy = GTK_UPDATE_ALWAYS;
424 spin_button->in_child = 2;
425 spin_button->click_child = 2;
426 spin_button->button = 0;
427 spin_button->need_timer = FALSE;
428 spin_button->timer_calls = 0;
429 spin_button->digits = 0;
430 spin_button->numeric = FALSE;
431 spin_button->wrap = FALSE;
432 spin_button->snap_to_ticks = FALSE;
433 gtk_spin_button_set_adjustment (spin_button,
434 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
438 gtk_spin_button_finalize (GObject *object)
440 g_return_if_fail (GTK_IS_SPIN_BUTTON (object));
442 gtk_object_unref (GTK_OBJECT (GTK_SPIN_BUTTON (object)->adjustment));
444 G_OBJECT_CLASS (parent_class)->finalize (object);
448 gtk_spin_button_map (GtkWidget *widget)
450 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
452 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
454 GTK_WIDGET_CLASS (parent_class)->map (widget);
455 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
460 gtk_spin_button_unmap (GtkWidget *widget)
462 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
464 if (GTK_WIDGET_MAPPED (widget))
466 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
467 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
472 gtk_spin_button_realize (GtkWidget *widget)
474 GtkSpinButton *spin_button;
475 GdkWindowAttr attributes;
476 gint attributes_mask;
481 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
483 spin_button = GTK_SPIN_BUTTON (widget);
484 arrow_size = spin_button_get_arrow_size (spin_button);
486 real_width = widget->allocation.width;
487 widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
488 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
489 GDK_KEY_RELEASE_MASK);
490 GTK_WIDGET_CLASS (parent_class)->realize (widget);
492 widget->allocation.width = real_width;
494 attributes.window_type = GDK_WINDOW_CHILD;
495 attributes.wclass = GDK_INPUT_OUTPUT;
496 attributes.visual = gtk_widget_get_visual (widget);
497 attributes.colormap = gtk_widget_get_colormap (widget);
498 attributes.event_mask = gtk_widget_get_events (widget);
499 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
500 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
501 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
503 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
505 attributes.x = (widget->allocation.x +
506 widget->allocation.width - arrow_size -
507 2 * widget->style->xthickness);
508 attributes.y = widget->allocation.y + (widget->allocation.height -
509 widget->requisition.height) / 2;
510 attributes.width = arrow_size + 2 * widget->style->xthickness;
511 attributes.height = widget->requisition.height;
513 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
514 &attributes, attributes_mask);
515 gdk_window_set_user_data (spin_button->panel, widget);
517 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
520 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
522 if (return_val == FALSE)
523 gtk_spin_button_default_output (spin_button);
527 gtk_spin_button_unrealize (GtkWidget *widget)
531 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
533 spin = GTK_SPIN_BUTTON (widget);
535 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
539 gdk_window_set_user_data (spin->panel, NULL);
540 gdk_window_destroy (spin->panel);
546 compute_double_length (double val, int digits)
552 if (fabs (val) > 1.0)
553 a = floor (log10 (fabs (val))) + 1;
565 return a + digits + extra;
569 gtk_spin_button_size_request (GtkWidget *widget,
570 GtkRequisition *requisition)
573 GtkSpinButton *spin_button;
576 g_return_if_fail (requisition != NULL);
577 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
579 entry = GTK_ENTRY (widget);
580 spin_button = GTK_SPIN_BUTTON (widget);
581 arrow_size = spin_button_get_arrow_size (spin_button);
583 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
585 if (entry->width_chars < 0)
587 PangoContext *context;
588 PangoFontMetrics *metrics;
594 gboolean interior_focus;
597 gtk_widget_style_get (widget,
598 "interior-focus", &interior_focus,
599 "focus-line-width", &focus_width,
602 context = gtk_widget_get_pango_context (widget);
603 metrics = pango_context_get_metrics (context,
604 widget->style->font_desc,
605 pango_context_get_language (context));
607 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
608 digit_width = PANGO_PIXELS (digit_width);
610 pango_font_metrics_unref (metrics);
612 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
614 width = MIN_SPIN_BUTTON_WIDTH;
615 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
616 spin_button->digits));
618 string_len = compute_double_length (spin_button->adjustment->upper,
619 spin_button->digits);
620 w = MIN (string_len, max_string_len) * digit_width;
621 width = MAX (width, w);
622 string_len = compute_double_length (spin_button->adjustment->lower,
623 spin_button->digits);
624 w = MIN (string_len, max_string_len) * digit_width;
625 width = MAX (width, w);
627 requisition->width = width + arrow_size + 2 * widget->style->xthickness;
629 requisition->width += 2 * focus_width;
633 requisition->width += arrow_size + 2 * widget->style->xthickness;
638 gtk_spin_button_size_allocate (GtkWidget *widget,
639 GtkAllocation *allocation)
642 GtkAllocation child_allocation;
645 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
646 g_return_if_fail (allocation != NULL);
648 spin = GTK_SPIN_BUTTON (widget);
649 arrow_size = spin_button_get_arrow_size (spin);
651 child_allocation = *allocation;
652 if (child_allocation.width > arrow_size + 2 * widget->style->xthickness)
653 child_allocation.width -= arrow_size + 2 * widget->style->xthickness;
655 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
656 child_allocation.x += arrow_size + 2 * widget->style->xthickness;
658 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &child_allocation);
660 widget->allocation = *allocation;
662 if (GTK_WIDGET_REALIZED (widget))
664 child_allocation.width = arrow_size + 2 * widget->style->xthickness;
665 child_allocation.height = widget->requisition.height;
667 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
668 child_allocation.x = (allocation->x + allocation->width -
669 arrow_size - 2 * widget->style->xthickness);
671 child_allocation.x = allocation->x;
673 child_allocation.y = allocation->y + (allocation->height - widget->requisition.height) / 2;
675 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
678 child_allocation.width,
679 child_allocation.height);
684 gtk_spin_button_expose (GtkWidget *widget,
685 GdkEventExpose *event)
690 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
691 g_return_val_if_fail (event != NULL, FALSE);
693 spin = GTK_SPIN_BUTTON (widget);
694 arrow_size = spin_button_get_arrow_size (spin);
696 if (GTK_WIDGET_DRAWABLE (widget))
698 GtkShadowType shadow_type;
700 /* FIXME this seems like really broken code -
701 * why aren't we looking at event->window
702 * and acting accordingly?
705 shadow_type = spin_button_get_shadow_type (spin);
706 if (shadow_type != GTK_SHADOW_NONE)
707 gtk_paint_box (widget->style, spin->panel,
708 GTK_STATE_NORMAL, shadow_type,
709 &event->area, widget, "spinbutton",
711 arrow_size + 2 * widget->style->xthickness,
712 widget->requisition.height);
715 gdk_window_set_back_pixmap (spin->panel, NULL, TRUE);
716 gdk_window_clear_area (spin->panel,
717 event->area.x, event->area.y,
718 event->area.width, event->area.height);
720 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
721 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
723 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
730 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
733 GtkShadowType spin_shadow_type;
734 GtkStateType state_type;
735 GtkShadowType shadow_type;
741 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
743 widget = GTK_WIDGET (spin_button);
744 spin_shadow_type = spin_button_get_shadow_type (spin_button);
745 arrow_size = spin_button_get_arrow_size (spin_button);
747 if (GTK_WIDGET_DRAWABLE (spin_button))
749 if (!spin_button->wrap &&
750 (((arrow == GTK_ARROW_UP &&
751 (spin_button->adjustment->upper - spin_button->adjustment->value
753 ((arrow == GTK_ARROW_DOWN &&
754 (spin_button->adjustment->value - spin_button->adjustment->lower
757 shadow_type = GTK_SHADOW_ETCHED_IN;
758 state_type = GTK_STATE_NORMAL;
762 if (spin_button->in_child == arrow)
764 if (spin_button->click_child == arrow)
765 state_type = GTK_STATE_ACTIVE;
767 state_type = GTK_STATE_PRELIGHT;
770 state_type = GTK_STATE_NORMAL;
772 if (spin_button->click_child == arrow)
773 shadow_type = GTK_SHADOW_IN;
775 shadow_type = GTK_SHADOW_OUT;
777 if (arrow == GTK_ARROW_UP)
779 if (spin_shadow_type != GTK_SHADOW_NONE)
781 x = widget->style->xthickness;
782 y = widget->style->ythickness;
786 x = widget->style->xthickness - 1;
787 y = widget->style->ythickness - 1;
789 gtk_paint_arrow (widget->style, spin_button->panel,
790 state_type, shadow_type,
791 NULL, widget, "spinbutton",
794 widget->requisition.height / 2
795 - widget->style->ythickness);
799 if (spin_shadow_type != GTK_SHADOW_NONE)
801 x = widget->style->xthickness;
802 y = widget->requisition.height / 2;
806 x = widget->style->xthickness - 1;
807 y = widget->requisition.height / 2 + 1;
809 gtk_paint_arrow (widget->style, spin_button->panel,
810 state_type, shadow_type,
811 NULL, widget, "spinbutton",
814 widget->requisition.height / 2
815 - widget->style->ythickness);
821 gtk_spin_button_enter_notify (GtkWidget *widget,
822 GdkEventCrossing *event)
826 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
827 g_return_val_if_fail (event != NULL, FALSE);
829 spin = GTK_SPIN_BUTTON (widget);
831 if (event->window == spin->panel)
836 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
838 if (y <= widget->requisition.height / 2)
840 spin->in_child = GTK_ARROW_UP;
841 if (spin->click_child == 2)
842 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
846 spin->in_child = GTK_ARROW_DOWN;
847 if (spin->click_child == 2)
848 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
855 gtk_spin_button_leave_notify (GtkWidget *widget,
856 GdkEventCrossing *event)
860 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
861 g_return_val_if_fail (event != NULL, FALSE);
863 spin = GTK_SPIN_BUTTON (widget);
865 if (event->window == spin->panel && spin->click_child == 2)
867 if (spin->in_child == GTK_ARROW_UP)
870 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
875 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
882 gtk_spin_button_focus_out (GtkWidget *widget,
883 GdkEventFocus *event)
885 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
886 g_return_val_if_fail (event != NULL, FALSE);
888 if (GTK_ENTRY (widget)->editable)
889 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
891 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
895 gtk_spin_button_scroll (GtkWidget *widget,
896 GdkEventScroll *event)
900 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
901 g_return_val_if_fail (event != NULL, FALSE);
903 spin = GTK_SPIN_BUTTON (widget);
905 if (event->direction == GDK_SCROLL_UP)
907 if (!GTK_WIDGET_HAS_FOCUS (widget))
908 gtk_widget_grab_focus (widget);
909 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
911 else if (event->direction == GDK_SCROLL_DOWN)
913 if (!GTK_WIDGET_HAS_FOCUS (widget))
914 gtk_widget_grab_focus (widget);
915 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
924 gtk_spin_button_button_press (GtkWidget *widget,
925 GdkEventButton *event)
929 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
930 g_return_val_if_fail (event != NULL, FALSE);
932 spin = GTK_SPIN_BUTTON (widget);
936 if (event->window == spin->panel)
938 if (!GTK_WIDGET_HAS_FOCUS (widget))
939 gtk_widget_grab_focus (widget);
940 gtk_grab_add (widget);
941 spin->button = event->button;
943 if (GTK_ENTRY (widget)->editable)
944 gtk_spin_button_update (spin);
946 if (event->y <= widget->requisition.height / 2)
948 spin->click_child = GTK_ARROW_UP;
949 if (event->button == 1)
951 gtk_spin_button_real_spin (spin,
952 spin->adjustment->step_increment);
955 spin->timer_step = spin->adjustment->step_increment;
956 spin->need_timer = TRUE;
957 spin->timer = gtk_timeout_add
958 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
959 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
962 else if (event->button == 2)
964 gtk_spin_button_real_spin (spin,
965 spin->adjustment->page_increment);
968 spin->timer_step = spin->adjustment->page_increment;
969 spin->need_timer = TRUE;
970 spin->timer = gtk_timeout_add
971 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
972 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
975 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
979 spin->click_child = GTK_ARROW_DOWN;
980 if (event->button == 1)
982 gtk_spin_button_real_spin (spin,
983 -spin->adjustment->step_increment);
986 spin->timer_step = spin->adjustment->step_increment;
987 spin->need_timer = TRUE;
988 spin->timer = gtk_timeout_add
989 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
990 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
993 else if (event->button == 2)
995 gtk_spin_button_real_spin (spin,
996 -spin->adjustment->page_increment);
999 spin->timer_step = spin->adjustment->page_increment;
1000 spin->need_timer = TRUE;
1001 spin->timer = gtk_timeout_add
1002 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
1003 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
1006 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1011 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1017 gtk_spin_button_button_release (GtkWidget *widget,
1018 GdkEventButton *event)
1020 GtkSpinButton *spin;
1023 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1024 g_return_val_if_fail (event != NULL, FALSE);
1026 spin = GTK_SPIN_BUTTON (widget);
1027 arrow_size = spin_button_get_arrow_size (spin);
1029 if (event->button == spin->button)
1035 gtk_timeout_remove (spin->timer);
1037 spin->timer_calls = 0;
1038 spin->need_timer = FALSE;
1041 if (event->button == 3)
1043 if (event->y >= 0 && event->x >= 0 &&
1044 event->y <= widget->requisition.height &&
1045 event->x <= arrow_size + 2 * widget->style->xthickness)
1047 if (spin->click_child == GTK_ARROW_UP &&
1048 event->y <= widget->requisition.height / 2)
1052 diff = spin->adjustment->upper - spin->adjustment->value;
1054 gtk_spin_button_real_spin (spin, diff);
1056 else if (spin->click_child == GTK_ARROW_DOWN &&
1057 event->y > widget->requisition.height / 2)
1061 diff = spin->adjustment->value - spin->adjustment->lower;
1063 gtk_spin_button_real_spin (spin, -diff);
1067 gtk_grab_remove (widget);
1068 click_child = spin->click_child;
1069 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);