2 xtk_entry
liquibyte edited this page 2026-11-27 16:51:41 -06:00

xtk_entry.h

A single-line text field, for text, whole numbers, decimals or hex colors.

An entry is a one-line field the user types into. It has a blinking caret, selection with the mouse and the keyboard, the clipboard (Ctrl+C, Ctrl+X, Ctrl+V and middle-click paste of the PRIMARY selection), undo and redo, a right-click menu (Cut, Copy, Paste, Select All, Clear, Undo, Redo), an optional placeholder hint, a password mode and a read-only mode. Its kind (EntryKind) decides which characters it accepts, so an entry for a number refuses letters as they are typed or pasted.

Making an entry. Fill an XtkEntryConfig and call xtk_entry_create(). The geometry usually comes from config.entry_widget (padding, border width, corner radius) and the length limit from config.max_length. The colors and font come from the XtkConfig you pass: the entry copies it and uses the block that matches its kind (entry_text, entry_int, entry_float or entry_hex). The height is not given: the entry sets it from its font, padding and border, and never goes below that. The entry is made unmapped; xtk_entry_show() or the first xtk_entry_set_position() maps it. It owns its window, its fonts and its right-click menu, and xtk_entry_destroy() frees them. The clipboard is the caller's: make one with xtk_clipboard_create(), share it among all widgets, and destroy it after them.

Events. Pass every X event to xtk_entry_handle_event(). It returns 1 when it used the event: an Expose, a button press on the entry, a key press while the entry has focus, or an event for its open right-click menu. It returns 0 for everything else, including Tab and Shift+Tab, so the program can move focus. While the right-click menu is open (xtk_entry_menu_is_visible() returns 1), the entry must get every event, whatever its window, or the menu cannot close. Also pass the clipboard's events to xtk_clipboard_handle_event(), or paste does not work.

Focus. A left or middle click focuses the entry. The entry does not take focus from X FocusIn events, because window managers send them when the pointer only crosses a window. Only one entry has focus at a time; when one takes it, the callbacks given to xtk_entry_register_focus_callback() run, so other widgets can drop their own focus. A key press reaches the focused entry whatever window it names, so pass key presses to the focused entry only, or to all entries (the unfocused ones ignore them). Call xtk_entry_update_blink() often (for example every 50 ms) to blink the caret, and xtk_entry_handle_window_focus() when the top-level window gains or loses focus, so the caret hides while the window is inactive.

The change callback. on_change is not called for each keystroke. It is called when the value is committed: on Enter, when the entry loses focus (another entry is clicked, xtk_entry_focus() with 0, or an X FocusOut while focused), on xtk_entry_commit_focused(), and after each undo and redo. Read the text there with xtk_entry_get_text(). xtk_entry_set_text() does not call it.

Keys. Left and Right move by one character, with Ctrl by one word (words end at spaces); Home and End go to the ends; Shift with any of these extends the selection. Backspace and Delete remove one character or the selection; Ctrl+Backspace removes the word before the caret. Ctrl+A selects all, Ctrl+Z undoes, and Ctrl+Y or Ctrl+Shift+Z redoes. A double click selects a word and a triple click selects all. Each change is one undo step. The first Ctrl+Z after the entry is focused again undoes the whole previous visit at once: it restores the text the entry had before it was last focused and edited.

Theme keys. The entry reads the section of its kind: [entry-text], [entry-int], [entry-float] or [entry-hex]. Each has these keys (the defaults are the same for all four):

  • font (also font-family): the font family. Default "DejaVu Sans".
  • font-size: the font's pixel size. Default 12.
  • color: the text. Default F2F2F2.
  • background: the field. Default #262626. It can be a gradient.
  • border: the border. Default #808080.
  • focus-border: the border while the entry has focus. Default #80B3FF.
  • valid-border: the border for validation state 2. Default #008000.
  • invalid-border: the border for validation state 1. Default #800000.
  • hover-border: the border while the pointer is over the entry and it does not have focus. Default A6A6A6. [entry-hex] does not read it.

