/*
 * c8ui-gallery: the bigger widgets to try out by hand: text views, a list
 * of 5000 items in rows or a grid, a hub, a pivot of small controls, a
 * flyout, progress bars and rings, the app bar (right click the list,
 * or the Menu key anywhere), pickers (numbers, stars, dates, times, a
 * calendar), layout (grid, expander, split, flip view, info bars) and a
 * tree beside a details list with column headers, and more: a search box
 * with suggestions, a menu with ticks and submenus, a strip that scrolls
 * sideways and a semantic zoom (Ctrl+wheel, Ctrl+minus, a pinch). Reader:
 * rich text in columns and linked boxes, a viewbox. Tiles & media: toggle
 * and repeat buttons, item containers, media controls, cells that span,
 * the nav bar (F2, or its button) and the settings flyout.
 */
#include <locale.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <xkbcommon/xkbcommon-keysyms.h>
#include <c8ui/c8ui.h>
#include <c8ui/c8ui-wayland.h>

#define ITEMS 5000

static const char *const icons[] = {
	"folder", "text-x-generic", "image-x-generic", "audio-x-generic",
	"video-x-generic", "application-pdf",
};
#define ICONS (int)(sizeof(icons) / sizeof(icons[0]))

static struct {
	struct c8ui_root *root;
	struct c8ui_widget *pages, *bar, *list, *status, *text;
	struct c8ui_widget *columns, *details, *tree, *stack;
	struct c8ui_widget *transport, *navbar, *step_label;
	/* Narrow: the page list or a page, with a back bar over the page */
	struct c8ui_widget *side, *right, *nav, *back_bar, *back_title, *split;
	bool narrow, in_page;
	struct c8ui_icon *icons[ICONS];
	double media_position;
	bool playing;
	int step;
} g;

/* A page's margins and big title, smaller and gone when narrow (the back
 * bar names the page then) */
static void page_pad(struct c8ui_widget *box) {
	c8ui_box_set_padding(box, 40, 30, 40, 20);
	c8ui_box_set_narrow_padding(box, 16, 8, 16, 12);
}

static void page_title(struct c8ui_widget *box, const char *text) {
	struct c8ui_widget *title = c8ui_label_new(text, C8UI_TEXT_HEADER);
	c8ui_widget_add(box, title);
	c8ui_widget_set_narrow_hidden(title, true);
}

static void say(const char *s) {
	c8ui_label_set_text(g.status, s);
}

static void draw_item(struct c8ui_widget *list, cairo_t *cr,
		const struct c8ui_list_item *item, void *data) {
	char title[32], sub[32];
	snprintf(title, sizeof(title), "Item %d", item->index + 1);
	snprintf(sub, sizeof(sub), "%d KB", (item->index * 37) % 9000);
	c8ui_list_draw_text(list, cr, item, g.icons[item->index % ICONS], title,
		sub);
}

static void opened(struct c8ui_widget *list, int index, void *data) {
	char s[64];
	snprintf(s, sizeof(s), "Opened item %d", index + 1);
	c8ui_label_set_text(g.status, s);
}

static void selected(struct c8ui_widget *list, void *data) {
	char s[64];
	snprintf(s, sizeof(s), "%d selected", c8ui_list_selected_count(list));
	c8ui_label_set_text(g.status, s);
}

static void context(struct c8ui_widget *list, int index, double x, double y,
		void *data) {
	c8ui_appbar_show(g.bar);
}

static void text_changed(struct c8ui_widget *view, void *data) {
	int line, col;
	c8ui_textview_get_position(view, &line, &col);
	char s[64];
	snprintf(s, sizeof(s), "Line %d, column %d%s", line, col,
		c8ui_textview_is_modified(view) ? " (changed)" : "");
	c8ui_label_set_text(g.status, s);
}

static void grid_toggled(struct c8ui_widget *cmd, void *data) {
	bool grid = c8ui_appbar_command_get_toggle(cmd);
	c8ui_list_set_mode(g.list, grid ? C8UI_LIST_GRID : C8UI_LIST_ROWS,
		grid ? 150 : 0, grid ? 130 : 56);
}

static void select_all(struct c8ui_widget *cmd, void *data) {
	c8ui_list_select_all(g.list, true);
}

static void clear(struct c8ui_widget *cmd, void *data) {
	c8ui_list_select_all(g.list, false);
}

static struct c8ui_widget *column(const char *what, int n) {
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 8);
	for (int i = 0; i < n; i++) {
		char s[64];
		snprintf(s, sizeof(s), "%s %d", what, i + 1);
		c8ui_widget_add(box, c8ui_label_new(s, C8UI_TEXT_ITEM));
	}
	struct c8ui_widget *scroll = c8ui_scroll_new(box);
	scroll->fill = true;
	scroll->expand = true;
	return scroll;
}

static void flyout_done(void *data) {
	c8ui_label_set_text(g.status, "The flyout closed");
}

static void renamed(struct c8ui_widget *button, void *data) {
	c8ui_label_set_text(g.status, "Renamed (not really)");
}

static void show_flyout(struct c8ui_widget *button, void *data) {
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 12);
	box->fixed_width = 320;
	c8ui_widget_add(box, c8ui_label_new("Rename", C8UI_TEXT_TITLE));
	struct c8ui_widget *entry = c8ui_entry_new(NULL, "Holiday photos", NULL,
		NULL);
	entry->fill = true;
	c8ui_widget_add(box, entry);
	c8ui_widget_add(box, c8ui_button_new("Rename", renamed, NULL));
	c8ui_flyout_show(g.root, &button->rect, box, flyout_done, NULL);
}

static void searched(struct c8ui_widget *entry, void *data) {
	char s[128];
	snprintf(s, sizeof(s), "Searched for \"%s\"", c8ui_entry_get(entry));
	c8ui_label_set_text(g.status, s);
}

static void loading_toggled(struct c8ui_widget *box, void *data) {
	c8ui_ring_set_active(data, c8ui_checkbox_get(box));
}

static struct c8ui_widget *controls_pivot(void) {
	struct c8ui_widget *pivot = c8ui_pivot_new(NULL, NULL);
	pivot->expand = true;

	struct c8ui_widget *forms = c8ui_box_new(C8UI_VERTICAL, 14);
	struct c8ui_widget *search = c8ui_entry_new(NULL, "", searched, NULL);
	c8ui_entry_set_search(search, true);
	c8ui_entry_set_placeholder(search, "Search");
	c8ui_widget_add(forms, search);
	struct c8ui_widget *ring = c8ui_ring_new(40);
	c8ui_widget_add(forms, c8ui_checkbox_new("Loading", true, loading_toggled,
		ring));
	c8ui_widget_add(forms, ring);
	static const char *const modes[] = { "Standard", "Scientific",
		"Converter" };
	c8ui_widget_add(forms, c8ui_radio_new("Mode", modes, 3, 0, NULL, NULL));
	c8ui_widget_add(forms, c8ui_button_new("Rename…", show_flyout, NULL));
	c8ui_pivot_add(pivot, "controls", c8ui_scroll_new(forms));

