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

xtk_datagrid.h

A small table of editable text cells, with a header row and an Add Row button.

A data grid is a table for editing a modest amount of data in a form: a header row with one label per column, a row of text cells for each record, and a "+ Add Row" button under the last row. Each cell is an xtk_textview, so it edits like any text field. The rows have alternating backgrounds; no grid lines are drawn, only a one-pixel outline around the whole table. When the rows are taller than the widget, it scrolls them itself and shows a thin scrollbar on its right.

The program identifies each row by a long long id of its own (a database key, for example), which the grid only carries, and each column by a string key, which stays the same when columns are moved. The grid keeps no data of its own beyond the text in its cells: the program saves each change as the cell-changed callback reports it.

Making one. xtk_datagrid_create() takes the parent window, the program's XtkConfig, an XtkClipboard for the cells, and the position and size. The window is mapped at once. The grid keeps the XtkConfig pointer (it must stay valid while the grid exists) and copies the colors it needs. Then:

  1. xtk_datagrid_set_columns() sets the column keys and header labels.
  2. xtk_datagrid_add_row() adds each row with its id and its text.
  3. The callbacks report cell edits, Add Row clicks and removed rows.

Free the grid with xtk_datagrid_destroy().

Events. Pass every X event to xtk_datagrid_handle_event(); it passes them on to the cells, the header labels and the button. Call xtk_datagrid_update_blink() on the timer that blinks the other text cursors.

  • Typing in a cell edits it. Tab and Shift+Tab move to the next and the previous cell, row by row, and wrap around; Ctrl+Tab is left to the program's focus manager.
  • A click on a header sorts the rows by that column; a second click on the same header reverses the order. An arrow on the header shows the sort.
  • Pressing within 5 pixels of the right edge of a header and dragging changes the width of that column.
  • Dragging a header and releasing it over another header swaps the two columns.
  • Ctrl+click on a cell selects or deselects its row (the click also puts the text cursor in the cell). Selected rows are drawn in the selection colors; the program reads them for actions such as deleting rows.
  • The wheel anywhere over the grid scrolls it by 40 pixels; the scrollbar thumb can be dragged.
  • "+ Add Row" fires the add-row callback; the program then adds the row.

