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

xtk_slider.h

A slider for choosing a number in a range by dragging a handle.

An XtkSlider is a track with a handle on it, horizontal or vertical. The value is a double between a minimum and a maximum. It can be continuous or snap to equal steps (xtk_slider_set_step_count()), show tick marks, fill the track up to the handle, and run the other way (xtk_slider_set_inverted()). The handle can be square, round or a pointer. For a color picker the track can show a gradient given by the program (xtk_slider_set_gradient()), and the handle can take the color under it.

Creating a slider. xtk_slider_create() takes a SliderBlock for the colors (normally &config.slider), the position, the length along the track and the thickness across it (normally config.slider_widget.width and config.slider_widget.height for a horizontal slider), the orientation, and the range with the first value. The block is copied. The window is not mapped until xtk_slider_set_position() or xtk_slider_show(). The program frees the slider with xtk_slider_destroy().

Events. The program passes every XEvent to xtk_slider_handle_event(). A Button1 press puts the value where the pointer is (anywhere on the track, not only on the handle) and starts a drag; motion during the drag follows the pointer. A press also gives the slider the X input focus. With the focus, Left and Down lower the value by one step (1% of the range when there are no steps), Right and Up raise it, Page Down and Page Up move ten steps, and Home and End go to the minimum and the maximum. The arrows mean lower and higher whatever xtk_slider_set_inverted() says.

The callback (xtk_slider_set_callback()) runs on every press and drag motion and every key that moves the value, and also on xtk_slider_set_value(), also when the value did not change.

While the pointer is over the slider or a drag is going on, the handle uses the hover colors; with the focus (and no hover) its border is handle-focus-border.

