1 xtk_theme
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_theme.h

Themes: a few role colors that set every widget's colors, plus overrides for single keys.

A theme sets the colors of all the widgets at once. Its core is a set of role colors (XtkRole): the window color, the text on it, the field color, the accent, and so on. xtk_theme_apply() works out every widget color of an XtkConfig from the roles, so a theme of three or four colors already gives a complete look; a role the theme leaves out is derived from the others. On top of the roles, a theme can set any single key of any section, as a config file does (overrides), including gradients and bevels (see xtk_paint.h) and sizes. A program can also keep its own colors in a theme, in the [app] section.

Using a theme. A program keeps the theme's name in its config ([main] theme = Dark). After it has read its config, and before it makes its widgets, it calls xtk_theme_load_named(), which looks for the theme in the program's own themes folder, then among the installed themes, then among the 14 built-in ones, and applies it.

Example.

XtkConfig config;
xtk_config_init_defaults(&config);
xtk_config_load(&config, config_path);          // may set [main] theme
if (!xtk_theme_load_named(&config, "myprogram", config.main.theme)) {
    XtkTheme *theme = xtk_theme_new();
    if (theme && xtk_theme_builtin("Dark", theme)) xtk_theme_apply(&config, theme);
    xtk_theme_free(theme);
}
// ... now make the widgets with &config.button and the other blocks ...
 * 

Theme file format. A theme is a text file named Name.conf, in the same line format as a config file (see xtk_config.h): [section] lines, key = value lines, and comment lines that start with # or ;. There are no comments at the end of a line.

[theme]
name = Dark

[colors]
window = #2D2D2D
window-text = #E5E5EA
accent = #598CE5

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

[button-widget]
border-radius = 4

[app]
editor-keyword = #CC7832
 * 
  • [theme]: name, the theme's name. Without it, xtk_theme_read() names the theme after its file (without .conf).
  • [colors]: the roles, by the names in xtk_theme_role_names (window, window-text, base, base-text, button, button-text, border, accent, accent-text, hover, active, muted, link, valid, invalid, tooltip, tooltip-text). A value can be any color xtk_paint_parse_color() reads; alpha is ignored. Unknown names and colors that cannot be read are skipped.
  • [app]: the program's own keys. Its lines are kept as they are (comment lines too, without their leading and trailing blanks; empty lines are dropped) until the next [section] line, and read with xtk_theme_app_get(). The toolkit does not use them.
  • Any other section: overrides. Each key = value is kept and later set with xtk_config_set(), so it means what it means in a config file: color keys (a color or a gradient) in the widget sections, highlight and shade for a bevel, and size keys in the [name-widget] sections.
  • Lines before the first section are ignored.

Derived roles. A role the theme does not give is worked out as follows (a dark theme is one whose window color has a luminance below 0.5):

  • window: #2D2D2D
  • window-text: #E5E5EA on a dark window, #1E1E1E on a light one
  • base: the window color 15% toward black (dark) or 80% toward white (light)
  • base-text, button-text: window-text
  • button: the window color 6% toward window-text
  • border: the window color 35% toward window-text
  • accent: #3584E4
  • accent-text: white when the accent's luminance is below 0.55, else black
  • hover: the button color 10% toward window-text
  • active: the button color 30% toward the accent
  • muted: window-text 40% toward the window color
  • link: the accent
  • valid: #26A269; invalid: #E01B24
  • tooltip: base; tooltip-text: base-text

Where themes are found.

  1. The program's own themes folder: $XDG_CONFIG_HOME/program/themes, or ~/.config/program/themes when XDG_CONFIG_HOME is not set or is a relative path (xtk_theme_directory()).
  2. Installed themes, as GTK themes are installed, in Theme/xtk/Name.conf inside ~/.themes, then $XDG_DATA_HOME/themes (or ~/.local/share/themes), then /usr/share/themes. These are read only.
  3. The built-in themes (xtk_theme_builtin()), found by xtk_theme_load_named() when no file has the name.

