Headless: the grid lays twenty cells out in the columns its width holds, shows its scrollbar only when the rows overflow and narrows itself for it, draws only the cells on screen and says which range to prepare, scrolls one row a wheel notch, scrolls a cell into view, answers which cell is at a point, gaps included, and hands a press on a cell to its caller. A wheel callback sees Ctrl held, from the key events the context was given, and takes Ctrl+wheel to change the cell size instead of scrolling.
#define ALLEGRO_UNSTABLE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <allegro5/allegro_primitives.h>
if (!ok) {
} else {
}
}
int opt = 0;
while ((opt = getopt(argc, argv, "V:")) != -1) {
if (opt == 'V') {
} else {
return -1;
}
}
return 0;
}
static void draw_cell(
int widget_id,
size_t index,
float x,
float y,
float w,
float h,
void* user_data) {
(void)widget_id;
(void)index;
(void)user_data;
al_draw_filled_rectangle(x, y, x + w, y + h, al_map_rgb(90, 90, 90));
}
static void draw_background(
int widget_id,
float x,
float y,
float w,
float h,
void* user_data) {
(void)widget_id;
(void)user_data;
al_draw_filled_rectangle(x, y, x + w, y + h, al_map_rgb(20, 20, 20));
}
static void visible(
int widget_id,
size_t first,
size_t last,
void* user_data) {
(void)widget_id;
(void)user_data;
}
static void press(
int widget_id,
size_t index,
int button,
float x,
float y,
void* user_data) {
(void)widget_id;
(void)user_data;
}
static int wheel(
int widget_id,
int dz,
int modifiers,
void* user_data) {
(void)user_data;
if (!(modifiers & ALLEGRO_KEYMOD_CTRL)) return 0;
return 1;
}
ALLEGRO_EVENT ev;
memset(&ev, 0, sizeof(ev));
ev.type = (ALLEGRO_EVENT_TYPE)
type;
ev.keyboard.keycode = keycode;
}
ALLEGRO_EVENT ev;
memset(&ev, 0, sizeof(ev));
ev.type = (ALLEGRO_EVENT_TYPE)
type;
ev.mouse.x = (int)x;
ev.mouse.y = (int)y;
ev.mouse.dx = (
type == ALLEGRO_EVENT_MOUSE_AXES && dz == 0) ? 1 : 0;
ev.mouse.dz = dz;
ev.mouse.button = (unsigned int)button;
}
ALLEGRO_BITMAP* target = NULL;
int old = al_get_new_bitmap_flags();
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
target = al_create_bitmap(400, 300);
al_set_new_bitmap_flags(old);
al_set_target_bitmap(target);
al_clear_to_color(al_map_rgb(0, 0, 0));
al_set_target_bitmap(NULL);
al_destroy_bitmap(target);
}
int main(
int argc,
char** argv) {
size_t at = 0;
float x = 0.0f, y = 0.0f, w = 0.0f, h = 0.0f;
fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
return 2;
}
if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
return 1;
}
ALLEGRO_FONT*
font = al_create_builtin_font();
return 1;
}
memset(&cb, 0, sizeof(cb));
feed(
gui, ALLEGRO_EVENT_MOUSE_AXES, 50.0f, 50.0f, 0, -1);
feed(
gui, ALLEGRO_EVENT_MOUSE_AXES, 105.0f, 120.0f, 0, 0);
feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 105.0f, 120.0f, 1, 0);
feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 105.0f, 120.0f, 1, 0);
feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 105.0f, 120.0f, 2, 0);
feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 105.0f, 120.0f, 2, 0);
feed(
gui, ALLEGRO_EVENT_MOUSE_AXES, 245.0f, 190.0f, 0, 0);
feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 245.0f, 170.0f, 1, 0);
feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 245.0f, 170.0f, 1, 0);
expect_true(
"rows that fit hide the scrollbar, go back to the top and widen again",
memset(&cb, 0, sizeof(cb));
expect_true(
"a grid with a wheel callback is made", zoomed >= 0);
feed(
gui, ALLEGRO_EVENT_MOUSE_AXES, 50.0f, 50.0f, 0, -1);
feed_key(
gui, ALLEGRO_EVENT_KEY_DOWN, ALLEGRO_KEY_RCTRL);
feed(
gui, ALLEGRO_EVENT_MOUSE_AXES, 50.0f, 50.0f, 0, 2);
feed_key(
gui, ALLEGRO_EVENT_KEY_DOWN, ALLEGRO_KEY_LCTRL);
feed_key(
gui, ALLEGRO_EVENT_KEY_UP, ALLEGRO_KEY_RCTRL);
fprintf(stderr,
"ex_gui_grid: %d failure(s)\n",
failures);
return 1;
}
printf("ex_gui_grid: ok\n");
return 0;
}
void process_args(int argc, char **argv)
static void expect_true(const char *label, int cond)
static void feed(N_GUI_CTX *gui, int type, int x, int y, int button)
feed one mouse event at display coordinates
static N_GUI_MOUSE_EVENT last
the last event the widget was told of, and how many
static void press(int widget_id, size_t index, int button, float x, float y, void *user_data)
a press on a cell
static size_t pressed_index
static int cells_drawn
what the callbacks saw
static void draw(N_GUI_CTX *gui)
draw the whole context into a memory bitmap
static int pressed_button
static N_GUI_CTX * zoom_gui
the context the wheel callback resizes the grid in
static void visible(int widget_id, size_t first, size_t last, void *user_data)
the range to prepare
static int wheels
what the wheel callback saw, and the cell size it keeps
static void draw_background(int widget_id, float x, float y, float w, float h, void *user_data)
draw behind
static void draw_cell(int widget_id, size_t index, float x, float y, float w, float h, void *user_data)
draw one cell
static void feed_key(N_GUI_CTX *gui, int type, int keycode)
feed one key event
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
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.
double n_gui_scrollbar_get_pos(N_GUI_CTX *ctx, int widget_id)
get the current scroll position of a scrollbar widget
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
#define N_GUI_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
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_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_get_key_modifiers(const N_GUI_CTX *ctx)
the modifier keys held down now, as the GUI has seen them
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
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_INFO
informational
void(* draw_background)(int widget_id, float x, float y, float w, float h, void *user_data)
Draw behind the cells, over the whole grid, before any cell; with no cells, the only thing drawn.
void(* press)(int widget_id, size_t index, int button, float x, float y, void *user_data)
A press of button (1 or 2) on cell index, at screen x, y.
void(* draw_cell)(int widget_id, size_t index, float x, float y, float w, float h, void *user_data)
Draw cell index in the box at x, y, w by h, on screen.
int(* wheel)(int widget_id, int dz, int modifiers, void *user_data)
A wheel turn of dz notches over the grid, positive away from the user, with the modifier keys held (A...
void(* visible)(int widget_id, size_t first, size_t last, void *user_data)
Called after each draw with the cells drawn and one row beyond, first included and last excluded: wha...
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_scroll(N_GUI_CTX *ctx, int widget_id, float scroll)
Set the scroll offset, clamped to the content.
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.
void n_gui_grid_set_count(N_GUI_CTX *ctx, int widget_id, size_t count)
Set how many cells there are.
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.
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_scrollbar_showing(N_GUI_CTX *ctx, int widget_id)
Whether the scrollbar is shown, which is whether the rows overflow.
float n_gui_grid_get_scroll(N_GUI_CTX *ctx, int widget_id)
The scroll offset, in pixels from the top of the content.
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.
void n_gui_grid_set_visible(N_GUI_CTX *ctx, int widget_id, int visible)
Show or hide the grid and its scrollbar together.
int n_gui_grid_scrollbar(N_GUI_CTX *ctx, int widget_id)
The grid's scrollbar widget.
What the grid asks of its caller.
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
A virtualised grid of cells for N_GUI, scrolled vertically.