gallery.c 38 KB

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