Class: Gtk::SpinButton

Inherits:
Widget
  • Object
show all
Defined in:
(unknown)

Overview

A GtkSpinButton is an ideal way to allow the user to set the value of some attribute.

An example GtkSpinButton

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 [classGtk.Adjustment] 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 [propertyGtk.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));
}

CSS nodes

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

GtkSpinButtons 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_ACCESSIBLE_ROLE_SPIN_BUTTON role.

Instance Method Summary collapse

Methods inherited from Widget

#accessible_role, #accessible_role=, #action_set_enabled, #activate, #activate_action, #activate_action_variant, #activate_default, #activate_signal, #activate_signal=, #activate_signal_from_name=, #add_binding, #add_binding_action, #add_binding_signal, #add_controller, #add_css_class, #add_mnemonic_label, #add_shortcut, #add_tick_callback, #allocate, #allocated_baseline, #allocated_height, #allocated_width, #baseline, #bind_template_callback_full, #bind_template_child_full, #can_focus, #can_focus=, #can_focus?, #can_target, #can_target=, #can_target?, #child_focus, #child_visible, #child_visible=, #clipboard, #compute_bounds, #compute_expand, #compute_point, #compute_transform, #contains, #create_pango_context, #create_pango_layout, #css_classes, #css_classes=, #css_name, #css_name=, #cursor, #cursor=, #cursor_from_name=, default_direction, default_direction=, #direction, #direction=, #display, #dispose_template, #drag_check_threshold, #error_bell, #first_child, #focus_child, #focus_child=, #focus_on_click, #focus_on_click=, #focus_on_click?, #focusable, #focusable=, #focusable?, #font_map, #font_map=, #font_options, #font_options=, #frame_clock, #get_allocation, #get_ancestor, #get_color, #get_preferred_size, #get_size, #get_size_request, #get_template_child, #grab_focus, #halign, #halign=, #has_css_class, #has_default, #has_default=, #has_default?, #has_focus, #has_focus=, #has_focus?, #has_tooltip, #has_tooltip=, #has_tooltip?, #has_visible_focus, #height, #height_request, #height_request=, #hexpand, #hexpand=, #hexpand?, #hexpand_set, #hexpand_set=, #hexpand_set?, #hide, #in_destruction, #init_template, #insert_action_group, #insert_after, #insert_before, #install_action, #install_property_action, #is_ancestor, #is_drawable, #is_focus, #is_sensitive, #is_visible, #keynav_failed, #last_child, #layout_manager, #layout_manager=, #layout_manager_type, #layout_manager_type=, #list_mnemonic_labels, #map, #mapped, #margin_bottom, #margin_bottom=, #margin_end, #margin_end=, #margin_start, #margin_start=, #margin_top, #margin_top=, #measure, #mnemonic_activate, #name, #name=, #native, #next_sibling, #observe_children, #observe_controllers, #opacity, #opacity=, #overflow, #overflow=, #pango_context, #parent, #parent=, #pick, #prev_sibling, #primary_clipboard, #query_action, #queue_allocate, #queue_draw, #queue_resize, #realize, #realized, #receives_default, #receives_default=, #receives_default?, #remove_controller, #remove_css_class, #remove_mnemonic_label, #remove_tick_callback, #request_mode, #root, #root=, #scale_factor, #scale_factor=, #sensitive, #sensitive=, #sensitive?, #set_size_request, #set_state_flags, #settings, #should_layout, #show, #size_allocate, #snapshot_child, #state_flags, #style_context, #template=, #template_from_resource=, #template_scope=, #tooltip_markup, #tooltip_markup=, #tooltip_text, #tooltip_text=, #translate_coordinates, #trigger_tooltip_query, #unmap, #unparent, #unrealize, #unset_state_flags, #valign, #valign=, #vexpand, #vexpand=, #vexpand?, #vexpand_set, #vexpand_set=, #vexpand_set?, #visible, #visible=, #visible?, #width, #width_request, #width_request=

Constructor Details

#initialize(min, max, step) ⇒ Gtk::Widget

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 [methodGtk.SpinButton.set_digits] to correct it.

Parameters:

  • min (Float)

    Minimum allowable value

  • max (Float)

    Maximum allowable value

  • step (Float)

    Increment added or subtracted by spinning the widget

Instance Method Details

#activates_defaultBoolean

Retrieves the value set by [methodGtk.SpinButton.set_activates_default].

Returns:

  • (Boolean)

    true if the spin button will activate the default widget

#activates_default=(activates_default) ⇒ Boolean

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

See [signalGtk.SpinButton::activate] for what counts as activation.

Parameters:

  • activates_default (Boolean)

