gtkspinbutton.c 51.6 KB
Newer Older
1
2
3
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
4
5
6
 * GtkSpinButton widget for GTK+
 * Copyright (C) 1998 Lars Hamann and Stefan Jeske
 *
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
10
11
12
13
14
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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
19
20
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
21
22
 */

23
/*
24
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
25
26
27
28
29
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

30
31
32
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
33
34
#include <string.h>
#include <locale.h>
35
36
37
38
#include "gdk/gdkkeysyms.h"
#include "gtkspinbutton.h"
#include "gtkmain.h"
#include "gtksignal.h"
39
#include "gtksettings.h"
40
41


42
43
44
45
46
47
#define MIN_SPIN_BUTTON_WIDTH              30
#define ARROW_SIZE                         11
#define SPIN_BUTTON_INITIAL_TIMER_DELAY    200
#define SPIN_BUTTON_TIMER_DELAY            20
#define MAX_TEXT_LENGTH                    256
#define MAX_TIMER_CALLS                    5
48
#define EPSILON                            1e-5
49

50
51
52
53
54
55
56
57
58
59
60
61
enum {
  ARG_0,
  ARG_ADJUSTMENT,
  ARG_CLIMB_RATE,
  ARG_DIGITS,
  ARG_SNAP_TO_TICKS,
  ARG_NUMERIC,
  ARG_WRAP,
  ARG_UPDATE_POLICY,
  ARG_VALUE
};

62
63
64
65
66
/* Signals */
enum
{
  INPUT,
  OUTPUT,
67
  VALUE_CHANGED,
68
69
  LAST_SIGNAL
};
70
71
72

static void gtk_spin_button_class_init     (GtkSpinButtonClass *klass);
static void gtk_spin_button_init           (GtkSpinButton      *spin_button);
73
static void gtk_spin_button_finalize       (GObject            *object);
74
75
76
77
78
79
static void gtk_spin_button_set_arg        (GtkObject          *object,
					    GtkArg             *arg,
					    guint               arg_id);
static void gtk_spin_button_get_arg        (GtkObject          *object,
					    GtkArg             *arg,
					    guint               arg_id);
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
static void gtk_spin_button_map            (GtkWidget          *widget);
static void gtk_spin_button_unmap          (GtkWidget          *widget);
static void gtk_spin_button_realize        (GtkWidget          *widget);
static void gtk_spin_button_unrealize      (GtkWidget          *widget);
static void gtk_spin_button_size_request   (GtkWidget          *widget,
					    GtkRequisition     *requisition);
static void gtk_spin_button_size_allocate  (GtkWidget          *widget,
					    GtkAllocation      *allocation);
static gint gtk_spin_button_expose         (GtkWidget          *widget,
					    GdkEventExpose     *event);
static gint gtk_spin_button_button_press   (GtkWidget          *widget,
					    GdkEventButton     *event);
static gint gtk_spin_button_button_release (GtkWidget          *widget,
					    GdkEventButton     *event);
static gint gtk_spin_button_motion_notify  (GtkWidget          *widget,
					    GdkEventMotion     *event);
static gint gtk_spin_button_enter_notify   (GtkWidget          *widget,
					    GdkEventCrossing   *event);
static gint gtk_spin_button_leave_notify   (GtkWidget          *widget,
					    GdkEventCrossing   *event);
static gint gtk_spin_button_focus_out      (GtkWidget          *widget,
					    GdkEventFocus      *event);
static void gtk_spin_button_draw_arrow     (GtkSpinButton      *spin_button, 
					    guint               arrow);
static gint gtk_spin_button_timer          (GtkSpinButton      *spin_button);
105
static void gtk_spin_button_value_changed  (GtkAdjustment      *adjustment,
106
107
108
					    GtkSpinButton      *spin_button); 
static gint gtk_spin_button_key_press      (GtkWidget          *widget,
					    GdkEventKey        *event);
109
110
static gint gtk_spin_button_key_release    (GtkWidget          *widget,
					    GdkEventKey        *event);
111
112
static gint gtk_spin_button_scroll         (GtkWidget          *widget,
					    GdkEventScroll     *event);
Owen Taylor's avatar
Owen Taylor committed
113
static void gtk_spin_button_activate       (GtkEntry           *entry);
114
static void gtk_spin_button_snap           (GtkSpinButton      *spin_button,
115
					    gdouble             val);
Owen Taylor's avatar
Owen Taylor committed
116
static void gtk_spin_button_insert_text    (GtkEntry           *entry,
117
					    const gchar        *new_text,
118
					    gint                new_text_length,
119
					    gint               *position);
120
static void gtk_spin_button_real_spin      (GtkSpinButton      *spin_button,
121
					    gdouble             step);
122
static gint gtk_spin_button_default_input  (GtkSpinButton      *spin_button,
123
					    gdouble            *new_val);
124
static gint gtk_spin_button_default_output (GtkSpinButton      *spin_button);
125
static gint spin_button_get_shadow_type    (GtkSpinButton      *spin_button);
126
127


128
static GtkEntryClass *parent_class = NULL;
129
static guint spinbutton_signals[LAST_SIGNAL] = {0};
130
131