	/* A picture drawn here, as there are none to load */
	cairo_surface_t *pic = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
		480, 320);
	cairo_t *cr = cairo_create(pic);
	cairo_pattern_t *sky = cairo_pattern_create_linear(0, 0, 0, 320);
	cairo_pattern_add_color_stop_rgb(sky, 0, 0.1, 0.35, 0.75);
	cairo_pattern_add_color_stop_rgb(sky, 1, 0.95, 0.6, 0.3);
	cairo_set_source(cr, sky);
	cairo_paint(cr);
	cairo_pattern_destroy(sky);
	cairo_set_source_rgb(cr, 1, 0.9, 0.5);
	cairo_arc(cr, 330, 200, 50, 0, 6.3);
	cairo_fill(cr);
	cairo_set_source_rgb(cr, 0.1, 0.2, 0.15);
	cairo_move_to(cr, 0, 320);
	cairo_line_to(cr, 0, 230);
	cairo_line_to(cr, 140, 160);
	cairo_line_to(cr, 260, 250);
	cairo_line_to(cr, 480, 210);
	cairo_line_to(cr, 480, 320);
	cairo_fill(cr);
	cairo_destroy(cr);
	struct c8ui_widget *pictures = c8ui_box_new(C8UI_HORIZONTAL, 20);
	c8ui_box_set_wrap(pictures, true);
	c8ui_widget_add(pictures, c8ui_image_new(pic, 320, 0));
	struct c8ui_widget *cover = c8ui_image_new(pic, 200, 200);
	c8ui_image_set_fit(cover, C8UI_FIT_COVER);
	c8ui_widget_add(pictures, cover);
	cairo_surface_destroy(pic);
	c8ui_pivot_add(pivot, "pictures", pictures);

	c8ui_pivot_add(pivot, "a long list", column("Row", 60));
	/* Windows Phone's way: the app's name over headers that go round */
	c8ui_pivot_set_title(pivot, "c8ui gallery");
	c8ui_pivot_set_wrap(pivot, true);
	return pivot;
}

static struct c8ui_widget *hub(void) {
	struct c8ui_widget *hub = c8ui_hub_new("Music");
	hub->expand = true;
	/* A wide picture behind it all, to show it panning slower than the
	 * sections: dusk with a skyline along the bottom */
	cairo_surface_t *sky = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
		1800, 600);
	cairo_t *cr = cairo_create(sky);
	cairo_pattern_t *dusk = cairo_pattern_create_linear(0, 0, 1800, 600);
	cairo_pattern_add_color_stop_rgb(dusk, 0, 0.10, 0.05, 0.25);
	cairo_pattern_add_color_stop_rgb(dusk, 1, 0.45, 0.12, 0.20);
	cairo_set_source(cr, dusk);
	cairo_paint(cr);
	cairo_pattern_destroy(dusk);
	cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
	for (int x = 0; x < 1800; x += 70) {
		double tall = 60 + (x * 37 % 170);
		cairo_rectangle(cr, x, 600 - tall, 56, tall);
	}
	cairo_fill(cr);
	cairo_destroy(cr);
	c8ui_hub_set_background(hub, sky);
	cairo_surface_destroy(sky);
	c8ui_widget_set_scheme(hub, C8UI_SCHEME_DARK); // white text on it
	c8ui_hub_add(hub, "Now playing", column("Song", 40), 360);
	c8ui_hub_add(hub, "Albums", column("Album", 40), 360);
	c8ui_hub_add(hub, "Artists", column("Artist", 40), 360);
	c8ui_hub_add(hub, "Radio", column("Station", 6), 360);
	return hub;
}

/* Pickers */

static void number_changed(struct c8ui_widget *w, void *data) {
	char s[64];
	snprintf(s, sizeof(s), "%s: %g", (const char *)data, c8ui_number_get(w));
	say(s);
}

static void rated(struct c8ui_widget *w, void *data) {
	char s[64];
	snprintf(s, sizeof(s), "Rated %g stars", c8ui_rating_get(w));
	say(s);
}

static void date_changed(struct c8ui_widget *w, void *data) {
	int y, m, d;
	c8ui_date_get(w, &y, &m, &d);
	char s[64];
	snprintf(s, sizeof(s), "Date: %04d-%02d-%02d", y, m, d);
	say(s);
}

static void time_changed(struct c8ui_widget *w, void *data) {
	int h, m;
	c8ui_time_get(w, &h, &m);
	char s[64];
	snprintf(s, sizeof(s), "Time: %02d:%02d", h, m);
	say(s);
}

static void day_picked(struct c8ui_widget *w, void *data) {
	int y, m, d;
	c8ui_calendar_get(w, &y, &m, &d);
	char s[64];
	snprintf(s, sizeof(s), "Picked %04d-%02d-%02d", y, m, d);
	say(s);
}

/* Something on every 3rd and 10th, and on Fridays the 13th */
static bool busy_day(int year, int month, int day, void *data) {
	return day % 10 == 3 || day == 10 || day == 13;
}

static struct c8ui_widget *pickers_page(void) {
	struct c8ui_widget *row = c8ui_box_new(C8UI_HORIZONTAL, 60);
	c8ui_box_set_wrap(row, true);
	struct c8ui_widget *left = c8ui_box_new(C8UI_VERTICAL, 20);
	c8ui_widget_add(left, c8ui_number_new("Copies", 1, 99, 1, 2, 0,
		number_changed, "Copies"));
	c8ui_widget_add(left, c8ui_number_new("Margin (cm)", 0, 10, 0.25, 2.5,
		2, number_changed, "Margin"));
	c8ui_widget_add(left, c8ui_label_new("Your rating", C8UI_TEXT_BODY));
	c8ui_widget_add(left, c8ui_rating_new(5, 3, rated, NULL));
	struct c8ui_widget *average = c8ui_rating_new(5, 3.6, NULL, NULL);
	c8ui_rating_set_readonly(average, true);
	c8ui_widget_set_tooltip(average, "3.6 from 812 ratings");
	c8ui_widget_add(left, c8ui_label_new("Average (rest the pointer on it)",
		C8UI_TEXT_BODY));
	c8ui_widget_add(left, average);
	c8ui_widget_add(left, c8ui_date_new("Date", 2026, 11, 2, date_changed,
		NULL));
	struct c8ui_widget *time = c8ui_time_new("Alarm", 7, 30, time_changed,
		NULL);
	c8ui_time_set_minute_step(time, 5);
	c8ui_widget_add(left, time);
	struct c8ui_widget *time12 = c8ui_time_new("On a 12 hour clock", 19, 45,
		time_changed, NULL);
	c8ui_time_set_24h(time12, false);
	c8ui_widget_add(left, time12);
	c8ui_widget_add(row, left);
	struct c8ui_widget *cal = c8ui_calendar_new(2026, 11, 2, day_picked,
		NULL);
	c8ui_calendar_set_marks(cal, busy_day, NULL);
	c8ui_widget_add(row, cal);
	return row;
}

