gallery.c 34 KB

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