132
GtkType
133
gtk_spin_button_get_type (void)
134
135
136
137
138
{
  static guint spin_button_type = 0;

  if (!spin_button_type)
    {
139
      static const GtkTypeInfo spin_button_info =
140
141
142
143
144
145
      {
	"GtkSpinButton",
	sizeof (GtkSpinButton),
	sizeof (GtkSpinButtonClass),
	(GtkClassInitFunc) gtk_spin_button_class_init,
	(GtkObjectInitFunc) gtk_spin_button_init,
146
147
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
148
        (GtkClassInitFunc) NULL,
149
150
      };

151
      spin_button_type = gtk_type_unique (GTK_TYPE_ENTRY, &spin_button_info);
152
153
154
155
156
157
158
    }
  return spin_button_type;
}

static void
gtk_spin_button_class_init (GtkSpinButtonClass *class)
{
159
  GObjectClass     *gobject_class = G_OBJECT_CLASS (class);
160
161
  GtkObjectClass   *object_class;
  GtkWidgetClass   *widget_class;
Owen Taylor's avatar
Owen Taylor committed
162
  GtkEntryClass    *entry_class;
163

164
165
  object_class   = (GtkObjectClass*)   class;
  widget_class   = (GtkWidgetClass*)   class;
Owen Taylor's avatar
Owen Taylor committed
166
  entry_class    = (GtkEntryClass*)    class; 
167

168
169
  parent_class = gtk_type_class (GTK_TYPE_ENTRY);

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  gobject_class->finalize = gtk_spin_button_finalize;

  object_class->set_arg = gtk_spin_button_set_arg;
  object_class->get_arg = gtk_spin_button_get_arg;

  widget_class->map = gtk_spin_button_map;
  widget_class->unmap = gtk_spin_button_unmap;
  widget_class->realize = gtk_spin_button_realize;
  widget_class->unrealize = gtk_spin_button_unrealize;
  widget_class->size_request = gtk_spin_button_size_request;
  widget_class->size_allocate = gtk_spin_button_size_allocate;
  widget_class->expose_event = gtk_spin_button_expose;
  widget_class->scroll_event = gtk_spin_button_scroll;
  widget_class->button_press_event = gtk_spin_button_button_press;
  widget_class->button_release_event = gtk_spin_button_button_release;
  widget_class->motion_notify_event = gtk_spin_button_motion_notify;
  widget_class->key_press_event = gtk_spin_button_key_press;
  widget_class->key_release_event = gtk_spin_button_key_release;
  widget_class->enter_notify_event = gtk_spin_button_enter_notify;
  widget_class->leave_notify_event = gtk_spin_button_leave_notify;
  widget_class->focus_out_event = gtk_spin_button_focus_out;

Owen Taylor's avatar
Owen Taylor committed
192
193
  entry_class->insert_text = gtk_spin_button_insert_text;
  entry_class->activate = gtk_spin_button_activate;
194
195
196
197

  class->input = NULL;
  class->output = NULL;

198
199
200
201
202
  gtk_object_add_arg_type ("GtkSpinButton::adjustment",
			   GTK_TYPE_ADJUSTMENT,
			   GTK_ARG_READWRITE,
			   ARG_ADJUSTMENT);
  gtk_object_add_arg_type ("GtkSpinButton::climb_rate",
203
			   GTK_TYPE_DOUBLE,
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
			   GTK_ARG_READWRITE,
			   ARG_CLIMB_RATE);
  gtk_object_add_arg_type ("GtkSpinButton::digits",
			   GTK_TYPE_UINT,
			   GTK_ARG_READWRITE,
			   ARG_DIGITS);
  gtk_object_add_arg_type ("GtkSpinButton::snap_to_ticks",
			   GTK_TYPE_BOOL,
			   GTK_ARG_READWRITE,
			   ARG_SNAP_TO_TICKS);
  gtk_object_add_arg_type ("GtkSpinButton::numeric",
			   GTK_TYPE_BOOL,
			   GTK_ARG_READWRITE,
			   ARG_NUMERIC);
  gtk_object_add_arg_type ("GtkSpinButton::wrap",
			   GTK_TYPE_BOOL,
			   GTK_ARG_READWRITE,
			   ARG_WRAP);
  gtk_object_add_arg_type ("GtkSpinButton::update_policy",
			   GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
			   GTK_ARG_READWRITE,
			   ARG_UPDATE_POLICY);
  gtk_object_add_arg_type ("GtkSpinButton::value",
227
			   GTK_TYPE_DOUBLE,
228
229
			   GTK_ARG_READWRITE,
			   ARG_VALUE);
230
231
232
233
  
  gtk_widget_class_install_style_property_parser (widget_class,
						  g_param_spec_enum ("shadow_type", "Shadow Type", NULL,
								     GTK_TYPE_SHADOW_TYPE,
234
								     GTK_SHADOW_IN,
235
236
								     G_PARAM_READABLE),
						  gtk_rc_property_parse_enum);
237
238
239
  spinbutton_signals[INPUT] =
    gtk_signal_new ("input",
		    GTK_RUN_LAST,
240
		    GTK_CLASS_TYPE (object_class),
241
242
243
244
245
		    GTK_SIGNAL_OFFSET (GtkSpinButtonClass, input),
		    gtk_marshal_INT__POINTER,
		    GTK_TYPE_INT, 1, GTK_TYPE_POINTER);

  spinbutton_signals[OUTPUT] =
246
247
248
249
250
251
252
    g_signal_newc ("output",
		   G_TYPE_FROM_CLASS(object_class),
		   G_SIGNAL_RUN_LAST,
		   G_STRUCT_OFFSET(GtkSpinButtonClass, output),
		   _gtk_boolean_handled_accumulator, NULL,
		   gtk_marshal_BOOLEAN__VOID,
		   G_TYPE_BOOLEAN, 0);
253
254
255
256
257
258
259
260

  spinbutton_signals[VALUE_CHANGED] =
    gtk_signal_new ("value_changed",
		    GTK_RUN_LAST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkSpinButtonClass, value_changed),
		    gtk_marshal_VOID__VOID,
		    GTK_TYPE_NONE, 0);
