1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GtkSpinButton widget for GTK+
5 * Copyright (C) 1998 Lars Hamann and Stefan Jeske
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
36 #include "gdk/gdkkeysyms.h"
37 #include "gtkbindings.h"
38 #include "gtkspinbutton.h"
39 #include "gtkentryprivate.h"
41 #include "gtkmarshalers.h"
42 #include "gtksettings.h"
43 #include "gtkprivate.h"
46 #define MIN_SPIN_BUTTON_WIDTH 30
47 #define MAX_TIMER_CALLS 5
50 #define MIN_ARROW_WIDTH 6
53 struct _GtkSpinButtonPrivate
55 GtkSpinButtonUpdatePolicy update_policy;
56 GtkAdjustment *adjustment;
66 guint click_child : 2; /* valid: GTK_ARROW_UP=0, GTK_ARROW_DOWN=1 or 2=NONE/BOTH */
71 guint snap_to_ticks : 1;
72 guint timer_calls : 3;
99 static void gtk_spin_button_editable_init (GtkEditableInterface *iface);
100 static void gtk_spin_button_finalize (GObject *object);
101 static void gtk_spin_button_set_property (GObject *object,
105 static void gtk_spin_button_get_property (GObject *object,
109 static void gtk_spin_button_destroy (GtkWidget *widget);
110 static void gtk_spin_button_map (GtkWidget *widget);
111 static void gtk_spin_button_unmap (GtkWidget *widget);
112 static void gtk_spin_button_realize (GtkWidget *widget);
113 static void gtk_spin_button_unrealize (GtkWidget *widget);
114 static void gtk_spin_button_get_preferred_width (GtkWidget *widget,
118 static void gtk_spin_button_size_allocate (GtkWidget *widget,
119 GtkAllocation *allocation);
120 static gint gtk_spin_button_draw (GtkWidget *widget,
122 static gint gtk_spin_button_button_press (GtkWidget *widget,
123 GdkEventButton *event);
124 static gint gtk_spin_button_button_release (GtkWidget *widget,
125 GdkEventButton *event);
126 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
127 GdkEventMotion *event);
128 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
129 GdkEventCrossing *event);
130 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
131 GdkEventCrossing *event);
132 static gint gtk_spin_button_focus_out (GtkWidget *widget,
133 GdkEventFocus *event);
134 static void gtk_spin_button_grab_notify (GtkWidget *widget,
135 gboolean was_grabbed);
136 static void gtk_spin_button_state_flags_changed (GtkWidget *widget,
137 GtkStateFlags previous_state);
138 static void gtk_spin_button_style_updated (GtkWidget *widget);
139 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
140 GtkStyleContext *context,
142 GtkArrowType arrow_type);
143 static gboolean gtk_spin_button_timer (GtkSpinButton *spin_button);
144 static void gtk_spin_button_stop_spinning (GtkSpinButton *spin);
145 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
146 GtkSpinButton *spin_button);
147 static gint gtk_spin_button_key_release (GtkWidget *widget,
149 static gint gtk_spin_button_scroll (GtkWidget *widget,
150 GdkEventScroll *event);
151 static void gtk_spin_button_activate (GtkEntry *entry);
152 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
157 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
159 static void gtk_spin_button_insert_text (GtkEditable *editable,
160 const gchar *new_text,
161 gint new_text_length,
163 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
165 static void gtk_spin_button_real_change_value (GtkSpinButton *spin,
166 GtkScrollType scroll);
168 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
170 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
172 static gint spin_button_get_arrow_size (GtkSpinButton *spin_button);
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 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 widget_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->get_preferred_width = gtk_spin_button_get_preferred_width;
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_flags_changed = gtk_spin_button_state_flags_changed;
217 widget_class->style_updated = gtk_spin_button_style_updated;
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 = 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;
507 GtkStyleContext *context;
509 spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
510 GTK_TYPE_SPIN_BUTTON,
511 GtkSpinButtonPrivate);
512 priv = spin_button->priv;
514 priv->adjustment = NULL;
517 priv->climb_rate = 0.0;
518 priv->timer_step = 0.0;
519 priv->update_policy = GTK_UPDATE_ALWAYS;
520 priv->in_child = NO_ARROW;
521 priv->click_child = NO_ARROW;
523 priv->need_timer = FALSE;
524 priv->timer_calls = 0;
526 priv->numeric = FALSE;
528 priv->snap_to_ticks = FALSE;
530 gtk_spin_button_set_adjustment (spin_button,
531 gtk_adjustment_new (0, 0, 0, 0, 0, 0));
533 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
534 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINBUTTON);
538 gtk_spin_button_finalize (GObject *object)
540 gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
542 G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
546 gtk_spin_button_destroy (GtkWidget *widget)
548 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
550 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->destroy (widget);
554 gtk_spin_button_map (GtkWidget *widget)
556 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
557 GtkSpinButtonPrivate *priv = spin_button->priv;
559 if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
561 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
562 gdk_window_show (priv->panel);
567 gtk_spin_button_unmap (GtkWidget *widget)
569 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
570 GtkSpinButtonPrivate *priv = spin_button->priv;
572 if (gtk_widget_get_mapped (widget))
574 gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
576 gdk_window_hide (priv->panel);
577 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
582 gtk_spin_button_realize (GtkWidget *widget)
584 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
585 GtkSpinButtonPrivate *priv = spin_button->priv;
586 GtkStyleContext *context;
588 GtkAllocation allocation;
589 GtkRequisition requisition;
590 GdkWindowAttr attributes;
591 gint attributes_mask;
596 arrow_size = spin_button_get_arrow_size (spin_button);
598 gtk_widget_get_preferred_size (widget, &requisition, NULL);
599 gtk_widget_get_allocation (widget, &allocation);
601 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
602 GDK_KEY_RELEASE_MASK);
603 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
605 attributes.window_type = GDK_WINDOW_CHILD;
606 attributes.wclass = GDK_INPUT_ONLY;
607 attributes.visual = gtk_widget_get_visual (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;
615 state = gtk_widget_get_state_flags (widget);
616 context = gtk_widget_get_style_context (widget);
617 gtk_style_context_get_padding (context, state, &padding);
619 attributes.x = allocation.x + allocation.width - arrow_size - (padding.left + padding.right);
620 attributes.y = allocation.y + (allocation.height - requisition.height) / 2;
621 attributes.width = arrow_size + padding.left + padding.right;
622 attributes.height = requisition.height;
624 priv->panel = gdk_window_new (gtk_widget_get_window (widget),
625 &attributes, attributes_mask);
626 gdk_window_set_user_data (priv->panel, widget);
629 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
630 if (return_val == FALSE)
631 gtk_spin_button_default_output (spin_button);
633 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
637 gtk_spin_button_unrealize (GtkWidget *widget)
639 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
640 GtkSpinButtonPrivate *priv = spin->priv;
642 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
646 gdk_window_set_user_data (priv->panel, NULL);
647 gdk_window_destroy (priv->panel);
653 compute_double_length (double val, int digits)
659 if (fabs (val) > 1.0)
660 a = floor (log10 (fabs (val))) + 1;
672 return a + digits + extra;
676 gtk_spin_button_get_preferred_width (GtkWidget *widget,
680 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
681 GtkSpinButtonPrivate *priv = spin_button->priv;
682 GtkEntry *entry = GTK_ENTRY (widget);
683 GtkStyleContext *style_context;
687 style_context = gtk_widget_get_style_context (widget);
689 arrow_size = spin_button_get_arrow_size (spin_button);
691 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->get_preferred_width (widget, minimum, natural);
693 if (gtk_entry_get_width_chars (entry) < 0)
695 PangoContext *context;
696 const PangoFontDescription *font_desc;
697 PangoFontMetrics *metrics;
703 gboolean interior_focus;
705 gint xborder, yborder;
706 GtkBorder inner_border;
708 gtk_widget_style_get (widget,
709 "interior-focus", &interior_focus,
710 "focus-line-width", &focus_width,
713 font_desc = gtk_style_context_get_font (style_context, 0);
715 context = gtk_widget_get_pango_context (widget);
716 metrics = pango_context_get_metrics (context, font_desc,
717 pango_context_get_language (context));
719 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
720 digit_width = PANGO_SCALE *
721 ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
723 pango_font_metrics_unref (metrics);
725 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
726 width = MIN_SPIN_BUTTON_WIDTH;
727 max_string_len = MAX (10, compute_double_length (1e9 * priv->adjustment->step_increment,
730 string_len = compute_double_length (priv->adjustment->upper,
732 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
733 width = MAX (width, w);
734 string_len = compute_double_length (priv->adjustment->lower, priv->digits);
735 w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
736 width = MAX (width, w);
738 _gtk_entry_get_borders (entry, &xborder, &yborder);
739 _gtk_entry_effective_inner_border (entry, &inner_border);
741 width += xborder * 2 + inner_border.left + inner_border.right;
747 gtk_style_context_get_padding (style_context,
748 gtk_widget_get_state_flags (widget),
751 *minimum += arrow_size + padding.left + padding.right;
752 *natural += arrow_size + padding.left + padding.right;
756 gtk_spin_button_size_allocate (GtkWidget *widget,
757 GtkAllocation *allocation)
759 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
760 GtkSpinButtonPrivate *priv = spin->priv;
761 GtkAllocation panel_allocation;
762 GtkRequisition requisition;
763 GtkStyleContext *context;
769 arrow_size = spin_button_get_arrow_size (spin);
770 context = gtk_widget_get_style_context (widget);
771 state = gtk_widget_get_state_flags (widget);
773 gtk_style_context_get_padding (context, state, &padding);
774 panel_width = arrow_size + padding.left + padding.right;
776 gtk_widget_get_preferred_size (widget, &requisition, NULL);
778 gtk_widget_set_allocation (widget, allocation);
780 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
781 panel_allocation.x = allocation->x;
783 panel_allocation.x = allocation->x + allocation->width - panel_width;
785 panel_allocation.width = panel_width;
786 panel_allocation.height = MIN (requisition.height, allocation->height);
788 panel_allocation.y = allocation->y +
789 (allocation->height - requisition.height) / 2;
791 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
793 if (gtk_widget_get_realized (widget))
795 gdk_window_move_resize (priv->panel,
798 panel_allocation.width,
799 panel_allocation.height);
802 gtk_widget_queue_draw (GTK_WIDGET (spin));
806 gtk_spin_button_draw (GtkWidget *widget,
809 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
810 GtkSpinButtonPrivate *priv = spin->priv;
811 GtkStyleContext *context;
812 GtkStateFlags state = 0;
815 is_rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
816 context = gtk_widget_get_style_context (widget);
817 gtk_style_context_save (context);
820 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_LEFT);
822 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_RIGHT);
824 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
826 gtk_style_context_restore (context);
829 state = gtk_widget_get_state_flags (widget);
831 if (gtk_widget_has_focus (widget))
832 state |= GTK_STATE_FLAG_FOCUSED;
834 gtk_style_context_save (context);
835 gtk_style_context_set_state (context, state);
836 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_ENTRY);
839 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_RIGHT);
841 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_LEFT);
843 gtk_cairo_transform_to_window (cr, widget, priv->panel);
845 if (gtk_entry_get_has_frame (GTK_ENTRY (widget)))
846 gtk_render_background (context, cr, 0, 0,
847 gdk_window_get_width (priv->panel),
848 gdk_window_get_height (priv->panel));
850 gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_UP);
851 gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_DOWN);
853 gtk_style_context_restore (context);
860 spin_button_at_limit (GtkSpinButton *spin_button,
863 GtkSpinButtonPrivate *priv = spin_button->priv;
864 GtkArrowType effective_arrow;
869 if (priv->adjustment->step_increment > 0)
870 effective_arrow = arrow;
872 effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
874 if (effective_arrow == GTK_ARROW_UP &&
875 (priv->adjustment->upper - priv->adjustment->value <= EPSILON))
878 if (effective_arrow == GTK_ARROW_DOWN &&
879 (priv->adjustment->value - priv->adjustment->lower <= EPSILON))
886 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
887 GtkStyleContext *context,
889 GtkArrowType arrow_type)
891 GtkSpinButtonPrivate *priv;
892 GtkJunctionSides junction;
904 g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
906 gtk_style_context_save (context);
907 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
909 priv = spin_button->priv;
910 widget = GTK_WIDGET (spin_button);
911 junction = gtk_style_context_get_junction_sides (context);
913 panel_height = gdk_window_get_height (priv->panel);
915 if (arrow_type == GTK_ARROW_UP)
920 height = panel_height / 2;
922 junction |= GTK_JUNCTION_BOTTOM;
927 y = panel_height / 2;
929 height = (panel_height + 1) / 2;
931 junction |= GTK_JUNCTION_TOP;
934 if (spin_button_at_limit (spin_button, arrow_type))
935 state = GTK_STATE_FLAG_INSENSITIVE;
938 if (priv->click_child == arrow_type)
939 state = GTK_STATE_ACTIVE;
942 if (priv->in_child == arrow_type &&
943 priv->click_child == NO_ARROW)
944 state = GTK_STATE_FLAG_PRELIGHT;
946 state = gtk_widget_get_state_flags (widget);
950 gtk_style_context_get_padding (context, state, &padding);
951 gtk_style_context_set_junction_sides (context, junction);
952 gtk_style_context_set_state (context, state);
954 width = spin_button_get_arrow_size (spin_button) + padding.left + padding.right;
956 gtk_render_background (context, cr, x, y, width, height);
957 gtk_render_frame (context, cr, x, y, width, height);
959 height = panel_height;
961 if (arrow_type == GTK_ARROW_DOWN)
964 height = height - y - 2;
969 height = height / 2 - 2;
974 if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
980 w -= w % 2 - 1; /* force odd */
983 x += (width - w) / 2;
984 y += (height - h) / 2;
989 gtk_render_arrow (context, cr,
992 gtk_style_context_restore (context);
996 gtk_spin_button_enter_notify (GtkWidget *widget,
997 GdkEventCrossing *event)
999 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1000 GtkSpinButtonPrivate *priv = spin->priv;
1001 GtkRequisition requisition;
1003 if (event->window == priv->panel)
1009 device = gdk_event_get_device ((GdkEvent *) event);
1010 gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
1012 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1014 if (y <= requisition.height / 2)
1015 priv->in_child = GTK_ARROW_UP;
1017 priv->in_child = GTK_ARROW_DOWN;
1019 gtk_widget_queue_draw (GTK_WIDGET (spin));
1022 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event)
1023 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1029 gtk_spin_button_leave_notify (GtkWidget *widget,
1030 GdkEventCrossing *event)
1032 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1033 GtkSpinButtonPrivate *priv = spin->priv;
1035 priv->in_child = NO_ARROW;
1036 gtk_widget_queue_draw (GTK_WIDGET (spin));
1038 if (GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event)
1039 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1045 gtk_spin_button_focus_out (GtkWidget *widget,
1046 GdkEventFocus *event)
1048 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1049 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1051 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1055 gtk_spin_button_grab_notify (GtkWidget *widget,
1056 gboolean was_grabbed)
1058 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1062 gtk_spin_button_stop_spinning (spin);
1063 gtk_widget_queue_draw (GTK_WIDGET (spin));
1068 gtk_spin_button_state_flags_changed (GtkWidget *widget,
1069 GtkStateFlags previous_state)
1071 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1073 if (!gtk_widget_is_sensitive (widget))
1075 gtk_spin_button_stop_spinning (spin);
1076 gtk_widget_queue_draw (GTK_WIDGET (spin));
1081 gtk_spin_button_style_updated (GtkWidget *widget)
1083 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1084 GtkSpinButtonPrivate *priv = spin->priv;
1086 if (gtk_widget_get_realized (widget))
1088 GtkStyleContext *context;
1090 context = gtk_widget_get_style_context (widget);
1091 gtk_style_context_set_background (context, priv->panel);
1094 GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_updated (widget);
1099 gtk_spin_button_scroll (GtkWidget *widget,
1100 GdkEventScroll *event)
1102 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1103 GtkSpinButtonPrivate *priv = spin->priv;
1105 if (event->direction == GDK_SCROLL_UP)
1107 if (!gtk_widget_has_focus (widget))
1108 gtk_widget_grab_focus (widget);
1109 gtk_spin_button_real_spin (spin, priv->adjustment->step_increment);
1111 else if (event->direction == GDK_SCROLL_DOWN)
1113 if (!gtk_widget_has_focus (widget))
1114 gtk_widget_grab_focus (widget);
1115 gtk_spin_button_real_spin (spin, -priv->adjustment->step_increment);
1124 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1126 GtkSpinButtonPrivate *priv = spin->priv;
1130 g_source_remove (priv->timer);
1132 priv->timer_calls = 0;
1133 priv->need_timer = FALSE;
1138 priv->timer_step = priv->adjustment->step_increment;
1139 priv->timer_calls = 0;
1141 priv->click_child = NO_ARROW;
1146 start_spinning (GtkSpinButton *spin,
1147 GtkArrowType click_child,
1150 GtkSpinButtonPrivate *priv;
1152 g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1156 priv->click_child = click_child;
1160 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1163 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1165 priv->timer_step = step;
1166 priv->need_timer = TRUE;
1167 priv->timer = gdk_threads_add_timeout (timeout,
1168 (GSourceFunc) gtk_spin_button_timer,
1171 gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1173 gtk_widget_queue_draw (GTK_WIDGET (spin));
1177 gtk_spin_button_button_press (GtkWidget *widget,
1178 GdkEventButton *event)
1180 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1181 GtkSpinButtonPrivate *priv = spin->priv;
1185 if (event->window == priv->panel)
1187 GtkRequisition requisition;
1189 if (!gtk_widget_has_focus (widget))
1190 gtk_widget_grab_focus (widget);
1191 priv->button = event->button;
1193 if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1194 gtk_spin_button_update (spin);
1196 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1198 if (event->y <= requisition.height / 2)
1200 if (event->button == 1)
1201 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->step_increment);
1202 else if (event->button == 2)
1203 start_spinning (spin, GTK_ARROW_UP, priv->adjustment->page_increment);
1205 priv->click_child = GTK_ARROW_UP;
1209 if (event->button == 1)
1210 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->step_increment);
1211 else if (event->button == 2)
1212 start_spinning (spin, GTK_ARROW_DOWN, priv->adjustment->page_increment);
1214 priv->click_child = GTK_ARROW_DOWN;
1219 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1225 gtk_spin_button_button_release (GtkWidget *widget,
1226 GdkEventButton *event)
1228 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1229 GtkSpinButtonPrivate *priv = spin->priv;
1232 arrow_size = spin_button_get_arrow_size (spin);
1234 if (event->button == priv->button)
1236 int click_child = priv->click_child;
1238 gtk_spin_button_stop_spinning (spin);
1240 if (event->button == 3)
1242 GtkRequisition requisition;
1243 GtkStyleContext *context;
1244 GtkStateFlags state;
1247 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1249 context = gtk_widget_get_style_context (widget);
1250 state = gtk_widget_get_state_flags (widget);
1251 gtk_style_context_get_padding (context, state, &padding);
1253 if (event->y >= 0 && event->x >= 0 &&
1254 event->y <= requisition.height &&
1255 event->x <= arrow_size + padding.left + padding.right)
1257 if (click_child == GTK_ARROW_UP &&
1258 event->y <= requisition.height / 2)
1262 diff = priv->adjustment->upper - priv->adjustment->value;
1264 gtk_spin_button_real_spin (spin, diff);
1266 else if (click_child == GTK_ARROW_DOWN &&
1267 event->y > requisition.height / 2)
1271 diff = priv->adjustment->value - priv->adjustment->lower;
1273 gtk_spin_button_real_spin (spin, -diff);
1277 gtk_widget_queue_draw (GTK_WIDGET (spin));
1282 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1286 gtk_spin_button_motion_notify (GtkWidget *widget,
1287 GdkEventMotion *event)
1289 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1290 GtkSpinButtonPrivate *priv = spin->priv;
1295 if (event->window == priv->panel)
1297 GtkRequisition requisition;
1300 gdk_event_request_motions (event);
1302 gtk_widget_get_preferred_size (widget, &requisition, NULL);
1304 if (y <= requisition.height / 2 &&
1305 priv->in_child == GTK_ARROW_DOWN)
1307 priv->in_child = GTK_ARROW_UP;
1308 gtk_widget_queue_draw (GTK_WIDGET (spin));
1310 else if (y > requisition.height / 2 &&
1311 priv->in_child == GTK_ARROW_UP)
1313 priv->in_child = GTK_ARROW_DOWN;
1314 gtk_widget_queue_draw (GTK_WIDGET (spin));
1320 return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1324 gtk_spin_button_timer (GtkSpinButton *spin_button)
1326 GtkSpinButtonPrivate *priv = spin_button->priv;
1327 gboolean retval = FALSE;
1331 if (priv->click_child == GTK_ARROW_UP)
1332 gtk_spin_button_real_spin (spin_button, priv->timer_step);
1334 gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1336 if (priv->need_timer)
1338 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1341 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1343 priv->need_timer = FALSE;
1344 priv->timer = gdk_threads_add_timeout (timeout,
1345 (GSourceFunc) gtk_spin_button_timer,
1346 (gpointer) spin_button);
1350 if (priv->climb_rate > 0.0 && priv->timer_step
1351 < priv->adjustment->page_increment)
1353 if (priv->timer_calls < MAX_TIMER_CALLS)
1354 priv->timer_calls++;
1357 priv->timer_calls = 0;
1358 priv->timer_step += priv->climb_rate;
1369 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1370 GtkSpinButton *spin_button)
1372 gboolean return_val;
1374 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1377 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1378 if (return_val == FALSE)
1379 gtk_spin_button_default_output (spin_button);
1381 g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1383 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1385 g_object_notify (G_OBJECT (spin_button), "value");
1389 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1390 GtkScrollType scroll)
1392 GtkSpinButtonPrivate *priv = spin->priv;
1395 /* When the key binding is activated, there may be an outstanding
1396 * value, so we first have to commit what is currently written in
1397 * the spin buttons text entry. See #106574
1399 gtk_spin_button_update (spin);
1401 old_value = priv->adjustment->value;
1403 /* We don't test whether the entry is editable, since
1404 * this key binding conceptually corresponds to changing
1405 * the value with the buttons using the mouse, which
1406 * we allow for non-editable spin buttons.
1410 case GTK_SCROLL_STEP_BACKWARD:
1411 case GTK_SCROLL_STEP_DOWN:
1412 case GTK_SCROLL_STEP_LEFT:
1413 gtk_spin_button_real_spin (spin, -priv->timer_step);
1415 if (priv->climb_rate > 0.0 && priv->timer_step
1416 < priv->adjustment->page_increment)
1418 if (priv->timer_calls < MAX_TIMER_CALLS)
1419 priv->timer_calls++;
1422 priv->timer_calls = 0;
1423 priv->timer_step += priv->climb_rate;
1428 case GTK_SCROLL_STEP_FORWARD:
1429 case GTK_SCROLL_STEP_UP:
1430 case GTK_SCROLL_STEP_RIGHT:
1431 gtk_spin_button_real_spin (spin, priv->timer_step);
1433 if (priv->climb_rate > 0.0 && priv->timer_step
1434 < priv->adjustment->page_increment)
1436 if (priv->timer_calls < MAX_TIMER_CALLS)
1437 priv->timer_calls++;
1440 priv->timer_calls = 0;
1441 priv->timer_step += priv->climb_rate;
1446 case GTK_SCROLL_PAGE_BACKWARD:
1447 case GTK_SCROLL_PAGE_DOWN:
1448 case GTK_SCROLL_PAGE_LEFT:
1449 gtk_spin_button_real_spin (spin, -priv->adjustment->page_increment);
1452 case GTK_SCROLL_PAGE_FORWARD:
1453 case GTK_SCROLL_PAGE_UP:
1454 case GTK_SCROLL_PAGE_RIGHT:
1455 gtk_spin_button_real_spin (spin, priv->adjustment->page_increment);
1458 case GTK_SCROLL_START:
1460 gdouble diff = priv->adjustment->value - priv->adjustment->lower;
1462 gtk_spin_button_real_spin (spin, -diff);
1466 case GTK_SCROLL_END:
1468 gdouble diff = priv->adjustment->upper - priv->adjustment->value;
1470 gtk_spin_button_real_spin (spin, diff);
1475 g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1479 gtk_spin_button_update (spin);
1481 if (priv->adjustment->value == old_value)
1482 gtk_widget_error_bell (GTK_WIDGET (spin));
1486 gtk_spin_button_key_release (GtkWidget *widget,
1489 GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1490 GtkSpinButtonPrivate *priv = spin->priv;
1492 /* We only get a release at the end of a key repeat run, so reset the timer_step */
1493 priv->timer_step = priv->adjustment->step_increment;
1494 priv->timer_calls = 0;
1500 gtk_spin_button_snap (GtkSpinButton *spin_button,
1503 GtkSpinButtonPrivate *priv = spin_button->priv;
1507 inc = priv->adjustment->step_increment;
1511 tmp = (val - priv->adjustment->lower) / inc;
1512 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1513 val = priv->adjustment->lower + floor (tmp) * inc;
1515 val = priv->adjustment->lower + ceil (tmp) * inc;
1517 gtk_spin_button_set_value (spin_button, val);
1521 gtk_spin_button_activate (GtkEntry *entry)
1523 if (gtk_editable_get_editable (GTK_EDITABLE (entry)))
1524 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1526 /* Chain up so that entry->activates_default is honored */
1527 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1531 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1537 GtkStyleContext *context;
1538 GtkStateFlags state;
1544 GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1546 widget = GTK_WIDGET (entry);
1547 state = gtk_widget_get_state_flags (widget);
1548 context = gtk_widget_get_style_context (widget);
1549 gtk_style_context_get_padding (context, state, &padding);
1551 arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1552 panel_width = arrow_size + padding.left + padding.right;
1555 *width -= panel_width;
1557 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1562 gtk_spin_button_insert_text (GtkEditable *editable,
1563 const gchar *new_text,
1564 gint new_text_length,
1567 GtkEntry *entry = GTK_ENTRY (editable);
1568 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1569 GtkSpinButtonPrivate *priv = spin->priv;
1570 GtkEditableInterface *parent_editable_iface;
1572 parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1584 const gchar *entry_text;
1586 entry_length = gtk_entry_get_text_length (entry);
1587 entry_text = gtk_entry_get_text (entry);
1591 if (*(lc->negative_sign))
1592 neg_sign = *(lc->negative_sign);
1596 if (*(lc->positive_sign))
1597 pos_sign = *(lc->positive_sign);
1602 /* Workaround for bug caused by some Windows application messing
1603 * up the positive sign of the current locale, more specifically
1604 * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1605 * See bug #330743 and for instance
1606 * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1608 * I don't know if the positive sign always gets bogusly set to
1609 * a digit when the above Registry value is corrupted as
1610 * described. (In my test case, it got set to "8", and in the
1611 * bug report above it presumably was set ot "0".) Probably it
1612 * might get set to almost anything? So how to distinguish a
1613 * bogus value from some correct one for some locale? That is
1614 * probably hard, but at least we should filter out the
1617 if (pos_sign >= '0' && pos_sign <= '9')
1621 for (sign=0, i=0; i<entry_length; i++)
1622 if ((entry_text[i] == neg_sign) ||
1623 (entry_text[i] == pos_sign))
1629 if (sign && !(*position))
1632 for (dotpos=-1, i=0; i<entry_length; i++)
1633 if (entry_text[i] == *(lc->decimal_point))
1639 if (dotpos > -1 && *position > dotpos &&
1640 (gint)priv->digits - entry_length
1641 + dotpos - new_text_length + 1 < 0)
1644 for (i = 0; i < new_text_length; i++)
1646 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1648 if (sign || (*position) || i)
1652 else if (new_text[i] == *(lc->decimal_point))
1654 if (!priv->digits || dotpos > -1 ||
1655 (new_text_length - 1 - i + entry_length
1656 - *position > (gint)priv->digits))
1658 dotpos = *position + i;
1660 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1665 parent_editable_iface->insert_text (editable, new_text,
1666 new_text_length, position);
1670 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1673 GtkSpinButtonPrivate *priv = spin_button->priv;
1675 gdouble new_value = 0.0;
1676 gboolean wrapped = FALSE;
1678 adj = priv->adjustment;
1680 new_value = adj->value + increment;
1686 if (fabs (adj->value - adj->upper) < EPSILON)
1688 new_value = adj->lower;
1691 else if (new_value > adj->upper)
1692 new_value = adj->upper;
1695 new_value = MIN (new_value, adj->upper);
1697 else if (increment < 0)
1701 if (fabs (adj->value - adj->lower) < EPSILON)
1703 new_value = adj->upper;
1706 else if (new_value < adj->lower)
1707 new_value = adj->lower;
1710 new_value = MAX (new_value, adj->lower);
1713 if (fabs (new_value - adj->value) > EPSILON)
1714 gtk_adjustment_set_value (adj, new_value);
1717 g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1719 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1723 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1728 *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1730 return GTK_INPUT_ERROR;
1736 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1738 GtkSpinButtonPrivate *priv = spin_button->priv;
1740 gchar *buf = g_strdup_printf ("%0.*f", priv->digits, priv->adjustment->value);
1742 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1743 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1749 /***********************************************************
1750 ***********************************************************
1751 *** Public interface ***
1752 ***********************************************************
1753 ***********************************************************/
1757 * gtk_spin_button_configure:
1758 * @spin_button: a #GtkSpinButton
1759 * @adjustment: (allow-none): a #GtkAdjustment.
1760 * @climb_rate: the new climb rate.
1761 * @digits: the number of decimal places to display in the spin button.
1763 * Changes the properties of an existing spin button. The adjustment, climb rate,
1764 * and number of decimal places are all changed accordingly, after this function call.
1767 gtk_spin_button_configure (GtkSpinButton *spin_button,
1768 GtkAdjustment *adjustment,
1772 GtkSpinButtonPrivate *priv;
1774 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1776 priv = spin_button->priv;
1779 gtk_spin_button_set_adjustment (spin_button, adjustment);
1781 adjustment = priv->adjustment;
1783 g_object_freeze_notify (G_OBJECT (spin_button));
1784 if (priv->digits != digits)
1786 priv->digits = digits;
1787 g_object_notify (G_OBJECT (spin_button), "digits");
1790 if (priv->climb_rate != climb_rate)
1792 priv->climb_rate = climb_rate;
1793 g_object_notify (G_OBJECT (spin_button), "climb-rate");
1795 g_object_thaw_notify (G_OBJECT (spin_button));
1797 gtk_adjustment_value_changed (adjustment);
1801 gtk_spin_button_new (GtkAdjustment *adjustment,
1805 GtkSpinButton *spin;
1808 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1810 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1812 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1814 return GTK_WIDGET (spin);
1818 * gtk_spin_button_new_with_range:
1819 * @min: Minimum allowable value
1820 * @max: Maximum allowable value
1821 * @step: Increment added or subtracted by spinning the widget
1823 * This is a convenience constructor that allows creation of a numeric
1824 * #GtkSpinButton without manually creating an adjustment. The value is
1825 * initially set to the minimum value and a page increment of 10 * @step
1826 * is the default. The precision of the spin button is equivalent to the
1827 * precision of @step.
1829 * Note that the way in which the precision is derived works best if @step
1830 * is a power of ten. If the resulting precision is not suitable for your
1831 * needs, use gtk_spin_button_set_digits() to correct it.
1833 * Return value: The new spin button as a #GtkWidget.
1836 gtk_spin_button_new_with_range (gdouble min,
1841 GtkSpinButton *spin;
1844 g_return_val_if_fail (min <= max, NULL);
1845 g_return_val_if_fail (step != 0.0, NULL);
1847 spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1849 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1851 if (fabs (step) >= 1.0 || step == 0.0)
1854 digits = abs ((gint) floor (log10 (fabs (step))));
1855 if (digits > MAX_DIGITS)
1856 digits = MAX_DIGITS;
1859 gtk_spin_button_configure (spin, adj, step, digits);
1861 gtk_spin_button_set_numeric (spin, TRUE);
1863 return GTK_WIDGET (spin);
1866 /* Callback used when the spin button's adjustment changes. We need to redraw
1867 * the arrows when the adjustment's range changes, and reevaluate our size request.
1870 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1872 GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1873 GtkSpinButtonPrivate *priv = spin_button->priv;
1875 priv->timer_step = priv->adjustment->step_increment;
1876 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1880 * gtk_spin_button_set_adjustment:
1881 * @spin_button: a #GtkSpinButton
1882 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1884 * Replaces the #GtkAdjustment associated with @spin_button.
1887 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1888 GtkAdjustment *adjustment)
1890 GtkSpinButtonPrivate *priv;
1892 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1894 priv = spin_button->priv;
1896 if (priv->adjustment != adjustment)
1898 if (priv->adjustment)
1900 g_signal_handlers_disconnect_by_func (priv->adjustment,
1901 gtk_spin_button_value_changed,
1903 g_signal_handlers_disconnect_by_func (priv->adjustment,
1904 adjustment_changed_cb,
1906 g_object_unref (priv->adjustment);
1908 priv->adjustment = adjustment;
1911 g_object_ref_sink (adjustment);
1912 g_signal_connect (adjustment, "value-changed",
1913 G_CALLBACK (gtk_spin_button_value_changed),
1915 g_signal_connect (adjustment, "changed",
1916 G_CALLBACK (adjustment_changed_cb),
1918 priv->timer_step = priv->adjustment->step_increment;
1921 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1924 g_object_notify (G_OBJECT (spin_button), "adjustment");
1928 * gtk_spin_button_get_adjustment:
1929 * @spin_button: a #GtkSpinButton
1931 * Get the adjustment associated with a #GtkSpinButton
1933 * Return value: (transfer none): the #GtkAdjustment of @spin_button
1936 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1938 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1940 return spin_button->priv->adjustment;
1944 * gtk_spin_button_set_digits:
1945 * @spin_button: a #GtkSpinButton
1946 * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1948 * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1952 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1955 GtkSpinButtonPrivate *priv;
1957 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1959 priv = spin_button->priv;
1961 if (priv->digits != digits)
1963 priv->digits = digits;
1964 gtk_spin_button_value_changed (priv->adjustment, spin_button);
1965 g_object_notify (G_OBJECT (spin_button), "digits");
1967 /* since lower/upper may have changed */
1968 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1973 * gtk_spin_button_get_digits:
1974 * @spin_button: a #GtkSpinButton
1976 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1978 * Returns: the current precision
1981 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1983 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1985 return spin_button->priv->digits;
1989 * gtk_spin_button_set_increments:
1990 * @spin_button: a #GtkSpinButton
1991 * @step: increment applied for a button 1 press.
1992 * @page: increment applied for a button 2 press.
1994 * Sets the step and page increments for spin_button. This affects how
1995 * quickly the value changes when the spin button's arrows are activated.
1998 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
2002 GtkSpinButtonPrivate *priv;
2004 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2006 priv = spin_button->priv;
2008 priv->adjustment->step_increment = step;
2009 priv->adjustment->page_increment = page;
2013 * gtk_spin_button_get_increments:
2014 * @spin_button: a #GtkSpinButton
2015 * @step: (allow-none): location to store step increment, or %NULL
2016 * @page: (allow-none): location to store page increment, or %NULL
2018 * Gets the current step and page the increments used by @spin_button. See
2019 * gtk_spin_button_set_increments().
2022 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
2026 GtkSpinButtonPrivate *priv;
2028 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2030 priv = spin_button->priv;
2033 *step = priv->adjustment->step_increment;
2035 *page = priv->adjustment->page_increment;
2039 * gtk_spin_button_set_range:
2040 * @spin_button: a #GtkSpinButton
2041 * @min: minimum allowable value
2042 * @max: maximum allowable value
2044 * Sets the minimum and maximum allowable values for @spin_button
2047 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2051 GtkSpinButtonPrivate *priv;
2054 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2056 priv = spin_button->priv;
2058 priv->adjustment->lower = min;
2059 priv->adjustment->upper = max;
2061 value = CLAMP (priv->adjustment->value,
2062 priv->adjustment->lower,
2063 (priv->adjustment->upper - priv->adjustment->page_size));
2065 if (value != priv->adjustment->value)
2066 gtk_spin_button_set_value (spin_button, value);
2068 gtk_adjustment_changed (priv->adjustment);
2072 * gtk_spin_button_get_range:
2073 * @spin_button: a #GtkSpinButton
2074 * @min: (allow-none): location to store minimum allowed value, or %NULL
2075 * @max: (allow-none): location to store maximum allowed value, or %NULL
2077 * Gets the range allowed for @spin_button. See
2078 * gtk_spin_button_set_range().
2081 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2085 GtkSpinButtonPrivate *priv;
2087 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2089 priv = spin_button->priv;
2092 *min = priv->adjustment->lower;
2094 *max = priv->adjustment->upper;
2098 * gtk_spin_button_get_value:
2099 * @spin_button: a #GtkSpinButton
2101 * Get the value in the @spin_button.
2103 * Return value: the value of @spin_button
2106 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2108 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2110 return spin_button->priv->adjustment->value;
2114 * gtk_spin_button_get_value_as_int:
2115 * @spin_button: a #GtkSpinButton
2117 * Get the value @spin_button represented as an integer.
2119 * Return value: the value of @spin_button
2122 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2124 GtkSpinButtonPrivate *priv;
2127 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2129 priv = spin_button->priv;
2131 val = priv->adjustment->value;
2132 if (val - floor (val) < ceil (val) - val)
2139 * gtk_spin_button_set_value:
2140 * @spin_button: a #GtkSpinButton
2141 * @value: the new value
2143 * Set the value of @spin_button.
2146 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2149 GtkSpinButtonPrivate *priv;
2151 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2153 priv = spin_button->priv;
2155 if (fabs (value - priv->adjustment->value) > EPSILON)
2156 gtk_adjustment_set_value (priv->adjustment, value);
2159 gint return_val = FALSE;
2160 g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2161 if (return_val == FALSE)
2162 gtk_spin_button_default_output (spin_button);
2167 * gtk_spin_button_set_update_policy:
2168 * @spin_button: a #GtkSpinButton
2169 * @policy: a #GtkSpinButtonUpdatePolicy value
2171 * Sets the update behavior of a spin button. This determines whether the
2172 * spin button is always updated or only when a valid value is set.
2175 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
2176 GtkSpinButtonUpdatePolicy policy)
2178 GtkSpinButtonPrivate *priv;
2180 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2182 priv = spin_button->priv;
2184 if (priv->update_policy != policy)
2186 priv->update_policy = policy;
2187 g_object_notify (G_OBJECT (spin_button), "update-policy");
2192 * gtk_spin_button_get_update_policy:
2193 * @spin_button: a #GtkSpinButton
2195 * Gets the update behavior of a spin button. See
2196 * gtk_spin_button_set_update_policy().
2198 * Return value: the current update policy
2200 GtkSpinButtonUpdatePolicy
2201 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2203 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2205 return spin_button->priv->update_policy;
2209 * gtk_spin_button_set_numeric:
2210 * @spin_button: a #GtkSpinButton
2211 * @numeric: flag indicating if only numeric entry is allowed.
2213 * Sets the flag that determines if non-numeric text can be typed into
2217 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2220 GtkSpinButtonPrivate *priv;
2222 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2224 priv = spin_button->priv;
2226 numeric = numeric != FALSE;
2228 if (priv->numeric != numeric)
2230 priv->numeric = numeric;
2231 g_object_notify (G_OBJECT (spin_button), "numeric");
2236 * gtk_spin_button_get_numeric:
2237 * @spin_button: a #GtkSpinButton
2239 * Returns whether non-numeric text can be typed into the spin button.
2240 * See gtk_spin_button_set_numeric().
2242 * Return value: %TRUE if only numeric text can be entered
2245 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2247 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2249 return spin_button->priv->numeric;
2253 * gtk_spin_button_set_wrap:
2254 * @spin_button: a #GtkSpinButton
2255 * @wrap: a flag indicating if wrapping behavior is performed.
2257 * Sets the flag that determines if a spin button value wraps around to the
2258 * opposite limit when the upper or lower limit of the range is exceeded.
2261 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
2264 GtkSpinButtonPrivate *priv;
2266 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2268 priv = spin_button->priv;
2270 wrap = wrap != FALSE;
2272 if (priv->wrap != wrap)
2274 priv->wrap = (wrap != 0);
2276 g_object_notify (G_OBJECT (spin_button), "wrap");
2281 * gtk_spin_button_get_wrap:
2282 * @spin_button: a #GtkSpinButton
2284 * Returns whether the spin button's value wraps around to the
2285 * opposite limit when the upper or lower limit of the range is
2286 * exceeded. See gtk_spin_button_set_wrap().
2288 * Return value: %TRUE if the spin button wraps around
2291 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2293 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2295 return spin_button->priv->wrap;
2299 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2301 const PangoFontDescription *font_desc;
2302 GtkStyleContext *context;
2306 /* FIXME: use getter */
2307 context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
2308 font_desc = gtk_style_context_get_font (context, 0);
2310 size = pango_font_description_get_size (font_desc);
2311 arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2313 return arrow_size - arrow_size % 2; /* force even */
2317 * gtk_spin_button_set_snap_to_ticks:
2318 * @spin_button: a #GtkSpinButton
2319 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2321 * Sets the policy as to whether values are corrected to the nearest step
2322 * increment when a spin button is activated after providing an invalid value.
2325 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2326 gboolean snap_to_ticks)
2328 GtkSpinButtonPrivate *priv;
2331 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2333 priv = spin_button->priv;
2335 new_val = (snap_to_ticks != 0);
2337 if (new_val != priv->snap_to_ticks)
2339 priv->snap_to_ticks = new_val;
2340 if (new_val && gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
2341 gtk_spin_button_update (spin_button);
2343 g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2348 * gtk_spin_button_get_snap_to_ticks:
2349 * @spin_button: a #GtkSpinButton
2351 * Returns whether the values are corrected to the nearest step. See
2352 * gtk_spin_button_set_snap_to_ticks().
2354 * Return value: %TRUE if values are snapped to the nearest step.
2357 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2359 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2361 return spin_button->priv->snap_to_ticks;
2365 * gtk_spin_button_spin:
2366 * @spin_button: a #GtkSpinButton
2367 * @direction: a #GtkSpinType indicating the direction to spin.
2368 * @increment: step increment to apply in the specified direction.
2370 * Increment or decrement a spin button's value in a specified direction
2371 * by a specified amount.
2374 gtk_spin_button_spin (GtkSpinButton *spin_button,
2375 GtkSpinType direction,
2378 GtkSpinButtonPrivate *priv;
2382 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2384 priv = spin_button->priv;
2386 adj = priv->adjustment;
2388 /* for compatibility with the 1.0.x version of this function */
2389 if (increment != 0 && increment != adj->step_increment &&
2390 (direction == GTK_SPIN_STEP_FORWARD ||
2391 direction == GTK_SPIN_STEP_BACKWARD))
2393 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2394 increment = -increment;
2395 direction = GTK_SPIN_USER_DEFINED;
2400 case GTK_SPIN_STEP_FORWARD:
2402 gtk_spin_button_real_spin (spin_button, adj->step_increment);
2405 case GTK_SPIN_STEP_BACKWARD:
2407 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2410 case GTK_SPIN_PAGE_FORWARD:
2412 gtk_spin_button_real_spin (spin_button, adj->page_increment);
2415 case GTK_SPIN_PAGE_BACKWARD:
2417 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2422 diff = adj->value - adj->lower;
2424 gtk_spin_button_real_spin (spin_button, -diff);
2429 diff = adj->upper - adj->value;
2431 gtk_spin_button_real_spin (spin_button, diff);
2434 case GTK_SPIN_USER_DEFINED:
2437 gtk_spin_button_real_spin (spin_button, increment);
2446 * gtk_spin_button_update:
2447 * @spin_button: a #GtkSpinButton
2449 * Manually force an update of the spin button.
2452 gtk_spin_button_update (GtkSpinButton *spin_button)
2454 GtkSpinButtonPrivate *priv;
2459 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2461 priv = spin_button->priv;
2464 g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2465 if (return_val == FALSE)
2467 return_val = gtk_spin_button_default_input (spin_button, &val);
2468 error = (return_val == GTK_INPUT_ERROR);
2470 else if (return_val == GTK_INPUT_ERROR)
2473 gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2475 if (priv->update_policy == GTK_UPDATE_ALWAYS)
2477 if (val < priv->adjustment->lower)
2478 val = priv->adjustment->lower;
2479 else if (val > priv->adjustment->upper)
2480 val = priv->adjustment->upper;
2482 else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2484 val < priv->adjustment->lower ||
2485 val > priv->adjustment->upper))
2487 gtk_spin_button_value_changed (priv->adjustment, spin_button);
2491 if (priv->snap_to_ticks)
2492 gtk_spin_button_snap (spin_button, val);
2494 gtk_spin_button_set_value (spin_button, val);
2498 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2500 return spin_button->priv->panel;