1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GtkSpinButton widget for GTK+
5 * Copyright (C) 1998 Lars Hamann and Stefan Jeske
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
35 #include "gdk/gdkkeysyms.h"
36 #include "gtkbindings.h"
37 #include "gtkspinbutton.h"
39 #include "gtkmarshalers.h"
40 #include "gtksettings.h"
43 #define MIN_SPIN_BUTTON_WIDTH 30
44 #define SPIN_BUTTON_INITIAL_TIMER_DELAY 200
45 #define SPIN_BUTTON_TIMER_DELAY 20
46 #define MAX_TIMER_CALLS 5
49 #define MIN_ARROW_WIDTH 6
73 static void gtk_spin_button_class_init (GtkSpinButtonClass *klass);
74 static void gtk_spin_button_editable_init (GtkEditableClass *iface);
75 static void gtk_spin_button_init (GtkSpinButton *spin_button);
76 static void gtk_spin_button_finalize (GObject *object);
77 static void gtk_spin_button_destroy (GtkObject *object);
78 static void gtk_spin_button_set_property (GObject *object,
82 static void gtk_spin_button_get_property (GObject *object,
86 static void gtk_spin_button_map (GtkWidget *widget);
87 static void gtk_spin_button_unmap (GtkWidget *widget);
88 static void gtk_spin_button_realize (GtkWidget *widget);
89 static void gtk_spin_button_unrealize (GtkWidget *widget);
90 static void gtk_spin_button_size_request (GtkWidget *widget,
91 GtkRequisition *requisition);
92 static void gtk_spin_button_size_allocate (GtkWidget *widget,
93 GtkAllocation *allocation);
94 static gint gtk_spin_button_expose (GtkWidget *widget,
95 GdkEventExpose *event);
96 static gint gtk_spin_button_button_press (GtkWidget *widget,
97 GdkEventButton *event);
98 static gint gtk_spin_button_button_release (GtkWidget *widget,
99 GdkEventButton *event);
100 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
101 GdkEventMotion *event);
102 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
103 GdkEventCrossing *event);
104 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
105 GdkEventCrossing *event);
106 static gint gtk_spin_button_focus_out (GtkWidget *widget,
107 GdkEventFocus *event);
108 static void gtk_spin_button_grab_notify (GtkWidget *widget,
109 gboolean was_grabbed);
110 static void gtk_spin_button_state_changed (GtkWidget *widget,
111 GtkStateType previous_state);
112 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
113 GtkArrowType arrow_type);
114 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
115 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
116 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
117 GtkSpinButton *spin_button);
118 static gint gtk_spin_button_key_release (GtkWidget *widget,
120 static gint gtk_spin_button_scroll (GtkWidget *widget,
121 GdkEventScroll *event);
122 static void gtk_spin_button_activate (GtkEntry *entry);
123 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
125 static void gtk_spin_button_insert_text (GtkEditable *editable,
126 const gchar *new_text,
127 gint new_text_length,
129 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
131 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
132 GtkScrollType scroll);
134 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
136 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
138 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
139 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
140 static void spin_button_redraw (GtkSpinButton *spin_button);
143 static GtkEntryClass *parent_class = NULL;
144 static guint spinbutton_signals[LAST_SIGNAL] = {0};
149 gtk_spin_button_get_type (void)
151 static GType spin_button_type = 0;
153 if (!spin_button_type)
155 static const GTypeInfo spin_button_info =
157 sizeof (GtkSpinButtonClass),
158 NULL, /* base_init */
159 NULL, /* base_finalize */
160 (GClassInitFunc) gtk_spin_button_class_init,
161 NULL, /* class_finalize */
162 NULL, /* class_data */
163 sizeof (GtkSpinButton),
165 (GInstanceInitFunc) gtk_spin_button_init,
168 static const GInterfaceInfo editable_info =
170 (GInterfaceInitFunc) gtk_spin_button_editable_init, /* interface_init */
171 NULL, /* interface_finalize */
172 NULL /* interface_data */
176 g_type_register_static (GTK_TYPE_ENTRY, "GtkSpinButton",
177 &spin_button_info, 0);
179 g_type_add_interface_static (spin_button_type,
183 return spin_button_type;
186 #define add_spin_binding(binding_set, keyval, mask, scroll) \
187 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
189 GTK_TYPE_SCROLL_TYPE, scroll)
192 gtk_spin_button_class_init (GtkSpinButtonClass *class)
194 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
195 GtkObjectClass *object_class;
196 GtkWidgetClass *widget_class;
197 GtkEntryClass *entry_class;
198 GtkBindingSet *binding_set;
200 object_class = (GtkObjectClass*) class;
201 widget_class = (GtkWidgetClass*) class;
202 entry_class = (GtkEntryClass*) class;
204 parent_class = g_type_class_peek_parent (class);
206 gobject_class->finalize = gtk_spin_button_finalize;
208 gobject_class->set_property = gtk_spin_button_set_property;
209 gobject_class->get_property = gtk_spin_button_get_property;
211 object_class->destroy = gtk_spin_button_destroy;
213 widget_class->map = gtk_spin_button_map;
214 widget_class->unmap = gtk_spin_button_unmap;
215 widget_class->realize = gtk_spin_button_realize;
216 widget_class->unrealize = gtk_spin_button_unrealize;
217 widget_class->size_request = gtk_spin_button_size_request;
218 widget_class->size_allocate = gtk_spin_button_size_allocate;
219 widget_class->expose_event = gtk_spin_button_expose;
220 widget_class->scroll_event = gtk_spin_button_scroll;
221 widget_class->button_press_event = gtk_spin_button_button_press;
222 widget_class->button_release_event = gtk_spin_button_button_release;
223 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
224 widget_class->key_release_event = gtk_spin_button_key_release;
225 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
226 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
227 widget_class->focus_out_event = gtk_spin_button_focus_out;
228 widget_class->grab_notify = gtk_spin_button_grab_notify;
229 widget_class->state_changed = gtk_spin_button_state_changed;
231 entry_class->activate = gtk_spin_button_activate;
234 class->output = NULL;
235 class->change_value = gtk_spin_button_real_change_value;
237 g_object_class_install_property (gobject_class,
239 g_param_spec_object ("adjustment",
241 _("The adjustment that holds the value of the spinbutton"),
245 g_object_class_install_property (gobject_class,
247 g_param_spec_double ("climb_rate",
249 _("The acceleration rate when you hold down a button"),
255 g_object_class_install_property (gobject_class,
257 g_param_spec_uint ("digits",
259 _("The number of decimal places to display"),
265 g_object_class_install_property (gobject_class,
267 g_param_spec_boolean ("snap_to_ticks",
269 _("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
273 g_object_class_install_property (gobject_class,
275 g_param_spec_boolean ("numeric",
277 _("Whether non-numeric characters should be ignored"),
281 g_object_class_install_property (gobject_class,
283 g_param_spec_boolean ("wrap",
285 _("Whether a spin button should wrap upon reaching its limits"),
289 g_object_class_install_property (gobject_class,
291 g_param_spec_enum ("update_policy",
293 _("Whether the spin button should update always, or only when the value is legal"),
294 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
298 g_object_class_install_property (gobject_class,
300 g_param_spec_double ("value",
302 _("Reads the current value, or sets a new value"),
308 gtk_widget_class_install_style_property_parser (widget_class,
309 g_param_spec_enum ("shadow_type",
311 _("Style of bevel around the spin button"),
312 GTK_TYPE_SHADOW_TYPE,
315 gtk_rc_property_parse_enum);
316 spinbutton_signals[INPUT] =
317 g_signal_new ("input",
318 G_TYPE_FROM_CLASS (gobject_class),
320 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
322 _gtk_marshal_INT__POINTER,
326 spinbutton_signals[OUTPUT] =
327 g_signal_new ("output",
328 G_TYPE_FROM_CLASS (gobject_class),
330 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
331 _gtk_boolean_handled_accumulator, NULL,
332 _gtk_marshal_BOOLEAN__VOID,
335 spinbutton_signals[VALUE_CHANGED] =
336 g_signal_new ("value_changed",
337 G_TYPE_FROM_CLASS (gobject_class),
339 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
341 _gtk_marshal_VOID__VOID,
345 spinbutton_signals[CHANGE_VALUE] =
346 g_signal_new ("change_value",
347 G_TYPE_FROM_CLASS (gobject_class),
348 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
349 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
351 _gtk_marshal_VOID__ENUM,
353 GTK_TYPE_SCROLL_TYPE);
355 binding_set = gtk_binding_set_by_class (class);
357 add_spin_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
358 add_spin_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
359 add_spin_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
360 add_spin_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
361 add_spin_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
362 add_spin_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
363 add_spin_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
364 add_spin_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
368 gtk_spin_button_editable_init (GtkEditableClass *iface)
370 iface->insert_text = gtk_spin_button_insert_text;
374 gtk_spin_button_set_property (GObject *object,
379 GtkSpinButton *spin_button;
381 spin_button = GTK_SPIN_BUTTON (object);
385 GtkAdjustment *adjustment;
387 case PROP_ADJUSTMENT:
388 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
390 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
391 gtk_spin_button_set_adjustment (spin_button, adjustment);
393 case PROP_CLIMB_RATE:
394 gtk_spin_button_configure (spin_button,
395 spin_button->adjustment,
396 g_value_get_double (value),
397 spin_button->digits);
400 gtk_spin_button_configure (spin_button,
401 spin_button->adjustment,
402 spin_button->climb_rate,
403 g_value_get_uint (value));
405 case PROP_SNAP_TO_TICKS:
406 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
409 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
412 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
414 case PROP_UPDATE_POLICY:
415 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
418 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
426 gtk_spin_button_get_property (GObject *object,
431 GtkSpinButton *spin_button;
433 spin_button = GTK_SPIN_BUTTON (object);
437 case PROP_ADJUSTMENT:
438 g_value_set_object (value, spin_button->adjustment);
440 case PROP_CLIMB_RATE:
441 g_value_set_double (value, spin_button->climb_rate);
444 g_value_set_uint (value, spin_button->digits);
446 case PROP_SNAP_TO_TICKS:
447 g_value_set_boolean (value, spin_button->snap_to_ticks);
450 g_value_set_boolean (value, spin_button->numeric);
453 g_value_set_boolean (value, spin_button->wrap);
455 case PROP_UPDATE_POLICY:
456 g_value_set_enum (value, spin_button->update_policy);
459 g_value_set_double (value, spin_button->adjustment->value);
462 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
468 gtk_spin_button_init (GtkSpinButton *spin_button)
470 spin_button->adjustment = NULL;
471 spin_button->panel = NULL;
472 spin_button->timer = 0;
473 spin_button->climb_rate = 0.0;
474 spin_button->timer_step = 0.0;
475 spin_button->update_policy = GTK_UPDATE_ALWAYS;
476 spin_button->in_child = NO_ARROW;
477 spin_button->click_child = NO_ARROW;
478 spin_button->button = 0;
479 spin_button->need_timer = FALSE;
480 spin_button->timer_calls = 0;
481 spin_button->digits = 0;
482 spin_button->numeric = FALSE;
483 spin_button->wrap = FALSE;
484 spin_button->snap_to_ticks = FALSE;
486 gtk_spin_button_set_adjustment (spin_button,
487 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
491 gtk_spin_button_finalize (GObject *object)
493 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
495 G_OBJECT_CLASS (parent_class)->finalize (object);
499 gtk_spin_button_destroy (GtkObject *object)
501 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
503 GTK_OBJECT_CLASS (parent_class)->destroy (object);
507 gtk_spin_button_map (GtkWidget *widget)
509 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
511 GTK_WIDGET_CLASS (parent_class)->map (widget);
512 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
517 gtk_spin_button_unmap (GtkWidget *widget)
519 if (GTK_WIDGET_MAPPED (widget))
521 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
522 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
527 gtk_spin_button_realize (GtkWidget *widget)
529 GtkSpinButton *spin_button;
530 GdkWindowAttr attributes;
531 gint attributes_mask;
536 spin_button = GTK_SPIN_BUTTON (widget);
537 arrow_size = spin_button_get_arrow_size (spin_button);
539 real_width = widget->allocation.width;
540 widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
541 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
542 GDK_KEY_RELEASE_MASK);
543 GTK_WIDGET_CLASS (parent_class)->realize (widget);
545 widget->allocation.width = real_width;
547 attributes.window_type = GDK_WINDOW_CHILD;
548 attributes.wclass = GDK_INPUT_OUTPUT;
549 attributes.visual = gtk_widget_get_visual (widget);
550 attributes.colormap = gtk_widget_get_colormap (widget);
551 attributes.event_mask = gtk_widget_get_events (widget);
552 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
553 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
554 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
556 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
558 attributes.x = (widget->allocation.x +
559 widget->allocation.width - arrow_size -
560 2 * widget->style->xthickness);
561 attributes.y = widget->allocation.y + (widget->allocation.height -
562 widget->requisition.height) / 2;
563 attributes.width = arrow_size + 2 * widget->style->xthickness;
564 attributes.height = widget->requisition.height;
566 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
567 &attributes, attributes_mask);
568 gdk_window_set_user_data (spin_button->panel, widget);
570 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
573 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
574 if (return_val == FALSE)
575 gtk_spin_button_default_output (spin_button);
577 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
581 gtk_spin_button_unrealize (GtkWidget *widget)
583 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
585 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
589 gdk_window_set_user_data (spin->panel, NULL);
590 gdk_window_destroy (spin->panel);
596 compute_double_length (double val, int digits)
602 if (fabs (val) > 1.0)
603 a = floor (log10 (fabs (val))) + 1;
615 return a + digits + extra;
619 gtk_spin_button_size_request (GtkWidget *widget,
620 GtkRequisition *requisition)
623 GtkSpinButton *spin_button;
626 entry = GTK_ENTRY (widget);
627 spin_button = GTK_SPIN_BUTTON (widget);
628 arrow_size = spin_button_get_arrow_size (spin_button);
630 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
632 if (entry->width_chars < 0)
634 PangoContext *context;
635 PangoFontMetrics *metrics;
641 gboolean interior_focus;
644 gtk_widget_style_get (widget,
645 "interior-focus", &interior_focus,
646 "focus-line-width", &focus_width,
649 context = gtk_widget_get_pango_context (widget);
650 metrics = pango_context_get_metrics (context,
651 widget->style->font_desc,
652 pango_context_get_language (context));
654 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
655 digit_width = PANGO_PIXELS (digit_width);
657 pango_font_metrics_unref (metrics);
659 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
661 width = MIN_SPIN_BUTTON_WIDTH;
662 max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
663 spin_button->digits));
665 string_len = compute_double_length (spin_button->adjustment->upper,
666 spin_button->digits);
667 w = MIN (string_len, max_string_len) * digit_width;
668 width = MAX (width, w);
669 string_len = compute_double_length (spin_button->adjustment->lower,
670 spin_button->digits);
671 w = MIN (string_len, max_string_len) * digit_width;
672 width = MAX (width, w);
674 requisition->width = width;
676 requisition->width += 2 * focus_width;
679 requisition->width += arrow_size + 2 * widget->style->xthickness;
683 gtk_spin_button_size_allocate (GtkWidget *widget,
684 GtkAllocation *allocation)
687 GtkAllocation entry_allocation;
688 GtkAllocation panel_allocation;
692 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
693 g_return_if_fail (allocation != NULL);
695 spin = GTK_SPIN_BUTTON (widget);
696 arrow_size = spin_button_get_arrow_size (spin);
697 panel_width = arrow_size + 2 * widget->style->xthickness;
699 widget->allocation = *allocation;
701 entry_allocation = *allocation;
702 entry_allocation.width -= panel_width;
704 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
706 entry_allocation.x += panel_width;
707 panel_allocation.x = allocation->x;
711 panel_allocation.x = allocation->x + allocation->width - panel_width;
714 panel_allocation.width = panel_width;
715 panel_allocation.height = MIN (widget->requisition.height, allocation->height);
717 panel_allocation.y = allocation->y + (allocation->height -
718 panel_allocation.height) / 2;
720 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &entry_allocation);
722 if (GTK_WIDGET_REALIZED (widget))
724 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
727 panel_allocation.width,
728 panel_allocation.height);
731 spin_button_redraw (spin);
735 gtk_spin_button_expose (GtkWidget *widget,
736 GdkEventExpose *event)
740 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
741 g_return_val_if_fail (event != NULL, FALSE);
743 spin = GTK_SPIN_BUTTON (widget);
745 if (GTK_WIDGET_DRAWABLE (widget))
747 GtkShadowType shadow_type;
750 if (event->window != spin->panel)
751 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
753 /* we redraw the panel even if it wasn't exposed. This is
754 * because spin->panel is not a child window of widget->window,
755 * so it will not be invalidated by eg. gtk_widget_queue_draw().
760 gdk_drawable_get_size (spin->panel, &rect.width, &rect.height);
762 shadow_type = spin_button_get_shadow_type (spin);
764 gdk_window_begin_paint_rect (spin->panel, &rect);
766 if (shadow_type != GTK_SHADOW_NONE)
768 gtk_paint_box (widget->style, spin->panel,
769 GTK_STATE_NORMAL, shadow_type,
770 NULL, widget, "spinbutton",
771 rect.x, rect.y, rect.width, rect.height);
774 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
775 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
777 gdk_window_end_paint (spin->panel);
784 spin_button_at_limit (GtkSpinButton *spin_button,
787 if (spin_button->wrap)
790 if (arrow == GTK_ARROW_UP &&
791 (spin_button->adjustment->upper - spin_button->adjustment->value <= EPSILON))
794 if (arrow == GTK_ARROW_DOWN &&
795 (spin_button->adjustment->value - spin_button->adjustment->lower <= EPSILON))
802 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
803 GtkArrowType arrow_type)
805 GtkStateType state_type;
806 GtkShadowType shadow_type;
814 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
815 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
817 widget = GTK_WIDGET (spin_button);
819 if (GTK_WIDGET_DRAWABLE (widget))
821 width = spin_button_get_arrow_size (spin_button) + 2 * widget->style->xthickness;
823 if (arrow_type == GTK_ARROW_UP)
828 height = widget->requisition.height / 2;
833 y = widget->requisition.height / 2;
835 height = (widget->requisition.height + 1) / 2;
838 if (spin_button_at_limit (spin_button, arrow_type))
840 shadow_type = GTK_SHADOW_OUT;
841 state_type = GTK_STATE_INSENSITIVE;
845 if (spin_button->click_child == arrow_type)
847 state_type = GTK_STATE_ACTIVE;
848 shadow_type = GTK_SHADOW_IN;
852 if (spin_button->in_child == arrow_type &&
853 spin_button->click_child == NO_ARROW)
855 state_type = GTK_STATE_PRELIGHT;
859 state_type = GTK_WIDGET_STATE (widget);
862 shadow_type = GTK_SHADOW_OUT;
866 gtk_paint_box (widget->style, spin_button->panel,
867 state_type, shadow_type,
869 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
870 x, y, width, height);
872 height = widget->requisition.height;
874 if (arrow_type == GTK_ARROW_DOWN)
877 height = height - y - 2;
882 height = height / 2 - 2;
887 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
893 w -= w % 2 - 1; /* force odd */
896 x += (width - w) / 2;
897 y += (height - h) / 2;
902 gtk_paint_arrow (widget->style, spin_button->panel,
903 state_type, shadow_type,
904 NULL, widget, "spinbutton",
906 x, y, width, height);
911 gtk_spin_button_enter_notify (GtkWidget *widget,
912 GdkEventCrossing *event)
914 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
916 if (event->window == spin->panel)
921 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
923 if (y <= widget->requisition.height / 2)
924 spin->in_child = GTK_ARROW_UP;
926 spin->in_child = GTK_ARROW_DOWN;
928 spin_button_redraw (spin);
935 gtk_spin_button_leave_notify (GtkWidget *widget,
936 GdkEventCrossing *event)
938 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
940 spin->in_child = NO_ARROW;
941 spin_button_redraw (spin);
947 gtk_spin_button_focus_out (GtkWidget *widget,
948 GdkEventFocus *event)
950 if (GTK_ENTRY (widget)->editable)
951 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
953 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
957 gtk_spin_button_grab_notify (GtkWidget *widget,
958 gboolean was_grabbed)
960 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
964 gtk_spin_button_stop_spinning (spin);
965 spin_button_redraw (spin);
970 gtk_spin_button_state_changed (GtkWidget *widget,
971 GtkStateType previous_state)
973 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
975 if (!GTK_WIDGET_IS_SENSITIVE (widget))
977 gtk_spin_button_stop_spinning (spin);
978 spin_button_redraw (spin);
983 gtk_spin_button_scroll (GtkWidget *widget,
984 GdkEventScroll *event)
986 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
988 if (event->direction == GDK_SCROLL_UP)
990 if (!GTK_WIDGET_HAS_FOCUS (widget))
991 gtk_widget_grab_focus (widget);
992 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
994 else if (event->direction == GDK_SCROLL_DOWN)
996 if (!GTK_WIDGET_HAS_FOCUS (widget))
997 gtk_widget_grab_focus (widget);
998 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
1007 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1011 g_source_remove (spin->timer);
1013 spin->timer_calls = 0;
1014 spin->need_timer = FALSE;
1019 spin->timer_step = spin->adjustment->step_increment;
1020 spin->timer_calls = 0;
1022 spin->click_child = NO_ARROW;
1027 start_spinning (GtkSpinButton *spin,
1028 GtkArrowType click_child,
1031 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1033 spin->click_child = click_child;
1034 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1038 spin->timer_step = step;
1039 spin->need_timer = TRUE;
1040 spin->timer = g_timeout_add (SPIN_BUTTON_INITIAL_TIMER_DELAY,
1041 (GtkFunction) gtk_spin_button_timer,
1045 spin_button_redraw (spin);
1049 gtk_spin_button_button_press (GtkWidget *widget,
1050 GdkEventButton *event)
1052 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1056 if (event->window == spin->panel)
1058 if (!GTK_WIDGET_HAS_FOCUS (widget))
1059 gtk_widget_grab_focus (widget);
1060 spin->button = event->button;
1062 if (GTK_ENTRY (widget)->editable)
1063 gtk_spin_button_update (spin);
1065 if (event->y <= widget->requisition.height / 2)
1067 if (event->button == 1)
1068 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->step_increment);
1069 else if (event->button == 2)
1070 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->page_increment);
1072 spin->click_child = GTK_ARROW_UP;
1076 if (event->button == 1)
1077 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->step_increment);
1078 else if (event->button == 2)
1079 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->page_increment);
1081 spin->click_child = GTK_ARROW_DOWN;
1086 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1092 gtk_spin_button_button_release (GtkWidget *widget,
1093 GdkEventButton *event)
1095 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1098 arrow_size = spin_button_get_arrow_size (spin);
1100 if (event->button == spin->button)
1102 int click_child = spin->click_child;
1104 gtk_spin_button_stop_spinning (spin);
1106 if (event->button == 3)
1108 if (event->y >= 0 && event->x >= 0 &&
1109 event->y <= widget->requisition.height &&
1110 event->x <= arrow_size + 2 * widget->style->xthickness)
1112 if (click_child == GTK_ARROW_UP &&
1113 event->y <= widget->requisition.height / 2)
1117 diff = spin->adjustment->upper - spin->adjustment->value;
1119 gtk_spin_button_real_spin (spin, diff);
1121 else if (click_child == GTK_ARROW_DOWN &&
1122 event->y > widget->requisition.height / 2)
1126 diff = spin->adjustment->value - spin->adjustment->lower;
1128 gtk_spin_button_real_spin (spin, -diff);
1132 spin_button_redraw (spin);
1137 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1141 gtk_spin_button_motion_notify (GtkWidget *widget,
1142 GdkEventMotion *event)
1144 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1149 if (event->window == spin->panel)
1153 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1155 if (y <= widget->requisition.height / 2 &&
1156 spin->in_child == GTK_ARROW_DOWN)
1158 spin->in_child = GTK_ARROW_UP;
1159 spin_button_redraw (spin);
1161 else if (y > widget->requisition.height / 2 &&
1162 spin->in_child == GTK_ARROW_UP)
1164 spin->in_child = GTK_ARROW_DOWN;
1165 spin_button_redraw (spin);
1171 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1175 gtk_spin_button_timer (GtkSpinButton *spin_button)
1177 gboolean retval = FALSE;
1179 GDK_THREADS_ENTER ();
1181 if (spin_button->timer)
1183 if (spin_button->click_child == GTK_ARROW_UP)
1184 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1186 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1188 if (spin_button->need_timer)
1190 spin_button->need_timer = FALSE;
1191 spin_button->timer = g_timeout_add (SPIN_BUTTON_TIMER_DELAY,
1192 (GtkFunction) gtk_spin_button_timer,
1193 (gpointer) spin_button);
1197 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1198 < spin_button->adjustment->page_increment)
1200 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1201 spin_button->timer_calls++;
1204 spin_button->timer_calls = 0;
1205 spin_button->timer_step += spin_button->climb_rate;
1212 GDK_THREADS_LEAVE ();
1218 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1219 GtkSpinButton *spin_button)
1221 gboolean return_val;
1223 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1226 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1227 if (return_val == FALSE)
1228 gtk_spin_button_default_output (spin_button);
1230 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1232 spin_button_redraw (spin_button);
1234 g_object_notify (G_OBJECT (spin_button), "value");
1238 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1239 GtkScrollType scroll)
1241 /* We don't test whether the entry is editable, since
1242 * this key binding conceptually corresponds to changing
1243 * the value with the buttons using the mouse, which
1244 * we allow for non-editable spin buttons.
1248 case GTK_SCROLL_STEP_BACKWARD:
1249 case GTK_SCROLL_STEP_DOWN:
1250 case GTK_SCROLL_STEP_LEFT:
1251 gtk_spin_button_real_spin (spin, -spin->timer_step);
1253 if (spin->climb_rate > 0.0 && spin->timer_step
1254 < spin->adjustment->page_increment)
1256 if (spin->timer_calls < MAX_TIMER_CALLS)
1257 spin->timer_calls++;
1260 spin->timer_calls = 0;
1261 spin->timer_step += spin->climb_rate;
1266 case GTK_SCROLL_STEP_FORWARD:
1267 case GTK_SCROLL_STEP_UP:
1268 case GTK_SCROLL_STEP_RIGHT:
1269 gtk_spin_button_real_spin (spin, spin->timer_step);
1271 if (spin->climb_rate > 0.0 && spin->timer_step
1272 < spin->adjustment->page_increment)
1274 if (spin->timer_calls < MAX_TIMER_CALLS)
1275 spin->timer_calls++;
1278 spin->timer_calls = 0;
1279 spin->timer_step += spin->climb_rate;
1284 case GTK_SCROLL_PAGE_BACKWARD:
1285 case GTK_SCROLL_PAGE_DOWN:
1286 case GTK_SCROLL_PAGE_LEFT:
1287 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1290 case GTK_SCROLL_PAGE_FORWARD:
1291 case GTK_SCROLL_PAGE_UP:
1292 case GTK_SCROLL_PAGE_RIGHT:
1293 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1296 case GTK_SCROLL_START:
1298 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1300 gtk_spin_button_real_spin (spin, -diff);
1304 case GTK_SCROLL_END:
1306 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1308 gtk_spin_button_real_spin (spin, diff);
1313 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1317 gtk_spin_button_update (spin);
1321 gtk_spin_button_key_release (GtkWidget *widget,
1324 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1326 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1327 spin->timer_step = spin->adjustment->step_increment;
1328 spin->timer_calls = 0;
1334 gtk_spin_button_snap (GtkSpinButton *spin_button,
1340 inc = spin_button->adjustment->step_increment;
1344 tmp = (val - spin_button->adjustment->lower) / inc;
1345 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1346 val = spin_button->adjustment->lower + floor (tmp) * inc;
1348 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1350 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1351 gtk_adjustment_set_value (spin_button->adjustment, val);
1354 gint return_val = FALSE;
1355 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1356 if (return_val == FALSE)
1357 gtk_spin_button_default_output (spin_button);
1362 gtk_spin_button_activate (GtkEntry *entry)
1364 if (entry->editable)
1365 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1367 /* Chain up so that entry->activates_default is honored */
1368 parent_class->activate (entry);
1372 gtk_spin_button_insert_text (GtkEditable *editable,
1373 const gchar *new_text,
1374 gint new_text_length,
1377 GtkEntry *entry = GTK_ENTRY (editable);
1378 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1379 GtkEditableClass *parent_editable_iface = g_type_interface_peek (parent_class, GTK_TYPE_EDITABLE);
1391 entry_length = entry->text_length;
1395 if (*(lc->negative_sign))
1396 neg_sign = *(lc->negative_sign);
1400 if (*(lc->positive_sign))
1401 pos_sign = *(lc->positive_sign);
1405 for (sign=0, i=0; i<entry_length; i++)
1406 if ((entry->text[i] == neg_sign) ||
1407 (entry->text[i] == pos_sign))
1413 if (sign && !(*position))
1416 for (dotpos=-1, i=0; i<entry_length; i++)
1417 if (entry->text[i] == *(lc->decimal_point))
1423 if (dotpos > -1 && *position > dotpos &&
1424 (gint)spin->digits - entry_length
1425 + dotpos - new_text_length + 1 < 0)
1428 for (i = 0; i < new_text_length; i++)
1430 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1432 if (sign || (*position) || i)
1436 else if (new_text[i] == *(lc->decimal_point))
1438 if (!spin->digits || dotpos > -1 ||
1439 (new_text_length - 1 - i + entry_length
1440 - *position > (gint)spin->digits))
1442 dotpos = *position + i;
1444 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1449 parent_editable_iface->insert_text (editable, new_text,
1450 new_text_length, position);
1454 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1458 gdouble new_value = 0.0;
1460 adj = spin_button->adjustment;
1462 new_value = adj->value + increment;
1466 if (spin_button->wrap)
1468 if (fabs (adj->value - adj->upper) < EPSILON)
1469 new_value = adj->lower;
1470 else if (new_value > adj->upper)
1471 new_value = adj->upper;
1474 new_value = MIN (new_value, adj->upper);
1476 else if (increment < 0)
1478 if (spin_button->wrap)
1480 if (fabs (adj->value - adj->lower) < EPSILON)
1481 new_value = adj->upper;
1482 else if (new_value < adj->lower)
1483 new_value = adj->lower;
1486 new_value = MAX (new_value, adj->lower);
1489 if (fabs (new_value - adj->value) > EPSILON)
1490 gtk_adjustment_set_value (adj, new_value);
1492 spin_button_redraw (spin_button);
1496 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1501 *new_val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1503 return GTK_INPUT_ERROR;
1509 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1511 gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1513 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1514 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1520 /***********************************************************
1521 ***********************************************************
1522 *** Public interface ***
1523 ***********************************************************
1524 ***********************************************************/
1528 gtk_spin_button_configure (GtkSpinButton *spin_button,
1529 GtkAdjustment *adjustment,
1533 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1536 gtk_spin_button_set_adjustment (spin_button, adjustment);
1538 adjustment = spin_button->adjustment;
1540 g_object_freeze_notify (G_OBJECT (spin_button));
1541 if (spin_button->digits != digits)
1543 spin_button->digits = digits;
1544 g_object_notify (G_OBJECT (spin_button), "digits");
1547 if (spin_button->climb_rate != climb_rate)
1549 spin_button->climb_rate = climb_rate;
1550 g_object_notify (G_OBJECT (spin_button), "climb_rate");
1552 g_object_thaw_notify (G_OBJECT (spin_button));
1554 gtk_adjustment_value_changed (adjustment);
1558 gtk_spin_button_new (GtkAdjustment *adjustment,
1562 GtkSpinButton *spin;
1565 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1567 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1569 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1571 return GTK_WIDGET (spin);
1575 * gtk_spin_button_new_with_range:
1576 * @min: Minimum allowable value
1577 * @max: Maximum allowable value
1578 * @step: Increment added or subtracted by spinning the widget
1580 * This is a convenience constructor that allows creation of a numeric
1581 * #GtkSpinButton without manually creating an adjustment. The value is
1582 * initially set to the minimum value and a page increment of 10 * @step
1583 * is the default. The precision of the spin button is equivalent to the
1584 * precision of @step.
1586 * Return value: The new spin button as a #GtkWidget.
1589 gtk_spin_button_new_with_range (gdouble min,
1594 GtkSpinButton *spin;
1597 g_return_val_if_fail (min < max, NULL);
1598 g_return_val_if_fail (step != 0.0, NULL);
1600 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1602 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1604 if (fabs (step) >= 1.0 || step == 0.0)
1607 digits = abs ((gint) floor (log10 (fabs (step))));
1608 if (digits > MAX_DIGITS)
1609 digits = MAX_DIGITS;
1612 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1614 gtk_spin_button_set_numeric (spin, TRUE);
1616 return GTK_WIDGET (spin);
1619 /* Callback used when the spin button's adjustment changes. We need to redraw
1620 * the arrows when the adjustment's range changes, and reevaluate our size request.
1623 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1625 GtkSpinButton *spin_button;
1627 spin_button = GTK_SPIN_BUTTON (data);
1629 spin_button->timer_step = spin_button->adjustment->step_increment;
1630 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1634 * gtk_spin_button_set_adjustment:
1635 * @spin_button: a #GtkSpinButton
1636 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1638 * Replaces the #GtkAdjustment associated with @spin_button.
1641 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1642 GtkAdjustment *adjustment)
1644 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1646 if (spin_button->adjustment != adjustment)
1648 if (spin_button->adjustment)
1650 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1651 gtk_spin_button_value_changed,
1653 g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1654 adjustment_changed_cb,
1656 g_object_unref (spin_button->adjustment);
1658 spin_button->adjustment = adjustment;
1661 g_object_ref (adjustment);
1662 gtk_object_sink (GTK_OBJECT (adjustment));
1663 g_signal_connect (adjustment, "value_changed",
1664 G_CALLBACK (gtk_spin_button_value_changed),
1666 g_signal_connect (adjustment, "changed",
1667 G_CALLBACK (adjustment_changed_cb),
1669 spin_button->timer_step = spin_button->adjustment->step_increment;
1672 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1675 g_object_notify (G_OBJECT (spin_button), "adjustment");
1679 * gtk_spin_button_get_adjustment:
1682 * Get the adjustment associated with a #GtkSpinButton
1684 * Return value: the #GtkAdjustment of @spin_button
1687 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1689 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1691 return spin_button->adjustment;
1695 * gtk_spin_button_set_digits:
1696 * @spin_button: a #GtkSpinButton
1697 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1699 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1703 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1706 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1708 if (spin_button->digits != digits)
1710 spin_button->digits = digits;
1711 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1712 g_object_notify (G_OBJECT (spin_button), "digits");
1714 /* since lower/upper may have changed */
1715 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1720 * gtk_spin_button_get_digits:
1721 * @spin_button: a #GtkSpinButton
1723 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1725 * Returns: the current precision
1728 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1730 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1732 return spin_button->digits;
1736 * gtk_spin_button_set_increments:
1737 * @spin_button: a #GtkSpinButton
1738 * @step: increment applied for a button 1 press.
1739 * @page: increment applied for a button 2 press.
1741 * Sets the step and page increments for spin_button. This affects how
1742 * quickly the value changes when the spin button's arrows are activated.
1745 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1749 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1751 spin_button->adjustment->step_increment = step;
1752 spin_button->adjustment->page_increment = page;
1756 * gtk_spin_button_get_increments:
1757 * @spin_button: a #GtkSpinButton
1758 * @step: location to store step increment, or %NULL
1759 * @page: location to store page increment, or %NULL
1761 * Gets the current step and page the increments used by @spin_button. See
1762 * gtk_spin_button_set_increments().
1765 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1769 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1772 *step = spin_button->adjustment->step_increment;
1774 *page = spin_button->adjustment->page_increment;
1778 * gtk_spin_button_set_range:
1779 * @spin_button: a #GtkSpinButton
1780 * @min: minimum allowable value
1781 * @max: maximum allowable value
1783 * Sets the minimum and maximum allowable values for @spin_button
1786 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1792 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1794 spin_button->adjustment->lower = min;
1795 spin_button->adjustment->upper = max;
1797 value = CLAMP (spin_button->adjustment->value,
1798 spin_button->adjustment->lower,
1799 (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1801 if (value != spin_button->adjustment->value)
1802 gtk_spin_button_set_value (spin_button, value);
1804 gtk_adjustment_changed (spin_button->adjustment);
1808 * gtk_spin_button_get_range:
1809 * @spin_button: a #GtkSpinButton
1810 * @min: location to store minimum allowed value, or %NULL
1811 * @max: location to store maximum allowed value, or %NULL
1813 * Gets the range allowed for @spin_button. See
1814 * gtk_spin_button_set_range().
1817 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1821 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1824 *min = spin_button->adjustment->lower;
1826 *max = spin_button->adjustment->upper;
1830 * gtk_spin_button_get_value:
1831 * @spin_button: a #GtkSpinButton
1833 * Get the value in the @spin_button.
1835 * Return value: the value of @spin_button
1838 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1840 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1842 return spin_button->adjustment->value;
1846 * gtk_spin_button_get_value_as_int:
1847 * @spin_button: a #GtkSpinButton
1849 * Get the value @spin_button represented as an integer.
1851 * Return value: the value of @spin_button
1854 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1858 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1860 val = spin_button->adjustment->value;
1861 if (val - floor (val) < ceil (val) - val)
1868 * gtk_spin_button_set_value:
1869 * @spin_button: a #GtkSpinButton
1870 * @value: the new value
1872 * Set the value of @spin_button.
1875 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1878 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1880 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1881 gtk_adjustment_set_value (spin_button->adjustment, value);
1884 gint return_val = FALSE;
1885 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1886 if (return_val == FALSE)
1887 gtk_spin_button_default_output (spin_button);
1892 * gtk_spin_button_set_update_policy:
1893 * @spin_button: a #GtkSpinButton
1894 * @policy: a #GtkSpinButtonUpdatePolicy value
1896 * Sets the update behavior of a spin button. This determines whether the
1897 * spin button is always updated or only when a valid value is set.
1900 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1901 GtkSpinButtonUpdatePolicy policy)
1903 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1905 if (spin_button->update_policy != policy)
1907 spin_button->update_policy = policy;
1908 g_object_notify (G_OBJECT (spin_button), "update_policy");
1913 * gtk_spin_button_get_update_policy:
1914 * @spin_button: a #GtkSpinButton
1916 * Gets the update behavior of a spin button. See
1917 * gtk_spin_button_set_update_policy().
1919 * Return value: the current update policy
1921 GtkSpinButtonUpdatePolicy
1922 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1924 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1926 return spin_button->update_policy;
1930 * gtk_spin_button_set_numeric:
1931 * @spin_button: a #GtkSpinButton
1932 * @numeric: flag indicating if only numeric entry is allowed.
1934 * Sets the flag that determines if non-numeric text can be typed into
1938 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1941 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1943 numeric = numeric != FALSE;
1945 if (spin_button->numeric != numeric)
1947 spin_button->numeric = numeric;
1948 g_object_notify (G_OBJECT (spin_button), "numeric");
1953 * gtk_spin_button_get_numeric:
1954 * @spin_button: a #GtkSpinButton
1956 * Returns whether non-numeric text can be typed into the spin button.
1957 * See gtk_spin_button_set_numeric().
1959 * Return value: %TRUE if only numeric text can be entered
1962 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1964 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1966 return spin_button->numeric;
1970 * gtk_spin_button_set_wrap:
1971 * @spin_button: a #GtkSpinButton
1972 * @wrap: a flag indicating if wrapping behavior is performed.
1974 * Sets the flag that determines if a spin button value wraps around to the
1975 * opposite limit when the upper or lower limit of the range is exceeded.
1978 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1981 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1983 wrap = wrap != FALSE;
1985 if (spin_button->wrap != wrap)
1987 spin_button->wrap = (wrap != 0);
1989 g_object_notify (G_OBJECT (spin_button), "wrap");
1994 * gtk_spin_button_get_wrap:
1995 * @spin_button: a #GtkSpinButton
1997 * Returns whether the spin button's value wraps around to the
1998 * opposite limit when the upper or lower limit of the range is
1999 * exceeded. See gtk_spin_button_set_wrap().
2001 * Return value: %TRUE if the spin button wraps around
2004 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2006 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2008 return spin_button->wrap;
2012 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2014 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
2017 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2019 return arrow_size - arrow_size % 2; /* force even */
2023 * spin_button_get_shadow_type:
2024 * @spin_button: a #GtkSpinButton
2026 * Convenience function to Get the shadow type from the underlying widget's
2029 * Return value: the #GtkShadowType
2032 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2034 GtkShadowType rc_shadow_type;
2036 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
2038 return rc_shadow_type;
2042 * gtk_spin_button_set_snap_to_ticks:
2043 * @spin_button: a #GtkSpinButton
2044 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2046 * Sets the policy as to whether values are corrected to the nearest step
2047 * increment when a spin button is activated after providing an invalid value.
2050 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2051 gboolean snap_to_ticks)
2055 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2057 new_val = (snap_to_ticks != 0);
2059 if (new_val != spin_button->snap_to_ticks)
2061 spin_button->snap_to_ticks = new_val;
2062 if (new_val && GTK_ENTRY (spin_button)->editable)
2063 gtk_spin_button_update (spin_button);
2065 g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
2070 * gtk_spin_button_get_snap_to_ticks:
2071 * @spin_button: a #GtkSpinButton
2073 * Returns whether the values are corrected to the nearest step. See
2074 * gtk_spin_button_set_snap_to_ticks().
2076 * Return value: %TRUE if values are snapped to the nearest step.
2079 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2081 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2083 return spin_button->snap_to_ticks;
2087 * gtk_spin_button_spin:
2088 * @spin_button: a #GtkSpinButton
2089 * @direction: a #GtkSpinType indicating the direction to spin.
2090 * @increment: step increment to apply in the specified direction.
2092 * Increment or decrement a spin button's value in a specified direction
2093 * by a specified amount.
2096 gtk_spin_button_spin (GtkSpinButton *spin_button,
2097 GtkSpinType direction,
2103 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2105 adj = spin_button->adjustment;
2107 /* for compatibility with the 1.0.x version of this function */
2108 if (increment != 0 && increment != adj->step_increment &&
2109 (direction == GTK_SPIN_STEP_FORWARD ||
2110 direction == GTK_SPIN_STEP_BACKWARD))
2112 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2113 increment = -increment;
2114 direction = GTK_SPIN_USER_DEFINED;
2119 case GTK_SPIN_STEP_FORWARD:
2121 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2124 case GTK_SPIN_STEP_BACKWARD:
2126 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2129 case GTK_SPIN_PAGE_FORWARD:
2131 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2134 case GTK_SPIN_PAGE_BACKWARD:
2136 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2141 diff = adj->value - adj->lower;
2143 gtk_spin_button_real_spin (spin_button, -diff);
2148 diff = adj->upper - adj->value;
2150 gtk_spin_button_real_spin (spin_button, diff);
2153 case GTK_SPIN_USER_DEFINED:
2156 gtk_spin_button_real_spin (spin_button, increment);
2165 * gtk_spin_button_update:
2166 * @spin_button: a #GtkSpinButton
2168 * Manually force an update of the spin button.
2171 gtk_spin_button_update (GtkSpinButton *spin_button)
2177 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2180 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2181 if (return_val == FALSE)
2183 return_val = gtk_spin_button_default_input (spin_button, &val);
2184 error = (return_val == GTK_INPUT_ERROR);
2186 else if (return_val == GTK_INPUT_ERROR)
2189 spin_button_redraw (spin_button);
2191 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2193 if (val < spin_button->adjustment->lower)
2194 val = spin_button->adjustment->lower;
2195 else if (val > spin_button->adjustment->upper)
2196 val = spin_button->adjustment->upper;
2198 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
2200 val < spin_button->adjustment->lower ||
2201 val > spin_button->adjustment->upper))
2203 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2207 if (spin_button->snap_to_ticks)
2208 gtk_spin_button_snap (spin_button, val);
2211 if (fabs (val - spin_button->adjustment->value) > EPSILON)
2212 gtk_adjustment_set_value (spin_button->adjustment, val);
2216 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0,
2218 if (return_val == FALSE)
2219 gtk_spin_button_default_output (spin_button);
2225 spin_button_redraw (GtkSpinButton *spin_button)
2229 widget = GTK_WIDGET (spin_button);
2231 if (GTK_WIDGET_DRAWABLE (widget))
2233 gtk_widget_queue_draw (widget);
2235 /* We must invalidate the panel window ourselves, because it
2236 * is not a child of widget->window
2238 gdk_window_invalidate_rect (spin_button->panel, NULL, TRUE);