A small, self-contained X11 widget toolkit in C. No other toolkit — just Xlib, Xrender, and Xft.
Find a file
liquibyte e1edabea92
xtk 1.13: an entry's right-click menu stays open, and the rich text's code blocks select what is under the pointer
- xtk_entry: the right-click menu of a text field closed as
  soon as it opened. The menu's own keyboard grab sends the field a
  FocusOut, and the field hid its menu on it.
- xtk_richtext: in a code block a click, a drag, Up and Down
  measured the line in the body font while it is drawn in the monospace
  one, so the caret and the selection landed on other characters than the
  ones under the pointer. A drag whose button release went elsewhere now
  ends when the button is up.
- xtk_version.h and the Doxyfile: 1.13.
2026-11-27 16:50:57 -06:00
screenshots
src xtk 1.13: an entry's right-click menu stays open, and the rich text's code blocks select what is under the pointer 2026-11-27 16:50:57 -06:00
test xtk 1.13: an entry's right-click menu stays open, and the rich text's code blocks select what is under the pointer 2026-11-27 16:50:57 -06:00
CREDITS.md
LICENSE
logo.png
Makefile xtk 1.13: an entry's right-click menu stays open, and the rich text's code blocks select what is under the pointer 2026-11-27 16:50:57 -06:00
README.md

xtk

A small, self-contained X11 widget toolkit in C. No other toolkit — just Xlib, Xrender, and Xft.

Widgets

Buttons, checkboxes and radio buttons (label on either side of the box, drawn over whatever window they sit on), sliders, spin buttons, combo boxes (items can be greyed out and skipped), text entries (text/int/float/hex), labels, menus and menu bars, popup menus, notebooks, panels, frames (titled group boxes), progress bars, status bars, tooltips, text views, a tree view, a small editable data grid, file dialogs, color swatches, scheme wheels (up to 32 markers, with one saturation/value shared by all or one per marker), gradients, a full color-picker dialog, a zoom/magnifier widget (with an optional live hex readout of the pixel under the cursor; it keeps a wider capture after a pick, so resizing or changing the magnification shows real pixels, and saves and restores it), a freedesktop.org system-tray icon, grid-based layout management, clipboard handling (with outgoing-selection INCR support for large copies), double-buffered drawing (DBE), and a color-space conversion library (RGB/HSV/HSL/Lab, LCHab/LCHuv/OKLCH/CAM16 and more).

An optional keyboard focus manager (xtk_focus) moves focus between a window's widgets on Ctrl-Tab/Ctrl-Shift-Tab, skipping disabled and hidden ones; each widget draws its own focus ring and claims focus on click. Text input (xtk_entry, xtk_textview, xtk_richtext) goes through a minimal XIM/XIC integration (xtk_ime) for proper composed/international input instead of raw XLookupString().

A unified rich-text/Markdown editor (xtk_richtext, plus a formatting toolbar, xtk_richtext_toolbar) rounds out the toolkit: lossless bidirectional Markdown ⇄ rich-text conversion, inline formatting, headings, lists, block quotes, tables, images, hyperlinks, footnotes, admonitions, and LaTeX math (xtk_mathtext, via real OpenType MATH-table parsing) — undo/redo, clipboard, and PRIMARY-selection included. A node/edge graph canvas (xtk_graph) and a docking tool-window shell (xtk_toolwindow/xtk_dockframe, with xtk_splitpane underneath) are also included for applications that need dockable, floatable panels: left, center and right columns and a bottom row under all three columns or two (the third then full height, chosen by dropping in a bottom corner), panels split beside or under each other, the whole layout saved and restored exactly, and xtk_dockframe_reset_layout() for an application's default.

Also included: small import/parsing libraries for CSV, JSON, and XML (xtk_csv/xtk_json/xtk_xml, the latter tolerant enough for real-world HTML fragments too); an image loader (xtk_image, with hand-rolled PNG/baseline-JPEG/GIF/BMP decoders, no vendored code); a small named icon set (xtk_icon: procedural glyphs plus tinted SVG assets) for tree rows, tags and toolbars, which draws from the user's freedesktop.org icon theme when it has the icon (xtk_icon_theme: the desktop's theme from XSETTINGS, GTK or KDE, or one every xtk program shares in ~/.config/xtk/icons.conf, followed live; a file's icon by its type from shared-mime-info); a 2D vector rasterizer (xtk_vector — path fill/stroke, gradients, clipping, transforms) and an SVG parser/renderer built on it (xtk_svg: style sheets, use, clip paths, masks, filters, markers, patterns, embedded images and text, checked against Inkscape on 13,796 installed theme icons); and a text shaper (xtk_shape: ligatures, the joined forms of Arabic letters and emoji made of more than one character, from a font's OpenType GSUB table, written from the OpenType specification). A character that is an emoji by default is drawn from a color font when one is installed (xtk_config_covering_font(), xtk_config_emoji_font()).

