First Program
This page walks through a short and complete Xtk program: a window that asks for a name, with a text entry and a button that answers. It shows the steps that every Xtk program takes: open the display, choose the look, make the widgets, and run the event loop.
What it does
The program opens a window with a label that says "What is your name?", a one-line entry under it and a button labelled Greet. Type a name, click Greet (or press Space or Enter while the button has the focus), and the label says "Hello, name!". Closing the window ends the program.
The window after a click on Greet. Each widget is a child window of the program's own window.
The program
Save it as hello.c.
/* hello.c - a first xtk program: a label, a text entry and a button. */
#include <stdio.h>
#include <sys/select.h>
#include <X11/Xlib.h>
#include "xtk_button.h"
#include "xtk_clipboard.h"
#include "xtk_config.h"
#include "xtk_entry.h"
#include "xtk_label.h"
#include "xtk_paint.h"
#include "xtk_theme.h"
static XtkLabel *label;
static XtkEntry *entry;
/* Called when the button is clicked (or pressed with Space or Enter). */
static void on_greet(XtkButton *button, void *user_data) {
(void)button;
(void)user_data;
char text[1100];
snprintf(text, sizeof(text), "Hello, %s!", xtk_entry_get_text(entry));
xtk_label_set_text(label, text);
}
int main(void) {
Display *display = XOpenDisplay(NULL);
if (!display) {
fprintf(stderr, "hello: cannot open the display\n");
return 1;
}
const int screen = DefaultScreen(display);
/* The look: xtk's defaults, then one of its built-in themes over them. */
static 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);
/* The program's own top-level window. */
Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, 360, 110, 0, 0,
xtk_paint_pixel(display, screen, config.main.background));
XStoreName(display, window, "Hello");
XSelectInput(display, window, ExposureMask | StructureNotifyMask);
Atom delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &delete_window, 1);
/* The widgets, each a child window of it. */
XtkClipboard *clipboard = xtk_clipboard_create(display);
label = xtk_label_create(display, screen, window, 16, 16, 328, config.label_widget.padding, 0, 0, 0,
"What is your name?", &config.label);
XtkEntryConfig entry_config = {
.kind = ENTRY_TEXT,
.x_position = 16,
.y_position = 56,
.width = 220,
.padding = config.entry_widget.padding,
.border_width = config.entry_widget.border_width,
.border_radius = config.entry_widget.border_radius,
};
entry = xtk_entry_create(display, screen, window, &config, &entry_config, clipboard);
XtkButton *button = xtk_button_create(display, window, &config.button, 96, 30, 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);
if (!clipboard || !label || !entry || !button) {
fprintf(stderr, "hello: cannot make the widgets\n");
return 1;
}
xtk_button_set_label(button, "Greet");
xtk_button_set_clicked_callback(button, on_greet, NULL);
xtk_label_show(label);
xtk_entry_show(entry);
xtk_button_set_position(button, 248, 56);
XMapWindow(display, window);
/* The event loop: every event to the widgets, and the caret's blink in between. */
const int connection = ConnectionNumber(display);
int running = 1;
while (running) {
while (running && XPending(display)) {
XEvent event;
XNextEvent(display, &event);
if (event.type == ClientMessage && (Atom)event.xclient.data.l[0] == delete_window) {
running = 0;
break;
}
if (xtk_clipboard_handle_event(clipboard, &event)) continue;
if (xtk_entry_handle_event(entry, &event)) continue;
if (xtk_button_handle_event(button, &event)) continue;
xtk_label_handle_event(label, &event);
}
xtk_entry_update_blink(entry);
XFlush(display);
fd_set ready;
FD_ZERO(&ready);
FD_SET(connection, &ready);
struct timeval wait = {0, 50000}; /* at most 50 ms, for the blink */
select(connection + 1, &ready, NULL, NULL, &wait);
}
xtk_button_destroy(button);
xtk_entry_destroy(entry);
xtk_label_destroy(label);
xtk_clipboard_destroy(clipboard);
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}
Building it
Build Xtk first (make in its folder, see Overview). Then compile the program with Xtk's headers, and link it with libxtk.a and the X libraries:
cc -std=gnu17 -Wall -Wextra hello.c -o hello -I/path/to/xtk/src \
$(pkg-config --cflags x11 xrender xft fontconfig freetype2 xcursor) \
/path/to/xtk/libxtk.a \
$(pkg-config --libs x11 xrender xft fontconfig freetype2 xcursor) -lm -lXext -lXpm
After make install, the headers are in /usr/include/xtk and the library is /usr/lib/libxtk.a (or the same folders under the PREFIX you gave). Then run it with ./hello.
How it works
1. The display and the look
XOpenDisplay() connects to the X server, as in any Xlib program. Then the program decides how its widgets look. xtk_config_init_defaults() fills an XtkConfig with the toolkit's defaults: one block of colors, fonts and sizes for each kind of widget (config.button, config.label, config.entry_text ...) and the sizes that go with them (config.button_widget, config.entry_widget ...). A theme then sets its colors over those defaults: here the built-in theme "Dark", with xtk_theme_builtin() and xtk_theme_apply(). The theme itself is no longer needed once it is applied, so it is freed; the config must stay, because the widgets read it. It is static because it is large. Themes explains themes, the theme files a user can change, and the settings file.
2. The window
Xtk has no top-level window type: the program makes its main window itself, with plain Xlib, and gives it the theme's window color, config.main.background. xtk_paint_pixel() turns that color into the pixel value X needs. The WM_DELETE_WINDOW protocol makes the window manager send a message when the user closes the window, instead of cutting the connection.
3. The widgets
Each widget is made as a child window of the program's window. Most of them need the display, a parent window, a style block from the config, and their size:
- The label (
xtk_label_create()) gets its place (16, 16), its width (328 pixels; 0 would make it as wide as its text), the padding fromconfig.label_widget, no border, its text, and its colors and font fromconfig.label. It copies the text and the style block. - The entry (
xtk_entry_create()) is described by anXtkEntryConfig: its kind (ENTRY_TEXT; there are also entries for whole numbers, decimal numbers and hexadecimal), its place, its width, and its padding, border and corner radius fromconfig.entry_widget. Fields left out of the initializer are zero, which means no length limit and no change callback. The entry also takes the whole config (its colors come from[entry-text], its caret and selection from[main]and[behavior]) and a clipboard, for copy and paste. - The button (
xtk_button_create()) gets its style block,config.button, its size, and the padding, border widths and corner radius fromconfig.button_widget.xtk_button_set_label()gives it its text, andxtk_button_set_clicked_callback()the function to call when it is clicked.
The widgets are made unmapped. xtk_label_show() and xtk_entry_show() map them where they were made; xtk_button_set_position() places the button and maps it. Last, XMapWindow() shows the program's window, with everything in it.
4. The callback
on_greet() runs when the button is clicked. Like every Xtk callback, it gets the widget first and the program's user_data pointer last (NULL here). It reads the entry's text with xtk_entry_get_text(), which returns the entry's own copy, and gives the label its new text with xtk_label_set_text(), which copies it. The label redraws itself when the next events reach it.
5. The event loop
Xtk has no main loop of its own: the program runs it, and gives each X event to the widgets.
XPending()andXNextEvent()take the events that have arrived.- The close message from the window manager ends the loop.
- The clipboard comes first, because it answers other programs that want the text the user copied.
- Then each widget gets the event, until one returns a value other than 0, which means the event was its own. A click on the button makes
xtk_button_handle_event()callon_greet()and return 2. - Between events,
xtk_entry_update_blink()blinks the entry's caret.select()waits for the next event, but not longer than 50 milliseconds, so that the caret keeps blinking while nothing happens.
At the end, the widgets are destroyed before the window they live in, and the clipboard after the entry that uses it.
The Event Loop says more about events, return values, the functions to call between events, and keyboard focus.
Where to go next
- Move the focus with the keyboard. An
XtkFocusManagermoves the keyboard focus from widget to widget withCtrl+TabandCtrl+Shift+Tab(The Event Loop). - Typing accents and Asian text. Call
setlocale(LC_ALL, "")before opening the display, open an input method withxtk_ime_open(), give it to the entry withxtk_entry_set_ime(), and letXFilterEvent()see each event first (The Event Loop). - Let the user choose a theme.
xtk_theme_load_named(&config, "hello", "Dark")reads the theme "Dark" from the user's theme folders,~/.config/hello/themes/first, and falls back to the built-in one.xtk_theme_install_builtins()copies the built-in themes there for the user to change (Themes). - Show pictures from files you did not make. Decode them in the jailed worker (Safety).
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
