gallery.c 33 KB

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