261
262
}

263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
static void
gtk_spin_button_set_arg (GtkObject        *object,
			 GtkArg           *arg,
			 guint             arg_id)
{
  GtkSpinButton *spin_button;

  spin_button = GTK_SPIN_BUTTON (object);
  
  switch (arg_id)
    {
      GtkAdjustment *adjustment;

    case ARG_ADJUSTMENT:
      adjustment = GTK_VALUE_POINTER (*arg);
      if (!adjustment)
	adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
      gtk_spin_button_set_adjustment (spin_button, adjustment);
      break;
    case ARG_CLIMB_RATE:
      gtk_spin_button_configure (spin_button,
				 spin_button->adjustment,
285
				 GTK_VALUE_DOUBLE (*arg),
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
				 spin_button->digits);
      break;
    case ARG_DIGITS:
      gtk_spin_button_configure (spin_button,
				 spin_button->adjustment,
				 spin_button->climb_rate,
				 GTK_VALUE_UINT (*arg));
      break;
    case ARG_SNAP_TO_TICKS:
      gtk_spin_button_set_snap_to_ticks (spin_button, GTK_VALUE_BOOL (*arg));
      break;
    case ARG_NUMERIC:
      gtk_spin_button_set_numeric (spin_button, GTK_VALUE_BOOL (*arg));
      break;
    case ARG_WRAP:
      gtk_spin_button_set_wrap (spin_button, GTK_VALUE_BOOL (*arg));
      break;
    case ARG_UPDATE_POLICY:
      gtk_spin_button_set_update_policy (spin_button, GTK_VALUE_ENUM (*arg));
      break;
    case ARG_VALUE:
307
      gtk_spin_button_set_value (spin_button, GTK_VALUE_DOUBLE (*arg));
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
      break;
    default:
      break;
    }
}

static void
gtk_spin_button_get_arg (GtkObject        *object,
			 GtkArg           *arg,
			 guint             arg_id)
{
  GtkSpinButton *spin_button;

  spin_button = GTK_SPIN_BUTTON (object);
  
  switch (arg_id)
    {
    case ARG_ADJUSTMENT:
      GTK_VALUE_POINTER (*arg) = spin_button->adjustment;
      break;
    case ARG_CLIMB_RATE:
329
      GTK_VALUE_DOUBLE (*arg) = spin_button->climb_rate;
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
      break;
    case ARG_DIGITS:
      GTK_VALUE_UINT (*arg) = spin_button->digits;
      break;
    case ARG_SNAP_TO_TICKS:
      GTK_VALUE_BOOL (*arg) = spin_button->snap_to_ticks;
      break;
    case ARG_NUMERIC:
      GTK_VALUE_BOOL (*arg) = spin_button->numeric;
      break;
    case ARG_WRAP:
      GTK_VALUE_BOOL (*arg) = spin_button->wrap;
      break;
    case ARG_UPDATE_POLICY:
      GTK_VALUE_ENUM (*arg) = spin_button->update_policy;
      break;
    case ARG_VALUE:
347
      GTK_VALUE_DOUBLE (*arg) = spin_button->adjustment->value;
348
349
350
351
352
353
354
      break;
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

355
356
357
358
static void
gtk_spin_button_init (GtkSpinButton *spin_button)
{
  spin_button->adjustment = NULL;
359
  spin_button->panel = NULL;
360
  spin_button->timer = 0;
361
  spin_button->ev_time = 0;
362
363
  spin_button->climb_rate = 0.0;
  spin_button->timer_step = 0.0;
364
  spin_button->update_policy = GTK_UPDATE_ALWAYS;
365
366
367
  spin_button->in_child = 2;
  spin_button->click_child = 2;
  spin_button->button = 0;
368
  spin_button->need_timer = FALSE;
369
370
  spin_button->timer_calls = 0;
  spin_button->digits = 0;
371
372
373
  spin_button->numeric = FALSE;
  spin_button->wrap = FALSE;
  spin_button->snap_to_ticks = FALSE;
374
  gtk_spin_button_set_adjustment (spin_button,
375
	  (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
376
377
378
}

static void
379
gtk_spin_button_finalize (GObject *object)
380
381
382
383
384
{
  g_return_if_fail (GTK_IS_SPIN_BUTTON (object));

  gtk_object_unref (GTK_OBJECT (GTK_SPIN_BUTTON (object)->adjustment));
  
385
  G_OBJECT_CLASS (parent_class)->finalize (object);
386
387
388
389
390
391
392
393
}

static void
gtk_spin_button_map (GtkWidget *widget)
{
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));

Tim Janik's avatar
Tim Janik committed
394
  if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
395
396
    {
      GTK_WIDGET_CLASS (parent_class)->map (widget);
397
      gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
    }
}

static void
gtk_spin_button_unmap (GtkWidget *widget)
{
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));

  if (GTK_WIDGET_MAPPED (widget))
    {
      gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
      GTK_WIDGET_CLASS (parent_class)->unmap (widget);
    }
}

