1 xtk_radiobutton
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_radiobutton.h

Radio buttons: one choice out of a group.

An XtkRadioButton is a small circle with an optional label beside it; the selected one shows a dot. Radio buttons that belong to the same XtkRadioButtonGroup are exclusive: selecting one clears the one selected before, so at most one in the group is selected.

Creating radio buttons. First make the group with xtk_radiobutton_group_create(), then each button with xtk_radiobutton_create(), passing the group. The create call takes the whole XtkConfig (it copies it and reads the radiobutton block), a position and the label. The circle is circle-size pixels across (16); the widget is 4 pixels higher, or as high as the label's font when that is higher, and as wide as the circle, the label-gap (6) and the label need. Its window has the parent's background (ParentRelative), so only the circle is filled. The window is not mapped until xtk_radiobutton_set_position() or xtk_radiobutton_show(). No button is selected at first; select the default choice with xtk_radiobutton_set_selected().

The program owns the buttons and the group. A button destroyed with xtk_radiobutton_destroy() leaves its group; a group destroyed with xtk_radiobutton_group_destroy() lets go of its buttons, which then stand alone. Either may go first.

Events. The program passes every XEvent to each button's xtk_radiobutton_handle_event(). It returns 1 when the event was for that button and 0 otherwise. A Button1 press and release both on a button (a release after the pointer was dragged off it does nothing), or Space while it has the keyboard focus, selects it. Up and Left move the keyboard focus to the previous button of the group, Down and Right to the next one, without changing the selection: every radio button is its own Tab stop. A mouse press gives the button the X input focus.

When the selection of a group changes to another button, the group's callback runs (xtk_radiobutton_group_set_changed_callback()). It runs for a change from code (xtk_radiobutton_set_selected()) too, but not when a button that is already selected is clicked, and not when a button is unselected.