When two folders have a theme of the same name, the first one in this order is used.

Limitations.

#include "xtk_theme.h"

Types: XtkRole, XtkThemeOverride, XtkTheme, XtkThemeEntry

Constants: xtk_theme_role_names, XTK_THEME_OVERRIDE_MAX, XTK_THEME_BUILTIN_COUNT, xtk_theme_builtin_names

Functions: xtk_theme_role_find, xtk_theme_new, xtk_theme_free, xtk_theme_copy, xtk_theme_read, xtk_theme_write, xtk_theme_parse, xtk_theme_write_file, xtk_theme_resolve_roles, xtk_theme_set_role, xtk_theme_override_get, xtk_theme_override_set, xtk_theme_app_get, xtk_theme_app_set, xtk_theme_apply, xtk_theme_apply_colors, xtk_theme_load, xtk_theme_capture, xtk_theme_complete, xtk_theme_directory, xtk_theme_list, xtk_theme_path, xtk_theme_list_all, xtk_theme_find, xtk_theme_load_named, xtk_theme_builtin, xtk_theme_install_builtins

Types

XtkRole

A color role: one job a color does across every widget.

Name Value Description
XTK_ROLE_WINDOW Window backgrounds (window)
XTK_ROLE_WINDOW_TEXT Text on window backgrounds (window-text)
XTK_ROLE_BASE Fields: entries, lists, text views (base)
XTK_ROLE_BASE_TEXT Text in fields (base-text)
XTK_ROLE_BUTTON Buttons, combo boxes, tabs, headers (button)
XTK_ROLE_BUTTON_TEXT Text on buttons (button-text)
XTK_ROLE_BORDER Borders and frames (border)
XTK_ROLE_ACCENT Selection, focus, checked marks, the slider's fill (accent)
XTK_ROLE_ACCENT_TEXT Text on the accent color (accent-text)
XTK_ROLE_HOVER What the pointer is over (hover)
XTK_ROLE_ACTIVE Pressed buttons, open menus, the active tab (active)
XTK_ROLE_MUTED Secondary text, line numbers, icons (muted)
XTK_ROLE_LINK Links (link)
XTK_ROLE_VALID A value that is valid, such as an entry's valid border (valid)
XTK_ROLE_INVALID A value that is not valid (invalid)
XTK_ROLE_TOOLTIP Tooltip backgrounds (tooltip)
XTK_ROLE_TOOLTIP_TEXT Text on tooltips (tooltip-text)
XTK_ROLE_COUNT The number of roles.

XtkThemeOverride

One key a theme sets outright, as a config file line would.

Field Type Description
section[40] char The config section, for example "button".
key[48] char The key in it, for example "background".
value[200] char Its value, as a config file writes it.

XtkTheme

A theme: role colors, overrides and a program's own [app] keys.

Make it with xtk_theme_new() and free it with xtk_theme_free(); change it through the functions below rather than the fields.

Field Type Description
name[64] char The theme's name.
roles[XTK_ROLE_COUNT] ConfigColor Each role's color, where the theme gives it.
roles_set unsigned int One bit per role (1 << role) that the theme gives; the other roles are derived.
n int The number of overrides in over.
over[XTK_THEME_OVERRIDE_MAX] XtkThemeOverride The overrides, in the order they were given.
app char * The [app] section's lines, as written, one per line (on the heap; NULL if none)

XtkThemeEntry

A theme a program can use: one in its own folder, or an installed one (read only).

Field Type Description
name[64] char The theme's name (its file name without .conf)
path[1200] char The theme file's path.
system int 1 for an installed theme (read only), 0 for one in the program's own folder

Constants

xtk_theme_role_names

const char* const xtk_theme_role_names[XTK_ROLE_COUNT][XTK_ROLE_COUNT];

The roles' names as a theme's [colors] section writes them, indexed by XtkRole.

XTK_THEME_OVERRIDE_MAX

