1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GtkSpinButton widget for GTK+
5 * Copyright (C) 1998 Lars Hamann and Stefan Jeske
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
36 #include "gdk/gdkkeysyms.h"
37 #include "gtkbindings.h"
38 #include "gtkspinbutton.h"
39 #include "gtkentryprivate.h"
41 #include "gtkmarshalers.h"
42 #include "gtksettings.h"
43 #include "gtkprivate.h"
46 #define MIN_SPIN_BUTTON_WIDTH 30
47 #define MAX_TIMER_CALLS 5
50 #define MIN_ARROW_WIDTH 6
53 struct _GtkSpinButtonPrivate
55 GtkSpinButtonUpdatePolicy update_policy;
56 GtkAdjustment *adjustment;
66 guint click_child : 2; /* valid: GTK_ARROW_UP=0, GTK_ARROW_DOWN=1 or 2=NONE/BOTH */
71 guint snap_to_ticks : 1;
72 guint timer_calls : 3;
99 static void gtk_spin_button_editable_init (GtkEditableInterface *iface);
100 static void gtk_spin_button_finalize (GObject *object);
101 static void gtk_spin_button_set_property (GObject *object,
105 static void gtk_spin_button_get_property (GObject *object,
109 static void gtk_spin_button_destroy (GtkWidget *widget);
110 static void gtk_spin_button_map (GtkWidget *widget);
111 static void gtk_spin_button_unmap (GtkWidget *widget);
112 static void gtk_spin_button_realize (GtkWidget *widget);
113 static void gtk_spin_button_unrealize (GtkWidget *widget);
114 static void gtk_spin_button_size_request (GtkWidget *widget,
115 GtkRequisition *requisition);
116 static void gtk_spin_button_size_allocate (GtkWidget *widget,
117 GtkAllocation *allocation);
118 static gint gtk_spin_button_draw (GtkWidget *widget,
120 static gint gtk_spin_button_button_press (GtkWidget *widget,
121 GdkEventButton *event);
122 static gint gtk_spin_button_button_release (GtkWidget *widget,
123 GdkEventButton *event);
124 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
125 GdkEventMotion *event);
126 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
127 GdkEventCrossing *event);
128 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
129 GdkEventCrossing *event);
130 static gint gtk_spin_button_focus_out (GtkWidget *widget,
131 GdkEventFocus *event);
132 static void gtk_spin_button_grab_notify (GtkWidget *widget,
133 gboolean was_grabbed);
134 static void gtk_spin_button_state_changed (GtkWidget *widget,
135 GtkStateType previous_state);
136 static void gtk_spin_button_style_set (GtkWidget *widget,
137 GtkStyle *previous_style);
138 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
140 GtkArrowType arrow_type);
141 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
142 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
143 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
144 GtkSpinButton *spin_button);
145 static gint gtk_spin_button_key_release (GtkWidget *widget,
147 static gint gtk_spin_button_scroll (GtkWidget *widget,
148 GdkEventScroll *event);
149 static void gtk_spin_button_activate (GtkEntry *entry);
150 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
155 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
157 static void gtk_spin_button_insert_text (GtkEditable *editable,
158 const gchar *new_text,
159 gint new_text_length,
161 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
163 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
164 GtkScrollType scroll);
166 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
168 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
170 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
171 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
173 static guint spinbutton_signals[LAST_SIGNAL] = {0};
177 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
178 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
179 gtk_spin_button_editable_init))
181 #define add_spin_binding(binding_set, keyval, mask, scroll) \
182 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
184 GTK_TYPE_SCROLL_TYPE, scroll)
187 gtk_spin_button_class_init (GtkSpinButtonClass *class)
189 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
190 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
191 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
192 GtkBindingSet *binding_set;
194 gobject_class->finalize = gtk_spin_button_finalize;
195 gobject_class->set_property = gtk_spin_button_set_property;
196 gobject_class->get_property = gtk_spin_button_get_property;
198 widget_class->destroy = gtk_spin_button_destroy;
199 widget_class->map = gtk_spin_button_map;
200 widget_class->unmap = gtk_spin_button_unmap;
201 widget_class->realize = gtk_spin_button_realize;
202 widget_class->unrealize = gtk_spin_button_unrealize;
203 widget_class->size_request = gtk_spin_button_size_request;
204 widget_class->size_allocate = gtk_spin_button_size_allocate;
205 widget_class->draw = gtk_spin_button_draw;
206 widget_class->scroll_event = gtk_spin_button_scroll;
207 widget_class->button_press_event = gtk_spin_button_button_press;
208 widget_class->button_release_event = gtk_spin_button_button_release;
209 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
210 widget_class->key_release_event = gtk_spin_button_key_release;
211 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
212 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
213 widget_class->focus_out_event = gtk_spin_button_focus_out;
214 widget_class->grab_notify = gtk_spin_button_grab_notify;
215 widget_class->state_changed = gtk_spin_button_state_changed;
216 widget_class->style_set = gtk_spin_button_style_set;
218 entry_class->activate = gtk_spin_button_activate;
219 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
222 class->output = NULL;
223 class->change_value = gtk_spin_button_real_change_value;
225 g_object_class_install_property (gobject_class,
227 g_param_spec_object ("adjustment",
229 P_("The adjustment that holds the value of the spin button"),
231 GTK_PARAM_READWRITE));
233 g_object_class_install_property (gobject_class,
235 g_param_spec_double ("climb-rate",
237 P_("The acceleration rate when you hold down a button"),
241 GTK_PARAM_READWRITE));
243 g_object_class_install_property (gobject_class,
245 g_param_spec_uint ("digits",
247 P_("The number of decimal places to display"),
251 GTK_PARAM_READWRITE));
253 g_object_class_install_property (gobject_class,
255 g_param_spec_boolean ("snap-to-ticks",
257 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
259 GTK_PARAM_READWRITE));
261 g_object_class_install_property (gobject_class,
263 g_param_spec_boolean ("numeric",
265 P_("Whether non-numeric characters should be ignored"),
267 GTK_PARAM_READWRITE));
269 g_object_class_install_property (gobject_class,
271 g_param_spec_boolean ("wrap",
273 P_("Whether a spin button should wrap upon reaching its limits"),
275 GTK_PARAM_READWRITE));
277 g_object_class_install_property (gobject_class,
279 g_param_spec_enum ("update-policy",
281 P_("Whether the spin button should update always, or only when the value is legal"),
282 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
284 GTK_PARAM_READWRITE));
286 g_object_class_install_property (gobject_class,
288 g_param_spec_double ("value",
290 P_("Reads the current value, or sets a new value"),
294 GTK_PARAM_READWRITE));
296 gtk_widget_class_install_style_property_parser (widget_class,
297 g_param_spec_enum ("shadow-type",
299 P_("Style of bevel around the spin button"),
300 GTK_TYPE_SHADOW_TYPE,
303 gtk_rc_property_parse_enum);
304 spinbutton_signals[INPUT] =
305 g_signal_new (I_("input"),
306 G_TYPE_FROM_CLASS (gobject_class),
308 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
310 _gtk_marshal_INT__POINTER,
315 * GtkSpinButton::output:
316 * @spin_button: the object which received the signal
318 * The ::output signal can be used to change to formatting
319 * of the value that is displayed in the spin buttons entry.
321 * /* show leading zeros */
323 * on_output (GtkSpinButton *spin,
326 * GtkAdjustment *adj;
330 * adj = gtk_spin_button_get_adjustment (spin);
331 * value = (int)gtk_adjustment_get_value (adj);
332 * text = g_strdup_printf ("%02d", value);
333 * gtk_entry_set_text (GTK_ENTRY (spin), text);
340 * Returns: %TRUE if the value has been displayed.
342 spinbutton_signals[OUTPUT] =
343 g_signal_new (I_("output"),
344 G_TYPE_FROM_CLASS (gobject_class),
346 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
347 _gtk_boolean_handled_accumulator, NULL,
348 _gtk_marshal_BOOLEAN__VOID,
351 spinbutton_signals[VALUE_CHANGED] =
352 g_signal_new (I_("value-changed"),
353 G_TYPE_FROM_CLASS (gobject_class),
355 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
357 _gtk_marshal_VOID__VOID,
361 * GtkSpinButton::wrapped:
362 * @spinbutton: the object which received the signal
364 * The wrapped signal is emitted right after the spinbutton wraps
365 * from its maximum to minimum value or vice-versa.
369 spinbutton_signals[WRAPPED] =
370 g_signal_new (I_("wrapped"),
371 G_TYPE_FROM_CLASS (gobject_class),
373 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
375 _gtk_marshal_VOID__VOID,
379 spinbutton_signals[CHANGE_VALUE] =
380 g_signal_new (I_("change-value"),
381 G_TYPE_FROM_CLASS (gobject_class),
382 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
383 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
385 _gtk_marshal_VOID__ENUM,
387 GTK_TYPE_SCROLL_TYPE);
389 binding_set = gtk_binding_set_by_class (class);
391 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
392 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
393 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
394 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
395 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
396 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
397 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
398 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
400 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
404 gtk_spin_button_editable_init (GtkEditableInterface *iface)
406 iface->insert_text = gtk_spin_button_insert_text;
410 gtk_spin_button_set_property (GObject *object,
415 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
416 GtkSpinButtonPrivate *priv = spin_button->priv;
420 GtkAdjustment *adjustment;
422 case PROP_ADJUSTMENT:
423 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
425 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
426 gtk_spin_button_set_adjustment (spin_button, adjustment);
428 case PROP_CLIMB_RATE:
429 gtk_spin_button_configure (spin_button,
431 g_value_get_double (value),
435 gtk_spin_button_configure (spin_button,
438 g_value_get_uint (value));
440 case PROP_SNAP_TO_TICKS:
441 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
444 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
447 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
449 case PROP_UPDATE_POLICY:
450 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
453 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
456 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
462 gtk_spin_button_get_property (GObject *object,
467 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
468 GtkSpinButtonPrivate *priv = spin_button->priv;
472 case PROP_ADJUSTMENT:
473 g_value_set_object (value, priv->adjustment);
475 case PROP_CLIMB_RATE:
476 g_value_set_double (value, priv->climb_rate);
479 g_value_set_uint (value, priv->digits);
481 case PROP_SNAP_TO_TICKS:
482 g_value_set_boolean (value, priv->snap_to_ticks);
485 g_value_set_boolean (value, priv->numeric);
488 g_value_set_boolean (value, priv->wrap);
490 case PROP_UPDATE_POLICY:
491 g_value_set_enum (value, priv->update_policy);
494 g_value_set_double (value, priv->adjustment->value);
497 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
503 gtk_spin_button_init (GtkSpinButton *spin_button)
505 GtkSpinButtonPrivate *priv;
507 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
508 GTK_TYPE_SPIN_BUTTON,
509 GtkSpinButtonPrivate);
510 priv = spin_button->priv;
512 priv->adjustment = NULL;
515 priv->climb_rate = 0.0;
516 priv->timer_step = 0.0;
517 priv->update_policy = GTK_UPDATE_ALWAYS;
518 priv->in_child = NO_ARROW;
519 priv->click_child = NO_ARROW;
521 priv->need_timer = FALSE;
522 priv->timer_calls = 0;
524 priv->numeric = FALSE;
526 priv->snap_to_ticks = FALSE;
528 gtk_spin_button_set_adjustment (spin_button,
529 gtk_adjustment_new (0, 0, 0, 0, 0, 0));
533 gtk_spin_button_finalize (GObject *object)
535 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
537 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
541 gtk_spin_button_destroy (GtkWidget *widget)
543 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
545 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->destroy (widget);
549 gtk_spin_button_map (GtkWidget *widget)
551 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
552 GtkSpinButtonPrivate *priv = spin_button->priv;
554 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
556 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
557 gdk_window_show (priv->panel);
562 gtk_spin_button_unmap (GtkWidget *widget)
564 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
565 GtkSpinButtonPrivate *priv = spin_button->priv;
567 if (gtk_widget_get_mapped (widget))
569 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
571 gdk_window_hide (priv->panel);
572 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
577 gtk_spin_button_realize (GtkWidget *widget)
579 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
580 GtkSpinButtonPrivate *priv = spin_button->priv;
581 GtkAllocation allocation;
582 GtkRequisition requisition;
584 GdkWindowAttr attributes;
585 gint attributes_mask;
589 style = gtk_widget_get_style (widget);
591 arrow_size = spin_button_get_arrow_size (spin_button);
593 gtk_widget_get_preferred_size (widget, &requisition, NULL);
594 gtk_widget_get_allocation (widget, &allocation);
596 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
597 GDK_KEY_RELEASE_MASK);
598 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
600 attributes.window_type = GDK_WINDOW_CHILD;
601 attributes.wclass = GDK_INPUT_OUTPUT;
602 attributes.visual = gtk_widget_get_visual (widget);
603 attributes.event_mask = gtk_widget_get_events (widget);
604 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
605 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
606 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
608 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
610 attributes.x = allocation.width - arrow_size - 2 * style->xthickness;
611 attributes.y = (allocation.height - requisition.height) / 2;
612 attributes.width = arrow_size + 2 * style->xthickness;
613 attributes.height = requisition.height;
615 priv->panel = gdk_window_new (gtk_widget_get_window (widget),
616 &attributes, attributes_mask);
617 gdk_window_set_user_data (priv->panel, widget);
619 gtk_style_set_background (style,
620 priv->panel, GTK_STATE_NORMAL);
623 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
624 if (return_val == FALSE)
625 gtk_spin_button_default_output (spin_button);
627 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
631 gtk_spin_button_unrealize (GtkWidget *widget)
633 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
634 GtkSpinButtonPrivate *priv = spin->priv;
636 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
640 gdk_window_set_user_data (priv->panel, NULL);
641 gdk_window_destroy (priv->panel);
647 compute_double_length (double val, int digits)
653 if (fabs (val) > 1.0)
654 a = floor (log10 (fabs (val))) + 1;
666 return a + digits + extra;
670 gtk_spin_button_size_request (GtkWidget *widget,
671 GtkRequisition *requisition)
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)->size_request (widget, requisition);
685 if (entry->width_chars < 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 */
717 width = MIN_SPIN_BUTTON_WIDTH;
718 max_string_len = MAX (10, compute_double_length (1e9 * priv->adjustment->step_increment,
721 string_len = compute_double_length (priv->adjustment->upper,
723 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
724 width = MAX (width, w);
725 string_len = compute_double_length (priv->adjustment->lower,
727 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
728 width = MAX (width, w);
730 _gtk_entry_get_borders (entry, &xborder, &yborder);
731 _gtk_entry_effective_inner_border (entry, &inner_border);
733 requisition->width = width + xborder * 2 + inner_border.left + inner_border.right;
736 requisition->width += arrow_size + 2 * style->xthickness;
740 gtk_spin_button_size_allocate (GtkWidget *widget,
741 GtkAllocation *allocation)
743 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
744 GtkSpinButtonPrivate *priv = spin->priv;
745 GtkAllocation panel_allocation;
746 GtkRequisition requisition;
750 arrow_size = spin_button_get_arrow_size (spin);
751 panel_width = arrow_size + 2 * gtk_widget_get_style (widget)->xthickness;
753 gtk_widget_get_preferred_size (widget, &requisition, NULL);
755 gtk_widget_set_allocation (widget, allocation);
757 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
758 panel_allocation.x = 0;
760 panel_allocation.x = allocation->width - panel_width;
762 panel_allocation.width = panel_width;
763 panel_allocation.height = MIN (requisition.height, allocation->height);
765 panel_allocation.y = 0;
767 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
769 if (gtk_widget_get_realized (widget))
771 gdk_window_move_resize (priv->panel,
774 panel_allocation.width,
775 panel_allocation.height);
778 gtk_widget_queue_draw (GTK_WIDGET (spin));
782 gtk_spin_button_draw (GtkWidget *widget,
785 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
786 GtkSpinButtonPrivate *priv = spin->priv;
788 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
790 if (gtk_cairo_should_draw_window (cr, priv->panel))
792 GtkShadowType shadow_type;
794 shadow_type = spin_button_get_shadow_type (spin);
796 gtk_cairo_transform_to_window (cr, widget, priv->panel);
798 if (shadow_type != GTK_SHADOW_NONE)
802 state = gtk_widget_has_focus (widget) ?
803 GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
805 gtk_paint_box (gtk_widget_get_style (widget), cr,
807 widget, "spinbutton",
809 gdk_window_get_width (priv->panel),
810 gdk_window_get_height (priv->panel));
813 gtk_spin_button_draw_arrow (spin, cr, GTK_ARROW_UP);
814 gtk_spin_button_draw_arrow (spin, cr, GTK_ARROW_DOWN);
821 spin_button_at_limit (GtkSpinButton *spin_button,
824 GtkSpinButtonPrivate *priv = spin_button->priv;
825 GtkArrowType effective_arrow;
830 if (priv->adjustment->step_increment > 0)
831 effective_arrow = arrow;
833 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
835 if (effective_arrow == GTK_ARROW_UP &&
836 (priv->adjustment->upper - priv->adjustment->value <= EPSILON))
839 if (effective_arrow == GTK_ARROW_DOWN &&
840 (priv->adjustment->value - priv->adjustment->lower <= EPSILON))
847 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
849 GtkArrowType arrow_type)
851 GtkSpinButtonPrivate *priv;
852 GtkRequisition requisition;
853 GtkStateType state_type;
854 GtkShadowType shadow_type;
863 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
865 priv = spin_button->priv;
866 widget = GTK_WIDGET (spin_button);
868 style = gtk_widget_get_style (widget);
869 gtk_widget_get_preferred_size (widget, &requisition, NULL);
871 width = spin_button_get_arrow_size (spin_button) + 2 * style->xthickness;
873 if (arrow_type == GTK_ARROW_UP)
878 height = requisition.height / 2;
883 y = requisition.height / 2;
885 height = (requisition.height + 1) / 2;
888 if (spin_button_at_limit (spin_button, arrow_type))
890 shadow_type = GTK_SHADOW_OUT;
891 state_type = GTK_STATE_INSENSITIVE;
895 if (priv->click_child == arrow_type)
897 state_type = GTK_STATE_ACTIVE;
898 shadow_type = GTK_SHADOW_IN;
902 if (priv->in_child == arrow_type &&
903 priv->click_child == NO_ARROW)
905 state_type = GTK_STATE_PRELIGHT;
909 state_type = gtk_widget_get_state (widget);
912 shadow_type = GTK_SHADOW_OUT;
916 gtk_paint_box (style, cr,
917 state_type, shadow_type,
919 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
920 x, y, width, height);
922 height = requisition.height;
924 if (arrow_type == GTK_ARROW_DOWN)
927 height = height - y - 2;
932 height = height / 2 - 2;
937 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
943 w -= w % 2 - 1; /* force odd */
946 x += (width - w) / 2;
947 y += (height - h) / 2;
952 gtk_paint_arrow (style, cr,
953 state_type, shadow_type,
954 widget, "spinbutton",
956 x, y, width, height);
960 gtk_spin_button_enter_notify (GtkWidget *widget,
961 GdkEventCrossing *event)
963 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
964 GtkSpinButtonPrivate *priv = spin->priv;
965 GtkRequisition requisition;
967 if (event->window == priv->panel)
973 device = gdk_event_get_device ((GdkEvent *) event);
974 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
976 gtk_widget_get_preferred_size (widget, &requisition, NULL);
978 if (y <= requisition.height / 2)
979 priv->in_child = GTK_ARROW_UP;
981 priv->in_child = GTK_ARROW_DOWN;
983 gtk_widget_queue_draw (GTK_WIDGET (spin));
986 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event)
987 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
993 gtk_spin_button_leave_notify (GtkWidget *widget,
994 GdkEventCrossing *event)
996 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
997 GtkSpinButtonPrivate *priv = spin->priv;
999 priv->in_child = NO_ARROW;
1000 gtk_widget_queue_draw (GTK_WIDGET (spin));
1002 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event)
1003 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1009 gtk_spin_button_focus_out (GtkWidget *widget,
1010 GdkEventFocus *event)
1012 if (GTK_ENTRY (widget)->editable)
1013 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1015 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1019 gtk_spin_button_grab_notify (GtkWidget *widget,
1020 gboolean was_grabbed)
1022 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1026 gtk_spin_button_stop_spinning (spin);
1027 gtk_widget_queue_draw (GTK_WIDGET (spin));
1032 gtk_spin_button_state_changed (GtkWidget *widget,
1033 GtkStateType previous_state)
1035 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1037 if (!gtk_widget_is_sensitive (widget))
1039 gtk_spin_button_stop_spinning (spin);
1040 gtk_widget_queue_draw (GTK_WIDGET (spin));
1045 gtk_spin_button_style_set (GtkWidget *widget,
1046 GtkStyle *previous_style)
1048 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1049 GtkSpinButtonPrivate *priv = spin->priv;
1051 if (previous_style && gtk_widget_get_realized (widget))
1052 gtk_style_set_background (gtk_widget_get_style (widget),
1053 priv->panel, GTK_STATE_NORMAL);
1055 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_set (widget, previous_style);
1060 gtk_spin_button_scroll (GtkWidget *widget,
1061 GdkEventScroll *event)
1063 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1064 GtkSpinButtonPrivate *priv = spin->priv;
1066 if (event->direction == GDK_SCROLL_UP)
1068 if (!gtk_widget_has_focus (widget))
1069 gtk_widget_grab_focus (widget);
1070 gtk_spin_button_real_spin (spin, priv->adjustment->step_increment);
1072 else if (event->direction == GDK_SCROLL_DOWN)
1074 if (!gtk_widget_has_focus (widget))
1075 gtk_widget_grab_focus (widget);
1076 gtk_spin_button_real_spin (spin, -priv->adjustment->step_increment);
1085 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1087 GtkSpinButtonPrivate *priv = spin->priv;
1091 g_source_remove (priv->timer);
1093 priv->timer_calls = 0;
1094 priv->need_timer = FALSE;
1099 priv->timer_step = priv->adjustment->step_increment;
1100 priv->timer_calls = 0;
1102 priv->click_child = NO_ARROW;
1107 start_spinning (GtkSpinButton *spin,
1108 GtkArrowType click_child,
1111 GtkSpinButtonPrivate *priv;
1113 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1117 priv->click_child = click_child;
1121 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1124 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1126 priv->timer_step = step;
1127 priv->need_timer = TRUE;
1128 priv->timer = gdk_threads_add_timeout (timeout,
1129 (GSourceFunc) gtk_spin_button_timer,
1132 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1134 gtk_widget_queue_draw (GTK_WIDGET (spin));
1138 gtk_spin_button_button_press (GtkWidget *widget,
1139 GdkEventButton *event)
1141 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1142 GtkSpinButtonPrivate *priv = spin->priv;
1146 if (event->window == priv->panel)
1148 GtkRequisition requisition;
1150 if (!gtk_widget_has_focus (widget))
1151 gtk_widget_grab_focus (widget);
1152 priv->button = event->button;
1154 if (GTK_ENTRY (widget)->editable)
1155 gtk_spin_button_update (spin);
1157 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1159 if (event->y <= requisition.height / 2)
1161 if (event->button == 1)
1162 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->step_increment);
1163 else if (event->button == 2)
1164 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->page_increment);
1166 priv->click_child = GTK_ARROW_UP;
1170 if (event->button == 1)
1171 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->step_increment);
1172 else if (event->button == 2)
1173 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->page_increment);
1175 priv->click_child = GTK_ARROW_DOWN;
1180 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1186 gtk_spin_button_button_release (GtkWidget *widget,
1187 GdkEventButton *event)
1189 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1190 GtkSpinButtonPrivate *priv = spin->priv;
1193 arrow_size = spin_button_get_arrow_size (spin);
1195 if (event->button == priv->button)
1197 int click_child = priv->click_child;
1199 gtk_spin_button_stop_spinning (spin);
1201 if (event->button == 3)
1203 GtkRequisition requisition;
1205 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1207 if (event->y >= 0 && event->x >= 0 &&
1208 event->y <= requisition.height &&
1209 event->x <= arrow_size + 2 * gtk_widget_get_style (widget)->xthickness)
1211 if (click_child == GTK_ARROW_UP &&
1212 event->y <= requisition.height / 2)
1216 diff = priv->adjustment->upper - priv->adjustment->value;
1218 gtk_spin_button_real_spin (spin, diff);
1220 else if (click_child == GTK_ARROW_DOWN &&
1221 event->y > requisition.height / 2)
1225 diff = priv->adjustment->value - priv->adjustment->lower;
1227 gtk_spin_button_real_spin (spin, -diff);
1231 gtk_widget_queue_draw (GTK_WIDGET (spin));
1236 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1240 gtk_spin_button_motion_notify (GtkWidget *widget,
1241 GdkEventMotion *event)
1243 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1244 GtkSpinButtonPrivate *priv = spin->priv;
1249 if (event->window == priv->panel)
1251 GtkRequisition requisition;
1254 gdk_event_request_motions (event);
1256 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1258 if (y <= requisition.height / 2 &&
1259 priv->in_child == GTK_ARROW_DOWN)
1261 priv->in_child = GTK_ARROW_UP;
1262 gtk_widget_queue_draw (GTK_WIDGET (spin));
1264 else if (y > requisition.height / 2 &&
1265 priv->in_child == GTK_ARROW_UP)
1267 priv->in_child = GTK_ARROW_DOWN;
1268 gtk_widget_queue_draw (GTK_WIDGET (spin));
1274 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1278 gtk_spin_button_timer (GtkSpinButton *spin_button)
1280 GtkSpinButtonPrivate *priv = spin_button->priv;
1281 gboolean retval = FALSE;
1285 if (priv->click_child == GTK_ARROW_UP)
1286 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1288 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1290 if (priv->need_timer)
1292 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1295 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1297 priv->need_timer = FALSE;
1298 priv->timer = gdk_threads_add_timeout (timeout,
1299 (GSourceFunc) gtk_spin_button_timer,
1300 (gpointer) spin_button);
1304 if (priv->climb_rate > 0.0 && priv->timer_step
1305 < priv->adjustment->page_increment)
1307 if (priv->timer_calls < MAX_TIMER_CALLS)
1308 priv->timer_calls++;
1311 priv->timer_calls = 0;
1312 priv->timer_step += priv->climb_rate;
1323 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1324 GtkSpinButton *spin_button)
1326 gboolean return_val;
1328 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1331 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1332 if (return_val == FALSE)
1333 gtk_spin_button_default_output (spin_button);
1335 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1337 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1339 g_object_notify (G_OBJECT (spin_button), "value");
1343 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1344 GtkScrollType scroll)
1346 GtkSpinButtonPrivate *priv = spin->priv;
1349 /* When the key binding is activated, there may be an outstanding
1350 * value, so we first have to commit what is currently written in
1351 * the spin buttons text entry. See #106574
1353 gtk_spin_button_update (spin);
1355 old_value = priv->adjustment->value;
1357 /* We don't test whether the entry is editable, since
1358 * this key binding conceptually corresponds to changing
1359 * the value with the buttons using the mouse, which
1360 * we allow for non-editable spin buttons.
1364 case GTK_SCROLL_STEP_BACKWARD:
1365 case GTK_SCROLL_STEP_DOWN:
1366 case GTK_SCROLL_STEP_LEFT:
1367 gtk_spin_button_real_spin (spin, -priv->timer_step);
1369 if (priv->climb_rate > 0.0 && priv->timer_step
1370 < priv->adjustment->page_increment)
1372 if (priv->timer_calls < MAX_TIMER_CALLS)
1373 priv->timer_calls++;
1376 priv->timer_calls = 0;
1377 priv->timer_step += priv->climb_rate;
1382 case GTK_SCROLL_STEP_FORWARD:
1383 case GTK_SCROLL_STEP_UP:
1384 case GTK_SCROLL_STEP_RIGHT:
1385 gtk_spin_button_real_spin (spin, priv->timer_step);
1387 if (priv->climb_rate > 0.0 && priv->timer_step
1388 < priv->adjustment->page_increment)
1390 if (priv->timer_calls < MAX_TIMER_CALLS)
1391 priv->timer_calls++;
1394 priv->timer_calls = 0;
1395 priv->timer_step += priv->climb_rate;
1400 case GTK_SCROLL_PAGE_BACKWARD:
1401 case GTK_SCROLL_PAGE_DOWN:
1402 case GTK_SCROLL_PAGE_LEFT:
1403 gtk_spin_button_real_spin (spin, -priv->adjustment->page_increment);
1406 case GTK_SCROLL_PAGE_FORWARD:
1407 case GTK_SCROLL_PAGE_UP:
1408 case GTK_SCROLL_PAGE_RIGHT:
1409 gtk_spin_button_real_spin (spin, priv->adjustment->page_increment);
1412 case GTK_SCROLL_START:
1414 gdouble diff = priv->adjustment->value - priv->adjustment->lower;
1416 gtk_spin_button_real_spin (spin, -diff);
1420 case GTK_SCROLL_END:
1422 gdouble diff = priv->adjustment->upper - priv->adjustment->value;
1424 gtk_spin_button_real_spin (spin, diff);
1429 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1433 gtk_spin_button_update (spin);
1435 if (priv->adjustment->value == old_value)
1436 gtk_widget_error_bell (GTK_WIDGET (spin));
1440 gtk_spin_button_key_release (GtkWidget *widget,
1443 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1444 GtkSpinButtonPrivate *priv = spin->priv;
1446 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1447 priv->timer_step = priv->adjustment->step_increment;
1448 priv->timer_calls = 0;
1454 gtk_spin_button_snap (GtkSpinButton *spin_button,
1457 GtkSpinButtonPrivate *priv = spin_button->priv;
1461 inc = priv->adjustment->step_increment;
1465 tmp = (val - priv->adjustment->lower) / inc;
1466 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1467 val = priv->adjustment->lower + floor (tmp) * inc;
1469 val = priv->adjustment->lower + ceil (tmp) * inc;
1471 gtk_spin_button_set_value (spin_button, val);
1475 gtk_spin_button_activate (GtkEntry *entry)
1477 if (entry->editable)
1478 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1480 /* Chain up so that entry->activates_default is honored */
1481 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1485 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1494 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1496 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1497 panel_width = arrow_size + 2 * gtk_widget_get_style (GTK_WIDGET (entry))->xthickness;
1500 *width -= panel_width;
1502 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1507 gtk_spin_button_insert_text (GtkEditable *editable,
1508 const gchar *new_text,
1509 gint new_text_length,
1512 GtkEntry *entry = GTK_ENTRY (editable);
1513 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1514 GtkSpinButtonPrivate *priv = spin->priv;
1515 GtkEditableInterface *parent_editable_iface;
1517 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1529 const gchar *entry_text;
1531 entry_length = gtk_entry_get_text_length (entry);
1532 entry_text = gtk_entry_get_text (entry);
1536 if (*(lc->negative_sign))
1537 neg_sign = *(lc->negative_sign);
1541 if (*(lc->positive_sign))
1542 pos_sign = *(lc->positive_sign);
1547 /* Workaround for bug caused by some Windows application messing
1548 * up the positive sign of the current locale, more specifically
1549 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1550 * See bug #330743 and for instance
1551 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1553 * I don't know if the positive sign always gets bogusly set to
1554 * a digit when the above Registry value is corrupted as
1555 * described. (In my test case, it got set to "8", and in the
1556 * bug report above it presumably was set ot "0".) Probably it
1557 * might get set to almost anything? So how to distinguish a
1558 * bogus value from some correct one for some locale? That is
1559 * probably hard, but at least we should filter out the
1562 if (pos_sign >= '0' && pos_sign <= '9')
1566 for (sign=0, i=0; i<entry_length; i++)
1567 if ((entry_text[i] == neg_sign) ||
1568 (entry_text[i] == pos_sign))
1574 if (sign && !(*position))
1577 for (dotpos=-1, i=0; i<entry_length; i++)
1578 if (entry_text[i] == *(lc->decimal_point))
1584 if (dotpos > -1 && *position > dotpos &&
1585 (gint)priv->digits - entry_length
1586 + dotpos - new_text_length + 1 < 0)
1589 for (i = 0; i < new_text_length; i++)
1591 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1593 if (sign || (*position) || i)
1597 else if (new_text[i] == *(lc->decimal_point))
1599 if (!priv->digits || dotpos > -1 ||
1600 (new_text_length - 1 - i + entry_length
1601 - *position > (gint)priv->digits))
1603 dotpos = *position + i;
1605 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1610 parent_editable_iface->insert_text (editable, new_text,
1611 new_text_length, position);
1615 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1618 GtkSpinButtonPrivate *priv = spin_button->priv;
1620 gdouble new_value = 0.0;
1621 gboolean wrapped = FALSE;
1623 adj = priv->adjustment;
1625 new_value = adj->value + increment;
1631 if (fabs (adj->value - adj->upper) < EPSILON)
1633 new_value = adj->lower;
1636 else if (new_value > adj->upper)
1637 new_value = adj->upper;
1640 new_value = MIN (new_value, adj->upper);
1642 else if (increment < 0)
1646 if (fabs (adj->value - adj->lower) < EPSILON)
1648 new_value = adj->upper;
1651 else if (new_value < adj->lower)
1652 new_value = adj->lower;
1655 new_value = MAX (new_value, adj->lower);
1658 if (fabs (new_value - adj->value) > EPSILON)
1659 gtk_adjustment_set_value (adj, new_value);
1662 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1664 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1668 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1673 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1675 return GTK_INPUT_ERROR;
1681 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1683 GtkSpinButtonPrivate *priv = spin_button->priv;
1685 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, priv->adjustment->value);
1687 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1688 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1694 /***********************************************************
1695 ***********************************************************
1696 *** Public interface ***
1697 ***********************************************************
1698 ***********************************************************/
1702 * gtk_spin_button_configure:
1703 * @spin_button: a #GtkSpinButton
1704 * @adjustment: (allow-none): a #GtkAdjustment.
1705 * @climb_rate: the new climb rate.
1706 * @digits: the number of decimal places to display in the spin button.
1708 * Changes the properties of an existing spin button. The adjustment, climb rate,
1709 * and number of decimal places are all changed accordingly, after this function call.
1712 gtk_spin_button_configure (GtkSpinButton *spin_button,
1713 GtkAdjustment *adjustment,
1717 GtkSpinButtonPrivate *priv;
1719 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1721 priv = spin_button->priv;
1724 gtk_spin_button_set_adjustment (spin_button, adjustment);
1726 adjustment = priv->adjustment;
1728 g_object_freeze_notify (G_OBJECT (spin_button));
1729 if (priv->digits != digits)
1731 priv->digits = digits;
1732 g_object_notify (G_OBJECT (spin_button), "digits");
1735 if (priv->climb_rate != climb_rate)
1737 priv->climb_rate = climb_rate;
1738 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1740 g_object_thaw_notify (G_OBJECT (spin_button));
1742 gtk_adjustment_value_changed (adjustment);
1746 gtk_spin_button_new (GtkAdjustment *adjustment,
1750 GtkSpinButton *spin;
1753 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1755 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1757 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1759 return GTK_WIDGET (spin);
1763 * gtk_spin_button_new_with_range:
1764 * @min: Minimum allowable value
1765 * @max: Maximum allowable value
1766 * @step: Increment added or subtracted by spinning the widget
1768 * This is a convenience constructor that allows creation of a numeric
1769 * #GtkSpinButton without manually creating an adjustment. The value is
1770 * initially set to the minimum value and a page increment of 10 * @step
1771 * is the default. The precision of the spin button is equivalent to the
1772 * precision of @step.
1774 * Note that the way in which the precision is derived works best if @step
1775 * is a power of ten. If the resulting precision is not suitable for your
1776 * needs, use gtk_spin_button_set_digits() to correct it.
1778 * Return value: The new spin button as a #GtkWidget.
1781 gtk_spin_button_new_with_range (gdouble min,
1786 GtkSpinButton *spin;
1789 g_return_val_if_fail (min <= max, NULL);
1790 g_return_val_if_fail (step != 0.0, NULL);
1792 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1794 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1796 if (fabs (step) >= 1.0 || step == 0.0)
1799 digits = abs ((gint) floor (log10 (fabs (step))));
1800 if (digits > MAX_DIGITS)
1801 digits = MAX_DIGITS;
1804 gtk_spin_button_configure (spin, adj, step, digits);
1806 gtk_spin_button_set_numeric (spin, TRUE);
1808 return GTK_WIDGET (spin);
1811 /* Callback used when the spin button's adjustment changes. We need to redraw
1812 * the arrows when the adjustment's range changes, and reevaluate our size request.
1815 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1817 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1818 GtkSpinButtonPrivate *priv = spin_button->priv;
1820 priv->timer_step = priv->adjustment->step_increment;
1821 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1825 * gtk_spin_button_set_adjustment:
1826 * @spin_button: a #GtkSpinButton
1827 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1829 * Replaces the #GtkAdjustment associated with @spin_button.
1832 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1833 GtkAdjustment *adjustment)
1835 GtkSpinButtonPrivate *priv;
1837 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1839 priv = spin_button->priv;
1841 if (priv->adjustment != adjustment)
1843 if (priv->adjustment)
1845 g_signal_handlers_disconnect_by_func (priv->adjustment,
1846 gtk_spin_button_value_changed,
1848 g_signal_handlers_disconnect_by_func (priv->adjustment,
1849 adjustment_changed_cb,
1851 g_object_unref (priv->adjustment);
1853 priv->adjustment = adjustment;
1856 g_object_ref_sink (adjustment);
1857 g_signal_connect (adjustment, "value-changed",
1858 G_CALLBACK (gtk_spin_button_value_changed),
1860 g_signal_connect (adjustment, "changed",
1861 G_CALLBACK (adjustment_changed_cb),
1863 priv->timer_step = priv->adjustment->step_increment;
1866 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1869 g_object_notify (G_OBJECT (spin_button), "adjustment");
1873 * gtk_spin_button_get_adjustment:
1874 * @spin_button: a #GtkSpinButton
1876 * Get the adjustment associated with a #GtkSpinButton
1878 * Return value: (transfer none): the #GtkAdjustment of @spin_button
1881 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1883 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1885 return spin_button->priv->adjustment;
1889 * gtk_spin_button_set_digits:
1890 * @spin_button: a #GtkSpinButton
1891 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1893 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1897 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1900 GtkSpinButtonPrivate *priv;
1902 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1904 priv = spin_button->priv;
1906 if (priv->digits != digits)
1908 priv->digits = digits;
1909 gtk_spin_button_value_changed (priv->adjustment, spin_button);
1910 g_object_notify (G_OBJECT (spin_button), "digits");
1912 /* since lower/upper may have changed */
1913 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1918 * gtk_spin_button_get_digits:
1919 * @spin_button: a #GtkSpinButton
1921 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1923 * Returns: the current precision
1926 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1928 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1930 return spin_button->priv->digits;
1934 * gtk_spin_button_set_increments:
1935 * @spin_button: a #GtkSpinButton
1936 * @step: increment applied for a button 1 press.
1937 * @page: increment applied for a button 2 press.
1939 * Sets the step and page increments for spin_button. This affects how
1940 * quickly the value changes when the spin button's arrows are activated.
1943 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1947 GtkSpinButtonPrivate *priv;
1949 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1951 priv = spin_button->priv;
1953 priv->adjustment->step_increment = step;
1954 priv->adjustment->page_increment = page;
1958 * gtk_spin_button_get_increments:
1959 * @spin_button: a #GtkSpinButton
1960 * @step: (allow-none): location to store step increment, or %NULL
1961 * @page: (allow-none): location to store page increment, or %NULL
1963 * Gets the current step and page the increments used by @spin_button. See
1964 * gtk_spin_button_set_increments().
1967 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1971 GtkSpinButtonPrivate *priv;
1973 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1975 priv = spin_button->priv;
1978 *step = priv->adjustment->step_increment;
1980 *page = priv->adjustment->page_increment;
1984 * gtk_spin_button_set_range:
1985 * @spin_button: a #GtkSpinButton
1986 * @min: minimum allowable value
1987 * @max: maximum allowable value
1989 * Sets the minimum and maximum allowable values for @spin_button
1992 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1996 GtkSpinButtonPrivate *priv;
1999 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2001 priv = spin_button->priv;
2003 priv->adjustment->lower = min;
2004 priv->adjustment->upper = max;
2006 value = CLAMP (priv->adjustment->value,
2007 priv->adjustment->lower,
2008 (priv->adjustment->upper - priv->adjustment->page_size));
2010 if (value != priv->adjustment->value)
2011 gtk_spin_button_set_value (spin_button, value);
2013 gtk_adjustment_changed (priv->adjustment);
2017 * gtk_spin_button_get_range:
2018 * @spin_button: a #GtkSpinButton
2019 * @min: (allow-none): location to store minimum allowed value, or %NULL
2020 * @max: (allow-none): location to store maximum allowed value, or %NULL
2022 * Gets the range allowed for @spin_button. See
2023 * gtk_spin_button_set_range().
2026 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2030 GtkSpinButtonPrivate *priv;
2032 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2034 priv = spin_button->priv;
2037 *min = priv->adjustment->lower;
2039 *max = priv->adjustment->upper;
2043 * gtk_spin_button_get_value:
2044 * @spin_button: a #GtkSpinButton
2046 * Get the value in the @spin_button.
2048 * Return value: the value of @spin_button
2051 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2053 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2055 return spin_button->priv->adjustment->value;
2059 * gtk_spin_button_get_value_as_int:
2060 * @spin_button: a #GtkSpinButton
2062 * Get the value @spin_button represented as an integer.
2064 * Return value: the value of @spin_button
2067 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2069 GtkSpinButtonPrivate *priv;
2072 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2074 priv = spin_button->priv;
2076 val = priv->adjustment->value;
2077 if (val - floor (val) < ceil (val) - val)
2084 * gtk_spin_button_set_value:
2085 * @spin_button: a #GtkSpinButton
2086 * @value: the new value
2088 * Set the value of @spin_button.
2091 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2094 GtkSpinButtonPrivate *priv;
2096 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2098 priv = spin_button->priv;
2100 if (fabs (value - priv->adjustment->value) > EPSILON)
2101 gtk_adjustment_set_value (priv->adjustment, value);
2104 gint return_val = FALSE;
2105 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2106 if (return_val == FALSE)
2107 gtk_spin_button_default_output (spin_button);
2112 * gtk_spin_button_set_update_policy:
2113 * @spin_button: a #GtkSpinButton
2114 * @policy: a #GtkSpinButtonUpdatePolicy value
2116 * Sets the update behavior of a spin button. This determines whether the
2117 * spin button is always updated or only when a valid value is set.
2120 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2121 GtkSpinButtonUpdatePolicy policy)
2123 GtkSpinButtonPrivate *priv;
2125 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2127 priv = spin_button->priv;
2129 if (priv->update_policy != policy)
2131 priv->update_policy = policy;
2132 g_object_notify (G_OBJECT (spin_button), "update-policy");
2137 * gtk_spin_button_get_update_policy:
2138 * @spin_button: a #GtkSpinButton
2140 * Gets the update behavior of a spin button. See
2141 * gtk_spin_button_set_update_policy().
2143 * Return value: the current update policy
2145 GtkSpinButtonUpdatePolicy
2146 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2148 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2150 return spin_button->priv->update_policy;
2154 * gtk_spin_button_set_numeric:
2155 * @spin_button: a #GtkSpinButton
2156 * @numeric: flag indicating if only numeric entry is allowed.
2158 * Sets the flag that determines if non-numeric text can be typed into
2162 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2165 GtkSpinButtonPrivate *priv;
2167 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2169 priv = spin_button->priv;
2171 numeric = numeric != FALSE;
2173 if (priv->numeric != numeric)
2175 priv->numeric = numeric;
2176 g_object_notify (G_OBJECT (spin_button), "numeric");
2181 * gtk_spin_button_get_numeric:
2182 * @spin_button: a #GtkSpinButton
2184 * Returns whether non-numeric text can be typed into the spin button.
2185 * See gtk_spin_button_set_numeric().
2187 * Return value: %TRUE if only numeric text can be entered
2190 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2192 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2194 return spin_button->priv->numeric;
2198 * gtk_spin_button_set_wrap:
2199 * @spin_button: a #GtkSpinButton
2200 * @wrap: a flag indicating if wrapping behavior is performed.
2202 * Sets the flag that determines if a spin button value wraps around to the
2203 * opposite limit when the upper or lower limit of the range is exceeded.
2206 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2209 GtkSpinButtonPrivate *priv;
2211 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2213 priv = spin_button->priv;
2215 wrap = wrap != FALSE;
2217 if (priv->wrap != wrap)
2219 priv->wrap = (wrap != 0);
2221 g_object_notify (G_OBJECT (spin_button), "wrap");
2226 * gtk_spin_button_get_wrap:
2227 * @spin_button: a #GtkSpinButton
2229 * Returns whether the spin button's value wraps around to the
2230 * opposite limit when the upper or lower limit of the range is
2231 * exceeded. See gtk_spin_button_set_wrap().
2233 * Return value: %TRUE if the spin button wraps around
2236 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2238 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2240 return spin_button->priv->wrap;
2244 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2250 style = gtk_widget_get_style (GTK_WIDGET (spin_button));
2251 size = pango_font_description_get_size (style->font_desc);
2252 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2254 return arrow_size - arrow_size % 2; /* force even */
2258 * spin_button_get_shadow_type:
2259 * @spin_button: a #GtkSpinButton
2261 * Convenience function to Get the shadow type from the underlying widget's
2264 * Return value: the #GtkShadowType
2267 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2269 GtkShadowType rc_shadow_type;
2271 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow-type", &rc_shadow_type, NULL);
2273 return rc_shadow_type;
2277 * gtk_spin_button_set_snap_to_ticks:
2278 * @spin_button: a #GtkSpinButton
2279 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2281 * Sets the policy as to whether values are corrected to the nearest step
2282 * increment when a spin button is activated after providing an invalid value.
2285 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2286 gboolean snap_to_ticks)
2288 GtkSpinButtonPrivate *priv;
2291 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2293 priv = spin_button->priv;
2295 new_val = (snap_to_ticks != 0);
2297 if (new_val != priv->snap_to_ticks)
2299 priv->snap_to_ticks = new_val;
2300 if (new_val && GTK_ENTRY (spin_button)->editable)
2301 gtk_spin_button_update (spin_button);
2303 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2308 * gtk_spin_button_get_snap_to_ticks:
2309 * @spin_button: a #GtkSpinButton
2311 * Returns whether the values are corrected to the nearest step. See
2312 * gtk_spin_button_set_snap_to_ticks().
2314 * Return value: %TRUE if values are snapped to the nearest step.
2317 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2319 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2321 return spin_button->priv->snap_to_ticks;
2325 * gtk_spin_button_spin:
2326 * @spin_button: a #GtkSpinButton
2327 * @direction: a #GtkSpinType indicating the direction to spin.
2328 * @increment: step increment to apply in the specified direction.
2330 * Increment or decrement a spin button's value in a specified direction
2331 * by a specified amount.
2334 gtk_spin_button_spin (GtkSpinButton *spin_button,
2335 GtkSpinType direction,
2338 GtkSpinButtonPrivate *priv;
2342 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2344 priv = spin_button->priv;
2346 adj = priv->adjustment;
2348 /* for compatibility with the 1.0.x version of this function */
2349 if (increment != 0 && increment != adj->step_increment &&
2350 (direction == GTK_SPIN_STEP_FORWARD ||
2351 direction == GTK_SPIN_STEP_BACKWARD))
2353 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2354 increment = -increment;
2355 direction = GTK_SPIN_USER_DEFINED;
2360 case GTK_SPIN_STEP_FORWARD:
2362 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2365 case GTK_SPIN_STEP_BACKWARD:
2367 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2370 case GTK_SPIN_PAGE_FORWARD:
2372 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2375 case GTK_SPIN_PAGE_BACKWARD:
2377 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2382 diff = adj->value - adj->lower;
2384 gtk_spin_button_real_spin (spin_button, -diff);
2389 diff = adj->upper - adj->value;
2391 gtk_spin_button_real_spin (spin_button, diff);
2394 case GTK_SPIN_USER_DEFINED:
2397 gtk_spin_button_real_spin (spin_button, increment);
2406 * gtk_spin_button_update:
2407 * @spin_button: a #GtkSpinButton
2409 * Manually force an update of the spin button.
2412 gtk_spin_button_update (GtkSpinButton *spin_button)
2414 GtkSpinButtonPrivate *priv;
2419 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2421 priv = spin_button->priv;
2424 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2425 if (return_val == FALSE)
2427 return_val = gtk_spin_button_default_input (spin_button, &val);
2428 error = (return_val == GTK_INPUT_ERROR);
2430 else if (return_val == GTK_INPUT_ERROR)
2433 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2435 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2437 if (val < priv->adjustment->lower)
2438 val = priv->adjustment->lower;
2439 else if (val > priv->adjustment->upper)
2440 val = priv->adjustment->upper;
2442 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2444 val < priv->adjustment->lower ||
2445 val > priv->adjustment->upper))
2447 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2451 if (priv->snap_to_ticks)
2452 gtk_spin_button_snap (spin_button, val);
2454 gtk_spin_button_set_value (spin_button, val);
2458 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2460 return spin_button->priv->panel;