It also reads selection, selection-text and cursor-color in [main] (the selection background, the selected text, and the caret); cursor-blink-ms (default 600), cursor-width (the caret's thickness in pixels, default 1), hex-case (upper or lower: the case hex digits take as they are typed, default upper) and undo-depth (the number of undo steps, default 64) in [behavior]; and auto-copy-primary (true to put a selection made with the mouse, or with Select All in the menu, in the PRIMARY selection; default false) in [clipboard]. The right-click menu uses the [menu] section. Changes to the XtkConfig reach a made entry only through xtk_entry_set_theme().

Example.

static void name_committed(XtkEntry *entry, void *user_data) {
    (void)user_data;
    printf("name: %s\n", xtk_entry_get_text(entry));
}

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);
XtkClipboard *clipboard = xtk_clipboard_create(display);

XtkEntryConfig entry_config = {
    .kind = ENTRY_TEXT,
    .x_position = 10, .y_position = 10, .width = 240,
    .padding = config.entry_widget.padding,
    .border_width = config.entry_widget.border_width,
    .border_radius = config.entry_widget.border_radius,
    .max_length = config.max_length.text,
    .on_change = name_committed,
};
XtkEntry *entry = xtk_entry_create(display, screen, window, &config, &entry_config, clipboard);
xtk_entry_set_placeholder(entry, "Your name");
xtk_entry_show(entry);

for (;;) {
    XEvent event;
    while (XPending(display)) {
        XNextEvent(display, &event);
        if (xtk_clipboard_handle_event(clipboard, &event)) continue;
        xtk_entry_handle_event(entry, &event);
    }
    xtk_entry_update_blink(entry);
    usleep(50000);
}

xtk_entry_destroy(entry);
xtk_clipboard_destroy(clipboard);
 * 

Limitations.

  • Lengths and positions are in bytes of UTF-8, not characters: the length limit counts bytes, and xtk_entry_set_selection() takes byte offsets.
  • ENTRY_INT and ENTRY_FLOAT accept a minus sign anywhere, not only at the start; the program checks the number when it reads it.
  • In password mode each character of more than one byte shows as several asterisks.
  • The entry is not thread-safe; use it from the thread that owns the Display.
#include "xtk_entry.h"

Types: EntryKind, XtkEntry, XtkEntryCallback, XtkEntryConfig

Functions: xtk_entry_create, xtk_entry_destroy, xtk_entry_focus, xtk_entry_is_focused_check, xtk_entry_menu_is_visible, xtk_entry_handle_window_focus, xtk_entry_draw, xtk_entry_draw_noflush, xtk_entry_set_theme, xtk_entry_set_theme_noflush, xtk_entry_set_position, xtk_entry_set_position_noflush, xtk_entry_resize, xtk_entry_resize_noflush, xtk_entry_get_window, xtk_entry_get_size, xtk_entry_measure_text, xtk_entry_set_max_length, xtk_entry_set_text, xtk_entry_set_text_no_draw, xtk_entry_get_text, xtk_entry_set_placeholder, xtk_entry_set_password_mode, xtk_entry_get_password_mode, xtk_entry_set_ime, xtk_entry_set_read_only, xtk_entry_is_read_only, xtk_entry_handle_event, xtk_entry_update_blink, xtk_entry_unfocus_all, xtk_entry_register_focus_callback, xtk_entry_commit_focused, xtk_entry_show, xtk_entry_hide, xtk_entry_set_callback, xtk_entry_set_validation_state, xtk_entry_get_validation_state, xtk_entry_get_validation_flash_start, xtk_entry_set_validation_flash_start, xtk_entry_config_init_defaults, xtk_entry_select, xtk_entry_set_selection

Types

EntryKind

What an entry accepts; it also picks the theme section.

