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 _GtkSpinButtonPrivate
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 (GtkEditableInterface *iface);
100 static void gtk_spin_button_finalize (GObject *object);
101 static void gtk_spin_button_set_property (GObject *object,
105 static void gtk_spin_button_get_property (GObject *object,
109 static void gtk_spin_button_destroy (GtkWidget *widget);
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_draw (GtkWidget *widget,
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 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
191 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
192 GtkBindingSet *binding_set;
194 gobject_class->finalize = gtk_spin_button_finalize;
195 gobject_class->set_property = gtk_spin_button_set_property;
196 gobject_class->get_property = gtk_spin_button_get_property;
198 widget_class->destroy = gtk_spin_button_destroy;
199 widget_class->map = gtk_spin_button_map;
200 widget_class->unmap = gtk_spin_button_unmap;
201 widget_class->realize = gtk_spin_button_realize;
202 widget_class->unrealize = gtk_spin_button_unrealize;
203 widget_class->size_request = gtk_spin_button_size_request;
204 widget_class->size_allocate = gtk_spin_button_size_allocate;
205 widget_class->draw = gtk_spin_button_draw;
206 widget_class->scroll_event = gtk_spin_button_scroll;
207 widget_class->button_press_event = gtk_spin_button_button_press;
208 widget_class->button_release_event = gtk_spin_button_button_release;
209 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
210 widget_class->key_release_event = gtk_spin_button_key_release;
211 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
212 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
213 widget_class->focus_out_event = gtk_spin_button_focus_out;
214 widget_class->grab_notify = gtk_spin_button_grab_notify;
215 widget_class->state_changed = gtk_spin_button_state_changed;
216 widget_class->style_set = gtk_spin_button_style_set;
218 entry_class->activate = gtk_spin_button_activate;
219 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
222 class->output = NULL;
223 class->change_value = gtk_spin_button_real_change_value;
225 g_object_class_install_property (gobject_class,
227 g_param_spec_object ("adjustment",
229 P_("The adjustment that holds the value of the spin button"),
231 GTK_PARAM_READWRITE));
233 g_object_class_install_property (gobject_class,
235 g_param_spec_double ("climb-rate",
237 P_("The acceleration rate when you hold down a button"),
241 GTK_PARAM_READWRITE));
243 g_object_class_install_property (gobject_class,
245 g_param_spec_uint ("digits",
247 P_("The number of decimal places to display"),
251 GTK_PARAM_READWRITE));
253 g_object_class_install_property (gobject_class,
255 g_param_spec_boolean ("snap-to-ticks",
257 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
259 GTK_PARAM_READWRITE));
261 g_object_class_install_property (gobject_class,
263 g_param_spec_boolean ("numeric",
265 P_("Whether non-numeric characters should be ignored"),
267 GTK_PARAM_READWRITE));
269 g_object_class_install_property (gobject_class,
271 g_param_spec_boolean ("wrap",
273 P_("Whether a spin button should wrap upon reaching its limits"),
275 GTK_PARAM_READWRITE));
277 g_object_class_install_property (gobject_class,
279 g_param_spec_enum ("update-policy",
281 P_("Whether the spin button should update always, or only when the value is legal"),
282 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
284 GTK_PARAM_READWRITE));
286 g_object_class_install_property (gobject_class,
288 g_param_spec_double ("value",
290 P_("Reads the current value, or sets a new value"),
294 GTK_PARAM_READWRITE));
296 gtk_widget_class_install_style_property_parser (widget_class,
297 g_param_spec_enum ("shadow-type",
299 P_("Style of bevel around the spin button"),
300 GTK_TYPE_SHADOW_TYPE,
303 gtk_rc_property_parse_enum);
304 spinbutton_signals[INPUT] =
305 g_signal_new (I_("input"),
306 G_TYPE_FROM_CLASS (gobject_class),
308 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
310 _gtk_marshal_INT__POINTER,
315 * GtkSpinButton::output:
316 * @spin_button: the object which received the signal
318 * The ::output signal can be used to change to formatting
319 * of the value that is displayed in the spin buttons entry.
321 * /* show leading zeros */
323 * on_output (GtkSpinButton *spin,
326 * GtkAdjustment *adj;
330 * adj = gtk_spin_button_get_adjustment (spin);
331 * value = (int)gtk_adjustment_get_value (adj);
332 * text = g_strdup_printf ("%02d", value);
333 * gtk_entry_set_text (GTK_ENTRY (spin), text);
340 * Returns: %TRUE if the value has been displayed.
342 spinbutton_signals[OUTPUT] =
343 g_signal_new (I_("output"),
344 G_TYPE_FROM_CLASS (gobject_class),
346 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
347 _gtk_boolean_handled_accumulator, NULL,
348 _gtk_marshal_BOOLEAN__VOID,
351 spinbutton_signals[VALUE_CHANGED] =
352 g_signal_new (I_("value-changed"),
353 G_TYPE_FROM_CLASS (gobject_class),
355 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
357 _gtk_marshal_VOID__VOID,
361 * GtkSpinButton::wrapped:
362 * @spinbutton: the object which received the signal
364 * The wrapped signal is emitted right after the spinbutton wraps
365 * from its maximum to minimum value or vice-versa.
369 spinbutton_signals[WRAPPED] =
370 g_signal_new (I_("wrapped"),
371 G_TYPE_FROM_CLASS (gobject_class),
373 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
375 _gtk_marshal_VOID__VOID,
379 spinbutton_signals[CHANGE_VALUE] =
380 g_signal_new (I_("change-value"),
381 G_TYPE_FROM_CLASS (gobject_class),
382 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
383 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
385 _gtk_marshal_VOID__ENUM,
387 GTK_TYPE_SCROLL_TYPE);
389 binding_set = gtk_binding_set_by_class (class);
391 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
392 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
393 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
394 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
395 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
396 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
397 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
398 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
400 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
404 gtk_spin_button_editable_init (GtkEditableInterface *iface)
406 iface->insert_text = gtk_spin_button_insert_text;
410 gtk_spin_button_set_property (GObject *object,
415 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
416 GtkSpinButtonPrivate *priv = spin_button->priv;
420 GtkAdjustment *adjustment;
422 case PROP_ADJUSTMENT:
423 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
425 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
426 gtk_spin_button_set_adjustment (spin_button, adjustment);
428 case PROP_CLIMB_RATE:
429 gtk_spin_button_configure (spin_button,
431 g_value_get_double (value),
435 gtk_spin_button_configure (spin_button,
438 g_value_get_uint (value));
440 case PROP_SNAP_TO_TICKS:
441 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
444 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
447 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
449 case PROP_UPDATE_POLICY:
450 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
453 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
456 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
462 gtk_spin_button_get_property (GObject *object,
467 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
468 GtkSpinButtonPrivate *priv = spin_button->priv;
472 case PROP_ADJUSTMENT:
473 g_value_set_object (value, priv->adjustment);
475 case PROP_CLIMB_RATE:
476 g_value_set_double (value, priv->climb_rate);
479 g_value_set_uint (value, priv->digits);
481 case PROP_SNAP_TO_TICKS:
482 g_value_set_boolean (value, priv->snap_to_ticks);
485 g_value_set_boolean (value, priv->numeric);
488 g_value_set_boolean (value, priv->wrap);
490 case PROP_UPDATE_POLICY:
491 g_value_set_enum (value, priv->update_policy);
494 g_value_set_double (value, priv->adjustment->value);
497 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
503 gtk_spin_button_init (GtkSpinButton *spin_button)
505 GtkSpinButtonPrivate *priv;
507 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
508 GTK_TYPE_SPIN_BUTTON,
509 GtkSpinButtonPrivate);
510 priv = spin_button->priv;
512 priv->adjustment = NULL;
515 priv->climb_rate = 0.0;
516 priv->timer_step = 0.0;
517 priv->update_policy = GTK_UPDATE_ALWAYS;
518 priv->in_child = NO_ARROW;
519 priv->click_child = NO_ARROW;
521 priv->need_timer = FALSE;
522 priv->timer_calls = 0;
524 priv->numeric = FALSE;
526 priv->snap_to_ticks = FALSE;
528 gtk_spin_button_set_adjustment (spin_button,
529 gtk_adjustment_new (0, 0, 0, 0, 0, 0));
533 gtk_spin_button_finalize (GObject *object)
535 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
537 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
541 gtk_spin_button_destroy (GtkWidget *widget)
543 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
545 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->destroy (widget);
549 gtk_spin_button_map (GtkWidget *widget)
551 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
552 GtkSpinButtonPrivate *priv = spin_button->priv;
554 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
556 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
557 gdk_window_show (priv->panel);
562 gtk_spin_button_unmap (GtkWidget *widget)
564 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
565 GtkSpinButtonPrivate *priv = spin_button->priv;
567 if (gtk_widget_get_mapped (widget))
569 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
571 gdk_window_hide (priv->panel);
572 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
577 gtk_spin_button_realize (GtkWidget *widget)
579 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
580 GtkSpinButtonPrivate *priv = spin_button->priv;
581 GtkAllocation allocation;
582 GtkRequisition requisition;
584 GdkWindowAttr attributes;
585 gint attributes_mask;
589 style = gtk_widget_get_style (widget);
591 arrow_size = spin_button_get_arrow_size (spin_button);
593 gtk_widget_get_preferred_size (widget, &requisition, NULL);
594 gtk_widget_get_allocation (widget, &allocation);
596 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
597 GDK_KEY_RELEASE_MASK);
598 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
600 attributes.window_type = GDK_WINDOW_CHILD;
601 attributes.wclass = GDK_INPUT_ONLY;
602 attributes.visual = gtk_widget_get_visual (widget);
603 attributes.event_mask = gtk_widget_get_events (widget);
604 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
605 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
606 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
608 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
610 attributes.x = allocation.width - arrow_size - 2 * style->xthickness;
611 attributes.y = (allocation.height - requisition.height) / 2;
612 attributes.width = arrow_size + 2 * style->xthickness;
613 attributes.height = requisition.height;
615 priv->panel = gdk_window_new (gtk_widget_get_window (widget),
616 &attributes, attributes_mask);
617 gdk_window_set_user_data (priv->panel, widget);
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 GtkSpinButtonPrivate *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 GtkSpinButtonPrivate *priv = spin_button->priv;
672 GtkEntry *entry = GTK_ENTRY (widget);
676 style = gtk_widget_get_style (widget);
678 arrow_size = spin_button_get_arrow_size (spin_button);
680 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_request (widget, requisition);
682 if (entry->width_chars < 0)
684 PangoContext *context;
685 PangoFontMetrics *metrics;
691 gboolean interior_focus;
693 gint xborder, yborder;
694 GtkBorder inner_border;
696 gtk_widget_style_get (widget,
697 "interior-focus", &interior_focus,
698 "focus-line-width", &focus_width,
701 context = gtk_widget_get_pango_context (widget);
702 metrics = pango_context_get_metrics (context,
704 pango_context_get_language (context));
706 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
707 digit_width = PANGO_SCALE *
708 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
710 pango_font_metrics_unref (metrics);
712 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
714 width = MIN_SPIN_BUTTON_WIDTH;
715 max_string_len = MAX (10, compute_double_length (1e9 * priv->adjustment->step_increment,
718 string_len = compute_double_length (priv->adjustment->upper,
720 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
721 width = MAX (width, w);
722 string_len = compute_double_length (priv->adjustment->lower,
724 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
725 width = MAX (width, w);
727 _gtk_entry_get_borders (entry, &xborder, &yborder);
728 _gtk_entry_effective_inner_border (entry, &inner_border);
730 requisition->width = width + xborder * 2 + inner_border.left + inner_border.right;
733 requisition->width += arrow_size + 2 * style->xthickness;
737 gtk_spin_button_size_allocate (GtkWidget *widget,
738 GtkAllocation *allocation)
740 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
741 GtkSpinButtonPrivate *priv = spin->priv;
742 GtkAllocation panel_allocation;
743 GtkRequisition requisition;
747 arrow_size = spin_button_get_arrow_size (spin);
748 panel_width = arrow_size + 2 * gtk_widget_get_style (widget)->xthickness;
750 gtk_widget_get_preferred_size (widget, &requisition, NULL);
752 gtk_widget_set_allocation (widget, allocation);
754 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
755 panel_allocation.x = 0;
757 panel_allocation.x = allocation->width - panel_width;
759 panel_allocation.width = panel_width;
760 panel_allocation.height = MIN (requisition.height, allocation->height);
762 panel_allocation.y = 0;
764 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
766 if (gtk_widget_get_realized (widget))
768 gdk_window_move_resize (priv->panel,
771 panel_allocation.width,
772 panel_allocation.height);
775 gtk_widget_queue_draw (GTK_WIDGET (spin));
779 gtk_spin_button_draw (GtkWidget *widget,
782 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
783 GtkSpinButtonPrivate *priv = spin->priv;
784 GtkShadowType shadow_type;
787 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
791 shadow_type = spin_button_get_shadow_type (spin);
793 state = gtk_widget_has_focus (widget) ?
794 GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
796 gtk_cairo_transform_to_window (cr, widget, priv->panel);
798 gtk_paint_box (gtk_widget_get_style (widget), cr,
800 widget, "spinbutton",
802 gdk_window_get_width (priv->panel),
803 gdk_window_get_height (priv->panel));
805 gtk_spin_button_draw_arrow (spin, cr, GTK_ARROW_UP);
806 gtk_spin_button_draw_arrow (spin, cr, GTK_ARROW_DOWN);
814 spin_button_at_limit (GtkSpinButton *spin_button,
817 GtkSpinButtonPrivate *priv = spin_button->priv;
818 GtkArrowType effective_arrow;
823 if (priv->adjustment->step_increment > 0)
824 effective_arrow = arrow;
826 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
828 if (effective_arrow == GTK_ARROW_UP &&
829 (priv->adjustment->upper - priv->adjustment->value <= EPSILON))
832 if (effective_arrow == GTK_ARROW_DOWN &&
833 (priv->adjustment->value - priv->adjustment->lower <= EPSILON))
840 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
842 GtkArrowType arrow_type)
844 GtkSpinButtonPrivate *priv;
845 GtkStateType state_type;
846 GtkShadowType shadow_type;
856 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
858 priv = spin_button->priv;
859 widget = GTK_WIDGET (spin_button);
861 style = gtk_widget_get_style (widget);
863 width = spin_button_get_arrow_size (spin_button) + 2 * style->xthickness;
864 panel_height = gdk_window_get_height (priv->panel);
866 if (arrow_type == GTK_ARROW_UP)
871 height = panel_height / 2;
876 y = panel_height / 2;
878 height = (panel_height + 1) / 2;
881 if (spin_button_at_limit (spin_button, arrow_type))
883 shadow_type = GTK_SHADOW_OUT;
884 state_type = GTK_STATE_INSENSITIVE;
888 if (priv->click_child == arrow_type)
890 state_type = GTK_STATE_ACTIVE;
891 shadow_type = GTK_SHADOW_IN;
895 if (priv->in_child == arrow_type &&
896 priv->click_child == NO_ARROW)
898 state_type = GTK_STATE_PRELIGHT;
902 state_type = gtk_widget_get_state (widget);
905 shadow_type = GTK_SHADOW_OUT;
909 gtk_paint_box (style, cr,
910 state_type, shadow_type,
912 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
913 x, y, width, height);
915 height = panel_height;
917 if (arrow_type == GTK_ARROW_DOWN)
920 height = height - y - 2;
925 height = height / 2 - 2;
930 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
936 w -= w % 2 - 1; /* force odd */
939 x += (width - w) / 2;
940 y += (height - h) / 2;
945 gtk_paint_arrow (style, cr,
946 state_type, shadow_type,
947 widget, "spinbutton",
949 x, y, width, height);
953 gtk_spin_button_enter_notify (GtkWidget *widget,
954 GdkEventCrossing *event)
956 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
957 GtkSpinButtonPrivate *priv = spin->priv;
958 GtkRequisition requisition;
960 if (event->window == priv->panel)
966 device = gdk_event_get_device ((GdkEvent *) event);
967 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
969 gtk_widget_get_preferred_size (widget, &requisition, NULL);
971 if (y <= 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 GtkSpinButtonPrivate *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 GtkSpinButtonPrivate *priv = spin->priv;
1044 if (previous_style && gtk_widget_get_realized (widget))
1045 gtk_style_set_background (gtk_widget_get_style (widget),
1046 priv->panel, GTK_STATE_NORMAL);
1048 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_set (widget, previous_style);
1053 gtk_spin_button_scroll (GtkWidget *widget,
1054 GdkEventScroll *event)
1056 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1057 GtkSpinButtonPrivate *priv = spin->priv;
1059 if (event->direction == GDK_SCROLL_UP)
1061 if (!gtk_widget_has_focus (widget))
1062 gtk_widget_grab_focus (widget);
1063 gtk_spin_button_real_spin (spin, priv->adjustment->step_increment);
1065 else if (event->direction == GDK_SCROLL_DOWN)
1067 if (!gtk_widget_has_focus (widget))
1068 gtk_widget_grab_focus (widget);
1069 gtk_spin_button_real_spin (spin, -priv->adjustment->step_increment);
1078 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1080 GtkSpinButtonPrivate *priv = spin->priv;
1084 g_source_remove (priv->timer);
1086 priv->timer_calls = 0;
1087 priv->need_timer = FALSE;
1092 priv->timer_step = priv->adjustment->step_increment;
1093 priv->timer_calls = 0;
1095 priv->click_child = NO_ARROW;
1100 start_spinning (GtkSpinButton *spin,
1101 GtkArrowType click_child,
1104 GtkSpinButtonPrivate *priv;
1106 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1110 priv->click_child = click_child;
1114 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1117 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1119 priv->timer_step = step;
1120 priv->need_timer = TRUE;
1121 priv->timer = gdk_threads_add_timeout (timeout,
1122 (GSourceFunc) gtk_spin_button_timer,
1125 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1127 gtk_widget_queue_draw (GTK_WIDGET (spin));
1131 gtk_spin_button_button_press (GtkWidget *widget,
1132 GdkEventButton *event)
1134 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1135 GtkSpinButtonPrivate *priv = spin->priv;
1139 if (event->window == priv->panel)
1141 GtkRequisition requisition;
1143 if (!gtk_widget_has_focus (widget))
1144 gtk_widget_grab_focus (widget);
1145 priv->button = event->button;
1147 if (GTK_ENTRY (widget)->editable)
1148 gtk_spin_button_update (spin);
1150 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1152 if (event->y <= requisition.height / 2)
1154 if (event->button == 1)
1155 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->step_increment);
1156 else if (event->button == 2)
1157 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->page_increment);
1159 priv->click_child = GTK_ARROW_UP;
1163 if (event->button == 1)
1164 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->step_increment);
1165 else if (event->button == 2)
1166 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->page_increment);
1168 priv->click_child = GTK_ARROW_DOWN;
1173 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1179 gtk_spin_button_button_release (GtkWidget *widget,
1180 GdkEventButton *event)
1182 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1183 GtkSpinButtonPrivate *priv = spin->priv;
1186 arrow_size = spin_button_get_arrow_size (spin);
1188 if (event->button == priv->button)
1190 int click_child = priv->click_child;
1192 gtk_spin_button_stop_spinning (spin);
1194 if (event->button == 3)
1196 GtkRequisition requisition;
1198 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1200 if (event->y >= 0 && event->x >= 0 &&
1201 event->y <= requisition.height &&
1202 event->x <= arrow_size + 2 * gtk_widget_get_style (widget)->xthickness)
1204 if (click_child == GTK_ARROW_UP &&
1205 event->y <= requisition.height / 2)
1209 diff = priv->adjustment->upper - priv->adjustment->value;
1211 gtk_spin_button_real_spin (spin, diff);
1213 else if (click_child == GTK_ARROW_DOWN &&
1214 event->y > requisition.height / 2)
1218 diff = priv->adjustment->value - priv->adjustment->lower;
1220 gtk_spin_button_real_spin (spin, -diff);
1224 gtk_widget_queue_draw (GTK_WIDGET (spin));
1229 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1233 gtk_spin_button_motion_notify (GtkWidget *widget,
1234 GdkEventMotion *event)
1236 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1237 GtkSpinButtonPrivate *priv = spin->priv;
1242 if (event->window == priv->panel)
1244 GtkRequisition requisition;
1247 gdk_event_request_motions (event);
1249 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1251 if (y <= requisition.height / 2 &&
1252 priv->in_child == GTK_ARROW_DOWN)
1254 priv->in_child = GTK_ARROW_UP;
1255 gtk_widget_queue_draw (GTK_WIDGET (spin));
1257 else if (y > requisition.height / 2 &&
1258 priv->in_child == GTK_ARROW_UP)
1260 priv->in_child = GTK_ARROW_DOWN;
1261 gtk_widget_queue_draw (GTK_WIDGET (spin));
1267 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1271 gtk_spin_button_timer (GtkSpinButton *spin_button)
1273 GtkSpinButtonPrivate *priv = spin_button->priv;
1274 gboolean retval = FALSE;
1278 if (priv->click_child == GTK_ARROW_UP)
1279 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1281 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1283 if (priv->need_timer)
1285 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1288 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1290 priv->need_timer = FALSE;
1291 priv->timer = gdk_threads_add_timeout (timeout,
1292 (GSourceFunc) gtk_spin_button_timer,
1293 (gpointer) spin_button);
1297 if (priv->climb_rate > 0.0 && priv->timer_step
1298 < priv->adjustment->page_increment)
1300 if (priv->timer_calls < MAX_TIMER_CALLS)
1301 priv->timer_calls++;
1304 priv->timer_calls = 0;
1305 priv->timer_step += priv->climb_rate;
1316 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1317 GtkSpinButton *spin_button)
1319 gboolean return_val;
1321 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1324 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1325 if (return_val == FALSE)
1326 gtk_spin_button_default_output (spin_button);
1328 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1330 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1332 g_object_notify (G_OBJECT (spin_button), "value");
1336 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1337 GtkScrollType scroll)
1339 GtkSpinButtonPrivate *priv = spin->priv;
1342 /* When the key binding is activated, there may be an outstanding
1343 * value, so we first have to commit what is currently written in
1344 * the spin buttons text entry. See #106574
1346 gtk_spin_button_update (spin);
1348 old_value = priv->adjustment->value;
1350 /* We don't test whether the entry is editable, since
1351 * this key binding conceptually corresponds to changing
1352 * the value with the buttons using the mouse, which
1353 * we allow for non-editable spin buttons.
1357 case GTK_SCROLL_STEP_BACKWARD:
1358 case GTK_SCROLL_STEP_DOWN:
1359 case GTK_SCROLL_STEP_LEFT:
1360 gtk_spin_button_real_spin (spin, -priv->timer_step);
1362 if (priv->climb_rate > 0.0 && priv->timer_step
1363 < priv->adjustment->page_increment)
1365 if (priv->timer_calls < MAX_TIMER_CALLS)
1366 priv->timer_calls++;
1369 priv->timer_calls = 0;
1370 priv->timer_step += priv->climb_rate;
1375 case GTK_SCROLL_STEP_FORWARD:
1376 case GTK_SCROLL_STEP_UP:
1377 case GTK_SCROLL_STEP_RIGHT:
1378 gtk_spin_button_real_spin (spin, priv->timer_step);
1380 if (priv->climb_rate > 0.0 && priv->timer_step
1381 < priv->adjustment->page_increment)
1383 if (priv->timer_calls < MAX_TIMER_CALLS)
1384 priv->timer_calls++;
1387 priv->timer_calls = 0;
1388 priv->timer_step += priv->climb_rate;
1393 case GTK_SCROLL_PAGE_BACKWARD:
1394 case GTK_SCROLL_PAGE_DOWN:
1395 case GTK_SCROLL_PAGE_LEFT:
1396 gtk_spin_button_real_spin (spin, -priv->adjustment->page_increment);
1399 case GTK_SCROLL_PAGE_FORWARD:
1400 case GTK_SCROLL_PAGE_UP:
1401 case GTK_SCROLL_PAGE_RIGHT:
1402 gtk_spin_button_real_spin (spin, priv->adjustment->page_increment);
1405 case GTK_SCROLL_START:
1407 gdouble diff = priv->adjustment->value - priv->adjustment->lower;
1409 gtk_spin_button_real_spin (spin, -diff);
1413 case GTK_SCROLL_END:
1415 gdouble diff = priv->adjustment->upper - priv->adjustment->value;
1417 gtk_spin_button_real_spin (spin, diff);
1422 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1426 gtk_spin_button_update (spin);
1428 if (priv->adjustment->value == old_value)
1429 gtk_widget_error_bell (GTK_WIDGET (spin));
1433 gtk_spin_button_key_release (GtkWidget *widget,
1436 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1437 GtkSpinButtonPrivate *priv = spin->priv;
1439 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1440 priv->timer_step = priv->adjustment->step_increment;
1441 priv->timer_calls = 0;
1447 gtk_spin_button_snap (GtkSpinButton *spin_button,
1450 GtkSpinButtonPrivate *priv = spin_button->priv;
1454 inc = priv->adjustment->step_increment;
1458 tmp = (val - priv->adjustment->lower) / inc;
1459 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1460 val = priv->adjustment->lower + floor (tmp) * inc;
1462 val = priv->adjustment->lower + ceil (tmp) * inc;
1464 gtk_spin_button_set_value (spin_button, val);
1468 gtk_spin_button_activate (GtkEntry *entry)
1470 if (entry->editable)
1471 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1473 /* Chain up so that entry->activates_default is honored */
1474 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1478 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1487 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1489 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1490 panel_width = arrow_size + 2 * gtk_widget_get_style (GTK_WIDGET (entry))->xthickness;
1493 *width -= panel_width;
1495 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1500 gtk_spin_button_insert_text (GtkEditable *editable,
1501 const gchar *new_text,
1502 gint new_text_length,
1505 GtkEntry *entry = GTK_ENTRY (editable);
1506 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1507 GtkSpinButtonPrivate *priv = spin->priv;
1508 GtkEditableInterface *parent_editable_iface;
1510 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1522 const gchar *entry_text;
1524 entry_length = gtk_entry_get_text_length (entry);
1525 entry_text = gtk_entry_get_text (entry);
1529 if (*(lc->negative_sign))
1530 neg_sign = *(lc->negative_sign);
1534 if (*(lc->positive_sign))
1535 pos_sign = *(lc->positive_sign);
1540 /* Workaround for bug caused by some Windows application messing
1541 * up the positive sign of the current locale, more specifically
1542 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1543 * See bug #330743 and for instance
1544 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1546 * I don't know if the positive sign always gets bogusly set to
1547 * a digit when the above Registry value is corrupted as
1548 * described. (In my test case, it got set to "8", and in the
1549 * bug report above it presumably was set ot "0".) Probably it
1550 * might get set to almost anything? So how to distinguish a
1551 * bogus value from some correct one for some locale? That is
1552 * probably hard, but at least we should filter out the
1555 if (pos_sign >= '0' && pos_sign <= '9')
1559 for (sign=0, i=0; i<entry_length; i++)
1560 if ((entry_text[i] == neg_sign) ||
1561 (entry_text[i] == pos_sign))
1567 if (sign && !(*position))
1570 for (dotpos=-1, i=0; i<entry_length; i++)
1571 if (entry_text[i] == *(lc->decimal_point))
1577 if (dotpos > -1 && *position > dotpos &&
1578 (gint)priv->digits - entry_length
1579 + dotpos - new_text_length + 1 < 0)
1582 for (i = 0; i < new_text_length; i++)
1584 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1586 if (sign || (*position) || i)
1590 else if (new_text[i] == *(lc->decimal_point))
1592 if (!priv->digits || dotpos > -1 ||
1593 (new_text_length - 1 - i + entry_length
1594 - *position > (gint)priv->digits))
1596 dotpos = *position + i;
1598 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1603 parent_editable_iface->insert_text (editable, new_text,
1604 new_text_length, position);
1608 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1611 GtkSpinButtonPrivate *priv = spin_button->priv;
1613 gdouble new_value = 0.0;
1614 gboolean wrapped = FALSE;
1616 adj = priv->adjustment;
1618 new_value = adj->value + increment;
1624 if (fabs (adj->value - adj->upper) < EPSILON)
1626 new_value = adj->lower;
1629 else if (new_value > adj->upper)
1630 new_value = adj->upper;
1633 new_value = MIN (new_value, adj->upper);
1635 else if (increment < 0)
1639 if (fabs (adj->value - adj->lower) < EPSILON)
1641 new_value = adj->upper;
1644 else if (new_value < adj->lower)
1645 new_value = adj->lower;
1648 new_value = MAX (new_value, adj->lower);
1651 if (fabs (new_value - adj->value) > EPSILON)
1652 gtk_adjustment_set_value (adj, new_value);
1655 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1657 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1661 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1666 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1668 return GTK_INPUT_ERROR;
1674 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1676 GtkSpinButtonPrivate *priv = spin_button->priv;
1678 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, priv->adjustment->value);
1680 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1681 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1687 /***********************************************************
1688 ***********************************************************
1689 *** Public interface ***
1690 ***********************************************************
1691 ***********************************************************/
1695 * gtk_spin_button_configure:
1696 * @spin_button: a #GtkSpinButton
1697 * @adjustment: (allow-none): a #GtkAdjustment.
1698 * @climb_rate: the new climb rate.
1699 * @digits: the number of decimal places to display in the spin button.
1701 * Changes the properties of an existing spin button. The adjustment, climb rate,
1702 * and number of decimal places are all changed accordingly, after this function call.
1705 gtk_spin_button_configure (GtkSpinButton *spin_button,
1706 GtkAdjustment *adjustment,
1710 GtkSpinButtonPrivate *priv;
1712 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1714 priv = spin_button->priv;
1717 gtk_spin_button_set_adjustment (spin_button, adjustment);
1719 adjustment = priv->adjustment;
1721 g_object_freeze_notify (G_OBJECT (spin_button));
1722 if (priv->digits != digits)
1724 priv->digits = digits;
1725 g_object_notify (G_OBJECT (spin_button), "digits");
1728 if (priv->climb_rate != climb_rate)
1730 priv->climb_rate = climb_rate;
1731 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1733 g_object_thaw_notify (G_OBJECT (spin_button));
1735 gtk_adjustment_value_changed (adjustment);
1739 gtk_spin_button_new (GtkAdjustment *adjustment,
1743 GtkSpinButton *spin;
1746 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1748 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1750 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1752 return GTK_WIDGET (spin);
1756 * gtk_spin_button_new_with_range:
1757 * @min: Minimum allowable value
1758 * @max: Maximum allowable value
1759 * @step: Increment added or subtracted by spinning the widget
1761 * This is a convenience constructor that allows creation of a numeric
1762 * #GtkSpinButton without manually creating an adjustment. The value is
1763 * initially set to the minimum value and a page increment of 10 * @step
1764 * is the default. The precision of the spin button is equivalent to the
1765 * precision of @step.
1767 * Note that the way in which the precision is derived works best if @step
1768 * is a power of ten. If the resulting precision is not suitable for your
1769 * needs, use gtk_spin_button_set_digits() to correct it.
1771 * Return value: The new spin button as a #GtkWidget.
1774 gtk_spin_button_new_with_range (gdouble min,
1779 GtkSpinButton *spin;
1782 g_return_val_if_fail (min <= max, NULL);
1783 g_return_val_if_fail (step != 0.0, NULL);
1785 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1787 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1789 if (fabs (step) >= 1.0 || step == 0.0)
1792 digits = abs ((gint) floor (log10 (fabs (step))));
1793 if (digits > MAX_DIGITS)
1794 digits = MAX_DIGITS;
1797 gtk_spin_button_configure (spin, adj, step, digits);
1799 gtk_spin_button_set_numeric (spin, TRUE);
1801 return GTK_WIDGET (spin);
1804 /* Callback used when the spin button's adjustment changes. We need to redraw
1805 * the arrows when the adjustment's range changes, and reevaluate our size request.
1808 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1810 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1811 GtkSpinButtonPrivate *priv = spin_button->priv;
1813 priv->timer_step = priv->adjustment->step_increment;
1814 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1818 * gtk_spin_button_set_adjustment:
1819 * @spin_button: a #GtkSpinButton
1820 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1822 * Replaces the #GtkAdjustment associated with @spin_button.
1825 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1826 GtkAdjustment *adjustment)
1828 GtkSpinButtonPrivate *priv;
1830 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1832 priv = spin_button->priv;
1834 if (priv->adjustment != adjustment)
1836 if (priv->adjustment)
1838 g_signal_handlers_disconnect_by_func (priv->adjustment,
1839 gtk_spin_button_value_changed,
1841 g_signal_handlers_disconnect_by_func (priv->adjustment,
1842 adjustment_changed_cb,
1844 g_object_unref (priv->adjustment);
1846 priv->adjustment = adjustment;
1849 g_object_ref_sink (adjustment);
1850 g_signal_connect (adjustment, "value-changed",
1851 G_CALLBACK (gtk_spin_button_value_changed),
1853 g_signal_connect (adjustment, "changed",
1854 G_CALLBACK (adjustment_changed_cb),
1856 priv->timer_step = priv->adjustment->step_increment;
1859 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1862 g_object_notify (G_OBJECT (spin_button), "adjustment");
1866 * gtk_spin_button_get_adjustment:
1867 * @spin_button: a #GtkSpinButton
1869 * Get the adjustment associated with a #GtkSpinButton
1871 * Return value: (transfer none): the #GtkAdjustment of @spin_button
1874 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1876 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1878 return spin_button->priv->adjustment;
1882 * gtk_spin_button_set_digits:
1883 * @spin_button: a #GtkSpinButton
1884 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1886 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1890 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1893 GtkSpinButtonPrivate *priv;
1895 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1897 priv = spin_button->priv;
1899 if (priv->digits != digits)
1901 priv->digits = digits;
1902 gtk_spin_button_value_changed (priv->adjustment, spin_button);
1903 g_object_notify (G_OBJECT (spin_button), "digits");
1905 /* since lower/upper may have changed */
1906 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1911 * gtk_spin_button_get_digits:
1912 * @spin_button: a #GtkSpinButton
1914 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1916 * Returns: the current precision
1919 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1921 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1923 return spin_button->priv->digits;
1927 * gtk_spin_button_set_increments:
1928 * @spin_button: a #GtkSpinButton
1929 * @step: increment applied for a button 1 press.
1930 * @page: increment applied for a button 2 press.
1932 * Sets the step and page increments for spin_button. This affects how
1933 * quickly the value changes when the spin button's arrows are activated.
1936 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1940 GtkSpinButtonPrivate *priv;
1942 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1944 priv = spin_button->priv;
1946 priv->adjustment->step_increment = step;
1947 priv->adjustment->page_increment = page;
1951 * gtk_spin_button_get_increments:
1952 * @spin_button: a #GtkSpinButton
1953 * @step: (allow-none): location to store step increment, or %NULL
1954 * @page: (allow-none): location to store page increment, or %NULL
1956 * Gets the current step and page the increments used by @spin_button. See
1957 * gtk_spin_button_set_increments().
1960 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1964 GtkSpinButtonPrivate *priv;
1966 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1968 priv = spin_button->priv;
1971 *step = priv->adjustment->step_increment;
1973 *page = priv->adjustment->page_increment;
1977 * gtk_spin_button_set_range:
1978 * @spin_button: a #GtkSpinButton
1979 * @min: minimum allowable value
1980 * @max: maximum allowable value
1982 * Sets the minimum and maximum allowable values for @spin_button
1985 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1989 GtkSpinButtonPrivate *priv;
1992 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1994 priv = spin_button->priv;
1996 priv->adjustment->lower = min;
1997 priv->adjustment->upper = max;
1999 value = CLAMP (priv->adjustment->value,
2000 priv->adjustment->lower,
2001 (priv->adjustment->upper - priv->adjustment->page_size));
2003 if (value != priv->adjustment->value)
2004 gtk_spin_button_set_value (spin_button, value);
2006 gtk_adjustment_changed (priv->adjustment);
2010 * gtk_spin_button_get_range:
2011 * @spin_button: a #GtkSpinButton
2012 * @min: (allow-none): location to store minimum allowed value, or %NULL
2013 * @max: (allow-none): location to store maximum allowed value, or %NULL
2015 * Gets the range allowed for @spin_button. See
2016 * gtk_spin_button_set_range().
2019 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2023 GtkSpinButtonPrivate *priv;
2025 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2027 priv = spin_button->priv;
2030 *min = priv->adjustment->lower;
2032 *max = priv->adjustment->upper;
2036 * gtk_spin_button_get_value:
2037 * @spin_button: a #GtkSpinButton
2039 * Get the value in the @spin_button.
2041 * Return value: the value of @spin_button
2044 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2046 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2048 return spin_button->priv->adjustment->value;
2052 * gtk_spin_button_get_value_as_int:
2053 * @spin_button: a #GtkSpinButton
2055 * Get the value @spin_button represented as an integer.
2057 * Return value: the value of @spin_button
2060 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2062 GtkSpinButtonPrivate *priv;
2065 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2067 priv = spin_button->priv;
2069 val = priv->adjustment->value;
2070 if (val - floor (val) < ceil (val) - val)
2077 * gtk_spin_button_set_value:
2078 * @spin_button: a #GtkSpinButton
2079 * @value: the new value
2081 * Set the value of @spin_button.
2084 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2087 GtkSpinButtonPrivate *priv;
2089 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2091 priv = spin_button->priv;
2093 if (fabs (value - priv->adjustment->value) > EPSILON)
2094 gtk_adjustment_set_value (priv->adjustment, value);
2097 gint return_val = FALSE;
2098 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2099 if (return_val == FALSE)
2100 gtk_spin_button_default_output (spin_button);
2105 * gtk_spin_button_set_update_policy:
2106 * @spin_button: a #GtkSpinButton
2107 * @policy: a #GtkSpinButtonUpdatePolicy value
2109 * Sets the update behavior of a spin button. This determines whether the
2110 * spin button is always updated or only when a valid value is set.
2113 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2114 GtkSpinButtonUpdatePolicy policy)
2116 GtkSpinButtonPrivate *priv;
2118 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2120 priv = spin_button->priv;
2122 if (priv->update_policy != policy)
2124 priv->update_policy = policy;
2125 g_object_notify (G_OBJECT (spin_button), "update-policy");
2130 * gtk_spin_button_get_update_policy:
2131 * @spin_button: a #GtkSpinButton
2133 * Gets the update behavior of a spin button. See
2134 * gtk_spin_button_set_update_policy().
2136 * Return value: the current update policy
2138 GtkSpinButtonUpdatePolicy
2139 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2141 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2143 return spin_button->priv->update_policy;
2147 * gtk_spin_button_set_numeric:
2148 * @spin_button: a #GtkSpinButton
2149 * @numeric: flag indicating if only numeric entry is allowed.
2151 * Sets the flag that determines if non-numeric text can be typed into
2155 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2158 GtkSpinButtonPrivate *priv;
2160 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2162 priv = spin_button->priv;
2164 numeric = numeric != FALSE;
2166 if (priv->numeric != numeric)
2168 priv->numeric = numeric;
2169 g_object_notify (G_OBJECT (spin_button), "numeric");
2174 * gtk_spin_button_get_numeric:
2175 * @spin_button: a #GtkSpinButton
2177 * Returns whether non-numeric text can be typed into the spin button.
2178 * See gtk_spin_button_set_numeric().
2180 * Return value: %TRUE if only numeric text can be entered
2183 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2185 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2187 return spin_button->priv->numeric;
2191 * gtk_spin_button_set_wrap:
2192 * @spin_button: a #GtkSpinButton
2193 * @wrap: a flag indicating if wrapping behavior is performed.
2195 * Sets the flag that determines if a spin button value wraps around to the
2196 * opposite limit when the upper or lower limit of the range is exceeded.
2199 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2202 GtkSpinButtonPrivate *priv;
2204 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2206 priv = spin_button->priv;
2208 wrap = wrap != FALSE;
2210 if (priv->wrap != wrap)
2212 priv->wrap = (wrap != 0);
2214 g_object_notify (G_OBJECT (spin_button), "wrap");
2219 * gtk_spin_button_get_wrap:
2220 * @spin_button: a #GtkSpinButton
2222 * Returns whether the spin button's value wraps around to the
2223 * opposite limit when the upper or lower limit of the range is
2224 * exceeded. See gtk_spin_button_set_wrap().
2226 * Return value: %TRUE if the spin button wraps around
2229 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2231 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2233 return spin_button->priv->wrap;
2237 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2243 style = gtk_widget_get_style (GTK_WIDGET (spin_button));
2244 size = pango_font_description_get_size (style->font_desc);
2245 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2247 return arrow_size - arrow_size % 2; /* force even */
2251 * spin_button_get_shadow_type:
2252 * @spin_button: a #GtkSpinButton
2254 * Convenience function to Get the shadow type from the underlying widget's
2257 * Return value: the #GtkShadowType
2260 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2262 GtkShadowType rc_shadow_type;
2264 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow-type", &rc_shadow_type, NULL);
2266 return rc_shadow_type;
2270 * gtk_spin_button_set_snap_to_ticks:
2271 * @spin_button: a #GtkSpinButton
2272 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2274 * Sets the policy as to whether values are corrected to the nearest step
2275 * increment when a spin button is activated after providing an invalid value.
2278 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2279 gboolean snap_to_ticks)
2281 GtkSpinButtonPrivate *priv;
2284 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2286 priv = spin_button->priv;
2288 new_val = (snap_to_ticks != 0);
2290 if (new_val != priv->snap_to_ticks)
2292 priv->snap_to_ticks = new_val;
2293 if (new_val && GTK_ENTRY (spin_button)->editable)
2294 gtk_spin_button_update (spin_button);
2296 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2301 * gtk_spin_button_get_snap_to_ticks:
2302 * @spin_button: a #GtkSpinButton
2304 * Returns whether the values are corrected to the nearest step. See
2305 * gtk_spin_button_set_snap_to_ticks().
2307 * Return value: %TRUE if values are snapped to the nearest step.
2310 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2312 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2314 return spin_button->priv->snap_to_ticks;
2318 * gtk_spin_button_spin:
2319 * @spin_button: a #GtkSpinButton
2320 * @direction: a #GtkSpinType indicating the direction to spin.
2321 * @increment: step increment to apply in the specified direction.
2323 * Increment or decrement a spin button's value in a specified direction
2324 * by a specified amount.
2327 gtk_spin_button_spin (GtkSpinButton *spin_button,
2328 GtkSpinType direction,
2331 GtkSpinButtonPrivate *priv;
2335 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2337 priv = spin_button->priv;
2339 adj = priv->adjustment;
2341 /* for compatibility with the 1.0.x version of this function */
2342 if (increment != 0 && increment != adj->step_increment &&
2343 (direction == GTK_SPIN_STEP_FORWARD ||
2344 direction == GTK_SPIN_STEP_BACKWARD))
2346 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2347 increment = -increment;
2348 direction = GTK_SPIN_USER_DEFINED;
2353 case GTK_SPIN_STEP_FORWARD:
2355 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2358 case GTK_SPIN_STEP_BACKWARD:
2360 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2363 case GTK_SPIN_PAGE_FORWARD:
2365 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2368 case GTK_SPIN_PAGE_BACKWARD:
2370 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2375 diff = adj->value - adj->lower;
2377 gtk_spin_button_real_spin (spin_button, -diff);
2382 diff = adj->upper - adj->value;
2384 gtk_spin_button_real_spin (spin_button, diff);
2387 case GTK_SPIN_USER_DEFINED:
2390 gtk_spin_button_real_spin (spin_button, increment);
2399 * gtk_spin_button_update:
2400 * @spin_button: a #GtkSpinButton
2402 * Manually force an update of the spin button.
2405 gtk_spin_button_update (GtkSpinButton *spin_button)
2407 GtkSpinButtonPrivate *priv;
2412 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2414 priv = spin_button->priv;
2417 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2418 if (return_val == FALSE)
2420 return_val = gtk_spin_button_default_input (spin_button, &val);
2421 error = (return_val == GTK_INPUT_ERROR);
2423 else if (return_val == GTK_INPUT_ERROR)
2426 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2428 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2430 if (val < priv->adjustment->lower)
2431 val = priv->adjustment->lower;
2432 else if (val > priv->adjustment->upper)
2433 val = priv->adjustment->upper;
2435 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2437 val < priv->adjustment->lower ||
2438 val > priv->adjustment->upper))
2440 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2444 if (priv->snap_to_ticks)
2445 gtk_spin_button_snap (spin_button, val);
2447 gtk_spin_button_set_value (spin_button, val);
2451 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2453 return spin_button->priv->panel;