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_get_preferred_width (GtkWidget *widget,
118 static void gtk_spin_button_size_allocate (GtkWidget *widget,
119 GtkAllocation *allocation);
120 static gint gtk_spin_button_draw (GtkWidget *widget,
122 static gint gtk_spin_button_button_press (GtkWidget *widget,
123 GdkEventButton *event);
124 static gint gtk_spin_button_button_release (GtkWidget *widget,
125 GdkEventButton *event);
126 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
127 GdkEventMotion *event);
128 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
129 GdkEventCrossing *event);
130 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
131 GdkEventCrossing *event);
132 static gint gtk_spin_button_focus_out (GtkWidget *widget,
133 GdkEventFocus *event);
134 static void gtk_spin_button_grab_notify (GtkWidget *widget,
135 gboolean was_grabbed);
136 static void gtk_spin_button_state_changed (GtkWidget *widget,
137 GtkStateType previous_state);
138 static void gtk_spin_button_style_set (GtkWidget *widget,
139 GtkStyle *previous_style);
140 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
142 GtkArrowType arrow_type);
143 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
144 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
145 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
146 GtkSpinButton *spin_button);
147 static gint gtk_spin_button_key_release (GtkWidget *widget,
149 static gint gtk_spin_button_scroll (GtkWidget *widget,
150 GdkEventScroll *event);
151 static void gtk_spin_button_activate (GtkEntry *entry);
152 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
157 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
159 static void gtk_spin_button_insert_text (GtkEditable *editable,
160 const gchar *new_text,
161 gint new_text_length,
163 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
165 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
166 GtkScrollType scroll);
168 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
170 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
172 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
173 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
175 static guint spinbutton_signals[LAST_SIGNAL] = {0};
179 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
180 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
181 gtk_spin_button_editable_init))
183 #define add_spin_binding(binding_set, keyval, mask, scroll) \
184 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
186 GTK_TYPE_SCROLL_TYPE, scroll)
189 gtk_spin_button_class_init (GtkSpinButtonClass *class)
191 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
192 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
193 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
194 GtkBindingSet *binding_set;
196 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 widget_class->destroy = gtk_spin_button_destroy;
201 widget_class->map = gtk_spin_button_map;
202 widget_class->unmap = gtk_spin_button_unmap;
203 widget_class->realize = gtk_spin_button_realize;
204 widget_class->unrealize = gtk_spin_button_unrealize;
205 widget_class->get_preferred_width = gtk_spin_button_get_preferred_width;
206 widget_class->size_allocate = gtk_spin_button_size_allocate;
207 widget_class->draw = gtk_spin_button_draw;
208 widget_class->scroll_event = gtk_spin_button_scroll;
209 widget_class->button_press_event = gtk_spin_button_button_press;
210 widget_class->button_release_event = gtk_spin_button_button_release;
211 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
212 widget_class->key_release_event = gtk_spin_button_key_release;
213 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
214 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
215 widget_class->focus_out_event = gtk_spin_button_focus_out;
216 widget_class->grab_notify = gtk_spin_button_grab_notify;
217 widget_class->state_changed = gtk_spin_button_state_changed;
218 widget_class->style_set = gtk_spin_button_style_set;
220 entry_class->activate = gtk_spin_button_activate;
221 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
224 class->output = NULL;
225 class->change_value = gtk_spin_button_real_change_value;
227 g_object_class_install_property (gobject_class,
229 g_param_spec_object ("adjustment",
231 P_("The adjustment that holds the value of the spin button"),
233 GTK_PARAM_READWRITE));
235 g_object_class_install_property (gobject_class,
237 g_param_spec_double ("climb-rate",
239 P_("The acceleration rate when you hold down a button"),
243 GTK_PARAM_READWRITE));
245 g_object_class_install_property (gobject_class,
247 g_param_spec_uint ("digits",
249 P_("The number of decimal places to display"),
253 GTK_PARAM_READWRITE));
255 g_object_class_install_property (gobject_class,
257 g_param_spec_boolean ("snap-to-ticks",
259 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
261 GTK_PARAM_READWRITE));
263 g_object_class_install_property (gobject_class,
265 g_param_spec_boolean ("numeric",
267 P_("Whether non-numeric characters should be ignored"),
269 GTK_PARAM_READWRITE));
271 g_object_class_install_property (gobject_class,
273 g_param_spec_boolean ("wrap",
275 P_("Whether a spin button should wrap upon reaching its limits"),
277 GTK_PARAM_READWRITE));
279 g_object_class_install_property (gobject_class,
281 g_param_spec_enum ("update-policy",
283 P_("Whether the spin button should update always, or only when the value is legal"),
284 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
286 GTK_PARAM_READWRITE));
288 g_object_class_install_property (gobject_class,
290 g_param_spec_double ("value",
292 P_("Reads the current value, or sets a new value"),
296 GTK_PARAM_READWRITE));
298 gtk_widget_class_install_style_property_parser (widget_class,
299 g_param_spec_enum ("shadow-type",
301 P_("Style of bevel around the spin button"),
302 GTK_TYPE_SHADOW_TYPE,
305 gtk_rc_property_parse_enum);
306 spinbutton_signals[INPUT] =
307 g_signal_new (I_("input"),
308 G_TYPE_FROM_CLASS (gobject_class),
310 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
312 _gtk_marshal_INT__POINTER,
317 * GtkSpinButton::output:
318 * @spin_button: the object which received the signal
320 * The ::output signal can be used to change to formatting
321 * of the value that is displayed in the spin buttons entry.
323 * /* show leading zeros */
325 * on_output (GtkSpinButton *spin,
328 * GtkAdjustment *adj;
332 * adj = gtk_spin_button_get_adjustment (spin);
333 * value = (int)gtk_adjustment_get_value (adj);
334 * text = g_strdup_printf ("%02d", value);
335 * gtk_entry_set_text (GTK_ENTRY (spin), text);
342 * Returns: %TRUE if the value has been displayed.
344 spinbutton_signals[OUTPUT] =
345 g_signal_new (I_("output"),
346 G_TYPE_FROM_CLASS (gobject_class),
348 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
349 _gtk_boolean_handled_accumulator, NULL,
350 _gtk_marshal_BOOLEAN__VOID,
353 spinbutton_signals[VALUE_CHANGED] =
354 g_signal_new (I_("value-changed"),
355 G_TYPE_FROM_CLASS (gobject_class),
357 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
359 _gtk_marshal_VOID__VOID,
363 * GtkSpinButton::wrapped:
364 * @spinbutton: the object which received the signal
366 * The wrapped signal is emitted right after the spinbutton wraps
367 * from its maximum to minimum value or vice-versa.
371 spinbutton_signals[WRAPPED] =
372 g_signal_new (I_("wrapped"),
373 G_TYPE_FROM_CLASS (gobject_class),
375 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
377 _gtk_marshal_VOID__VOID,
381 spinbutton_signals[CHANGE_VALUE] =
382 g_signal_new (I_("change-value"),
383 G_TYPE_FROM_CLASS (gobject_class),
384 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
385 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
387 _gtk_marshal_VOID__ENUM,
389 GTK_TYPE_SCROLL_TYPE);
391 binding_set = gtk_binding_set_by_class (class);
393 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
394 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
395 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
396 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
397 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
398 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
399 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
400 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
402 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
406 gtk_spin_button_editable_init (GtkEditableInterface *iface)
408 iface->insert_text = gtk_spin_button_insert_text;
412 gtk_spin_button_set_property (GObject *object,
417 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
418 GtkSpinButtonPrivate *priv = spin_button->priv;
422 GtkAdjustment *adjustment;
424 case PROP_ADJUSTMENT:
425 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
427 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
428 gtk_spin_button_set_adjustment (spin_button, adjustment);
430 case PROP_CLIMB_RATE:
431 gtk_spin_button_configure (spin_button,
433 g_value_get_double (value),
437 gtk_spin_button_configure (spin_button,
440 g_value_get_uint (value));
442 case PROP_SNAP_TO_TICKS:
443 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
446 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
449 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
451 case PROP_UPDATE_POLICY:
452 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
455 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
458 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
464 gtk_spin_button_get_property (GObject *object,
469 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
470 GtkSpinButtonPrivate *priv = spin_button->priv;
474 case PROP_ADJUSTMENT:
475 g_value_set_object (value, priv->adjustment);
477 case PROP_CLIMB_RATE:
478 g_value_set_double (value, priv->climb_rate);
481 g_value_set_uint (value, priv->digits);
483 case PROP_SNAP_TO_TICKS:
484 g_value_set_boolean (value, priv->snap_to_ticks);
487 g_value_set_boolean (value, priv->numeric);
490 g_value_set_boolean (value, priv->wrap);
492 case PROP_UPDATE_POLICY:
493 g_value_set_enum (value, priv->update_policy);
496 g_value_set_double (value, priv->adjustment->value);
499 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
505 gtk_spin_button_init (GtkSpinButton *spin_button)
507 GtkSpinButtonPrivate *priv;
509 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
510 GTK_TYPE_SPIN_BUTTON,
511 GtkSpinButtonPrivate);
512 priv = spin_button->priv;
514 priv->adjustment = NULL;
517 priv->climb_rate = 0.0;
518 priv->timer_step = 0.0;
519 priv->update_policy = GTK_UPDATE_ALWAYS;
520 priv->in_child = NO_ARROW;
521 priv->click_child = NO_ARROW;
523 priv->need_timer = FALSE;
524 priv->timer_calls = 0;
526 priv->numeric = FALSE;
528 priv->snap_to_ticks = FALSE;
530 gtk_spin_button_set_adjustment (spin_button,
531 gtk_adjustment_new (0, 0, 0, 0, 0, 0));
535 gtk_spin_button_finalize (GObject *object)
537 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
539 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
543 gtk_spin_button_destroy (GtkWidget *widget)
545 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
547 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->destroy (widget);
551 gtk_spin_button_map (GtkWidget *widget)
553 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
554 GtkSpinButtonPrivate *priv = spin_button->priv;
556 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
558 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
559 gdk_window_show (priv->panel);
564 gtk_spin_button_unmap (GtkWidget *widget)
566 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
567 GtkSpinButtonPrivate *priv = spin_button->priv;
569 if (gtk_widget_get_mapped (widget))
571 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
573 gdk_window_hide (priv->panel);
574 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
579 gtk_spin_button_realize (GtkWidget *widget)
581 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
582 GtkSpinButtonPrivate *priv = spin_button->priv;
583 GtkAllocation allocation;
584 GtkRequisition requisition;
586 GdkWindowAttr attributes;
587 gint attributes_mask;
591 style = gtk_widget_get_style (widget);
593 arrow_size = spin_button_get_arrow_size (spin_button);
595 gtk_widget_get_preferred_size (widget, &requisition, NULL);
596 gtk_widget_get_allocation (widget, &allocation);
598 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
599 GDK_KEY_RELEASE_MASK);
600 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
602 attributes.window_type = GDK_WINDOW_CHILD;
603 attributes.wclass = GDK_INPUT_ONLY;
604 attributes.visual = gtk_widget_get_visual (widget);
605 attributes.event_mask = gtk_widget_get_events (widget);
606 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
607 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
608 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
610 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
612 attributes.x = allocation.x + allocation.width - arrow_size - 2 * style->xthickness;
613 attributes.y = allocation.y + (allocation.height - requisition.height) / 2;
614 attributes.width = arrow_size + 2 * style->xthickness;
615 attributes.height = requisition.height;
617 priv->panel = gdk_window_new (gtk_widget_get_window (widget),
618 &attributes, attributes_mask);
619 gdk_window_set_user_data (priv->panel, widget);
622 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
623 if (return_val == FALSE)
624 gtk_spin_button_default_output (spin_button);
626 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
630 gtk_spin_button_unrealize (GtkWidget *widget)
632 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
633 GtkSpinButtonPrivate *priv = spin->priv;
635 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
639 gdk_window_set_user_data (priv->panel, NULL);
640 gdk_window_destroy (priv->panel);
646 compute_double_length (double val, int digits)
652 if (fabs (val) > 1.0)
653 a = floor (log10 (fabs (val))) + 1;
665 return a + digits + extra;
669 gtk_spin_button_get_preferred_width (GtkWidget *widget,
673 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
674 GtkSpinButtonPrivate *priv = spin_button->priv;
675 GtkEntry *entry = GTK_ENTRY (widget);
679 style = gtk_widget_get_style (widget);
681 arrow_size = spin_button_get_arrow_size (spin_button);
683 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->get_preferred_width (widget, minimum, natural);
685 if (gtk_entry_get_width_chars (entry) < 0)
687 PangoContext *context;
688 PangoFontMetrics *metrics;
694 gboolean interior_focus;
696 gint xborder, yborder;
697 GtkBorder inner_border;
699 gtk_widget_style_get (widget,
700 "interior-focus", &interior_focus,
701 "focus-line-width", &focus_width,
704 context = gtk_widget_get_pango_context (widget);
705 metrics = pango_context_get_metrics (context,
707 pango_context_get_language (context));
709 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
710 digit_width = PANGO_SCALE *
711 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
713 pango_font_metrics_unref (metrics);
715 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
716 width = MIN_SPIN_BUTTON_WIDTH;
717 max_string_len = MAX (10, compute_double_length (1e9 * priv->adjustment->step_increment,
720 string_len = compute_double_length (priv->adjustment->upper,
722 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
723 *minimum = MAX (*minimum, w);
724 *natural = MAX (*natural, w);
725 string_len = compute_double_length (priv->adjustment->lower, priv->digits);
726 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
727 *minimum = MAX (*minimum, w);
728 *natural = MAX (*natural, w);
730 _gtk_entry_get_borders (entry, &xborder, &yborder);
731 _gtk_entry_effective_inner_border (entry, &inner_border);
733 *minimum += xborder * 2 + inner_border.left + inner_border.right;
734 *natural += xborder * 2 + inner_border.left + inner_border.right;
737 *minimum += arrow_size + 2 * style->xthickness;
738 *natural += arrow_size + 2 * style->xthickness;
742 gtk_spin_button_size_allocate (GtkWidget *widget,
743 GtkAllocation *allocation)
745 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
746 GtkSpinButtonPrivate *priv = spin->priv;
747 GtkAllocation panel_allocation;
748 GtkRequisition requisition;
752 arrow_size = spin_button_get_arrow_size (spin);
753 panel_width = arrow_size + 2 * gtk_widget_get_style (widget)->xthickness;
755 gtk_widget_get_preferred_size (widget, &requisition, NULL);
757 gtk_widget_set_allocation (widget, allocation);
759 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
760 panel_allocation.x = allocation->x;
762 panel_allocation.x = allocation->x + allocation->width - panel_width;
764 panel_allocation.width = panel_width;
765 panel_allocation.height = MIN (requisition.height, allocation->height);
767 panel_allocation.y = allocation->y +
768 (allocation->height - requisition.height) / 2;
770 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
772 if (gtk_widget_get_realized (widget))
774 gdk_window_move_resize (priv->panel,
777 panel_allocation.width,
778 panel_allocation.height);
781 gtk_widget_queue_draw (GTK_WIDGET (spin));
785 gtk_spin_button_draw (GtkWidget *widget,
788 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
789 GtkSpinButtonPrivate *priv = spin->priv;
790 GtkShadowType shadow_type;
793 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
797 shadow_type = spin_button_get_shadow_type (spin);
799 state = gtk_widget_has_focus (widget) ?
800 GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
802 gtk_cairo_transform_to_window (cr, widget, priv->panel);
804 gtk_paint_box (gtk_widget_get_style (widget), cr,
806 widget, "spinbutton",
808 gdk_window_get_width (priv->panel),
809 gdk_window_get_height (priv->panel));
811 gtk_spin_button_draw_arrow (spin, cr, GTK_ARROW_UP);
812 gtk_spin_button_draw_arrow (spin, cr, GTK_ARROW_DOWN);
820 spin_button_at_limit (GtkSpinButton *spin_button,
823 GtkSpinButtonPrivate *priv = spin_button->priv;
824 GtkArrowType effective_arrow;
829 if (priv->adjustment->step_increment > 0)
830 effective_arrow = arrow;
832 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
834 if (effective_arrow == GTK_ARROW_UP &&
835 (priv->adjustment->upper - priv->adjustment->value <= EPSILON))
838 if (effective_arrow == GTK_ARROW_DOWN &&
839 (priv->adjustment->value - priv->adjustment->lower <= EPSILON))
846 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
848 GtkArrowType arrow_type)
850 GtkSpinButtonPrivate *priv;
851 GtkStateType state_type;
852 GtkShadowType shadow_type;
862 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
864 priv = spin_button->priv;
865 widget = GTK_WIDGET (spin_button);
867 style = gtk_widget_get_style (widget);
869 width = spin_button_get_arrow_size (spin_button) + 2 * style->xthickness;
870 panel_height = gdk_window_get_height (priv->panel);
872 if (arrow_type == GTK_ARROW_UP)
877 height = panel_height / 2;
882 y = panel_height / 2;
884 height = (panel_height + 1) / 2;
887 if (spin_button_at_limit (spin_button, arrow_type))
889 shadow_type = GTK_SHADOW_OUT;
890 state_type = GTK_STATE_INSENSITIVE;
894 if (priv->click_child == arrow_type)
896 state_type = GTK_STATE_ACTIVE;
897 shadow_type = GTK_SHADOW_IN;
901 if (priv->in_child == arrow_type &&
902 priv->click_child == NO_ARROW)
904 state_type = GTK_STATE_PRELIGHT;
908 state_type = gtk_widget_get_state (widget);
911 shadow_type = GTK_SHADOW_OUT;
915 gtk_paint_box (style, cr,
916 state_type, shadow_type,
918 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
919 x, y, width, height);
921 height = panel_height;
923 if (arrow_type == GTK_ARROW_DOWN)
926 height = height - y - 2;
931 height = height / 2 - 2;
936 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
942 w -= w % 2 - 1; /* force odd */
945 x += (width - w) / 2;
946 y += (height - h) / 2;
951 gtk_paint_arrow (style, cr,
952 state_type, shadow_type,
953 widget, "spinbutton",
955 x, y, width, height);
959 gtk_spin_button_enter_notify (GtkWidget *widget,
960 GdkEventCrossing *event)
962 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
963 GtkSpinButtonPrivate *priv = spin->priv;
964 GtkRequisition requisition;
966 if (event->window == priv->panel)
972 device = gdk_event_get_device ((GdkEvent *) event);
973 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
975 gtk_widget_get_preferred_size (widget, &requisition, NULL);
977 if (y <= requisition.height / 2)
978 priv->in_child = GTK_ARROW_UP;
980 priv->in_child = GTK_ARROW_DOWN;
982 gtk_widget_queue_draw (GTK_WIDGET (spin));
985 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event)
986 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
992 gtk_spin_button_leave_notify (GtkWidget *widget,
993 GdkEventCrossing *event)
995 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
996 GtkSpinButtonPrivate *priv = spin->priv;
998 priv->in_child = NO_ARROW;
999 gtk_widget_queue_draw (GTK_WIDGET (spin));
1001 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event)
1002 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1008 gtk_spin_button_focus_out (GtkWidget *widget,
1009 GdkEventFocus *event)
1011 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1012 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1014 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1018 gtk_spin_button_grab_notify (GtkWidget *widget,
1019 gboolean was_grabbed)
1021 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1025 gtk_spin_button_stop_spinning (spin);
1026 gtk_widget_queue_draw (GTK_WIDGET (spin));
1031 gtk_spin_button_state_changed (GtkWidget *widget,
1032 GtkStateType previous_state)
1034 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1036 if (!gtk_widget_is_sensitive (widget))
1038 gtk_spin_button_stop_spinning (spin);
1039 gtk_widget_queue_draw (GTK_WIDGET (spin));
1044 gtk_spin_button_style_set (GtkWidget *widget,
1045 GtkStyle *previous_style)
1047 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1048 GtkSpinButtonPrivate *priv = spin->priv;
1050 if (previous_style && gtk_widget_get_realized (widget))
1051 gtk_style_set_background (gtk_widget_get_style (widget),
1052 priv->panel, GTK_STATE_NORMAL);
1054 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_set (widget, previous_style);
1059 gtk_spin_button_scroll (GtkWidget *widget,
1060 GdkEventScroll *event)
1062 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1063 GtkSpinButtonPrivate *priv = spin->priv;
1065 if (event->direction == GDK_SCROLL_UP)
1067 if (!gtk_widget_has_focus (widget))
1068 gtk_widget_grab_focus (widget);
1069 gtk_spin_button_real_spin (spin, priv->adjustment->step_increment);
1071 else if (event->direction == GDK_SCROLL_DOWN)
1073 if (!gtk_widget_has_focus (widget))
1074 gtk_widget_grab_focus (widget);
1075 gtk_spin_button_real_spin (spin, -priv->adjustment->step_increment);
1084 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1086 GtkSpinButtonPrivate *priv = spin->priv;
1090 g_source_remove (priv->timer);
1092 priv->timer_calls = 0;
1093 priv->need_timer = FALSE;
1098 priv->timer_step = priv->adjustment->step_increment;
1099 priv->timer_calls = 0;
1101 priv->click_child = NO_ARROW;
1106 start_spinning (GtkSpinButton *spin,
1107 GtkArrowType click_child,
1110 GtkSpinButtonPrivate *priv;
1112 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1116 priv->click_child = click_child;
1120 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1123 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1125 priv->timer_step = step;
1126 priv->need_timer = TRUE;
1127 priv->timer = gdk_threads_add_timeout (timeout,
1128 (GSourceFunc) gtk_spin_button_timer,
1131 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1133 gtk_widget_queue_draw (GTK_WIDGET (spin));
1137 gtk_spin_button_button_press (GtkWidget *widget,
1138 GdkEventButton *event)
1140 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1141 GtkSpinButtonPrivate *priv = spin->priv;
1145 if (event->window == priv->panel)
1147 GtkRequisition requisition;
1149 if (!gtk_widget_has_focus (widget))
1150 gtk_widget_grab_focus (widget);
1151 priv->button = event->button;
1153 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1154 gtk_spin_button_update (spin);
1156 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1158 if (event->y <= requisition.height / 2)
1160 if (event->button == 1)
1161 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->step_increment);
1162 else if (event->button == 2)
1163 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->page_increment);
1165 priv->click_child = GTK_ARROW_UP;
1169 if (event->button == 1)
1170 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->step_increment);
1171 else if (event->button == 2)
1172 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->page_increment);
1174 priv->click_child = GTK_ARROW_DOWN;
1179 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1185 gtk_spin_button_button_release (GtkWidget *widget,
1186 GdkEventButton *event)
1188 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1189 GtkSpinButtonPrivate *priv = spin->priv;
1192 arrow_size = spin_button_get_arrow_size (spin);
1194 if (event->button == priv->button)
1196 int click_child = priv->click_child;
1198 gtk_spin_button_stop_spinning (spin);
1200 if (event->button == 3)
1202 GtkRequisition requisition;
1204 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1206 if (event->y >= 0 && event->x >= 0 &&
1207 event->y <= requisition.height &&
1208 event->x <= arrow_size + 2 * gtk_widget_get_style (widget)->xthickness)
1210 if (click_child == GTK_ARROW_UP &&
1211 event->y <= requisition.height / 2)
1215 diff = priv->adjustment->upper - priv->adjustment->value;
1217 gtk_spin_button_real_spin (spin, diff);
1219 else if (click_child == GTK_ARROW_DOWN &&
1220 event->y > requisition.height / 2)
1224 diff = priv->adjustment->value - priv->adjustment->lower;
1226 gtk_spin_button_real_spin (spin, -diff);
1230 gtk_widget_queue_draw (GTK_WIDGET (spin));
1235 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1239 gtk_spin_button_motion_notify (GtkWidget *widget,
1240 GdkEventMotion *event)
1242 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1243 GtkSpinButtonPrivate *priv = spin->priv;
1248 if (event->window == priv->panel)
1250 GtkRequisition requisition;
1253 gdk_event_request_motions (event);
1255 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1257 if (y <= requisition.height / 2 &&
1258 priv->in_child == GTK_ARROW_DOWN)
1260 priv->in_child = GTK_ARROW_UP;
1261 gtk_widget_queue_draw (GTK_WIDGET (spin));
1263 else if (y > requisition.height / 2 &&
1264 priv->in_child == GTK_ARROW_UP)
1266 priv->in_child = GTK_ARROW_DOWN;
1267 gtk_widget_queue_draw (GTK_WIDGET (spin));
1273 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1277 gtk_spin_button_timer (GtkSpinButton *spin_button)
1279 GtkSpinButtonPrivate *priv = spin_button->priv;
1280 gboolean retval = FALSE;
1284 if (priv->click_child == GTK_ARROW_UP)
1285 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1287 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1289 if (priv->need_timer)
1291 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1294 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1296 priv->need_timer = FALSE;
1297 priv->timer = gdk_threads_add_timeout (timeout,
1298 (GSourceFunc) gtk_spin_button_timer,
1299 (gpointer) spin_button);
1303 if (priv->climb_rate > 0.0 && priv->timer_step
1304 < priv->adjustment->page_increment)
1306 if (priv->timer_calls < MAX_TIMER_CALLS)
1307 priv->timer_calls++;
1310 priv->timer_calls = 0;
1311 priv->timer_step += priv->climb_rate;
1322 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1323 GtkSpinButton *spin_button)
1325 gboolean return_val;
1327 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1330 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1331 if (return_val == FALSE)
1332 gtk_spin_button_default_output (spin_button);
1334 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1336 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1338 g_object_notify (G_OBJECT (spin_button), "value");
1342 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1343 GtkScrollType scroll)
1345 GtkSpinButtonPrivate *priv = spin->priv;
1348 /* When the key binding is activated, there may be an outstanding
1349 * value, so we first have to commit what is currently written in
1350 * the spin buttons text entry. See #106574
1352 gtk_spin_button_update (spin);
1354 old_value = priv->adjustment->value;
1356 /* We don't test whether the entry is editable, since
1357 * this key binding conceptually corresponds to changing
1358 * the value with the buttons using the mouse, which
1359 * we allow for non-editable spin buttons.
1363 case GTK_SCROLL_STEP_BACKWARD:
1364 case GTK_SCROLL_STEP_DOWN:
1365 case GTK_SCROLL_STEP_LEFT:
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_STEP_FORWARD:
1382 case GTK_SCROLL_STEP_UP:
1383 case GTK_SCROLL_STEP_RIGHT:
1384 gtk_spin_button_real_spin (spin, priv->timer_step);
1386 if (priv->climb_rate > 0.0 && priv->timer_step
1387 < priv->adjustment->page_increment)
1389 if (priv->timer_calls < MAX_TIMER_CALLS)
1390 priv->timer_calls++;
1393 priv->timer_calls = 0;
1394 priv->timer_step += priv->climb_rate;
1399 case GTK_SCROLL_PAGE_BACKWARD:
1400 case GTK_SCROLL_PAGE_DOWN:
1401 case GTK_SCROLL_PAGE_LEFT:
1402 gtk_spin_button_real_spin (spin, -priv->adjustment->page_increment);
1405 case GTK_SCROLL_PAGE_FORWARD:
1406 case GTK_SCROLL_PAGE_UP:
1407 case GTK_SCROLL_PAGE_RIGHT:
1408 gtk_spin_button_real_spin (spin, priv->adjustment->page_increment);
1411 case GTK_SCROLL_START:
1413 gdouble diff = priv->adjustment->value - priv->adjustment->lower;
1415 gtk_spin_button_real_spin (spin, -diff);
1419 case GTK_SCROLL_END:
1421 gdouble diff = priv->adjustment->upper - priv->adjustment->value;
1423 gtk_spin_button_real_spin (spin, diff);
1428 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1432 gtk_spin_button_update (spin);
1434 if (priv->adjustment->value == old_value)
1435 gtk_widget_error_bell (GTK_WIDGET (spin));
1439 gtk_spin_button_key_release (GtkWidget *widget,
1442 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1443 GtkSpinButtonPrivate *priv = spin->priv;
1445 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1446 priv->timer_step = priv->adjustment->step_increment;
1447 priv->timer_calls = 0;
1453 gtk_spin_button_snap (GtkSpinButton *spin_button,
1456 GtkSpinButtonPrivate *priv = spin_button->priv;
1460 inc = priv->adjustment->step_increment;
1464 tmp = (val - priv->adjustment->lower) / inc;
1465 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1466 val = priv->adjustment->lower + floor (tmp) * inc;
1468 val = priv->adjustment->lower + ceil (tmp) * inc;
1470 gtk_spin_button_set_value (spin_button, val);
1474 gtk_spin_button_activate (GtkEntry *entry)
1476 if (gtk_editable_get_editable (GTK_EDITABLE (entry)))
1477 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1479 /* Chain up so that entry->activates_default is honored */
1480 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1484 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1493 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1495 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1496 panel_width = arrow_size + 2 * gtk_widget_get_style (GTK_WIDGET (entry))->xthickness;
1499 *width -= panel_width;
1501 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1506 gtk_spin_button_insert_text (GtkEditable *editable,
1507 const gchar *new_text,
1508 gint new_text_length,
1511 GtkEntry *entry = GTK_ENTRY (editable);
1512 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1513 GtkSpinButtonPrivate *priv = spin->priv;
1514 GtkEditableInterface *parent_editable_iface;
1516 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1528 const gchar *entry_text;
1530 entry_length = gtk_entry_get_text_length (entry);
1531 entry_text = gtk_entry_get_text (entry);
1535 if (*(lc->negative_sign))
1536 neg_sign = *(lc->negative_sign);
1540 if (*(lc->positive_sign))
1541 pos_sign = *(lc->positive_sign);
1546 /* Workaround for bug caused by some Windows application messing
1547 * up the positive sign of the current locale, more specifically
1548 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1549 * See bug #330743 and for instance
1550 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1552 * I don't know if the positive sign always gets bogusly set to
1553 * a digit when the above Registry value is corrupted as
1554 * described. (In my test case, it got set to "8", and in the
1555 * bug report above it presumably was set ot "0".) Probably it
1556 * might get set to almost anything? So how to distinguish a
1557 * bogus value from some correct one for some locale? That is
1558 * probably hard, but at least we should filter out the
1561 if (pos_sign >= '0' && pos_sign <= '9')
1565 for (sign=0, i=0; i<entry_length; i++)
1566 if ((entry_text[i] == neg_sign) ||
1567 (entry_text[i] == pos_sign))
1573 if (sign && !(*position))
1576 for (dotpos=-1, i=0; i<entry_length; i++)
1577 if (entry_text[i] == *(lc->decimal_point))
1583 if (dotpos > -1 && *position > dotpos &&
1584 (gint)priv->digits - entry_length
1585 + dotpos - new_text_length + 1 < 0)
1588 for (i = 0; i < new_text_length; i++)
1590 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1592 if (sign || (*position) || i)
1596 else if (new_text[i] == *(lc->decimal_point))
1598 if (!priv->digits || dotpos > -1 ||
1599 (new_text_length - 1 - i + entry_length
1600 - *position > (gint)priv->digits))
1602 dotpos = *position + i;
1604 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1609 parent_editable_iface->insert_text (editable, new_text,
1610 new_text_length, position);
1614 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1617 GtkSpinButtonPrivate *priv = spin_button->priv;
1619 gdouble new_value = 0.0;
1620 gboolean wrapped = FALSE;
1622 adj = priv->adjustment;
1624 new_value = adj->value + increment;
1630 if (fabs (adj->value - adj->upper) < EPSILON)
1632 new_value = adj->lower;
1635 else if (new_value > adj->upper)
1636 new_value = adj->upper;
1639 new_value = MIN (new_value, adj->upper);
1641 else if (increment < 0)
1645 if (fabs (adj->value - adj->lower) < EPSILON)
1647 new_value = adj->upper;
1650 else if (new_value < adj->lower)
1651 new_value = adj->lower;
1654 new_value = MAX (new_value, adj->lower);
1657 if (fabs (new_value - adj->value) > EPSILON)
1658 gtk_adjustment_set_value (adj, new_value);
1661 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1663 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1667 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1672 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1674 return GTK_INPUT_ERROR;
1680 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1682 GtkSpinButtonPrivate *priv = spin_button->priv;
1684 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, priv->adjustment->value);
1686 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1687 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1693 /***********************************************************
1694 ***********************************************************
1695 *** Public interface ***
1696 ***********************************************************
1697 ***********************************************************/
1701 * gtk_spin_button_configure:
1702 * @spin_button: a #GtkSpinButton
1703 * @adjustment: (allow-none): a #GtkAdjustment.
1704 * @climb_rate: the new climb rate.
1705 * @digits: the number of decimal places to display in the spin button.
1707 * Changes the properties of an existing spin button. The adjustment, climb rate,
1708 * and number of decimal places are all changed accordingly, after this function call.
1711 gtk_spin_button_configure (GtkSpinButton *spin_button,
1712 GtkAdjustment *adjustment,
1716 GtkSpinButtonPrivate *priv;
1718 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1720 priv = spin_button->priv;
1723 gtk_spin_button_set_adjustment (spin_button, adjustment);
1725 adjustment = priv->adjustment;
1727 g_object_freeze_notify (G_OBJECT (spin_button));
1728 if (priv->digits != digits)
1730 priv->digits = digits;
1731 g_object_notify (G_OBJECT (spin_button), "digits");
1734 if (priv->climb_rate != climb_rate)
1736 priv->climb_rate = climb_rate;
1737 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1739 g_object_thaw_notify (G_OBJECT (spin_button));
1741 gtk_adjustment_value_changed (adjustment);
1745 gtk_spin_button_new (GtkAdjustment *adjustment,
1749 GtkSpinButton *spin;
1752 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1754 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1756 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1758 return GTK_WIDGET (spin);
1762 * gtk_spin_button_new_with_range:
1763 * @min: Minimum allowable value
1764 * @max: Maximum allowable value
1765 * @step: Increment added or subtracted by spinning the widget
1767 * This is a convenience constructor that allows creation of a numeric
1768 * #GtkSpinButton without manually creating an adjustment. The value is
1769 * initially set to the minimum value and a page increment of 10 * @step
1770 * is the default. The precision of the spin button is equivalent to the
1771 * precision of @step.
1773 * Note that the way in which the precision is derived works best if @step
1774 * is a power of ten. If the resulting precision is not suitable for your
1775 * needs, use gtk_spin_button_set_digits() to correct it.
1777 * Return value: The new spin button as a #GtkWidget.
1780 gtk_spin_button_new_with_range (gdouble min,
1785 GtkSpinButton *spin;
1788 g_return_val_if_fail (min <= max, NULL);
1789 g_return_val_if_fail (step != 0.0, NULL);
1791 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1793 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1795 if (fabs (step) >= 1.0 || step == 0.0)
1798 digits = abs ((gint) floor (log10 (fabs (step))));
1799 if (digits > MAX_DIGITS)
1800 digits = MAX_DIGITS;
1803 gtk_spin_button_configure (spin, adj, step, digits);
1805 gtk_spin_button_set_numeric (spin, TRUE);
1807 return GTK_WIDGET (spin);
1810 /* Callback used when the spin button's adjustment changes. We need to redraw
1811 * the arrows when the adjustment's range changes, and reevaluate our size request.
1814 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1816 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1817 GtkSpinButtonPrivate *priv = spin_button->priv;
1819 priv->timer_step = priv->adjustment->step_increment;
1820 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1824 * gtk_spin_button_set_adjustment:
1825 * @spin_button: a #GtkSpinButton
1826 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1828 * Replaces the #GtkAdjustment associated with @spin_button.
1831 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1832 GtkAdjustment *adjustment)
1834 GtkSpinButtonPrivate *priv;
1836 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1838 priv = spin_button->priv;
1840 if (priv->adjustment != adjustment)
1842 if (priv->adjustment)
1844 g_signal_handlers_disconnect_by_func (priv->adjustment,
1845 gtk_spin_button_value_changed,
1847 g_signal_handlers_disconnect_by_func (priv->adjustment,
1848 adjustment_changed_cb,
1850 g_object_unref (priv->adjustment);
1852 priv->adjustment = adjustment;
1855 g_object_ref_sink (adjustment);
1856 g_signal_connect (adjustment, "value-changed",
1857 G_CALLBACK (gtk_spin_button_value_changed),
1859 g_signal_connect (adjustment, "changed",
1860 G_CALLBACK (adjustment_changed_cb),
1862 priv->timer_step = priv->adjustment->step_increment;
1865 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1868 g_object_notify (G_OBJECT (spin_button), "adjustment");
1872 * gtk_spin_button_get_adjustment:
1873 * @spin_button: a #GtkSpinButton
1875 * Get the adjustment associated with a #GtkSpinButton
1877 * Return value: (transfer none): the #GtkAdjustment of @spin_button
1880 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1882 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1884 return spin_button->priv->adjustment;
1888 * gtk_spin_button_set_digits:
1889 * @spin_button: a #GtkSpinButton
1890 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1892 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1896 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1899 GtkSpinButtonPrivate *priv;
1901 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1903 priv = spin_button->priv;
1905 if (priv->digits != digits)
1907 priv->digits = digits;
1908 gtk_spin_button_value_changed (priv->adjustment, spin_button);
1909 g_object_notify (G_OBJECT (spin_button), "digits");
1911 /* since lower/upper may have changed */
1912 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1917 * gtk_spin_button_get_digits:
1918 * @spin_button: a #GtkSpinButton
1920 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1922 * Returns: the current precision
1925 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1927 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1929 return spin_button->priv->digits;
1933 * gtk_spin_button_set_increments:
1934 * @spin_button: a #GtkSpinButton
1935 * @step: increment applied for a button 1 press.
1936 * @page: increment applied for a button 2 press.
1938 * Sets the step and page increments for spin_button. This affects how
1939 * quickly the value changes when the spin button's arrows are activated.
1942 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1946 GtkSpinButtonPrivate *priv;
1948 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1950 priv = spin_button->priv;
1952 priv->adjustment->step_increment = step;
1953 priv->adjustment->page_increment = page;
1957 * gtk_spin_button_get_increments:
1958 * @spin_button: a #GtkSpinButton
1959 * @step: (allow-none): location to store step increment, or %NULL
1960 * @page: (allow-none): location to store page increment, or %NULL
1962 * Gets the current step and page the increments used by @spin_button. See
1963 * gtk_spin_button_set_increments().
1966 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1970 GtkSpinButtonPrivate *priv;
1972 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1974 priv = spin_button->priv;
1977 *step = priv->adjustment->step_increment;
1979 *page = priv->adjustment->page_increment;
1983 * gtk_spin_button_set_range:
1984 * @spin_button: a #GtkSpinButton
1985 * @min: minimum allowable value
1986 * @max: maximum allowable value
1988 * Sets the minimum and maximum allowable values for @spin_button
1991 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1995 GtkSpinButtonPrivate *priv;
1998 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2000 priv = spin_button->priv;
2002 priv->adjustment->lower = min;
2003 priv->adjustment->upper = max;
2005 value = CLAMP (priv->adjustment->value,
2006 priv->adjustment->lower,
2007 (priv->adjustment->upper - priv->adjustment->page_size));
2009 if (value != priv->adjustment->value)
2010 gtk_spin_button_set_value (spin_button, value);
2012 gtk_adjustment_changed (priv->adjustment);
2016 * gtk_spin_button_get_range:
2017 * @spin_button: a #GtkSpinButton
2018 * @min: (allow-none): location to store minimum allowed value, or %NULL
2019 * @max: (allow-none): location to store maximum allowed value, or %NULL
2021 * Gets the range allowed for @spin_button. See
2022 * gtk_spin_button_set_range().
2025 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2029 GtkSpinButtonPrivate *priv;
2031 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2033 priv = spin_button->priv;
2036 *min = priv->adjustment->lower;
2038 *max = priv->adjustment->upper;
2042 * gtk_spin_button_get_value:
2043 * @spin_button: a #GtkSpinButton
2045 * Get the value in the @spin_button.
2047 * Return value: the value of @spin_button
2050 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2052 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2054 return spin_button->priv->adjustment->value;
2058 * gtk_spin_button_get_value_as_int:
2059 * @spin_button: a #GtkSpinButton
2061 * Get the value @spin_button represented as an integer.
2063 * Return value: the value of @spin_button
2066 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2068 GtkSpinButtonPrivate *priv;
2071 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2073 priv = spin_button->priv;
2075 val = priv->adjustment->value;
2076 if (val - floor (val) < ceil (val) - val)
2083 * gtk_spin_button_set_value:
2084 * @spin_button: a #GtkSpinButton
2085 * @value: the new value
2087 * Set the value of @spin_button.
2090 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2093 GtkSpinButtonPrivate *priv;
2095 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2097 priv = spin_button->priv;
2099 if (fabs (value - priv->adjustment->value) > EPSILON)
2100 gtk_adjustment_set_value (priv->adjustment, value);
2103 gint return_val = FALSE;
2104 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2105 if (return_val == FALSE)
2106 gtk_spin_button_default_output (spin_button);
2111 * gtk_spin_button_set_update_policy:
2112 * @spin_button: a #GtkSpinButton
2113 * @policy: a #GtkSpinButtonUpdatePolicy value
2115 * Sets the update behavior of a spin button. This determines whether the
2116 * spin button is always updated or only when a valid value is set.
2119 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2120 GtkSpinButtonUpdatePolicy policy)
2122 GtkSpinButtonPrivate *priv;
2124 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2126 priv = spin_button->priv;
2128 if (priv->update_policy != policy)
2130 priv->update_policy = policy;
2131 g_object_notify (G_OBJECT (spin_button), "update-policy");
2136 * gtk_spin_button_get_update_policy:
2137 * @spin_button: a #GtkSpinButton
2139 * Gets the update behavior of a spin button. See
2140 * gtk_spin_button_set_update_policy().
2142 * Return value: the current update policy
2144 GtkSpinButtonUpdatePolicy
2145 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2147 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2149 return spin_button->priv->update_policy;
2153 * gtk_spin_button_set_numeric:
2154 * @spin_button: a #GtkSpinButton
2155 * @numeric: flag indicating if only numeric entry is allowed.
2157 * Sets the flag that determines if non-numeric text can be typed into
2161 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2164 GtkSpinButtonPrivate *priv;
2166 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2168 priv = spin_button->priv;
2170 numeric = numeric != FALSE;
2172 if (priv->numeric != numeric)
2174 priv->numeric = numeric;
2175 g_object_notify (G_OBJECT (spin_button), "numeric");
2180 * gtk_spin_button_get_numeric:
2181 * @spin_button: a #GtkSpinButton
2183 * Returns whether non-numeric text can be typed into the spin button.
2184 * See gtk_spin_button_set_numeric().
2186 * Return value: %TRUE if only numeric text can be entered
2189 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2191 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2193 return spin_button->priv->numeric;
2197 * gtk_spin_button_set_wrap:
2198 * @spin_button: a #GtkSpinButton
2199 * @wrap: a flag indicating if wrapping behavior is performed.
2201 * Sets the flag that determines if a spin button value wraps around to the
2202 * opposite limit when the upper or lower limit of the range is exceeded.
2205 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2208 GtkSpinButtonPrivate *priv;
2210 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2212 priv = spin_button->priv;
2214 wrap = wrap != FALSE;
2216 if (priv->wrap != wrap)
2218 priv->wrap = (wrap != 0);
2220 g_object_notify (G_OBJECT (spin_button), "wrap");
2225 * gtk_spin_button_get_wrap:
2226 * @spin_button: a #GtkSpinButton
2228 * Returns whether the spin button's value wraps around to the
2229 * opposite limit when the upper or lower limit of the range is
2230 * exceeded. See gtk_spin_button_set_wrap().
2232 * Return value: %TRUE if the spin button wraps around
2235 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2237 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2239 return spin_button->priv->wrap;
2243 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2249 style = gtk_widget_get_style (GTK_WIDGET (spin_button));
2250 size = pango_font_description_get_size (style->font_desc);
2251 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2253 return arrow_size - arrow_size % 2; /* force even */
2257 * spin_button_get_shadow_type:
2258 * @spin_button: a #GtkSpinButton
2260 * Convenience function to Get the shadow type from the underlying widget's
2263 * Return value: the #GtkShadowType
2266 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2268 GtkShadowType rc_shadow_type;
2270 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow-type", &rc_shadow_type, NULL);
2272 return rc_shadow_type;
2276 * gtk_spin_button_set_snap_to_ticks:
2277 * @spin_button: a #GtkSpinButton
2278 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2280 * Sets the policy as to whether values are corrected to the nearest step
2281 * increment when a spin button is activated after providing an invalid value.
2284 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2285 gboolean snap_to_ticks)
2287 GtkSpinButtonPrivate *priv;
2290 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2292 priv = spin_button->priv;
2294 new_val = (snap_to_ticks != 0);
2296 if (new_val != priv->snap_to_ticks)
2298 priv->snap_to_ticks = new_val;
2299 if (new_val && gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
2300 gtk_spin_button_update (spin_button);
2302 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2307 * gtk_spin_button_get_snap_to_ticks:
2308 * @spin_button: a #GtkSpinButton
2310 * Returns whether the values are corrected to the nearest step. See
2311 * gtk_spin_button_set_snap_to_ticks().
2313 * Return value: %TRUE if values are snapped to the nearest step.
2316 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2318 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2320 return spin_button->priv->snap_to_ticks;
2324 * gtk_spin_button_spin:
2325 * @spin_button: a #GtkSpinButton
2326 * @direction: a #GtkSpinType indicating the direction to spin.
2327 * @increment: step increment to apply in the specified direction.
2329 * Increment or decrement a spin button's value in a specified direction
2330 * by a specified amount.
2333 gtk_spin_button_spin (GtkSpinButton *spin_button,
2334 GtkSpinType direction,
2337 GtkSpinButtonPrivate *priv;
2341 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2343 priv = spin_button->priv;
2345 adj = priv->adjustment;
2347 /* for compatibility with the 1.0.x version of this function */
2348 if (increment != 0 && increment != adj->step_increment &&
2349 (direction == GTK_SPIN_STEP_FORWARD ||
2350 direction == GTK_SPIN_STEP_BACKWARD))
2352 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2353 increment = -increment;
2354 direction = GTK_SPIN_USER_DEFINED;
2359 case GTK_SPIN_STEP_FORWARD:
2361 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2364 case GTK_SPIN_STEP_BACKWARD:
2366 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2369 case GTK_SPIN_PAGE_FORWARD:
2371 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2374 case GTK_SPIN_PAGE_BACKWARD:
2376 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2381 diff = adj->value - adj->lower;
2383 gtk_spin_button_real_spin (spin_button, -diff);
2388 diff = adj->upper - adj->value;
2390 gtk_spin_button_real_spin (spin_button, diff);
2393 case GTK_SPIN_USER_DEFINED:
2396 gtk_spin_button_real_spin (spin_button, increment);
2405 * gtk_spin_button_update:
2406 * @spin_button: a #GtkSpinButton
2408 * Manually force an update of the spin button.
2411 gtk_spin_button_update (GtkSpinButton *spin_button)
2413 GtkSpinButtonPrivate *priv;
2418 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2420 priv = spin_button->priv;
2423 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2424 if (return_val == FALSE)
2426 return_val = gtk_spin_button_default_input (spin_button, &val);
2427 error = (return_val == GTK_INPUT_ERROR);
2429 else if (return_val == GTK_INPUT_ERROR)
2432 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2434 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2436 if (val < priv->adjustment->lower)
2437 val = priv->adjustment->lower;
2438 else if (val > priv->adjustment->upper)
2439 val = priv->adjustment->upper;
2441 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2443 val < priv->adjustment->lower ||
2444 val > priv->adjustment->upper))
2446 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2450 if (priv->snap_to_ticks)
2451 gtk_spin_button_snap (spin_button, val);
2453 gtk_spin_button_set_value (spin_button, val);
2457 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2459 return spin_button->priv->panel;