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

xtk_progressbar.h

A progress bar that shows how much of a task is done, or that work is going on.

An XtkProgressBar is a rounded track that fills from left to right (or from the bottom up, see xtk_progressbar_set_vertical()). It has two modes:

  • Determinate: xtk_progressbar_set_fraction() gives the part that is done, from 0.0 to 1.0, and the bar shows it filled, with "NN%" over it by default.
  • Indeterminate ("pulsing"): for work whose length is not known, xtk_progressbar_set_pulsing() makes a block a quarter of the track long move back and forth. The bar has no timer of its own: the program calls xtk_progressbar_update() on every tick of its main loop, and the block moves 2% of the track at most every 16 ms.

Setting a fraction ends pulsing, so the two modes do not mix. The text over the bar can be replaced with any text (xtk_progressbar_set_text()) or turned off (xtk_progressbar_set_show_text()).

Creating a progress bar. xtk_progressbar_create() takes the position and the size and the whole XtkConfig, of which it reads config.progressbar (colors and font) and the border width and radius in config.progressbar_widget. The usual height is config.progressbar_widget.height. Unlike most widgets, the bar is mapped at once. The program frees it with xtk_progressbar_destroy().

Events. The bar takes no input. The program passes events to xtk_progressbar_handle_event() so that it redraws on Expose; every change made through the API redraws at once.