static void
gtk_spin_button_realize (GtkWidget *widget)
{
417
  GtkSpinButton *spin_button;
418
419
  GdkWindowAttr attributes;
  gint attributes_mask;
420
  guint real_width;
421
  gint return_val;
422
423
424

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
425
  
426
  spin_button = GTK_SPIN_BUTTON (widget);
427

428
  real_width = widget->allocation.width;
429
  widget->allocation.width -= ARROW_SIZE + 2 * widget->style->xthickness;
430
431
  gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
			 GDK_KEY_RELEASE_MASK);
432
  GTK_WIDGET_CLASS (parent_class)->realize (widget);
433

434
435
  widget->allocation.width = real_width;
  
436
437
438
439
440
441
442
443
444
445
446
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = gtk_widget_get_events (widget);
  attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK 
    | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK 
    | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

447
  attributes.x = (widget->allocation.x + widget->allocation.width - ARROW_SIZE -
448
		  2 * widget->style->xthickness);
449
450
  attributes.y = widget->allocation.y + (widget->allocation.height -
					 widget->requisition.height) / 2;
451
  attributes.width = ARROW_SIZE + 2 * widget->style->xthickness;
452
  attributes.height = widget->requisition.height;
453
  
454
455
456
  spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget), 
				       &attributes, attributes_mask);
  gdk_window_set_user_data (spin_button->panel, widget);
457

458
459
460
461
462
463
464
  gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);

  return_val = FALSE;
  gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
		   &return_val);
  if (return_val == FALSE)
    gtk_spin_button_default_output (spin_button);
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
}

static void
gtk_spin_button_unrealize (GtkWidget *widget)
{
  GtkSpinButton *spin;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));

  spin = GTK_SPIN_BUTTON (widget);

  GTK_WIDGET_CLASS (parent_class)->unrealize (widget);

  if (spin->panel)
    {
Tim Janik's avatar
Tim Janik committed
481
      gdk_window_set_user_data (spin->panel, NULL);
482
483
484
485
486
487
488
489
490
      gdk_window_destroy (spin->panel);
      spin->panel = NULL;
    }
}

static void
gtk_spin_button_size_request (GtkWidget      *widget,
			      GtkRequisition *requisition)
{
491
  GtkEntry *entry;
492
  GtkSpinButton *spin_button;
493
  
494
495
496
497
  g_return_if_fail (widget != NULL);
  g_return_if_fail (requisition != NULL);
  g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));

498
  entry = GTK_ENTRY (widget);
499
  spin_button = GTK_SPIN_BUTTON (widget);
500
  
501
502
503
  GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);

  if (entry->width_chars < 0)
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
    {
      PangoFontMetrics metrics;
      PangoFont *font;
      gchar *lang;
      gchar buf[MAX_TEXT_LENGTH];
      gint width;
      gint w;


      font = pango_context_load_font (gtk_widget_get_pango_context (widget),
                                      widget->style->font_desc);
      lang = pango_context_get_lang (gtk_widget_get_pango_context (widget));
      pango_font_get_metrics (font, lang, &metrics);
      g_free (lang);
      g_object_unref (G_OBJECT (font));
      
      /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
      
      width = MIN_SPIN_BUTTON_WIDTH;
      
      sprintf (buf, "%0.*f", spin_button->digits, spin_button->adjustment->upper);
      w = strlen (buf) * PANGO_PIXELS (metrics.approximate_digit_width);
      width = MAX (width, w);
      sprintf (buf, "%0.*f", spin_button->digits, spin_button->adjustment->lower);
      w = strlen (buf) * PANGO_PIXELS (metrics.approximate_digit_width);
      width = MAX (width, w);
      
      requisition->width = width + ARROW_SIZE + 2 * widget->style->xthickness;
    }
533
534
  else
    requisition->width += ARROW_SIZE + 2 * widget->style->xthickness;
535
536
537
538
539
540
541
542
543
544
545
546
547
}

static void
gtk_spin_button_size_allocate (GtkWidget     *widget,
			       GtkAllocation *allocation)
{
  GtkAllocation child_allocation;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
  g_return_if_fail (allocation != NULL);

  child_allocation = *allocation;
548
549
  if (child_allocation.width > ARROW_SIZE + 2 * widget->style->xthickness)
    child_allocation.width -= ARROW_SIZE + 2 * widget->style->xthickness;
550

551
  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
552
    child_allocation.x += ARROW_SIZE + 2 * widget->style->xthickness;
553

554
555
556
  GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &child_allocation);

  widget->allocation = *allocation;
Tim Janik's avatar
Tim Janik committed
557
558
559

  if (GTK_WIDGET_REALIZED (widget))
    {
560
      child_allocation.width = ARROW_SIZE + 2 * widget->style->xthickness;
561
562
563
564
      child_allocation.height = widget->requisition.height;

      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
	child_allocation.x = (allocation->x + allocation->width - ARROW_SIZE - 
565
			      2 * widget->style->xthickness);
566
      else
567
568
	child_allocation.x = allocation->x;      

569
      child_allocation.y = allocation->y + (allocation->height - widget->requisition.height) / 2;
Tim Janik's avatar
Tim Janik committed
570
571
572
573
574
575
576

      gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel, 
			      child_allocation.x,
			      child_allocation.y,
			      child_allocation.width,
			      child_allocation.height); 
    }
577
578
}

579
580
581
static gint
gtk_spin_button_expose (GtkWidget      *widget,
			GdkEventExpose *event)
