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

xtk_splitpane.h

A container that shows two windows side by side or one above the other, with a divider the user drags to share the space.

A split pane holds two child windows, called the first and the second slot. In a horizontal split they sit side by side (first on the left) with a vertical divider between them; in a vertical split they are stacked (first on top) with a horizontal divider. The user drags the divider with the left mouse button to change how the space is shared; the program can set the share (the ratio) itself, and can collapse either slot, which gives all the space to the other slot and hides the divider. The divider shows a resize cursor and, when it is long enough, three small grip dots. Split panes can be nested; xtk_dockframe.h builds its regions from them.

The children are plain X windows (for example the window of another widget, as with xtk_notebook.h pages). xtk_grid.h, by contrast, places widgets through callbacks.

Making it. Call xtk_splitpane_create() with the parent window, the box, the orientation and the splitpane block of the XtkConfig. The pane maps its window. Then call xtk_splitpane_set_divider_thickness() with config.splitpane_widget.divider_thickness and xtk_splitpane_set_child() for each slot, with config.splitpane_widget.min_size (or any other minimum); the pane does not read these geometry values itself. The pane moves each child into its own window, then sizes, maps and unmaps it. It does not own the children: xtk_splitpane_destroy() gives them back to the pane's parent (unmapped) before it destroys the pane's windows.

Events. Pass each XEvent to xtk_splitpane_handle_event(). It handles the divider's events: it redraws it on Expose, and a drag with the left button moves it. During a drag the pane lays out the children on every motion event and calls the ratio-changed callback each time, and once more when the button is released. Events of the child windows are not the pane's; give them to the child widgets. The pane takes no keyboard input.

Theme keys. Section [splitpane]:

  • background: the pane's own background, seen where no child covers it (a gradient is allowed). Default #2E2E2E.
  • divider-background: the divider's color (a gradient is allowed). The grip dots are this color made lighter or darker. Default #404040.

Section [splitpane-widget] (pixels; the program passes them on, see above):

  • divider-thickness: the divider's width. Default 3.
  • min-size: a minimum size for a slot. Default 32.

The defaults are those xtk_config_init_defaults() sets; a theme applied with xtk_theme_apply() replaces the colors.

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

XtkSplitPane *pane = xtk_splitpane_create(display, screen, window,
                                          0, 0, 800, 600,
                                          XTK_SPLITPANE_HORIZONTAL,
                                          &config.splitpane);
xtk_splitpane_set_divider_thickness(pane, config.splitpane_widget.divider_thickness);
Window left = XCreateSimpleWindow(display, window, 0, 0, 1, 1, 0, 0, 0);
Window right = XCreateSimpleWindow(display, window, 0, 0, 1, 1, 0, 0, 0);
xtk_splitpane_set_child(pane, XTK_SPLITPANE_FIRST, left, config.splitpane_widget.min_size);
xtk_splitpane_set_child(pane, XTK_SPLITPANE_SECOND, right, config.splitpane_widget.min_size);
xtk_splitpane_set_ratio(pane, 0.3f);

XEvent event;
while (running) {
    XNextEvent(display, &event);
    if (xtk_splitpane_handle_event(pane, &event)) continue;
    // ... the children and the program's other windows
}
xtk_splitpane_destroy(pane);   // left and right are given back to window
 * 

Limitations.

  • The pane keeps the SplitPaneBlock pointer, so the block must stay valid as long as the pane does.
  • The divider cannot be moved while a slot is collapsed.
#include "xtk_splitpane.h"

Types: XtkSplitPane, XtkSplitPaneOrientation, XtkSplitPaneSlot, XtkSplitPaneRatioChangedCallback

Functions: xtk_splitpane_create, xtk_splitpane_destroy, xtk_splitpane_set_child, xtk_splitpane_set_ratio, xtk_splitpane_get_ratio, xtk_splitpane_set_collapsed, xtk_splitpane_is_collapsed, xtk_splitpane_set_position, xtk_splitpane_resize, xtk_splitpane_get_window, xtk_splitpane_get_slot_size, xtk_splitpane_set_divider_thickness, xtk_splitpane_handle_event, xtk_splitpane_draw, xtk_splitpane_set_theme, xtk_splitpane_set_ratio_changed_callback, xtk_splitpane_config_init_defaults, xtk_splitpane_config_parse_styling, xtk_splitpane_config_write_styling, xtk_splitpane_widget_config_init_defaults, xtk_splitpane_widget_config_parse, xtk_splitpane_widget_config_write

Types

XtkSplitPane

typedef struct XtkSplitPane XtkSplitPane;

A split pane; opaque, made by xtk_splitpane_create().

XtkSplitPaneOrientation

How a split pane places its two slots.

Name Value Description
XTK_SPLITPANE_HORIZONTAL 0 Side by side, first on the left; the divider is a vertical bar.
XTK_SPLITPANE_VERTICAL One above the other, first on top; the divider is a horizontal bar.

XtkSplitPaneSlot

