]> Pileus Git - ~andy/gtk/blob - gtk/gtkspinbutton.c
Merge branch 'win32-theme2'
[~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   if (arrow_type == GTK_ARROW_UP)
1002     gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOP);
1003   else
1004     gtk_style_context_add_class (context, GTK_STYLE_CLASS_BOTTOM);
1005
1006   priv = spin_button->priv;
1007   widget = GTK_WIDGET (spin_button);
1008   junction = gtk_style_context_get_junction_sides (context);
1009
1010   panel_height = gdk_window_get_height (priv->panel);
1011
1012   if (spin_button_at_limit (spin_button, arrow_type))
1013     state = GTK_STATE_FLAG_INSENSITIVE;
1014   else
1015     {
1016       if (priv->click_child == arrow_type)
1017         state = GTK_STATE_ACTIVE;
1018       else
1019         {
1020           if (priv->in_child == arrow_type &&
1021               priv->click_child == NO_ARROW)
1022             state = GTK_STATE_FLAG_PRELIGHT;
1023           else
1024             state = gtk_widget_get_state_flags (widget);
1025         }
1026     }
1027
1028   /* first, draw the background and the frame */
1029   if (arrow_type == GTK_ARROW_UP)
1030     {
1031       x = 0;
1032       y = 0;
1033
1034       junction |= GTK_JUNCTION_BOTTOM;
1035     }
1036   else if (arrow_type == GTK_ARROW_DOWN)
1037     {
1038       x = 0;
1039       y = panel_height / 2.0;
1040
1041       junction |= GTK_JUNCTION_TOP;
1042     }
1043
1044   gtk_style_context_set_junction_sides (context, junction);
1045   gtk_style_context_set_state (context, state);
1046
1047   height = panel_height / 2.0;
1048   width = gdk_window_get_width (priv->panel);
1049   gtk_render_background (context, cr,
1050                          x, y, width, height);
1051   gtk_render_frame (context, cr,
1052                     x, y, width, height);
1053
1054   /* make the actual rendered arrow smaller than text size */
1055   size = spin_button_get_arrow_size (spin_button);
1056   size = MIN (size, width);
1057   size *= 0.8;
1058
1059   x = (width - size) / 2.0;
1060
1061   if (arrow_type == GTK_ARROW_UP)
1062     {
1063       y = (height - size / 2.0) / 2.0;
1064       angle = 0;
1065     }
1066   else if (arrow_type == GTK_ARROW_DOWN)
1067     {
1068       y = height + ((height - size / 2.0) / 2.0) - size / 2.0;
1069       angle = G_PI;
1070     }
1071
1072   gtk_render_arrow (context, cr,
1073                     angle, x, y, size);
1074
1075   gtk_style_context_restore (context);
1076 }
1077
1078 static gint
1079 gtk_spin_button_enter_notify (GtkWidget        *widget,
1080                               GdkEventCrossing *event)
1081 {
1082   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1083   GtkSpinButtonPrivate *priv = spin->priv;
1084   GtkRequisition requisition;
1085   gint req_height;
1086
1087   if (event->window == priv->panel)
1088     {
1089       GdkDevice *device;
1090       gint x;
1091       gint y;
1092
1093       device = gdk_event_get_device ((GdkEvent *) event);
1094       gdk_window_get_device_position (priv->panel, device, &x, &y, NULL);
1095
1096       gtk_widget_get_preferred_size (widget, &requisition, NULL);
1097       req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1098
1099       if (y <= req_height / 2)
1100         priv->in_child = GTK_ARROW_UP;
1101       else
1102         priv->in_child = GTK_ARROW_DOWN;
1103
1104       gtk_widget_queue_draw (GTK_WIDGET (spin));
1105     }
1106
1107   return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->enter_notify_event (widget, event);
1108 }
1109
1110 static gint
1111 gtk_spin_button_leave_notify (GtkWidget        *widget,
1112                               GdkEventCrossing *event)
1113 {
1114   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1115   GtkSpinButtonPrivate *priv = spin->priv;
1116
1117   if (priv->in_child != NO_ARROW)
1118     {
1119       priv->in_child = NO_ARROW;
1120       gtk_widget_queue_draw (GTK_WIDGET (spin));
1121     }
1122
1123   return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->leave_notify_event (widget, event);
1124 }
1125
1126 static gint
1127 gtk_spin_button_focus_out (GtkWidget     *widget,
1128                            GdkEventFocus *event)
1129 {
1130   if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1131     gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
1132
1133   return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->focus_out_event (widget, event);
1134 }
1135
1136 static void
1137 gtk_spin_button_grab_notify (GtkWidget *widget,
1138                              gboolean   was_grabbed)
1139 {
1140   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1141
1142   if (!was_grabbed)
1143     {
1144       if (gtk_spin_button_stop_spinning (spin))
1145         gtk_widget_queue_draw (GTK_WIDGET (spin));
1146     }
1147 }
1148
1149 static void
1150 gtk_spin_button_state_flags_changed (GtkWidget     *widget,
1151                                      GtkStateFlags  previous_state)
1152 {
1153   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1154
1155   if (!gtk_widget_is_sensitive (widget))
1156     {
1157       if (gtk_spin_button_stop_spinning (spin))
1158         gtk_widget_queue_draw (GTK_WIDGET (spin));
1159     }
1160 }
1161
1162 static void
1163 gtk_spin_button_style_updated (GtkWidget *widget)
1164 {
1165   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1166   GtkSpinButtonPrivate *priv = spin->priv;
1167
1168   if (gtk_widget_get_realized (widget))
1169     {
1170       GtkStyleContext *context;
1171
1172       context = gtk_widget_get_style_context (widget);
1173       gtk_style_context_set_background (context, priv->panel);
1174     }
1175
1176   GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->style_updated (widget);
1177 }
1178
1179
1180 static gint
1181 gtk_spin_button_scroll (GtkWidget      *widget,
1182                         GdkEventScroll *event)
1183 {
1184   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1185   GtkSpinButtonPrivate *priv = spin->priv;
1186
1187   if (event->direction == GDK_SCROLL_UP)
1188     {
1189       if (!gtk_widget_has_focus (widget))
1190         gtk_widget_grab_focus (widget);
1191       gtk_spin_button_real_spin (spin, gtk_adjustment_get_step_increment (priv->adjustment));
1192     }
1193   else if (event->direction == GDK_SCROLL_DOWN)
1194     {
1195       if (!gtk_widget_has_focus (widget))
1196         gtk_widget_grab_focus (widget);
1197       gtk_spin_button_real_spin (spin, -gtk_adjustment_get_step_increment (priv->adjustment));
1198     }
1199   else
1200     return FALSE;
1201
1202   return TRUE;
1203 }
1204
1205 static gboolean
1206 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1207 {
1208   GtkSpinButtonPrivate *priv = spin->priv;
1209   gboolean did_spin = FALSE;
1210
1211   if (priv->timer)
1212     {
1213       g_source_remove (priv->timer);
1214       priv->timer = 0;
1215       priv->need_timer = FALSE;
1216
1217       did_spin = TRUE;
1218     }
1219
1220   priv->button = 0;
1221   priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1222   priv->timer_calls = 0;
1223
1224   priv->click_child = NO_ARROW;
1225
1226   return did_spin;
1227 }
1228
1229 static void
1230 start_spinning (GtkSpinButton *spin,
1231                 GtkArrowType   click_child,
1232                 gdouble        step)
1233 {
1234   GtkSpinButtonPrivate *priv;
1235
1236   g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1237
1238   priv = spin->priv;
1239
1240   priv->click_child = click_child;
1241
1242   if (!priv->timer)
1243     {
1244       GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin));
1245       guint        timeout;
1246
1247       g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1248
1249       priv->timer_step = step;
1250       priv->need_timer = TRUE;
1251       priv->timer = gdk_threads_add_timeout (timeout,
1252                                    (GSourceFunc) gtk_spin_button_timer,
1253                                    (gpointer) spin);
1254     }
1255   gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1256
1257   gtk_widget_queue_draw (GTK_WIDGET (spin));
1258 }
1259
1260 static gint
1261 gtk_spin_button_button_press (GtkWidget      *widget,
1262                               GdkEventButton *event)
1263 {
1264   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1265   GtkSpinButtonPrivate *priv = spin->priv;
1266
1267   if (!priv->button)
1268     {
1269       if (event->window == priv->panel)
1270         {
1271           GtkRequisition requisition;
1272           gint req_height;
1273
1274           if (!gtk_widget_has_focus (widget))
1275             gtk_widget_grab_focus (widget);
1276           priv->button = event->button;
1277
1278           if (gtk_editable_get_editable (GTK_EDITABLE (widget)))
1279             gtk_spin_button_update (spin);
1280
1281           gtk_widget_get_preferred_size (widget, &requisition, NULL);
1282           req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1283
1284           if (event->y <= req_height / 2)
1285             {
1286               if (event->button == 1)
1287                 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_step_increment (priv->adjustment));
1288               else if (event->button == 2)
1289                 start_spinning (spin, GTK_ARROW_UP, gtk_adjustment_get_page_increment (priv->adjustment));
1290               else
1291                 priv->click_child = GTK_ARROW_UP;
1292             }
1293           else
1294             {
1295               if (event->button == 1)
1296                 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_step_increment (priv->adjustment));
1297               else if (event->button == 2)
1298                 start_spinning (spin, GTK_ARROW_DOWN, gtk_adjustment_get_page_increment (priv->adjustment));
1299               else
1300                 priv->click_child = GTK_ARROW_DOWN;
1301             }
1302           return TRUE;
1303         }
1304       else
1305         return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_press_event (widget, event);
1306     }
1307   return FALSE;
1308 }
1309
1310 static gint
1311 gtk_spin_button_button_release (GtkWidget      *widget,
1312                                 GdkEventButton *event)
1313 {
1314   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1315   GtkSpinButtonPrivate *priv = spin->priv;
1316   gint arrow_size;
1317
1318   arrow_size = spin_button_get_arrow_size (spin);
1319
1320   if (event->button == priv->button)
1321     {
1322       int click_child = priv->click_child;
1323
1324       gtk_spin_button_stop_spinning (spin);
1325
1326       if (event->button == 3)
1327         {
1328           GtkRequisition requisition;
1329           gint req_height;
1330           GtkStyleContext *context;
1331           GtkStateFlags state;
1332           GtkBorder padding;
1333
1334           gtk_widget_get_preferred_size (widget, &requisition, NULL);
1335           req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1336
1337           context = gtk_widget_get_style_context (widget);
1338           state = gtk_widget_get_state_flags (widget);
1339           gtk_style_context_get_padding (context, state, &padding);
1340
1341           if (event->y >= 0 && event->x >= 0 &&
1342               event->y <= req_height &&
1343               event->x <= arrow_size + padding.left + padding.right)
1344             {
1345               if (click_child == GTK_ARROW_UP &&
1346                   event->y <= req_height / 2)
1347                 {
1348                   gdouble diff;
1349
1350                   diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1351                   if (diff > EPSILON)
1352                     gtk_spin_button_real_spin (spin, diff);
1353                 }
1354               else if (click_child == GTK_ARROW_DOWN &&
1355                        event->y > req_height / 2)
1356                 {
1357                   gdouble diff;
1358
1359                   diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1360                   if (diff > EPSILON)
1361                     gtk_spin_button_real_spin (spin, -diff);
1362                 }
1363             }
1364         }
1365       gtk_widget_queue_draw (GTK_WIDGET (spin));
1366
1367       return TRUE;
1368     }
1369   else
1370     return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->button_release_event (widget, event);
1371 }
1372
1373 static gint
1374 gtk_spin_button_motion_notify (GtkWidget      *widget,
1375                                GdkEventMotion *event)
1376 {
1377   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1378   GtkSpinButtonPrivate *priv = spin->priv;
1379
1380   if (priv->button)
1381     return FALSE;
1382
1383   if (event->window == priv->panel)
1384     {
1385       GtkRequisition requisition;
1386       gint req_height;
1387       gint y = event->y;
1388
1389       gdk_event_request_motions (event);
1390
1391       gtk_widget_get_preferred_size (widget, &requisition, NULL);
1392       req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
1393
1394       if (y <= req_height / 2 &&
1395           priv->in_child == GTK_ARROW_DOWN)
1396         {
1397           priv->in_child = GTK_ARROW_UP;
1398           gtk_widget_queue_draw (GTK_WIDGET (spin));
1399         }
1400       else if (y > req_height / 2 &&
1401           priv->in_child == GTK_ARROW_UP)
1402         {
1403           priv->in_child = GTK_ARROW_DOWN;
1404           gtk_widget_queue_draw (GTK_WIDGET (spin));
1405         }
1406
1407       return FALSE;
1408     }
1409
1410   return GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->motion_notify_event (widget, event);
1411 }
1412
1413 static gint
1414 gtk_spin_button_timer (GtkSpinButton *spin_button)
1415 {
1416   GtkSpinButtonPrivate *priv = spin_button->priv;
1417   gboolean retval = FALSE;
1418
1419   if (priv->timer)
1420     {
1421       if (priv->click_child == GTK_ARROW_UP)
1422         gtk_spin_button_real_spin (spin_button, priv->timer_step);
1423       else
1424         gtk_spin_button_real_spin (spin_button, -priv->timer_step);
1425
1426       if (priv->need_timer)
1427         {
1428           GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (spin_button));
1429           guint        timeout;
1430
1431           g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1432
1433           priv->need_timer = FALSE;
1434           priv->timer = gdk_threads_add_timeout (timeout,
1435                                               (GSourceFunc) gtk_spin_button_timer,
1436                                               (gpointer) spin_button);
1437         }
1438       else
1439         {
1440           if (priv->climb_rate > 0.0 && priv->timer_step
1441               < gtk_adjustment_get_page_increment (priv->adjustment))
1442             {
1443               if (priv->timer_calls < MAX_TIMER_CALLS)
1444                 priv->timer_calls++;
1445               else
1446                 {
1447                   priv->timer_calls = 0;
1448                   priv->timer_step += priv->climb_rate;
1449                 }
1450             }
1451           retval = TRUE;
1452         }
1453     }
1454
1455   return retval;
1456 }
1457
1458 static void
1459 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1460                                GtkSpinButton *spin_button)
1461 {
1462   gboolean return_val;
1463
1464   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1465
1466   return_val = FALSE;
1467   g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1468   if (return_val == FALSE)
1469     gtk_spin_button_default_output (spin_button);
1470
1471   g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1472
1473   gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1474
1475   g_object_notify (G_OBJECT (spin_button), "value");
1476 }
1477
1478 static void
1479 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1480                                    GtkScrollType  scroll)
1481 {
1482   GtkSpinButtonPrivate *priv = spin->priv;
1483   gdouble old_value;
1484
1485   /* When the key binding is activated, there may be an outstanding
1486    * value, so we first have to commit what is currently written in
1487    * the spin buttons text entry. See #106574
1488    */
1489   gtk_spin_button_update (spin);
1490
1491   old_value = gtk_adjustment_get_value (priv->adjustment);
1492
1493   /* We don't test whether the entry is editable, since
1494    * this key binding conceptually corresponds to changing
1495    * the value with the buttons using the mouse, which
1496    * we allow for non-editable spin buttons.
1497    */
1498   switch (scroll)
1499     {
1500     case GTK_SCROLL_STEP_BACKWARD:
1501     case GTK_SCROLL_STEP_DOWN:
1502     case GTK_SCROLL_STEP_LEFT:
1503       gtk_spin_button_real_spin (spin, -priv->timer_step);
1504
1505       if (priv->climb_rate > 0.0 && priv->timer_step
1506           < gtk_adjustment_get_page_increment (priv->adjustment))
1507         {
1508           if (priv->timer_calls < MAX_TIMER_CALLS)
1509             priv->timer_calls++;
1510           else
1511             {
1512               priv->timer_calls = 0;
1513               priv->timer_step += priv->climb_rate;
1514             }
1515         }
1516       break;
1517
1518     case GTK_SCROLL_STEP_FORWARD:
1519     case GTK_SCROLL_STEP_UP:
1520     case GTK_SCROLL_STEP_RIGHT:
1521       gtk_spin_button_real_spin (spin, priv->timer_step);
1522
1523       if (priv->climb_rate > 0.0 && priv->timer_step
1524           < gtk_adjustment_get_page_increment (priv->adjustment))
1525         {
1526           if (priv->timer_calls < MAX_TIMER_CALLS)
1527             priv->timer_calls++;
1528           else
1529             {
1530               priv->timer_calls = 0;
1531               priv->timer_step += priv->climb_rate;
1532             }
1533         }
1534       break;
1535
1536     case GTK_SCROLL_PAGE_BACKWARD:
1537     case GTK_SCROLL_PAGE_DOWN:
1538     case GTK_SCROLL_PAGE_LEFT:
1539       gtk_spin_button_real_spin (spin, -gtk_adjustment_get_page_increment (priv->adjustment));
1540       break;
1541
1542     case GTK_SCROLL_PAGE_FORWARD:
1543     case GTK_SCROLL_PAGE_UP:
1544     case GTK_SCROLL_PAGE_RIGHT:
1545       gtk_spin_button_real_spin (spin, gtk_adjustment_get_page_increment (priv->adjustment));
1546       break;
1547
1548     case GTK_SCROLL_START:
1549       {
1550         gdouble diff = gtk_adjustment_get_value (priv->adjustment) - gtk_adjustment_get_lower (priv->adjustment);
1551         if (diff > EPSILON)
1552           gtk_spin_button_real_spin (spin, -diff);
1553         break;
1554       }
1555
1556     case GTK_SCROLL_END:
1557       {
1558         gdouble diff = gtk_adjustment_get_upper (priv->adjustment) - gtk_adjustment_get_value (priv->adjustment);
1559         if (diff > EPSILON)
1560           gtk_spin_button_real_spin (spin, diff);
1561         break;
1562       }
1563
1564     default:
1565       g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1566       break;
1567     }
1568
1569   gtk_spin_button_update (spin);
1570
1571   if (gtk_adjustment_get_value (priv->adjustment) == old_value)
1572     gtk_widget_error_bell (GTK_WIDGET (spin));
1573 }
1574
1575 static gint
1576 gtk_spin_button_key_release (GtkWidget   *widget,
1577                              GdkEventKey *event)
1578 {
1579   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1580   GtkSpinButtonPrivate *priv = spin->priv;
1581
1582   /* We only get a release at the end of a key repeat run, so reset the timer_step */
1583   priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1584   priv->timer_calls = 0;
1585
1586   return TRUE;
1587 }
1588
1589 static void
1590 gtk_spin_button_snap (GtkSpinButton *spin_button,
1591                       gdouble        val)
1592 {
1593   GtkSpinButtonPrivate *priv = spin_button->priv;
1594   gdouble inc;
1595   gdouble tmp;
1596
1597   inc = gtk_adjustment_get_step_increment (priv->adjustment);
1598   if (inc == 0)
1599     return;
1600
1601   tmp = (val - gtk_adjustment_get_lower (priv->adjustment)) / inc;
1602   if (tmp - floor (tmp) < ceil (tmp) - tmp)
1603     val = gtk_adjustment_get_lower (priv->adjustment) + floor (tmp) * inc;
1604   else
1605     val = gtk_adjustment_get_lower (priv->adjustment) + ceil (tmp) * inc;
1606
1607   gtk_spin_button_set_value (spin_button, val);
1608 }
1609
1610 static void
1611 gtk_spin_button_activate (GtkEntry *entry)
1612 {
1613   if (gtk_editable_get_editable (GTK_EDITABLE (entry)))
1614     gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1615
1616   /* Chain up so that entry->activates_default is honored */
1617   GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->activate (entry);
1618 }
1619
1620 static void
1621 gtk_spin_button_get_text_area_size (GtkEntry *entry,
1622                                     gint     *x,
1623                                     gint     *y,
1624                                     gint     *width,
1625                                     gint     *height)
1626 {
1627   GtkStyleContext *context;
1628   GtkStateFlags state;
1629   GtkWidget *widget;
1630   GtkBorder padding;
1631   gint arrow_size;
1632   gint panel_width;
1633
1634   GTK_ENTRY_CLASS (gtk_spin_button_parent_class)->get_text_area_size (entry, x, y, width, height);
1635
1636   widget = GTK_WIDGET (entry);
1637   state = gtk_widget_get_state_flags (widget);
1638   context = gtk_widget_get_style_context (widget);
1639   gtk_style_context_get_padding (context, state, &padding);
1640
1641   arrow_size = spin_button_get_arrow_size (GTK_SPIN_BUTTON (entry));
1642   panel_width = arrow_size + padding.left + padding.right;
1643
1644   if (width)
1645     *width -= panel_width;
1646
1647   if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL && x)
1648     *x += panel_width;
1649 }
1650
1651 static void
1652 gtk_spin_button_insert_text (GtkEditable *editable,
1653                              const gchar *new_text,
1654                              gint         new_text_length,
1655                              gint        *position)
1656 {
1657   GtkEntry *entry = GTK_ENTRY (editable);
1658   GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1659   GtkSpinButtonPrivate *priv = spin->priv;
1660   GtkEditableInterface *parent_editable_iface;
1661
1662   parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
1663                                                  GTK_TYPE_EDITABLE);
1664
1665   if (priv->numeric)
1666     {
1667       struct lconv *lc;
1668       gboolean sign;
1669       gint dotpos = -1;
1670       gint i;
1671       guint32 pos_sign;
1672       guint32 neg_sign;
1673       gint entry_length;
1674       const gchar *entry_text;
1675
1676       entry_length = gtk_entry_get_text_length (entry);
1677       entry_text = gtk_entry_get_text (entry);
1678
1679       lc = localeconv ();
1680
1681       if (*(lc->negative_sign))
1682         neg_sign = *(lc->negative_sign);
1683       else
1684         neg_sign = '-';
1685
1686       if (*(lc->positive_sign))
1687         pos_sign = *(lc->positive_sign);
1688       else
1689         pos_sign = '+';
1690
1691 #ifdef G_OS_WIN32
1692       /* Workaround for bug caused by some Windows application messing
1693        * up the positive sign of the current locale, more specifically
1694        * HKEY_CURRENT_USER\Control Panel\International\sPositiveSign.
1695        * See bug #330743 and for instance
1696        * http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic36024.aspx
1697        *
1698        * I don't know if the positive sign always gets bogusly set to
1699        * a digit when the above Registry value is corrupted as
1700        * described. (In my test case, it got set to "8", and in the
1701        * bug report above it presumably was set ot "0".) Probably it
1702        * might get set to almost anything? So how to distinguish a
1703        * bogus value from some correct one for some locale? That is
1704        * probably hard, but at least we should filter out the
1705        * digits...
1706        */
1707       if (pos_sign >= '0' && pos_sign <= '9')
1708         pos_sign = '+';
1709 #endif
1710
1711       for (sign=0, i=0; i<entry_length; i++)
1712         if ((entry_text[i] == neg_sign) ||
1713             (entry_text[i] == pos_sign))
1714           {
1715             sign = 1;
1716             break;
1717           }
1718
1719       if (sign && !(*position))
1720         return;
1721
1722       for (dotpos=-1, i=0; i<entry_length; i++)
1723         if (entry_text[i] == *(lc->decimal_point))
1724           {
1725             dotpos = i;
1726             break;
1727           }
1728
1729       if (dotpos > -1 && *position > dotpos &&
1730           (gint)priv->digits - entry_length
1731             + dotpos - new_text_length + 1 < 0)
1732         return;
1733
1734       for (i = 0; i < new_text_length; i++)
1735         {
1736           if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1737             {
1738               if (sign || (*position) || i)
1739                 return;
1740               sign = TRUE;
1741             }
1742           else if (new_text[i] == *(lc->decimal_point))
1743             {
1744               if (!priv->digits || dotpos > -1 ||
1745                   (new_text_length - 1 - i + entry_length
1746                     - *position > (gint)priv->digits))
1747                 return;
1748               dotpos = *position + i;
1749             }
1750           else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1751             return;
1752         }
1753     }
1754
1755   parent_editable_iface->insert_text (editable, new_text,
1756                                       new_text_length, position);
1757 }
1758
1759 static void
1760 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1761                            gdouble        increment)
1762 {
1763   GtkSpinButtonPrivate *priv = spin_button->priv;
1764   GtkAdjustment *adjustment;
1765   gdouble new_value = 0.0;
1766   gboolean wrapped = FALSE;
1767
1768   adjustment = priv->adjustment;
1769
1770   new_value = gtk_adjustment_get_value (adjustment) + increment;
1771
1772   if (increment > 0)
1773     {
1774       if (priv->wrap)
1775         {
1776           if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_upper (adjustment)) < EPSILON)
1777             {
1778               new_value = gtk_adjustment_get_lower (adjustment);
1779               wrapped = TRUE;
1780             }
1781           else if (new_value > gtk_adjustment_get_upper (adjustment))
1782             new_value = gtk_adjustment_get_upper (adjustment);
1783         }
1784       else
1785         new_value = MIN (new_value, gtk_adjustment_get_upper (adjustment));
1786     }
1787   else if (increment < 0)
1788     {
1789       if (priv->wrap)
1790         {
1791           if (fabs (gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment)) < EPSILON)
1792             {
1793               new_value = gtk_adjustment_get_upper (adjustment);
1794               wrapped = TRUE;
1795             }
1796           else if (new_value < gtk_adjustment_get_lower (adjustment))
1797             new_value = gtk_adjustment_get_lower (adjustment);
1798         }
1799       else
1800         new_value = MAX (new_value, gtk_adjustment_get_lower (adjustment));
1801     }
1802
1803   if (fabs (new_value - gtk_adjustment_get_value (adjustment)) > EPSILON)
1804     gtk_adjustment_set_value (adjustment, new_value);
1805
1806   if (wrapped)
1807     g_signal_emit (spin_button, spinbutton_signals[WRAPPED], 0);
1808
1809   gtk_widget_queue_draw (GTK_WIDGET (spin_button));
1810 }
1811
1812 static gint
1813 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1814                                gdouble       *new_val)
1815 {
1816   gchar *err = NULL;
1817
1818   *new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1819   if (*err)
1820     return GTK_INPUT_ERROR;
1821   else
1822     return FALSE;
1823 }
1824
1825 static gint
1826 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1827 {
1828   GtkSpinButtonPrivate *priv = spin_button->priv;
1829
1830   gchar *buf = g_strdup_printf ("%0.*f", priv->digits, gtk_adjustment_get_value (priv->adjustment));
1831
1832   if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1833     gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1834   g_free (buf);
1835   return FALSE;
1836 }
1837
1838
1839 /***********************************************************
1840  ***********************************************************
1841  ***                  Public interface                   ***
1842  ***********************************************************
1843  ***********************************************************/
1844
1845
1846 /**
1847  * gtk_spin_button_configure:
1848  * @spin_button: a #GtkSpinButton
1849  * @adjustment: (allow-none):  a #GtkAdjustment
1850  * @climb_rate: the new climb rate
1851  * @digits: the number of decimal places to display in the spin button
1852  *
1853  * Changes the properties of an existing spin button. The adjustment,
1854  * climb rate, and number of decimal places are all changed accordingly,
1855  * after this function call.
1856  */
1857 void
1858 gtk_spin_button_configure (GtkSpinButton *spin_button,
1859                            GtkAdjustment *adjustment,
1860                            gdouble        climb_rate,
1861                            guint          digits)
1862 {
1863   GtkSpinButtonPrivate *priv;
1864
1865   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1866
1867   priv = spin_button->priv;
1868
1869   if (adjustment)
1870     gtk_spin_button_set_adjustment (spin_button, adjustment);
1871   else
1872     adjustment = priv->adjustment;
1873
1874   g_object_freeze_notify (G_OBJECT (spin_button));
1875   if (priv->digits != digits)
1876     {
1877       priv->digits = digits;
1878       g_object_notify (G_OBJECT (spin_button), "digits");
1879     }
1880
1881   if (priv->climb_rate != climb_rate)
1882     {
1883       priv->climb_rate = climb_rate;
1884       g_object_notify (G_OBJECT (spin_button), "climb-rate");
1885     }
1886   g_object_thaw_notify (G_OBJECT (spin_button));
1887
1888   gtk_adjustment_value_changed (adjustment);
1889 }
1890
1891 /**
1892  * gtk_spin_button_new:
1893  * @adjustment: (allow-none): the #GtkAdjustment object that this spin
1894  *     button should use, or %NULL
1895  * @climb_rate: specifies how much the spin button changes when an arrow
1896  *     is clicked on
1897  * @digits: the number of decimal places to display
1898  *
1899  * Creates a new #GtkSpinButton.
1900  *
1901  * Returns: The new spin button as a #GtkWidget
1902  */
1903 GtkWidget *
1904 gtk_spin_button_new (GtkAdjustment *adjustment,
1905                      gdouble        climb_rate,
1906                      guint          digits)
1907 {
1908   GtkSpinButton *spin;
1909
1910   if (adjustment)
1911     g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1912
1913   spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1914
1915   gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1916
1917   return GTK_WIDGET (spin);
1918 }
1919
1920 /**
1921  * gtk_spin_button_new_with_range:
1922  * @min: Minimum allowable value
1923  * @max: Maximum allowable value
1924  * @step: Increment added or subtracted by spinning the widget
1925  *
1926  * This is a convenience constructor that allows creation of a numeric
1927  * #GtkSpinButton without manually creating an adjustment. The value is
1928  * initially set to the minimum value and a page increment of 10 * @step
1929  * is the default. The precision of the spin button is equivalent to the
1930  * precision of @step.
1931  *
1932  * Note that the way in which the precision is derived works best if @step
1933  * is a power of ten. If the resulting precision is not suitable for your
1934  * needs, use gtk_spin_button_set_digits() to correct it.
1935  *
1936  * Return value: The new spin button as a #GtkWidget
1937  */
1938 GtkWidget *
1939 gtk_spin_button_new_with_range (gdouble min,
1940                                 gdouble max,
1941                                 gdouble step)
1942 {
1943   GtkAdjustment *adjustment;
1944   GtkSpinButton *spin;
1945   gint digits;
1946
1947   g_return_val_if_fail (min <= max, NULL);
1948   g_return_val_if_fail (step != 0.0, NULL);
1949
1950   spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1951
1952   adjustment = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1953
1954   if (fabs (step) >= 1.0 || step == 0.0)
1955     digits = 0;
1956   else {
1957     digits = abs ((gint) floor (log10 (fabs (step))));
1958     if (digits > MAX_DIGITS)
1959       digits = MAX_DIGITS;
1960   }
1961
1962   gtk_spin_button_configure (spin, adjustment, step, digits);
1963
1964   gtk_spin_button_set_numeric (spin, TRUE);
1965
1966   return GTK_WIDGET (spin);
1967 }
1968
1969 /* Callback used when the spin button's adjustment changes.
1970  * We need to redraw the arrows when the adjustment's range
1971  * changes, and reevaluate our size request.
1972  */
1973 static void
1974 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1975 {
1976   GtkSpinButton *spin_button = GTK_SPIN_BUTTON (data);
1977   GtkSpinButtonPrivate *priv = spin_button->priv;
1978
1979   priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
1980   gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1981 }
1982
1983 /**
1984  * gtk_spin_button_set_adjustment:
1985  * @spin_button: a #GtkSpinButton
1986  * @adjustment: a #GtkAdjustment to replace the existing adjustment
1987  *
1988  * Replaces the #GtkAdjustment associated with @spin_button.
1989  */
1990 void
1991 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1992                                 GtkAdjustment *adjustment)
1993 {
1994   GtkSpinButtonPrivate *priv;
1995
1996   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1997
1998   priv = spin_button->priv;
1999
2000   if (priv->adjustment != adjustment)
2001     {
2002       if (priv->adjustment)
2003         {
2004           g_signal_handlers_disconnect_by_func (priv->adjustment,
2005                                                 gtk_spin_button_value_changed,
2006                                                 spin_button);
2007           g_signal_handlers_disconnect_by_func (priv->adjustment,
2008                                                 adjustment_changed_cb,
2009                                                 spin_button);
2010           g_object_unref (priv->adjustment);
2011         }
2012       priv->adjustment = adjustment;
2013       if (adjustment)
2014         {
2015           g_object_ref_sink (adjustment);
2016           g_signal_connect (adjustment, "value-changed",
2017                             G_CALLBACK (gtk_spin_button_value_changed),
2018                             spin_button);
2019           g_signal_connect (adjustment, "changed",
2020                             G_CALLBACK (adjustment_changed_cb),
2021                             spin_button);
2022           priv->timer_step = gtk_adjustment_get_step_increment (priv->adjustment);
2023         }
2024
2025       gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2026     }
2027
2028   g_object_notify (G_OBJECT (spin_button), "adjustment");
2029 }
2030
2031 /**
2032  * gtk_spin_button_get_adjustment:
2033  * @spin_button: a #GtkSpinButton
2034  *
2035  * Get the adjustment associated with a #GtkSpinButton
2036  *
2037  * Return value: (transfer none): the #GtkAdjustment of @spin_button
2038  **/
2039 GtkAdjustment *
2040 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
2041 {
2042   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
2043
2044   return spin_button->priv->adjustment;
2045 }
2046
2047 /**
2048  * gtk_spin_button_set_digits:
2049  * @spin_button: a #GtkSpinButton
2050  * @digits: the number of digits after the decimal point to be displayed for the spin button's value
2051  *
2052  * Set the precision to be displayed by @spin_button. Up to 20 digit precision
2053  * is allowed.
2054  **/
2055 void
2056 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
2057                             guint          digits)
2058 {
2059   GtkSpinButtonPrivate *priv;
2060
2061   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2062
2063   priv = spin_button->priv;
2064
2065   if (priv->digits != digits)
2066     {
2067       priv->digits = digits;
2068       gtk_spin_button_value_changed (priv->adjustment, spin_button);
2069       g_object_notify (G_OBJECT (spin_button), "digits");
2070
2071       /* since lower/upper may have changed */
2072       gtk_widget_queue_resize (GTK_WIDGET (spin_button));
2073     }
2074 }
2075
2076 /**
2077  * gtk_spin_button_get_digits:
2078  * @spin_button: a #GtkSpinButton
2079  *
2080  * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
2081  *
2082  * Returns: the current precision
2083  **/
2084 guint
2085 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
2086 {
2087   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2088
2089   return spin_button->priv->digits;
2090 }
2091
2092 /**
2093  * gtk_spin_button_set_increments:
2094  * @spin_button: a #GtkSpinButton
2095  * @step: increment applied for a button 1 press.
2096  * @page: increment applied for a button 2 press.
2097  *
2098  * Sets the step and page increments for spin_button.  This affects how
2099  * quickly the value changes when the spin button's arrows are activated.
2100  **/
2101 void
2102 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
2103                                 gdouble        step,
2104                                 gdouble        page)
2105 {
2106   GtkSpinButtonPrivate *priv;
2107
2108   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2109
2110   priv = spin_button->priv;
2111
2112   gtk_adjustment_configure (priv->adjustment,
2113                             gtk_adjustment_get_value (priv->adjustment),
2114                             gtk_adjustment_get_lower (priv->adjustment),
2115                             gtk_adjustment_get_upper (priv->adjustment),
2116                             step,
2117                             page,
2118                             gtk_adjustment_get_page_size (priv->adjustment));
2119 }
2120
2121 /**
2122  * gtk_spin_button_get_increments:
2123  * @spin_button: a #GtkSpinButton
2124  * @step: (out) (allow-none): location to store step increment, or %NULL
2125  * @page: (out) (allow-none): location to store page increment, or %NULL
2126  *
2127  * Gets the current step and page the increments used by @spin_button. See
2128  * gtk_spin_button_set_increments().
2129  **/
2130 void
2131 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
2132                                 gdouble       *step,
2133                                 gdouble       *page)
2134 {
2135   GtkSpinButtonPrivate *priv;
2136
2137   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2138
2139   priv = spin_button->priv;
2140
2141   if (step)
2142     *step = gtk_adjustment_get_step_increment (priv->adjustment);
2143   if (page)
2144     *page = gtk_adjustment_get_page_increment (priv->adjustment);
2145 }
2146
2147 /**
2148  * gtk_spin_button_set_range:
2149  * @spin_button: a #GtkSpinButton
2150  * @min: minimum allowable value
2151  * @max: maximum allowable value
2152  *
2153  * Sets the minimum and maximum allowable values for @spin_button.
2154  *
2155  * If the current value is outside this range, it will be adjusted
2156  * to fit within the range, otherwise it will remain unchanged.
2157  */
2158 void
2159 gtk_spin_button_set_range (GtkSpinButton *spin_button,
2160                            gdouble        min,
2161                            gdouble        max)
2162 {
2163   GtkAdjustment *adjustment;
2164
2165   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2166
2167   adjustment = spin_button->priv->adjustment;
2168
2169   gtk_adjustment_configure (adjustment,
2170                             CLAMP (gtk_adjustment_get_value (adjustment), min, max),
2171                             min,
2172                             max,
2173                             gtk_adjustment_get_step_increment (adjustment),
2174                             gtk_adjustment_get_page_increment (adjustment),
2175                             gtk_adjustment_get_page_size (adjustment));
2176 }
2177
2178 /**
2179  * gtk_spin_button_get_range:
2180  * @spin_button: a #GtkSpinButton
2181  * @min: (out) (allow-none): location to store minimum allowed value, or %NULL
2182  * @max: (out) (allow-none): location to store maximum allowed value, or %NULL
2183  *
2184  * Gets the range allowed for @spin_button.
2185  * See gtk_spin_button_set_range().
2186  */
2187 void
2188 gtk_spin_button_get_range (GtkSpinButton *spin_button,
2189                            gdouble       *min,
2190                            gdouble       *max)
2191 {
2192   GtkSpinButtonPrivate *priv;
2193
2194   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2195
2196   priv = spin_button->priv;
2197
2198   if (min)
2199     *min = gtk_adjustment_get_lower (priv->adjustment);
2200   if (max)
2201     *max = gtk_adjustment_get_upper (priv->adjustment);
2202 }
2203
2204 /**
2205  * gtk_spin_button_get_value:
2206  * @spin_button: a #GtkSpinButton
2207  *
2208  * Get the value in the @spin_button.
2209  *
2210  * Return value: the value of @spin_button
2211  */
2212 gdouble
2213 gtk_spin_button_get_value (GtkSpinButton *spin_button)
2214 {
2215   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
2216
2217   return gtk_adjustment_get_value (spin_button->priv->adjustment);
2218 }
2219
2220 /**
2221  * gtk_spin_button_get_value_as_int:
2222  * @spin_button: a #GtkSpinButton
2223  *
2224  * Get the value @spin_button represented as an integer.
2225  *
2226  * Return value: the value of @spin_button
2227  */
2228 gint
2229 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
2230 {
2231   GtkSpinButtonPrivate *priv;
2232   gdouble val;
2233
2234   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
2235
2236   priv = spin_button->priv;
2237
2238   val = gtk_adjustment_get_value (priv->adjustment);
2239   if (val - floor (val) < ceil (val) - val)
2240     return floor (val);
2241   else
2242     return ceil (val);
2243 }
2244
2245 /**
2246  * gtk_spin_button_set_value:
2247  * @spin_button: a #GtkSpinButton
2248  * @value: the new value
2249  *
2250  * Sets the value of @spin_button.
2251  */
2252 void
2253 gtk_spin_button_set_value (GtkSpinButton *spin_button,
2254                            gdouble        value)
2255 {
2256   GtkSpinButtonPrivate *priv;
2257
2258   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2259
2260   priv = spin_button->priv;
2261
2262   if (fabs (value - gtk_adjustment_get_value (priv->adjustment)) > EPSILON)
2263     gtk_adjustment_set_value (priv->adjustment, value);
2264   else
2265     {
2266       gint return_val = FALSE;
2267       g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
2268       if (return_val == FALSE)
2269         gtk_spin_button_default_output (spin_button);
2270     }
2271 }
2272
2273 /**
2274  * gtk_spin_button_set_update_policy:
2275  * @spin_button: a #GtkSpinButton
2276  * @policy: a #GtkSpinButtonUpdatePolicy value
2277  *
2278  * Sets the update behavior of a spin button.
2279  * This determines wether the spin button is always updated
2280  * or only when a valid value is set.
2281  */
2282 void
2283 gtk_spin_button_set_update_policy (GtkSpinButton             *spin_button,
2284                                    GtkSpinButtonUpdatePolicy  policy)
2285 {
2286   GtkSpinButtonPrivate *priv;
2287
2288   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2289
2290   priv = spin_button->priv;
2291
2292   if (priv->update_policy != policy)
2293     {
2294       priv->update_policy = policy;
2295       g_object_notify (G_OBJECT (spin_button), "update-policy");
2296     }
2297 }
2298
2299 /**
2300  * gtk_spin_button_get_update_policy:
2301  * @spin_button: a #GtkSpinButton
2302  *
2303  * Gets the update behavior of a spin button.
2304  * See gtk_spin_button_set_update_policy().
2305  *
2306  * Return value: the current update policy
2307  */
2308 GtkSpinButtonUpdatePolicy
2309 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
2310 {
2311   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
2312
2313   return spin_button->priv->update_policy;
2314 }
2315
2316 /**
2317  * gtk_spin_button_set_numeric:
2318  * @spin_button: a #GtkSpinButton
2319  * @numeric: flag indicating if only numeric entry is allowed
2320  *
2321  * Sets the flag that determines if non-numeric text can be typed
2322  * into the spin button.
2323  */
2324 void
2325 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
2326                              gboolean       numeric)
2327 {
2328   GtkSpinButtonPrivate *priv;
2329
2330   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2331
2332   priv = spin_button->priv;
2333
2334   numeric = numeric != FALSE;
2335
2336   if (priv->numeric != numeric)
2337     {
2338        priv->numeric = numeric;
2339        g_object_notify (G_OBJECT (spin_button), "numeric");
2340     }
2341 }
2342
2343 /**
2344  * gtk_spin_button_get_numeric:
2345  * @spin_button: a #GtkSpinButton
2346  *
2347  * Returns whether non-numeric text can be typed into the spin button.
2348  * See gtk_spin_button_set_numeric().
2349  *
2350  * Return value: %TRUE if only numeric text can be entered
2351  */
2352 gboolean
2353 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
2354 {
2355   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2356
2357   return spin_button->priv->numeric;
2358 }
2359
2360 /**
2361  * gtk_spin_button_set_wrap:
2362  * @spin_button: a #GtkSpinButton
2363  * @wrap: a flag indicating if wrapping behavior is performed
2364  *
2365  * Sets the flag that determines if a spin button value wraps
2366  * around to the opposite limit when the upper or lower limit
2367  * of the range is exceeded.
2368  */
2369 void
2370 gtk_spin_button_set_wrap (GtkSpinButton  *spin_button,
2371                           gboolean        wrap)
2372 {
2373   GtkSpinButtonPrivate *priv;
2374
2375   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2376
2377   priv = spin_button->priv;
2378
2379   wrap = wrap != FALSE;
2380
2381   if (priv->wrap != wrap)
2382     {
2383        priv->wrap = wrap;
2384
2385        g_object_notify (G_OBJECT (spin_button), "wrap");
2386     }
2387 }
2388
2389 /**
2390  * gtk_spin_button_get_wrap:
2391  * @spin_button: a #GtkSpinButton
2392  *
2393  * Returns whether the spin button's value wraps around to the
2394  * opposite limit when the upper or lower limit of the range is
2395  * exceeded. See gtk_spin_button_set_wrap().
2396  *
2397  * Return value: %TRUE if the spin button wraps around
2398  */
2399 gboolean
2400 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2401 {
2402   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2403
2404   return spin_button->priv->wrap;
2405 }
2406
2407 static gint
2408 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2409 {
2410   const PangoFontDescription *font_desc;
2411   GtkStyleContext *context;
2412   gint size;
2413   gint arrow_size;
2414
2415   /* FIXME: use getter */
2416   context = gtk_widget_get_style_context (GTK_WIDGET (spin_button));
2417   font_desc = gtk_style_context_get_font (context, 0);
2418
2419   size = pango_font_description_get_size (font_desc);
2420   arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2421
2422   return arrow_size - arrow_size % 2; /* force even */
2423 }
2424
2425 /**
2426  * gtk_spin_button_set_snap_to_ticks:
2427  * @spin_button: a #GtkSpinButton
2428  * @snap_to_ticks: a flag indicating if invalid values should be corrected
2429  *
2430  * Sets the policy as to whether values are corrected to the
2431  * nearest step increment when a spin button is activated after
2432  * providing an invalid value.
2433  */
2434 void
2435 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2436                                    gboolean       snap_to_ticks)
2437 {
2438   GtkSpinButtonPrivate *priv;
2439   guint new_val;
2440
2441   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2442
2443   priv = spin_button->priv;
2444
2445   new_val = (snap_to_ticks != 0);
2446
2447   if (new_val != priv->snap_to_ticks)
2448     {
2449       priv->snap_to_ticks = new_val;
2450       if (new_val && gtk_editable_get_editable (GTK_EDITABLE (spin_button)))
2451         gtk_spin_button_update (spin_button);
2452
2453       g_object_notify (G_OBJECT (spin_button), "snap-to-ticks");
2454     }
2455 }
2456
2457 /**
2458  * gtk_spin_button_get_snap_to_ticks:
2459  * @spin_button: a #GtkSpinButton
2460  *
2461  * Returns whether the values are corrected to the nearest step.
2462  * See gtk_spin_button_set_snap_to_ticks().
2463  *
2464  * Return value: %TRUE if values are snapped to the nearest step
2465  */
2466 gboolean
2467 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2468 {
2469   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2470
2471   return spin_button->priv->snap_to_ticks;
2472 }
2473
2474 /**
2475  * gtk_spin_button_spin:
2476  * @spin_button: a #GtkSpinButton
2477  * @direction: a #GtkSpinType indicating the direction to spin
2478  * @increment: step increment to apply in the specified direction
2479  *
2480  * Increment or decrement a spin button's value in a specified
2481  * direction by a specified amount.
2482  */
2483 void
2484 gtk_spin_button_spin (GtkSpinButton *spin_button,
2485                       GtkSpinType    direction,
2486                       gdouble        increment)
2487 {
2488   GtkSpinButtonPrivate *priv;
2489   GtkAdjustment *adjustment;
2490   gdouble diff;
2491
2492   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2493
2494   priv = spin_button->priv;
2495
2496   adjustment = priv->adjustment;
2497
2498   /* for compatibility with the 1.0.x version of this function */
2499   if (increment != 0 && increment != gtk_adjustment_get_step_increment (adjustment) &&
2500       (direction == GTK_SPIN_STEP_FORWARD ||
2501        direction == GTK_SPIN_STEP_BACKWARD))
2502     {
2503       if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2504         increment = -increment;
2505       direction = GTK_SPIN_USER_DEFINED;
2506     }
2507
2508   switch (direction)
2509     {
2510     case GTK_SPIN_STEP_FORWARD:
2511
2512       gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_step_increment (adjustment));
2513       break;
2514
2515     case GTK_SPIN_STEP_BACKWARD:
2516
2517       gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_step_increment (adjustment));
2518       break;
2519
2520     case GTK_SPIN_PAGE_FORWARD:
2521
2522       gtk_spin_button_real_spin (spin_button, gtk_adjustment_get_page_increment (adjustment));
2523       break;
2524
2525     case GTK_SPIN_PAGE_BACKWARD:
2526
2527       gtk_spin_button_real_spin (spin_button, -gtk_adjustment_get_page_increment (adjustment));
2528       break;
2529
2530     case GTK_SPIN_HOME:
2531
2532       diff = gtk_adjustment_get_value (adjustment) - gtk_adjustment_get_lower (adjustment);
2533       if (diff > EPSILON)
2534         gtk_spin_button_real_spin (spin_button, -diff);
2535       break;
2536
2537     case GTK_SPIN_END:
2538
2539       diff = gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_value (adjustment);
2540       if (diff > EPSILON)
2541         gtk_spin_button_real_spin (spin_button, diff);
2542       break;
2543
2544     case GTK_SPIN_USER_DEFINED:
2545
2546       if (increment != 0)
2547         gtk_spin_button_real_spin (spin_button, increment);
2548       break;
2549
2550     default:
2551       break;
2552     }
2553 }
2554
2555 /**
2556  * gtk_spin_button_update:
2557  * @spin_button: a #GtkSpinButton
2558  *
2559  * Manually force an update of the spin button.
2560  */
2561 void
2562 gtk_spin_button_update (GtkSpinButton *spin_button)
2563 {
2564   GtkSpinButtonPrivate *priv;
2565   gdouble val;
2566   gint error = 0;
2567   gint return_val;
2568
2569   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2570
2571   priv = spin_button->priv;
2572
2573   return_val = FALSE;
2574   g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2575   if (return_val == FALSE)
2576     {
2577       return_val = gtk_spin_button_default_input (spin_button, &val);
2578       error = (return_val == GTK_INPUT_ERROR);
2579     }
2580   else if (return_val == GTK_INPUT_ERROR)
2581     error = 1;
2582
2583   gtk_widget_queue_draw (GTK_WIDGET (spin_button));
2584
2585   if (priv->update_policy == GTK_UPDATE_ALWAYS)
2586     {
2587       if (val < gtk_adjustment_get_lower (priv->adjustment))
2588         val = gtk_adjustment_get_lower (priv->adjustment);
2589       else if (val > gtk_adjustment_get_upper (priv->adjustment))
2590         val = gtk_adjustment_get_upper (priv->adjustment);
2591     }
2592   else if ((priv->update_policy == GTK_UPDATE_IF_VALID) &&
2593            (error ||
2594             val < gtk_adjustment_get_lower (priv->adjustment) ||
2595             val > gtk_adjustment_get_upper (priv->adjustment)))
2596     {
2597       gtk_spin_button_value_changed (priv->adjustment, spin_button);
2598       return;
2599     }
2600
2601   if (priv->snap_to_ticks)
2602     gtk_spin_button_snap (spin_button, val);
2603   else
2604     gtk_spin_button_set_value (spin_button, val);
2605 }
2606
2607 GdkWindow *
2608 _gtk_spin_button_get_panel (GtkSpinButton *spin_button)
2609 {
2610   return spin_button->priv->panel;
2611 }