]> Pileus Git - ~andy/gtk/blob - gtk/gtkspinbutton.c
Improve docs. (116364, Morten Welinder)
[~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 <stdio.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34 #include <locale.h>
35 #include "gdk/gdkkeysyms.h"
36 #include "gtkbindings.h"
37 #include "gtkspinbutton.h"
38 #include "gtkmain.h"
39 #include "gtkmarshalers.h"
40 #include "gtksettings.h"
41 #include "gtkintl.h"
42
43 #define MIN_SPIN_BUTTON_WIDTH              30
44 #define SPIN_BUTTON_INITIAL_TIMER_DELAY    200
45 #define SPIN_BUTTON_TIMER_DELAY            20
46 #define MAX_TIMER_CALLS                    5
47 #define EPSILON                            1e-10
48 #define MAX_DIGITS                         20
49 #define MIN_ARROW_WIDTH                    6
50
51 enum {
52   PROP_0,
53   PROP_ADJUSTMENT,
54   PROP_CLIMB_RATE,
55   PROP_DIGITS,
56   PROP_SNAP_TO_TICKS,
57   PROP_NUMERIC,
58   PROP_WRAP,
59   PROP_UPDATE_POLICY,
60   PROP_VALUE
61 };
62
63 /* Signals */
64 enum
65 {
66   INPUT,
67   OUTPUT,
68   VALUE_CHANGED,
69   CHANGE_VALUE,
70   LAST_SIGNAL
71 };
72
73 static void gtk_spin_button_class_init     (GtkSpinButtonClass *klass);
74 static void gtk_spin_button_editable_init  (GtkEditableClass   *iface);
75 static void gtk_spin_button_init           (GtkSpinButton      *spin_button);
76 static void gtk_spin_button_finalize       (GObject            *object);
77 static void gtk_spin_button_destroy        (GtkObject          *object);
78 static void gtk_spin_button_set_property   (GObject         *object,
79                                             guint            prop_id,
80                                             const GValue    *value,
81                                             GParamSpec      *pspec);
82 static void gtk_spin_button_get_property   (GObject         *object,
83                                             guint            prop_id,
84                                             GValue          *value,
85                                             GParamSpec      *pspec);
86 static void gtk_spin_button_map            (GtkWidget          *widget);
87 static void gtk_spin_button_unmap          (GtkWidget          *widget);
88 static void gtk_spin_button_realize        (GtkWidget          *widget);
89 static void gtk_spin_button_unrealize      (GtkWidget          *widget);
90 static void gtk_spin_button_size_request   (GtkWidget          *widget,
91                                             GtkRequisition     *requisition);
92 static void gtk_spin_button_size_allocate  (GtkWidget          *widget,
93                                             GtkAllocation      *allocation);
94 static gint gtk_spin_button_expose         (GtkWidget          *widget,
95                                             GdkEventExpose     *event);
96 static gint gtk_spin_button_button_press   (GtkWidget          *widget,
97                                             GdkEventButton     *event);
98 static gint gtk_spin_button_button_release (GtkWidget          *widget,
99                                             GdkEventButton     *event);
100 static gint gtk_spin_button_motion_notify  (GtkWidget          *widget,
101                                             GdkEventMotion     *event);
102 static gint gtk_spin_button_enter_notify   (GtkWidget          *widget,
103                                             GdkEventCrossing   *event);
104 static gint gtk_spin_button_leave_notify   (GtkWidget          *widget,
105                                             GdkEventCrossing   *event);
106 static gint gtk_spin_button_focus_out      (GtkWidget          *widget,
107                                             GdkEventFocus      *event);
108 static void gtk_spin_button_grab_notify    (GtkWidget          *widget,
109                                             gboolean            was_grabbed);
110 static void gtk_spin_button_state_changed  (GtkWidget          *widget,
111                                             GtkStateType        previous_state);
112 static void gtk_spin_button_draw_arrow     (GtkSpinButton      *spin_button, 
113                                             GtkArrowType        arrow_type);
114 static gint gtk_spin_button_timer          (GtkSpinButton      *spin_button);
115 static void gtk_spin_button_stop_spinning  (GtkSpinButton      *spin);
116 static void gtk_spin_button_value_changed  (GtkAdjustment      *adjustment,
117                                             GtkSpinButton      *spin_button); 
118 static gint gtk_spin_button_key_release    (GtkWidget          *widget,
119                                             GdkEventKey        *event);
120 static gint gtk_spin_button_scroll         (GtkWidget          *widget,
121                                             GdkEventScroll     *event);
122 static void gtk_spin_button_activate       (GtkEntry           *entry);
123 static void gtk_spin_button_snap           (GtkSpinButton      *spin_button,
124                                             gdouble             val);
125 static void gtk_spin_button_insert_text    (GtkEditable        *editable,
126                                             const gchar        *new_text,
127                                             gint                new_text_length,
128                                             gint               *position);
129 static void gtk_spin_button_real_spin      (GtkSpinButton      *spin_button,
130                                             gdouble             step);
131 static void gtk_spin_button_real_change_value (GtkSpinButton   *spin,
132                                                GtkScrollType    scroll);
133
134 static gint gtk_spin_button_default_input  (GtkSpinButton      *spin_button,
135                                             gdouble            *new_val);
136 static gint gtk_spin_button_default_output (GtkSpinButton      *spin_button);
137
138 static gint spin_button_get_arrow_size     (GtkSpinButton      *spin_button);
139 static gint spin_button_get_shadow_type    (GtkSpinButton      *spin_button);
140 static void spin_button_redraw             (GtkSpinButton      *spin_button);
141
142
143 static GtkEntryClass *parent_class = NULL;
144 static guint spinbutton_signals[LAST_SIGNAL] = {0};
145
146 #define NO_ARROW 2
147
148 GType
149 gtk_spin_button_get_type (void)
150 {
151   static GType spin_button_type = 0;
152
153   if (!spin_button_type)
154     {
155       static const GTypeInfo spin_button_info =
156       {
157         sizeof (GtkSpinButtonClass),
158         NULL,           /* base_init */
159         NULL,           /* base_finalize */
160         (GClassInitFunc) gtk_spin_button_class_init,
161         NULL,           /* class_finalize */
162         NULL,           /* class_data */
163         sizeof (GtkSpinButton),
164         0,              /* n_preallocs */
165         (GInstanceInitFunc) gtk_spin_button_init,
166       };
167
168       static const GInterfaceInfo editable_info =
169       {
170         (GInterfaceInitFunc) gtk_spin_button_editable_init, /* interface_init */
171         NULL, /* interface_finalize */
172         NULL  /* interface_data */
173       };
174
175       spin_button_type =
176         g_type_register_static (GTK_TYPE_ENTRY, "GtkSpinButton",
177                                 &spin_button_info, 0);
178
179       g_type_add_interface_static (spin_button_type,
180                                    GTK_TYPE_EDITABLE,
181                                    &editable_info);
182     }
183   return spin_button_type;
184 }
185
186 #define add_spin_binding(binding_set, keyval, mask, scroll)            \
187   gtk_binding_entry_add_signal (binding_set, keyval, mask,             \
188                                 "change_value", 1,                     \
189                                 GTK_TYPE_SCROLL_TYPE, scroll)
190
191 static void
192 gtk_spin_button_class_init (GtkSpinButtonClass *class)
193 {
194   GObjectClass     *gobject_class = G_OBJECT_CLASS (class);
195   GtkObjectClass   *object_class;
196   GtkWidgetClass   *widget_class;
197   GtkEntryClass    *entry_class;
198   GtkBindingSet    *binding_set;
199
200   object_class   = (GtkObjectClass*)   class;
201   widget_class   = (GtkWidgetClass*)   class;
202   entry_class    = (GtkEntryClass*)    class;
203
204   parent_class = g_type_class_peek_parent (class);
205
206   gobject_class->finalize = gtk_spin_button_finalize;
207
208   gobject_class->set_property = gtk_spin_button_set_property;
209   gobject_class->get_property = gtk_spin_button_get_property;
210
211   object_class->destroy = gtk_spin_button_destroy;
212
213   widget_class->map = gtk_spin_button_map;
214   widget_class->unmap = gtk_spin_button_unmap;
215   widget_class->realize = gtk_spin_button_realize;
216   widget_class->unrealize = gtk_spin_button_unrealize;
217   widget_class->size_request = gtk_spin_button_size_request;
218   widget_class->size_allocate = gtk_spin_button_size_allocate;
219   widget_class->expose_event = gtk_spin_button_expose;
220   widget_class->scroll_event = gtk_spin_button_scroll;
221   widget_class->button_press_event = gtk_spin_button_button_press;
222   widget_class->button_release_event = gtk_spin_button_button_release;
223   widget_class->motion_notify_event = gtk_spin_button_motion_notify;
224   widget_class->key_release_event = gtk_spin_button_key_release;
225   widget_class->enter_notify_event = gtk_spin_button_enter_notify;
226   widget_class->leave_notify_event = gtk_spin_button_leave_notify;
227   widget_class->focus_out_event = gtk_spin_button_focus_out;
228   widget_class->grab_notify = gtk_spin_button_grab_notify;
229   widget_class->state_changed = gtk_spin_button_state_changed;
230
231   entry_class->activate = gtk_spin_button_activate;
232
233   class->input = NULL;
234   class->output = NULL;
235   class->change_value = gtk_spin_button_real_change_value;
236
237   g_object_class_install_property (gobject_class,
238                                    PROP_ADJUSTMENT,
239                                    g_param_spec_object ("adjustment",
240                                                         _("Adjustment"),
241                                                         _("The adjustment that holds the value of the spinbutton"),
242                                                         GTK_TYPE_ADJUSTMENT,
243                                                         G_PARAM_READWRITE));
244   
245   g_object_class_install_property (gobject_class,
246                                    PROP_CLIMB_RATE,
247                                    g_param_spec_double ("climb_rate",
248                                                         _("Climb Rate"),
249                                                         _("The acceleration rate when you hold down a button"),
250                                                         0.0,
251                                                         G_MAXDOUBLE,
252                                                         0.0,
253                                                         G_PARAM_READWRITE));  
254   
255   g_object_class_install_property (gobject_class,
256                                    PROP_DIGITS,
257                                    g_param_spec_uint ("digits",
258                                                       _("Digits"),
259                                                       _("The number of decimal places to display"),
260                                                       0,
261                                                       MAX_DIGITS,
262                                                       0,
263                                                       G_PARAM_READWRITE));
264   
265   g_object_class_install_property (gobject_class,
266                                    PROP_SNAP_TO_TICKS,
267                                    g_param_spec_boolean ("snap_to_ticks",
268                                                          _("Snap to Ticks"),
269                                                          _("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
270                                                          FALSE,
271                                                          G_PARAM_READWRITE));
272   
273   g_object_class_install_property (gobject_class,
274                                    PROP_NUMERIC,
275                                    g_param_spec_boolean ("numeric",
276                                                          _("Numeric"),
277                                                          _("Whether non-numeric characters should be ignored"),
278                                                          FALSE,
279                                                          G_PARAM_READWRITE));
280   
281   g_object_class_install_property (gobject_class,
282                                    PROP_WRAP,
283                                    g_param_spec_boolean ("wrap",
284                                                          _("Wrap"),
285                                                          _("Whether a spin button should wrap upon reaching its limits"),
286                                                          FALSE,
287                                                          G_PARAM_READWRITE));
288   
289   g_object_class_install_property (gobject_class,
290                                    PROP_UPDATE_POLICY,
291                                    g_param_spec_enum ("update_policy",
292                                                       _("Update Policy"),
293                                                       _("Whether the spin button should update always, or only when the value is legal"),
294                                                       GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
295                                                       GTK_UPDATE_ALWAYS,
296                                                       G_PARAM_READWRITE));
297   
298   g_object_class_install_property (gobject_class,
299                                    PROP_VALUE,
300                                    g_param_spec_double ("value",
301                                                         _("Value"),
302                                                         _("Reads the current value, or sets a new value"),
303                                                         -G_MAXDOUBLE,
304                                                         G_MAXDOUBLE,
305                                                         0.0,
306                                                         G_PARAM_READWRITE));  
307   
308   gtk_widget_class_install_style_property_parser (widget_class,
309                                                   g_param_spec_enum ("shadow_type", 
310                                                                      "Shadow Type", 
311                                                                      _("Style of bevel around the spin button"),
312                                                                      GTK_TYPE_SHADOW_TYPE,
313                                                                      GTK_SHADOW_IN,
314                                                                      G_PARAM_READABLE),
315                                                   gtk_rc_property_parse_enum);
316   spinbutton_signals[INPUT] =
317     g_signal_new ("input",
318                   G_TYPE_FROM_CLASS (gobject_class),
319                   G_SIGNAL_RUN_LAST,
320                   G_STRUCT_OFFSET (GtkSpinButtonClass, input),
321                   NULL, NULL,
322                   _gtk_marshal_INT__POINTER,
323                   G_TYPE_INT, 1,
324                   G_TYPE_POINTER);
325
326   spinbutton_signals[OUTPUT] =
327     g_signal_new ("output",
328                   G_TYPE_FROM_CLASS (gobject_class),
329                   G_SIGNAL_RUN_LAST,
330                   G_STRUCT_OFFSET (GtkSpinButtonClass, output),
331                   _gtk_boolean_handled_accumulator, NULL,
332                   _gtk_marshal_BOOLEAN__VOID,
333                   G_TYPE_BOOLEAN, 0);
334
335   spinbutton_signals[VALUE_CHANGED] =
336     g_signal_new ("value_changed",
337                   G_TYPE_FROM_CLASS (gobject_class),
338                   G_SIGNAL_RUN_LAST,
339                   G_STRUCT_OFFSET (GtkSpinButtonClass, value_changed),
340                   NULL, NULL,
341                   _gtk_marshal_VOID__VOID,
342                   G_TYPE_NONE, 0);
343
344   /* Action signals */
345   spinbutton_signals[CHANGE_VALUE] =
346     g_signal_new ("change_value",
347                   G_TYPE_FROM_CLASS (gobject_class),
348                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
349                   G_STRUCT_OFFSET (GtkSpinButtonClass, change_value),
350                   NULL, NULL,
351                   _gtk_marshal_VOID__ENUM,
352                   G_TYPE_NONE, 1,
353                   GTK_TYPE_SCROLL_TYPE);
354   
355   binding_set = gtk_binding_set_by_class (class);
356   
357   add_spin_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
358   add_spin_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
359   add_spin_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
360   add_spin_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
361   add_spin_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
362   add_spin_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_DOWN);
363   add_spin_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_END);
364   add_spin_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_START);
365 }
366
367 static void
368 gtk_spin_button_editable_init (GtkEditableClass *iface)
369 {
370   iface->insert_text = gtk_spin_button_insert_text;
371 }
372
373 static void
374 gtk_spin_button_set_property (GObject      *object,
375                               guint         prop_id,
376                               const GValue *value,
377                               GParamSpec   *pspec)
378 {
379   GtkSpinButton *spin_button;
380
381   spin_button = GTK_SPIN_BUTTON (object);
382   
383   switch (prop_id)
384     {
385       GtkAdjustment *adjustment;
386
387     case PROP_ADJUSTMENT:
388       adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
389       if (!adjustment)
390         adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
391       gtk_spin_button_set_adjustment (spin_button, adjustment);
392       break;
393     case PROP_CLIMB_RATE:
394       gtk_spin_button_configure (spin_button,
395                                  spin_button->adjustment,
396                                  g_value_get_double (value),
397                                  spin_button->digits);
398       break;
399     case PROP_DIGITS:
400       gtk_spin_button_configure (spin_button,
401                                  spin_button->adjustment,
402                                  spin_button->climb_rate,
403                                  g_value_get_uint (value));
404       break;
405     case PROP_SNAP_TO_TICKS:
406       gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
407       break;
408     case PROP_NUMERIC:
409       gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
410       break;
411     case PROP_WRAP:
412       gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
413       break;
414     case PROP_UPDATE_POLICY:
415       gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
416       break;
417     case PROP_VALUE:
418       gtk_spin_button_set_value (spin_button, g_value_get_double (value));
419       break;
420     default:
421       break;
422     }
423 }
424
425 static void
426 gtk_spin_button_get_property (GObject      *object,
427                               guint         prop_id,
428                               GValue       *value,
429                               GParamSpec   *pspec)
430 {
431   GtkSpinButton *spin_button;
432
433   spin_button = GTK_SPIN_BUTTON (object);
434   
435   switch (prop_id)
436     {
437     case PROP_ADJUSTMENT:
438       g_value_set_object (value, spin_button->adjustment);
439       break;
440     case PROP_CLIMB_RATE:
441       g_value_set_double (value, spin_button->climb_rate);
442       break;
443     case PROP_DIGITS:
444       g_value_set_uint (value, spin_button->digits);
445       break;
446     case PROP_SNAP_TO_TICKS:
447       g_value_set_boolean (value, spin_button->snap_to_ticks);
448       break;
449     case PROP_NUMERIC:
450       g_value_set_boolean (value, spin_button->numeric);
451       break;
452     case PROP_WRAP:
453       g_value_set_boolean (value, spin_button->wrap);
454       break;
455     case PROP_UPDATE_POLICY:
456       g_value_set_enum (value, spin_button->update_policy);
457       break;
458      case PROP_VALUE:
459        g_value_set_double (value, spin_button->adjustment->value);
460       break;
461     default:
462       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
463       break;
464     }
465 }
466
467 static void
468 gtk_spin_button_init (GtkSpinButton *spin_button)
469 {
470   spin_button->adjustment = NULL;
471   spin_button->panel = NULL;
472   spin_button->timer = 0;
473   spin_button->climb_rate = 0.0;
474   spin_button->timer_step = 0.0;
475   spin_button->update_policy = GTK_UPDATE_ALWAYS;
476   spin_button->in_child = NO_ARROW;
477   spin_button->click_child = NO_ARROW;
478   spin_button->button = 0;
479   spin_button->need_timer = FALSE;
480   spin_button->timer_calls = 0;
481   spin_button->digits = 0;
482   spin_button->numeric = FALSE;
483   spin_button->wrap = FALSE;
484   spin_button->snap_to_ticks = FALSE;
485
486   gtk_spin_button_set_adjustment (spin_button,
487           (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
488 }
489
490 static void
491 gtk_spin_button_finalize (GObject *object)
492 {
493   gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (object), NULL);
494   
495   G_OBJECT_CLASS (parent_class)->finalize (object);
496 }
497
498 static void
499 gtk_spin_button_destroy (GtkObject *object)
500 {
501   gtk_spin_button_stop_spinning (GTK_SPIN_BUTTON (object));
502   
503   GTK_OBJECT_CLASS (parent_class)->destroy (object);
504 }
505
506 static void
507 gtk_spin_button_map (GtkWidget *widget)
508 {
509   if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
510     {
511       GTK_WIDGET_CLASS (parent_class)->map (widget);
512       gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
513     }
514 }
515
516 static void
517 gtk_spin_button_unmap (GtkWidget *widget)
518 {
519   if (GTK_WIDGET_MAPPED (widget))
520     {
521       gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
522       GTK_WIDGET_CLASS (parent_class)->unmap (widget);
523     }
524 }
525
526 static void
527 gtk_spin_button_realize (GtkWidget *widget)
528 {
529   GtkSpinButton *spin_button;
530   GdkWindowAttr attributes;
531   gint attributes_mask;
532   guint real_width;
533   gboolean return_val;
534   gint arrow_size;
535
536   spin_button = GTK_SPIN_BUTTON (widget);
537   arrow_size = spin_button_get_arrow_size (spin_button);
538
539   real_width = widget->allocation.width;
540   widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
541   gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
542                          GDK_KEY_RELEASE_MASK);
543   GTK_WIDGET_CLASS (parent_class)->realize (widget);
544
545   widget->allocation.width = real_width;
546   
547   attributes.window_type = GDK_WINDOW_CHILD;
548   attributes.wclass = GDK_INPUT_OUTPUT;
549   attributes.visual = gtk_widget_get_visual (widget);
550   attributes.colormap = gtk_widget_get_colormap (widget);
551   attributes.event_mask = gtk_widget_get_events (widget);
552   attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK 
553     | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK 
554     | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
555
556   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
557
558   attributes.x = (widget->allocation.x +
559                   widget->allocation.width - arrow_size -
560                   2 * widget->style->xthickness);
561   attributes.y = widget->allocation.y + (widget->allocation.height -
562                                          widget->requisition.height) / 2;
563   attributes.width = arrow_size + 2 * widget->style->xthickness;
564   attributes.height = widget->requisition.height;
565   
566   spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget), 
567                                        &attributes, attributes_mask);
568   gdk_window_set_user_data (spin_button->panel, widget);
569
570   gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
571
572   return_val = FALSE;
573   g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
574   if (return_val == FALSE)
575     gtk_spin_button_default_output (spin_button);
576
577   gtk_widget_queue_resize (GTK_WIDGET (spin_button));
578 }
579
580 static void
581 gtk_spin_button_unrealize (GtkWidget *widget)
582 {
583   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
584
585   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
586
587   if (spin->panel)
588     {
589       gdk_window_set_user_data (spin->panel, NULL);
590       gdk_window_destroy (spin->panel);
591       spin->panel = NULL;
592     }
593 }
594
595 static int
596 compute_double_length (double val, int digits)
597 {
598   int a;
599   int extra;
600
601   a = 1;
602   if (fabs (val) > 1.0)
603     a = floor (log10 (fabs (val))) + 1;  
604
605   extra = 0;
606   
607   /* The dot: */
608   if (digits > 0)
609     extra++;
610
611   /* The sign: */
612   if (val < 0)
613     extra++;
614
615   return a + digits + extra;
616 }
617
618 static void
619 gtk_spin_button_size_request (GtkWidget      *widget,
620                               GtkRequisition *requisition)
621 {
622   GtkEntry *entry;
623   GtkSpinButton *spin_button;
624   gint arrow_size;
625
626   entry = GTK_ENTRY (widget);
627   spin_button = GTK_SPIN_BUTTON (widget);
628   arrow_size = spin_button_get_arrow_size (spin_button);
629   
630   GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
631
632   if (entry->width_chars < 0)
633     {
634       PangoContext *context;
635       PangoFontMetrics *metrics;
636       gint width;
637       gint w;
638       gint string_len;
639       gint max_string_len;
640       gint digit_width;
641       gboolean interior_focus;
642       gint focus_width;
643
644       gtk_widget_style_get (widget,
645                             "interior-focus", &interior_focus,
646                             "focus-line-width", &focus_width,
647                             NULL);
648
649       context = gtk_widget_get_pango_context (widget);
650       metrics = pango_context_get_metrics (context,
651                                            widget->style->font_desc,
652                                            pango_context_get_language (context));
653
654       digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
655       digit_width = PANGO_PIXELS (digit_width);
656
657       pango_font_metrics_unref (metrics);
658       
659       /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
660       
661       width = MIN_SPIN_BUTTON_WIDTH;
662       max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
663                                                        spin_button->digits));
664
665       string_len = compute_double_length (spin_button->adjustment->upper,
666                                           spin_button->digits);
667       w = MIN (string_len, max_string_len) * digit_width;
668       width = MAX (width, w);
669       string_len = compute_double_length (spin_button->adjustment->lower,
670                                           spin_button->digits);
671       w = MIN (string_len, max_string_len) * digit_width;
672       width = MAX (width, w);
673       
674       requisition->width = width;
675       if (interior_focus)
676         requisition->width += 2 * focus_width;
677     }
678
679   requisition->width += arrow_size + 2 * widget->style->xthickness;
680 }
681
682 static void
683 gtk_spin_button_size_allocate (GtkWidget     *widget,
684                                GtkAllocation *allocation)
685 {
686   GtkSpinButton *spin;
687   GtkAllocation entry_allocation;
688   GtkAllocation panel_allocation;
689   gint arrow_size;
690   gint panel_width;
691
692   g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
693   g_return_if_fail (allocation != NULL);
694
695   spin = GTK_SPIN_BUTTON (widget);
696   arrow_size = spin_button_get_arrow_size (spin);
697   panel_width = arrow_size + 2 * widget->style->xthickness;
698   
699   widget->allocation = *allocation;
700   
701   entry_allocation = *allocation;
702   entry_allocation.width -= panel_width;
703
704   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
705     {
706       entry_allocation.x += panel_width;
707       panel_allocation.x = allocation->x;
708     }
709   else
710     {
711       panel_allocation.x = allocation->x + allocation->width - panel_width;
712     }
713
714   panel_allocation.width = panel_width;
715   panel_allocation.height = MIN (widget->requisition.height, allocation->height);
716
717   panel_allocation.y = allocation->y + (allocation->height -
718                                        panel_allocation.height) / 2;
719
720   GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &entry_allocation);
721
722   if (GTK_WIDGET_REALIZED (widget))
723     {
724       gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel, 
725                               panel_allocation.x,
726                               panel_allocation.y,
727                               panel_allocation.width,
728                               panel_allocation.height); 
729     }
730
731   spin_button_redraw (spin);
732 }
733
734 static gint
735 gtk_spin_button_expose (GtkWidget      *widget,
736                         GdkEventExpose *event)
737 {
738   GtkSpinButton *spin;
739
740   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
741   g_return_val_if_fail (event != NULL, FALSE);
742
743   spin = GTK_SPIN_BUTTON (widget);
744
745   if (GTK_WIDGET_DRAWABLE (widget))
746     {
747       GtkShadowType shadow_type;
748       GdkRectangle rect;
749
750       if (event->window != spin->panel)
751         GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
752
753       /* we redraw the panel even if it wasn't exposed. This is
754        * because spin->panel is not a child window of widget->window,
755        * so it will not be invalidated by eg. gtk_widget_queue_draw().
756        */
757       rect.x = 0;
758       rect.y = 0;
759
760       gdk_drawable_get_size (spin->panel, &rect.width, &rect.height);
761
762       shadow_type = spin_button_get_shadow_type (spin);
763       
764       gdk_window_begin_paint_rect (spin->panel, &rect);      
765
766       if (shadow_type != GTK_SHADOW_NONE)
767         {
768           gtk_paint_box (widget->style, spin->panel,
769                          GTK_STATE_NORMAL, shadow_type,
770                          NULL, widget, "spinbutton",
771                          rect.x, rect.y, rect.width, rect.height);
772         }
773
774       gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
775       gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
776
777       gdk_window_end_paint (spin->panel);
778     }
779   
780   return FALSE;
781 }
782
783 static gboolean
784 spin_button_at_limit (GtkSpinButton *spin_button,
785                      GtkArrowType   arrow)
786 {
787   if (spin_button->wrap)
788     return FALSE;
789
790   if (arrow == GTK_ARROW_UP &&
791       (spin_button->adjustment->upper - spin_button->adjustment->value <= EPSILON))
792     return TRUE;
793   
794   if (arrow == GTK_ARROW_DOWN &&
795       (spin_button->adjustment->value - spin_button->adjustment->lower <= EPSILON))
796     return TRUE;
797   
798   return FALSE;
799 }
800
801 static void
802 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button, 
803                             GtkArrowType   arrow_type)
804 {
805   GtkStateType state_type;
806   GtkShadowType shadow_type;
807   GtkWidget *widget;
808   gint x;
809   gint y;
810   gint height;
811   gint width;
812   gint h, w;
813
814   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
815   g_return_if_fail (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN);
816   
817   widget = GTK_WIDGET (spin_button);
818
819   if (GTK_WIDGET_DRAWABLE (widget))
820     {
821       width = spin_button_get_arrow_size (spin_button) + 2 * widget->style->xthickness;
822
823       if (arrow_type == GTK_ARROW_UP)
824         {
825           x = 0;
826           y = 0;
827
828           height = widget->requisition.height / 2;
829         }
830       else
831         {
832           x = 0;
833           y = widget->requisition.height / 2;
834
835           height = (widget->requisition.height + 1) / 2;
836         }
837
838       if (spin_button_at_limit (spin_button, arrow_type))
839         {
840           shadow_type = GTK_SHADOW_OUT;
841           state_type = GTK_STATE_INSENSITIVE;
842         }
843       else
844         {
845           if (spin_button->click_child == arrow_type)
846             {
847               state_type = GTK_STATE_ACTIVE;
848               shadow_type = GTK_SHADOW_IN;
849             }
850           else
851             {
852               if (spin_button->in_child == arrow_type &&
853                   spin_button->click_child == NO_ARROW)
854                 {
855                   state_type = GTK_STATE_PRELIGHT;
856                 }
857               else
858                 {
859                   state_type = GTK_WIDGET_STATE (widget);
860                 }
861               
862               shadow_type = GTK_SHADOW_OUT;
863             }
864         }
865       
866       gtk_paint_box (widget->style, spin_button->panel,
867                      state_type, shadow_type,
868                      NULL, widget,
869                      (arrow_type == GTK_ARROW_UP)? "spinbutton_up" : "spinbutton_down",
870                      x, y, width, height);
871
872       height = widget->requisition.height;
873
874       if (arrow_type == GTK_ARROW_DOWN)
875         {
876           y = height / 2;
877           height = height - y - 2;
878         }
879       else
880         {
881           y = 2;
882           height = height / 2 - 2;
883         }
884
885       width -= 3;
886
887       if (widget && gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
888         x = 2;
889       else
890         x = 1;
891
892       w = width / 2;
893       w -= w % 2 - 1; /* force odd */
894       h = (w + 1) / 2;
895       
896       x += (width - w) / 2;
897       y += (height - h) / 2;
898       
899       height = h;
900       width = w;
901
902       gtk_paint_arrow (widget->style, spin_button->panel,
903                        state_type, shadow_type, 
904                        NULL, widget, "spinbutton",
905                        arrow_type, TRUE, 
906                        x, y, width, height);
907     }
908 }
909
910 static gint
911 gtk_spin_button_enter_notify (GtkWidget        *widget,
912                               GdkEventCrossing *event)
913 {
914   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
915
916   if (event->window == spin->panel)
917     {
918       gint x;
919       gint y;
920
921       gdk_window_get_pointer (spin->panel, &x, &y, NULL);
922
923       if (y <= widget->requisition.height / 2)
924         spin->in_child = GTK_ARROW_UP;
925       else
926         spin->in_child = GTK_ARROW_DOWN;
927
928       spin_button_redraw (spin);
929     }
930   
931   return FALSE;
932 }
933
934 static gint
935 gtk_spin_button_leave_notify (GtkWidget        *widget,
936                               GdkEventCrossing *event)
937 {
938   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
939
940   spin->in_child = NO_ARROW;
941   spin_button_redraw (spin);
942   
943   return FALSE;
944 }
945
946 static gint
947 gtk_spin_button_focus_out (GtkWidget     *widget,
948                            GdkEventFocus *event)
949 {
950   if (GTK_ENTRY (widget)->editable)
951     gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
952
953   return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
954 }
955
956 static void
957 gtk_spin_button_grab_notify (GtkWidget *widget,
958                              gboolean   was_grabbed)
959 {
960   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
961
962   if (!was_grabbed)
963     {
964       gtk_spin_button_stop_spinning (spin);
965       spin_button_redraw (spin);
966     }
967 }
968
969 static void
970 gtk_spin_button_state_changed (GtkWidget    *widget,
971                                GtkStateType  previous_state)
972 {
973   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
974
975   if (!GTK_WIDGET_IS_SENSITIVE (widget))
976     {
977       gtk_spin_button_stop_spinning (spin);    
978       spin_button_redraw (spin);
979     }
980 }
981
982 static gint
983 gtk_spin_button_scroll (GtkWidget      *widget,
984                         GdkEventScroll *event)
985 {
986   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
987
988   if (event->direction == GDK_SCROLL_UP)
989     {
990       if (!GTK_WIDGET_HAS_FOCUS (widget))
991         gtk_widget_grab_focus (widget);
992       gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
993     }
994   else if (event->direction == GDK_SCROLL_DOWN)
995     {
996       if (!GTK_WIDGET_HAS_FOCUS (widget))
997         gtk_widget_grab_focus (widget);
998       gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment); 
999     }
1000   else
1001     return FALSE;
1002
1003   return TRUE;
1004 }
1005
1006 static void
1007 gtk_spin_button_stop_spinning (GtkSpinButton *spin)
1008 {
1009   if (spin->timer)
1010     {
1011       g_source_remove (spin->timer);
1012       spin->timer = 0;
1013       spin->timer_calls = 0;
1014       spin->need_timer = FALSE;
1015     }
1016
1017   spin->button = 0;
1018   spin->timer = 0;
1019   spin->timer_step = spin->adjustment->step_increment;
1020   spin->timer_calls = 0;
1021
1022   spin->click_child = NO_ARROW;
1023   spin->button = 0;
1024 }
1025
1026 static void
1027 start_spinning (GtkSpinButton *spin,
1028                 GtkArrowType   click_child,
1029                 gfloat         step)
1030 {
1031   g_return_if_fail (click_child == GTK_ARROW_UP || click_child == GTK_ARROW_DOWN);
1032   
1033   spin->click_child = click_child;
1034   gtk_spin_button_real_spin (spin, click_child == GTK_ARROW_UP ? step : -step);
1035   
1036   if (!spin->timer)
1037     {
1038       spin->timer_step = step;
1039       spin->need_timer = TRUE;
1040       spin->timer = g_timeout_add (SPIN_BUTTON_INITIAL_TIMER_DELAY, 
1041                                    (GtkFunction) gtk_spin_button_timer, 
1042                                    (gpointer) spin);
1043     }
1044
1045   spin_button_redraw (spin);
1046 }
1047
1048 static gint
1049 gtk_spin_button_button_press (GtkWidget      *widget,
1050                               GdkEventButton *event)
1051 {
1052   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1053
1054   if (!spin->button)
1055     {
1056       if (event->window == spin->panel)
1057         {
1058           if (!GTK_WIDGET_HAS_FOCUS (widget))
1059             gtk_widget_grab_focus (widget);
1060           spin->button = event->button;
1061           
1062           if (GTK_ENTRY (widget)->editable)
1063             gtk_spin_button_update (spin);
1064           
1065           if (event->y <= widget->requisition.height / 2)
1066             {
1067               if (event->button == 1)
1068                 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->step_increment);
1069               else if (event->button == 2)
1070                 start_spinning (spin, GTK_ARROW_UP, spin->adjustment->page_increment);
1071               else
1072                 spin->click_child = GTK_ARROW_UP;
1073             }
1074           else 
1075             {
1076               if (event->button == 1)
1077                 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->step_increment);
1078               else if (event->button == 2)
1079                 start_spinning (spin, GTK_ARROW_DOWN, spin->adjustment->page_increment);
1080               else
1081                 spin->click_child = GTK_ARROW_DOWN;
1082             }
1083           return TRUE;
1084         }
1085       else
1086         return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1087     }
1088   return FALSE;
1089 }
1090
1091 static gint
1092 gtk_spin_button_button_release (GtkWidget      *widget,
1093                                 GdkEventButton *event)
1094 {
1095   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1096   gint arrow_size;
1097
1098   arrow_size = spin_button_get_arrow_size (spin);
1099
1100   if (event->button == spin->button)
1101     {
1102       int click_child = spin->click_child;
1103
1104       gtk_spin_button_stop_spinning (spin);
1105
1106       if (event->button == 3)
1107         {
1108           if (event->y >= 0 && event->x >= 0 && 
1109               event->y <= widget->requisition.height &&
1110               event->x <= arrow_size + 2 * widget->style->xthickness)
1111             {
1112               if (click_child == GTK_ARROW_UP &&
1113                   event->y <= widget->requisition.height / 2)
1114                 {
1115                   gdouble diff;
1116
1117                   diff = spin->adjustment->upper - spin->adjustment->value;
1118                   if (diff > EPSILON)
1119                     gtk_spin_button_real_spin (spin, diff);
1120                 }
1121               else if (click_child == GTK_ARROW_DOWN &&
1122                        event->y > widget->requisition.height / 2)
1123                 {
1124                   gdouble diff;
1125
1126                   diff = spin->adjustment->value - spin->adjustment->lower;
1127                   if (diff > EPSILON)
1128                     gtk_spin_button_real_spin (spin, -diff);
1129                 }
1130             }
1131         }                 
1132       spin_button_redraw (spin);
1133
1134       return TRUE;
1135     }
1136   else
1137     return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1138 }
1139
1140 static gint
1141 gtk_spin_button_motion_notify (GtkWidget      *widget,
1142                                GdkEventMotion *event)
1143 {
1144   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1145
1146   if (spin->button)
1147     return FALSE;
1148
1149   if (event->window == spin->panel)
1150     {
1151       gint y;
1152       
1153       gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
1154   
1155       if (y <= widget->requisition.height / 2 && 
1156           spin->in_child == GTK_ARROW_DOWN)
1157         {
1158           spin->in_child = GTK_ARROW_UP;
1159           spin_button_redraw (spin);
1160         }
1161       else if (y > widget->requisition.height / 2 && 
1162           spin->in_child == GTK_ARROW_UP)
1163         {
1164           spin->in_child = GTK_ARROW_DOWN;
1165           spin_button_redraw (spin);
1166         }
1167       
1168       return FALSE;
1169     }
1170           
1171   return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1172 }
1173
1174 static gint
1175 gtk_spin_button_timer (GtkSpinButton *spin_button)
1176 {
1177   gboolean retval = FALSE;
1178   
1179   GDK_THREADS_ENTER ();
1180
1181   if (spin_button->timer)
1182     {
1183       if (spin_button->click_child == GTK_ARROW_UP)
1184         gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1185       else
1186         gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1187
1188       if (spin_button->need_timer)
1189         {
1190           spin_button->need_timer = FALSE;
1191           spin_button->timer = g_timeout_add (SPIN_BUTTON_TIMER_DELAY, 
1192                                               (GtkFunction) gtk_spin_button_timer, 
1193                                               (gpointer) spin_button);
1194         }
1195       else 
1196         {
1197           if (spin_button->climb_rate > 0.0 && spin_button->timer_step 
1198               < spin_button->adjustment->page_increment)
1199             {
1200               if (spin_button->timer_calls < MAX_TIMER_CALLS)
1201                 spin_button->timer_calls++;
1202               else 
1203                 {
1204                   spin_button->timer_calls = 0;
1205                   spin_button->timer_step += spin_button->climb_rate;
1206                 }
1207             }
1208           retval = TRUE;
1209         }
1210     }
1211
1212   GDK_THREADS_LEAVE ();
1213
1214   return retval;
1215 }
1216
1217 static void
1218 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1219                                GtkSpinButton *spin_button)
1220 {
1221   gboolean return_val;
1222
1223   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1224
1225   return_val = FALSE;
1226   g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1227   if (return_val == FALSE)
1228     gtk_spin_button_default_output (spin_button);
1229
1230   g_signal_emit (spin_button, spinbutton_signals[VALUE_CHANGED], 0);
1231
1232   spin_button_redraw (spin_button);
1233   
1234   g_object_notify (G_OBJECT (spin_button), "value");
1235 }
1236
1237 static void
1238 gtk_spin_button_real_change_value (GtkSpinButton *spin,
1239                                    GtkScrollType  scroll)
1240 {
1241   /* We don't test whether the entry is editable, since
1242    * this key binding conceptually corresponds to changing
1243    * the value with the buttons using the mouse, which
1244    * we allow for non-editable spin buttons.
1245    */
1246   switch (scroll)
1247     {
1248     case GTK_SCROLL_STEP_BACKWARD:
1249     case GTK_SCROLL_STEP_DOWN:
1250     case GTK_SCROLL_STEP_LEFT:
1251       gtk_spin_button_real_spin (spin, -spin->timer_step);
1252       
1253       if (spin->climb_rate > 0.0 && spin->timer_step
1254           < spin->adjustment->page_increment)
1255         {
1256           if (spin->timer_calls < MAX_TIMER_CALLS)
1257             spin->timer_calls++;
1258           else 
1259             {
1260               spin->timer_calls = 0;
1261               spin->timer_step += spin->climb_rate;
1262             }
1263         }
1264       break;
1265       
1266     case GTK_SCROLL_STEP_FORWARD:
1267     case GTK_SCROLL_STEP_UP:
1268     case GTK_SCROLL_STEP_RIGHT:
1269       gtk_spin_button_real_spin (spin, spin->timer_step);
1270       
1271       if (spin->climb_rate > 0.0 && spin->timer_step
1272           < spin->adjustment->page_increment)
1273         {
1274           if (spin->timer_calls < MAX_TIMER_CALLS)
1275             spin->timer_calls++;
1276           else 
1277             {
1278               spin->timer_calls = 0;
1279               spin->timer_step += spin->climb_rate;
1280             }
1281         }
1282       break;
1283       
1284     case GTK_SCROLL_PAGE_BACKWARD:
1285     case GTK_SCROLL_PAGE_DOWN:
1286     case GTK_SCROLL_PAGE_LEFT:
1287       gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1288       break;
1289       
1290     case GTK_SCROLL_PAGE_FORWARD:
1291     case GTK_SCROLL_PAGE_UP:
1292     case GTK_SCROLL_PAGE_RIGHT:
1293       gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1294       break;
1295       
1296     case GTK_SCROLL_START:
1297       {
1298         gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1299         if (diff > EPSILON)
1300           gtk_spin_button_real_spin (spin, -diff);
1301         break;
1302       }
1303       
1304     case GTK_SCROLL_END:
1305       {
1306         gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1307         if (diff > EPSILON)
1308           gtk_spin_button_real_spin (spin, diff);
1309         break;
1310       }
1311       
1312     default:
1313       g_warning ("Invalid scroll type %d for GtkSpinButton::change-value", scroll);
1314       break;
1315     }
1316   
1317   gtk_spin_button_update (spin);
1318 }
1319
1320 static gint
1321 gtk_spin_button_key_release (GtkWidget   *widget,
1322                              GdkEventKey *event)
1323 {
1324   GtkSpinButton *spin = GTK_SPIN_BUTTON (widget);
1325
1326   /* We only get a release at the end of a key repeat run, so reset the timer_step */
1327   spin->timer_step = spin->adjustment->step_increment;
1328   spin->timer_calls = 0;
1329   
1330   return TRUE;
1331 }
1332
1333 static void
1334 gtk_spin_button_snap (GtkSpinButton *spin_button,
1335                       gdouble        val)
1336 {
1337   gdouble inc;
1338   gdouble tmp;
1339
1340   inc = spin_button->adjustment->step_increment;
1341   if (inc == 0)
1342     return;
1343   
1344   tmp = (val - spin_button->adjustment->lower) / inc;
1345   if (tmp - floor (tmp) < ceil (tmp) - tmp)
1346     val = spin_button->adjustment->lower + floor (tmp) * inc;
1347   else
1348     val = spin_button->adjustment->lower + ceil (tmp) * inc;
1349
1350   if (fabs (val - spin_button->adjustment->value) > EPSILON)
1351     gtk_adjustment_set_value (spin_button->adjustment, val);
1352   else
1353     {
1354       gint return_val = FALSE;
1355       g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1356       if (return_val == FALSE)
1357         gtk_spin_button_default_output (spin_button);
1358     }
1359 }
1360
1361 static void
1362 gtk_spin_button_activate (GtkEntry *entry)
1363 {
1364   if (entry->editable)
1365     gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1366
1367   /* Chain up so that entry->activates_default is honored */
1368   parent_class->activate (entry);
1369 }
1370
1371 static void
1372 gtk_spin_button_insert_text (GtkEditable *editable,
1373                              const gchar *new_text,
1374                              gint         new_text_length,
1375                              gint        *position)
1376 {
1377   GtkEntry *entry = GTK_ENTRY (editable);
1378   GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1379   GtkEditableClass *parent_editable_iface = g_type_interface_peek (parent_class, GTK_TYPE_EDITABLE);
1380  
1381   if (spin->numeric)
1382     {
1383       struct lconv *lc;
1384       gboolean sign;
1385       gint dotpos = -1;
1386       gint i;
1387       GdkWChar pos_sign;
1388       GdkWChar neg_sign;
1389       gint entry_length;
1390
1391       entry_length = entry->text_length;
1392
1393       lc = localeconv ();
1394
1395       if (*(lc->negative_sign))
1396         neg_sign = *(lc->negative_sign);
1397       else 
1398         neg_sign = '-';
1399
1400       if (*(lc->positive_sign))
1401         pos_sign = *(lc->positive_sign);
1402       else 
1403         pos_sign = '+';
1404
1405       for (sign=0, i=0; i<entry_length; i++)
1406         if ((entry->text[i] == neg_sign) ||
1407             (entry->text[i] == pos_sign))
1408           {
1409             sign = 1;
1410             break;
1411           }
1412
1413       if (sign && !(*position))
1414         return;
1415
1416       for (dotpos=-1, i=0; i<entry_length; i++)
1417         if (entry->text[i] == *(lc->decimal_point))
1418           {
1419             dotpos = i;
1420             break;
1421           }
1422
1423       if (dotpos > -1 && *position > dotpos &&
1424           (gint)spin->digits - entry_length
1425             + dotpos - new_text_length + 1 < 0)
1426         return;
1427
1428       for (i = 0; i < new_text_length; i++)
1429         {
1430           if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1431             {
1432               if (sign || (*position) || i)
1433                 return;
1434               sign = TRUE;
1435             }
1436           else if (new_text[i] == *(lc->decimal_point))
1437             {
1438               if (!spin->digits || dotpos > -1 || 
1439                   (new_text_length - 1 - i + entry_length
1440                     - *position > (gint)spin->digits)) 
1441                 return;
1442               dotpos = *position + i;
1443             }
1444           else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1445             return;
1446         }
1447     }
1448
1449   parent_editable_iface->insert_text (editable, new_text,
1450                                       new_text_length, position);
1451 }
1452
1453 static void
1454 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1455                            gdouble        increment)
1456 {
1457   GtkAdjustment *adj;
1458   gdouble new_value = 0.0;
1459   
1460   adj = spin_button->adjustment;
1461
1462   new_value = adj->value + increment;
1463
1464   if (increment > 0)
1465     {
1466       if (spin_button->wrap)
1467         {
1468           if (fabs (adj->value - adj->upper) < EPSILON)
1469             new_value = adj->lower;
1470           else if (new_value > adj->upper)
1471             new_value = adj->upper;
1472         }
1473       else
1474         new_value = MIN (new_value, adj->upper);
1475     }
1476   else if (increment < 0) 
1477     {
1478       if (spin_button->wrap)
1479         {
1480           if (fabs (adj->value - adj->lower) < EPSILON)
1481             new_value = adj->upper;
1482           else if (new_value < adj->lower)
1483             new_value = adj->lower;
1484         }
1485       else
1486         new_value = MAX (new_value, adj->lower);
1487     }
1488
1489   if (fabs (new_value - adj->value) > EPSILON)
1490     gtk_adjustment_set_value (adj, new_value);
1491
1492   spin_button_redraw (spin_button);
1493 }
1494
1495 static gint
1496 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1497                                gdouble       *new_val)
1498 {
1499   gchar *err = NULL;
1500
1501   *new_val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1502   if (*err)
1503     return GTK_INPUT_ERROR;
1504   else
1505     return FALSE;
1506 }
1507
1508 static gint
1509 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1510 {
1511   gchar *buf = g_strdup_printf ("%0.*f", spin_button->digits, spin_button->adjustment->value);
1512
1513   if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1514     gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1515   g_free (buf);
1516   return FALSE;
1517 }
1518
1519
1520 /***********************************************************
1521  ***********************************************************
1522  ***                  Public interface                   ***
1523  ***********************************************************
1524  ***********************************************************/
1525
1526
1527 void
1528 gtk_spin_button_configure (GtkSpinButton  *spin_button,
1529                            GtkAdjustment  *adjustment,
1530                            gdouble         climb_rate,
1531                            guint           digits)
1532 {
1533   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1534
1535   if (adjustment)
1536     gtk_spin_button_set_adjustment (spin_button, adjustment);
1537   else
1538     adjustment = spin_button->adjustment;
1539
1540   g_object_freeze_notify (G_OBJECT (spin_button));
1541   if (spin_button->digits != digits) 
1542     {
1543       spin_button->digits = digits;
1544       g_object_notify (G_OBJECT (spin_button), "digits");
1545     }
1546
1547   if (spin_button->climb_rate != climb_rate)
1548     {
1549       spin_button->climb_rate = climb_rate;
1550       g_object_notify (G_OBJECT (spin_button), "climb_rate");
1551     }
1552   g_object_thaw_notify (G_OBJECT (spin_button));
1553
1554   gtk_adjustment_value_changed (adjustment);
1555 }
1556
1557 GtkWidget *
1558 gtk_spin_button_new (GtkAdjustment *adjustment,
1559                      gdouble        climb_rate,
1560                      guint          digits)
1561 {
1562   GtkSpinButton *spin;
1563
1564   if (adjustment)
1565     g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1566
1567   spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1568
1569   gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1570
1571   return GTK_WIDGET (spin);
1572 }
1573
1574 /**
1575  * gtk_spin_button_new_with_range:
1576  * @min: Minimum allowable value
1577  * @max: Maximum allowable value
1578  * @step: Increment added or subtracted by spinning the widget
1579  * 
1580  * This is a convenience constructor that allows creation of a numeric 
1581  * #GtkSpinButton without manually creating an adjustment. The value is 
1582  * initially set to the minimum value and a page increment of 10 * @step
1583  * is the default. The precision of the spin button is equivalent to the 
1584  * precision of @step.
1585  * 
1586  * Return value: The new spin button as a #GtkWidget.
1587  **/
1588 GtkWidget *
1589 gtk_spin_button_new_with_range (gdouble min,
1590                                 gdouble max,
1591                                 gdouble step)
1592 {
1593   GtkObject *adj;
1594   GtkSpinButton *spin;
1595   gint digits;
1596
1597   g_return_val_if_fail (min < max, NULL);
1598   g_return_val_if_fail (step != 0.0, NULL);
1599
1600   spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
1601
1602   adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1603
1604   if (fabs (step) >= 1.0 || step == 0.0)
1605     digits = 0;
1606   else {
1607     digits = abs ((gint) floor (log10 (fabs (step))));
1608     if (digits > MAX_DIGITS)
1609       digits = MAX_DIGITS;
1610   }
1611
1612   gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1613
1614   gtk_spin_button_set_numeric (spin, TRUE);
1615
1616   return GTK_WIDGET (spin);
1617 }
1618
1619 /* Callback used when the spin button's adjustment changes.  We need to redraw
1620  * the arrows when the adjustment's range changes, and reevaluate our size request.
1621  */
1622 static void
1623 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1624 {
1625   GtkSpinButton *spin_button;
1626
1627   spin_button = GTK_SPIN_BUTTON (data);
1628
1629   spin_button->timer_step = spin_button->adjustment->step_increment;
1630   gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1631 }
1632
1633 /**
1634  * gtk_spin_button_set_adjustment:
1635  * @spin_button: a #GtkSpinButton
1636  * @adjustment: a #GtkAdjustment to replace the existing adjustment
1637  * 
1638  * Replaces the #GtkAdjustment associated with @spin_button.
1639  **/
1640 void
1641 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1642                                 GtkAdjustment *adjustment)
1643 {
1644   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1645
1646   if (spin_button->adjustment != adjustment)
1647     {
1648       if (spin_button->adjustment)
1649         {
1650           g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1651                                                 gtk_spin_button_value_changed,
1652                                                 spin_button);
1653           g_signal_handlers_disconnect_by_func (spin_button->adjustment,
1654                                                 adjustment_changed_cb,
1655                                                 spin_button);
1656           g_object_unref (spin_button->adjustment);
1657         }
1658       spin_button->adjustment = adjustment;
1659       if (adjustment)
1660         {
1661           g_object_ref (adjustment);
1662           gtk_object_sink (GTK_OBJECT (adjustment));
1663           g_signal_connect (adjustment, "value_changed",
1664                             G_CALLBACK (gtk_spin_button_value_changed),
1665                             spin_button);
1666           g_signal_connect (adjustment, "changed",
1667                             G_CALLBACK (adjustment_changed_cb),
1668                             spin_button);
1669           spin_button->timer_step = spin_button->adjustment->step_increment;
1670         }
1671
1672       gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1673     }
1674
1675   g_object_notify (G_OBJECT (spin_button), "adjustment");
1676 }
1677
1678 /**
1679  * gtk_spin_button_get_adjustment:
1680  * @spin_button: 
1681  * 
1682  * Get the adjustment associated with a #GtkSpinButton
1683  * 
1684  * Return value: the #GtkAdjustment of @spin_button
1685  **/
1686 GtkAdjustment *
1687 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1688 {
1689   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1690
1691   return spin_button->adjustment;
1692 }
1693
1694 /**
1695  * gtk_spin_button_set_digits:
1696  * @spin_button: a #GtkSpinButton
1697  * @digits: the number of digits after the decimal point to be displayed for the spin button's value
1698  * 
1699  * Set the precision to be displayed by @spin_button. Up to 20 digit precision
1700  * is allowed.
1701  **/
1702 void
1703 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1704                             guint          digits)
1705 {
1706   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1707
1708   if (spin_button->digits != digits)
1709     {
1710       spin_button->digits = digits;
1711       gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1712       g_object_notify (G_OBJECT (spin_button), "digits");
1713       
1714       /* since lower/upper may have changed */
1715       gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1716     }
1717 }
1718
1719 /**
1720  * gtk_spin_button_get_digits:
1721  * @spin_button: a #GtkSpinButton
1722  *
1723  * Fetches the precision of @spin_button. See gtk_spin_button_set_digits().
1724  *
1725  * Returns: the current precision
1726  **/
1727 guint
1728 gtk_spin_button_get_digits (GtkSpinButton *spin_button)
1729 {
1730   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1731
1732   return spin_button->digits;
1733 }
1734
1735 /**
1736  * gtk_spin_button_set_increments:
1737  * @spin_button: a #GtkSpinButton
1738  * @step: increment applied for a button 1 press.
1739  * @page: increment applied for a button 2 press.
1740  * 
1741  * Sets the step and page increments for spin_button.  This affects how 
1742  * quickly the value changes when the spin button's arrows are activated.
1743  **/
1744 void
1745 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1746                                 gdouble        step,
1747                                 gdouble        page)
1748 {
1749   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1750
1751   spin_button->adjustment->step_increment = step;
1752   spin_button->adjustment->page_increment = page;
1753 }
1754
1755 /**
1756  * gtk_spin_button_get_increments:
1757  * @spin_button: a #GtkSpinButton
1758  * @step: location to store step increment, or %NULL
1759  * @page: location to store page increment, or %NULL
1760  *
1761  * Gets the current step and page the increments used by @spin_button. See
1762  * gtk_spin_button_set_increments().
1763  **/
1764 void
1765 gtk_spin_button_get_increments (GtkSpinButton *spin_button,
1766                                 gdouble       *step,
1767                                 gdouble       *page)
1768 {
1769   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1770
1771   if (step)
1772     *step = spin_button->adjustment->step_increment;
1773   if (page)
1774     *page = spin_button->adjustment->page_increment;
1775 }
1776
1777 /**
1778  * gtk_spin_button_set_range:
1779  * @spin_button: a #GtkSpinButton
1780  * @min: minimum allowable value
1781  * @max: maximum allowable value
1782  * 
1783  * Sets the minimum and maximum allowable values for @spin_button
1784  **/
1785 void
1786 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1787                            gdouble        min,
1788                            gdouble        max)
1789 {
1790   gdouble value;
1791   
1792   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1793
1794   spin_button->adjustment->lower = min;
1795   spin_button->adjustment->upper = max;
1796
1797   value = CLAMP (spin_button->adjustment->value,
1798                  spin_button->adjustment->lower,
1799                  (spin_button->adjustment->upper - spin_button->adjustment->page_size));
1800
1801   if (value != spin_button->adjustment->value)
1802     gtk_spin_button_set_value (spin_button, value);
1803
1804   gtk_adjustment_changed (spin_button->adjustment);
1805 }
1806
1807 /**
1808  * gtk_spin_button_get_range:
1809  * @spin_button: a #GtkSpinButton
1810  * @min: location to store minimum allowed value, or %NULL
1811  * @max: location to store maximum allowed value, or %NULL
1812  *
1813  * Gets the range allowed for @spin_button. See
1814  * gtk_spin_button_set_range().
1815  **/
1816 void
1817 gtk_spin_button_get_range (GtkSpinButton *spin_button,
1818                            gdouble       *min,
1819                            gdouble       *max)
1820 {
1821   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1822
1823   if (min)
1824     *min = spin_button->adjustment->lower;
1825   if (max)
1826     *max = spin_button->adjustment->upper;
1827 }
1828
1829 /**
1830  * gtk_spin_button_get_value:
1831  * @spin_button: a #GtkSpinButton
1832  * 
1833  * Get the value in the @spin_button.
1834  * 
1835  * Return value: the value of @spin_button
1836  **/
1837 gdouble
1838 gtk_spin_button_get_value (GtkSpinButton *spin_button)
1839 {
1840   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1841
1842   return spin_button->adjustment->value;
1843 }
1844
1845 /**
1846  * gtk_spin_button_get_value_as_int:
1847  * @spin_button: a #GtkSpinButton
1848  * 
1849  * Get the value @spin_button represented as an integer.
1850  * 
1851  * Return value: the value of @spin_button
1852  **/
1853 gint
1854 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1855 {
1856   gdouble val;
1857
1858   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1859
1860   val = spin_button->adjustment->value;
1861   if (val - floor (val) < ceil (val) - val)
1862     return floor (val);
1863   else
1864     return ceil (val);
1865 }
1866
1867 /**
1868  * gtk_spin_button_set_value:
1869  * @spin_button: a #GtkSpinButton
1870  * @value: the new value
1871  * 
1872  * Set the value of @spin_button.
1873  **/
1874 void 
1875 gtk_spin_button_set_value (GtkSpinButton *spin_button, 
1876                            gdouble        value)
1877 {
1878   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1879
1880   if (fabs (value - spin_button->adjustment->value) > EPSILON)
1881     gtk_adjustment_set_value (spin_button->adjustment, value);
1882   else
1883     {
1884       gint return_val = FALSE;
1885       g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0, &return_val);
1886       if (return_val == FALSE)
1887         gtk_spin_button_default_output (spin_button);
1888     }
1889 }
1890
1891 /**
1892  * gtk_spin_button_set_update_policy:
1893  * @spin_button: a #GtkSpinButton 
1894  * @policy: a #GtkSpinButtonUpdatePolicy value
1895  * 
1896  * Sets the update behavior of a spin button. This determines whether the
1897  * spin button is always updated or only when a valid value is set.
1898  **/
1899 void
1900 gtk_spin_button_set_update_policy (GtkSpinButton             *spin_button,
1901                                    GtkSpinButtonUpdatePolicy  policy)
1902 {
1903   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1904
1905   if (spin_button->update_policy != policy)
1906     {
1907       spin_button->update_policy = policy;
1908       g_object_notify (G_OBJECT (spin_button), "update_policy");
1909     }
1910 }
1911
1912 /**
1913  * gtk_spin_button_get_update_policy:
1914  * @spin_button: a #GtkSpinButton
1915  *
1916  * Gets the update behavior of a spin button. See
1917  * gtk_spin_button_set_update_policy().
1918  *
1919  * Return value: the current update policy
1920  **/
1921 GtkSpinButtonUpdatePolicy
1922 gtk_spin_button_get_update_policy (GtkSpinButton *spin_button)
1923 {
1924   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), GTK_UPDATE_ALWAYS);
1925
1926   return spin_button->update_policy;
1927 }
1928
1929 /**
1930  * gtk_spin_button_set_numeric:
1931  * @spin_button: a #GtkSpinButton 
1932  * @numeric: flag indicating if only numeric entry is allowed. 
1933  * 
1934  * Sets the flag that determines if non-numeric text can be typed into
1935  * the spin button.
1936  **/
1937 void
1938 gtk_spin_button_set_numeric (GtkSpinButton  *spin_button,
1939                              gboolean        numeric)
1940 {
1941   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1942
1943   numeric = numeric != FALSE;
1944
1945   if (spin_button->numeric != numeric)
1946     {
1947        spin_button->numeric = numeric;
1948        g_object_notify (G_OBJECT (spin_button), "numeric");
1949     }
1950 }
1951
1952 /**
1953  * gtk_spin_button_get_numeric:
1954  * @spin_button: a #GtkSpinButton
1955  *
1956  * Returns whether non-numeric text can be typed into the spin button.
1957  * See gtk_spin_button_set_numeric().
1958  *
1959  * Return value: %TRUE if only numeric text can be entered
1960  **/
1961 gboolean
1962 gtk_spin_button_get_numeric (GtkSpinButton *spin_button)
1963 {
1964   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
1965
1966   return spin_button->numeric;
1967 }
1968
1969 /**
1970  * gtk_spin_button_set_wrap:
1971  * @spin_button: a #GtkSpinButton 
1972  * @wrap: a flag indicating if wrapping behavior is performed.
1973  * 
1974  * Sets the flag that determines if a spin button value wraps around to the
1975  * opposite limit when the upper or lower limit of the range is exceeded.
1976  **/
1977 void
1978 gtk_spin_button_set_wrap (GtkSpinButton  *spin_button,
1979                           gboolean        wrap)
1980 {
1981   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1982
1983   wrap = wrap != FALSE; 
1984
1985   if (spin_button->wrap != wrap)
1986     {
1987        spin_button->wrap = (wrap != 0);
1988   
1989        g_object_notify (G_OBJECT (spin_button), "wrap");
1990     }
1991 }
1992
1993 /**
1994  * gtk_spin_button_get_wrap:
1995  * @spin_button: a #GtkSpinButton
1996  *
1997  * Returns whether the spin button's value wraps around to the
1998  * opposite limit when the upper or lower limit of the range is
1999  * exceeded. See gtk_spin_button_set_wrap().
2000  *
2001  * Return value: %TRUE if the spin button wraps around
2002  **/
2003 gboolean
2004 gtk_spin_button_get_wrap (GtkSpinButton *spin_button)
2005 {
2006   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2007
2008   return spin_button->wrap;
2009 }
2010
2011 static gint
2012 spin_button_get_arrow_size (GtkSpinButton *spin_button)
2013 {
2014   gint size = pango_font_description_get_size (GTK_WIDGET (spin_button)->style->font_desc);
2015   gint arrow_size;
2016
2017   arrow_size = MAX (PANGO_PIXELS (size), MIN_ARROW_WIDTH);
2018
2019   return arrow_size - arrow_size % 2; /* force even */
2020 }
2021
2022 /**
2023  * spin_button_get_shadow_type:
2024  * @spin_button: a #GtkSpinButton 
2025  * 
2026  * Convenience function to Get the shadow type from the underlying widget's
2027  * style.
2028  * 
2029  * Return value: the #GtkShadowType
2030  **/
2031 static gint
2032 spin_button_get_shadow_type (GtkSpinButton *spin_button)
2033 {
2034   GtkShadowType rc_shadow_type;
2035
2036   gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
2037
2038   return rc_shadow_type;
2039 }
2040
2041 /**
2042  * gtk_spin_button_set_snap_to_ticks:
2043  * @spin_button: a #GtkSpinButton 
2044  * @snap_to_ticks: a flag indicating if invalid values should be corrected.
2045  * 
2046  * Sets the policy as to whether values are corrected to the nearest step 
2047  * increment when a spin button is activated after providing an invalid value.
2048  **/
2049 void
2050 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
2051                                    gboolean       snap_to_ticks)
2052 {
2053   guint new_val;
2054
2055   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2056
2057   new_val = (snap_to_ticks != 0);
2058
2059   if (new_val != spin_button->snap_to_ticks)
2060     {
2061       spin_button->snap_to_ticks = new_val;
2062       if (new_val && GTK_ENTRY (spin_button)->editable)
2063         gtk_spin_button_update (spin_button);
2064       
2065       g_object_notify (G_OBJECT (spin_button), "snap_to_ticks");
2066     }
2067 }
2068
2069 /**
2070  * gtk_spin_button_get_snap_to_ticks:
2071  * @spin_button: a #GtkSpinButton
2072  *
2073  * Returns whether the values are corrected to the nearest step. See
2074  * gtk_spin_button_set_snap_to_ticks().
2075  *
2076  * Return value: %TRUE if values are snapped to the nearest step.
2077  **/
2078 gboolean
2079 gtk_spin_button_get_snap_to_ticks (GtkSpinButton *spin_button)
2080 {
2081   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), FALSE);
2082
2083   return spin_button->snap_to_ticks;
2084 }
2085
2086 /**
2087  * gtk_spin_button_spin:
2088  * @spin_button: a #GtkSpinButton 
2089  * @direction: a #GtkSpinType indicating the direction to spin.
2090  * @increment: step increment to apply in the specified direction.
2091  * 
2092  * Increment or decrement a spin button's value in a specified direction
2093  * by a specified amount. 
2094  **/
2095 void
2096 gtk_spin_button_spin (GtkSpinButton *spin_button,
2097                       GtkSpinType    direction,
2098                       gdouble        increment)
2099 {
2100   GtkAdjustment *adj;
2101   gdouble diff;
2102
2103   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2104   
2105   adj = spin_button->adjustment;
2106
2107   /* for compatibility with the 1.0.x version of this function */
2108   if (increment != 0 && increment != adj->step_increment &&
2109       (direction == GTK_SPIN_STEP_FORWARD ||
2110        direction == GTK_SPIN_STEP_BACKWARD))
2111     {
2112       if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
2113         increment = -increment;
2114       direction = GTK_SPIN_USER_DEFINED;
2115     }
2116
2117   switch (direction)
2118     {
2119     case GTK_SPIN_STEP_FORWARD:
2120
2121       gtk_spin_button_real_spin (spin_button, adj->step_increment);
2122       break;
2123
2124     case GTK_SPIN_STEP_BACKWARD:
2125
2126       gtk_spin_button_real_spin (spin_button, -adj->step_increment);
2127       break;
2128
2129     case GTK_SPIN_PAGE_FORWARD:
2130
2131       gtk_spin_button_real_spin (spin_button, adj->page_increment);
2132       break;
2133
2134     case GTK_SPIN_PAGE_BACKWARD:
2135
2136       gtk_spin_button_real_spin (spin_button, -adj->page_increment);
2137       break;
2138
2139     case GTK_SPIN_HOME:
2140
2141       diff = adj->value - adj->lower;
2142       if (diff > EPSILON)
2143         gtk_spin_button_real_spin (spin_button, -diff);
2144       break;
2145
2146     case GTK_SPIN_END:
2147
2148       diff = adj->upper - adj->value;
2149       if (diff > EPSILON)
2150         gtk_spin_button_real_spin (spin_button, diff);
2151       break;
2152
2153     case GTK_SPIN_USER_DEFINED:
2154
2155       if (increment != 0)
2156         gtk_spin_button_real_spin (spin_button, increment);
2157       break;
2158
2159     default:
2160       break;
2161     }
2162 }
2163
2164 /**
2165  * gtk_spin_button_update:
2166  * @spin_button: a #GtkSpinButton 
2167  * 
2168  * Manually force an update of the spin button.
2169  **/
2170 void 
2171 gtk_spin_button_update (GtkSpinButton *spin_button)
2172 {
2173   gdouble val;
2174   gint error = 0;
2175   gint return_val;
2176
2177   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
2178
2179   return_val = FALSE;
2180   g_signal_emit (spin_button, spinbutton_signals[INPUT], 0, &val, &return_val);
2181   if (return_val == FALSE)
2182     {
2183       return_val = gtk_spin_button_default_input (spin_button, &val);
2184       error = (return_val == GTK_INPUT_ERROR);
2185     }
2186   else if (return_val == GTK_INPUT_ERROR)
2187     error = 1;
2188
2189   spin_button_redraw (spin_button);
2190
2191   if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
2192     {
2193       if (val < spin_button->adjustment->lower)
2194         val = spin_button->adjustment->lower;
2195       else if (val > spin_button->adjustment->upper)
2196         val = spin_button->adjustment->upper;
2197     }
2198   else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) && 
2199            (error ||
2200            val < spin_button->adjustment->lower ||
2201            val > spin_button->adjustment->upper))
2202     {
2203       gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
2204       return;
2205     }
2206
2207   if (spin_button->snap_to_ticks)
2208     gtk_spin_button_snap (spin_button, val);
2209   else
2210     {
2211       if (fabs (val - spin_button->adjustment->value) > EPSILON)
2212         gtk_adjustment_set_value (spin_button->adjustment, val);
2213       else
2214         {
2215           return_val = FALSE;
2216           g_signal_emit (spin_button, spinbutton_signals[OUTPUT], 0,
2217                          &return_val);
2218           if (return_val == FALSE)
2219             gtk_spin_button_default_output (spin_button);
2220         }
2221     }
2222 }
2223
2224 static void
2225 spin_button_redraw (GtkSpinButton *spin_button)
2226 {
2227   GtkWidget *widget;
2228
2229   widget = GTK_WIDGET (spin_button);
2230
2231   if (GTK_WIDGET_DRAWABLE (widget))
2232     {
2233       gtk_widget_queue_draw (widget);
2234
2235       /* We must invalidate the panel window ourselves, because it
2236        * is not a child of widget->window
2237        */
2238       gdk_window_invalidate_rect (spin_button->panel, NULL, TRUE);
2239     }
2240 }