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

xtk_scrollbar.h

A vertical or horizontal scrollbar for content the program scrolls itself.

An XtkScrollbar is a track with a thumb that the user drags. The program tells it how big the content is and how much of it shows (xtk_scrollbar_set_range()); the scrollbar sizes the thumb to match and keeps a scroll value from 0 to content size minus viewport size. The scrollbar never moves anything itself: the program reads the value (in its callback, or with xtk_scrollbar_get_value()) and moves its own content. Use it for a window that scrolls by itself and is not already one of the toolkit's scrolling widgets, so that all scrollbars look the same.

Creating a scrollbar. xtk_scrollbar_create() takes the position, the length along the scrolling direction, the orientation and the whole XtkConfig (copied; it reads config.scrollbar and config.scrollbar_widget). The thickness across comes from config.scrollbar_widget.thickness. The scrollbar is mapped at once. The program frees it with xtk_scrollbar_destroy().

Events. The program passes every XEvent to xtk_scrollbar_handle_event(). Pressing Button1 anywhere on the scrollbar starts a drag, and the value follows the pointer from where it was: a press on the track does not jump there. While the pointer moves during a drag, the value changes and the callback (xtk_scrollbar_set_callback()) runs on every motion event. The thumb takes the thumb-hover color while the pointer is over the scrollbar and during a drag. The scrollbar does not handle the mouse wheel or keys: the program scrolls its content and calls xtk_scrollbar_set_value(), which moves the thumb.

When the content fits (content size not larger than viewport size), no thumb is drawn and xtk_scrollbar_is_needed() returns 0; most programs then hide the scrollbar.