/* Layout */

static void page_turned(struct c8ui_widget *w, void *data) {
	char s[64];
	snprintf(s, sizeof(s), "Picture %d", c8ui_flip_get(w) + 1);
	say(s);
}

static void retry(struct c8ui_widget *w, void *data) {
	say("Trying again (not really)");
}

static void step_next(struct c8ui_widget *w, void *data) {
	int at = c8ui_stack_get(g.stack);
	c8ui_stack_set(g.stack, (at + 1) % g.stack->child_count, true);
}

static void back(struct c8ui_widget *w, void *data) {
	int at = c8ui_stack_get(g.stack);
	if (at > 0) {
		c8ui_stack_set(g.stack, at - 1, true);
	} else {
		say("Back (Alt+Left or the mouse's back button work too)");
	}
}

/* A picture to flip through: a colour and a number */
static struct c8ui_widget *slide(int i) {
	static const uint32_t colors[] = { 0x1ba1e2, 0x339933, 0xe51400,
		0xf09609, 0x8cbf26 };
	cairo_surface_t *pic = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
		600, 300);
	cairo_t *cr = cairo_create(pic);
	uint32_t c = colors[i % 5];
	cairo_set_source_rgb(cr, (c >> 16) / 255.0, ((c >> 8) & 0xff) / 255.0,
		(c & 0xff) / 255.0);
	cairo_paint(cr);
	cairo_set_source_rgb(cr, 1, 1, 1);
	cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL,
		CAIRO_FONT_WEIGHT_NORMAL);
	cairo_set_font_size(cr, 120);
	char n[16];
	snprintf(n, sizeof(n), "%d", i + 1);
	cairo_move_to(cr, 260, 200);
	cairo_show_text(cr, n);
	cairo_destroy(cr);
	struct c8ui_widget *img = c8ui_image_new(pic, 0, 0);
	c8ui_image_set_fit(img, C8UI_FIT_COVER);
	img->fill = true;
	cairo_surface_destroy(pic);
	return img;
}

static struct c8ui_widget *layout_page(void) {
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 20);

	struct c8ui_widget *offline = c8ui_infobar_new(C8UI_SEVERITY_WARNING,
		"You're offline", "Changes are kept here and sent when you're back.");
	c8ui_infobar_add_action(offline, "Try again", retry, NULL);
	c8ui_infobar_set_closable(offline, true, NULL, NULL);
	c8ui_widget_add(box, offline);
	struct c8ui_widget *saved = c8ui_infobar_new(C8UI_SEVERITY_SUCCESS, NULL,
		"Saved to Documents.");
	c8ui_infobar_set_closable(saved, true, NULL, NULL);
	c8ui_widget_add(box, saved);
	c8ui_widget_add(box, c8ui_infobar_new(C8UI_SEVERITY_ERROR,
		"Couldn't sync", NULL));

	struct c8ui_widget *flip = c8ui_flip_new(page_turned, NULL);
	flip->fixed_height = 260;
	c8ui_flip_set_dots(flip, true);
	for (int i = 0; i < 5; i++) {
		c8ui_widget_add(flip, slide(i));
	}
	c8ui_widget_add(box, flip);

	/* A wizard: a stack of steps, with a back button */
	struct c8ui_widget *row = c8ui_box_new(C8UI_HORIZONTAL, 16);
	c8ui_widget_add(row, c8ui_back_button_new(back, NULL));
	g.stack = c8ui_stack_new();
	g.stack->expand = true;
	const char *steps[] = { "Step 1: pick a folder",
		"Step 2: choose what to copy", "Step 3: done" };
	for (int i = 0; i < 3; i++) {
		c8ui_widget_add(g.stack, c8ui_label_new(steps[i],
			C8UI_TEXT_SUBHEADER));
	}
	c8ui_widget_add(row, g.stack);
	c8ui_widget_add(row, c8ui_icon_button_new("go-next", "Next", step_next,
		NULL));
	struct c8ui_widget *shuffle = c8ui_icon_button_new(
		"media-playlist-shuffle", NULL, NULL, NULL);
	c8ui_widget_set_tooltip(shuffle, "Shuffle");
	c8ui_icon_button_set_toggle(shuffle, true);
	c8ui_widget_add(row, shuffle);
	c8ui_widget_add(box, row);
	c8ui_widget_add(box, c8ui_separator_new(C8UI_HORIZONTAL));

	/* A form in a grid: labels in one column, fields in the other */
	struct c8ui_widget *more = c8ui_grid_new(2, 20, 12);
	c8ui_grid_set_column(more, 0, 120, false);
	c8ui_grid_set_column(more, 1, 0, true);
	c8ui_widget_add(more, c8ui_label_new("Name", C8UI_TEXT_BODY));
	struct c8ui_widget *name = c8ui_entry_new(NULL, "Ada", NULL, NULL);
	name->fill = true;
	c8ui_widget_add(more, name);
	c8ui_widget_add(more, c8ui_label_new("Server", C8UI_TEXT_BODY));
	struct c8ui_widget *server = c8ui_entry_new(NULL, "mail.example.org",
		NULL, NULL);
	server->fill = true;
	c8ui_widget_add(more, server);
	c8ui_widget_add(more, c8ui_label_new("Port", C8UI_TEXT_BODY));
	c8ui_widget_add(more, c8ui_number_new(NULL, 1, 65535, 1, 993, 0, NULL,
		NULL));
	c8ui_grid_add(more, c8ui_checkbox_new("Use TLS (spans both columns)",
		true, NULL, NULL), 2);
	c8ui_widget_add(box, c8ui_expander_new("Advanced settings", more, false,
		NULL, NULL));
	return box;
}

/* Tree and details */

struct file {
	const char *name, *kind, *size;
	long kb;
};

static const struct file files[] = {
	{ "Budget 2026.ods", "Spreadsheet", "48 KB", 48 },
	{ "Holiday.jpg", "Picture", "3.1 MB", 3100 },
	{ "Notes.md", "Text", "2 KB", 2 },
	{ "Talk.odp", "Presentation", "1.2 MB", 1200 },
	{ "Song.flac", "Music", "28 MB", 28000 },
	{ "Invoice 112.pdf", "PDF", "96 KB", 96 },
};
#define FILES (int)(sizeof(files) / sizeof(files[0]))
static int order[FILES];

