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 (GtkObject *object);
110 static void gtk_spin_button_map (GtkWidget *widget);
111 static void gtk_spin_button_unmap (GtkWidget *widget);
112 static void gtk_spin_button_realize (GtkWidget *widget);
113 static void gtk_spin_button_unrealize (GtkWidget *widget);
114 static void gtk_spin_button_size_request (GtkWidget *widget,
115 GtkRequisition *requisition);
116 static void gtk_spin_button_size_allocate (GtkWidget *widget,
117 GtkAllocation *allocation);
118 static gint gtk_spin_button_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 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
191 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
192 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
193 GtkBindingSet *binding_set;
195 gobject_class->finalize = gtk_spin_button_finalize;
196 gobject_class->set_property = gtk_spin_button_set_property;
197 gobject_class->get_property = gtk_spin_button_get_property;
199 object_class->destroy = gtk_spin_button_destroy;
200 widget_class->map = gtk_spin_button_map;
201 widget_class->unmap = gtk_spin_button_unmap;
202 widget_class->realize = gtk_spin_button_realize;
203 widget_class->unrealize = gtk_spin_button_unrealize;
204 widget_class->size_request = gtk_spin_button_size_request;
205 widget_class->size_allocate = gtk_spin_button_size_allocate;
206 widget_class->draw = gtk_spin_button_draw;
207 widget_class->scroll_event = gtk_spin_button_scroll;
208 widget_class->button_press_event = gtk_spin_button_button_press;
209 widget_class->button_release_event = gtk_spin_button_button_release;
210 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
211 widget_class->key_release_event = gtk_spin_button_key_release;
212 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
213 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
214 widget_class->focus_out_event = gtk_spin_button_focus_out;
215 widget_class->grab_notify = gtk_spin_button_grab_notify;
216 widget_class->state_changed = gtk_spin_button_state_changed;
217 widget_class->style_set = gtk_spin_button_style_set;
219 entry_class->activate = gtk_spin_button_activate;
220 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
223 class->output = NULL;
224 class->change_value = gtk_spin_button_real_change_value;
226 g_object_class_install_property (gobject_class,
228 g_param_spec_object ("adjustment",
230 P_("The adjustment that holds the value of the spin button"),
232 GTK_PARAM_READWRITE));
234 g_object_class_install_property (gobject_class,
236 g_param_spec_double ("climb-rate",
238 P_("The acceleration rate when you hold down a button"),
242 GTK_PARAM_READWRITE));
244 g_object_class_install_property (gobject_class,
246 g_param_spec_uint ("digits",
248 P_("The number of decimal places to display"),
252 GTK_PARAM_READWRITE));
254 g_object_class_install_property (gobject_class,
256 g_param_spec_boolean ("snap-to-ticks",
258 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
260 GTK_PARAM_READWRITE));
262 g_object_class_install_property (gobject_class,
264 g_param_spec_boolean ("numeric",
266 P_("Whether non-numeric characters should be ignored"),
268 GTK_PARAM_READWRITE));
270 g_object_class_install_property (gobject_class,
272 g_param_spec_boolean ("wrap",
274 P_("Whether a spin button should wrap upon reaching its limits"),
276 GTK_PARAM_READWRITE));
278 g_object_class_install_property (gobject_class,
280 g_param_spec_enum ("update-policy",
282 P_("Whether the spin button should update always, or only when the value is legal"),
283 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
285 GTK_PARAM_READWRITE));
287 g_object_class_install_property (gobject_class,
289 g_param_spec_double ("value",
291 P_("Reads the current value, or sets a new value"),
295 GTK_PARAM_READWRITE));
297 gtk_widget_class_install_style_property_parser (widget_class,
298 g_param_spec_enum ("shadow-type",
300 P_("Style of bevel around the spin button"),
301 GTK_TYPE_SHADOW_TYPE,
304 gtk_rc_property_parse_enum);
305 spinbutton_signals[INPUT] =
306 g_signal_new (I_("input"),
307 G_TYPE_FROM_CLASS (gobject_class),
309 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
311 _gtk_marshal_INT__POINTER,
316 * GtkSpinButton::output:
317 * @spin_button: the object which received the signal
319 * The ::output signal can be used to change to formatting
320 * of the value that is displayed in the spin buttons entry.
322 * /* show leading zeros */
324 * on_output (GtkSpinButton *spin,
327 * GtkAdjustment *adj;
331 * adj = gtk_spin_button_get_adjustment (spin);
332 * value = (int)gtk_adjustment_get_value (adj);
333 * text = g_strdup_printf ("%02d", value);
334 * gtk_entry_set_text (GTK_ENTRY (spin), text);
341 * Returns: %TRUE if the value has been displayed.
343 spinbutton_signals[OUTPUT] =
344 g_signal_new (I_("output"),
345 G_TYPE_FROM_CLASS (gobject_class),
347 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
348 _gtk_boolean_handled_accumulator, NULL,
349 _gtk_marshal_BOOLEAN__VOID,
352 spinbutton_signals[VALUE_CHANGED] =
353 g_signal_new (I_("value-changed"),
354 G_TYPE_FROM_CLASS (gobject_class),
356 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
358 _gtk_marshal_VOID__VOID,
362 * GtkSpinButton::wrapped:
363 * @spinbutton: the object which received the signal
365 * The wrapped signal is emitted right after the spinbutton wraps
366 * from its maximum to minimum value or vice-versa.
370 spinbutton_signals[WRAPPED] =
371 g_signal_new (I_("wrapped"),
372 G_TYPE_FROM_CLASS (gobject_class),
374 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
376 _gtk_marshal_VOID__VOID,
380 spinbutton_signals[CHANGE_VALUE] =
381 g_signal_new (I_("change-value"),
382 G_TYPE_FROM_CLASS (gobject_class),
383 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
384 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
386 _gtk_marshal_VOID__ENUM,
388 GTK_TYPE_SCROLL_TYPE);
390 binding_set = gtk_binding_set_by_class (class);
392 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
393 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
394 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
395 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
396 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
397 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
398 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
399 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
401 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
405 gtk_spin_button_editable_init (GtkEditableInterface *iface)
407 iface->insert_text = gtk_spin_button_insert_text;
411 gtk_spin_button_set_property (GObject *object,
416 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
417 GtkSpinButtonPrivate *priv = spin_button->priv;
421 GtkAdjustment *adjustment;
423 case PROP_ADJUSTMENT:
424 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
426 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
427 gtk_spin_button_set_adjustment (spin_button, adjustment);
429 case PROP_CLIMB_RATE:
430 gtk_spin_button_configure (spin_button,
432 g_value_get_double (value),
436 gtk_spin_button_configure (spin_button,
439 g_value_get_uint (value));
441 case PROP_SNAP_TO_TICKS:
442 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
445 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
448 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
450 case PROP_UPDATE_POLICY:
451 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
454 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
457 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
463 gtk_spin_button_get_property (GObject *object,
468 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
469 GtkSpinButtonPrivate *priv = spin_button->priv;
473 case PROP_ADJUSTMENT:
474 g_value_set_object (value, priv->adjustment);
476 case PROP_CLIMB_RATE:
477 g_value_set_double (value, priv->climb_rate);
480 g_value_set_uint (value, priv->digits);
482 case PROP_SNAP_TO_TICKS:
483 g_value_set_boolean (value, priv->snap_to_ticks);
486 g_value_set_boolean (value, priv->numeric);
489 g_value_set_boolean (value, priv->wrap);
491 case PROP_UPDATE_POLICY:
492 g_value_set_enum (value, priv->update_policy);
495 g_value_set_double (value, priv->adjustment->value);
498 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
504 gtk_spin_button_init (GtkSpinButton *spin_button)
506 GtkSpinButtonPrivate *priv;
508 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
509 GTK_TYPE_SPIN_BUTTON,
510 GtkSpinButtonPrivate);
511 priv = spin_button->priv;
513 priv->adjustment = NULL;
516 priv->climb_rate = 0.0;
517 priv->timer_step = 0.0;
518 priv->update_policy = GTK_UPDATE_ALWAYS;
519 priv->in_child = NO_ARROW;
520 priv->click_child = NO_ARROW;
522 priv->need_timer = FALSE;
523 priv->timer_calls = 0;
525 priv->numeric = FALSE;
527 priv->snap_to_ticks = FALSE;
529 gtk_spin_button_set_adjustment (spin_button,
530 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
534 gtk_spin_button_finalize (GObject *object)
536 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
538 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
542 gtk_spin_button_destroy (GtkObject *object)
544 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
546 GTK_OBJECT_CLASS (gtk_spin_button_parent_class)->destroy (object);
550 gtk_spin_button_map (GtkWidget *widget)
552 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
553 GtkSpinButtonPrivate *priv = spin_button->priv;
555 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
557 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
558 gdk_window_show (priv->panel);
563 gtk_spin_button_unmap (GtkWidget *widget)
565 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
566 GtkSpinButtonPrivate *priv = spin_button->priv;
568 if (gtk_widget_get_mapped (widget))
570 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
572 gdk_window_hide (priv->panel);
573 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
578 gtk_spin_button_realize (GtkWidget *widget)
580 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
581 GtkSpinButtonPrivate *priv = spin_button->priv;
582 GtkAllocation allocation;
583 GtkRequisition requisition;
585 GdkWindowAttr attributes;
586 gint attributes_mask;
590 style = gtk_widget_get_style (widget);
592 arrow_size = spin_button_get_arrow_size (spin_button);
594 gtk_widget_get_preferred_size (widget, &requisition, NULL);
595 gtk_widget_get_allocation (widget, &allocation);
597 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
598 GDK_KEY_RELEASE_MASK);
599 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
601 attributes.window_type = GDK_WINDOW_CHILD;
602 attributes.wclass = GDK_INPUT_OUTPUT;
603 attributes.visual = gtk_widget_get_visual (widget);
604 attributes.event_mask = gtk_widget_get_events (widget);
605 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
606 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
607 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
609 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
611 attributes.x = allocation.width - arrow_size - 2 * style->xthickness;
612 attributes.y = (allocation.height - requisition.height) / 2;
613 attributes.width = arrow_size + 2 * style->xthickness;
614 attributes.height = requisition.height;
616 priv->panel = gdk_window_new (gtk_widget_get_window (widget),
617 &attributes, attributes_mask);
618 gdk_window_set_user_data (priv->panel, widget);
620 gtk_style_set_background (style,
621 priv->panel, GTK_STATE_NORMAL);
624 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
625 if (return_val == FALSE)
626 gtk_spin_button_default_output (spin_button);
628 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
632 gtk_spin_button_unrealize (GtkWidget *widget)
634 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
635 GtkSpinButtonPrivate *priv = spin->priv;
637 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
641 gdk_window_set_user_data (priv->panel, NULL);
642 gdk_window_destroy (priv->panel);
648 compute_double_length (double val, int digits)
654 if (fabs (val) > 1.0)
655 a = floor (log10 (fabs (val))) + 1;
667 return a + digits + extra;
671 gtk_spin_button_size_request (GtkWidget *widget,
672 GtkRequisition *requisition)
674 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
675 GtkSpinButtonPrivate *priv = spin_button->priv;
676 GtkEntry *entry = GTK_ENTRY (widget);
680 style = gtk_widget_get_style (widget);
682 arrow_size = spin_button_get_arrow_size (spin_button);
684 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_request (widget, requisition);
686 if (entry->width_chars < 0)
688 PangoContext *context;
689 PangoFontMetrics *metrics;
695 gboolean interior_focus;
697 gint xborder, yborder;
698 GtkBorder inner_border;
700 gtk_widget_style_get (widget,
701 "interior-focus", &interior_focus,
702 "focus-line-width", &focus_width,
705 context = gtk_widget_get_pango_context (widget);
706 metrics = pango_context_get_metrics (context,
708 pango_context_get_language (context));
710 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
711 digit_width = PANGO_SCALE *
712 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
714 pango_font_metrics_unref (metrics);
716 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
718 width = MIN_SPIN_BUTTON_WIDTH;
719 max_string_len = MAX (10, compute_double_length (1e9 * priv->adjustment->step_increment,
722 string_len = compute_double_length (priv->adjustment->upper,
724 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
725 width = MAX (width, w);
726 string_len = compute_double_length (priv->adjustment->lower,
728 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
729 width = MAX (width, w);
731 _gtk_entry_get_borders (entry, &xborder, &yborder);
732 _gtk_entry_effective_inner_border (entry, &inner_border);
734 requisition->width = width + xborder * 2 + inner_border.left + inner_border.right;
737 requisition->width += arrow_size + 2 * style->xthickness;
741 gtk_spin_button_size_allocate (GtkWidget *widget,
742 GtkAllocation *allocation)
744 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
745 GtkSpinButtonPrivate *priv = spin->priv;
746 GtkAllocation panel_allocation;
747 GtkRequisition requisition;
751 arrow_size = spin_button_get_arrow_size (spin);
752 panel_width = arrow_size + 2 * gtk_widget_get_style (widget)->xthickness;
754 gtk_widget_get_preferred_size (widget, &requisition, NULL);
756 gtk_widget_set_allocation (widget, allocation);
758 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
759 panel_allocation.x = 0;
761 panel_allocation.x = allocation->width - panel_width;
763 panel_allocation.width = panel_width;
764 panel_allocation.height = MIN (requisition.height, allocation->height);
766 panel_allocation.y = 0;
768 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
770 if (gtk_widget_get_realized (widget))
772 gdk_window_move_resize (priv->panel,
775 panel_allocation.width,
776 panel_allocation.height);
779 gtk_widget_queue_draw (GTK_WIDGET (spin));
783 gtk_spin_button_draw (GtkWidget *widget,
786 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
787 GtkSpinButtonPrivate *priv = spin->priv;
789 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
791 if (gtk_cairo_should_draw_window (cr, priv->panel))
793 GtkShadowType shadow_type;
795 shadow_type = spin_button_get_shadow_type (spin);
797 gtk_cairo_transform_to_window (cr, widget, priv->panel);
799 if (shadow_type != GTK_SHADOW_NONE)
803 state = gtk_widget_has_focus (widget) ?
804 GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
806 gtk_paint_box (gtk_widget_get_style (widget), cr,
808 widget, "spinbutton",
810 gdk_window_get_width (priv->panel),
811 gdk_window_get_height (priv->panel));
814 gtk_spin_button_draw_arrow (spin, cr, GTK_ARROW_UP);
815 gtk_spin_button_draw_arrow (spin, cr, GTK_ARROW_DOWN);
822 spin_button_at_limit (GtkSpinButton *spin_button,
825 GtkSpinButtonPrivate *priv = spin_button->priv;
826 GtkArrowType effective_arrow;
831 if (priv->adjustment->step_increment > 0)
832 effective_arrow = arrow;
834 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
836 if (effective_arrow == GTK_ARROW_UP &&
837 (priv->adjustment->upper - priv->adjustment->value <= EPSILON))
840 if (effective_arrow == GTK_ARROW_DOWN &&
841 (priv->adjustment->value - priv->adjustment->lower <= EPSILON))
848 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
850 GtkArrowType arrow_type)
852 GtkSpinButtonPrivate *priv;
853 GtkRequisition requisition;
854 GtkStateType state_type;
855 GtkShadowType shadow_type;
864 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
866 priv = spin_button->priv;
867 widget = GTK_WIDGET (spin_button);
869 style = gtk_widget_get_style (widget);
870 gtk_widget_get_preferred_size (widget, &requisition, NULL);
872 width = spin_button_get_arrow_size (spin_button) + 2 * style->xthickness;
874 if (arrow_type == GTK_ARROW_UP)
879 height = requisition.height / 2;
884 y = requisition.height / 2;
886 height = (requisition.height + 1) / 2;
889 if (spin_button_at_limit (spin_button, arrow_type))
891 shadow_type = GTK_SHADOW_OUT;
892 state_type = GTK_STATE_INSENSITIVE;
896 if (priv->click_child == arrow_type)
898 state_type = GTK_STATE_ACTIVE;
899 shadow_type = GTK_SHADOW_IN;
903 if (priv->in_child == arrow_type &&
904 priv->click_child == NO_ARROW)
906 state_type = GTK_STATE_PRELIGHT;
910 state_type = gtk_widget_get_state (widget);
913 shadow_type = GTK_SHADOW_OUT;
917 gtk_paint_box (style, cr,
918 state_type, shadow_type,
920 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
921 x, y, width, height);
923 height = requisition.height;
925 if (arrow_type == GTK_ARROW_DOWN)
928 height = height - y - 2;
933 height = height / 2 - 2;
938 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
944 w -= w % 2 - 1; /* force odd */
947 x += (width - w) / 2;
948 y += (height - h) / 2;
953 gtk_paint_arrow (style, cr,
954 state_type, shadow_type,
955 widget, "spinbutton",
957 x, y, width, height);
961 gtk_spin_button_enter_notify (GtkWidget *widget,
962 GdkEventCrossing *event)
964 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
965 GtkSpinButtonPrivate *priv = spin->priv;
966 GtkRequisition requisition;
968 if (event->window == priv->panel)
974 device = gdk_event_get_device ((GdkEvent *) event);
975 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
977 gtk_widget_get_preferred_size (widget, &requisition, NULL);
979 if (y <= requisition.height / 2)
980 priv->in_child = GTK_ARROW_UP;
982 priv->in_child = GTK_ARROW_DOWN;
984 gtk_widget_queue_draw (GTK_WIDGET (spin));
987 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event)
988 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
994 gtk_spin_button_leave_notify (GtkWidget *widget,
995 GdkEventCrossing *event)
997 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
998 GtkSpinButtonPrivate *priv = spin->priv;
1000 priv->in_child = NO_ARROW;
1001 gtk_widget_queue_draw (GTK_WIDGET (spin));
1003 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event)
1004 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1010 gtk_spin_button_focus_out (GtkWidget *widget,
1011 GdkEventFocus *event)
1013 if (GTK_ENTRY (widget)->editable)
1014 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1016 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1020 gtk_spin_button_grab_notify (GtkWidget *widget,
1021 gboolean was_grabbed)
1023 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1027 gtk_spin_button_stop_spinning (spin);
1028 gtk_widget_queue_draw (GTK_WIDGET (spin));
1033 gtk_spin_button_state_changed (GtkWidget *widget,
1034 GtkStateType previous_state)
1036 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1038 if (!gtk_widget_is_sensitive (widget))
1040 gtk_spin_button_stop_spinning (spin);
1041 gtk_widget_queue_draw (GTK_WIDGET (spin));
1046 gtk_spin_button_style_set (GtkWidget *widget,
1047 GtkStyle *previous_style)
1049 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1050 GtkSpinButtonPrivate *priv = spin->priv;
1052 if (previous_style && gtk_widget_get_realized (widget))
1053 gtk_style_set_background (gtk_widget_get_style (widget),
1054 priv->panel, GTK_STATE_NORMAL);
1056 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_set (widget, previous_style);
1061 gtk_spin_button_scroll (GtkWidget *widget,
1062 GdkEventScroll *event)
1064 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1065 GtkSpinButtonPrivate *priv = spin->priv;
1067 if (event->direction == GDK_SCROLL_UP)
1069 if (!gtk_widget_has_focus (widget))
1070 gtk_widget_grab_focus (widget);
1071 gtk_spin_button_real_spin (spin, priv->adjustment->step_increment);
1073 else if (event->direction == GDK_SCROLL_DOWN)
1075 if (!gtk_widget_has_focus (widget))
1076 gtk_widget_grab_focus (widget);
1077 gtk_spin_button_real_spin (spin, -priv->adjustment->step_increment);
1086 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1088 GtkSpinButtonPrivate *priv = spin->priv;
1092 g_source_remove (priv->timer);
1094 priv->timer_calls = 0;
1095 priv->need_timer = FALSE;
1100 priv->timer_step = priv->adjustment->step_increment;
1101 priv->timer_calls = 0;
1103 priv->click_child = NO_ARROW;
1108 start_spinning (GtkSpinButton *spin,
1109 GtkArrowType click_child,
1112 GtkSpinButtonPrivate *priv;
1114 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1118 priv->click_child = click_child;
1122 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1125 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1127 priv->timer_step = step;
1128 priv->need_timer = TRUE;
1129 priv->timer = gdk_threads_add_timeout (timeout,
1130 (GSourceFunc) gtk_spin_button_timer,
1133 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1135 gtk_widget_queue_draw (GTK_WIDGET (spin));
1139 gtk_spin_button_button_press (GtkWidget *widget,
1140 GdkEventButton *event)
1142 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1143 GtkSpinButtonPrivate *priv = spin->priv;
1147 if (event->window == priv->panel)
1149 GtkRequisition requisition;
1151 if (!gtk_widget_has_focus (widget))
1152 gtk_widget_grab_focus (widget);
1153 priv->button = event->button;
1155 if (GTK_ENTRY (widget)->editable)
1156 gtk_spin_button_update (spin);
1158 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1160 if (event->y <= requisition.height / 2)
1162 if (event->button == 1)
1163 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->step_increment);
1164 else if (event->button == 2)
1165 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->page_increment);
1167 priv->click_child = GTK_ARROW_UP;
1171 if (event->button == 1)
1172 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->step_increment);
1173 else if (event->button == 2)
1174 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->page_increment);
1176 priv->click_child = GTK_ARROW_DOWN;
1181 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1187 gtk_spin_button_button_release (GtkWidget *widget,
1188 GdkEventButton *event)
1190 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1191 GtkSpinButtonPrivate *priv = spin->priv;
1194 arrow_size = spin_button_get_arrow_size (spin);
1196 if (event->button == priv->button)
1198 int click_child = priv->click_child;
1200 gtk_spin_button_stop_spinning (spin);
1202 if (event->button == 3)
1204 GtkRequisition requisition;
1206 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1208 if (event->y >= 0 && event->x >= 0 &&
1209 event->y <= requisition.height &&
1210 event->x <= arrow_size + 2 * gtk_widget_get_style (widget)->xthickness)
1212 if (click_child == GTK_ARROW_UP &&
1213 event->y <= requisition.height / 2)
1217 diff = priv->adjustment->upper - priv->adjustment->value;
1219 gtk_spin_button_real_spin (spin, diff);
1221 else if (click_child == GTK_ARROW_DOWN &&
1222 event->y > requisition.height / 2)
1226 diff = priv->adjustment->value - priv->adjustment->lower;
1228 gtk_spin_button_real_spin (spin, -diff);
1232 gtk_widget_queue_draw (GTK_WIDGET (spin));
1237 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1241 gtk_spin_button_motion_notify (GtkWidget *widget,
1242 GdkEventMotion *event)
1244 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1245 GtkSpinButtonPrivate *priv = spin->priv;
1250 if (event->window == priv->panel)
1252 GtkRequisition requisition;
1255 gdk_event_request_motions (event);
1257 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1259 if (y <= requisition.height / 2 &&
1260 priv->in_child == GTK_ARROW_DOWN)
1262 priv->in_child = GTK_ARROW_UP;
1263 gtk_widget_queue_draw (GTK_WIDGET (spin));
1265 else if (y > requisition.height / 2 &&
1266 priv->in_child == GTK_ARROW_UP)
1268 priv->in_child = GTK_ARROW_DOWN;
1269 gtk_widget_queue_draw (GTK_WIDGET (spin));
1275 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1279 gtk_spin_button_timer (GtkSpinButton *spin_button)
1281 GtkSpinButtonPrivate *priv = spin_button->priv;
1282 gboolean retval = FALSE;
1286 if (priv->click_child == GTK_ARROW_UP)
1287 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1289 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1291 if (priv->need_timer)
1293 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1296 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1298 priv->need_timer = FALSE;
1299 priv->timer = gdk_threads_add_timeout (timeout,
1300 (GSourceFunc) gtk_spin_button_timer,
1301 (gpointer) spin_button);
1305 if (priv->climb_rate > 0.0 && priv->timer_step
1306 < priv->adjustment->page_increment)
1308 if (priv->timer_calls < MAX_TIMER_CALLS)
1309 priv->timer_calls++;
1312 priv->timer_calls = 0;
1313 priv->timer_step += priv->climb_rate;
1324 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1325 GtkSpinButton *spin_button)
1327 gboolean return_val;
1329 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1332 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1333 if (return_val == FALSE)
1334 gtk_spin_button_default_output (spin_button);
1336 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1338 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1340 g_object_notify (G_OBJECT (spin_button), "value");
1344 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1345 GtkScrollType scroll)
1347 GtkSpinButtonPrivate *priv = spin->priv;
1350 /* When the key binding is activated, there may be an outstanding
1351 * value, so we first have to commit what is currently written in
1352 * the spin buttons text entry. See #106574
1354 gtk_spin_button_update (spin);
1356 old_value = priv->adjustment->value;
1358 /* We don't test whether the entry is editable, since
1359 * this key binding conceptually corresponds to changing
1360 * the value with the buttons using the mouse, which
1361 * we allow for non-editable spin buttons.
1365 case GTK_SCROLL_STEP_BACKWARD:
1366 case GTK_SCROLL_STEP_DOWN:
1367 case GTK_SCROLL_STEP_LEFT:
1368 gtk_spin_button_real_spin (spin, -priv->timer_step);
1370 if (priv->climb_rate > 0.0 && priv->timer_step
1371 < priv->adjustment->page_increment)
1373 if (priv->timer_calls < MAX_TIMER_CALLS)
1374 priv->timer_calls++;
1377 priv->timer_calls = 0;
1378 priv->timer_step += priv->climb_rate;
1383 case GTK_SCROLL_STEP_FORWARD:
1384 case GTK_SCROLL_STEP_UP:
1385 case GTK_SCROLL_STEP_RIGHT:
1386 gtk_spin_button_real_spin (spin, priv->timer_step);
1388 if (priv->climb_rate > 0.0 && priv->timer_step
1389 < priv->adjustment->page_increment)
1391 if (priv->timer_calls < MAX_TIMER_CALLS)
1392 priv->timer_calls++;
1395 priv->timer_calls = 0;
1396 priv->timer_step += priv->climb_rate;
1401 case GTK_SCROLL_PAGE_BACKWARD:
1402 case GTK_SCROLL_PAGE_DOWN:
1403 case GTK_SCROLL_PAGE_LEFT:
1404 gtk_spin_button_real_spin (spin, -priv->adjustment->page_increment);
1407 case GTK_SCROLL_PAGE_FORWARD:
1408 case GTK_SCROLL_PAGE_UP:
1409 case GTK_SCROLL_PAGE_RIGHT:
1410 gtk_spin_button_real_spin (spin, priv->adjustment->page_increment);
1413 case GTK_SCROLL_START:
1415 gdouble diff = priv->adjustment->value - priv->adjustment->lower;
1417 gtk_spin_button_real_spin (spin, -diff);
1421 case GTK_SCROLL_END:
1423 gdouble diff = priv->adjustment->upper - priv->adjustment->value;
1425 gtk_spin_button_real_spin (spin, diff);
1430 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1434 gtk_spin_button_update (spin);
1436 if (priv->adjustment->value == old_value)
1437 gtk_widget_error_bell (GTK_WIDGET (spin));
1441 gtk_spin_button_key_release (GtkWidget *widget,
1444 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1445 GtkSpinButtonPrivate *priv = spin->priv;
1447 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1448 priv->timer_step = priv->adjustment->step_increment;
1449 priv->timer_calls = 0;
1455 gtk_spin_button_snap (GtkSpinButton *spin_button,
1458 GtkSpinButtonPrivate *priv = spin_button->priv;
1462 inc = priv->adjustment->step_increment;
1466 tmp = (val - priv->adjustment->lower) / inc;
1467 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1468 val = priv->adjustment->lower + floor (tmp) * inc;
1470 val = priv->adjustment->lower + ceil (tmp) * inc;
1472 gtk_spin_button_set_value (spin_button, val);
1476 gtk_spin_button_activate (GtkEntry *entry)
1478 if (entry->editable)
1479 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1481 /* Chain up so that entry->activates_default is honored */
1482 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1486 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1495 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1497 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1498 panel_width = arrow_size + 2 * gtk_widget_get_style (GTK_WIDGET (entry))->xthickness;
1501 *width -= panel_width;
1503 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1508 gtk_spin_button_insert_text (GtkEditable *editable,
1509 const gchar *new_text,
1510 gint new_text_length,
1513 GtkEntry *entry = GTK_ENTRY (editable);
1514 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1515 GtkSpinButtonPrivate *priv = spin->priv;
1516 GtkEditableInterface *parent_editable_iface;
1518 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1530 const gchar *entry_text;
1532 entry_length = gtk_entry_get_text_length (entry);
1533 entry_text = gtk_entry_get_text (entry);
1537 if (*(lc->negative_sign))
1538 neg_sign = *(lc->negative_sign);
1542 if (*(lc->positive_sign))
1543 pos_sign = *(lc->positive_sign);
1548 /* Workaround for bug caused by some Windows application messing
1549 * up the positive sign of the current locale, more specifically
1550 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1551 * See bug #330743 and for instance
1552 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1554 * I don't know if the positive sign always gets bogusly set to
1555 * a digit when the above Registry value is corrupted as
1556 * described. (In my test case, it got set to "8", and in the
1557 * bug report above it presumably was set ot "0".) Probably it
1558 * might get set to almost anything? So how to distinguish a
1559 * bogus value from some correct one for some locale? That is
1560 * probably hard, but at least we should filter out the
1563 if (pos_sign >= '0' && pos_sign <= '9')
1567 for (sign=0, i=0; i<entry_length; i++)
1568 if ((entry_text[i] == neg_sign) ||
1569 (entry_text[i] == pos_sign))
1575 if (sign && !(*position))
1578 for (dotpos=-1, i=0; i<entry_length; i++)
1579 if (entry_text[i] == *(lc->decimal_point))
1585 if (dotpos > -1 && *position > dotpos &&
1586 (gint)priv->digits - entry_length
1587 + dotpos - new_text_length + 1 < 0)
1590 for (i = 0; i < new_text_length; i++)
1592 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1594 if (sign || (*position) || i)
1598 else if (new_text[i] == *(lc->decimal_point))
1600 if (!priv->digits || dotpos > -1 ||
1601 (new_text_length - 1 - i + entry_length
1602 - *position > (gint)priv->digits))
1604 dotpos = *position + i;
1606 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1611 parent_editable_iface->insert_text (editable, new_text,
1612 new_text_length, position);
1616 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1619 GtkSpinButtonPrivate *priv = spin_button->priv;
1621 gdouble new_value = 0.0;
1622 gboolean wrapped = FALSE;
1624 adj = priv->adjustment;
1626 new_value = adj->value + increment;
1632 if (fabs (adj->value - adj->upper) < EPSILON)
1634 new_value = adj->lower;
1637 else if (new_value > adj->upper)
1638 new_value = adj->upper;
1641 new_value = MIN (new_value, adj->upper);
1643 else if (increment < 0)
1647 if (fabs (adj->value - adj->lower) < EPSILON)
1649 new_value = adj->upper;
1652 else if (new_value < adj->lower)
1653 new_value = adj->lower;
1656 new_value = MAX (new_value, adj->lower);
1659 if (fabs (new_value - adj->value) > EPSILON)
1660 gtk_adjustment_set_value (adj, new_value);
1663 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1665 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1669 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1674 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1676 return GTK_INPUT_ERROR;
1682 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1684 GtkSpinButtonPrivate *priv = spin_button->priv;
1686 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, priv->adjustment->value);
1688 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1689 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1695 /***********************************************************
1696 ***********************************************************
1697 *** Public interface ***
1698 ***********************************************************
1699 ***********************************************************/
1703 * gtk_spin_button_configure:
1704 * @spin_button: a #GtkSpinButton
1705 * @adjustment: (allow-none): a #GtkAdjustment.
1706 * @climb_rate: the new climb rate.
1707 * @digits: the number of decimal places to display in the spin button.
1709 * Changes the properties of an existing spin button. The adjustment, climb rate,
1710 * and number of decimal places are all changed accordingly, after this function call.
1713 gtk_spin_button_configure (GtkSpinButton *spin_button,
1714 GtkAdjustment *adjustment,
1718 GtkSpinButtonPrivate *priv;
1720 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1722 priv = spin_button->priv;
1725 gtk_spin_button_set_adjustment (spin_button, adjustment);
1727 adjustment = priv->adjustment;
1729 g_object_freeze_notify (G_OBJECT (spin_button));
1730 if (priv->digits != digits)
1732 priv->digits = digits;
1733 g_object_notify (G_OBJECT (spin_button), "digits");
1736 if (priv->climb_rate != climb_rate)
1738 priv->climb_rate = climb_rate;
1739 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1741 g_object_thaw_notify (G_OBJECT (spin_button));
1743 gtk_adjustment_value_changed (adjustment);
1747 gtk_spin_button_new (GtkAdjustment *adjustment,
1751 GtkSpinButton *spin;
1754 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1756 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1758 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1760 return GTK_WIDGET (spin);
1764 * gtk_spin_button_new_with_range:
1765 * @min: Minimum allowable value
1766 * @max: Maximum allowable value
1767 * @step: Increment added or subtracted by spinning the widget
1769 * This is a convenience constructor that allows creation of a numeric
1770 * #GtkSpinButton without manually creating an adjustment. The value is
1771 * initially set to the minimum value and a page increment of 10 * @step
1772 * is the default. The precision of the spin button is equivalent to the
1773 * precision of @step.
1775 * Note that the way in which the precision is derived works best if @step
1776 * is a power of ten. If the resulting precision is not suitable for your
1777 * needs, use gtk_spin_button_set_digits() to correct it.
1779 * Return value: The new spin button as a #GtkWidget.
1782 gtk_spin_button_new_with_range (gdouble min,
1787 GtkSpinButton *spin;
1790 g_return_val_if_fail (min <= max, NULL);
1791 g_return_val_if_fail (step != 0.0, NULL);
1793 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1795 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1797 if (fabs (step) >= 1.0 || step == 0.0)
1800 digits = abs ((gint) floor (log10 (fabs (step))));
1801 if (digits > MAX_DIGITS)
1802 digits = MAX_DIGITS;
1805 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1807 gtk_spin_button_set_numeric (spin, TRUE);
1809 return GTK_WIDGET (spin);
1812 /* Callback used when the spin button's adjustment changes. We need to redraw
1813 * the arrows when the adjustment's range changes, and reevaluate our size request.
1816 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1818 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1819 GtkSpinButtonPrivate *priv = spin_button->priv;
1821 priv->timer_step = priv->adjustment->step_increment;
1822 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1826 * gtk_spin_button_set_adjustment:
1827 * @spin_button: a #GtkSpinButton
1828 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1830 * Replaces the #GtkAdjustment associated with @spin_button.
1833 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1834 GtkAdjustment *adjustment)
1836 GtkSpinButtonPrivate *priv;
1838 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1840 priv = spin_button->priv;
1842 if (priv->adjustment != adjustment)
1844 if (priv->adjustment)
1846 g_signal_handlers_disconnect_by_func (priv->adjustment,
1847 gtk_spin_button_value_changed,
1849 g_signal_handlers_disconnect_by_func (priv->adjustment,
1850 adjustment_changed_cb,
1852 g_object_unref (priv->adjustment);
1854 priv->adjustment = adjustment;
1857 g_object_ref_sink (adjustment);
1858 g_signal_connect (adjustment, "value-changed",
1859 G_CALLBACK (gtk_spin_button_value_changed),
1861 g_signal_connect (adjustment, "changed",
1862 G_CALLBACK (adjustment_changed_cb),
1864 priv->timer_step = priv->adjustment->step_increment;
1867 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1870 g_object_notify (G_OBJECT (spin_button), "adjustment");
1874 * gtk_spin_button_get_adjustment:
1875 * @spin_button: a #GtkSpinButton
1877 * Get the adjustment associated with a #GtkSpinButton
1879 * Return value: (transfer none): the #GtkAdjustment of @spin_button
1882 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1884 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1886 return spin_button->priv->adjustment;
1890 * gtk_spin_button_set_digits:
1891 * @spin_button: a #GtkSpinButton
1892 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1894 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1898 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1901 GtkSpinButtonPrivate *priv;
1903 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1905 priv = spin_button->priv;
1907 if (priv->digits != digits)
1909 priv->digits = digits;
1910 gtk_spin_button_value_changed (priv->adjustment, spin_button);
1911 g_object_notify (G_OBJECT (spin_button), "digits");
1913 /* since lower/upper may have changed */
1914 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1919 * gtk_spin_button_get_digits:
1920 * @spin_button: a #GtkSpinButton
1922 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1924 * Returns: the current precision
1927 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1929 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1931 return spin_button->priv->digits;
1935 * gtk_spin_button_set_increments:
1936 * @spin_button: a #GtkSpinButton
1937 * @step: increment applied for a button 1 press.
1938 * @page: increment applied for a button 2 press.
1940 * Sets the step and page increments for spin_button. This affects how
1941 * quickly the value changes when the spin button's arrows are activated.
1944 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1948 GtkSpinButtonPrivate *priv;
1950 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1952 priv = spin_button->priv;
1954 priv->adjustment->step_increment = step;
1955 priv->adjustment->page_increment = page;
1959 * gtk_spin_button_get_increments:
1960 * @spin_button: a #GtkSpinButton
1961 * @step: (allow-none): location to store step increment, or %NULL
1962 * @page: (allow-none): location to store page increment, or %NULL
1964 * Gets the current step and page the increments used by @spin_button. See
1965 * gtk_spin_button_set_increments().
1968 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1972 GtkSpinButtonPrivate *priv;
1974 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1976 priv = spin_button->priv;
1979 *step = priv->adjustment->step_increment;
1981 *page = priv->adjustment->page_increment;
1985 * gtk_spin_button_set_range:
1986 * @spin_button: a #GtkSpinButton
1987 * @min: minimum allowable value
1988 * @max: maximum allowable value
1990 * Sets the minimum and maximum allowable values for @spin_button
1993 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1997 GtkSpinButtonPrivate *priv;
2000 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2002 priv = spin_button->priv;
2004 priv->adjustment->lower = min;
2005 priv->adjustment->upper = max;
2007 value = CLAMP (priv->adjustment->value,
2008 priv->adjustment->lower,
2009 (priv->adjustment->upper - priv->adjustment->page_size));
2011 if (value != priv->adjustment->value)
2012 gtk_spin_button_set_value (spin_button, value);
2014 gtk_adjustment_changed (priv->adjustment);
2018 * gtk_spin_button_get_range:
2019 * @spin_button: a #GtkSpinButton
2020 * @min: (allow-none): location to store minimum allowed value, or %NULL
2021 * @max: (allow-none): location to store maximum allowed value, or %NULL
2023 * Gets the range allowed for @spin_button. See
2024 * gtk_spin_button_set_range().
2027 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2031 GtkSpinButtonPrivate *priv;
2033 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2035 priv = spin_button->priv;
2038 *min = priv->adjustment->lower;
2040 *max = priv->adjustment->upper;
2044 * gtk_spin_button_get_value:
2045 * @spin_button: a #GtkSpinButton
2047 * Get the value in the @spin_button.
2049 * Return value: the value of @spin_button
2052 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2054 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2056 return spin_button->priv->adjustment->value;
2060 * gtk_spin_button_get_value_as_int:
2061 * @spin_button: a #GtkSpinButton
2063 * Get the value @spin_button represented as an integer.
2065 * Return value: the value of @spin_button
2068 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2070 GtkSpinButtonPrivate *priv;
2073 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2075 priv = spin_button->priv;
2077 val = priv->adjustment->value;
2078 if (val - floor (val) < ceil (val) - val)
2085 * gtk_spin_button_set_value:
2086 * @spin_button: a #GtkSpinButton
2087 * @value: the new value
2089 * Set the value of @spin_button.
2092 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2095 GtkSpinButtonPrivate *priv;
2097 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2099 priv = spin_button->priv;
2101 if (fabs (value - priv->adjustment->value) > EPSILON)
2102 gtk_adjustment_set_value (priv->adjustment, value);
2105 gint return_val = FALSE;
2106 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2107 if (return_val == FALSE)
2108 gtk_spin_button_default_output (spin_button);
2113 * gtk_spin_button_set_update_policy:
2114 * @spin_button: a #GtkSpinButton
2115 * @policy: a #GtkSpinButtonUpdatePolicy value
2117 * Sets the update behavior of a spin button. This determines whether the
2118 * spin button is always updated or only when a valid value is set.
2121 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2122 GtkSpinButtonUpdatePolicy policy)
2124 GtkSpinButtonPrivate *priv;
2126 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2128 priv = spin_button->priv;
2130 if (priv->update_policy != policy)
2132 priv->update_policy = policy;
2133 g_object_notify (G_OBJECT (spin_button), "update-policy");
2138 * gtk_spin_button_get_update_policy:
2139 * @spin_button: a #GtkSpinButton
2141 * Gets the update behavior of a spin button. See
2142 * gtk_spin_button_set_update_policy().
2144 * Return value: the current update policy
2146 GtkSpinButtonUpdatePolicy
2147 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2149 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2151 return spin_button->priv->update_policy;
2155 * gtk_spin_button_set_numeric:
2156 * @spin_button: a #GtkSpinButton
2157 * @numeric: flag indicating if only numeric entry is allowed.
2159 * Sets the flag that determines if non-numeric text can be typed into
2163 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2166 GtkSpinButtonPrivate *priv;
2168 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2170 priv = spin_button->priv;
2172 numeric = numeric != FALSE;
2174 if (priv->numeric != numeric)
2176 priv->numeric = numeric;
2177 g_object_notify (G_OBJECT (spin_button), "numeric");
2182 * gtk_spin_button_get_numeric:
2183 * @spin_button: a #GtkSpinButton
2185 * Returns whether non-numeric text can be typed into the spin button.
2186 * See gtk_spin_button_set_numeric().
2188 * Return value: %TRUE if only numeric text can be entered
2191 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2193 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2195 return spin_button->priv->numeric;
2199 * gtk_spin_button_set_wrap:
2200 * @spin_button: a #GtkSpinButton
2201 * @wrap: a flag indicating if wrapping behavior is performed.
2203 * Sets the flag that determines if a spin button value wraps around to the
2204 * opposite limit when the upper or lower limit of the range is exceeded.
2207 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2210 GtkSpinButtonPrivate *priv;
2212 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2214 priv = spin_button->priv;
2216 wrap = wrap != FALSE;
2218 if (priv->wrap != wrap)
2220 priv->wrap = (wrap != 0);
2222 g_object_notify (G_OBJECT (spin_button), "wrap");
2227 * gtk_spin_button_get_wrap:
2228 * @spin_button: a #GtkSpinButton
2230 * Returns whether the spin button's value wraps around to the
2231 * opposite limit when the upper or lower limit of the range is
2232 * exceeded. See gtk_spin_button_set_wrap().
2234 * Return value: %TRUE if the spin button wraps around
2237 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2239 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2241 return spin_button->priv->wrap;
2245 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2251 style = gtk_widget_get_style (GTK_WIDGET (spin_button));
2252 size = pango_font_description_get_size (style->font_desc);
2253 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2255 return arrow_size - arrow_size % 2; /* force even */
2259 * spin_button_get_shadow_type:
2260 * @spin_button: a #GtkSpinButton
2262 * Convenience function to Get the shadow type from the underlying widget's
2265 * Return value: the #GtkShadowType
2268 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2270 GtkShadowType rc_shadow_type;
2272 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow-type", &rc_shadow_type, NULL);
2274 return rc_shadow_type;
2278 * gtk_spin_button_set_snap_to_ticks:
2279 * @spin_button: a #GtkSpinButton
2280 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2282 * Sets the policy as to whether values are corrected to the nearest step
2283 * increment when a spin button is activated after providing an invalid value.
2286 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2287 gboolean snap_to_ticks)
2289 GtkSpinButtonPrivate *priv;
2292 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2294 priv = spin_button->priv;
2296 new_val = (snap_to_ticks != 0);
2298 if (new_val != priv->snap_to_ticks)
2300 priv->snap_to_ticks = new_val;
2301 if (new_val && GTK_ENTRY (spin_button)->editable)
2302 gtk_spin_button_update (spin_button);
2304 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2309 * gtk_spin_button_get_snap_to_ticks:
2310 * @spin_button: a #GtkSpinButton
2312 * Returns whether the values are corrected to the nearest step. See
2313 * gtk_spin_button_set_snap_to_ticks().
2315 * Return value: %TRUE if values are snapped to the nearest step.
2318 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2320 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2322 return spin_button->priv->snap_to_ticks;
2326 * gtk_spin_button_spin:
2327 * @spin_button: a #GtkSpinButton
2328 * @direction: a #GtkSpinType indicating the direction to spin.
2329 * @increment: step increment to apply in the specified direction.
2331 * Increment or decrement a spin button's value in a specified direction
2332 * by a specified amount.
2335 gtk_spin_button_spin (GtkSpinButton *spin_button,
2336 GtkSpinType direction,
2339 GtkSpinButtonPrivate *priv;
2343 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2345 priv = spin_button->priv;
2347 adj = priv->adjustment;
2349 /* for compatibility with the 1.0.x version of this function */
2350 if (increment != 0 && increment != adj->step_increment &&
2351 (direction == GTK_SPIN_STEP_FORWARD ||
2352 direction == GTK_SPIN_STEP_BACKWARD))
2354 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2355 increment = -increment;
2356 direction = GTK_SPIN_USER_DEFINED;
2361 case GTK_SPIN_STEP_FORWARD:
2363 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2366 case GTK_SPIN_STEP_BACKWARD:
2368 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2371 case GTK_SPIN_PAGE_FORWARD:
2373 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2376 case GTK_SPIN_PAGE_BACKWARD:
2378 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2383 diff = adj->value - adj->lower;
2385 gtk_spin_button_real_spin (spin_button, -diff);
2390 diff = adj->upper - adj->value;
2392 gtk_spin_button_real_spin (spin_button, diff);
2395 case GTK_SPIN_USER_DEFINED:
2398 gtk_spin_button_real_spin (spin_button, increment);
2407 * gtk_spin_button_update:
2408 * @spin_button: a #GtkSpinButton
2410 * Manually force an update of the spin button.
2413 gtk_spin_button_update (GtkSpinButton *spin_button)
2415 GtkSpinButtonPrivate *priv;
2420 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2422 priv = spin_button->priv;
2425 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2426 if (return_val == FALSE)
2428 return_val = gtk_spin_button_default_input (spin_button, &val);
2429 error = (return_val == GTK_INPUT_ERROR);
2431 else if (return_val == GTK_INPUT_ERROR)
2434 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2436 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2438 if (val < priv->adjustment->lower)
2439 val = priv->adjustment->lower;
2440 else if (val > priv->adjustment->upper)
2441 val = priv->adjustment->upper;
2443 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2445 val < priv->adjustment->lower ||
2446 val > priv->adjustment->upper))
2448 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2452 if (priv->snap_to_ticks)
2453 gtk_spin_button_snap (spin_button, val);
2455 gtk_spin_button_set_value (spin_button, val);
2459 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2461 return spin_button->priv->panel;