Theme keys. [progressbar]:

  • background (default #292929): the empty track.
  • border (#4D4D4D): the line around the track.
  • fill (#338CE6): the filled part.
  • fill-pulse (#338CE6): the moving block of a pulsing bar.
  • text (#F2F2F2): the text over the bar's empty track.
  • fill-text (#FFFFFF; from a theme, its accent-text color): the text where it crosses the filled part, so it reads on a light fill and a dark one alike.
  • font-family (sans) and font-size (11; 0 means no text).

background, fill and fill-pulse also accept a CSS gradient; a gradient for fill spans the whole track and the fill shows its part.

[progressbar-widget]:

  • height (22): the height a program normally gives the bar.
  • border-width (1): the width of the border; 0 for none.
  • border-radius (6): the corner radius.

Example.

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);

XtkProgressBar *bar = xtk_progressbar_create(display, DefaultScreen(display), parent,
    10, 10, 300, config.progressbar_widget.height, &config);
xtk_progressbar_set_text(bar, "Scanning...");
xtk_progressbar_set_pulsing(bar, 1);

while (scanning) {
    while (XPending(display)) {
        XEvent event;
        XNextEvent(display, &event);
        xtk_progressbar_handle_event(bar, &event);
    }
    xtk_progressbar_update(bar);   // moves the pulse block
    usleep(10000);
}
xtk_progressbar_set_text(bar, NULL);
xtk_progressbar_set_fraction(bar, 1.0);   // shows "100%"
 * 

Limitations.

  • The text is always horizontal, also on a vertical bar, and is cut to 127 bytes.
#include "xtk_progressbar.h"

Types: XtkProgressBar

Functions: xtk_progressbar_create, xtk_progressbar_destroy, xtk_progressbar_get_window, xtk_progressbar_set_enabled, xtk_progressbar_get_enabled, xtk_progressbar_set_vertical, xtk_progressbar_get_vertical, xtk_progressbar_show, xtk_progressbar_hide, xtk_progressbar_set_position, xtk_progressbar_resize, xtk_progressbar_set_fraction, xtk_progressbar_get_fraction, xtk_progressbar_set_pulsing, xtk_progressbar_get_pulsing, xtk_progressbar_update, xtk_progressbar_set_text, xtk_progressbar_set_show_text, xtk_progressbar_handle_event, xtk_progressbar_draw, xtk_progressbar_set_theme, xtk_progressbar_config_init_defaults, xtk_progressbar_config_parse_styling, xtk_progressbar_config_write_styling, xtk_progressbar_widget_config_init_defaults, xtk_progressbar_widget_config_parse, xtk_progressbar_widget_config_write

Types

XtkProgressBar

typedef struct XtkProgressBar XtkProgressBar;

A progress bar. The structure is private; make one with xtk_progressbar_create().

Functions

xtk_progressbar_create

XtkProgressBar *xtk_progressbar_create(
    Display *display,
    int screen,
    Window parent,
    int x,
    int y,
    int width,
    int height,
    const XtkConfig *config);

Create a progress bar, mapped, at 0%.

Parameter Description
display The X display.
screen The screen number.
parent The window the bar sits in.
x The x position in the parent, in pixels.
y The y position in the parent, in pixels.
width The width in pixels (at least 1).
height The height in pixels (at least 1).
config The configuration whose progressbar and progressbar_widget blocks give the look; it is copied. It may be NULL, but then the bar has no colors set, no border and no text until xtk_progressbar_set_theme().

Returns: The new progress bar, or NULL if display is NULL, memory runs out, or the window cannot be made.

xtk_progressbar_destroy

void xtk_progressbar_destroy(XtkProgressBar *progressbar);

Destroy a progress bar: its window, font and memory.

Parameter Description
progressbar The bar, or NULL (then nothing happens).

xtk_progressbar_get_window

Window xtk_progressbar_get_window(const XtkProgressBar *progressbar);

The progress bar's X window.

Parameter Description
progressbar The bar.

Returns: The window, or None if progressbar is NULL.

xtk_progressbar_set_enabled

void xtk_progressbar_set_enabled(XtkProgressBar *progressbar, int enabled);

Enable or disable the progress bar, and redraw.

A disabled bar shows its fill, pulse block and text dimmed (blended toward background), to show that the task it stands for is not running. It goes on updating as usual. Bars start enabled.

Parameter Description
progressbar The bar.
enabled Nonzero to enable, 0 to disable.

xtk_progressbar_get_enabled

int xtk_progressbar_get_enabled(const XtkProgressBar *progressbar);

Whether the progress bar is enabled.

Parameter Description
progressbar The bar.

Returns: 1 if enabled, 0 if disabled or if progressbar is NULL.

xtk_progressbar_set_vertical

void xtk_progressbar_set_vertical(XtkProgressBar *progressbar, int vertical);

Make the bar vertical or horizontal, and redraw.

A vertical bar fills from the bottom up and pulses up and down. The size is not changed: give the bar a tall size with xtk_progressbar_resize().

Parameter Description
progressbar The bar.
vertical Nonzero for vertical, 0 for horizontal (the default).

xtk_progressbar_get_vertical

int xtk_progressbar_get_vertical(const XtkProgressBar *progressbar);

Whether the bar is vertical.

Parameter Description
progressbar The bar.

Returns: 1 if vertical, 0 if horizontal or if progressbar is NULL.

xtk_progressbar_show

void xtk_progressbar_show(XtkProgressBar *progressbar);

Show the progress bar: map its window.

Parameter Description
progressbar The bar.

xtk_progressbar_hide

void xtk_progressbar_hide(XtkProgressBar *progressbar);

Hide the progress bar: unmap its window.

Parameter Description
progressbar The bar.

xtk_progressbar_set_position

void xtk_progressbar_set_position(XtkProgressBar *progressbar, int x, int y);

Move the progress bar within its parent. It does not map the bar.

Parameter Description
progressbar The bar.
x The new x position in the parent, in pixels.
y The new y position in the parent, in pixels.

xtk_progressbar_resize

void xtk_progressbar_resize(XtkProgressBar *progressbar, int width, int height);

Change the size, reshape the corners and redraw.

Parameter Description
progressbar The bar.
width The new width in pixels; less than 1 becomes 1.
height The new height in pixels; less than 1 becomes 1.

xtk_progressbar_set_fraction

void xtk_progressbar_set_fraction(XtkProgressBar *progressbar, double fraction);

Set how much is done, and redraw.

This also stops pulsing.

Parameter Description
progressbar The bar.
fraction The part done, from 0.0 to 1.0; values outside are clamped.

xtk_progressbar_get_fraction

double xtk_progressbar_get_fraction(const XtkProgressBar *progressbar);

The fraction last set.

Parameter Description
progressbar The bar.

Returns: The fraction, from 0.0 to 1.0, or 0.0 if progressbar is NULL.

xtk_progressbar_set_pulsing

void xtk_progressbar_set_pulsing(XtkProgressBar *progressbar, int pulsing);

Start or stop pulsing, and redraw.

Starting puts the block at the start of the track. While pulsing, the bar shows no "NN%" text, only text set with xtk_progressbar_set_text(). Stopping shows the last fraction again.

Parameter Description
progressbar The bar.
pulsing Nonzero to pulse, 0 to stop.

xtk_progressbar_get_pulsing

int xtk_progressbar_get_pulsing(const XtkProgressBar *progressbar);

Whether the bar is pulsing.

Parameter Description
progressbar The bar.

Returns: 1 if pulsing, 0 if not or if progressbar is NULL.

xtk_progressbar_update

void xtk_progressbar_update(XtkProgressBar *progressbar);

Move the pulse block one step, if it is time.

Call it on every tick of the main loop. It does nothing unless the bar is pulsing and at least 16 ms have gone by since the last step, so it is cheap to call all the time.

Parameter Description
progressbar The bar, or NULL.

xtk_progressbar_set_text

void xtk_progressbar_set_text(XtkProgressBar *progressbar, const char *text);

Set the text shown centered over the bar, and redraw.

It takes the place of the automatic "NN%". The text is copied (at most 127 bytes).

Parameter Description
progressbar The bar.
text The text, or NULL or "" for the automatic text: "NN%" in determinate mode and none while pulsing.

xtk_progressbar_set_show_text

void xtk_progressbar_set_show_text(XtkProgressBar *progressbar, int show);

Turn the text over the bar on or off, and redraw.

It is on by default; off hides both the automatic and the set text.

Parameter Description
progressbar The bar.
show Nonzero to show text, 0 to show none.

xtk_progressbar_handle_event

int xtk_progressbar_handle_event(XtkProgressBar *progressbar, XEvent *event);

Handle an X event: redraw on the last Expose of a series.

Parameter Description
progressbar The bar.
event The event.

Returns: 1 if it was an Expose (with count 0) for the bar's window and the bar redrew; 0 for every other event. The bar takes no input.

xtk_progressbar_draw

void xtk_progressbar_draw(XtkProgressBar *progressbar);

Redraw the progress bar now.

Parameter Description
progressbar The bar, or NULL.

xtk_progressbar_set_theme

void xtk_progressbar_set_theme(XtkProgressBar *progressbar, const XtkConfig *config);

Take new colors, font, border width and radius from a configuration, and redraw.

Call it after the theme changes. The window's rounded shape follows the new radius at once.

Parameter Description
progressbar The bar.
config The configuration; its progressbar and progressbar_widget blocks are copied. NULL does nothing.

xtk_progressbar_config_init_defaults

void xtk_progressbar_config_init_defaults(ProgressBarBlock *progressbar_config);

Fill a ProgressBarBlock with the default [progressbar] colors and font.

xtk_config_init_defaults() already calls it for config.progressbar.

Parameter Description
progressbar_config The block to fill; NULL does nothing.

xtk_progressbar_config_parse_styling

int xtk_progressbar_config_parse_styling(
    ProgressBarBlock *progressbar_config,
    const char *key,
    const char *value);

Set one [progressbar] key in a ProgressBarBlock.

Parameter Description
progressbar_config The block to change.
key The key name, such as fill-pulse.
value The value as written in the file.

Returns: 1 if the key is known and was set; 0 if it is unknown or an argument is NULL.

xtk_progressbar_config_write_styling

int xtk_progressbar_config_write_styling(
    FILE *file,
    const ProgressBarBlock *progressbar_config);

Write a ProgressBarBlock as a [progressbar] section.

Parameter Description
file The file to write to.
progressbar_config The block to write.

Returns: 1 on success, 0 if an argument is NULL.

xtk_progressbar_widget_config_init_defaults

void xtk_progressbar_widget_config_init_defaults(XtkConfig *config);

Set config.progressbar_widget to the default [progressbar-widget] values.

Parameter Description
config The configuration; NULL does nothing.

xtk_progressbar_widget_config_parse

int xtk_progressbar_widget_config_parse(
    XtkConfig *config,
    const char *key,
    const char *value);

Set one [progressbar-widget] key in config.progressbar_widget.

Parameter Description
config The configuration.
key The key name: height, border-width or border-radius.
value The value as written in the file.

Returns: 1 if the key is known and was set; 0 if it is unknown or an argument is NULL.

xtk_progressbar_widget_config_write

int xtk_progressbar_widget_config_write(FILE *file, const XtkConfig *config);

Write config.progressbar_widget as a [progressbar-widget] section.

Parameter Description
file The file to write to.
config The configuration.

Returns: 1 on success, 0 if an argument is NULL.