static void draw_file(struct c8ui_widget *list, cairo_t *cr,
		const struct c8ui_list_item *item, void *data) {
	const struct file *f = &files[order[item->index]];
	c8ui_columns_draw_cell(g.columns, cr, item, 0, f->name, false);
	c8ui_columns_draw_cell(g.columns, cr, item, 1, f->kind, true);
	c8ui_columns_draw_cell(g.columns, cr, item, 2, f->size, true);
}

static int sort_column;
static bool sort_down;

static int by_column(const void *a, const void *b) {
	const struct file *x = &files[*(const int *)a];
	const struct file *y = &files[*(const int *)b];
	int r = sort_column == 2 ? (x->kb > y->kb) - (x->kb < y->kb) :
		sort_column == 1 ? strcmp(x->kind, y->kind) :
		strcmp(x->name, y->name);
	return sort_down ? -r : r;
}

static void columns_changed(struct c8ui_widget *w, void *data) {
	sort_column = c8ui_columns_get_sort(w, &sort_down);
	if (sort_column >= 0) {
		qsort(order, FILES, sizeof(int), by_column);
	}
	c8ui_list_redraw(g.details);
}

/* Folders open to three more each, made up as they open */
static void expanding(struct c8ui_widget *tree, int item, void *data) {
	static bool filled[4096];
	if (item < 4096 && filled[item]) {
		return; // done before
	}
	if (item < 4096) {
		filled[item] = true;
	}
	for (int i = 0; i < 3; i++) {
		char name[64];
		snprintf(name, sizeof(name), "%s %c", c8ui_tree_get_label(tree, item),
			'a' + i);
		int child = c8ui_tree_add(tree, item, "folder", name, NULL);
		c8ui_tree_set_has_children(tree, child, true);
	}
}

static void folder_picked(struct c8ui_widget *tree, int item, void *data) {
	char s[128];
	snprintf(s, sizeof(s), "Folder: %s", c8ui_tree_get_label(tree, item));
	say(s);
}

static struct c8ui_widget *tree_page(void) {
	g.tree = c8ui_tree_new();
	c8ui_tree_on_expand(g.tree, expanding, NULL);
	c8ui_tree_on_select(g.tree, folder_picked, NULL);
	const char *places[] = { "Documents", "Music", "Pictures", "Videos" };
	for (int i = 0; i < 4; i++) {
		int item = c8ui_tree_add(g.tree, 0, "folder", places[i], NULL);
		c8ui_tree_set_has_children(g.tree, item, true);
	}
	struct c8ui_widget *tree_scroll = c8ui_scroll_new(g.tree);

	struct c8ui_widget *right = c8ui_box_new(C8UI_VERTICAL, 0);
	c8ui_box_set_padding(right, 12, 0, 0, 0);
	g.columns = c8ui_columns_new(columns_changed, NULL);
	c8ui_columns_add(g.columns, "Name", 220);
	c8ui_columns_add(g.columns, "Type", 140);
	c8ui_columns_add(g.columns, "Size", 100);
	c8ui_columns_set_sort(g.columns, 0, false);
	c8ui_widget_add(right, g.columns);
	for (int i = 0; i < FILES; i++) {
		order[i] = i;
	}
	qsort(order, FILES, sizeof(int), by_column);
	g.details = c8ui_list_new(C8UI_LIST_ROWS, 0, 40, draw_file, NULL);
	c8ui_list_set_count(g.details, FILES);
	struct c8ui_widget *details_scroll = c8ui_scroll_new(g.details);
	details_scroll->expand = true;
	details_scroll->fill = true;
	c8ui_widget_add(right, details_scroll);

	struct c8ui_widget *split = g.split = c8ui_split_new(C8UI_HORIZONTAL,
		tree_scroll, right, 260);
	split->expand = true;
	return split;
}

/* More: suggestions, menus, sideways scrolling, semantic zoom */

static const char *const fruit[] = { "Apple", "Apricot", "Avocado",
	"Banana", "Blackberry", "Blueberry", "Cherry", "Clementine", "Coconut",
	"Cranberry", "Date", "Dragon fruit", "Fig", "Grape", "Grapefruit",
	"Guava", "Kiwi", "Lemon", "Lime", "Lychee", "Mango", "Melon",
	"Nectarine", "Orange", "Papaya", "Peach", "Pear", "Pineapple", "Plum",
	"Pomegranate", "Raspberry", "Strawberry", "Tangerine", "Watermelon" };
#define FRUIT (int)(sizeof(fruit) / sizeof(fruit[0]))

static void fruit_typed(struct c8ui_widget *entry, void *data) {
	const char *typed = c8ui_entry_get(entry);
	const char *found[FRUIT];
	int n = 0;
	size_t len = strlen(typed);
	for (int i = 0; len > 0 && i < FRUIT; i++) {
		if (strncasecmp(fruit[i], typed, len) == 0) {
			found[n++] = fruit[i];
		}
	}
	c8ui_entry_set_suggestions(entry, found, n);
}

static void fruit_searched(struct c8ui_widget *entry, void *data) {
	char s[128];
	snprintf(s, sizeof(s), "Looked for \"%s\"", c8ui_entry_get(entry));
	say(s);
}

static bool wrap_on = true;
static int sort_by = 1;

static void menu_chosen(int id, void *data) {
	char s[64];
	if (id == 10) {
		wrap_on = !wrap_on;
	} else if (id >= 20 && id < 23) {
		sort_by = id - 20;
	}
	snprintf(s, sizeof(s), id < 0 ? "Menu dismissed" : "Menu item %d", id);
	say(s);
}

static void show_menu(struct c8ui_widget *button, void *data) {
	static const struct c8ui_menu_item more[] = {
		{ .label = "Group by type", .id = 30 },
		{ .label = "Group by date", .id = 31 },
	};
	struct c8ui_menu_item sort[] = {
		{ .label = "Name", .id = 20, .kind = C8UI_MENU_RADIO,
			.checked = sort_by == 0 },
		{ .label = "Date", .id = 21, .kind = C8UI_MENU_RADIO,
			.checked = sort_by == 1 },
		{ .label = "Size", .id = 22, .kind = C8UI_MENU_RADIO,
			.checked = sort_by == 2 },
		{ .kind = C8UI_MENU_SEPARATOR },
		{ .label = "More", .submenu = more, .submenu_count = 2 },
	};
	struct c8ui_menu_item items[] = {
		{ .label = "Cut", .id = 1, .shortcut = "Ctrl+X" },
		{ .label = "Copy", .id = 2, .shortcut = "Ctrl+C" },
		{ .label = "Paste", .id = 3, .shortcut = "Ctrl+V",
			.disabled = true },
		{ .kind = C8UI_MENU_SEPARATOR },
		{ .label = "Word wrap", .id = 10, .kind = C8UI_MENU_CHECK,
			.checked = wrap_on },
		{ .label = "Sort by", .submenu = sort, .submenu_count = 5 },
	};
	c8ui_menu_show_items(g.root, button->rect.x,
		button->rect.y + button->rect.height, false, items, 6, menu_chosen,
		NULL);
}

