gallery.c 33 KB

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