Name Value Description
ENTRY_TEXT 0 Any printable text, including non-ASCII UTF-8; style from [entry-text]
ENTRY_INT Digits, the minus sign, spaces and commas; style from [entry-int]
ENTRY_FLOAT Digits, the minus sign, the point, spaces and commas; style from [entry-float]
ENTRY_HEX Hex digits and '#', the digits put in the case of hex-case; style from [entry-hex]

XtkEntry

typedef struct XtkEntry XtkEntry;

An entry; opaque, made by xtk_entry_create() and freed by xtk_entry_destroy().

XtkEntryCallback

typedef void (*XtkEntryCallback)(XtkEntry *entry, void *user_data);

The function an entry calls when its value is committed.

See "The change callback" in the file description for when it is called.

XtkEntryConfig

How to make an entry: its kind, place, geometry and callback.

The struct is copied by xtk_entry_create(). Zero the fields you do not set (a designated initializer does that).

Field Type Description
kind EntryKind What the entry accepts (ENTRY_TEXT, ENTRY_INT, ENTRY_FLOAT, ENTRY_HEX)
x_position int X position in the parent window.
y_position int Y position in the parent window.
width int Width in pixels; the height comes from the font.
padding int Space between the border and the text, in pixels (usually config.entry_widget.padding)
border_width int Border width in pixels (usually config.entry_widget.border_width)
border_radius int Corner radius in pixels (usually config.entry_widget.border_radius)
max_length int The most bytes of text it accepts; 0 or less for no limit.
select_all_on_focus int Non-zero: the click that focuses the entry selects all its text.
on_change XtkEntryCallback Called when the value is committed; may be NULL.
user_data void * Passed to on_change.

Functions

xtk_entry_create

XtkEntry *xtk_entry_create(
    Display *display,
    int screen,
    Window parent,
    const XtkConfig *theme,
    const XtkEntryConfig *config,
    XtkClipboard *clipboard);

Make an entry, unmapped, as a child of a window.

The entry copies theme and config; later changes to them do not reach it (use xtk_entry_set_theme()). Map it with xtk_entry_show() or xtk_entry_set_position().

Parameter Description
display The X display
screen The screen number
parent The window the entry is made in
theme The configuration its colors, font and behavior come from; must not be NULL
config Its kind, place and geometry; must not be NULL
clipboard The clipboard for copy and paste, owned by the caller; it must outlive the entry An unknown kind is made as ENTRY_TEXT. An undo-depth of 0 or less keeps one undo step.

Returns: The entry, or NULL if display, theme or config is NULL or the entry cannot be allocated. Running out of memory later, for its text or its undo steps, ends the program.

xtk_entry_destroy

void xtk_entry_destroy(XtkEntry *entry);

Destroy an entry: its window, fonts, menu and text.

The callback is not called. If the entry had focus, no entry has focus afterwards.

Parameter Description
entry The entry (NULL is ignored)

xtk_entry_focus

void xtk_entry_focus(XtkEntry *entry, int f);

Give the entry focus, or take it away.

With 1, the entry that had focus loses it (without its callback), the focus callbacks run, the caret shows, and the entry asks X for keyboard focus if its window is viewable. With 0, an entry that had focus loses it and its on_change callback is called; xtk_entry_is_focused_check() already returns 0 inside the callback.

Parameter Description
entry The entry
f 1 to focus, 0 to unfocus

xtk_entry_is_focused_check

int xtk_entry_is_focused_check(const XtkEntry *entry);

Whether the entry has focus.

Parameter Description
entry The entry

Returns: 1 if it is the focused entry, 0 if not or if entry is NULL

xtk_entry_menu_is_visible

int xtk_entry_menu_is_visible(const XtkEntry *entry);

Whether the entry's right-click menu is open.

While it is open the menu holds the pointer and the keyboard, and every button, motion and key event arrives with the menu's window, not the entry's. A program that filters events by window must pass all of them to xtk_entry_handle_event() while this returns 1, or the menu cannot close and its grab locks the whole display.

Parameter Description
entry The entry

Returns: 1 if the menu is open, 0 if not or if entry is NULL

xtk_entry_handle_window_focus

