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

xtk_checkbox.h

A check box with a text label.

An XtkCheckbox is a small square box that the user turns on and off, with an optional label beside it. Besides checked and unchecked it has an indeterminate ("mixed") state, drawn as a dash, for a box that stands for several others of which only some are checked. The checked mark can be an X, a check mark or a filled square (xtk_checkbox_set_check_style()).

Creating a check box. xtk_checkbox_create() takes the whole XtkConfig (it copies it and reads the checkbox block), a position and the label. The box is box-size pixels square (16); the widget is 4 pixels higher than the box, or as high as the label's font when that is higher, and as wide as the box, the label-gap (6) and the label need. Its window has the parent's background (ParentRelative), so only the box itself is filled. The window is not mapped until xtk_checkbox_set_position() or xtk_checkbox_show(). The program frees the check box with xtk_checkbox_destroy().

Events. The program passes every XEvent to xtk_checkbox_handle_event(). It returns 1 when the event was for the check box and 0 otherwise. A Button1 press and release both on the check box (a release after the pointer was dragged off it does nothing), or Space while it has the keyboard focus, changes the state and then calls the callback set with xtk_checkbox_set_callback(). A mouse press gives it the X input focus, so Space works right after a click. Changing the state from code (xtk_checkbox_set_checked(), xtk_checkbox_set_state()) does not call the callback.

A click normally toggles between checked and unchecked, and turns an indeterminate box checked. With xtk_checkbox_set_tristate() each click moves on one state: unchecked, checked, indeterminate, unchecked.