582
583
584
{
  GtkSpinButton *spin;

585
586
587
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
588
589
590
591
592

  spin = GTK_SPIN_BUTTON (widget);

  if (GTK_WIDGET_DRAWABLE (widget))
    {
593
594
      GtkShadowType shadow_type;

595
596
597
598
      /* FIXME this seems like really broken code -
       * why aren't we looking at event->window
       * and acting accordingly?
       */
599
600
601

      shadow_type = spin_button_get_shadow_type (spin);
      if (shadow_type != GTK_SHADOW_NONE)
602
	gtk_paint_box (widget->style, spin->panel,
603
		       GTK_STATE_NORMAL, shadow_type,
604
		       &event->area, widget, "spinbutton",
605
		       0, 0, 
606
		       ARROW_SIZE + 2 * widget->style->xthickness,
607
608
609
610
		       widget->requisition.height); 
      else
	 {
	    gdk_window_set_back_pixmap (spin->panel, NULL, TRUE);
611
612
613
	    gdk_window_clear_area (spin->panel,
                                   event->area.x, event->area.y,
                                   event->area.width, event->area.height);
614
615
616
617
	 }
       gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
       gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);

618
       GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
619
620
621
622
623
624
625
626
627
    }

  return FALSE;
}

static void
gtk_spin_button_draw_arrow (GtkSpinButton *spin_button, 
			    guint          arrow)
{
628
  GtkShadowType spin_shadow_type;
629
630
631
  GtkStateType state_type;
  GtkShadowType shadow_type;
  GtkWidget *widget;
632
633
  gint x;
  gint y;
634
635
636
637
638

  g_return_if_fail (spin_button != NULL);
  g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
  
  widget = GTK_WIDGET (spin_button);
639
  spin_shadow_type = spin_button_get_shadow_type (spin_button);
640
641
642

  if (GTK_WIDGET_DRAWABLE (spin_button))
    {
643
644
645
646
647
648
649
650
651
652
653
      if (!spin_button->wrap &&
	  (((arrow == GTK_ARROW_UP &&
	  (spin_button->adjustment->upper - spin_button->adjustment->value
	   <= EPSILON))) ||
	  ((arrow == GTK_ARROW_DOWN &&
	  (spin_button->adjustment->value - spin_button->adjustment->lower
	   <= EPSILON)))))
	{
	  shadow_type = GTK_SHADOW_ETCHED_IN;
	  state_type = GTK_STATE_NORMAL;
	}
654
      else
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
	{
	  if (spin_button->in_child == arrow)
	    {
	      if (spin_button->click_child == arrow)
		state_type = GTK_STATE_ACTIVE;
	      else
		state_type = GTK_STATE_PRELIGHT;
	    }
	  else
	    state_type = GTK_STATE_NORMAL;
	  
	  if (spin_button->click_child == arrow)
	    shadow_type = GTK_SHADOW_IN;
	  else
	    shadow_type = GTK_SHADOW_OUT;
	}
671
672
      if (arrow == GTK_ARROW_UP)
	{
673
	  if (spin_shadow_type != GTK_SHADOW_NONE)
674
	    {
675
676
	      x = widget->style->xthickness;
	      y = widget->style->ythickness;
677
678
679
	    }
	  else
	    {
680
681
	      x = widget->style->xthickness - 1;
	      y = widget->style->ythickness - 1;
682
	    }
683
684
685
686
687
	  gtk_paint_arrow (widget->style, spin_button->panel,
			   state_type, shadow_type, 
			   NULL, widget, "spinbutton",
			   arrow, TRUE, 
			   x, y, ARROW_SIZE, widget->requisition.height / 2 
688
			   - widget->style->ythickness);
689
690
691
	}
      else
	{
692
	  if (spin_shadow_type != GTK_SHADOW_NONE)
693
	    {
694
	      x = widget->style->xthickness;
695
696
697
698
	      y = widget->requisition.height / 2;
	    }
	  else
	    {
699
	      x = widget->style->xthickness - 1;
700
701
	      y = widget->requisition.height / 2 + 1;
	    }
702
703
704
705
706
	  gtk_paint_arrow (widget->style, spin_button->panel,
			   state_type, shadow_type, 
			   NULL, widget, "spinbutton",
			   arrow, TRUE, 
			   x, y, ARROW_SIZE, widget->requisition.height / 2 
707
			   - widget->style->ythickness);
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
	}
    }
}

static gint
gtk_spin_button_enter_notify (GtkWidget        *widget,
			      GdkEventCrossing *event)
{
  GtkSpinButton *spin;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  spin = GTK_SPIN_BUTTON (widget);

  if (event->window == spin->panel)
    {
      gint x;
      gint y;

      gdk_window_get_pointer (spin->panel, &x, &y, NULL);

      if (y <= widget->requisition.height / 2)
	{
	  spin->in_child = GTK_ARROW_UP;
	  if (spin->click_child == 2) 
	    gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
	}
      else
	{
	  spin->in_child = GTK_ARROW_DOWN;
	  if (spin->click_child == 2) 
	    gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
	}
    }
  return FALSE;
}

static gint
gtk_spin_button_leave_notify (GtkWidget        *widget,
			      GdkEventCrossing *event)
{
  GtkSpinButton *spin;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  spin = GTK_SPIN_BUTTON (widget);

  if (event->window == spin->panel && spin->click_child == 2)
    {
      if (spin->in_child == GTK_ARROW_UP) 
	{
	  spin->in_child = 2;
	  gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
	}
      else
	{
	  spin->in_child = 2;
	  gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
	}
    }
  return FALSE;
}