For data that can be hostile, a jail: xtk_sandbox (Landlock and seccomp, Linux 5.13 or later on x86-64 or ARM64) locks a process in so it can read only what its rules list, write nothing, reach no network and start or signal nothing; xtk_worker is one such process for the program, started the first time it is needed and kept, which can read no file at all. It decodes pictures (xtk_worker_decode_image(), xtk_worker_decode_image_file(), and xtk_worker_richtext_decoder() for a rich text's pictures) and runs jobs of the program's own (xtk_worker_add_job()), and the program checks every answer before it uses it. A program starts it with one line at the top of main(): if (xtk_worker_is_worker(argc, argv)) return xtk_worker_main();.

Widget styling and geometry are driven by an INI-style config system (xtk_config) with live-reloadable theme files. The config holds the toolkit's appearance and widget settings only; an application keeps its own state in its own sections of the same file.

Themes

One theme format for every xtk program (xtk_theme): 17 role colors (window, base, button, border, accent, hover, active, ...) that every widget follows, any widget's own keys over them, and an [app] section for a program's own colors. Any background key takes a CSS linear-gradient() or radial-gradient(), and highlight/shade draw a 3D edge (xtk_paint).

Every color a widget draws is a theme key, listed once in xtk_config.c (xtk_config_color_key_count()/xtk_config_color_key_at()); only a disabled widget's dimming and a graph's tinting are blends of keys. A theme file a program writes lists every key, and xtk_theme_complete() fills a theme out to all of them.

[theme]
name = My Theme

[colors]
window = #2D2D2D
accent = #598CE5

[button]
background = linear-gradient(180deg, #3A3A40, #2A2A2E)
highlight = #FFFFFF18

Each program keeps its themes in ~/.config/<program>/themes/, where xtk_theme_install_builtins() puts xtk's 14 presets (Chocolate, Dark, Ember, Forest, Gold, Graphite, Light, Midnight, Ocean, Rose, Rust, Slate, Teal, Violet - the same in every program) as starting points. xtk_theme_list_all() also reads themes installed the way GTK themes are: <Theme>/xtk/<Name>.conf in ~/.themes, ~/.local/share/themes and /usr/share/themes (read only).

API reference

The public headers in src/ are the reference for every function's behavior. They carry Doxygen comments, so an HTML reference can be generated with Doxygen:

doxygen -g Doxyfile    # once; set INPUT = src
doxygen Doxyfile

Every widget follows the same conventions; a header documents only where it differs.

  • Lifetime. xtk_<widget>_create() returns a widget (or NULL); xtk_<widget>_destroy() frees everything it made. Every function accepts NULL for the widget and does nothing. Destroy widgets before the window they live in.
  • Events. xtk_<widget>_handle_event() returns 0 for "not mine", 1 for "consumed" (Expose included) and 2 or more for a result the header names (e.g. a click). The menubar and popup menu return the chosen item or -1, so test >= 0 there. A disabled widget repaints on Expose and returns 0 for everything else.
  • Callbacks. One typed callback per event, widget pointer first, user_data last; setting one replaces the old, NULL clears it. They fire after the widget's state is updated. A callback for a user action (click, pick, drop) may destroy the widget; a header marks the ones that may not with "Callback safety".
  • Ownership. Strings and arrays passed in are copied; the caller keeps its own. Pointers returned are owned by the widget and stay valid until the next call that changes that value, or destroy. A function that returns memory the caller must free() says so. Theme/config pointers are borrowed and must outlive the widget unless the header says they are copied.

Dependencies

x11 xrender xft fontconfig freetype2 xcursor

plus libXext and libXpm at link time.

No third-party source is vendored in src/ — every decoder/parser/ rasterizer (image formats, Markdown, XML, the 2D vector engine, SVG) is this project's own implementation. A few of them were built by studying (not copying) existing small programs — see CREDITS.md for the full writeup, including xtk_markdown's adaptation of md4c, xtk_vector/xtk_svg's study of plutovg/plutosvg, and xtk_zoom's roots in the classic xzoom/grabc utilities.

Building

make            # produces libxtk.a
make install    # installs to /usr (override with PREFIX=, e.g. PREFIX=/usr/local)
make check      # the gallery's self-check, headless (needs xvfb-run)

make check builds the gallery and runs it with XTK_GALLERY_CHECK set: it holds every widget it shows to the conventions above (an event for another window returns 0, an Expose returns nonzero, a disabled widget ignores clicks, a hidden one stays hidden when placed), applies every built-in theme, shows every page, destroys everything, and exits nonzero on any failure or X error.

The widget gallery, test/xtk-gallery, shows every widget working, on ten pages, in any theme (View > Theme) - every range horizontal and vertical, sliders and scroll bars showing their hover:

cd test && make && ./xtk-gallery

A host program can embed it: XTK_GALLERY_THEME names the theme it draws (read again on SIGHUP), XTK_GALLERY_CONTROL_FD/XTK_GALLERY_REPLY_FD a command channel, and XTK_GALLERY_GEOMETRY ("X Y WIDTH HEIGHT" on the screen) where its window opens, unmanaged, so taking it in doesn't move it.

Using

xtk can be used in either of two ways.

As a library. Link against libxtk.a:

cc myapp.c -I/path/to/xtk/src /path/to/xtk/libxtk.a \
   $(pkg-config --libs x11 xrender xft fontconfig freetype2 xcursor) \
   -lm -lXext -lXpm

After make install, use -I$(PREFIX)/include/xtk and $(PREFIX)/lib/libxtk.a instead. The Debian package (xtk_<version>_amd64.deb, on the project's release page) installs the same, with the default PREFIX=/usr: the headers in /usr/include/xtk, the library as /usr/lib/libxtk.a, and it depends on the development packages of the X libraries. The library is compiled with -ffunction-sections -fdata-sections; link your application with -Wl,--gc-sections and unused widgets are stripped from the final binary.

Compiled in. Copy only the sources you need into your program and build them with it. Take each widget's .c/.h and the core files it includes (xtk_draw, xtk_surface, xtk_dbe, xtk_config, xtk_config_registry, xtk_xerror and so on); each file's #include lines name exactly what it depends on. Widgets register their own config sections, so a widget that isn't compiled in is simply absent.

Screenshots

The gallery's pages in the Light and Dark presets. Full-size images live in screenshots/.

Light Dark
Buttons
Text
Ranges
Lists
Rich text
Color
Data
Graphics
Docking
System

Credits

Prior art studied or adapted is listed in CREDITS.md.

License

Artistic License 2.0 — see LICENSE.