Struct gtk4::SpinButton

source ·
#[repr(transparent)]
pub struct SpinButton { /* private fields */ }
Expand description

A SpinButton 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 Entry, SpinButton 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 SpinButton are through an adjustment. See the Adjustment documentation for more details about an adjustment’s properties.

Note that SpinButton 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 property::Editable::width-chars to a value != -1.

Using a GtkSpinButton to get an integer

⚠️ The following code is in c ⚠️

// 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

⚠️ The following code is in c ⚠️

// 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

SpinButtons 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 Text 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

SpinButton uses the AccessibleRole::SpinButton role.

Implements

WidgetExt, glib::ObjectExt, AccessibleExt, BuildableExt, ConstraintTargetExt, AccessibleRangeExt, CellEditableExt, EditableExt, OrientableExt, WidgetExtManual, AccessibleExtManual, EditableExtManual

Implementations§

Creates a new SpinButton.

adjustment

the Adjustment that this spin button should use

climb_rate

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

the number of decimal places to display

Returns

The new SpinButton

Creates a new SpinButton with the given properties.

This is a convenience constructor that allows creation of a numeric SpinButton 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 set_digits() to correct it.

min

Minimum allowable value

max

Maximum allowable value

step

Increment added or subtracted by spinning the widget

Returns

The new SpinButton

Creates a new builder-pattern struct instance to construct SpinButton objects.

This method returns an instance of SpinButtonBuilder which can be used to create SpinButton objects.

Changes the properties of an existing spin button.

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

adjustment

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

climb_rate

the new climb rate

digits

the number of decimal places to display in the spin button

Get the adjustment associated with a SpinButton.

Returns

the Adjustment of @self

Returns the acceleration rate for repeated changes.

Returns

the acceleration rate

Fetches the precision of @self.

Returns

the current precision

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

See set_increments().

Returns
step

location to store step increment

page

location to store page increment

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

Returns

true if only numeric text can be entered

Gets the range allowed for @self.

See set_range().

Returns
min

location to store minimum allowed value

max

location to store maximum allowed value

Returns whether the values are corrected to the nearest step.

Returns

true if values are snapped to the nearest step

Gets the update behavior of a spin button.

See set_update_policy().

Returns

the current update policy

Get the value in the @self.

Returns

the value of @self

Get the value @self represented as an integer.

Returns

the value of @self

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

true if the spin button wraps around

Replaces the Adjustment associated with @self.

adjustment

a Adjustment to replace the existing adjustment

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

climb_rate

the rate of acceleration, must be >= 0

Set the precision to be displayed by @self.

Up to 20 digit precision is allowed.

digits

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

Sets the step and page increments for spin_button.

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

step

increment applied for a button 1 press.

page

increment applied for a button 2 press.

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

numeric

flag indicating if only numeric entry is allowed

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.

min

minimum allowable value

max

maximum allowable value

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.

snap_to_ticks

a flag indicating if invalid values should be corrected

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.

policy

a SpinButtonUpdatePolicy value

Sets the value of @self.

value

the new value

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.

wrap

a flag indicating if wrapping behavior is performed

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

direction

a SpinType indicating the direction to spin

increment

step increment to apply in the specified direction

Manually force an update of the spin button.

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.

scroll

a ScrollType to specify the speed and amount of change

Emitted to tweak the formatting of the value for display.

⚠️ The following code is in c ⚠️

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

   adjustment = gtk_spin_button_get_adjustment (spin);
   value = (int)gtk_adjustment_get_value (adjustment);
   text = g_strdup_printf ("%02d", value);
   gtk_editable_set_text (GTK_EDITABLE (spin), text):
   g_free (text);

   return TRUE;
}
Returns

true if the value has been displayed

Emitted when the value is changed.

Also see the signal::SpinButton::output signal.

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

Emitted to convert the users input into a double value.

The signal handler is expected to use EditableExt::text() to retrieve the text of the spinbutton and set @new_value to the new value.

