1.185 <GtkSpinButton>

Class: <GtkSpinButton>

It derives from <GtkWidget>.

Allows to enter or change numeric values.

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 an adjustment. See the <GtkAdjustment> documentation for more details about an adjustment’s properties.

Note that <GtkSpinButton> will by default make its entry large enough to accommodate the lower and upper bounds of the adjustment. If this is not desired, the automatic sizing can be turned off by explicitly setting editable:width-chars to a value != -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.

int
grab_int_value (GtkSpinButton *button,
                gpointer       user_data)
{
  return gtk_spin_button_get_value_as_int (button);
}

void
create_integer_spin_button (void)
{

  GtkWidget *window, *button;
  GtkAdjustment *adjustment;

  adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0);

  window = gtk_window_new ();

  // creates the spinbutton, with no decimal places
  button = gtk_spin_button_new (adjustment, 1.0, 0);
  gtk_window_set_child (GTK_WINDOW (window), button);

  gtk_window_present (GTK_WINDOW (window));
}

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.

float
grab_float_value (GtkSpinButton *button,
                  gpointer       user_data)
{
  return gtk_spin_button_get_value (button);
}

void
create_floating_spin_button (void)
{
  GtkWidget *window, *button;
  GtkAdjustment *adjustment;

  adjustment = gtk_adjustment_new (2.500, 0.0, 5.0, 0.001, 0.1, 0.0);

  window = gtk_window_new ();

  // creates the spinbutton, with three decimal places
  button = gtk_spin_button_new (adjustment, 0.001, 3);
  gtk_window_set_child (GTK_WINDOW (window), button);

  gtk_window_present (GTK_WINDOW (window));
}

Shortcuts and Gestures

The following signals have default keybindings:

CSS nodes

spinbutton.horizontal
├── text
│    ├── undershoot.left
│    ╰── undershoot.right
├── button.down
╰── button.up
spinbutton.vertical
├── button.up
├── text
│    ├── undershoot.left
│    ╰── undershoot.right
╰── button.down

<GtkSpinButton>s main CSS node has the name spinbutton. It creates subnodes for the entry and the two buttons, with these names. The button nodes have the style classes .up and .down. The <GtkText> subnodes (if present) are put below the text node. The orientation of the spin button is reflected in the .vertical or .horizontal style class on the main node.

Accessibility

<GtkSpinButton> uses the Gtk.AccessibleRole.spin_button role.

Properties

Property: spin-button:activates-default self

Whether to activate the default widget when the spin button is activated.

See spin-button:activate for what counts as activation.

Its value is a boolean.

Set it with (set! (spin-button:activates-default self) value).

Property: spin-button:adjustment self

The adjustment that holds the value of the spin button.

Its value is a <GtkAdjustment>, or #f.

Set it with (set! (spin-button:adjustment self) value).

Property: spin-button:climb-rate self

The acceleration rate when you hold down a button or key.

Its value is a real number.

Set it with (set! (spin-button:climb-rate self) value).

Property: spin-button:digits self

The number of decimal places to display.

Its value is an unsigned 32-bit integer.

Set it with (set! (spin-button:digits self) value).

Property: spin-button:numeric self

Whether non-numeric characters should be ignored.

Its value is a boolean.

Set it with (set! (spin-button:numeric self) value).

Property: spin-button:snap-to-ticks self

Whether erroneous values are automatically changed to the spin buttons nearest step increment.

Its value is a boolean.

Set it with (set! (spin-button:snap-to-ticks self) value).

Property: spin-button:update-policy self

Whether the spin button should update always, or only when the value is acceptable.

Its value is a <GtkSpinButtonUpdatePolicy> (or, to set it, one of its symbols).

Set it with (set! (spin-button:update-policy self) value).

Property: spin-button:value self

The current value.

Its value is a real number.

Set it with (set! (spin-button:value self) value).

Property: spin-button:wrap self

Whether a spin button should wrap upon reaching its limits.

Its value is a boolean.

Set it with (set! (spin-button:wrap self) value).

Procedures

Method: spin-button:configure self adjustment climb-rate digits
Method: configure self adjustment climb-rate digits

Changes the properties of an existing spin button.

The adjustment, climb rate, and number of decimal places are updated accordingly.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

adjustment

Passed: a <GtkAdjustment>, or #f.

a <GtkAdjustment> to replace the spin button’s existing adjustment, or #f to leave its current adjustment unchanged

climb-rate

Passed: a real number.

the new climb rate

digits

Passed: an unsigned 32-bit integer.

the number of decimal places to display in the spin button

Method: spin-button:get-activates-default? self ⇒ %return
Method: get-activates-default? self ⇒ %return

Retrieves the value set by spin-button:set-activates-default.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

%return

Returned: a boolean.

#t if the spin button will activate the default widget

