1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GtkSpinButton widget for GTK+
5 * Copyright (C) 1998 Lars Hamann and Stefan Jeske
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
36 #include "gdk/gdkkeysyms.h"
37 #include "gtkbindings.h"
38 #include "gtkspinbutton.h"
39 #include "gtkentryprivate.h"
41 #include "gtkmarshalers.h"
42 #include "gtksettings.h"
43 #include "gtksizerequest.h"
44 #include "gtkprivate.h"
47 #define MIN_SPIN_BUTTON_WIDTH 30
48 #define MAX_TIMER_CALLS 5
51 #define MIN_ARROW_WIDTH 6
54 struct _GtkSpinButtonPrivate
56 GtkSpinButtonUpdatePolicy update_policy;
57 GtkAdjustment *adjustment;
67 guint click_child : 2; /* valid: GTK_ARROW_UP=0, GTK_ARROW_DOWN=1 or 2=NONE/BOTH */
72 guint snap_to_ticks : 1;
73 guint timer_calls : 3;
100 static void gtk_spin_button_editable_init (GtkEditableInterface *iface);
101 static void gtk_spin_button_finalize (GObject *object);
102 static void gtk_spin_button_destroy (GtkObject *object);
103 static void gtk_spin_button_set_property (GObject *object,
107 static void gtk_spin_button_get_property (GObject *object,
111 static void gtk_spin_button_map (GtkWidget *widget);
112 static void gtk_spin_button_unmap (GtkWidget *widget);
113 static void gtk_spin_button_realize (GtkWidget *widget);
114 static void gtk_spin_button_unrealize (GtkWidget *widget);
115 static void gtk_spin_button_size_request (GtkWidget *widget,
116 GtkRequisition *requisition);
117 static void gtk_spin_button_size_allocate (GtkWidget *widget,
118 GtkAllocation *allocation);
119 static gint gtk_spin_button_expose (GtkWidget *widget,
120 GdkEventExpose *event);
121 static gint gtk_spin_button_button_press (GtkWidget *widget,
122 GdkEventButton *event);
123 static gint gtk_spin_button_button_release (GtkWidget *widget,
124 GdkEventButton *event);
125 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
126 GdkEventMotion *event);
127 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
128 GdkEventCrossing *event);
129 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
130 GdkEventCrossing *event);
131 static gint gtk_spin_button_focus_out (GtkWidget *widget,
132 GdkEventFocus *event);
133 static void gtk_spin_button_grab_notify (GtkWidget *widget,
134 gboolean was_grabbed);
135 static void gtk_spin_button_state_changed (GtkWidget *widget,
136 GtkStateType previous_state);
137 static void gtk_spin_button_style_set (GtkWidget *widget,
138 GtkStyle *previous_style);
139 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
141 GtkArrowType arrow_type);
142 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
143 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
144 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
145 GtkSpinButton *spin_button);
146 static gint gtk_spin_button_key_release (GtkWidget *widget,
148 static gint gtk_spin_button_scroll (GtkWidget *widget,
149 GdkEventScroll *event);
150 static void gtk_spin_button_activate (GtkEntry *entry);
151 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
156 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
158 static void gtk_spin_button_insert_text (GtkEditable *editable,
159 const gchar *new_text,
160 gint new_text_length,
162 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
164 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
165 GtkScrollType scroll);
167 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
169 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
171 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
172 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
174 static guint spinbutton_signals[LAST_SIGNAL] = {0};
178 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
179 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
180 gtk_spin_button_editable_init))
182 #define add_spin_binding(binding_set, keyval, mask, scroll) \
183 gtk_binding_entry_add_signal (binding_set, keyval, mask, \
185 GTK_TYPE_SCROLL_TYPE, scroll)
188 gtk_spin_button_class_init (GtkSpinButtonClass *class)
190 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
191 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
192 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
193 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
194 GtkBindingSet *binding_set;
196 gobject_class->finalize = gtk_spin_button_finalize;
198 gobject_class->set_property = gtk_spin_button_set_property;
199 gobject_class->get_property = gtk_spin_button_get_property;
201 object_class->destroy = gtk_spin_button_destroy;
203 widget_class->map = gtk_spin_button_map;
204 widget_class->unmap = gtk_spin_button_unmap;
205 widget_class->realize = gtk_spin_button_realize;
206 widget_class->unrealize = gtk_spin_button_unrealize;
207 widget_class->size_request = gtk_spin_button_size_request;
208 widget_class->size_allocate = gtk_spin_button_size_allocate;
209 widget_class->expose_event = gtk_spin_button_expose;
210 widget_class->scroll_event = gtk_spin_button_scroll;
211 widget_class->button_press_event = gtk_spin_button_button_press;
212 widget_class->button_release_event = gtk_spin_button_button_release;
213 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
214 widget_class->key_release_event = gtk_spin_button_key_release;
215 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
216 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
217 widget_class->focus_out_event = gtk_spin_button_focus_out;
218 widget_class->grab_notify = gtk_spin_button_grab_notify;
219 widget_class->state_changed = gtk_spin_button_state_changed;
220 widget_class->style_set = gtk_spin_button_style_set;
222 entry_class->activate = gtk_spin_button_activate;
223 entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
226 class->output = NULL;
227 class->change_value = gtk_spin_button_real_change_value;
229 g_object_class_install_property (gobject_class,
231 g_param_spec_object ("adjustment",
233 P_("The adjustment that holds the value of the spin button"),
235 GTK_PARAM_READWRITE));
237 g_object_class_install_property (gobject_class,
239 g_param_spec_double ("climb-rate",
241 P_("The acceleration rate when you hold down a button"),
245 GTK_PARAM_READWRITE));
247 g_object_class_install_property (gobject_class,
249 g_param_spec_uint ("digits",
251 P_("The number of decimal places to display"),
255 GTK_PARAM_READWRITE));
257 g_object_class_install_property (gobject_class,
259 g_param_spec_boolean ("snap-to-ticks",
261 P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
263 GTK_PARAM_READWRITE));
265 g_object_class_install_property (gobject_class,
267 g_param_spec_boolean ("numeric",
269 P_("Whether non-numeric characters should be ignored"),
271 GTK_PARAM_READWRITE));
273 g_object_class_install_property (gobject_class,
275 g_param_spec_boolean ("wrap",
277 P_("Whether a spin button should wrap upon reaching its limits"),
279 GTK_PARAM_READWRITE));
281 g_object_class_install_property (gobject_class,
283 g_param_spec_enum ("update-policy",
285 P_("Whether the spin button should update always, or only when the value is legal"),
286 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
288 GTK_PARAM_READWRITE));
290 g_object_class_install_property (gobject_class,
292 g_param_spec_double ("value",
294 P_("Reads the current value, or sets a new value"),
298 GTK_PARAM_READWRITE));
300 gtk_widget_class_install_style_property_parser (widget_class,
301 g_param_spec_enum ("shadow-type",
303 P_("Style of bevel around the spin button"),
304 GTK_TYPE_SHADOW_TYPE,
307 gtk_rc_property_parse_enum);
308 spinbutton_signals[INPUT] =
309 g_signal_new (I_("input"),
310 G_TYPE_FROM_CLASS (gobject_class),
312 G_STRUCT_OFFSET (GtkSpinButtonClass, input),
314 _gtk_marshal_INT__POINTER,
319 * GtkSpinButton::output:
320 * @spin_button: the object which received the signal
322 * The ::output signal can be used to change to formatting
323 * of the value that is displayed in the spin buttons entry.
325 * /* show leading zeros */
327 * on_output (GtkSpinButton *spin,
330 * GtkAdjustment *adj;
334 * adj = gtk_spin_button_get_adjustment (spin);
335 * value = (int)gtk_adjustment_get_value (adj);
336 * text = g_strdup_printf ("%02d", value);
337 * gtk_entry_set_text (GTK_ENTRY (spin), text);
344 * Returns: %TRUE if the value has been displayed.
346 spinbutton_signals[OUTPUT] =
347 g_signal_new (I_("output"),
348 G_TYPE_FROM_CLASS (gobject_class),
350 G_STRUCT_OFFSET (GtkSpinButtonClass, output),
351 _gtk_boolean_handled_accumulator, NULL,
352 _gtk_marshal_BOOLEAN__VOID,
355 spinbutton_signals[VALUE_CHANGED] =
356 g_signal_new (I_("value-changed"),
357 G_TYPE_FROM_CLASS (gobject_class),
359 G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
361 _gtk_marshal_VOID__VOID,
365 * GtkSpinButton::wrapped:
366 * @spinbutton: the object which received the signal
368 * The wrapped signal is emitted right after the spinbutton wraps
369 * from its maximum to minimum value or vice-versa.
373 spinbutton_signals[WRAPPED] =
374 g_signal_new (I_("wrapped"),
375 G_TYPE_FROM_CLASS (gobject_class),
377 G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
379 _gtk_marshal_VOID__VOID,
383 spinbutton_signals[CHANGE_VALUE] =
384 g_signal_new (I_("change-value"),
385 G_TYPE_FROM_CLASS (gobject_class),
386 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
387 G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
389 _gtk_marshal_VOID__ENUM,
391 GTK_TYPE_SCROLL_TYPE);
393 binding_set = gtk_binding_set_by_class (class);
395 add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
396 add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
397 add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
398 add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
399 add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
400 add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
401 add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
402 add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
404 g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
408 gtk_spin_button_editable_init (GtkEditableInterface *iface)
410 iface->insert_text = gtk_spin_button_insert_text;
414 gtk_spin_button_set_property (GObject *object,
419 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
420 GtkSpinButtonPrivate *priv = spin_button->priv;
424 GtkAdjustment *adjustment;
426 case PROP_ADJUSTMENT:
427 adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
429 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
430 gtk_spin_button_set_adjustment (spin_button, adjustment);
432 case PROP_CLIMB_RATE:
433 gtk_spin_button_configure (spin_button,
435 g_value_get_double (value),
439 gtk_spin_button_configure (spin_button,
442 g_value_get_uint (value));
444 case PROP_SNAP_TO_TICKS:
445 gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
448 gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
451 gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
453 case PROP_UPDATE_POLICY:
454 gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
457 gtk_spin_button_set_value (spin_button, g_value_get_double (value));
460 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
466 gtk_spin_button_get_property (GObject *object,
471 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
472 GtkSpinButtonPrivate *priv = spin_button->priv;
476 case PROP_ADJUSTMENT:
477 g_value_set_object (value, priv->adjustment);
479 case PROP_CLIMB_RATE:
480 g_value_set_double (value, priv->climb_rate);
483 g_value_set_uint (value, priv->digits);
485 case PROP_SNAP_TO_TICKS:
486 g_value_set_boolean (value, priv->snap_to_ticks);
489 g_value_set_boolean (value, priv->numeric);
492 g_value_set_boolean (value, priv->wrap);
494 case PROP_UPDATE_POLICY:
495 g_value_set_enum (value, priv->update_policy);
498 g_value_set_double (value, priv->adjustment->value);
501 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
507 gtk_spin_button_init (GtkSpinButton *spin_button)
509 GtkSpinButtonPrivate *priv;
511 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
512 GTK_TYPE_SPIN_BUTTON,
513 GtkSpinButtonPrivate);
514 priv = spin_button->priv;
516 priv->adjustment = NULL;
519 priv->climb_rate = 0.0;
520 priv->timer_step = 0.0;
521 priv->update_policy = GTK_UPDATE_ALWAYS;
522 priv->in_child = NO_ARROW;
523 priv->click_child = NO_ARROW;
525 priv->need_timer = FALSE;
526 priv->timer_calls = 0;
528 priv->numeric = FALSE;
530 priv->snap_to_ticks = FALSE;
532 gtk_spin_button_set_adjustment (spin_button,
533 (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
537 gtk_spin_button_finalize (GObject *object)
539 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
541 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
545 gtk_spin_button_destroy (GtkObject *object)
547 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
549 GTK_OBJECT_CLASS (gtk_spin_button_parent_class)->destroy (object);
553 gtk_spin_button_map (GtkWidget *widget)
555 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
556 GtkSpinButtonPrivate *priv = spin_button->priv;
558 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
560 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
561 gdk_window_show (priv->panel);
566 gtk_spin_button_unmap (GtkWidget *widget)
568 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
569 GtkSpinButtonPrivate *priv = spin_button->priv;
571 if (gtk_widget_get_mapped (widget))
573 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
575 gdk_window_hide (priv->panel);
576 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
581 gtk_spin_button_realize (GtkWidget *widget)
583 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
584 GtkSpinButtonPrivate *priv = spin_button->priv;
585 GtkAllocation allocation;
586 GtkRequisition requisition;
588 GdkWindowAttr attributes;
589 gint attributes_mask;
593 style = gtk_widget_get_style (widget);
595 arrow_size = spin_button_get_arrow_size (spin_button);
597 gtk_size_request_get_size (GTK_SIZE_REQUEST (spin_button), &requisition, NULL);
598 gtk_widget_get_allocation (widget, &allocation);
600 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
601 GDK_KEY_RELEASE_MASK);
602 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
604 attributes.window_type = GDK_WINDOW_CHILD;
605 attributes.wclass = GDK_INPUT_OUTPUT;
606 attributes.visual = gtk_widget_get_visual (widget);
607 attributes.colormap = gtk_widget_get_colormap (widget);
608 attributes.event_mask = gtk_widget_get_events (widget);
609 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
610 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
611 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
613 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
615 attributes.x = allocation.width - arrow_size - 2 * style->xthickness;
616 attributes.y = (allocation.height - requisition.height) / 2;
617 attributes.width = arrow_size + 2 * style->xthickness;
618 attributes.height = requisition.height;
620 priv->panel = gdk_window_new (gtk_widget_get_window (widget),
621 &attributes, attributes_mask);
622 gdk_window_set_user_data (priv->panel, widget);
624 gtk_style_set_background (style,
625 priv->panel, GTK_STATE_NORMAL);
628 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
629 if (return_val == FALSE)
630 gtk_spin_button_default_output (spin_button);
632 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
636 gtk_spin_button_unrealize (GtkWidget *widget)
638 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
639 GtkSpinButtonPrivate *priv = spin->priv;
641 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
645 gdk_window_set_user_data (priv->panel, NULL);
646 gdk_window_destroy (priv->panel);
652 compute_double_length (double val, int digits)
658 if (fabs (val) > 1.0)
659 a = floor (log10 (fabs (val))) + 1;
671 return a + digits + extra;
675 gtk_spin_button_size_request (GtkWidget *widget,
676 GtkRequisition *requisition)
678 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
679 GtkSpinButtonPrivate *priv = spin_button->priv;
680 GtkEntry *entry = GTK_ENTRY (widget);
684 style = gtk_widget_get_style (widget);
686 arrow_size = spin_button_get_arrow_size (spin_button);
688 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_request (widget, requisition);
690 if (entry->width_chars < 0)
692 PangoContext *context;
693 PangoFontMetrics *metrics;
699 gboolean interior_focus;
701 gint xborder, yborder;
702 GtkBorder inner_border;
704 gtk_widget_style_get (widget,
705 "interior-focus", &interior_focus,
706 "focus-line-width", &focus_width,
709 context = gtk_widget_get_pango_context (widget);
710 metrics = pango_context_get_metrics (context,
712 pango_context_get_language (context));
714 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
715 digit_width = PANGO_SCALE *
716 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
718 pango_font_metrics_unref (metrics);
720 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
722 width = MIN_SPIN_BUTTON_WIDTH;
723 max_string_len = MAX (10, compute_double_length (1e9 * priv->adjustment->step_increment,
726 string_len = compute_double_length (priv->adjustment->upper,
728 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
729 width = MAX (width, w);
730 string_len = compute_double_length (priv->adjustment->lower,
732 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
733 width = MAX (width, w);
735 _gtk_entry_get_borders (entry, &xborder, &yborder);
736 _gtk_entry_effective_inner_border (entry, &inner_border);
738 requisition->width = width + xborder * 2 + inner_border.left + inner_border.right;
741 requisition->width += arrow_size + 2 * style->xthickness;
745 gtk_spin_button_size_allocate (GtkWidget *widget,
746 GtkAllocation *allocation)
748 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
749 GtkSpinButtonPrivate *priv = spin->priv;
750 GtkAllocation panel_allocation;
751 GtkRequisition requisition;
755 arrow_size = spin_button_get_arrow_size (spin);
756 panel_width = arrow_size + 2 * gtk_widget_get_style (widget)->xthickness;
758 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
760 gtk_widget_set_allocation (widget, allocation);
762 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
763 panel_allocation.x = 0;
765 panel_allocation.x = allocation->width - panel_width;
767 panel_allocation.width = panel_width;
768 panel_allocation.height = MIN (requisition.height, allocation->height);
770 panel_allocation.y = 0;
772 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
774 if (gtk_widget_get_realized (widget))
776 gdk_window_move_resize (priv->panel,
779 panel_allocation.width,
780 panel_allocation.height);
783 gtk_widget_queue_draw (GTK_WIDGET (spin));
787 gtk_spin_button_expose (GtkWidget *widget,
788 GdkEventExpose *event)
790 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
791 GtkSpinButtonPrivate *priv = spin->priv;
793 if (gtk_widget_is_drawable (widget))
795 if (event->window == priv->panel)
797 GtkShadowType shadow_type;
799 shadow_type = spin_button_get_shadow_type (spin);
801 if (shadow_type != GTK_SHADOW_NONE)
806 state = gtk_widget_has_focus (widget) ?
807 GTK_STATE_ACTIVE : gtk_widget_get_state (widget);
809 gdk_drawable_get_size (priv->panel, &width, &height);
811 gtk_paint_box (gtk_widget_get_style (widget), priv->panel,
813 &event->area, widget, "spinbutton",
814 0, 0, width, height);
817 gtk_spin_button_draw_arrow (spin, &event->area, GTK_ARROW_UP);
818 gtk_spin_button_draw_arrow (spin, &event->area, GTK_ARROW_DOWN);
821 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->expose_event (widget, event);
828 spin_button_at_limit (GtkSpinButton *spin_button,
831 GtkSpinButtonPrivate *priv = spin_button->priv;
832 GtkArrowType effective_arrow;
837 if (priv->adjustment->step_increment > 0)
838 effective_arrow = arrow;
840 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
842 if (effective_arrow == GTK_ARROW_UP &&
843 (priv->adjustment->upper - priv->adjustment->value <= EPSILON))
846 if (effective_arrow == GTK_ARROW_DOWN &&
847 (priv->adjustment->value - priv->adjustment->lower <= EPSILON))
854 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
856 GtkArrowType arrow_type)
858 GtkSpinButtonPrivate *priv;
859 GtkStateType state_type;
860 GtkShadowType shadow_type;
869 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
871 priv = spin_button->priv;
872 widget = GTK_WIDGET (spin_button);
874 if (gtk_widget_is_drawable (widget))
876 GtkRequisition requisition;
878 style = gtk_widget_get_style (widget);
879 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
881 width = spin_button_get_arrow_size (spin_button) + 2 * style->xthickness;
883 if (arrow_type == GTK_ARROW_UP)
888 height = requisition.height / 2;
893 y = requisition.height / 2;
895 height = (requisition.height + 1) / 2;
898 if (spin_button_at_limit (spin_button, arrow_type))
900 shadow_type = GTK_SHADOW_OUT;
901 state_type = GTK_STATE_INSENSITIVE;
905 if (priv->click_child == arrow_type)
907 state_type = GTK_STATE_ACTIVE;
908 shadow_type = GTK_SHADOW_IN;
912 if (priv->in_child == arrow_type &&
913 priv->click_child == NO_ARROW)
915 state_type = GTK_STATE_PRELIGHT;
919 state_type = gtk_widget_get_state (widget);
922 shadow_type = GTK_SHADOW_OUT;
926 gtk_paint_box (style, priv->panel,
927 state_type, shadow_type,
929 (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
930 x, y, width, height);
932 height = requisition.height;
934 if (arrow_type == GTK_ARROW_DOWN)
937 height = height - y - 2;
942 height = height / 2 - 2;
947 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
953 w -= w % 2 - 1; /* force odd */
956 x += (width - w) / 2;
957 y += (height - h) / 2;
962 gtk_paint_arrow (style, priv->panel,
963 state_type, shadow_type,
964 area, widget, "spinbutton",
966 x, y, width, height);
971 gtk_spin_button_enter_notify (GtkWidget *widget,
972 GdkEventCrossing *event)
974 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
975 GtkSpinButtonPrivate *priv = spin->priv;
976 GtkRequisition requisition;
978 if (event->window == priv->panel)
984 device = gdk_event_get_device ((GdkEvent *) event);
985 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
987 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
989 if (y <= requisition.height / 2)
990 priv->in_child = GTK_ARROW_UP;
992 priv->in_child = GTK_ARROW_DOWN;
994 gtk_widget_queue_draw (GTK_WIDGET (spin));
997 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event)
998 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1004 gtk_spin_button_leave_notify (GtkWidget *widget,
1005 GdkEventCrossing *event)
1007 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1008 GtkSpinButtonPrivate *priv = spin->priv;
1010 priv->in_child = NO_ARROW;
1011 gtk_widget_queue_draw (GTK_WIDGET (spin));
1013 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event)
1014 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1020 gtk_spin_button_focus_out (GtkWidget *widget,
1021 GdkEventFocus *event)
1023 if (GTK_ENTRY (widget)->editable)
1024 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1026 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1030 gtk_spin_button_grab_notify (GtkWidget *widget,
1031 gboolean was_grabbed)
1033 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1037 gtk_spin_button_stop_spinning (spin);
1038 gtk_widget_queue_draw (GTK_WIDGET (spin));
1043 gtk_spin_button_state_changed (GtkWidget *widget,
1044 GtkStateType previous_state)
1046 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1048 if (!gtk_widget_is_sensitive (widget))
1050 gtk_spin_button_stop_spinning (spin);
1051 gtk_widget_queue_draw (GTK_WIDGET (spin));
1056 gtk_spin_button_style_set (GtkWidget *widget,
1057 GtkStyle *previous_style)
1059 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1060 GtkSpinButtonPrivate *priv = spin->priv;
1062 if (previous_style && gtk_widget_get_realized (widget))
1063 gtk_style_set_background (gtk_widget_get_style (widget),
1064 priv->panel, GTK_STATE_NORMAL);
1066 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_set (widget, previous_style);
1071 gtk_spin_button_scroll (GtkWidget *widget,
1072 GdkEventScroll *event)
1074 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1075 GtkSpinButtonPrivate *priv = spin->priv;
1077 if (event->direction == GDK_SCROLL_UP)
1079 if (!gtk_widget_has_focus (widget))
1080 gtk_widget_grab_focus (widget);
1081 gtk_spin_button_real_spin (spin, priv->adjustment->step_increment);
1083 else if (event->direction == GDK_SCROLL_DOWN)
1085 if (!gtk_widget_has_focus (widget))
1086 gtk_widget_grab_focus (widget);
1087 gtk_spin_button_real_spin (spin, -priv->adjustment->step_increment);
1096 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1098 GtkSpinButtonPrivate *priv = spin->priv;
1102 g_source_remove (priv->timer);
1104 priv->timer_calls = 0;
1105 priv->need_timer = FALSE;
1110 priv->timer_step = priv->adjustment->step_increment;
1111 priv->timer_calls = 0;
1113 priv->click_child = NO_ARROW;
1118 start_spinning (GtkSpinButton *spin,
1119 GtkArrowType click_child,
1122 GtkSpinButtonPrivate *priv;
1124 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1128 priv->click_child = click_child;
1132 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1135 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1137 priv->timer_step = step;
1138 priv->need_timer = TRUE;
1139 priv->timer = gdk_threads_add_timeout (timeout,
1140 (GSourceFunc) gtk_spin_button_timer,
1143 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1145 gtk_widget_queue_draw (GTK_WIDGET (spin));
1149 gtk_spin_button_button_press (GtkWidget *widget,
1150 GdkEventButton *event)
1152 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1153 GtkSpinButtonPrivate *priv = spin->priv;
1157 if (event->window == priv->panel)
1159 GtkRequisition requisition;
1161 if (!gtk_widget_has_focus (widget))
1162 gtk_widget_grab_focus (widget);
1163 priv->button = event->button;
1165 if (GTK_ENTRY (widget)->editable)
1166 gtk_spin_button_update (spin);
1168 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
1170 if (event->y <= requisition.height / 2)
1172 if (event->button == 1)
1173 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->step_increment);
1174 else if (event->button == 2)
1175 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->page_increment);
1177 priv->click_child = GTK_ARROW_UP;
1181 if (event->button == 1)
1182 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->step_increment);
1183 else if (event->button == 2)
1184 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->page_increment);
1186 priv->click_child = GTK_ARROW_DOWN;
1191 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1197 gtk_spin_button_button_release (GtkWidget *widget,
1198 GdkEventButton *event)
1200 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1201 GtkSpinButtonPrivate *priv = spin->priv;
1204 arrow_size = spin_button_get_arrow_size (spin);
1206 if (event->button == priv->button)
1208 int click_child = priv->click_child;
1210 gtk_spin_button_stop_spinning (spin);
1212 if (event->button == 3)
1214 GtkRequisition requisition;
1216 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
1218 if (event->y >= 0 && event->x >= 0 &&
1219 event->y <= requisition.height &&
1220 event->x <= arrow_size + 2 * gtk_widget_get_style (widget)->xthickness)
1222 if (click_child == GTK_ARROW_UP &&
1223 event->y <= requisition.height / 2)
1227 diff = priv->adjustment->upper - priv->adjustment->value;
1229 gtk_spin_button_real_spin (spin, diff);
1231 else if (click_child == GTK_ARROW_DOWN &&
1232 event->y > requisition.height / 2)
1236 diff = priv->adjustment->value - priv->adjustment->lower;
1238 gtk_spin_button_real_spin (spin, -diff);
1242 gtk_widget_queue_draw (GTK_WIDGET (spin));
1247 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1251 gtk_spin_button_motion_notify (GtkWidget *widget,
1252 GdkEventMotion *event)
1254 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1255 GtkSpinButtonPrivate *priv = spin->priv;
1260 if (event->window == priv->panel)
1262 GtkRequisition requisition;
1265 gdk_event_request_motions (event);
1267 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
1269 if (y <= requisition.height / 2 &&
1270 priv->in_child == GTK_ARROW_DOWN)
1272 priv->in_child = GTK_ARROW_UP;
1273 gtk_widget_queue_draw (GTK_WIDGET (spin));
1275 else if (y > requisition.height / 2 &&
1276 priv->in_child == GTK_ARROW_UP)
1278 priv->in_child = GTK_ARROW_DOWN;
1279 gtk_widget_queue_draw (GTK_WIDGET (spin));
1285 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1289 gtk_spin_button_timer (GtkSpinButton *spin_button)
1291 GtkSpinButtonPrivate *priv = spin_button->priv;
1292 gboolean retval = FALSE;
1296 if (priv->click_child == GTK_ARROW_UP)
1297 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1299 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1301 if (priv->need_timer)
1303 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1306 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1308 priv->need_timer = FALSE;
1309 priv->timer = gdk_threads_add_timeout (timeout,
1310 (GSourceFunc) gtk_spin_button_timer,
1311 (gpointer) spin_button);
1315 if (priv->climb_rate > 0.0 && priv->timer_step
1316 < priv->adjustment->page_increment)
1318 if (priv->timer_calls < MAX_TIMER_CALLS)
1319 priv->timer_calls++;
1322 priv->timer_calls = 0;
1323 priv->timer_step += priv->climb_rate;
1334 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1335 GtkSpinButton *spin_button)
1337 gboolean return_val;
1339 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1342 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1343 if (return_val == FALSE)
1344 gtk_spin_button_default_output (spin_button);
1346 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1348 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1350 g_object_notify (G_OBJECT (spin_button), "value");
1354 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1355 GtkScrollType scroll)
1357 GtkSpinButtonPrivate *priv = spin->priv;
1360 /* When the key binding is activated, there may be an outstanding
1361 * value, so we first have to commit what is currently written in
1362 * the spin buttons text entry. See #106574
1364 gtk_spin_button_update (spin);
1366 old_value = priv->adjustment->value;
1368 /* We don't test whether the entry is editable, since
1369 * this key binding conceptually corresponds to changing
1370 * the value with the buttons using the mouse, which
1371 * we allow for non-editable spin buttons.
1375 case GTK_SCROLL_STEP_BACKWARD:
1376 case GTK_SCROLL_STEP_DOWN:
1377 case GTK_SCROLL_STEP_LEFT:
1378 gtk_spin_button_real_spin (spin, -priv->timer_step);
1380 if (priv->climb_rate > 0.0 && priv->timer_step
1381 < priv->adjustment->page_increment)
1383 if (priv->timer_calls < MAX_TIMER_CALLS)
1384 priv->timer_calls++;
1387 priv->timer_calls = 0;
1388 priv->timer_step += priv->climb_rate;
1393 case GTK_SCROLL_STEP_FORWARD:
1394 case GTK_SCROLL_STEP_UP:
1395 case GTK_SCROLL_STEP_RIGHT:
1396 gtk_spin_button_real_spin (spin, priv->timer_step);
1398 if (priv->climb_rate > 0.0 && priv->timer_step
1399 < priv->adjustment->page_increment)
1401 if (priv->timer_calls < MAX_TIMER_CALLS)
1402 priv->timer_calls++;
1405 priv->timer_calls = 0;
1406 priv->timer_step += priv->climb_rate;
1411 case GTK_SCROLL_PAGE_BACKWARD:
1412 case GTK_SCROLL_PAGE_DOWN:
1413 case GTK_SCROLL_PAGE_LEFT:
1414 gtk_spin_button_real_spin (spin, -priv->adjustment->page_increment);
1417 case GTK_SCROLL_PAGE_FORWARD:
1418 case GTK_SCROLL_PAGE_UP:
1419 case GTK_SCROLL_PAGE_RIGHT:
1420 gtk_spin_button_real_spin (spin, priv->adjustment->page_increment);
1423 case GTK_SCROLL_START:
1425 gdouble diff = priv->adjustment->value - priv->adjustment->lower;
1427 gtk_spin_button_real_spin (spin, -diff);
1431 case GTK_SCROLL_END:
1433 gdouble diff = priv->adjustment->upper - priv->adjustment->value;
1435 gtk_spin_button_real_spin (spin, diff);
1440 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1444 gtk_spin_button_update (spin);
1446 if (priv->adjustment->value == old_value)
1447 gtk_widget_error_bell (GTK_WIDGET (spin));
1451 gtk_spin_button_key_release (GtkWidget *widget,
1454 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1455 GtkSpinButtonPrivate *priv = spin->priv;
1457 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1458 priv->timer_step = priv->adjustment->step_increment;
1459 priv->timer_calls = 0;
1465 gtk_spin_button_snap (GtkSpinButton *spin_button,
1468 GtkSpinButtonPrivate *priv = spin_button->priv;
1472 inc = priv->adjustment->step_increment;
1476 tmp = (val - priv->adjustment->lower) / inc;
1477 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1478 val = priv->adjustment->lower + floor (tmp) * inc;
1480 val = priv->adjustment->lower + ceil (tmp) * inc;
1482 gtk_spin_button_set_value (spin_button, val);
1486 gtk_spin_button_activate (GtkEntry *entry)
1488 if (entry->editable)
1489 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1491 /* Chain up so that entry->activates_default is honored */
1492 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1496 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1505 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1507 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1508 panel_width = arrow_size + 2 * gtk_widget_get_style (GTK_WIDGET (entry))->xthickness;
1511 *width -= panel_width;
1513 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1518 gtk_spin_button_insert_text (GtkEditable *editable,
1519 const gchar *new_text,
1520 gint new_text_length,
1523 GtkEntry *entry = GTK_ENTRY (editable);
1524 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1525 GtkSpinButtonPrivate *priv = spin->priv;
1526 GtkEditableInterface *parent_editable_iface;
1528 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1540 const gchar *entry_text;
1542 entry_length = gtk_entry_get_text_length (entry);
1543 entry_text = gtk_entry_get_text (entry);
1547 if (*(lc->negative_sign))
1548 neg_sign = *(lc->negative_sign);
1552 if (*(lc->positive_sign))
1553 pos_sign = *(lc->positive_sign);
1558 /* Workaround for bug caused by some Windows application messing
1559 * up the positive sign of the current locale, more specifically
1560 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1561 * See bug #330743 and for instance
1562 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1564 * I don't know if the positive sign always gets bogusly set to
1565 * a digit when the above Registry value is corrupted as
1566 * described. (In my test case, it got set to "8", and in the
1567 * bug report above it presumably was set ot "0".) Probably it
1568 * might get set to almost anything? So how to distinguish a
1569 * bogus value from some correct one for some locale? That is
1570 * probably hard, but at least we should filter out the
1573 if (pos_sign >= '0' && pos_sign <= '9')
1577 for (sign=0, i=0; i<entry_length; i++)
1578 if ((entry_text[i] == neg_sign) ||
1579 (entry_text[i] == pos_sign))
1585 if (sign && !(*position))
1588 for (dotpos=-1, i=0; i<entry_length; i++)
1589 if (entry_text[i] == *(lc->decimal_point))
1595 if (dotpos > -1 && *position > dotpos &&
1596 (gint)priv->digits - entry_length
1597 + dotpos - new_text_length + 1 < 0)
1600 for (i = 0; i < new_text_length; i++)
1602 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1604 if (sign || (*position) || i)
1608 else if (new_text[i] == *(lc->decimal_point))
1610 if (!priv->digits || dotpos > -1 ||
1611 (new_text_length - 1 - i + entry_length
1612 - *position > (gint)priv->digits))
1614 dotpos = *position + i;
1616 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1621 parent_editable_iface->insert_text (editable, new_text,
1622 new_text_length, position);
1626 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1629 GtkSpinButtonPrivate *priv = spin_button->priv;
1631 gdouble new_value = 0.0;
1632 gboolean wrapped = FALSE;
1634 adj = priv->adjustment;
1636 new_value = adj->value + increment;
1642 if (fabs (adj->value - adj->upper) < EPSILON)
1644 new_value = adj->lower;
1647 else if (new_value > adj->upper)
1648 new_value = adj->upper;
1651 new_value = MIN (new_value, adj->upper);
1653 else if (increment < 0)
1657 if (fabs (adj->value - adj->lower) < EPSILON)
1659 new_value = adj->upper;
1662 else if (new_value < adj->lower)
1663 new_value = adj->lower;
1666 new_value = MAX (new_value, adj->lower);
1669 if (fabs (new_value - adj->value) > EPSILON)
1670 gtk_adjustment_set_value (adj, new_value);
1673 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1675 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1679 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1684 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1686 return GTK_INPUT_ERROR;
1692 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1694 GtkSpinButtonPrivate *priv = spin_button->priv;
1696 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, priv->adjustment->value);
1698 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1699 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1705 /***********************************************************
1706 ***********************************************************
1707 *** Public interface ***
1708 ***********************************************************
1709 ***********************************************************/
1713 * gtk_spin_button_configure:
1714 * @spin_button: a #GtkSpinButton
1715 * @adjustment: (allow-none): a #GtkAdjustment.
1716 * @climb_rate: the new climb rate.
1717 * @digits: the number of decimal places to display in the spin button.
1719 * Changes the properties of an existing spin button. The adjustment, climb rate,
1720 * and number of decimal places are all changed accordingly, after this function call.
1723 gtk_spin_button_configure (GtkSpinButton *spin_button,
1724 GtkAdjustment *adjustment,
1728 GtkSpinButtonPrivate *priv;
1730 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1732 priv = spin_button->priv;
1735 gtk_spin_button_set_adjustment (spin_button, adjustment);
1737 adjustment = priv->adjustment;
1739 g_object_freeze_notify (G_OBJECT (spin_button));
1740 if (priv->digits != digits)
1742 priv->digits = digits;
1743 g_object_notify (G_OBJECT (spin_button), "digits");
1746 if (priv->climb_rate != climb_rate)
1748 priv->climb_rate = climb_rate;
1749 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1751 g_object_thaw_notify (G_OBJECT (spin_button));
1753 gtk_adjustment_value_changed (adjustment);
1757 gtk_spin_button_new (GtkAdjustment *adjustment,
1761 GtkSpinButton *spin;
1764 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1766 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1768 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1770 return GTK_WIDGET (spin);
1774 * gtk_spin_button_new_with_range:
1775 * @min: Minimum allowable value
1776 * @max: Maximum allowable value
1777 * @step: Increment added or subtracted by spinning the widget
1779 * This is a convenience constructor that allows creation of a numeric
1780 * #GtkSpinButton without manually creating an adjustment. The value is
1781 * initially set to the minimum value and a page increment of 10 * @step
1782 * is the default. The precision of the spin button is equivalent to the
1783 * precision of @step.
1785 * Note that the way in which the precision is derived works best if @step
1786 * is a power of ten. If the resulting precision is not suitable for your
1787 * needs, use gtk_spin_button_set_digits() to correct it.
1789 * Return value: The new spin button as a #GtkWidget.
1792 gtk_spin_button_new_with_range (gdouble min,
1797 GtkSpinButton *spin;
1800 g_return_val_if_fail (min <= max, NULL);
1801 g_return_val_if_fail (step != 0.0, NULL);
1803 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1805 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1807 if (fabs (step) >= 1.0 || step == 0.0)
1810 digits = abs ((gint) floor (log10 (fabs (step))));
1811 if (digits > MAX_DIGITS)
1812 digits = MAX_DIGITS;
1815 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1817 gtk_spin_button_set_numeric (spin, TRUE);
1819 return GTK_WIDGET (spin);
1822 /* Callback used when the spin button's adjustment changes. We need to redraw
1823 * the arrows when the adjustment's range changes, and reevaluate our size request.
1826 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1828 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1829 GtkSpinButtonPrivate *priv = spin_button->priv;
1831 priv->timer_step = priv->adjustment->step_increment;
1832 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1836 * gtk_spin_button_set_adjustment:
1837 * @spin_button: a #GtkSpinButton
1838 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1840 * Replaces the #GtkAdjustment associated with @spin_button.
1843 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1844 GtkAdjustment *adjustment)
1846 GtkSpinButtonPrivate *priv;
1848 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1850 priv = spin_button->priv;
1852 if (priv->adjustment != adjustment)
1854 if (priv->adjustment)
1856 g_signal_handlers_disconnect_by_func (priv->adjustment,
1857 gtk_spin_button_value_changed,
1859 g_signal_handlers_disconnect_by_func (priv->adjustment,
1860 adjustment_changed_cb,
1862 g_object_unref (priv->adjustment);
1864 priv->adjustment = adjustment;
1867 g_object_ref_sink (adjustment);
1868 g_signal_connect (adjustment, "value-changed",
1869 G_CALLBACK (gtk_spin_button_value_changed),
1871 g_signal_connect (adjustment, "changed",
1872 G_CALLBACK (adjustment_changed_cb),
1874 priv->timer_step = priv->adjustment->step_increment;
1877 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1880 g_object_notify (G_OBJECT (spin_button), "adjustment");
1884 * gtk_spin_button_get_adjustment:
1885 * @spin_button: a #GtkSpinButton
1887 * Get the adjustment associated with a #GtkSpinButton
1889 * Return value: (transfer none): the #GtkAdjustment of @spin_button
1892 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1894 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1896 return spin_button->priv->adjustment;
1900 * gtk_spin_button_set_digits:
1901 * @spin_button: a #GtkSpinButton
1902 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1904 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1908 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1911 GtkSpinButtonPrivate *priv;
1913 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1915 priv = spin_button->priv;
1917 if (priv->digits != digits)
1919 priv->digits = digits;
1920 gtk_spin_button_value_changed (priv->adjustment, spin_button);
1921 g_object_notify (G_OBJECT (spin_button), "digits");
1923 /* since lower/upper may have changed */
1924 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1929 * gtk_spin_button_get_digits:
1930 * @spin_button: a #GtkSpinButton
1932 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1934 * Returns: the current precision
1937 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1939 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1941 return spin_button->priv->digits;
1945 * gtk_spin_button_set_increments:
1946 * @spin_button: a #GtkSpinButton
1947 * @step: increment applied for a button 1 press.
1948 * @page: increment applied for a button 2 press.
1950 * Sets the step and page increments for spin_button. This affects how
1951 * quickly the value changes when the spin button's arrows are activated.
1954 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1958 GtkSpinButtonPrivate *priv;
1960 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1962 priv = spin_button->priv;
1964 priv->adjustment->step_increment = step;
1965 priv->adjustment->page_increment = page;
1969 * gtk_spin_button_get_increments:
1970 * @spin_button: a #GtkSpinButton
1971 * @step: (allow-none): location to store step increment, or %NULL
1972 * @page: (allow-none): location to store page increment, or %NULL
1974 * Gets the current step and page the increments used by @spin_button. See
1975 * gtk_spin_button_set_increments().
1978 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1982 GtkSpinButtonPrivate *priv;
1984 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1986 priv = spin_button->priv;
1989 *step = priv->adjustment->step_increment;
1991 *page = priv->adjustment->page_increment;
1995 * gtk_spin_button_set_range:
1996 * @spin_button: a #GtkSpinButton
1997 * @min: minimum allowable value
1998 * @max: maximum allowable value
2000 * Sets the minimum and maximum allowable values for @spin_button
2003 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2007 GtkSpinButtonPrivate *priv;
2010 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2012 priv = spin_button->priv;
2014 priv->adjustment->lower = min;
2015 priv->adjustment->upper = max;
2017 value = CLAMP (priv->adjustment->value,
2018 priv->adjustment->lower,
2019 (priv->adjustment->upper - priv->adjustment->page_size));
2021 if (value != priv->adjustment->value)
2022 gtk_spin_button_set_value (spin_button, value);
2024 gtk_adjustment_changed (priv->adjustment);
2028 * gtk_spin_button_get_range:
2029 * @spin_button: a #GtkSpinButton
2030 * @min: (allow-none): location to store minimum allowed value, or %NULL
2031 * @max: (allow-none): location to store maximum allowed value, or %NULL
2033 * Gets the range allowed for @spin_button. See
2034 * gtk_spin_button_set_range().
2037 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2041 GtkSpinButtonPrivate *priv;
2043 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2045 priv = spin_button->priv;
2048 *min = priv->adjustment->lower;
2050 *max = priv->adjustment->upper;
2054 * gtk_spin_button_get_value:
2055 * @spin_button: a #GtkSpinButton
2057 * Get the value in the @spin_button.
2059 * Return value: the value of @spin_button
2062 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2064 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2066 return spin_button->priv->adjustment->value;
2070 * gtk_spin_button_get_value_as_int:
2071 * @spin_button: a #GtkSpinButton
2073 * Get the value @spin_button represented as an integer.
2075 * Return value: the value of @spin_button
2078 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2080 GtkSpinButtonPrivate *priv;
2083 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2085 priv = spin_button->priv;
2087 val = priv->adjustment->value;
2088 if (val - floor (val) < ceil (val) - val)
2095 * gtk_spin_button_set_value:
2096 * @spin_button: a #GtkSpinButton
2097 * @value: the new value
2099 * Set the value of @spin_button.
2102 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2105 GtkSpinButtonPrivate *priv;
2107 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2109 priv = spin_button->priv;
2111 if (fabs (value - priv->adjustment->value) > EPSILON)
2112 gtk_adjustment_set_value (priv->adjustment, value);
2115 gint return_val = FALSE;
2116 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2117 if (return_val == FALSE)
2118 gtk_spin_button_default_output (spin_button);
2123 * gtk_spin_button_set_update_policy:
2124 * @spin_button: a #GtkSpinButton
2125 * @policy: a #GtkSpinButtonUpdatePolicy value
2127 * Sets the update behavior of a spin button. This determines whether the
2128 * spin button is always updated or only when a valid value is set.
2131 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2132 GtkSpinButtonUpdatePolicy policy)
2134 GtkSpinButtonPrivate *priv;
2136 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2138 priv = spin_button->priv;
2140 if (priv->update_policy != policy)
2142 priv->update_policy = policy;
2143 g_object_notify (G_OBJECT (spin_button), "update-policy");
2148 * gtk_spin_button_get_update_policy:
2149 * @spin_button: a #GtkSpinButton
2151 * Gets the update behavior of a spin button. See
2152 * gtk_spin_button_set_update_policy().
2154 * Return value: the current update policy
2156 GtkSpinButtonUpdatePolicy
2157 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2159 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2161 return spin_button->priv->update_policy;
2165 * gtk_spin_button_set_numeric:
2166 * @spin_button: a #GtkSpinButton
2167 * @numeric: flag indicating if only numeric entry is allowed.
2169 * Sets the flag that determines if non-numeric text can be typed into
2173 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2176 GtkSpinButtonPrivate *priv;
2178 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2180 priv = spin_button->priv;
2182 numeric = numeric != FALSE;
2184 if (priv->numeric != numeric)
2186 priv->numeric = numeric;
2187 g_object_notify (G_OBJECT (spin_button), "numeric");
2192 * gtk_spin_button_get_numeric:
2193 * @spin_button: a #GtkSpinButton
2195 * Returns whether non-numeric text can be typed into the spin button.
2196 * See gtk_spin_button_set_numeric().
2198 * Return value: %TRUE if only numeric text can be entered
2201 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2203 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2205 return spin_button->priv->numeric;
2209 * gtk_spin_button_set_wrap:
2210 * @spin_button: a #GtkSpinButton
2211 * @wrap: a flag indicating if wrapping behavior is performed.
2213 * Sets the flag that determines if a spin button value wraps around to the
2214 * opposite limit when the upper or lower limit of the range is exceeded.
2217 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2220 GtkSpinButtonPrivate *priv;
2222 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2224 priv = spin_button->priv;
2226 wrap = wrap != FALSE;
2228 if (priv->wrap != wrap)
2230 priv->wrap = (wrap != 0);
2232 g_object_notify (G_OBJECT (spin_button), "wrap");
2237 * gtk_spin_button_get_wrap:
2238 * @spin_button: a #GtkSpinButton
2240 * Returns whether the spin button's value wraps around to the
2241 * opposite limit when the upper or lower limit of the range is
2242 * exceeded. See gtk_spin_button_set_wrap().
2244 * Return value: %TRUE if the spin button wraps around
2247 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2249 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2251 return spin_button->priv->wrap;
2255 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2261 style = gtk_widget_get_style (GTK_WIDGET (spin_button));
2262 size = pango_font_description_get_size (style->font_desc);
2263 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2265 return arrow_size - arrow_size % 2; /* force even */
2269 * spin_button_get_shadow_type:
2270 * @spin_button: a #GtkSpinButton
2272 * Convenience function to Get the shadow type from the underlying widget's
2275 * Return value: the #GtkShadowType
2278 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2280 GtkShadowType rc_shadow_type;
2282 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow-type", &rc_shadow_type, NULL);
2284 return rc_shadow_type;
2288 * gtk_spin_button_set_snap_to_ticks:
2289 * @spin_button: a #GtkSpinButton
2290 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2292 * Sets the policy as to whether values are corrected to the nearest step
2293 * increment when a spin button is activated after providing an invalid value.
2296 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2297 gboolean snap_to_ticks)
2299 GtkSpinButtonPrivate *priv;
2302 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2304 priv = spin_button->priv;
2306 new_val = (snap_to_ticks != 0);
2308 if (new_val != priv->snap_to_ticks)
2310 priv->snap_to_ticks = new_val;
2311 if (new_val && GTK_ENTRY (spin_button)->editable)
2312 gtk_spin_button_update (spin_button);
2314 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2319 * gtk_spin_button_get_snap_to_ticks:
2320 * @spin_button: a #GtkSpinButton
2322 * Returns whether the values are corrected to the nearest step. See
2323 * gtk_spin_button_set_snap_to_ticks().
2325 * Return value: %TRUE if values are snapped to the nearest step.
2328 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2330 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2332 return spin_button->priv->snap_to_ticks;
2336 * gtk_spin_button_spin:
2337 * @spin_button: a #GtkSpinButton
2338 * @direction: a #GtkSpinType indicating the direction to spin.
2339 * @increment: step increment to apply in the specified direction.
2341 * Increment or decrement a spin button's value in a specified direction
2342 * by a specified amount.
2345 gtk_spin_button_spin (GtkSpinButton *spin_button,
2346 GtkSpinType direction,
2349 GtkSpinButtonPrivate *priv;
2353 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2355 priv = spin_button->priv;
2357 adj = priv->adjustment;
2359 /* for compatibility with the 1.0.x version of this function */
2360 if (increment != 0 && increment != adj->step_increment &&
2361 (direction == GTK_SPIN_STEP_FORWARD ||
2362 direction == GTK_SPIN_STEP_BACKWARD))
2364 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2365 increment = -increment;
2366 direction = GTK_SPIN_USER_DEFINED;
2371 case GTK_SPIN_STEP_FORWARD:
2373 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2376 case GTK_SPIN_STEP_BACKWARD:
2378 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2381 case GTK_SPIN_PAGE_FORWARD:
2383 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2386 case GTK_SPIN_PAGE_BACKWARD:
2388 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2393 diff = adj->value - adj->lower;
2395 gtk_spin_button_real_spin (spin_button, -diff);
2400 diff = adj->upper - adj->value;
2402 gtk_spin_button_real_spin (spin_button, diff);
2405 case GTK_SPIN_USER_DEFINED:
2408 gtk_spin_button_real_spin (spin_button, increment);
2417 * gtk_spin_button_update:
2418 * @spin_button: a #GtkSpinButton
2420 * Manually force an update of the spin button.
2423 gtk_spin_button_update (GtkSpinButton *spin_button)
2425 GtkSpinButtonPrivate *priv;
2430 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2432 priv = spin_button->priv;
2435 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2436 if (return_val == FALSE)
2438 return_val = gtk_spin_button_default_input (spin_button, &val);
2439 error = (return_val == GTK_INPUT_ERROR);
2441 else if (return_val == GTK_INPUT_ERROR)
2444 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2446 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2448 if (val < priv->adjustment->lower)
2449 val = priv->adjustment->lower;
2450 else if (val > priv->adjustment->upper)
2451 val = priv->adjustment->upper;
2453 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2455 val < priv->adjustment->lower ||
2456 val > priv->adjustment->upper))
2458 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2462 if (priv->snap_to_ticks)
2463 gtk_spin_button_snap (spin_button, val);
2465 gtk_spin_button_set_value (spin_button, val);
2469 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2471 return spin_button->priv->panel;