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"
39 #include "gtkentryprivate.h"
41 #include "gtkmarshalers.h"
42 #include "gtksettings.h"
43 #include "gtkprivate.h"
46 #define MIN_SPIN_BUTTON_WIDTH 30
47 #define MAX_TIMER_CALLS 5
50 #define MIN_ARROW_WIDTH 6
53 struct _GtkSpinButtonPriv
55 GtkSpinButtonUpdatePolicy update_policy;
56 GtkAdjustment *adjustment;
66 guint click_child : 2; /* valid: GTK_ARROW_UP=0, GTK_ARROW_DOWN=1 or 2=NONE/BOTH */
71 guint snap_to_ticks : 1;
72 guint timer_calls : 3;
99 static void gtk_spin_button_editable_init (GtkEditableClass *iface);
100 static void gtk_spin_button_finalize (GObject *object);
101 static void gtk_spin_button_destroy (GtkObject *object);
102 static void gtk_spin_button_set_property (GObject *object,
106 static void gtk_spin_button_get_property (GObject *object,
110 static void gtk_spin_button_map (GtkWidget *widget);
111 static void gtk_spin_button_unmap (GtkWidget *widget);
112 static void gtk_spin_button_realize (GtkWidget *widget);
113 static void gtk_spin_button_unrealize (GtkWidget *widget);
114 static void gtk_spin_button_size_request (GtkWidget *widget,
115 GtkRequisition *requisition);
116 static void gtk_spin_button_size_allocate (GtkWidget *widget,
117 GtkAllocation *allocation);
118 static gint gtk_spin_button_expose (GtkWidget *widget,
119 GdkEventExpose *event);
120 static gint gtk_spin_button_button_press (GtkWidget *widget,
121 GdkEventButton *event);
122 static gint gtk_spin_button_button_release (GtkWidget *widget,
123 GdkEventButton *event);
124 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
125 GdkEventMotion *event);
126 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
127 GdkEventCrossing *event);
128 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
129 GdkEventCrossing *event);
130 static gint gtk_spin_button_focus_out (GtkWidget *widget,
131 GdkEventFocus *event);
132 static void gtk_spin_button_grab_notify (GtkWidget *widget,
133 gboolean was_grabbed);
134 static void gtk_spin_button_state_changed (GtkWidget *widget,
135 GtkStateType previous_state);
136 static void gtk_spin_button_style_set (GtkWidget *widget,
137 GtkStyle *previous_style);
138 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
140 GtkArrowType arrow_type);
141 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
142 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
143 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
144 GtkSpinButton *spin_button);
145 static gint gtk_spin_button_key_release (GtkWidget *widget,
147 static gint gtk_spin_button_scroll (GtkWidget *widget,
148 GdkEventScroll *event);
149 static void gtk_spin_button_activate (GtkEntry *entry);
150 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
155 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
157 static void gtk_spin_button_insert_text (GtkEditable *editable,
158 const gchar *new_text,
159 gint new_text_length,
161 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
163 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
164 GtkScrollType scroll);
166 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
168 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
170 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
171 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
173 static guint spinbutton_signals[LAST_SIGNAL] = {0};
177 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
178 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
179 gtk_spin_button_editable_init))
181 #define add_spin_binding(binding_set, keyval, mask, scroll) \
182 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
184 GTK_TYPE_SCROLL_TYPE, scroll)
187 gtk_spin_button_class_init (GtkSpinButtonClass *class)
189 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
190 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
191 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
192 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
193 GtkBindingSet *binding_set;
195 gobject_class->finalize = gtk_spin_button_finalize;
197 gobject_class->set_property = gtk_spin_button_set_property;
198 gobject_class->get_property = gtk_spin_button_get_property;
200 object_class->destroy = gtk_spin_button_destroy;
202 widget_class->map = gtk_spin_button_map;
203 widget_class->unmap = gtk_spin_button_unmap;
204 widget_class->realize = gtk_spin_button_realize;
205 widget_class->unrealize = gtk_spin_button_unrealize;
206 widget_class->size_request = gtk_spin_button_size_request;
207 widget_class->size_allocate = gtk_spin_button_size_allocate;
208 widget_class->expose_event = gtk_spin_button_expose;
209 widget_class->scroll_event = gtk_spin_button_scroll;
210 widget_class->button_press_event = gtk_spin_button_button_press;
211 widget_class->button_release_event = gtk_spin_button_button_release;
212 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
213 widget_class->key_release_event = gtk_spin_button_key_release;
214 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
215 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
216 widget_class->focus_out_event = gtk_spin_button_focus_out;
217 widget_class->grab_notify = gtk_spin_button_grab_notify;
218 widget_class->state_changed = gtk_spin_button_state_changed;
219 widget_class->style_set = gtk_spin_button_style_set;
221 entry_class->activate = gtk_spin_button_activate;
222 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
225 class->output = NULL;
226 class->change_value = gtk_spin_button_real_change_value;
228 g_object_class_install_property (gobject_class,
230 g_param_spec_object ("adjustment",
232 P_("The adjustment that holds the value of the spinbutton"),
234 GTK_PARAM_READWRITE));
236 g_object_class_install_property (gobject_class,
238 g_param_spec_double ("climb-rate",
240 P_("The acceleration rate when you hold down a button"),
244 GTK_PARAM_READWRITE));
246 g_object_class_install_property (gobject_class,
248 g_param_spec_uint ("digits",
250 P_("The number of decimal places to display"),
254 GTK_PARAM_READWRITE));
256 g_object_class_install_property (gobject_class,
258 g_param_spec_boolean ("snap-to-ticks",
260 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
262 GTK_PARAM_READWRITE));
264 g_object_class_install_property (gobject_class,
266 g_param_spec_boolean ("numeric",
268 P_("Whether non-numeric characters should be ignored"),
270 GTK_PARAM_READWRITE));
272 g_object_class_install_property (gobject_class,
274 g_param_spec_boolean ("wrap",
276 P_("Whether a spin button should wrap upon reaching its limits"),
278 GTK_PARAM_READWRITE));
280 g_object_class_install_property (gobject_class,
282 g_param_spec_enum ("update-policy",
284 P_("Whether the spin button should update always, or only when the value is legal"),
285 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
287 GTK_PARAM_READWRITE));
289 g_object_class_install_property (gobject_class,
291 g_param_spec_double ("value",
293 P_("Reads the current value, or sets a new value"),
297 GTK_PARAM_READWRITE));
299 gtk_widget_class_install_style_property_parser (widget_class,
300 g_param_spec_enum ("shadow-type",
302 P_("Style of bevel around the spin button"),
303 GTK_TYPE_SHADOW_TYPE,
306 gtk_rc_property_parse_enum);
307 spinbutton_signals[INPUT] =
308 g_signal_new (I_("input"),
309 G_TYPE_FROM_CLASS (gobject_class),
311 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
313 _gtk_marshal_INT__POINTER,
318 * GtkSpinButton::output:
319 * @spin_button: the object which received the signal
321 * The ::output signal can be used to change to formatting
322 * of the value that is displayed in the spin buttons entry.
324 * /* show leading zeros */
326 * on_output (GtkSpinButton *spin,
329 * GtkAdjustment *adj;
333 * adj = gtk_spin_button_get_adjustment (spin);
334 * value = (int)gtk_adjustment_get_value (adj);
335 * text = g_strdup_printf ("%02d", value);
336 * gtk_entry_set_text (GTK_ENTRY (spin), text);
343 * Returns: %TRUE if the value has been displayed.
345 spinbutton_signals[OUTPUT] =
346 g_signal_new (I_("output"),
347 G_TYPE_FROM_CLASS (gobject_class),
349 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
350 _gtk_boolean_handled_accumulator, NULL,
351 _gtk_marshal_BOOLEAN__VOID,
354 spinbutton_signals[VALUE_CHANGED] =
355 g_signal_new (I_("value-changed"),
356 G_TYPE_FROM_CLASS (gobject_class),
358 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
360 _gtk_marshal_VOID__VOID,
364 * GtkSpinButton::wrapped:
365 * @spinbutton: the object which received the signal
367 * The wrapped signal is emitted right after the spinbutton wraps
368 * from its maximum to minimum value or vice-versa.
372 spinbutton_signals[WRAPPED] =
373 g_signal_new (I_("wrapped"),
374 G_TYPE_FROM_CLASS (gobject_class),
376 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
378 _gtk_marshal_VOID__VOID,
382 spinbutton_signals[CHANGE_VALUE] =
383 g_signal_new (I_("change-value"),
384 G_TYPE_FROM_CLASS (gobject_class),
385 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
386 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
388 _gtk_marshal_VOID__ENUM,
390 GTK_TYPE_SCROLL_TYPE);
392 binding_set = gtk_binding_set_by_class (class);
394 add_spin_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
395 add_spin_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
396 add_spin_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
397 add_spin_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
398 add_spin_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
399 add_spin_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
400 add_spin_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
401 add_spin_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
403 g_type_class_add_private (class, sizeof (GtkSpinButtonPriv));
407 gtk_spin_button_editable_init (GtkEditableClass *iface)
409 iface->insert_text = gtk_spin_button_insert_text;
413 gtk_spin_button_set_property (GObject *object,
418 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
419 GtkSpinButtonPriv *priv = spin_button->priv;
423 GtkAdjustment *adjustment;
425 case PROP_ADJUSTMENT:
426 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
428 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
429 gtk_spin_button_set_adjustment (spin_button, adjustment);
431 case PROP_CLIMB_RATE:
432 gtk_spin_button_configure (spin_button,
434 g_value_get_double (value),
438 gtk_spin_button_configure (spin_button,
441 g_value_get_uint (value));
443 case PROP_SNAP_TO_TICKS:
444 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
447 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
450 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
452 case PROP_UPDATE_POLICY:
453 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
456 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
459 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
465 gtk_spin_button_get_property (GObject *object,
470 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
471 GtkSpinButtonPriv *priv = spin_button->priv;
475 case PROP_ADJUSTMENT:
476 g_value_set_object (value, priv->adjustment);
478 case PROP_CLIMB_RATE:
479 g_value_set_double (value, priv->climb_rate);
482 g_value_set_uint (value, priv->digits);
484 case PROP_SNAP_TO_TICKS:
485 g_value_set_boolean (value, priv->snap_to_ticks);
488 g_value_set_boolean (value, priv->numeric);
491 g_value_set_boolean (value, priv->wrap);
493 case PROP_UPDATE_POLICY:
494 g_value_set_enum (value, priv->update_policy);
497 g_value_set_double (value, priv->adjustment->value);
500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
506 gtk_spin_button_init (GtkSpinButton *spin_button)
508 GtkSpinButtonPriv *priv;
510 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
511 GTK_TYPE_SPIN_BUTTON,
513 priv = spin_button->priv;
515 priv->adjustment = NULL;
518 priv->climb_rate = 0.0;
519 priv->timer_step = 0.0;
520 priv->update_policy = GTK_UPDATE_ALWAYS;
521 priv->in_child = NO_ARROW;
522 priv->click_child = NO_ARROW;
524 priv->need_timer = FALSE;
525 priv->timer_calls = 0;
527 priv->numeric = FALSE;
529 priv->snap_to_ticks = FALSE;
531 gtk_spin_button_set_adjustment (spin_button,
532 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
536 gtk_spin_button_finalize (GObject *object)
538 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
540 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
544 gtk_spin_button_destroy (GtkObject *object)
546 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
548 GTK_OBJECT_CLASS (gtk_spin_button_parent_class)->destroy (object);
552 gtk_spin_button_map (GtkWidget *widget)
554 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
555 GtkSpinButtonPriv *priv = spin_button->priv;
557 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
559 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
560 gdk_window_show (priv->panel);
565 gtk_spin_button_unmap (GtkWidget *widget)
567 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
568 GtkSpinButtonPriv *priv = spin_button->priv;
570 if (gtk_widget_get_mapped (widget))
572 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
574 gdk_window_hide (priv->panel);
575 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
580 gtk_spin_button_realize (GtkWidget *widget)
582 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
583 GtkSpinButtonPriv *priv = spin_button->priv;
584 GdkWindowAttr attributes;
585 gint attributes_mask;
589 arrow_size = spin_button_get_arrow_size (spin_button);
591 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
592 GDK_KEY_RELEASE_MASK);
593 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
595 attributes.window_type = GDK_WINDOW_CHILD;
596 attributes.wclass = GDK_INPUT_OUTPUT;
597 attributes.visual = gtk_widget_get_visual (widget);
598 attributes.colormap = gtk_widget_get_colormap (widget);
599 attributes.event_mask = gtk_widget_get_events (widget);
600 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
601 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
602 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
604 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
606 attributes.x = (widget->allocation.width - arrow_size -
607 2 * widget->style->xthickness);
608 attributes.y = (widget->allocation.height -
609 widget->requisition.height) / 2;
610 attributes.width = arrow_size + 2 * widget->style->xthickness;
611 attributes.height = widget->requisition.height;
613 priv->panel = gdk_window_new (widget->window,
614 &attributes, attributes_mask);
615 gdk_window_set_user_data (priv->panel, widget);
617 gtk_style_set_background (widget->style, priv->panel, GTK_STATE_NORMAL);
620 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
621 if (return_val == FALSE)
622 gtk_spin_button_default_output (spin_button);
624 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
628 gtk_spin_button_unrealize (GtkWidget *widget)
630 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
631 GtkSpinButtonPriv *priv = spin->priv;
633 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
637 gdk_window_set_user_data (priv->panel, NULL);
638 gdk_window_destroy (priv->panel);
644 compute_double_length (double val, int digits)
650 if (fabs (val) > 1.0)
651 a = floor (log10 (fabs (val))) + 1;
663 return a + digits + extra;
667 gtk_spin_button_size_request (GtkWidget *widget,
668 GtkRequisition *requisition)
670 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
671 GtkSpinButtonPriv *priv = spin_button->priv;
672 GtkEntry *entry = GTK_ENTRY (widget);
675 arrow_size = spin_button_get_arrow_size (spin_button);
677 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_request (widget, requisition);
679 if (entry->width_chars < 0)
681 PangoContext *context;
682 PangoFontMetrics *metrics;
688 gboolean interior_focus;
690 gint xborder, yborder;
691 GtkBorder inner_border;
693 gtk_widget_style_get (widget,
694 "interior-focus", &interior_focus,
695 "focus-line-width", &focus_width,
698 context = gtk_widget_get_pango_context (widget);
699 metrics = pango_context_get_metrics (context,
700 widget->style->font_desc,
701 pango_context_get_language (context));
703 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
704 digit_width = PANGO_SCALE *
705 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
707 pango_font_metrics_unref (metrics);
709 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
711 width = MIN_SPIN_BUTTON_WIDTH;
712 max_string_len = MAX (10, compute_double_length (1e9 * priv->adjustment->step_increment,
715 string_len = compute_double_length (priv->adjustment->upper,
717 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
718 width = MAX (width, w);
719 string_len = compute_double_length (priv->adjustment->lower,
721 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
722 width = MAX (width, w);
724 _gtk_entry_get_borders (entry, &xborder, &yborder);
725 _gtk_entry_effective_inner_border (entry, &inner_border);
727 requisition->width = width + xborder * 2 + inner_border.left + inner_border.right;
730 requisition->width += arrow_size + 2 * widget->style->xthickness;
734 gtk_spin_button_size_allocate (GtkWidget *widget,
735 GtkAllocation *allocation)
737 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
738 GtkSpinButtonPriv *priv = spin->priv;
739 GtkAllocation panel_allocation;
743 arrow_size = spin_button_get_arrow_size (spin);
744 panel_width = arrow_size + 2 * widget->style->xthickness;
746 widget->allocation = *allocation;
748 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
749 panel_allocation.x = 0;
751 panel_allocation.x = allocation->width - panel_width;
753 panel_allocation.width = panel_width;
754 panel_allocation.height = MIN (widget->requisition.height, allocation->height);
756 panel_allocation.y = 0;
758 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
760 if (gtk_widget_get_realized (widget))
762 gdk_window_move_resize (priv->panel,
765 panel_allocation.width,
766 panel_allocation.height);
769 gtk_widget_queue_draw (GTK_WIDGET (spin));
773 gtk_spin_button_expose (GtkWidget *widget,
774 GdkEventExpose *event)
776 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
777 GtkSpinButtonPriv *priv = spin->priv;
779 if (gtk_widget_is_drawable (widget))
781 if (event->window == priv->panel)
783 GtkShadowType shadow_type;
785 shadow_type = spin_button_get_shadow_type (spin);
787 if (shadow_type != GTK_SHADOW_NONE)
793 gtk_widget_style_get (widget, "state-hint", &state_hint, NULL);
795 state = gtk_widget_has_focus (widget) ?
796 GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
798 state = GTK_STATE_NORMAL;
800 gdk_drawable_get_size (priv->panel, &width, &height);
802 gtk_paint_box (widget->style, priv->panel,
804 &event->area, widget, "spinbutton",
805 0, 0, width, height);
808 gtk_spin_button_draw_arrow (spin, &event->area, GTK_ARROW_UP);
809 gtk_spin_button_draw_arrow (spin, &event->area, GTK_ARROW_DOWN);
812 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->expose_event (widget, event);
819 spin_button_at_limit (GtkSpinButton *spin_button,
822 GtkSpinButtonPriv *priv = spin_button->priv;
823 GtkArrowType effective_arrow;
828 if (priv->adjustment->step_increment > 0)
829 effective_arrow = arrow;
831 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
833 if (effective_arrow == GTK_ARROW_UP &&
834 (priv->adjustment->upper - priv->adjustment->value <= EPSILON))
837 if (effective_arrow == GTK_ARROW_DOWN &&
838 (priv->adjustment->value - priv->adjustment->lower <= EPSILON))
845 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
847 GtkArrowType arrow_type)
849 GtkSpinButtonPriv *priv;
850 GtkStateType state_type;
851 GtkShadowType shadow_type;
859 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
861 priv = spin_button->priv;
862 widget = GTK_WIDGET (spin_button);
864 if (gtk_widget_is_drawable (widget))
866 width = spin_button_get_arrow_size (spin_button) + 2 * widget->style->xthickness;
868 if (arrow_type == GTK_ARROW_UP)
873 height = widget->requisition.height / 2;
878 y = widget->requisition.height / 2;
880 height = (widget->requisition.height + 1) / 2;
883 if (spin_button_at_limit (spin_button, arrow_type))
885 shadow_type = GTK_SHADOW_OUT;
886 state_type = GTK_STATE_INSENSITIVE;
890 if (priv->click_child == arrow_type)
892 state_type = GTK_STATE_ACTIVE;
893 shadow_type = GTK_SHADOW_IN;
897 if (priv->in_child == arrow_type &&
898 priv->click_child == NO_ARROW)
900 state_type = GTK_STATE_PRELIGHT;
904 state_type = gtk_widget_get_state (widget);
907 shadow_type = GTK_SHADOW_OUT;
911 gtk_paint_box (widget->style, priv->panel,
912 state_type, shadow_type,
914 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
915 x, y, width, height);
917 height = widget->requisition.height;
919 if (arrow_type == GTK_ARROW_DOWN)
922 height = height - y - 2;
927 height = height / 2 - 2;
932 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
938 w -= w % 2 - 1; /* force odd */
941 x += (width - w) / 2;
942 y += (height - h) / 2;
947 gtk_paint_arrow (widget->style, priv->panel,
948 state_type, shadow_type,
949 area, widget, "spinbutton",
951 x, y, width, height);
956 gtk_spin_button_enter_notify (GtkWidget *widget,
957 GdkEventCrossing *event)
959 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
960 GtkSpinButtonPriv *priv = spin->priv;
962 if (event->window == priv->panel)
968 device = gdk_event_get_device ((GdkEvent *) event);
969 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
971 if (y <= widget->requisition.height / 2)
972 priv->in_child = GTK_ARROW_UP;
974 priv->in_child = GTK_ARROW_DOWN;
976 gtk_widget_queue_draw (GTK_WIDGET (spin));
979 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event)
980 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
986 gtk_spin_button_leave_notify (GtkWidget *widget,
987 GdkEventCrossing *event)
989 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
990 GtkSpinButtonPriv *priv = spin->priv;
992 priv->in_child = NO_ARROW;
993 gtk_widget_queue_draw (GTK_WIDGET (spin));
995 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event)
996 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1002 gtk_spin_button_focus_out (GtkWidget *widget,
1003 GdkEventFocus *event)
1005 if (GTK_ENTRY (widget)->editable)
1006 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1008 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1012 gtk_spin_button_grab_notify (GtkWidget *widget,
1013 gboolean was_grabbed)
1015 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1019 gtk_spin_button_stop_spinning (spin);
1020 gtk_widget_queue_draw (GTK_WIDGET (spin));
1025 gtk_spin_button_state_changed (GtkWidget *widget,
1026 GtkStateType previous_state)
1028 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1030 if (!gtk_widget_is_sensitive (widget))
1032 gtk_spin_button_stop_spinning (spin);
1033 gtk_widget_queue_draw (GTK_WIDGET (spin));
1038 gtk_spin_button_style_set (GtkWidget *widget,
1039 GtkStyle *previous_style)
1041 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1042 GtkSpinButtonPriv *priv = spin->priv;
1044 if (previous_style && gtk_widget_get_realized (widget))
1045 gtk_style_set_background (widget->style, priv->panel, GTK_STATE_NORMAL);
1047 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_set (widget, previous_style);
1052 gtk_spin_button_scroll (GtkWidget *widget,
1053 GdkEventScroll *event)
1055 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1056 GtkSpinButtonPriv *priv = spin->priv;
1058 if (event->direction == GDK_SCROLL_UP)
1060 if (!gtk_widget_has_focus (widget))
1061 gtk_widget_grab_focus (widget);
1062 gtk_spin_button_real_spin (spin, priv->adjustment->step_increment);
1064 else if (event->direction == GDK_SCROLL_DOWN)
1066 if (!gtk_widget_has_focus (widget))
1067 gtk_widget_grab_focus (widget);
1068 gtk_spin_button_real_spin (spin, -priv->adjustment->step_increment);
1077 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1079 GtkSpinButtonPriv *priv = spin->priv;
1083 g_source_remove (priv->timer);
1085 priv->timer_calls = 0;
1086 priv->need_timer = FALSE;
1091 priv->timer_step = priv->adjustment->step_increment;
1092 priv->timer_calls = 0;
1094 priv->click_child = NO_ARROW;
1099 start_spinning (GtkSpinButton *spin,
1100 GtkArrowType click_child,
1103 GtkSpinButtonPriv *priv;
1105 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1109 priv->click_child = click_child;
1113 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1116 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1118 priv->timer_step = step;
1119 priv->need_timer = TRUE;
1120 priv->timer = gdk_threads_add_timeout (timeout,
1121 (GSourceFunc) gtk_spin_button_timer,
1124 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1126 gtk_widget_queue_draw (GTK_WIDGET (spin));
1130 gtk_spin_button_button_press (GtkWidget *widget,
1131 GdkEventButton *event)
1133 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1134 GtkSpinButtonPriv *priv = spin->priv;
1138 if (event->window == priv->panel)
1140 if (!gtk_widget_has_focus (widget))
1141 gtk_widget_grab_focus (widget);
1142 priv->button = event->button;
1144 if (GTK_ENTRY (widget)->editable)
1145 gtk_spin_button_update (spin);
1147 if (event->y <= widget->requisition.height / 2)
1149 if (event->button == 1)
1150 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->step_increment);
1151 else if (event->button == 2)
1152 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->page_increment);
1154 priv->click_child = GTK_ARROW_UP;
1158 if (event->button == 1)
1159 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->step_increment);
1160 else if (event->button == 2)
1161 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->page_increment);
1163 priv->click_child = GTK_ARROW_DOWN;
1168 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1174 gtk_spin_button_button_release (GtkWidget *widget,
1175 GdkEventButton *event)
1177 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1178 GtkSpinButtonPriv *priv = spin->priv;
1181 arrow_size = spin_button_get_arrow_size (spin);
1183 if (event->button == priv->button)
1185 int click_child = priv->click_child;
1187 gtk_spin_button_stop_spinning (spin);
1189 if (event->button == 3)
1191 if (event->y >= 0 && event->x >= 0 &&
1192 event->y <= widget->requisition.height &&
1193 event->x <= arrow_size + 2 * widget->style->xthickness)
1195 if (click_child == GTK_ARROW_UP &&
1196 event->y <= widget->requisition.height / 2)
1200 diff = priv->adjustment->upper - priv->adjustment->value;
1202 gtk_spin_button_real_spin (spin, diff);
1204 else if (click_child == GTK_ARROW_DOWN &&
1205 event->y > widget->requisition.height / 2)
1209 diff = priv->adjustment->value - priv->adjustment->lower;
1211 gtk_spin_button_real_spin (spin, -diff);
1215 gtk_widget_queue_draw (GTK_WIDGET (spin));
1220 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1224 gtk_spin_button_motion_notify (GtkWidget *widget,
1225 GdkEventMotion *event)
1227 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1228 GtkSpinButtonPriv *priv = spin->priv;
1233 if (event->window == priv->panel)
1237 gdk_event_request_motions (event);
1239 if (y <= widget->requisition.height / 2 &&
1240 priv->in_child == GTK_ARROW_DOWN)
1242 priv->in_child = GTK_ARROW_UP;
1243 gtk_widget_queue_draw (GTK_WIDGET (spin));
1245 else if (y > widget->requisition.height / 2 &&
1246 priv->in_child == GTK_ARROW_UP)
1248 priv->in_child = GTK_ARROW_DOWN;
1249 gtk_widget_queue_draw (GTK_WIDGET (spin));
1255 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1259 gtk_spin_button_timer (GtkSpinButton *spin_button)
1261 GtkSpinButtonPriv *priv = spin_button->priv;
1262 gboolean retval = FALSE;
1266 if (priv->click_child == GTK_ARROW_UP)
1267 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1269 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1271 if (priv->need_timer)
1273 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1276 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1278 priv->need_timer = FALSE;
1279 priv->timer = gdk_threads_add_timeout (timeout,
1280 (GSourceFunc) gtk_spin_button_timer,
1281 (gpointer) spin_button);
1285 if (priv->climb_rate > 0.0 && priv->timer_step
1286 < priv->adjustment->page_increment)
1288 if (priv->timer_calls < MAX_TIMER_CALLS)
1289 priv->timer_calls++;
1292 priv->timer_calls = 0;
1293 priv->timer_step += priv->climb_rate;
1304 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1305 GtkSpinButton *spin_button)
1307 gboolean return_val;
1309 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1312 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1313 if (return_val == FALSE)
1314 gtk_spin_button_default_output (spin_button);
1316 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1318 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1320 g_object_notify (G_OBJECT (spin_button), "value");
1324 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1325 GtkScrollType scroll)
1327 GtkSpinButtonPriv *priv = spin->priv;
1330 /* When the key binding is activated, there may be an outstanding
1331 * value, so we first have to commit what is currently written in
1332 * the spin buttons text entry. See #106574
1334 gtk_spin_button_update (spin);
1336 old_value = priv->adjustment->value;
1338 /* We don't test whether the entry is editable, since
1339 * this key binding conceptually corresponds to changing
1340 * the value with the buttons using the mouse, which
1341 * we allow for non-editable spin buttons.
1345 case GTK_SCROLL_STEP_BACKWARD:
1346 case GTK_SCROLL_STEP_DOWN:
1347 case GTK_SCROLL_STEP_LEFT:
1348 gtk_spin_button_real_spin (spin, -priv->timer_step);
1350 if (priv->climb_rate > 0.0 && priv->timer_step
1351 < priv->adjustment->page_increment)
1353 if (priv->timer_calls < MAX_TIMER_CALLS)
1354 priv->timer_calls++;
1357 priv->timer_calls = 0;
1358 priv->timer_step += priv->climb_rate;
1363 case GTK_SCROLL_STEP_FORWARD:
1364 case GTK_SCROLL_STEP_UP:
1365 case GTK_SCROLL_STEP_RIGHT:
1366 gtk_spin_button_real_spin (spin, priv->timer_step);
1368 if (priv->climb_rate > 0.0 && priv->timer_step
1369 < priv->adjustment->page_increment)
1371 if (priv->timer_calls < MAX_TIMER_CALLS)
1372 priv->timer_calls++;
1375 priv->timer_calls = 0;
1376 priv->timer_step += priv->climb_rate;
1381 case GTK_SCROLL_PAGE_BACKWARD:
1382 case GTK_SCROLL_PAGE_DOWN:
1383 case GTK_SCROLL_PAGE_LEFT:
1384 gtk_spin_button_real_spin (spin, -priv->adjustment->page_increment);
1387 case GTK_SCROLL_PAGE_FORWARD:
1388 case GTK_SCROLL_PAGE_UP:
1389 case GTK_SCROLL_PAGE_RIGHT:
1390 gtk_spin_button_real_spin (spin, priv->adjustment->page_increment);
1393 case GTK_SCROLL_START:
1395 gdouble diff = priv->adjustment->value - priv->adjustment->lower;
1397 gtk_spin_button_real_spin (spin, -diff);
1401 case GTK_SCROLL_END:
1403 gdouble diff = priv->adjustment->upper - priv->adjustment->value;
1405 gtk_spin_button_real_spin (spin, diff);
1410 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1414 gtk_spin_button_update (spin);
1416 if (priv->adjustment->value == old_value)
1417 gtk_widget_error_bell (GTK_WIDGET (spin));
1421 gtk_spin_button_key_release (GtkWidget *widget,
1424 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1425 GtkSpinButtonPriv *priv = spin->priv;
1427 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1428 priv->timer_step = priv->adjustment->step_increment;
1429 priv->timer_calls = 0;
1435 gtk_spin_button_snap (GtkSpinButton *spin_button,
1438 GtkSpinButtonPriv *priv = spin_button->priv;
1442 inc = priv->adjustment->step_increment;
1446 tmp = (val - priv->adjustment->lower) / inc;
1447 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1448 val = priv->adjustment->lower + floor (tmp) * inc;
1450 val = priv->adjustment->lower + ceil (tmp) * inc;
1452 gtk_spin_button_set_value (spin_button, val);
1456 gtk_spin_button_activate (GtkEntry *entry)
1458 if (entry->editable)
1459 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1461 /* Chain up so that entry->activates_default is honored */
1462 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1466 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1475 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1477 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1478 panel_width = arrow_size + 2 * GTK_WIDGET (entry)->style->xthickness;
1481 *width -= panel_width;
1483 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1488 gtk_spin_button_insert_text (GtkEditable *editable,
1489 const gchar *new_text,
1490 gint new_text_length,
1493 GtkEntry *entry = GTK_ENTRY (editable);
1494 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1495 GtkSpinButtonPriv *priv = spin->priv;
1496 GtkEditableClass *parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class, GTK_TYPE_EDITABLE);
1507 const gchar *entry_text;
1509 entry_length = gtk_entry_get_text_length (entry);
1510 entry_text = gtk_entry_get_text (entry);
1514 if (*(lc->negative_sign))
1515 neg_sign = *(lc->negative_sign);
1519 if (*(lc->positive_sign))
1520 pos_sign = *(lc->positive_sign);
1525 /* Workaround for bug caused by some Windows application messing
1526 * up the positive sign of the current locale, more specifically
1527 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1528 * See bug #330743 and for instance
1529 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1531 * I don't know if the positive sign always gets bogusly set to
1532 * a digit when the above Registry value is corrupted as
1533 * described. (In my test case, it got set to "8", and in the
1534 * bug report above it presumably was set ot "0".) Probably it
1535 * might get set to almost anything? So how to distinguish a
1536 * bogus value from some correct one for some locale? That is
1537 * probably hard, but at least we should filter out the
1540 if (pos_sign >= '0' && pos_sign <= '9')
1544 for (sign=0, i=0; i<entry_length; i++)
1545 if ((entry_text[i] == neg_sign) ||
1546 (entry_text[i] == pos_sign))
1552 if (sign && !(*position))
1555 for (dotpos=-1, i=0; i<entry_length; i++)
1556 if (entry_text[i] == *(lc->decimal_point))
1562 if (dotpos > -1 && *position > dotpos &&
1563 (gint)priv->digits - entry_length
1564 + dotpos - new_text_length + 1 < 0)
1567 for (i = 0; i < new_text_length; i++)
1569 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1571 if (sign || (*position) || i)
1575 else if (new_text[i] == *(lc->decimal_point))
1577 if (!priv->digits || dotpos > -1 ||
1578 (new_text_length - 1 - i + entry_length
1579 - *position > (gint)priv->digits))
1581 dotpos = *position + i;
1583 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1588 parent_editable_iface->insert_text (editable, new_text,
1589 new_text_length, position);
1593 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1596 GtkSpinButtonPriv *priv = spin_button->priv;
1598 gdouble new_value = 0.0;
1599 gboolean wrapped = FALSE;
1601 adj = priv->adjustment;
1603 new_value = adj->value + increment;
1609 if (fabs (adj->value - adj->upper) < EPSILON)
1611 new_value = adj->lower;
1614 else if (new_value > adj->upper)
1615 new_value = adj->upper;
1618 new_value = MIN (new_value, adj->upper);
1620 else if (increment < 0)
1624 if (fabs (adj->value - adj->lower) < EPSILON)
1626 new_value = adj->upper;
1629 else if (new_value < adj->lower)
1630 new_value = adj->lower;
1633 new_value = MAX (new_value, adj->lower);
1636 if (fabs (new_value - adj->value) > EPSILON)
1637 gtk_adjustment_set_value (adj, new_value);
1640 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1642 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1646 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1651 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1653 return GTK_INPUT_ERROR;
1659 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1661 GtkSpinButtonPriv *priv = spin_button->priv;
1663 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, priv->adjustment->value);
1665 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1666 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1672 /***********************************************************
1673 ***********************************************************
1674 *** Public interface ***
1675 ***********************************************************
1676 ***********************************************************/
1680 * gtk_spin_button_configure:
1681 * @spin_button: a #GtkSpinButton
1682 * @adjustment: (allow-none): a #GtkAdjustment.
1683 * @climb_rate: the new climb rate.
1684 * @digits: the number of decimal places to display in the spin button.
1686 * Changes the properties of an existing spin button. The adjustment, climb rate,
1687 * and number of decimal places are all changed accordingly, after this function call.
1690 gtk_spin_button_configure (GtkSpinButton *spin_button,
1691 GtkAdjustment *adjustment,
1695 GtkSpinButtonPriv *priv;
1697 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1699 priv = spin_button->priv;
1702 gtk_spin_button_set_adjustment (spin_button, adjustment);
1704 adjustment = priv->adjustment;
1706 g_object_freeze_notify (G_OBJECT (spin_button));
1707 if (priv->digits != digits)
1709 priv->digits = digits;
1710 g_object_notify (G_OBJECT (spin_button), "digits");
1713 if (priv->climb_rate != climb_rate)
1715 priv->climb_rate = climb_rate;
1716 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1718 g_object_thaw_notify (G_OBJECT (spin_button));
1720 gtk_adjustment_value_changed (adjustment);
1724 gtk_spin_button_new (GtkAdjustment *adjustment,
1728 GtkSpinButton *spin;
1731 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1733 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1735 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1737 return GTK_WIDGET (spin);
1741 * gtk_spin_button_new_with_range:
1742 * @min: Minimum allowable value
1743 * @max: Maximum allowable value
1744 * @step: Increment added or subtracted by spinning the widget
1746 * This is a convenience constructor that allows creation of a numeric
1747 * #GtkSpinButton without manually creating an adjustment. The value is
1748 * initially set to the minimum value and a page increment of 10 * @step
1749 * is the default. The precision of the spin button is equivalent to the
1750 * precision of @step.
1752 * Note that the way in which the precision is derived works best if @step
1753 * is a power of ten. If the resulting precision is not suitable for your
1754 * needs, use gtk_spin_button_set_digits() to correct it.
1756 * Return value: The new spin button as a #GtkWidget.
1759 gtk_spin_button_new_with_range (gdouble min,
1764 GtkSpinButton *spin;
1767 g_return_val_if_fail (min <= max, NULL);
1768 g_return_val_if_fail (step != 0.0, NULL);
1770 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1772 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1774 if (fabs (step) >= 1.0 || step == 0.0)
1777 digits = abs ((gint) floor (log10 (fabs (step))));
1778 if (digits > MAX_DIGITS)
1779 digits = MAX_DIGITS;
1782 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1784 gtk_spin_button_set_numeric (spin, TRUE);
1786 return GTK_WIDGET (spin);
1789 /* Callback used when the spin button's adjustment changes. We need to redraw
1790 * the arrows when the adjustment's range changes, and reevaluate our size request.
1793 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1795 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1796 GtkSpinButtonPriv *priv = spin_button->priv;
1798 priv->timer_step = priv->adjustment->step_increment;
1799 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1803 * gtk_spin_button_set_adjustment:
1804 * @spin_button: a #GtkSpinButton
1805 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1807 * Replaces the #GtkAdjustment associated with @spin_button.
1810 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1811 GtkAdjustment *adjustment)
1813 GtkSpinButtonPriv *priv;
1815 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1817 priv = spin_button->priv;
1819 if (priv->adjustment != adjustment)
1821 if (priv->adjustment)
1823 g_signal_handlers_disconnect_by_func (priv->adjustment,
1824 gtk_spin_button_value_changed,
1826 g_signal_handlers_disconnect_by_func (priv->adjustment,
1827 adjustment_changed_cb,
1829 g_object_unref (priv->adjustment);
1831 priv->adjustment = adjustment;
1834 g_object_ref_sink (adjustment);
1835 g_signal_connect (adjustment, "value-changed",
1836 G_CALLBACK (gtk_spin_button_value_changed),
1838 g_signal_connect (adjustment, "changed",
1839 G_CALLBACK (adjustment_changed_cb),
1841 priv->timer_step = priv->adjustment->step_increment;
1844 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1847 g_object_notify (G_OBJECT (spin_button), "adjustment");
1851 * gtk_spin_button_get_adjustment:
1852 * @spin_button: a #GtkSpinButton
1854 * Get the adjustment associated with a #GtkSpinButton
1856 * Return value: the #GtkAdjustment of @spin_button
1859 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1861 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1863 return spin_button->priv->adjustment;
1867 * gtk_spin_button_set_digits:
1868 * @spin_button: a #GtkSpinButton
1869 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1871 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1875 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1878 GtkSpinButtonPriv *priv;
1880 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1882 priv = spin_button->priv;
1884 if (priv->digits != digits)
1886 priv->digits = digits;
1887 gtk_spin_button_value_changed (priv->adjustment, spin_button);
1888 g_object_notify (G_OBJECT (spin_button), "digits");
1890 /* since lower/upper may have changed */
1891 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1896 * gtk_spin_button_get_digits:
1897 * @spin_button: a #GtkSpinButton
1899 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1901 * Returns: the current precision
1904 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1906 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1908 return spin_button->priv->digits;
1912 * gtk_spin_button_set_increments:
1913 * @spin_button: a #GtkSpinButton
1914 * @step: increment applied for a button 1 press.
1915 * @page: increment applied for a button 2 press.
1917 * Sets the step and page increments for spin_button. This affects how
1918 * quickly the value changes when the spin button's arrows are activated.
1921 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1925 GtkSpinButtonPriv *priv;
1927 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1929 priv = spin_button->priv;
1931 priv->adjustment->step_increment = step;
1932 priv->adjustment->page_increment = page;
1936 * gtk_spin_button_get_increments:
1937 * @spin_button: a #GtkSpinButton
1938 * @step: (allow-none): location to store step increment, or %NULL
1939 * @page: (allow-none): location to store page increment, or %NULL
1941 * Gets the current step and page the increments used by @spin_button. See
1942 * gtk_spin_button_set_increments().
1945 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1949 GtkSpinButtonPriv *priv;
1951 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1953 priv = spin_button->priv;
1956 *step = priv->adjustment->step_increment;
1958 *page = priv->adjustment->page_increment;
1962 * gtk_spin_button_set_range:
1963 * @spin_button: a #GtkSpinButton
1964 * @min: minimum allowable value
1965 * @max: maximum allowable value
1967 * Sets the minimum and maximum allowable values for @spin_button
1970 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1974 GtkSpinButtonPriv *priv;
1977 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1979 priv = spin_button->priv;
1981 priv->adjustment->lower = min;
1982 priv->adjustment->upper = max;
1984 value = CLAMP (priv->adjustment->value,
1985 priv->adjustment->lower,
1986 (priv->adjustment->upper - priv->adjustment->page_size));
1988 if (value != priv->adjustment->value)
1989 gtk_spin_button_set_value (spin_button, value);
1991 gtk_adjustment_changed (priv->adjustment);
1995 * gtk_spin_button_get_range:
1996 * @spin_button: a #GtkSpinButton
1997 * @min: (allow-none): location to store minimum allowed value, or %NULL
1998 * @max: (allow-none): location to store maximum allowed value, or %NULL
2000 * Gets the range allowed for @spin_button. See
2001 * gtk_spin_button_set_range().
2004 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2008 GtkSpinButtonPriv *priv;
2010 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2012 priv = spin_button->priv;
2015 *min = priv->adjustment->lower;
2017 *max = priv->adjustment->upper;
2021 * gtk_spin_button_get_value:
2022 * @spin_button: a #GtkSpinButton
2024 * Get the value in the @spin_button.
2026 * Return value: the value of @spin_button
2029 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2031 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2033 return spin_button->priv->adjustment->value;
2037 * gtk_spin_button_get_value_as_int:
2038 * @spin_button: a #GtkSpinButton
2040 * Get the value @spin_button represented as an integer.
2042 * Return value: the value of @spin_button
2045 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2047 GtkSpinButtonPriv *priv;
2050 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2052 priv = spin_button->priv;
2054 val = priv->adjustment->value;
2055 if (val - floor (val) < ceil (val) - val)
2062 * gtk_spin_button_set_value:
2063 * @spin_button: a #GtkSpinButton
2064 * @value: the new value
2066 * Set the value of @spin_button.
2069 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2072 GtkSpinButtonPriv *priv;
2074 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2076 priv = spin_button->priv;
2078 if (fabs (value - priv->adjustment->value) > EPSILON)
2079 gtk_adjustment_set_value (priv->adjustment, value);
2082 gint return_val = FALSE;
2083 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2084 if (return_val == FALSE)
2085 gtk_spin_button_default_output (spin_button);
2090 * gtk_spin_button_set_update_policy:
2091 * @spin_button: a #GtkSpinButton
2092 * @policy: a #GtkSpinButtonUpdatePolicy value
2094 * Sets the update behavior of a spin button. This determines whether the
2095 * spin button is always updated or only when a valid value is set.
2098 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2099 GtkSpinButtonUpdatePolicy policy)
2101 GtkSpinButtonPriv *priv;
2103 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2105 priv = spin_button->priv;
2107 if (priv->update_policy != policy)
2109 priv->update_policy = policy;
2110 g_object_notify (G_OBJECT (spin_button), "update-policy");
2115 * gtk_spin_button_get_update_policy:
2116 * @spin_button: a #GtkSpinButton
2118 * Gets the update behavior of a spin button. See
2119 * gtk_spin_button_set_update_policy().
2121 * Return value: the current update policy
2123 GtkSpinButtonUpdatePolicy
2124 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2126 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2128 return spin_button->priv->update_policy;
2132 * gtk_spin_button_set_numeric:
2133 * @spin_button: a #GtkSpinButton
2134 * @numeric: flag indicating if only numeric entry is allowed.
2136 * Sets the flag that determines if non-numeric text can be typed into
2140 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2143 GtkSpinButtonPriv *priv;
2145 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2147 priv = spin_button->priv;
2149 numeric = numeric != FALSE;
2151 if (priv->numeric != numeric)
2153 priv->numeric = numeric;
2154 g_object_notify (G_OBJECT (spin_button), "numeric");
2159 * gtk_spin_button_get_numeric:
2160 * @spin_button: a #GtkSpinButton
2162 * Returns whether non-numeric text can be typed into the spin button.
2163 * See gtk_spin_button_set_numeric().
2165 * Return value: %TRUE if only numeric text can be entered
2168 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2170 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2172 return spin_button->priv->numeric;
2176 * gtk_spin_button_set_wrap:
2177 * @spin_button: a #GtkSpinButton
2178 * @wrap: a flag indicating if wrapping behavior is performed.
2180 * Sets the flag that determines if a spin button value wraps around to the
2181 * opposite limit when the upper or lower limit of the range is exceeded.
2184 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2187 GtkSpinButtonPriv *priv;
2189 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2191 priv = spin_button->priv;
2193 wrap = wrap != FALSE;
2195 if (priv->wrap != wrap)
2197 priv->wrap = (wrap != 0);
2199 g_object_notify (G_OBJECT (spin_button), "wrap");
2204 * gtk_spin_button_get_wrap:
2205 * @spin_button: a #GtkSpinButton
2207 * Returns whether the spin button's value wraps around to the
2208 * opposite limit when the upper or lower limit of the range is
2209 * exceeded. See gtk_spin_button_set_wrap().
2211 * Return value: %TRUE if the spin button wraps around
2214 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2216 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2218 return spin_button->priv->wrap;
2222 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2224 gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
2227 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2229 return arrow_size - arrow_size % 2; /* force even */
2233 * spin_button_get_shadow_type:
2234 * @spin_button: a #GtkSpinButton
2236 * Convenience function to Get the shadow type from the underlying widget's
2239 * Return value: the #GtkShadowType
2242 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2244 GtkShadowType rc_shadow_type;
2246 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow-type", &rc_shadow_type, NULL);
2248 return rc_shadow_type;
2252 * gtk_spin_button_set_snap_to_ticks:
2253 * @spin_button: a #GtkSpinButton
2254 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2256 * Sets the policy as to whether values are corrected to the nearest step
2257 * increment when a spin button is activated after providing an invalid value.
2260 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2261 gboolean snap_to_ticks)
2263 GtkSpinButtonPriv *priv;
2266 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2268 priv = spin_button->priv;
2270 new_val = (snap_to_ticks != 0);
2272 if (new_val != priv->snap_to_ticks)
2274 priv->snap_to_ticks = new_val;
2275 if (new_val && GTK_ENTRY (spin_button)->editable)
2276 gtk_spin_button_update (spin_button);
2278 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2283 * gtk_spin_button_get_snap_to_ticks:
2284 * @spin_button: a #GtkSpinButton
2286 * Returns whether the values are corrected to the nearest step. See
2287 * gtk_spin_button_set_snap_to_ticks().
2289 * Return value: %TRUE if values are snapped to the nearest step.
2292 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2294 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2296 return spin_button->priv->snap_to_ticks;
2300 * gtk_spin_button_spin:
2301 * @spin_button: a #GtkSpinButton
2302 * @direction: a #GtkSpinType indicating the direction to spin.
2303 * @increment: step increment to apply in the specified direction.
2305 * Increment or decrement a spin button's value in a specified direction
2306 * by a specified amount.
2309 gtk_spin_button_spin (GtkSpinButton *spin_button,
2310 GtkSpinType direction,
2313 GtkSpinButtonPriv *priv;
2317 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2319 priv = spin_button->priv;
2321 adj = priv->adjustment;
2323 /* for compatibility with the 1.0.x version of this function */
2324 if (increment != 0 && increment != adj->step_increment &&
2325 (direction == GTK_SPIN_STEP_FORWARD ||
2326 direction == GTK_SPIN_STEP_BACKWARD))
2328 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2329 increment = -increment;
2330 direction = GTK_SPIN_USER_DEFINED;
2335 case GTK_SPIN_STEP_FORWARD:
2337 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2340 case GTK_SPIN_STEP_BACKWARD:
2342 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2345 case GTK_SPIN_PAGE_FORWARD:
2347 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2350 case GTK_SPIN_PAGE_BACKWARD:
2352 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2357 diff = adj->value - adj->lower;
2359 gtk_spin_button_real_spin (spin_button, -diff);
2364 diff = adj->upper - adj->value;
2366 gtk_spin_button_real_spin (spin_button, diff);
2369 case GTK_SPIN_USER_DEFINED:
2372 gtk_spin_button_real_spin (spin_button, increment);
2381 * gtk_spin_button_update:
2382 * @spin_button: a #GtkSpinButton
2384 * Manually force an update of the spin button.
2387 gtk_spin_button_update (GtkSpinButton *spin_button)
2389 GtkSpinButtonPriv *priv;
2394 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2396 priv = spin_button->priv;
2399 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2400 if (return_val == FALSE)
2402 return_val = gtk_spin_button_default_input (spin_button, &val);
2403 error = (return_val == GTK_INPUT_ERROR);
2405 else if (return_val == GTK_INPUT_ERROR)
2408 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2410 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2412 if (val < priv->adjustment->lower)
2413 val = priv->adjustment->lower;
2414 else if (val > priv->adjustment->upper)
2415 val = priv->adjustment->upper;
2417 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2419 val < priv->adjustment->lower ||
2420 val > priv->adjustment->upper))
2422 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2426 if (priv->snap_to_ticks)
2427 gtk_spin_button_snap (spin_button, val);
2429 gtk_spin_button_set_value (spin_button, val);
2433 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2435 return spin_button->priv->panel;