gallery.c 39 KB

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