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/.
36 #include "gdk/gdkkeysyms.h"
37 #include "gtkbindings.h"
38 #include "gtkspinbutton.h"
40 #include "gtkmarshalers.h"
41 #include "gtksettings.h"
42 #include "gtkprivate.h"
46 #define MIN_SPIN_BUTTON_WIDTH 30
47 #define SPIN_BUTTON_INITIAL_TIMER_DELAY 200
48 #define SPIN_BUTTON_TIMER_DELAY 20
49 #define MAX_TIMER_CALLS 5
52 #define MIN_ARROW_WIDTH 6
76 static void gtk_spin_button_class_init (GtkSpinButtonClass *klass);
77 static void gtk_spin_button_editable_init (GtkEditableClass *iface);
78 static void gtk_spin_button_init (GtkSpinButton *spin_button);
79 static void gtk_spin_button_finalize (GObject *object);
80 static void gtk_spin_button_destroy (GtkObject *object);
81 static void gtk_spin_button_set_property (GObject *object,
85 static void gtk_spin_button_get_property (GObject *object,
89 static void gtk_spin_button_map (GtkWidget *widget);
90 static void gtk_spin_button_unmap (GtkWidget *widget);
91 static void gtk_spin_button_realize (GtkWidget *widget);
92 static void gtk_spin_button_unrealize (GtkWidget *widget);
93 static void gtk_spin_button_size_request (GtkWidget *widget,
94 GtkRequisition *requisition);
95 static void gtk_spin_button_size_allocate (GtkWidget *widget,
96 GtkAllocation *allocation);
97 static gint gtk_spin_button_expose (GtkWidget *widget,
98 GdkEventExpose *event);
99 static gint gtk_spin_button_button_press (GtkWidget *widget,
100 GdkEventButton *event);
101 static gint gtk_spin_button_button_release (GtkWidget *widget,
102 GdkEventButton *event);
103 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
104 GdkEventMotion *event);
105 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
106 GdkEventCrossing *event);
107 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
108 GdkEventCrossing *event);
109 static gint gtk_spin_button_focus_out (GtkWidget *widget,
110 GdkEventFocus *event);
111 static void gtk_spin_button_grab_notify (GtkWidget *widget,
112 gboolean was_grabbed);
113 static void gtk_spin_button_state_changed (GtkWidget *widget,
114 GtkStateType previous_state);
115 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
116 GtkArrowType arrow_type);
117 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
118 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
119 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
120 GtkSpinButton *spin_button);
121 static gint gtk_spin_button_key_release (GtkWidget *widget,
123 static gint gtk_spin_button_scroll (GtkWidget *widget,
124 GdkEventScroll *event);
125 static void gtk_spin_button_activate (GtkEntry *entry);
126 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
128 static void gtk_spin_button_insert_text (GtkEditable *editable,
129 const gchar *new_text,
130 gint new_text_length,
132 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
134 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
135 GtkScrollType scroll);
137 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
139 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
141 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
142 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
143 static void spin_button_redraw (GtkSpinButton *spin_button);
146 static GtkEntryClass *parent_class = NULL;
147 static guint spinbutton_signals[LAST_SIGNAL] = {0};
152 gtk_spin_button_get_type (void)
154 static GType spin_button_type = 0;
156 if (!spin_button_type)
158 static const GTypeInfo spin_button_info =
160 sizeof (GtkSpinButtonClass),
161 NULL, /* base_init */
162 NULL, /* base_finalize */
163 (GClassInitFunc) gtk_spin_button_class_init,
164 NULL, /* class_finalize */
165 NULL, /* class_data */
166 sizeof (GtkSpinButton),
168 (GInstanceInitFunc) gtk_spin_button_init,
171 static const GInterfaceInfo editable_info =
173 (GInterfaceInitFunc) gtk_spin_button_editable_init, /* interface_init */
174 NULL, /* interface_finalize */
175 NULL /* interface_data */
179 g_type_register_static (GTK_TYPE_ENTRY, g_intern_static_string ("GtkSpinButton"),
180 &spin_button_info, 0);
182 g_type_add_interface_static (spin_button_type,
186 return spin_button_type;
189 #define add_spin_binding(binding_set, keyval, mask, scroll) \
190 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
192 GTK_TYPE_SCROLL_TYPE, scroll)
195 gtk_spin_button_class_init (GtkSpinButtonClass *class)
197 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
198 GtkObjectClass *object_class;
199 GtkWidgetClass *widget_class;
200 GtkEntryClass *entry_class;
201 GtkBindingSet *binding_set;
203 object_class = (GtkObjectClass*) class;
204 widget_class = (GtkWidgetClass*) class;
205 entry_class = (GtkEntryClass*) class;
207 parent_class = g_type_class_peek_parent (class);
209 gobject_class->finalize = gtk_spin_button_finalize;
211 gobject_class->set_property = gtk_spin_button_set_property;
212 gobject_class->get_property = gtk_spin_button_get_property;
214 object_class->destroy = gtk_spin_button_destroy;
216 widget_class->map = gtk_spin_button_map;
217 widget_class->unmap = gtk_spin_button_unmap;
218 widget_class->realize = gtk_spin_button_realize;
219 widget_class->unrealize = gtk_spin_button_unrealize;
220 widget_class->size_request = gtk_spin_button_size_request;
221 widget_class->size_allocate = gtk_spin_button_size_allocate;
222 widget_class->expose_event = gtk_spin_button_expose;
223 widget_class->scroll_event = gtk_spin_button_scroll;
224 widget_class->button_press_event = gtk_spin_button_button_press;
225 widget_class->button_release_event = gtk_spin_button_button_release;
226 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
227 widget_class->key_release_event = gtk_spin_button_key_release;
228 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
229 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
230 widget_class->focus_out_event = gtk_spin_button_focus_out;
231 widget_class->grab_notify = gtk_spin_button_grab_notify;
232 widget_class->state_changed = gtk_spin_button_state_changed;
234 entry_class->activate = gtk_spin_button_activate;
237 class->output = NULL;
238 class->change_value = gtk_spin_button_real_change_value;
240 g_object_class_install_property (gobject_class,
242 g_param_spec_object ("adjustment",
244 P_("The adjustment that holds the value of the spinbutton"),
246 GTK_PARAM_READWRITE));
248 g_object_class_install_property (gobject_class,
250 g_param_spec_double ("climb-rate",
252 P_("The acceleration rate when you hold down a button"),
256 GTK_PARAM_READWRITE));
258 g_object_class_install_property (gobject_class,
260 g_param_spec_uint ("digits",
262 P_("The number of decimal places to display"),
266 GTK_PARAM_READWRITE));
268 g_object_class_install_property (gobject_class,
270 g_param_spec_boolean ("snap-to-ticks",
272 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
274 GTK_PARAM_READWRITE));
276 g_object_class_install_property (gobject_class,
278 g_param_spec_boolean ("numeric",
280 P_("Whether non-numeric characters should be ignored"),
282 GTK_PARAM_READWRITE));
284 g_object_class_install_property (gobject_class,
286 g_param_spec_boolean ("wrap",
288 P_("Whether a spin button should wrap upon reaching its limits"),
290 GTK_PARAM_READWRITE));
292 g_object_class_install_property (gobject_class,
294 g_param_spec_enum ("update-policy",
296 P_("Whether the spin button should update always, or only when the value is legal"),
297 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
299 GTK_PARAM_READWRITE));
301 g_object_class_install_property (gobject_class,
303 g_param_spec_double ("value",
305 P_("Reads the current value, or sets a new value"),
309 GTK_PARAM_READWRITE));
311 gtk_widget_class_install_style_property_parser (widget_class,
312 g_param_spec_enum ("shadow-type",
314 P_("Style of bevel around the spin button"),
315 GTK_TYPE_SHADOW_TYPE,
318 gtk_rc_property_parse_enum);
319 spinbutton_signals[INPUT] =
320 g_signal_new ("input",
321 G_TYPE_FROM_CLASS (gobject_class),
323 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
325 _gtk_marshal_INT__POINTER,
329 spinbutton_signals[OUTPUT] =
330 g_signal_new ("output",
331 G_TYPE_FROM_CLASS (gobject_class),
333 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
334 _gtk_boolean_handled_accumulator, NULL,
335 _gtk_marshal_BOOLEAN__VOID,
338 spinbutton_signals[VALUE_CHANGED] =
339 g_signal_new ("value_changed",
340 G_TYPE_FROM_CLASS (gobject_class),
342 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
344 _gtk_marshal_VOID__VOID,
348 spinbutton_signals[CHANGE_VALUE] =
349 g_signal_new ("change_value",
350 G_TYPE_FROM_CLASS (gobject_class),
351 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
352 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
354 _gtk_marshal_VOID__ENUM,
356 GTK_TYPE_SCROLL_TYPE);
358 binding_set = gtk_binding_set_by_class (class);
360 add_spin_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
361 add_spin_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
362 add_spin_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
363 add_spin_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
364 add_spin_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
365 add_spin_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
366 add_spin_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
367 add_spin_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
371 gtk_spin_button_editable_init (GtkEditableClass *iface)
373 iface->insert_text = gtk_spin_button_insert_text;
377 gtk_spin_button_set_property (GObject *object,
382 GtkSpinButton *spin_button;
384 spin_button = GTK_SPIN_BUTTON (object);
388 GtkAdjustment *adjustment;
390 case PROP_ADJUSTMENT:
391 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
393 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
394 gtk_spin_button_set_adjustment (spin_button, adjustment);
396 case PROP_CLIMB_RATE:
397 gtk_spin_button_configure (spin_button,
398 spin_button->adjustment,
399 g_value_get_double (value),
400 spin_button->digits);
403 gtk_spin_button_configure (spin_button,
404 spin_button->adjustment,
405 spin_button->climb_rate,
406 g_value_get_uint (value));
408 case PROP_SNAP_TO_TICKS:
409 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
412 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
415 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
417 case PROP_UPDATE_POLICY:
418 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
421 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
429 gtk_spin_button_get_property (GObject *object,
434 GtkSpinButton *spin_button;
436 spin_button = GTK_SPIN_BUTTON (object);
440 case PROP_ADJUSTMENT:
441 g_value_set_object (value, spin_button->adjustment);
443 case PROP_CLIMB_RATE:
444 g_value_set_double (value, spin_button->climb_rate);
447 g_value_set_uint (value, spin_button->digits);
449 case PROP_SNAP_TO_TICKS:
450 g_value_set_boolean (value, spin_button->snap_to_ticks);
453 g_value_set_boolean (value, spin_button->numeric);
456 g_value_set_boolean (value, spin_button->wrap);
458 case PROP_UPDATE_POLICY:
459 g_value_set_enum (value, spin_button->update_policy);
462 g_value_set_double (value, spin_button->adjustment->value);
465 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
471 gtk_spin_button_init (GtkSpinButton *spin_button)
473 spin_button->adjustment = NULL;
474 spin_button->panel = NULL;
475 spin_button->timer = 0;
476 spin_button->climb_rate = 0.0;
477 spin_button->timer_step = 0.0;
478 spin_button->update_policy = GTK_UPDATE_ALWAYS;
479 spin_button->in_child = NO_ARROW;
480 spin_button->click_child = NO_ARROW;
481 spin_button->button = 0;
482 spin_button->need_timer = FALSE;
483 spin_button->timer_calls = 0;
484 spin_button->digits = 0;
485 spin_button->numeric = FALSE;
486 spin_button->wrap = FALSE;
487 spin_button->snap_to_ticks = FALSE;
489 gtk_spin_button_set_adjustment (spin_button,
490 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
494 gtk_spin_button_finalize (GObject *object)
496 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
498 G_OBJECT_CLASS (parent_class)->finalize (object);
502 gtk_spin_button_destroy (GtkObject *object)
504 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
506 GTK_OBJECT_CLASS (parent_class)->destroy (object);
510 gtk_spin_button_map (GtkWidget *widget)
512 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
514 GTK_WIDGET_CLASS (parent_class)->map (widget);
515 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
520 gtk_spin_button_unmap (GtkWidget *widget)
522 if (GTK_WIDGET_MAPPED (widget))
524 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
526 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
527 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
532 gtk_spin_button_realize (GtkWidget *widget)
534 GtkSpinButton *spin_button;
535 GdkWindowAttr attributes;
536 gint attributes_mask;
541 spin_button = GTK_SPIN_BUTTON (widget);
542 arrow_size = spin_button_get_arrow_size (spin_button);
544 real_width = widget->allocation.width;
545 widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
546 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
547 GDK_KEY_RELEASE_MASK);
548 GTK_WIDGET_CLASS (parent_class)->realize (widget);
550 widget->allocation.width = real_width;
552 attributes.window_type = GDK_WINDOW_CHILD;
553 attributes.wclass = GDK_INPUT_OUTPUT;
554 attributes.visual = gtk_widget_get_visual (widget);
555 attributes.colormap = gtk_widget_get_colormap (widget);
556 attributes.event_mask = gtk_widget_get_events (widget);
557 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
558 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
559 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
561 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
563 attributes.x = (widget->allocation.x +
564 widget->allocation.width - arrow_size -
565 2 * widget->style->xthickness);
566 attributes.y = widget->allocation.y + (widget->allocation.height -
567 widget->requisition.height) / 2;
568 attributes.width = arrow_size + 2 * widget->style->xthickness;
569 attributes.height = widget->requisition.height;
571 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
572 &attributes, attributes_mask);
573 gdk_window_set_user_data (spin_button->panel, widget);
575 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
578 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
579 if (return_val == FALSE)
580 gtk_spin_button_default_output (spin_button);
582 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
586 gtk_spin_button_unrealize (GtkWidget *widget)
588 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
590 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
594 gdk_window_set_user_data (spin->panel, NULL);
595 gdk_window_destroy (spin->panel);
601 compute_double_length (double val, int digits)
607 if (fabs (val) > 1.0)
608 a = floor (log10 (fabs (val))) + 1;
620 return a + digits + extra;
624 gtk_spin_button_size_request (GtkWidget *widget,
625 GtkRequisition *requisition)
628 GtkSpinButton *spin_button;
631 entry = GTK_ENTRY (widget);
632 spin_button = GTK_SPIN_BUTTON (widget);
633 arrow_size = spin_button_get_arrow_size (spin_button);
635 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
637 if (entry->width_chars < 0)
639 PangoContext *context;
640 PangoFontMetrics *metrics;
646 gboolean interior_focus;
648 gint xborder, yborder;
650 gtk_widget_style_get (widget,
651 "interior-focus", &interior_focus,
652 "focus-line-width", &focus_width,
655 context = gtk_widget_get_pango_context (widget);
656 metrics = pango_context_get_metrics (context,
657 widget->style->font_desc,
658 pango_context_get_language (context));
660 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
661 digit_width = PANGO_SCALE *
662 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
664 pango_font_metrics_unref (metrics);
666 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
668 width = MIN_SPIN_BUTTON_WIDTH;
669 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
670 spin_button->digits));
672 string_len = compute_double_length (spin_button->adjustment->upper,
673 spin_button->digits);
674 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
675 width = MAX (width, w);
676 string_len = compute_double_length (spin_button->adjustment->lower,
677 spin_button->digits);
678 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
679 width = MAX (width, w);
681 _gtk_entry_get_borders (entry, &xborder, &yborder);
683 xborder += 2; /* INNER_BORDER */
685 requisition->width = width + xborder * 2;
688 requisition->width += arrow_size + 2 * widget->style->xthickness;
692 gtk_spin_button_size_allocate (GtkWidget *widget,
693 GtkAllocation *allocation)
696 GtkAllocation entry_allocation;
697 GtkAllocation panel_allocation;
701 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
702 g_return_if_fail (allocation != NULL);
704 spin = GTK_SPIN_BUTTON (widget);
705 arrow_size = spin_button_get_arrow_size (spin);
706 panel_width = arrow_size + 2 * widget->style->xthickness;
708 widget->allocation = *allocation;
710 entry_allocation = *allocation;
711 entry_allocation.width -= panel_width;
713 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
715 entry_allocation.x += panel_width;
716 panel_allocation.x = allocation->x;
720 panel_allocation.x = allocation->x + allocation->width - panel_width;
723 panel_allocation.width = panel_width;
724 panel_allocation.height = MIN (widget->requisition.height, allocation->height);
726 panel_allocation.y = allocation->y + (allocation->height -
727 panel_allocation.height) / 2;
729 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &entry_allocation);
731 if (GTK_WIDGET_REALIZED (widget))
733 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
736 panel_allocation.width,
737 panel_allocation.height);
740 spin_button_redraw (spin);
744 gtk_spin_button_expose (GtkWidget *widget,
745 GdkEventExpose *event)
749 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
750 g_return_val_if_fail (event != NULL, FALSE);
752 spin = GTK_SPIN_BUTTON (widget);
754 if (GTK_WIDGET_DRAWABLE (widget))
756 GtkShadowType shadow_type;
759 if (event->window != spin->panel)
760 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
762 /* we redraw the panel even if it wasn't exposed. This is
763 * because spin->panel is not a child window of widget->window,
764 * so it will not be invalidated by eg. gtk_widget_queue_draw().
769 gdk_drawable_get_size (spin->panel, &rect.width, &rect.height);
771 shadow_type = spin_button_get_shadow_type (spin);
773 gdk_window_begin_paint_rect (spin->panel, &rect);
775 if (shadow_type != GTK_SHADOW_NONE)
777 gtk_paint_box (widget->style, spin->panel,
778 GTK_STATE_NORMAL, shadow_type,
779 NULL, widget, "spinbutton",
780 rect.x, rect.y, rect.width, rect.height);
783 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
784 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
786 gdk_window_end_paint (spin->panel);
793 spin_button_at_limit (GtkSpinButton *spin_button,
796 GtkArrowType effective_arrow;
798 if (spin_button->wrap)
801 if (spin_button->adjustment->step_increment > 0)
802 effective_arrow = arrow;
804 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
806 if (effective_arrow == GTK_ARROW_UP &&
807 (spin_button->adjustment->upper - spin_button->adjustment->value <= EPSILON))
810 if (effective_arrow == GTK_ARROW_DOWN &&
811 (spin_button->adjustment->value - spin_button->adjustment->lower <= EPSILON))
818 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
819 GtkArrowType arrow_type)
821 GtkStateType state_type;
822 GtkShadowType shadow_type;
830 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
831 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
833 widget = GTK_WIDGET (spin_button);
835 if (GTK_WIDGET_DRAWABLE (widget))
837 width = spin_button_get_arrow_size (spin_button) + 2 * widget->style->xthickness;
839 if (arrow_type == GTK_ARROW_UP)
844 height = widget->requisition.height / 2;
849 y = widget->requisition.height / 2;
851 height = (widget->requisition.height + 1) / 2;
854 if (spin_button_at_limit (spin_button, arrow_type))
856 shadow_type = GTK_SHADOW_OUT;
857 state_type = GTK_STATE_INSENSITIVE;
861 if (spin_button->click_child == arrow_type)
863 state_type = GTK_STATE_ACTIVE;
864 shadow_type = GTK_SHADOW_IN;
868 if (spin_button->in_child == arrow_type &&
869 spin_button->click_child == NO_ARROW)
871 state_type = GTK_STATE_PRELIGHT;
875 state_type = GTK_WIDGET_STATE (widget);
878 shadow_type = GTK_SHADOW_OUT;
882 gtk_paint_box (widget->style, spin_button->panel,
883 state_type, shadow_type,
885 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
886 x, y, width, height);
888 height = widget->requisition.height;
890 if (arrow_type == GTK_ARROW_DOWN)
893 height = height - y - 2;
898 height = height / 2 - 2;
903 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
909 w -= w % 2 - 1; /* force odd */
912 x += (width - w) / 2;
913 y += (height - h) / 2;
918 gtk_paint_arrow (widget->style, spin_button->panel,
919 state_type, shadow_type,
920 NULL, widget, "spinbutton",
922 x, y, width, height);
927 gtk_spin_button_enter_notify (GtkWidget *widget,
928 GdkEventCrossing *event)
930 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
932 if (event->window == spin->panel)
937 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
939 if (y <= widget->requisition.height / 2)
940 spin->in_child = GTK_ARROW_UP;
942 spin->in_child = GTK_ARROW_DOWN;
944 spin_button_redraw (spin);
951 gtk_spin_button_leave_notify (GtkWidget *widget,
952 GdkEventCrossing *event)
954 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
956 spin->in_child = NO_ARROW;
957 spin_button_redraw (spin);
963 gtk_spin_button_focus_out (GtkWidget *widget,
964 GdkEventFocus *event)
966 if (GTK_ENTRY (widget)->editable)
967 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
969 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
973 gtk_spin_button_grab_notify (GtkWidget *widget,
974 gboolean was_grabbed)
976 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
980 gtk_spin_button_stop_spinning (spin);
981 spin_button_redraw (spin);
986 gtk_spin_button_state_changed (GtkWidget *widget,
987 GtkStateType previous_state)
989 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
991 if (!GTK_WIDGET_IS_SENSITIVE (widget))
993 gtk_spin_button_stop_spinning (spin);
994 spin_button_redraw (spin);
999 gtk_spin_button_scroll (GtkWidget *widget,
1000 GdkEventScroll *event)
1002 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1004 if (event->direction == GDK_SCROLL_UP)
1006 if (!GTK_WIDGET_HAS_FOCUS (widget))
1007 gtk_widget_grab_focus (widget);
1008 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
1010 else if (event->direction == GDK_SCROLL_DOWN)
1012 if (!GTK_WIDGET_HAS_FOCUS (widget))
1013 gtk_widget_grab_focus (widget);
1014 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
1023 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1027 g_source_remove (spin->timer);
1029 spin->timer_calls = 0;
1030 spin->need_timer = FALSE;
1035 spin->timer_step = spin->adjustment->step_increment;
1036 spin->timer_calls = 0;
1038 spin->click_child = NO_ARROW;
1043 start_spinning (GtkSpinButton *spin,
1044 GtkArrowType click_child,
1047 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1049 spin->click_child = click_child;
1053 spin->timer_step = step;
1054 spin->need_timer = TRUE;
1055 spin->timer = g_timeout_add (SPIN_BUTTON_INITIAL_TIMER_DELAY,
1056 (GSourceFunc) gtk_spin_button_timer,
1059 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1061 spin_button_redraw (spin);
1065 gtk_spin_button_button_press (GtkWidget *widget,
1066 GdkEventButton *event)
1068 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1072 if (event->window == spin->panel)
1074 if (!GTK_WIDGET_HAS_FOCUS (widget))
1075 gtk_widget_grab_focus (widget);
1076 spin->button = event->button;
1078 if (GTK_ENTRY (widget)->editable)
1079 gtk_spin_button_update (spin);
1081 if (event->y <= widget->requisition.height / 2)
1083 if (event->button == 1)
1084 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->step_increment);
1085 else if (event->button == 2)
1086 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->page_increment);
1088 spin->click_child = GTK_ARROW_UP;
1092 if (event->button == 1)
1093 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->step_increment);
1094 else if (event->button == 2)
1095 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->page_increment);
1097 spin->click_child = GTK_ARROW_DOWN;
1102 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1108 gtk_spin_button_button_release (GtkWidget *widget,
1109 GdkEventButton *event)
1111 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1114 arrow_size = spin_button_get_arrow_size (spin);
1116 if (event->button == spin->button)
1118 int click_child = spin->click_child;
1120 gtk_spin_button_stop_spinning (spin);
1122 if (event->button == 3)
1124 if (event->y >= 0 && event->x >= 0 &&
1125 event->y <= widget->requisition.height &&
1126 event->x <= arrow_size + 2 * widget->style->xthickness)
1128 if (click_child == GTK_ARROW_UP &&
1129 event->y <= widget->requisition.height / 2)
1133 diff = spin->adjustment->upper - spin->adjustment->value;
1135 gtk_spin_button_real_spin (spin, diff);
1137 else if (click_child == GTK_ARROW_DOWN &&
1138 event->y > widget->requisition.height / 2)
1142 diff = spin->adjustment->value - spin->adjustment->lower;
1144 gtk_spin_button_real_spin (spin, -diff);
1148 spin_button_redraw (spin);
1153 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1157 gtk_spin_button_motion_notify (GtkWidget *widget,
1158 GdkEventMotion *event)
1160 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1165 if (event->window == spin->panel)
1169 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1171 if (y <= widget->requisition.height / 2 &&
1172 spin->in_child == GTK_ARROW_DOWN)
1174 spin->in_child = GTK_ARROW_UP;
1175 spin_button_redraw (spin);
1177 else if (y > widget->requisition.height / 2 &&
1178 spin->in_child == GTK_ARROW_UP)
1180 spin->in_child = GTK_ARROW_DOWN;
1181 spin_button_redraw (spin);
1187 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1191 gtk_spin_button_timer (GtkSpinButton *spin_button)
1193 gboolean retval = FALSE;
1195 GDK_THREADS_ENTER ();
1197 if (spin_button->timer)
1199 if (spin_button->click_child == GTK_ARROW_UP)
1200 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1202 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1204 if (spin_button->need_timer)
1206 spin_button->need_timer = FALSE;
1207 spin_button->timer = g_timeout_add (SPIN_BUTTON_TIMER_DELAY,
1208 (GSourceFunc) gtk_spin_button_timer,
1209 (gpointer) spin_button);
1213 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1214 < spin_button->adjustment->page_increment)
1216 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1217 spin_button->timer_calls++;
1220 spin_button->timer_calls = 0;
1221 spin_button->timer_step += spin_button->climb_rate;
1228 GDK_THREADS_LEAVE ();
1234 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1235 GtkSpinButton *spin_button)
1237 gboolean return_val;
1239 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1242 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1243 if (return_val == FALSE)
1244 gtk_spin_button_default_output (spin_button);
1246 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1248 spin_button_redraw (spin_button);
1250 g_object_notify (G_OBJECT (spin_button), "value");
1254 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1255 GtkScrollType scroll)
1257 /* We don't test whether the entry is editable, since
1258 * this key binding conceptually corresponds to changing
1259 * the value with the buttons using the mouse, which
1260 * we allow for non-editable spin buttons.
1264 case GTK_SCROLL_STEP_BACKWARD:
1265 case GTK_SCROLL_STEP_DOWN:
1266 case GTK_SCROLL_STEP_LEFT:
1267 gtk_spin_button_real_spin (spin, -spin->timer_step);
1269 if (spin->climb_rate > 0.0 && spin->timer_step
1270 < spin->adjustment->page_increment)
1272 if (spin->timer_calls < MAX_TIMER_CALLS)
1273 spin->timer_calls++;
1276 spin->timer_calls = 0;
1277 spin->timer_step += spin->climb_rate;
1282 case GTK_SCROLL_STEP_FORWARD:
1283 case GTK_SCROLL_STEP_UP:
1284 case GTK_SCROLL_STEP_RIGHT:
1285 gtk_spin_button_real_spin (spin, spin->timer_step);
1287 if (spin->climb_rate > 0.0 && spin->timer_step
1288 < spin->adjustment->page_increment)
1290 if (spin->timer_calls < MAX_TIMER_CALLS)
1291 spin->timer_calls++;
1294 spin->timer_calls = 0;
1295 spin->timer_step += spin->climb_rate;
1300 case GTK_SCROLL_PAGE_BACKWARD:
1301 case GTK_SCROLL_PAGE_DOWN:
1302 case GTK_SCROLL_PAGE_LEFT:
1303 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1306 case GTK_SCROLL_PAGE_FORWARD:
1307 case GTK_SCROLL_PAGE_UP:
1308 case GTK_SCROLL_PAGE_RIGHT:
1309 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1312 case GTK_SCROLL_START:
1314 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1316 gtk_spin_button_real_spin (spin, -diff);
1320 case GTK_SCROLL_END:
1322 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1324 gtk_spin_button_real_spin (spin, diff);
1329 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1333 gtk_spin_button_update (spin);
1337 gtk_spin_button_key_release (GtkWidget *widget,
1340 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1342 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1343 spin->timer_step = spin->adjustment->step_increment;
1344 spin->timer_calls = 0;
1350 gtk_spin_button_snap (GtkSpinButton *spin_button,
1356 inc = spin_button->adjustment->step_increment;
1360 tmp = (val - spin_button->adjustment->lower) / inc;
1361 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1362 val = spin_button->adjustment->lower + floor (tmp) * inc;
1364 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1366 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1367 gtk_adjustment_set_value (spin_button->adjustment, val);
1370 gint return_val = FALSE;
1371 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1372 if (return_val == FALSE)
1373 gtk_spin_button_default_output (spin_button);
1378 gtk_spin_button_activate (GtkEntry *entry)
1380 if (entry->editable)
1381 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1383 /* Chain up so that entry->activates_default is honored */
1384 parent_class->activate (entry);
1388 gtk_spin_button_insert_text (GtkEditable *editable,
1389 const gchar *new_text,
1390 gint new_text_length,
1393 GtkEntry *entry = GTK_ENTRY (editable);
1394 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1395 GtkEditableClass *parent_editable_iface = g_type_interface_peek (parent_class, GTK_TYPE_EDITABLE);
1407 entry_length = entry->text_length;
1411 if (*(lc->negative_sign))
1412 neg_sign = *(lc->negative_sign);
1416 if (*(lc->positive_sign))
1417 pos_sign = *(lc->positive_sign);
1421 for (sign=0, i=0; i<entry_length; i++)
1422 if ((entry->text[i] == neg_sign) ||
1423 (entry->text[i] == pos_sign))
1429 if (sign && !(*position))
1432 for (dotpos=-1, i=0; i<entry_length; i++)
1433 if (entry->text[i] == *(lc->decimal_point))
1439 if (dotpos > -1 && *position > dotpos &&
1440 (gint)spin->digits - entry_length
1441 + dotpos - new_text_length + 1 < 0)
1444 for (i = 0; i < new_text_length; i++)
1446 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1448 if (sign || (*position) || i)
1452 else if (new_text[i] == *(lc->decimal_point))
1454 if (!spin->digits || dotpos > -1 ||
1455 (new_text_length - 1 - i + entry_length
1456 - *position > (gint)spin->digits))
1458 dotpos = *position + i;
1460 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1465 parent_editable_iface->insert_text (editable, new_text,
1466 new_text_length, position);
1470 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1474 gdouble new_value = 0.0;
1476 adj = spin_button->adjustment;
1478 new_value = adj->value + increment;
1482 if (spin_button->wrap)
1484 if (fabs (adj->value - adj->upper) < EPSILON)
1485 new_value = adj->lower;
1486 else if (new_value > adj->upper)
1487 new_value = adj->upper;
1490 new_value = MIN (new_value, adj->upper);
1492 else if (increment < 0)
1494 if (spin_button->wrap)
1496 if (fabs (adj->value - adj->lower) < EPSILON)
1497 new_value = adj->upper;
1498 else if (new_value < adj->lower)
1499 new_value = adj->lower;
1502 new_value = MAX (new_value, adj->lower);
1505 if (fabs (new_value - adj->value) > EPSILON)
1506 gtk_adjustment_set_value (adj, new_value);
1508 spin_button_redraw (spin_button);
1512 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1517 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1519 return GTK_INPUT_ERROR;
1525 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1527 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1529 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1530 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1536 /***********************************************************
1537 ***********************************************************
1538 *** Public interface ***
1539 ***********************************************************
1540 ***********************************************************/
1544 gtk_spin_button_configure (GtkSpinButton *spin_button,
1545 GtkAdjustment *adjustment,
1549 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1552 gtk_spin_button_set_adjustment (spin_button, adjustment);
1554 adjustment = spin_button->adjustment;
1556 g_object_freeze_notify (G_OBJECT (spin_button));
1557 if (spin_button->digits != digits)
1559 spin_button->digits = digits;
1560 g_object_notify (G_OBJECT (spin_button), "digits");
1563 if (spin_button->climb_rate != climb_rate)
1565 spin_button->climb_rate = climb_rate;
1566 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1568 g_object_thaw_notify (G_OBJECT (spin_button));
1570 gtk_adjustment_value_changed (adjustment);
1574 gtk_spin_button_new (GtkAdjustment *adjustment,
1578 GtkSpinButton *spin;
1581 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1583 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1585 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1587 return GTK_WIDGET (spin);
1591 * gtk_spin_button_new_with_range:
1592 * @min: Minimum allowable value
1593 * @max: Maximum allowable value
1594 * @step: Increment added or subtracted by spinning the widget
1596 * This is a convenience constructor that allows creation of a numeric
1597 * #GtkSpinButton without manually creating an adjustment. The value is
1598 * initially set to the minimum value and a page increment of 10 * @step
1599 * is the default. The precision of the spin button is equivalent to the
1600 * precision of @step.
1602 * Note that the way in which the precision is derived works best if @step
1603 * is a power of ten. If the resulting precision is not suitable for your
1604 * needs, use gtk_spin_button_set_digits() to correct it.
1606 * Return value: The new spin button as a #GtkWidget.
1609 gtk_spin_button_new_with_range (gdouble min,
1614 GtkSpinButton *spin;
1617 g_return_val_if_fail (min <= max, NULL);
1618 g_return_val_if_fail (step != 0.0, NULL);
1620 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1622 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1624 if (fabs (step) >= 1.0 || step == 0.0)
1627 digits = abs ((gint) floor (log10 (fabs (step))));
1628 if (digits > MAX_DIGITS)
1629 digits = MAX_DIGITS;
1632 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1634 gtk_spin_button_set_numeric (spin, TRUE);
1636 return GTK_WIDGET (spin);
1639 /* Callback used when the spin button's adjustment changes. We need to redraw
1640 * the arrows when the adjustment's range changes, and reevaluate our size request.
1643 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1645 GtkSpinButton *spin_button;
1647 spin_button = GTK_SPIN_BUTTON (data);
1649 spin_button->timer_step = spin_button->adjustment->step_increment;
1650 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1654 * gtk_spin_button_set_adjustment:
1655 * @spin_button: a #GtkSpinButton
1656 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1658 * Replaces the #GtkAdjustment associated with @spin_button.
1661 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1662 GtkAdjustment *adjustment)
1664 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1666 if (spin_button->adjustment != adjustment)
1668 if (spin_button->adjustment)
1670 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1671 gtk_spin_button_value_changed,
1673 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1674 adjustment_changed_cb,
1676 g_object_unref (spin_button->adjustment);
1678 spin_button->adjustment = adjustment;
1681 g_object_ref (adjustment);
1682 gtk_object_sink (GTK_OBJECT (adjustment));
1683 g_signal_connect (adjustment, "value_changed",
1684 G_CALLBACK (gtk_spin_button_value_changed),
1686 g_signal_connect (adjustment, "changed",
1687 G_CALLBACK (adjustment_changed_cb),
1689 spin_button->timer_step = spin_button->adjustment->step_increment;
1692 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1695 g_object_notify (G_OBJECT (spin_button), "adjustment");
1699 * gtk_spin_button_get_adjustment:
1700 * @spin_button: a #GtkSpinButton
1702 * Get the adjustment associated with a #GtkSpinButton
1704 * Return value: the #GtkAdjustment of @spin_button
1707 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1709 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1711 return spin_button->adjustment;
1715 * gtk_spin_button_set_digits:
1716 * @spin_button: a #GtkSpinButton
1717 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1719 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1723 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1726 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1728 if (spin_button->digits != digits)
1730 spin_button->digits = digits;
1731 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1732 g_object_notify (G_OBJECT (spin_button), "digits");
1734 /* since lower/upper may have changed */
1735 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1740 * gtk_spin_button_get_digits:
1741 * @spin_button: a #GtkSpinButton
1743 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1745 * Returns: the current precision
1748 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1750 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1752 return spin_button->digits;
1756 * gtk_spin_button_set_increments:
1757 * @spin_button: a #GtkSpinButton
1758 * @step: increment applied for a button 1 press.
1759 * @page: increment applied for a button 2 press.
1761 * Sets the step and page increments for spin_button. This affects how
1762 * quickly the value changes when the spin button's arrows are activated.
1765 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1769 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1771 spin_button->adjustment->step_increment = step;
1772 spin_button->adjustment->page_increment = page;
1776 * gtk_spin_button_get_increments:
1777 * @spin_button: a #GtkSpinButton
1778 * @step: location to store step increment, or %NULL
1779 * @page: location to store page increment, or %NULL
1781 * Gets the current step and page the increments used by @spin_button. See
1782 * gtk_spin_button_set_increments().
1785 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1789 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1792 *step = spin_button->adjustment->step_increment;
1794 *page = spin_button->adjustment->page_increment;
1798 * gtk_spin_button_set_range:
1799 * @spin_button: a #GtkSpinButton
1800 * @min: minimum allowable value
1801 * @max: maximum allowable value
1803 * Sets the minimum and maximum allowable values for @spin_button
1806 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1812 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1814 spin_button->adjustment->lower = min;
1815 spin_button->adjustment->upper = max;
1817 value = CLAMP (spin_button->adjustment->value,
1818 spin_button->adjustment->lower,
1819 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1821 if (value != spin_button->adjustment->value)
1822 gtk_spin_button_set_value (spin_button, value);
1824 gtk_adjustment_changed (spin_button->adjustment);
1828 * gtk_spin_button_get_range:
1829 * @spin_button: a #GtkSpinButton
1830 * @min: location to store minimum allowed value, or %NULL
1831 * @max: location to store maximum allowed value, or %NULL
1833 * Gets the range allowed for @spin_button. See
1834 * gtk_spin_button_set_range().
1837 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1841 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1844 *min = spin_button->adjustment->lower;
1846 *max = spin_button->adjustment->upper;
1850 * gtk_spin_button_get_value:
1851 * @spin_button: a #GtkSpinButton
1853 * Get the value in the @spin_button.
1855 * Return value: the value of @spin_button
1858 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1860 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1862 return spin_button->adjustment->value;
1866 * gtk_spin_button_get_value_as_int:
1867 * @spin_button: a #GtkSpinButton
1869 * Get the value @spin_button represented as an integer.
1871 * Return value: the value of @spin_button
1874 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1878 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1880 val = spin_button->adjustment->value;
1881 if (val - floor (val) < ceil (val) - val)
1888 * gtk_spin_button_set_value:
1889 * @spin_button: a #GtkSpinButton
1890 * @value: the new value
1892 * Set the value of @spin_button.
1895 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1898 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1900 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1901 gtk_adjustment_set_value (spin_button->adjustment, value);
1904 gint return_val = FALSE;
1905 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1906 if (return_val == FALSE)
1907 gtk_spin_button_default_output (spin_button);
1912 * gtk_spin_button_set_update_policy:
1913 * @spin_button: a #GtkSpinButton
1914 * @policy: a #GtkSpinButtonUpdatePolicy value
1916 * Sets the update behavior of a spin button. This determines whether the
1917 * spin button is always updated or only when a valid value is set.
1920 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1921 GtkSpinButtonUpdatePolicy policy)
1923 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1925 if (spin_button->update_policy != policy)
1927 spin_button->update_policy = policy;
1928 g_object_notify (G_OBJECT (spin_button), "update-policy");
1933 * gtk_spin_button_get_update_policy:
1934 * @spin_button: a #GtkSpinButton
1936 * Gets the update behavior of a spin button. See
1937 * gtk_spin_button_set_update_policy().
1939 * Return value: the current update policy
1941 GtkSpinButtonUpdatePolicy
1942 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1944 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1946 return spin_button->update_policy;
1950 * gtk_spin_button_set_numeric:
1951 * @spin_button: a #GtkSpinButton
1952 * @numeric: flag indicating if only numeric entry is allowed.
1954 * Sets the flag that determines if non-numeric text can be typed into
1958 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1961 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1963 numeric = numeric != FALSE;
1965 if (spin_button->numeric != numeric)
1967 spin_button->numeric = numeric;
1968 g_object_notify (G_OBJECT (spin_button), "numeric");
1973 * gtk_spin_button_get_numeric:
1974 * @spin_button: a #GtkSpinButton
1976 * Returns whether non-numeric text can be typed into the spin button.
1977 * See gtk_spin_button_set_numeric().
1979 * Return value: %TRUE if only numeric text can be entered
1982 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1984 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1986 return spin_button->numeric;
1990 * gtk_spin_button_set_wrap:
1991 * @spin_button: a #GtkSpinButton
1992 * @wrap: a flag indicating if wrapping behavior is performed.
1994 * Sets the flag that determines if a spin button value wraps around to the
1995 * opposite limit when the upper or lower limit of the range is exceeded.
1998 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2001 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2003 wrap = wrap != FALSE;
2005 if (spin_button->wrap != wrap)
2007 spin_button->wrap = (wrap != 0);
2009 g_object_notify (G_OBJECT (spin_button), "wrap");
2014 * gtk_spin_button_get_wrap:
2015 * @spin_button: a #GtkSpinButton
2017 * Returns whether the spin button's value wraps around to the
2018 * opposite limit when the upper or lower limit of the range is
2019 * exceeded. See gtk_spin_button_set_wrap().
2021 * Return value: %TRUE if the spin button wraps around
2024 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2026 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2028 return spin_button->wrap;
2032 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2034 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
2037 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2039 return arrow_size - arrow_size % 2; /* force even */
2043 * spin_button_get_shadow_type:
2044 * @spin_button: a #GtkSpinButton
2046 * Convenience function to Get the shadow type from the underlying widget's
2049 * Return value: the #GtkShadowType
2052 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2054 GtkShadowType rc_shadow_type;
2056 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow-type", &rc_shadow_type, NULL);
2058 return rc_shadow_type;
2062 * gtk_spin_button_set_snap_to_ticks:
2063 * @spin_button: a #GtkSpinButton
2064 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2066 * Sets the policy as to whether values are corrected to the nearest step
2067 * increment when a spin button is activated after providing an invalid value.
2070 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2071 gboolean snap_to_ticks)
2075 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2077 new_val = (snap_to_ticks != 0);
2079 if (new_val != spin_button->snap_to_ticks)
2081 spin_button->snap_to_ticks = new_val;
2082 if (new_val && GTK_ENTRY (spin_button)->editable)
2083 gtk_spin_button_update (spin_button);
2085 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2090 * gtk_spin_button_get_snap_to_ticks:
2091 * @spin_button: a #GtkSpinButton
2093 * Returns whether the values are corrected to the nearest step. See
2094 * gtk_spin_button_set_snap_to_ticks().
2096 * Return value: %TRUE if values are snapped to the nearest step.
2099 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2101 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2103 return spin_button->snap_to_ticks;
2107 * gtk_spin_button_spin:
2108 * @spin_button: a #GtkSpinButton
2109 * @direction: a #GtkSpinType indicating the direction to spin.
2110 * @increment: step increment to apply in the specified direction.
2112 * Increment or decrement a spin button's value in a specified direction
2113 * by a specified amount.
2116 gtk_spin_button_spin (GtkSpinButton *spin_button,
2117 GtkSpinType direction,
2123 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2125 adj = spin_button->adjustment;
2127 /* for compatibility with the 1.0.x version of this function */
2128 if (increment != 0 && increment != adj->step_increment &&
2129 (direction == GTK_SPIN_STEP_FORWARD ||
2130 direction == GTK_SPIN_STEP_BACKWARD))
2132 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2133 increment = -increment;
2134 direction = GTK_SPIN_USER_DEFINED;
2139 case GTK_SPIN_STEP_FORWARD:
2141 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2144 case GTK_SPIN_STEP_BACKWARD:
2146 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2149 case GTK_SPIN_PAGE_FORWARD:
2151 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2154 case GTK_SPIN_PAGE_BACKWARD:
2156 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2161 diff = adj->value - adj->lower;
2163 gtk_spin_button_real_spin (spin_button, -diff);
2168 diff = adj->upper - adj->value;
2170 gtk_spin_button_real_spin (spin_button, diff);
2173 case GTK_SPIN_USER_DEFINED:
2176 gtk_spin_button_real_spin (spin_button, increment);
2185 * gtk_spin_button_update:
2186 * @spin_button: a #GtkSpinButton
2188 * Manually force an update of the spin button.
2191 gtk_spin_button_update (GtkSpinButton *spin_button)
2197 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2200 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2201 if (return_val == FALSE)
2203 return_val = gtk_spin_button_default_input (spin_button, &val);
2204 error = (return_val == GTK_INPUT_ERROR);
2206 else if (return_val == GTK_INPUT_ERROR)
2209 spin_button_redraw (spin_button);
2211 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2213 if (val < spin_button->adjustment->lower)
2214 val = spin_button->adjustment->lower;
2215 else if (val > spin_button->adjustment->upper)
2216 val = spin_button->adjustment->upper;
2218 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2220 val < spin_button->adjustment->lower ||
2221 val > spin_button->adjustment->upper))
2223 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2227 if (spin_button->snap_to_ticks)
2228 gtk_spin_button_snap (spin_button, val);
2231 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2232 gtk_adjustment_set_value (spin_button->adjustment, val);
2236 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0,
2238 if (return_val == FALSE)
2239 gtk_spin_button_default_output (spin_button);
2245 spin_button_redraw (GtkSpinButton *spin_button)
2249 widget = GTK_WIDGET (spin_button);
2251 if (GTK_WIDGET_DRAWABLE (widget))
2253 gtk_widget_queue_draw (widget);
2255 /* We must invalidate the panel window ourselves, because it
2256 * is not a child of widget->window
2258 gdk_window_invalidate_rect (spin_button->panel, NULL, TRUE);
2262 #define __GTK_SPIN_BUTTON_C__
2263 #include "gtkaliasdef.c"