gallery.c 36 KB

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