Table of Contents
- xtk_spinbutton.h
- Types
- Functions
- xtk_spinbutton_create
- xtk_spinbutton_destroy
- xtk_spinbutton_draw
- xtk_spinbutton_set_value
- xtk_spinbutton_get_value
- xtk_spinbutton_set_enabled
- xtk_spinbutton_get_enabled
- xtk_spinbutton_show
- xtk_spinbutton_hide
- xtk_spinbutton_set_range
- xtk_spinbutton_set_float_scale
- xtk_spinbutton_get_window
- xtk_spinbutton_handle_event
- xtk_spinbutton_draw_frame
- xtk_spinbutton_set_position
- xtk_spinbutton_get_size
- xtk_spinbutton_update_repeat
- xtk_spinbutton_update_blink
- xtk_spinbutton_set_callback
- xtk_spinbutton_unfocus_all
- xtk_spinbutton_commit_focused
- xtk_spinbutton_register_focus_callback
- xtk_spinbutton_is_focused
- xtk_spinbutton_set_focused
- xtk_spinbutton_set_theme
- xtk_spinbutton_config_init_defaults
- xtk_spinbutton_config_parse_styling
- xtk_spinbutton_config_write_styling
- xtk_spinbutton_widget_config_init_defaults
- xtk_spinbutton_widget_config_parse
- xtk_spinbutton_widget_config_write
- xtk_spinbutton_set_button_hover
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
xtk_spinbutton.h
A number entry with + and − buttons.
An XtkSpinButton is a small text field for a whole number, with a + button above a − button at its right. The value stays between a minimum and a maximum and moves by a step. The user can type a number, click the buttons (holding one down repeats), use the arrow keys or the mouse wheel. The field has the usual editing keys, a selection, the clipboard, and a context menu (Cut, Copy, Paste, Select All, Clear). In float mode (xtk_spinbutton_set_float_scale()) the whole number is shown divided by a scale, so 764 with a scale of 1000 shows as "0.764".
Creating a spin button. xtk_spinbutton_create() takes the whole XtkConfig (copied), an XtkClipboard for copy and paste, the position, the width (0 for config.spinbutton_widget.width), and the range and step. The first value is the minimum. The height is [spinbutton-widget] height, or more when the font needs more. The window is not mapped until xtk_spinbutton_set_position() or xtk_spinbutton_show(). The program frees the spin button with xtk_spinbutton_destroy(); the clipboard stays the program's.
Events. The program passes every XEvent to xtk_spinbutton_handle_event(), and calls xtk_spinbutton_update_repeat() and xtk_spinbutton_update_blink() from a regular timer tick (for the held-down buttons and the blinking cursor). A click in the text gives it the X input focus. With the focus:
- Up and Down add or take away one step, Page Up and Page Down ten steps, from the number shown: text being typed is taken first.
- Digits (and a minus sign at the start when the minimum is below 0; a decimal point in float mode) are typed into the text. Left, Right, Home, End (with Shift to select), Backspace and Delete edit it.
- Ctrl+A selects all, Ctrl+C, Ctrl+X and Ctrl+V use the clipboard; a middle click pastes the primary selection.
- Return takes the typed text as the value, clamped to the range. Losing the X focus does the same; when the focus goes to another window that is not this one's parent (another widget or program), the spin button also stops showing the focus.
- Tab is not used, so a focus manager can move on.
Typed text is not the value until it is taken (Return, focus loss, xtk_spinbutton_commit_focused()). The mouse wheel anywhere over the widget steps the value. A click on + or − steps once on release; held for 400 ms, it repeats every 75 ms (through xtk_spinbutton_update_repeat()).
The callback (xtk_spinbutton_set_callback()) runs after every change of the value made by the user: typing taken with Return or focus loss, paste, the keys, the wheel, the buttons and their repeat. It does not run for xtk_spinbutton_set_value() or xtk_spinbutton_set_range().
Only one spin button shows the focus at a time, and the spin buttons and xtk_entry fields unfocus each other (see xtk_spinbutton_unfocus_all() and xtk_spinbutton_register_focus_callback()).
Theme keys. [spinbutton]:
background(default#262626): the field.hover-background(#333333): the field while the pointer is over it.hover-border(#808080): the frame while the pointer is over it.border(#808080): the frame and the lines between the field and the buttons.focus-border(the toolkit's focus color): the frame while it has the focus.color(#F2F2F2): the number.button-hover-background(#405980): a button under the pointer.button-color(#F2F2F2): the + and − signs.font-family(orfont,DejaVu Sans) andfont-size(12). The signs always use 12 pixels of the same family.border-width(1) andborder-radius(4): the frame; 0 for none, or for square corners.padding(4): the space between the frame and the number.button-width(0): the width of the + and − column; 0 uses[spinbutton-widget]button-width.
background, hover-background and button-hover-background also accept a CSS gradient.
[spinbutton-widget]: width (120), button-width (16, the width of the
- and − column) and
height(24, the least height; a larger font makes the spin button taller).
The spin button also uses other sections: the selection colors of [main] (selection, selection-text), the text cursor's color (cursor-color in [main] or [behavior]), its blink time ([behavior] cursor-blink-ms, default 600 ms), and [menu] for its context menu.
Example.
static XtkSpinButton *width_spin;
static void on_width(void *user_data) {
(void)user_data;
printf("Width: %d\n", xtk_spinbutton_get_value(width_spin));
}
XtkConfig config;
xtk_config_init_defaults(&config);
XtkTheme *theme = xtk_theme_new();
if (theme && xtk_theme_builtin("Dark", theme)) xtk_theme_apply(&config, theme);
xtk_theme_free(theme);
width_spin = xtk_spinbutton_create(display, DefaultScreen(display), parent, &config,
clipboard, 0, 0, 0, 1, 500, 1);
xtk_spinbutton_set_value(width_spin, 80);
xtk_spinbutton_set_callback(width_spin, on_width, NULL);
xtk_spinbutton_set_position(width_spin, 10, 10); // maps it
while (running) {
while (XPending(display)) {
XEvent event;
XNextEvent(display, &event);
xtk_spinbutton_handle_event(width_spin, &event);
}
xtk_spinbutton_update_repeat(width_spin);
xtk_spinbutton_update_blink(width_spin);
usleep(10000);
}
xtk_spinbutton_destroy(width_spin);
*
Limitations.
- The value is an int; float mode only changes how it is shown and typed.
- The text holds at most 11 characters (15 in float mode).
#include "xtk_spinbutton.h"
Types: XtkSpinButton
Functions: xtk_spinbutton_create, xtk_spinbutton_destroy, xtk_spinbutton_draw, xtk_spinbutton_set_value, xtk_spinbutton_get_value, xtk_spinbutton_set_enabled, xtk_spinbutton_get_enabled, xtk_spinbutton_show, xtk_spinbutton_hide, xtk_spinbutton_set_range, xtk_spinbutton_set_float_scale, xtk_spinbutton_get_window, xtk_spinbutton_handle_event, xtk_spinbutton_draw_frame, xtk_spinbutton_set_position, xtk_spinbutton_get_size, xtk_spinbutton_update_repeat, xtk_spinbutton_update_blink, xtk_spinbutton_set_callback, xtk_spinbutton_unfocus_all, xtk_spinbutton_commit_focused, xtk_spinbutton_register_focus_callback, xtk_spinbutton_is_focused, xtk_spinbutton_set_focused, xtk_spinbutton_set_theme, xtk_spinbutton_config_init_defaults, xtk_spinbutton_config_parse_styling, xtk_spinbutton_config_write_styling, xtk_spinbutton_widget_config_init_defaults, xtk_spinbutton_widget_config_parse, xtk_spinbutton_widget_config_write, xtk_spinbutton_set_button_hover
Types
XtkSpinButton
typedef struct XtkSpinButton XtkSpinButton;
A number entry with + and − buttons. The structure is private; make one with xtk_spinbutton_create().
Functions
xtk_spinbutton_create
XtkSpinButton *xtk_spinbutton_create(
Display *display,
int screen,
Window parent,
const XtkConfig *theme,
XtkClipboard *clipboard,
int x,
int y,
int width,
int min_value,
int max_value,
int step);
Create a spin button.
Its window is not mapped until xtk_spinbutton_set_position() or xtk_spinbutton_show().
| Parameter | Description |
|---|---|
display |
The X display. |
screen |
The screen number. |
parent |
The window the spin button sits in. |
theme |
The configuration; copied (see the theme keys above). |
clipboard |
The clipboard for copy and paste, or NULL for none. The program keeps it alive as long as the spin button. |
x |
The x position in the parent, in pixels. |
y |
The y position in the parent, in pixels. |
width |
The width in pixels, or 0 (or less) for theme->spinbutton_widget.width. |
min_value |
The smallest value; also the first value. |
max_value |
The largest value. |
step |
The amount one click or arrow key adds or takes away; 0 or less means 1. |
Returns: The new spin button, or NULL if display or theme is NULL, memory runs out, or the window or the font cannot be made.
xtk_spinbutton_destroy
void xtk_spinbutton_destroy(XtkSpinButton *spinbutton);
Destroy a spin button: its window, context menu, fonts and memory.
| Parameter | Description |
|---|---|
spinbutton |
The spin button, or NULL (then nothing happens). |
xtk_spinbutton_draw
void xtk_spinbutton_draw(XtkSpinButton *spinbutton);
Redraw the spin button now.
The spin button redraws itself on Expose and on every change. Call this after an ancestor window is resized or reparented, when no Expose may come for its own window.
| Parameter | Description |
|---|---|
spinbutton |
The spin button, or NULL. |
xtk_spinbutton_set_value
void xtk_spinbutton_set_value(XtkSpinButton *spinbutton, int value);
Set the value, and redraw.
The value is clamped to the range and the text shows it. The callback does not run.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
value |
The new value (in float mode, the scaled whole number). |
xtk_spinbutton_get_value
int xtk_spinbutton_get_value(const XtkSpinButton *spinbutton);
The current value.
Text being typed is not part of it until it is taken (Return, focus loss or xtk_spinbutton_commit_focused()).
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
Returns: The value, or 0 if spinbutton is NULL.
xtk_spinbutton_set_enabled
void xtk_spinbutton_set_enabled(XtkSpinButton *spinbutton, int enabled);
Enable or disable the spin button, and redraw.
A disabled spin button shows only its number, in muted colors (the field 85% background and 15% color, the text 40% color), and handles only Expose. Spin buttons start enabled.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
enabled |
Nonzero to enable, 0 to disable. |
xtk_spinbutton_get_enabled
int xtk_spinbutton_get_enabled(const XtkSpinButton *spinbutton);
Whether the spin button is enabled.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
Returns: 1 if enabled, 0 if disabled or if spinbutton is NULL.
xtk_spinbutton_show
void xtk_spinbutton_show(XtkSpinButton *spinbutton);
Show the spin button: map its window.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
xtk_spinbutton_hide
void xtk_spinbutton_hide(XtkSpinButton *spinbutton);
Hide the spin button: unmap its window.
It stays unmapped when xtk_spinbutton_set_position() moves it, until xtk_spinbutton_show().
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
xtk_spinbutton_set_range
void xtk_spinbutton_set_range(
XtkSpinButton *spinbutton,
int min_value,
int max_value,
int step);
Change the range and the step.
A value outside the new range moves to the nearer end (and redraws); the callback does not run.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
min_value |
The smallest value. |
max_value |
The largest value. |
step |
The amount one click or arrow key adds or takes away; 0 or less means 1. |
xtk_spinbutton_set_float_scale
void xtk_spinbutton_set_float_scale(
XtkSpinButton *spinbutton,
double scale,
int decimals);
Show the value as a decimal number, or as a whole number again.
In float mode the value stays a whole number, shown divided by scale: with a scale of 1000 and 3 decimals, 764 shows as "0.764", and typing "0.5" gives 500. The range and the step are in the same scaled units. Redraws.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
scale |
The number to divide by, or 0 (or less) for whole numbers. |
decimals |
The number of decimal places shown; 0 shows whole numbers; less than 0 means 3. |
xtk_spinbutton_get_window
Window xtk_spinbutton_get_window(XtkSpinButton *spinbutton);
The spin button's X window.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
Returns: The window, or None if spinbutton is NULL.
xtk_spinbutton_handle_event
int xtk_spinbutton_handle_event(XtkSpinButton *spinbutton, XEvent *event);
Handle an X event.
While its context menu is open, the menu gets the event first. Other events for other windows return 0. A disabled spin button handles only Expose.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
event |
The event. |
Returns: 1 if the spin button (or its menu) used the event, including Expose; 0 otherwise.
xtk_spinbutton_draw_frame
void xtk_spinbutton_draw_frame(XtkSpinButton *spinbutton);
Redraw the spin button; the same as xtk_spinbutton_draw().
| Parameter | Description |
|---|---|
spinbutton |
The spin button, or NULL. |
xtk_spinbutton_set_position
void xtk_spinbutton_set_position(XtkSpinButton *spinbutton, int x, int y);
Move the spin button within its parent.
This also maps its window, unless it is hidden with xtk_spinbutton_hide().
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
x |
The new x position in the parent, in pixels. |
y |
The new y position in the parent, in pixels. |
xtk_spinbutton_get_size
void xtk_spinbutton_get_size(const XtkSpinButton *spinbutton, int *width, int *height);
The spin button's size.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. If it is NULL, both outputs are set to 0. |
width |
Receives the width in pixels; may be NULL. |
height |
Receives the height in pixels; may be NULL. |
xtk_spinbutton_update_repeat
void xtk_spinbutton_update_repeat(XtkSpinButton *spinbutton);
Repeat the step while + or − is held down.
Call it from a regular timer tick. It does nothing unless a button is held; the first repeat comes after 400 ms, the next ones every 75 ms, and each one that changes the value runs the callback.
| Parameter | Description |
|---|---|
spinbutton |
The spin button, or NULL. |
xtk_spinbutton_update_blink
void xtk_spinbutton_update_blink(XtkSpinButton *spinbutton);
Blink the text cursor.
Call it from a regular timer tick. It does nothing unless the spin button has the focus, and redraws only when the cursor appears or disappears.
| Parameter | Description |
|---|---|
spinbutton |
The spin button, or NULL. |
xtk_spinbutton_set_callback
void xtk_spinbutton_set_callback(
XtkSpinButton *spinbutton,
void(*)(void *user_data)callback,
void *user_data);
Set or remove the callback that runs when the user changes the value.
It runs after typing is taken (Return or focus loss), a paste, Up, Down, Page Up, Page Down, the wheel, a click on + or −, and each auto-repeat step, when the value really changed. It does not run for xtk_spinbutton_set_value(). It may read or set the value, but must not destroy the spin button.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
callback |
The callback, or NULL to remove it. It gets only user_data; read the value with xtk_spinbutton_get_value(). |
user_data |
A pointer passed to the callback unchanged. |
xtk_spinbutton_unfocus_all
void xtk_spinbutton_unfocus_all(void);
Take the focus look away from the spin button that has it.
xtk_entry calls it when an entry takes the focus, so that only one text cursor shows. It does not take the typed text as the value.
xtk_spinbutton_commit_focused
void xtk_spinbutton_commit_focused(void);
Take the typed text of the focused spin button as its value.
A click on a window that does not take the X focus (a slider, a color area) gives the spin button no FocusOut, so its typed text would stay untaken. A program calls this on every ButtonPress before passing the event on. It keeps the focus where it is, and runs the callback if the value changes.
xtk_spinbutton_register_focus_callback
void xtk_spinbutton_register_focus_callback(void(*)(void)callback);
Add a function that runs whenever any spin button takes the focus.
Other text widgets use it to drop their own focus look. At most 8 functions can be added; more are ignored.
| Parameter | Description |
|---|---|
callback |
The function; adding the same one twice has no effect. |
xtk_spinbutton_is_focused
int xtk_spinbutton_is_focused(const XtkSpinButton *spinbutton);
Whether the spin button has the focus (shows its cursor).
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
Returns: 1 if it has the focus, 0 if not or if spinbutton is NULL.
xtk_spinbutton_set_focused
void xtk_spinbutton_set_focused(XtkSpinButton *spinbutton, int focused);
Give the spin button the focus, or take its focus look away.
For a program that moves focus with Tab. With focused nonzero it does what a click in the text does: the other spin buttons and entries lose their focus look, the focus callbacks run, the spin button takes the X input focus (if its window is viewable) and its cursor starts blinking. With 0 it only clears the focus look; the X focus moves when another widget takes it.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
focused |
Nonzero to take the focus, 0 to clear the focus look. |
xtk_spinbutton_set_theme
void xtk_spinbutton_set_theme(XtkSpinButton *spinbutton, const XtkConfig *theme);
Give the spin button new colors and a new font, and redraw.
Call it after the theme changes. See the limitations above.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
theme |
The configuration; copied. NULL does nothing. |
xtk_spinbutton_config_init_defaults
void xtk_spinbutton_config_init_defaults(SpinButtonBlock *spinbutton_config);
Fill a SpinButtonBlock with the default [spinbutton] colors and font.
xtk_config_init_defaults() already calls it for config.spinbutton.
| Parameter | Description |
|---|---|
spinbutton_config |
The block to fill; NULL does nothing. |
xtk_spinbutton_config_parse_styling
int xtk_spinbutton_config_parse_styling(
SpinButtonBlock *spinbutton_config,
const char *key,
const char *value);
Set one [spinbutton] key in a SpinButtonBlock.
| Parameter | Description |
|---|---|
spinbutton_config |
The block to change. |
key |
The key name, such as hover-background. |
value |
The value as written in the file. |
Returns: 1 if the key is known and was set, 0 if the key is unknown or an argument is NULL (the convention of every section handler, see xtk_config_registry.h).
xtk_spinbutton_config_write_styling
int xtk_spinbutton_config_write_styling(
FILE *file,
const SpinButtonBlock *spinbutton_config);
Write a SpinButtonBlock as a [spinbutton] section.
| Parameter | Description |
|---|---|
file |
The file to write to. |
spinbutton_config |
The block to write. |
Returns: 0 on success, -1 if an argument is NULL.
xtk_spinbutton_widget_config_init_defaults
void xtk_spinbutton_widget_config_init_defaults(XtkConfig *config);
Set config.spinbutton_widget to the default [spinbutton-widget] values.
| Parameter | Description |
|---|---|
config |
The configuration; NULL does nothing. |
xtk_spinbutton_widget_config_parse
int xtk_spinbutton_widget_config_parse(
XtkConfig *config,
const char *key,
const char *value);
Set one [spinbutton-widget] key in config.spinbutton_widget.
| Parameter | Description |
|---|---|
config |
The configuration. |
key |
The key name: width, height or button-width. |
value |
The value as written in the file. |
Returns: 1 if the key is known and was set, 0 if the key is unknown or an argument is NULL (the convention of every section handler, see xtk_config_registry.h).
xtk_spinbutton_widget_config_write
int xtk_spinbutton_widget_config_write(FILE *file, const XtkConfig *config);
Write config.spinbutton_widget as a [spinbutton-widget] section.
| Parameter | Description |
|---|---|
file |
The file to write to. |
config |
The configuration. |
Returns: 0 on success, -1 if an argument is NULL.
xtk_spinbutton_set_button_hover
void xtk_spinbutton_set_button_hover(XtkSpinButton *spinbutton, int which);
Show the + or − button in its hover color without the pointer.
For a program that lights the spin button together with something else. Redraws if it changes; the next pointer motion over the widget sets it again.
| Parameter | Description |
|---|---|
spinbutton |
The spin button. |
which |
1 for the + button, 2 for the − button, 0 for neither. |
xtk API
Widgets
- button
- checkbox
- radiobutton
- label
- entry
- textview
- edit_history
- spinbutton
- slider
- progressbar
- combobox
- scrollbar
- swatch
- panel
- statusbar
- tooltip
- toolbar
- icongrid
- iconview
- pathbar
- treeview
- datagrid
- graph
- scheme_wheel
- zoom
- tray
Containers and menus
Dialogs
Rich text
Drawing and images
Color
Configuration and themes
Input and the X server
Safety
Data formats