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

xtk_icon.h

Named icons for rows, buttons and menus, drawn in one color at any size.

This module draws small icons by name ("key"). With an icon theme in use (xtk_icon_theme.h: the desktop's, unless the user chose another or xtk's own), a key that stands for a standard action or thing ("device-floppy" is "document-save") is drawn from the theme, in the theme's own colors, as GTK programs show it. Any other key, and any key the theme has no icon for, is one of xtk's own one-color icons. There are three kinds of those, and all are drawn with xtk_icon_draw():

  • Drawn glyphs. 26 keys, such as "person", "place", "book" and "gear", are drawn with plain Xlib calls (through xtk_draw.h) and need no files. They are made for tagging tree and list rows by the kind of thing they stand for. xtk_icon_list() gives their keys and xtk_icon_display_name() a name to show for each.
  • Asset icons. A fixed set of keys used by xtk's own widgets and common menus ("bold", "folder-open", "device-floppy", "settings", "chevron-down" and others) are SVG files from the Tabler icon set (https://tabler.io/icons, MIT licence).
  • Catalog icons. Every other SVG file in the icon folder. The catalog functions (xtk_icon_catalog_count(), xtk_icon_catalog_key(), xtk_icon_catalog_search(), xtk_icon_catalog_draw()) let a program offer an icon picker over the whole set.

The icon folder. Asset and catalog icons are read from a folder that holds an outline/ and a filled/ folder of SVG files. By default it is icons/ beside the program's binary, or, when that does not exist, ../share/<binary name>/icons/ from the binary's folder (so /usr/bin/foo reads /usr/share/foo/icons/); when neither exists, asset and catalog icons are not drawn. The binary is found through /proc/self/exe, which is Linux only. A program can name another folder with xtk_icon_set_asset_root(). xtk_icon_set_style() chooses between the outline and the filled set; a filled icon that does not exist is drawn from the outline set.

Color and caching. An SVG icon is rendered with xtk_svg.h in the color of the given pixel (its currentColor) and kept in a cache of 512 images as XRender pictures, keyed by style, key, size and color. When the cache is full, the icon drawn longest ago is dropped. Later draws of the same icon only composite the cached image. Call xtk_icon_cache_shutdown() before the display is closed to free those server resources.

Sizes. The size of every draw call is in pixels. For a screen with a high pixel density, compute it with xtk_icon_scaled_size().

Example.

int size = xtk_icon_scaled_size(display, DefaultScreen(display), 16);
xtk_icon_draw(display, window, gc, "person", 4, 4, size, text_pixel);
xtk_icon_draw(display, window, gc, "device-floppy", 4 + size + 4, 4, size, text_pixel);
// ... before XCloseDisplay():
xtk_icon_cache_shutdown(display);
 * 

Limitations.

  • SVG icons need a TrueColor or DirectColor default visual; with another visual they are not drawn and one warning is printed to stderr.
  • The color of an SVG icon is taken from the default screen's visual, whatever screen the drawable is on.
  • The catalog lists the outline set only, and it is read one time, on the first call of a catalog function.
#include "xtk_icon.h"

Types: XtkIconState

Functions: xtk_icon_draw, xtk_icon_draw_state, xtk_icon_draw_symbolic, xtk_icon_draw_named, xtk_icon_draw_file, xtk_icon_draw_named_state, xtk_icon_draw_file_state, xtk_icon_list, xtk_icon_display_name, xtk_icon_get_scale_factor, xtk_icon_scaled_size, xtk_icon_cache_shutdown, xtk_icon_set_style, xtk_icon_get_style, xtk_icon_set_asset_root, xtk_icon_catalog_count, xtk_icon_catalog_key, xtk_icon_catalog_display_name, xtk_icon_catalog_search, xtk_icon_catalog_draw

Types

XtkIconState

How a theme's icon is drawn: as it is, dimmed (something that cannot be used), or lightened (under the pointer).

