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