One of the two slots of a split pane.

Name Value Description
XTK_SPLITPANE_FIRST 0 The left slot (horizontal) or the top slot (vertical)
XTK_SPLITPANE_SECOND The right slot (horizontal) or the bottom slot (vertical)

XtkSplitPaneRatioChangedCallback

typedef void (*XtkSplitPaneRatioChangedCallback)(XtkSplitPane *splitpane, float ratio, void *user_data);

Called while the user drags the divider: on every motion, and once more when the button is released.

It is not called when the program sets the ratio. A program uses it to remember the ratio, or to lay out other windows to match.

Functions

xtk_splitpane_create

XtkSplitPane *xtk_splitpane_create(
    Display *display,
    int screen,
    Window parent,
    int x,
    int y,
    int width,
    int height,
    XtkSplitPaneOrientation orientation,
    const SplitPaneBlock *theme);

Create an empty split pane and map its window.

The ratio starts at 0.5, the divider is 3 pixels thick and each slot's minimum is 32 pixels until xtk_splitpane_set_child() sets it.

Parameter Description
display The X display
screen The screen number
parent The parent window
x The X position in the parent, in pixels
y The Y position in the parent, in pixels
width The width in pixels; must be at least 1
height The height in pixels; must be at least 1
orientation How the two slots are placed
theme The colors, usually &config.splitpane. The pane keeps the pointer, so the block must stay valid as long as the pane does. May be NULL (black, and the divider is not drawn until xtk_splitpane_set_theme()).

Returns: The new split pane, or NULL if display or parent is 0, memory runs out or the window cannot be made

xtk_splitpane_destroy

void xtk_splitpane_destroy(XtkSplitPane *splitpane);

Destroy a split pane, but not its two children.

Each child that is still inside the pane is moved back to the pane's parent (at 0, 0) and unmapped first, so the program keeps it. A child that the program has already moved elsewhere is left alone.

Parameter Description
splitpane The split pane (NULL does nothing)

xtk_splitpane_set_child

void xtk_splitpane_set_child(
    XtkSplitPane *splitpane,
    XtkSplitPaneSlot slot,
    Window child,
    int min_size);

Put a window into a slot, set the slot's minimum size, and lay the pane out again.

The window is moved into the pane's window, sized to the slot and mapped. A window that was in the slot before is given back to the pane's parent, unmapped, as xtk_splitpane_destroy() gives back the children.

Parameter Description
splitpane The split pane (NULL does nothing)
slot The slot
child The window to hold, or None to leave the slot empty
min_size The slot's smallest size in pixels (its width in a horizontal split, its height in a vertical one); values below 0 become 0. A pane too small for both minimums gives the first slot less.

xtk_splitpane_set_ratio

void xtk_splitpane_set_ratio(XtkSplitPane *splitpane, float ratio);

Set how much of the space the first slot gets, and lay the pane out again.

The ratio-changed callback is not called. The ratio is also the one a collapsed slot returns to.

Parameter Description
splitpane The split pane (NULL does nothing)
ratio The first slot's share of the space beside the divider, from 0.0 to 1.0 (values outside become 0.0 or 1.0). The slots' minimum sizes still apply to the layout, but not to the stored ratio.

xtk_splitpane_get_ratio

float xtk_splitpane_get_ratio(const XtkSplitPane *splitpane);

Get the first slot's share of the space.

Parameter Description
splitpane The split pane

Returns: The ratio last set or dragged to, from 0.0 to 1.0 (before the minimum sizes are applied), or 0.5 if splitpane is NULL

xtk_splitpane_set_collapsed

void xtk_splitpane_set_collapsed(
    XtkSplitPane *splitpane,
    XtkSplitPaneSlot slot,
    int collapsed);

Collapse a slot or show it again, and lay the pane out again.

A collapsed slot's window is unmapped and the other slot fills the whole pane, without a divider. If both slots are collapsed, the first one is hidden.

Parameter Description
splitpane The split pane (NULL does nothing)
slot The slot
collapsed 1 to collapse it; 0 to show it again at the ratio it had before

xtk_splitpane_is_collapsed

int xtk_splitpane_is_collapsed(const XtkSplitPane *splitpane, XtkSplitPaneSlot slot);

Tell whether a slot is collapsed.

Parameter Description
splitpane The split pane
slot The slot

Returns: 1 if it is collapsed, 0 if not, or if splitpane is NULL or slot is not a valid slot

xtk_splitpane_set_position

void xtk_splitpane_set_position(XtkSplitPane *splitpane, int x, int y);

Move the split pane within its parent.

Parameter Description
splitpane The split pane (NULL does nothing)
x The new X position in the parent, in pixels
y The new Y position in the parent, in pixels

xtk_splitpane_resize

void xtk_splitpane_resize(XtkSplitPane *splitpane, int width, int height);

Resize the split pane and lay out both children again at the same ratio.

Nothing happens when the size does not change.