Name Value Description
XTK_ICON_NORMAL 0 In its own colors.
XTK_ICON_DIMMED 1 Grey and faint.
XTK_ICON_LIGHTENED 2 Each color dodged by 15% grey, as Thunar lights the icon under the pointer.

Functions

xtk_icon_draw

void xtk_icon_draw(
    Display *display,
    Drawable drawable,
    GC gc,
    const char *key,
    int x,
    int y,
    int size,
    unsigned long pixel);

Draws one icon by its key in a square box.

A key "own:KEY" draws xtk's own icon for KEY, never the icon theme's: for something that keeps its look whatever the theme, such as a program's About ("own:info-circle"). A key "file:NAME" draws the icon of a file of that name, as xtk_icon_draw_file() does (a tree row can show a file's icon this way; "file:.c" is enough for the type). Any other key is looked up in this order: the user's icon theme, for a key with a standard name (xtk_icon_theme_names_for_key()), in the theme's colors (Save All "files", Close All "files-off" and Build "hammer", when the theme has no icon of theirs, built from its own: two of its Saves; two documents with its Close as a badge; its source file with its gears as a badge); an asset icon key, a drawn glyph key (see xtk_icon_list()), and last the name of a catalog SVG file without ".svg", in pixel. A key that is none of them draws nothing; this is not an error. An SVG icon is composited with its anti-aliased edges over what is already in the drawable.

Parameter Description
display The display connection.
drawable The window or pixmap to draw into.
gc A graphics context for the drawn glyphs. Its foreground is set to pixel and left so.
key The icon's key. NULL draws nothing.
x The X coordinate of the box's top left corner.
y The Y coordinate of the box's top left corner.
size The side of the box in pixels. 0 or less draws nothing. The glyph is scaled to the box.
pixel The pixel value of the color to draw in, as allocated on the display's default colormap.

xtk_icon_draw_state

void xtk_icon_draw_state(
    Display *display,
    Drawable drawable,
    GC gc,
    const char *key,
    int x,
    int y,
    int size,
    unsigned long pixel,
    int dimmed);

Draws an icon of the user's icon theme by its standard name, in the theme's colors.

The name is a freedesktop.org one, such as "document-save", "folder" or "text-x-csrc". The theme's file nearest the size is drawn fitted into the box. Draws one icon by its key as xtk_icon_draw() does, normal or dimmed.

Dimmed is for something that cannot be used now, such as a greyed toolbar button or menu item: a theme's icon is drawn in grey and faint, as GTK shows it; xtk's own icons are drawn in pixel, which the caller makes the dimmed color.

Parameter Description
display The display connection.
drawable The window or pixmap to draw into.
name The standard name.
x The X coordinate of the box's top left corner.
y The Y coordinate of the box's top left corner.
size The side of the box in pixels.
display The display connection.
drawable The window or pixmap to draw into.
gc A graphics context for the drawn glyphs.
key The icon's key. NULL draws nothing.
x The X coordinate of the box's top left corner.
y The Y coordinate of the box's top left corner.
size The side of the box in pixels.
pixel The color of xtk's own icons.
dimmed 1 to draw it dimmed.

