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

xtk_button.h

A push button with a text label.

An XtkButton is a rectangular button with rounded corners and a centered text label. It has a normal, a hover and a pressed look, a focus ring for keyboard use, and an optional tooltip and Alt+letter mnemonic. It can also work as an on/off (toggle) button. A program that wants an icon on the button draws it with an overlay callback (xtk_button_set_overlay()).

Creating a button. xtk_button_create() takes a ButtonBlock for the colors and the font (normally &config.button) and the size and border geometry as separate numbers (normally from config.button_widget). The button copies the block, so the caller's block can change or go away afterwards. The new button has no label (call xtk_button_set_label()) and its window is not mapped yet: it appears when the program first calls xtk_button_set_position() or xtk_button_show(), so it is never seen at its placeholder position (0, 0). The program owns the button and frees it with xtk_button_destroy(), which also destroys its window and tooltip.

Events. The program passes every XEvent to xtk_button_handle_event(). It returns 0 when the event is not for this button, 1 when the button used it (a press, hover, expose or focus change), and 2 when the button was clicked: Button1 pressed and released on it (a release after the pointer was dragged off the button does not click), Space, Return or keypad Enter while it has focus, or its Alt+letter mnemonic. At the same moment it returns 2 it calls the clicked callback, if one is set (xtk_button_set_clicked_callback()), so a program can use either the return value or the callback. A mouse press on the button gives it the X input focus, so Space and Return work right after a click; moving the pointer out of the button clears the focus ring again. A disabled button ignores everything except Expose.

The button redraws itself whenever its state changes. A program that also uses the tooltip calls xtk_tooltip_update() on xtk_button_get_tooltip() from a regular timer tick, because the tooltip appears after a delay.