static gint
gtk_spin_button_focus_out (GtkWidget     *widget,
			   GdkEventFocus *event)
{
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

Owen Taylor's avatar
Owen Taylor committed
783
  if (GTK_ENTRY (widget)->editable)
784
    gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
785

786
  return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
787
788
}

789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
static gint
gtk_spin_button_scroll (GtkWidget      *widget,
			GdkEventScroll *event)
{
  GtkSpinButton *spin;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  spin = GTK_SPIN_BUTTON (widget);

  if (event->direction == GDK_SCROLL_UP)
    {
      if (!GTK_WIDGET_HAS_FOCUS (widget))
	gtk_widget_grab_focus (widget);
      gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
    }
  else if (event->direction == GDK_SCROLL_DOWN)
    {
      if (!GTK_WIDGET_HAS_FOCUS (widget))
	gtk_widget_grab_focus (widget);
      gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment); 
    }
  else
    return FALSE;

  return TRUE;
}

819
820
821
822
823
824
825
826
827
828
829
830
831
832
static gint
gtk_spin_button_button_press (GtkWidget      *widget,
			      GdkEventButton *event)
{
  GtkSpinButton *spin;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  spin = GTK_SPIN_BUTTON (widget);

  if (!spin->button)
    {
833
      if (event->window == spin->panel)
834
835
836
837
838
	{
	  if (!GTK_WIDGET_HAS_FOCUS (widget))
	    gtk_widget_grab_focus (widget);
	  gtk_grab_add (widget);
	  spin->button = event->button;
839
	  
Owen Taylor's avatar
Owen Taylor committed
840
	  if (GTK_ENTRY (widget)->editable)
841
	    gtk_spin_button_update (spin);
842
	  
843
844
845
846
847
	  if (event->y <= widget->requisition.height / 2)
	    {
	      spin->click_child = GTK_ARROW_UP;
	      if (event->button == 1)
		{
848
		 gtk_spin_button_real_spin (spin, 
849
					    spin->adjustment->step_increment);
850
851
852
853
854
855
856
857
858
859
860
		  if (!spin->timer)
		    {
		      spin->timer_step = spin->adjustment->step_increment;
		      spin->need_timer = TRUE;
		      spin->timer = gtk_timeout_add 
			(SPIN_BUTTON_INITIAL_TIMER_DELAY, 
			 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
		    }
		}
	      else if (event->button == 2)
		{
861
		 gtk_spin_button_real_spin (spin, 
862
					    spin->adjustment->page_increment);
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
		  if (!spin->timer) 
		    {
		      spin->timer_step = spin->adjustment->page_increment;
		      spin->need_timer = TRUE;
		      spin->timer = gtk_timeout_add 
			(SPIN_BUTTON_INITIAL_TIMER_DELAY, 
			 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
		    }
		}
	      gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
	    }
	  else 
	    {
	      spin->click_child = GTK_ARROW_DOWN;
	      if (event->button == 1)
		{
879
880
		  gtk_spin_button_real_spin (spin,
					     -spin->adjustment->step_increment);
881
882
883
884
885
886
887
888
889
890
891
		  if (!spin->timer)
		    {
		      spin->timer_step = spin->adjustment->step_increment;
		      spin->need_timer = TRUE;
		      spin->timer = gtk_timeout_add 
			(SPIN_BUTTON_INITIAL_TIMER_DELAY, 
			 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
		    }
		}      
	      else if (event->button == 2)
		{
892
893
		  gtk_spin_button_real_spin (spin,
					     -spin->adjustment->page_increment);
894
895
896
897
898
899
900
901
902
903
904
		  if (!spin->timer) 
		    {
		      spin->timer_step = spin->adjustment->page_increment;
		      spin->need_timer = TRUE;
		      spin->timer = gtk_timeout_add 
			(SPIN_BUTTON_INITIAL_TIMER_DELAY, 
			 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
		    }
		}
	      gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
	    }
905
	  return TRUE;
906
	}
907
      else
908
	return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
    }
  return FALSE;
}

static gint
gtk_spin_button_button_release (GtkWidget      *widget,
				GdkEventButton *event)
{
  GtkSpinButton *spin;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  spin = GTK_SPIN_BUTTON (widget);

  if (event->button == spin->button)
    {
      guint click_child;

      if (spin->timer)
	{
	  gtk_timeout_remove (spin->timer);
	  spin->timer = 0;
	  spin->timer_calls = 0;
	  spin->need_timer = FALSE;
	}

      if (event->button == 3)
	{
	  if (event->y >= 0 && event->x >= 0 && 
	      event->y <= widget->requisition.height &&
941
	      event->x <= ARROW_SIZE + 2 * widget->style->xthickness)
942
943
944
	    {
	      if (spin->click_child == GTK_ARROW_UP &&
		  event->y <= widget->requisition.height / 2)
945
		{
946
		  gdouble diff;
947
948
949
950
951

		  diff = spin->adjustment->upper - spin->adjustment->value;
		  if (diff > EPSILON)
		    gtk_spin_button_real_spin (spin, diff);
		}
952
953
	      else if (spin->click_child == GTK_ARROW_DOWN &&
		       event->y > widget->requisition.height / 2)
954
		{
955
		  gdouble diff;
956
957
958
959
960

		  diff = spin->adjustment->value - spin->adjustment->lower;
		  if (diff > EPSILON)
		    gtk_spin_button_real_spin (spin, -diff);
		}
961
962
963
964
965
966
967
	    }
	}		  
      gtk_grab_remove (widget);
      click_child = spin->click_child;
      spin->click_child = 2;
      spin->button = 0;
      gtk_spin_button_draw_arrow (spin, click_child);
968
      return TRUE;
969
970
    }
  else
971
    return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015

  return FALSE;
}

static gint
gtk_spin_button_motion_notify (GtkWidget      *widget,
			       GdkEventMotion *event)
{
  GtkSpinButton *spin;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  spin = GTK_SPIN_BUTTON (widget);
  
  if (spin->button)
    return FALSE;

  if (event->window == spin->panel)
    {
      gint y;

      y = event->y;
      if (event->is_hint)
	gdk_window_get_pointer (spin->panel, NULL, &y, NULL);

      if (y <= widget->requisition.height / 2 && 
	  spin->in_child == GTK_ARROW_DOWN)
	{
	  spin->in_child = GTK_ARROW_UP;
	  gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
	  gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
	}
      else if (y > widget->requisition.height / 2 && 
	  spin->in_child == GTK_ARROW_UP)
	{
	  spin->in_child = GTK_ARROW_DOWN;
	  gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
	  gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
	}
      return FALSE;
    }
	  
1016
  return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1017
1018
1019
1020
1021
}

static gint
gtk_spin_button_timer (GtkSpinButton *spin_button)
{
1022
1023
  gboolean retval = FALSE;
  
1024
  GDK_THREADS_ENTER ();
1025
1026
1027

  if (spin_button->timer)
    {
1028
1029
1030
1031
      if (spin_button->click_child == GTK_ARROW_UP)
	gtk_spin_button_real_spin (spin_button,	spin_button->timer_step);
      else
	gtk_spin_button_real_spin (spin_button,	-spin_button->timer_step);
1032
1033
1034
1035
1036
1037
1038
1039

      if (spin_button->need_timer)
	{
	  spin_button->need_timer = FALSE;
	  spin_button->timer = gtk_timeout_add 
	    (SPIN_BUTTON_TIMER_DELAY, (GtkFunction) gtk_spin_button_timer, 
	     (gpointer) spin_button);
	}
1040
      else 
1041
	{
1042
1043
	  if (spin_button->climb_rate > 0.0 && spin_button->timer_step 
	      < spin_button->adjustment->page_increment)
1044
	    {
1045
1046
1047
1048
1049
1050
1051
	      if (spin_button->timer_calls < MAX_TIMER_CALLS)
		spin_button->timer_calls++;
	      else 
		{
		  spin_button->timer_calls = 0;
		  spin_button->timer_step += spin_button->climb_rate;
		}
1052
	    }
1053
	  retval = TRUE;
1054
	}
1055
    }
1056

1057
  GDK_THREADS_LEAVE ();
1058
1059

  return retval;
1060
1061
1062
}

static void
1063
gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1064
1065
			       GtkSpinButton *spin_button)
{
1066
  gint return_val;
1067

1068
1069
  g_return_if_fail (adjustment != NULL);
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1070

1071
1072
1073
1074
1075
  return_val = FALSE;
  gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
		   &return_val);
  if (return_val == FALSE)
    gtk_spin_button_default_output (spin_button);
1076

1077
1078
1079
  gtk_signal_emit (GTK_OBJECT (spin_button), 
		   spinbutton_signals[VALUE_CHANGED]);

1080
1081
  gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_UP);
  gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_DOWN);
1082
}
1083

