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