Overview
Xtk is a small widget toolkit for X11, written in C. It draws its widgets itself with Xlib, Xft and XRender, and needs no other toolkit. This page says what is in it, the rules that every widget follows, and how to build and install it. The other pages of this guide show how to write a program with it; the API reference has one page for each header, with every type and function.
What is in the toolkit
Xtk is a library of separate parts. Each part is one header and one source file in src/. A program includes the headers of the parts it uses and links with libxtk.a. There is no central object that owns every widget: the program creates the widgets it needs, gives them the X events, and destroys them.
Each layer uses the layers below it. The program uses the widgets, and may use any other part directly. The red border marks the jail: that work runs in a separate process.
The parts, by group. Each name is a header in src/ and a page of the API reference.
| Group | Headers | What they give |
|---|---|---|
| Widgets | xtk_button, xtk_checkbox, xtk_radiobutton, xtk_label, xtk_entry, xtk_textview, xtk_edit_history, xtk_spinbutton, xtk_slider, xtk_progressbar, xtk_combobox, xtk_scrollbar, xtk_swatch, xtk_panel, xtk_statusbar, xtk_tooltip, xtk_toolbar, xtk_icongrid, xtk_treeview, xtk_datagrid, xtk_graph, xtk_scheme_wheel, xtk_zoom, xtk_tray |
Buttons and toggle buttons, check boxes, radio buttons, labels, one-line entries for text and numbers, a multi-line text editor and its undo history, spin buttons, sliders, progress bars, combo boxes, scroll bars, color swatches and color maps, a status bar, tooltips, an icon toolbar, a grid of icons, a tree view, an editable table, a node and edge graph, a color scheme wheel, a screen magnifier, and an icon in the system tray. |
| Containers and menus | xtk_grid, xtk_frame, xtk_notebook, xtk_splitpane, xtk_dockframe, xtk_toolwindow, xtk_menu, xtk_popup_menu |
A grid that lays out child windows, a titled frame, tabbed pages, a split pane, a frame of dockable and floating tool windows, a menu bar and popup menus. |
| Dialogs | xtk_filedialog, xtk_colorpicker |
A dialog to open or save files and choose folders, and a color picker dialog. |
| Rich text | xtk_richtext, xtk_richtext_toolbar, xtk_markdown, xtk_mathtext |
A rich text editor that reads and writes Markdown, its formatting toolbar, a Markdown parser, and LaTeX math drawn with a font's OpenType math table. |
| Drawing and images | xtk_draw, xtk_paint, xtk_surface, xtk_dbe, xtk_vector, xtk_svg, xtk_image, xtk_shape, xtk_icon, xtk_icon_theme |
Shapes and lines, theme gradients and bevels, flicker-free paint targets, the double-buffer extension, a 2D vector rasterizer, an SVG renderer, PNG, JPEG, GIF, BMP and XPM images, text shaping, a set of named icons, and the user's icon theme. |
| Color | xtk_color_space, xtk_gradient |
Conversions between color spaces (RGB, HSV, HSL, Lab, LCh, OKLCH, CAM16 and more), and color gradients: their stops, the color space they blend in, and drawing them. |
| Configuration and themes | xtk_config, xtk_config_registry, xtk_theme, xtk_version |
The settings of every widget and their file, themes, and the toolkit's version. |
| Input and the X server | xtk_clipboard, xtk_ime, xtk_focus, xtk_xerror |
The clipboard and the primary selection, input methods for composed and Asian text, moving the keyboard focus between widgets, and catching X errors. |
| Safety | xtk_sandbox, xtk_worker |
A jail for a process that reads untrusted data, and one jailed worker process for the program. |
| Data formats | xtk_json, xtk_xml, xtk_csv |
Parsers for JSON, XML (tolerant enough for HTML fragments, and able to turn them into plain text or Markdown) and CSV (which can become a Markdown table). |
The rules every widget follows
The widgets share a few conventions, so that learning one teaches most of the others. A header describes only where its widget differs.
- Each widget is a window. A widget is made as a child window of a window the program gives it, with
xtk_widget_create(), which returns the widget or NULL. Most widgets are made unmapped, and appear when they are placed (xtk_widget_set_position()) or shown (xtk_widget_show()).xtk_widget_destroy()frees everything the widget made, its windows too. Destroy the widgets before the window they live in. - A NULL widget is harmless. The functions of a widget accept NULL for the widget and do nothing (or return 0 or NULL).
- Events. The program gives every X event to
xtk_widget_handle_event(). It returns 0 when the event was not the widget's, 1 when the widget used it (anExposeof its window included), and 2 or more for a result that the header names, such as a click. The menu bar and the popup menu return the chosen item, or -1 when nothing was chosen; the menu bar also returnsXTK_MENUBAR_CONSUMEDwhen Escape only closed its open menu. A disabled widget still repaints itself onExpose, and returns 0 for everything else. See The Event Loop. - Callbacks. A widget has one callback for each kind of event, set with a
xtk_widget_set_..._callback()function. The widget comes first in its parameters and the program'suser_datapointer last; a few callbacks are not given the widget, such as the spin button's and the data grid's row callbacks. Setting a callback replaces the one before; NULL removes it. A callback runs after the widget's state has changed, so the widget already shows the new value. Where a callback must not destroy its own widget, the header says so where it describes the callback. - Ownership. Strings and arrays given to a widget are copied, unless the header says otherwise; the program keeps its own. A pointer that a widget returns belongs to the widget, and stays valid until the next call that changes that value, or until the widget is destroyed. A function that returns memory the program must free says so.
- Looks. A widget takes its colors, fonts and sizes from an
XtkConfig, a structure with one block for each kind of widget, whichxtk_config_init_defaults()fills with the defaults and a theme changes. Some widgets copy what they need from it, others keep a pointer to it, so keep theXtkConfigfor as long as the widgets live. To change the look of a running program, change the config (apply another theme) and call each widget'sxtk_widget_set_theme(). See Themes. - One thread. Like Xlib, the widgets are not thread-safe. Make every call of a widget from the thread that runs the event loop.
Building and installing
Xtk is built with make. It needs a C compiler that understands -std=gnu17 (GCC or Clang), make, pkg-config, and the development files of X11, Xrender, Xft, fontconfig, FreeType, Xcursor, Xext and Xpm. On Debian and Ubuntu, for example:
sudo apt install build-essential pkg-config libx11-dev libxrender-dev libxft-dev \
libfontconfig-dev libfreetype-dev libxcursor-dev libxext-dev libxpm-dev
Then, in the source folder:
| Command | What it does |
|---|---|
make |
Builds the static library libxtk.a. The build treats every warning as an error. The object files are removed afterwards; make KEEP_OBJS=1 keeps them. |
make install |
Installs the library to /usr/lib/libxtk.a and the headers to /usr/include/xtk/. Use PREFIX=/usr/local (or PREFIX=~/.local) to install elsewhere, and DESTDIR=... to stage the install for a package. |
make clean |
Removes the object files and the library. |
Using it in a program
There are two ways. As a library, a program is compiled with the headers and linked with libxtk.a and the X libraries:
cc myprogram.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/usr/include/xtk and /usr/lib/libxtk.a (or the folders under your PREFIX). The library is compiled with -ffunction-sections -fdata-sections, so linking with -Wl,--gc-sections leaves out the code the program never calls. First Program shows a whole example.
Compiled in, a program copies the source files it needs into its own tree and compiles them with its own code. Take each widget's .c and .h and the files it includes (such as xtk_draw, xtk_surface, xtk_dbe, xtk_config, xtk_config_registry, xtk_paint and xtk_xerror); the #include lines of each file name what it needs.
A widget adds its sections to the settings file by itself, when the program starts (see Themes). With a static library, only the widgets that the program uses are linked in, so only their sections exist: a program that has no tree view reads no [treeview] section.
The pages of this guide
-
A short, complete program with a label, an entry and a button: how it is built, and what each part of it does.
-
How events reach the widgets, what they return, callbacks, the functions a loop calls between events, keyboard focus, input methods, the clipboard and X errors.
-
The settings of every widget, the theme file and its role colors, gradients and bevels, where themes are found, and every section and key of the settings file.
-
Paint targets without flicker, shapes, anti-aliased lines, colors and fonts, gradients, images, icons, vector graphics and shaped text.
-
The jail for code that reads untrusted data, and the jailed worker that decodes pictures and runs a program's own jobs.
The API reference has a page for each header: its types, constants and functions, each parameter and each return value. The headers in src/ hold the same text, as comments.
xtk API
Widgets
- button
- checkbox
- radiobutton
- label
- entry
- textview
- edit_history
- spinbutton
- slider
- progressbar
- combobox
- scrollbar
- swatch
- panel
- statusbar
- tooltip
- toolbar
- icongrid
- iconview
- pathbar
- treeview
- datagrid
- graph
- scheme_wheel
- zoom
- tray
Containers and menus
Dialogs
Rich text
Drawing and images
Color
Configuration and themes
Input and the X server
Safety
Data formats
