| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359 |
- /* gallery: an interactive showcase of every widget and primitive in ngui.
- *
- * One scrolling page holds a section per part of the toolkit: typography,
- * the drawing primitives, the theme palette, every procedural icon, buttons,
- * text fields, the scroll pane and the ngui_cut_* layout helpers. The
- * controls are live, so all the input paths the toolkit offers are exercised.
- * ESC closes the window after a focused field has cancelled its edit. */
- #include "ucmd_lib.h"
- #include "ngui.h"
- #define START_W 860
- #define START_H 600
- #define HEADER_H 64
- #define NAV_H 35
- #define STATUS_H 24
- #define SEC_HEAD_H 34
- #define SEC_GAP 24
- #define PAGE_BOTTOM 24
- #define PAGE_PAD 12
- #define CARD_PAD 16
- #define TYPO_H 220
- #define SHAPES_H 154
- #define THEME_H 140
- #define ICONS_H 100
- #define BUTTONS_H 140
- #define INPUTS_H 250
- #define SCROLL_H 220
- #define LAYOUT_H 190
- #define CURSORS_H 230
- #define LIST_ROWS 12
- #define LIST_ROW_H 24
- enum section {
- SEC_TEXT,
- SEC_SHAPES,
- SEC_THEME,
- SEC_ICONS,
- SEC_BUTTONS,
- SEC_INPUTS,
- SEC_SCROLL,
- SEC_LAYOUT,
- SEC_CURSORS,
- SEC_COUNT
- };
- enum {
- ID_HEADER_HOME = 1,
- ID_HEADER_PAGE_UP,
- ID_HEADER_RESET,
- ID_BTN_CLICK,
- ID_BTN_RESET,
- ID_ICON_PLUS,
- ID_ICON_REFRESH,
- ID_ICON_PREV,
- ID_ICON_NEXT,
- ID_IN_NAME,
- ID_IN_EMAIL,
- ID_IN_EVENTS,
- ID_IN_DISABLED,
- ID_BTN_RESET_FIELDS,
- ID_NAV_BASE = 0x100,
- ID_ICON_BASE = 0x200
- };
- static const int sec_card_h[SEC_COUNT] = {
- TYPO_H, SHAPES_H, THEME_H, ICONS_H,
- BUTTONS_H, INPUTS_H, SCROLL_H, LAYOUT_H, CURSORS_H
- };
- static const char *const sec_titles[SEC_COUNT] = {
- "Text", "Shapes", "Theme", "Icons",
- "Buttons", "Inputs", "Scroll", "Layout", "Cursors"
- };
- static const char *const sec_descs[SEC_COUNT] = {
- "styles, truncation and measurement",
- "rectangles, frames, lines and clipping",
- "the ngui_theme_dark palette",
- "every Adwaita symbolic icon",
- "buttons and icon buttons",
- "single-line text fields",
- "a nested draggable scroll pane",
- "ngui_cut_* layout helpers",
- "every mouse cursor shape"
- };
- static const char *const color_names[15] = {
- "bg", "panel", "panel_alt", "border", "text",
- "text_dim", "accent", "selection", "button", "button_hot",
- "button_down", "icon_folder", "icon_file", "icon_disk", "error"
- };
- static const char *const list_items[LIST_ROWS] = {
- "ngui_fill_rect", "ngui_frame", "ngui_line", "ngui_text",
- "ngui_text_styled", "ngui_icon", "ngui_button",
- "ngui_icon_button", "ngui_text_input", "ngui_scroll_begin",
- "ngui_scroll_end", "ngui_cut_left"
- };
- static struct ngui ui;
- static struct ngui_scroll page;
- static struct ngui_scroll list_scroll;
- static struct ngui_rect scroll_pane_rect;
- static int scroll_pane_valid;
- static struct ngui_input name_in;
- static struct ngui_input email_in;
- static struct ngui_input events_in;
- static struct ngui_input disabled_in;
- static int sec_y[SEC_COUNT];
- static int sec_content_h;
- static int button_clicks;
- static int icon_clicks;
- static int list_selected = 2;
- static int feedback_error;
- static char event_text[96];
- static char input_feedback[160];
- static int quit;
- /* ---- tiny freestanding string helpers ---- */
- static unsigned int s_len(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static void s_copy(char *d, const char *s, unsigned int cap)
- {
- unsigned int i = 0;
- if (!cap)
- return;
- while (s && s[i] && i + 1 < cap) {
- d[i] = s[i];
- i++;
- }
- d[i] = '\0';
- }
- static void s_append(char *d, unsigned int *n, const char *s,
- unsigned int cap)
- {
- while (s && *s && *n + 1 < cap)
- d[(*n)++] = *s++;
- d[*n] = '\0';
- }
- static void s_append_uint(char *d, unsigned int *n, unsigned int v,
- unsigned int cap)
- {
- char tmp[12];
- unsigned int i = 0, j;
- do {
- tmp[i++] = (char)('0' + v % 10);
- v /= 10;
- } while (v && i < sizeof(tmp));
- for (j = 0; j < i && *n + 1 < cap; j++)
- d[(*n)++] = tmp[i - 1 - j];
- d[*n] = '\0';
- }
- static void event_set(const char *s)
- {
- s_copy(event_text, s, sizeof(event_text));
- }
- static struct ngui_rect rect_make(int x, int y, int w, int h)
- {
- struct ngui_rect r;
- r.x = x;
- r.y = y;
- r.w = w;
- r.h = h;
- return r;
- }
- /* ---- layout ---- */
- static void gallery_layout(void)
- {
- int i, y = 0;
- for (i = 0; i < SEC_COUNT; i++) {
- sec_y[i] = y;
- y += SEC_HEAD_H + sec_card_h[i] + SEC_GAP;
- }
- y -= SEC_GAP;
- sec_content_h = y + PAGE_BOTTOM;
- }
- static int page_percent(void)
- {
- int max = page.content_h - page.view_h;
- if (max <= 0)
- return 0;
- return page.offset * 100 / max;
- }
- static int active_section(void)
- {
- int max = page.content_h - page.view_h;
- int probe = page.offset + 60;
- int i, active = 0;
- if (max > 0 && page.offset >= max - 1)
- return SEC_COUNT - 1;
- for (i = 0; i < SEC_COUNT; i++)
- if (sec_y[i] <= probe)
- active = i;
- return active;
- }
- static const char *focus_name(void)
- {
- switch (ngui_focus(&ui)) {
- case ID_IN_NAME:
- return "name";
- case ID_IN_EMAIL:
- return "prefilled";
- case ID_IN_EVENTS:
- return "events";
- default:
- return "none";
- }
- }
- /* Every widget is clipped by the scroll pane, but ngui's hit tests use the
- * widget rectangle directly. Skip widgets that are completely outside the
- * current clip so a click on the status bar can never reach a scrolled-away
- * control. */
- static int rect_visible(struct ngui *g, struct ngui_rect r)
- {
- struct ngui_rect c;
- if (g->nclip > 0)
- c = g->clip[g->nclip - 1];
- else
- c = rect_make(0, 0, g->w, g->h);
- return r.x < c.x + c.w && r.x + r.w > c.x &&
- r.y < c.y + c.h && r.y + r.h > c.y;
- }
- static int gallery_button(struct ngui *g, unsigned int id,
- struct ngui_rect r, const char *label)
- {
- if (!rect_visible(g, r))
- return 0;
- return ngui_button(g, id, r, label);
- }
- static int gallery_icon_button(struct ngui *g, unsigned int id,
- struct ngui_rect r, enum ngui_icon icon)
- {
- if (!rect_visible(g, r))
- return 0;
- return ngui_icon_button(g, id, r, icon);
- }
- static int gallery_input(struct ngui *g, unsigned int id,
- struct ngui_rect r, struct ngui_input *in)
- {
- if (!rect_visible(g, r))
- return 0;
- return ngui_text_input(g, id, r, in);
- }
- static void center_text(struct ngui *g, struct ngui_rect r, const char *s,
- unsigned short color)
- {
- int tw = (int)ngui_text_width_ui(g, s);
- int x = r.x + (r.w - tw) / 2;
- int y = r.y + (r.h - 16) / 2;
- ngui_push_clip(g, r);
- ngui_text(g, x, y, s, color);
- ngui_pop_clip(g);
- }
- /* ---- state ---- */
- static void reset_demo(struct ngui *g)
- {
- button_clicks = 0;
- icon_clicks = 0;
- list_selected = 2;
- ngui_input_set(&name_in, "");
- ngui_input_set(&email_in, "ada@nebos.org");
- ngui_input_set(&events_in, "");
- ngui_input_set(&disabled_in, "Read-only value");
- ngui_set_focus(g, 0);
- s_copy(input_feedback, "No events yet - edit a field.",
- sizeof(input_feedback));
- feedback_error = 0;
- event_set("widgets reset");
- list_scroll.offset = 0;
- ngui_scroll_to(&page, 0);
- ngui_invalidate(g);
- }
- static void note_input(struct ngui *g, int result, const char *field,
- const struct ngui_input *in)
- {
- char b[160];
- unsigned int n = 0;
- (void)g;
- if (!result)
- return;
- if (result & NGUI_INPUT_SUBMIT) {
- s_append(b, &n, "SUBMIT ", sizeof(b));
- s_append(b, &n, field, sizeof(b));
- s_append(b, &n, ": ", sizeof(b));
- s_append(b, &n, in->text, sizeof(b));
- feedback_error = 0;
- } else if (result & NGUI_INPUT_CANCEL) {
- s_append(b, &n, "CANCEL ", sizeof(b));
- s_append(b, &n, field, sizeof(b));
- s_append(b, &n, " - focus released", sizeof(b));
- feedback_error = 1;
- } else if (result & NGUI_INPUT_CHANGED) {
- s_append(b, &n, "CHANGED ", sizeof(b));
- s_append(b, &n, field, sizeof(b));
- s_append(b, &n, ": ", sizeof(b));
- s_append(b, &n, in->text, sizeof(b));
- feedback_error = 0;
- } else {
- return;
- }
- s_copy(input_feedback, b, sizeof(input_feedback));
- s_copy(event_text, b, sizeof(event_text));
- }
- /* ---- header and navigation ---- */
- static void draw_header(struct ngui *g, struct ngui_rect r)
- {
- int bx = r.x + r.w - 16 - (3 * 30 + 2 * 8);
- int by = r.y + 18;
- int sub_right = bx > r.x + 560 ? bx - 14 : r.x + r.w - 18;
- int step;
- ngui_fill_rect(g, r, g->theme.panel);
- ngui_fill_rect(g, rect_make(r.x, r.y, r.w, 2), g->theme.accent);
- ngui_text_styled(g, r.x + 18, r.y + 9, "ngui", 4,
- g->theme.accent, 2, 1);
- ngui_text_styled(g, r.x + 18 + 64 + 10, r.y + 9,
- "widget gallery", 14, g->theme.text, 2, 0);
- ngui_text_ellipsis(g, rect_make(r.x + 18, r.y + 40,
- sub_right - (r.x + 18), 16),
- r.x + 18, r.y + 40,
- "Every control and primitive exported by ngui.h - resize and play",
- g->theme.text_dim);
- ngui_fill_rect(g, rect_make(r.x, r.y + r.h - 1, r.w, 1),
- g->theme.border);
- if (bx < r.x + 560)
- return;
- ngui_fill_rect(g, rect_make(bx - 14, r.y + 18, 1, 28),
- g->theme.panel_alt);
- if (gallery_icon_button(g, ID_HEADER_HOME,
- rect_make(bx, by, 30, 28),
- NGUI_ICON_HOME)) {
- ngui_scroll_to(&page, 0);
- event_set("jumped to top");
- ngui_invalidate(g);
- }
- if (gallery_icon_button(g, ID_HEADER_PAGE_UP,
- rect_make(bx + 38, by, 30, 28),
- NGUI_ICON_UP)) {
- step = page.view_h > 120 ? page.view_h * 2 / 3 : 120;
- ngui_scroll_to(&page, page.offset - step);
- event_set("scrolled up");
- ngui_invalidate(g);
- }
- if (gallery_icon_button(g, ID_HEADER_RESET,
- rect_make(bx + 76, by, 30, 28),
- NGUI_ICON_REFRESH)) {
- reset_demo(g);
- }
- }
- static void draw_nav(struct ngui *g, struct ngui_rect r)
- {
- int x = r.x + 16;
- int y = r.y + 5;
- int active = active_section();
- int i;
- ngui_fill_rect(g, r, g->theme.bg);
- ngui_fill_rect(g, rect_make(r.x, r.y + r.h - 1, r.w, 1),
- g->theme.border);
- for (i = 0; i < SEC_COUNT; i++) {
- int w = (int)ngui_text_width_ui(g, sec_titles[i]) + 20;
- if (x + w > r.x + r.w - 12)
- break;
- if (gallery_button(g, ID_NAV_BASE + (unsigned int)i,
- rect_make(x, y, w, 25), sec_titles[i])) {
- char b[96];
- unsigned int n = 0;
- ngui_scroll_to(&page, sec_y[i]);
- s_append(b, &n, "jumped to ", sizeof(b));
- s_append(b, &n, sec_titles[i], sizeof(b));
- event_set(b);
- ngui_invalidate(g);
- }
- if (i == active)
- ngui_fill_rect(g, rect_make(x + 4, y + 25, w - 8, 2),
- g->theme.accent);
- x += w + 6;
- }
- }
- /* ---- sections ---- */
- static void draw_section_head(struct ngui *g, int x, int y, int w, int idx)
- {
- char num[3];
- int dx;
- num[0] = '0';
- num[1] = (char)('1' + idx);
- num[2] = '\0';
- ngui_fill_rect(g, rect_make(x, y + 5, 3, 18), g->theme.accent);
- ngui_text(g, x + 12, y + 6, num, g->theme.accent);
- ngui_text_styled(g, x + 34, y + 6, sec_titles[idx],
- s_len(sec_titles[idx]), g->theme.text, 1, 1);
- dx = x + 34 + (int)ngui_text_width_ui(g, sec_titles[idx]) + 14;
- if (dx < x + w - 16)
- ngui_text_ellipsis(g, rect_make(dx, y + 6, x + w - dx, 16),
- dx, y + 6, sec_descs[idx],
- g->theme.text_dim);
- }
- static void draw_typography(struct ngui *g, struct ngui_rect card)
- {
- int x = card.x + CARD_PAD;
- int y = card.y + 14;
- int label_w = 190;
- int value_x = x + label_w;
- int value_w = card.x + card.w - CARD_PAD - value_x;
- char buf[64];
- unsigned int n = 0;
- ngui_text(g, x, y + 4, "Heading", g->theme.text_dim);
- ngui_push_clip(g, rect_make(value_x, y, value_w, 32));
- ngui_text_styled(g, value_x, y, "Widget gallery", 14,
- g->theme.text, 2, 1);
- ngui_pop_clip(g);
- y += 38;
- ngui_text(g, x, y + 2, "Body", g->theme.text_dim);
- ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
- value_x, y + 2,
- "The quick brown fox jumps over the lazy dog.",
- g->theme.text);
- y += 22;
- ngui_text(g, x, y + 2, "Dim / secondary", g->theme.text_dim);
- ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
- value_x, y + 2,
- "Secondary text recedes into the background.",
- g->theme.text_dim);
- y += 22;
- ngui_text(g, x, y + 2, "Accent", g->theme.text_dim);
- ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
- value_x, y + 2,
- "Accent: selection, focus and interactive detail.",
- g->theme.accent);
- y += 22;
- ngui_text(g, x, y + 2, "Error", g->theme.text_dim);
- ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
- value_x, y + 2,
- "Error: the widget could not be opened.",
- g->theme.error);
- y += 22;
- ngui_text(g, x, y + 2, "ngui_text_n (n = 17)", g->theme.text_dim);
- ngui_push_clip(g, rect_make(value_x, y + 2, value_w, 16));
- ngui_text_n(g, value_x, y + 2, "draws only seventeen chars", 17,
- g->theme.text);
- ngui_pop_clip(g);
- y += 22;
- ngui_text(g, x, y + 2, "ngui_text_ellipsis", g->theme.text_dim);
- ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
- value_x, y + 2,
- "This caption is deliberately much too long to fit inside its rectangle, so ngui crops it with three dots.",
- g->theme.text);
- y += 22;
- ngui_text(g, x, y + 2, "ngui_text_width", g->theme.text_dim);
- n = 0;
- s_append(buf, &n, "ngui_text_width(\"ngui\") = ", sizeof(buf));
- s_append_uint(buf, &n, ngui_text_width_ui(g, "ngui"), sizeof(buf));
- s_append(buf, &n, " px", sizeof(buf));
- ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
- value_x, y + 2, buf, g->theme.text);
- }
- static void draw_shapes(struct ngui *g, struct ngui_rect card)
- {
- int x = card.x + CARD_PAD;
- int top = card.y + 16;
- int label_w = 120;
- int right_x = card.x + card.w / 2 + 8;
- int right_w = card.x + card.w - CARD_PAD - right_x;
- struct ngui_rect fr, inner;
- int px[7] = {0, 22, 44, 66, 88, 110, 132};
- int py[7] = {32, 18, 26, 8, 16, 4, 12};
- int i;
- ngui_text(g, x, top + 3, "fill_rect", g->theme.text_dim);
- for (i = 0; i < 5; i++) {
- unsigned short colors[5];
- struct ngui_rect sw;
- colors[0] = g->theme.accent;
- colors[1] = g->theme.selection;
- colors[2] = g->theme.icon_folder;
- colors[3] = g->theme.error;
- colors[4] = g->theme.panel_alt;
- sw = rect_make(x + label_w + i * 28, top, 22, 22);
- ngui_fill_rect(g, sw, colors[i]);
- ngui_frame(g, sw, g->theme.border);
- }
- ngui_text(g, x, top + 37, "frame / inset", g->theme.text_dim);
- fr = rect_make(x + label_w, top + 34, 150, 28);
- ngui_fill_rect(g, fr, g->theme.panel_alt);
- ngui_frame(g, fr, g->theme.border);
- inner = fr;
- ngui_inset(&inner, 5);
- ngui_frame(g, inner, g->theme.accent);
- ngui_text(g, x, top + 75, "ngui_line", g->theme.text_dim);
- for (i = 0; i < 6; i++)
- ngui_line(g, x + label_w + px[i], top + 72 + py[i],
- x + label_w + px[i + 1], top + 72 + py[i + 1],
- g->theme.accent);
- if (right_w < 160)
- return;
- ngui_text(g, right_x, card.y + 16, "push_clip / pop_clip",
- g->theme.text_dim);
- {
- struct ngui_rect vp = rect_make(right_x, card.y + 36,
- right_w, card.h - 52);
- ngui_fill_rect(g, vp, g->theme.bg);
- ngui_push_clip(g, vp);
- for (i = -vp.h; i < vp.w; i += 16) {
- unsigned short c = ((i / 16) & 1) ?
- g->theme.panel_alt : g->theme.button;
- ngui_line(g, vp.x + i, vp.y,
- vp.x + i + vp.h, vp.y + vp.h, c);
- }
- ngui_fill_rect(g, rect_make(vp.x - 20, vp.y + 14, 60, 30),
- g->theme.icon_folder);
- ngui_text_styled(g, vp.x - 24, vp.y + vp.h / 2 - 24,
- "CLIPPED", 7, g->theme.accent, 3, 1);
- ngui_pop_clip(g);
- ngui_frame(g, vp, g->theme.accent);
- }
- }
- static void draw_theme(struct ngui *g, struct ngui_rect card)
- {
- unsigned short colors[15];
- int colw = (card.w - 2 * CARD_PAD) / 3;
- int i;
- colors[0] = g->theme.bg;
- colors[1] = g->theme.panel;
- colors[2] = g->theme.panel_alt;
- colors[3] = g->theme.border;
- colors[4] = g->theme.text;
- colors[5] = g->theme.text_dim;
- colors[6] = g->theme.accent;
- colors[7] = g->theme.selection;
- colors[8] = g->theme.button;
- colors[9] = g->theme.button_hot;
- colors[10] = g->theme.button_down;
- colors[11] = g->theme.icon_folder;
- colors[12] = g->theme.icon_file;
- colors[13] = g->theme.icon_disk;
- colors[14] = g->theme.error;
- for (i = 0; i < 15; i++) {
- int cx = card.x + CARD_PAD + (i % 3) * colw;
- int cy = card.y + 14 + (i / 3) * 22;
- struct ngui_rect sw = rect_make(cx, cy, 16, 16);
- ngui_fill_rect(g, sw, colors[i]);
- ngui_frame(g, sw, g->theme.border);
- if (colw > 20)
- ngui_text_ellipsis(g, rect_make(cx + 24, cy,
- colw - 24, 16),
- cx + 24, cy, color_names[i],
- g->theme.text);
- }
- }
- static void draw_icons(struct ngui *g, struct ngui_rect card)
- {
- int cell = (card.w - 2 * CARD_PAD) / NGUI_ICON_COUNT;
- int bs = cell >= 56 ? 40 : 30;
- int y = card.y + 12;
- int i;
- char count[24];
- unsigned int n = 0;
- if (cell < 20)
- return;
- if (bs > cell - 4)
- bs = cell - 4;
- for (i = 0; i < NGUI_ICON_COUNT; i++) {
- const char *name = ngui_icon_name((enum ngui_icon)i);
- int cx = card.x + CARD_PAD + i * cell;
- int bx = cx + (cell - bs) / 2;
- int tw, tx, ly = y + bs + 4;
- struct ngui_rect lr;
- if (gallery_icon_button(g, ID_ICON_BASE + (unsigned int)i,
- rect_make(bx, y, bs, bs),
- (enum ngui_icon)i)) {
- char b[64];
- unsigned int m = 0;
- icon_clicks++;
- s_append(b, &m, "icon '", sizeof(b));
- s_append(b, &m, name, sizeof(b));
- s_append(b, &m, "' - ", sizeof(b));
- s_append_uint(b, &m, (unsigned int)icon_clicks,
- sizeof(b));
- s_append(b, &m, icon_clicks == 1 ?
- " click" : " clicks", sizeof(b));
- event_set(b);
- ngui_invalidate(g);
- }
- tw = (int)ngui_text_width_ui(g, name);
- tx = cx + (cell - tw) / 2;
- lr = rect_make(cx + 2, ly, cell - 4, 16);
- if (tx < lr.x)
- tx = lr.x;
- ngui_text_ellipsis(g, lr, tx, ly, name,
- g->theme.text_dim);
- }
- s_append(count, &n, "icon clicks: ", sizeof(count));
- s_append_uint(count, &n, (unsigned int)icon_clicks, sizeof(count));
- ngui_text_ellipsis(g, rect_make(card.x + CARD_PAD, card.y + 74,
- card.w - 2 * CARD_PAD, 16),
- card.x + CARD_PAD, card.y + 74,
- "Click an icon button - every one is live",
- g->theme.text_dim);
- if (card.w > 650) {
- int cw = (int)ngui_text_width_ui(g, count);
- ngui_text(g, card.x + card.w - CARD_PAD - cw,
- card.y + 74, count, g->theme.text_dim);
- }
- }
- static void draw_buttons(struct ngui *g, struct ngui_rect card)
- {
- int x = card.x + CARD_PAD;
- int y = card.y + 18;
- char num[16];
- char buf[32];
- unsigned int n;
- if (gallery_button(g, ID_BTN_CLICK, rect_make(x, y, 96, 28),
- "Click me")) {
- char b[64];
- unsigned int m = 0;
- button_clicks++;
- s_append(b, &m, "button clicked - total ", sizeof(b));
- s_append_uint(b, &m, (unsigned int)button_clicks, sizeof(b));
- event_set(b);
- }
- if (gallery_button(g, ID_BTN_RESET, rect_make(x + 104, y, 80, 28),
- "Reset")) {
- button_clicks = 0;
- event_set("button counter reset");
- }
- if (gallery_icon_button(g, ID_ICON_PLUS,
- rect_make(x + 192, y, 30, 28),
- NGUI_ICON_PLUS)) {
- char b[64];
- unsigned int m = 0;
- button_clicks++;
- s_append(b, &m, "icon button '+' - total ", sizeof(b));
- s_append_uint(b, &m, (unsigned int)button_clicks, sizeof(b));
- event_set(b);
- }
- if (gallery_icon_button(g, ID_ICON_REFRESH,
- rect_make(x + 228, y, 30, 28),
- NGUI_ICON_REFRESH)) {
- button_clicks = 0;
- event_set("button counter reset");
- }
- if (gallery_icon_button(g, ID_ICON_PREV,
- rect_make(x + 264, y, 30, 28),
- NGUI_ICON_CHEVRON_LEFT))
- event_set("chevron left pressed");
- if (gallery_icon_button(g, ID_ICON_NEXT,
- rect_make(x + 300, y, 30, 28),
- NGUI_ICON_CHEVRON_RIGHT))
- event_set("chevron right pressed");
- n = 0;
- s_append_uint(num, &n, (unsigned int)button_clicks, sizeof(num));
- if (card.w > 620) {
- int right = card.x + card.w - CARD_PAD;
- int vw = (int)ngui_text_width_px(g, num, n, g->font_px * 2, 1, 0, 0);
- ngui_text(g, right - 96, card.y + 18, "TOTAL CLICKS",
- g->theme.text_dim);
- ngui_text_styled(g, right - vw, card.y + 40, num, n,
- g->theme.accent, 2, 1);
- } else {
- unsigned int m = 0;
- s_append(buf, &m, "clicks: ", sizeof(buf));
- s_append_uint(buf, &m, (unsigned int)button_clicks,
- sizeof(buf));
- ngui_text(g, x, card.y + 56, buf, g->theme.accent);
- }
- ngui_text_ellipsis(g, rect_make(x, card.y + 72, card.w - 2 * CARD_PAD,
- 16),
- x, card.y + 72,
- "Buttons fire on release inside; hover lights the raised edge.",
- g->theme.text_dim);
- ngui_text_ellipsis(g, rect_make(x, card.y + 94, card.w - 2 * CARD_PAD,
- 16),
- x, card.y + 94,
- "Pressed buttons keep the accent fill until you let go.",
- g->theme.text_dim);
- ngui_text_ellipsis(g, rect_make(x, card.y + 116, card.w - 2 * CARD_PAD,
- 16),
- x, card.y + 116,
- "ngui_button(ui, id, rect, label) / ngui_icon_button(ui, id, rect, icon)",
- g->theme.border);
- }
- static void draw_inputs(struct ngui *g, struct ngui_rect card)
- {
- int x = card.x + CARD_PAD;
- int top = card.y + 16;
- int label_w = 126;
- int iw = card.w / 2 - 80;
- int field_y = top;
- int help_x, help_w, help_h;
- int result;
- int feedback_y = top + 144;
- if (iw < 220)
- iw = 220;
- if (iw > 340)
- iw = 340;
- ngui_text(g, x, field_y + 4, "Name", g->theme.text_dim);
- result = gallery_input(g, ID_IN_NAME,
- rect_make(x + label_w, field_y, iw, 24),
- &name_in);
- note_input(g, result, "name", &name_in);
- field_y += 36;
- ngui_text(g, x, field_y + 4, "Prefilled", g->theme.text_dim);
- result = gallery_input(g, ID_IN_EMAIL,
- rect_make(x + label_w, field_y, iw, 24),
- &email_in);
- note_input(g, result, "prefilled", &email_in);
- field_y += 36;
- ngui_text(g, x, field_y + 4, "Events", g->theme.text_dim);
- result = gallery_input(g, ID_IN_EVENTS,
- rect_make(x + label_w, field_y, iw, 24),
- &events_in);
- note_input(g, result, "events", &events_in);
- field_y += 36;
- ngui_text(g, x, field_y + 4, "Disabled", g->theme.text_dim);
- /* A disabled field ignores clicks and keys, so it never reports an
- * event; the value stays in the same proportional face, dimmed. */
- gallery_input(g, ID_IN_DISABLED,
- rect_make(x + label_w, field_y, iw, 24),
- &disabled_in);
- help_x = x + label_w + iw + 24;
- help_w = card.x + card.w - CARD_PAD - help_x;
- help_h = 200;
- if (help_w >= 150) {
- int hx = help_x;
- int hy = top;
- ngui_fill_rect(g, rect_make(hx, hy, help_w, help_h),
- g->theme.bg);
- ngui_frame(g, rect_make(hx, hy, help_w, help_h),
- g->theme.border);
- ngui_text(g, hx + 10, hy + 9, "TEXT INPUT",
- g->theme.accent);
- ngui_text(g, hx + 10, hy + 36, "Return - submit",
- g->theme.text_dim);
- ngui_text(g, hx + 10, hy + 54, "Esc - cancel focus",
- g->theme.text_dim);
- ngui_text(g, hx + 10, hy + 72, "Arrows - move caret",
- g->theme.text_dim);
- ngui_text(g, hx + 10, hy + 90, "Ctrl+Left/Right - words",
- g->theme.text_dim);
- ngui_text(g, hx + 10, hy + 108, "Ctrl+Del/Bksp - words",
- g->theme.text_dim);
- ngui_text(g, hx + 10, hy + 126, "Drag - select, Ctrl+A all",
- g->theme.text_dim);
- ngui_text(g, hx + 10, hy + 144, "Home/End, Ctrl+U - ends/clear",
- g->theme.text_dim);
- ngui_text(g, hx + 10, hy + 162, "Tab - focus first",
- g->theme.text_dim);
- ngui_text(g, hx + 10, hy + 180, "Disabled - no input",
- g->theme.text_dim);
- }
- if (input_feedback[0])
- ngui_text_ellipsis(g, rect_make(x, feedback_y,
- (help_w >= 150 ?
- help_x - x - 12 :
- card.w - 2 * CARD_PAD),
- 16),
- x, feedback_y, input_feedback,
- feedback_error ? g->theme.error :
- g->theme.accent);
- ngui_text_ellipsis(g, rect_make(x, top + 166,
- (help_w >= 150 ?
- help_x - x - 12 :
- card.w - 2 * CARD_PAD), 16),
- x, top + 166,
- "Return submits, Esc cancels, Ctrl+U clears.",
- g->theme.text_dim);
- if (gallery_button(g, ID_BTN_RESET_FIELDS,
- rect_make(x, top + 186, 104, 26),
- "Reset fields")) {
- ngui_input_set(&name_in, "");
- ngui_input_set(&email_in, "ada@nebos.org");
- ngui_input_set(&events_in, "");
- ngui_input_set(&disabled_in, "Read-only value");
- ngui_set_focus(g, 0);
- s_copy(input_feedback, "Fields reset to their defaults.",
- sizeof(input_feedback));
- feedback_error = 0;
- event_set("input fields reset");
- }
- if (help_w >= 150)
- ngui_text(g, x + 120, top + 192,
- "ngui_input_set() rewrites a field.",
- g->theme.border);
- }
- static void draw_scroll(struct ngui *g, struct ngui_rect card)
- {
- int x = card.x + CARD_PAD;
- int top = card.y + 16;
- int pane_w = card.w - 2 * CARD_PAD - 190;
- int pane_h = card.h - 56;
- struct ngui_rect pane;
- int help_x, help_w;
- int i;
- scroll_pane_valid = 0;
- if (pane_w > 520)
- pane_w = 520;
- if (pane_w < 260)
- pane_w = 260;
- if (card.w < 590)
- pane_w = card.w - 2 * CARD_PAD;
- if (pane_w < 120)
- return;
- pane = rect_make(x, card.y + 40, pane_w, pane_h);
- scroll_pane_rect = pane;
- scroll_pane_valid = 1;
- ngui_text_ellipsis(g, rect_make(x, top, card.w - 2 * CARD_PAD, 16),
- x, top,
- "A nested pane: hover for its scrollbar, then drag it.",
- g->theme.text_dim);
- list_scroll.content_h = LIST_ROWS * LIST_ROW_H;
- ngui_fill_rect(g, pane, g->theme.bg);
- ngui_scroll_begin(g, pane, &list_scroll);
- ngui_scroll_wheel(g, pane, &list_scroll);
- if (g->pressed && ngui_rect_contains(&pane, g->press_x, g->press_y)) {
- int idx = (g->press_y - pane.y + list_scroll.offset) /
- LIST_ROW_H;
- if (idx >= 0 && idx < LIST_ROWS) {
- char b[64];
- unsigned int n = 0;
- list_selected = idx;
- ngui_scroll_ensure_visible(&list_scroll,
- idx * LIST_ROW_H,
- LIST_ROW_H);
- s_append(b, &n, "row ", sizeof(b));
- s_append_uint(b, &n, (unsigned int)(idx + 1),
- sizeof(b));
- s_append(b, &n, " - ", sizeof(b));
- s_append(b, &n, list_items[idx], sizeof(b));
- event_set(b);
- ngui_invalidate(g);
- }
- }
- for (i = 0; i < LIST_ROWS; i++) {
- int ry = pane.y + i * LIST_ROW_H - list_scroll.offset;
- struct ngui_rect row = rect_make(pane.x, ry, pane.w,
- LIST_ROW_H);
- char label[64];
- unsigned int n = 0;
- unsigned short bg;
- int hover = g->inside &&
- ngui_rect_contains(&pane, g->mouse_x, g->mouse_y) &&
- (g->mouse_y - pane.y + list_scroll.offset) /
- LIST_ROW_H == i;
- if (i == list_selected)
- bg = g->theme.selection;
- else if (hover)
- bg = g->theme.button_hot;
- else
- bg = (i & 1) ? g->theme.panel : g->theme.bg;
- ngui_fill_rect(g, row, bg);
- ngui_icon(g, rect_make(pane.x + 8, ry + 4, 16, 16),
- (i % 3) == 2 ? NGUI_ICON_DISK : NGUI_ICON_FILE,
- i == list_selected ? g->theme.text :
- g->theme.icon_file);
- s_append_uint(label, &n, (unsigned int)(i + 1), sizeof(label));
- if (i + 1 < 10)
- s_append(label, &n, " ", sizeof(label));
- else
- s_append(label, &n, " ", sizeof(label));
- s_append(label, &n, list_items[i], sizeof(label));
- ngui_text_ellipsis(g, rect_make(pane.x + 32, ry + 4,
- pane.w - 44, 16),
- pane.x + 32, ry + 4, label,
- i == list_selected ? g->theme.text :
- g->theme.text_dim);
- }
- ngui_scroll_end(g, pane, &list_scroll);
- help_x = x + pane_w + 24;
- help_w = card.x + card.w - CARD_PAD - help_x;
- if (help_w >= 150) {
- char sel[32];
- unsigned int n = 0;
- int hy = card.y + 40;
- int hh = pane_h;
- ngui_fill_rect(g, rect_make(help_x, hy, help_w, hh),
- g->theme.bg);
- ngui_frame(g, rect_make(help_x, hy, help_w, hh),
- g->theme.border);
- ngui_text(g, help_x + 10, hy + 9, "ngui_scroll",
- g->theme.accent);
- ngui_text(g, help_x + 10, hy + 36, "12 rows x 24 px",
- g->theme.text_dim);
- ngui_text(g, help_x + 10, hy + 54, "view_h follows pane",
- g->theme.text_dim);
- ngui_text(g, help_x + 10, hy + 72, "drag thumb or track",
- g->theme.text_dim);
- ngui_text(g, help_x + 10, hy + 90, "clip hides overflow",
- g->theme.text_dim);
- ngui_text(g, help_x + 10, hy + 108, "ensure_visible()",
- g->theme.text_dim);
- n = 0;
- s_append(sel, &n, "selected: ", sizeof(sel));
- s_append_uint(sel, &n, (unsigned int)(list_selected + 1),
- sizeof(sel));
- ngui_text(g, help_x + 10, hy + 126, sel,
- g->theme.accent);
- }
- }
- static void draw_layout(struct ngui *g, struct ngui_rect card)
- {
- int x = card.x + CARD_PAD;
- int y = card.y + 16;
- int ow = card.w - 2 * CARD_PAD;
- struct ngui_rect outer, top, bottom, left, right, center;
- int hx;
- if (ow > 520)
- ow = 520;
- outer = rect_make(x, y, ow, card.h - 32);
- if (outer.w < 200 || outer.h < 80)
- return;
- top = ngui_cut_top(&outer, 30);
- bottom = ngui_cut_bottom(&outer, 30);
- left = ngui_cut_left(&outer, 120);
- right = ngui_cut_right(&outer, 120);
- center = outer;
- ngui_fill_rect(g, top, g->theme.selection);
- ngui_frame(g, top, g->theme.border);
- center_text(g, top, "ngui_cut_top(r, 30)", g->theme.text);
- ngui_fill_rect(g, bottom, g->theme.button);
- ngui_frame(g, bottom, g->theme.border);
- center_text(g, bottom, "ngui_cut_bottom(r, 30)", g->theme.text);
- ngui_fill_rect(g, left, g->theme.panel_alt);
- ngui_frame(g, left, g->theme.border);
- center_text(g, left, "cut_left", g->theme.text);
- ngui_fill_rect(g, right, g->theme.panel_alt);
- ngui_frame(g, right, g->theme.border);
- center_text(g, right, "cut_right", g->theme.text);
- ngui_fill_rect(g, center, g->theme.bg);
- ngui_frame(g, center, g->theme.border);
- center_text(g, center, "leftover rect", g->theme.text_dim);
- hx = x + ow + 24;
- if (card.x + card.w - CARD_PAD - hx < 150)
- return;
- ngui_text(g, hx, card.y + 16, "layout helpers",
- g->theme.accent);
- ngui_text(g, hx, card.y + 40, "ngui_cut_top(r, h)",
- g->theme.text_dim);
- ngui_text(g, hx, card.y + 58, "ngui_cut_bottom(r, h)",
- g->theme.text_dim);
- ngui_text(g, hx, card.y + 76, "ngui_cut_left(r, w)",
- g->theme.text_dim);
- ngui_text(g, hx, card.y + 94, "ngui_cut_right(r, w)",
- g->theme.text_dim);
- ngui_text(g, hx, card.y + 112, "ngui_inset(r, n)",
- g->theme.text_dim);
- ngui_text(g, hx, card.y + 136, "Each cut shrinks r and",
- g->theme.border);
- ngui_text(g, hx, card.y + 154, "returns the cut piece.",
- g->theme.border);
- }
- #define CURSOR_NAME_(e, css, file) css,
- static const char *const cursor_names[CURSOR_COUNT] = {
- CURSOR_LIST(CURSOR_NAME_)
- };
- #undef CURSOR_NAME_
- /* One cell per cursor shape: hovering a cell shows that cursor (the
- * Adwaita set from /ramfs/cursors, see include/cursor_abi.h). */
- static void draw_cursors(struct ngui *g, struct ngui_rect card)
- {
- int inner_w = card.w - 2 * CARD_PAD;
- int cols = inner_w / 120;
- int rows, cell_w, cell_h, i;
- int y0 = card.y + 32;
- if (cols < 3)
- cols = 3;
- rows = (CURSOR_COUNT + cols - 1) / cols;
- cell_w = inner_w / cols;
- cell_h = (card.y + card.h - 10 - y0) / rows;
- if (cell_h > 30)
- cell_h = 30;
- if (cell_w < 20 || cell_h < 12)
- return;
- ngui_text(g, card.x + CARD_PAD, card.y + 10,
- "Hover a cell to see the cursor (CSS cursor names).",
- g->theme.text_dim);
- for (i = 0; i < CURSOR_COUNT; i++) {
- struct ngui_rect c = rect_make(
- card.x + CARD_PAD + (i % cols) * cell_w + 2,
- y0 + (i / cols) * cell_h + 2, cell_w - 4, cell_h - 4);
- int hot = ngui_cursor_rect(g, c, i);
- ngui_fill_rect(g, c, hot ? g->theme.button_hot :
- g->theme.panel_alt);
- ngui_frame(g, c, hot ? g->theme.accent : g->theme.border);
- ngui_text_ellipsis(g, rect_make(c.x + 6, c.y, c.w - 12, c.h),
- c.x + 6, c.y + (c.h - 16) / 2,
- cursor_names[i],
- hot ? g->theme.text : g->theme.text_dim);
- }
- }
- static void draw_body(struct ngui *g, struct ngui_rect body)
- {
- int x = body.x + PAGE_PAD;
- int w = body.w - PAGE_PAD * 2;
- int i;
- page.content_h = sec_content_h;
- ngui_fill_rect(g, body, g->theme.bg);
- ngui_scroll_begin(g, body, &page);
- /* Leave wheel scroll for the nested pane when the pointer is over it;
- * the rect comes from the previous frame, before this one redraws it. */
- if (!(scroll_pane_valid &&
- ngui_rect_contains(&scroll_pane_rect, g->mouse_x, g->mouse_y)))
- ngui_scroll_wheel(g, body, &page);
- scroll_pane_valid = 0;
- for (i = 0; i < SEC_COUNT; i++) {
- int sy = body.y - page.offset + sec_y[i];
- struct ngui_rect card;
- if (sy + SEC_HEAD_H + sec_card_h[i] < body.y - 4 ||
- sy > body.y + body.h + 4)
- continue;
- draw_section_head(g, x, sy, w, i);
- card = rect_make(x, sy + SEC_HEAD_H, w, sec_card_h[i]);
- switch (i) {
- case SEC_TEXT:
- draw_typography(g, card);
- break;
- case SEC_SHAPES:
- draw_shapes(g, card);
- break;
- case SEC_THEME:
- draw_theme(g, card);
- break;
- case SEC_ICONS:
- draw_icons(g, card);
- break;
- case SEC_BUTTONS:
- draw_buttons(g, card);
- break;
- case SEC_INPUTS:
- draw_inputs(g, card);
- break;
- case SEC_SCROLL:
- draw_scroll(g, card);
- break;
- case SEC_LAYOUT:
- draw_layout(g, card);
- break;
- case SEC_CURSORS:
- draw_cursors(g, card);
- break;
- default:
- break;
- }
- }
- ngui_scroll_end(g, body, &page);
- }
- static void draw_status(struct ngui *g, struct ngui_rect r)
- {
- char left[160];
- char right[64];
- unsigned int n = 0, m = 0;
- int rw, rx;
- ngui_fill_rect(g, r, g->theme.panel);
- ngui_fill_rect(g, rect_make(r.x, r.y, r.w, 1), g->theme.border);
- if (g->inside) {
- s_append(right, &m, "x ", sizeof(right));
- s_append_uint(right, &m, (unsigned int)g->mouse_x,
- sizeof(right));
- s_append(right, &m, " y ", sizeof(right));
- s_append_uint(right, &m, (unsigned int)g->mouse_y,
- sizeof(right));
- } else {
- s_append(right, &m, "pointer outside", sizeof(right));
- }
- s_append(right, &m, " ", sizeof(right));
- s_append_uint(right, &m, (unsigned int)page_percent(),
- sizeof(right));
- s_append(right, &m, "%", sizeof(right));
- rw = (int)ngui_text_width_ui(g, right);
- rx = r.x + r.w - rw - 10;
- ngui_text(g, rx, r.y + 4, right, g->theme.text_dim);
- if (ngui_focus(&ui)) {
- s_append(left, &n, "editing ", sizeof(left));
- s_append(left, &n, focus_name(), sizeof(left));
- s_append(left, &n, " | ", sizeof(left));
- }
- if (event_text[0])
- s_append(left, &n, event_text, sizeof(left));
- else
- s_append(left, &n, "Ready - every ngui control on one page",
- sizeof(left));
- if (rx - (r.x + 10) > 40)
- ngui_text_ellipsis(g, rect_make(r.x + 10, r.y,
- rx - r.x - 20, r.h),
- r.x + 10, r.y + 4, left, g->theme.text);
- }
- static void draw(struct ngui *g)
- {
- struct ngui_rect all = {0, 0, g->w, g->h};
- struct ngui_rect header, nav, status, body;
- if (g->w < 420 || g->h < 300) {
- ngui_fill_rect(g, all, g->theme.bg);
- ngui_text(g, 16, 16, "The gallery needs a larger window.",
- g->theme.text);
- ngui_text(g, 16, 38, "Resize the window to continue.",
- g->theme.text_dim);
- return;
- }
- header = ngui_cut_top(&all, HEADER_H);
- nav = ngui_cut_top(&all, NAV_H);
- status = ngui_cut_bottom(&all, STATUS_H);
- body = all;
- draw_header(g, header);
- draw_nav(g, nav);
- draw_body(g, body);
- draw_status(g, status);
- }
- static void handle_keys(struct ngui *g)
- {
- int k;
- if (ngui_focus(g)) {
- /* A focused field consumed its keys; Ctrl+C is never consumed. */
- while ((k = ngui_key(g)) != 0)
- if (k == 0x03)
- quit = 1;
- return;
- }
- k = ngui_key_peek(g);
- if (k == NGUI_KEY_PAGE_UP || k == NGUI_KEY_PAGE_DOWN) {
- if (ngui_scroll_keys(g, &page)) {
- event_set(k == NGUI_KEY_PAGE_UP ?
- "page up" : "page down");
- ngui_invalidate(g);
- }
- return;
- }
- while ((k = ngui_key(g)) != 0) {
- switch (k) {
- case 0x03: /* Ctrl+C */
- case NGUI_KEY_ESC:
- quit = 1;
- return;
- case NGUI_KEY_UP:
- ngui_scroll_to(&page, page.offset - 36);
- ngui_invalidate(g);
- break;
- case NGUI_KEY_DOWN:
- ngui_scroll_to(&page, page.offset + 36);
- ngui_invalidate(g);
- break;
- case NGUI_KEY_HOME:
- ngui_scroll_to(&page, 0);
- event_set("jumped to top");
- ngui_invalidate(g);
- break;
- case NGUI_KEY_END:
- ngui_scroll_to(&page, page.content_h - page.view_h);
- event_set("jumped to the end");
- ngui_invalidate(g);
- break;
- case NGUI_KEY_TAB:
- ngui_set_focus(g, ID_IN_NAME);
- ngui_scroll_to(&page, sec_y[SEC_INPUTS]);
- event_set("focused the name field");
- ngui_invalidate(g);
- break;
- default:
- break;
- }
- }
- }
- void _start(const char *arg)
- {
- struct win_info wi;
- (void)arg;
- if (u_win_create(START_W, START_H, "ngui gallery", &wi) != 0) {
- u_puts("gallery: no graphics window\n");
- u_exit(1);
- }
- ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width,
- (int)wi.height);
- ngui_font_attach(&ui);
- name_in.placeholder = "Type a name";
- email_in.placeholder = "you@example.com";
- events_in.placeholder = "Type and press Return";
- disabled_in.placeholder = "Cannot edit";
- disabled_in.disabled = 1;
- ngui_input_set(&disabled_in, "Read-only value");
- gallery_layout();
- page.content_h = sec_content_h;
- page.view_h = START_H - HEADER_H - NAV_H - STATUS_H;
- ngui_input_set(&email_in, "ada@nebos.org");
- s_copy(input_feedback, "No events yet - edit a field.",
- sizeof(input_feedback));
- s_copy(event_text, "Ready - every ngui control on one page",
- sizeof(event_text));
- for (;;) {
- int want = ngui_begin(&ui);
- if (quit)
- break;
- if (!want) {
- ngui_end_ex(&ui, 0);
- u_sleep_ms(10);
- continue;
- }
- draw(&ui);
- handle_keys(&ui);
- if (quit)
- break;
- ngui_end(&ui);
- u_sleep_ms(10);
- }
- u_win_close();
- u_exit(0);
- }
|