Theme keys. [checkbox]:

  • color (default #D9D9D9): the label text. foreground, its old name, is read too.
  • background (#2E2E2E): the inside of the box only; it also accepts a CSS gradient.
  • border (#897C4F): the box border when unchecked.
  • hover-border (#BCAF82): the box border while the pointer is over it or it is pressed.
  • focus-border (the toolkit's focus color): the box border while it has keyboard focus.
  • check-foreground (#D9D9D9): the mark, and the box border while it is checked or indeterminate.
  • font-family or font (DejaVu Sans) and font-size (10).
  • box-size (16), border-width (1), border-radius (2) and label-gap (6), in pixels: the box, its border and corners, and the gap between the box and the label.

Example.

static void on_wrap(XtkCheckbox *checkbox, int checked, void *user_data) {
    (void)checkbox;
    (void)user_data;
    printf("Wrap lines: %s\n", checked ? "on" : "off");
}

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

XtkCheckbox *wrap = xtk_checkbox_create(display, parent, &config, 10, 40, "Wrap lines");
xtk_checkbox_set_checked(wrap, 1);
xtk_checkbox_set_callback(wrap, on_wrap, NULL);
xtk_checkbox_show(wrap);

XEvent event;
while (running) {
    XNextEvent(display, &event);
    xtk_checkbox_handle_event(wrap, &event);
}
xtk_checkbox_destroy(wrap);
 * 

Limitations.

#include "xtk_checkbox.h"

Types: XtkCheckbox, XtkCheckStyle, XtkCheckState, XtkCheckboxCallback

Functions: xtk_checkbox_set_check_style, xtk_checkbox_set_label_left, xtk_checkbox_get_label_left, xtk_checkbox_create, xtk_checkbox_destroy, xtk_checkbox_draw, xtk_checkbox_get_window, xtk_checkbox_get_size, xtk_checkbox_set_label, xtk_checkbox_set_checked, xtk_checkbox_is_checked, xtk_checkbox_set_state, xtk_checkbox_get_state, xtk_checkbox_is_indeterminate, xtk_checkbox_set_tristate, xtk_checkbox_get_tristate, xtk_checkbox_set_position, xtk_checkbox_show, xtk_checkbox_hide, xtk_checkbox_handle_event, xtk_checkbox_set_focused, xtk_checkbox_get_focused, xtk_checkbox_get_focus_window, xtk_checkbox_set_callback, xtk_checkbox_set_enabled, xtk_checkbox_set_underline, xtk_checkbox_get_enabled, xtk_checkbox_set_theme, xtk_checkbox_config_init_defaults, xtk_checkbox_config_parse_styling, xtk_checkbox_config_write_styling

Types

XtkCheckbox

typedef struct XtkCheckbox XtkCheckbox;

A check box with a label. The structure is private; make one with xtk_checkbox_create().

XtkCheckStyle

How the checked state is drawn inside the box (see xtk_checkbox_set_check_style()).

Name Value Description
XTK_CHECK_STYLE_X 0 An X across the box (the default)
XTK_CHECK_STYLE_CHECK A check mark.
XTK_CHECK_STYLE_FILL A filled square inside the box.

XtkCheckState

The three states of a check box (see xtk_checkbox_set_state()).

Name Value Description
XTK_CHECK_UNCHECKED 0 Not checked: an empty box.
XTK_CHECK_CHECKED Checked: the box shows its mark.
XTK_CHECK_INDETERMINATE Mixed, such as a "select all" box when only some are checked: a dash.

XtkCheckboxCallback

typedef void (*XtkCheckboxCallback)(XtkCheckbox *checkbox, int checked, void *user_data);

Called after the user changes the state with a click or Space.

Functions

xtk_checkbox_set_check_style

void xtk_checkbox_set_check_style(XtkCheckbox *checkbox, XtkCheckStyle style);

Choose how the checked state is drawn, and redraw.

The indeterminate state is always a dash, whatever the style.

Parameter Description
checkbox The check box.
style The mark; a new check box uses XTK_CHECK_STYLE_X.

xtk_checkbox_set_label_left

void xtk_checkbox_set_label_left(XtkCheckbox *checkbox, int left);

Put the label before the box ("Alpha [x]") or after it ("[x] Alpha", the default), and redraw.

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

Parameter Description
checkbox The check box.
left Nonzero: label first, box at the right edge. 0: box first.

xtk_checkbox_get_label_left

int xtk_checkbox_get_label_left(const XtkCheckbox *checkbox);

Whether the label comes before the box.

Parameter Description
checkbox The check box.

Returns: 1 if the label comes first, 0 if the box does or if checkbox is NULL.

xtk_checkbox_create

XtkCheckbox *xtk_checkbox_create(
    Display *display,
    Window parent,
    const XtkConfig *theme,
    int x,
    int y,
    const char *label);

Create a check box.

It starts unchecked and enabled. Its window is not mapped until xtk_checkbox_set_position() or xtk_checkbox_show().

Parameter Description
display The X display.
parent The window the check box sits in.
theme The configuration; it is copied, and its checkbox 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.

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

xtk_checkbox_destroy

void xtk_checkbox_destroy(XtkCheckbox *checkbox);

Destroy a check box: its window, font and memory.

Parameter Description
checkbox The check box, or NULL (then nothing happens).

xtk_checkbox_draw

void xtk_checkbox_draw(XtkCheckbox *checkbox);

Redraw the check box now.

The check box 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 check box's own window.

Parameter Description
checkbox The check box, or NULL.

xtk_checkbox_get_window

Window xtk_checkbox_get_window(XtkCheckbox *checkbox);

The check box's X window.

Parameter Description
checkbox The check box.

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

xtk_checkbox_get_size

void xtk_checkbox_get_size(const XtkCheckbox *checkbox, int *w, int *h);

The check box's current size.

Parameter Description
checkbox The check box. 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_checkbox_set_label

void xtk_checkbox_set_label(XtkCheckbox *checkbox, const char *label);

Change the label, resize the window to fit it, and redraw.

Parameter Description
checkbox The check box.
label The new text, or NULL for none; copied.

xtk_checkbox_set_checked

void xtk_checkbox_set_checked(XtkCheckbox *checkbox, int checked);

Check or uncheck the box, and redraw.

This also clears the indeterminate state. It does not call the callback.

Parameter Description
checkbox The check box.
checked Nonzero to check it, 0 to uncheck it.

xtk_checkbox_is_checked

int xtk_checkbox_is_checked(const XtkCheckbox *checkbox);

Whether the box is checked.

Parameter Description
checkbox The check box.

Returns: 1 if checked; 0 if unchecked, indeterminate, or checkbox is NULL.

xtk_checkbox_set_state

void xtk_checkbox_set_state(XtkCheckbox *checkbox, XtkCheckState state);

Set the state, including the indeterminate state, and redraw.

It does not call the callback. Without tristate mode the user can only leave the indeterminate state (a click makes it checked), so only the program puts a box into it.

Parameter Description
checkbox The check box.
state The new state.

xtk_checkbox_get_state

XtkCheckState xtk_checkbox_get_state(const XtkCheckbox *checkbox);

The current state, including the indeterminate state.

Parameter Description
checkbox The check box.

Returns: The state, or XTK_CHECK_UNCHECKED if checkbox is NULL.

xtk_checkbox_is_indeterminate

int xtk_checkbox_is_indeterminate(const XtkCheckbox *checkbox);

Whether the box is in the indeterminate state.

Parameter Description
checkbox The check box.

Returns: 1 if indeterminate, 0 if not or if checkbox is NULL.

xtk_checkbox_set_tristate

void xtk_checkbox_set_tristate(XtkCheckbox *checkbox, int tristate);

Let clicks go through all three states.

With tristate mode on, each click or Space moves on one state: unchecked, checked, indeterminate, then unchecked again. Off (the default), a click toggles between checked and unchecked and makes an indeterminate box checked. It changes only how clicks act; xtk_checkbox_set_state() works the same either way.

Parameter Description
checkbox The check box.
tristate Nonzero to turn tristate mode on, 0 to turn it off.

xtk_checkbox_get_tristate

int xtk_checkbox_get_tristate(const XtkCheckbox *checkbox);

Whether tristate mode is on.

Parameter Description
checkbox The check box.

Returns: 1 if on, 0 if off or if checkbox is NULL.

xtk_checkbox_set_position

void xtk_checkbox_set_position(XtkCheckbox *checkbox, int x, int y);

Move the check box within its parent.

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

Parameter Description
checkbox The check box.
x The new x position in the parent, in pixels.
y The new y position in the parent, in pixels.

xtk_checkbox_show

void xtk_checkbox_show(XtkCheckbox *checkbox);

Show the check box: map its window.

Parameter Description
checkbox The check box.

xtk_checkbox_hide

void xtk_checkbox_hide(XtkCheckbox *checkbox);

Hide the check box: unmap its window.

It stays unmapped when xtk_checkbox_set_position() moves it, until xtk_checkbox_show().

Parameter Description
checkbox The check box.

xtk_checkbox_handle_event

int xtk_checkbox_handle_event(XtkCheckbox *checkbox, const XEvent *event);

Handle an X event.

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

Parameter Description
checkbox The check box.
event The event.

Returns: 1 if the event was for the check box and it used it (expose, hover, press, release, focus, Space); 0 otherwise. A release or Space that changed the state has already called the callback.

xtk_checkbox_set_focused

void xtk_checkbox_set_focused(XtkCheckbox *checkbox, int focused);

Give the check box keyboard focus, or clear its focus ring.

For a program that moves focus with Tab. With focused nonzero the check box takes the X input focus (if its window is viewable) and draws its border in focus-border; Space then toggles it. With 0 it only clears the focus ring: the X focus moves when another widget takes it.

Parameter Description
checkbox The check box.
focused Nonzero to take the focus, 0 to clear the focus ring.

xtk_checkbox_get_focused

int xtk_checkbox_get_focused(const XtkCheckbox *checkbox);

Whether the check box shows keyboard focus.

Parameter Description
checkbox The check box.

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

xtk_checkbox_get_focus_window

Window xtk_checkbox_get_focus_window(const XtkCheckbox *checkbox);

The window that takes the X input focus for this check box.

Parameter Description
checkbox The check box.

Returns: The check box's window, or None if checkbox is NULL.

xtk_checkbox_set_callback

void xtk_checkbox_set_callback(
    XtkCheckbox *checkbox,
    XtkCheckboxCallback callback,
    void *user_data);

Set or remove the callback for state changes made by the user.

Parameter Description
checkbox The check box.
callback The callback, or NULL to remove it.
user_data A pointer passed to the callback unchanged.

xtk_checkbox_set_enabled

void xtk_checkbox_set_enabled(XtkCheckbox *checkbox, int enabled);

Enable or disable the check box, and redraw.

A disabled check box draws its border, mark and label dimmed (blended toward background) and ignores the mouse and keyboard. Disabling clears the hover, pressed and focus looks. Check boxes start enabled.

Parameter Description
checkbox The check box.
enabled Nonzero to enable, 0 to disable.

xtk_checkbox_set_underline

void xtk_checkbox_set_underline(XtkCheckbox *checkbox, char letter);

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

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

Parameter Description
checkbox The check box.
letter The letter (either case), or 0 for none.

xtk_checkbox_get_enabled

int xtk_checkbox_get_enabled(const XtkCheckbox *checkbox);

Whether the check box is enabled.

Parameter Description
checkbox The check box.

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

xtk_checkbox_set_theme

void xtk_checkbox_set_theme(XtkCheckbox *checkbox, const XtkConfig *theme);

Give the check box new colors and a new font, and redraw.

Call it after the theme changes. The label is measured again with the new font, box size and gap, and the widget resized to fit; when the new font cannot be opened, the old one is kept.

Parameter Description
checkbox The check box.
theme The configuration; copied. NULL does nothing.

xtk_checkbox_config_init_defaults

void xtk_checkbox_config_init_defaults(CheckboxBlock *checkbox_config);

Fill a CheckboxBlock with the default [checkbox] colors and font.

xtk_config_init_defaults() already calls it for config.checkbox.

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

xtk_checkbox_config_parse_styling

int xtk_checkbox_config_parse_styling(
    CheckboxBlock *checkbox_config,
    const char *key,
    const char *value);

Set one [checkbox] key in a CheckboxBlock.

Parameter Description
checkbox_config The block to change.
key The key name, such as check-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. (This is the other way round from xtk_button_config_parse_styling().)

xtk_checkbox_config_write_styling

int xtk_checkbox_config_write_styling(FILE *file, const CheckboxBlock *checkbox_config);

Write a CheckboxBlock as a [checkbox] section.

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

Returns: 1 on success, 0 if an argument is NULL.