Theme keys. [slider]:

  • background (default #2E2E2E): the whole widget behind the track.
  • border (#808080), border-width (1) and border-radius (0): a frame around the widget; none when the width is 0.
  • color (#F2F2F2): the tick marks.
  • track-background (#1F1F1F): the track.
  • track-border (#808080): the line around the track.
  • fill-background (#598CE6): the filled part of the track (see xtk_slider_set_fill_track()).
  • handle-background (#4D4D4D): the handle's fill.
  • handle-foreground (#F2F2F2): the grip, a short line across the handle.
  • handle-border (#808080): the handle's border.
  • handle-hover-background (#808080) and handle-hover-border (#808080): the handle's fill and border while hovered or dragged.
  • handle-focus-border (the toolkit's focus color): the handle's border while the slider has the focus.
  • track-height (0): the track's thickness in pixels; 0 is a third of the slider's thickness, at least 4.
  • handle-height (0): the handle's size across the track; 0 is half the slider's thickness (xtk_slider_set_handle_size() overrides it).
  • handle-width (0): the handle's size along the track (for the square and round handles); 0 is two thirds of its size across.

The slider draws no text, so it has no font keys. background, track-background, fill-background, handle-background and handle-hover-background accept a CSS gradient.

[slider-widget]: width (200) and height (24), the size a program normally gives a horizontal slider.

Example.

static void on_volume(XtkSlider *slider, double value, void *user_data) {
    (void)slider;
    (void)user_data;
    printf("Volume: %.0f\n", value);
}

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

XtkSlider *volume = xtk_slider_create(display, DefaultScreen(display), parent, 0, 0,
    config.slider_widget.width, config.slider_widget.height,
    SLIDER_HORIZONTAL, &config.slider, 0.0, 100.0, 50.0);
xtk_slider_set_step_count(volume, 10);     // snaps to 0, 10, ... 100, with ticks
xtk_slider_set_fill_track(volume, 1);
xtk_slider_set_callback(volume, on_volume, NULL);
xtk_slider_set_position(volume, 10, 10);  // maps the slider

XEvent event;
while (running) {
    XNextEvent(display, &event);
    xtk_slider_handle_event(volume, &event);
}
xtk_slider_destroy(volume);
 * 

Limitations.

  • The slider is not thread-safe; use it from the thread that owns the Display.
#include "xtk_slider.h"

Types: SliderOrientation, SliderHandleStyle, XtkSlider, SliderChangedCallback

Functions: xtk_slider_create, xtk_slider_destroy, xtk_slider_get_window, xtk_slider_handle_event, xtk_slider_set_focused, xtk_slider_get_focused, xtk_slider_get_focus_window, xtk_slider_draw, xtk_slider_set_theme, xtk_slider_set_position, xtk_slider_set_size, xtk_slider_get_size, xtk_slider_set_orientation, xtk_slider_get_orientation, xtk_slider_set_default_geometry, xtk_slider_set_default_min_span, xtk_slider_set_range, xtk_slider_set_value, xtk_slider_get_value, xtk_slider_set_step_count, xtk_slider_set_tick_count, xtk_slider_set_handle_round, xtk_slider_set_fill_track, xtk_slider_set_inverted, xtk_slider_set_handle_size, xtk_slider_set_handle_style, xtk_slider_get_handle_style, xtk_slider_set_gradient, xtk_slider_clear_gradient, xtk_slider_set_handle_follows_gradient, xtk_slider_set_handle_color, xtk_slider_get_gradient_color, xtk_slider_set_enabled, xtk_slider_get_enabled, xtk_slider_show, xtk_slider_hide, xtk_slider_config_init_defaults, xtk_slider_widget_config_init_defaults, xtk_slider_widget_config_parse, xtk_slider_set_callback, xtk_slider_set_hover

Types

SliderOrientation

Which way a slider's track runs.

Name Value Description
SLIDER_HORIZONTAL 0 Left to right; the minimum is at the left unless inverted.
SLIDER_VERTICAL 1 Bottom to top; the minimum is at the bottom unless inverted.

SliderHandleStyle

The shape of a slider's handle.

Name Value Description
SLIDER_HANDLE_SQUARE 0 A rounded rectangle across the track (the default)
SLIDER_HANDLE_ROUND 1 A circle.
SLIDER_HANDLE_POINTER 2 An arrow pointing across the track (down on a horizontal slider, right on a vertical one)

XtkSlider

typedef struct XtkSlider XtkSlider;

A slider. The structure is private; make one with xtk_slider_create().

SliderChangedCallback

typedef void (*SliderChangedCallback)(XtkSlider *slider, double value, void *user_data);

Called when the slider's value is set.

It runs on a press and on every drag motion, on every key that moves the value, and on xtk_slider_set_value(), whether or not the value changed.

Functions

xtk_slider_create

XtkSlider *xtk_slider_create(
    Display *display,
    int screen,
    Window parent,
    int x_position,
    int y_position,
    int length,
    int thickness,
    SliderOrientation orientation,
    const SliderBlock *theme,
    double min_value,
    double max_value,
    double initial_value);

Create a slider.

Its window is not mapped until xtk_slider_set_position() or xtk_slider_show().

Parameter Description
display The X display.
screen The screen number.
parent The window the slider sits in; it must not be None.
x_position The x position in the parent, in pixels.
y_position The y position in the parent, in pixels.
length The size along the track in pixels; 0 or less uses the default length (100, see xtk_slider_set_default_geometry()).
thickness The size across the track in pixels; 0 or less uses the default thickness (24).
orientation Horizontal or vertical.
theme The colors, normally &config.slider; copied. NULL uses a light built-in set of colors.
min_value The lowest value.
max_value The highest value. If it is not above min_value, the maximum becomes min_value plus the default span (1.0, see xtk_slider_set_default_min_span()).
initial_value The first value, clamped to the range.

Returns: The new slider, or NULL if display is NULL, parent is None, memory runs out, or the window cannot be made.

xtk_slider_destroy

void xtk_slider_destroy(XtkSlider *slider);

Destroy a slider: its window and memory.

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

xtk_slider_get_window

Window xtk_slider_get_window(XtkSlider *slider);

The slider's X window.

Parameter Description
slider The slider.

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

xtk_slider_handle_event

int xtk_slider_handle_event(XtkSlider *slider, const XEvent *event);

Handle an X event.

Events for other windows return 0 at once. A disabled slider handles only Expose.

Parameter Description
slider The slider.
event The event.

Returns: 1 if the slider used the event (expose, Button1 press or release, motion during a drag, enter, leave, focus, or one of its keys); 0 otherwise.

xtk_slider_set_focused

void xtk_slider_set_focused(XtkSlider *slider, int focused);

Give the slider keyboard focus, or clear its focus ring.

For a program that moves focus with Tab. With focused nonzero the slider takes the X input focus (if its window is viewable) and draws the handle's border in handle-focus-border; the arrow keys, Page Up, Page Down, Home and End then move the value. With 0 it only clears the focus ring: the X focus moves when another widget takes it.

Parameter Description
slider The slider.
focused Nonzero to take the focus, 0 to clear the focus ring.

xtk_slider_get_focused

int xtk_slider_get_focused(const XtkSlider *slider);

Whether the slider shows keyboard focus.

Parameter Description
slider The slider.

Returns: 1 if focused, 0 if not or if slider is NULL.

xtk_slider_get_focus_window

Window xtk_slider_get_focus_window(const XtkSlider *slider);

The window that takes the X input focus for this slider.

Parameter Description
slider The slider.

Returns: The slider's window, or None if slider is NULL.

xtk_slider_draw

void xtk_slider_draw(XtkSlider *slider);

Redraw the slider now.

Parameter Description
slider The slider, or NULL.

xtk_slider_set_theme

void xtk_slider_set_theme(XtkSlider *slider, const SliderBlock *theme);

Give the slider new colors, and redraw.

Parameter Description
slider The slider.
theme The new colors, normally &config.slider; copied. NULL does nothing.

xtk_slider_set_position

void xtk_slider_set_position(XtkSlider *slider, int x_position, int y_position);

Move the slider within its parent.

This also maps its window, unless it is hidden with xtk_slider_hide().

Parameter Description
slider The slider.
x_position The new x position in the parent, in pixels.
y_position The new y position in the parent, in pixels.

xtk_slider_set_size

void xtk_slider_set_size(XtkSlider *slider, int length, int thickness);

Change the slider's size, and redraw.

Parameter Description
slider The slider.
length The new size along the track; 0 or less keeps the old one.
thickness The new size across the track; 0 or less keeps the old one.

xtk_slider_get_size

void xtk_slider_get_size(const XtkSlider *slider, int *width, int *height);

The slider's window size.

Parameter Description
slider The slider. If it is NULL, both outputs are set to 0.
width Receives the width in pixels (the length of a horizontal slider, the thickness of a vertical one); may be NULL.
height Receives the height in pixels; may be NULL.

xtk_slider_set_orientation

void xtk_slider_set_orientation(XtkSlider *slider, SliderOrientation orientation);

Make the slider horizontal or vertical.

The length and thickness stay, so the window turns a quarter turn: its width and height swap.

Parameter Description
slider The slider.
orientation The new orientation.

xtk_slider_get_orientation

SliderOrientation xtk_slider_get_orientation(const XtkSlider *slider);

Whether the slider is horizontal or vertical.

Parameter Description
slider The slider.

Returns: The orientation; SLIDER_HORIZONTAL if slider is NULL.

xtk_slider_set_default_geometry

void xtk_slider_set_default_geometry(int length, int thickness);

Set the size xtk_slider_create() uses when it is given 0 or less.

It applies to every slider made afterwards in the program.

Parameter Description
length The default length in pixels (first 100); 0 or less keeps the current one.
thickness The default thickness in pixels (first 24); 0 or less keeps the current one.

xtk_slider_set_default_min_span

void xtk_slider_set_default_min_span(double min_span);

Set the span used when a range has no width.

When xtk_slider_create() or xtk_slider_set_range() gets a maximum that is not above the minimum, the maximum becomes the minimum plus this span. It applies to the whole program.

Parameter Description
min_span The span (first 1.0); 0 or less sets 1.0.

xtk_slider_set_range

void xtk_slider_set_range(XtkSlider *slider, double min_value, double max_value);

Change the range, and redraw.

The value is clamped to the new range and snapped to the steps. The callback does not run.

Parameter Description
slider The slider.
min_value The new lowest value.
max_value The new highest value; if it is not above min_value, see xtk_slider_set_default_min_span().

xtk_slider_set_value

void xtk_slider_set_value(XtkSlider *slider, double value);

Set the value, and redraw.

The value is clamped to the range and snapped to the steps. Unlike most xtk setters, this runs the callback.

Parameter Description
slider The slider.
value The new value.

xtk_slider_get_value

double xtk_slider_get_value(const XtkSlider *slider);

The current value.

Parameter Description
slider The slider.

Returns: The value, or 0.0 if slider is NULL.

xtk_slider_set_step_count

void xtk_slider_set_step_count(XtkSlider *slider, int step_count);

Make the value snap to equal steps across the range, and redraw.

A tick mark is drawn at every step, in place of the ticks of xtk_slider_set_tick_count(). The current value is snapped at once (the callback does not run). The step also sets how far an arrow key moves.

Parameter Description
slider The slider.
step_count The number of steps between the minimum and the maximum, or 0 for a continuous value. Less than 0 counts as 0.

xtk_slider_set_tick_count

void xtk_slider_set_tick_count(XtkSlider *slider, int tick_count);

Draw tick marks at equal intervals, without snapping, and redraw.

Parameter Description
slider The slider.
tick_count The number of intervals (ticks are drawn at both ends and between them), or 0 for none. A step count, when set, takes its place.

xtk_slider_set_handle_round

void xtk_slider_set_handle_round(XtkSlider *slider, int round);

Make the handle round or square, and redraw.

The same as xtk_slider_set_handle_style() with SLIDER_HANDLE_ROUND or SLIDER_HANDLE_SQUARE.

Parameter Description
slider The slider.
round Nonzero for round, 0 for square.

xtk_slider_set_fill_track

void xtk_slider_set_fill_track(XtkSlider *slider, int fill_enabled);

Fill the track from the minimum up to the handle with fill-background, and redraw.

Parameter Description
slider The slider.
fill_enabled Nonzero to fill it, 0 for a plain track (the default).

xtk_slider_set_inverted

void xtk_slider_set_inverted(XtkSlider *slider, int inverted);

Run the value the other way along the track, and redraw.

The keys keep their meaning: Right and Up still raise the value.

Parameter Description
slider The slider.
inverted Nonzero for the minimum at the right (horizontal) or the top (vertical); 0 for the left or the bottom (the default).

xtk_slider_set_handle_size

void xtk_slider_set_handle_size(XtkSlider *slider, int handle_size_pixels);

Set the handle's size, and redraw.

Parameter Description
slider The slider.
handle_size_pixels The size along its longer side, in pixels; 0 or less for the default, half the thickness. It is never smaller than the track's thickness plus 2.

xtk_slider_set_handle_style

void xtk_slider_set_handle_style(XtkSlider *slider, SliderHandleStyle style);

Set the handle's shape, and redraw.

Parameter Description
slider The slider.
style The shape; an unknown value means SLIDER_HANDLE_SQUARE.

xtk_slider_get_handle_style

SliderHandleStyle xtk_slider_get_handle_style(const XtkSlider *slider);

The handle's shape.

Parameter Description
slider The slider.

Returns: The shape; SLIDER_HANDLE_SQUARE if slider is NULL.

xtk_slider_set_gradient

void xtk_slider_set_gradient(
    XtkSlider *slider,
    const uint8_t *r_stops,
    const uint8_t *g_stops,
    const uint8_t *b_stops,
    int count);

Show a gradient of the program's colors in the track, and redraw.

The stops are spread evenly from the minimum end to the maximum end, on a horizontal or a vertical slider. While a gradient is set, it takes the place of track-background and the theme's track gradient.

Parameter Description
slider The slider.
r_stops The red value (0-255) of each stop.
g_stops The green value of each stop.
b_stops The blue value of each stop.
count The number of stops, at most 256 (more are ignored). Fewer than 2, or a NULL array, clears the gradient.

xtk_slider_clear_gradient

void xtk_slider_clear_gradient(XtkSlider *slider);

Remove the program's gradient from the track, and redraw.

Parameter Description
slider The slider.

xtk_slider_set_handle_follows_gradient

void xtk_slider_set_handle_follows_gradient(XtkSlider *slider, int enabled);

Fill the handle with the gradient's color at the current value, and redraw.

Only while a gradient is set (xtk_slider_set_gradient()). The handle then has no grip, and its border is the same color with its lightness inverted, so it shows on any color.

Parameter Description
slider The slider.
enabled Nonzero to follow the gradient, 0 for the theme's colors.

xtk_slider_set_handle_color

void xtk_slider_set_handle_color(
    XtkSlider *slider,
    int enabled,
    uint8_t r,
    uint8_t g,
    uint8_t b);

Fill the handle with one fixed color, and redraw.

For example, the handle of an alpha slider can show the color whose alpha it sets. The border is the same color with its lightness inverted, and the handle has no grip. It takes the place of xtk_slider_set_handle_follows_gradient().

Parameter Description
slider The slider.
enabled Nonzero to use r, g, b; 0 to go back to the theme's (or the gradient's) color.
r Red, 0-255.
g Green, 0-255.
b Blue, 0-255.

xtk_slider_get_gradient_color

int xtk_slider_get_gradient_color(
    const XtkSlider *slider,
    double value,
    uint8_t *r_out,
    uint8_t *g_out,
    uint8_t *b_out);

The program's gradient's color at a value.

The same color the handle takes with xtk_slider_set_handle_follows_gradient(), for showing it elsewhere too (a preview while dragging, say).

Parameter Description
slider The slider.
value The value to look at, clamped to the range. Pass xtk_slider_get_value() for the handle's color.
r_out Receives the red value (0-255); may be NULL.
g_out Receives the green value; may be NULL.
b_out Receives the blue value; may be NULL.

Returns: 1 on success; 0 if slider is NULL or has no gradient, and then the outputs are not touched.

xtk_slider_set_enabled

void xtk_slider_set_enabled(XtkSlider *slider, int enabled);

Enable or disable the slider, and redraw.

A disabled slider draws its track, handle and ticks in muted colors (the track 85% background, the handle 60%) and ignores all input; it still redraws on Expose. Sliders start enabled.

Parameter Description
slider The slider.
enabled Nonzero to enable, 0 to disable.

xtk_slider_get_enabled

int xtk_slider_get_enabled(const XtkSlider *slider);

Whether the slider is enabled.

Parameter Description
slider The slider.

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

xtk_slider_show

void xtk_slider_show(XtkSlider *slider);

Show the slider: map its window.

Parameter Description
slider The slider.

xtk_slider_hide

void xtk_slider_hide(XtkSlider *slider);

Hide the slider: unmap its window.

It stays unmapped when xtk_slider_set_position() moves it, until xtk_slider_show().

Parameter Description
slider The slider.

xtk_slider_config_init_defaults

void xtk_slider_config_init_defaults(SliderBlock *slider_config);

Fill a SliderBlock with the default [slider] values.

xtk_config_init_defaults() already calls it for config.slider. It leaves track_border, fill_background and handle_border as they are.

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

xtk_slider_widget_config_init_defaults

void xtk_slider_widget_config_init_defaults(XtkConfig *config);

Set config.slider_widget to the default [slider-widget] size (200 by 24).

Parameter Description
config The configuration; NULL does nothing.

xtk_slider_widget_config_parse

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

Set one [slider-widget] key in config.slider_widget.

Parameter Description
config The configuration.
key The key name: width or height.
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_slider_set_callback

void xtk_slider_set_callback(
    XtkSlider *slider,
    SliderChangedCallback callback,
    void *user_data);

Set or remove the callback that runs when the value is set.

Parameter Description
slider The slider.
callback The callback, or NULL to remove it.
user_data A pointer passed to the callback unchanged.

xtk_slider_set_hover

void xtk_slider_set_hover(XtkSlider *slider, int hover);

Show the handle in its hover colors, or not, without the pointer.

For a program that lights the slider together with something else. Redraws if the state changes; the next EnterNotify or LeaveNotify sets it again.

Parameter Description
slider The slider.
hover Nonzero for the hover colors, 0 for the normal colors.