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 function, which illustrates the information that it holds:
$adjustment = Gtk2::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:
$spin = Gtk2::SpinButton->new ($adjustment, $climb_rate, $digits); |
The climb_rate argument take 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 function:
Gtk2::SpinButton->configure ($spin, $adjustment, $climb_rate, $digits); |
The spin argument specifies the Spin Button widget that is to be reconfigured. The other arguments are as specified above.
The adjustment can be set and retrieved independantly using the following two functions:
Gtk2::SpinButton->set_adjustment ($spin, $adjustment); $adjustment = Gtk2::SpinButton->get_adjustment ($spin); |
The number of decimal places can also be altered using:
Gtk2::SpinButton->set_digits($spin, $digits); |
The value that a Spin Button is currently displaying can be changed using the following function:
Gtk2::SpinButton->set_value($spin, $value); |
The current value of a Spin Button can be retrieved as either a floating point or integer value with the following functions:
Gtk2::SpinButton->get_value ($spin); Gtk2::SpinButton->get_value_as_int ($spin); |
If you want to alter the value of a Spin Button relative to its current value, then the following function can be used:
Gtk2::SpinButton->spin($spin, $direction, $increment); |
The direction parameter can take one of the following values:
'step-forward'/'GTK_SPIN_STEP_FORWARD' 'step-backward'/'GTK_SPIN_STEP_BACKWARD' 'page-forward'/'GTK_SPIN_PAGE_FORWARD' 'page-backward'/'GTK_SPIN_PAGE_BACKWARD' 'home'/'GTK_SPIN_HOME' 'end'/'GTK_SPIN_END' 'user-defined'/'GTK_SPIN_USER_DEFINED' |
This function packs in quite a bit of functionality, which I will attempt to clearly explain. Many of these settings use values from the Adjustment object that is associated with a Spin Button.
step-forward and 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.
page-forward and page-backward simply alter the value of the Spin Button by increment.
home sets the value of the Spin Button to the bottom of the Adjustments range.
end sets the value of the Spin Button to the top of the Adjustments range.
user-defined simply alters the value of the Spin Button by the specified amount.
We move away from functions for setting and retreving the range attributes of the Spin Button now, and move onto functions that effect the appearance and behaviour of the Spin Button widget itself.
The first of these functions 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:
Gtk2::SpinButton->set_numeric ($spin, $numeric); |
You can set whether a Spin Button will wrap around between the upper and lower range values with the following function:
Gtk2::SpinButton->set_wrap ($spin, $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 function:
Gtk2::SpinButton->set_snap_to_ticks ($spin, $snap_to_ticks); |
The update policy of a Spin Button can be changed with the following function:
Gtk2::SpinButton->set_update_policy ($spin, $policy); |
The possible values of policy are either always or 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 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 always we ignore errors while converting text into a numeric value.
Finally, you can explicitly request that a Spin Button update itself:
Gtk2::SpinButton->update ($spin); |
It's example time again.
use Glib qw/TRUE FALSE/; use Gtk2 '-init'; our $spinner1; sub get_value { my ($button, $format) = @_; if ('int' eq $format) { $button->{val_label}->set_text (sprintf ("%d", $spinner1->get_value_as_int)); } else { $button->{val_label}->set_text (sprintf ("%0.*f", $spinner1->get_digits, $spinner1->get_value)); } } $window = Gtk2::Window->new ('toplevel'); $window->signal_connect (destroy => sub { Gtk2->main_quit; 0; }); $window->set_title ("Spin Button"); $main_vbox = Gtk2::VBox->new (FALSE, 5); $main_vbox->set_border_width (10); $window->add ($main_vbox); $frame = Gtk2::Frame->new ("Not accelerated"); $main_vbox->pack_start ($frame, TRUE, TRUE, 0); $vbox = Gtk2::VBox->new (FALSE, 0); $vbox->set_border_width (10); $frame->add ($vbox); # Day, month, year spinners $hbox = Gtk2::HBox->new (FALSE, 0); $vbox->pack_start ($hbox, TRUE, TRUE, 5); $vbox2 = Gtk2::VBox->new (FALSE, 0); $hbox->pack_start ($vbox2, TRUE, TRUE, 5); $label = Gtk2::Label->new ("Day :"); $label->set_alignment (0.0, 0.5); # left halignment, middle valignment $vbox2->pack_start ($label, FALSE, TRUE, 0); $adj = Gtk2::Adjustment->new (1.0, 1.0, 31.0, 1.0, 5.0, 0.0); $spinner = Gtk2::SpinButton->new ($adj, 0, 0); $spinner->set_wrap (TRUE); $vbox2->pack_start ($spinner, FALSE, TRUE, 0); $vbox2 = Gtk2::VBox->new (FALSE, 0); $hbox->pack_start ($vbox2, TRUE, TRUE, 5); $label = Gtk2::Label->new ("Month :"); $label->set_alignment (0.0, 0.5); # left halignment, middle valignment $vbox2->pack_start ($label, FALSE, TRUE, 0); $adj = Gtk2::Adjustment->new (1.0, 1.0, 12.0, 1.0, 5.0, 0.0); $spinner = Gtk2::SpinButton->new ($adj, 0, 0); $spinner->set_wrap (TRUE); $vbox2->pack_start ($spinner, FALSE, TRUE, 0); $vbox2 = Gtk2::VBox->new (FALSE, 0); $hbox->pack_start ($vbox2, TRUE, TRUE, 5); $label = Gtk2::Label->new ("Year :"); $label->set_alignment (0.0, 0.5); # left halignment, middle valignment $vbox2->pack_start ($label, FALSE, TRUE, 0); $adj = Gtk2::Adjustment->new (1998.0, 1.0, 2100.0, 1.0, 100.0, 0.0); $spinner = Gtk2::SpinButton->new ($adj, 0, 0); $spinner->set_wrap (TRUE); $spinner->set_size_request (55, -1); $vbox2->pack_start ($spinner, FALSE, TRUE, 0); $frame = Gtk2::Frame->new ("Accelerated"); $main_vbox->pack_start ($frame, TRUE, TRUE, 0); $vbox = Gtk2::VBox->new (FALSE, 0); $vbox->set_border_width (5); $frame->add ($vbox); $hbox = Gtk2::HBox->new (FALSE, 0); $vbox->pack_start ($hbox, TRUE, TRUE, 5); $vbox2 = Gtk2::VBox->new (FALSE, 0); $hbox->pack_start ($vbox2, TRUE, TRUE, 5); $label = Gtk2::Label->new ("Value :"); $label->set_alignment (0.0, 0.5); # left halignment, middle valignment $vbox2->pack_start ($label, FALSE, TRUE, 0); $adj = Gtk2::Adjustment->new (0.0, -10000.0, 10000.0, 0.5, 100.0, 0.0); $spinner1 = Gtk2::SpinButton->new ($adj, 1.0, 2); $spinner1->set_wrap (TRUE); $spinner1->set_size_request (100, -1); $vbox2->pack_start ($spinner1, FALSE, TRUE, 0); $vbox2 = Gtk2::VBox->new (FALSE, 0); $hbox->pack_start ($vbox2, TRUE, TRUE, 5); $label = Gtk2::Label->new ("Digits :"); $label->set_alignment (0.0, 0.5); # left halignment, middle valignment $vbox2->pack_start ($label, FALSE, TRUE, 0); $adj = Gtk2::Adjustment->new (2, 1, 5, 1, 1, 0); $spinner2 = Gtk2::SpinButton->new ($adj, 0.0, 0); $spinner2->set_wrap (TRUE); $adj->signal_connect (value_changed => sub { $spinner1->set_digits ($spinner2->get_value_as_int ()); }); $vbox2->pack_start ($spinner2, FALSE, TRUE, 0); $button = Gtk2::CheckButton->new ("Snap to 0.5-ticks"); $button->signal_connect (clicked => sub { $spinner1->set_snap_to_ticks ($_[0]->get_active); }); $vbox->pack_start ($button, TRUE, TRUE, 0); $button->set_active (TRUE); $button = Gtk2::CheckButton->new ("Numeric only input mode"); $button->signal_connect (clicked => sub { $spinner1->set_numeric ($_[0]->get_active); }); $vbox->pack_start ($button, TRUE, TRUE, 0); $button->set_active (TRUE); $val_label = Gtk2::Label->new (""); $hbox = Gtk2::HBox->new (FALSE, 0); $vbox->pack_start ($hbox, FALSE, TRUE, 5); $button = Gtk2::Button->new ("Value as Int"); $button->{val_label} = $val_label; $button->signal_connect (clicked => \&get_value, 'int'); $hbox->pack_start ($button, TRUE, TRUE, 5); $button = Gtk2::Button->new ("Value as Float"); $button->{val_label} = $val_label; $button->signal_connect (clicked => \&get_value, 'float'); $hbox->pack_start ($button, TRUE, TRUE, 5); $vbox->pack_start ($val_label, TRUE, TRUE, 0); $val_label->set_text ("0"); $hbox = Gtk2::HBox->new (FALSE, 0); $main_vbox->pack_start ($hbox, FALSE, TRUE, 0); $button = Gtk2::Button->new ("Close"); $button->signal_connect (clicked => sub { $window->destroy; }); $hbox->pack_start ($button, TRUE, TRUE, 0); $window->show_all; Gtk2->main; 0; |