Theme keys. [button] holds the colors and the font:

  • background (default #262626): the normal background.
  • hover-background (#404047): the background while the pointer is over the button.
  • active-background (#1A1A1F): the background while it is pressed, and while a toggle button is on.
  • border (#808080), hover-border (#80B3FF), active-border (#598CE6): the border color in the same three states.
  • focus-border (the toolkit's focus color): the border while the button has keyboard focus, and the outline of a default button.
  • color (#F2F2F2): the label text.
  • font-family (or font, default DejaVu Sans) and font-size (12).
  • highlight and shade: an optional bevel, a light line inside the top edge and a dark line inside the bottom edge (see xtk_paint.h).

The three background keys also accept a CSS gradient, such as linear-gradient(180deg, #4A4A50, #33333A).

[button-widget] holds the geometry, which the program passes to xtk_button_create():

  • width (88) and height (0, which means: as high as the font needs).
  • padding (8): the space between the label and the border.
  • border-width, hover-border-width, active-border-width (1 each): the border width in the normal, hover and pressed states.
  • border-radius (4): the corner radius.

Example.

static void on_save(XtkButton *button, void *user_data) {
    (void)button;
    (void)user_data;
    puts("Save clicked");
}

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

XtkButton *save = xtk_button_create(display, parent, &config.button,
    config.button_widget.width, config.button_widget.height,
    config.button_widget.padding, config.button_widget.border_width,
    config.button_widget.hover_border_width,
    config.button_widget.active_border_width,
    config.button_widget.border_radius);
xtk_button_set_label(save, "Save");
xtk_button_set_mnemonic(save, 's');
xtk_button_set_clicked_callback(save, on_save, NULL);
xtk_button_set_position(save, 10, 10);   // maps the button

XEvent event;
while (running) {
    XNextEvent(display, &event);
    xtk_button_handle_event(save, &event);
}
xtk_button_destroy(save);
 * 

Limitations.

  • A longer label does not make the button wider: the program measures the text and calls xtk_button_resize() if it must fit.
  • The tooltip text is cut to 127 bytes.
  • The Alt+letter mnemonic compares the letter with the key's unshifted KeySym, so it works for ASCII letters and digits, not for other characters.
  • The button is not thread-safe; use it from the thread that owns the Display.
#include "xtk_button.h"

Types: XtkButton, XtkButtonClickedCallback, XtkButtonOverlayCallback

Functions: xtk_button_set_overlay, xtk_button_create, xtk_button_destroy, xtk_button_get_window, xtk_button_get_size, xtk_button_handle_event, xtk_button_set_parent_background, xtk_button_draw, xtk_button_reset, xtk_button_set_enabled, xtk_button_get_enabled, xtk_button_set_focused, xtk_button_get_focused, xtk_button_set_default, xtk_button_get_default, xtk_button_set_mnemonic, xtk_button_set_underline, xtk_button_get_mnemonic, xtk_button_set_tooltip, xtk_button_get_tooltip, xtk_button_sync_hover, xtk_button_activate, xtk_button_get_focus_window, xtk_button_set_pressed, xtk_button_set_toggled, xtk_button_set_toggle_mode, xtk_button_get_toggle_mode, xtk_button_get_toggled, xtk_button_set_label, xtk_button_set_clicked_callback, xtk_button_set_position, xtk_button_resize, xtk_button_reallocate_dbe, xtk_button_show, xtk_button_hide, xtk_button_set_theme, xtk_button_set_border_radius, xtk_button_set_geometry, xtk_button_config_init_defaults, xtk_button_config_parse_styling, xtk_button_config_write_styling, xtk_button_widget_config_init_defaults, xtk_button_widget_config_parse, xtk_button_widget_config_write, xtk_button_get_hover

Types

XtkButton

typedef struct XtkButton XtkButton;

A button. The structure is private; use the functions in this header.

XtkButtonClickedCallback

typedef void (*XtkButtonClickedCallback)(XtkButton *button, void *user_data);

Called when the button is clicked.

It is called from xtk_button_handle_event() at the moment that call returns 2 (Button1 pressed and released on the button, Space or Return while it has focus, or its mnemonic), and from xtk_button_activate(). For a button in toggle mode the toggled state has already flipped when it is called.

XtkButtonOverlayCallback

typedef void (*XtkButtonOverlayCallback)(XtkButton *button, Drawable target, void *user_data);

Draws extra content on top of the button, such as an icon.

It is called at the end of every xtk_button_draw(), after the background, border and label and before the frame is shown.

Functions

xtk_button_set_overlay

void xtk_button_set_overlay(
    XtkButton *button,
    XtkButtonOverlayCallback callback,
    void *user_data);

Set or clear the button's overlay painter.

The painter runs on every redraw, so the program does not have to redraw its icon after each event. Setting it does not redraw the button; call xtk_button_draw() if the button is already visible.

Parameter Description
button The button.
callback The painter, or NULL to remove it.
user_data A pointer passed to callback unchanged; the button never frees it.

xtk_button_create

XtkButton *xtk_button_create(
    Display *display,
    Window parent_window,
    const ButtonBlock *xtk_button_style,
    int width,
    int height,
    int padding,
    int border_width,
    int hover_border_width,
    int active_border_width,
    int border_radius);

Create a button.

The button's window is a child of parent_window at (0, 0), not mapped until xtk_button_set_position() or xtk_button_show().

Parameter Description
display The X display.
parent_window The window the button sits in.
xtk_button_style The colors and font, normally &config.button. It is copied and must not be NULL.
width The width in pixels.
height The height in pixels, or 0 (or less) to fit the font: twice the padding plus the font height (14 when no font could be opened) plus 2, at least 22.
padding The space between the label and the border, in pixels.
border_width The border width in the normal state.
hover_border_width The border width while hovered or focused.
active_border_width The border width while pressed or toggled on.
border_radius The corner radius in pixels; the window is shaped to the rounded rectangle.

Returns: The new button, or NULL if display is NULL or memory runs out.

xtk_button_destroy

void xtk_button_destroy(XtkButton *button);

Destroy a button: its window, tooltip, font and memory.

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

xtk_button_get_window

Window xtk_button_get_window(const XtkButton *button);

The button's X window.

Parameter Description
button The button.

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

xtk_button_get_size

void xtk_button_get_size(const XtkButton *button, int *w, int *h);

The button's current size.

Parameter Description
button 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_button_handle_event

int xtk_button_handle_event(XtkButton *button, const XEvent *event);

Handle an X event.

Pass every event to it. Events for other windows return 0, except a KeyPress with Alt held that matches the button's mnemonic: that activates the button from whichever window has the focus. The button's tooltip sees the event first. A disabled button handles only Expose.

Parameter Description
button The button; NULL returns 0.
event The event.

Returns: 0 if the event was not for this button; 1 if the button used it (press, expose, hover, motion, focus); 2 if the button was clicked (Button1 pressed and released on it, Space, Return or keypad Enter while it has focus, or Alt plus its mnemonic). The clicked callback has already run when 2 is returned.

xtk_button_set_parent_background

void xtk_button_set_parent_background(XtkButton *button, ConfigColor parent_background);

Store the color of the window behind the button.

The corners outside the rounded rectangle are filled with this color before the button is drawn; where the window shape cuts them away, as it usually does, the parent shows through and the color is not seen. Until it is set, the corners are filled with the button's own background color. The button is redrawn.

Parameter Description
button The button.
parent_background The parent's background color.

xtk_button_draw

void xtk_button_draw(XtkButton *button);

Redraw the button now, including its overlay.

Parameter Description
button The button, or NULL.

xtk_button_reset

void xtk_button_reset(XtkButton *button);

Clear the pressed look and work out the hover look again.

It asks the X server where the pointer is, instead of trusting crossing events, and redraws. Call it when a dialog that holds the button is shown again: unmapping a window sends no LeaveNotify, so a button clicked just before its dialog closed would otherwise still look hovered.

Parameter Description
button The button.

xtk_button_set_enabled

void xtk_button_set_enabled(XtkButton *button, int enabled);

Enable or disable the button.

A disabled button draws its label dimmed (blended toward the background) and ignores the mouse and keyboard: xtk_button_handle_event() returns 0 for everything but Expose. Disabling clears the pressed, hover and focus looks; a toggle button that is on stays on. Buttons start enabled.

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

xtk_button_get_enabled

int xtk_button_get_enabled(const XtkButton *button);

Whether the button is enabled.

Parameter Description
button The button.

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

xtk_button_set_focused

void xtk_button_set_focused(XtkButton *button, int focused);

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

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

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

xtk_button_get_focused

int xtk_button_get_focused(const XtkButton *button);

Whether the button shows keyboard focus.

Parameter Description
button The button.

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

xtk_button_set_default

void xtk_button_set_default(XtkButton *button, int is_default);

Mark the button as the default button of its dialog, or unmark it.

A default button is drawn with an outline in the focus-border color. The mark does not make Return click it by itself: the program calls xtk_button_activate() on the default button when a Return reaches its event loop that no other widget used.

Parameter Description
button The button.
is_default Nonzero to mark it, 0 to unmark it.

xtk_button_get_default

int xtk_button_get_default(const XtkButton *button);

Whether the button is marked as the default button.

Parameter Description
button The button.

Returns: 1 if it is marked, 0 if not or if button is NULL.

xtk_button_set_mnemonic

void xtk_button_set_mnemonic(XtkButton *button, char mnemonic);

Set or clear the button's Alt+letter mnemonic.

Alt with this letter clicks the button whatever window has the focus, as long as the program passes the KeyPress to xtk_button_handle_event(). The first occurrence of the letter in the label (either case) is underlined; if the label has no such letter, the shortcut still works without a mark. A disabled or hidden button does not react to it.

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

xtk_button_set_underline

void xtk_button_set_underline(XtkButton *button, char letter);

Underline a letter of the label without making it a shortcut.

For a dialog that handles its own Alt+letter keys. A mnemonic set with xtk_button_set_mnemonic() takes the place of this letter.

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

xtk_button_get_mnemonic

char xtk_button_get_mnemonic(const XtkButton *button);

The button's mnemonic letter.

Parameter Description
button The button.

Returns: The letter as it was set, or 0 if none is set or button is NULL.

xtk_button_set_tooltip

void xtk_button_set_tooltip(XtkButton *button, const char *text);

Set or clear the tooltip shown while the pointer rests on the button.

The text is copied (at most 127 bytes). The tooltip appears after a short delay, which needs xtk_tooltip_update() on xtk_button_get_tooltip() from the program's timer tick.

Parameter Description
button The button.
text The tooltip text, or NULL or "" for no tooltip. NULL also hides a tooltip that is showing.

xtk_button_get_tooltip

struct XtkTooltip *xtk_button_get_tooltip(const XtkButton *button);

The button's tooltip widget.

The button owns it; the program only calls xtk_tooltip_update() on it regularly so that the delayed tooltip can appear.

Parameter Description
button The button.

Returns: The tooltip, or NULL if button is NULL or the tooltip could not be made.

xtk_button_sync_hover

int xtk_button_sync_hover(XtkButton *button);

Clear a hover or focus look that the pointer has left.

While another window holds a pointer grab (an open drop-down, for example), X sends no LeaveNotify to the button, so it can stay lit after the pointer has gone. Call this regularly, for example from the same timer tick as xtk_tooltip_update(). It costs nothing unless the button shows hover or focus; then it asks the server where the pointer is and, if it is outside, clears both looks, hides the tooltip and redraws.

Parameter Description
button The button.

Returns: 1 if it redrew the button, 0 if nothing changed.

xtk_button_activate

void xtk_button_activate(XtkButton *button);

Click the button from code.

It flashes the pressed look, flips a button in toggle mode, and calls the clicked callback, as a real click does. A disabled button does nothing. Use it, for example, to click the default button on Return (see xtk_button_set_default()).

Parameter Description
button The button.

xtk_button_get_focus_window

Window xtk_button_get_focus_window(const XtkButton *button);

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

For a Tab-focus manager that compares it with XGetInputFocus().

Parameter Description
button The button.

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

xtk_button_set_pressed

void xtk_button_set_pressed(XtkButton *button, int pressed);

Show or clear the pressed look from code, and redraw.

This is the look a held mouse button gives, not an on/off state: the next mouse release, xtk_button_reset() and disabling the button all clear it, and a disabled button ignores it. For a button that stays on, use xtk_button_set_toggled().

Parameter Description
button The button.
pressed 1 to show the pressed look, 0 to clear it.

xtk_button_set_toggled

void xtk_button_set_toggled(XtkButton *button, int toggled);

Turn an on/off (toggle) button on or off, and redraw.

A button that is on keeps the pressed colors (active-background and active-border) until it is turned off. Clicks, xtk_button_reset() and enabling or disabling do not change it, unless toggle mode is set (see xtk_button_set_toggle_mode()). While on and hovered it shows the hover border over the pressed colors.

Without toggle mode, the program decides when the button is on: for example, on while an action runs and off when it ends.

Parameter Description
button The button.
toggled 1 for on, 0 for off.

xtk_button_set_toggle_mode

void xtk_button_set_toggle_mode(XtkButton *button, int toggle_mode);

Make each click flip the button on and off.

Parameter Description
button The button.
toggle_mode 1: every click (mouse, Space, Return, mnemonic or xtk_button_activate()) flips the toggled state before the click is reported, so the clicked callback reads the new state with xtk_button_get_toggled(). 0 (the default): clicks leave the toggled state alone.

xtk_button_get_toggle_mode

int xtk_button_get_toggle_mode(const XtkButton *button);

Whether each click flips the button (see xtk_button_set_toggle_mode()).

Parameter Description
button The button.

Returns: 1 if each click flips it, 0 if not or if button is NULL.

xtk_button_get_toggled

int xtk_button_get_toggled(const XtkButton *button);

Whether an on/off (toggle) button is on.

Parameter Description
button The button.

Returns: 1 if on (see xtk_button_set_toggled()), 0 if off or if button is NULL.

xtk_button_set_label

void xtk_button_set_label(XtkButton *button, const char *label);

Set the label text, and redraw.

The label is centered in the button. The string is copied. The button does not change size: measure the text and call xtk_button_resize() if it must fit.

Parameter Description
button The button.
label The text, or NULL for no label.

xtk_button_set_clicked_callback

void xtk_button_set_clicked_callback(
    XtkButton *button,
    XtkButtonClickedCallback clicked,
    void *user_data);

Set or remove the clicked callback.

The callback runs whenever xtk_button_handle_event() returns 2 and on xtk_button_activate(); the return value works as before.

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

xtk_button_set_position

void xtk_button_set_position(XtkButton *button, int x_position, int y_position);

Move the button within its parent.

This also maps the button's window, unless the button is hidden with xtk_button_hide().

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

xtk_button_resize

void xtk_button_resize(XtkButton *button, int width, int height);

Change the button's size, reshape its corners and redraw.

Parameter Description
button The button.
width The new width in pixels; 0 or less is ignored.
height The new height in pixels; 0 or less is ignored.

xtk_button_reallocate_dbe

void xtk_button_reallocate_dbe(XtkButton *button);

Make the button's back buffer again from nothing, and redraw.

Only for repairing a back buffer that something outside the button has spoiled. One known case: a strip of buttons (a toolbar) right after a window-manager dialog opens or closes over it (see xtk_toolbar_reallocate_dbe()). A resize does not need it, and a normal redraw is xtk_button_draw().

Parameter Description
button The button.

xtk_button_show

void xtk_button_show(XtkButton *button);

Show the button: map its window and clear its hidden state.

Parameter Description
button The button.

xtk_button_hide

void xtk_button_hide(XtkButton *button);

Hide the button: unmap its window.

A hidden button stays unmapped when xtk_button_set_position() moves it, and its mnemonic does nothing, until xtk_button_show().

Parameter Description
button The button.

xtk_button_set_theme

void xtk_button_set_theme(XtkButton *button, const ButtonBlock *xtk_button_style);

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

Call it after the theme changes. The size and the border geometry stay as they are (see xtk_button_set_geometry() and xtk_button_set_border_radius()).

Parameter Description
button The button.
xtk_button_style The new colors and font, normally &config.button; copied. NULL does nothing.

xtk_button_set_border_radius

void xtk_button_set_border_radius(XtkButton *button, int radius);

Change the corner radius of the drawn border, and redraw.

The window shape is not changed: it keeps the radius it got at xtk_button_create() until the next xtk_button_resize(), so the corners fit the new radius only after a resize.

Parameter Description
button The button.
radius The new radius in pixels.

xtk_button_set_geometry

void xtk_button_set_geometry(
    XtkButton *button,
    int padding,
    int border_width,
    int hover_border_width,
    int active_border_width);

Change the padding and the border widths, and redraw.

Parameter Description
button The button.
padding The new padding in pixels. It is stored for later; the label is always centered.
border_width The border width in the normal state.
hover_border_width The border width while hovered or focused.
active_border_width The border width while pressed or toggled on.

xtk_button_config_init_defaults

void xtk_button_config_init_defaults(ButtonBlock *button_config);

Fill a ButtonBlock with the default [button] colors and font.

xtk_config_init_defaults() already calls it for config.button.

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

xtk_button_config_parse_styling

int xtk_button_config_parse_styling(
    ButtonBlock *button_config,
    const char *key,
    const char *value);

Set one [button] key in a ButtonBlock.

Parameter Description
button_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_button_config_write_styling

int xtk_button_config_write_styling(FILE *file, const ButtonBlock *button_config);

Write a ButtonBlock as a [button] section.

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

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

xtk_button_widget_config_init_defaults

void xtk_button_widget_config_init_defaults(XtkConfig *config);

Set config.button_widget to the default [button-widget] geometry.

Parameter Description
config The configuration; NULL does nothing.

xtk_button_widget_config_parse

int xtk_button_widget_config_parse(XtkConfig *config, const char *key, const char *value);

Set one [button-widget] key in config.button_widget.

Parameter Description
config The configuration.
key The key name, such as border-radius.
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_button_widget_config_write

int xtk_button_widget_config_write(FILE *file, const XtkConfig *config);

Write config.button_widget as a [button-widget] section.

Parameter Description
file The file to write to.
config The configuration.

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

xtk_button_get_hover

int xtk_button_get_hover(const XtkButton *button);

Whether the button shows its hover look.

For a program that draws the hover state of something else in step with the button.

Parameter Description
button The button.

Returns: 1 if it shows its hover look, 0 if not or if button is NULL.