void xtk_entry_handle_window_focus(XtkEntry *entry, int has_focus);

Tell the entry that the top-level window gained or lost focus.

The caret is shown only while the window has focus. Call it from the top-level window's FocusIn and FocusOut. The entry assumes at first that the window has focus.

Parameter Description
entry The entry
has_focus 1 if the window gained focus, 0 if it lost it

xtk_entry_draw

void xtk_entry_draw(XtkEntry *entry);

Draw the entry now and flush the display.

The entry draws itself after every change it makes; call this only after a change it cannot see.

Parameter Description
entry The entry (NULL is ignored)

xtk_entry_draw_noflush

void xtk_entry_draw_noflush(XtkEntry *entry);

Draw the entry without flushing the display.

Use it to draw several widgets and then flush once with XFlush().

Parameter Description
entry The entry (NULL is ignored)

xtk_entry_set_theme

void xtk_entry_set_theme(XtkEntry *entry, const XtkConfig *theme);

Give the entry new colors, fonts and behavior, and redraw it.

The entry copies the configuration again, reopens its font (which can change its height), and remakes its right-click menu.

Parameter Description
entry The entry
theme The new configuration; must not be NULL

xtk_entry_set_theme_noflush

void xtk_entry_set_theme_noflush(XtkEntry *entry, const XtkConfig *theme);

Like xtk_entry_set_theme(), but without flushing the display.

Parameter Description
entry The entry
theme The new configuration; must not be NULL

xtk_entry_set_position

void xtk_entry_set_position(XtkEntry *entry, int x, int y);

Move the entry, map it unless xtk_entry_hide() hid it, and redraw it.

Parameter Description
entry The entry
x The new x position in the parent window
y The new y position in the parent window

xtk_entry_set_position_noflush

void xtk_entry_set_position_noflush(XtkEntry *entry, int x, int y);

Like xtk_entry_set_position(), but without flushing the display.

Parameter Description
entry The entry
x The new x position in the parent window
y The new y position in the parent window

xtk_entry_resize

void xtk_entry_resize(XtkEntry *entry, int w, int h);

Resize the entry and redraw it.

The height never goes below what the font needs.

Parameter Description
entry The entry
w The new width in pixels; 0 or less keeps the width
h The new height in pixels; 0 or less keeps the height

xtk_entry_resize_noflush

void xtk_entry_resize_noflush(XtkEntry *entry, int w, int h);

Like xtk_entry_resize(), but without flushing the display.

Parameter Description
entry The entry
w The new width in pixels; 0 or less keeps the width
h The new height in pixels; 0 or less keeps the height

xtk_entry_get_window

Window xtk_entry_get_window(const XtkEntry *entry);

The entry's window.

Parameter Description
entry The entry

Returns: The window, or None if entry is NULL

xtk_entry_get_size

void xtk_entry_get_size(const XtkEntry *entry, int *width, int *height);

The entry's size, including its border.

Parameter Description
entry The entry; if NULL both sizes are 0
width Receives the width in pixels (may be NULL)
height Receives the height in pixels (may be NULL)

xtk_entry_measure_text

int xtk_entry_measure_text(XtkEntry *entry, const char *text);

The width an entry needs to show a string whole.

It is the text's width in the entry's font plus the padding and the border on both sides. Use it to size an entry to its contents.

Parameter Description
entry The entry
text The text to measure

Returns: The width in pixels, or 0 if an argument is NULL

xtk_entry_set_max_length

void xtk_entry_set_max_length(XtkEntry *entry, int max_length);

Change the length limit.

Text already in the entry is not cut.

Parameter Description
entry The entry
max_length The most bytes it accepts; 0 or less for no limit

xtk_entry_set_text

void xtk_entry_set_text(XtkEntry *entry, const char *t);

Replace the text, put the caret at its end, and redraw.

The text is not checked against the entry's kind or length limit, the undo history is kept, and the callback is not called.

Parameter Description
entry The entry
t The new text, copied; NULL for empty