1084
1085
1086
1087
1088
static gint
gtk_spin_button_key_press (GtkWidget     *widget,
			   GdkEventKey   *event)
{
  GtkSpinButton *spin;
1089
  gint key;
1090
  gboolean key_repeat = FALSE;
1091

1092
1093
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1094
  g_return_val_if_fail (event != NULL, FALSE);
1095
1096
  
  spin = GTK_SPIN_BUTTON (widget);
1097
  key = event->keyval;
1098

1099
1100
  key_repeat = (event->time == spin->ev_time);

Owen Taylor's avatar
Owen Taylor committed
1101
  if (GTK_ENTRY (widget)->editable &&
1102
1103
      (key == GDK_Up || key == GDK_Down || 
       key == GDK_Page_Up || key == GDK_Page_Down))
1104
1105
1106
    gtk_spin_button_update (spin);

  switch (key)
1107
1108
    {
    case GDK_Up:
1109

1110
1111
1112
1113
      if (GTK_WIDGET_HAS_FOCUS (widget))
	{
	  gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), 
					"key_press_event");
1114
1115
1116
	  if (!key_repeat)
	    spin->timer_step = spin->adjustment->step_increment;

1117
	 gtk_spin_button_real_spin (spin, spin->timer_step);
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132

	  if (key_repeat)
	    {
	      if (spin->climb_rate > 0.0 && spin->timer_step
		  < spin->adjustment->page_increment)
		{
		  if (spin->timer_calls < MAX_TIMER_CALLS)
		    spin->timer_calls++;
		  else 
		    {
		      spin->timer_calls = 0;
		      spin->timer_step += spin->climb_rate;
		    }
		}
	    }
1133
1134
1135
	  return TRUE;
	}
      return FALSE;
1136

1137
    case GDK_Down:
1138

1139
1140
1141
1142
      if (GTK_WIDGET_HAS_FOCUS (widget))
	{
	  gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), 
					"key_press_event");
1143
1144
1145
	  if (!key_repeat)
	    spin->timer_step = spin->adjustment->step_increment;