Parameter Description
splitpane The split pane (NULL does nothing)
width The new width in pixels; 0 or less does nothing
height The new height in pixels; 0 or less does nothing

xtk_splitpane_get_window

Window xtk_splitpane_get_window(const XtkSplitPane *splitpane);

Get the split pane's X window (the parent of both children).

Parameter Description
splitpane The split pane

Returns: The window, or None if splitpane is NULL

xtk_splitpane_get_slot_size

void xtk_splitpane_get_slot_size(
    const XtkSplitPane *splitpane,
    XtkSplitPaneSlot slot,
    int *width,
    int *height);

Get the size the pane last gave a slot.

The value is the pane's own, current right after a resize, ratio change or collapse, without asking the X server. A collapsed slot has a size of 0 along the split. Either pointer may be NULL.

Parameter Description
splitpane The split pane
slot The slot
width Receives the slot's width in pixels (0 if splitpane is NULL or slot is not a valid slot)
height Receives the slot's height in pixels (0 if splitpane is NULL or slot is not a valid slot)

xtk_splitpane_set_divider_thickness

void xtk_splitpane_set_divider_thickness(XtkSplitPane *splitpane, int thickness);

Set the divider's thickness and lay the pane out again.

The pane starts with 3 pixels. Call this after xtk_splitpane_create() with config.splitpane_widget.divider_thickness to use the configured value.

Parameter Description
splitpane The split pane (NULL does nothing)
thickness The divider's width (horizontal split) or height (vertical split) in pixels; values below 1 become 1

xtk_splitpane_handle_event

int xtk_splitpane_handle_event(XtkSplitPane *splitpane, XEvent *event);

Give an X event to the split pane.

A left-button press on the divider grabs the pointer and starts a drag; each motion moves the divider to the pointer and calls the ratio-changed callback; the release ends the drag and calls the callback once more.

Parameter Description
splitpane The split pane
event The event

Returns: 1 if the event was for the divider, or was the last Expose of the pane's own window; 0 otherwise (also for events of the children, and if an argument is NULL)

xtk_splitpane_draw

void xtk_splitpane_draw(XtkSplitPane *splitpane);

Redraw the divider now.

Parameter Description
splitpane The split pane (NULL does nothing)

xtk_splitpane_set_theme

void xtk_splitpane_set_theme(XtkSplitPane *splitpane, const SplitPaneBlock *theme);

Use new colors, and redraw the pane's background and the divider.

Parameter Description
splitpane The split pane (NULL does nothing)
theme The colors, usually &config.splitpane (NULL does nothing). The pane keeps the pointer.

xtk_splitpane_set_ratio_changed_callback

void xtk_splitpane_set_ratio_changed_callback(
    XtkSplitPane *splitpane,
    XtkSplitPaneRatioChangedCallback callback,
    void *user_data);

Set the function called while the divider is dragged.

Note: During a motion the callback runs while the pane holds the X server grabbed (XGrabServer()), so the whole layout change appears at once. The callback must therefore be quick: it must not block or wait for other clients (no dialogs, no loops around XSync()), and it must not destroy the pane. It may move and resize other windows.

Parameter Description
splitpane The split pane (NULL does nothing)
callback The function, or NULL for none
user_data A pointer passed to the function

xtk_splitpane_config_init_defaults

void xtk_splitpane_config_init_defaults(SplitPaneBlock *splitpane_config);

Set a SplitPaneBlock to the [splitpane] defaults (listed above).

xtk_config_init_defaults() already calls it for config.splitpane.

Parameter Description
splitpane_config The block to set (NULL does nothing)

xtk_splitpane_config_parse_styling

int xtk_splitpane_config_parse_styling(
    SplitPaneBlock *splitpane_config,
    const char *key,
    const char *value);

Set one [splitpane] key of a SplitPaneBlock from its text value.

Parameter Description
splitpane_config The block to change
key The key: "background" or "divider-background"
value The color as written in the file

Returns: 1 if the key is a [splitpane] key, 0 if not or an argument is NULL

xtk_splitpane_config_write_styling

int xtk_splitpane_config_write_styling(
    FILE *file,
    const SplitPaneBlock *splitpane_config);

Write a SplitPaneBlock as a [splitpane] section, with both keys.

Parameter Description
file The file to write to
splitpane_config The block to write

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

xtk_splitpane_widget_config_init_defaults

void xtk_splitpane_widget_config_init_defaults(XtkConfig *config);

Set the [splitpane-widget] defaults (listed above) in a configuration.

xtk_config_init_defaults() already calls it.

Parameter Description
config The configuration to change (NULL does nothing)

xtk_splitpane_widget_config_parse

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

Set one [splitpane-widget] key of a configuration from its text value.

Parameter Description
config The configuration to change
key The key: "divider-thickness" or "min-size"
value The value as written in the file (a whole number)

Returns: 1 if the key is a [splitpane-widget] key, 0 if not or an argument is NULL

xtk_splitpane_widget_config_write

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

Write a configuration's [splitpane-widget] section, with both keys.

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

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