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