static void draw_swatch(struct c8ui_widget *w, cairo_t *cr, void *data) {
	int i = (int)(intptr_t)data;
	uint32_t c = 0x1ba1e2 + (uint32_t)i * 0x0b1d07;
	cairo_set_source_rgb(cr, ((c >> 16) & 0xff) / 255.0,
		((c >> 8) & 0xff) / 255.0, (c & 0xff) / 255.0);
	cairo_rectangle(cr, w->rect.x, w->rect.y, w->rect.width, w->rect.height);
	cairo_fill(cr);
}

static void letter_picked(struct c8ui_widget *button, void *data) {
	c8ui_zoom_in_to(button->parent->parent, data);
}

static void zoomed(struct c8ui_widget *zoom, void *data) {
	say(c8ui_zoom_get(zoom) ? "Zoomed out: pick a letter" : "Zoomed in");
}

static struct c8ui_widget *more_page(void) {
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 16);
	box->fill = true;
	box->expand = true;
	page_pad(box);
	page_title(box, "More");

	struct c8ui_widget *row = c8ui_box_new(C8UI_HORIZONTAL, 20);
	struct c8ui_widget *search = c8ui_entry_new(NULL, "", fruit_searched,
		NULL);
	c8ui_entry_set_search(search, true);
	c8ui_entry_set_placeholder(search, "Search fruit (try \"p\")");
	c8ui_entry_on_change(search, fruit_typed, NULL);
	search->fixed_width = 320;
	c8ui_widget_add(row, search);
	c8ui_widget_add(row, c8ui_button_new("Menu…", show_menu, NULL));
	c8ui_widget_add(box, row);

	/* A strip of squares wider than the page */
	struct c8ui_widget *strip = c8ui_box_new(C8UI_HORIZONTAL, 8);
	for (int i = 0; i < 24; i++) {
		c8ui_widget_add(strip, c8ui_canvas_new(110, 90, draw_swatch, NULL,
			(void *)(intptr_t)i));
	}
	struct c8ui_widget *side = c8ui_scroll_new(strip);
	c8ui_scroll_set_directions(side, true, false);
	side->fixed_height = 100;
	side->fill = true;
	c8ui_widget_add(box, side);

	/* Semantic zoom: fruit by letter, and the letters */
	struct c8ui_widget *list = c8ui_box_new(C8UI_VERTICAL, 4);
	struct c8ui_widget *letters = c8ui_box_new(C8UI_HORIZONTAL, 8);
	c8ui_box_set_wrap(letters, true);
	c8ui_box_set_padding(letters, 0, 8, 0, 0);
	char last = 0;
	for (int i = 0; i < FRUIT; i++) {
		if (fruit[i][0] != last) {
			last = fruit[i][0];
			char l[2] = { last, 0 };
			struct c8ui_widget *header = c8ui_label_new(l,
				C8UI_TEXT_SUBHEADER);
			c8ui_label_set_color(header, C8UI_COLOR_ACCENT);
			c8ui_widget_add(list, header);
			struct c8ui_widget *b = c8ui_button_new(l, letter_picked, header);
			b->fixed_width = 64;
			b->fixed_height = 64;
			c8ui_widget_add(letters, b);
		}
		c8ui_widget_add(list, c8ui_label_new(fruit[i], C8UI_TEXT_ITEM));
	}
	struct c8ui_widget *in = c8ui_scroll_new(list);
	struct c8ui_widget *zoom = c8ui_zoom_new(in, letters, zoomed, NULL);
	c8ui_widget_add(box, zoom);
	return box;
}

/* From the Windows 8 samples: rich text in columns and linked boxes, a
 * viewbox, cells that span, item containers, toggle and repeat buttons,
 * media controls, the nav bar and the settings flyout */

static const char *const article =
	"<span size=\"xx-large\" weight=\"light\">The Metro style</span>\n"
	"<i>Content before chrome.</i> Windows 8 made text the main thing on "
	"the screen: large light headings, <b>generous margins</b> and body "
	"copy that flows <span foreground=\"#1ba1e2\">from one column into the "
	"next</span> instead of scrolling down a long page.\n\n"
	"A reader app gives the text a height and lets the width follow. "
	"Scroll sideways, with the wheel or a finger, and the columns come to "
	"you. Resize the window and the text is laid out again without any "
	"of it being lost between the columns.\n\n"
	"The same text can be poured into boxes of your own: the first takes "
	"as many lines as it has room for and hands the rest on. That is how "
	"a magazine layout puts a picture or a pull quote in the middle of an "
	"article. <b>Bold</b>, <i>italic</i>, <u>underline</u> and "
	"<span size=\"large\">larger</span> text all work through Pango "
	"markup.\n\n"
	"Heading three\nMore words to make the third column worth having, and "
	"a fourth: windows were once a thing you moved around; now they are "
	"the thing you read through.";

static struct c8ui_widget *reader_page(void) {
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 16);
	box->fill = true;
	box->expand = true;
	page_pad(box);
	page_title(box, "Reader");

	/* Columns across */
	struct c8ui_widget *cols = c8ui_rich_columns_new(article, 280, 40);
	cols->fixed_height = 260;
	struct c8ui_widget *side = c8ui_scroll_new(cols);
	c8ui_scroll_set_directions(side, true, false);
	side->fixed_height = 270;
	side->fill = true;
	c8ui_widget_add(box, side);

	/* The same text through three boxes of our own */
	c8ui_widget_add(box, c8ui_label_new("Linked boxes", C8UI_TEXT_TITLE));
	struct c8ui_widget *row = c8ui_box_new(C8UI_HORIZONTAL, 20);
	c8ui_box_set_wrap(row, true); // fewer across when narrow
	struct c8ui_widget *first = c8ui_rich_new(article);
	struct c8ui_widget *second = c8ui_rich_overflow_new();
	struct c8ui_widget *third = c8ui_rich_overflow_new();
	c8ui_rich_set_overflow(first, second);
	c8ui_rich_set_overflow(second, third);
	c8ui_rich_set_font(first, NULL, 13);
	c8ui_rich_set_justify(first, true);
	struct c8ui_widget *boxes[] = { first, second, third };
	for (int i = 0; i < 3; i++) {
		boxes[i]->fixed_width = 220;
		boxes[i]->fixed_height = 130;
		c8ui_widget_add(row, boxes[i]);
	}
	c8ui_widget_add(box, row);

	/* A scaled picture of a widget */
	c8ui_widget_add(box, c8ui_label_new("Viewbox", C8UI_TEXT_TITLE));
	struct c8ui_widget *inner = c8ui_box_new(C8UI_VERTICAL, 8);
	c8ui_box_set_padding(inner, 20, 20, 20, 20);
	inner->has_background = true;
	inner->background = 0x1ba1e2;
	c8ui_widget_add(inner, c8ui_label_new("800 px of heading", C8UI_TEXT_HEADER));
	c8ui_widget_add(inner, c8ui_button_new("Not clickable", NULL, NULL));
	inner->fixed_width = 500;
	struct c8ui_widget *view = c8ui_viewbox_new(inner);
	view->fixed_width = 250;
	view->align = C8UI_ALIGN_START;
	c8ui_widget_add(box, view);
	return box;
}