#define XTK_THEME_OVERRIDE_MAX 512

The most overrides a theme holds.

XTK_THEME_BUILTIN_COUNT

#define XTK_THEME_BUILTIN_COUNT 14

The number of built-in themes: starting points that a program can write into its own themes folder.

xtk_theme_builtin_names

const char* const xtk_theme_builtin_names[XTK_THEME_BUILTIN_COUNT][XTK_THEME_BUILTIN_COUNT];

The built-in themes' names: Chocolate, Dark, Ember, Forest, Gold, Graphite, Light, Midnight, Ocean, Rose, Rust, Slate, Teal and Violet.

Functions

xtk_theme_role_find

int xtk_theme_role_find(const char *name);

Find a role by its name.

Parameter Description
name The role's name, as in xtk_theme_role_names, for example "accent"

Returns: The role, or -1 if no role has that name (or name is NULL)

xtk_theme_new

XtkTheme *xtk_theme_new(void);

Make a new, empty theme: no name, no roles, no overrides.

Returns: The theme, which the caller frees with xtk_theme_free(); or NULL if memory ran out

xtk_theme_free

void xtk_theme_free(XtkTheme *theme);

Free a theme made by xtk_theme_new(), with its [app] text.

Parameter Description
theme The theme, or NULL

xtk_theme_copy

void xtk_theme_copy(XtkTheme *destination, const XtkTheme *source);

Copy one theme over another.

The destination's old [app] text is freed and the source's is duplicated. Does nothing if either is NULL or both are the same theme.

Parameter Description
destination The theme to overwrite
source The theme to copy

xtk_theme_read

int xtk_theme_read(XtkTheme *theme, const char *path);

Read a theme file.

Whatever the theme held before is dropped. The file is read in the format described at the top of this page. Without a [theme] name, the theme is named after the file, without .conf.

Parameter Description
theme The theme that receives the file (from xtk_theme_new())
path The file's path

Returns: 1 if the file was read, 0 if an argument is NULL or the file could not be opened. The contents are not checked: a file with nothing in it gives an empty theme and 1.

xtk_theme_write

int xtk_theme_write(const XtkTheme *theme, const char *path);

Write a theme file.

An existing file is replaced. See xtk_theme_write_file() for what is written.

Parameter Description
theme The theme
path The file's path

Returns: 1 on success, 0 if an argument is NULL or the file could not be opened, written or closed

xtk_theme_parse

int xtk_theme_parse(XtkTheme *theme, const char *text, const char *name);

Read a theme from text in the theme file format.

Whatever the theme held before is dropped.

Parameter Description
theme The theme that receives the text (from xtk_theme_new())
text The text, as a theme file holds it
name The theme's name when the text has no [theme] name; NULL gives ""

Returns: 1 on success, 0 if theme or text is NULL or the text could not be read

xtk_theme_write_file

int xtk_theme_write_file(const XtkTheme *theme, FILE *file);

Write a theme into an open file.

Writes a short comment header, [theme] with the name, [colors] with the roles the theme gives (always the section line, even with no roles), each section of overrides once, in the order its first override was given, and the [app] lines.

Parameter Description
theme The theme
file The open file

Returns: 1 if nothing failed, 0 if an argument is NULL or a write failed

xtk_theme_resolve_roles

void xtk_theme_resolve_roles(const XtkTheme *theme, ConfigColor out[XTK_ROLE_COUNT]);

Every role's color: the theme's where it gives one, else derived from the others.

The rules are listed at the top of this page. All the colors are opaque.

Parameter Description
theme The theme; NULL gives every role its derived default
out Receives one color per role, indexed by XtkRole

xtk_theme_set_role

void xtk_theme_set_role(XtkTheme *theme, XtkRole role, ConfigColor color, int set);

Give a role a color, or leave it to be derived.

Parameter Description
theme The theme
role The role
color Its color (alpha is ignored; stored even when set is 0)
set 1 to set the role, 0 to have it derived from the others