Returns:

  • (Boolean)

    activates-default

  • (Boolean)

    activates-default

#activates_default?Boolean

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

See [signalGtk.SpinButton::activate] for what counts as activation.

Returns:

  • (Boolean)

    activates-default

#adjustmentGtk::Adjustment

The adjustment that holds the value of the spin button.

Returns:

#adjustment=(adjustment) ⇒ Gtk::Adjustment

The adjustment that holds the value of the spin button.

Parameters:

Returns:

#climb_rateFloat

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

Returns:

  • (Float)

    climb-rate

#climb_rate=(climb_rate) ⇒ Float

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

Parameters:

  • climb_rate (Float)

Returns:

  • (Float)

    climb-rate

  • (Float)

    climb-rate

#configure(adjustment, climb_rate, digits) ⇒ nil

Changes the properties of an existing spin button.

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

Parameters:

  • adjustment (Gtk::Adjustment)

    a GtkAdjustment to replace the spin button’s existing adjustment, or nil to leave its current adjustment unchanged

  • climb_rate (Float)

    the new climb rate

  • digits (Integer)

    the number of decimal places to display in the spin button

Returns:

  • (nil)

#digitsInteger

The number of decimal places to display.

Returns:

  • (Integer)

    digits

#digits=(digits) ⇒ Integer

The number of decimal places to display.

Parameters:

  • digits (Integer)

Returns:

  • (Integer)

    digits

  • (Integer)

    digits

#get_increments(step, page) ⇒ nil

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

See [methodGtk.SpinButton.set_increments].

Parameters:

  • step (Float)

    location to store step increment

  • page (Float)

    location to store page increment

Returns:

  • (nil)

#get_range(min, max) ⇒ nil

Gets the range allowed for spin_button.

See [methodGtk.SpinButton.set_range].

Parameters:

  • min (Float)

    location to store minimum allowed value

  • max (Float)

    location to store maximum allowed value

Returns:

  • (nil)

#numericBoolean

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

Returns:

  • (Boolean)

    true if only numeric text can be entered

#numeric=(numeric) ⇒ Boolean

Whether non-numeric characters should be ignored.

Parameters:

  • numeric (Boolean)

Returns:

  • (Boolean)

    numeric

  • (Boolean)

    numeric

#numeric?Boolean

Whether non-numeric characters should be ignored.

Returns:

  • (Boolean)

    numeric

#set_increments(step, page) ⇒ nil

Sets the step and page increments for spin_button.

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

Parameters:

  • step (Float)

    increment applied for a button 1 press.

  • page (Float)

    increment applied for a button 2 press.

Returns:

  • (nil)

#set_range(min, max) ⇒ nil

Sets the minimum and maximum allowable values for spin_button.

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

Parameters:

  • min (Float)

    minimum allowable value

  • max (Float)

    maximum allowable value

Returns:

  • (nil)

#snap_to_ticksBoolean

Returns whether the values are corrected to the nearest step.

Returns:

  • (Boolean)

    true if values are snapped to the nearest step

#snap_to_ticks=(snap_to_ticks) ⇒ Boolean

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

Parameters:

  • snap_to_ticks (Boolean)

Returns:

  • (Boolean)

    snap-to-ticks

  • (Boolean)

    snap-to-ticks

#snap_to_ticks?Boolean

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

Returns:

  • (Boolean)

    snap-to-ticks

#spin(direction, increment) ⇒ nil

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

Parameters:

  • direction (Gtk::SpinType)

    a GtkSpinType indicating the direction to spin

  • increment (Float)

    step increment to apply in the specified direction

Returns:

  • (nil)

#updatenil

Manually force an update of the spin button.

Returns:

  • (nil)

#update_policyGtk::SpinButtonUpdatePolicy

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

Returns:

#update_policy=(update_policy) ⇒ Gtk::SpinButtonUpdatePolicy

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

Parameters:

Returns:

#valueFloat

The current value.

Returns:

  • (Float)

    value

#value=(value) ⇒ Float

The current value.

Parameters:

  • value (Float)

Returns:

  • (Float)

    value

  • (Float)

    value

#value_as_intInteger

Get the value spin_button represented as an integer.

Returns:

  • (Integer)

    the value of spin_button

#wrapBoolean

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

Returns:

  • (Boolean)

    true if the spin button wraps around

#wrap=(wrap) ⇒ Boolean

Whether a spin button should wrap upon reaching its limits.

Parameters:

  • wrap (Boolean)

Returns:

  • (Boolean)

    wrap

  • (Boolean)

    wrap

#wrap?Boolean

Whether a spin button should wrap upon reaching its limits.

Returns:

  • (Boolean)

    wrap