1146
	 gtk_spin_button_real_spin (spin, -spin->timer_step);
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161

	  if (key_repeat)
	    {
	      if (spin->climb_rate > 0.0 && spin->timer_step
		  < spin->adjustment->page_increment)
		{
		  if (spin->timer_calls < MAX_TIMER_CALLS)
		    spin->timer_calls++;
		  else 
		    {
		      spin->timer_calls = 0;
		      spin->timer_step += spin->climb_rate;
		    }
		}
	    }
1162
1163
1164
	  return TRUE;
	}
      return FALSE;
1165

1166
    case GDK_Page_Up:
1167

1168
      if (event->state & GDK_CONTROL_MASK)
1169
	{
1170
	  gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1171
1172
1173
	  if (diff > EPSILON)
	    gtk_spin_button_real_spin (spin, diff);
	}
1174
      else
1175
	gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1176
      return TRUE;
1177

1178
    case GDK_Page_Down:
1179

1180
      if (event->state & GDK_CONTROL_MASK)
1181
	{
1182
	  gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1183
1184
1185
	  if (diff > EPSILON)
	    gtk_spin_button_real_spin (spin, -diff);
	}
1186
      else
1187
	gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1188
      return TRUE;
1189

1190
1191
1192
    default:
      break;
    }
1193

1194
1195
1196
  return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
}

1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
static gint
gtk_spin_button_key_release (GtkWidget   *widget,
			     GdkEventKey *event)
{
  GtkSpinButton *spin;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
  
  spin = GTK_SPIN_BUTTON (widget);
  
  spin->ev_time = event->time;
  return TRUE;
}

1212
1213
static void
gtk_spin_button_snap (GtkSpinButton *spin_button,
1214
		      gdouble        val)
1215
{
1216
1217
  gdouble inc;
  gdouble tmp;
1218
1219
1220
1221
1222
1223
1224
  
  inc = spin_button->adjustment->step_increment;
  tmp = (val - spin_button->adjustment->lower) / inc;
  if (tmp - floor (tmp) < ceil (tmp) - tmp)
    val = spin_button->adjustment->lower + floor (tmp) * inc;
  else
    val = spin_button->adjustment->lower + ceil (tmp) * inc;
1225
1226
1227
1228
1229

  if (fabs (val - spin_button->adjustment->value) > EPSILON)
    gtk_adjustment_set_value (spin_button->adjustment, val);
  else
    {
1230
1231
1232
1233
1234
      gint return_val = FALSE;
      gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
		       &return_val);
      if (return_val == FALSE)
	gtk_spin_button_default_output (spin_button);
1235
    }
1236
1237
1238
}

static void
Owen Taylor's avatar
Owen Taylor committed
1239
gtk_spin_button_activate (GtkEntry *entry)
1240
{
Owen Taylor's avatar
Owen Taylor committed
1241
1242
  if (entry->editable)
    gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1243
1244
}

1245
static void
Owen Taylor's avatar
Owen Taylor committed
1246
gtk_spin_button_insert_text (GtkEntry    *entry,
1247
1248
1249
1250
			     const gchar *new_text,
			     gint         new_text_length,
			     gint        *position)
{
Owen Taylor's avatar
Owen Taylor committed
1251
1252
  GtkEditable *editable = GTK_EDITABLE (entry);
  GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1253
1254
1255
1256
1257
1258
1259
 
  if (spin->numeric)
    {
      struct lconv *lc;
      gboolean sign;
      gint dotpos = -1;
      gint i;
1260
1261
      GdkWChar pos_sign;
      GdkWChar neg_sign;
1262
      gint entry_length;
1263
1264

      entry_length = entry->text_length;
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277

      lc = localeconv ();

      if (*(lc->negative_sign))
	neg_sign = *(lc->negative_sign);
      else 
	neg_sign = '-';

      if (*(lc->positive_sign))
	pos_sign = *(lc->positive_sign);
      else 
	pos_sign = '+';

1278
1279
1280
1281
1282
1283
1284
      for (sign=0, i=0; i<entry_length; i++)
	if ((entry->text[i] == neg_sign) ||
	    (entry->text[i] == pos_sign))
	  {
	    sign = 1;
	    break;
	  }
1285
1286
1287
1288

      if (sign && !(*position))
	return;

1289
1290
1291
1292
1293
1294
1295
      for (dotpos=-1, i=0; i<entry_length; i++)
	if (entry->text[i] == *(lc->decimal_point))
	  {
	    dotpos = i;
	    break;
	  }

1296
      if (dotpos > -1 && *position > dotpos &&
1297
	  (gint)spin->digits - entry_length
1298
	    + dotpos - new_text_length + 1 < 0)
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
	return;

      for (i = 0; i < new_text_length; i++)
	{
	  if (new_text[i] == neg_sign || new_text[i] == pos_sign)
	    {
	      if (sign || (*position) || i)
		return;
	      sign = TRUE;
	    }
	  else if (new_text[i] == *(lc->decimal_point))
	    {
	      if (!spin->digits || dotpos > -1 || 
1312
 		  (new_text_length - 1 - i + entry_length
1313
		    - *position > (gint)spin->digits)) 
1314
1315
1316
1317
1318
1319
1320
1321
		return;
	      dotpos = *position + i;
	    }
	  else if (new_text[i] < 0x30 || new_text[i] > 0x39)
	    return;
	}
    }

Owen Taylor's avatar
Owen Taylor committed
1322
1323
  GTK_ENTRY_CLASS (parent_class)->insert_text (entry, new_text,
					       new_text_length, position);
1324
1325