gallery.c 35 KB

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