10.9. Spin Buttons

The Spin Button widget is generally used to allow the user to select a value from a range of numeric values. It consists of a text entry box with up and down arrow buttons attached to the side. Selecting one of the buttons causes the value to "spin" up and down the range of possible values. The entry box may also be edited directly to enter a specific value.

The Spin Button allows the value to have zero or a number of decimal places and to be incremented/decremented in configurable steps. The action of holding down one of the buttons optionally results in an acceleration of change in the value according to how long it is depressed.

The Spin Button uses an Adjustment object to hold information about the range of values that the spin button can take. This makes for a powerful Spin Button widget.

Recall that an adjustment widget is created with the following method, which illustrates the information that it holds:

Gtk::Adjustment.new( value, lower, upper, step_increment, page_increment, page_size )

These attributes of an Adjustment are used by the Spin Button in the following way:

Additionally, mouse button 3 can be used to jump directly to the upper or lower values when used to select one of the buttons. Lets look at how to create a Spin Button:

Gtk::SpinButton.new( Gtk:Adjustment, climb_rate, digits )

The climb_rate argument takes a value between 0.0 and 1.0 and indicates the amount of acceleration that the Spin Button has. The digits argument specifies the number of decimal places to which the value will be displayed.

A Spin Button can be reconfigured after creation using the following method:

Gtk::SpinButton#configure( Gtk::Adjustment, climb_rate, digits )

The arguments are as specified above.

The adjustment can be set and retrieved independantly using the following methods:

# Syntax sugar again
Gtk::SpinButton#set_adjustment( Gtk::Adjustment )
Gtk::SpinButton#adjustment=( Gtk::Adjustment )

Gtk::SpinButton#adjustment

The number of decimal places can also be altered using:

Gtk::SpinButton#set_digits( digits )
Gtk::SpinButtons#digits=( digits )

The value that a Spin Button is currently displaying can be changed using the following method:

Gtk::SpinButton#set_value( value )
Gtk::SpinButton#value=( value )

The current value of a Spin Button can be retrieved with the following method:

Gtk::SpinButton#value

If you want to alter the value of a Spin Button relative to its current value, then the following method can be used:

Gtk::SpinButton#spin( direction, increment )

The direction parameter can take one of the following values:

  Gtk::SpinButton::STEP_FORWARD
  Gtk::SpinButton::STEP_BACKWARD
  Gtk::SpinButton::PAGE_FORWARD
  Gtk::SpinButton::PAGE_BACKWARD
  Gtk::SpinButton::HOME
  Gtk::SpinButton::END
  Gtk::SpinButton::USER_DEFINED

This method packs in quite a bit of methodality, which I will attempt to clearly explain. Many of these settings use values from the Adjustment object that is associated with a Spin Button.

Gtk::SpinButton::STEP_FORWARD and Gtk::SpinButton::STEP_BACKWARD change the value of the Spin Button by the amount specified by increment, unless increment is equal to 0, in which case the value is changed by the value of step_increment in the Adjustment.

Gtk::Spin::PAGE_FORWARD and Gtk::Spin::PAGE_BACKWARD simply alter the value of the Spin Button by increment.

Gtk::Spin::HOME sets the value of the Spin Button to the bottom of the Adjustments range.

Gtk::Spin::END sets the value of the Spin Button to the top of the Adjustments range.

Gtk::Spin::USER_DEFINED simply alters the value of the Spin Button by the specified amount.

We move away from methods for setting and retreving the range attributes of the Spin Button now, and move onto methods that effect the appearance and behaviour of the Spin Button widget itself.

The first of these methods is used to constrain the text box of the Spin Button such that it may only contain a numeric value. This prevents a user from typing anything other than numeric values into the text box of a Spin Button:

Gtk::SpinButton#set_numeric( numeric )
Gtk::SpinButton#numeric=( numeric )

You can set whether a Spin Button will wrap around between the upper and lower range values with the following methods:

Gtk::SpinButton#set_wrap( wrap )
Gtk::SpinButton#wrap=( wrap )

You can set a Spin Button to round the value to the nearest step_increment, which is set within the Adjustment object used with the Spin Button. This is accomplished with the following methods:

Gtk::SpinButton#set_snap_to_ticks( snap_to_ticks )
Gtk::SpinButton#snap_to_ticks=( snap_to_ticks )

The update policy of a Spin Button can be changed with the following method:

Gtk::SpinButton#set_update_policy( policy )
Gtk::SpinButton#update_policy=( policy )

The possible values of policy are either Gtk::UPDATE_ALWAYS or Gtk::UPDATE_IF_VALID.

These policies affect the behavior of a Spin Button when parsing inserted text and syncing its value with the values of the Adjustment.

In the case of Gtk::UPDATE_IF_VALID the Spin Button only value gets changed if the text input is a numeric value that is within the range specified by the Adjustment. Otherwise the text is reset to the current value.

In case of Gtk::UPDATE_ALWAYS we ignore errors while converting text into a numeric value.

Finally, you can explicitly request that a Spin Button update itself:

Gtk::SpinButton#update

It's example time again.

INSERT SPIN BUTTON SCREENSHOT HERE

CODE
CODE
CODE
CODE
CODE
CODE
CODE

Prev Next