gallery.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. /* gallery: an interactive showcase of every widget and primitive in ngui.
  2. *
  3. * One scrolling page holds a section per part of the toolkit: typography,
  4. * the drawing primitives, the theme palette, every procedural icon, buttons,
  5. * text fields, the scroll pane and the ngui_cut_* layout helpers. The
  6. * controls are live, so all the input paths the toolkit offers are exercised.
  7. * ESC closes the window after a focused field has cancelled its edit. */
  8. #include "ucmd_lib.h"
  9. #include "ngui.h"
  10. #define START_W 860
  11. #define START_H 600
  12. #define HEADER_H 64
  13. #define NAV_H 35
  14. #define STATUS_H 24
  15. #define SEC_HEAD_H 34
  16. #define SEC_GAP 24
  17. #define PAGE_BOTTOM 24
  18. #define PAGE_PAD 12
  19. #define CARD_PAD 16
  20. #define TYPO_H 220
  21. #define SHAPES_H 154
  22. #define THEME_H 140
  23. #define ICONS_H 100
  24. #define BUTTONS_H 140
  25. #define INPUTS_H 250
  26. #define SCROLL_H 220
  27. #define LAYOUT_H 190
  28. #define LIST_ROWS 12
  29. #define LIST_ROW_H 24
  30. enum section {
  31. SEC_TEXT,
  32. SEC_SHAPES,
  33. SEC_THEME,
  34. SEC_ICONS,
  35. SEC_BUTTONS,
  36. SEC_INPUTS,
  37. SEC_SCROLL,
  38. SEC_LAYOUT,
  39. SEC_COUNT
  40. };
  41. enum {
  42. ID_HEADER_HOME = 1,
  43. ID_HEADER_PAGE_UP,
  44. ID_HEADER_RESET,
  45. ID_BTN_CLICK,
  46. ID_BTN_RESET,
  47. ID_ICON_PLUS,
  48. ID_ICON_REFRESH,
  49. ID_ICON_PREV,
  50. ID_ICON_NEXT,
  51. ID_IN_NAME,
  52. ID_IN_EMAIL,
  53. ID_IN_EVENTS,
  54. ID_IN_DISABLED,
  55. ID_BTN_RESET_FIELDS,
  56. ID_NAV_BASE = 0x100,
  57. ID_ICON_BASE = 0x200
  58. };
  59. static const int sec_card_h[SEC_COUNT] = {
  60. TYPO_H, SHAPES_H, THEME_H, ICONS_H,
  61. BUTTONS_H, INPUTS_H, SCROLL_H, LAYOUT_H
  62. };
  63. static const char *const sec_titles[SEC_COUNT] = {
  64. "Text", "Shapes", "Theme", "Icons",
  65. "Buttons", "Inputs", "Scroll", "Layout"
  66. };
  67. static const char *const sec_descs[SEC_COUNT] = {
  68. "styles, truncation and measurement",
  69. "rectangles, frames, lines and clipping",
  70. "the ngui_theme_dark palette",
  71. "every Adwaita symbolic icon",
  72. "buttons and icon buttons",
  73. "single-line text fields",
  74. "a nested draggable scroll pane",
  75. "ngui_cut_* layout helpers"
  76. };
  77. static const char *const color_names[15] = {
  78. "bg", "panel", "panel_alt", "border", "text",
  79. "text_dim", "accent", "selection", "button", "button_hot",
  80. "button_down", "icon_folder", "icon_file", "icon_disk", "error"
  81. };
  82. static const char *const list_items[LIST_ROWS] = {
  83. "ngui_fill_rect", "ngui_frame", "ngui_line", "ngui_text",
  84. "ngui_text_styled", "ngui_icon", "ngui_button",
  85. "ngui_icon_button", "ngui_text_input", "ngui_scroll_begin",
  86. "ngui_scroll_end", "ngui_cut_left"
  87. };
  88. static struct ngui ui;
  89. static struct ngui_scroll page;
  90. static struct ngui_scroll list_scroll;
  91. static struct ngui_rect scroll_pane_rect;
  92. static int scroll_pane_valid;
  93. static struct ngui_input name_in;
  94. static struct ngui_input email_in;
  95. static struct ngui_input events_in;
  96. static struct ngui_input disabled_in;
  97. static int sec_y[SEC_COUNT];
  98. static int sec_content_h;
  99. static int button_clicks;
  100. static int icon_clicks;
  101. static int list_selected = 2;
  102. static int feedback_error;
  103. static char event_text[96];
  104. static char input_feedback[160];
  105. static int quit;
  106. /* ---- tiny freestanding string helpers ---- */
  107. static unsigned int s_len(const char *s)
  108. {
  109. unsigned int n = 0;
  110. while (s && s[n])
  111. n++;
  112. return n;
  113. }
  114. static void s_copy(char *d, const char *s, unsigned int cap)
  115. {
  116. unsigned int i = 0;
  117. if (!cap)
  118. return;
  119. while (s && s[i] && i + 1 < cap) {
  120. d[i] = s[i];
  121. i++;
  122. }
  123. d[i] = '\0';
  124. }
  125. static void s_append(char *d, unsigned int *n, const char *s,
  126. unsigned int cap)
  127. {
  128. while (s && *s && *n + 1 < cap)
  129. d[(*n)++] = *s++;
  130. d[*n] = '\0';
  131. }
  132. static void s_append_uint(char *d, unsigned int *n, unsigned int v,
  133. unsigned int cap)
  134. {
  135. char tmp[12];
  136. unsigned int i = 0, j;
  137. do {
  138. tmp[i++] = (char)('0' + v % 10);
  139. v /= 10;
  140. } while (v && i < sizeof(tmp));
  141. for (j = 0; j < i && *n + 1 < cap; j++)
  142. d[(*n)++] = tmp[i - 1 - j];
  143. d[*n] = '\0';
  144. }
  145. static void event_set(const char *s)
  146. {
  147. s_copy(event_text, s, sizeof(event_text));
  148. }
  149. static struct ngui_rect rect_make(int x, int y, int w, int h)
  150. {
  151. struct ngui_rect r;
  152. r.x = x;
  153. r.y = y;
  154. r.w = w;
  155. r.h = h;
  156. return r;
  157. }
  158. /* ---- layout ---- */
  159. static void gallery_layout(void)
  160. {
  161. int i, y = 0;
  162. for (i = 0; i < SEC_COUNT; i++) {
  163. sec_y[i] = y;
  164. y += SEC_HEAD_H + sec_card_h[i] + SEC_GAP;
  165. }
  166. y -= SEC_GAP;
  167. sec_content_h = y + PAGE_BOTTOM;
  168. }
  169. static int page_percent(void)
  170. {
  171. int max = page.content_h - page.view_h;
  172. if (max <= 0)
  173. return 0;
  174. return page.offset * 100 / max;
  175. }
  176. static int active_section(void)
  177. {
  178. int max = page.content_h - page.view_h;
  179. int probe = page.offset + 60;
  180. int i, active = 0;
  181. if (max > 0 && page.offset >= max - 1)
  182. return SEC_LAYOUT;
  183. for (i = 0; i < SEC_COUNT; i++)
  184. if (sec_y[i] <= probe)
  185. active = i;
  186. return active;
  187. }
  188. static const char *focus_name(void)
  189. {
  190. switch (ngui_focus(&ui)) {
  191. case ID_IN_NAME:
  192. return "name";
  193. case ID_IN_EMAIL:
  194. return "prefilled";
  195. case ID_IN_EVENTS:
  196. return "events";
  197. default:
  198. return "none";
  199. }
  200. }
  201. /* Every widget is clipped by the scroll pane, but ngui's hit tests use the
  202. * widget rectangle directly. Skip widgets that are completely outside the
  203. * current clip so a click on the status bar can never reach a scrolled-away
  204. * control. */
  205. static int rect_visible(struct ngui *g, struct ngui_rect r)
  206. {
  207. struct ngui_rect c;
  208. if (g->nclip > 0)
  209. c = g->clip[g->nclip - 1];
  210. else
  211. c = rect_make(0, 0, g->w, g->h);
  212. return r.x < c.x + c.w && r.x + r.w > c.x &&
  213. r.y < c.y + c.h && r.y + r.h > c.y;
  214. }
  215. static int gallery_button(struct ngui *g, unsigned int id,
  216. struct ngui_rect r, const char *label)
  217. {
  218. if (!rect_visible(g, r))
  219. return 0;
  220. return ngui_button(g, id, r, label);
  221. }
  222. static int gallery_icon_button(struct ngui *g, unsigned int id,
  223. struct ngui_rect r, enum ngui_icon icon)
  224. {
  225. if (!rect_visible(g, r))
  226. return 0;
  227. return ngui_icon_button(g, id, r, icon);
  228. }
  229. static int gallery_input(struct ngui *g, unsigned int id,
  230. struct ngui_rect r, struct ngui_input *in)
  231. {
  232. if (!rect_visible(g, r))
  233. return 0;
  234. return ngui_text_input(g, id, r, in);
  235. }
  236. static void center_text(struct ngui *g, struct ngui_rect r, const char *s,
  237. unsigned short color)
  238. {
  239. int tw = (int)ngui_text_width_ui(g, s);
  240. int x = r.x + (r.w - tw) / 2;
  241. int y = r.y + (r.h - 16) / 2;
  242. ngui_push_clip(g, r);
  243. ngui_text(g, x, y, s, color);
  244. ngui_pop_clip(g);
  245. }
  246. /* ---- state ---- */
  247. static void reset_demo(struct ngui *g)
  248. {
  249. button_clicks = 0;
  250. icon_clicks = 0;
  251. list_selected = 2;
  252. ngui_input_set(&name_in, "");
  253. ngui_input_set(&email_in, "ada@nebos.org");
  254. ngui_input_set(&events_in, "");
  255. ngui_input_set(&disabled_in, "Read-only value");
  256. ngui_set_focus(g, 0);
  257. s_copy(input_feedback, "No events yet - edit a field.",
  258. sizeof(input_feedback));
  259. feedback_error = 0;
  260. event_set("widgets reset");
  261. list_scroll.offset = 0;
  262. ngui_scroll_to(&page, 0);
  263. ngui_invalidate(g);
  264. }
  265. static void note_input(struct ngui *g, int result, const char *field,
  266. const struct ngui_input *in)
  267. {
  268. char b[160];
  269. unsigned int n = 0;
  270. (void)g;
  271. if (!result)
  272. return;
  273. if (result & NGUI_INPUT_SUBMIT) {
  274. s_append(b, &n, "SUBMIT ", sizeof(b));
  275. s_append(b, &n, field, sizeof(b));
  276. s_append(b, &n, ": ", sizeof(b));
  277. s_append(b, &n, in->text, sizeof(b));
  278. feedback_error = 0;
  279. } else if (result & NGUI_INPUT_CANCEL) {
  280. s_append(b, &n, "CANCEL ", sizeof(b));
  281. s_append(b, &n, field, sizeof(b));
  282. s_append(b, &n, " - focus released", sizeof(b));
  283. feedback_error = 1;
  284. } else if (result & NGUI_INPUT_CHANGED) {
  285. s_append(b, &n, "CHANGED ", sizeof(b));
  286. s_append(b, &n, field, sizeof(b));
  287. s_append(b, &n, ": ", sizeof(b));
  288. s_append(b, &n, in->text, sizeof(b));
  289. feedback_error = 0;
  290. } else {
  291. return;
  292. }
  293. s_copy(input_feedback, b, sizeof(input_feedback));
  294. s_copy(event_text, b, sizeof(event_text));
  295. }
  296. /* ---- header and navigation ---- */
  297. static void draw_header(struct ngui *g, struct ngui_rect r)
  298. {
  299. int bx = r.x + r.w - 16 - (3 * 30 + 2 * 8);
  300. int by = r.y + 18;
  301. int sub_right = bx > r.x + 560 ? bx - 14 : r.x + r.w - 18;
  302. int step;
  303. ngui_fill_rect(g, r, g->theme.panel);
  304. ngui_fill_rect(g, rect_make(r.x, r.y, r.w, 2), g->theme.accent);
  305. ngui_text_styled(g, r.x + 18, r.y + 9, "ngui", 4,
  306. g->theme.accent, 2, 1);
  307. ngui_text_styled(g, r.x + 18 + 64 + 10, r.y + 9,
  308. "widget gallery", 14, g->theme.text, 2, 0);
  309. ngui_text_ellipsis(g, rect_make(r.x + 18, r.y + 40,
  310. sub_right - (r.x + 18), 16),
  311. r.x + 18, r.y + 40,
  312. "Every control and primitive exported by ngui.h - resize and play",
  313. g->theme.text_dim);
  314. ngui_fill_rect(g, rect_make(r.x, r.y + r.h - 1, r.w, 1),
  315. g->theme.border);
  316. if (bx < r.x + 560)
  317. return;
  318. ngui_fill_rect(g, rect_make(bx - 14, r.y + 18, 1, 28),
  319. g->theme.panel_alt);
  320. if (gallery_icon_button(g, ID_HEADER_HOME,
  321. rect_make(bx, by, 30, 28),
  322. NGUI_ICON_HOME)) {
  323. ngui_scroll_to(&page, 0);
  324. event_set("jumped to top");
  325. ngui_invalidate(g);
  326. }
  327. if (gallery_icon_button(g, ID_HEADER_PAGE_UP,
  328. rect_make(bx + 38, by, 30, 28),
  329. NGUI_ICON_UP)) {
  330. step = page.view_h > 120 ? page.view_h * 2 / 3 : 120;
  331. ngui_scroll_to(&page, page.offset - step);
  332. event_set("scrolled up");
  333. ngui_invalidate(g);
  334. }
  335. if (gallery_icon_button(g, ID_HEADER_RESET,
  336. rect_make(bx + 76, by, 30, 28),
  337. NGUI_ICON_REFRESH)) {
  338. reset_demo(g);
  339. }
  340. }
  341. static void draw_nav(struct ngui *g, struct ngui_rect r)
  342. {
  343. int x = r.x + 16;
  344. int y = r.y + 5;
  345. int active = active_section();
  346. int i;
  347. ngui_fill_rect(g, r, g->theme.bg);
  348. ngui_fill_rect(g, rect_make(r.x, r.y + r.h - 1, r.w, 1),
  349. g->theme.border);
  350. for (i = 0; i < SEC_COUNT; i++) {
  351. int w = (int)ngui_text_width_ui(g, sec_titles[i]) + 20;
  352. if (x + w > r.x + r.w - 12)
  353. break;
  354. if (gallery_button(g, ID_NAV_BASE + (unsigned int)i,
  355. rect_make(x, y, w, 25), sec_titles[i])) {
  356. char b[96];
  357. unsigned int n = 0;
  358. ngui_scroll_to(&page, sec_y[i]);
  359. s_append(b, &n, "jumped to ", sizeof(b));
  360. s_append(b, &n, sec_titles[i], sizeof(b));
  361. event_set(b);
  362. ngui_invalidate(g);
  363. }
  364. if (i == active)
  365. ngui_fill_rect(g, rect_make(x + 4, y + 25, w - 8, 2),
  366. g->theme.accent);
  367. x += w + 6;
  368. }
  369. }
  370. /* ---- sections ---- */
  371. static void draw_section_head(struct ngui *g, int x, int y, int w, int idx)
  372. {
  373. char num[3];
  374. int dx;
  375. num[0] = '0';
  376. num[1] = (char)('1' + idx);
  377. num[2] = '\0';
  378. ngui_fill_rect(g, rect_make(x, y + 5, 3, 18), g->theme.accent);
  379. ngui_text(g, x + 12, y + 6, num, g->theme.accent);
  380. ngui_text_styled(g, x + 34, y + 6, sec_titles[idx],
  381. s_len(sec_titles[idx]), g->theme.text, 1, 1);
  382. dx = x + 34 + (int)ngui_text_width_ui(g, sec_titles[idx]) + 14;
  383. if (dx < x + w - 16)
  384. ngui_text_ellipsis(g, rect_make(dx, y + 6, x + w - dx, 16),
  385. dx, y + 6, sec_descs[idx],
  386. g->theme.text_dim);
  387. }
  388. static void draw_typography(struct ngui *g, struct ngui_rect card)
  389. {
  390. int x = card.x + CARD_PAD;
  391. int y = card.y + 14;
  392. int label_w = 190;
  393. int value_x = x + label_w;
  394. int value_w = card.x + card.w - CARD_PAD - value_x;
  395. char buf[64];
  396. unsigned int n = 0;
  397. ngui_text(g, x, y + 4, "Heading", g->theme.text_dim);
  398. ngui_push_clip(g, rect_make(value_x, y, value_w, 32));
  399. ngui_text_styled(g, value_x, y, "Widget gallery", 14,
  400. g->theme.text, 2, 1);
  401. ngui_pop_clip(g);
  402. y += 38;
  403. ngui_text(g, x, y + 2, "Body", g->theme.text_dim);
  404. ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
  405. value_x, y + 2,
  406. "The quick brown fox jumps over the lazy dog.",
  407. g->theme.text);
  408. y += 22;
  409. ngui_text(g, x, y + 2, "Dim / secondary", g->theme.text_dim);
  410. ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
  411. value_x, y + 2,
  412. "Secondary text recedes into the background.",
  413. g->theme.text_dim);
  414. y += 22;
  415. ngui_text(g, x, y + 2, "Accent", g->theme.text_dim);
  416. ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
  417. value_x, y + 2,
  418. "Accent: selection, focus and interactive detail.",
  419. g->theme.accent);
  420. y += 22;
  421. ngui_text(g, x, y + 2, "Error", g->theme.text_dim);
  422. ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
  423. value_x, y + 2,
  424. "Error: the widget could not be opened.",
  425. g->theme.error);
  426. y += 22;
  427. ngui_text(g, x, y + 2, "ngui_text_n (n = 17)", g->theme.text_dim);
  428. ngui_push_clip(g, rect_make(value_x, y + 2, value_w, 16));
  429. ngui_text_n(g, value_x, y + 2, "draws only seventeen chars", 17,
  430. g->theme.text);
  431. ngui_pop_clip(g);
  432. y += 22;
  433. ngui_text(g, x, y + 2, "ngui_text_ellipsis", g->theme.text_dim);
  434. ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
  435. value_x, y + 2,
  436. "This caption is deliberately much too long to fit inside its rectangle, so ngui crops it with three dots.",
  437. g->theme.text);
  438. y += 22;
  439. ngui_text(g, x, y + 2, "ngui_text_width", g->theme.text_dim);
  440. n = 0;
  441. s_append(buf, &n, "ngui_text_width(\"ngui\") = ", sizeof(buf));
  442. s_append_uint(buf, &n, ngui_text_width_ui(g, "ngui"), sizeof(buf));
  443. s_append(buf, &n, " px", sizeof(buf));
  444. ngui_text_ellipsis(g, rect_make(value_x, y + 2, value_w, 16),
  445. value_x, y + 2, buf, g->theme.text);
  446. }
  447. static void draw_shapes(struct ngui *g, struct ngui_rect card)
  448. {
  449. int x = card.x + CARD_PAD;
  450. int top = card.y + 16;
  451. int label_w = 120;
  452. int right_x = card.x + card.w / 2 + 8;
  453. int right_w = card.x + card.w - CARD_PAD - right_x;
  454. struct ngui_rect fr, inner;
  455. int px[7] = {0, 22, 44, 66, 88, 110, 132};
  456. int py[7] = {32, 18, 26, 8, 16, 4, 12};
  457. int i;
  458. ngui_text(g, x, top + 3, "fill_rect", g->theme.text_dim);
  459. for (i = 0; i < 5; i++) {
  460. unsigned short colors[5];
  461. struct ngui_rect sw;
  462. colors[0] = g->theme.accent;
  463. colors[1] = g->theme.selection;
  464. colors[2] = g->theme.icon_folder;
  465. colors[3] = g->theme.error;
  466. colors[4] = g->theme.panel_alt;
  467. sw = rect_make(x + label_w + i * 28, top, 22, 22);
  468. ngui_fill_rect(g, sw, colors[i]);
  469. ngui_frame(g, sw, g->theme.border);
  470. }
  471. ngui_text(g, x, top + 37, "frame / inset", g->theme.text_dim);
  472. fr = rect_make(x + label_w, top + 34, 150, 28);
  473. ngui_fill_rect(g, fr, g->theme.panel_alt);
  474. ngui_frame(g, fr, g->theme.border);
  475. inner = fr;
  476. ngui_inset(&inner, 5);
  477. ngui_frame(g, inner, g->theme.accent);
  478. ngui_text(g, x, top + 75, "ngui_line", g->theme.text_dim);
  479. for (i = 0; i < 6; i++)
  480. ngui_line(g, x + label_w + px[i], top + 72 + py[i],
  481. x + label_w + px[i + 1], top + 72 + py[i + 1],
  482. g->theme.accent);
  483. if (right_w < 160)
  484. return;
  485. ngui_text(g, right_x, card.y + 16, "push_clip / pop_clip",
  486. g->theme.text_dim);
  487. {
  488. struct ngui_rect vp = rect_make(right_x, card.y + 36,
  489. right_w, card.h - 52);
  490. ngui_fill_rect(g, vp, g->theme.bg);
  491. ngui_push_clip(g, vp);
  492. for (i = -vp.h; i < vp.w; i += 16) {
  493. unsigned short c = ((i / 16) & 1) ?
  494. g->theme.panel_alt : g->theme.button;
  495. ngui_line(g, vp.x + i, vp.y,
  496. vp.x + i + vp.h, vp.y + vp.h, c);
  497. }
  498. ngui_fill_rect(g, rect_make(vp.x - 20, vp.y + 14, 60, 30),
  499. g->theme.icon_folder);
  500. ngui_text_styled(g, vp.x - 24, vp.y + vp.h / 2 - 24,
  501. "CLIPPED", 7, g->theme.accent, 3, 1);
  502. ngui_pop_clip(g);
  503. ngui_frame(g, vp, g->theme.accent);
  504. }
  505. }
  506. static void draw_theme(struct ngui *g, struct ngui_rect card)
  507. {
  508. unsigned short colors[15];
  509. int colw = (card.w - 2 * CARD_PAD) / 3;
  510. int i;
  511. colors[0] = g->theme.bg;
  512. colors[1] = g->theme.panel;
  513. colors[2] = g->theme.panel_alt;
  514. colors[3] = g->theme.border;
  515. colors[4] = g->theme.text;
  516. colors[5] = g->theme.text_dim;
  517. colors[6] = g->theme.accent;
  518. colors[7] = g->theme.selection;
  519. colors[8] = g->theme.button;
  520. colors[9] = g->theme.button_hot;
  521. colors[10] = g->theme.button_down;
  522. colors[11] = g->theme.icon_folder;
  523. colors[12] = g->theme.icon_file;
  524. colors[13] = g->theme.icon_disk;
  525. colors[14] = g->theme.error;
  526. for (i = 0; i < 15; i++) {
  527. int cx = card.x + CARD_PAD + (i % 3) * colw;
  528. int cy = card.y + 14 + (i / 3) * 22;
  529. struct ngui_rect sw = rect_make(cx, cy, 16, 16);
  530. ngui_fill_rect(g, sw, colors[i]);
  531. ngui_frame(g, sw, g->theme.border);
  532. if (colw > 20)
  533. ngui_text_ellipsis(g, rect_make(cx + 24, cy,
  534. colw - 24, 16),
  535. cx + 24, cy, color_names[i],
  536. g->theme.text);
  537. }
  538. }
  539. static void draw_icons(struct ngui *g, struct ngui_rect card)
  540. {
  541. int cell = (card.w - 2 * CARD_PAD) / NGUI_ICON_COUNT;
  542. int bs = cell >= 56 ? 40 : 30;
  543. int y = card.y + 12;
  544. int i;
  545. char count[24];
  546. unsigned int n = 0;
  547. if (cell < 20)
  548. return;
  549. if (bs > cell - 4)
  550. bs = cell - 4;
  551. for (i = 0; i < NGUI_ICON_COUNT; i++) {
  552. const char *name = ngui_icon_name((enum ngui_icon)i);
  553. int cx = card.x + CARD_PAD + i * cell;
  554. int bx = cx + (cell - bs) / 2;
  555. int tw, tx, ly = y + bs + 4;
  556. struct ngui_rect lr;
  557. if (gallery_icon_button(g, ID_ICON_BASE + (unsigned int)i,
  558. rect_make(bx, y, bs, bs),
  559. (enum ngui_icon)i)) {
  560. char b[64];
  561. unsigned int m = 0;
  562. icon_clicks++;
  563. s_append(b, &m, "icon '", sizeof(b));
  564. s_append(b, &m, name, sizeof(b));
  565. s_append(b, &m, "' - ", sizeof(b));
  566. s_append_uint(b, &m, (unsigned int)icon_clicks,
  567. sizeof(b));
  568. s_append(b, &m, icon_clicks == 1 ?
  569. " click" : " clicks", sizeof(b));
  570. event_set(b);
  571. ngui_invalidate(g);
  572. }
  573. tw = (int)ngui_text_width_ui(g, name);
  574. tx = cx + (cell - tw) / 2;
  575. lr = rect_make(cx + 2, ly, cell - 4, 16);
  576. if (tx < lr.x)
  577. tx = lr.x;
  578. ngui_text_ellipsis(g, lr, tx, ly, name,
  579. g->theme.text_dim);
  580. }
  581. s_append(count, &n, "icon clicks: ", sizeof(count));
  582. s_append_uint(count, &n, (unsigned int)icon_clicks, sizeof(count));
  583. ngui_text_ellipsis(g, rect_make(card.x + CARD_PAD, card.y + 74,
  584. card.w - 2 * CARD_PAD, 16),
  585. card.x + CARD_PAD, card.y + 74,
  586. "Click an icon button - every one is live",
  587. g->theme.text_dim);
  588. if (card.w > 650) {
  589. int cw = (int)ngui_text_width_ui(g, count);
  590. ngui_text(g, card.x + card.w - CARD_PAD - cw,
  591. card.y + 74, count, g->theme.text_dim);
  592. }
  593. }
  594. static void draw_buttons(struct ngui *g, struct ngui_rect card)
  595. {
  596. int x = card.x + CARD_PAD;
  597. int y = card.y + 18;
  598. char num[16];
  599. char buf[32];
  600. unsigned int n;
  601. if (gallery_button(g, ID_BTN_CLICK, rect_make(x, y, 96, 28),
  602. "Click me")) {
  603. char b[64];
  604. unsigned int m = 0;
  605. button_clicks++;
  606. s_append(b, &m, "button clicked - total ", sizeof(b));
  607. s_append_uint(b, &m, (unsigned int)button_clicks, sizeof(b));
  608. event_set(b);
  609. }
  610. if (gallery_button(g, ID_BTN_RESET, rect_make(x + 104, y, 80, 28),
  611. "Reset")) {
  612. button_clicks = 0;
  613. event_set("button counter reset");
  614. }
  615. if (gallery_icon_button(g, ID_ICON_PLUS,
  616. rect_make(x + 192, y, 30, 28),
  617. NGUI_ICON_PLUS)) {
  618. char b[64];
  619. unsigned int m = 0;
  620. button_clicks++;
  621. s_append(b, &m, "icon button '+' - total ", sizeof(b));
  622. s_append_uint(b, &m, (unsigned int)button_clicks, sizeof(b));
  623. event_set(b);
  624. }
  625. if (gallery_icon_button(g, ID_ICON_REFRESH,
  626. rect_make(x + 228, y, 30, 28),
  627. NGUI_ICON_REFRESH)) {
  628. button_clicks = 0;
  629. event_set("button counter reset");
  630. }
  631. if (gallery_icon_button(g, ID_ICON_PREV,
  632. rect_make(x + 264, y, 30, 28),
  633. NGUI_ICON_CHEVRON_LEFT))
  634. event_set("chevron left pressed");
  635. if (gallery_icon_button(g, ID_ICON_NEXT,
  636. rect_make(x + 300, y, 30, 28),
  637. NGUI_ICON_CHEVRON_RIGHT))
  638. event_set("chevron right pressed");
  639. n = 0;
  640. s_append_uint(num, &n, (unsigned int)button_clicks, sizeof(num));
  641. if (card.w > 620) {
  642. int right = card.x + card.w - CARD_PAD;
  643. int vw = (int)ngui_text_width_px(g, num, n, g->font_px * 2, 1, 0, 0);
  644. ngui_text(g, right - 96, card.y + 18, "TOTAL CLICKS",
  645. g->theme.text_dim);
  646. ngui_text_styled(g, right - vw, card.y + 40, num, n,
  647. g->theme.accent, 2, 1);
  648. } else {
  649. unsigned int m = 0;
  650. s_append(buf, &m, "clicks: ", sizeof(buf));
  651. s_append_uint(buf, &m, (unsigned int)button_clicks,
  652. sizeof(buf));
  653. ngui_text(g, x, card.y + 56, buf, g->theme.accent);
  654. }
  655. ngui_text_ellipsis(g, rect_make(x, card.y + 72, card.w - 2 * CARD_PAD,
  656. 16),
  657. x, card.y + 72,
  658. "Buttons fire on release inside; hover lights the raised edge.",
  659. g->theme.text_dim);
  660. ngui_text_ellipsis(g, rect_make(x, card.y + 94, card.w - 2 * CARD_PAD,
  661. 16),
  662. x, card.y + 94,
  663. "Pressed buttons keep the accent fill until you let go.",
  664. g->theme.text_dim);
  665. ngui_text_ellipsis(g, rect_make(x, card.y + 116, card.w - 2 * CARD_PAD,
  666. 16),
  667. x, card.y + 116,
  668. "ngui_button(ui, id, rect, label) / ngui_icon_button(ui, id, rect, icon)",
  669. g->theme.border);
  670. }
  671. static void draw_inputs(struct ngui *g, struct ngui_rect card)
  672. {
  673. int x = card.x + CARD_PAD;
  674. int top = card.y + 16;
  675. int label_w = 126;
  676. int iw = card.w / 2 - 80;
  677. int field_y = top;
  678. int help_x, help_w, help_h;
  679. int result;
  680. int feedback_y = top + 144;
  681. if (iw < 220)
  682. iw = 220;
  683. if (iw > 340)
  684. iw = 340;
  685. ngui_text(g, x, field_y + 4, "Name", g->theme.text_dim);
  686. result = gallery_input(g, ID_IN_NAME,
  687. rect_make(x + label_w, field_y, iw, 24),
  688. &name_in);
  689. note_input(g, result, "name", &name_in);
  690. field_y += 36;
  691. ngui_text(g, x, field_y + 4, "Prefilled", g->theme.text_dim);
  692. result = gallery_input(g, ID_IN_EMAIL,
  693. rect_make(x + label_w, field_y, iw, 24),
  694. &email_in);
  695. note_input(g, result, "prefilled", &email_in);
  696. field_y += 36;
  697. ngui_text(g, x, field_y + 4, "Events", g->theme.text_dim);
  698. result = gallery_input(g, ID_IN_EVENTS,
  699. rect_make(x + label_w, field_y, iw, 24),
  700. &events_in);
  701. note_input(g, result, "events", &events_in);
  702. field_y += 36;
  703. ngui_text(g, x, field_y + 4, "Disabled", g->theme.text_dim);
  704. /* A disabled field ignores clicks and keys, so it never reports an
  705. * event; the value stays in the same proportional face, dimmed. */
  706. gallery_input(g, ID_IN_DISABLED,
  707. rect_make(x + label_w, field_y, iw, 24),
  708. &disabled_in);
  709. help_x = x + label_w + iw + 24;
  710. help_w = card.x + card.w - CARD_PAD - help_x;
  711. help_h = 200;
  712. if (help_w >= 150) {
  713. int hx = help_x;
  714. int hy = top;
  715. ngui_fill_rect(g, rect_make(hx, hy, help_w, help_h),
  716. g->theme.bg);
  717. ngui_frame(g, rect_make(hx, hy, help_w, help_h),
  718. g->theme.border);
  719. ngui_text(g, hx + 10, hy + 9, "TEXT INPUT",
  720. g->theme.accent);
  721. ngui_text(g, hx + 10, hy + 36, "Return - submit",
  722. g->theme.text_dim);
  723. ngui_text(g, hx + 10, hy + 54, "Esc - cancel focus",
  724. g->theme.text_dim);
  725. ngui_text(g, hx + 10, hy + 72, "Arrows - move caret",
  726. g->theme.text_dim);
  727. ngui_text(g, hx + 10, hy + 90, "Ctrl+Left/Right - words",
  728. g->theme.text_dim);
  729. ngui_text(g, hx + 10, hy + 108, "Ctrl+Del/Bksp - words",
  730. g->theme.text_dim);
  731. ngui_text(g, hx + 10, hy + 126, "Drag - select, Ctrl+A all",
  732. g->theme.text_dim);
  733. ngui_text(g, hx + 10, hy + 144, "Home/End, Ctrl+U - ends/clear",
  734. g->theme.text_dim);
  735. ngui_text(g, hx + 10, hy + 162, "Tab - focus first",
  736. g->theme.text_dim);
  737. ngui_text(g, hx + 10, hy + 180, "Disabled - no input",
  738. g->theme.text_dim);
  739. }
  740. if (input_feedback[0])
  741. ngui_text_ellipsis(g, rect_make(x, feedback_y,
  742. (help_w >= 150 ?
  743. help_x - x - 12 :
  744. card.w - 2 * CARD_PAD),
  745. 16),
  746. x, feedback_y, input_feedback,
  747. feedback_error ? g->theme.error :
  748. g->theme.accent);
  749. ngui_text_ellipsis(g, rect_make(x, top + 166,
  750. (help_w >= 150 ?
  751. help_x - x - 12 :
  752. card.w - 2 * CARD_PAD), 16),
  753. x, top + 166,
  754. "Return submits, Esc cancels, Ctrl+U clears.",
  755. g->theme.text_dim);
  756. if (gallery_button(g, ID_BTN_RESET_FIELDS,
  757. rect_make(x, top + 186, 104, 26),
  758. "Reset fields")) {
  759. ngui_input_set(&name_in, "");
  760. ngui_input_set(&email_in, "ada@nebos.org");
  761. ngui_input_set(&events_in, "");
  762. ngui_input_set(&disabled_in, "Read-only value");
  763. ngui_set_focus(g, 0);
  764. s_copy(input_feedback, "Fields reset to their defaults.",
  765. sizeof(input_feedback));
  766. feedback_error = 0;
  767. event_set("input fields reset");
  768. }
  769. if (help_w >= 150)
  770. ngui_text(g, x + 120, top + 192,
  771. "ngui_input_set() rewrites a field.",
  772. g->theme.border);
  773. }
  774. static void draw_scroll(struct ngui *g, struct ngui_rect card)
  775. {
  776. int x = card.x + CARD_PAD;
  777. int top = card.y + 16;
  778. int pane_w = card.w - 2 * CARD_PAD - 190;
  779. int pane_h = card.h - 56;
  780. struct ngui_rect pane;
  781. int help_x, help_w;
  782. int i;
  783. scroll_pane_valid = 0;
  784. if (pane_w > 520)
  785. pane_w = 520;
  786. if (pane_w < 260)
  787. pane_w = 260;
  788. if (card.w < 590)
  789. pane_w = card.w - 2 * CARD_PAD;
  790. if (pane_w < 120)
  791. return;
  792. pane = rect_make(x, card.y + 40, pane_w, pane_h);
  793. scroll_pane_rect = pane;
  794. scroll_pane_valid = 1;
  795. ngui_text_ellipsis(g, rect_make(x, top, card.w - 2 * CARD_PAD, 16),
  796. x, top,
  797. "A nested pane: click a row, drag its scrollbar.",
  798. g->theme.text_dim);
  799. list_scroll.content_h = LIST_ROWS * LIST_ROW_H;
  800. ngui_fill_rect(g, pane, g->theme.bg);
  801. ngui_scroll_begin(g, pane, &list_scroll);
  802. ngui_scroll_wheel(g, pane, &list_scroll);
  803. if (g->pressed && ngui_rect_contains(&pane, g->press_x, g->press_y) &&
  804. g->press_x < pane.x + pane.w - 9) {
  805. int idx = (g->press_y - pane.y + list_scroll.offset) /
  806. LIST_ROW_H;
  807. if (idx >= 0 && idx < LIST_ROWS) {
  808. char b[64];
  809. unsigned int n = 0;
  810. list_selected = idx;
  811. ngui_scroll_ensure_visible(&list_scroll,
  812. idx * LIST_ROW_H,
  813. LIST_ROW_H);
  814. s_append(b, &n, "row ", sizeof(b));
  815. s_append_uint(b, &n, (unsigned int)(idx + 1),
  816. sizeof(b));
  817. s_append(b, &n, " - ", sizeof(b));
  818. s_append(b, &n, list_items[idx], sizeof(b));
  819. event_set(b);
  820. ngui_invalidate(g);
  821. }
  822. }
  823. for (i = 0; i < LIST_ROWS; i++) {
  824. int ry = pane.y + i * LIST_ROW_H - list_scroll.offset;
  825. struct ngui_rect row = rect_make(pane.x, ry, pane.w - 9,
  826. LIST_ROW_H);
  827. char label[64];
  828. unsigned int n = 0;
  829. unsigned short bg;
  830. int hover = g->inside &&
  831. ngui_rect_contains(&pane, g->mouse_x, g->mouse_y) &&
  832. g->mouse_x < pane.x + pane.w - 9 &&
  833. (g->mouse_y - pane.y + list_scroll.offset) /
  834. LIST_ROW_H == i;
  835. if (i == list_selected)
  836. bg = g->theme.selection;
  837. else if (hover)
  838. bg = g->theme.button_hot;
  839. else
  840. bg = (i & 1) ? g->theme.panel : g->theme.bg;
  841. ngui_fill_rect(g, row, bg);
  842. ngui_icon(g, rect_make(pane.x + 8, ry + 4, 16, 16),
  843. (i % 3) == 2 ? NGUI_ICON_DISK : NGUI_ICON_FILE,
  844. i == list_selected ? g->theme.text :
  845. g->theme.icon_file);
  846. s_append_uint(label, &n, (unsigned int)(i + 1), sizeof(label));
  847. if (i + 1 < 10)
  848. s_append(label, &n, " ", sizeof(label));
  849. else
  850. s_append(label, &n, " ", sizeof(label));
  851. s_append(label, &n, list_items[i], sizeof(label));
  852. ngui_text_ellipsis(g, rect_make(pane.x + 32, ry + 4,
  853. pane.w - 9 - 44, 16),
  854. pane.x + 32, ry + 4, label,
  855. i == list_selected ? g->theme.text :
  856. g->theme.text_dim);
  857. }
  858. ngui_scroll_end(g, pane, &list_scroll);
  859. help_x = x + pane_w + 24;
  860. help_w = card.x + card.w - CARD_PAD - help_x;
  861. if (help_w >= 150) {
  862. char sel[32];
  863. unsigned int n = 0;
  864. int hy = card.y + 40;
  865. int hh = pane_h;
  866. ngui_fill_rect(g, rect_make(help_x, hy, help_w, hh),
  867. g->theme.bg);
  868. ngui_frame(g, rect_make(help_x, hy, help_w, hh),
  869. g->theme.border);
  870. ngui_text(g, help_x + 10, hy + 9, "ngui_scroll",
  871. g->theme.accent);
  872. ngui_text(g, help_x + 10, hy + 36, "12 rows x 24 px",
  873. g->theme.text_dim);
  874. ngui_text(g, help_x + 10, hy + 54, "view_h follows pane",
  875. g->theme.text_dim);
  876. ngui_text(g, help_x + 10, hy + 72, "drag thumb or track",
  877. g->theme.text_dim);
  878. ngui_text(g, help_x + 10, hy + 90, "clip hides overflow",
  879. g->theme.text_dim);
  880. ngui_text(g, help_x + 10, hy + 108, "ensure_visible()",
  881. g->theme.text_dim);
  882. n = 0;
  883. s_append(sel, &n, "selected: ", sizeof(sel));
  884. s_append_uint(sel, &n, (unsigned int)(list_selected + 1),
  885. sizeof(sel));
  886. ngui_text(g, help_x + 10, hy + 126, sel,
  887. g->theme.accent);
  888. }
  889. }
  890. static void draw_layout(struct ngui *g, struct ngui_rect card)
  891. {
  892. int x = card.x + CARD_PAD;
  893. int y = card.y + 16;
  894. int ow = card.w - 2 * CARD_PAD;
  895. struct ngui_rect outer, top, bottom, left, right, center;
  896. int hx;
  897. if (ow > 520)
  898. ow = 520;
  899. outer = rect_make(x, y, ow, card.h - 32);
  900. if (outer.w < 200 || outer.h < 80)
  901. return;
  902. top = ngui_cut_top(&outer, 30);
  903. bottom = ngui_cut_bottom(&outer, 30);
  904. left = ngui_cut_left(&outer, 120);
  905. right = ngui_cut_right(&outer, 120);
  906. center = outer;
  907. ngui_fill_rect(g, top, g->theme.selection);
  908. ngui_frame(g, top, g->theme.border);
  909. center_text(g, top, "ngui_cut_top(r, 30)", g->theme.text);
  910. ngui_fill_rect(g, bottom, g->theme.button);
  911. ngui_frame(g, bottom, g->theme.border);
  912. center_text(g, bottom, "ngui_cut_bottom(r, 30)", g->theme.text);
  913. ngui_fill_rect(g, left, g->theme.panel_alt);
  914. ngui_frame(g, left, g->theme.border);
  915. center_text(g, left, "cut_left", g->theme.text);
  916. ngui_fill_rect(g, right, g->theme.panel_alt);
  917. ngui_frame(g, right, g->theme.border);
  918. center_text(g, right, "cut_right", g->theme.text);
  919. ngui_fill_rect(g, center, g->theme.bg);
  920. ngui_frame(g, center, g->theme.border);
  921. center_text(g, center, "leftover rect", g->theme.text_dim);
  922. hx = x + ow + 24;
  923. if (card.x + card.w - CARD_PAD - hx < 150)
  924. return;
  925. ngui_text(g, hx, card.y + 16, "layout helpers",
  926. g->theme.accent);
  927. ngui_text(g, hx, card.y + 40, "ngui_cut_top(r, h)",
  928. g->theme.text_dim);
  929. ngui_text(g, hx, card.y + 58, "ngui_cut_bottom(r, h)",
  930. g->theme.text_dim);
  931. ngui_text(g, hx, card.y + 76, "ngui_cut_left(r, w)",
  932. g->theme.text_dim);
  933. ngui_text(g, hx, card.y + 94, "ngui_cut_right(r, w)",
  934. g->theme.text_dim);
  935. ngui_text(g, hx, card.y + 112, "ngui_inset(r, n)",
  936. g->theme.text_dim);
  937. ngui_text(g, hx, card.y + 136, "Each cut shrinks r and",
  938. g->theme.border);
  939. ngui_text(g, hx, card.y + 154, "returns the cut piece.",
  940. g->theme.border);
  941. }
  942. static void draw_body(struct ngui *g, struct ngui_rect body)
  943. {
  944. int x = body.x + PAGE_PAD;
  945. int w = body.w - PAGE_PAD * 2 - 9;
  946. int i;
  947. page.content_h = sec_content_h;
  948. ngui_fill_rect(g, body, g->theme.bg);
  949. ngui_scroll_begin(g, body, &page);
  950. /* Leave wheel scroll for the nested pane when the pointer is over it;
  951. * the rect comes from the previous frame, before this one redraws it. */
  952. if (!(scroll_pane_valid &&
  953. ngui_rect_contains(&scroll_pane_rect, g->mouse_x, g->mouse_y)))
  954. ngui_scroll_wheel(g, body, &page);
  955. scroll_pane_valid = 0;
  956. for (i = 0; i < SEC_COUNT; i++) {
  957. int sy = body.y - page.offset + sec_y[i];
  958. struct ngui_rect card;
  959. if (sy + SEC_HEAD_H + sec_card_h[i] < body.y - 4 ||
  960. sy > body.y + body.h + 4)
  961. continue;
  962. draw_section_head(g, x, sy, w, i);
  963. card = rect_make(x, sy + SEC_HEAD_H, w, sec_card_h[i]);
  964. switch (i) {
  965. case SEC_TEXT:
  966. draw_typography(g, card);
  967. break;
  968. case SEC_SHAPES:
  969. draw_shapes(g, card);
  970. break;
  971. case SEC_THEME:
  972. draw_theme(g, card);
  973. break;
  974. case SEC_ICONS:
  975. draw_icons(g, card);
  976. break;
  977. case SEC_BUTTONS:
  978. draw_buttons(g, card);
  979. break;
  980. case SEC_INPUTS:
  981. draw_inputs(g, card);
  982. break;
  983. case SEC_SCROLL:
  984. draw_scroll(g, card);
  985. break;
  986. case SEC_LAYOUT:
  987. draw_layout(g, card);
  988. break;
  989. default:
  990. break;
  991. }
  992. }
  993. ngui_scroll_end(g, body, &page);
  994. }
  995. static void draw_status(struct ngui *g, struct ngui_rect r)
  996. {
  997. char left[160];
  998. char right[64];
  999. unsigned int n = 0, m = 0;
  1000. int rw, rx;
  1001. ngui_fill_rect(g, r, g->theme.panel);
  1002. ngui_fill_rect(g, rect_make(r.x, r.y, r.w, 1), g->theme.border);
  1003. if (g->inside) {
  1004. s_append(right, &m, "x ", sizeof(right));
  1005. s_append_uint(right, &m, (unsigned int)g->mouse_x,
  1006. sizeof(right));
  1007. s_append(right, &m, " y ", sizeof(right));
  1008. s_append_uint(right, &m, (unsigned int)g->mouse_y,
  1009. sizeof(right));
  1010. } else {
  1011. s_append(right, &m, "pointer outside", sizeof(right));
  1012. }
  1013. s_append(right, &m, " ", sizeof(right));
  1014. s_append_uint(right, &m, (unsigned int)page_percent(),
  1015. sizeof(right));
  1016. s_append(right, &m, "%", sizeof(right));
  1017. rw = (int)ngui_text_width_ui(g, right);
  1018. rx = r.x + r.w - rw - 10;
  1019. ngui_text(g, rx, r.y + 4, right, g->theme.text_dim);
  1020. if (ngui_focus(&ui)) {
  1021. s_append(left, &n, "editing ", sizeof(left));
  1022. s_append(left, &n, focus_name(), sizeof(left));
  1023. s_append(left, &n, " | ", sizeof(left));
  1024. }
  1025. if (event_text[0])
  1026. s_append(left, &n, event_text, sizeof(left));
  1027. else
  1028. s_append(left, &n, "Ready - every ngui control on one page",
  1029. sizeof(left));
  1030. if (rx - (r.x + 10) > 40)
  1031. ngui_text_ellipsis(g, rect_make(r.x + 10, r.y,
  1032. rx - r.x - 20, r.h),
  1033. r.x + 10, r.y + 4, left, g->theme.text);
  1034. }
  1035. static void draw(struct ngui *g)
  1036. {
  1037. struct ngui_rect all = {0, 0, g->w, g->h};
  1038. struct ngui_rect header, nav, status, body;
  1039. if (g->w < 420 || g->h < 300) {
  1040. ngui_fill_rect(g, all, g->theme.bg);
  1041. ngui_text(g, 16, 16, "The gallery needs a larger window.",
  1042. g->theme.text);
  1043. ngui_text(g, 16, 38, "Resize the window to continue.",
  1044. g->theme.text_dim);
  1045. return;
  1046. }
  1047. header = ngui_cut_top(&all, HEADER_H);
  1048. nav = ngui_cut_top(&all, NAV_H);
  1049. status = ngui_cut_bottom(&all, STATUS_H);
  1050. body = all;
  1051. draw_header(g, header);
  1052. draw_nav(g, nav);
  1053. draw_body(g, body);
  1054. draw_status(g, status);
  1055. }
  1056. static void handle_keys(struct ngui *g)
  1057. {
  1058. int k;
  1059. if (ngui_focus(g)) {
  1060. /* A focused field consumed its keys; Ctrl+C is never consumed. */
  1061. while ((k = ngui_key(g)) != 0)
  1062. if (k == 0x03)
  1063. quit = 1;
  1064. return;
  1065. }
  1066. k = ngui_key_peek(g);
  1067. if (k == NGUI_KEY_PAGE_UP || k == NGUI_KEY_PAGE_DOWN) {
  1068. if (ngui_scroll_keys(g, &page)) {
  1069. event_set(k == NGUI_KEY_PAGE_UP ?
  1070. "page up" : "page down");
  1071. ngui_invalidate(g);
  1072. }
  1073. return;
  1074. }
  1075. while ((k = ngui_key(g)) != 0) {
  1076. switch (k) {
  1077. case 0x03: /* Ctrl+C */
  1078. case NGUI_KEY_ESC:
  1079. quit = 1;
  1080. return;
  1081. case NGUI_KEY_UP:
  1082. ngui_scroll_to(&page, page.offset - 36);
  1083. ngui_invalidate(g);
  1084. break;
  1085. case NGUI_KEY_DOWN:
  1086. ngui_scroll_to(&page, page.offset + 36);
  1087. ngui_invalidate(g);
  1088. break;
  1089. case NGUI_KEY_HOME:
  1090. ngui_scroll_to(&page, 0);
  1091. event_set("jumped to top");
  1092. ngui_invalidate(g);
  1093. break;
  1094. case NGUI_KEY_END:
  1095. ngui_scroll_to(&page, page.content_h - page.view_h);
  1096. event_set("jumped to the end");
  1097. ngui_invalidate(g);
  1098. break;
  1099. case NGUI_KEY_TAB:
  1100. ngui_set_focus(g, ID_IN_NAME);
  1101. ngui_scroll_to(&page, sec_y[SEC_INPUTS]);
  1102. event_set("focused the name field");
  1103. ngui_invalidate(g);
  1104. break;
  1105. default:
  1106. break;
  1107. }
  1108. }
  1109. }
  1110. void _start(const char *arg)
  1111. {
  1112. struct win_info wi;
  1113. (void)arg;
  1114. if (u_win_create(START_W, START_H, "ngui gallery", &wi) != 0) {
  1115. u_puts("gallery: no graphics window\n");
  1116. u_exit(1);
  1117. }
  1118. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width,
  1119. (int)wi.height);
  1120. ngui_font_attach(&ui);
  1121. name_in.placeholder = "Type a name";
  1122. email_in.placeholder = "you@example.com";
  1123. events_in.placeholder = "Type and press Return";
  1124. disabled_in.placeholder = "Cannot edit";
  1125. disabled_in.disabled = 1;
  1126. ngui_input_set(&disabled_in, "Read-only value");
  1127. gallery_layout();
  1128. page.content_h = sec_content_h;
  1129. page.view_h = START_H - HEADER_H - NAV_H - STATUS_H;
  1130. ngui_input_set(&email_in, "ada@nebos.org");
  1131. s_copy(input_feedback, "No events yet - edit a field.",
  1132. sizeof(input_feedback));
  1133. s_copy(event_text, "Ready - every ngui control on one page",
  1134. sizeof(event_text));
  1135. for (;;) {
  1136. int want = ngui_begin(&ui);
  1137. if (quit)
  1138. break;
  1139. if (!want) {
  1140. ngui_end_ex(&ui, 0);
  1141. u_sleep_ms(10);
  1142. continue;
  1143. }
  1144. draw(&ui);
  1145. handle_keys(&ui);
  1146. if (quit)
  1147. break;
  1148. ngui_end(&ui);
  1149. u_sleep_ms(10);
  1150. }
  1151. u_win_close();
  1152. u_exit(0);
  1153. }