xtk_theme_override_get

const char *xtk_theme_override_get(
    const XtkTheme *theme,
    const char *section,
    const char *key);

The value a theme sets for one key.

Parameter Description
theme The theme
section The config section
key The key in it

Returns: The value, owned by the theme and valid until an override of the theme changes or is removed; or NULL if the theme does not set the key

xtk_theme_override_set

int xtk_theme_override_set(
    XtkTheme *theme,
    const char *section,
    const char *key,
    const char *value);

Set the value of one key in a theme, or remove it.

A new key is added at the end; an existing one keeps its place. Section, key and value are cut to the sizes of XtkThemeOverride; a section or key that is cut is looked up as cut, so setting it again changes the same override.

Parameter Description
theme The theme
section The config section
key The key in it
value The value, as a config file writes it, or NULL to remove the key

Returns: 1 on success (also when removing a key the theme does not have); 0 if theme, section or key is NULL, or the key is new and the theme already holds XTK_THEME_OVERRIDE_MAX overrides

xtk_theme_app_get

int xtk_theme_app_get(const XtkTheme *theme, const char *key, char *out, size_t size);

The value of one of the program's [app] keys.

Parameter Description
theme The theme
key The key
out Receives the value, without the blanks around it; cut to fit
size The size of out in bytes

Returns: 1 if the key was found, 0 if not (or an argument is NULL or size is 0)

xtk_theme_app_set

int xtk_theme_app_set(XtkTheme *theme, const char *key, const char *value);

Set one of the program's [app] keys, or remove it.

Any line for the key is removed, and the new line key = value is added at the end of the [app] lines. The line is cut to 511 characters.

Parameter Description
theme The theme
key The key
value The value, or NULL to remove the key

Returns: 1 on success, 0 if theme or key is NULL or empty, or memory ran out

xtk_theme_apply

void xtk_theme_apply(XtkConfig *config, const XtkTheme *theme);

Set a config's widget colors from a theme.

In order: every widget color of config is set from the roles; the paint table (xtk_paint.h) is emptied; every override is set with xtk_config_set(), so gradients and bevels go into the paint table. Finally, where the theme sets a menu's, the menu bar's or the tray menu's color but not its hover-color, the hover text color follows the text color. Fonts and sizes not named in an override keep their values. This does not record the theme's name in config; xtk_theme_load_named() does.

Parameter Description
config The configuration to update
theme The theme

xtk_theme_apply_colors

void xtk_theme_apply_colors(XtkConfig *config, const XtkTheme *theme);

Set a config's colors from a theme without touching the paint table.

Roles first, then only the overrides that are color keys (see xtk_config_color_field()), a gradient as its average color, then the hover-color rule of xtk_theme_apply(). For working out what a theme looks like, for example in a theme editor's preview, not for drawing the program's widgets.

Parameter Description
config The configuration to update
theme The theme

xtk_theme_load

int xtk_theme_load(XtkConfig *config, const char *path);

Read a theme file and apply it to a config with xtk_theme_apply().

Parameter Description
config The configuration to update
path The theme file's path

Returns: 1 if the file was read (and applied, when config is not NULL); 0 if it could not be read, and config is then unchanged

xtk_theme_capture

int xtk_theme_capture(XtkTheme *theme, const XtkConfig *config);

Add overrides to a theme, so that it gives a config's colors exactly.

For each color in config whose value differs from what the theme's roles alone would give, an override is added with the config's value. Use it to turn a program's current look, or an older theme format, into a theme. The sections [behavior], [clipboard], [sizes], [zoom], [zoom-widget] and [zoom-hex] are left out.

Parameter Description
theme The theme to add overrides to (its roles should be set first)
config The configuration whose colors are kept

Returns: The number of overrides added

xtk_theme_complete

int xtk_theme_complete(XtkTheme *theme);

Add every color key to a theme, with the value the theme gives it.