xtk_entry_set_text_no_draw

void xtk_entry_set_text_no_draw(XtkEntry *entry, const char *t);

Like xtk_entry_set_text(), but without drawing.

Parameter Description
entry The entry
t The new text, copied; NULL for empty

xtk_entry_get_text

const char *xtk_entry_get_text(XtkEntry *entry);

The entry's text.

Parameter Description
entry The entry

Returns: The text as UTF-8, owned by the entry and valid until its next change; "" if entry is NULL. Never the placeholder.

xtk_entry_set_placeholder

void xtk_entry_set_placeholder(XtkEntry *entry, const char *placeholder);

Set or clear the placeholder: a dimmed hint shown while the entry is empty.

The placeholder shows only while the text is empty and the entry does not have focus. It goes away as soon as the entry is focused and comes back when focus leaves an empty entry. It is never part of the text.

Parameter Description
entry The entry
placeholder The hint, copied; NULL or "" to clear it

xtk_entry_set_password_mode

void xtk_entry_set_password_mode(XtkEntry *entry, int password);

Turn password mode on or off.

In password mode every byte of the text is drawn as '*'. Copy and Cut do nothing and a selection is not put in PRIMARY, so the text never leaves the entry through the clipboard. Otherwise only the drawing and the mapping of clicks to positions change: the text, xtk_entry_get_text(), paste, undo and the callback all work with the real text. It is off when the entry is made.

Parameter Description
entry The entry
password Non-zero to hide the text, 0 to show it

xtk_entry_get_password_mode

int xtk_entry_get_password_mode(const XtkEntry *entry);

Whether password mode is on.

Parameter Description
entry The entry

Returns: 1 if it is on, 0 if it is off or entry is NULL

xtk_entry_set_ime

void xtk_entry_set_ime(XtkEntry *entry, XIM input_method);

Attach or detach an input method, for composed and CJK input.

With an input method the entry reads keys through it, so text that the input method composes is typed into the entry. See xtk_ime.h for the setup the program needs (setlocale() and XFilterEvent() in the event loop). It can be called at any time after xtk_entry_create().

Parameter Description
entry The entry
input_method An XIM from xtk_ime_open(), or NULL to detach (also when xtk_ime_open() returned NULL)

xtk_entry_set_read_only

void xtk_entry_set_read_only(XtkEntry *entry, int read_only);

Turn read-only mode on or off.

A read-only entry still takes focus, selects and copies, and has its right-click menu, but typing, paste, cut, clear, undo and redo do nothing, and the menu shows those items disabled. The entry is not redrawn.

Parameter Description
entry The entry
read_only 1 to stop editing, 0 to allow it

xtk_entry_is_read_only

int xtk_entry_is_read_only(const XtkEntry *entry);

Whether the entry is read-only.

Parameter Description
entry The entry

Returns: 1 if it is read-only, 0 if not or if entry is NULL

xtk_entry_handle_event

int xtk_entry_handle_event(XtkEntry *entry, XEvent *event);

Give the entry an X event.

Pass it every event; see "Events" in the file description.

Parameter Description
entry The entry
event The event

Returns: 1 if the entry used the event (an Expose of its window, a button press on it, a key press other than Tab while it has focus, or an event for its right-click menu), 0 otherwise. Button releases, motion, focus and configure events return 0 even when the entry acted on them.

xtk_entry_update_blink

void xtk_entry_update_blink(XtkEntry *entry);

Blink the caret.

Call it often from the event loop (for example every 50 ms). When the entry has focus and the window has focus, the caret turns on or off once every cursor-blink-ms milliseconds and the entry is redrawn.

Parameter Description
entry The entry (NULL is ignored)

xtk_entry_unfocus_all

void xtk_entry_unfocus_all(void);

Take focus from whichever entry has it.

Other input widgets call this when they take focus. The entry loses focus and is redrawn; its callback is not called.

xtk_entry_register_focus_callback

void xtk_entry_register_focus_callback(void(*)(void)callback);