Method: spin-button:get-adjustment self ⇒ %return
Method: get-adjustment self ⇒ %return

Get the adjustment associated with a <GtkSpinButton>.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

%return

Returned: a <GtkAdjustment>.

the <GtkAdjustment> of self

Method: spin-button:get-climb-rate self ⇒ %return
Method: get-climb-rate self ⇒ %return

Returns the acceleration rate for repeated changes.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

%return

Returned: a real number.

the acceleration rate

Method: spin-button:get-digits self ⇒ %return
Method: get-digits self ⇒ %return

Fetches the precision of self.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

%return

Returned: an unsigned 32-bit integer.

the current precision

Method: spin-button:get-increments self ⇒ step page
Method: get-increments self ⇒ step page

Gets the current step and page the increments used by self.

See spin-button:set-increments.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

step

Returned: a real number.

location to store step increment

page

Returned: a real number.

location to store page increment

Method: spin-button:get-numeric? self ⇒ %return
Method: get-numeric? self ⇒ %return

Returns whether non-numeric text can be typed into the spin button.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

%return

Returned: a boolean.

#t if only numeric text can be entered

Method: spin-button:get-range self ⇒ min max
Method: get-range self ⇒ min max

Gets the range allowed for self.

See spin-button:set-range.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

min

Returned: a real number.

location to store minimum allowed value

max

Returned: a real number.

location to store maximum allowed value

Method: spin-button:get-snap-to-ticks? self ⇒ %return
Method: get-snap-to-ticks? self ⇒ %return

Returns whether the values are corrected to the nearest step.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

%return

Returned: a boolean.

#t if values are snapped to the nearest step

Method: spin-button:get-update-policy self ⇒ %return
Method: get-update-policy self ⇒ %return

Gets the update behavior of a spin button.

See spin-button:set-update-policy.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

%return

Returned: a <GtkSpinButtonUpdatePolicy>.

the current update policy

Method: spin-button:get-value self ⇒ %return
Method: get-value self ⇒ %return

Get the value in the self.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

%return

Returned: a real number.

the value of self

Method: spin-button:get-value-as-int self ⇒ %return
Method: get-value-as-int self ⇒ %return

Get the value self represented as an integer.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

%return

Returned: a 32-bit integer.

the value of self

Method: spin-button:get-wrap? self ⇒ %return
Method: get-wrap? self ⇒ %return

Returns whether the spin button’s value wraps around to the opposite limit when the upper or lower limit of the range is exceeded.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

%return

Returned: a boolean.

#t if the spin button wraps around

Procedure: spin-button:new adjustment climb-rate digits ⇒ %return

Creates a new <GtkSpinButton>.

adjustment

Passed: a <GtkAdjustment>, or #f.

the <GtkAdjustment> that this spin button should use

climb-rate

Passed: a real number.

specifies by how much the rate of change in the value will accelerate if you continue to hold down an up/down button or arrow key

digits

Passed: an unsigned 32-bit integer.

the number of decimal places to display

%return

Returned: a <GtkWidget>.

The new <GtkSpinButton>

Procedure: spin-button:new-with-range min max step ⇒ %return

Creates a new <GtkSpinButton> with the given properties.

This is a convenience constructor that allows creation of a numeric <GtkSpinButton> without manually creating an adjustment. The value is initially set to the minimum value and a page increment of 10 * step is the default. The precision of the spin button is equivalent to the precision of step.

Note that the way in which the precision is derived works best if step is a power of ten. If the resulting precision is not suitable for your needs, use spin-button:set-digits to correct it.

min

Passed: a real number.

Minimum allowable value

max

Passed: a real number.

Maximum allowable value

step

Passed: a real number.

Increment added or subtracted by spinning the widget

%return

Returned: a <GtkWidget>.

The new <GtkSpinButton>

Method: spin-button:set-activates-default self activates-default
Method: set-activates-default self activates-default

Sets whether activating the spin button will activate the default widget for the window containing the spin button.

See spin-button:activate for what counts as activation.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

activates-default

Passed: a boolean.

#t to activate window’s default widget on activation

Method: spin-button:set-adjustment self adjustment
Method: set-adjustment self adjustment

Replaces the <GtkAdjustment> associated with self.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

adjustment

Passed: a <GtkAdjustment>.

a <GtkAdjustment> to replace the existing adjustment

Method: spin-button:set-climb-rate self climb-rate
Method: set-climb-rate self climb-rate

Sets the acceleration rate for repeated changes when you hold down a button or key.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

climb-rate

Passed: a real number.

the rate of acceleration, must be >= 0

Method: spin-button:set-digits self digits
Method: set-digits self digits

Set the precision to be displayed by self.

Up to 20 digit precision is allowed.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

digits

Passed: an unsigned 32-bit integer.

the number of digits after the decimal point to be displayed for the spin button’s value

Method: spin-button:set-increments self step page
Method: set-increments self step page

