GTK+ Reference Manual
<<< Previous Page | Home | Up | Next Page >>>

# GtkSpinButton

## Name

GtkSpinButton -- retrieve an integer or floating-point number from the user.

## Synopsis

    #include <gtk/gtk.h>

    struct      GtkSpinButton;
    enum        GtkSpinButtonUpdatePolicy;
    enum        GtkSpinType;
    void        gtk_spin_button_configure       (GtkSpinButton *spin_button,
                                                 GtkAdjustment *adjustment,
                                                 gfloat climb_rate,
                                                 guint digits);
    GtkWidget*  gtk_spin_button_new             (GtkAdjustment *adjustment,
                                                 gfloat climb_rate,
                                                 guint digits);
    void        gtk_spin_button_set_adjustment  (GtkSpinButton *spin_button,
                                                 GtkAdjustment *adjustment);
    GtkAdjustment* gtk_spin_button_get_adjustment
                                                (GtkSpinButton *spin_button);
    void        gtk_spin_button_set_digits      (GtkSpinButton *spin_button,
                                                 guint digits);
    gfloat      gtk_spin_button_get_value_as_float
                                                (GtkSpinButton *spin_button);
    gint        gtk_spin_button_get_value_as_int
                                                (GtkSpinButton *spin_button);
    void        gtk_spin_button_set_value       (GtkSpinButton *spin_button,
                                                 gfloat value);
    void        gtk_spin_button_set_update_policy
                                                (GtkSpinButton *spin_button,
                                                 GtkSpinButtonUpdatePolicy policy);
    void        gtk_spin_button_set_numeric     (GtkSpinButton *spin_button,
                                                 gboolean numeric);
    void        gtk_spin_button_spin            (GtkSpinButton *spin_button,
                                                 GtkSpinType direction,
                                                 gfloat increment);
    void        gtk_spin_button_set_wrap        (GtkSpinButton *spin_button,
                                                 gboolean wrap);
    void        gtk_spin_button_set_shadow_type (GtkSpinButton *spin_button,
                                                 GtkShadowType shadow_type);
    void        gtk_spin_button_set_snap_to_ticks
                                                (GtkSpinButton *spin_button,
                                                 gboolean snap_to_ticks);
    void        gtk_spin_button_update          (GtkSpinButton *spin_button);

## Object Hierarchy

      GtkObject
       +----GtkWidget
             +----GtkEditable
                   +----GtkEntry
                         +----GtkSpinButton

## Args

      "adjustment"           GtkAdjustment        : Read / Write
      "climb_rate"           gfloat               : Read / Write
      "digits"               guint                : Read / Write
      "snap_to_ticks"        gboolean             : Read / Write
      "numeric"              gboolean             : Read / Write
      "wrap"                 gboolean             : Read / Write
      "update_policy"        GtkSpinButtonUpdatePolicy : Read / Write
      "shadow_type"          GtkShadowType        : Read / Write
      "value"                gfloat               : Read / Write

## Description

A GtkSpinButton is an ideal way to allow the user to set the value of some attribute. Rather than having to directly type a number into a GtkEntry, GtkSpinButton allows the user to click on one of two arrows to increment or decrement the displayed value. A value can still be typed in, with the bonus that it can be checked to ensure it is in a given range.

The main properties of a GtkSpinButton are through a GtkAdjustment. See the GtkAdjustment section for more details about an adjustment's properties.

GtkSpinButton widgets are created with a call to gtk_spin_button_new().

The GtkAdjustment of a spin button can be set or retrieved with a call to gtk_spin_button_set_adjustment() or gtk_spin_button_get_adjustment(), respectively.

The number of digits after the decimal point of a spin button can be altered with gtk_spin_button_set_digits().

To retrieve values from a spin button, use gtk_spin_button_get_value_as_float() if you require a floating point number, or gtk_spin_button_get_value_as_int() if you require an integer.

To set the value of a GtkSpinButton, use gtk_spin_button_set_value(). To change the update behaviour of a spin button, use gtk_spin_button_set_update_policy().

When a spin button reaches it's upper or lower limit, it can either stop spinning, or wrap around and continue spinning from the opposite limit. For example, if five is the upper limit and the lower limit is zero, upon reaching the value five, the spin button can change it's value back to zero and continue spinning upwards. This behaviour is set with gtk_spin_button_set_wrap().

A border around a spin button's arrows can be created using gtk_spin_button_set_shadow_type().

A number may be entered that is invalid, given a spin button's range. An erroneous number can be corrected as soon as the spin button is 'activated' using gtk_spin_button_snap_to_ticks(), which will alter the current value to the nearest step increment. (See GtkAdjustment for step increments).

Because a spin contains a GtkEntry, alphabetic characters may be entered. These can be ignored by using gtk_spin_button_set_numeric() with a value of TRUE. Then only numeric values, '-' and a decimal point will be accepted.

To manually increment or decrement the spin button, use gtk_spin_button_spin(), and to force an update (refresh), use gtk_spin_button_update().

