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 "gtksizerequest.h"
44 #include "gtkprivate.h"
47 #define MIN_SPIN_BUTTON_WIDTH 30
48 #define MAX_TIMER_CALLS 5
51 #define MIN_ARROW_WIDTH 6
54 struct _GtkSpinButtonPrivate
56 GtkSpinButtonUpdatePolicy update_policy;
57 GtkAdjustment *adjustment;
67 guint click_child : 2; /* valid: GTK_ARROW_UP=0, GTK_ARROW_DOWN=1 or 2=NONE/BOTH */
72 guint snap_to_ticks : 1;
73 guint timer_calls : 3;
100 static void gtk_spin_button_editable_init (GtkEditableInterface *iface);
101 static void gtk_spin_button_finalize (GObject *object);
102 static void gtk_spin_button_destroy (GtkObject *object);
103 static void gtk_spin_button_set_property (GObject *object,
107 static void gtk_spin_button_get_property (GObject *object,
111 static void gtk_spin_button_map (GtkWidget *widget);
112 static void gtk_spin_button_unmap (GtkWidget *widget);
113 static void gtk_spin_button_realize (GtkWidget *widget);
114 static void gtk_spin_button_unrealize (GtkWidget *widget);
115 static void gtk_spin_button_size_request (GtkWidget *widget,
116 GtkRequisition *requisition);
117 static void gtk_spin_button_size_allocate (GtkWidget *widget,
118 GtkAllocation *allocation);
119 static gint gtk_spin_button_draw (GtkWidget *widget,
121 static gint gtk_spin_button_button_press (GtkWidget *widget,
122 GdkEventButton *event);
123 static gint gtk_spin_button_button_release (GtkWidget *widget,
124 GdkEventButton *event);
125 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
126 GdkEventMotion *event);
127 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
128 GdkEventCrossing *event);
129 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
130 GdkEventCrossing *event);
131 static gint gtk_spin_button_focus_out (GtkWidget *widget,
132 GdkEventFocus *event);
133 static void gtk_spin_button_grab_notify (GtkWidget *widget,
134 gboolean was_grabbed);
135 static void gtk_spin_button_state_changed (GtkWidget *widget,
136 GtkStateType previous_state);
137 static void gtk_spin_button_style_set (GtkWidget *widget,
138 GtkStyle *previous_style);
139 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
141 GtkArrowType arrow_type);
142 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
143 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
144 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
145 GtkSpinButton *spin_button);
146 static gint gtk_spin_button_key_release (GtkWidget *widget,
148 static gint gtk_spin_button_scroll (GtkWidget *widget,
149 GdkEventScroll *event);
150 static void gtk_spin_button_activate (GtkEntry *entry);
151 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
156 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
158 static void gtk_spin_button_insert_text (GtkEditable *editable,
159 const gchar *new_text,
160 gint new_text_length,
162 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
164 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
165 GtkScrollType scroll);
167 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
169 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
171 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
172 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
174 static guint spinbutton_signals[LAST_SIGNAL] = {0};
178 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
179 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
180 gtk_spin_button_editable_init))
182 #define add_spin_binding(binding_set, keyval, mask, scroll) \
183 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
185 GTK_TYPE_SCROLL_TYPE, scroll)
188 gtk_spin_button_class_init (GtkSpinButtonClass *class)
190 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
191 GtkObjectClass *object_class = GTK_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;
198 gobject_class->set_property = gtk_spin_button_set_property;
199 gobject_class->get_property = gtk_spin_button_get_property;
201 object_class->destroy = gtk_spin_button_destroy;
203 widget_class->map = gtk_spin_button_map;
204 widget_class->unmap = gtk_spin_button_unmap;
205 widget_class->realize = gtk_spin_button_realize;
206 widget_class->unrealize = gtk_spin_button_unrealize;
207 widget_class->size_request = gtk_spin_button_size_request;
208 widget_class->size_allocate = gtk_spin_button_size_allocate;
209 widget_class->draw = gtk_spin_button_draw;
210 widget_class->scroll_event = gtk_spin_button_scroll;
211 widget_class->button_press_event = gtk_spin_button_button_press;
212 widget_class->button_release_event = gtk_spin_button_button_release;
213 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
214 widget_class->key_release_event = gtk_spin_button_key_release;
215 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
216 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
217 widget_class->focus_out_event = gtk_spin_button_focus_out;
218 widget_class->grab_notify = gtk_spin_button_grab_notify;
219 widget_class->state_changed = gtk_spin_button_state_changed;
220 widget_class->style_set = gtk_spin_button_style_set;
222 entry_class->activate = gtk_spin_button_activate;
223 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
226 class->output = NULL;
227 class->change_value = gtk_spin_button_real_change_value;
229 g_object_class_install_property (gobject_class,
231 g_param_spec_object ("adjustment",
233 P_("The adjustment that holds the value of the spin button"),
235 GTK_PARAM_READWRITE));
237 g_object_class_install_property (gobject_class,
239 g_param_spec_double ("climb-rate",
241 P_("The acceleration rate when you hold down a button"),
245 GTK_PARAM_READWRITE));
247 g_object_class_install_property (gobject_class,
249 g_param_spec_uint ("digits",
251 P_("The number of decimal places to display"),
255 GTK_PARAM_READWRITE));
257 g_object_class_install_property (gobject_class,
259 g_param_spec_boolean ("snap-to-ticks",
261 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
263 GTK_PARAM_READWRITE));
265 g_object_class_install_property (gobject_class,
267 g_param_spec_boolean ("numeric",
269 P_("Whether non-numeric characters should be ignored"),
271 GTK_PARAM_READWRITE));
273 g_object_class_install_property (gobject_class,
275 g_param_spec_boolean ("wrap",
277 P_("Whether a spin button should wrap upon reaching its limits"),
279 GTK_PARAM_READWRITE));
281 g_object_class_install_property (gobject_class,
283 g_param_spec_enum ("update-policy",
285 P_("Whether the spin button should update always, or only when the value is legal"),
286 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
288 GTK_PARAM_READWRITE));
290 g_object_class_install_property (gobject_class,
292 g_param_spec_double ("value",
294 P_("Reads the current value, or sets a new value"),
298 GTK_PARAM_READWRITE));
300 gtk_widget_class_install_style_property_parser (widget_class,
301 g_param_spec_enum ("shadow-type",
303 P_("Style of bevel around the spin button"),
304 GTK_TYPE_SHADOW_TYPE,
307 gtk_rc_property_parse_enum);
308 spinbutton_signals[INPUT] =
309 g_signal_new (I_("input"),
310 G_TYPE_FROM_CLASS (gobject_class),
312 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
314 _gtk_marshal_INT__POINTER,
319 * GtkSpinButton::output:
320 * @spin_button: the object which received the signal
322 * The ::output signal can be used to change to formatting
323 * of the value that is displayed in the spin buttons entry.
325 * /* show leading zeros */
327 * on_output (GtkSpinButton *spin,
330 * GtkAdjustment *adj;
334 * adj = gtk_spin_button_get_adjustment (spin);
335 * value = (int)gtk_adjustment_get_value (adj);
336 * text = g_strdup_printf ("%02d", value);
337 * gtk_entry_set_text (GTK_ENTRY (spin), text);
344 * Returns: %TRUE if the value has been displayed.
346 spinbutton_signals[OUTPUT] =
347 g_signal_new (I_("output"),
348 G_TYPE_FROM_CLASS (gobject_class),
350 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
351 _gtk_boolean_handled_accumulator, NULL,
352 _gtk_marshal_BOOLEAN__VOID,
355 spinbutton_signals[VALUE_CHANGED] =
356 g_signal_new (I_("value-changed"),
357 G_TYPE_FROM_CLASS (gobject_class),
359 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
361 _gtk_marshal_VOID__VOID,
365 * GtkSpinButton::wrapped:
366 * @spinbutton: the object which received the signal
368 * The wrapped signal is emitted right after the spinbutton wraps
369 * from its maximum to minimum value or vice-versa.
373 spinbutton_signals[WRAPPED] =
374 g_signal_new (I_("wrapped"),
375 G_TYPE_FROM_CLASS (gobject_class),
377 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
379 _gtk_marshal_VOID__VOID,
383 spinbutton_signals[CHANGE_VALUE] =
384 g_signal_new (I_("change-value"),
385 G_TYPE_FROM_CLASS (gobject_class),
386 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
387 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
389 _gtk_marshal_VOID__ENUM,
391 GTK_TYPE_SCROLL_TYPE);
393 binding_set = gtk_binding_set_by_class (class);
395 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
396 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
397 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
398 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
399 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
400 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
401 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
402 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
404 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
408 gtk_spin_button_editable_init (GtkEditableInterface *iface)
410 iface->insert_text = gtk_spin_button_insert_text;
414 gtk_spin_button_set_property (GObject *object,
419 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
420 GtkSpinButtonPrivate *priv = spin_button->priv;
424 GtkAdjustment *adjustment;
426 case PROP_ADJUSTMENT:
427 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
429 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
430 gtk_spin_button_set_adjustment (spin_button, adjustment);
432 case PROP_CLIMB_RATE:
433 gtk_spin_button_configure (spin_button,
435 g_value_get_double (value),
439 gtk_spin_button_configure (spin_button,
442 g_value_get_uint (value));
444 case PROP_SNAP_TO_TICKS:
445 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
448 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
451 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
453 case PROP_UPDATE_POLICY:
454 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
457 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
460 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
466 gtk_spin_button_get_property (GObject *object,
471 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
472 GtkSpinButtonPrivate *priv = spin_button->priv;
476 case PROP_ADJUSTMENT:
477 g_value_set_object (value, priv->adjustment);
479 case PROP_CLIMB_RATE:
480 g_value_set_double (value, priv->climb_rate);
483 g_value_set_uint (value, priv->digits);
485 case PROP_SNAP_TO_TICKS:
486 g_value_set_boolean (value, priv->snap_to_ticks);
489 g_value_set_boolean (value, priv->numeric);
492 g_value_set_boolean (value, priv->wrap);
494 case PROP_UPDATE_POLICY:
495 g_value_set_enum (value, priv->update_policy);
498 g_value_set_double (value, priv->adjustment->value);
501 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
507 gtk_spin_button_init (GtkSpinButton *spin_button)
509 GtkSpinButtonPrivate *priv;
511 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
512 GTK_TYPE_SPIN_BUTTON,
513 GtkSpinButtonPrivate);
514 priv = spin_button->priv;
516 priv->adjustment = NULL;
519 priv->climb_rate = 0.0;
520 priv->timer_step = 0.0;
521 priv->update_policy = GTK_UPDATE_ALWAYS;
522 priv->in_child = NO_ARROW;
523 priv->click_child = NO_ARROW;
525 priv->need_timer = FALSE;
526 priv->timer_calls = 0;
528 priv->numeric = FALSE;
530 priv->snap_to_ticks = FALSE;
532 gtk_spin_button_set_adjustment (spin_button,
533 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
537 gtk_spin_button_finalize (GObject *object)
539 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
541 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
545 gtk_spin_button_destroy (GtkObject *object)
547 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
549 GTK_OBJECT_CLASS (gtk_spin_button_parent_class)->destroy (object);
553 gtk_spin_button_map (GtkWidget *widget)
555 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
556 GtkSpinButtonPrivate *priv = spin_button->priv;
558 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
560 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
561 gdk_window_show (priv->panel);
566 gtk_spin_button_unmap (GtkWidget *widget)
568 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
569 GtkSpinButtonPrivate *priv = spin_button->priv;
571 if (gtk_widget_get_mapped (widget))
573 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
575 gdk_window_hide (priv->panel);
576 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
581 gtk_spin_button_realize (GtkWidget *widget)
583 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
584 GtkSpinButtonPrivate *priv = spin_button->priv;
585 GtkAllocation allocation;
586 GtkRequisition requisition;
588 GdkWindowAttr attributes;
589 gint attributes_mask;
593 style = gtk_widget_get_style (widget);
595 arrow_size = spin_button_get_arrow_size (spin_button);
597 gtk_size_request_get_size (GTK_SIZE_REQUEST (spin_button), &requisition, NULL);
598 gtk_widget_get_allocation (widget, &allocation);
600 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
601 GDK_KEY_RELEASE_MASK);
602 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
604 attributes.window_type = GDK_WINDOW_CHILD;
605 attributes.wclass = GDK_INPUT_OUTPUT;
606 attributes.visual = gtk_widget_get_visual (widget);
607 attributes.event_mask = gtk_widget_get_events (widget);
608 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
609 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
610 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
612 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
614 attributes.x = allocation.width - arrow_size - 2 * style->xthickness;
615 attributes.y = (allocation.height - requisition.height) / 2;
616 attributes.width = arrow_size + 2 * style->xthickness;
617 attributes.height = requisition.height;
619 priv->panel = gdk_window_new (gtk_widget_get_window (widget),
620 &attributes, attributes_mask);
621 gdk_window_set_user_data (priv->panel, widget);
623 gtk_style_set_background (style,
624 priv->panel, GTK_STATE_NORMAL);
627 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
628 if (return_val == FALSE)
629 gtk_spin_button_default_output (spin_button);
631 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
635 gtk_spin_button_unrealize (GtkWidget *widget)
637 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
638 GtkSpinButtonPrivate *priv = spin->priv;
640 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
644 gdk_window_set_user_data (priv->panel, NULL);
645 gdk_window_destroy (priv->panel);
651 compute_double_length (double val, int digits)
657 if (fabs (val) > 1.0)
658 a = floor (log10 (fabs (val))) + 1;
670 return a + digits + extra;
674 gtk_spin_button_size_request (GtkWidget *widget,
675 GtkRequisition *requisition)
677 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
678 GtkSpinButtonPrivate *priv = spin_button->priv;
679 GtkEntry *entry = GTK_ENTRY (widget);
683 style = gtk_widget_get_style (widget);
685 arrow_size = spin_button_get_arrow_size (spin_button);
687 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_request (widget, requisition);
689 if (entry->width_chars < 0)
691 PangoContext *context;
692 PangoFontMetrics *metrics;
698 gboolean interior_focus;
700 gint xborder, yborder;
701 GtkBorder inner_border;
703 gtk_widget_style_get (widget,
704 "interior-focus", &interior_focus,
705 "focus-line-width", &focus_width,
708 context = gtk_widget_get_pango_context (widget);
709 metrics = pango_context_get_metrics (context,
711 pango_context_get_language (context));
713 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
714 digit_width = PANGO_SCALE *
715 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
717 pango_font_metrics_unref (metrics);
719 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
721 width = MIN_SPIN_BUTTON_WIDTH;
722 max_string_len = MAX (10, compute_double_length (1e9 * priv->adjustment->step_increment,
725 string_len = compute_double_length (priv->adjustment->upper,
727 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
728 width = MAX (width, w);
729 string_len = compute_double_length (priv->adjustment->lower,
731 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
732 width = MAX (width, w);
734 _gtk_entry_get_borders (entry, &xborder, &yborder);
735 _gtk_entry_effective_inner_border (entry, &inner_border);
737 requisition->width = width + xborder * 2 + inner_border.left + inner_border.right;
740 requisition->width += arrow_size + 2 * style->xthickness;
744 gtk_spin_button_size_allocate (GtkWidget *widget,
745 GtkAllocation *allocation)
747 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
748 GtkSpinButtonPrivate *priv = spin->priv;
749 GtkAllocation panel_allocation;
750 GtkRequisition requisition;
754 arrow_size = spin_button_get_arrow_size (spin);
755 panel_width = arrow_size + 2 * gtk_widget_get_style (widget)->xthickness;
757 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
759 gtk_widget_set_allocation (widget, allocation);
761 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
762 panel_allocation.x = 0;
764 panel_allocation.x = allocation->width - panel_width;
766 panel_allocation.width = panel_width;
767 panel_allocation.height = MIN (requisition.height, allocation->height);
769 panel_allocation.y = 0;
771 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
773 if (gtk_widget_get_realized (widget))
775 gdk_window_move_resize (priv->panel,
778 panel_allocation.width,
779 panel_allocation.height);
782 gtk_widget_queue_draw (GTK_WIDGET (spin));
786 gtk_spin_button_draw (GtkWidget *widget,
789 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
790 GtkSpinButtonPrivate *priv = spin->priv;
792 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
794 if (gtk_cairo_should_draw_window (cr, priv->panel))
796 GtkShadowType shadow_type;
799 shadow_type = spin_button_get_shadow_type (spin);
801 gdk_window_get_position (priv->panel, &x, &y);
802 cairo_translate (cr, x, y);
804 if (shadow_type != GTK_SHADOW_NONE)
809 state = gtk_widget_has_focus (widget) ?
810 GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
812 gdk_drawable_get_size (priv->panel, &width, &height);
814 gtk_paint_box (gtk_widget_get_style (widget), cr,
816 widget, "spinbutton",
817 0, 0, width, height);
820 gtk_spin_button_draw_arrow (spin, cr, GTK_ARROW_UP);
821 gtk_spin_button_draw_arrow (spin, cr, GTK_ARROW_DOWN);
828 spin_button_at_limit (GtkSpinButton *spin_button,
831 GtkSpinButtonPrivate *priv = spin_button->priv;
832 GtkArrowType effective_arrow;
837 if (priv->adjustment->step_increment > 0)
838 effective_arrow = arrow;
840 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
842 if (effective_arrow == GTK_ARROW_UP &&
843 (priv->adjustment->upper - priv->adjustment->value <= EPSILON))
846 if (effective_arrow == GTK_ARROW_DOWN &&
847 (priv->adjustment->value - priv->adjustment->lower <= EPSILON))
854 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
856 GtkArrowType arrow_type)
858 GtkSpinButtonPrivate *priv;
859 GtkRequisition requisition;
860 GtkStateType state_type;
861 GtkShadowType shadow_type;
870 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
872 priv = spin_button->priv;
873 widget = GTK_WIDGET (spin_button);
875 style = gtk_widget_get_style (widget);
876 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
878 width = spin_button_get_arrow_size (spin_button) + 2 * style->xthickness;
880 if (arrow_type == GTK_ARROW_UP)
885 height = requisition.height / 2;
890 y = requisition.height / 2;
892 height = (requisition.height + 1) / 2;
895 if (spin_button_at_limit (spin_button, arrow_type))
897 shadow_type = GTK_SHADOW_OUT;
898 state_type = GTK_STATE_INSENSITIVE;
902 if (priv->click_child == arrow_type)
904 state_type = GTK_STATE_ACTIVE;
905 shadow_type = GTK_SHADOW_IN;
909 if (priv->in_child == arrow_type &&
910 priv->click_child == NO_ARROW)
912 state_type = GTK_STATE_PRELIGHT;
916 state_type = gtk_widget_get_state (widget);
919 shadow_type = GTK_SHADOW_OUT;
923 gtk_paint_box (style, cr,
924 state_type, shadow_type,
926 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
927 x, y, width, height);
929 height = requisition.height;
931 if (arrow_type == GTK_ARROW_DOWN)
934 height = height - y - 2;
939 height = height / 2 - 2;
944 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
950 w -= w % 2 - 1; /* force odd */
953 x += (width - w) / 2;
954 y += (height - h) / 2;
959 gtk_paint_arrow (style, cr,
960 state_type, shadow_type,
961 widget, "spinbutton",
963 x, y, width, height);
967 gtk_spin_button_enter_notify (GtkWidget *widget,
968 GdkEventCrossing *event)
970 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
971 GtkSpinButtonPrivate *priv = spin->priv;
972 GtkRequisition requisition;
974 if (event->window == priv->panel)
980 device = gdk_event_get_device ((GdkEvent *) event);
981 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
983 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
985 if (y <= requisition.height / 2)
986 priv->in_child = GTK_ARROW_UP;
988 priv->in_child = GTK_ARROW_DOWN;
990 gtk_widget_queue_draw (GTK_WIDGET (spin));
993 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event)
994 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1000 gtk_spin_button_leave_notify (GtkWidget *widget,
1001 GdkEventCrossing *event)
1003 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1004 GtkSpinButtonPrivate *priv = spin->priv;
1006 priv->in_child = NO_ARROW;
1007 gtk_widget_queue_draw (GTK_WIDGET (spin));
1009 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event)
1010 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1016 gtk_spin_button_focus_out (GtkWidget *widget,
1017 GdkEventFocus *event)
1019 if (GTK_ENTRY (widget)->editable)
1020 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1022 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1026 gtk_spin_button_grab_notify (GtkWidget *widget,
1027 gboolean was_grabbed)
1029 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1033 gtk_spin_button_stop_spinning (spin);
1034 gtk_widget_queue_draw (GTK_WIDGET (spin));
1039 gtk_spin_button_state_changed (GtkWidget *widget,
1040 GtkStateType previous_state)
1042 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1044 if (!gtk_widget_is_sensitive (widget))
1046 gtk_spin_button_stop_spinning (spin);
1047 gtk_widget_queue_draw (GTK_WIDGET (spin));
1052 gtk_spin_button_style_set (GtkWidget *widget,
1053 GtkStyle *previous_style)
1055 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1056 GtkSpinButtonPrivate *priv = spin->priv;
1058 if (previous_style && gtk_widget_get_realized (widget))
1059 gtk_style_set_background (gtk_widget_get_style (widget),
1060 priv->panel, GTK_STATE_NORMAL);
1062 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_set (widget, previous_style);
1067 gtk_spin_button_scroll (GtkWidget *widget,
1068 GdkEventScroll *event)
1070 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1071 GtkSpinButtonPrivate *priv = spin->priv;
1073 if (event->direction == GDK_SCROLL_UP)
1075 if (!gtk_widget_has_focus (widget))
1076 gtk_widget_grab_focus (widget);
1077 gtk_spin_button_real_spin (spin, priv->adjustment->step_increment);
1079 else if (event->direction == GDK_SCROLL_DOWN)
1081 if (!gtk_widget_has_focus (widget))
1082 gtk_widget_grab_focus (widget);
1083 gtk_spin_button_real_spin (spin, -priv->adjustment->step_increment);
1092 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1094 GtkSpinButtonPrivate *priv = spin->priv;
1098 g_source_remove (priv->timer);
1100 priv->timer_calls = 0;
1101 priv->need_timer = FALSE;
1106 priv->timer_step = priv->adjustment->step_increment;
1107 priv->timer_calls = 0;
1109 priv->click_child = NO_ARROW;
1114 start_spinning (GtkSpinButton *spin,
1115 GtkArrowType click_child,
1118 GtkSpinButtonPrivate *priv;
1120 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1124 priv->click_child = click_child;
1128 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1131 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1133 priv->timer_step = step;
1134 priv->need_timer = TRUE;
1135 priv->timer = gdk_threads_add_timeout (timeout,
1136 (GSourceFunc) gtk_spin_button_timer,
1139 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1141 gtk_widget_queue_draw (GTK_WIDGET (spin));
1145 gtk_spin_button_button_press (GtkWidget *widget,
1146 GdkEventButton *event)
1148 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1149 GtkSpinButtonPrivate *priv = spin->priv;
1153 if (event->window == priv->panel)
1155 GtkRequisition requisition;
1157 if (!gtk_widget_has_focus (widget))
1158 gtk_widget_grab_focus (widget);
1159 priv->button = event->button;
1161 if (GTK_ENTRY (widget)->editable)
1162 gtk_spin_button_update (spin);
1164 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
1166 if (event->y <= requisition.height / 2)
1168 if (event->button == 1)
1169 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->step_increment);
1170 else if (event->button == 2)
1171 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->page_increment);
1173 priv->click_child = GTK_ARROW_UP;
1177 if (event->button == 1)
1178 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->step_increment);
1179 else if (event->button == 2)
1180 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->page_increment);
1182 priv->click_child = GTK_ARROW_DOWN;
1187 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1193 gtk_spin_button_button_release (GtkWidget *widget,
1194 GdkEventButton *event)
1196 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1197 GtkSpinButtonPrivate *priv = spin->priv;
1200 arrow_size = spin_button_get_arrow_size (spin);
1202 if (event->button == priv->button)
1204 int click_child = priv->click_child;
1206 gtk_spin_button_stop_spinning (spin);
1208 if (event->button == 3)
1210 GtkRequisition requisition;
1212 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
1214 if (event->y >= 0 && event->x >= 0 &&
1215 event->y <= requisition.height &&
1216 event->x <= arrow_size + 2 * gtk_widget_get_style (widget)->xthickness)
1218 if (click_child == GTK_ARROW_UP &&
1219 event->y <= requisition.height / 2)
1223 diff = priv->adjustment->upper - priv->adjustment->value;
1225 gtk_spin_button_real_spin (spin, diff);
1227 else if (click_child == GTK_ARROW_DOWN &&
1228 event->y > requisition.height / 2)
1232 diff = priv->adjustment->value - priv->adjustment->lower;
1234 gtk_spin_button_real_spin (spin, -diff);
1238 gtk_widget_queue_draw (GTK_WIDGET (spin));
1243 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1247 gtk_spin_button_motion_notify (GtkWidget *widget,
1248 GdkEventMotion *event)
1250 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1251 GtkSpinButtonPrivate *priv = spin->priv;
1256 if (event->window == priv->panel)
1258 GtkRequisition requisition;
1261 gdk_event_request_motions (event);
1263 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
1265 if (y <= requisition.height / 2 &&
1266 priv->in_child == GTK_ARROW_DOWN)
1268 priv->in_child = GTK_ARROW_UP;
1269 gtk_widget_queue_draw (GTK_WIDGET (spin));
1271 else if (y > requisition.height / 2 &&
1272 priv->in_child == GTK_ARROW_UP)
1274 priv->in_child = GTK_ARROW_DOWN;
1275 gtk_widget_queue_draw (GTK_WIDGET (spin));
1281 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1285 gtk_spin_button_timer (GtkSpinButton *spin_button)
1287 GtkSpinButtonPrivate *priv = spin_button->priv;
1288 gboolean retval = FALSE;
1292 if (priv->click_child == GTK_ARROW_UP)
1293 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1295 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1297 if (priv->need_timer)
1299 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1302 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1304 priv->need_timer = FALSE;
1305 priv->timer = gdk_threads_add_timeout (timeout,
1306 (GSourceFunc) gtk_spin_button_timer,
1307 (gpointer) spin_button);
1311 if (priv->climb_rate > 0.0 && priv->timer_step
1312 < priv->adjustment->page_increment)
1314 if (priv->timer_calls < MAX_TIMER_CALLS)
1315 priv->timer_calls++;
1318 priv->timer_calls = 0;
1319 priv->timer_step += priv->climb_rate;
1330 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1331 GtkSpinButton *spin_button)
1333 gboolean return_val;
1335 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1338 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1339 if (return_val == FALSE)
1340 gtk_spin_button_default_output (spin_button);
1342 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1344 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1346 g_object_notify (G_OBJECT (spin_button), "value");
1350 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1351 GtkScrollType scroll)
1353 GtkSpinButtonPrivate *priv = spin->priv;
1356 /* When the key binding is activated, there may be an outstanding
1357 * value, so we first have to commit what is currently written in
1358 * the spin buttons text entry. See #106574
1360 gtk_spin_button_update (spin);
1362 old_value = priv->adjustment->value;
1364 /* We don't test whether the entry is editable, since
1365 * this key binding conceptually corresponds to changing
1366 * the value with the buttons using the mouse, which
1367 * we allow for non-editable spin buttons.
1371 case GTK_SCROLL_STEP_BACKWARD:
1372 case GTK_SCROLL_STEP_DOWN:
1373 case GTK_SCROLL_STEP_LEFT:
1374 gtk_spin_button_real_spin (spin, -priv->timer_step);
1376 if (priv->climb_rate > 0.0 && priv->timer_step
1377 < priv->adjustment->page_increment)
1379 if (priv->timer_calls < MAX_TIMER_CALLS)
1380 priv->timer_calls++;
1383 priv->timer_calls = 0;
1384 priv->timer_step += priv->climb_rate;
1389 case GTK_SCROLL_STEP_FORWARD:
1390 case GTK_SCROLL_STEP_UP:
1391 case GTK_SCROLL_STEP_RIGHT:
1392 gtk_spin_button_real_spin (spin, priv->timer_step);
1394 if (priv->climb_rate > 0.0 && priv->timer_step
1395 < priv->adjustment->page_increment)
1397 if (priv->timer_calls < MAX_TIMER_CALLS)
1398 priv->timer_calls++;
1401 priv->timer_calls = 0;
1402 priv->timer_step += priv->climb_rate;
1407 case GTK_SCROLL_PAGE_BACKWARD:
1408 case GTK_SCROLL_PAGE_DOWN:
1409 case GTK_SCROLL_PAGE_LEFT:
1410 gtk_spin_button_real_spin (spin, -priv->adjustment->page_increment);
1413 case GTK_SCROLL_PAGE_FORWARD:
1414 case GTK_SCROLL_PAGE_UP:
1415 case GTK_SCROLL_PAGE_RIGHT:
1416 gtk_spin_button_real_spin (spin, priv->adjustment->page_increment);
1419 case GTK_SCROLL_START:
1421 gdouble diff = priv->adjustment->value - priv->adjustment->lower;
1423 gtk_spin_button_real_spin (spin, -diff);
1427 case GTK_SCROLL_END:
1429 gdouble diff = priv->adjustment->upper - priv->adjustment->value;
1431 gtk_spin_button_real_spin (spin, diff);
1436 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1440 gtk_spin_button_update (spin);
1442 if (priv->adjustment->value == old_value)
1443 gtk_widget_error_bell (GTK_WIDGET (spin));
1447 gtk_spin_button_key_release (GtkWidget *widget,
1450 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1451 GtkSpinButtonPrivate *priv = spin->priv;
1453 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1454 priv->timer_step = priv->adjustment->step_increment;
1455 priv->timer_calls = 0;
1461 gtk_spin_button_snap (GtkSpinButton *spin_button,
1464 GtkSpinButtonPrivate *priv = spin_button->priv;
1468 inc = priv->adjustment->step_increment;
1472 tmp = (val - priv->adjustment->lower) / inc;
1473 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1474 val = priv->adjustment->lower + floor (tmp) * inc;
1476 val = priv->adjustment->lower + ceil (tmp) * inc;
1478 gtk_spin_button_set_value (spin_button, val);
1482 gtk_spin_button_activate (GtkEntry *entry)
1484 if (entry->editable)
1485 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1487 /* Chain up so that entry->activates_default is honored */
1488 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1492 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1501 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1503 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1504 panel_width = arrow_size + 2 * gtk_widget_get_style (GTK_WIDGET (entry))->xthickness;
1507 *width -= panel_width;
1509 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1514 gtk_spin_button_insert_text (GtkEditable *editable,
1515 const gchar *new_text,
1516 gint new_text_length,
1519 GtkEntry *entry = GTK_ENTRY (editable);
1520 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1521 GtkSpinButtonPrivate *priv = spin->priv;
1522 GtkEditableInterface *parent_editable_iface;
1524 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1536 const gchar *entry_text;
1538 entry_length = gtk_entry_get_text_length (entry);
1539 entry_text = gtk_entry_get_text (entry);
1543 if (*(lc->negative_sign))
1544 neg_sign = *(lc->negative_sign);
1548 if (*(lc->positive_sign))
1549 pos_sign = *(lc->positive_sign);
1554 /* Workaround for bug caused by some Windows application messing
1555 * up the positive sign of the current locale, more specifically
1556 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1557 * See bug #330743 and for instance
1558 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1560 * I don't know if the positive sign always gets bogusly set to
1561 * a digit when the above Registry value is corrupted as
1562 * described. (In my test case, it got set to "8", and in the
1563 * bug report above it presumably was set ot "0".) Probably it
1564 * might get set to almost anything? So how to distinguish a
1565 * bogus value from some correct one for some locale? That is
1566 * probably hard, but at least we should filter out the
1569 if (pos_sign >= '0' && pos_sign <= '9')
1573 for (sign=0, i=0; i<entry_length; i++)
1574 if ((entry_text[i] == neg_sign) ||
1575 (entry_text[i] == pos_sign))
1581 if (sign && !(*position))
1584 for (dotpos=-1, i=0; i<entry_length; i++)
1585 if (entry_text[i] == *(lc->decimal_point))
1591 if (dotpos > -1 && *position > dotpos &&
1592 (gint)priv->digits - entry_length
1593 + dotpos - new_text_length + 1 < 0)
1596 for (i = 0; i < new_text_length; i++)
1598 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1600 if (sign || (*position) || i)
1604 else if (new_text[i] == *(lc->decimal_point))
1606 if (!priv->digits || dotpos > -1 ||
1607 (new_text_length - 1 - i + entry_length
1608 - *position > (gint)priv->digits))
1610 dotpos = *position + i;
1612 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1617 parent_editable_iface->insert_text (editable, new_text,
1618 new_text_length, position);
1622 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1625 GtkSpinButtonPrivate *priv = spin_button->priv;
1627 gdouble new_value = 0.0;
1628 gboolean wrapped = FALSE;
1630 adj = priv->adjustment;
1632 new_value = adj->value + increment;
1638 if (fabs (adj->value - adj->upper) < EPSILON)
1640 new_value = adj->lower;
1643 else if (new_value > adj->upper)
1644 new_value = adj->upper;
1647 new_value = MIN (new_value, adj->upper);
1649 else if (increment < 0)
1653 if (fabs (adj->value - adj->lower) < EPSILON)
1655 new_value = adj->upper;
1658 else if (new_value < adj->lower)
1659 new_value = adj->lower;
1662 new_value = MAX (new_value, adj->lower);
1665 if (fabs (new_value - adj->value) > EPSILON)
1666 gtk_adjustment_set_value (adj, new_value);
1669 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1671 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1675 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1680 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1682 return GTK_INPUT_ERROR;
1688 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1690 GtkSpinButtonPrivate *priv = spin_button->priv;
1692 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, priv->adjustment->value);
1694 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1695 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1701 /***********************************************************
1702 ***********************************************************
1703 *** Public interface ***
1704 ***********************************************************
1705 ***********************************************************/
1709 * gtk_spin_button_configure:
1710 * @spin_button: a #GtkSpinButton
1711 * @adjustment: (allow-none): a #GtkAdjustment.
1712 * @climb_rate: the new climb rate.
1713 * @digits: the number of decimal places to display in the spin button.
1715 * Changes the properties of an existing spin button. The adjustment, climb rate,
1716 * and number of decimal places are all changed accordingly, after this function call.
1719 gtk_spin_button_configure (GtkSpinButton *spin_button,
1720 GtkAdjustment *adjustment,
1724 GtkSpinButtonPrivate *priv;
1726 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1728 priv = spin_button->priv;
1731 gtk_spin_button_set_adjustment (spin_button, adjustment);
1733 adjustment = priv->adjustment;
1735 g_object_freeze_notify (G_OBJECT (spin_button));
1736 if (priv->digits != digits)
1738 priv->digits = digits;
1739 g_object_notify (G_OBJECT (spin_button), "digits");
1742 if (priv->climb_rate != climb_rate)
1744 priv->climb_rate = climb_rate;
1745 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1747 g_object_thaw_notify (G_OBJECT (spin_button));
1749 gtk_adjustment_value_changed (adjustment);
1753 gtk_spin_button_new (GtkAdjustment *adjustment,
1757 GtkSpinButton *spin;
1760 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1762 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1764 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1766 return GTK_WIDGET (spin);
1770 * gtk_spin_button_new_with_range:
1771 * @min: Minimum allowable value
1772 * @max: Maximum allowable value
1773 * @step: Increment added or subtracted by spinning the widget
1775 * This is a convenience constructor that allows creation of a numeric
1776 * #GtkSpinButton without manually creating an adjustment. The value is
1777 * initially set to the minimum value and a page increment of 10 * @step
1778 * is the default. The precision of the spin button is equivalent to the
1779 * precision of @step.
1781 * Note that the way in which the precision is derived works best if @step
1782 * is a power of ten. If the resulting precision is not suitable for your
1783 * needs, use gtk_spin_button_set_digits() to correct it.
1785 * Return value: The new spin button as a #GtkWidget.
1788 gtk_spin_button_new_with_range (gdouble min,
1793 GtkSpinButton *spin;
1796 g_return_val_if_fail (min <= max, NULL);
1797 g_return_val_if_fail (step != 0.0, NULL);
1799 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1801 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1803 if (fabs (step) >= 1.0 || step == 0.0)
1806 digits = abs ((gint) floor (log10 (fabs (step))));
1807 if (digits > MAX_DIGITS)
1808 digits = MAX_DIGITS;
1811 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1813 gtk_spin_button_set_numeric (spin, TRUE);
1815 return GTK_WIDGET (spin);
1818 /* Callback used when the spin button's adjustment changes. We need to redraw
1819 * the arrows when the adjustment's range changes, and reevaluate our size request.
1822 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1824 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1825 GtkSpinButtonPrivate *priv = spin_button->priv;
1827 priv->timer_step = priv->adjustment->step_increment;
1828 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1832 * gtk_spin_button_set_adjustment:
1833 * @spin_button: a #GtkSpinButton
1834 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1836 * Replaces the #GtkAdjustment associated with @spin_button.
1839 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1840 GtkAdjustment *adjustment)
1842 GtkSpinButtonPrivate *priv;
1844 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1846 priv = spin_button->priv;
1848 if (priv->adjustment != adjustment)
1850 if (priv->adjustment)
1852 g_signal_handlers_disconnect_by_func (priv->adjustment,
1853 gtk_spin_button_value_changed,
1855 g_signal_handlers_disconnect_by_func (priv->adjustment,
1856 adjustment_changed_cb,
1858 g_object_unref (priv->adjustment);
1860 priv->adjustment = adjustment;
1863 g_object_ref_sink (adjustment);
1864 g_signal_connect (adjustment, "value-changed",
1865 G_CALLBACK (gtk_spin_button_value_changed),
1867 g_signal_connect (adjustment, "changed",
1868 G_CALLBACK (adjustment_changed_cb),
1870 priv->timer_step = priv->adjustment->step_increment;
1873 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1876 g_object_notify (G_OBJECT (spin_button), "adjustment");
1880 * gtk_spin_button_get_adjustment:
1881 * @spin_button: a #GtkSpinButton
1883 * Get the adjustment associated with a #GtkSpinButton
1885 * Return value: (transfer none): the #GtkAdjustment of @spin_button
1888 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1890 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1892 return spin_button->priv->adjustment;
1896 * gtk_spin_button_set_digits:
1897 * @spin_button: a #GtkSpinButton
1898 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1900 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1904 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1907 GtkSpinButtonPrivate *priv;
1909 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1911 priv = spin_button->priv;
1913 if (priv->digits != digits)
1915 priv->digits = digits;
1916 gtk_spin_button_value_changed (priv->adjustment, spin_button);
1917 g_object_notify (G_OBJECT (spin_button), "digits");
1919 /* since lower/upper may have changed */
1920 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1925 * gtk_spin_button_get_digits:
1926 * @spin_button: a #GtkSpinButton
1928 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1930 * Returns: the current precision
1933 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1935 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1937 return spin_button->priv->digits;
1941 * gtk_spin_button_set_increments:
1942 * @spin_button: a #GtkSpinButton
1943 * @step: increment applied for a button 1 press.
1944 * @page: increment applied for a button 2 press.
1946 * Sets the step and page increments for spin_button. This affects how
1947 * quickly the value changes when the spin button's arrows are activated.
1950 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1954 GtkSpinButtonPrivate *priv;
1956 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1958 priv = spin_button->priv;
1960 priv->adjustment->step_increment = step;
1961 priv->adjustment->page_increment = page;
1965 * gtk_spin_button_get_increments:
1966 * @spin_button: a #GtkSpinButton
1967 * @step: (allow-none): location to store step increment, or %NULL
1968 * @page: (allow-none): location to store page increment, or %NULL
1970 * Gets the current step and page the increments used by @spin_button. See
1971 * gtk_spin_button_set_increments().
1974 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1978 GtkSpinButtonPrivate *priv;
1980 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1982 priv = spin_button->priv;
1985 *step = priv->adjustment->step_increment;
1987 *page = priv->adjustment->page_increment;
1991 * gtk_spin_button_set_range:
1992 * @spin_button: a #GtkSpinButton
1993 * @min: minimum allowable value
1994 * @max: maximum allowable value
1996 * Sets the minimum and maximum allowable values for @spin_button
1999 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2003 GtkSpinButtonPrivate *priv;
2006 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2008 priv = spin_button->priv;
2010 priv->adjustment->lower = min;
2011 priv->adjustment->upper = max;
2013 value = CLAMP (priv->adjustment->value,
2014 priv->adjustment->lower,
2015 (priv->adjustment->upper - priv->adjustment->page_size));
2017 if (value != priv->adjustment->value)
2018 gtk_spin_button_set_value (spin_button, value);
2020 gtk_adjustment_changed (priv->adjustment);
2024 * gtk_spin_button_get_range:
2025 * @spin_button: a #GtkSpinButton
2026 * @min: (allow-none): location to store minimum allowed value, or %NULL
2027 * @max: (allow-none): location to store maximum allowed value, or %NULL
2029 * Gets the range allowed for @spin_button. See
2030 * gtk_spin_button_set_range().
2033 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2037 GtkSpinButtonPrivate *priv;
2039 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2041 priv = spin_button->priv;
2044 *min = priv->adjustment->lower;
2046 *max = priv->adjustment->upper;
2050 * gtk_spin_button_get_value:
2051 * @spin_button: a #GtkSpinButton
2053 * Get the value in the @spin_button.
2055 * Return value: the value of @spin_button
2058 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2060 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2062 return spin_button->priv->adjustment->value;
2066 * gtk_spin_button_get_value_as_int:
2067 * @spin_button: a #GtkSpinButton
2069 * Get the value @spin_button represented as an integer.
2071 * Return value: the value of @spin_button
2074 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2076 GtkSpinButtonPrivate *priv;
2079 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2081 priv = spin_button->priv;
2083 val = priv->adjustment->value;
2084 if (val - floor (val) < ceil (val) - val)
2091 * gtk_spin_button_set_value:
2092 * @spin_button: a #GtkSpinButton
2093 * @value: the new value
2095 * Set the value of @spin_button.
2098 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2101 GtkSpinButtonPrivate *priv;
2103 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2105 priv = spin_button->priv;
2107 if (fabs (value - priv->adjustment->value) > EPSILON)
2108 gtk_adjustment_set_value (priv->adjustment, value);
2111 gint return_val = FALSE;
2112 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2113 if (return_val == FALSE)
2114 gtk_spin_button_default_output (spin_button);
2119 * gtk_spin_button_set_update_policy:
2120 * @spin_button: a #GtkSpinButton
2121 * @policy: a #GtkSpinButtonUpdatePolicy value
2123 * Sets the update behavior of a spin button. This determines whether the
2124 * spin button is always updated or only when a valid value is set.
2127 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2128 GtkSpinButtonUpdatePolicy policy)
2130 GtkSpinButtonPrivate *priv;
2132 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2134 priv = spin_button->priv;
2136 if (priv->update_policy != policy)
2138 priv->update_policy = policy;
2139 g_object_notify (G_OBJECT (spin_button), "update-policy");
2144 * gtk_spin_button_get_update_policy:
2145 * @spin_button: a #GtkSpinButton
2147 * Gets the update behavior of a spin button. See
2148 * gtk_spin_button_set_update_policy().
2150 * Return value: the current update policy
2152 GtkSpinButtonUpdatePolicy
2153 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2155 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2157 return spin_button->priv->update_policy;
2161 * gtk_spin_button_set_numeric:
2162 * @spin_button: a #GtkSpinButton
2163 * @numeric: flag indicating if only numeric entry is allowed.
2165 * Sets the flag that determines if non-numeric text can be typed into
2169 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2172 GtkSpinButtonPrivate *priv;
2174 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2176 priv = spin_button->priv;
2178 numeric = numeric != FALSE;
2180 if (priv->numeric != numeric)
2182 priv->numeric = numeric;
2183 g_object_notify (G_OBJECT (spin_button), "numeric");
2188 * gtk_spin_button_get_numeric:
2189 * @spin_button: a #GtkSpinButton
2191 * Returns whether non-numeric text can be typed into the spin button.
2192 * See gtk_spin_button_set_numeric().
2194 * Return value: %TRUE if only numeric text can be entered
2197 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2199 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2201 return spin_button->priv->numeric;
2205 * gtk_spin_button_set_wrap:
2206 * @spin_button: a #GtkSpinButton
2207 * @wrap: a flag indicating if wrapping behavior is performed.
2209 * Sets the flag that determines if a spin button value wraps around to the
2210 * opposite limit when the upper or lower limit of the range is exceeded.
2213 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2216 GtkSpinButtonPrivate *priv;
2218 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2220 priv = spin_button->priv;
2222 wrap = wrap != FALSE;
2224 if (priv->wrap != wrap)
2226 priv->wrap = (wrap != 0);
2228 g_object_notify (G_OBJECT (spin_button), "wrap");
2233 * gtk_spin_button_get_wrap:
2234 * @spin_button: a #GtkSpinButton
2236 * Returns whether the spin button's value wraps around to the
2237 * opposite limit when the upper or lower limit of the range is
2238 * exceeded. See gtk_spin_button_set_wrap().
2240 * Return value: %TRUE if the spin button wraps around
2243 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2245 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2247 return spin_button->priv->wrap;
2251 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2257 style = gtk_widget_get_style (GTK_WIDGET (spin_button));
2258 size = pango_font_description_get_size (style->font_desc);
2259 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2261 return arrow_size - arrow_size % 2; /* force even */
2265 * spin_button_get_shadow_type:
2266 * @spin_button: a #GtkSpinButton
2268 * Convenience function to Get the shadow type from the underlying widget's
2271 * Return value: the #GtkShadowType
2274 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2276 GtkShadowType rc_shadow_type;
2278 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow-type", &rc_shadow_type, NULL);
2280 return rc_shadow_type;
2284 * gtk_spin_button_set_snap_to_ticks:
2285 * @spin_button: a #GtkSpinButton
2286 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2288 * Sets the policy as to whether values are corrected to the nearest step
2289 * increment when a spin button is activated after providing an invalid value.
2292 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2293 gboolean snap_to_ticks)
2295 GtkSpinButtonPrivate *priv;
2298 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2300 priv = spin_button->priv;
2302 new_val = (snap_to_ticks != 0);
2304 if (new_val != priv->snap_to_ticks)
2306 priv->snap_to_ticks = new_val;
2307 if (new_val && GTK_ENTRY (spin_button)->editable)
2308 gtk_spin_button_update (spin_button);
2310 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2315 * gtk_spin_button_get_snap_to_ticks:
2316 * @spin_button: a #GtkSpinButton
2318 * Returns whether the values are corrected to the nearest step. See
2319 * gtk_spin_button_set_snap_to_ticks().
2321 * Return value: %TRUE if values are snapped to the nearest step.
2324 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2326 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2328 return spin_button->priv->snap_to_ticks;
2332 * gtk_spin_button_spin:
2333 * @spin_button: a #GtkSpinButton
2334 * @direction: a #GtkSpinType indicating the direction to spin.
2335 * @increment: step increment to apply in the specified direction.
2337 * Increment or decrement a spin button's value in a specified direction
2338 * by a specified amount.
2341 gtk_spin_button_spin (GtkSpinButton *spin_button,
2342 GtkSpinType direction,
2345 GtkSpinButtonPrivate *priv;
2349 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2351 priv = spin_button->priv;
2353 adj = priv->adjustment;
2355 /* for compatibility with the 1.0.x version of this function */
2356 if (increment != 0 && increment != adj->step_increment &&
2357 (direction == GTK_SPIN_STEP_FORWARD ||
2358 direction == GTK_SPIN_STEP_BACKWARD))
2360 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2361 increment = -increment;
2362 direction = GTK_SPIN_USER_DEFINED;
2367 case GTK_SPIN_STEP_FORWARD:
2369 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2372 case GTK_SPIN_STEP_BACKWARD:
2374 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2377 case GTK_SPIN_PAGE_FORWARD:
2379 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2382 case GTK_SPIN_PAGE_BACKWARD:
2384 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2389 diff = adj->value - adj->lower;
2391 gtk_spin_button_real_spin (spin_button, -diff);
2396 diff = adj->upper - adj->value;
2398 gtk_spin_button_real_spin (spin_button, diff);
2401 case GTK_SPIN_USER_DEFINED:
2404 gtk_spin_button_real_spin (spin_button, increment);
2413 * gtk_spin_button_update:
2414 * @spin_button: a #GtkSpinButton
2416 * Manually force an update of the spin button.
2419 gtk_spin_button_update (GtkSpinButton *spin_button)
2421 GtkSpinButtonPrivate *priv;
2426 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2428 priv = spin_button->priv;
2431 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2432 if (return_val == FALSE)
2434 return_val = gtk_spin_button_default_input (spin_button, &val);
2435 error = (return_val == GTK_INPUT_ERROR);
2437 else if (return_val == GTK_INPUT_ERROR)
2440 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2442 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2444 if (val < priv->adjustment->lower)
2445 val = priv->adjustment->lower;
2446 else if (val > priv->adjustment->upper)
2447 val = priv->adjustment->upper;
2449 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2451 val < priv->adjustment->lower ||
2452 val > priv->adjustment->upper))
2454 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2458 if (priv->snap_to_ticks)
2459 gtk_spin_button_snap (spin_button, val);
2461 gtk_spin_button_set_value (spin_button, val);
2465 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2467 return spin_button->priv->panel;