Each color key of [main] and of each registered widget section that the theme does not set yet is added as an override, with the color the theme draws it in. A key the theme already sets keeps its value (a gradient stays a gradient). The theme file then holds all of the toolkit's colors, not only the ones the theme changes. Only the sections of widgets linked into the program are included.

Parameter Description
theme The theme

Returns: The number of keys added

xtk_theme_directory

int xtk_theme_directory(const char *program, char *out, size_t size);

The path of a program's own themes folder.

$XDG_CONFIG_HOME/program/themes, or $HOME/.config/program/themes when XDG_CONFIG_HOME is not set or is not an absolute path (the XDG rule). The folder is not made; xtk_theme_install_builtins() makes it.

Parameter Description
program The program's name
out Receives the path
size The size of out in bytes

Returns: 1 on success, 0 if neither XDG_CONFIG_HOME nor HOME is set (or an argument is NULL or size is 0)

xtk_theme_list

int xtk_theme_list(const char *directory, char names[][64], int max);

The names of the themes in a folder, sorted alphabetically with case ignored.

Each file Name.conf (not starting with a dot, with a name shorter than 64 characters) gives the name Name. When the folder holds more than max themes, the first max of the sorted list are written.

Parameter Description
directory The folder
names Receives the names
max The most names to write

Returns: The number of names written; 0 if the folder cannot be opened

xtk_theme_path

void xtk_theme_path(const char *directory, const char *name, char *out, size_t size);

The path of a theme's file in a folder: directory/name.conf.

Parameter Description
directory The folder; NULL gives "."
name The theme's name
out Receives the path
size The size of out in bytes

xtk_theme_list_all

int xtk_theme_list_all(const char *program, XtkThemeEntry *out, int max);

Every theme file a program can use, sorted by name with case ignored.

A theme in the program's own folder hides an installed theme of the same name (see the search order at the top of this page); the list is then sorted as a whole. Built-in themes are not listed.

Parameter Description
program The program's name; NULL lists only the installed themes
out Receives the themes
max The most themes to write

Returns: The number of themes written

xtk_theme_find

int xtk_theme_find(const char *program, const char *name, char *path, size_t size);

Find a theme file by its name, as xtk_theme_list_all() lists them.

A theme whose name matches exactly is found first; when none does, case is ignored, as xtk_theme_builtin() ignores it. Built-in themes are not found here.

Parameter Description
program The program's name; NULL looks only among the installed themes
name The theme's name
path Receives the file's path
size The size of path in bytes

Returns: 1 if found, 0 otherwise

xtk_theme_load_named

int xtk_theme_load_named(XtkConfig *config, const char *program, const char *name);

Find a theme by name and apply it to a config.

Looks for a file with xtk_theme_find(), else a built-in theme with that name (case ignored), and applies it with xtk_theme_apply(). Then, if the theme left the focus borders of buttons, checkboxes, text views or graphs at the toolkit's default blue, they get the entry's focus border color instead (older theme files left them so), unless that color is itself one of the default blues. Finally the name is stored in config->main.theme, so it is saved with the config.

Parameter Description
config The configuration to update
program The program's name
name The theme's name (it may be config->main.theme itself)

Returns: 1 if a theme was found and applied, 0 otherwise (also when name is NULL or empty; config is then unchanged)

xtk_theme_builtin

int xtk_theme_builtin(const char *name, XtkTheme *theme);

Get a built-in theme by name.

Parameter Description
name The theme's name, case ignored, for example "Dark"
theme Receives the theme (from xtk_theme_new()); what it held before is dropped

Returns: 1 if found, 0 if no built-in theme has that name (or an argument is NULL, or memory ran out)

xtk_theme_install_builtins

int xtk_theme_install_builtins(const char *directory);

Write the built-in themes into a folder as theme files.

The folder, and the folders above it, are made when missing. A theme whose file already exists there is not written, so the user's changes are kept.

Parameter Description
directory The folder, usually from xtk_theme_directory()

Returns: The number of theme files written; 0 if directory is NULL or cannot be made