static void span_of(struct c8ui_widget *list, int i, int *cols, int *rows,
		void *data) {
	/* Start's sizes: mostly small, some wide, a few big */
	if (i % 7 == 0) {
		*cols = *rows = 2;
	} else if (i % 3 == 0) {
		*cols = 2;
	}
}

static void draw_span(struct c8ui_widget *list, cairo_t *cr,
		const struct c8ui_list_item *item, void *data) {
	uint32_t c = 0x1ba1e2 + (uint32_t)item->index * 0x0b1d07;
	cairo_set_source_rgb(cr, ((c >> 16) & 0xff) / 255.0,
		((c >> 8) & 0xff) / 255.0, (c & 0xff) / 255.0);
	struct c8ui_rect r = item->rect;
	cairo_rectangle(cr, r.x, r.y, r.width, r.height);
	cairo_fill(cr);
	char text[16];
	snprintf(text, sizeof(text), "%d", item->index);
	cairo_set_source_rgb(cr, 1, 1, 1);
	c8ui_text_draw(cr, NULL, c8ui_text_font(C8UI_TEXT_BODY_STRONG), text,
		r.x + 8, r.y + r.height - 28, 0, 1);
}

static void transport_acted(struct c8ui_widget *t,
		enum c8ui_transport_action action, double value, void *data) {
	char text[64];
	switch (action) {
	case C8UI_TRANSPORT_PLAY:
	case C8UI_TRANSPORT_PAUSE:
		g.playing = action == C8UI_TRANSPORT_PLAY;
		c8ui_transport_set_playing(t, g.playing);
		say(action == C8UI_TRANSPORT_PLAY ? "Play" : "Pause");
		break;
	case C8UI_TRANSPORT_SEEK:
		g.media_position = value;
		c8ui_transport_set_position(t, value);
		snprintf(text, sizeof(text), "Seek to %.0f s%s", value,
			c8ui_transport_is_live(t) ? " (dragging)" : "");
		say(text);
		break;
	case C8UI_TRANSPORT_VOLUME:
		c8ui_transport_set_volume(t, value, false);
		snprintf(text, sizeof(text), "Volume %.0f%%", value * 100);
		say(text);
		break;
	case C8UI_TRANSPORT_MUTE:
		c8ui_transport_set_volume(t, 0.5, value > 0);
		say(value > 0 ? "Muted" : "Unmuted");
		break;
	case C8UI_TRANSPORT_FULLSCREEN:
		c8ui_transport_set_fullscreen(t, value > 0);
		say(value > 0 ? "Full screen" : "Left full screen");
		break;
	case C8UI_TRANSPORT_PREVIOUS:
		g.media_position = 0;
		c8ui_transport_set_position(t, 0);
		break;
	case C8UI_TRANSPORT_NEXT:
		say("Next track");
		break;
	}
}

static int media_tick(void *data) {
	if (g.playing && !c8ui_transport_is_live(g.transport)) {
		g.media_position += 0.25;
		if (g.media_position > 215) {
			g.media_position = 0;
		}
		c8ui_transport_set_position(g.transport, g.media_position);
	}
	return 250;
}

static void stepped(struct c8ui_widget *button, void *data) {
	char text[32];
	g.step += (int)(intptr_t)data;
	snprintf(text, sizeof(text), "Held down: %d", g.step);
	c8ui_label_set_text(g.step_label, text);
}

static void flipped(struct c8ui_widget *button, void *data) {
	say(c8ui_togglebutton_get(button) ? "Bold on" : "Bold off");
}

static void item_invoked(struct c8ui_widget *item, void *data) {
	say("Invoked");
}

static void item_selected(struct c8ui_widget *item, void *data) {
	say(c8ui_item_container_get_selected(item) ? "Selected" : "Unselected");
}

static void nav_chosen(struct c8ui_widget *bar, int index, void *data) {
	char text[48];
	snprintf(text, sizeof(text), "Nav bar: place %d", index + 1);
	say(text);
}

static void show_navbar(struct c8ui_widget *button, void *data) {
	c8ui_navbar_toggle(g.navbar);
}

static void flyout_closed(void *data) {
	say("Settings flyout closed");
}

static void show_settings_flyout(struct c8ui_widget *button, void *data) {
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 20);
	c8ui_widget_add(box, c8ui_toggle_new("Sync settings", NULL, true, NULL,
		NULL));
	c8ui_widget_add(box, c8ui_slider_new("Text size", 10, 30, 1, 15, NULL,
		NULL));
	c8ui_widget_add(box, c8ui_checkbox_new("Open links in a new window", false,
		NULL, NULL));
	for (int i = 0; i < 12; i++) {
		c8ui_widget_add(box, c8ui_label_new("More settings…", C8UI_TEXT_BODY));
	}
	c8ui_settings_flyout_show(g.root, "Options", box, (intptr_t)data != 0,
		flyout_closed, NULL);
}

