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"
44 #define MIN_SPIN_BUTTON_WIDTH 30
45 #define SPIN_BUTTON_INITIAL_TIMER_DELAY 200
46 #define SPIN_BUTTON_TIMER_DELAY 20
47 #define MAX_TIMER_CALLS 5
50 #define MIN_ARROW_WIDTH 6
74 static void gtk_spin_button_class_init (GtkSpinButtonClass *klass);
75 static void gtk_spin_button_editable_init (GtkEditableClass *iface);
76 static void gtk_spin_button_init (GtkSpinButton *spin_button);
77 static void gtk_spin_button_finalize (GObject *object);
78 static void gtk_spin_button_destroy (GtkObject *object);
79 static void gtk_spin_button_set_property (GObject *object,
83 static void gtk_spin_button_get_property (GObject *object,
87 static void gtk_spin_button_map (GtkWidget *widget);
88 static void gtk_spin_button_unmap (GtkWidget *widget);
89 static void gtk_spin_button_realize (GtkWidget *widget);
90 static void gtk_spin_button_unrealize (GtkWidget *widget);
91 static void gtk_spin_button_size_request (GtkWidget *widget,
92 GtkRequisition *requisition);
93 static void gtk_spin_button_size_allocate (GtkWidget *widget,
94 GtkAllocation *allocation);
95 static gint gtk_spin_button_expose (GtkWidget *widget,
96 GdkEventExpose *event);
97 static gint gtk_spin_button_button_press (GtkWidget *widget,
98 GdkEventButton *event);
99 static gint gtk_spin_button_button_release (GtkWidget *widget,
100 GdkEventButton *event);
101 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
102 GdkEventMotion *event);
103 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
104 GdkEventCrossing *event);
105 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
106 GdkEventCrossing *event);
107 static gint gtk_spin_button_focus_out (GtkWidget *widget,
108 GdkEventFocus *event);
109 static void gtk_spin_button_grab_notify (GtkWidget *widget,
110 gboolean was_grabbed);
111 static void gtk_spin_button_state_changed (GtkWidget *widget,
112 GtkStateType previous_state);
113 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
114 GtkArrowType arrow_type);
115 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
116 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
117 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
118 GtkSpinButton *spin_button);
119 static gint gtk_spin_button_key_release (GtkWidget *widget,
121 static gint gtk_spin_button_scroll (GtkWidget *widget,
122 GdkEventScroll *event);
123 static void gtk_spin_button_activate (GtkEntry *entry);
124 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
126 static void gtk_spin_button_insert_text (GtkEditable *editable,
127 const gchar *new_text,
128 gint new_text_length,
130 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
132 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
133 GtkScrollType scroll);
135 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
137 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
139 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
140 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
141 static void spin_button_redraw (GtkSpinButton *spin_button);
144 static GtkEntryClass *parent_class = NULL;
145 static guint spinbutton_signals[LAST_SIGNAL] = {0};
150 gtk_spin_button_get_type (void)
152 static GType spin_button_type = 0;
154 if (!spin_button_type)
156 static const GTypeInfo spin_button_info =
158 sizeof (GtkSpinButtonClass),
159 NULL, /* base_init */
160 NULL, /* base_finalize */
161 (GClassInitFunc) gtk_spin_button_class_init,
162 NULL, /* class_finalize */
163 NULL, /* class_data */
164 sizeof (GtkSpinButton),
166 (GInstanceInitFunc) gtk_spin_button_init,
169 static const GInterfaceInfo editable_info =
171 (GInterfaceInitFunc) gtk_spin_button_editable_init, /* interface_init */
172 NULL, /* interface_finalize */
173 NULL /* interface_data */
177 g_type_register_static (GTK_TYPE_ENTRY, "GtkSpinButton",
178 &spin_button_info, 0);
180 g_type_add_interface_static (spin_button_type,
184 return spin_button_type;
187 #define add_spin_binding(binding_set, keyval, mask, scroll) \
188 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
190 GTK_TYPE_SCROLL_TYPE, scroll)
193 gtk_spin_button_class_init (GtkSpinButtonClass *class)
195 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
196 GtkObjectClass *object_class;
197 GtkWidgetClass *widget_class;
198 GtkEntryClass *entry_class;
199 GtkBindingSet *binding_set;
201 object_class = (GtkObjectClass*) class;
202 widget_class = (GtkWidgetClass*) class;
203 entry_class = (GtkEntryClass*) class;
205 parent_class = g_type_class_peek_parent (class);
207 gobject_class->finalize = gtk_spin_button_finalize;
209 gobject_class->set_property = gtk_spin_button_set_property;
210 gobject_class->get_property = gtk_spin_button_get_property;
212 object_class->destroy = gtk_spin_button_destroy;
214 widget_class->map = gtk_spin_button_map;
215 widget_class->unmap = gtk_spin_button_unmap;
216 widget_class->realize = gtk_spin_button_realize;
217 widget_class->unrealize = gtk_spin_button_unrealize;
218 widget_class->size_request = gtk_spin_button_size_request;
219 widget_class->size_allocate = gtk_spin_button_size_allocate;
220 widget_class->expose_event = gtk_spin_button_expose;
221 widget_class->scroll_event = gtk_spin_button_scroll;
222 widget_class->button_press_event = gtk_spin_button_button_press;
223 widget_class->button_release_event = gtk_spin_button_button_release;
224 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
225 widget_class->key_release_event = gtk_spin_button_key_release;
226 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
227 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
228 widget_class->focus_out_event = gtk_spin_button_focus_out;
229 widget_class->grab_notify = gtk_spin_button_grab_notify;
230 widget_class->state_changed = gtk_spin_button_state_changed;
232 entry_class->activate = gtk_spin_button_activate;
235 class->output = NULL;
236 class->change_value = gtk_spin_button_real_change_value;
238 g_object_class_install_property (gobject_class,
240 g_param_spec_object ("adjustment",
242 P_("The adjustment that holds the value of the spinbutton"),
246 g_object_class_install_property (gobject_class,
248 g_param_spec_double ("climb_rate",
250 P_("The acceleration rate when you hold down a button"),
256 g_object_class_install_property (gobject_class,
258 g_param_spec_uint ("digits",
260 P_("The number of decimal places to display"),
266 g_object_class_install_property (gobject_class,
268 g_param_spec_boolean ("snap_to_ticks",
270 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
274 g_object_class_install_property (gobject_class,
276 g_param_spec_boolean ("numeric",
278 P_("Whether non-numeric characters should be ignored"),
282 g_object_class_install_property (gobject_class,
284 g_param_spec_boolean ("wrap",
286 P_("Whether a spin button should wrap upon reaching its limits"),
290 g_object_class_install_property (gobject_class,
292 g_param_spec_enum ("update_policy",
294 P_("Whether the spin button should update always, or only when the value is legal"),
295 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
299 g_object_class_install_property (gobject_class,
301 g_param_spec_double ("value",
303 P_("Reads the current value, or sets a new value"),
309 gtk_widget_class_install_style_property_parser (widget_class,
310 g_param_spec_enum ("shadow_type",
312 P_("Style of bevel around the spin button"),
313 GTK_TYPE_SHADOW_TYPE,
316 gtk_rc_property_parse_enum);
317 spinbutton_signals[INPUT] =
318 g_signal_new ("input",
319 G_TYPE_FROM_CLASS (gobject_class),
321 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
323 _gtk_marshal_INT__POINTER,
327 spinbutton_signals[OUTPUT] =
328 g_signal_new ("output",
329 G_TYPE_FROM_CLASS (gobject_class),
331 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
332 _gtk_boolean_handled_accumulator, NULL,
333 _gtk_marshal_BOOLEAN__VOID,
336 spinbutton_signals[VALUE_CHANGED] =
337 g_signal_new ("value_changed",
338 G_TYPE_FROM_CLASS (gobject_class),
340 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
342 _gtk_marshal_VOID__VOID,
346 spinbutton_signals[CHANGE_VALUE] =
347 g_signal_new ("change_value",
348 G_TYPE_FROM_CLASS (gobject_class),
349 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
350 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
352 _gtk_marshal_VOID__ENUM,
354 GTK_TYPE_SCROLL_TYPE);
356 binding_set = gtk_binding_set_by_class (class);
358 add_spin_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
359 add_spin_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
360 add_spin_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
361 add_spin_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
362 add_spin_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
363 add_spin_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
364 add_spin_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
365 add_spin_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
369 gtk_spin_button_editable_init (GtkEditableClass *iface)
371 iface->insert_text = gtk_spin_button_insert_text;
375 gtk_spin_button_set_property (GObject *object,
380 GtkSpinButton *spin_button;
382 spin_button = GTK_SPIN_BUTTON (object);
386 GtkAdjustment *adjustment;
388 case PROP_ADJUSTMENT:
389 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
391 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
392 gtk_spin_button_set_adjustment (spin_button, adjustment);
394 case PROP_CLIMB_RATE:
395 gtk_spin_button_configure (spin_button,
396 spin_button->adjustment,
397 g_value_get_double (value),
398 spin_button->digits);
401 gtk_spin_button_configure (spin_button,
402 spin_button->adjustment,
403 spin_button->climb_rate,
404 g_value_get_uint (value));
406 case PROP_SNAP_TO_TICKS:
407 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
410 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
413 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
415 case PROP_UPDATE_POLICY:
416 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
419 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
427 gtk_spin_button_get_property (GObject *object,
432 GtkSpinButton *spin_button;
434 spin_button = GTK_SPIN_BUTTON (object);
438 case PROP_ADJUSTMENT:
439 g_value_set_object (value, spin_button->adjustment);
441 case PROP_CLIMB_RATE:
442 g_value_set_double (value, spin_button->climb_rate);
445 g_value_set_uint (value, spin_button->digits);
447 case PROP_SNAP_TO_TICKS:
448 g_value_set_boolean (value, spin_button->snap_to_ticks);
451 g_value_set_boolean (value, spin_button->numeric);
454 g_value_set_boolean (value, spin_button->wrap);
456 case PROP_UPDATE_POLICY:
457 g_value_set_enum (value, spin_button->update_policy);
460 g_value_set_double (value, spin_button->adjustment->value);
463 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
469 gtk_spin_button_init (GtkSpinButton *spin_button)
471 spin_button->adjustment = NULL;
472 spin_button->panel = NULL;
473 spin_button->timer = 0;
474 spin_button->climb_rate = 0.0;
475 spin_button->timer_step = 0.0;
476 spin_button->update_policy = GTK_UPDATE_ALWAYS;
477 spin_button->in_child = NO_ARROW;
478 spin_button->click_child = NO_ARROW;
479 spin_button->button = 0;
480 spin_button->need_timer = FALSE;
481 spin_button->timer_calls = 0;
482 spin_button->digits = 0;
483 spin_button->numeric = FALSE;
484 spin_button->wrap = FALSE;
485 spin_button->snap_to_ticks = FALSE;
487 gtk_spin_button_set_adjustment (spin_button,
488 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
492 gtk_spin_button_finalize (GObject *object)
494 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
496 G_OBJECT_CLASS (parent_class)->finalize (object);
500 gtk_spin_button_destroy (GtkObject *object)
502 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
504 GTK_OBJECT_CLASS (parent_class)->destroy (object);
508 gtk_spin_button_map (GtkWidget *widget)
510 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
512 GTK_WIDGET_CLASS (parent_class)->map (widget);
513 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
518 gtk_spin_button_unmap (GtkWidget *widget)
520 if (GTK_WIDGET_MAPPED (widget))
522 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
523 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
528 gtk_spin_button_realize (GtkWidget *widget)
530 GtkSpinButton *spin_button;
531 GdkWindowAttr attributes;
532 gint attributes_mask;
537 spin_button = GTK_SPIN_BUTTON (widget);
538 arrow_size = spin_button_get_arrow_size (spin_button);
540 real_width = widget->allocation.width;
541 widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
542 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
543 GDK_KEY_RELEASE_MASK);
544 GTK_WIDGET_CLASS (parent_class)->realize (widget);
546 widget->allocation.width = real_width;
548 attributes.window_type = GDK_WINDOW_CHILD;
549 attributes.wclass = GDK_INPUT_OUTPUT;
550 attributes.visual = gtk_widget_get_visual (widget);
551 attributes.colormap = gtk_widget_get_colormap (widget);
552 attributes.event_mask = gtk_widget_get_events (widget);
553 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
554 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
555 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
557 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
559 attributes.x = (widget->allocation.x +
560 widget->allocation.width - arrow_size -
561 2 * widget->style->xthickness);
562 attributes.y = widget->allocation.y + (widget->allocation.height -
563 widget->requisition.height) / 2;
564 attributes.width = arrow_size + 2 * widget->style->xthickness;
565 attributes.height = widget->requisition.height;
567 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
568 &attributes, attributes_mask);
569 gdk_window_set_user_data (spin_button->panel, widget);
571 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
574 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
575 if (return_val == FALSE)
576 gtk_spin_button_default_output (spin_button);
578 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
582 gtk_spin_button_unrealize (GtkWidget *widget)
584 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
586 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
590 gdk_window_set_user_data (spin->panel, NULL);
591 gdk_window_destroy (spin->panel);
597 compute_double_length (double val, int digits)
603 if (fabs (val) > 1.0)
604 a = floor (log10 (fabs (val))) + 1;
616 return a + digits + extra;
620 gtk_spin_button_size_request (GtkWidget *widget,
621 GtkRequisition *requisition)
624 GtkSpinButton *spin_button;
627 entry = GTK_ENTRY (widget);
628 spin_button = GTK_SPIN_BUTTON (widget);
629 arrow_size = spin_button_get_arrow_size (spin_button);
631 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
633 if (entry->width_chars < 0)
635 PangoContext *context;
636 PangoFontMetrics *metrics;
642 gboolean interior_focus;
645 gtk_widget_style_get (widget,
646 "interior-focus", &interior_focus,
647 "focus-line-width", &focus_width,
650 context = gtk_widget_get_pango_context (widget);
651 metrics = pango_context_get_metrics (context,
652 widget->style->font_desc,
653 pango_context_get_language (context));
655 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
656 digit_width = PANGO_SCALE *
657 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
659 pango_font_metrics_unref (metrics);
661 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
663 width = MIN_SPIN_BUTTON_WIDTH;
664 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
665 spin_button->digits));
667 string_len = compute_double_length (spin_button->adjustment->upper,
668 spin_button->digits);
669 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
670 width = MAX (width, w);
671 string_len = compute_double_length (spin_button->adjustment->lower,
672 spin_button->digits);
673 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
674 width = MAX (width, w);
676 requisition->width = width + /* INNER_BORDER */ 2 * 2;
678 requisition->width += 2 * focus_width;
681 requisition->width += arrow_size + 2 * widget->style->xthickness;
685 gtk_spin_button_size_allocate (GtkWidget *widget,
686 GtkAllocation *allocation)
689 GtkAllocation entry_allocation;
690 GtkAllocation panel_allocation;
694 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
695 g_return_if_fail (allocation != NULL);
697 spin = GTK_SPIN_BUTTON (widget);
698 arrow_size = spin_button_get_arrow_size (spin);
699 panel_width = arrow_size + 2 * widget->style->xthickness;
701 widget->allocation = *allocation;
703 entry_allocation = *allocation;
704 entry_allocation.width -= panel_width;
706 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
708 entry_allocation.x += panel_width;
709 panel_allocation.x = allocation->x;
713 panel_allocation.x = allocation->x + allocation->width - panel_width;
716 panel_allocation.width = panel_width;
717 panel_allocation.height = MIN (widget->requisition.height, allocation->height);
719 panel_allocation.y = allocation->y + (allocation->height -
720 panel_allocation.height) / 2;
722 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &entry_allocation);
724 if (GTK_WIDGET_REALIZED (widget))
726 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
729 panel_allocation.width,
730 panel_allocation.height);
733 spin_button_redraw (spin);
737 gtk_spin_button_expose (GtkWidget *widget,
738 GdkEventExpose *event)
742 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
743 g_return_val_if_fail (event != NULL, FALSE);
745 spin = GTK_SPIN_BUTTON (widget);
747 if (GTK_WIDGET_DRAWABLE (widget))
749 GtkShadowType shadow_type;
752 if (event->window != spin->panel)
753 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
755 /* we redraw the panel even if it wasn't exposed. This is
756 * because spin->panel is not a child window of widget->window,
757 * so it will not be invalidated by eg. gtk_widget_queue_draw().
762 gdk_drawable_get_size (spin->panel, &rect.width, &rect.height);
764 shadow_type = spin_button_get_shadow_type (spin);
766 gdk_window_begin_paint_rect (spin->panel, &rect);
768 if (shadow_type != GTK_SHADOW_NONE)
770 gtk_paint_box (widget->style, spin->panel,
771 GTK_STATE_NORMAL, shadow_type,
772 NULL, widget, "spinbutton",
773 rect.x, rect.y, rect.width, rect.height);
776 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
777 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
779 gdk_window_end_paint (spin->panel);
786 spin_button_at_limit (GtkSpinButton *spin_button,
789 if (spin_button->wrap)
792 if (arrow == GTK_ARROW_UP &&
793 (spin_button->adjustment->upper - spin_button->adjustment->value <= EPSILON))
796 if (arrow == GTK_ARROW_DOWN &&
797 (spin_button->adjustment->value - spin_button->adjustment->lower <= EPSILON))
804 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
805 GtkArrowType arrow_type)
807 GtkStateType state_type;
808 GtkShadowType shadow_type;
816 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
817 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
819 widget = GTK_WIDGET (spin_button);
821 if (GTK_WIDGET_DRAWABLE (widget))
823 width = spin_button_get_arrow_size (spin_button) + 2 * widget->style->xthickness;
825 if (arrow_type == GTK_ARROW_UP)
830 height = widget->requisition.height / 2;
835 y = widget->requisition.height / 2;
837 height = (widget->requisition.height + 1) / 2;
840 if (spin_button_at_limit (spin_button, arrow_type))
842 shadow_type = GTK_SHADOW_OUT;
843 state_type = GTK_STATE_INSENSITIVE;
847 if (spin_button->click_child == arrow_type)
849 state_type = GTK_STATE_ACTIVE;
850 shadow_type = GTK_SHADOW_IN;
854 if (spin_button->in_child == arrow_type &&
855 spin_button->click_child == NO_ARROW)
857 state_type = GTK_STATE_PRELIGHT;
861 state_type = GTK_WIDGET_STATE (widget);
864 shadow_type = GTK_SHADOW_OUT;
868 gtk_paint_box (widget->style, spin_button->panel,
869 state_type, shadow_type,
871 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
872 x, y, width, height);
874 height = widget->requisition.height;
876 if (arrow_type == GTK_ARROW_DOWN)
879 height = height - y - 2;
884 height = height / 2 - 2;
889 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
895 w -= w % 2 - 1; /* force odd */
898 x += (width - w) / 2;
899 y += (height - h) / 2;
904 gtk_paint_arrow (widget->style, spin_button->panel,
905 state_type, shadow_type,
906 NULL, widget, "spinbutton",
908 x, y, width, height);
913 gtk_spin_button_enter_notify (GtkWidget *widget,
914 GdkEventCrossing *event)
916 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
918 if (event->window == spin->panel)
923 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
925 if (y <= widget->requisition.height / 2)
926 spin->in_child = GTK_ARROW_UP;
928 spin->in_child = GTK_ARROW_DOWN;
930 spin_button_redraw (spin);
937 gtk_spin_button_leave_notify (GtkWidget *widget,
938 GdkEventCrossing *event)
940 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
942 spin->in_child = NO_ARROW;
943 spin_button_redraw (spin);
949 gtk_spin_button_focus_out (GtkWidget *widget,
950 GdkEventFocus *event)
952 if (GTK_ENTRY (widget)->editable)
953 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
955 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
959 gtk_spin_button_grab_notify (GtkWidget *widget,
960 gboolean was_grabbed)
962 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
966 gtk_spin_button_stop_spinning (spin);
967 spin_button_redraw (spin);
972 gtk_spin_button_state_changed (GtkWidget *widget,
973 GtkStateType previous_state)
975 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
977 if (!GTK_WIDGET_IS_SENSITIVE (widget))
979 gtk_spin_button_stop_spinning (spin);
980 spin_button_redraw (spin);
985 gtk_spin_button_scroll (GtkWidget *widget,
986 GdkEventScroll *event)
988 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
990 if (event->direction == GDK_SCROLL_UP)
992 if (!GTK_WIDGET_HAS_FOCUS (widget))
993 gtk_widget_grab_focus (widget);
994 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
996 else if (event->direction == GDK_SCROLL_DOWN)
998 if (!GTK_WIDGET_HAS_FOCUS (widget))
999 gtk_widget_grab_focus (widget);
1000 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
1009 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1013 g_source_remove (spin->timer);
1015 spin->timer_calls = 0;
1016 spin->need_timer = FALSE;
1021 spin->timer_step = spin->adjustment->step_increment;
1022 spin->timer_calls = 0;
1024 spin->click_child = NO_ARROW;
1029 start_spinning (GtkSpinButton *spin,
1030 GtkArrowType click_child,
1033 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1035 spin->click_child = click_child;
1036 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1040 spin->timer_step = step;
1041 spin->need_timer = TRUE;
1042 spin->timer = g_timeout_add (SPIN_BUTTON_INITIAL_TIMER_DELAY,
1043 (GSourceFunc) gtk_spin_button_timer,
1047 spin_button_redraw (spin);
1051 gtk_spin_button_button_press (GtkWidget *widget,
1052 GdkEventButton *event)
1054 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1058 if (event->window == spin->panel)
1060 if (!GTK_WIDGET_HAS_FOCUS (widget))
1061 gtk_widget_grab_focus (widget);
1062 spin->button = event->button;
1064 if (GTK_ENTRY (widget)->editable)
1065 gtk_spin_button_update (spin);
1067 if (event->y <= widget->requisition.height / 2)
1069 if (event->button == 1)
1070 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->step_increment);
1071 else if (event->button == 2)
1072 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->page_increment);
1074 spin->click_child = GTK_ARROW_UP;
1078 if (event->button == 1)
1079 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->step_increment);
1080 else if (event->button == 2)
1081 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->page_increment);
1083 spin->click_child = GTK_ARROW_DOWN;
1088 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1094 gtk_spin_button_button_release (GtkWidget *widget,
1095 GdkEventButton *event)
1097 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1100 arrow_size = spin_button_get_arrow_size (spin);
1102 if (event->button == spin->button)
1104 int click_child = spin->click_child;
1106 gtk_spin_button_stop_spinning (spin);
1108 if (event->button == 3)
1110 if (event->y >= 0 && event->x >= 0 &&
1111 event->y <= widget->requisition.height &&
1112 event->x <= arrow_size + 2 * widget->style->xthickness)
1114 if (click_child == GTK_ARROW_UP &&
1115 event->y <= widget->requisition.height / 2)
1119 diff = spin->adjustment->upper - spin->adjustment->value;
1121 gtk_spin_button_real_spin (spin, diff);
1123 else if (click_child == GTK_ARROW_DOWN &&
1124 event->y > widget->requisition.height / 2)
1128 diff = spin->adjustment->value - spin->adjustment->lower;
1130 gtk_spin_button_real_spin (spin, -diff);
1134 spin_button_redraw (spin);
1139 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1143 gtk_spin_button_motion_notify (GtkWidget *widget,
1144 GdkEventMotion *event)
1146 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1151 if (event->window == spin->panel)
1155 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1157 if (y <= widget->requisition.height / 2 &&
1158 spin->in_child == GTK_ARROW_DOWN)
1160 spin->in_child = GTK_ARROW_UP;
1161 spin_button_redraw (spin);
1163 else if (y > widget->requisition.height / 2 &&
1164 spin->in_child == GTK_ARROW_UP)
1166 spin->in_child = GTK_ARROW_DOWN;
1167 spin_button_redraw (spin);
1173 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1177 gtk_spin_button_timer (GtkSpinButton *spin_button)
1179 gboolean retval = FALSE;
1181 GDK_THREADS_ENTER ();
1183 if (spin_button->timer)
1185 if (spin_button->click_child == GTK_ARROW_UP)
1186 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1188 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1190 if (spin_button->need_timer)
1192 spin_button->need_timer = FALSE;
1193 spin_button->timer = g_timeout_add (SPIN_BUTTON_TIMER_DELAY,
1194 (GSourceFunc) gtk_spin_button_timer,
1195 (gpointer) spin_button);
1199 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1200 < spin_button->adjustment->page_increment)
1202 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1203 spin_button->timer_calls++;
1206 spin_button->timer_calls = 0;
1207 spin_button->timer_step += spin_button->climb_rate;
1214 GDK_THREADS_LEAVE ();
1220 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1221 GtkSpinButton *spin_button)
1223 gboolean return_val;
1225 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1228 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1229 if (return_val == FALSE)
1230 gtk_spin_button_default_output (spin_button);
1232 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1234 spin_button_redraw (spin_button);
1236 g_object_notify (G_OBJECT (spin_button), "value");
1240 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1241 GtkScrollType scroll)
1243 /* We don't test whether the entry is editable, since
1244 * this key binding conceptually corresponds to changing
1245 * the value with the buttons using the mouse, which
1246 * we allow for non-editable spin buttons.
1250 case GTK_SCROLL_STEP_BACKWARD:
1251 case GTK_SCROLL_STEP_DOWN:
1252 case GTK_SCROLL_STEP_LEFT:
1253 gtk_spin_button_real_spin (spin, -spin->timer_step);
1255 if (spin->climb_rate > 0.0 && spin->timer_step
1256 < spin->adjustment->page_increment)
1258 if (spin->timer_calls < MAX_TIMER_CALLS)
1259 spin->timer_calls++;
1262 spin->timer_calls = 0;
1263 spin->timer_step += spin->climb_rate;
1268 case GTK_SCROLL_STEP_FORWARD:
1269 case GTK_SCROLL_STEP_UP:
1270 case GTK_SCROLL_STEP_RIGHT:
1271 gtk_spin_button_real_spin (spin, spin->timer_step);
1273 if (spin->climb_rate > 0.0 && spin->timer_step
1274 < spin->adjustment->page_increment)
1276 if (spin->timer_calls < MAX_TIMER_CALLS)
1277 spin->timer_calls++;
1280 spin->timer_calls = 0;
1281 spin->timer_step += spin->climb_rate;
1286 case GTK_SCROLL_PAGE_BACKWARD:
1287 case GTK_SCROLL_PAGE_DOWN:
1288 case GTK_SCROLL_PAGE_LEFT:
1289 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1292 case GTK_SCROLL_PAGE_FORWARD:
1293 case GTK_SCROLL_PAGE_UP:
1294 case GTK_SCROLL_PAGE_RIGHT:
1295 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1298 case GTK_SCROLL_START:
1300 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1302 gtk_spin_button_real_spin (spin, -diff);
1306 case GTK_SCROLL_END:
1308 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1310 gtk_spin_button_real_spin (spin, diff);
1315 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1319 gtk_spin_button_update (spin);
1323 gtk_spin_button_key_release (GtkWidget *widget,
1326 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1328 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1329 spin->timer_step = spin->adjustment->step_increment;
1330 spin->timer_calls = 0;
1336 gtk_spin_button_snap (GtkSpinButton *spin_button,
1342 inc = spin_button->adjustment->step_increment;
1346 tmp = (val - spin_button->adjustment->lower) / inc;
1347 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1348 val = spin_button->adjustment->lower + floor (tmp) * inc;
1350 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1352 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1353 gtk_adjustment_set_value (spin_button->adjustment, val);
1356 gint return_val = FALSE;
1357 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1358 if (return_val == FALSE)
1359 gtk_spin_button_default_output (spin_button);
1364 gtk_spin_button_activate (GtkEntry *entry)
1366 if (entry->editable)
1367 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1369 /* Chain up so that entry->activates_default is honored */
1370 parent_class->activate (entry);
1374 gtk_spin_button_insert_text (GtkEditable *editable,
1375 const gchar *new_text,
1376 gint new_text_length,
1379 GtkEntry *entry = GTK_ENTRY (editable);
1380 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1381 GtkEditableClass *parent_editable_iface = g_type_interface_peek (parent_class, GTK_TYPE_EDITABLE);
1393 entry_length = entry->text_length;
1397 if (*(lc->negative_sign))
1398 neg_sign = *(lc->negative_sign);
1402 if (*(lc->positive_sign))
1403 pos_sign = *(lc->positive_sign);
1407 for (sign=0, i=0; i<entry_length; i++)
1408 if ((entry->text[i] == neg_sign) ||
1409 (entry->text[i] == pos_sign))
1415 if (sign && !(*position))
1418 for (dotpos=-1, i=0; i<entry_length; i++)
1419 if (entry->text[i] == *(lc->decimal_point))
1425 if (dotpos > -1 && *position > dotpos &&
1426 (gint)spin->digits - entry_length
1427 + dotpos - new_text_length + 1 < 0)
1430 for (i = 0; i < new_text_length; i++)
1432 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1434 if (sign || (*position) || i)
1438 else if (new_text[i] == *(lc->decimal_point))
1440 if (!spin->digits || dotpos > -1 ||
1441 (new_text_length - 1 - i + entry_length
1442 - *position > (gint)spin->digits))
1444 dotpos = *position + i;
1446 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1451 parent_editable_iface->insert_text (editable, new_text,
1452 new_text_length, position);
1456 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1460 gdouble new_value = 0.0;
1462 adj = spin_button->adjustment;
1464 new_value = adj->value + increment;
1468 if (spin_button->wrap)
1470 if (fabs (adj->value - adj->upper) < EPSILON)
1471 new_value = adj->lower;
1472 else if (new_value > adj->upper)
1473 new_value = adj->upper;
1476 new_value = MIN (new_value, adj->upper);
1478 else if (increment < 0)
1480 if (spin_button->wrap)
1482 if (fabs (adj->value - adj->lower) < EPSILON)
1483 new_value = adj->upper;
1484 else if (new_value < adj->lower)
1485 new_value = adj->lower;
1488 new_value = MAX (new_value, adj->lower);
1491 if (fabs (new_value - adj->value) > EPSILON)
1492 gtk_adjustment_set_value (adj, new_value);
1494 spin_button_redraw (spin_button);
1498 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1503 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1505 return GTK_INPUT_ERROR;
1511 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1513 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1515 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1516 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1522 /***********************************************************
1523 ***********************************************************
1524 *** Public interface ***
1525 ***********************************************************
1526 ***********************************************************/
1530 gtk_spin_button_configure (GtkSpinButton *spin_button,
1531 GtkAdjustment *adjustment,
1535 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1538 gtk_spin_button_set_adjustment (spin_button, adjustment);
1540 adjustment = spin_button->adjustment;
1542 g_object_freeze_notify (G_OBJECT (spin_button));
1543 if (spin_button->digits != digits)
1545 spin_button->digits = digits;
1546 g_object_notify (G_OBJECT (spin_button), "digits");
1549 if (spin_button->climb_rate != climb_rate)
1551 spin_button->climb_rate = climb_rate;
1552 g_object_notify (G_OBJECT (spin_button), "climb_rate");
1554 g_object_thaw_notify (G_OBJECT (spin_button));
1556 gtk_adjustment_value_changed (adjustment);
1560 gtk_spin_button_new (GtkAdjustment *adjustment,
1564 GtkSpinButton *spin;
1567 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1569 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1571 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1573 return GTK_WIDGET (spin);
1577 * gtk_spin_button_new_with_range:
1578 * @min: Minimum allowable value
1579 * @max: Maximum allowable value
1580 * @step: Increment added or subtracted by spinning the widget
1582 * This is a convenience constructor that allows creation of a numeric
1583 * #GtkSpinButton without manually creating an adjustment. The value is
1584 * initially set to the minimum value and a page increment of 10 * @step
1585 * is the default. The precision of the spin button is equivalent to the
1586 * precision of @step.
1588 * Return value: The new spin button as a #GtkWidget.
1591 gtk_spin_button_new_with_range (gdouble min,
1596 GtkSpinButton *spin;
1599 g_return_val_if_fail (min < max, NULL);
1600 g_return_val_if_fail (step != 0.0, NULL);
1602 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1604 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1606 if (fabs (step) >= 1.0 || step == 0.0)
1609 digits = abs ((gint) floor (log10 (fabs (step))));
1610 if (digits > MAX_DIGITS)
1611 digits = MAX_DIGITS;
1614 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1616 gtk_spin_button_set_numeric (spin, TRUE);
1618 return GTK_WIDGET (spin);
1621 /* Callback used when the spin button's adjustment changes. We need to redraw
1622 * the arrows when the adjustment's range changes, and reevaluate our size request.
1625 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1627 GtkSpinButton *spin_button;
1629 spin_button = GTK_SPIN_BUTTON (data);
1631 spin_button->timer_step = spin_button->adjustment->step_increment;
1632 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1636 * gtk_spin_button_set_adjustment:
1637 * @spin_button: a #GtkSpinButton
1638 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1640 * Replaces the #GtkAdjustment associated with @spin_button.
1643 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1644 GtkAdjustment *adjustment)
1646 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1648 if (spin_button->adjustment != adjustment)
1650 if (spin_button->adjustment)
1652 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1653 gtk_spin_button_value_changed,
1655 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1656 adjustment_changed_cb,
1658 g_object_unref (spin_button->adjustment);
1660 spin_button->adjustment = adjustment;
1663 g_object_ref (adjustment);
1664 gtk_object_sink (GTK_OBJECT (adjustment));
1665 g_signal_connect (adjustment, "value_changed",
1666 G_CALLBACK (gtk_spin_button_value_changed),
1668 g_signal_connect (adjustment, "changed",
1669 G_CALLBACK (adjustment_changed_cb),
1671 spin_button->timer_step = spin_button->adjustment->step_increment;
1674 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1677 g_object_notify (G_OBJECT (spin_button), "adjustment");
1681 * gtk_spin_button_get_adjustment:
1684 * Get the adjustment associated with a #GtkSpinButton
1686 * Return value: the #GtkAdjustment of @spin_button
1689 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1691 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1693 return spin_button->adjustment;
1697 * gtk_spin_button_set_digits:
1698 * @spin_button: a #GtkSpinButton
1699 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1701 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1705 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1708 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1710 if (spin_button->digits != digits)
1712 spin_button->digits = digits;
1713 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1714 g_object_notify (G_OBJECT (spin_button), "digits");
1716 /* since lower/upper may have changed */
1717 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1722 * gtk_spin_button_get_digits:
1723 * @spin_button: a #GtkSpinButton
1725 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1727 * Returns: the current precision
1730 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1732 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1734 return spin_button->digits;
1738 * gtk_spin_button_set_increments:
1739 * @spin_button: a #GtkSpinButton
1740 * @step: increment applied for a button 1 press.
1741 * @page: increment applied for a button 2 press.
1743 * Sets the step and page increments for spin_button. This affects how
1744 * quickly the value changes when the spin button's arrows are activated.
1747 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1751 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1753 spin_button->adjustment->step_increment = step;
1754 spin_button->adjustment->page_increment = page;
1758 * gtk_spin_button_get_increments:
1759 * @spin_button: a #GtkSpinButton
1760 * @step: location to store step increment, or %NULL
1761 * @page: location to store page increment, or %NULL
1763 * Gets the current step and page the increments used by @spin_button. See
1764 * gtk_spin_button_set_increments().
1767 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1771 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1774 *step = spin_button->adjustment->step_increment;
1776 *page = spin_button->adjustment->page_increment;
1780 * gtk_spin_button_set_range:
1781 * @spin_button: a #GtkSpinButton
1782 * @min: minimum allowable value
1783 * @max: maximum allowable value
1785 * Sets the minimum and maximum allowable values for @spin_button
1788 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1794 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1796 spin_button->adjustment->lower = min;
1797 spin_button->adjustment->upper = max;
1799 value = CLAMP (spin_button->adjustment->value,
1800 spin_button->adjustment->lower,
1801 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1803 if (value != spin_button->adjustment->value)
1804 gtk_spin_button_set_value (spin_button, value);
1806 gtk_adjustment_changed (spin_button->adjustment);
1810 * gtk_spin_button_get_range:
1811 * @spin_button: a #GtkSpinButton
1812 * @min: location to store minimum allowed value, or %NULL
1813 * @max: location to store maximum allowed value, or %NULL
1815 * Gets the range allowed for @spin_button. See
1816 * gtk_spin_button_set_range().
1819 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1823 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1826 *min = spin_button->adjustment->lower;
1828 *max = spin_button->adjustment->upper;
1832 * gtk_spin_button_get_value:
1833 * @spin_button: a #GtkSpinButton
1835 * Get the value in the @spin_button.
1837 * Return value: the value of @spin_button
1840 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1842 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1844 return spin_button->adjustment->value;
1848 * gtk_spin_button_get_value_as_int:
1849 * @spin_button: a #GtkSpinButton
1851 * Get the value @spin_button represented as an integer.
1853 * Return value: the value of @spin_button
1856 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1860 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1862 val = spin_button->adjustment->value;
1863 if (val - floor (val) < ceil (val) - val)
1870 * gtk_spin_button_set_value:
1871 * @spin_button: a #GtkSpinButton
1872 * @value: the new value
1874 * Set the value of @spin_button.
1877 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1880 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1882 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1883 gtk_adjustment_set_value (spin_button->adjustment, value);
1886 gint return_val = FALSE;
1887 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1888 if (return_val == FALSE)
1889 gtk_spin_button_default_output (spin_button);
1894 * gtk_spin_button_set_update_policy:
1895 * @spin_button: a #GtkSpinButton
1896 * @policy: a #GtkSpinButtonUpdatePolicy value
1898 * Sets the update behavior of a spin button. This determines whether the
1899 * spin button is always updated or only when a valid value is set.
1902 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1903 GtkSpinButtonUpdatePolicy policy)
1905 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1907 if (spin_button->update_policy != policy)
1909 spin_button->update_policy = policy;
1910 g_object_notify (G_OBJECT (spin_button), "update_policy");
1915 * gtk_spin_button_get_update_policy:
1916 * @spin_button: a #GtkSpinButton
1918 * Gets the update behavior of a spin button. See
1919 * gtk_spin_button_set_update_policy().
1921 * Return value: the current update policy
1923 GtkSpinButtonUpdatePolicy
1924 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1926 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1928 return spin_button->update_policy;
1932 * gtk_spin_button_set_numeric:
1933 * @spin_button: a #GtkSpinButton
1934 * @numeric: flag indicating if only numeric entry is allowed.
1936 * Sets the flag that determines if non-numeric text can be typed into
1940 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1943 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1945 numeric = numeric != FALSE;
1947 if (spin_button->numeric != numeric)
1949 spin_button->numeric = numeric;
1950 g_object_notify (G_OBJECT (spin_button), "numeric");
1955 * gtk_spin_button_get_numeric:
1956 * @spin_button: a #GtkSpinButton
1958 * Returns whether non-numeric text can be typed into the spin button.
1959 * See gtk_spin_button_set_numeric().
1961 * Return value: %TRUE if only numeric text can be entered
1964 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1966 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1968 return spin_button->numeric;
1972 * gtk_spin_button_set_wrap:
1973 * @spin_button: a #GtkSpinButton
1974 * @wrap: a flag indicating if wrapping behavior is performed.
1976 * Sets the flag that determines if a spin button value wraps around to the
1977 * opposite limit when the upper or lower limit of the range is exceeded.
1980 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1983 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1985 wrap = wrap != FALSE;
1987 if (spin_button->wrap != wrap)
1989 spin_button->wrap = (wrap != 0);
1991 g_object_notify (G_OBJECT (spin_button), "wrap");
1996 * gtk_spin_button_get_wrap:
1997 * @spin_button: a #GtkSpinButton
1999 * Returns whether the spin button's value wraps around to the
2000 * opposite limit when the upper or lower limit of the range is
2001 * exceeded. See gtk_spin_button_set_wrap().
2003 * Return value: %TRUE if the spin button wraps around
2006 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2008 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2010 return spin_button->wrap;
2014 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2016 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
2019 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2021 return arrow_size - arrow_size % 2; /* force even */
2025 * spin_button_get_shadow_type:
2026 * @spin_button: a #GtkSpinButton
2028 * Convenience function to Get the shadow type from the underlying widget's
2031 * Return value: the #GtkShadowType
2034 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2036 GtkShadowType rc_shadow_type;
2038 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
2040 return rc_shadow_type;
2044 * gtk_spin_button_set_snap_to_ticks:
2045 * @spin_button: a #GtkSpinButton
2046 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2048 * Sets the policy as to whether values are corrected to the nearest step
2049 * increment when a spin button is activated after providing an invalid value.
2052 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2053 gboolean snap_to_ticks)
2057 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2059 new_val = (snap_to_ticks != 0);
2061 if (new_val != spin_button->snap_to_ticks)
2063 spin_button->snap_to_ticks = new_val;
2064 if (new_val && GTK_ENTRY (spin_button)->editable)
2065 gtk_spin_button_update (spin_button);
2067 g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
2072 * gtk_spin_button_get_snap_to_ticks:
2073 * @spin_button: a #GtkSpinButton
2075 * Returns whether the values are corrected to the nearest step. See
2076 * gtk_spin_button_set_snap_to_ticks().
2078 * Return value: %TRUE if values are snapped to the nearest step.
2081 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2083 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2085 return spin_button->snap_to_ticks;
2089 * gtk_spin_button_spin:
2090 * @spin_button: a #GtkSpinButton
2091 * @direction: a #GtkSpinType indicating the direction to spin.
2092 * @increment: step increment to apply in the specified direction.
2094 * Increment or decrement a spin button's value in a specified direction
2095 * by a specified amount.
2098 gtk_spin_button_spin (GtkSpinButton *spin_button,
2099 GtkSpinType direction,
2105 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2107 adj = spin_button->adjustment;
2109 /* for compatibility with the 1.0.x version of this function */
2110 if (increment != 0 && increment != adj->step_increment &&
2111 (direction == GTK_SPIN_STEP_FORWARD ||
2112 direction == GTK_SPIN_STEP_BACKWARD))
2114 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2115 increment = -increment;
2116 direction = GTK_SPIN_USER_DEFINED;
2121 case GTK_SPIN_STEP_FORWARD:
2123 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2126 case GTK_SPIN_STEP_BACKWARD:
2128 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2131 case GTK_SPIN_PAGE_FORWARD:
2133 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2136 case GTK_SPIN_PAGE_BACKWARD:
2138 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2143 diff = adj->value - adj->lower;
2145 gtk_spin_button_real_spin (spin_button, -diff);
2150 diff = adj->upper - adj->value;
2152 gtk_spin_button_real_spin (spin_button, diff);
2155 case GTK_SPIN_USER_DEFINED:
2158 gtk_spin_button_real_spin (spin_button, increment);
2167 * gtk_spin_button_update:
2168 * @spin_button: a #GtkSpinButton
2170 * Manually force an update of the spin button.
2173 gtk_spin_button_update (GtkSpinButton *spin_button)
2179 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2182 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2183 if (return_val == FALSE)
2185 return_val = gtk_spin_button_default_input (spin_button, &val);
2186 error = (return_val == GTK_INPUT_ERROR);
2188 else if (return_val == GTK_INPUT_ERROR)
2191 spin_button_redraw (spin_button);
2193 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2195 if (val < spin_button->adjustment->lower)
2196 val = spin_button->adjustment->lower;
2197 else if (val > spin_button->adjustment->upper)
2198 val = spin_button->adjustment->upper;
2200 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2202 val < spin_button->adjustment->lower ||
2203 val > spin_button->adjustment->upper))
2205 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2209 if (spin_button->snap_to_ticks)
2210 gtk_spin_button_snap (spin_button, val);
2213 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2214 gtk_adjustment_set_value (spin_button->adjustment, val);
2218 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0,
2220 if (return_val == FALSE)
2221 gtk_spin_button_default_output (spin_button);
2227 spin_button_redraw (GtkSpinButton *spin_button)
2231 widget = GTK_WIDGET (spin_button);
2233 if (GTK_WIDGET_DRAWABLE (widget))
2235 gtk_widget_queue_draw (widget);
2237 /* We must invalidate the panel window ourselves, because it
2238 * is not a child of widget->window
2240 gdk_window_invalidate_rect (spin_button->panel, NULL, TRUE);