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