gallery.c 41 KB

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