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