]> Pileus Git - ~andy/gtk/blob - gtk/gtkspinbutton.c
Patch from Bill Haneman (with many modifications) to make the focus color
[~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-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_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_set_property   (GObject         *object,
76                                             guint            prop_id,
77                                             const GValue    *value,
78                                             GParamSpec      *pspec);
79 static void gtk_spin_button_get_property   (GObject         *object,
80                                             guint            prop_id,
81                                             GValue          *value,
82                                             GParamSpec      *pspec);
83 static void gtk_spin_button_map            (GtkWidget          *widget);
84 static void gtk_spin_button_unmap          (GtkWidget          *widget);
85 static void gtk_spin_button_realize        (GtkWidget          *widget);
86 static void gtk_spin_button_unrealize      (GtkWidget          *widget);
87 static void gtk_spin_button_size_request   (GtkWidget          *widget,
88                                             GtkRequisition     *requisition);
89 static void gtk_spin_button_size_allocate  (GtkWidget          *widget,
90                                             GtkAllocation      *allocation);
91 static gint gtk_spin_button_expose         (GtkWidget          *widget,
92                                             GdkEventExpose     *event);
93 static gint gtk_spin_button_button_press   (GtkWidget          *widget,
94                                             GdkEventButton     *event);
95 static gint gtk_spin_button_button_release (GtkWidget          *widget,
96                                             GdkEventButton     *event);
97 static gint gtk_spin_button_motion_notify  (GtkWidget          *widget,
98                                             GdkEventMotion     *event);
99 static gint gtk_spin_button_enter_notify   (GtkWidget          *widget,
100                                             GdkEventCrossing   *event);
101 static gint gtk_spin_button_leave_notify   (GtkWidget          *widget,
102                                             GdkEventCrossing   *event);
103 static gint gtk_spin_button_focus_out      (GtkWidget          *widget,
104                                             GdkEventFocus      *event);
105 static void gtk_spin_button_draw_arrow     (GtkSpinButton      *spin_button, 
106                                             guint               arrow);
107 static gint gtk_spin_button_timer          (GtkSpinButton      *spin_button);
108 static void gtk_spin_button_value_changed  (GtkAdjustment      *adjustment,
109                                             GtkSpinButton      *spin_button); 
110 static gint gtk_spin_button_key_press      (GtkWidget          *widget,
111                                             GdkEventKey        *event);
112 static gint gtk_spin_button_key_release    (GtkWidget          *widget,
113                                             GdkEventKey        *event);
114 static gint gtk_spin_button_scroll         (GtkWidget          *widget,
115                                             GdkEventScroll     *event);
116 static void gtk_spin_button_activate       (GtkEntry           *entry);
117 static void gtk_spin_button_snap           (GtkSpinButton      *spin_button,
118                                             gdouble             val);
119 static void gtk_spin_button_insert_text    (GtkEditable        *editable,
120                                             const gchar        *new_text,
121                                             gint                new_text_length,
122                                             gint               *position);
123 static void gtk_spin_button_real_spin      (GtkSpinButton      *spin_button,
124                                             gdouble             step);
125 static gint gtk_spin_button_default_input  (GtkSpinButton      *spin_button,
126                                             gdouble            *new_val);
127 static gint gtk_spin_button_default_output (GtkSpinButton      *spin_button);
128 static gint spin_button_get_arrow_size     (GtkSpinButton      *spin_button);
129 static gint spin_button_get_shadow_type    (GtkSpinButton      *spin_button);
130
131
132 static GtkEntryClass *parent_class = NULL;
133 static guint spinbutton_signals[LAST_SIGNAL] = {0};
134
135
136 GtkType
137 gtk_spin_button_get_type (void)
138 {
139   static GtkType spin_button_type = 0;
140
141   if (!spin_button_type)
142     {
143       static const GtkTypeInfo spin_button_info =
144       {
145         "GtkSpinButton",
146         sizeof (GtkSpinButton),
147         sizeof (GtkSpinButtonClass),
148         (GtkClassInitFunc) gtk_spin_button_class_init,
149         (GtkObjectInitFunc) gtk_spin_button_init,
150         /* reserved_1 */ NULL,
151         /* reserved_2 */ NULL,
152         (GtkClassInitFunc) NULL,
153       };
154
155       static const GInterfaceInfo editable_info =
156       {
157         (GInterfaceInitFunc) gtk_spin_button_editable_init, /* interface_init */
158         NULL, /* interface_finalize */
159         NULL  /* interface_data */
160       };
161
162       spin_button_type = gtk_type_unique (GTK_TYPE_ENTRY, &spin_button_info);
163       g_type_add_interface_static (spin_button_type,
164                                    GTK_TYPE_EDITABLE,
165                                    &editable_info);
166     }
167   return spin_button_type;
168 }
169
170 static void
171 gtk_spin_button_class_init (GtkSpinButtonClass *class)
172 {
173   GObjectClass     *gobject_class = G_OBJECT_CLASS (class);
174   GtkObjectClass   *object_class;
175   GtkWidgetClass   *widget_class;
176   GtkEntryClass    *entry_class;
177
178   object_class   = (GtkObjectClass*)   class;
179   widget_class   = (GtkWidgetClass*)   class;
180   entry_class    = (GtkEntryClass*)    class;
181
182   parent_class = gtk_type_class (GTK_TYPE_ENTRY);
183
184   gobject_class->finalize = gtk_spin_button_finalize;
185
186   gobject_class->set_property = gtk_spin_button_set_property;
187   gobject_class->get_property = gtk_spin_button_get_property;
188
189   widget_class->map = gtk_spin_button_map;
190   widget_class->unmap = gtk_spin_button_unmap;
191   widget_class->realize = gtk_spin_button_realize;
192   widget_class->unrealize = gtk_spin_button_unrealize;
193   widget_class->size_request = gtk_spin_button_size_request;
194   widget_class->size_allocate = gtk_spin_button_size_allocate;
195   widget_class->expose_event = gtk_spin_button_expose;
196   widget_class->scroll_event = gtk_spin_button_scroll;
197   widget_class->button_press_event = gtk_spin_button_button_press;
198   widget_class->button_release_event = gtk_spin_button_button_release;
199   widget_class->motion_notify_event = gtk_spin_button_motion_notify;
200   widget_class->key_press_event = gtk_spin_button_key_press;
201   widget_class->key_release_event = gtk_spin_button_key_release;
202   widget_class->enter_notify_event = gtk_spin_button_enter_notify;
203   widget_class->leave_notify_event = gtk_spin_button_leave_notify;
204   widget_class->focus_out_event = gtk_spin_button_focus_out;
205
206   entry_class->activate = gtk_spin_button_activate;
207
208   class->input = NULL;
209   class->output = NULL;
210
211   g_object_class_install_property (gobject_class,
212                                    PROP_ADJUSTMENT,
213                                    g_param_spec_object ("adjustment",
214                                                         _("Adjustment"),
215                                                         _("The adjustment that holds the value of the spinbutton"),
216                                                         GTK_TYPE_ADJUSTMENT,
217                                                         G_PARAM_READWRITE));
218   
219   g_object_class_install_property (gobject_class,
220                                    PROP_CLIMB_RATE,
221                                    g_param_spec_double ("climb_rate",
222                                                         _("Climb Rate"),
223                                                         _("The acceleration rate when you hold down a button"),
224                                                         0.0,
225                                                         G_MAXDOUBLE,
226                                                         0.0,
227                                                         G_PARAM_READWRITE));  
228   
229   g_object_class_install_property (gobject_class,
230                                    PROP_DIGITS,
231                                    g_param_spec_uint ("digits",
232                                                       _("Digits"),
233                                                       _("The number of decimal places to display"),
234                                                       0,
235                                                       MAX_DIGITS,
236                                                       0,
237                                                       G_PARAM_READWRITE));
238   
239   g_object_class_install_property (gobject_class,
240                                    PROP_SNAP_TO_TICKS,
241                                    g_param_spec_boolean ("snap_to_ticks",
242                                                          _("Snap to Ticks"),
243                                                          _("Whether erroneous values are automatically changed to a spin button's nearest step increment"),
244                                                          FALSE,
245                                                          G_PARAM_READWRITE));
246   
247   g_object_class_install_property (gobject_class,
248                                    PROP_NUMERIC,
249                                    g_param_spec_boolean ("numeric",
250                                                          _("Numeric"),
251                                                          _("Whether non-numeric characters should be ignored"),
252                                                          FALSE,
253                                                          G_PARAM_READWRITE));
254   
255   g_object_class_install_property (gobject_class,
256                                    PROP_WRAP,
257                                    g_param_spec_boolean ("wrap",
258                                                          _("Wrap"),
259                                                          _("Whether a spin button should wrap upon reaching its limits"),
260                                                          FALSE,
261                                                          G_PARAM_READWRITE));
262   
263   g_object_class_install_property (gobject_class,
264                                    PROP_UPDATE_POLICY,
265                                    g_param_spec_enum ("update_policy",
266                                                       _("Update Policy"),
267                                                       _("Whether the spin button should update always, or only when the value is legal"),
268                                                       GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
269                                                       GTK_UPDATE_ALWAYS,
270                                                       G_PARAM_READWRITE));
271   
272   g_object_class_install_property (gobject_class,
273                                    PROP_VALUE,
274                                    g_param_spec_double ("value",
275                                                         _("Value"),
276                                                         _("Reads the current value, or sets a new value"),
277                                                         -G_MAXDOUBLE,
278                                                         G_MAXDOUBLE,
279                                                         0.0,
280                                                         G_PARAM_READWRITE));  
281   
282   gtk_widget_class_install_style_property_parser (widget_class,
283                                                   g_param_spec_enum ("shadow_type", "Shadow Type", NULL,
284                                                                      GTK_TYPE_SHADOW_TYPE,
285                                                                      GTK_SHADOW_IN,
286                                                                      G_PARAM_READABLE),
287                                                   gtk_rc_property_parse_enum);
288   spinbutton_signals[INPUT] =
289     gtk_signal_new ("input",
290                     GTK_RUN_LAST,
291                     GTK_CLASS_TYPE (object_class),
292                     GTK_SIGNAL_OFFSET (GtkSpinButtonClass, input),
293                     _gtk_marshal_INT__POINTER,
294                     GTK_TYPE_INT, 1, GTK_TYPE_POINTER);
295
296   spinbutton_signals[OUTPUT] =
297     g_signal_new ("output",
298                   G_TYPE_FROM_CLASS(object_class),
299                   G_SIGNAL_RUN_LAST,
300                   G_STRUCT_OFFSET(GtkSpinButtonClass, output),
301                   _gtk_boolean_handled_accumulator, NULL,
302                   _gtk_marshal_BOOLEAN__VOID,
303                   G_TYPE_BOOLEAN, 0);
304
305   spinbutton_signals[VALUE_CHANGED] =
306     gtk_signal_new ("value_changed",
307                     GTK_RUN_LAST,
308                     GTK_CLASS_TYPE (object_class),
309                     GTK_SIGNAL_OFFSET (GtkSpinButtonClass, value_changed),
310                     _gtk_marshal_VOID__VOID,
311                     GTK_TYPE_NONE, 0);
312 }
313
314 static void
315 gtk_spin_button_editable_init (GtkEditableClass *iface)
316 {
317   iface->insert_text = gtk_spin_button_insert_text;
318 }
319
320 static void
321 gtk_spin_button_set_property (GObject      *object,
322                               guint         prop_id,
323                               const GValue *value,
324                               GParamSpec   *pspec)
325 {
326   GtkSpinButton *spin_button;
327
328   spin_button = GTK_SPIN_BUTTON (object);
329   
330   switch (prop_id)
331     {
332       GtkAdjustment *adjustment;
333
334     case PROP_ADJUSTMENT:
335       adjustment = GTK_ADJUSTMENT (g_value_get_object (value));
336       if (!adjustment)
337         adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
338       gtk_spin_button_set_adjustment (spin_button, adjustment);
339       break;
340     case PROP_CLIMB_RATE:
341       gtk_spin_button_configure (spin_button,
342                                  spin_button->adjustment,
343                                  g_value_get_double (value),
344                                  spin_button->digits);
345       break;
346     case PROP_DIGITS:
347       gtk_spin_button_configure (spin_button,
348                                  spin_button->adjustment,
349                                  spin_button->climb_rate,
350                                  g_value_get_uint (value));
351       break;
352     case PROP_SNAP_TO_TICKS:
353       gtk_spin_button_set_snap_to_ticks (spin_button, g_value_get_boolean (value));
354       break;
355     case PROP_NUMERIC:
356       gtk_spin_button_set_numeric (spin_button, g_value_get_boolean (value));
357       break;
358     case PROP_WRAP:
359       gtk_spin_button_set_wrap (spin_button, g_value_get_boolean (value));
360       break;
361     case PROP_UPDATE_POLICY:
362       gtk_spin_button_set_update_policy (spin_button, g_value_get_enum (value));
363       break;
364     case PROP_VALUE:
365       gtk_spin_button_set_value (spin_button, g_value_get_double (value));
366       break;
367     default:
368       break;
369     }
370 }
371
372 static void
373 gtk_spin_button_get_property (GObject      *object,
374                               guint         prop_id,
375                               GValue       *value,
376                               GParamSpec   *pspec)
377 {
378   GtkSpinButton *spin_button;
379
380   spin_button = GTK_SPIN_BUTTON (object);
381   
382   switch (prop_id)
383     {
384     case PROP_ADJUSTMENT:
385       g_value_set_object (value, G_OBJECT (spin_button->adjustment));
386       break;
387     case PROP_CLIMB_RATE:
388       g_value_set_double (value, spin_button->climb_rate);
389       break;
390     case PROP_DIGITS:
391       g_value_set_uint (value, spin_button->digits);
392       break;
393     case PROP_SNAP_TO_TICKS:
394       g_value_set_boolean (value, spin_button->snap_to_ticks);
395       break;
396     case PROP_NUMERIC:
397       g_value_set_boolean (value, spin_button->numeric);
398       break;
399     case PROP_WRAP:
400       g_value_set_boolean (value, spin_button->wrap);
401       break;
402     case PROP_UPDATE_POLICY:
403       g_value_set_enum (value, spin_button->update_policy);
404       break;
405      case PROP_VALUE:
406        g_value_set_double (value, spin_button->adjustment->value);
407       break;
408     default:
409       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
410       break;
411     }
412 }
413
414 static void
415 gtk_spin_button_init (GtkSpinButton *spin_button)
416 {
417   spin_button->adjustment = NULL;
418   spin_button->panel = NULL;
419   spin_button->timer = 0;
420   spin_button->ev_time = 0;
421   spin_button->climb_rate = 0.0;
422   spin_button->timer_step = 0.0;
423   spin_button->update_policy = GTK_UPDATE_ALWAYS;
424   spin_button->in_child = 2;
425   spin_button->click_child = 2;
426   spin_button->button = 0;
427   spin_button->need_timer = FALSE;
428   spin_button->timer_calls = 0;
429   spin_button->digits = 0;
430   spin_button->numeric = FALSE;
431   spin_button->wrap = FALSE;
432   spin_button->snap_to_ticks = FALSE;
433   gtk_spin_button_set_adjustment (spin_button,
434           (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
435 }
436
437 static void
438 gtk_spin_button_finalize (GObject *object)
439 {
440   g_return_if_fail (GTK_IS_SPIN_BUTTON (object));
441
442   gtk_object_unref (GTK_OBJECT (GTK_SPIN_BUTTON (object)->adjustment));
443   
444   G_OBJECT_CLASS (parent_class)->finalize (object);
445 }
446
447 static void
448 gtk_spin_button_map (GtkWidget *widget)
449 {
450   g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
451
452   if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
453     {
454       GTK_WIDGET_CLASS (parent_class)->map (widget);
455       gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
456     }
457 }
458
459 static void
460 gtk_spin_button_unmap (GtkWidget *widget)
461 {
462   g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
463
464   if (GTK_WIDGET_MAPPED (widget))
465     {
466       gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
467       GTK_WIDGET_CLASS (parent_class)->unmap (widget);
468     }
469 }
470
471 static void
472 gtk_spin_button_realize (GtkWidget *widget)
473 {
474   GtkSpinButton *spin_button;
475   GdkWindowAttr attributes;
476   gint attributes_mask;
477   guint real_width;
478   gint return_val;
479   gint arrow_size;
480
481   g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
482   
483   spin_button = GTK_SPIN_BUTTON (widget);
484   arrow_size = spin_button_get_arrow_size (spin_button);
485
486   real_width = widget->allocation.width;
487   widget->allocation.width -= arrow_size + 2 * widget->style->xthickness;
488   gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
489                          GDK_KEY_RELEASE_MASK);
490   GTK_WIDGET_CLASS (parent_class)->realize (widget);
491
492   widget->allocation.width = real_width;
493   
494   attributes.window_type = GDK_WINDOW_CHILD;
495   attributes.wclass = GDK_INPUT_OUTPUT;
496   attributes.visual = gtk_widget_get_visual (widget);
497   attributes.colormap = gtk_widget_get_colormap (widget);
498   attributes.event_mask = gtk_widget_get_events (widget);
499   attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK 
500     | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK 
501     | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
502
503   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
504
505   attributes.x = (widget->allocation.x +
506                   widget->allocation.width - arrow_size -
507                   2 * widget->style->xthickness);
508   attributes.y = widget->allocation.y + (widget->allocation.height -
509                                          widget->requisition.height) / 2;
510   attributes.width = arrow_size + 2 * widget->style->xthickness;
511   attributes.height = widget->requisition.height;
512   
513   spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget), 
514                                        &attributes, attributes_mask);
515   gdk_window_set_user_data (spin_button->panel, widget);
516
517   gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
518
519   return_val = FALSE;
520   gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
521                    &return_val);
522   if (return_val == FALSE)
523     gtk_spin_button_default_output (spin_button);
524 }
525
526 static void
527 gtk_spin_button_unrealize (GtkWidget *widget)
528 {
529   GtkSpinButton *spin;
530
531   g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
532
533   spin = GTK_SPIN_BUTTON (widget);
534
535   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
536
537   if (spin->panel)
538     {
539       gdk_window_set_user_data (spin->panel, NULL);
540       gdk_window_destroy (spin->panel);
541       spin->panel = NULL;
542     }
543 }
544
545 static int
546 compute_double_length (double val, int digits)
547 {
548   int a;
549   int extra;
550
551   a = 1;
552   if (fabs (val) > 1.0)
553     a = floor (log10 (fabs (val))) + 1;  
554
555   extra = 0;
556   
557   /* The dot: */
558   if (digits > 0)
559     extra++;
560
561   /* The sign: */
562   if (val < 0)
563     extra++;
564
565   return a + digits + extra;
566 }
567
568 static void
569 gtk_spin_button_size_request (GtkWidget      *widget,
570                               GtkRequisition *requisition)
571 {
572   GtkEntry *entry;
573   GtkSpinButton *spin_button;
574   gint arrow_size;
575   
576   g_return_if_fail (requisition != NULL);
577   g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
578
579   entry = GTK_ENTRY (widget);
580   spin_button = GTK_SPIN_BUTTON (widget);
581   arrow_size = spin_button_get_arrow_size (spin_button);
582   
583   GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
584
585   if (entry->width_chars < 0)
586     {
587       PangoContext *context;
588       PangoFontMetrics *metrics;
589       gint width;
590       gint w;
591       gint string_len;
592       gint max_string_len;
593       gint digit_width;
594       gboolean interior_focus;
595       gint focus_width;
596
597       gtk_widget_style_get (widget,
598                             "interior-focus", &interior_focus,
599                             "focus-line-width", &focus_width,
600                             NULL);
601
602       context = gtk_widget_get_pango_context (widget);
603       metrics = pango_context_get_metrics (context,
604                                            widget->style->font_desc,
605                                            pango_context_get_language (context));
606
607       digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
608       digit_width = PANGO_PIXELS (digit_width);
609
610       pango_font_metrics_unref (metrics);
611       
612       /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
613       
614       width = MIN_SPIN_BUTTON_WIDTH;
615       max_string_len = MAX (10, compute_double_length (1e9 * spin_button->adjustment->step_increment,
616                                                        spin_button->digits));
617
618       string_len = compute_double_length (spin_button->adjustment->upper,
619                                           spin_button->digits);
620       w = MIN (string_len, max_string_len) * digit_width;
621       width = MAX (width, w);
622       string_len = compute_double_length (spin_button->adjustment->lower,
623                                           spin_button->digits);
624       w = MIN (string_len, max_string_len) * digit_width;
625       width = MAX (width, w);
626       
627       requisition->width = width + arrow_size + 2 * widget->style->xthickness;
628       if (interior_focus)
629         requisition->width += 2 * focus_width;
630     }
631   else
632     {
633       requisition->width += arrow_size + 2 * widget->style->xthickness;
634     }
635 }
636
637 static void
638 gtk_spin_button_size_allocate (GtkWidget     *widget,
639                                GtkAllocation *allocation)
640 {
641   GtkSpinButton *spin;
642   GtkAllocation child_allocation;
643   gint arrow_size;
644
645   g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
646   g_return_if_fail (allocation != NULL);
647
648   spin = GTK_SPIN_BUTTON (widget);
649   arrow_size = spin_button_get_arrow_size (spin);
650
651   child_allocation = *allocation;
652   if (child_allocation.width > arrow_size + 2 * widget->style->xthickness)
653     child_allocation.width -= arrow_size + 2 * widget->style->xthickness;
654
655   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
656     child_allocation.x += arrow_size + 2 * widget->style->xthickness;
657
658   GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &child_allocation);
659
660   widget->allocation = *allocation;
661
662   if (GTK_WIDGET_REALIZED (widget))
663     {
664       child_allocation.width = arrow_size + 2 * widget->style->xthickness;
665       child_allocation.height = widget->requisition.height;
666
667       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
668         child_allocation.x = (allocation->x + allocation->width -
669                               arrow_size - 2 * widget->style->xthickness);
670       else
671         child_allocation.x = allocation->x;      
672
673       child_allocation.y = allocation->y + (allocation->height - widget->requisition.height) / 2;
674
675       gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel, 
676                               child_allocation.x,
677                               child_allocation.y,
678                               child_allocation.width,
679                               child_allocation.height); 
680     }
681 }
682
683 static gint
684 gtk_spin_button_expose (GtkWidget      *widget,
685                         GdkEventExpose *event)
686 {
687   GtkSpinButton *spin;
688   gint arrow_size;
689
690   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
691   g_return_val_if_fail (event != NULL, FALSE);
692
693   spin = GTK_SPIN_BUTTON (widget);
694   arrow_size = spin_button_get_arrow_size (spin);
695
696   if (GTK_WIDGET_DRAWABLE (widget))
697     {
698       GtkShadowType shadow_type;
699
700       /* FIXME this seems like really broken code -
701        * why aren't we looking at event->window
702        * and acting accordingly?
703        */
704
705       shadow_type = spin_button_get_shadow_type (spin);
706       if (shadow_type != GTK_SHADOW_NONE)
707         gtk_paint_box (widget->style, spin->panel,
708                        GTK_STATE_NORMAL, shadow_type,
709                        &event->area, widget, "spinbutton",
710                        0, 0, 
711                        arrow_size + 2 * widget->style->xthickness,
712                        widget->requisition.height); 
713       else
714          {
715             gdk_window_set_back_pixmap (spin->panel, NULL, TRUE);
716             gdk_window_clear_area (spin->panel,
717                                    event->area.x, event->area.y,
718                                    event->area.width, event->area.height);
719          }
720        gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
721        gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
722
723        GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
724     }
725
726   return FALSE;
727 }
728
729 static void
730 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button, 
731                             guint          arrow)
732 {
733   GtkShadowType spin_shadow_type;
734   GtkStateType state_type;
735   GtkShadowType shadow_type;
736   GtkWidget *widget;
737   gint x;
738   gint y;
739   gint arrow_size;
740
741   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
742   
743   widget = GTK_WIDGET (spin_button);
744   spin_shadow_type = spin_button_get_shadow_type (spin_button);
745   arrow_size = spin_button_get_arrow_size (spin_button);
746
747   if (GTK_WIDGET_DRAWABLE (spin_button))
748     {
749       if (!spin_button->wrap &&
750           (((arrow == GTK_ARROW_UP &&
751           (spin_button->adjustment->upper - spin_button->adjustment->value
752            <= EPSILON))) ||
753           ((arrow == GTK_ARROW_DOWN &&
754           (spin_button->adjustment->value - spin_button->adjustment->lower
755            <= EPSILON)))))
756         {
757           shadow_type = GTK_SHADOW_ETCHED_IN;
758           state_type = GTK_STATE_NORMAL;
759         }
760       else
761         {
762           if (spin_button->in_child == arrow)
763             {
764               if (spin_button->click_child == arrow)
765                 state_type = GTK_STATE_ACTIVE;
766               else
767                 state_type = GTK_STATE_PRELIGHT;
768             }
769           else
770             state_type = GTK_STATE_NORMAL;
771           
772           if (spin_button->click_child == arrow)
773             shadow_type = GTK_SHADOW_IN;
774           else
775             shadow_type = GTK_SHADOW_OUT;
776         }
777       if (arrow == GTK_ARROW_UP)
778         {
779           if (spin_shadow_type != GTK_SHADOW_NONE)
780             {
781               x = widget->style->xthickness;
782               y = widget->style->ythickness;
783             }
784           else
785             {
786               x = widget->style->xthickness - 1;
787               y = widget->style->ythickness - 1;
788             }
789           gtk_paint_arrow (widget->style, spin_button->panel,
790                            state_type, shadow_type, 
791                            NULL, widget, "spinbutton",
792                            arrow, TRUE, 
793                            x, y, arrow_size,
794                            widget->requisition.height / 2 
795                            - widget->style->ythickness);
796         }
797       else
798         {
799           if (spin_shadow_type != GTK_SHADOW_NONE)
800             {
801               x = widget->style->xthickness;
802               y = widget->requisition.height / 2;
803             }
804           else
805             {
806               x = widget->style->xthickness - 1;
807               y = widget->requisition.height / 2 + 1;
808             }
809           gtk_paint_arrow (widget->style, spin_button->panel,
810                            state_type, shadow_type, 
811                            NULL, widget, "spinbutton",
812                            arrow, TRUE, 
813                            x, y, arrow_size,
814                            widget->requisition.height / 2 
815                            - widget->style->ythickness);
816         }
817     }
818 }
819
820 static gint
821 gtk_spin_button_enter_notify (GtkWidget        *widget,
822                               GdkEventCrossing *event)
823 {
824   GtkSpinButton *spin;
825
826   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
827   g_return_val_if_fail (event != NULL, FALSE);
828
829   spin = GTK_SPIN_BUTTON (widget);
830
831   if (event->window == spin->panel)
832     {
833       gint x;
834       gint y;
835
836       gdk_window_get_pointer (spin->panel, &x, &y, NULL);
837
838       if (y <= widget->requisition.height / 2)
839         {
840           spin->in_child = GTK_ARROW_UP;
841           if (spin->click_child == 2) 
842             gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
843         }
844       else
845         {
846           spin->in_child = GTK_ARROW_DOWN;
847           if (spin->click_child == 2) 
848             gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
849         }
850     }
851   return FALSE;
852 }
853
854 static gint
855 gtk_spin_button_leave_notify (GtkWidget        *widget,
856                               GdkEventCrossing *event)
857 {
858   GtkSpinButton *spin;
859
860   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
861   g_return_val_if_fail (event != NULL, FALSE);
862
863   spin = GTK_SPIN_BUTTON (widget);
864
865   if (event->window == spin->panel && spin->click_child == 2)
866     {
867       if (spin->in_child == GTK_ARROW_UP) 
868         {
869           spin->in_child = 2;
870           gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
871         }
872       else
873         {
874           spin->in_child = 2;
875           gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
876         }
877     }
878   return FALSE;
879 }
880
881 static gint
882 gtk_spin_button_focus_out (GtkWidget     *widget,
883                            GdkEventFocus *event)
884 {
885   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
886   g_return_val_if_fail (event != NULL, FALSE);
887
888   if (GTK_ENTRY (widget)->editable)
889     gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
890
891   return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
892 }
893
894 static gint
895 gtk_spin_button_scroll (GtkWidget      *widget,
896                         GdkEventScroll *event)
897 {
898   GtkSpinButton *spin;
899
900   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
901   g_return_val_if_fail (event != NULL, FALSE);
902
903   spin = GTK_SPIN_BUTTON (widget);
904
905   if (event->direction == GDK_SCROLL_UP)
906     {
907       if (!GTK_WIDGET_HAS_FOCUS (widget))
908         gtk_widget_grab_focus (widget);
909       gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
910     }
911   else if (event->direction == GDK_SCROLL_DOWN)
912     {
913       if (!GTK_WIDGET_HAS_FOCUS (widget))
914         gtk_widget_grab_focus (widget);
915       gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment); 
916     }
917   else
918     return FALSE;
919
920   return TRUE;
921 }
922
923 static gint
924 gtk_spin_button_button_press (GtkWidget      *widget,
925                               GdkEventButton *event)
926 {
927   GtkSpinButton *spin;
928
929   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
930   g_return_val_if_fail (event != NULL, FALSE);
931
932   spin = GTK_SPIN_BUTTON (widget);
933
934   if (!spin->button)
935     {
936       if (event->window == spin->panel)
937         {
938           if (!GTK_WIDGET_HAS_FOCUS (widget))
939             gtk_widget_grab_focus (widget);
940           gtk_grab_add (widget);
941           spin->button = event->button;
942           
943           if (GTK_ENTRY (widget)->editable)
944             gtk_spin_button_update (spin);
945           
946           if (event->y <= widget->requisition.height / 2)
947             {
948               spin->click_child = GTK_ARROW_UP;
949               if (event->button == 1)
950                 {
951                  gtk_spin_button_real_spin (spin, 
952                                             spin->adjustment->step_increment);
953                   if (!spin->timer)
954                     {
955                       spin->timer_step = spin->adjustment->step_increment;
956                       spin->need_timer = TRUE;
957                       spin->timer = gtk_timeout_add 
958                         (SPIN_BUTTON_INITIAL_TIMER_DELAY, 
959                          (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
960                     }
961                 }
962               else if (event->button == 2)
963                 {
964                  gtk_spin_button_real_spin (spin, 
965                                             spin->adjustment->page_increment);
966                   if (!spin->timer) 
967                     {
968                       spin->timer_step = spin->adjustment->page_increment;
969                       spin->need_timer = TRUE;
970                       spin->timer = gtk_timeout_add 
971                         (SPIN_BUTTON_INITIAL_TIMER_DELAY, 
972                          (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
973                     }
974                 }
975               gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
976             }
977           else 
978             {
979               spin->click_child = GTK_ARROW_DOWN;
980               if (event->button == 1)
981                 {
982                   gtk_spin_button_real_spin (spin,
983                                              -spin->adjustment->step_increment);
984                   if (!spin->timer)
985                     {
986                       spin->timer_step = spin->adjustment->step_increment;
987                       spin->need_timer = TRUE;
988                       spin->timer = gtk_timeout_add 
989                         (SPIN_BUTTON_INITIAL_TIMER_DELAY, 
990                          (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
991                     }
992                 }      
993               else if (event->button == 2)
994                 {
995                   gtk_spin_button_real_spin (spin,
996                                              -spin->adjustment->page_increment);
997                   if (!spin->timer) 
998                     {
999                       spin->timer_step = spin->adjustment->page_increment;
1000                       spin->need_timer = TRUE;
1001                       spin->timer = gtk_timeout_add 
1002                         (SPIN_BUTTON_INITIAL_TIMER_DELAY, 
1003                          (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
1004                     }
1005                 }
1006               gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1007             }
1008           return TRUE;
1009         }
1010       else
1011         return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1012     }
1013   return FALSE;
1014 }
1015
1016 static gint
1017 gtk_spin_button_button_release (GtkWidget      *widget,
1018                                 GdkEventButton *event)
1019 {
1020   GtkSpinButton *spin;
1021   gint arrow_size;
1022
1023   g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1024   g_return_val_if_fail (event != NULL, FALSE);
1025
1026   spin = GTK_SPIN_BUTTON (widget);
1027   arrow_size = spin_button_get_arrow_size (spin);
1028
1029   if (event->button == spin->button)
1030     {
1031       guint click_child;
1032
1033       if (spin->timer)
1034         {
1035           gtk_timeout_remove (spin->timer);
1036           spin->timer = 0;
1037           spin->timer_calls = 0;
1038           spin->need_timer = FALSE;
1039         }
1040
1041       if (event->button == 3)
1042         {
1043           if (event->y >= 0 && event->x >= 0 && 
1044               event->y <= widget->requisition.height &&
1045               event->x <= arrow_size + 2 * widget->style->xthickness)
1046             {
1047               if (spin->click_child == GTK_ARROW_UP &&
1048                   event->y <= widget->requisition.height / 2)
1049                 {
1050                   gdouble diff;
1051
1052                   diff = spin->adjustment->upper - spin->adjustment->value;
1053                   if (diff > EPSILON)
1054                     gtk_spin_button_real_spin (spin, diff);
1055                 }
1056               else if (spin->click_child == GTK_ARROW_DOWN &&
1057                        event->y > widget->requisition.height / 2)
1058                 {
1059                   gdouble diff;
1060
1061                   diff = spin->adjustment->value - spin->adjustment->lower;
1062                   if (diff > EPSILON)
1063                     gtk_spin_button_real_spin (spin, -diff);
1064                 }
1065             }
1066         }                 
1067       gtk_grab_remove (widget);
1068       click_child = spin->click_child;
1069       spin->click_child = 2;
1070       spin->button = 0;
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