The default conversion uses g_strtod().

Returns

true for a successful conversion, false if the input was not handled, and GTK_INPUT_ERROR if the conversion failed.

new_value

return location for the new value

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Returns the type identifier of Self.

Auto Trait Implementations§

Blanket Implementations§

Updates an array of accessible properties. Read more
Updates an array of accessible relations. Read more
Updates an array of accessible states. Read more
Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Upcasts an object to a superclass or interface T. Read more
Upcasts an object to a reference of its superclass or interface T. Read more
Tries to downcast to a subclass or interface implementor T. Read more
Tries to downcast to a reference of its subclass or interface implementor T. Read more
Tries to cast to an object of type T. This handles upcasting, downcasting and casting between interface and interface implementors. All checks are performed at runtime, while downcast and upcast will do many checks at compile-time already. Read more
Tries to cast to reference to an object of type T. This handles upcasting, downcasting and casting between interface and interface implementors. All checks are performed at runtime, while downcast and upcast will do many checks at compile-time already. Read more
Casts to T unconditionally. Read more
Casts to &T unconditionally. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Returns true if the object is an instance of (can be cast to) T.
Returns the type of the object.
Returns the ObjectClass of the object. Read more
Returns the class of the object.
Returns the class of the object in the given type T. Read more
Returns the interface T of the object. Read more
Sets the property property_name of the object to value value. Read more
Sets the property property_name of the object to value value. Read more
Sets multiple properties of the object at once. Read more
Sets multiple properties of the object at once. Read more
Gets the property property_name of the object and cast it to the type V. Read more
Gets the property property_name of the object. Read more
Check if the object has a property property_name of the given type_. Read more
Get the type of the property property_name of this object. Read more
Get the ParamSpec of the property property_name of this object.
Return all ParamSpec of the properties of this object.
Freeze all property notifications until the return guard object is dropped. Read more
Set arbitrary data on this object with the given key. Read more
Return previously set arbitrary data of this object with the given key. Read more
Retrieve previously set arbitrary data of this object with the given key. Read more
Set arbitrary data on this object with the given key. Read more
Return previously set arbitrary data of this object with the given key. Read more
Retrieve previously set arbitrary data of this object with the given key. Read more
Block a given signal handler. Read more
Unblock a given signal handler.
Stop emission of the currently emitted signal.
Stop emission of the currently emitted signal by the (possibly detailed) signal name.
Connect to the signal signal_name on this object. Read more
Connect to the signal signal_id on this object. Read more
Connect to the signal signal_name on this object. Read more
Connect to the signal signal_id on this object. Read more
Connect to the signal signal_name on this object. Read more
Connect to the signal signal_id on this object. Read more
Connect a closure to the signal signal_name on this object. Read more
Connect a closure to the signal signal_id on this object. Read more
Limits the lifetime of closure to the lifetime of the object. When the object’s reference count drops to zero, the closure will be invalidated. An invalidated closure will ignore any calls to invoke_with_values, or invoke when using Rust closures.
Emit signal by signal id. Read more
Same as Self::emit but takes Value for the arguments.
Emit signal by its name. Read more
Emit signal by its name. Read more
Emit signal by its name with details. Read more
Emit signal by its name with details. Read more
Emit signal by signal id with details. Read more
Emit signal by signal id with details. Read more
Disconnect a previously connected signal handler.
Connect to the notify signal of the object. Read more
Connect to the notify signal of the object. Read more
Connect to the notify signal of the object. Read more
Notify that the given property has changed its value. Read more
Notify that the given property has changed its value. Read more
Downgrade this object to a weak reference.
Add a callback to be notified when the Object is disposed.
Add a callback to be notified when the Object is disposed. Read more
Bind property source_property on this object to the target_property on the target object. Read more
Returns the strong reference count of this object.
Runs the dispose mechanism of the object. Read more
Ensures that the type has been registered with the type system.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.