Datagrid keyboard navigation (headless).
Datagrid keyboard navigation (headless). Listbox, radiolist, combobox, dropmenu, slider and checkbox could all be driven from the keyboard; the datagrid could not, so a grid was mouse-only and every host that wanted keys had to bolt its own handler onto the event loop.
The datagrid is now focusable (Tab, and a click) and takes up/down, page up/down, home/end to move the selected row, firing on_select exactly as a click does and scrolling the new row into view. Shift extends a range when multiselect is on, and left/right scroll the columns when they overflow the pane.
#define ALLEGRO_UNSTABLE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
if (!cond) {
} else {
}
}
static void on_select(
int widget_id,
int row,
void* user_data) {
(void)widget_id;
(void)user_data;
}
static ALLEGRO_EVENT
key_event(
int keycode,
unsigned int modifiers) {
ALLEGRO_EVENT ev;
memset(&ev, 0, sizeof(ev));
ev.type = ALLEGRO_EVENT_KEY_CHAR;
ev.keyboard.keycode = keycode;
ev.keyboard.modifiers = modifiers;
return ev;
}
ALLEGRO_EVENT ev;
memset(&ev, 0, sizeof(ev));
ev.type = (
unsigned int)
type;
ev.mouse.x = x;
ev.mouse.y = y;
ev.mouse.button = 1;
return ev;
}
int win, grid, i;
int rows[8];
size_t nsel;
if (!al_init() || !al_init_font_addon()) {
return 1;
}
font = al_create_builtin_font();
return 1;
}
return 1;
}
for (i = 0; i < 20; i++) {
char a[16], b[16];
snprintf(a, sizeof(a), "r%02d", i);
snprintf(b, sizeof(b), "%d", i);
}
expect_true(
"a click focuses the grid",
gui->focused_widget_id == grid);
expect_true(
"shift+down extends the range", nsel == 3 && rows[0] == 0 && rows[2] == 2);
expect_true(
"a plain arrow collapses it", nsel == 1 && rows[0] == 3);
return 1;
}
return 0;
}
static void expect_true(const char *label, int cond)
static ALLEGRO_EVENT mouse_event(int type, int x, int y, int dz)
Build a left-button mouse event at (x, y).
static void on_select(int widget_id, int row, void *user_data)
Record a row selection, to prove the keyboard path fires the same callback a click does.
static int selected_seen[64]
Rows reported by the grid's on_select callback, in order.
static int nb_selected_seen
How many entries of selected_seen are used.
static ALLEGRO_EVENT key_event(int keycode, unsigned int modifiers)
Build a key event for keycode with modifiers.
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
static const char * row(N_GUI_TREE *tree, int index)
the text of a visible row, "" past the end
FONT * font
the default 8x8 font, valid after allegro_init()
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
set the display (viewport) size so global scrollbars can be computed.
void n_gui_datagrid_set_selected(N_GUI_CTX *ctx, int widget_id, int row)
set the selected row index (fires the on_select callback).
size_t n_gui_datagrid_get_selected_rows(N_GUI_CTX *ctx, int widget_id, int *out, size_t max)
fill out with the indices of the selected rows (ascending), up to max, and return the total number of...
int n_gui_datagrid_get_scroll_offset(N_GUI_CTX *ctx, int widget_id)
first visible data row (the vertical scroll position), or 0.
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
int n_gui_datagrid_add_column(N_GUI_CTX *ctx, int widget_id, const char *title, float width)
append a column with a header title and pixel width; returns the column index or -1
void n_gui_datagrid_set_multiselect(N_GUI_CTX *ctx, int widget_id, int enabled)
enable (1) or disable (0) multi-row selection.
#define N_GUI_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
void n_gui_datagrid_clear_rows(N_GUI_CTX *ctx, int widget_id)
remove all rows from a data grid (columns are kept)
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
int n_gui_datagrid_add_row(N_GUI_CTX *ctx, int widget_id, const char **values)
append a row of cell strings (values must have one entry per column); returns the row index or -1
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
int n_gui_datagrid_get_selected(N_GUI_CTX *ctx, int widget_id)
get the selected row index, or -1 if none
int n_gui_add_datagrid(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, void(*on_select)(int, int, void *), void *user_data)
Add a sortable data grid widget.
The top-level GUI context that holds all windows.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_NOTICE
normal but significant condition
#define LOG_INFO
informational
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.