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;
595 context = gtk_widget_get_pango_context (widget);
596 metrics = pango_context_get_metrics (context,
597 widget->style->font_desc,
598 pango_context_get_language (context));
600 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
601 digit_width = PANGO_PIXELS (digit_width);
603 pango_font_metrics_unref (metrics);
605 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
607 width = MIN_SPIN_BUTTON_WIDTH;
608 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
609 spin_button->digits));
611 string_len = compute_double_length (spin_button->adjustment->upper,
612 spin_button->digits);
613 w = MIN (string_len, max_string_len) * digit_width;
614 width = MAX (width, w);
615 string_len = compute_double_length (spin_button->adjustment->lower,
616 spin_button->digits);
617 w = MIN (string_len, max_string_len) * digit_width;
618 width = MAX (width, w);
620 requisition->width = (width + arrow_size +
621 2 * widget->style->xthickness);
624 requisition->width += arrow_size + 2 * widget->style->xthickness;
628 gtk_spin_button_size_allocate (GtkWidget *widget,
629 GtkAllocation *allocation)
632 GtkAllocation child_allocation;
635 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
636 g_return_if_fail (allocation != NULL);
638 spin = GTK_SPIN_BUTTON (widget);
639 arrow_size = spin_button_get_arrow_size (spin);
641 child_allocation = *allocation;
642 if (child_allocation.width > arrow_size + 2 * widget->style->xthickness)
643 child_allocation.width -= arrow_size + 2 * widget->style->xthickness;
645 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
646 child_allocation.x += arrow_size + 2 * widget->style->xthickness;
648 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &child_allocation);
650 widget->allocation = *allocation;
652 if (GTK_WIDGET_REALIZED (widget))
654 child_allocation.width = arrow_size + 2 * widget->style->xthickness;
655 child_allocation.height = widget->requisition.height;
657 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
658 child_allocation.x = (allocation->x + allocation->width -
659 arrow_size - 2 * widget->style->xthickness);
661 child_allocation.x = allocation->x;
663 child_allocation.y = allocation->y + (allocation->height - widget->requisition.height) / 2;
665 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
668 child_allocation.width,
669 child_allocation.height);
674 gtk_spin_button_expose (GtkWidget *widget,
675 GdkEventExpose *event)
680 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
681 g_return_val_if_fail (event != NULL, FALSE);
683 spin = GTK_SPIN_BUTTON (widget);
684 arrow_size = spin_button_get_arrow_size (spin);
686 if (GTK_WIDGET_DRAWABLE (widget))
688 GtkShadowType shadow_type;
690 /* FIXME this seems like really broken code -
691 * why aren't we looking at event->window
692 * and acting accordingly?
695 shadow_type = spin_button_get_shadow_type (spin);
696 if (shadow_type != GTK_SHADOW_NONE)
697 gtk_paint_box (widget->style, spin->panel,
698 GTK_STATE_NORMAL, shadow_type,
699 &event->area, widget, "spinbutton",
701 arrow_size + 2 * widget->style->xthickness,
702 widget->requisition.height);
705 gdk_window_set_back_pixmap (spin->panel, NULL, TRUE);
706 gdk_window_clear_area (spin->panel,
707 event->area.x, event->area.y,
708 event->area.width, event->area.height);
710 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
711 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
713 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
720 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
723 GtkShadowType spin_shadow_type;
724 GtkStateType state_type;
725 GtkShadowType shadow_type;
731 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
733 widget = GTK_WIDGET (spin_button);
734 spin_shadow_type = spin_button_get_shadow_type (spin_button);
735 arrow_size = spin_button_get_arrow_size (spin_button);
737 if (GTK_WIDGET_DRAWABLE (spin_button))
739 if (!spin_button->wrap &&
740 (((arrow == GTK_ARROW_UP &&
741 (spin_button->adjustment->upper - spin_button->adjustment->value
743 ((arrow == GTK_ARROW_DOWN &&
744 (spin_button->adjustment->value - spin_button->adjustment->lower
747 shadow_type = GTK_SHADOW_ETCHED_IN;
748 state_type = GTK_STATE_NORMAL;
752 if (spin_button->in_child == arrow)
754 if (spin_button->click_child == arrow)
755 state_type = GTK_STATE_ACTIVE;
757 state_type = GTK_STATE_PRELIGHT;
760 state_type = GTK_STATE_NORMAL;
762 if (spin_button->click_child == arrow)
763 shadow_type = GTK_SHADOW_IN;
765 shadow_type = GTK_SHADOW_OUT;
767 if (arrow == GTK_ARROW_UP)
769 if (spin_shadow_type != GTK_SHADOW_NONE)
771 x = widget->style->xthickness;
772 y = widget->style->ythickness;
776 x = widget->style->xthickness - 1;
777 y = widget->style->ythickness - 1;
779 gtk_paint_arrow (widget->style, spin_button->panel,
780 state_type, shadow_type,
781 NULL, widget, "spinbutton",
784 widget->requisition.height / 2
785 - widget->style->ythickness);
789 if (spin_shadow_type != GTK_SHADOW_NONE)
791 x = widget->style->xthickness;
792 y = widget->requisition.height / 2;
796 x = widget->style->xthickness - 1;
797 y = widget->requisition.height / 2 + 1;
799 gtk_paint_arrow (widget->style, spin_button->panel,
800 state_type, shadow_type,
801 NULL, widget, "spinbutton",
804 widget->requisition.height / 2
805 - widget->style->ythickness);
811 gtk_spin_button_enter_notify (GtkWidget *widget,
812 GdkEventCrossing *event)
816 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
817 g_return_val_if_fail (event != NULL, FALSE);
819 spin = GTK_SPIN_BUTTON (widget);
821 if (event->window == spin->panel)
826 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
828 if (y <= widget->requisition.height / 2)
830 spin->in_child = GTK_ARROW_UP;
831 if (spin->click_child == 2)
832 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
836 spin->in_child = GTK_ARROW_DOWN;
837 if (spin->click_child == 2)
838 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
845 gtk_spin_button_leave_notify (GtkWidget *widget,
846 GdkEventCrossing *event)
850 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
851 g_return_val_if_fail (event != NULL, FALSE);
853 spin = GTK_SPIN_BUTTON (widget);
855 if (event->window == spin->panel && spin->click_child == 2)
857 if (spin->in_child == GTK_ARROW_UP)
860 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
865 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
872 gtk_spin_button_focus_out (GtkWidget *widget,
873 GdkEventFocus *event)
875 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
876 g_return_val_if_fail (event != NULL, FALSE);
878 if (GTK_ENTRY (widget)->editable)
879 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
881 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
885 gtk_spin_button_scroll (GtkWidget *widget,
886 GdkEventScroll *event)
890 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
891 g_return_val_if_fail (event != NULL, FALSE);
893 spin = GTK_SPIN_BUTTON (widget);
895 if (event->direction == GDK_SCROLL_UP)
897 if (!GTK_WIDGET_HAS_FOCUS (widget))
898 gtk_widget_grab_focus (widget);
899 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
901 else if (event->direction == GDK_SCROLL_DOWN)
903 if (!GTK_WIDGET_HAS_FOCUS (widget))
904 gtk_widget_grab_focus (widget);
905 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
914 gtk_spin_button_button_press (GtkWidget *widget,
915 GdkEventButton *event)
919 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
920 g_return_val_if_fail (event != NULL, FALSE);
922 spin = GTK_SPIN_BUTTON (widget);
926 if (event->window == spin->panel)
928 if (!GTK_WIDGET_HAS_FOCUS (widget))
929 gtk_widget_grab_focus (widget);
930 gtk_grab_add (widget);
931 spin->button = event->button;
933 if (GTK_ENTRY (widget)->editable)
934 gtk_spin_button_update (spin);
936 if (event->y <= widget->requisition.height / 2)
938 spin->click_child = GTK_ARROW_UP;
939 if (event->button == 1)
941 gtk_spin_button_real_spin (spin,
942 spin->adjustment->step_increment);
945 spin->timer_step = spin->adjustment->step_increment;
946 spin->need_timer = TRUE;
947 spin->timer = gtk_timeout_add
948 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
949 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
952 else if (event->button == 2)
954 gtk_spin_button_real_spin (spin,
955 spin->adjustment->page_increment);
958 spin->timer_step = spin->adjustment->page_increment;
959 spin->need_timer = TRUE;
960 spin->timer = gtk_timeout_add
961 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
962 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
965 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
969 spin->click_child = GTK_ARROW_DOWN;
970 if (event->button == 1)
972 gtk_spin_button_real_spin (spin,
973 -spin->adjustment->step_increment);
976 spin->timer_step = spin->adjustment->step_increment;
977 spin->need_timer = TRUE;
978 spin->timer = gtk_timeout_add
979 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
980 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
983 else if (event->button == 2)
985 gtk_spin_button_real_spin (spin,
986 -spin->adjustment->page_increment);
989 spin->timer_step = spin->adjustment->page_increment;
990 spin->need_timer = TRUE;
991 spin->timer = gtk_timeout_add
992 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
993 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
996 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1001 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1007 gtk_spin_button_button_release (GtkWidget *widget,
1008 GdkEventButton *event)
1010 GtkSpinButton *spin;
1013 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1014 g_return_val_if_fail (event != NULL, FALSE);
1016 spin = GTK_SPIN_BUTTON (widget);
1017 arrow_size = spin_button_get_arrow_size (spin);
1019 if (event->button == spin->button)
1025 gtk_timeout_remove (spin->timer);
1027 spin->timer_calls = 0;
1028 spin->need_timer = FALSE;
1031 if (event->button == 3)
1033 if (event->y >= 0 && event->x >= 0 &&
1034 event->y <= widget->requisition.height &&
1035 event->x <= arrow_size + 2 * widget->style->xthickness)
1037 if (spin->click_child == GTK_ARROW_UP &&
1038 event->y <= widget->requisition.height / 2)
1042 diff = spin->adjustment->upper - spin->adjustment->value;
1044 gtk_spin_button_real_spin (spin, diff);
1046 else if (spin->click_child == GTK_ARROW_DOWN &&
1047 event->y > widget->requisition.height / 2)
1051 diff = spin->adjustment->value - spin->adjustment->lower;
1053 gtk_spin_button_real_spin (spin, -diff);
1057 gtk_grab_remove (widget);
1058 click_child = spin->click_child;
1059 spin->click_child = 2;
1061 gtk_spin_button_draw_arrow (spin, click_child);
1065 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1071 gtk_spin_button_motion_notify (GtkWidget *widget,
1072 GdkEventMotion *event)
1074 GtkSpinButton *spin;
1076 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1077 g_return_val_if_fail (event != NULL, FALSE);
1079 spin = GTK_SPIN_BUTTON (widget);
1084 if (event->window == spin->panel)
1090 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1092 if (y <= widget->requisition.height / 2 &&
1093 spin->in_child == GTK_ARROW_DOWN)
1095 spin->in_child = GTK_ARROW_UP;
1096 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1097 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1099 else if (y > widget->requisition.height / 2 &&
1100 spin->in_child == GTK_ARROW_UP)
1102 spin->in_child = GTK_ARROW_DOWN;
1103 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1104 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1109 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1113 gtk_spin_button_timer (GtkSpinButton *spin_button)
1115 gboolean retval = FALSE;
1117 GDK_THREADS_ENTER ();
1119 if (spin_button->timer)
1121 if (spin_button->click_child == GTK_ARROW_UP)
1122 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1124 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1126 if (spin_button->need_timer)
1128 spin_button->need_timer = FALSE;
1129 spin_button->timer = gtk_timeout_add
1130 (SPIN_BUTTON_TIMER_DELAY, (GtkFunction) gtk_spin_button_timer,
1131 (gpointer) spin_button);
1135 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1136 < spin_button->adjustment->page_increment)
1138 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1139 spin_button->timer_calls++;
1142 spin_button->timer_calls = 0;
1143 spin_button->timer_step += spin_button->climb_rate;
1150 GDK_THREADS_LEAVE ();
1156 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1157 GtkSpinButton *spin_button)
1161 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1164 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1166 if (return_val == FALSE)
1167 gtk_spin_button_default_output (spin_button);
1169 gtk_signal_emit (GTK_OBJECT (spin_button),
1170 spinbutton_signals[VALUE_CHANGED]);
1172 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_UP);
1173 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_DOWN);
1175 g_object_notify (G_OBJECT (spin_button), "value");
1179 gtk_spin_button_key_press (GtkWidget *widget,
1182 GtkSpinButton *spin;
1184 gboolean key_repeat = FALSE;
1185 gboolean retval = FALSE;
1187 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1188 g_return_val_if_fail (event != NULL, FALSE);
1190 spin = GTK_SPIN_BUTTON (widget);
1191 key = event->keyval;
1193 key_repeat = (event->time == spin->ev_time);
1195 if (GTK_ENTRY (widget)->editable)
1202 if (GTK_WIDGET_HAS_FOCUS (widget))
1204 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1207 spin->timer_step = spin->adjustment->step_increment;
1209 gtk_spin_button_real_spin (spin, spin->timer_step);
1213 if (spin->climb_rate > 0.0 && spin->timer_step
1214 < spin->adjustment->page_increment)
1216 if (spin->timer_calls < MAX_TIMER_CALLS)
1217 spin->timer_calls++;
1220 spin->timer_calls = 0;
1221 spin->timer_step += spin->climb_rate;
1232 if (GTK_WIDGET_HAS_FOCUS (widget))
1234 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1237 spin->timer_step = spin->adjustment->step_increment;
1239 gtk_spin_button_real_spin (spin, -spin->timer_step);
1243 if (spin->climb_rate > 0.0 && spin->timer_step
1244 < spin->adjustment->page_increment)
1246 if (spin->timer_calls < MAX_TIMER_CALLS)
1247 spin->timer_calls++;
1250 spin->timer_calls = 0;
1251 spin->timer_step += spin->climb_rate;
1259 case GDK_KP_Page_Up:
1262 if (event->state & GDK_CONTROL_MASK)
1264 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1266 gtk_spin_button_real_spin (spin, diff);
1269 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1274 case GDK_KP_Page_Down:
1277 if (event->state & GDK_CONTROL_MASK)
1279 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1281 gtk_spin_button_real_spin (spin, -diff);
1284 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1296 gtk_spin_button_update (spin);
1300 return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
1304 gtk_spin_button_key_release (GtkWidget *widget,
1307 GtkSpinButton *spin;
1309 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1311 spin = GTK_SPIN_BUTTON (widget);
1313 spin->ev_time = event->time;
1318 gtk_spin_button_snap (GtkSpinButton *spin_button,
1324 inc = spin_button->adjustment->step_increment;
1325 tmp = (val - spin_button->adjustment->lower) / inc;
1326 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1327 val = spin_button->adjustment->lower + floor (tmp) * inc;
1329 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1331 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1332 gtk_adjustment_set_value (spin_button->adjustment, val);
1335 gint return_val = FALSE;
1336 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1338 if (return_val == FALSE)
1339 gtk_spin_button_default_output (spin_button);
1344 gtk_spin_button_activate (GtkEntry *entry)
1346 if (entry->editable)
1347 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1351 gtk_spin_button_insert_text (GtkEditable *editable,
1352 const gchar *new_text,
1353 gint new_text_length,
1356 GtkEntry *entry = GTK_ENTRY (editable);
1357 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1358 GtkEditableClass *parent_editable_iface = g_type_interface_peek (parent_class, GTK_TYPE_EDITABLE);
1370 entry_length = entry->text_length;
1374 if (*(lc->negative_sign))
1375 neg_sign = *(lc->negative_sign);
1379 if (*(lc->positive_sign))
1380 pos_sign = *(lc->positive_sign);
1384 for (sign=0, i=0; i<entry_length; i++)
1385 if ((entry->text[i] == neg_sign) ||
1386 (entry->text[i] == pos_sign))
1392 if (sign && !(*position))
1395 for (dotpos=-1, i=0; i<entry_length; i++)
1396 if (entry->text[i] == *(lc->decimal_point))
1402 if (dotpos > -1 && *position > dotpos &&
1403 (gint)spin->digits - entry_length
1404 + dotpos - new_text_length + 1 < 0)
1407 for (i = 0; i < new_text_length; i++)
1409 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1411 if (sign || (*position) || i)
1415 else if (new_text[i] == *(lc->decimal_point))
1417 if (!spin->digits || dotpos > -1 ||
1418 (new_text_length - 1 - i + entry_length
1419 - *position > (gint)spin->digits))
1421 dotpos = *position + i;
1423 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1428 parent_editable_iface->insert_text (editable, new_text,
1429 new_text_length, position);
1433 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1437 gdouble new_value = 0.0;
1439 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1441 adj = spin_button->adjustment;
1443 new_value = adj->value + increment;
1447 if (spin_button->wrap)
1449 if (fabs (adj->value - adj->upper) < EPSILON)
1450 new_value = adj->lower;
1451 else if (new_value > adj->upper)
1452 new_value = adj->upper;
1455 new_value = MIN (new_value, adj->upper);
1457 else if (increment < 0)
1459 if (spin_button->wrap)
1461 if (fabs (adj->value - adj->lower) < EPSILON)
1462 new_value = adj->upper;
1463 else if (new_value < adj->lower)
1464 new_value = adj->lower;
1467 new_value = MAX (new_value, adj->lower);
1470 if (fabs (new_value - adj->value) > EPSILON)
1471 gtk_adjustment_set_value (adj, new_value);
1475 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1480 *new_val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1482 return GTK_INPUT_ERROR;
1488 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1490 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1492 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1493 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1499 /***********************************************************
1500 ***********************************************************
1501 *** Public interface ***
1502 ***********************************************************
1503 ***********************************************************/
1507 gtk_spin_button_configure (GtkSpinButton *spin_button,
1508 GtkAdjustment *adjustment,
1512 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1515 gtk_spin_button_set_adjustment (spin_button, adjustment);
1517 adjustment = spin_button->adjustment;
1519 g_object_freeze_notify (G_OBJECT (spin_button));
1520 if (spin_button->digits != digits)
1522 spin_button->digits = digits;
1523 g_object_notify (G_OBJECT (spin_button), "digits");
1526 if (spin_button->climb_rate != climb_rate)
1528 spin_button->climb_rate = climb_rate;
1529 g_object_notify (G_OBJECT (spin_button), "climb_rate");
1531 g_object_thaw_notify (G_OBJECT (spin_button));
1533 gtk_adjustment_value_changed (adjustment);
1537 gtk_spin_button_new (GtkAdjustment *adjustment,
1541 GtkSpinButton *spin;
1544 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1546 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1548 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1550 return GTK_WIDGET (spin);
1554 * gtk_spin_button_new_with_range:
1555 * @min: Minimum allowable value
1556 * @max: Maximum allowable value
1557 * @step: Increment added or subtracted by spinning the widget
1559 * This is a convenience constructor that allows creation of a numeric
1560 * #GtkSpinButton without manually creating an adjustment. The value is
1561 * initially set to the minimum value and a page increment of 10 * @step
1562 * is the default. The precision of the spin button is equivalent to the
1563 * precision of @step.
1565 * Return value: The new spin button as a #GtkWidget.
1568 gtk_spin_button_new_with_range (gdouble min,
1573 GtkSpinButton *spin;
1576 g_return_val_if_fail (min < max, NULL);
1577 g_return_val_if_fail (step != 0.0, NULL);
1579 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1581 adj = gtk_adjustment_new (min, min, max, step, 10 * step, step);
1583 if (fabs (step) >= 1.0 || step == 0.0)
1586 digits = abs ((gint) floor (log10 (fabs (step))));
1587 if (digits > MAX_DIGITS)
1588 digits = MAX_DIGITS;
1591 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1593 gtk_spin_button_set_numeric (spin, TRUE);
1595 return GTK_WIDGET (spin);
1598 /* Callback used when the spin button's adjustment changes. We need to redraw
1599 * the arrows when the adjustment's range changes, and reevaluate our size request.
1602 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1604 GtkSpinButton *spin_button;
1606 spin_button = GTK_SPIN_BUTTON (data);
1608 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1612 * gtk_spin_button_set_adjustment:
1613 * @spin_button: a #GtkSpinButton
1614 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1616 * Replaces the #GtkAdjustment associated with @spin_button.
1619 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1620 GtkAdjustment *adjustment)
1622 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1624 if (spin_button->adjustment != adjustment)
1626 if (spin_button->adjustment)
1628 gtk_signal_disconnect_by_data (GTK_OBJECT (spin_button->adjustment),
1629 (gpointer) spin_button);
1630 gtk_object_unref (GTK_OBJECT (spin_button->adjustment));
1632 spin_button->adjustment = adjustment;
1635 gtk_object_ref (GTK_OBJECT (adjustment));
1636 gtk_object_sink (GTK_OBJECT (adjustment));
1637 gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
1638 (GtkSignalFunc) gtk_spin_button_value_changed,
1639 (gpointer) spin_button);
1640 gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
1641 (GtkSignalFunc) adjustment_changed_cb,
1642 (gpointer) spin_button);
1645 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1648 g_object_notify (G_OBJECT (spin_button), "adjustment");
1652 * gtk_spin_button_get_adjustment:
1655 * Get the adjustment associated with a #GtkSpinButton
1657 * Return value: the #GtkAdjustment of @spin_button
1660 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1662 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1664 return spin_button->adjustment;
1668 * gtk_spin_button_set_digits:
1669 * @spin_button: a #GtkSpinButton
1670 * @digits: the number of digits to be displayed for the spin button's value
1672 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1676 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1679 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1681 if (spin_button->digits != digits)
1683 spin_button->digits = digits;
1684 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1685 g_object_notify (G_OBJECT (spin_button), "digits");
1687 /* since lower/upper may have changed */
1688 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1693 * gtk_spin_button_get_digits:
1694 * @spin_button: a #GtkSpinButton
1696 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1698 * Returns: the current precision
1701 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1703 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1705 return spin_button->digits;
1709 * gtk_spin_button_set_increments:
1710 * @spin_button: a #GtkSpinButton
1711 * @step: increment applied for a button 1 press.
1712 * @page: increment applied for a button 2 press.
1714 * Sets the step and page increments for spin_button. This affects how
1715 * quickly the value changes when the spin button's arrows are activated.
1718 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1722 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1724 spin_button->adjustment->step_increment = step;
1725 spin_button->adjustment->page_increment = page;
1729 * gtk_spin_button_get_increments:
1730 * @spin_button: a #GtkSpinButton
1731 * @step: location to store step increment, or %NULL
1732 * @page: location to store page increment, or %NULL
1734 * Gets the current step and page the increments used by @spin_button. See
1735 * gtk_spin_button_set_increments().
1738 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1742 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1745 *step = spin_button->adjustment->step_increment;
1747 *page = spin_button->adjustment->page_increment;
1751 * gtk_spin_button_set_range:
1752 * @spin_button: a #GtkSpinButton
1753 * @min: minimum allowable value
1754 * @max: maximum allowable value
1756 * Sets the minimum and maximum allowable values for @spin_button
1759 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1765 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1767 spin_button->adjustment->lower = min;
1768 spin_button->adjustment->upper = max;
1770 value = CLAMP (spin_button->adjustment->value,
1771 spin_button->adjustment->lower,
1772 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1774 if (value != spin_button->adjustment->value)
1775 gtk_spin_button_set_value (spin_button, value);
1777 gtk_adjustment_changed (spin_button->adjustment);
1781 * gtk_spin_button_get_range:
1782 * @spin_button: a #GtkSpinButton
1783 * @min: location to store minimum allowed value, or %NULL
1784 * @max: location to store maximum allowed value, or %NULL
1786 * Gets the range allowed for @spin_button. See
1787 * gtk_spin_button_set_range().
1790 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1794 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1797 *min = spin_button->adjustment->lower;
1799 *max = spin_button->adjustment->upper;
1803 * gtk_spin_button_get_value:
1804 * @spin_button: a #GtkSpinButton
1806 * Get the value in the @spin_button.
1808 * Return value: the value of @spin_button
1811 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1813 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1815 return spin_button->adjustment->value;
1819 * gtk_spin_button_get_value_as_int:
1820 * @spin_button: a #GtkSpinButton
1822 * Get the value @spin_button represented as an integer.
1824 * Return value: the value of @spin_button
1827 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1831 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1833 val = spin_button->adjustment->value;
1834 if (val - floor (val) < ceil (val) - val)
1841 * gtk_spin_button_set_value:
1842 * @spin_button: a #GtkSpinButton
1843 * @value: the new value
1845 * Set the value of @spin_button.
1848 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1851 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1853 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1854 gtk_adjustment_set_value (spin_button->adjustment, value);
1857 gint return_val = FALSE;
1858 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1860 if (return_val == FALSE)
1861 gtk_spin_button_default_output (spin_button);
1866 * gtk_spin_button_set_update_policy:
1867 * @spin_button: a #GtkSpinButton
1868 * @policy: a #GtkSpinButtonUpdatePolicy value
1870 * Sets the update behavior of a spin button. This determines whether the
1871 * spin button is always updated or only when a valid value is set.
1874 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1875 GtkSpinButtonUpdatePolicy policy)
1877 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1879 if (spin_button->update_policy != policy)
1881 spin_button->update_policy = policy;
1882 g_object_notify (G_OBJECT (spin_button), "update_policy");
1887 * gtk_spin_button_get_update_policy:
1888 * @spin_button: a #GtkSpinButton
1890 * Gets the update behavior of a spin button. See
1891 * gtk_spin_button_set_update_policy().
1893 * Return value: the current update policy
1895 GtkSpinButtonUpdatePolicy
1896 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1898 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1900 return spin_button->update_policy;
1904 * gtk_spin_button_set_numeric:
1905 * @spin_button: a #GtkSpinButton
1906 * @numeric: flag indicating if only numeric entry is allowed.
1908 * Sets the flag that determines if non-numeric text can be typed into
1912 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1915 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1917 spin_button->numeric = (numeric != 0);
1919 g_object_notify (G_OBJECT (spin_button), "numeric");
1923 * gtk_spin_button_get_numeric:
1924 * @spin_button: a #GtkSpinButton
1926 * Returns whether non-numeric text can be typed into the spin button.
1927 * See gtk_spin_button_set_numeric().
1929 * Return value: %TRUE if only numeric text can be entered
1932 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1934 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1936 return spin_button->numeric;
1940 * gtk_spin_button_set_wrap:
1941 * @spin_button: a #GtkSpinButton
1942 * @wrap: a flag indicating if wrapping behavior is performed.
1944 * Sets the flag that determines if a spin button value wraps around to the
1945 * opposite limit when the upper or lower limit of the range is exceeded.
1948 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1951 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1953 spin_button->wrap = (wrap != 0);
1955 g_object_notify (G_OBJECT (spin_button), "wrap");
1959 * gtk_spin_button_get_wrap:
1960 * @spin_button: a #GtkSpinButton
1962 * Returns whether the spin button's value wraps around to the
1963 * opposite limit when the upper or lower limit of the range is
1964 * exceeded. See gtk_spin_button_set_wrap().
1966 * Return value: %TRUE if the spin button wraps around
1969 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
1971 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1973 return spin_button->wrap;
1977 spin_button_get_arrow_size (GtkSpinButton *spin_button)
1979 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
1981 return PANGO_PIXELS (size);
1985 * spin_button_get_shadow_type:
1986 * @spin_button: a #GtkSpinButton
1988 * Convenience function to Get the shadow type from the underlying widget's
1991 * Return value: the #GtkShadowType
1994 spin_button_get_shadow_type (GtkSpinButton *spin_button)
1996 GtkShadowType rc_shadow_type;
1998 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
2000 return rc_shadow_type;
2004 * gtk_spin_button_set_snap_to_ticks:
2005 * @spin_button: a #GtkSpinButton
2006 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2008 * Sets the policy as to whether values are corrected to the nearest step
2009 * increment when a spin button is activated after providing an invalid value.
2012 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2013 gboolean snap_to_ticks)
2017 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2019 new_val = (snap_to_ticks != 0);
2021 if (new_val != spin_button->snap_to_ticks)
2023 spin_button->snap_to_ticks = new_val;
2024 if (new_val && GTK_ENTRY (spin_button)->editable)
2025 gtk_spin_button_update (spin_button);
2027 g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
2032 * gtk_spin_button_get_snap_to_ticks:
2033 * @spin_button: a #GtkSpinButton
2035 * Returns whether the values are corrected to the nearest step. See
2036 * gtk_spin_button_set_snap_to_ticks().
2038 * Return value: %TRUE if values are snapped to the nearest step.
2041 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2043 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2045 return spin_button->snap_to_ticks;
2049 * gtk_spin_button_spin:
2050 * @spin_button: a #GtkSpinButton
2051 * @direction: a #GtkSpinType indicating the direction to spin.
2052 * @increment: step increment to apply in the specified direction.
2054 * Increment or decrement a spin button's value in a specified direction
2055 * by a specified amount.
2058 gtk_spin_button_spin (GtkSpinButton *spin_button,
2059 GtkSpinType direction,
2065 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2067 adj = spin_button->adjustment;
2069 /* for compatibility with the 1.0.x version of this function */
2070 if (increment != 0 && increment != adj->step_increment &&
2071 (direction == GTK_SPIN_STEP_FORWARD ||
2072 direction == GTK_SPIN_STEP_BACKWARD))
2074 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2075 increment = -increment;
2076 direction = GTK_SPIN_USER_DEFINED;
2081 case GTK_SPIN_STEP_FORWARD:
2083 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2086 case GTK_SPIN_STEP_BACKWARD:
2088 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2091 case GTK_SPIN_PAGE_FORWARD:
2093 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2096 case GTK_SPIN_PAGE_BACKWARD:
2098 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2103 diff = adj->value - adj->lower;
2105 gtk_spin_button_real_spin (spin_button, -diff);
2110 diff = adj->upper - adj->value;
2112 gtk_spin_button_real_spin (spin_button, diff);
2115 case GTK_SPIN_USER_DEFINED:
2118 gtk_spin_button_real_spin (spin_button, increment);
2127 * gtk_spin_button_update:
2128 * @spin_button: a #GtkSpinButton
2130 * Manually force an update of the spin button.
2133 gtk_spin_button_update (GtkSpinButton *spin_button)
2139 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2142 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[INPUT],
2144 if (return_val == FALSE)
2146 return_val = gtk_spin_button_default_input (spin_button, &val);
2147 error = (return_val == GTK_INPUT_ERROR);
2149 else if (return_val == GTK_INPUT_ERROR)
2152 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2154 if (val < spin_button->adjustment->lower)
2155 val = spin_button->adjustment->lower;
2156 else if (val > spin_button->adjustment->upper)
2157 val = spin_button->adjustment->upper;
2159 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2161 val < spin_button->adjustment->lower ||
2162 val > spin_button->adjustment->upper))
2164 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2168 if (spin_button->snap_to_ticks)
2169 gtk_spin_button_snap (spin_button, val);
2172 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2173 gtk_adjustment_set_value (spin_button->adjustment, val);
2177 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
2179 if (return_val == FALSE)
2180 gtk_spin_button_default_output (spin_button);