Theme keys. [datagrid] (DataGridBlock, config.datagrid):

  • background: the cells of the even rows and the area around the cells (default #2E2E2E).
  • color: the text of the cells (default D9D9D9).
  • alternate-background: the cells of the odd rows, counting the first row as 1 (default #262626).
  • header-background, header-foreground: the header row (defaults #2E2E2E and D9D9D9).
  • selected-background, selected-foreground: the cells of selected rows (defaults #3870C7 and FFFFFF).

background, alternate-background, selected-background and header-background can be gradients. The grid also uses other sections: the cells take the rest of their style (font, cursor, text selection, focus border) from [textview], the header labels their font from [label], the Add Row button [button], the outline border of [frame], and the scrollbar thumb scrollbar-thumb of [textview] (thumb-hover of [scrollbar] while it is hovered or dragged).

Example.

static void cell_changed(void *user_data, long long row_id, const char *column_key, const char *text) {
    (void)user_data;
    printf("row %lld, %s = %s\n", row_id, column_key, text);
}

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

XtkDataGrid *grid = xtk_datagrid_create(display, screen, window, &config, clipboard, 10, 10, 400, 200);
const char *keys[] = { "item", "amount" };
const char *labels[] = { "Item", "Amount" };
xtk_datagrid_set_columns(grid, keys, labels, 2);
const char *row1[] = { "Flour", "500 g" };
xtk_datagrid_add_row(grid, 1, row1);
xtk_datagrid_set_cell_changed_callback(grid, cell_changed, NULL);

XEvent event;
for (;;) {
    XNextEvent(display, &event);
    xtk_datagrid_handle_event(grid, &event);
}
 * 

Limitations.

  • A grid has at most 8 columns and 512 rows. Column keys and labels are cut at 63 bytes.
  • Every row has the same height, one line of text; a cell can hold more lines, but only the first shows unless the cell is scrolled.
  • Sorting compares the text, ignoring case; it does not compare numbers by value.
  • There is no built-in way for the user to remove a row; the program calls xtk_datagrid_remove_row() from its own controls.
  • Removing a row, sorting and swapping columns make all the cells again, so the text cursor and the undo history of the cells are lost.
#include "xtk_datagrid.h"

Types: XtkDataGrid

Constants: XTK_DATAGRID_MAX_COLUMNS, XTK_DATAGRID_MAX_ROWS

Functions: xtk_datagrid_create, xtk_datagrid_destroy, xtk_datagrid_set_theme, xtk_datagrid_set_enabled, xtk_datagrid_get_enabled, xtk_datagrid_show, xtk_datagrid_hide, xtk_datagrid_get_hidden, xtk_datagrid_set_columns, xtk_datagrid_clear_rows, xtk_datagrid_add_row, xtk_datagrid_remove_row, xtk_datagrid_sort_by_column, xtk_datagrid_set_column_width, xtk_datagrid_swap_columns, xtk_datagrid_is_row_selected, xtk_datagrid_get_selected_row_count, xtk_datagrid_get_selected_row_at, xtk_datagrid_clear_row_selection, xtk_datagrid_set_position, xtk_datagrid_resize, xtk_datagrid_get_content_height, xtk_datagrid_handle_event, xtk_datagrid_get_window, xtk_datagrid_set_focused, xtk_datagrid_owns_window, xtk_datagrid_update_blink, xtk_datagrid_set_add_row_button_visible, xtk_datagrid_set_cell_changed_callback, xtk_datagrid_set_add_row_callback, xtk_datagrid_set_remove_row_callback, xtk_datagrid_set_row_selected_at

Types

XtkDataGrid

typedef struct XtkDataGrid XtkDataGrid;

A table of editable text cells (opaque). Made by xtk_datagrid_create().

Constants

XTK_DATAGRID_MAX_COLUMNS

#define XTK_DATAGRID_MAX_COLUMNS 8

The most columns a grid can have.

XTK_DATAGRID_MAX_ROWS

#define XTK_DATAGRID_MAX_ROWS 512

The most rows a grid can hold. xtk_datagrid_add_row() fails past it.

Functions

xtk_datagrid_create

XtkDataGrid *xtk_datagrid_create(
    Display *display,
    int screen,
    Window parent,
    const XtkConfig *theme,
    XtkClipboard *clipboard,
    int x,
    int y,
    int width,
    int height);

Creates a data grid, mapped, with no columns and no rows.

Call xtk_datagrid_set_columns() before adding rows.

Parameter Description
display The X display.
screen The screen number.
parent The parent window.
theme The program's configuration. The grid takes its colors from [datagrid] and the style of its parts from textview, label, button and frame. The pointer is kept and must stay valid.
clipboard The clipboard the cells use for copy and paste.
x The x position in the parent.
y The y position in the parent.
width The width in pixels.
height The height in pixels; rows that do not fit scroll.

Returns: The new data grid, or NULL when display or theme is NULL or memory runs out.

xtk_datagrid_destroy

void xtk_datagrid_destroy(XtkDataGrid *datagrid);

Destroys the grid with all its cells, labels and windows.

Parameter Description
datagrid The data grid. NULL is allowed and does nothing.

xtk_datagrid_set_theme

void xtk_datagrid_set_theme(XtkDataGrid *datagrid, const XtkConfig *theme);

Gives the grid new colors and styles, and redraws it.

The grid and its cells copy the colors they use, so a change to the XtkConfig shows only after this call, even when the pointer is the same.

Parameter Description
datagrid The data grid.
theme The configuration, usually the same pointer as before. NULL does nothing. The pointer is kept.

xtk_datagrid_set_enabled

void xtk_datagrid_set_enabled(XtkDataGrid *datagrid, int enabled);

Enables or disables the whole grid.

A disabled grid draws its cells, headers and button dimmed and takes no input: no editing, Tab, sorting, resizing, moving of columns, row selection, scrolling or Add Row. It still redraws on Expose.

Parameter Description
datagrid The data grid.
enabled 1 to enable it, 0 to disable it.

xtk_datagrid_get_enabled

int xtk_datagrid_get_enabled(const XtkDataGrid *datagrid);

Tells whether the grid is enabled.

Parameter Description
datagrid The data grid, or NULL.

Returns: 1 if it is enabled (the default), 0 if it is disabled or datagrid is NULL.

xtk_datagrid_show

void xtk_datagrid_show(XtkDataGrid *datagrid);

Maps the grid's window, and with it all its parts.

Parameter Description
datagrid The data grid.

xtk_datagrid_hide

void xtk_datagrid_hide(XtkDataGrid *datagrid);

Unmaps the grid's window, and with it all its parts.

Parameter Description
datagrid The data grid.

xtk_datagrid_get_hidden

int xtk_datagrid_get_hidden(const XtkDataGrid *datagrid);

Tells whether the grid was hidden with xtk_datagrid_hide().

Parameter Description
datagrid The data grid, or NULL.

Returns: 1 if it is hidden, 0 if not or datagrid is NULL.

xtk_datagrid_set_columns

int xtk_datagrid_set_columns(
    XtkDataGrid *datagrid,
    const char **keys,
    const char **labels,
    int count);

Sets the columns, and removes every row.

The header labels and the Add Row button (unless it was turned off with xtk_datagrid_set_add_row_button_visible()) are made again. The sort and the column widths set before are cleared, and so is the row selection. The remove-row callback is not called for the rows that go.

Parameter Description
datagrid The data grid.
keys The column keys, in display order; each is cut at 63 bytes, and a NULL entry is "". A NULL array makes every key "".
labels The header texts, in the same order; a NULL entry is "". A NULL array makes every label "".
count The number of columns. At most XTK_DATAGRID_MAX_COLUMNS are used.

Returns: 1 if all count columns were set, 0 if count was more than XTK_DATAGRID_MAX_COLUMNS (the grid then has the first XTK_DATAGRID_MAX_COLUMNS columns) or datagrid is NULL.

xtk_datagrid_clear_rows

void xtk_datagrid_clear_rows(XtkDataGrid *datagrid);

Removes every row; the columns and the header stay.

The row selection is cleared. The remove-row callback is not called.

Parameter Description
datagrid The data grid.

xtk_datagrid_add_row

int xtk_datagrid_add_row(
    XtkDataGrid *datagrid,
    long long row_id,
    const char *const *values);

Adds a row at the bottom.

Setting the text here does not call the cell-changed callback.

Parameter Description
datagrid The data grid.
row_id The program's id for the row. The grid passes it back in the callbacks and does nothing else with it; ids should be unique.
values The text of each cell, in column order, or NULL for an empty row. A NULL entry leaves that cell empty. When not NULL, the array must have an entry for every column.

Returns: 1 if the row was added; 0 if the grid already holds XTK_DATAGRID_MAX_ROWS rows, has no columns set yet, or datagrid is NULL.

xtk_datagrid_remove_row

void xtk_datagrid_remove_row(XtkDataGrid *datagrid, long long row_id);

Removes the row with an id, then calls the remove-row callback.

The grid has no control of its own for removing a row; the program calls this from its own controls. The text of the other rows and the selection are kept. Nothing happens when no row has this id.

Parameter Description
datagrid The data grid.
row_id The id of the row.

xtk_datagrid_sort_by_column

void xtk_datagrid_sort_by_column(XtkDataGrid *datagrid, int column_index, int descending);

Sorts the rows by the text of one column.

The text is compared ignoring case, not as numbers. The row ids and the selection go with their rows. The header of the column shows an arrow for the direction. A click on a header does the same thing. Nothing happens when the grid has no rows.

Parameter Description
datagrid The data grid.
column_index The column, from 0.
descending 0 for A to Z, 1 for Z to A.

xtk_datagrid_set_column_width

void xtk_datagrid_set_column_width(XtkDataGrid *datagrid, int column_index, int width);

Sets the width of a column, or lets it share the free space again.

This is what the user does by dragging the right edge of a header; use it to give the columns their first widths after xtk_datagrid_set_columns().

Parameter Description
datagrid The data grid.
column_index The column, from 0.
width The width in pixels (at least 24 is used), or 0 to let the column share the free width equally with the other unsized columns, as all columns do at first.

xtk_datagrid_swap_columns

void xtk_datagrid_swap_columns(XtkDataGrid *datagrid, int column_a, int column_b);

Swaps the places of two columns.

The headers, the text of every row and the set widths move together. This is what the user does by dragging one header onto another.

Parameter Description
datagrid The data grid.
column_a The first column, from 0.
column_b The second column, from 0.

xtk_datagrid_is_row_selected

int xtk_datagrid_is_row_selected(const XtkDataGrid *datagrid, long long row_id);

Tells whether a row is selected.

Parameter Description
datagrid The data grid, or NULL.
row_id The id of the row.

Returns: 1 if the row is selected, 0 if not or datagrid is NULL.

xtk_datagrid_get_selected_row_count

int xtk_datagrid_get_selected_row_count(const XtkDataGrid *datagrid);

Gives the number of selected rows.

Parameter Description
datagrid The data grid, or NULL.

Returns: The number of selected rows; 0 when datagrid is NULL.

xtk_datagrid_get_selected_row_at

long long xtk_datagrid_get_selected_row_at(const XtkDataGrid *datagrid, int index);

Gives the id of one selected row.

The selected rows are in the order they were selected, and they stay in that order when a row is removed, the rows are sorted or columns are swapped.

Parameter Description
datagrid The data grid, or NULL.
index Which selected row, from 0 to xtk_datagrid_get_selected_row_count() - 1.

Returns: The row's id, or 0 when index is out of range or datagrid is NULL (0 can also be a real id).

xtk_datagrid_clear_row_selection

void xtk_datagrid_clear_row_selection(XtkDataGrid *datagrid);

Deselects every row.

Parameter Description
datagrid The data grid.

xtk_datagrid_set_position

void xtk_datagrid_set_position(XtkDataGrid *datagrid, int x, int y);

Moves the grid within its parent.

Parameter Description
datagrid The data grid.
x The new x position in the parent.
y The new y position in the parent.

xtk_datagrid_resize

void xtk_datagrid_resize(XtkDataGrid *datagrid, int width, int height);

Resizes the grid and lays out its columns and rows again.

Parameter Description
datagrid The data grid.
width The new width in pixels (at least 1 is used).
height The new height in pixels (at least 1 is used).

xtk_datagrid_get_content_height

int xtk_datagrid_get_content_height(const XtkDataGrid *datagrid);

Gives the height of the whole table.

This is the height of the header, all rows and the Add Row button: what a program needs to make room for the whole table, for example in a form that grows with it.

Parameter Description
datagrid The data grid, or NULL.

Returns: The height in pixels; 0 when datagrid is NULL.

xtk_datagrid_handle_event

int xtk_datagrid_handle_event(XtkDataGrid *datagrid, XEvent *event);

Handles one X event.

Pass every event of the program's event loop; the grid passes it on to its cells, header labels and button. See the top of this page for what the mouse and the keys do.

Parameter Description
datagrid The data grid.
event The event.

Returns: 1 if the grid or one of its parts used the event, 0 if not.

xtk_datagrid_get_window

Window xtk_datagrid_get_window(const XtkDataGrid *datagrid);

Gives the grid's outer window.

A focus manager uses it to check that the grid is viewable. The keyboard focus itself is always in one of the cells; see xtk_datagrid_owns_window().

Parameter Description
datagrid The data grid, or NULL.

Returns: The outer window, or None when datagrid is NULL.

xtk_datagrid_set_focused

void xtk_datagrid_set_focused(XtkDataGrid *datagrid, int focused);

Gives the keyboard focus to the first cell.

This is for a focus manager moving the focus into the grid; a click on a cell gives it the focus by itself. It does nothing when the grid has no rows.

Parameter Description
datagrid The data grid.
focused 1 to focus the first cell (row 0, column 0). 0 does nothing: the focus leaves when another widget takes it.

xtk_datagrid_owns_window

int xtk_datagrid_owns_window(void *grid, Window window);

Tells whether a window is one of the grid's cells.

Its form fits the owns_window callback of xtk_focus_manager_register_composite() (see XtkFocusOwnsWindowCallback in xtk_focus.h): the focus of a grid is in whichever cell has it, not in one fixed window.

Parameter Description
grid The data grid, as an XtkDataGrid pointer.
window The window to test, such as the one XGetInputFocus() gives.

Returns: 1 if the window is one of the grid's cells, 0 if not or grid is NULL.

void xtk_datagrid_update_blink(XtkDataGrid *datagrid);

Blinks the text cursor of the focused cell.

Call it on the same timer that blinks the program's other text cursors (as xtk_entry_update_blink() is called).

Parameter Description
datagrid The data grid. NULL does nothing.

xtk_datagrid_set_add_row_button_visible

void xtk_datagrid_set_add_row_button_visible(XtkDataGrid *datagrid, int visible);

Shows or hides the "+ Add Row" button.

The button is made by xtk_datagrid_set_columns(), so call this before it. Hide it when the program adds rows in another way.

Parameter Description
datagrid The data grid.
visible 1 to have the button (the default), 0 not to.

xtk_datagrid_set_cell_changed_callback

void xtk_datagrid_set_cell_changed_callback(
    XtkDataGrid *datagrid,
    void(*)(void *user_data, long long row_id, const char *column_key, const char *text)callback,
    void *user_data);

Sets the function called when the user changes the text of a cell.

It is called on every edit (each key typed, a paste, an undo), not when the program sets the text.

Parameter Description
datagrid The data grid.
callback The function, or NULL for none. It receives user_data, the row's id, the column's key and the cell's whole new text.
user_data Passed to the function.

xtk_datagrid_set_add_row_callback

void xtk_datagrid_set_add_row_callback(
    XtkDataGrid *datagrid,
    void(*)(void *user_data)callback,
    void *user_data);

Sets the function called when "+ Add Row" is clicked.

The grid does not add a row itself: the program makes a record with a new id and calls xtk_datagrid_add_row().

Parameter Description
datagrid The data grid.
callback The function, or NULL for none. It receives user_data.
user_data Passed to the function.

xtk_datagrid_set_remove_row_callback

void xtk_datagrid_set_remove_row_callback(
    XtkDataGrid *datagrid,
    void(*)(void *user_data, long long row_id)callback,
    void *user_data);

Sets the function called after xtk_datagrid_remove_row() removes a row.

The program can delete its own record there.

Parameter Description
datagrid The data grid.
callback The function, or NULL for none. It receives user_data and the id of the removed row.
user_data Passed to the function.

xtk_datagrid_set_row_selected_at

void xtk_datagrid_set_row_selected_at(XtkDataGrid *datagrid, int index, int selected);

Selects or deselects a row by its place in the grid.

This lets a program show a selection made without the pointer.

Parameter Description
datagrid The data grid.
index The row's place in the grid, from 0 at the top. Nothing happens when it is out of range.
selected 1 to select the row, 0 to deselect it.