Returns: 1 when it was drawn; 0 when the theme has no such icon (or the user chose xtk's own icons), and nothing was drawn.

xtk_icon_draw_symbolic

void xtk_icon_draw_symbolic(
    Display *display,
    Drawable drawable,
    GC gc,
    const char *key,
    int x,
    int y,
    int size,
    unsigned long pixel);

Draws one icon by its key as a symbolic icon: one color, pixel's.

The icon theme's symbolic icon for the key (each of its names with "-symbolic" after it), else Adwaita's symbolic icon (GTK's own fallback), else xtk's own icon; all of them drawn in pixel's color only, so a row of icons from the three reads as one set and follows the window's text color. For formatting toolbars, and any toolbar a program draws "symbolic". A disabled button passes its dimmed color.

Parameter Description
display The X display
drawable Where to draw
gc A graphics context, for xtk's own drawn glyphs
key The icon key, as for xtk_icon_draw()
x The box's left edge
y The box's top edge
size The box's size in pixels
pixel The one color

xtk_icon_draw_named

int xtk_icon_draw_named(
    Display *display,
    Drawable drawable,
    const char *name,
    int x,
    int y,
    int size);

xtk_icon_draw_file

void xtk_icon_draw_file(
    Display *display,
    Drawable drawable,
    GC gc,
    const char *file_name,
    int is_folder,
    int x,
    int y,
    int size,
    unsigned long pixel);

Draws a file's or a folder's icon: the icon theme's for its type, else xtk's own.

The type comes from the file's name (xtk_icon_theme_file_icons()). With no theme icon for it, xtk's "folder" or "file" icon is drawn in pixel.

Parameter Description
display The display connection.
drawable The window or pixmap to draw into.
gc A graphics context, for xtk's own icons.
file_name The file's name or path.
is_folder 1 for a folder.
x The X coordinate of the box's top left corner.
y The Y coordinate of the box's top left corner.
size The side of the box in pixels.
pixel The color of xtk's own icon, when that is drawn.

xtk_icon_draw_named_state

int xtk_icon_draw_named_state(
    Display *display,
    Drawable drawable,
    const char *name,
    int x,
    int y,
    int size,
    XtkIconState state);

xtk_icon_draw_named() in a state.

Returns: 1 when the theme had the icon and it was drawn, else 0

xtk_icon_draw_file_state

void xtk_icon_draw_file_state(
    Display *display,
    Drawable drawable,
    GC gc,
    const char *file_name,
    int is_folder,
    int x,
    int y,
    int size,
    unsigned long pixel,
    XtkIconState state);

xtk_icon_draw_file() in a state; xtk's own icon (no theme icon) is drawn as it is.

xtk_icon_list

const char *const *xtk_icon_list(int *count_out);

Gives the keys of the drawn glyphs, in a fixed order for display.

Only the 26 drawn glyphs are listed, not the asset or catalog icons.

Parameter Description
count_out Receives the number of keys; may be NULL.

Returns: A static array of keys that the caller must not free or change. It does not end with NULL, so use count_out.

xtk_icon_display_name

const char *xtk_icon_display_name(const char *key);

Gives the name to show for a drawn glyph's key.

For example "person" gives "Person" and "speech" gives "Dialogue". For a catalog key, use xtk_icon_catalog_display_name().

Parameter Description
key A key from xtk_icon_list().

Returns: A static string; NULL when key is NULL or not a drawn glyph's key.

xtk_icon_get_scale_factor

double xtk_icon_get_scale_factor(Display *display, int screen);

Gives the screen's scale factor for sizing icons and other pixel measures on screens with a high pixel density.

The factor is the screen's dots per inch divided by 96, from the width in pixels and in millimeters that the X server reports. The draw functions do not apply it themselves; a caller uses it to compute their size.

Parameter Description
display The display connection; NULL gives 1.0.
screen The screen number.

Returns: About 1.0 on a 96 dpi screen and about 2.0 on a 192 dpi screen. It is never less than 1.0, and it is 1.0 when the server reports a size of 0.

xtk_icon_scaled_size

int xtk_icon_scaled_size(Display *display, int screen, int base_size);

Scales an icon size by xtk_icon_get_scale_factor().

Parameter Description
display The display connection.
screen The screen number.
base_size The size in pixels for a 96 dpi screen.

Returns: base_size times the scale factor, rounded to the nearest pixel, and at least 1.

xtk_icon_cache_shutdown

void xtk_icon_cache_shutdown(Display *display);

Frees all cached SVG icon images and their X server resources.

Call it once before the display is closed. It is safe when no SVG icon was drawn. Icons drawn after it are rendered and cached again.

Parameter Description
display The connection the icons were drawn on.

xtk_icon_set_style

void xtk_icon_set_style(const char *style);

Chooses the outline or the filled set for SVG icons.

The choice applies to asset and catalog icons from the next draw on; drawn glyphs do not change. An icon that has no file in the filled set, such as the text formatting icons, is drawn from the outline set.

Parameter Description
style "filled" for the filled set. Any other value, NULL too, chooses "outline", which is the default.

xtk_icon_get_style

const char *xtk_icon_get_style(void);

Gives the icon set chosen with xtk_icon_set_style().

Returns: "outline" or "filled", in a static buffer that the next xtk_icon_set_style() overwrites. "outline" when the style was never set.

xtk_icon_set_asset_root

void xtk_icon_set_asset_root(const char *directory);

Sets the folder that SVG icons are read from.

The folder is the one that holds outline/ and filled/. Use it when the program's icons are not in one of the default places (see the file description). Call it before the first icon is drawn: the catalog is read only one time, and icons already in the cache are not read again.

Parameter Description
directory The folder, with or without a trailing '/'. NULL or "" goes back to the default folder.

xtk_icon_catalog_count

int xtk_icon_catalog_count(void);

Gives the number of icons in the catalog.

The catalog is every .svg file in the icon folder's outline/ folder. The first call of any catalog function reads the folder, and the list is kept until the program exits.

Returns: The number of icons; 0 when the folder cannot be read.

xtk_icon_catalog_key

const char *xtk_icon_catalog_key(int index);

Gives the key of a catalog icon: its file name without ".svg".

The key can be drawn with xtk_icon_catalog_draw(), and also with xtk_icon_draw() when it is not the key of a drawn glyph.

Parameter Description
index The icon's index, from 0 to xtk_icon_catalog_count() - 1. The order is the order of the folder listing and does not change while the program runs.

Returns: The key, owned by the catalog; NULL when index is out of range.

xtk_icon_catalog_display_name

void xtk_icon_catalog_display_name(const char *key, char *out, size_t out_size);

Makes a name to show from a catalog key.

Hyphens and underscores become spaces and each word starts with a capital letter, so "alert-triangle" gives "Alert Triangle". This only changes the text; the key is not looked up.

Parameter Description
key The key. NULL gives an empty string.
out Receives the name, always ended with a NUL byte and cut short when it does not fit.
out_size The size of out in bytes. 0 writes nothing.
int xtk_icon_catalog_search(const char *query, int *out_indices, int out_cap);

Finds the catalog icons whose key or tags contain a text.

The test ignores case. The tags are the tags: [...] list that Tabler files have in a comment near their start.

Parameter Description
query The text to look for. NULL or "" matches every icon.
out_indices Receives the indices of the matches, in catalog order. Give it xtk_icon_catalog_count() entries to be sure to get all.
out_cap The number of entries in out_indices. Matches past this number are not written.

Returns: The number of indices written to out_indices.

xtk_icon_catalog_draw

void xtk_icon_catalog_draw(
    Display *display,
    Drawable drawable,
    const char *key,
    int x,
    int y,
    int size,
    unsigned long pixel);

Draws a catalog icon by its file name.

Use this for keys from xtk_icon_catalog_key(), for example in an icon picker: unlike xtk_icon_draw(), it never draws a glyph with the same key instead. The icon is read from the chosen style's folder (see xtk_icon_set_style()) and cached as for xtk_icon_draw(). Nothing is drawn when the file does not exist.

Parameter Description
display The display connection.
drawable The window or pixmap to draw into.
key The SVG file's name without ".svg", such as a key from xtk_icon_catalog_key(). NULL draws nothing.
x The X coordinate of the box's top left corner.
y The Y coordinate of the box's top left corner.
size The side of the box in pixels. 0 or less draws nothing.
pixel The pixel value of the color to draw in.