]> Pileus Git - ~andy/gtk/blob - gtk/gtkspinbutton.c
GtkSpinButton: Clarify clamping behavior in gtk_spin_button_set_range doc
[~andy/gtk] / gtk / gtkspinbutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * GtkSpinButton widget for GTK+
5  * Copyright (C) 1998 Lars Hamann and Stefan Jeske
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /*
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/.
28  */
29
30 #include "config.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <math.h>
35 #include <string.h>
36 #include <locale.h>
37
38 #include "gtkbindings.h"
39 #include "gtkspinbutton.h"
40 #include "gtkentryprivate.h"
41 #include "gtkmarshalers.h"
42 #include "gtksettings.h"
43 #include "gtkprivate.h"
44 #include "gtkintl.h"
45 #include "gtktypebuiltins.h"
46
47 #include "a11y/gtkspinbuttonaccessible.h"
48
49 #define MIN_SPIN_BUTTON_WIDTH 30
50 #define MAX_TIMER_CALLS       5
51 #define EPSILON               1e-10
52 #define MAX_DIGITS            20
53 #define MIN_ARROW_WIDTH       6
54
55
56 /**
57  * SECTION:gtkspinbutton
58  * @Title: GtkSpinButton
59  * @Short_description: Retrieve an integer or floating-point number from
60  *     the user
61  * @See_also: #GtkEntry
62  *
63  * A #GtkSpinButton is an ideal way to allow the user to set the value of
64  * some attribute. Rather than having to directly type a number into a
65  * #GtkEntry, GtkSpinButton allows the user to click on one of two arrows
66  * to increment or decrement the displayed value. A value can still be
67  * typed in, with the bonus that it can be checked to ensure it is in a
68  * given range.
69  *
70  * The main properties of a GtkSpinButton are through an adjustment.
71  * See the #GtkAdjustment section for more details about an adjustment's
72  * properties.
73  *
74  * <example>
75  * <title>Using a GtkSpinButton to get an integer</title>
76  * <programlisting>
77  * /&ast; Provides a function to retrieve an integer value from a
78  *  &ast; GtkSpinButton and creates a spin button to model percentage
79  *  &ast; values.
80  *  &ast;/
81  *
82  * gint
83  * grab_int_value (GtkSpinButton *button,
84  *                 gpointer       user_data)
85  * {
86  *   return gtk_spin_button_get_value_as_int (button);
87  * }
88  *
89  * void
90  * create_integer_spin_button (void)
91  * {
92  *
93  *   GtkWidget *window, *button;
94  *   GtkAdjustment *adjustment;
95  *
96  *   adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0);
97  *
98  *   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
99  *   gtk_container_set_border_width (GTK_CONTAINER (window), 5);
100  *
101  *   /&ast; creates the spinbutton, with no decimal places &ast;/
102  *   button = gtk_spin_button_new (adjustment, 1.0, 0);
103  *   gtk_container_add (GTK_CONTAINER (window), button);
104  *
105  *   gtk_widget_show_all (window);
106  * }
107  * </programlisting>
108  * </example>
109  *
110  * <example>
111  * <title>Using a GtkSpinButton to get a floating point value</title>
112  * <programlisting>
113  * /&ast; Provides a function to retrieve a floating point value from a
114  *  &ast; GtkSpinButton, and creates a high precision spin button.
115  *  &ast;/
116  *
117  * gfloat
118  * grab_float_value (GtkSpinButton *button,
119  *                   gpointer       user_data)
120  * {
121  *   return gtk_spin_button_get_value (button);
122  * }
123  *
124  * void
125  * create_floating_spin_button (void)
126  * {
127  *   GtkWidget *window, *button;
128  *   GtkAdjustment *adjustment;
129  *
130  *   adjustment = gtk_adjustment_new (2.500, 0.0, 5.0, 0.001, 0.1, 0.0);
131  *
132  *   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
133  *   gtk_container_set_border_width (GTK_CONTAINER (window), 5);
134  *
135  *   /&ast; creates the spinbutton, with three decimal places &ast;/
136  *   button = gtk_spin_button_new (adjustment, 0.001, 3);
137  *   gtk_container_add (GTK_CONTAINER (window), button);
138  *
139  *   gtk_widget_show_all (window);
140  * }
141  * </programlisting>
142  * </example>
143  */
144
145 struct _GtkSpinButtonPrivate
146 {
147   GtkAdjustment *adjustment;
148
149   GdkWindow     *panel;
150
151   guint32        timer;
152
153   GtkSpinButtonUpdatePolicy update_policy;
154
155   gdouble        climb_rate;
156   gdouble        timer_step;
157
158   guint          button        : 2;
159   guint          click_child   : 2; /* valid: GTK_ARROW_UP=0, GTK_ARROW_DOWN=1 or 2=NONE/BOTH */
160   guint          digits        : 10;
161   guint          in_child      : 2;
162   guint          need_timer    : 1;
163   guint          numeric       : 1;
164   guint          snap_to_ticks : 1;
165   guint          timer_calls   : 3;
166   guint          wrap          : 1;
167 };
168
169 enum {
170   PROP_0,
171   PROP_ADJUSTMENT,
172   PROP_CLIMB_RATE,
173   PROP_DIGITS,
174   PROP_SNAP_TO_TICKS,
175   PROP_NUMERIC,
176   PROP_WRAP,
177   PROP_UPDATE_POLICY,
178   PROP_VALUE
179 };
180
181 /* Signals */
182 enum
183 {
184   INPUT,
185   OUTPUT,
186   VALUE_CHANGED,
187   CHANGE_VALUE,
188   WRAPPED,
189   LAST_SIGNAL
190 };
191
192 static void gtk_spin_button_editable_init  (GtkEditableInterface *iface);
193 static void gtk_spin_button_finalize       (GObject            *object);
194 static void gtk_spin_button_set_property   (GObject         *object,
195                                             guint            prop_id,
196                                             const GValue    *value,
197                                             GParamSpec      *pspec);
198 static void gtk_spin_button_get_property   (GObject         *object,
199                                             guint            prop_id,
200                                             GValue          *value,
201                                             GParamSpec      *pspec);
202 static void gtk_spin_button_destroy        (GtkWidget          *widget);
203 static void gtk_spin_button_map            (GtkWidget          *widget);
204 static void gtk_spin_button_unmap          (GtkWidget          *widget);
205 static void gtk_spin_button_realize        (GtkWidget          *widget);
206 static void gtk_spin_button_unrealize      (GtkWidget          *widget);
207 static void gtk_spin_button_get_preferred_width  (GtkWidget          *widget,
208                                                   gint               *minimum,
209                                                   gint               *natural);
210
211 static void gtk_spin_button_size_allocate  (GtkWidget          *widget,
212                                             GtkAllocation      *allocation);
213 static gint gtk_spin_button_draw           (GtkWidget          *widget,
214                                             cairo_t            *cr);
215 static gint gtk_spin_button_button_press   (GtkWidget          *widget,
216                                             GdkEventButton     *event);
217 static gint gtk_spin_button_button_release (GtkWidget          *widget,
218                                             GdkEventButton     *event);
219 static gint gtk_spin_button_motion_notify  (GtkWidget          *widget,
220                                             GdkEventMotion     *event);
221 static gint gtk_spin_button_enter_notify   (GtkWidget          *widget,
222                                             GdkEventCrossing   *event);
223 static gint gtk_spin_button_leave_notify   (GtkWidget          *widget,
224                                             GdkEventCrossing   *event);
225 static gint gtk_spin_button_focus_out      (GtkWidget          *widget,
226                                             GdkEventFocus      *event);
227 static void gtk_spin_button_grab_notify    (GtkWidget          *widget,
228                                             gboolean            was_grabbed);
229 static void gtk_spin_button_state_flags_changed  (GtkWidget     *widget,
230                                                   GtkStateFlags  previous_state);
231 static void gtk_spin_button_style_updated  (GtkWidget          *widget);
232 static void gtk_spin_button_draw_arrow     (GtkSpinButton      *spin_button,
233                                             GtkStyleContext    *context,
234                                             cairo_t            *cr,
235                                             GtkArrowType        arrow_type);
236 static gboolean gtk_spin_button_timer          (GtkSpinButton      *spin_button);
237 static gboolean gtk_spin_button_stop_spinning  (GtkSpinButton      *spin);
238 static void gtk_spin_button_value_changed  (GtkAdjustment      *adjustment,
239                                             GtkSpinButton      *spin_button);
240 static gint gtk_spin_button_key_release    (GtkWidget          *widget,
241                                             GdkEventKey        *event);
242 static gint gtk_spin_button_scroll         (GtkWidget          *widget,
243                                             GdkEventScroll     *event);
244 static void gtk_spin_button_activate       (GtkEntry           *entry);
245 static void gtk_spin_button_get_text_area_size (GtkEntry *entry,
246                                                 gint     *x,
247                                                 gint     *y,
248                                                 gint     *width,
249                                                 gint     *height);
250 static void gtk_spin_button_snap           (GtkSpinButton      *spin_button,
251                                             gdouble             val);
252 static void gtk_spin_button_insert_text    (GtkEditable        *editable,
253                                             const gchar        *new_text,
254                                             gint                new_text_length,
255                                             gint               *position);
256 static void gtk_spin_button_real_spin      (GtkSpinButton      *spin_button,
257                                             gdouble             step);
258 static void gtk_spin_button_real_change_value (GtkSpinButton   *spin,
259                                                GtkScrollType    scroll);
260
261 static gint gtk_spin_button_default_input  (GtkSpinButton      *spin_button,
262                                             gdouble            *new_val);
263 static gint gtk_spin_button_default_output (GtkSpinButton      *spin_button);
264
265 static gint spin_button_get_arrow_size     (GtkSpinButton      *spin_button);
266
267 static guint spinbutton_signals[LAST_SIGNAL] = {0};
268
269 #define NO_ARROW 2
270
271 G_DEFINE_TYPE_WITH_CODE (GtkSpinButton, gtk_spin_button, GTK_TYPE_ENTRY,
272                          G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
273                                                 gtk_spin_button_editable_init))
274
275 #define add_spin_binding(binding_set, keyval, mask, scroll)            \
276   gtk_binding_entry_add_signal (binding_set, keyval, mask,             \
277                                 "change_value", 1,                     \
278                                 GTK_TYPE_SCROLL_TYPE, scroll)
279
280 static void
281 gtk_spin_button_class_init (GtkSpinButtonClass *class)
282 {
283   GObjectClass     *gobject_class = G_OBJECT_CLASS (class);
284   GtkWidgetClass   *widget_class = GTK_WIDGET_CLASS (class);
285   GtkEntryClass    *entry_class = GTK_ENTRY_CLASS (class);
286   GtkBindingSet    *binding_set;
287
288   gobject_class->finalize = gtk_spin_button_finalize;
289   gobject_class->set_property = gtk_spin_button_set_property;
290   gobject_class->get_property = gtk_spin_button_get_property;
291
292   widget_class->destroy = gtk_spin_button_destroy;
293   widget_class->map = gtk_spin_button_map;
294   widget_class->unmap = gtk_spin_button_unmap;
295   widget_class->realize = gtk_spin_button_realize;
296   widget_class->unrealize = gtk_spin_button_unrealize;
297   widget_class->get_preferred_width = gtk_spin_button_get_preferred_width;
298   widget_class->size_allocate = gtk_spin_button_size_allocate;
299   widget_class->draw = gtk_spin_button_draw;
300   widget_class->scroll_event = gtk_spin_button_scroll;
301   widget_class->button_press_event = gtk_spin_button_button_press;
302   widget_class->button_release_event = gtk_spin_button_button_release;
303   widget_class->motion_notify_event = gtk_spin_button_motion_notify;
304   widget_class->key_release_event = gtk_spin_button_key_release;
305   widget_class->enter_notify_event = gtk_spin_button_enter_notify;
306   widget_class->leave_notify_event = gtk_spin_button_leave_notify;
307   widget_class->focus_out_event = gtk_spin_button_focus_out;
308   widget_class->grab_notify = gtk_spin_button_grab_notify;
309   widget_class->state_flags_changed = gtk_spin_button_state_flags_changed;
310   widget_class->style_updated = gtk_spin_button_style_updated;
311
312   entry_class->activate = gtk_spin_button_activate;
313   entry_class->get_text_area_size = gtk_spin_button_get_text_area_size;
314
315   class->input = NULL;
316   class->output = NULL;
317   class->change_value = gtk_spin_button_real_change_value;
318
319   g_object_class_install_property (gobject_class,
320                                    PROP_ADJUSTMENT,
321                                    g_param_spec_object ("adjustment",
322                                                         P_("Adjustment"),
323                                                         P_("The adjustment that holds the value of the spin button"),
324                                                         GTK_TYPE_ADJUSTMENT,
325                                                         GTK_PARAM_READWRITE));
326
327   g_object_class_install_property (gobject_class,
328                                    PROP_CLIMB_RATE,
329                                    g_param_spec_double ("climb-rate",
330                                                         P_("Climb Rate"),
331                                                         P_("The acceleration rate when you hold down a button"),
332                                                         0.0,
333                                                         G_MAXDOUBLE,
334                                                         0.0,
335                                                         GTK_PARAM_READWRITE));
336
337   g_object_class_install_property (gobject_class,
338                                    PROP_DIGITS,
339                                    g_param_spec_uint ("digits",
340                                                       P_("Digits"),
341                                                       P_("The number of decimal places to display"),
342                                                       0,
343                                                       MAX_DIGITS,
344                                                       0,
345                                                       GTK_PARAM_READWRITE));
346
347   g_object_class_install_property (gobject_class,
348                                    PROP_SNAP_TO_TICKS,
349                                    g_param_spec_boolean ("snap-to-ticks",
350                                                          P_("Snap to Ticks"),
351                                                          P_("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
352                                                          FALSE,
353                                                          GTK_PARAM_READWRITE));
354
355   g_object_class_install_property (gobject_class,
356                                    PROP_NUMERIC,
357                                    g_param_spec_boolean ("numeric",
358                                                          P_("Numeric"),
359                                                          P_("Whether non-numeric characters should be ignored"),
360                                                          FALSE,
361                                                          GTK_PARAM_READWRITE));
362
363   g_object_class_install_property (gobject_class,
364                                    PROP_WRAP,
365                                    g_param_spec_boolean ("wrap",
366                                                          P_("Wrap"),
367                                                          P_("Whether a spin button should wrap upon reaching its limits"),
368                                                          FALSE,
369                                                          GTK_PARAM_READWRITE));
370
371   g_object_class_install_property (gobject_class,
372                                    PROP_UPDATE_POLICY,
373                                    g_param_spec_enum ("update-policy",
374                                                       P_("Update Policy"),
375                                                       P_("Whether the spin button should update always, or only when the value is legal"),
376                                                       GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
377                                                       GTK_UPDATE_ALWAYS,
378                                                       GTK_PARAM_READWRITE));
379
380   g_object_class_install_property (gobject_class,
381                                    PROP_VALUE,
382                                    g_param_spec_double ("value",
383                                                         P_("Value"),
384                                                         P_("Reads the current value, or sets a new value"),
385                                                         -G_MAXDOUBLE,
386                                                         G_MAXDOUBLE,
387                                                         0.0,
388                                                         GTK_PARAM_READWRITE));
389
390   gtk_widget_class_install_style_property_parser (widget_class,
391                                                   g_param_spec_enum ("shadow-type",
392                                                                      "Shadow Type",
393                                                                      P_("Style of bevel around the spin button"),
394                                                                      GTK_TYPE_SHADOW_TYPE,
395                                                                      GTK_SHADOW_IN,
396                                                                      GTK_PARAM_READABLE),
397                                                   gtk_rc_property_parse_enum);
398
399   /**
400    * GtkSpinButton::input:
401    * @spin_button: the object on which the signal was emitted
402    * @new_value: (out) (type double): return location for the new value
403    *
404    * The ::input signal can be used to influence the conversion of
405    * the users input into a double value. The signal handler is
406    * expected to use gtk_entry_get_text() to retrieve the text of
407    * the entry and set @new_value to the new value.
408    *
409    * The default conversion uses g_strtod().
410    *
411    * Returns: %TRUE for a successful conversion, %FALSE if the input
412    *     was not handled, and %GTK_INPUT_ERROR if the conversion failed.
413    */
414   spinbutton_signals[INPUT] =
415     g_signal_new (I_("input"),
416                   G_TYPE_FROM_CLASS (gobject_class),
417                   G_SIGNAL_RUN_LAST,
418                   G_STRUCT_OFFSET (GtkSpinButtonClass, input),
419                   NULL, NULL,
420                   _gtk_marshal_INT__POINTER,
421                   G_TYPE_INT, 1,
422                   G_TYPE_POINTER);
423
424   /**
425    * GtkSpinButton::output:
426    * @spin_button: the object which received the signal
427    *
428    * The ::output signal can be used to change to formatting
429    * of the value that is displayed in the spin buttons entry.
430    * |[
431    * /&ast; show leading zeros &ast;/
432    * static gboolean
433    * on_output (GtkSpinButton *spin,
434    *            gpointer       data)
435    * {
436    *    GtkAdjustment *adjustment;
437    *    gchar *text;
438    *    int value;
439    *
440    *    adjustment = gtk_spin_button_get_adjustment (spin);
441    *    value = (int)gtk_adjustment_get_value (adjustment);
442    *    text = g_strdup_printf ("%02d", value);
443    *    gtk_entry_set_text (GTK_ENTRY (spin), text);
444    *    g_free (text);
445    *
446    *    return TRUE;
447    * }
448    * ]|
449    *
450    * Returns: %TRUE if the value has been displayed
451    */
452   spinbutton_signals[OUTPUT] =
453     g_signal_new (I_("output"),
454                   G_TYPE_FROM_CLASS (gobject_class),
455                   G_SIGNAL_RUN_LAST,
456                   G_STRUCT_OFFSET (GtkSpinButtonClass, output),
457                   _gtk_boolean_handled_accumulator, NULL,
458                   _gtk_marshal_BOOLEAN__VOID,
459                   G_TYPE_BOOLEAN, 0);
460
461   spinbutton_signals[VALUE_CHANGED] =
462     g_signal_new (I_("value-changed"),
463                   G_TYPE_FROM_CLASS (gobject_class),
464                   G_SIGNAL_RUN_LAST,
465                   G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
466                   NULL, NULL,
467                   _gtk_marshal_VOID__VOID,
468                   G_TYPE_NONE, 0);
469
470   /**
471    * GtkSpinButton::wrapped:
472    * @spinbutton: the object which received the signal
473    *
474    * The wrapped signal is emitted right after the spinbutton wraps
475    * from its maximum to minimum value or vice-versa.
476    *
477    * Since: 2.10
478    */
479   spinbutton_signals[WRAPPED] =
480     g_signal_new (I_("wrapped"),
481                   G_TYPE_FROM_CLASS (gobject_class),
482                   G_SIGNAL_RUN_LAST,
483                   G_STRUCT_OFFSET (GtkSpinButtonClass, wrapped),
484                   NULL, NULL,
485                   _gtk_marshal_VOID__VOID,
486                   G_TYPE_NONE, 0);
487
488   /* Action signals */
489   spinbutton_signals[CHANGE_VALUE] =
490     g_signal_new (I_("change-value"),
491                   G_TYPE_FROM_CLASS (gobject_class),
492                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
493                   G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
494                   NULL, NULL,
495                   _gtk_marshal_VOID__ENUM,
496                   G_TYPE_NONE, 1,
497                   GTK_TYPE_SCROLL_TYPE);
498
499   binding_set = gtk_binding_set_by_class (class);
500
501   add_spin_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
502   add_spin_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
503   add_spin_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
504   add_spin_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
505   add_spin_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
506   add_spin_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
507   add_spin_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
508   add_spin_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
509
510   g_type_class_add_private (class, sizeof (GtkSpinButtonPrivate));
511
512   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SPIN_BUTTON_ACCESSIBLE);
513 }
514
515 static void
516 gtk_spin_button_editable_init (GtkEditableInterface *iface)
517 {
518   iface->insert_text = gtk_spin_button_insert_text;
519 }
520
521 static void
522 gtk_spin_button_set_property (GObject      *object,
523                               guint         prop_id,
524                               const GValue *value,
525                               GParamSpec   *pspec)
526 {
527   GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
528   GtkSpinButtonPrivate *priv = spin_button->priv;
529
530   switch (prop_id)
531     {
532       GtkAdjustment *adjustment;
533
534     case PROP_ADJUSTMENT:
535       adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
536       if (!adjustment)
537         adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
538       gtk_spin_button_set_adjustment (spin_button, adjustment);
539       break;
540     case PROP_CLIMB_RATE:
541       gtk_spin_button_configure (spin_button,
542                                  priv->adjustment,
543                                  g_value_get_double (value),
544                                  priv->digits);
545       break;
546     case PROP_DIGITS:
547       gtk_spin_button_configure (spin_button,
548                                  priv->adjustment,
549                                  priv->climb_rate,
550                                  g_value_get_uint (value));
551       break;
552     case PROP_SNAP_TO_TICKS:
553       gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
554       break;
555     case PROP_NUMERIC:
556       gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
557       break;
558     case PROP_WRAP:
559       gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
560       break;
561     case PROP_UPDATE_POLICY:
562       gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
563       break;
564     case PROP_VALUE:
565       gtk_spin_button_set_value (spin_button, g_value_get_double (value));
566       break;
567     default:
568       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
569       break;
570     }
571 }
572
573 static void
574 gtk_spin_button_get_property (GObject      *object,
575                               guint         prop_id,
576                               GValue       *value,
577                               GParamSpec   *pspec)
578 {
579   GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
580   GtkSpinButtonPrivate *priv = spin_button->priv;
581
582   switch (prop_id)
583     {
584     case PROP_ADJUSTMENT:
585       g_value_set_object (value, priv->adjustment);
586       break;
587     case PROP_CLIMB_RATE:
588       g_value_set_double (value, priv->climb_rate);
589       break;
590     case PROP_DIGITS:
591       g_value_set_uint (value, priv->digits);
592       break;
593     case PROP_SNAP_TO_TICKS:
594       g_value_set_boolean (value, priv->snap_to_ticks);
595       break;
596     case PROP_NUMERIC:
597       g_value_set_boolean (value, priv->numeric);
598       break;
599     case PROP_WRAP:
600       g_value_set_boolean (value, priv->wrap);
601       break;
602     case PROP_UPDATE_POLICY:
603       g_value_set_enum (value, priv->update_policy);
604       break;
605      case PROP_VALUE:
606        g_value_set_double (value, gtk_adjustment_get_value (priv->adjustment));
607       break;
608     default:
609       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
610       break;
611     }
612 }
613
614 static void
615 gtk_spin_button_init (GtkSpinButton *spin_button)
616 {
617   GtkSpinButtonPrivate *priv;
618   GtkStyleContext *context;
619
620   spin_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (spin_button,
621                                                    GTK_TYPE_SPIN_BUTTON,
622                                                    GtkSpinButtonPrivate);
623   priv = spin_button->priv;
624
625   priv->adjustment = NULL;
626   priv->panel = NULL;
627   priv->timer = 0;
628   priv->climb_rate = 0.0;
629   priv->timer_step = 0.0;
630   priv->update_policy = GTK_UPDATE_ALWAYS;
631   priv->in_child = NO_ARROW;
632   priv->click_child = NO_ARROW;
633   priv->button = 0;
634   priv->need_timer = FALSE;
635   priv->timer_calls = 0;
636   priv->digits = 0;
637   priv->numeric = FALSE;
638   priv->wrap = FALSE;
639   priv->snap_to_ticks = FALSE;
640
641   gtk_spin_button_set_adjustment (spin_button,
642                                   gtk_adjustment_new (0, 0, 0, 0, 0, 0));
643
644   context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
645   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINBUTTON);
646 }
647
648 static void
649 gtk_spin_button_finalize (GObject *object)
650 {
651   gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
652
653   G_OBJECT_CLASS (gtk_spin_button_parent_class)->finalize (object);
654 }
655
656 static void
657 gtk_spin_button_destroy (GtkWidget *widget)
658 {
659   gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
660
661   GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->destroy (widget);
662 }
663
664 static void
665 gtk_spin_button_map (GtkWidget *widget)
666 {
667   GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
668   GtkSpinButtonPrivate *priv = spin_button->priv;
669
670   if (gtk_widget_get_realized (widget) && !gtk_widget_get_mapped (widget))
671     {
672       GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->map (widget);
673       gdk_window_show (priv->panel);
674     }
675 }
676
677 static void
678 gtk_spin_button_unmap (GtkWidget *widget)
679 {
680   GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
681   GtkSpinButtonPrivate *priv = spin_button->priv;
682
683   if (gtk_widget_get_mapped (widget))
684     {
685       gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (widget));
686
687       gdk_window_hide (priv->panel);
688       GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unmap (widget);
689     }
690 }
691
692 static void
693 gtk_spin_button_realize (GtkWidget *widget)
694 {
695   GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
696   GtkSpinButtonPrivate *priv = spin_button->priv;
697   GtkStyleContext *context;
698   GtkStateFlags state;
699   GtkAllocation allocation;
700   GtkRequisition requisition;
701   GdkWindowAttr attributes;
702   gint attributes_mask;
703   gboolean return_val;
704   gint arrow_size;
705   gint req_height;
706   GtkBorder padding;
707
708   arrow_size = spin_button_get_arrow_size (spin_button);
709
710   gtk_widget_get_preferred_size (widget, &requisition, NULL);
711   req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
712   gtk_widget_get_allocation (widget, &allocation);
713
714   gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
715                          GDK_KEY_RELEASE_MASK);
716   GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
717
718   attributes.window_type = GDK_WINDOW_CHILD;
719   attributes.wclass = GDK_INPUT_ONLY;
720   attributes.visual = gtk_widget_get_visual (widget);
721   attributes.event_mask = gtk_widget_get_events (widget);
722   attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
723     | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
724     | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
725
726   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
727
728   state = gtk_widget_get_state_flags (widget);
729   context = gtk_widget_get_style_context (widget);
730   gtk_style_context_get_padding (context, state, &padding);
731
732   attributes.x = allocation.x + allocation.width - arrow_size - (padding.left + padding.right);
733   attributes.y = allocation.y + (allocation.height - req_height) / 2;
734   attributes.width = arrow_size + padding.left + padding.right;
735   attributes.height = req_height;
736
737   priv->panel = gdk_window_new (gtk_widget_get_window (widget),
738                                 &attributes, attributes_mask);
739   gdk_window_set_user_data (priv->panel, widget);
740
741   return_val = FALSE;
742   g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
743   if (return_val == FALSE)
744     gtk_spin_button_default_output (spin_button);
745
746   gtk_widget_queue_resize (GTK_WIDGET (spin_button));
747 }
748
749 static void
750 gtk_spin_button_unrealize (GtkWidget *widget)
751 {
752   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
753   GtkSpinButtonPrivate *priv = spin->priv;
754
755   GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->unrealize (widget);
756
757   if (priv->panel)
758     {
759       gdk_window_set_user_data (priv->panel, NULL);
760       gdk_window_destroy (priv->panel);
761       priv->panel = NULL;
762     }
763 }
764
765 static int
766 compute_double_length (double val, int digits)
767 {
768   int a;
769   int extra;
770
771   a = 1;
772   if (fabs (val) > 1.0)
773     a = floor (log10 (fabs (val))) + 1;
774
775   extra = 0;
776
777   /* The dot: */
778   if (digits > 0)
779     extra++;
780
781   /* The sign: */
782   if (val < 0)
783     extra++;
784
785   return a + digits + extra;
786 }
787
788 static void
789 gtk_spin_button_get_preferred_width (GtkWidget *widget,
790                                      gint      *minimum,
791                                      gint      *natural)
792 {
793   GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
794   GtkSpinButtonPrivate *priv = spin_button->priv;
795   GtkEntry *entry = GTK_ENTRY (widget);
796   GtkStyleContext *style_context;
797   GtkBorder padding;
798   gint arrow_size;
799
800   style_context = gtk_widget_get_style_context (widget);
801
802   arrow_size = spin_button_get_arrow_size (spin_button);
803
804   GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->get_preferred_width (widget, minimum, natural);
805
806   if (gtk_entry_get_width_chars (entry) < 0)
807     {
808       PangoContext *context;
809       const PangoFontDescription *font_desc;
810       PangoFontMetrics *metrics;
811       gint width;
812       gint w;
813       gint string_len;
814       gint max_string_len;
815       gint digit_width;
816       gboolean interior_focus;
817       gint focus_width;
818       gint xborder, yborder;
819       GtkBorder inner_border;
820
821       gtk_widget_style_get (widget,
822                             "interior-focus", &interior_focus,
823                             "focus-line-width", &focus_width,
824                             NULL);
825
826       font_desc = gtk_style_context_get_font (style_context, 0);
827
828       context = gtk_widget_get_pango_context (widget);
829       metrics = pango_context_get_metrics (context, font_desc,
830                                            pango_context_get_language (context));
831
832       digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
833       digit_width = PANGO_SCALE *
834         ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
835
836       pango_font_metrics_unref (metrics);
837
838       /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
839       width = MIN_SPIN_BUTTON_WIDTH;
840       max_string_len = MAX (10, compute_double_length (1e9 * gtk_adjustment_get_step_increment (priv->adjustment),
841                                                        priv->digits));
842
843       string_len = compute_double_length (gtk_adjustment_get_upper (priv->adjustment),
844                                           priv->digits);
845       w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
846       width = MAX (width, w);
847       string_len = compute_double_length (gtk_adjustment_get_lower (priv->adjustment), priv->digits);
848       w = PANGO_PIXELS (MIN (string_len, max_string_len) * digit_width);
849       width = MAX (width, w);
850
851       _gtk_entry_get_borders (entry, &xborder, &yborder);
852       _gtk_entry_effective_inner_border (entry, &inner_border);
853
854       width += xborder * 2 + inner_border.left + inner_border.right;
855
856       *minimum = width;
857       *natural = width;
858     }
859
860   gtk_style_context_get_padding (style_context,
861                                  gtk_widget_get_state_flags (widget),
862                                  &padding);
863
864   *minimum += arrow_size + padding.left + padding.right;
865   *natural += arrow_size + padding.left + padding.right;
866 }
867
868 static void
869 gtk_spin_button_size_allocate (GtkWidget     *widget,
870                                GtkAllocation *allocation)
871 {
872   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
873   GtkSpinButtonPrivate *priv = spin->priv;
874   GtkAllocation panel_allocation;
875   GtkRequisition requisition;
876   GtkStyleContext *context;
877   GtkStateFlags state;
878   GtkBorder padding;
879   gint arrow_size;
880   gint panel_width;
881   gint req_height;
882
883   arrow_size = spin_button_get_arrow_size (spin);
884   context = gtk_widget_get_style_context (widget);
885   state = gtk_widget_get_state_flags (widget);
886
887   gtk_style_context_get_padding (context, state, &padding);
888   panel_width = arrow_size + padding.left + padding.right;
889
890   gtk_widget_get_preferred_size (widget, &requisition, NULL);
891   req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
892
893   gtk_widget_set_allocation (widget, allocation);
894
895   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
896     panel_allocation.x = allocation->x;
897   else
898     panel_allocation.x = allocation->x + allocation->width - panel_width;
899
900   panel_allocation.width = panel_width;
901   panel_allocation.height = MIN (req_height, allocation->height);
902
903   panel_allocation.y = allocation->y +
904                        (allocation->height - req_height) / 2;
905
906   GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->size_allocate (widget, allocation);
907
908   if (gtk_widget_get_realized (widget))
909     {
910       gdk_window_move_resize (priv->panel,
911                               panel_allocation.x,
912                               panel_allocation.y,
913                               panel_allocation.width,
914                               panel_allocation.height);
915     }
916
917   gtk_widget_queue_draw (GTK_WIDGET (spin));
918 }
919
920 static gint
921 gtk_spin_button_draw (GtkWidget      *widget,
922                       cairo_t        *cr)
923 {
924   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
925   GtkSpinButtonPrivate *priv = spin->priv;
926   GtkStyleContext *context;
927   GtkStateFlags state = 0;
928   gboolean is_rtl;
929
930   is_rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
931   context = gtk_widget_get_style_context (widget);
932
933   cairo_save (cr);
934   GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->draw (widget, cr);
935   cairo_restore (cr);
936
937   state = gtk_widget_get_state_flags (widget);
938
939   gtk_style_context_save (context);
940   gtk_style_context_set_state (context, state);
941
942   if (is_rtl)
943     gtk_style_context_set_junction_sides (context, GTK_JUNCTION_RIGHT);
944   else
945     gtk_style_context_set_junction_sides (context, GTK_JUNCTION_LEFT);
946
947   gtk_cairo_transform_to_window (cr, widget, priv->panel);
948
949   gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_UP);
950   gtk_spin_button_draw_arrow (spin, context, cr, GTK_ARROW_DOWN);
951
952   gtk_style_context_restore (context);
953
954   return FALSE;
955 }
956
957 static gboolean
958 spin_button_at_limit (GtkSpinButton *spin_button,
959                      GtkArrowType   arrow)
960 {
961   GtkSpinButtonPrivate *priv = spin_button->priv;
962   GtkArrowType effective_arrow;
963
964   if (priv->wrap)
965     return FALSE;
966
967   if (gtk_adjustment_get_step_increment (priv->adjustment) > 0)
968     effective_arrow = arrow;
969   else
970     effective_arrow = arrow == GTK_ARROW_UP ? GTK_ARROW_DOWN : GTK_ARROW_UP;
971
972   if (effective_arrow == GTK_ARROW_UP &&
973       (gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment) <= EPSILON))
974     return TRUE;
975
976   if (effective_arrow == GTK_ARROW_DOWN &&
977       (gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment) <= EPSILON))
978     return TRUE;
979
980   return FALSE;
981 }
982
983 static void
984 gtk_spin_button_draw_arrow (GtkSpinButton   *spin_button,
985                             GtkStyleContext *context,
986                             cairo_t         *cr,
987                             GtkArrowType     arrow_type)
988 {
989   GtkSpinButtonPrivate *priv;
990   GtkJunctionSides junction;
991   GtkStateFlags state;
992   GtkWidget *widget;
993   gdouble angle;
994   gint panel_height;
995   gdouble size, width, height, x, y;
996
997   g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
998
999   gtk_style_context_save (context);
1000   gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
1001
1002   priv = spin_button->priv;
1003   widget = GTK_WIDGET (spin_button);
1004   junction = gtk_style_context_get_junction_sides (context);
1005
1006   panel_height = gdk_window_get_height (priv->panel);
1007
1008   if (spin_button_at_limit (spin_button, arrow_type))
1009     state = GTK_STATE_FLAG_INSENSITIVE;
1010   else
1011     {
1012       if (priv->click_child == arrow_type)
1013         state = GTK_STATE_ACTIVE;
1014       else
1015         {
1016           if (priv->in_child == arrow_type &&
1017               priv->click_child == NO_ARROW)
1018             state = GTK_STATE_FLAG_PRELIGHT;
1019           else
1020             state = gtk_widget_get_state_flags (widget);
1021         }
1022     }
1023
1024   /* first, draw the background and the frame */
1025   if (arrow_type == GTK_ARROW_UP)
1026     {
1027       x = 0;
1028       y = 0;
1029
1030       junction |= GTK_JUNCTION_BOTTOM;
1031     }
1032   else if (arrow_type == GTK_ARROW_DOWN)
1033     {
1034       x = 0;
1035       y = panel_height / 2.0;
1036
1037       junction |= GTK_JUNCTION_TOP;
1038     }
1039
1040   gtk_style_context_set_junction_sides (context, junction);
1041   gtk_style_context_set_state (context, state);
1042
1043   height = panel_height / 2.0;
1044   width = gdk_window_get_width (priv->panel);
1045   gtk_render_background (context, cr,
1046                          x, y, width, height);
1047   gtk_render_frame (context, cr,
1048                     x, y, width, height);
1049
1050   /* make the actual rendered arrow smaller than text size */
1051   size = spin_button_get_arrow_size (spin_button);
1052   size = MIN (size, width);
1053   size *= 0.8;
1054
1055   x = (width - size) / 2.0;
1056
1057   if (arrow_type == GTK_ARROW_UP)
1058     {
1059       y = (height - size / 2.0) / 2.0;
1060       angle = 0;
1061     }
1062   else if (arrow_type == GTK_ARROW_DOWN)
1063     {
1064       y = height + ((height - size / 2.0) / 2.0) - size / 2.0;
1065       angle = G_PI;
1066     }
1067
1068   gtk_render_arrow (context, cr,
1069                     angle, x, y, size);
1070
1071   gtk_style_context_restore (context);
1072 }
1073
1074 static gint
1075 gtk_spin_button_enter_notify (GtkWidget        *widget,
1076                               GdkEventCrossing *event)
1077 {
1078   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1079   GtkSpinButtonPrivate *priv = spin->priv;
1080   GtkRequisition requisition;
1081   gint req_height;
1082
1083   if (event->window == priv->panel)
1084     {
1085       GdkDevice *device;
1086       gint x;
1087       gint y;
1088
1089       device = gdk_event_get_device ((GdkEvent *) event);
1090       gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
1091
1092       gtk_widget_get_preferred_size (widget, &requisition, NULL);
1093       req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1094
1095       if (y <= req_height / 2)
1096         priv->in_child = GTK_ARROW_UP;
1097       else
1098         priv->in_child = GTK_ARROW_DOWN;
1099
1100       gtk_widget_queue_draw (GTK_WIDGET (spin));
1101     }
1102
1103   return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1104 }
1105
1106 static gint
1107 gtk_spin_button_leave_notify (GtkWidget        *widget,
1108                               GdkEventCrossing *event)
1109 {
1110   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1111   GtkSpinButtonPrivate *priv = spin->priv;
1112
1113   if (priv->in_child != NO_ARROW)
1114     {
1115       priv->in_child = NO_ARROW;
1116       gtk_widget_queue_draw (GTK_WIDGET (spin));
1117     }
1118
1119   return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1120 }
1121
1122 static gint
1123 gtk_spin_button_focus_out (GtkWidget     *widget,
1124                            GdkEventFocus *event)
1125 {
1126   if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1127     gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1128
1129   return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1130 }
1131
1132 static void
1133 gtk_spin_button_grab_notify (GtkWidget *widget,
1134                              gboolean   was_grabbed)
1135 {
1136   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1137
1138   if (!was_grabbed)
1139     {
1140       if (gtk_spin_button_stop_spinning (spin))
1141         gtk_widget_queue_draw (GTK_WIDGET (spin));
1142     }
1143 }
1144
1145 static void
1146 gtk_spin_button_state_flags_changed (GtkWidget     *widget,
1147                                      GtkStateFlags  previous_state)
1148 {
1149   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1150
1151   if (!gtk_widget_is_sensitive (widget))
1152     {
1153       if (gtk_spin_button_stop_spinning (spin))
1154         gtk_widget_queue_draw (GTK_WIDGET (spin));
1155     }
1156 }
1157
1158 static void
1159 gtk_spin_button_style_updated (GtkWidget *widget)
1160 {
1161   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1162   GtkSpinButtonPrivate *priv = spin->priv;
1163
1164   if (gtk_widget_get_realized (widget))
1165     {
1166       GtkStyleContext *context;
1167
1168       context = gtk_widget_get_style_context (widget);
1169       gtk_style_context_set_background (context, priv->panel);
1170     }
1171
1172   GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_updated (widget);
1173 }
1174
1175
1176 static gint
1177 gtk_spin_button_scroll (GtkWidget      *widget,
1178                         GdkEventScroll *event)
1179 {
1180   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1181   GtkSpinButtonPrivate *priv = spin->priv;
1182
1183   if (event->direction == GDK_SCROLL_UP)
1184     {
1185       if (!gtk_widget_has_focus (widget))
1186         gtk_widget_grab_focus (widget);
1187       gtk_spin_button_real_spin (spin, gtk_adjustment_get_step_increment (priv->adjustment));
1188     }
1189   else if (event->direction == GDK_SCROLL_DOWN)
1190     {
1191       if (!gtk_widget_has_focus (widget))
1192         gtk_widget_grab_focus (widget);
1193       gtk_spin_button_real_spin (spin, -gtk_adjustment_get_step_increment (priv->adjustment));
1194     }
1195   else
1196     return FALSE;
1197
1198   return TRUE;
1199 }
1200
1201 static gboolean
1202 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1203 {
1204   GtkSpinButtonPrivate *priv = spin->priv;
1205   gboolean did_spin = FALSE;
1206
1207   if (priv->timer)
1208     {
1209       g_source_remove (priv->timer);
1210       priv->timer = 0;
1211       priv->need_timer = FALSE;
1212
1213       did_spin = TRUE;
1214     }
1215
1216   priv->button = 0;
1217   priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1218   priv->timer_calls = 0;
1219
1220   priv->click_child = NO_ARROW;
1221
1222   return did_spin;
1223 }
1224
1225 static void
1226 start_spinning (GtkSpinButton *spin,
1227                 GtkArrowType   click_child,
1228                 gdouble        step)
1229 {
1230   GtkSpinButtonPrivate *priv;
1231
1232   g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1233
1234   priv = spin->priv;
1235
1236   priv->click_child = click_child;
1237
1238   if (!priv->timer)
1239     {
1240       GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1241       guint        timeout;
1242
1243       g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1244
1245       priv->timer_step = step;
1246       priv->need_timer = TRUE;
1247       priv->timer = gdk_threads_add_timeout (timeout,
1248                                    (GSourceFunc) gtk_spin_button_timer,
1249                                    (gpointer) spin);
1250     }
1251   gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1252
1253   gtk_widget_queue_draw (GTK_WIDGET (spin));
1254 }
1255
1256 static gint
1257 gtk_spin_button_button_press (GtkWidget      *widget,
1258                               GdkEventButton *event)
1259 {
1260   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1261   GtkSpinButtonPrivate *priv = spin->priv;
1262
1263   if (!priv->button)
1264     {
1265       if (event->window == priv->panel)
1266         {
1267           GtkRequisition requisition;
1268           gint req_height;
1269
1270           if (!gtk_widget_has_focus (widget))
1271             gtk_widget_grab_focus (widget);
1272           priv->button = event->button;
1273
1274           if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1275             gtk_spin_button_update (spin);
1276
1277           gtk_widget_get_preferred_size (widget, &requisition, NULL);
1278           req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1279
1280           if (event->y <= req_height / 2)
1281             {
1282               if (event->button == 1)
1283                 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_step_increment (priv->adjustment));
1284               else if (event->button == 2)
1285                 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_page_increment (priv->adjustment));
1286               else
1287                 priv->click_child = GTK_ARROW_UP;
1288             }
1289           else
1290             {
1291               if (event->button == 1)
1292                 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_step_increment (priv->adjustment));
1293               else if (event->button == 2)
1294                 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_page_increment (priv->adjustment));
1295               else
1296                 priv->click_child = GTK_ARROW_DOWN;
1297             }
1298           return TRUE;
1299         }
1300       else
1301         return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1302     }
1303   return FALSE;
1304 }
1305
1306 static gint
1307 gtk_spin_button_button_release (GtkWidget      *widget,
1308                                 GdkEventButton *event)
1309 {
1310   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1311   GtkSpinButtonPrivate *priv = spin->priv;
1312   gint arrow_size;
1313
1314   arrow_size = spin_button_get_arrow_size (spin);
1315
1316   if (event->button == priv->button)
1317     {
1318       int click_child = priv->click_child;
1319
1320       gtk_spin_button_stop_spinning (spin);
1321
1322       if (event->button == 3)
1323         {
1324           GtkRequisition requisition;
1325           gint req_height;
1326           GtkStyleContext *context;
1327           GtkStateFlags state;
1328           GtkBorder padding;
1329
1330           gtk_widget_get_preferred_size (widget, &requisition, NULL);
1331           req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1332
1333           context = gtk_widget_get_style_context (widget);
1334           state = gtk_widget_get_state_flags (widget);
1335           gtk_style_context_get_padding (context, state, &padding);
1336
1337           if (event->y >= 0 && event->x >= 0 &&
1338               event->y <= req_height &&
1339               event->x <= arrow_size + padding.left + padding.right)
1340             {
1341               if (click_child == GTK_ARROW_UP &&
1342                   event->y <= req_height / 2)
1343                 {
1344                   gdouble diff;
1345
1346                   diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1347                   if (diff > EPSILON)
1348                     gtk_spin_button_real_spin (spin, diff);
1349                 }
1350               else if (click_child == GTK_ARROW_DOWN &&
1351                        event->y > req_height / 2)
1352                 {
1353                   gdouble diff;
1354
1355                   diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1356                   if (diff > EPSILON)
1357                     gtk_spin_button_real_spin (spin, -diff);
1358                 }
1359             }
1360         }
1361       gtk_widget_queue_draw (GTK_WIDGET (spin));
1362
1363       return TRUE;
1364     }
1365   else
1366     return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1367 }
1368
1369 static gint
1370 gtk_spin_button_motion_notify (GtkWidget      *widget,
1371                                GdkEventMotion *event)
1372 {
1373   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1374   GtkSpinButtonPrivate *priv = spin->priv;
1375
1376   if (priv->button)
1377     return FALSE;
1378
1379   if (event->window == priv->panel)
1380     {
1381       GtkRequisition requisition;
1382       gint req_height;
1383       gint y = event->y;
1384
1385       gdk_event_request_motions (event);
1386
1387       gtk_widget_get_preferred_size (widget, &requisition, NULL);
1388       req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1389
1390       if (y <= req_height / 2 &&
1391           priv->in_child == GTK_ARROW_DOWN)
1392         {
1393           priv->in_child = GTK_ARROW_UP;
1394           gtk_widget_queue_draw (GTK_WIDGET (spin));
1395         }
1396       else if (y > req_height / 2 &&
1397           priv->in_child == GTK_ARROW_UP)
1398         {
1399           priv->in_child = GTK_ARROW_DOWN;
1400           gtk_widget_queue_draw (GTK_WIDGET (spin));
1401         }
1402
1403       return FALSE;
1404     }
1405
1406   return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1407 }
1408
1409 static gint
1410 gtk_spin_button_timer (GtkSpinButton *spin_button)
1411 {
1412   GtkSpinButtonPrivate *priv = spin_button->priv;
1413   gboolean retval = FALSE;
1414
1415   if (priv->timer)
1416     {
1417       if (priv->click_child == GTK_ARROW_UP)
1418         gtk_spin_button_real_spin (spin_button, priv->timer_step);
1419       else
1420         gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1421
1422       if (priv->need_timer)
1423         {
1424           GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1425           guint        timeout;
1426
1427           g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1428
1429           priv->need_timer = FALSE;
1430           priv->timer = gdk_threads_add_timeout (timeout,
1431                                               (GSourceFunc) gtk_spin_button_timer,
1432                                               (gpointer) spin_button);
1433         }
1434       else
1435         {
1436           if (priv->climb_rate > 0.0 && priv->timer_step
1437               < gtk_adjustment_get_page_increment (priv->adjustment))
1438             {
1439               if (priv->timer_calls < MAX_TIMER_CALLS)
1440                 priv->timer_calls++;
1441               else
1442                 {
1443                   priv->timer_calls = 0;
1444                   priv->timer_step += priv->climb_rate;
1445                 }
1446             }
1447           retval = TRUE;
1448         }
1449     }
1450
1451   return retval;
1452 }
1453
1454 static void
1455 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1456                                GtkSpinButton *spin_button)
1457 {
1458   gboolean return_val;
1459
1460   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1461
1462   return_val = FALSE;
1463   g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1464   if (return_val == FALSE)
1465     gtk_spin_button_default_output (spin_button);
1466
1467   g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1468
1469   gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1470
1471   g_object_notify (G_OBJECT (spin_button), "value");
1472 }
1473
1474 static void
1475 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1476                                    GtkScrollType  scroll)
1477 {
1478   GtkSpinButtonPrivate *priv = spin->priv;
1479   gdouble old_value;
1480
1481   /* When the key binding is activated, there may be an outstanding
1482    * value, so we first have to commit what is currently written in
1483    * the spin buttons text entry. See #106574
1484    */
1485   gtk_spin_button_update (spin);
1486
1487   old_value = gtk_adjustment_get_value (priv->adjustment);
1488
1489   /* We don't test whether the entry is editable, since
1490    * this key binding conceptually corresponds to changing
1491    * the value with the buttons using the mouse, which
1492    * we allow for non-editable spin buttons.
1493    */
1494   switch (scroll)
1495     {
1496     case GTK_SCROLL_STEP_BACKWARD:
1497     case GTK_SCROLL_STEP_DOWN:
1498     case GTK_SCROLL_STEP_LEFT:
1499       gtk_spin_button_real_spin (spin, -priv->timer_step);
1500
1501       if (priv->climb_rate > 0.0 && priv->timer_step
1502           < gtk_adjustment_get_page_increment (priv->adjustment))
1503         {
1504           if (priv->timer_calls < MAX_TIMER_CALLS)
1505             priv->timer_calls++;
1506           else
1507             {
1508               priv->timer_calls = 0;
1509               priv->timer_step += priv->climb_rate;
1510             }
1511         }
1512       break;
1513
1514     case GTK_SCROLL_STEP_FORWARD:
1515     case GTK_SCROLL_STEP_UP:
1516     case GTK_SCROLL_STEP_RIGHT:
1517       gtk_spin_button_real_spin (spin, priv->timer_step);
1518
1519       if (priv->climb_rate > 0.0 && priv->timer_step
1520           < gtk_adjustment_get_page_increment (priv->adjustment))
1521         {
1522           if (priv->timer_calls < MAX_TIMER_CALLS)
1523             priv->timer_calls++;
1524           else
1525             {
1526               priv->timer_calls = 0;
1527               priv->timer_step += priv->climb_rate;
1528             }
1529         }
1530       break;
1531
1532     case GTK_SCROLL_PAGE_BACKWARD:
1533     case GTK_SCROLL_PAGE_DOWN:
1534     case GTK_SCROLL_PAGE_LEFT:
1535       gtk_spin_button_real_spin (spin, -gtk_adjustment_get_page_increment (priv->adjustment));
1536       break;
1537
1538     case GTK_SCROLL_PAGE_FORWARD:
1539     case GTK_SCROLL_PAGE_UP:
1540     case GTK_SCROLL_PAGE_RIGHT:
1541       gtk_spin_button_real_spin (spin, gtk_adjustment_get_page_increment (priv->adjustment));
1542       break;
1543
1544     case GTK_SCROLL_START:
1545       {
1546         gdouble diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1547         if (diff > EPSILON)
1548           gtk_spin_button_real_spin (spin, -diff);
1549         break;
1550       }
1551
1552     case GTK_SCROLL_END:
1553       {
1554         gdouble diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1555         if (diff > EPSILON)
1556           gtk_spin_button_real_spin (spin, diff);
1557         break;
1558       }
1559
1560     default:
1561       g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1562       break;
1563     }
1564
1565   gtk_spin_button_update (spin);
1566
1567   if (gtk_adjustment_get_value (priv->adjustment) == old_value)
1568     gtk_widget_error_bell (GTK_WIDGET (spin));
1569 }
1570
1571 static gint
1572 gtk_spin_button_key_release (GtkWidget   *widget,
1573                              GdkEventKey *event)
1574 {
1575   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1576   GtkSpinButtonPrivate *priv = spin->priv;
1577
1578   /* We only get a release at the end of a key repeat run, so reset the timer_step */
1579   priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1580   priv->timer_calls = 0;
1581
1582   return TRUE;
1583 }
1584
1585 static void
1586 gtk_spin_button_snap (GtkSpinButton *spin_button,
1587                       gdouble        val)
1588 {
1589   GtkSpinButtonPrivate *priv = spin_button->priv;
1590   gdouble inc;
1591   gdouble tmp;
1592
1593   inc = gtk_adjustment_get_step_increment (priv->adjustment);
1594   if (inc == 0)
1595     return;
1596
1597   tmp = (val - gtk_adjustment_get_lower (priv->adjustment)) / inc;
1598   if (tmp - floor (tmp) < ceil (tmp) - tmp)
1599     val = gtk_adjustment_get_lower (priv->adjustment) + floor (tmp) * inc;
1600   else
1601     val = gtk_adjustment_get_lower (priv->adjustment) + ceil (tmp) * inc;
1602
1603   gtk_spin_button_set_value (spin_button, val);
1604 }
1605
1606 static void
1607 gtk_spin_button_activate (GtkEntry *entry)
1608 {
1609   if (gtk_editable_get_editable (GTK_EDITABLE (entry)))
1610     gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1611
1612   /* Chain up so that entry->activates_default is honored */
1613   GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1614 }
1615
1616 static void
1617 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1618                                     gint     *x,
1619                                     gint     *y,
1620                                     gint     *width,
1621                                     gint     *height)
1622 {
1623   GtkStyleContext *context;
1624   GtkStateFlags state;
1625   GtkWidget *widget;
1626   GtkBorder padding;
1627   gint arrow_size;
1628   gint panel_width;
1629
1630   GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1631
1632   widget = GTK_WIDGET (entry);
1633   state = gtk_widget_get_state_flags (widget);
1634   context = gtk_widget_get_style_context (widget);
1635   gtk_style_context_get_padding (context, state, &padding);
1636
1637   arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1638   panel_width = arrow_size + padding.left + padding.right;
1639
1640   if (width)
1641     *width -= panel_width;
1642
1643   if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1644     *x += panel_width;
1645 }
1646
1647 static void
1648 gtk_spin_button_insert_text (GtkEditable *editable,
1649                              const gchar *new_text,
1650                              gint         new_text_length,
1651                              gint        *position)
1652 {
1653   GtkEntry *entry = GTK_ENTRY (editable);
1654   GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1655   GtkSpinButtonPrivate *priv = spin->priv;
1656   GtkEditableInterface *parent_editable_iface;
1657
1658   parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1659                                                  GTK_TYPE_EDITABLE);
1660
1661   if (priv->numeric)
1662     {
1663       struct lconv *lc;
1664       gboolean sign;
1665       gint dotpos = -1;
1666       gint i;
1667       guint32 pos_sign;
1668       guint32 neg_sign;
1669       gint entry_length;
1670       const gchar *entry_text;
1671
1672       entry_length = gtk_entry_get_text_length (entry);
1673       entry_text = gtk_entry_get_text (entry);
1674
1675       lc = localeconv ();
1676
1677       if (*(lc->negative_sign))
1678         neg_sign = *(lc->negative_sign);
1679       else
1680         neg_sign = '-';
1681
1682       if (*(lc->positive_sign))
1683         pos_sign = *(lc->positive_sign);
1684       else
1685         pos_sign = '+';
1686
1687 #ifdef G_OS_WIN32
1688       /* Workaround for bug caused by some Windows application messing
1689        * up the positive sign of the current locale, more specifically
1690        * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1691        * See bug #330743 and for instance
1692        * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1693        *
1694        * I don't know if the positive sign always gets bogusly set to
1695        * a digit when the above Registry value is corrupted as
1696        * described. (In my test case, it got set to "8", and in the
1697        * bug report above it presumably was set ot "0".) Probably it
1698        * might get set to almost anything? So how to distinguish a
1699        * bogus value from some correct one for some locale? That is
1700        * probably hard, but at least we should filter out the
1701        * digits...
1702        */
1703       if (pos_sign >= '0' && pos_sign <= '9')
1704         pos_sign = '+';
1705 #endif
1706
1707       for (sign=0, i=0; i<entry_length; i++)
1708         if ((entry_text[i] == neg_sign) ||
1709             (entry_text[i] == pos_sign))
1710           {
1711             sign = 1;
1712             break;
1713           }
1714
1715       if (sign && !(*position))
1716         return;
1717
1718       for (dotpos=-1, i=0; i<entry_length; i++)
1719         if (entry_text[i] == *(lc->decimal_point))
1720           {
1721             dotpos = i;
1722             break;
1723           }
1724
1725       if (dotpos > -1 && *position > dotpos &&
1726           (gint)priv->digits - entry_length
1727             + dotpos - new_text_length + 1 < 0)
1728         return;
1729
1730       for (i = 0; i < new_text_length; i++)
1731         {
1732           if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1733             {
1734               if (sign || (*position) || i)
1735                 return;
1736               sign = TRUE;
1737             }
1738           else if (new_text[i] == *(lc->decimal_point))
1739             {
1740               if (!priv->digits || dotpos > -1 ||
1741                   (new_text_length - 1 - i + entry_length
1742                     - *position > (gint)priv->digits))
1743                 return;
1744               dotpos = *position + i;
1745             }
1746           else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1747             return;
1748         }
1749     }
1750
1751   parent_editable_iface->insert_text (editable, new_text,
1752                                       new_text_length, position);
1753 }
1754
1755 static void
1756 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1757                            gdouble        increment)
1758 {
1759   GtkSpinButtonPrivate *priv = spin_button->priv;
1760   GtkAdjustment *adjustment;
1761   gdouble new_value = 0.0;
1762   gboolean wrapped = FALSE;
1763
1764   adjustment = priv->adjustment;
1765
1766   new_value = gtk_adjustment_get_value (adjustment) + increment;
1767
1768   if (increment > 0)
1769     {
1770       if (priv->wrap)
1771         {
1772           if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_upper (adjustment)) < EPSILON)
1773             {
1774               new_value = gtk_adjustment_get_lower (adjustment);
1775               wrapped = TRUE;
1776             }
1777           else if (new_value > gtk_adjustment_get_upper (adjustment))
1778             new_value = gtk_adjustment_get_upper (adjustment);
1779         }
1780       else
1781         new_value = MIN (new_value, gtk_adjustment_get_upper (adjustment));
1782     }
1783   else if (increment < 0)
1784     {
1785       if (priv->wrap)
1786         {
1787           if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment)) < EPSILON)
1788             {
1789               new_value = gtk_adjustment_get_upper (adjustment);
1790               wrapped = TRUE;
1791             }
1792           else if (new_value < gtk_adjustment_get_lower (adjustment))
1793             new_value = gtk_adjustment_get_lower (adjustment);
1794         }
1795       else
1796         new_value = MAX (new_value, gtk_adjustment_get_lower (adjustment));
1797     }
1798
1799   if (fabs (new_value - gtk_adjustment_get_value (adjustment)) > EPSILON)
1800     gtk_adjustment_set_value (adjustment, new_value);
1801
1802   if (wrapped)
1803     g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1804
1805   gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1806 }
1807
1808 static gint
1809 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1810                                gdouble       *new_val)
1811 {
1812   gchar *err = NULL;
1813
1814   *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1815   if (*err)
1816     return GTK_INPUT_ERROR;
1817   else
1818     return FALSE;
1819 }
1820
1821 static gint
1822 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1823 {
1824   GtkSpinButtonPrivate *priv = spin_button->priv;
1825
1826   gchar *buf = g_strdup_printf ("%0.*f", priv->digits, gtk_adjustment_get_value (priv->adjustment));
1827
1828   if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1829     gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1830   g_free (buf);
1831   return FALSE;
1832 }
1833
1834
1835 /***********************************************************
1836  ***********************************************************
1837  ***                  Public interface                   ***
1838  ***********************************************************
1839  ***********************************************************/
1840
1841
1842 /**
1843  * gtk_spin_button_configure:
1844  * @spin_button: a #GtkSpinButton
1845  * @adjustment: (allow-none):  a #GtkAdjustment
1846  * @climb_rate: the new climb rate
1847  * @digits: the number of decimal places to display in the spin button
1848  *
1849  * Changes the properties of an existing spin button. The adjustment,
1850  * climb rate, and number of decimal places are all changed accordingly,
1851  * after this function call.
1852  */
1853 void
1854 gtk_spin_button_configure (GtkSpinButton *spin_button,
1855                            GtkAdjustment *adjustment,
1856                            gdouble        climb_rate,
1857                            guint          digits)
1858 {
1859   GtkSpinButtonPrivate *priv;
1860
1861   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1862
1863   priv = spin_button->priv;
1864
1865   if (adjustment)
1866     gtk_spin_button_set_adjustment (spin_button, adjustment);
1867   else
1868     adjustment = priv->adjustment;
1869
1870   g_object_freeze_notify (G_OBJECT (spin_button));
1871   if (priv->digits != digits)
1872     {
1873       priv->digits = digits;
1874       g_object_notify (G_OBJECT (spin_button), "digits");
1875     }
1876
1877   if (priv->climb_rate != climb_rate)
1878     {
1879       priv->climb_rate = climb_rate;
1880       g_object_notify (G_OBJECT (spin_button), "climb-rate");
1881     }
1882   g_object_thaw_notify (G_OBJECT (spin_button));
1883
1884   gtk_adjustment_value_changed (adjustment);
1885 }
1886
1887 /**
1888  * gtk_spin_button_new:
1889  * @adjustment: (allow-none): the #GtkAdjustment object that this spin
1890  *     button should use, or %NULL
1891  * @climb_rate: specifies how much the spin button changes when an arrow
1892  *     is clicked on
1893  * @digits: the number of decimal places to display
1894  *
1895  * Creates a new #GtkSpinButton.
1896  *
1897  * Returns: The new spin button as a #GtkWidget
1898  */
1899 GtkWidget *
1900 gtk_spin_button_new (GtkAdjustment *adjustment,
1901                      gdouble        climb_rate,
1902                      guint          digits)
1903 {
1904   GtkSpinButton *spin;
1905
1906   if (adjustment)
1907     g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1908
1909   spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1910
1911   gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1912
1913   return GTK_WIDGET (spin);
1914 }
1915
1916 /**
1917  * gtk_spin_button_new_with_range:
1918  * @min: Minimum allowable value
1919  * @max: Maximum allowable value
1920  * @step: Increment added or subtracted by spinning the widget
1921  *
1922  * This is a convenience constructor that allows creation of a numeric
1923  * #GtkSpinButton without manually creating an adjustment. The value is
1924  * initially set to the minimum value and a page increment of 10 * @step
1925  * is the default. The precision of the spin button is equivalent to the
1926  * precision of @step.
1927  *
1928  * Note that the way in which the precision is derived works best if @step
1929  * is a power of ten. If the resulting precision is not suitable for your
1930  * needs, use gtk_spin_button_set_digits() to correct it.
1931  *
1932  * Return value: The new spin button as a #GtkWidget
1933  */
1934 GtkWidget *
1935 gtk_spin_button_new_with_range (gdouble min,
1936                                 gdouble max,
1937                                 gdouble step)
1938 {
1939   GtkAdjustment *adjustment;
1940   GtkSpinButton *spin;
1941   gint digits;
1942
1943   g_return_val_if_fail (min <= max, NULL);
1944   g_return_val_if_fail (step != 0.0, NULL);
1945
1946   spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1947
1948   adjustment = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1949
1950   if (fabs (step) >= 1.0 || step == 0.0)
1951     digits = 0;
1952   else {
1953     digits = abs ((gint) floor (log10 (fabs (step))));
1954     if (digits > MAX_DIGITS)
1955       digits = MAX_DIGITS;
1956   }
1957
1958   gtk_spin_button_configure (spin, adjustment, step, digits);
1959
1960   gtk_spin_button_set_numeric (spin, TRUE);
1961
1962   return GTK_WIDGET (spin);
1963 }
1964
1965 /* Callback used when the spin button's adjustment changes.
1966  * We need to redraw the arrows when the adjustment's range
1967  * changes, and reevaluate our size request.
1968  */
1969 static void
1970 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1971 {
1972   GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1973   GtkSpinButtonPrivate *priv = spin_button->priv;
1974
1975   priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1976   gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1977 }
1978
1979 /**
1980  * gtk_spin_button_set_adjustment:
1981  * @spin_button: a #GtkSpinButton
1982  * @adjustment: a #GtkAdjustment to replace the existing adjustment
1983  *
1984  * Replaces the #GtkAdjustment associated with @spin_button.
1985  */
1986 void
1987 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1988                                 GtkAdjustment *adjustment)
1989 {
1990   GtkSpinButtonPrivate *priv;
1991
1992   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1993
1994   priv = spin_button->priv;
1995
1996   if (priv->adjustment != adjustment)
1997     {
1998       if (priv->adjustment)
1999         {
2000           g_signal_handlers_disconnect_by_func (priv->adjustment,
2001                                                 gtk_spin_button_value_changed,
2002                                                 spin_button);
2003           g_signal_handlers_disconnect_by_func (priv->adjustment,
2004                                                 adjustment_changed_cb,
2005                                                 spin_button);
2006           g_object_unref (priv->adjustment);
2007         }
2008       priv->adjustment = adjustment;
2009       if (adjustment)
2010         {
2011           g_object_ref_sink (adjustment);
2012           g_signal_connect (adjustment, "value-changed",
2013                             G_CALLBACK (gtk_spin_button_value_changed),
2014                             spin_button);
2015           g_signal_connect (adjustment, "changed",
2016                             G_CALLBACK (adjustment_changed_cb),
2017                             spin_button);
2018           priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
2019         }
2020
2021       gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2022     }
2023
2024   g_object_notify (G_OBJECT (spin_button), "adjustment");
2025 }
2026
2027 /**
2028  * gtk_spin_button_get_adjustment:
2029  * @spin_button: a #GtkSpinButton
2030  *
2031  * Get the adjustment associated with a #GtkSpinButton
2032  *
2033  * Return value: (transfer none): the #GtkAdjustment of @spin_button
2034  **/
2035 GtkAdjustment *
2036 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
2037 {
2038   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
2039
2040   return spin_button->priv->adjustment;
2041 }
2042
2043 /**
2044  * gtk_spin_button_set_digits:
2045  * @spin_button: a #GtkSpinButton
2046  * @digits: the number of digits after the decimal point to be displayed for the spin button's value
2047  *
2048  * Set the precision to be displayed by @spin_button. Up to 20 digit precision
2049  * is allowed.
2050  **/
2051 void
2052 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
2053                             guint          digits)
2054 {
2055   GtkSpinButtonPrivate *priv;
2056
2057   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2058
2059   priv = spin_button->priv;
2060
2061   if (priv->digits != digits)
2062     {
2063       priv->digits = digits;
2064       gtk_spin_button_value_changed (priv->adjustment, spin_button);
2065       g_object_notify (G_OBJECT (spin_button), "digits");
2066
2067       /* since lower/upper may have changed */
2068       gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2069     }
2070 }
2071
2072 /**
2073  * gtk_spin_button_get_digits:
2074  * @spin_button: a #GtkSpinButton
2075  *
2076  * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
2077  *
2078  * Returns: the current precision
2079  **/
2080 guint
2081 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
2082 {
2083   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2084
2085   return spin_button->priv->digits;
2086 }
2087
2088 /**
2089  * gtk_spin_button_set_increments:
2090  * @spin_button: a #GtkSpinButton
2091  * @step: increment applied for a button 1 press.
2092  * @page: increment applied for a button 2 press.
2093  *
2094  * Sets the step and page increments for spin_button.  This affects how
2095  * quickly the value changes when the spin button's arrows are activated.
2096  **/
2097 void
2098 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
2099                                 gdouble        step,
2100                                 gdouble        page)
2101 {
2102   GtkSpinButtonPrivate *priv;
2103
2104   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2105
2106   priv = spin_button->priv;
2107
2108   gtk_adjustment_configure (priv->adjustment,
2109                             gtk_adjustment_get_value (priv->adjustment),
2110                             gtk_adjustment_get_lower (priv->adjustment),
2111                             gtk_adjustment_get_upper (priv->adjustment),
2112                             step,
2113                             page,
2114                             gtk_adjustment_get_page_size (priv->adjustment));
2115 }
2116
2117 /**
2118  * gtk_spin_button_get_increments:
2119  * @spin_button: a #GtkSpinButton
2120  * @step: (out) (allow-none): location to store step increment, or %NULL
2121  * @page: (out) (allow-none): location to store page increment, or %NULL
2122  *
2123  * Gets the current step and page the increments used by @spin_button. See
2124  * gtk_spin_button_set_increments().
2125  **/
2126 void
2127 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
2128                                 gdouble       *step,
2129                                 gdouble       *page)
2130 {
2131   GtkSpinButtonPrivate *priv;
2132
2133   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2134
2135   priv = spin_button->priv;
2136
2137   if (step)
2138     *step = gtk_adjustment_get_step_increment (priv->adjustment);
2139   if (page)
2140     *page = gtk_adjustment_get_page_increment (priv->adjustment);
2141 }
2142
2143 /**
2144  * gtk_spin_button_set_range:
2145  * @spin_button: a #GtkSpinButton
2146  * @min: minimum allowable value
2147  * @max: maximum allowable value
2148  *
2149  * Sets the minimum and maximum allowable values for @spin_button.
2150  *
2151  * If the current value is outside this range, it will be adjusted
2152  * to fit within the range, otherwise it will remain unchanged.
2153  */
2154 void
2155 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2156                            gdouble        min,
2157                            gdouble        max)
2158 {
2159   GtkAdjustment *adjustment;
2160
2161   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2162
2163   adjustment = spin_button->priv->adjustment;
2164
2165   gtk_adjustment_configure (adjustment,
2166                             CLAMP (gtk_adjustment_get_value (adjustment), min, max),
2167                             min,
2168                             max,
2169                             gtk_adjustment_get_step_increment (adjustment),
2170                             gtk_adjustment_get_page_increment (adjustment),
2171                             gtk_adjustment_get_page_size (adjustment));
2172 }
2173
2174 /**
2175  * gtk_spin_button_get_range:
2176  * @spin_button: a #GtkSpinButton
2177  * @min: (out) (allow-none): location to store minimum allowed value, or %NULL
2178  * @max: (out) (allow-none): location to store maximum allowed value, or %NULL
2179  *
2180  * Gets the range allowed for @spin_button.
2181  * See gtk_spin_button_set_range().
2182  */
2183 void
2184 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2185                            gdouble       *min,
2186                            gdouble       *max)
2187 {
2188   GtkSpinButtonPrivate *priv;
2189
2190   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2191
2192   priv = spin_button->priv;
2193
2194   if (min)
2195     *min = gtk_adjustment_get_lower (priv->adjustment);
2196   if (max)
2197     *max = gtk_adjustment_get_upper (priv->adjustment);
2198 }
2199
2200 /**
2201  * gtk_spin_button_get_value:
2202  * @spin_button: a #GtkSpinButton
2203  *
2204  * Get the value in the @spin_button.
2205  *
2206  * Return value: the value of @spin_button
2207  */
2208 gdouble
2209 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2210 {
2211   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2212
2213   return gtk_adjustment_get_value (spin_button->priv->adjustment);
2214 }
2215
2216 /**
2217  * gtk_spin_button_get_value_as_int:
2218  * @spin_button: a #GtkSpinButton
2219  *
2220  * Get the value @spin_button represented as an integer.
2221  *
2222  * Return value: the value of @spin_button
2223  */
2224 gint
2225 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2226 {
2227   GtkSpinButtonPrivate *priv;
2228   gdouble val;
2229
2230   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2231
2232   priv = spin_button->priv;
2233
2234   val = gtk_adjustment_get_value (priv->adjustment);
2235   if (val - floor (val) < ceil (val) - val)
2236     return floor (val);
2237   else
2238     return ceil (val);
2239 }
2240
2241 /**
2242  * gtk_spin_button_set_value:
2243  * @spin_button: a #GtkSpinButton
2244  * @value: the new value
2245  *
2246  * Sets the value of @spin_button.
2247  */
2248 void
2249 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2250                            gdouble        value)
2251 {
2252   GtkSpinButtonPrivate *priv;
2253
2254   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2255
2256   priv = spin_button->priv;
2257
2258   if (fabs (value - gtk_adjustment_get_value (priv->adjustment)) > EPSILON)
2259     gtk_adjustment_set_value (priv->adjustment, value);
2260   else
2261     {
2262       gint return_val = FALSE;
2263       g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2264       if (return_val == FALSE)
2265         gtk_spin_button_default_output (spin_button);
2266     }
2267 }
2268
2269 /**
2270  * gtk_spin_button_set_update_policy:
2271  * @spin_button: a #GtkSpinButton
2272  * @policy: a #GtkSpinButtonUpdatePolicy value
2273  *
2274  * Sets the update behavior of a spin button.
2275  * This determines wether the spin button is always updated
2276  * or only when a valid value is set.
2277  */
2278 void
2279 gtk_spin_button_set_update_policy (GtkSpinButton             *spin_button,
2280                                    GtkSpinButtonUpdatePolicy  policy)
2281 {
2282   GtkSpinButtonPrivate *priv;
2283
2284   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2285
2286   priv = spin_button->priv;
2287
2288   if (priv->update_policy != policy)
2289     {
2290       priv->update_policy = policy;
2291       g_object_notify (G_OBJECT (spin_button), "update-policy");
2292     }
2293 }
2294
2295 /**
2296  * gtk_spin_button_get_update_policy:
2297  * @spin_button: a #GtkSpinButton
2298  *
2299  * Gets the update behavior of a spin button.
2300  * See gtk_spin_button_set_update_policy().
2301  *
2302  * Return value: the current update policy
2303  */
2304 GtkSpinButtonUpdatePolicy
2305 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2306 {
2307   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2308
2309   return spin_button->priv->update_policy;
2310 }
2311
2312 /**
2313  * gtk_spin_button_set_numeric:
2314  * @spin_button: a #GtkSpinButton
2315  * @numeric: flag indicating if only numeric entry is allowed
2316  *
2317  * Sets the flag that determines if non-numeric text can be typed
2318  * into the spin button.
2319  */
2320 void
2321 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2322                              gboolean       numeric)
2323 {
2324   GtkSpinButtonPrivate *priv;
2325
2326   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2327
2328   priv = spin_button->priv;
2329
2330   numeric = numeric != FALSE;
2331
2332   if (priv->numeric != numeric)
2333     {
2334        priv->numeric = numeric;
2335        g_object_notify (G_OBJECT (spin_button), "numeric");
2336     }
2337 }
2338
2339 /**
2340  * gtk_spin_button_get_numeric:
2341  * @spin_button: a #GtkSpinButton
2342  *
2343  * Returns whether non-numeric text can be typed into the spin button.
2344  * See gtk_spin_button_set_numeric().
2345  *
2346  * Return value: %TRUE if only numeric text can be entered
2347  */
2348 gboolean
2349 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2350 {
2351   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2352
2353   return spin_button->priv->numeric;
2354 }
2355
2356 /**
2357  * gtk_spin_button_set_wrap:
2358  * @spin_button: a #GtkSpinButton
2359  * @wrap: a flag indicating if wrapping behavior is performed
2360  *
2361  * Sets the flag that determines if a spin button value wraps
2362  * around to the opposite limit when the upper or lower limit
2363  * of the range is exceeded.
2364  */
2365 void
2366 gtk_spin_button_set_wrap (GtkSpinButton  *spin_button,
2367                           gboolean        wrap)
2368 {
2369   GtkSpinButtonPrivate *priv;
2370
2371   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2372
2373   priv = spin_button->priv;
2374
2375   wrap = wrap != FALSE;
2376
2377   if (priv->wrap != wrap)
2378     {
2379        priv->wrap = wrap;
2380
2381        g_object_notify (G_OBJECT (spin_button), "wrap");
2382     }
2383 }
2384
2385 /**
2386  * gtk_spin_button_get_wrap:
2387  * @spin_button: a #GtkSpinButton
2388  *
2389  * Returns whether the spin button's value wraps around to the
2390  * opposite limit when the upper or lower limit of the range is
2391  * exceeded. See gtk_spin_button_set_wrap().
2392  *
2393  * Return value: %TRUE if the spin button wraps around
2394  */
2395 gboolean
2396 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2397 {
2398   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2399
2400   return spin_button->priv->wrap;
2401 }
2402
2403 static gint
2404 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2405 {
2406   const PangoFontDescription *font_desc;
2407   GtkStyleContext *context;
2408   gint size;
2409   gint arrow_size;
2410
2411   /* FIXME: use getter */
2412   context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
2413   font_desc = gtk_style_context_get_font (context, 0);
2414
2415   size = pango_font_description_get_size (font_desc);
2416   arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2417
2418   return arrow_size - arrow_size % 2; /* force even */
2419 }
2420
2421 /**
2422  * gtk_spin_button_set_snap_to_ticks:
2423  * @spin_button: a #GtkSpinButton
2424  * @snap_to_ticks: a flag indicating if invalid values should be corrected
2425  *
2426  * Sets the policy as to whether values are corrected to the
2427  * nearest step increment when a spin button is activated after
2428  * providing an invalid value.
2429  */
2430 void
2431 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2432                                    gboolean       snap_to_ticks)
2433 {
2434   GtkSpinButtonPrivate *priv;
2435   guint new_val;
2436
2437   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2438
2439   priv = spin_button->priv;
2440
2441   new_val = (snap_to_ticks != 0);
2442
2443   if (new_val != priv->snap_to_ticks)
2444     {
2445       priv->snap_to_ticks = new_val;
2446       if (new_val && gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
2447         gtk_spin_button_update (spin_button);
2448
2449       g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2450     }
2451 }
2452
2453 /**
2454  * gtk_spin_button_get_snap_to_ticks:
2455  * @spin_button: a #GtkSpinButton
2456  *
2457  * Returns whether the values are corrected to the nearest step.
2458  * See gtk_spin_button_set_snap_to_ticks().
2459  *
2460  * Return value: %TRUE if values are snapped to the nearest step
2461  */
2462 gboolean
2463 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2464 {
2465   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2466
2467   return spin_button->priv->snap_to_ticks;
2468 }
2469
2470 /**
2471  * gtk_spin_button_spin:
2472  * @spin_button: a #GtkSpinButton
2473  * @direction: a #GtkSpinType indicating the direction to spin
2474  * @increment: step increment to apply in the specified direction
2475  *
2476  * Increment or decrement a spin button's value in a specified
2477  * direction by a specified amount.
2478  */
2479 void
2480 gtk_spin_button_spin (GtkSpinButton *spin_button,
2481                       GtkSpinType    direction,
2482                       gdouble        increment)
2483 {
2484   GtkSpinButtonPrivate *priv;
2485   GtkAdjustment *adjustment;
2486   gdouble diff;
2487
2488   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2489
2490   priv = spin_button->priv;
2491
2492   adjustment = priv->adjustment;
2493
2494   /* for compatibility with the 1.0.x version of this function */
2495   if (increment != 0 && increment != gtk_adjustment_get_step_increment (adjustment) &&
2496       (direction == GTK_SPIN_STEP_FORWARD ||
2497        direction == GTK_SPIN_STEP_BACKWARD))
2498     {
2499       if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2500         increment = -increment;
2501       direction = GTK_SPIN_USER_DEFINED;
2502     }
2503
2504   switch (direction)
2505     {
2506     case GTK_SPIN_STEP_FORWARD:
2507
2508       gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_step_increment (adjustment));
2509       break;
2510
2511     case GTK_SPIN_STEP_BACKWARD:
2512
2513       gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_step_increment (adjustment));
2514       break;
2515
2516     case GTK_SPIN_PAGE_FORWARD:
2517
2518       gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_page_increment (adjustment));
2519       break;
2520
2521     case GTK_SPIN_PAGE_BACKWARD:
2522
2523       gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_page_increment (adjustment));
2524       break;
2525
2526     case GTK_SPIN_HOME:
2527
2528       diff = gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment);
2529       if (diff > EPSILON)
2530         gtk_spin_button_real_spin (spin_button, -diff);
2531       break;
2532
2533     case GTK_SPIN_END:
2534
2535       diff = gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_value (adjustment);
2536       if (diff > EPSILON)
2537         gtk_spin_button_real_spin (spin_button, diff);
2538       break;
2539
2540     case GTK_SPIN_USER_DEFINED:
2541
2542       if (increment != 0)
2543         gtk_spin_button_real_spin (spin_button, increment);
2544       break;
2545
2546     default:
2547       break;
2548     }
2549 }
2550
2551 /**
2552  * gtk_spin_button_update:
2553  * @spin_button: a #GtkSpinButton
2554  *
2555  * Manually force an update of the spin button.
2556  */
2557 void
2558 gtk_spin_button_update (GtkSpinButton *spin_button)
2559 {
2560   GtkSpinButtonPrivate *priv;
2561   gdouble val;
2562   gint error = 0;
2563   gint return_val;
2564
2565   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2566
2567   priv = spin_button->priv;
2568
2569   return_val = FALSE;
2570   g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2571   if (return_val == FALSE)
2572     {
2573       return_val = gtk_spin_button_default_input (spin_button, &val);
2574       error = (return_val == GTK_INPUT_ERROR);
2575     }
2576   else if (return_val == GTK_INPUT_ERROR)
2577     error = 1;
2578
2579   gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2580
2581   if (priv->update_policy == GTK_UPDATE_ALWAYS)
2582     {
2583       if (val < gtk_adjustment_get_lower (priv->adjustment))
2584         val = gtk_adjustment_get_lower (priv->adjustment);
2585       else if (val > gtk_adjustment_get_upper (priv->adjustment))
2586         val = gtk_adjustment_get_upper (priv->adjustment);
2587     }
2588   else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2589            (error ||
2590             val < gtk_adjustment_get_lower (priv->adjustment) ||
2591             val > gtk_adjustment_get_upper (priv->adjustment)))
2592     {
2593       gtk_spin_button_value_changed (priv->adjustment, spin_button);
2594       return;
2595     }
2596
2597   if (priv->snap_to_ticks)
2598     gtk_spin_button_snap (spin_button, val);
2599   else
2600     gtk_spin_button_set_value (spin_button, val);
2601 }
2602
2603 GdkWindow *
2604 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2605 {
2606   return spin_button->priv->panel;
2607 }