static struct c8ui_widget *tiles_page(void) {
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 20);
	box->fill = true;
	box->expand = true;
	page_pad(box);
	page_title(box, "Tiles & media");

	struct c8ui_widget *buttons = c8ui_box_new(C8UI_HORIZONTAL, 12);
	c8ui_widget_add(buttons, c8ui_togglebutton_new("Bold", false, flipped,
		NULL));
	c8ui_widget_add(buttons, c8ui_repeat_button_new("−", stepped,
		(void *)(intptr_t)-1));
	c8ui_widget_add(buttons, c8ui_repeat_button_new("+", stepped,
		(void *)(intptr_t)1));
	g.step_label = c8ui_label_new("Hold + or −", C8UI_TEXT_BODY);
	c8ui_widget_add(buttons, g.step_label);
	c8ui_widget_add(buttons, c8ui_button_new("Nav bar", show_navbar, NULL));
	c8ui_widget_add(buttons, c8ui_button_new("Settings", show_settings_flyout,
		(void *)(intptr_t)0));
	c8ui_widget_add(buttons, c8ui_button_new("Wide settings",
		show_settings_flyout, (void *)(intptr_t)1));
	c8ui_box_set_wrap(buttons, true);
	c8ui_widget_add(box, buttons);

	/* Item containers around anything */
	struct c8ui_widget *items = c8ui_box_new(C8UI_HORIZONTAL, 12);
	for (int i = 0; i < 3; i++) {
		struct c8ui_widget *inner = c8ui_box_new(C8UI_VERTICAL, 4);
		c8ui_box_set_padding(inner, 12, 12, 12, 12);
		char title[32];
		snprintf(title, sizeof(title), "Item %d", i + 1);
		c8ui_widget_add(inner, c8ui_label_new(title, C8UI_TEXT_TITLE));
		c8ui_widget_add(inner, c8ui_label_new(i == 1 ? "A tap only invokes" :
			"A tap selects", C8UI_TEXT_CAPTION));
		struct c8ui_widget *item = c8ui_item_container_new(inner);
		item->fixed_width = 170;
		if (i == 1) {
			c8ui_item_container_set_tap(item, C8UI_ITEM_TAP_INVOKE);
		}
		c8ui_item_container_on_invoke(item, item_invoked, NULL);
		c8ui_item_container_on_select(item, item_selected, NULL);
		c8ui_widget_add(items, item);
	}
	c8ui_widget_add(box, items);

	/* Media transport */
	g.transport = c8ui_transport_new(transport_acted, NULL);
	c8ui_transport_set_duration(g.transport, 215);
	c8ui_transport_set_buffered(g.transport, 0.6);
	c8ui_transport_set_buttons(g.transport, true, true);
	c8ui_root_timer_add(g.root, 250, media_tick, NULL);
	c8ui_widget_add(box, g.transport);

	/* Cells that span */
	struct c8ui_widget *spans = c8ui_list_new(C8UI_LIST_SPANS, 100, 100,
		draw_span, NULL);
	c8ui_list_set_spans(spans, span_of, NULL);
	c8ui_list_set_count(spans, 40);
	c8ui_list_set_multiple(spans, true);
	struct c8ui_widget *scroll = c8ui_scroll_new(spans);
	scroll->fill = true;
	scroll->expand = true;
	c8ui_widget_add(box, scroll);
	return box;
}

static struct c8ui_widget *page(const char *title, struct c8ui_widget *body) {
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 16);
	box->fill = true;
	box->expand = true;
	page_pad(box);
	page_title(box, title);
	struct c8ui_widget *scroll = c8ui_scroll_new(body);
	scroll->fill = true;
	scroll->expand = true;
	c8ui_widget_add(box, scroll);
	return box;
}

static const char *const page_names[] = {
	"Text", "List", "Hub", "Pivot", "Progress", "Pickers", "Layout", "Tree",
	"More", "Reader", "Tiles & media",
};
#define PAGES (int)(sizeof(page_names) / sizeof(page_names[0]))

/* Wide: the list beside the page. Narrow: one or the other filling the
 * window, like a phone app, the page under a back bar. Only which pane
 * shows and a few sizes are this one's: paddings are the boxes' own
 * (narrow padding) and the split stacks itself. (An app would use
 * c8ui_pages, which does all of this; the gallery shows the parts.) */
static void arrange(void) {
	bool list = g.narrow && !g.in_page;
	g.side->fixed_width = g.narrow ? 0 : 240;
	g.side->expand = g.narrow;
	c8ui_nav_set_menu(g.nav, g.narrow);
	c8ui_widget_set_hidden(g.side, g.narrow && g.in_page);
	c8ui_widget_set_hidden(g.right, list);
	c8ui_widget_set_hidden(g.back_bar, !g.narrow);
	/* The folders over the files there (the split's own doing) */
	c8ui_split_set_position(g.split, g.narrow ? 220 : 260);
	c8ui_columns_set_width(g.columns, 0, g.narrow ? 170 : 220);
	c8ui_columns_set_width(g.columns, 1, g.narrow ? 120 : 140);
	c8ui_label_set_text(g.back_title, page_names[c8ui_nav_get(g.nav)]);
	if (list) {
		c8ui_widget_grab_focus(g.nav);
	}
	c8ui_widget_queue_layout(g.side->parent);
}

static void nav_changed(struct c8ui_widget *nav, void *data) {
	int at = c8ui_nav_get(nav);
	for (int i = 0; i < g.pages->child_count; i++) {
		c8ui_widget_set_hidden(g.pages->children[i], i != at);
	}
	g.in_page = true;
	arrange();
	c8ui_widget_enter(g.pages->children[at]);
}

static void go_back(struct c8ui_widget *w, void *data) {
	if (!g.narrow || !g.in_page) {
		return;
	}
	g.in_page = false;
	arrange();
	c8ui_widget_enter(g.side);
}

static void resized(struct c8ui_root *root, double width, double height,
		void *data) {
	bool narrow = c8ui_root_is_narrow(root);
	if (narrow != g.narrow) {
		g.narrow = narrow;
		arrange();
	}
}

static bool keys(const struct c8ui_event *ev, void *data) {
	if (ev->sym == XKB_KEY_F2 && g.navbar) {
		c8ui_navbar_toggle(g.navbar);
		return true;
	}
	if (ev->sym == XKB_KEY_Menu) {
		c8ui_appbar_toggle(g.bar);
		return true;
	}
	if (ev->sym == XKB_KEY_Escape && g.narrow && g.in_page) {
		go_back(NULL, NULL);
		return true;
	}
	return false;
}