Theme keys. [scrollbar]:

  • track (default #292929): the track.
  • thumb (#666666): the thumb.
  • thumb-hover (#8C8C8C): the thumb while the pointer is over the scrollbar or the thumb is dragged.

All three accept a CSS gradient as well as a color.

[scrollbar-widget]:

  • thickness (8): the width of a vertical scrollbar, the height of a horizontal one.
  • min-thumb-length (24): the thumb is never shorter than this.

Example.

static void on_scroll(XtkScrollbar *scrollbar, int value, void *user_data) {
    (void)scrollbar;
    Window content = *(Window *)user_data;
    XMoveWindow(display, content, 0, -value);   // the program moves its content
}

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

XtkScrollbar *bar = xtk_scrollbar_create(display, DefaultScreen(display), viewport,
    viewport_width - config.scrollbar_widget.thickness, 0, viewport_height,
    XTK_SCROLLBAR_VERTICAL, &config);
xtk_scrollbar_set_range(bar, content_height, viewport_height);
xtk_scrollbar_set_callback(bar, on_scroll, &content);
if (!xtk_scrollbar_is_needed(bar)) xtk_scrollbar_hide(bar);

XEvent event;
while (running) {
    XNextEvent(display, &event);
    xtk_scrollbar_handle_event(bar, &event);
}
xtk_scrollbar_destroy(bar);
 * 
#include "xtk_scrollbar.h"

Types: XtkScrollbar, XtkScrollbarOrientation, XtkScrollbarScrollCallback

Functions: xtk_scrollbar_create, xtk_scrollbar_destroy, xtk_scrollbar_get_window, xtk_scrollbar_handle_event, xtk_scrollbar_set_range, xtk_scrollbar_is_needed, xtk_scrollbar_set_value, xtk_scrollbar_get_value, xtk_scrollbar_set_callback, xtk_scrollbar_set_position, xtk_scrollbar_resize, xtk_scrollbar_show, xtk_scrollbar_hide, xtk_scrollbar_set_theme, xtk_scrollbar_draw, xtk_scrollbar_config_init_defaults, xtk_scrollbar_config_parse_styling, xtk_scrollbar_config_write_styling, xtk_scrollbar_widget_config_init_defaults, xtk_scrollbar_widget_config_parse, xtk_scrollbar_widget_config_write, xtk_scrollbar_get_hover, xtk_scrollbar_set_hover

Types

XtkScrollbar

typedef struct XtkScrollbar XtkScrollbar;

A scrollbar. The structure is private; make one with xtk_scrollbar_create().

XtkScrollbarOrientation

Which way a scrollbar scrolls.

Name Value Description
XTK_SCROLLBAR_VERTICAL Up and down; the length is the height.
XTK_SCROLLBAR_HORIZONTAL Left and right; the length is the width.

XtkScrollbarScrollCallback

typedef void (*XtkScrollbarScrollCallback)(XtkScrollbar *scrollbar, int value, void *user_data);

Called while the user drags the scrollbar.

It runs on every pointer motion during a drag, also when the value did not change, not only at the end. Changes made with xtk_scrollbar_set_value() do not call it.

Functions

xtk_scrollbar_create

XtkScrollbar *xtk_scrollbar_create(
    Display *display,
    int screen,
    Window parent,
    int x,
    int y,
    int length,
    XtkScrollbarOrientation orientation,
    const XtkConfig *theme);

Create a scrollbar, mapped, with an empty range.

Parameter Description
display The X display.
screen The screen number.
parent The window the scrollbar sits in.
x The x position in the parent, in pixels.
y The y position in the parent, in pixels.
length The size along the scrolling direction (the height of a vertical scrollbar, the width of a horizontal one); at least 1. The thickness across is theme->scrollbar_widget.thickness (8 if that is 0).
orientation Vertical or horizontal.
theme The configuration; copied. NULL uses the toolkit defaults.

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

xtk_scrollbar_destroy

void xtk_scrollbar_destroy(XtkScrollbar *scrollbar);

Destroy a scrollbar: its window and memory.

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

xtk_scrollbar_get_window

Window xtk_scrollbar_get_window(const XtkScrollbar *scrollbar);

The scrollbar's X window.

Parameter Description
scrollbar The scrollbar.

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

xtk_scrollbar_handle_event

int xtk_scrollbar_handle_event(XtkScrollbar *scrollbar, XEvent *event);

Handle an X event.

Parameter Description
scrollbar The scrollbar.
event The event.

Returns: 1 if the scrollbar used the event: an Expose, Button1 press, motion or LeaveNotify on its window, or the Button1 release that ends a drag. 0 for every other event, including other buttons and the wheel.

xtk_scrollbar_set_range

void xtk_scrollbar_set_range(
    XtkScrollbar *scrollbar,
    int content_size,
    int viewport_size);

Set the size of the content and of the part that shows, and redraw.

Both are in the program's own units (pixels, rows, ...). They decide the thumb's length and how far it travels, and the value is clamped to the new range. Nothing happens if both sizes are the same as before.

Parameter Description
scrollbar The scrollbar.
content_size The size of all there is to scroll.
viewport_size The size of the part that shows. If it is not smaller than content_size, there is nothing to scroll (see xtk_scrollbar_is_needed()).

xtk_scrollbar_is_needed

int xtk_scrollbar_is_needed(const XtkScrollbar *scrollbar);

Whether there is anything to scroll.

When there is not, the scrollbar draws no thumb, and the program normally hides it.

Parameter Description
scrollbar The scrollbar.

Returns: 1 if the content is larger than the viewport, 0 if it is not or if scrollbar is NULL.

xtk_scrollbar_set_value

void xtk_scrollbar_set_value(XtkScrollbar *scrollbar, int value);

Set the scroll value, and redraw if it changed.

For a program that scrolls its content by other means (the wheel, keys). It does not call the callback.

Parameter Description
scrollbar The scrollbar.
value The new value; clamped to 0 .. content size minus viewport size.

xtk_scrollbar_get_value

int xtk_scrollbar_get_value(const XtkScrollbar *scrollbar);

The current scroll value.

Parameter Description
scrollbar The scrollbar.

Returns: The value, from 0 to content size minus viewport size, or 0 if scrollbar is NULL.

xtk_scrollbar_set_callback

void xtk_scrollbar_set_callback(
    XtkScrollbar *scrollbar,
    XtkScrollbarScrollCallback callback,
    void *user_data);

Set or remove the callback that runs while the user drags.

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

xtk_scrollbar_set_position

void xtk_scrollbar_set_position(XtkScrollbar *scrollbar, int x, int y);

Move the scrollbar within its parent.

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

xtk_scrollbar_resize

void xtk_scrollbar_resize(XtkScrollbar *scrollbar, int length);

Change the length along the scrolling direction, and redraw.

The thickness stays. Call xtk_scrollbar_set_range() as well when the viewport size changes with it.

Parameter Description
scrollbar The scrollbar.
length The new length in pixels; 0 or less is ignored.

xtk_scrollbar_show

void xtk_scrollbar_show(XtkScrollbar *scrollbar);

Show the scrollbar: map its window.

Parameter Description
scrollbar The scrollbar, or NULL.

xtk_scrollbar_hide

void xtk_scrollbar_hide(XtkScrollbar *scrollbar);

Hide the scrollbar: unmap its window.

Parameter Description
scrollbar The scrollbar, or NULL.

xtk_scrollbar_set_theme

void xtk_scrollbar_set_theme(XtkScrollbar *scrollbar, const XtkConfig *theme);

Take new colors and sizes from a configuration, and redraw.

Call it after the theme changes. A new thickness resizes the window at once; its length stays.

Parameter Description
scrollbar The scrollbar.
theme The configuration; copied. NULL does nothing.

xtk_scrollbar_draw

void xtk_scrollbar_draw(XtkScrollbar *scrollbar);

Redraw the scrollbar now.

Parameter Description
scrollbar The scrollbar, or NULL.

xtk_scrollbar_config_init_defaults

void xtk_scrollbar_config_init_defaults(ScrollbarBlock *scrollbar_config);

Fill a ScrollbarBlock with the default [scrollbar] colors.

xtk_config_init_defaults() already calls it for config.scrollbar.

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

xtk_scrollbar_config_parse_styling

int xtk_scrollbar_config_parse_styling(
    ScrollbarBlock *scrollbar_config,
    const char *key,
    const char *value);

Set one [scrollbar] key in a ScrollbarBlock.

Parameter Description
scrollbar_config The block to change.
key The key name: track, thumb or thumb-hover.
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_scrollbar_config_write_styling

int xtk_scrollbar_config_write_styling(
    FILE *file,
    const ScrollbarBlock *scrollbar_config);

Write a ScrollbarBlock as a [scrollbar] section (track, thumb and thumb-hover).

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

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

xtk_scrollbar_widget_config_init_defaults

void xtk_scrollbar_widget_config_init_defaults(XtkConfig *config);

Set config.scrollbar_widget to the default [scrollbar-widget] values.

Parameter Description
config The configuration; NULL does nothing.

xtk_scrollbar_widget_config_parse

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

Set one [scrollbar-widget] key in config.scrollbar_widget.

Parameter Description
config The configuration.
key The key name: thickness or min-thumb-length.
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_scrollbar_widget_config_write

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

Write config.scrollbar_widget as a [scrollbar-widget] section.

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

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

xtk_scrollbar_get_hover

int xtk_scrollbar_get_hover(const XtkScrollbar *scrollbar);

Whether the thumb shows its hover color.

Parameter Description
scrollbar The scrollbar.

Returns: 1 if it does, 0 if not or if scrollbar is NULL.

xtk_scrollbar_set_hover

void xtk_scrollbar_set_hover(XtkScrollbar *scrollbar, int hover);

Show the thumb in its hover color, or not, without the pointer.

For a program that lights the scrollbar together with something else, such as the view it scrolls. Redraws if the state changes.

Parameter Description
scrollbar The scrollbar.
hover Nonzero for the hover color, 0 for the normal color.