Theme keys. [radiobutton]:

  • color (default #D9D9D9): the label text.
  • background (#2E2E2E): the inside of the circle only; it also accepts a CSS gradient.
  • border (#897C4F): the circle when not selected.
  • hover-border (#BCAF82): the circle while the pointer is over it or it is pressed.
  • focus-border (the toolkit's focus color): the circle while it has keyboard focus.
  • fill-foreground (#D9D9D9): the dot, and the circle while selected.
  • font-family (or font, DejaVu Sans) and font-size (10).
  • circle-size (16), border-width (1) and label-gap (6), in pixels: the circle's diameter, its border and the gap between it and the label.

Example.

static void on_speed(XtkRadioButtonGroup *group, XtkRadioButton *selected, void *user_data) {
    (void)group;
    XtkRadioButton **fast = user_data;
    printf("Speed: %s\n", selected == *fast ? "fast" : "slow");
}

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);

XtkRadioButtonGroup *speed = xtk_radiobutton_group_create();
XtkRadioButton *slow = xtk_radiobutton_create(display, parent, &config, 10, 10, "Slow", speed);
XtkRadioButton *fast = xtk_radiobutton_create(display, parent, &config, 10, 34, "Fast", speed);
xtk_radiobutton_set_selected(slow, 1);
xtk_radiobutton_group_set_changed_callback(speed, on_speed, &fast);
xtk_radiobutton_show(slow);
xtk_radiobutton_show(fast);

XEvent event;
while (running) {
    XNextEvent(display, &event);
    xtk_radiobutton_handle_event(slow, &event);
    xtk_radiobutton_handle_event(fast, &event);
}
xtk_radiobutton_destroy(slow);
xtk_radiobutton_destroy(fast);
xtk_radiobutton_group_destroy(speed);
 * 

Limitations.

#include "xtk_radiobutton.h"

Types: XtkRadioButton, XtkRadioButtonGroup, XtkRadioButtonGroupChangedCallback

Functions: xtk_radiobutton_group_create, xtk_radiobutton_group_destroy, xtk_radiobutton_group_get_selected, xtk_radiobutton_group_set_changed_callback, xtk_radiobutton_group_set_enabled, xtk_radiobutton_create, xtk_radiobutton_destroy, xtk_radiobutton_draw, xtk_radiobutton_get_window, xtk_radiobutton_set_enabled, xtk_radiobutton_set_underline, xtk_radiobutton_get_enabled, xtk_radiobutton_set_label, xtk_radiobutton_set_label_left, xtk_radiobutton_get_label_left, xtk_radiobutton_set_selected, xtk_radiobutton_is_selected, xtk_radiobutton_set_position, xtk_radiobutton_get_size, xtk_radiobutton_handle_event, xtk_radiobutton_set_focused, xtk_radiobutton_get_focused, xtk_radiobutton_get_focus_window, xtk_radiobutton_set_theme, xtk_radiobutton_config_init_defaults, xtk_radiobutton_config_parse_styling, xtk_radiobutton_config_write_styling, xtk_radiobutton_show, xtk_radiobutton_hide

Types

XtkRadioButton

typedef struct XtkRadioButton XtkRadioButton;

A radio button. The structure is private; make one with xtk_radiobutton_create().

XtkRadioButtonGroup

typedef struct XtkRadioButtonGroup XtkRadioButtonGroup;

A set of radio buttons of which at most one is selected. The structure is private; make one with xtk_radiobutton_group_create().

XtkRadioButtonGroupChangedCallback

typedef void (*XtkRadioButtonGroupChangedCallback)(XtkRadioButtonGroup *group, XtkRadioButton *selected, void *user_data);

Called when another button of the group becomes the selected one.

It runs after the buttons are redrawn, so it may destroy widgets.

Functions

xtk_radiobutton_group_create

XtkRadioButtonGroup *xtk_radiobutton_group_create(void);

Create an empty radio button group.

Returns: The new group, or NULL if memory runs out.

xtk_radiobutton_group_destroy

void xtk_radiobutton_group_destroy(XtkRadioButtonGroup *group);

Free a group.

Its buttons are not destroyed: they leave the group and stand alone, so they may be destroyed before or after it.

Parameter Description
group The group, or NULL (then nothing happens).

xtk_radiobutton_group_get_selected

XtkRadioButton *xtk_radiobutton_group_get_selected(const XtkRadioButtonGroup *group);

The selected button of a group.

Parameter Description
group The group.

Returns: The selected button, or NULL if none is selected or group is NULL.

xtk_radiobutton_group_set_changed_callback

void xtk_radiobutton_group_set_changed_callback(
    XtkRadioButtonGroup *group,
    XtkRadioButtonGroupChangedCallback changed,
    void *user_data);

Set or remove the group's selection-changed callback.

It runs from xtk_radiobutton_set_selected() (and so from a click or Space) only when a button that was not selected becomes selected.

Parameter Description
group The group.
changed The callback, or NULL to remove it.
user_data A pointer passed to the callback unchanged.

xtk_radiobutton_group_set_enabled

void xtk_radiobutton_group_set_enabled(XtkRadioButtonGroup *group, int enabled);

Enable or disable every button now in the group.

The same as calling xtk_radiobutton_set_enabled() on each member. Buttons added later are not changed, and a single member can still be set on its own afterwards.

Parameter Description
group The group.
enabled Nonzero to enable, 0 to disable.

xtk_radiobutton_create

XtkRadioButton *xtk_radiobutton_create(
    Display *display,
    Window parent,
    const XtkConfig *theme,
    int x,
    int y,
    const char *label,
    XtkRadioButtonGroup *group);

Create a radio button.

It starts unselected and enabled. Its window is not mapped until xtk_radiobutton_set_position() or xtk_radiobutton_show().

Parameter Description
display The X display.
parent The window the button sits in.
theme The configuration; it is copied, and its radiobutton block gives the colors and the font.
x The x position in the parent, in pixels.
y The y position in the parent, in pixels.
label The label text, or NULL for none; copied.
group The group the button joins, or NULL for a button on its own (a click selects it, and only code unselects it).

Returns: The new radio button, or NULL if display or theme is NULL, memory runs out, or the window or the font cannot be made.

xtk_radiobutton_destroy

void xtk_radiobutton_destroy(XtkRadioButton *radiobutton);

Destroy a radio button: it leaves its group, and its window, font and memory are freed.

If it was the selected one, the group has no selection afterwards (the callback does not run).

Parameter Description
radiobutton The button, or NULL (then nothing happens).

xtk_radiobutton_draw

void xtk_radiobutton_draw(XtkRadioButton *radiobutton);

Redraw the radio button now.

The 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 the button's own window.

Parameter Description
radiobutton The button, or NULL.

xtk_radiobutton_get_window

Window xtk_radiobutton_get_window(XtkRadioButton *radiobutton);

The radio button's X window.

Parameter Description
radiobutton The button.

Returns: The window, or None if radiobutton is NULL.

xtk_radiobutton_set_enabled

void xtk_radiobutton_set_enabled(XtkRadioButton *radiobutton, int enabled);

Enable or disable the radio button, and redraw.

A disabled button draws its circle, dot and label dimmed (blended toward background) and ignores the mouse and keyboard. Disabling clears the hover, pressed and focus looks; the selection stays. Buttons start enabled.

Parameter Description
radiobutton The button.
enabled Nonzero to enable, 0 to disable.

xtk_radiobutton_set_underline

void xtk_radiobutton_set_underline(XtkRadioButton *radiobutton, char letter);

Underline a letter of the label, as the mark of a dialog's Alt+letter key for this radio button.

Only the mark is drawn: the dialog handles the key itself.

Parameter Description
radiobutton The button.
letter The letter (either case), or 0 for none.

xtk_radiobutton_get_enabled

int xtk_radiobutton_get_enabled(const XtkRadioButton *radiobutton);

Whether the radio button is enabled.

Parameter Description
radiobutton The button.

Returns: 1 if enabled, 0 if disabled or if radiobutton is NULL.

xtk_radiobutton_set_label

void xtk_radiobutton_set_label(XtkRadioButton *radiobutton, const char *label);

Change the label, and redraw.

The window is resized to fit the new label; NULL or "" leaves the circle alone.

Parameter Description
radiobutton The button.
label The new text, or NULL for none; copied.

xtk_radiobutton_set_label_left

void xtk_radiobutton_set_label_left(XtkRadioButton *radiobutton, int left);

Put the label before the circle ("Fast ( )") or after it ("( ) Fast", the default), and redraw.

The widget keeps its width; only the circle and the label change places.

Parameter Description
radiobutton The button.
left Nonzero: label first, circle at the right edge. 0: circle first.

xtk_radiobutton_get_label_left

int xtk_radiobutton_get_label_left(const XtkRadioButton *radiobutton);

Whether the label comes before the circle.

Parameter Description
radiobutton The button.

Returns: 1 if the label comes first, 0 if the circle does or if radiobutton is NULL.

xtk_radiobutton_set_selected

void xtk_radiobutton_set_selected(XtkRadioButton *radiobutton, int selected);

Select or unselect the radio button, and redraw.

Selecting it unselects the button of its group that was selected, and runs the group's callback if the button was not selected before. Unselecting it leaves the group with no selection and runs no callback.

Parameter Description
radiobutton The button.
selected Nonzero to select, 0 to unselect.

xtk_radiobutton_is_selected

int xtk_radiobutton_is_selected(const XtkRadioButton *radiobutton);

Whether the radio button is selected.

Parameter Description
radiobutton The button.

Returns: 1 if selected, 0 if not or if radiobutton is NULL.

xtk_radiobutton_set_position

void xtk_radiobutton_set_position(XtkRadioButton *radiobutton, int x, int y);

Move the radio button within its parent.

This also maps its window, unless it is hidden with xtk_radiobutton_hide().

Parameter Description
radiobutton The button.
x The new x position in the parent, in pixels.
y The new y position in the parent, in pixels.

xtk_radiobutton_get_size

void xtk_radiobutton_get_size(const XtkRadioButton *radiobutton, int *w, int *h);

The radio button's current size.

Parameter Description
radiobutton The button. If it is NULL, both outputs are set to 0.
w Receives the width in pixels; may be NULL.
h Receives the height in pixels; may be NULL.

xtk_radiobutton_handle_event

int xtk_radiobutton_handle_event(XtkRadioButton *radiobutton, const XEvent *event);

Handle an X event.

Events for other windows return 0 at once. A disabled button handles only Expose.

Parameter Description
radiobutton The button.
event The event.

Returns: 1 if the event was for this button and it used it (expose, hover, press, release, focus, Space, or an arrow key in a group of two or more); 0 otherwise.

xtk_radiobutton_set_focused

void xtk_radiobutton_set_focused(XtkRadioButton *radiobutton, int focused);

Give the radio button keyboard focus, or clear its focus ring.

For a program that moves focus with Tab; every radio button is its own Tab stop. With focused nonzero the button takes the X input focus (if its window is viewable) and draws its circle in focus-border; Space then selects it and the arrow keys move the focus within the group. With 0 it only clears the focus ring: the X focus moves when another widget takes it.

Parameter Description
radiobutton The button.
focused Nonzero to take the focus, 0 to clear the focus ring.

xtk_radiobutton_get_focused

int xtk_radiobutton_get_focused(const XtkRadioButton *radiobutton);

Whether the radio button shows keyboard focus.

Parameter Description
radiobutton The button.

Returns: 1 if focused, 0 if not or if radiobutton is NULL.

xtk_radiobutton_get_focus_window

Window xtk_radiobutton_get_focus_window(const XtkRadioButton *radiobutton);

The window that takes the X input focus for this radio button.

Parameter Description
radiobutton The button.

Returns: The button's window, or None if radiobutton is NULL.

xtk_radiobutton_set_theme

void xtk_radiobutton_set_theme(XtkRadioButton *radiobutton, const XtkConfig *theme);

Give the radio button new colors and a new font, and redraw.

Call it after the theme changes. If the font changes, the width is measured again for the label.

Parameter Description
radiobutton The button.
theme The configuration; copied. NULL does nothing.

xtk_radiobutton_config_init_defaults

void xtk_radiobutton_config_init_defaults(RadioButtonBlock *radiobutton_config);

Fill a RadioButtonBlock with the default [radiobutton] colors and font.

xtk_config_init_defaults() already calls it for config.radiobutton.

Parameter Description
radiobutton_config The block to fill; NULL does nothing.

xtk_radiobutton_config_parse_styling

int xtk_radiobutton_config_parse_styling(
    RadioButtonBlock *radiobutton_config,
    const char *key,
    const char *value);

Set one [radiobutton] key in a RadioButtonBlock.

Parameter Description
radiobutton_config The block to change.
key The key name, such as fill-foreground.
value The value as written in the file.

Returns: 1 if the key is known and was set; 0 if it is unknown or an argument is NULL.

xtk_radiobutton_config_write_styling

int xtk_radiobutton_config_write_styling(
    FILE *file,
    const RadioButtonBlock *radiobutton_config);

Write a RadioButtonBlock as a [radiobutton] section.

Parameter Description
file The file to write to.
radiobutton_config The block to write.

Returns: The number of keys written (8), or 0 if an argument is NULL.

xtk_radiobutton_show

void xtk_radiobutton_show(XtkRadioButton *radiobutton);

Show the radio button: map its window.

Parameter Description
radiobutton The button.

xtk_radiobutton_hide

void xtk_radiobutton_hide(XtkRadioButton *radiobutton);

Hide the radio button: unmap its window.

It stays unmapped when xtk_radiobutton_set_position() moves it, until xtk_radiobutton_show().

Parameter Description
radiobutton The button.