int main(void) {
	/* Dates and the first day of the week as the user's language has
	 * them; numbers stay C's, as c8-lua keeps them, so both galleries
	 * show the same */
	setlocale(LC_ALL, "");
	setlocale(LC_NUMERIC, "C");
	struct c8ui_app *app = c8ui_app_new("org.cont8nuum.Gallery");
	if (app == NULL) {
		fprintf(stderr, "c8ui-gallery: no Wayland compositor\n");
		return 1;
	}
	struct c8ui_window *win = c8ui_window_new(app, "Gallery", 1000, 700);
	g.root = c8ui_window_root(win);
	c8ui_root_set_key_handler(g.root, keys, NULL);
	for (int i = 0; i < ICONS; i++) {
		g.icons[i] = c8ui_icon_load(NULL, icons[i]);
	}

	struct c8ui_widget *main_box = c8ui_box_new(C8UI_HORIZONTAL, 0);
	main_box->fill = true;
	struct c8ui_widget *side = g.side = c8ui_box_new(C8UI_VERTICAL, 0);
	side->fixed_width = 240;
	side->fill = true;
	c8ui_widget_set_background(side, 0xf0f0f0);
	c8ui_box_set_padding(side, 0, 40, 0, 0);
	c8ui_box_set_narrow_padding(side, 0, 24, 0, 0);
	struct c8ui_widget *gallery = c8ui_box_new(C8UI_VERTICAL, 0);
	c8ui_box_set_padding(gallery, 20, 0, 20, 16);
	c8ui_widget_add(gallery, c8ui_label_new("Gallery", C8UI_TEXT_HEADER));
	struct c8ui_widget *nav = g.nav = c8ui_nav_new(nav_changed, NULL);
	for (int i = 0; i < PAGES; i++) {
		c8ui_nav_add(nav, page_names[i]);
	}
	c8ui_widget_add(side, gallery);
	c8ui_widget_add(side, nav);
	c8ui_widget_add(main_box, side);

	struct c8ui_widget *right = g.right = c8ui_box_new(C8UI_VERTICAL, 0);
	right->expand = true;
	right->fill = true;
	g.back_bar = c8ui_box_new(C8UI_HORIZONTAL, 12);
	c8ui_box_set_padding(g.back_bar, 16, 20, 16, 8);
	struct c8ui_widget *back_button = c8ui_back_button_new(go_back, NULL);
	back_button->align = C8UI_ALIGN_CENTER;
	c8ui_widget_add(g.back_bar, back_button);
	g.back_title = c8ui_label_new("", C8UI_TEXT_SUBHEADER);
	g.back_title->align = C8UI_ALIGN_CENTER;
	c8ui_widget_add(g.back_bar, g.back_title);
	c8ui_widget_set_hidden(g.back_bar, true);
	c8ui_widget_add(right, g.back_bar);
	g.pages = c8ui_box_new(C8UI_VERTICAL, 0);
	g.pages->expand = true;
	g.pages->fill = true;
	c8ui_widget_add(right, g.pages);
	g.status = c8ui_label_new("Right click the list, or press the Menu key, for the app bar",
		C8UI_TEXT_CAPTION);
	c8ui_label_set_color(g.status, C8UI_COLOR_SECONDARY);
	struct c8ui_widget *status_box = c8ui_box_new(C8UI_VERTICAL, 0);
	c8ui_box_set_padding(status_box, 40, 4, 40, 8);
	c8ui_box_set_narrow_padding(status_box, 16, 4, 16, 8);
	c8ui_widget_add(status_box, g.status);
	c8ui_widget_add(right, status_box);
	c8ui_widget_add(main_box, right);

	/* Text */
	struct c8ui_widget *text_box = c8ui_box_new(C8UI_VERTICAL, 16);
	struct c8ui_widget *field = c8ui_textview_new("");
	c8ui_textview_set_framed(field, true);
	c8ui_textview_set_accepts_tab(field, false);
	c8ui_textview_set_placeholder(field, "A framed one, for a form");
	field->fixed_height = 96;
	c8ui_widget_add(text_box, field);
	g.text = c8ui_textview_new(
		"Type here. Ctrl+Z undoes, Ctrl+Y or Ctrl+Shift+Z redoes.\n\n"
		"Double click selects a word, three clicks a line, and dragging "
		"after either goes on by words or lines. Up and Down keep to the "
		"column they started in; Home goes to the indent, then the start.\n"
		"\tTabs type a tab here; in the box above Tab moves on.");
	c8ui_textview_on_change(g.text, text_changed, NULL);
	c8ui_widget_add(text_box, g.text);
	c8ui_widget_add(g.pages, page("Text", text_box));

	/* List */
	g.list = c8ui_list_new(C8UI_LIST_ROWS, 0, 56, draw_item, NULL);
	c8ui_list_set_multiple(g.list, true);
	c8ui_list_set_count(g.list, ITEMS);
	c8ui_list_on_open(g.list, opened, NULL);
	c8ui_list_on_select(g.list, selected, NULL);
	c8ui_list_on_context(g.list, context, NULL);
	c8ui_widget_add(g.pages, page("List", g.list));

	/* Hub: edge to edge, its own title */
	c8ui_widget_add(g.pages, hub());

	/* Pivot */
	struct c8ui_widget *pivot_page = c8ui_box_new(C8UI_VERTICAL, 0);
	pivot_page->fill = true;
	pivot_page->expand = true;
	page_pad(pivot_page);
	c8ui_widget_add(pivot_page, controls_pivot());
	c8ui_widget_add(g.pages, pivot_page);

	/* Progress */
	struct c8ui_widget *progress = c8ui_box_new(C8UI_VERTICAL, 24);
	c8ui_widget_add(progress, c8ui_label_new("Copying 3 items",
		C8UI_TEXT_BODY));
	c8ui_widget_add(progress, c8ui_progress_new(0.35));
	c8ui_widget_add(progress, c8ui_label_new("Looking for updates",
		C8UI_TEXT_BODY));
	c8ui_widget_add(progress, c8ui_progress_new(-1));
	c8ui_widget_add(g.pages, page("Progress", progress));

	c8ui_widget_add(g.pages, page("Pickers", pickers_page()));
	c8ui_widget_add(g.pages, page("Layout", layout_page()));

	/* Tree: the split needs the page's height, so no scroll around it */
	struct c8ui_widget *tree_box = c8ui_box_new(C8UI_VERTICAL, 16);
	tree_box->fill = true;
	tree_box->expand = true;
	page_pad(tree_box);
	page_title(tree_box, "Tree");
	c8ui_widget_add(tree_box, tree_page());
	c8ui_widget_add(g.pages, tree_box);
	c8ui_widget_add(g.pages, more_page());
	c8ui_widget_add(g.pages, reader_page());
	c8ui_widget_add(g.pages, tiles_page());

	for (int i = 1; i < g.pages->child_count; i++) {
		c8ui_widget_set_hidden(g.pages->children[i], true);
	}

	g.bar = c8ui_appbar_new(g.root);
	c8ui_appbar_add(g.bar, "edit-select-all", "Select all", false,
		select_all, NULL);
	c8ui_appbar_add(g.bar, "edit-clear", "Clear selection", false, clear,
		NULL);
	struct c8ui_widget *grid = c8ui_appbar_add(g.bar, "view-grid", "Grid",
		true, grid_toggled, NULL);
	c8ui_appbar_command_set_toggle(grid, false);

	g.navbar = c8ui_navbar_new(g.root, nav_chosen, NULL);
	const char *places[] = { "Home", "Photos", "Music", "Videos", "Documents",
		"Downloads", "Settings", "Help", "Store", "People", "Mail", "Maps" };
	for (int i = 0; i < 12; i++) {
		c8ui_navbar_add(g.navbar, NULL, places[i]);
	}
	c8ui_navbar_set_current(g.navbar, 0);

	c8ui_root_set_child(g.root, main_box);
	c8ui_root_on_resize(g.root, resized, NULL);
	c8ui_widget_grab_focus(nav); // the arrows go through the pages
	int status = c8ui_app_run(app);
	for (int i = 0; i < ICONS; i++) {
		c8ui_icon_destroy(g.icons[i]);
	}
	c8ui_app_destroy(app);
	return status;
}
