5 Module N_GUI_GRID
gullradriel edited this page 2026-10-09 00:55:18 +02:00

GUI GRID: a virtualised, vertically scrolled grid

Structures

Name
struct N_GUI_GRID_CALLBACKS
What the grid asks of its caller.

Functions

Name
void n_gui_grid_set_visible(N_GUI_CTX * ctx, int widget_id, int visible)
Show or hide the grid and its scrollbar together.
void n_gui_grid_set_scroll(N_GUI_CTX * ctx, int widget_id, float scroll)
Set the scroll offset, clamped to the content.
void n_gui_grid_set_rect(N_GUI_CTX * ctx, int widget_id, float x, float y, float w, float h, float bar_w)
Place the grid and its scrollbar in a rectangle of the window.
void n_gui_grid_set_count(N_GUI_CTX * ctx, int widget_id, size_t count)
Set how many cells there are.
void n_gui_grid_set_cell_size(N_GUI_CTX * ctx, int widget_id, float cell_w, float cell_h, float gap)
Set the size of a cell and the gap around cells.
int n_gui_grid_scrollbar_showing(N_GUI_CTX * ctx, int widget_id)
Whether the scrollbar is shown, which is whether the rows overflow.
int n_gui_grid_scrollbar(N_GUI_CTX * ctx, int widget_id)
The grid's scrollbar widget.
void n_gui_grid_scroll_to(N_GUI_CTX * ctx, int widget_id, size_t index)
Scroll the least that brings a cell's whole row into view.
int n_gui_grid_new(N_GUI_CTX * ctx, int window_id, const N_GUI_GRID_CALLBACKS * callbacks, void * user_data)
Add a grid, and its scrollbar, to a window.
int n_gui_grid_index_at(N_GUI_CTX * ctx, int widget_id, float x, float y, size_t * index)
Which cell is at a point on screen.
float n_gui_grid_get_scroll(N_GUI_CTX * ctx, int widget_id)
The scroll offset, in pixels from the top of the content.
int n_gui_grid_columns(N_GUI_CTX * ctx, int widget_id)
How many cells a row holds at the grid's width.
int n_gui_grid_cell_rect(N_GUI_CTX * ctx, int widget_id, size_t index, float * x, float * y, float * w, float * h)
Where a cell is on screen, whether or not it is scrolled into view.

Functions Documentation

function n_gui_grid_set_visible

void n_gui_grid_set_visible(
    N_GUI_CTX * ctx,
    int widget_id,
    int visible
)

Show or hide the grid and its scrollbar together.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.
  • visible non-zero to show. The scrollbar shows only when needed.

function n_gui_grid_set_scroll

void n_gui_grid_set_scroll(
    N_GUI_CTX * ctx,
    int widget_id,
    float scroll
)

Set the scroll offset, clamped to the content.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.
  • scroll pixels from the top of the content.

function n_gui_grid_set_rect

void n_gui_grid_set_rect(
    N_GUI_CTX * ctx,
    int widget_id,
    float x,
    float y,
    float w,
    float h,
    float bar_w
)

Place the grid and its scrollbar in a rectangle of the window.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.
  • x left, relative to the window.
  • y top, relative to the window.
  • w width, scrollbar included.
  • h height.
  • bar_w width of the scrollbar's strip.

The scrollbar takes a strip of bar_w on the right when, and only when, the rows do not fit; the grid has the rest. Call it again after a change of count or of cell size that may change that answer; n_gui_grid_set_count() and n_gui_grid_set_cell_size() do so themselves.

function n_gui_grid_set_count

void n_gui_grid_set_count(
    N_GUI_CTX * ctx,
    int widget_id,
    size_t count
)

Set how many cells there are.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.
  • count how many.

The view is clamped to them.

function n_gui_grid_set_cell_size

void n_gui_grid_set_cell_size(
    N_GUI_CTX * ctx,
    int widget_id,
    float cell_w,
    float cell_h,
    float gap
)

Set the size of a cell and the gap around cells.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.
  • cell_w cell width, over 0.
  • cell_h cell height, over 0.
  • gap space between cells and around them, 0 or more.

The first row and the first column are one gap in from the edge, and each row is cell_h plus a gap below the one before.

function n_gui_grid_scrollbar_showing

int n_gui_grid_scrollbar_showing(
    N_GUI_CTX * ctx,
    int widget_id
)

Whether the scrollbar is shown, which is whether the rows overflow.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.

Return: 1 when shown.

function n_gui_grid_scrollbar

int n_gui_grid_scrollbar(
    N_GUI_CTX * ctx,
    int widget_id
)

The grid's scrollbar widget.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.

Return: the scrollbar's widget id, or -1 when widget_id is not a grid.

function n_gui_grid_scroll_to

void n_gui_grid_scroll_to(
    N_GUI_CTX * ctx,
    int widget_id,
    size_t index
)

Scroll the least that brings a cell's whole row into view.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.
  • index the cell. Past the last does nothing.

function n_gui_grid_new

int n_gui_grid_new(
    N_GUI_CTX * ctx,
    int window_id,
    const N_GUI_GRID_CALLBACKS * callbacks,
    void * user_data
)

Add a grid, and its scrollbar, to a window.

Parameters:

  • ctx the GUI context. Must not be NULL.
  • window_id the window.
  • callbacks what the grid asks; copied. draw_cell must not be NULL.
  • user_data handed back to every callback.

Return: the grid's widget id, or -1 on error. Its state is freed with it.

Both start at 16 by 16 pixels; place them with n_gui_grid_set_rect().

function n_gui_grid_index_at

int n_gui_grid_index_at(
    N_GUI_CTX * ctx,
    int widget_id,
    float x,
    float y,
    size_t * index
)

Which cell is at a point on screen.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.
  • x screen x.
  • y screen y.
  • index receives the cell. Must not be NULL.

Return: TRUE when a cell is there: the point is inside the grid, which is visible, and not past the last cell. A cell owns the gap to its right and the gap below it, and the last column owns whatever is left at the right edge, so there is no dead space between cells.

function n_gui_grid_get_scroll

float n_gui_grid_get_scroll(
    N_GUI_CTX * ctx,
    int widget_id
)

The scroll offset, in pixels from the top of the content.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.

Return: the offset, 0 when widget_id is not a grid.

function n_gui_grid_columns

int n_gui_grid_columns(
    N_GUI_CTX * ctx,
    int widget_id
)

How many cells a row holds at the grid's width.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.

Return: 1 or more, or 0 when widget_id is not a grid.

function n_gui_grid_cell_rect

int n_gui_grid_cell_rect(
    N_GUI_CTX * ctx,
    int widget_id,
    size_t index,
    float * x,
    float * y,
    float * w,
    float * h
)

Where a cell is on screen, whether or not it is scrolled into view.

Parameters:

  • ctx the GUI context.
  • widget_id the grid.
  • index the cell.
  • x receives its left. May be NULL.
  • y receives its top. May be NULL.
  • w receives its width. May be NULL.
  • h receives its height. May be NULL.

Return: TRUE when the cell exists and at least part of it is inside the grid, FALSE otherwise (the rectangle is still written).