Lists and tables#

A list shows many rows in a fixed box and lets the user pick one. Only the visible rows are built, so a list of 500 rows costs the same per frame as a list of 10.

A list#

static ui_list_t list;

ui_list_begin(&list, count, 14, UI_FILL);
for (int i; ui_list_next(&list, &i);) ui_label(names[i]);
int picked = ui_list_end(&list);
if (picked >= 0) open(picked);

Argument

Meaning

list

Your ui_list_t, which holds selected (the selected row) and top (the first visible row). Keep it in a static or global variable

count

The number of rows

row_h

The height of every row in pixels

h

The height of the list in pixels, or UI_FILL for the rest of the container

ui_list_next sets i to the index of the next visible row and returns true, or returns false after the last visible row. Everything you call between two ui_list_next calls goes into that row, stacked from the top of the row like in any container. ui_list_end closes the list and returns the index of the row the user activated with [enter] or [2nd] in this frame, or -1.

Rows are drawn in ui_style_row: the selected row in its focus colors when the list has focus, and in its UI_ON colors when it does not. Text in a row takes its colors from the row.

The showcase home list with a title and a caption in each row

The showcase home list: rows 28 pixels high, each with a label and a caption.#

Keys#

The whole list is one focus stop.

Key

Effect

[up], [down]

Move the selection. At the first or last row, the key leaves the list

[left], [right]

Page up and down by one screen of rows

[enter], [2nd]

Activate the selected row: ui_list_end returns its index

The list scrolls to keep the selection visible and shows a scroll bar when there are more rows than fit.

Reading and setting the selection#

list.selected is the selected row at any time, not only in the frame it was activated. You can set it to move the selection; the list keeps it between 0 and count - 1 and scrolls to it. When rows are deleted, the list clamps the selection on the next frame.

int action = ui_menu(&menu_open, actions, 3);
if (action == ACTION_DELETE) delete_file(list.selected);

Tables#

ui_columns called right after ui_list_begin applies to every row. Together with ui_header above the list, that makes a table:

static const char *const headings[] = {"Name", "Type", "Size"};
static const int widths[] = {UI_FILL, 64, 56};

ui_header(headings, widths, 3);
ui_list_begin(&list, count, 14, UI_FILL);
ui_columns(3, widths);
for (int i; ui_list_next(&list, &i);) {
  ui_label(files[i].name);
  ui_label(type_names[files[i].type]);
  ui_next_style(&style_number);
  ui_label(ui_fmt("%u", files[i].size));
}
if (ui_list_end(&list) >= 0) menu_open = true;

ui_header(labels, widths, count) draws one label per column in ui_style_header, with the same widths as ui_columns. The header has no scroll bar and the list does when it scrolls, so a fill column in the header is a few pixels wider than in the rows; right aligned cells that should line up with a heading are best given a fixed width.

A table of 500 files with Name, Type and Size columns

A table of 500 files. The size column uses a right aligned monospace style.#

Widgets in rows#

Rows are not focus stops of their own, and the widgets in them do not take focus. Use labels, captions, images and progress bars in rows, and act on the selected row with [enter], a menu or the softkeys.

Lists and menus#

A common pattern is to open a menu for the selected row. ui_menu opens next to the focused widget, and for a list that is the selected row:

if (ui_list_end(&list) >= 0) menu_open = true;
int action = ui_menu(&menu_open, action_names, 3);
A menu opened under the selected row of the files table

[enter] on a row opens a menu next to it.#