Register a function to call whenever an entry takes focus.

Other input widgets (a spin button, for example) use it to drop their own focus. Up to 8 functions can be registered; the same function is registered only once.

Parameter Description
callback The function to call; NULL is ignored

xtk_entry_commit_focused

void xtk_entry_commit_focused(void);

Call the focused entry's callback, keeping its focus.

A click on a widget that does not take X focus (a slider, for example) causes no FocusOut for the entry. Call this on every ButtonPress, before the widgets handle it, so a value being typed is committed when the user clicks elsewhere. It does nothing when no entry has focus.

xtk_entry_show

void xtk_entry_show(XtkEntry *entry);

Map the entry, and let xtk_entry_set_position() map it again.

Parameter Description
entry The entry (NULL is ignored)

xtk_entry_hide

void xtk_entry_hide(XtkEntry *entry);

Unmap the entry, and keep xtk_entry_set_position() from mapping it.

A focused entry loses its focus, without its callback.

Parameter Description
entry The entry (NULL is ignored)

xtk_entry_set_callback

void xtk_entry_set_callback(XtkEntry *entry, XtkEntryCallback callback, void *user_data);

Set or change the callback.

Parameter Description
entry The entry
callback The new callback; NULL for none
user_data The pointer to pass to it

xtk_entry_set_validation_state

void xtk_entry_set_validation_state(XtkEntry *entry, int state);

Set the validation state, which colors the border, and redraw.

The program checks the value itself (usually in its callback) and shows the result here: 1 draws the border in invalid-border, 2 in valid-border, and any other value gives the normal border (or focus-border while focused). The validation colors win over the focus color.

Parameter Description
entry The entry
state 0 for normal, 1 for invalid, 2 for valid

xtk_entry_get_validation_state

int xtk_entry_get_validation_state(const XtkEntry *entry);

The validation state set by xtk_entry_set_validation_state().

Parameter Description
entry The entry

Returns: The state, or 0 if entry is NULL

xtk_entry_get_validation_flash_start

long long xtk_entry_get_validation_flash_start(const XtkEntry *entry);

The time stored by xtk_entry_set_validation_flash_start().

Parameter Description
entry The entry

Returns: The time in milliseconds, or 0 if entry is NULL or none was stored

xtk_entry_set_validation_flash_start

void xtk_entry_set_validation_flash_start(XtkEntry *entry, long long timestamp);

Store a time with the entry, for a program that flashes the valid border.

The entry only keeps the value. A program can store when it set state 2 and set the state back to 0 when enough time has passed.

Parameter Description
entry The entry
timestamp The time in milliseconds

xtk_entry_config_init_defaults

void xtk_entry_config_init_defaults(struct EntryBlock *block, const char *xtk_entry_type);

Set an entry style block to the default font and colors.

xtk_config_init_defaults() already calls it for the four entry blocks. It sets the font, font size, the text, background, border, hover, focus, valid and invalid colors, and the block's border width; it does not touch padding or border_radius.

Parameter Description
block The block to set
xtk_entry_type The kind's name ("text", "int", "float", "hex"); not used

xtk_entry_select

void xtk_entry_select(XtkEntry *entry, int start, int end);

Select a range of the text as the user would, the caret at its end, and redraw.

Typing then replaces the range; Right puts the caret at its end, Left at its start, so text can be added on either side. For a field opened with part of its text ready to be replaced (a name without its extension).

Parameter Description
entry The entry
start The byte offset where the selection starts; below 0 counts as 0
end The byte offset where it ends, and the caret goes; -1 (or past the end) for the end

xtk_entry_set_selection

void xtk_entry_set_selection(XtkEntry *entry, int start, int end);

Select a range of the text and redraw, without moving the caret.

Use it to show a selection the user did not make, for example the match of a search.

Parameter Description
entry The entry
start The byte offset where the selection starts; below 0 counts as 0
end The byte offset where it ends; -1 (or past the end) for the end of the text. When start is not before end, nothing is selected.