Example 1. Using a GtkSpinButton to get an integer.

    /* Provides a function to retrieve an integer value from a GtkSpinButton
     * and creates a spin button to model percentage values.
     */

    gint grab_int_value (GtkSpinButton *a_spinner, gpointer user_data) {
       return gtk_spin_button_get_value_as_int (a_spinner);
    }

    void create_integer_spin_button(void) {

       GtkWidget *window, *spinner;
       GtkAdjustment *spinner_adj;

       spinner_adj = (GtkAdjustment *) gtk_adjustment_new(50.0, 0.0, 100.0, 1.0, 5.0, 5.0);

       window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
       gtk_container_set_border_width (GTK_CONTAINER (window), 5);

       /* creates the spinner, with no decimal places */
       spinner = gtk_spin_button_new (spinner_adj, 1.0, 0);
       gtk_container_add (GTK_CONTAINER(window), spinner);

       gtk_widget_show_all (window);
       return;
    }

Example 2. Using a GtkSpinButton to get a floating point value.

    /* Provides a function to retrieve a floating point value from a
     * GtkSpinButton, and creates a high precision spin button.
     */

    gfloat grab_int_value (GtkSpinButton *a_spinner, gpointer user_data) {
       return gtk_spin_button_get_value_as_float (a_spinner);
    }

    void create_floating_spin_button(void) {

       GtkWidget *window, *spinner;
       GtkAdjustment *spinner_adj;

       spinner_adj = (GtkAdjustment *) gtk_adjustment_new(2.500, 0.0, 5.0, 0.001, 0.1, 0.1);

       window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
       gtk_container_set_border_width (GTK_CONTAINER (window), 5);

       /* creates the spinner, with three decimal places */
       spinner = gtk_spin_button_new (spinner_adj, 0.001, 3);
       gtk_container_add (GTK_CONTAINER(window), spinner);

       gtk_widget_show_all (window);
       return;
    }

## Details

### struct GtkSpinButton

    struct GtkSpinButton;

`entry` is the GtkEntry part of the GtkSpinButton widget, and can be used accordingly. All other fields contain private data and should only be modified using the functions below.

### enum GtkSpinButtonUpdatePolicy

    typedef enum
    {
      GTK_UPDATE_ALWAYS,
      GTK_UPDATE_IF_VALID
    } GtkSpinButtonUpdatePolicy;

GTK_UPDATE_ALWAYS | When refreshing your GtkSpinButton, the value is always displayed.
GTK_UPDATE_IF_VALID | When refreshing your GtkSpinButton, the value is only displayed if it is valid within the bounds of the spin button's GtkAdjustment.

### enum GtkSpinType

    typedef enum
    {
      GTK_SPIN_STEP_FORWARD,
      GTK_SPIN_STEP_BACKWARD,
      GTK_SPIN_PAGE_FORWARD,
      GTK_SPIN_PAGE_BACKWARD,
      GTK_SPIN_HOME,
      GTK_SPIN_END,
      GTK_SPIN_USER_DEFINED
    } GtkSpinType;

GTK_SPIN_STEP_FORWARD, GTK_SPIN_STEP_BACKWARD, GTK_SPIN_PAGE_FORWARD, GTK_SPIN_PAGE_BACKWARD | These values spin a GtkSpinButton by the relevant values of the spin button's GtkAdjustment.
GTK_SPIN_HOME, GTK_SPIN_END | These set the spin button's value to the minimum or maxmimum possible values, (set by it's GtkAdjustment), respectively.
GTK_SPIN_USER_DEFINED | The programmer must specify the exact amount to spin the GtkSpinButton.

### gtk_spin_button_configure ()

    void        gtk_spin_button_configure       (GtkSpinButton *spin_button,
                                                 GtkAdjustment *adjustment,
                                                 gfloat climb_rate,
                                                 guint digits);

Changes the properties of an existing spin button. The adjustment, climb rate, and number of decimal places are all changed accordingly, after this function call.

`spin_button` : | a GtkSpinButton.
`adjustment` : | a GtkAdjustment.
`climb_rate` : | the new climb rate.
`digits` : | the number of decimal places to display in the spin button.

### gtk_spin_button_new ()

    GtkWidget*  gtk_spin_button_new             (GtkAdjustment *adjustment,
                                                 gfloat climb_rate,
                                                 guint digits);

Creates a new GtkSpinButton.

`adjustment` : | the GtkAdjustment object that this spin button should use.
`climb_rate` : | specifies how much the spin button changes when an arrow is clicked on.
`digits` : | the number of decimal places to display.
Returns : | a GtkWidget.

### gtk_spin_button_set_adjustment ()

    void        gtk_spin_button_set_adjustment  (GtkSpinButton *spin_button,
                                                 GtkAdjustment *adjustment);

Changes which GtkAdjustment is associated with a spin button.

`spin_button` : | a GtkSpinButton.
`adjustment` : | a GtkAdjustment.

### gtk_spin_button_get_adjustment ()

    GtkAdjustment* gtk_spin_button_get_adjustment
                                                (GtkSpinButton *spin_button);