Sets the step and page increments for spin_button.

This affects how quickly the value changes when the spin button’s arrows are activated.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

step

Passed: a real number.

increment applied for a button 1 press.

page

Passed: a real number.

increment applied for a button 2 press.

Method: spin-button:set-numeric self numeric
Method: set-numeric self numeric

Sets the flag that determines if non-numeric text can be typed into the spin button.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

numeric

Passed: a boolean.

flag indicating if only numeric entry is allowed

Method: spin-button:set-range self min max
Method: set-range self min max

Sets the minimum and maximum allowable values for self.

If the current value is outside this range, it will be adjusted to fit within the range, otherwise it will remain unchanged.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

min

Passed: a real number.

minimum allowable value

max

Passed: a real number.

maximum allowable value

Method: spin-button:set-snap-to-ticks self snap-to-ticks
Method: set-snap-to-ticks self snap-to-ticks

Sets the policy as to whether values are corrected to the nearest step increment when a spin button is activated after providing an invalid value.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

snap-to-ticks

Passed: a boolean.

a flag indicating if invalid values should be corrected

Method: spin-button:set-update-policy self policy
Method: set-update-policy self policy

Sets the update behavior of a spin button.

This determines whether the spin button is always updated or only when a valid value is set.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

policy

Passed: a <GtkSpinButtonUpdatePolicy>, or one of its symbols.

a <GtkSpinButtonUpdatePolicy> value

Method: spin-button:set-value self value
Method: set-value self value

Sets the value of self.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

value

Passed: a real number.

the new value

Method: spin-button:set-wrap self wrap
Method: set-wrap self wrap

Sets the flag that determines if a spin button value wraps around to the opposite limit when the upper or lower limit of the range is exceeded.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

wrap

Passed: a boolean.

a flag indicating if wrapping behavior is performed

Method: spin-button:spin self direction increment
Method: spin self direction increment

Increment or decrement a spin button’s value in a specified direction by a specified amount.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

direction

Passed: a <GtkSpinType>, or one of its symbols.

a <GtkSpinType> indicating the direction to spin

increment

Passed: a real number.

step increment to apply in the specified direction

Method: spin-button:update self
Method: update self

Manually force an update of the spin button.

self

Passed: a <GtkSpinButton>.

a <GtkSpinButton>

Signals

Signal: spin-button:activate

Connect a handler with (connect object spin-button:activate handler). It is called as (handler self).

Emitted when the spin button is activated.

The keybindings for this signal are all forms of the Enter key.

If the Enter key results in the value being committed to the spin button, then activation does not occur until Enter is pressed again.

self

Passed: a <GtkSpinButton>.

Signal: spin-button:change-value

Connect a handler with (connect object spin-button:change-value handler). It is called as (handler self scroll).

Emitted when the user initiates a value change.

This is a keybinding signal.

Applications should not connect to it, but may emit it with g_signal_emit_by_name() if they need to control the cursor programmatically.

The default bindings for this signal are Up/Down and PageUp/PageDown.

self

Passed: a <GtkSpinButton>.

scroll

Passed: a <GtkScrollType>.

a <GtkScrollType> to specify the speed and amount of change

Signal: spin-button:input

Connect a handler with (connect object spin-button:input handler). It is called as (handler self) ⇒ %return new-value.

Emitted to convert the users input into a double value.

The signal handler is expected to use editable:get-text to retrieve the text of the spinbutton and set new-value to the new value.

The default conversion uses strtod.

self

Passed: a <GtkSpinButton>.

%return

Returned: a 32-bit integer.

#t for a successful conversion, #f if the input was not handled, and GTK_INPUT_ERROR if the conversion failed.

new-value

Returned: a real number.

return location for the new value

Signal: spin-button:output

Connect a handler with (connect object spin-button:output handler). It is called as (handler self) ⇒ %return.

Emitted to tweak the formatting of the value for display.

// show leading zeros
static gboolean
on_output (GtkSpinButton *spin,
           gpointer       data)
{
   char *text;
   int value;

   value = gtk_spin_button_get_value_as_int (spin);
   text = g_strdup_printf ("%02d", value);
   gtk_editable_set_text (GTK_EDITABLE (spin), text):
   g_free (text);

   return TRUE;
}
self

Passed: a <GtkSpinButton>.

%return

Returned: a boolean.

#t if the value has been displayed

Signal: spin-button:value-changed

Connect a handler with (connect object spin-button:value-changed handler). It is called as (handler self).

Emitted when the value is changed.

Also see the spin-button:output signal.

self

Passed: a <GtkSpinButton>.

Signal: spin-button:wrapped

Connect a handler with (connect object spin-button:wrapped handler). It is called as (handler self).

Emitted right after the spinbutton wraps from its maximum to its minimum value or vice-versa.

self

Passed: a <GtkSpinButton>.