1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GtkSpinButton widget for GTK+
5 * 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 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
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.
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.
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/.
35 #include "gdk/gdkkeysyms.h"
36 #include "gtkspinbutton.h"
38 #include "gtksignal.h"
39 #include "gtksettings.h"
42 #define MIN_SPIN_BUTTON_WIDTH 30
44 #define SPIN_BUTTON_INITIAL_TIMER_DELAY 200
45 #define SPIN_BUTTON_TIMER_DELAY 20
46 #define MAX_TEXT_LENGTH 256
47 #define MAX_TIMER_CALLS 5
71 static void gtk_spin_button_class_init (GtkSpinButtonClass *klass);
72 static void gtk_spin_button_init (GtkSpinButton *spin_button);
73 static void gtk_spin_button_finalize (GObject *object);
74 static void gtk_spin_button_set_arg (GtkObject *object,
77 static void gtk_spin_button_get_arg (GtkObject *object,
80 static void gtk_spin_button_map (GtkWidget *widget);
81 static void gtk_spin_button_unmap (GtkWidget *widget);
82 static void gtk_spin_button_realize (GtkWidget *widget);
83 static void gtk_spin_button_unrealize (GtkWidget *widget);
84 static void gtk_spin_button_size_request (GtkWidget *widget,
85 GtkRequisition *requisition);
86 static void gtk_spin_button_size_allocate (GtkWidget *widget,
87 GtkAllocation *allocation);
88 static gint gtk_spin_button_expose (GtkWidget *widget,
89 GdkEventExpose *event);
90 static gint gtk_spin_button_button_press (GtkWidget *widget,
91 GdkEventButton *event);
92 static gint gtk_spin_button_button_release (GtkWidget *widget,
93 GdkEventButton *event);
94 static gint gtk_spin_button_motion_notify (GtkWidget *widget,
95 GdkEventMotion *event);
96 static gint gtk_spin_button_enter_notify (GtkWidget *widget,
97 GdkEventCrossing *event);
98 static gint gtk_spin_button_leave_notify (GtkWidget *widget,
99 GdkEventCrossing *event);
100 static gint gtk_spin_button_focus_out (GtkWidget *widget,
101 GdkEventFocus *event);
102 static void gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
104 static gint gtk_spin_button_timer (GtkSpinButton *spin_button);
105 static void gtk_spin_button_value_changed (GtkAdjustment *adjustment,
106 GtkSpinButton *spin_button);
107 static gint gtk_spin_button_key_press (GtkWidget *widget,
109 static gint gtk_spin_button_key_release (GtkWidget *widget,
111 static gint gtk_spin_button_scroll (GtkWidget *widget,
112 GdkEventScroll *event);
113 static void gtk_spin_button_activate (GtkEntry *entry);
114 static void gtk_spin_button_snap (GtkSpinButton *spin_button,
116 static void gtk_spin_button_insert_text (GtkEntry *entry,
117 const gchar *new_text,
118 gint new_text_length,
120 static void gtk_spin_button_real_spin (GtkSpinButton *spin_button,
122 static gint gtk_spin_button_default_input (GtkSpinButton *spin_button,
124 static gint gtk_spin_button_default_output (GtkSpinButton *spin_button);
125 static gint spin_button_get_shadow_type (GtkSpinButton *spin_button);
128 static GtkEntryClass *parent_class = NULL;
129 static guint spinbutton_signals[LAST_SIGNAL] = {0};
133 gtk_spin_button_get_type (void)
135 static guint spin_button_type = 0;
137 if (!spin_button_type)
139 static const GtkTypeInfo spin_button_info =
142 sizeof (GtkSpinButton),
143 sizeof (GtkSpinButtonClass),
144 (GtkClassInitFunc) gtk_spin_button_class_init,
145 (GtkObjectInitFunc) gtk_spin_button_init,
146 /* reserved_1 */ NULL,
147 /* reserved_2 */ NULL,
148 (GtkClassInitFunc) NULL,
151 spin_button_type = gtk_type_unique (GTK_TYPE_ENTRY, &spin_button_info);
153 return spin_button_type;
157 gtk_spin_button_class_init (GtkSpinButtonClass *class)
159 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
160 GtkObjectClass *object_class;
161 GtkWidgetClass *widget_class;
162 GtkEntryClass *entry_class;
164 object_class = (GtkObjectClass*) class;
165 widget_class = (GtkWidgetClass*) class;
166 entry_class = (GtkEntryClass*) class;
168 parent_class = gtk_type_class (GTK_TYPE_ENTRY);
170 gobject_class->finalize = gtk_spin_button_finalize;
172 object_class->set_arg = gtk_spin_button_set_arg;
173 object_class->get_arg = gtk_spin_button_get_arg;
175 widget_class->map = gtk_spin_button_map;
176 widget_class->unmap = gtk_spin_button_unmap;
177 widget_class->realize = gtk_spin_button_realize;
178 widget_class->unrealize = gtk_spin_button_unrealize;
179 widget_class->size_request = gtk_spin_button_size_request;
180 widget_class->size_allocate = gtk_spin_button_size_allocate;
181 widget_class->expose_event = gtk_spin_button_expose;
182 widget_class->scroll_event = gtk_spin_button_scroll;
183 widget_class->button_press_event = gtk_spin_button_button_press;
184 widget_class->button_release_event = gtk_spin_button_button_release;
185 widget_class->motion_notify_event = gtk_spin_button_motion_notify;
186 widget_class->key_press_event = gtk_spin_button_key_press;
187 widget_class->key_release_event = gtk_spin_button_key_release;
188 widget_class->enter_notify_event = gtk_spin_button_enter_notify;
189 widget_class->leave_notify_event = gtk_spin_button_leave_notify;
190 widget_class->focus_out_event = gtk_spin_button_focus_out;
192 entry_class->insert_text = gtk_spin_button_insert_text;
193 entry_class->activate = gtk_spin_button_activate;
196 class->output = NULL;
198 gtk_object_add_arg_type ("GtkSpinButton::adjustment",
202 gtk_object_add_arg_type ("GtkSpinButton::climb_rate",
206 gtk_object_add_arg_type ("GtkSpinButton::digits",
210 gtk_object_add_arg_type ("GtkSpinButton::snap_to_ticks",
214 gtk_object_add_arg_type ("GtkSpinButton::numeric",
218 gtk_object_add_arg_type ("GtkSpinButton::wrap",
222 gtk_object_add_arg_type ("GtkSpinButton::update_policy",
223 GTK_TYPE_SPIN_BUTTON_UPDATE_POLICY,
226 gtk_object_add_arg_type ("GtkSpinButton::value",
231 gtk_widget_class_install_style_property_parser (widget_class,
232 g_param_spec_enum ("shadow_type", "Shadow Type", NULL,
233 GTK_TYPE_SHADOW_TYPE,
236 gtk_rc_property_parse_enum);
237 spinbutton_signals[INPUT] =
238 gtk_signal_new ("input",
240 GTK_CLASS_TYPE (object_class),
241 GTK_SIGNAL_OFFSET (GtkSpinButtonClass, input),
242 gtk_marshal_INT__POINTER,
243 GTK_TYPE_INT, 1, GTK_TYPE_POINTER);
245 spinbutton_signals[OUTPUT] =
246 g_signal_newc ("output",
247 G_TYPE_FROM_CLASS(object_class),
249 G_STRUCT_OFFSET(GtkSpinButtonClass, output),
250 _gtk_boolean_handled_accumulator, NULL,
251 gtk_marshal_BOOLEAN__VOID,
254 spinbutton_signals[VALUE_CHANGED] =
255 gtk_signal_new ("value_changed",
257 GTK_CLASS_TYPE (object_class),
258 GTK_SIGNAL_OFFSET (GtkSpinButtonClass, value_changed),
259 gtk_marshal_VOID__VOID,
264 gtk_spin_button_set_arg (GtkObject *object,
268 GtkSpinButton *spin_button;
270 spin_button = GTK_SPIN_BUTTON (object);
274 GtkAdjustment *adjustment;
277 adjustment = GTK_VALUE_POINTER (*arg);
279 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
280 gtk_spin_button_set_adjustment (spin_button, adjustment);
283 gtk_spin_button_configure (spin_button,
284 spin_button->adjustment,
285 GTK_VALUE_DOUBLE (*arg),
286 spin_button->digits);
289 gtk_spin_button_configure (spin_button,
290 spin_button->adjustment,
291 spin_button->climb_rate,
292 GTK_VALUE_UINT (*arg));
294 case ARG_SNAP_TO_TICKS:
295 gtk_spin_button_set_snap_to_ticks (spin_button, GTK_VALUE_BOOL (*arg));
298 gtk_spin_button_set_numeric (spin_button, GTK_VALUE_BOOL (*arg));
301 gtk_spin_button_set_wrap (spin_button, GTK_VALUE_BOOL (*arg));
303 case ARG_UPDATE_POLICY:
304 gtk_spin_button_set_update_policy (spin_button, GTK_VALUE_ENUM (*arg));
307 gtk_spin_button_set_value (spin_button, GTK_VALUE_DOUBLE (*arg));
315 gtk_spin_button_get_arg (GtkObject *object,
319 GtkSpinButton *spin_button;
321 spin_button = GTK_SPIN_BUTTON (object);
326 GTK_VALUE_POINTER (*arg) = spin_button->adjustment;
329 GTK_VALUE_DOUBLE (*arg) = spin_button->climb_rate;
332 GTK_VALUE_UINT (*arg) = spin_button->digits;
334 case ARG_SNAP_TO_TICKS:
335 GTK_VALUE_BOOL (*arg) = spin_button->snap_to_ticks;
338 GTK_VALUE_BOOL (*arg) = spin_button->numeric;
341 GTK_VALUE_BOOL (*arg) = spin_button->wrap;
343 case ARG_UPDATE_POLICY:
344 GTK_VALUE_ENUM (*arg) = spin_button->update_policy;
347 GTK_VALUE_DOUBLE (*arg) = spin_button->adjustment->value;
350 arg->type = GTK_TYPE_INVALID;
356 gtk_spin_button_init (GtkSpinButton *spin_button)
358 spin_button->adjustment = NULL;
359 spin_button->panel = NULL;
360 spin_button->timer = 0;
361 spin_button->ev_time = 0;
362 spin_button->climb_rate = 0.0;
363 spin_button->timer_step = 0.0;
364 spin_button->update_policy = GTK_UPDATE_ALWAYS;
365 spin_button->in_child = 2;
366 spin_button->click_child = 2;
367 spin_button->button = 0;
368 spin_button->need_timer = FALSE;
369 spin_button->timer_calls = 0;
370 spin_button->digits = 0;
371 spin_button->numeric = FALSE;
372 spin_button->wrap = FALSE;
373 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));
379 gtk_spin_button_finalize (GObject *object)
381 g_return_if_fail (GTK_IS_SPIN_BUTTON (object));
383 gtk_object_unref (GTK_OBJECT (GTK_SPIN_BUTTON (object)->adjustment));
385 G_OBJECT_CLASS (parent_class)->finalize (object);
389 gtk_spin_button_map (GtkWidget *widget)
391 g_return_if_fail (widget != NULL);
392 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
394 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
396 GTK_WIDGET_CLASS (parent_class)->map (widget);
397 gdk_window_show (GTK_SPIN_BUTTON (widget)->panel);
402 gtk_spin_button_unmap (GtkWidget *widget)
404 g_return_if_fail (widget != NULL);
405 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
407 if (GTK_WIDGET_MAPPED (widget))
409 gdk_window_hide (GTK_SPIN_BUTTON (widget)->panel);
410 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
415 gtk_spin_button_realize (GtkWidget *widget)
417 GtkSpinButton *spin_button;
418 GdkWindowAttr attributes;
419 gint attributes_mask;
423 g_return_if_fail (widget != NULL);
424 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
426 spin_button = GTK_SPIN_BUTTON (widget);
428 real_width = widget->allocation.width;
429 widget->allocation.width -= ARROW_SIZE + 2 * widget->style->xthickness;
430 gtk_widget_set_events (widget, gtk_widget_get_events (widget) |
431 GDK_KEY_RELEASE_MASK);
432 GTK_WIDGET_CLASS (parent_class)->realize (widget);
434 widget->allocation.width = real_width;
436 attributes.window_type = GDK_WINDOW_CHILD;
437 attributes.wclass = GDK_INPUT_OUTPUT;
438 attributes.visual = gtk_widget_get_visual (widget);
439 attributes.colormap = gtk_widget_get_colormap (widget);
440 attributes.event_mask = gtk_widget_get_events (widget);
441 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
442 | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
443 | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
445 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 attributes.y = widget->allocation.y + (widget->allocation.height -
450 widget->requisition.height) / 2;
451 attributes.width = ARROW_SIZE + 2 * widget->style->xthickness;
452 attributes.height = widget->requisition.height;
454 spin_button->panel = gdk_window_new (gtk_widget_get_parent_window (widget),
455 &attributes, attributes_mask);
456 gdk_window_set_user_data (spin_button->panel, widget);
458 gtk_style_set_background (widget->style, spin_button->panel, GTK_STATE_NORMAL);
461 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
463 if (return_val == FALSE)
464 gtk_spin_button_default_output (spin_button);
468 gtk_spin_button_unrealize (GtkWidget *widget)
472 g_return_if_fail (widget != NULL);
473 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
475 spin = GTK_SPIN_BUTTON (widget);
477 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
481 gdk_window_set_user_data (spin->panel, NULL);
482 gdk_window_destroy (spin->panel);
488 gtk_spin_button_size_request (GtkWidget *widget,
489 GtkRequisition *requisition)
492 GtkSpinButton *spin_button;
494 g_return_if_fail (widget != NULL);
495 g_return_if_fail (requisition != NULL);
496 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
498 entry = GTK_ENTRY (widget);
499 spin_button = GTK_SPIN_BUTTON (widget);
501 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
503 if (entry->width_chars < 0)
505 PangoFontMetrics metrics;
508 gchar buf[MAX_TEXT_LENGTH];
513 font = pango_context_load_font (gtk_widget_get_pango_context (widget),
514 widget->style->font_desc);
515 lang = pango_context_get_lang (gtk_widget_get_pango_context (widget));
516 pango_font_get_metrics (font, lang, &metrics);
518 g_object_unref (G_OBJECT (font));
520 /* Get max of MIN_SPIN_BUTTON_WIDTH, size of upper, size of lower */
522 width = MIN_SPIN_BUTTON_WIDTH;
524 sprintf (buf, "%0.*f", spin_button->digits, spin_button->adjustment->upper);
525 w = strlen (buf) * PANGO_PIXELS (metrics.approximate_digit_width);
526 width = MAX (width, w);
527 sprintf (buf, "%0.*f", spin_button->digits, spin_button->adjustment->lower);
528 w = strlen (buf) * PANGO_PIXELS (metrics.approximate_digit_width);
529 width = MAX (width, w);
531 requisition->width = width + ARROW_SIZE + 2 * widget->style->xthickness;
534 requisition->width += ARROW_SIZE + 2 * widget->style->xthickness;
538 gtk_spin_button_size_allocate (GtkWidget *widget,
539 GtkAllocation *allocation)
541 GtkAllocation child_allocation;
543 g_return_if_fail (widget != NULL);
544 g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));
545 g_return_if_fail (allocation != NULL);
547 child_allocation = *allocation;
548 if (child_allocation.width > ARROW_SIZE + 2 * widget->style->xthickness)
549 child_allocation.width -= ARROW_SIZE + 2 * widget->style->xthickness;
551 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
552 child_allocation.x += ARROW_SIZE + 2 * widget->style->xthickness;
554 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, &child_allocation);
556 widget->allocation = *allocation;
558 if (GTK_WIDGET_REALIZED (widget))
560 child_allocation.width = ARROW_SIZE + 2 * widget->style->xthickness;
561 child_allocation.height = widget->requisition.height;
563 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
564 child_allocation.x = (allocation->x + allocation->width - ARROW_SIZE -
565 2 * widget->style->xthickness);
567 child_allocation.x = allocation->x;
569 child_allocation.y = allocation->y + (allocation->height - widget->requisition.height) / 2;
571 gdk_window_move_resize (GTK_SPIN_BUTTON (widget)->panel,
574 child_allocation.width,
575 child_allocation.height);
580 gtk_spin_button_expose (GtkWidget *widget,
581 GdkEventExpose *event)
585 g_return_val_if_fail (widget != NULL, FALSE);
586 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
587 g_return_val_if_fail (event != NULL, FALSE);
589 spin = GTK_SPIN_BUTTON (widget);
591 if (GTK_WIDGET_DRAWABLE (widget))
593 GtkShadowType shadow_type;
595 /* FIXME this seems like really broken code -
596 * why aren't we looking at event->window
597 * and acting accordingly?
600 shadow_type = spin_button_get_shadow_type (spin);
601 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",
606 ARROW_SIZE + 2 * widget->style->xthickness,
607 widget->requisition.height);
610 gdk_window_set_back_pixmap (spin->panel, NULL, TRUE);
611 gdk_window_clear_area (spin->panel,
612 event->area.x, event->area.y,
613 event->area.width, event->area.height);
615 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
616 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
618 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
625 gtk_spin_button_draw_arrow (GtkSpinButton *spin_button,
628 GtkShadowType spin_shadow_type;
629 GtkStateType state_type;
630 GtkShadowType shadow_type;
635 g_return_if_fail (spin_button != NULL);
636 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
638 widget = GTK_WIDGET (spin_button);
639 spin_shadow_type = spin_button_get_shadow_type (spin_button);
641 if (GTK_WIDGET_DRAWABLE (spin_button))
643 if (!spin_button->wrap &&
644 (((arrow == GTK_ARROW_UP &&
645 (spin_button->adjustment->upper - spin_button->adjustment->value
647 ((arrow == GTK_ARROW_DOWN &&
648 (spin_button->adjustment->value - spin_button->adjustment->lower
651 shadow_type = GTK_SHADOW_ETCHED_IN;
652 state_type = GTK_STATE_NORMAL;
656 if (spin_button->in_child == arrow)
658 if (spin_button->click_child == arrow)
659 state_type = GTK_STATE_ACTIVE;
661 state_type = GTK_STATE_PRELIGHT;
664 state_type = GTK_STATE_NORMAL;
666 if (spin_button->click_child == arrow)
667 shadow_type = GTK_SHADOW_IN;
669 shadow_type = GTK_SHADOW_OUT;
671 if (arrow == GTK_ARROW_UP)
673 if (spin_shadow_type != GTK_SHADOW_NONE)
675 x = widget->style->xthickness;
676 y = widget->style->ythickness;
680 x = widget->style->xthickness - 1;
681 y = widget->style->ythickness - 1;
683 gtk_paint_arrow (widget->style, spin_button->panel,
684 state_type, shadow_type,
685 NULL, widget, "spinbutton",
687 x, y, ARROW_SIZE, widget->requisition.height / 2
688 - widget->style->ythickness);
692 if (spin_shadow_type != GTK_SHADOW_NONE)
694 x = widget->style->xthickness;
695 y = widget->requisition.height / 2;
699 x = widget->style->xthickness - 1;
700 y = widget->requisition.height / 2 + 1;
702 gtk_paint_arrow (widget->style, spin_button->panel,
703 state_type, shadow_type,
704 NULL, widget, "spinbutton",
706 x, y, ARROW_SIZE, widget->requisition.height / 2
707 - widget->style->ythickness);
713 gtk_spin_button_enter_notify (GtkWidget *widget,
714 GdkEventCrossing *event)
718 g_return_val_if_fail (widget != NULL, FALSE);
719 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
720 g_return_val_if_fail (event != NULL, FALSE);
722 spin = GTK_SPIN_BUTTON (widget);
724 if (event->window == spin->panel)
729 gdk_window_get_pointer (spin->panel, &x, &y, NULL);
731 if (y <= widget->requisition.height / 2)
733 spin->in_child = GTK_ARROW_UP;
734 if (spin->click_child == 2)
735 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
739 spin->in_child = GTK_ARROW_DOWN;
740 if (spin->click_child == 2)
741 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
748 gtk_spin_button_leave_notify (GtkWidget *widget,
749 GdkEventCrossing *event)
753 g_return_val_if_fail (widget != NULL, FALSE);
754 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
755 g_return_val_if_fail (event != NULL, FALSE);
757 spin = GTK_SPIN_BUTTON (widget);
759 if (event->window == spin->panel && spin->click_child == 2)
761 if (spin->in_child == GTK_ARROW_UP)
764 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
769 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
776 gtk_spin_button_focus_out (GtkWidget *widget,
777 GdkEventFocus *event)
779 g_return_val_if_fail (widget != NULL, FALSE);
780 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
781 g_return_val_if_fail (event != NULL, FALSE);
783 if (GTK_ENTRY (widget)->editable)
784 gtk_spin_button_update (GTK_SPIN_BUTTON (widget));
786 return GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
790 gtk_spin_button_scroll (GtkWidget *widget,
791 GdkEventScroll *event)
795 g_return_val_if_fail (widget != NULL, FALSE);
796 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
797 g_return_val_if_fail (event != NULL, FALSE);
799 spin = GTK_SPIN_BUTTON (widget);
801 if (event->direction == GDK_SCROLL_UP)
803 if (!GTK_WIDGET_HAS_FOCUS (widget))
804 gtk_widget_grab_focus (widget);
805 gtk_spin_button_real_spin (spin, spin->adjustment->step_increment);
807 else if (event->direction == GDK_SCROLL_DOWN)
809 if (!GTK_WIDGET_HAS_FOCUS (widget))
810 gtk_widget_grab_focus (widget);
811 gtk_spin_button_real_spin (spin, -spin->adjustment->step_increment);
820 gtk_spin_button_button_press (GtkWidget *widget,
821 GdkEventButton *event)
825 g_return_val_if_fail (widget != NULL, FALSE);
826 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
827 g_return_val_if_fail (event != NULL, FALSE);
829 spin = GTK_SPIN_BUTTON (widget);
833 if (event->window == spin->panel)
835 if (!GTK_WIDGET_HAS_FOCUS (widget))
836 gtk_widget_grab_focus (widget);
837 gtk_grab_add (widget);
838 spin->button = event->button;
840 if (GTK_ENTRY (widget)->editable)
841 gtk_spin_button_update (spin);
843 if (event->y <= widget->requisition.height / 2)
845 spin->click_child = GTK_ARROW_UP;
846 if (event->button == 1)
848 gtk_spin_button_real_spin (spin,
849 spin->adjustment->step_increment);
852 spin->timer_step = spin->adjustment->step_increment;
853 spin->need_timer = TRUE;
854 spin->timer = gtk_timeout_add
855 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
856 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
859 else if (event->button == 2)
861 gtk_spin_button_real_spin (spin,
862 spin->adjustment->page_increment);
865 spin->timer_step = spin->adjustment->page_increment;
866 spin->need_timer = TRUE;
867 spin->timer = gtk_timeout_add
868 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
869 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
872 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
876 spin->click_child = GTK_ARROW_DOWN;
877 if (event->button == 1)
879 gtk_spin_button_real_spin (spin,
880 -spin->adjustment->step_increment);
883 spin->timer_step = spin->adjustment->step_increment;
884 spin->need_timer = TRUE;
885 spin->timer = gtk_timeout_add
886 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
887 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
890 else if (event->button == 2)
892 gtk_spin_button_real_spin (spin,
893 -spin->adjustment->page_increment);
896 spin->timer_step = spin->adjustment->page_increment;
897 spin->need_timer = TRUE;
898 spin->timer = gtk_timeout_add
899 (SPIN_BUTTON_INITIAL_TIMER_DELAY,
900 (GtkFunction) gtk_spin_button_timer, (gpointer) spin);
903 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
908 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
914 gtk_spin_button_button_release (GtkWidget *widget,
915 GdkEventButton *event)
919 g_return_val_if_fail (widget != NULL, FALSE);
920 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
921 g_return_val_if_fail (event != NULL, FALSE);
923 spin = GTK_SPIN_BUTTON (widget);
925 if (event->button == spin->button)
931 gtk_timeout_remove (spin->timer);
933 spin->timer_calls = 0;
934 spin->need_timer = FALSE;
937 if (event->button == 3)
939 if (event->y >= 0 && event->x >= 0 &&
940 event->y <= widget->requisition.height &&
941 event->x <= ARROW_SIZE + 2 * widget->style->xthickness)
943 if (spin->click_child == GTK_ARROW_UP &&
944 event->y <= widget->requisition.height / 2)
948 diff = spin->adjustment->upper - spin->adjustment->value;
950 gtk_spin_button_real_spin (spin, diff);
952 else if (spin->click_child == GTK_ARROW_DOWN &&
953 event->y > widget->requisition.height / 2)
957 diff = spin->adjustment->value - spin->adjustment->lower;
959 gtk_spin_button_real_spin (spin, -diff);
963 gtk_grab_remove (widget);
964 click_child = spin->click_child;
965 spin->click_child = 2;
967 gtk_spin_button_draw_arrow (spin, click_child);
971 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
977 gtk_spin_button_motion_notify (GtkWidget *widget,
978 GdkEventMotion *event)
982 g_return_val_if_fail (widget != NULL, FALSE);
983 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
984 g_return_val_if_fail (event != NULL, FALSE);
986 spin = GTK_SPIN_BUTTON (widget);
991 if (event->window == spin->panel)
997 gdk_window_get_pointer (spin->panel, NULL, &y, NULL);
999 if (y <= widget->requisition.height / 2 &&
1000 spin->in_child == GTK_ARROW_DOWN)
1002 spin->in_child = GTK_ARROW_UP;
1003 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1004 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1006 else if (y > widget->requisition.height / 2 &&
1007 spin->in_child == GTK_ARROW_UP)
1009 spin->in_child = GTK_ARROW_DOWN;
1010 gtk_spin_button_draw_arrow (spin, GTK_ARROW_UP);
1011 gtk_spin_button_draw_arrow (spin, GTK_ARROW_DOWN);
1016 return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
1020 gtk_spin_button_timer (GtkSpinButton *spin_button)
1022 gboolean retval = FALSE;
1024 GDK_THREADS_ENTER ();
1026 if (spin_button->timer)
1028 if (spin_button->click_child == GTK_ARROW_UP)
1029 gtk_spin_button_real_spin (spin_button, spin_button->timer_step);
1031 gtk_spin_button_real_spin (spin_button, -spin_button->timer_step);
1033 if (spin_button->need_timer)
1035 spin_button->need_timer = FALSE;
1036 spin_button->timer = gtk_timeout_add
1037 (SPIN_BUTTON_TIMER_DELAY, (GtkFunction) gtk_spin_button_timer,
1038 (gpointer) spin_button);
1042 if (spin_button->climb_rate > 0.0 && spin_button->timer_step
1043 < spin_button->adjustment->page_increment)
1045 if (spin_button->timer_calls < MAX_TIMER_CALLS)
1046 spin_button->timer_calls++;
1049 spin_button->timer_calls = 0;
1050 spin_button->timer_step += spin_button->climb_rate;
1057 GDK_THREADS_LEAVE ();
1063 gtk_spin_button_value_changed (GtkAdjustment *adjustment,
1064 GtkSpinButton *spin_button)
1068 g_return_if_fail (adjustment != NULL);
1069 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1072 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1074 if (return_val == FALSE)
1075 gtk_spin_button_default_output (spin_button);
1077 gtk_signal_emit (GTK_OBJECT (spin_button),
1078 spinbutton_signals[VALUE_CHANGED]);
1080 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_UP);
1081 gtk_spin_button_draw_arrow (spin_button, GTK_ARROW_DOWN);
1085 gtk_spin_button_key_press (GtkWidget *widget,
1088 GtkSpinButton *spin;
1090 gboolean key_repeat = FALSE;
1092 g_return_val_if_fail (widget != NULL, FALSE);
1093 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1094 g_return_val_if_fail (event != NULL, FALSE);
1096 spin = GTK_SPIN_BUTTON (widget);
1097 key = event->keyval;
1099 key_repeat = (event->time == spin->ev_time);
1101 if (GTK_ENTRY (widget)->editable &&
1102 (key == GDK_Up || key == GDK_Down ||
1103 key == GDK_Page_Up || key == GDK_Page_Down))
1104 gtk_spin_button_update (spin);
1110 if (GTK_WIDGET_HAS_FOCUS (widget))
1112 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1115 spin->timer_step = spin->adjustment->step_increment;
1117 gtk_spin_button_real_spin (spin, spin->timer_step);
1121 if (spin->climb_rate > 0.0 && spin->timer_step
1122 < spin->adjustment->page_increment)
1124 if (spin->timer_calls < MAX_TIMER_CALLS)
1125 spin->timer_calls++;
1128 spin->timer_calls = 0;
1129 spin->timer_step += spin->climb_rate;
1139 if (GTK_WIDGET_HAS_FOCUS (widget))
1141 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget),
1144 spin->timer_step = spin->adjustment->step_increment;
1146 gtk_spin_button_real_spin (spin, -spin->timer_step);
1150 if (spin->climb_rate > 0.0 && spin->timer_step
1151 < spin->adjustment->page_increment)
1153 if (spin->timer_calls < MAX_TIMER_CALLS)
1154 spin->timer_calls++;
1157 spin->timer_calls = 0;
1158 spin->timer_step += spin->climb_rate;
1168 if (event->state & GDK_CONTROL_MASK)
1170 gdouble diff = spin->adjustment->upper - spin->adjustment->value;
1172 gtk_spin_button_real_spin (spin, diff);
1175 gtk_spin_button_real_spin (spin, spin->adjustment->page_increment);
1180 if (event->state & GDK_CONTROL_MASK)
1182 gdouble diff = spin->adjustment->value - spin->adjustment->lower;
1184 gtk_spin_button_real_spin (spin, -diff);
1187 gtk_spin_button_real_spin (spin, -spin->adjustment->page_increment);
1194 return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
1198 gtk_spin_button_key_release (GtkWidget *widget,
1201 GtkSpinButton *spin;
1203 g_return_val_if_fail (widget != NULL, FALSE);
1204 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), FALSE);
1206 spin = GTK_SPIN_BUTTON (widget);
1208 spin->ev_time = event->time;
1213 gtk_spin_button_snap (GtkSpinButton *spin_button,
1219 inc = spin_button->adjustment->step_increment;
1220 tmp = (val - spin_button->adjustment->lower) / inc;
1221 if (tmp - floor (tmp) < ceil (tmp) - tmp)
1222 val = spin_button->adjustment->lower + floor (tmp) * inc;
1224 val = spin_button->adjustment->lower + ceil (tmp) * inc;
1226 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1227 gtk_adjustment_set_value (spin_button->adjustment, val);
1230 gint return_val = FALSE;
1231 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1233 if (return_val == FALSE)
1234 gtk_spin_button_default_output (spin_button);
1239 gtk_spin_button_activate (GtkEntry *entry)
1241 if (entry->editable)
1242 gtk_spin_button_update (GTK_SPIN_BUTTON (entry));
1246 gtk_spin_button_insert_text (GtkEntry *entry,
1247 const gchar *new_text,
1248 gint new_text_length,
1251 GtkEditable *editable = GTK_EDITABLE (entry);
1252 GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
1264 entry_length = entry->text_length;
1268 if (*(lc->negative_sign))
1269 neg_sign = *(lc->negative_sign);
1273 if (*(lc->positive_sign))
1274 pos_sign = *(lc->positive_sign);
1278 for (sign=0, i=0; i<entry_length; i++)
1279 if ((entry->text[i] == neg_sign) ||
1280 (entry->text[i] == pos_sign))
1286 if (sign && !(*position))
1289 for (dotpos=-1, i=0; i<entry_length; i++)
1290 if (entry->text[i] == *(lc->decimal_point))
1296 if (dotpos > -1 && *position > dotpos &&
1297 (gint)spin->digits - entry_length
1298 + dotpos - new_text_length + 1 < 0)
1301 for (i = 0; i < new_text_length; i++)
1303 if (new_text[i] == neg_sign || new_text[i] == pos_sign)
1305 if (sign || (*position) || i)
1309 else if (new_text[i] == *(lc->decimal_point))
1311 if (!spin->digits || dotpos > -1 ||
1312 (new_text_length - 1 - i + entry_length
1313 - *position > (gint)spin->digits))
1315 dotpos = *position + i;
1317 else if (new_text[i] < 0x30 || new_text[i] > 0x39)
1322 GTK_ENTRY_CLASS (parent_class)->insert_text (entry, new_text,
1323 new_text_length, position);
1327 gtk_spin_button_real_spin (GtkSpinButton *spin_button,
1331 gdouble new_value = 0.0;
1333 g_return_if_fail (spin_button != NULL);
1334 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1336 adj = spin_button->adjustment;
1338 new_value = adj->value + increment;
1342 if (spin_button->wrap)
1344 if (fabs (adj->value - adj->upper) < EPSILON)
1345 new_value = adj->lower;
1346 else if (new_value > adj->upper)
1347 new_value = adj->upper;
1350 new_value = MIN (new_value, adj->upper);
1352 else if (increment < 0)
1354 if (spin_button->wrap)
1356 if (fabs (adj->value - adj->lower) < EPSILON)
1357 new_value = adj->upper;
1358 else if (new_value < adj->lower)
1359 new_value = adj->lower;
1362 new_value = MAX (new_value, adj->lower);
1365 if (fabs (new_value - adj->value) > EPSILON)
1366 gtk_adjustment_set_value (adj, new_value);
1370 gtk_spin_button_default_input (GtkSpinButton *spin_button,
1375 *new_val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &err);
1377 return GTK_INPUT_ERROR;
1383 gtk_spin_button_default_output (GtkSpinButton *spin_button)
1385 gchar buf[MAX_TEXT_LENGTH];
1387 sprintf (buf, "%0.*f", spin_button->digits, spin_button->adjustment->value);
1388 if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
1389 gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
1394 /***********************************************************
1395 ***********************************************************
1396 *** Public interface ***
1397 ***********************************************************
1398 ***********************************************************/
1402 gtk_spin_button_configure (GtkSpinButton *spin_button,
1403 GtkAdjustment *adjustment,
1407 g_return_if_fail (spin_button != NULL);
1408 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1409 g_return_if_fail (digits < 6);
1412 gtk_spin_button_set_adjustment (spin_button, adjustment);
1414 adjustment = spin_button->adjustment;
1416 spin_button->digits = digits;
1417 spin_button->climb_rate = climb_rate;
1418 gtk_adjustment_value_changed (adjustment);
1422 gtk_spin_button_new (GtkAdjustment *adjustment,
1426 GtkSpinButton *spin;
1429 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
1430 g_return_val_if_fail (digits < 6, NULL);
1432 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1434 gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
1436 return GTK_WIDGET (spin);
1440 * gtk_spin_button_new_with_range:
1441 * @min: Minimum allowable value
1442 * @max: Maximum allowable value
1443 * @step: Increment added or subtracted by spinning the widget
1445 * This is a convenience constructor that allows creation of a numeric
1446 * #GtkSpinButton without manually creating an adjustment. The value is
1447 * initially set to the minimum value and a page increment of 10 * @step
1448 * is the default. The precision of the spin button is equivalent to the
1449 * precision of @step.
1451 * Return value: the newly instantiated spin button
1454 gtk_spin_button_new_with_range (gdouble min,
1459 GtkSpinButton *spin;
1462 g_return_val_if_fail (min < max, NULL);
1463 g_return_val_if_fail (step != 0.0, NULL);
1465 spin = gtk_type_new (GTK_TYPE_SPIN_BUTTON);
1467 adj = gtk_adjustment_new (min, min, max, step, 10 * step, step);
1469 if (fabs (step) >= 1.0)
1472 digits = abs ((gint) floor (log10 (fabs (step))));
1477 gtk_spin_button_configure (spin, GTK_ADJUSTMENT (adj), step, digits);
1479 gtk_spin_button_set_numeric (spin, TRUE);
1481 return GTK_WIDGET (spin);
1484 /* Callback used when the spin button's adjustment changes. We need to redraw
1485 * the arrows when the adjustment's range changes, and reevaluate our size request.
1488 adjustment_changed_cb (GtkAdjustment *adjustment, gpointer data)
1490 GtkSpinButton *spin_button;
1492 spin_button = GTK_SPIN_BUTTON (data);
1494 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1498 * gtk_spin_button_set_adjustment:
1499 * @spin_button: a #GtkSpinButton
1500 * @adjustment: a #GtkAdjustment to replace the existing adjustment
1502 * Replaces the #GtkAdjustment associated with @spin_button.
1505 gtk_spin_button_set_adjustment (GtkSpinButton *spin_button,
1506 GtkAdjustment *adjustment)
1508 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1510 if (spin_button->adjustment != adjustment)
1512 if (spin_button->adjustment)
1514 gtk_signal_disconnect_by_data (GTK_OBJECT (spin_button->adjustment),
1515 (gpointer) spin_button);
1516 gtk_object_unref (GTK_OBJECT (spin_button->adjustment));
1518 spin_button->adjustment = adjustment;
1521 gtk_object_ref (GTK_OBJECT (adjustment));
1522 gtk_object_sink (GTK_OBJECT (adjustment));
1523 gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
1524 (GtkSignalFunc) gtk_spin_button_value_changed,
1525 (gpointer) spin_button);
1526 gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
1527 (GtkSignalFunc) adjustment_changed_cb,
1528 (gpointer) spin_button);
1531 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1536 * gtk_spin_button_get_adjustment:
1539 * Get the adjustment associated with a #GtkSpinButton
1541 * Return value: the #GtkAdjustment of @spin_button
1544 gtk_spin_button_get_adjustment (GtkSpinButton *spin_button)
1546 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
1548 return spin_button->adjustment;
1552 * gtk_spin_button_set_digits:
1553 * @spin_button: a #GtkSpinButton
1554 * @digits: the number of digits to be displayed for the spin button's value
1556 * Set the precision to be displayed by @spin_button. Up to 5 digit precision
1560 gtk_spin_button_set_digits (GtkSpinButton *spin_button,
1563 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1564 g_return_if_fail (digits < 6);
1566 if (spin_button->digits != digits)
1568 spin_button->digits = digits;
1569 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1570 /* since lower/upper may have changed */
1571 gtk_widget_queue_resize (GTK_WIDGET (spin_button));
1576 * gtk_spin_button_set_increments:
1577 * @spin_button: a #GtkSpinButton
1578 * @step: increment applied for a button 1 press.
1579 * @page: increment applied for a button 2 press.
1581 * Sets the step and page increments for spin_button. This affects how
1582 * quickly the value changes when the spin button's arrows are activated.
1585 gtk_spin_button_set_increments (GtkSpinButton *spin_button,
1589 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1591 spin_button->adjustment->step_increment = step;
1592 spin_button->adjustment->page_increment = page;
1596 * gtk_spin_button_set_range:
1597 * @spin_button: a #GtkSpinButton
1598 * @min: minimum allowable value
1599 * @max: maximum allowable value
1601 * Sets the minimum and maximum allowable values for @spin_button
1604 gtk_spin_button_set_range (GtkSpinButton *spin_button,
1608 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1610 spin_button->adjustment->lower = min;
1611 spin_button->adjustment->upper = max;
1615 * gtk_spin_button_get_value_as_float:
1616 * @spin_button: a #GtkSpinButton
1618 * Get the value @spin_button represented as a floating point number.
1620 * Return value: the value of @spin_button
1623 gtk_spin_button_get_value_as_float (GtkSpinButton *spin_button)
1625 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0.0);
1627 return spin_button->adjustment->value;
1631 * gtk_spin_button_get_value_as_int:
1632 * @spin_button: a #GtkSpinButton
1634 * Get the value @spin_button represented as an integer.
1636 * Return value: the value of @spin_button
1639 gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button)
1643 g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), 0);
1645 val = spin_button->adjustment->value;
1646 if (val - floor (val) < ceil (val) - val)
1653 * gtk_spin_button_set_value:
1654 * @spin_button: a #GtkSpinButton
1655 * @value: the new value
1657 * Set the value of @spin_button.
1660 gtk_spin_button_set_value (GtkSpinButton *spin_button,
1663 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1665 if (fabs (value - spin_button->adjustment->value) > EPSILON)
1666 gtk_adjustment_set_value (spin_button->adjustment, value);
1669 gint return_val = FALSE;
1670 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1672 if (return_val == FALSE)
1673 gtk_spin_button_default_output (spin_button);
1678 * gtk_spin_button_set_update_policy:
1679 * @spin_button: a #GtkSpinButton
1680 * @policy: a #GtkSpinButtonUpdatePolicy value
1682 * Sets the update behavior of a spin button. This determines whether the
1683 * spin button is always updated or only when a valid value is set.
1686 gtk_spin_button_set_update_policy (GtkSpinButton *spin_button,
1687 GtkSpinButtonUpdatePolicy policy)
1689 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1691 spin_button->update_policy = policy;
1695 * gtk_spin_button_set_numeric:
1696 * @spin_button: a #GtkSpinButton
1697 * @numeric: flag indicating if only numeric entry is allowed.
1699 * Sets the flag that determines if non-numeric text can be typed into
1703 gtk_spin_button_set_numeric (GtkSpinButton *spin_button,
1706 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1708 spin_button->numeric = (numeric != 0);
1712 * gtk_spin_button_set_wrap:
1713 * @spin_button: a #GtkSpinButton
1714 * @wrap: a flag indicating if wrapping behavior is performed.
1716 * Sets the flag that determines if a spin button value wraps around to the
1717 * opposite limit when the upper or lower limit of the range is exceeded.
1720 gtk_spin_button_set_wrap (GtkSpinButton *spin_button,
1723 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1725 spin_button->wrap = (wrap != 0);
1729 * spin_button_get_shadow_type:
1730 * @spin_button: a #GtkSpinButton
1732 * Convenience function to Get the shadow type from the underlying widget's
1735 * Return value: the #GtkShadowType
1738 spin_button_get_shadow_type (GtkSpinButton *spin_button)
1740 GtkShadowType rc_shadow_type;
1742 gtk_widget_style_get (GTK_WIDGET (spin_button), "shadow_type", &rc_shadow_type, NULL);
1744 return rc_shadow_type;
1748 * gtk_spin_button_set_snap_to_ticks:
1749 * @spin_button: a #GtkSpinButton
1750 * @snap_to_ticks: a flag indicating if invalid values should be corrected.
1752 * Sets the policy as to whether values are corrected to the nearest step
1753 * increment when a spin button is activated after providing an invalid value.
1756 gtk_spin_button_set_snap_to_ticks (GtkSpinButton *spin_button,
1757 gboolean snap_to_ticks)
1761 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1763 new_val = (snap_to_ticks != 0);
1765 if (new_val != spin_button->snap_to_ticks)
1767 spin_button->snap_to_ticks = new_val;
1768 if (new_val && GTK_ENTRY (spin_button)->editable)
1769 gtk_spin_button_update (spin_button);
1774 * gtk_spin_button_spin:
1775 * @spin_button: a #GtkSpinButton
1776 * @direction: a #GtkSpinType indicating the direction to spin.
1777 * @increment: step increment to apply in the specified direction.
1779 * Increment or decrement a spin button's value in a specified direction
1780 * by a specified amount.
1783 gtk_spin_button_spin (GtkSpinButton *spin_button,
1784 GtkSpinType direction,
1790 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1792 adj = spin_button->adjustment;
1794 /* for compatibility with the 1.0.x version of this function */
1795 if (increment != 0 && increment != adj->step_increment &&
1796 (direction == GTK_SPIN_STEP_FORWARD ||
1797 direction == GTK_SPIN_STEP_BACKWARD))
1799 if (direction == GTK_SPIN_STEP_BACKWARD && increment > 0)
1800 increment = -increment;
1801 direction = GTK_SPIN_USER_DEFINED;
1806 case GTK_SPIN_STEP_FORWARD:
1808 gtk_spin_button_real_spin (spin_button, adj->step_increment);
1811 case GTK_SPIN_STEP_BACKWARD:
1813 gtk_spin_button_real_spin (spin_button, -adj->step_increment);
1816 case GTK_SPIN_PAGE_FORWARD:
1818 gtk_spin_button_real_spin (spin_button, adj->page_increment);
1821 case GTK_SPIN_PAGE_BACKWARD:
1823 gtk_spin_button_real_spin (spin_button, -adj->page_increment);
1828 diff = adj->value - adj->lower;
1830 gtk_spin_button_real_spin (spin_button, -diff);
1835 diff = adj->upper - adj->value;
1837 gtk_spin_button_real_spin (spin_button, diff);
1840 case GTK_SPIN_USER_DEFINED:
1843 gtk_spin_button_real_spin (spin_button, increment);
1852 * gtk_spin_button_update:
1853 * @spin_button: a #GtkSpinButton
1855 * Manually force an update of the spin button.
1858 gtk_spin_button_update (GtkSpinButton *spin_button)
1864 g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
1867 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[INPUT],
1869 if (return_val == FALSE)
1871 return_val = gtk_spin_button_default_input (spin_button, &val);
1872 error = (return_val == GTK_INPUT_ERROR);
1874 else if (return_val == GTK_INPUT_ERROR)
1877 if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
1879 if (val < spin_button->adjustment->lower)
1880 val = spin_button->adjustment->lower;
1881 else if (val > spin_button->adjustment->upper)
1882 val = spin_button->adjustment->upper;
1884 else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
1886 val < spin_button->adjustment->lower ||
1887 val > spin_button->adjustment->upper))
1889 gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
1893 if (spin_button->snap_to_ticks)
1894 gtk_spin_button_snap (spin_button, val);
1897 if (fabs (val - spin_button->adjustment->value) > EPSILON)
1898 gtk_adjustment_set_value (spin_button->adjustment, val);
1902 gtk_signal_emit (GTK_OBJECT (spin_button), spinbutton_signals[OUTPUT],
1904 if (return_val == FALSE)
1905 gtk_spin_button_default_output (spin_button);