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