Retrieves the GtkAdjustment used by a given spin button.

`spin_button` : | a GtkSpinButton.
Returns : | a GtkAdjustment.

### gtk_spin_button_set_digits ()

    void        gtk_spin_button_set_digits      (GtkSpinButton *spin_button,
                                                 guint digits);

Alters the number of decimal places that are displayed in a spin button.

`spin_button` : | a GtkSpinButton.
`digits` : | the number of decimal places.

### gtk_spin_button_get_value_as_float ()

    gfloat      gtk_spin_button_get_value_as_float
                                                (GtkSpinButton *spin_button);

Retrieves the current value of a GtkSpinButton. If the number has no decimal places, it is converted to a float before the function returns.

`spin_button` : | a GtkSpinButton.
Returns : | the value of `spin_button` as a gfloat.

### gtk_spin_button_get_value_as_int ()

    gint        gtk_spin_button_get_value_as_int
                                                (GtkSpinButton *spin_button);

Retrieves the current integer value of a GtkSpinButton.

`spin_button` : | a GtkSpinButton.
Returns : | the value of `spin_button` as a gint.

### gtk_spin_button_set_value ()

    void        gtk_spin_button_set_value       (GtkSpinButton *spin_button,
                                                 gfloat value);

Sets the value of a spin button.

`spin_button` : | a GtkSpinButton.
`value` : | the new floating point value.

### gtk_spin_button_set_update_policy ()

    void        gtk_spin_button_set_update_policy
                                                (GtkSpinButton *spin_button,
                                                 GtkSpinButtonUpdatePolicy policy);

Changes the way a spin button refreshes and updates itself. See `GtkSpinButtonUpdatePolicy` for more information.

`spin_button` : | a GtkSpinButton.
`policy` : | the new update policy.

### gtk_spin_button_set_numeric ()

    void        gtk_spin_button_set_numeric     (GtkSpinButton *spin_button,
                                                 gboolean numeric);

Sets how the spin button's GtkEntry reacts to alphabetic characters. A value of TRUE to `numeric` means that all non-numeric characters (except '-' and a decimal point) are ignored.

`spin_button` : | a GtkSpinButton.
`numeric` : | whether letters should be ignored.

### gtk_spin_button_spin ()

    void        gtk_spin_button_spin            (GtkSpinButton *spin_button,
                                                 GtkSpinType direction,
                                                 gfloat increment);

Performs an explicit 'spin' on a spin button.

`spin_button` : | a GtkSpinButton.
`direction` : | the type of spin to perform.
`increment` : | the amount to spin.

### gtk_spin_button_set_wrap ()

    void        gtk_spin_button_set_wrap        (GtkSpinButton *spin_button,
                                                 gboolean wrap);

Sets a spin button's value to the lower limit when it's upper limit is reached, and vice versa.

`spin_button` : | a GtkSpinButton.
`wrap` : | defaults to FALSE, set to TRUE to make the spin button wrap.

### gtk_spin_button_set_shadow_type ()

    void        gtk_spin_button_set_shadow_type (GtkSpinButton *spin_button,
                                                 GtkShadowType shadow_type);

Creates a border around the arrows of a GtkSpinButton. The type of border is determined by `shadow_type`.

`spin_button` : | a GtkSpinButton
`shadow_type` : | the new border type.

### gtk_spin_button_set_snap_to_ticks ()

    void        gtk_spin_button_set_snap_to_ticks
                                                (GtkSpinButton *spin_button,
                                                 gboolean snap_to_ticks);

Sets whether a number typed into a spin button should be snapped to the nearest step increment.

`spin_button` : | a GtkButton.
`snap_to_ticks` : | TRUE or FALSE.

### gtk_spin_button_update ()

    void        gtk_spin_button_update          (GtkSpinButton *spin_button);

Refreshes a spin button. The behaviour of the update is determined by gtk_spin_button_set_update_policy().

`spin_button` : | a GtkSpinButton.

## Args

"`adjustment`" (GtkAdjustment : Read / Write)

  the GtkAdjustment that defines a spin button's main properties.

"`climb_rate`" (gfloat : Read / Write)

  the amount a spin button changes when an arrow is clicked.

"`digits`" (guint : Read / Write)

  the number of decimal places to display.

"`snap_to_ticks`" (gboolean : Read / Write)

  whether erroneous values are automatically changed to a spin button's nearest step increment.

"`numeric`" (gboolean : Read / Write)

  whether non-numeric characters should be ignored.

"`wrap`" (gboolean : Read / Write)

  whether a spin button should wrap upon reaching its limits.

"`update_policy`" (GtkSpinButtonUpdatePolicy : Read / Write)

  how a spin button should be updated.

"`shadow_type`" (GtkShadowType : Read / Write)

  the type of border that surrounds the arrows of a spin button.

"`value`" (gfloat : Read / Write)

  reads the current value, or sets a new value.

## See Also

GtkEntry

  retrieve text rather than numbers.

<<< Previous Page | Home | Up | Next Page >>>
GtkSocket | GtkStatusbar