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