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

xtk_grid.h

A layout helper that places widgets in the cells of rows and columns.

A grid works out where widgets go in a window: it divides its box into rows and columns (together called tracks), and moves and sizes each widget to fit the cell, or span of cells, it was attached to. It has no window of its own and draws nothing (except outlines for checking a layout, see xtk_grid_draw_debug()). It does not know any widget type: for each widget the program gives three functions, to move it, to resize it and to report the size it wants.

Each track is sized in one of three ways (XtkGridSizeMode): to the largest widget in it (AUTO, the default), to a fixed number of pixels (FIXED), or to a weighted share of the space that is left (EXPAND). There are 4 pixels between tracks unless set otherwise. Within its cell, a widget fills the cell (the default) or keeps its own size and is aligned to the start, the center or the end. Cells can have margins, and the whole grid can have padding. Grids can be nested: a nested grid fills its cell and lays out its own widgets in it.

Making it. Call xtk_grid_create() with the window the widgets live in and the box to fill (in that window's coordinates). Set the tracks, attach the widgets (made by the program as children of that window), then call xtk_grid_layout(). The grid has no resize call: when the box changes, set the x, y, width and height fields of the XtkGrid and call xtk_grid_layout() again. Call it again also after widgets change the size they want. The program owns the grid and frees it with xtk_grid_destroy(). The grid never destroys the widgets attached to it, but it destroys every nested grid attached with xtk_grid_attach_grid().

Events. A grid takes no events and has no callbacks of its own; the widgets handle their events themselves.

Theme keys. None: the grid draws nothing in normal use.

Example.

static void move_button(void *widget, int x, int y) { xtk_button_set_position(widget, x, y); }
static void size_button(void *widget, int w, int h) { xtk_button_resize(widget, w, h); }
static void measure_button(void *widget, int *w, int *h) { xtk_button_get_size(widget, w, h); }

// ok and cancel are XtkButton widgets made with xtk_button_create(),
// as children of window.
XtkGrid *grid = xtk_grid_create(display, screen, window, 0, 0, 400, 40);
xtk_grid_set_padding(grid, 8, 8, 8, 8);
xtk_grid_set_column_size(grid, 0, XTK_GRID_SIZE_EXPAND, 1);
xtk_grid_attach_aligned(grid, ok, move_button, size_button, measure_button,
                        1, 0, 1, 1, XTK_ALIGN_START, XTK_ALIGN_CENTER);
xtk_grid_attach_aligned(grid, cancel, move_button, size_button, measure_button,
                        2, 0, 1, 1, XTK_ALIGN_START, XTK_ALIGN_CENTER);
xtk_grid_layout(grid);   // the buttons sit at the right; column 0 takes the rest

// when the window is resized to new_width:
grid->width = new_width;
xtk_grid_layout(grid);

xtk_grid_destroy(grid);  // the buttons stay; destroy them separately
 * 

Limitations.

  • Only widgets that span one track decide the size of an AUTO track; a widget that spans several tracks does not make them bigger.
  • A nested grid is measured by what its children ask for (fixed tracks at their size, the others at their largest one-track child), so an AUTO track that holds it is right from the first layout; its EXPAND tracks count only their children's size.
  • Cell alignment and cell margins have no effect on nested grids.
  • An EXPAND track is never smaller than 10 pixels, even when there is no room left; the grid can then grow past its box.
  • Rows and columns cannot be removed, only added.
#include "xtk_grid.h"

Types: XtkGridSizeMode, XtkAlignment, XtkGridChild, XtkGridTrack, XtkGrid

Functions: xtk_grid_create, xtk_grid_destroy, xtk_grid_set_padding, xtk_grid_set_row_spacing, xtk_grid_set_column_spacing, xtk_grid_set_content_align, xtk_grid_set_homogeneous, xtk_grid_set_row_size, xtk_grid_set_column_size, xtk_grid_set_row_spacing_after, xtk_grid_set_column_spacing_after, xtk_grid_ensure_size, xtk_grid_attach, xtk_grid_remove, xtk_grid_set_child_min_size, xtk_grid_attach_aligned, xtk_grid_attach_grid, xtk_grid_detach_grid, xtk_grid_set_cell_align, xtk_grid_set_cell_margin, xtk_grid_layout, xtk_grid_get_cell_bounds, xtk_grid_get_span_bounds, xtk_grid_get_content_size, xtk_grid_set_show_lines, xtk_grid_set_line_color, xtk_grid_draw_debug

Types

XtkGridSizeMode

How a row or column is sized.

Name Value Description
XTK_GRID_SIZE_AUTO As large as the largest widget in it that spans only this track (the default)
XTK_GRID_SIZE_FIXED A fixed size in pixels.
XTK_GRID_SIZE_EXPAND A share of the space the other tracks leave, by weight (at least 10 pixels)

XtkAlignment

Where a widget goes in its cell, or the content in the grid's box.

Name Value Description
XTK_ALIGN_START At the left (horizontally) or the top (vertically), at its own size.
XTK_ALIGN_CENTER In the middle, at its own size.
XTK_ALIGN_END At the right (horizontally) or the bottom (vertically), at its own size.
XTK_ALIGN_FILL Stretched to fill the cell (the default for attached widgets)

XtkGridChild

One attached widget: the functions that move and size it, and where it sits in the grid. The grid fills it in; a program reads it, if at all, through XtkGrid::children.

Field Type Description
widget void * The widget, passed to every function below.
set_position void (*)(void *widget, int x, int y) Moves the widget to x, y in the grid's parent window.
set_size void (*)(void *widget, int w, int h) Resizes the widget; NULL if it cannot be resized.
get_size void (*)(void *widget, int *w, int *h) Reports the size the widget wants; NULL counts as no size.
get_min_size void (*)(void *widget, int *w, int *h) Reports the smallest size the widget can shrink to (see xtk_grid_set_child_min_size()); NULL if it never shrinks below the size get_size() reports.
column int The first column of its cell.
row int The first row of its cell.
column_span int How many columns the cell spans (at least 1)
row_span int How many rows the cell spans (at least 1)
horizontal_align XtkAlignment Its horizontal place in the cell.
vertical_align XtkAlignment Its vertical place in the cell.
margin_left int Space between the cell's left edge and the widget, in pixels.
margin_right int Space between the widget and the cell's right edge, in pixels.
margin_top int Space between the cell's top edge and the widget, in pixels.
margin_bottom int Space between the widget and the cell's bottom edge, in pixels.

XtkGridTrack

One row or column, and the size and place the last layout gave it.

Field Type Description
mode XtkGridSizeMode How it is sized.
size int For FIXED, the size in pixels; for EXPAND, the weight (0 or less counts as 1); unused for AUTO.
computed_size int Its size in pixels after the last xtk_grid_layout()
computed_position int Its start after the last xtk_grid_layout(), in pixels from the grid's x (column) or y (row)
spacing_after int The space after it in pixels, or -1 for the grid's row or column spacing.

XtkGrid

A grid layout: its box, its rows and columns, and the widgets placed in them.

A grid layout; see struct XtkGrid for its fields.

The struct is public so a program can read the results of a layout and change the box (x, y, width, height) before calling xtk_grid_layout() again. Change the other fields only through the functions of this header.

Field Type Description
display Display * The X display.
screen int The screen number.
parent Window The window the widgets live in; positions are in its coordinates.
x int The box's X in the parent window.
y int The box's Y in the parent window.
width int The box's width in pixels.
height int The box's height in pixels.
pad_left int Padding inside the box's left edge, in pixels.
pad_right int Padding inside the box's right edge, in pixels.
pad_top int Padding inside the box's top edge, in pixels.
pad_bottom int Padding inside the box's bottom edge, in pixels.
row_spacing int The space between rows in pixels (default 4)
column_spacing int The space between columns in pixels (default 4)
content_horizontal_align XtkAlignment Where the tracks as a whole go across the box (default START)
content_vertical_align XtkAlignment Where the tracks as a whole go down the box (default START)
homogeneous_rows int 1 if all AUTO rows get the height of the tallest one (see xtk_grid_set_homogeneous()); 0 by default.
homogeneous_columns int 1 if all AUTO columns get the width of the widest one; 0 by default
rows XtkGridTrack * The rows, row_count of them in use.
columns XtkGridTrack * The columns, column_count of them in use.
row_count int The number of rows.
column_count int The number of columns.
row_capacity int The number of rows allocated.
column_capacity int The number of columns allocated.
children XtkGridChild * The attached widgets (nested grids included), child_count of them.
child_count int The number of attached widgets.
child_capacity int The number of children allocated.
subgrids XtkGrid ** The nested grids, which this grid destroys with itself.
subgrid_count int The number of nested grids.
subgrid_capacity int The number of nested grids allocated.
show_grid_lines int 1 if xtk_grid_draw_debug() draws outlines (see xtk_grid_set_show_lines())
grid_line_color unsigned long The outline color as a pixel value (default 0xFF00FF, magenta)
debug_gc GC The graphics context the outlines are drawn with.

Functions

xtk_grid_create

XtkGrid *xtk_grid_create(
    Display *display,
    int screen,
    Window parent,
    int x,
    int y,
    int width,
    int height);

Create an empty grid that lays out widgets in a box of a window.

The grid starts with no rows or columns; attaching widgets or setting track sizes adds them.

Parameter Description
display The X display
screen The screen number
parent The window the widgets live in
x The box's X in parent, in pixels
y The box's Y in parent, in pixels
width The box's width in pixels
height The box's height in pixels

Returns: The new grid, or NULL if display is NULL or memory runs out

xtk_grid_destroy

void xtk_grid_destroy(XtkGrid *grid);

Destroy the grid and every nested grid attached to it.

The attached widgets are not destroyed.

Parameter Description
grid The grid (NULL does nothing)

xtk_grid_set_padding

void xtk_grid_set_padding(XtkGrid *grid, int left, int top, int right, int bottom);

Set the padding between the box's edges and the tracks.

It takes effect at the next xtk_grid_layout().

Parameter Description
grid The grid (NULL does nothing)
left The left padding in pixels
top The top padding in pixels
right The right padding in pixels
bottom The bottom padding in pixels

xtk_grid_set_row_spacing

void xtk_grid_set_row_spacing(XtkGrid *grid, int spacing);

Set the space between rows (4 pixels by default).

A row's own spacing from xtk_grid_set_row_spacing_after() takes precedence. It takes effect at the next xtk_grid_layout().

Parameter Description
grid The grid (NULL does nothing)
spacing The space in pixels

xtk_grid_set_column_spacing

void xtk_grid_set_column_spacing(XtkGrid *grid, int spacing);

Set the space between columns (4 pixels by default).

A column's own spacing from xtk_grid_set_column_spacing_after() takes precedence. It takes effect at the next xtk_grid_layout().

Parameter Description
grid The grid (NULL does nothing)
spacing The space in pixels

xtk_grid_set_content_align

void xtk_grid_set_content_align(
    XtkGrid *grid,
    XtkAlignment h_align,
    XtkAlignment v_align);

Set where the tracks as a whole go in the box, when they need less space than it has.

FILL counts as START here. It takes effect at the next xtk_grid_layout().

Parameter Description
grid The grid (NULL does nothing)
h_align Where they go across the box (START by default)
v_align Where they go down the box (START by default)

xtk_grid_set_homogeneous

void xtk_grid_set_homogeneous(XtkGrid *grid, int rows, int columns);

Give all AUTO rows, all AUTO columns, or both, one common size: that of the largest of them.

FIXED and EXPAND tracks are not changed. When space is short, the shrinking described at xtk_grid_set_child_min_size() can still make the tracks differ. Both are off by default. It takes effect at the next xtk_grid_layout().

Parameter Description
grid The grid (NULL does nothing)
rows Not 0: every AUTO row gets the height of the tallest AUTO row. 0: each row has its own height.
columns The same for the widths of AUTO columns

xtk_grid_set_row_size

void xtk_grid_set_row_size(XtkGrid *grid, int row, XtkGridSizeMode mode, int value);

Set how a row is sized, adding rows up to it if needed.

Parameter Description
grid The grid (NULL does nothing)
row The row, from 0 (a negative row does nothing)
mode How it is sized
value For FIXED, the height in pixels; for EXPAND, the weight (0 or less counts as 1); unused for AUTO

xtk_grid_set_column_size

void xtk_grid_set_column_size(XtkGrid *grid, int column, XtkGridSizeMode mode, int value);

Set how a column is sized, adding columns up to it if needed.

Parameter Description
grid The grid (NULL does nothing)
column The column, from 0 (a negative column does nothing)
mode How it is sized
value For FIXED, the width in pixels; for EXPAND, the weight (0 or less counts as 1); unused for AUTO

xtk_grid_set_row_spacing_after

void xtk_grid_set_row_spacing_after(XtkGrid *grid, int row, int spacing);

Set the space after one row, in place of the grid's row spacing.

The space after the last row is never used.

Parameter Description
grid The grid (NULL does nothing)
row The row, from 0 (rows are added up to it if needed)
spacing The space in pixels, or -1 for the grid's row spacing

xtk_grid_set_column_spacing_after

void xtk_grid_set_column_spacing_after(XtkGrid *grid, int column, int spacing);

Set the space after one column, in place of the grid's column spacing.

The space after the last column is never used.

Parameter Description
grid The grid (NULL does nothing)
column The column, from 0 (columns are added up to it if needed)
spacing The space in pixels, or -1 for the grid's column spacing

xtk_grid_ensure_size

void xtk_grid_ensure_size(XtkGrid *grid, int rows, int columns);

Add AUTO rows and columns until the grid has at least this many.

The grid never removes tracks.

Parameter Description
grid The grid (NULL does nothing)
rows The number of rows needed
columns The number of columns needed

xtk_grid_attach

void xtk_grid_attach(
    XtkGrid *grid,
    void *widget,
    void(*)(void *, int, int)set_pos,
    void(*)(void *, int, int)set_size,
    void(*)(void *, int *, int *)get_size,
    int column,
    int row,
    int column_span,
    int row_span);

Attach a widget to a cell (or a span of cells), filling it.

It is xtk_grid_attach_aligned() with FILL for both alignments. Rows and columns are added as needed. The widget is placed at the next xtk_grid_layout().

Parameter Description
grid The grid (NULL does nothing)
widget The widget, passed to the three functions (NULL does nothing)
set_pos Moves the widget to x, y in the grid's parent window, or NULL
set_size Resizes the widget, or NULL if it cannot be resized
get_size Reports the size the widget wants, or NULL
column The cell's column, from 0; a negative one adds nothing
row The cell's row, from 0; a negative one adds nothing
column_span The number of columns the cell spans (values below 1 become 1)
row_span The number of rows the cell spans (values below 1 become 1)

xtk_grid_remove

void xtk_grid_remove(XtkGrid *grid, void *widget);

Take a widget out of the grid.

The widget is not destroyed and not moved. The tracks stay: an AUTO track that becomes empty is 0 pixels at the next layout. For a nested grid, use xtk_grid_detach_grid() instead.

Parameter Description
grid The grid (NULL does nothing)
widget The pointer given when the widget was attached (NULL or a pointer that was never attached does nothing)

xtk_grid_set_child_min_size

void xtk_grid_set_child_min_size(
    XtkGrid *grid,
    void *widget,
    void(*)(void *widget, int *w, int *h)get_min_size);

Give an attached widget a smallest size, below the size it wants.

It matters only when the AUTO tracks do not fit in the box. They then shrink, before anything overflows, each by a part of the shortfall in proportion to its slack: the size it wants minus the smallest size of its widgets. A widget without a smallest size has no slack, so its track does not shrink. FIXED tracks never shrink.

Parameter Description
grid The grid (NULL does nothing)
widget The pointer given when the widget was attached (a pointer that was never attached does nothing)
get_min_size Reports the smallest width and height the widget can have, or NULL to remove a smallest size set before

xtk_grid_attach_aligned

void xtk_grid_attach_aligned(
    XtkGrid *grid,
    void *widget,
    void(*)(void *, int, int)set_pos,
    void(*)(void *, int, int)set_size,
    void(*)(void *, int *, int *)get_size,
    int column,
    int row,
    int column_span,
    int row_span,
    XtkAlignment h_align,
    XtkAlignment v_align);

Attach a widget to a cell (or a span of cells), aligned in it.

Rows and columns are added as needed. At each xtk_grid_layout() the grid calls set_pos; it calls set_size only when at least one alignment is FILL, with the cell's size on a FILL axis and the widget's own size on the other. The widget's margins start at 0 (see xtk_grid_set_cell_margin()).

Parameter Description
grid The grid (NULL does nothing)
widget The widget, passed to the three functions (NULL does nothing)
set_pos Moves the widget to x, y in the grid's parent window, or NULL
set_size Resizes the widget, or NULL if it cannot be resized
get_size Reports the size the widget wants, or NULL
column The cell's column, from 0; a negative one adds nothing
row The cell's row, from 0; a negative one adds nothing
column_span The number of columns the cell spans (values below 1 become 1)
row_span The number of rows the cell spans (values below 1 become 1)
h_align Its horizontal place in the cell
v_align Its vertical place in the cell

xtk_grid_attach_grid

void xtk_grid_attach_grid(
    XtkGrid *parent,
    XtkGrid *child,
    int column,
    int row,
    int column_span,
    int row_span);

Attach a grid to a cell of another grid, as a nested grid.

At each xtk_grid_layout() of the parent, the nested grid's box is set to the cell (or span) and the nested grid is laid out in it. Use the nested grid's xtk_grid_set_content_align() to place its content in the cell; cell alignment and cell margins do not apply to it. The nested grid's own box and parent window are replaced by the cell, so make it with the same parent window as the outer grid.

Note: The parent grid takes the nested grid over: xtk_grid_destroy() of the parent destroys it too. Do not destroy it yourself while it is attached; take it out with xtk_grid_detach_grid() first.

Parameter Description
parent The outer grid (NULL does nothing)
child The nested grid (NULL does nothing)
column The cell's column, from 0
row The cell's row, from 0
column_span The number of columns the cell spans
row_span The number of rows the cell spans

xtk_grid_detach_grid

void xtk_grid_detach_grid(XtkGrid *parent, XtkGrid *child);

Take a nested grid out of its outer grid.

Undoes xtk_grid_attach_grid(): the outer grid no longer lays it out or destroys it, and the program owns it again (for example to destroy it and attach a new one).

Parameter Description
parent The outer grid (NULL does nothing)
child The nested grid (NULL does nothing)

xtk_grid_set_cell_align

void xtk_grid_set_cell_align(
    XtkGrid *grid,
    int row,
    int column,
    XtkAlignment h_align,
    XtkAlignment v_align);

Set the alignment of the widget whose cell starts at a row and column.

If several widgets start there, the first one attached is changed. It takes effect at the next xtk_grid_layout().

Parameter Description
grid The grid (NULL does nothing)
row The row the widget's cell starts at
column The column the widget's cell starts at
h_align Its horizontal place in the cell
v_align Its vertical place in the cell

xtk_grid_set_cell_margin

void xtk_grid_set_cell_margin(
    XtkGrid *grid,
    int row,
    int column,
    int left,
    int top,
    int right,
    int bottom);

Set the margins of the widget whose cell starts at a row and column.

The margins are space between the cell's edges and the widget; they also count in the size of an AUTO track. If several widgets start there, the first one attached is changed. It takes effect at the next xtk_grid_layout().

Parameter Description
grid The grid (NULL does nothing)
row The row the widget's cell starts at
column The column the widget's cell starts at
left The left margin in pixels
top The top margin in pixels
right The right margin in pixels
bottom The bottom margin in pixels

xtk_grid_layout

void xtk_grid_layout(XtkGrid *grid);

Work out the size and place of every track, then move and size every widget and lay out every nested grid.

Call it after the widgets are attached, and again whenever the box changes or a widget wants another size. It does nothing while the grid has no rows or no columns.

Parameter Description
grid The grid (NULL does nothing)

xtk_grid_get_cell_bounds

void xtk_grid_get_cell_bounds(
    XtkGrid *grid,
    int row,
    int column,
    int *x,
    int *y,
    int *w,
    int *h);

Get the box of one cell, as the last layout made it.

Any pointer may be NULL. All four values are 0 if grid is NULL or the cell is outside the grid.

Parameter Description
grid The grid
row The cell's row
column The cell's column
x Receives the cell's X in the parent window
y Receives the cell's Y in the parent window
w Receives the cell's width
h Receives the cell's height

xtk_grid_get_span_bounds

void xtk_grid_get_span_bounds(
    XtkGrid *grid,
    int row,
    int column,
    int row_span,
    int column_span,
    int *x,
    int *y,
    int *w,
    int *h);

Get the box of a span of cells, with the spacing inside it, as the last layout made it.

A span that runs past the last row or column is cut off there. Any pointer may be NULL. All four values are 0 if grid is NULL, row or column is negative or past the last one, or a span is below 1.

Parameter Description
grid The grid
row The first cell's row
column The first cell's column
row_span The number of rows spanned
column_span The number of columns spanned
x Receives the span's X in the parent window
y Receives the span's Y in the parent window
w Receives the span's width
h Receives the span's height

xtk_grid_get_content_size

void xtk_grid_get_content_size(XtkGrid *grid, int *w, int *h);

Get the size the content needs: every track and the spacing between them, plus the padding, as the last layout made them.

Use it to size a window to fit the grid (lay it out once first). Either pointer may be NULL.

Parameter Description
grid The grid; if NULL, both values are 0
w Receives the width in pixels
h Receives the height in pixels

xtk_grid_set_show_lines

void xtk_grid_set_show_lines(XtkGrid *grid, int show);

Turn the outlines of xtk_grid_draw_debug() on or off.

The outlines are for checking a layout while a program is being written. They are off by default, and a nested grid has its own setting.

Parameter Description
grid The grid (NULL does nothing)
show 1 to draw outlines, 0 not to

xtk_grid_set_line_color

void xtk_grid_set_line_color(XtkGrid *grid, unsigned long color);

Set the color of the outlines of xtk_grid_draw_debug() (magenta by default).

Parameter Description
grid The grid (NULL does nothing)
color The color as a pixel value of the screen

xtk_grid_draw_debug

void xtk_grid_draw_debug(XtkGrid *grid, Drawable target);

Draw an outline of the grid's box and of every track, if outlines are on (see xtk_grid_set_show_lines()).

The lines are drawn over the widgets. Nested grids are outlined too, when their own outlines are on. Call it after xtk_grid_layout(), for example after the parent window is redrawn.

Parameter Description
grid The grid (NULL does nothing)
target Where to draw: the grid's parent window, or a drawable of the same depth with the same coordinates