Drawing
The widgets draw themselves, but a program often has windows of its own to paint: a canvas, a preview, a custom widget. This page shows the parts of Xtk that the widgets draw with, so that a program can draw the same way: paint targets that do not flicker, shapes, colors and fonts, theme gradients, images, icons, vector graphics and shaped text.
Paint targets without flicker
Drawing straight into a window shows every step: the background, then the border, then the text, so a widget that is redrawn often flickers. An XtkSurface (xtk_surface.h) avoids that. It gives a window a paint target off the screen, and shows the finished picture in one step. It picks the best target the server has:
The first target that the server and the flags allow is used. The program draws the same way on each.
A surface belongs to one window and lives inside the program's own structure:
typedef struct {
Window window;
XtkSurface surface; /* zeroed before the first init */
GC gc;
} Canvas;
/* after XCreateWindow(): */
xtk_surface_init(&canvas->surface, display, screen, canvas->window, width, height,
XTK_SURFACE_XFT | XTK_SURFACE_PIXMAP_FALLBACK);
/* on Expose, and whenever it changes: */
Drawable target = xtk_surface_begin(&canvas->surface);
/* ... draw on target with canvas->gc, and text with canvas->surface.xft_draw ... */
xtk_surface_end(&canvas->surface);
/* after XResizeWindow(): */
xtk_surface_resized(&canvas->surface, new_width, new_height);
/* before XDestroyWindow(): */
xtk_surface_release(&canvas->surface);
| Call | What it does |
|---|---|
xtk_surface_init() |
Gives a window its target. The flags are XTK_SURFACE_XFT (keep an XftDraw for text, in surface.xft_draw), XTK_SURFACE_NO_DBE (never a back buffer) and XTK_SURFACE_PIXMAP_FALLBACK (a pixmap when there is no back buffer). The structure must be zeroed before the first call, and must stay at the same address until it is released. |
xtk_surface_begin(), xtk_surface_end() |
Start a paint (it returns the drawable to draw on) and show it. |
xtk_surface_resized() |
Tells the surface the window's new size. A back buffer follows the window by itself, so this is cheap enough for every step of a drag. |
xtk_surface_reallocate(), xtk_surface_reallocate_all() |
Make the buffers again, for one surface or for every surface of the display, and (for all) ask each window to repaint. This is a repair: some servers stop showing a buffer after a dialog of the window manager maps or unmaps next to the window. |
xtk_surface_drawable(), xtk_surface_is_buffered() |
The current target, and whether it is a back buffer. |
xtk_surface_release() |
Frees the buffer and the XftDraw. The window is the program's and is not destroyed. |
xtk_dbe.h is the lower layer: it asks the server for the double-buffer extension, and gives one shared context for each display (xtk_dbe_get_shared()) that the surfaces use.
Shapes and lines
xtk_draw.h draws the shapes the widgets are made of, with plain Xlib calls in the GC's color:
- Single shapes:
xtk_fill_rect(),xtk_stroke_rect(),xtk_fill_rounded_rect(),xtk_stroke_rounded_rect(),xtk_fill_bordered_rect()(a border and its inside, in two colors),xtk_fill_circle(),xtk_stroke_arc(),xtk_stroke_ellipse(),xtk_fill_polygon(),xtk_draw_polyline(),xtk_draw_line(), and fills with only some corners rounded (xtk_fill_rounded_rect_selective()...). - A path context (
XtkDrawContext):xtk_draw_init(), thenxtk_draw_move_to(),xtk_draw_line_to(),xtk_draw_arc(), andxtk_draw_stroke()orxtk_draw_fill()in the context's colors. - Window shapes (the X shape extension):
xtk_draw_apply_shape()cuts a window to a rounded rectangle,xtk_draw_apply_circle_shape()to a circle, andxtk_draw_apply_tooltip_shape()to a speech bubble, so the corners outside are not part of the window. - Smooth lines and circles with XRender:
xtk_draw_aa_line(),xtk_draw_aa_fill_circle()andxtk_draw_aa_stroke_circle(), whenxtk_draw_aa_available()says the server has it. - Shared-memory images (
XtkShmImage, MIT-SHM): a pixel buffer that the server reads directly, for large pictures, and gradient lines drawn through it (xtk_draw_gradient_hline(),xtk_draw_gradient_vline()).xtk_shm_available()says whether the server has it. - Text helpers:
xtk_text_centered_baseline()gives the baseline that centers a font in a box, andxtk_draw_mnemonic_underline()underlines the letter of anAltshortcut.
Colors and fonts
A color in the config is a ConfigColor: red, green, blue and alpha, each from 0 to 1. To draw with one:
| Call | What it gives |
|---|---|
xtk_config_color_to_pixel(), xtk_paint_pixel() |
The pixel value for a GC or a window background. |
xtk_config_color_to_xft() |
An XftColor for text, with its alpha. |
xtk_config_blend_color() |
A mixture of two colors. |
xtk_config_pixel_to_color() |
The color of a pixel value, the other way round. |
Fonts are opened by name through fontconfig with xtk_config_open_font(display, screen, family, size), the size in pixels; when nothing matches, it falls back to a sans font. A font rarely has every character. xtk_config_covering_font() gives, for one character, a font that has it: the intended font when it does, else the most similar installed font that does, and for an emoji a color font when one is installed (xtk_config_emoji_font()). The fonts it finds are kept for the life of the program.
Theme gradients and bevels
A theme can make a background a gradient and give it a bevel (Themes). To draw them in a window of its own, a program asks xtk_paint_for(section, key, flat) for the paint of a theme key (NULL when the theme gives that key no gradient, or when flat is not the gradient's average), and fills with xtk_paint_fill_rect(), xtk_paint_fill_rounded_rect() or xtk_paint_fill_bordered_rect(), which draw a flat color when the paint is NULL. xtk_paint_draw_bevel() draws the highlight and shade of a section, and xtk_paint_window_background() makes a gradient a window's background. xtk_paint_parse() reads a gradient from text, so a program can use the same syntax for its own settings.
Images, icons and vector graphics
| Header | What it gives |
|---|---|
xtk_image |
Reads PNG, JPEG, GIF, BMP and XPM files (or their bytes in memory) as a pixmap, as a picture with its alpha for XRender, or as RGBA pixels, at their own size or scaled down to fit. The decoders are Xtk's own. For files from untrusted places, decode them in the jailed worker instead (Safety). |
xtk_icon |
A set of named icons (folders, files, arrows, actions ...), drawn in one color at any size with xtk_icon_draw(), and the list of their names and display names. A name with a standard counterpart (Save, Open, Copy, Find ...) is drawn from the user's icon theme instead, in the theme's own colors, dimmed for something disabled (xtk_icon_draw_state()); xtk_icon_draw_file() draws a file's icon by its type. |
xtk_icon_theme |
The user's icon theme, as the freedesktop.org specifications say: which one it is (the program's own choice, else the one every xtk program shares in ~/.config/xtk/icons.conf, else the desktop's, read from XSETTINGS, GTK's or KDE's settings and followed while the program runs), the installed themes for a settings dialog, an icon's file by name and size through the themes a theme inherits, hicolor and the pixmaps, and a file's icon names from its type (shared-mime-info). xtk_icon_theme_lookup_key() finds the icon for one of xtk's icon keys: the Icon Naming Specification's name for what the key means, and where the spec has none (Save All, Close All, Build, choosing a color) the name the common themes use. A theme without that name keeps its look anyway: xtk_icon_draw() builds Save All, Close All and Build from the theme's own icons (two Saves; two documents with its Close as a badge; its source file with its gears as a badge), and the color chooser takes the theme's graphics-programs icon. Xtk's own icon only when even those are missing; never a legacy gtk-* name or another program's own name. xtk_icon_draw_symbolic() draws a key as GTK draws a symbolic icon, in one color: the theme's -symbolic icon (xtk_icon_theme_lookup_key_symbolic()), else Adwaita's, else Xtk's own, so a toolbar of them reads as one set under any theme; the rich text toolbar draws its icons this way. It looks theme by theme so the theme's own look wins, passing over an icon that is a link to, or the same file as, the icon of a key it must not look like (Save All and Save, Close All and Close). xtk_icon_theme_lookup_list() finds the best of a file's icon names, theme by theme (the spec's FindBestIcon). Lookups follow the Icon Theme, Icon Naming, shared-mime-info, Base Directory and XSETTINGS specifications; xtk_icon_theme_check() looks again when an icon folder changes. |
xtk_vector |
A small 2D rasterizer: paths of lines and curves, filled or stroked with anti-aliasing, solid colors and gradients, clipping and transforms, into an RGBA buffer. |
xtk_svg |
Draws SVG documents (icons and drawings) into RGBA images, with the rasterizer above: style sheets, the color property, lengths in every unit, use and symbol, clip paths, masks, filters, blend modes, markers, patterns, embedded and linked images, text in any font, and gzip-compressed files. It draws the icons of the installed icon themes as Inkscape does, and limits what a hostile file can make it do. |
xtk_gradient, xtk_color_space |
Color gradients with stops that blend in a chosen color space, drawn as strips, angled or radial previews, or written as CSS; and the color conversions they use. |
xtk_shape |
Text shaping from a font's OpenType tables: ligatures, the joined forms of Arabic letters, and emoji made of several characters. xtk_shape_run() gives the glyphs for a run of characters. |
Known limitations
- Xtk's own text widgets (the label, the entry, the text view and the rich text view) draw one glyph for each character and do not shape their text, and there is no glyph positioning (kerning, marks placed over letters).
- The rounded shapes of
xtk_draw.hare polygons without anti-aliasing; only thextk_draw_aa_*calls are smooth.
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
