ngui.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. /* ngui: immediate-mode widgets for RGB565 canvas windows. See ngui.h. */
  2. #include "ngui.h"
  3. #include "ucmd_lib.h"
  4. #include "font_8x16.h"
  5. const struct ngui_theme ngui_theme_dark = {
  6. .bg = NGUI_RGB(24, 28, 40),
  7. .panel = NGUI_RGB(38, 43, 58),
  8. .panel_alt = NGUI_RGB(50, 56, 74),
  9. .border = NGUI_RGB(92, 99, 124),
  10. .text = NGUI_RGB(228, 230, 240),
  11. .text_dim = NGUI_RGB(145, 151, 170),
  12. .accent = NGUI_RGB(84, 132, 224),
  13. .selection = NGUI_RGB(54, 92, 166),
  14. .button = NGUI_RGB(54, 61, 80),
  15. .button_hot = NGUI_RGB(70, 81, 108),
  16. .button_down = NGUI_RGB(48, 112, 204),
  17. .icon_folder = NGUI_RGB(238, 198, 88),
  18. .icon_file = NGUI_RGB(204, 210, 226),
  19. .icon_disk = NGUI_RGB(112, 174, 232),
  20. .error = NGUI_RGB(238, 116, 116),
  21. };
  22. static unsigned int slen(const char *s)
  23. {
  24. unsigned int n = 0;
  25. while (s && s[n])
  26. n++;
  27. return n;
  28. }
  29. static void scopy(char *dst, const char *src, unsigned int cap)
  30. {
  31. unsigned int i = 0;
  32. if (!cap)
  33. return;
  34. while (src && src[i] && i + 1 < cap) {
  35. dst[i] = src[i];
  36. i++;
  37. }
  38. dst[i] = '\0';
  39. }
  40. static int rect_empty(const struct ngui_rect *r)
  41. {
  42. return r->w <= 0 || r->h <= 0;
  43. }
  44. static int rect_intersect(const struct ngui_rect *a, const struct ngui_rect *b,
  45. struct ngui_rect *out)
  46. {
  47. int x0 = a->x > b->x ? a->x : b->x;
  48. int y0 = a->y > b->y ? a->y : b->y;
  49. int x1 = a->x + a->w < b->x + b->w ? a->x + a->w : b->x + b->w;
  50. int y1 = a->y + a->h < b->y + b->h ? a->y + a->h : b->y + b->h;
  51. out->x = x0;
  52. out->y = y0;
  53. out->w = x1 - x0;
  54. out->h = y1 - y0;
  55. return !rect_empty(out);
  56. }
  57. static struct ngui_rect rect_make(int x, int y, int w, int h)
  58. {
  59. struct ngui_rect r;
  60. r.x = x;
  61. r.y = y;
  62. r.w = w;
  63. r.h = h;
  64. return r;
  65. }
  66. int ngui_rect_contains(const struct ngui_rect *r, int x, int y)
  67. {
  68. return x >= r->x && y >= r->y && x < r->x + r->w && y < r->y + r->h;
  69. }
  70. void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h)
  71. {
  72. unsigned int i;
  73. for (i = 0; i < sizeof(*ui); i++)
  74. ((unsigned char *)ui)[i] = 0;
  75. ui->px = fb;
  76. ui->w = w;
  77. ui->h = h;
  78. ui->theme = ngui_theme_dark;
  79. ui->nclip = 0;
  80. ui->dirty = 1;
  81. ui->caret_on = 1;
  82. ui->last_blink = u_ticks_ms();
  83. ui->motion_ms = ui->last_blink;
  84. ui->last_mouse_x = -1;
  85. ui->last_mouse_y = -1;
  86. }
  87. void ngui_invalidate(struct ngui *ui)
  88. {
  89. ui->dirty = 1;
  90. }
  91. void ngui_set_focus(struct ngui *ui, unsigned int id)
  92. {
  93. if (ui->focus != id) {
  94. ui->focus = id;
  95. ui->last_blink = u_ticks_ms();
  96. ui->caret_on = 1;
  97. ui->dirty = 1;
  98. }
  99. }
  100. unsigned int ngui_focus(struct ngui *ui)
  101. {
  102. return ui->focus;
  103. }
  104. static void key_push(struct ngui *ui, int k)
  105. {
  106. unsigned int next = (ui->key_head + 1) % 32;
  107. if (next == ui->key_tail)
  108. return;
  109. ui->keys[ui->key_head] = k;
  110. ui->key_head = next;
  111. }
  112. int ngui_key_peek(struct ngui *ui)
  113. {
  114. if (ui->key_tail == ui->key_head)
  115. return 0;
  116. return ui->keys[ui->key_tail];
  117. }
  118. int ngui_key(struct ngui *ui)
  119. {
  120. int k = ngui_key_peek(ui);
  121. if (!k)
  122. return 0;
  123. ui->key_tail = (ui->key_tail + 1) % 32;
  124. return k;
  125. }
  126. int ngui_begin(struct ngui *ui)
  127. {
  128. struct win_info wi;
  129. struct win_pointer wp;
  130. struct win_event ev;
  131. unsigned int now;
  132. int want = ui->dirty;
  133. int i, k;
  134. ui->dirty = 0;
  135. ui->pressed = ui->released = 0;
  136. ui->clicked = 0;
  137. ui->focus_claimed = 0;
  138. ui->hot = 0;
  139. if (u_win_info(&wi) == 0) {
  140. if (wi.width != (unsigned int)ui->w ||
  141. wi.height != (unsigned int)ui->h) {
  142. if (u_win_ack_resize() == 0) {
  143. ui->px = (unsigned short *)wi.fb;
  144. ui->w = (int)wi.width;
  145. ui->h = (int)wi.height;
  146. }
  147. want = 1;
  148. } else if (wi.fb && wi.fb != (void *)ui->px) {
  149. ui->px = (unsigned short *)wi.fb;
  150. }
  151. }
  152. if (u_win_pointer(&wp) == 0) {
  153. ui->mouse_x = wp.x;
  154. ui->mouse_y = wp.y;
  155. ui->buttons = wp.buttons;
  156. ui->inside = wp.inside ? 1 : 0;
  157. ui->focused = wp.focused ? 1 : 0;
  158. } else {
  159. ui->mouse_x = ui->mouse_y = 0;
  160. ui->buttons = 0;
  161. ui->inside = 0;
  162. ui->focused = 0;
  163. }
  164. for (i = 0; i < 16; i++) {
  165. int r = u_win_event(&ev);
  166. if (r != 1)
  167. break;
  168. if (ev.type == WIN_EV_POINTER_DOWN) {
  169. ui->pressed |= 1;
  170. ui->press_x = ev.x;
  171. ui->press_y = ev.y;
  172. ui->clicked = 1;
  173. } else if (ev.type == WIN_EV_POINTER_UP) {
  174. ui->released |= 1;
  175. ui->release_x = ev.x;
  176. ui->release_y = ev.y;
  177. }
  178. }
  179. for (i = 0; i < 32; i++) {
  180. k = u_key_poll();
  181. if (!k)
  182. break;
  183. key_push(ui, k);
  184. want = 1;
  185. }
  186. now = u_ticks_ms();
  187. if (ui->focus) {
  188. if (now - ui->last_blink >= 500) {
  189. ui->last_blink = now;
  190. ui->caret_on = !ui->caret_on;
  191. want = 1;
  192. }
  193. } else {
  194. ui->caret_on = 1;
  195. }
  196. if (ui->inside != ui->last_inside) {
  197. want = 1;
  198. ui->motion_pending = 0;
  199. ui->motion_ms = now;
  200. } else if (ui->inside &&
  201. (ui->mouse_x != ui->last_mouse_x ||
  202. ui->mouse_y != ui->last_mouse_y)) {
  203. /* Hover feedback needs a repaint, but not one per mouse
  204. * report: while the pointer keeps moving, redraw at most every
  205. * 33 ms and remember that a final frame is still due. A held
  206. * button is a drag, which stays at full rate. */
  207. if (ui->buttons) {
  208. ui->motion_ms = now;
  209. ui->motion_pending = 0;
  210. want = 1;
  211. } else {
  212. ui->motion_pending = 1;
  213. if ((unsigned int)(now - ui->motion_ms) >=
  214. NGUI_MOTION_REDRAW_MS) {
  215. ui->motion_ms = now;
  216. ui->motion_pending = 0;
  217. want = 1;
  218. }
  219. }
  220. } else if (ui->motion_pending &&
  221. (unsigned int)(now - ui->motion_ms) >= NGUI_MOTION_REDRAW_MS) {
  222. ui->motion_ms = now;
  223. ui->motion_pending = 0;
  224. want = 1;
  225. }
  226. if (ui->focused != ui->last_focused)
  227. want = 1;
  228. ui->last_mouse_x = ui->mouse_x;
  229. ui->last_mouse_y = ui->mouse_y;
  230. ui->last_inside = ui->inside;
  231. ui->last_focused = ui->focused;
  232. if (ui->pressed || ui->released)
  233. want = 1;
  234. if (want)
  235. ui->motion_pending = 0;
  236. return want;
  237. }
  238. void ngui_end_ex(struct ngui *ui, int present)
  239. {
  240. if (present && u_win_present() != 0)
  241. ui->dirty = 1;
  242. if (ui->clicked && !ui->focus_claimed)
  243. ui->focus = 0;
  244. /* Only the release edge ends a press. The sampled button state is not
  245. * synchronized with the event queue: it can read 0 for a frame before
  246. * the release event is delivered. Clearing active on "!buttons" made
  247. * clicks fire only when the two happened to land in the same frame. */
  248. if (ui->released)
  249. ui->active = 0;
  250. ui->pressed = ui->released = 0;
  251. ui->clicked = 0;
  252. ui->focus_claimed = 0;
  253. ui->press_x = ui->press_y = 0;
  254. ui->release_x = ui->release_y = 0;
  255. }
  256. void ngui_end(struct ngui *ui)
  257. {
  258. ngui_end_ex(ui, 1);
  259. }
  260. struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h)
  261. {
  262. struct ngui_rect out = *r;
  263. if (h < 0)
  264. h = 0;
  265. if (h > r->h)
  266. h = r->h;
  267. out.h = h;
  268. r->y += h;
  269. r->h -= h;
  270. return out;
  271. }
  272. struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h)
  273. {
  274. struct ngui_rect out = *r;
  275. if (h < 0)
  276. h = 0;
  277. if (h > r->h)
  278. h = r->h;
  279. out.y = r->y + r->h - h;
  280. out.h = h;
  281. r->h -= h;
  282. return out;
  283. }
  284. struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w)
  285. {
  286. struct ngui_rect out = *r;
  287. if (w < 0)
  288. w = 0;
  289. if (w > r->w)
  290. w = r->w;
  291. out.w = w;
  292. r->x += w;
  293. r->w -= w;
  294. return out;
  295. }
  296. struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w)
  297. {
  298. struct ngui_rect out = *r;
  299. if (w < 0)
  300. w = 0;
  301. if (w > r->w)
  302. w = r->w;
  303. out.x = r->x + r->w - w;
  304. out.w = w;
  305. r->w -= w;
  306. return out;
  307. }
  308. void ngui_inset(struct ngui_rect *r, int n)
  309. {
  310. r->x += n;
  311. r->y += n;
  312. r->w -= n * 2;
  313. r->h -= n * 2;
  314. if (r->w < 0)
  315. r->w = 0;
  316. if (r->h < 0)
  317. r->h = 0;
  318. }
  319. void ngui_push_clip(struct ngui *ui, struct ngui_rect r)
  320. {
  321. struct ngui_rect out;
  322. struct ngui_rect canvas;
  323. if (ui->nclip >= 4)
  324. return;
  325. canvas = rect_make(0, 0, ui->w, ui->h);
  326. if (!rect_intersect(&r, &canvas, &out))
  327. out = rect_make(0, 0, 0, 0);
  328. if (ui->nclip > 0) {
  329. struct ngui_rect clipped = out;
  330. if (!rect_intersect(&clipped, &ui->clip[ui->nclip - 1], &out))
  331. out = rect_make(0, 0, 0, 0);
  332. }
  333. ui->clip[ui->nclip++] = out;
  334. }
  335. void ngui_pop_clip(struct ngui *ui)
  336. {
  337. if (ui->nclip > 0)
  338. ui->nclip--;
  339. }
  340. static int pixel_visible(struct ngui *ui, int x, int y)
  341. {
  342. struct ngui_rect *c;
  343. if (x < 0 || y < 0 || x >= ui->w || y >= ui->h)
  344. return 0;
  345. if (ui->nclip <= 0)
  346. return 1;
  347. c = &ui->clip[ui->nclip - 1];
  348. return x >= c->x && y >= c->y &&
  349. x < c->x + c->w && y < c->y + c->h;
  350. }
  351. void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color)
  352. {
  353. struct ngui_rect canvas;
  354. int x, y;
  355. if (rect_empty(&r))
  356. return;
  357. canvas = rect_make(0, 0, ui->w, ui->h);
  358. if (!rect_intersect(&r, &canvas, &r))
  359. return;
  360. if (ui->nclip > 0 &&
  361. !rect_intersect(&r, &ui->clip[ui->nclip - 1], &r))
  362. return;
  363. for (y = r.y; y < r.y + r.h; y++) {
  364. unsigned short *p = ui->px + y * ui->w + r.x;
  365. for (x = 0; x < r.w; x++)
  366. p[x] = color;
  367. }
  368. }
  369. void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color)
  370. {
  371. if (r.w <= 0 || r.h <= 0)
  372. return;
  373. ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), color);
  374. ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), color);
  375. if (r.h > 2) {
  376. ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), color);
  377. ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
  378. r.h - 2), color);
  379. }
  380. }
  381. static int ngui_isqrt(int v)
  382. {
  383. int r = 0, bit = 1 << 15;
  384. if (v <= 0)
  385. return 0;
  386. while (bit > v)
  387. bit >>= 1;
  388. while (bit) {
  389. if (v >= r + bit) {
  390. v -= r + bit;
  391. r = (r >> 1) + bit;
  392. } else {
  393. r >>= 1;
  394. }
  395. bit >>= 1;
  396. }
  397. return r;
  398. }
  399. /* Horizontal inset of one scanline of a corner with the given radius. */
  400. static int round_inset(int y, int h, int r)
  401. {
  402. int dy, d2;
  403. if (r <= 0)
  404. return 0;
  405. if (y < r)
  406. dy = r - y - 1;
  407. else if (y >= h - r)
  408. dy = y - (h - r);
  409. else
  410. return 0;
  411. d2 = r * r - dy * dy;
  412. if (d2 < 0)
  413. d2 = 0;
  414. return r - ngui_isqrt(d2);
  415. }
  416. void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
  417. int tl, int tr, int br, int bl,
  418. unsigned short color)
  419. {
  420. int y, half;
  421. if (rect_empty(&r))
  422. return;
  423. half = r.w < r.h ? r.w / 2 : r.h / 2;
  424. if (tl > half) tl = half;
  425. if (tr > half) tr = half;
  426. if (br > half) br = half;
  427. if (bl > half) bl = half;
  428. if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
  429. ngui_fill_rect(ui, r, color);
  430. return;
  431. }
  432. for (y = 0; y < r.h; y++) {
  433. int li = 0, ri = 0;
  434. if (y < tl)
  435. li = round_inset(y, r.h, tl);
  436. if (y < tr)
  437. ri = round_inset(y, r.h, tr);
  438. if (y >= r.h - bl) {
  439. int i = round_inset(y, r.h, bl);
  440. if (i > li)
  441. li = i;
  442. }
  443. if (y >= r.h - br) {
  444. int i = round_inset(y, r.h, br);
  445. if (i > ri)
  446. ri = i;
  447. }
  448. if (r.w - li - ri > 0)
  449. ngui_fill_rect(ui, rect_make(r.x + li, r.y + y,
  450. r.w - li - ri, 1), color);
  451. }
  452. }
  453. void ngui_frame_round(struct ngui *ui, struct ngui_rect r,
  454. int tl, int tr, int br, int bl, int thickness,
  455. unsigned short color)
  456. {
  457. int y, half;
  458. if (rect_empty(&r))
  459. return;
  460. if (thickness < 1)
  461. thickness = 1;
  462. half = r.w < r.h ? r.w / 2 : r.h / 2;
  463. if (tl > half) tl = half;
  464. if (tr > half) tr = half;
  465. if (br > half) br = half;
  466. if (bl > half) bl = half;
  467. if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
  468. ngui_frame(ui, r, color);
  469. return;
  470. }
  471. for (y = 0; y < r.h; y++) {
  472. int li = 0, ri = 0;
  473. int x0, x1;
  474. if (y < tl)
  475. li = round_inset(y, r.h, tl);
  476. if (y < tr)
  477. ri = round_inset(y, r.h, tr);
  478. if (y >= r.h - bl) {
  479. int i = round_inset(y, r.h, bl);
  480. if (i > li)
  481. li = i;
  482. }
  483. if (y >= r.h - br) {
  484. int i = round_inset(y, r.h, br);
  485. if (i > ri)
  486. ri = i;
  487. }
  488. x0 = r.x + li;
  489. x1 = r.x + r.w - 1 - ri;
  490. if (x0 > x1)
  491. continue;
  492. if (y < thickness || y >= r.h - thickness) {
  493. ngui_fill_rect(ui, rect_make(x0, r.y + y,
  494. x1 - x0 + 1, 1), color);
  495. } else {
  496. ngui_fill_rect(ui, rect_make(x0, r.y + y,
  497. thickness, 1), color);
  498. ngui_fill_rect(ui, rect_make(x1 - thickness + 1,
  499. r.y + y, thickness, 1), color);
  500. }
  501. }
  502. }
  503. static void ngui_pixel(struct ngui *ui, int x, int y, unsigned short color)
  504. {
  505. if (pixel_visible(ui, x, y))
  506. ui->px[y * ui->w + x] = color;
  507. }
  508. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  509. unsigned short color)
  510. {
  511. int dx = x1 - x0;
  512. int dy = y1 - y0;
  513. int sx = dx < 0 ? -1 : 1;
  514. int sy = dy < 0 ? -1 : 1;
  515. int err, e2;
  516. if (dx < 0)
  517. dx = -dx;
  518. if (dy < 0)
  519. dy = -dy;
  520. dy = -dy;
  521. err = dx + dy;
  522. for (;;) {
  523. ngui_pixel(ui, x0, y0, color);
  524. if (x0 == x1 && y0 == y1)
  525. break;
  526. e2 = err * 2;
  527. if (e2 >= dy) {
  528. err += dy;
  529. x0 += sx;
  530. }
  531. if (e2 <= dx) {
  532. err += dx;
  533. y0 += sy;
  534. }
  535. }
  536. }
  537. static void ngui_glyph(struct ngui *ui, int x, int y, unsigned char c,
  538. unsigned short color, int scale, int bold)
  539. {
  540. const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  541. int row, col, sx, sy;
  542. if (scale < 1)
  543. scale = 1;
  544. for (row = 0; row < FONT_8X16_ROWS; row++) {
  545. unsigned char bits = g[row];
  546. if (!bits)
  547. continue;
  548. for (col = 0; col < 8; col++) {
  549. if (!(bits & (0x80u >> col)))
  550. continue;
  551. for (sy = 0; sy < scale; sy++) {
  552. for (sx = 0; sx < scale; sx++) {
  553. ngui_pixel(ui,
  554. x + col * scale + sx +
  555. (bold && scale == 1 ? 1 : 0),
  556. y + row * scale + sy,
  557. color);
  558. }
  559. }
  560. if (bold && scale > 1)
  561. ngui_pixel(ui, x + col * scale + scale,
  562. y + row * scale, color);
  563. }
  564. }
  565. }
  566. unsigned int ngui_text_width(const char *s)
  567. {
  568. return slen(s) * 8;
  569. }
  570. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale)
  571. {
  572. (void)s;
  573. if (scale < 1)
  574. scale = 1;
  575. return n * 8u * (unsigned int)scale;
  576. }
  577. void ngui_text_n(struct ngui *ui, int x, int y, const char *s, unsigned int n,
  578. unsigned short color)
  579. {
  580. unsigned int i;
  581. for (i = 0; i < n && s && s[i]; i++)
  582. ngui_glyph(ui, x + (int)i * 8, y, (unsigned char)s[i], color, 1, 0);
  583. }
  584. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  585. unsigned short color)
  586. {
  587. ngui_text_n(ui, x, y, s, slen(s), color);
  588. }
  589. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  590. unsigned int n, unsigned short color, int scale, int bold)
  591. {
  592. unsigned int i;
  593. if (scale < 1)
  594. scale = 1;
  595. for (i = 0; i < n && s && s[i]; i++)
  596. ngui_glyph(ui, x + (int)i * 8 * scale, y, (unsigned char)s[i],
  597. color, scale, bold);
  598. }
  599. /* Draw a string upside down. The whole n-character block is rotated, so the
  600. * first character ends up on the right; used for the mirrored corner index on
  601. * a playing card. */
  602. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  603. unsigned int n, unsigned short color)
  604. {
  605. unsigned int i;
  606. int row, col;
  607. for (i = 0; i < n && s && s[i]; i++) {
  608. const unsigned char *g =
  609. &font_8x16[(unsigned int)(unsigned char)s[i] *
  610. FONT_8X16_ROWS];
  611. for (row = 0; row < FONT_8X16_ROWS; row++) {
  612. unsigned char bits = g[row];
  613. if (!bits)
  614. continue;
  615. for (col = 0; col < 8; col++) {
  616. if (!(bits & (0x80u >> col)))
  617. continue;
  618. ngui_pixel(ui,
  619. x + (int)(n - 1 - i) * 8 + 7 - col,
  620. y + FONT_8X16_ROWS - 1 - row,
  621. color);
  622. }
  623. }
  624. }
  625. }
  626. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  627. const char *s, unsigned short color)
  628. {
  629. unsigned int n = slen(s);
  630. int max = (r.x + r.w - x) / 8;
  631. unsigned int i;
  632. if (max <= 0)
  633. return;
  634. if (n <= (unsigned int)max || max < 4) {
  635. for (i = 0; i < n && (int)i < max; i++)
  636. ngui_glyph(ui, x + (int)i * 8, y,
  637. (unsigned char)s[i], color, 1, 0);
  638. return;
  639. }
  640. for (i = 0; i < (unsigned int)(max - 3); i++)
  641. ngui_glyph(ui, x + (int)i * 8, y, (unsigned char)s[i], color, 1, 0);
  642. ngui_glyph(ui, x + (max - 3) * 8, y, '.', color, 1, 0);
  643. ngui_glyph(ui, x + (max - 2) * 8, y, '.', color, 1, 0);
  644. ngui_glyph(ui, x + (max - 1) * 8, y, '.', color, 1, 0);
  645. }
  646. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  647. unsigned short color)
  648. {
  649. int x = r.x + r.w / 2 - 8;
  650. int y = r.y + r.h / 2 - 8;
  651. switch (icon) {
  652. case NGUI_ICON_FOLDER:
  653. ngui_fill_rect(ui, rect_make(x + 1, y + 3, 7, 4), color);
  654. ngui_fill_rect(ui, rect_make(x, y + 6, 16, 9), color);
  655. break;
  656. case NGUI_ICON_FILE:
  657. ngui_frame(ui, rect_make(x + 2, y, 12, 16), color);
  658. ngui_line(ui, x + 10, y, x + 13, y + 3, color);
  659. ngui_fill_rect(ui, rect_make(x + 4, y + 7, 8, 1), color);
  660. ngui_fill_rect(ui, rect_make(x + 4, y + 10, 8, 1), color);
  661. break;
  662. case NGUI_ICON_DISK:
  663. ngui_frame(ui, rect_make(x, y + 2, 16, 12), color);
  664. ngui_fill_rect(ui, rect_make(x + 2, y + 5, 12, 2), color);
  665. ngui_fill_rect(ui, rect_make(x + 11, y + 9, 3, 3), color);
  666. break;
  667. case NGUI_ICON_UP:
  668. ngui_line(ui, x + 8, y + 2, x + 3, y + 8, color);
  669. ngui_line(ui, x + 8, y + 2, x + 13, y + 8, color);
  670. ngui_fill_rect(ui, rect_make(x + 6, y + 7, 5, 7), color);
  671. break;
  672. case NGUI_ICON_HOME:
  673. ngui_line(ui, x + 2, y + 8, x + 8, y + 2, color);
  674. ngui_line(ui, x + 8, y + 2, x + 14, y + 8, color);
  675. ngui_frame(ui, rect_make(x + 3, y + 8, 10, 7), color);
  676. ngui_fill_rect(ui, rect_make(x + 7, y + 11, 3, 4), color);
  677. break;
  678. case NGUI_ICON_REFRESH:
  679. ngui_line(ui, x + 3, y + 8, x + 6, y + 4, color);
  680. ngui_line(ui, x + 6, y + 4, x + 10, y + 3, color);
  681. ngui_line(ui, x + 10, y + 3, x + 13, y + 6, color);
  682. ngui_line(ui, x + 13, y + 6, x + 13, y + 9, color);
  683. ngui_fill_rect(ui, rect_make(x + 11, y + 1, 4, 4), color);
  684. break;
  685. case NGUI_ICON_PLUS:
  686. ngui_fill_rect(ui, rect_make(x + 7, y + 3, 2, 10), color);
  687. ngui_fill_rect(ui, rect_make(x + 3, y + 7, 10, 2), color);
  688. break;
  689. case NGUI_ICON_CHEVRON_LEFT:
  690. ngui_line(ui, x + 10, y + 3, x + 6, y + 8, color);
  691. ngui_line(ui, x + 6, y + 8, x + 10, y + 13, color);
  692. break;
  693. case NGUI_ICON_CHEVRON_RIGHT:
  694. ngui_line(ui, x + 6, y + 3, x + 10, y + 8, color);
  695. ngui_line(ui, x + 10, y + 8, x + 6, y + 13, color);
  696. break;
  697. default:
  698. break;
  699. }
  700. }
  701. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  702. const char *label)
  703. {
  704. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  705. int press_hot = ui->pressed &&
  706. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  707. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  708. int fired = 0;
  709. unsigned short bg;
  710. int tw, tx, ty;
  711. if (press_hot)
  712. ui->active = id;
  713. if (ui->pressed && ui->released && press_hot && release_hot)
  714. fired = 1;
  715. if (ui->released && ui->active == id) {
  716. if (release_hot)
  717. fired = 1;
  718. ui->active = 0;
  719. }
  720. if (ui->active == id)
  721. bg = ui->theme.button_down;
  722. else if (hot)
  723. bg = ui->theme.button_hot;
  724. else
  725. bg = ui->theme.button;
  726. ngui_fill_rect(ui, r, bg);
  727. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  728. if (label && label[0]) {
  729. tw = (int)ngui_text_width(label);
  730. tx = r.x + (r.w - tw) / 2;
  731. ty = r.y + (r.h - 16) / 2;
  732. ngui_push_clip(ui, r);
  733. ngui_text(ui, tx, ty, label, ui->theme.text);
  734. ngui_pop_clip(ui);
  735. }
  736. if (hot)
  737. ui->hot = id;
  738. return fired;
  739. }
  740. /* One row of a drop-down menu: left-aligned label, optional check mark and
  741. * right-aligned shortcut. Uses the same press/release/active semantics as
  742. * ngui_button so a menu and its caller never disagree about a click. */
  743. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  744. const char *label, const char *shortcut, int checked)
  745. {
  746. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  747. int press_hot = ui->pressed &&
  748. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  749. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  750. int fired = 0;
  751. int selected;
  752. unsigned short bg, fg;
  753. int ty;
  754. if (press_hot)
  755. ui->active = id;
  756. if (ui->pressed && ui->released && press_hot && release_hot)
  757. fired = 1;
  758. if (ui->released && ui->active == id) {
  759. if (release_hot)
  760. fired = 1;
  761. ui->active = 0;
  762. }
  763. selected = (ui->active == id) || hot;
  764. bg = selected ? ui->theme.selection : ui->theme.panel;
  765. fg = selected ? NGUI_RGB(255, 255, 255) : ui->theme.text;
  766. ngui_fill_rect(ui, r, bg);
  767. ty = r.y + (r.h - 16) / 2;
  768. ngui_push_clip(ui, r);
  769. if (checked) {
  770. ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
  771. ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
  772. }
  773. if (label)
  774. ngui_text(ui, r.x + 22, ty, label, fg);
  775. if (shortcut) {
  776. int sw = (int)ngui_text_width(shortcut);
  777. ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
  778. }
  779. ngui_pop_clip(ui);
  780. if (hot)
  781. ui->hot = id;
  782. return fired;
  783. }
  784. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  785. enum ngui_icon icon){
  786. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  787. int press_hot = ui->pressed &&
  788. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  789. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  790. int fired = 0;
  791. unsigned short bg;
  792. if (press_hot)
  793. ui->active = id;
  794. if (ui->pressed && ui->released && press_hot && release_hot)
  795. fired = 1;
  796. if (ui->released && ui->active == id) {
  797. if (release_hot)
  798. fired = 1;
  799. ui->active = 0;
  800. }
  801. if (ui->active == id)
  802. bg = ui->theme.button_down;
  803. else if (hot)
  804. bg = ui->theme.button_hot;
  805. else
  806. bg = ui->theme.button;
  807. ngui_fill_rect(ui, r, bg);
  808. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  809. ngui_icon(ui, r, icon, ui->theme.text);
  810. if (hot)
  811. ui->hot = id;
  812. return fired;
  813. }
  814. void ngui_input_set(struct ngui_input *in, const char *s)
  815. {
  816. scopy(in->text, s, NGUI_INPUT_MAX);
  817. in->len = slen(in->text);
  818. in->cursor = in->len;
  819. in->scroll = 0;
  820. in->anchor = in->cursor;
  821. in->dragging = 0;
  822. in->last_click_ms = 0;
  823. }
  824. static int input_word_char(char c)
  825. {
  826. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  827. (c >= '0' && c <= '9') || c == '_';
  828. }
  829. /* Next word start after `pos` (Windows edit-control semantics). */
  830. static unsigned int input_word_next(const struct ngui_input *in,
  831. unsigned int pos)
  832. {
  833. while (pos < in->len && input_word_char(in->text[pos]))
  834. pos++;
  835. while (pos < in->len && !input_word_char(in->text[pos]))
  836. pos++;
  837. return pos;
  838. }
  839. /* Start of the previous word before `pos`. */
  840. static unsigned int input_word_prev(const struct ngui_input *in,
  841. unsigned int pos)
  842. {
  843. while (pos > 0 && !input_word_char(in->text[pos - 1]))
  844. pos--;
  845. while (pos > 0 && input_word_char(in->text[pos - 1]))
  846. pos--;
  847. return pos;
  848. }
  849. /* End of the word to the right of `pos`. A caret inside a word takes the
  850. * rest of that word and its trailing separators; a caret on separators takes
  851. * them plus the next word. */
  852. static unsigned int input_word_delete_end(const struct ngui_input *in,
  853. unsigned int pos)
  854. {
  855. if (pos < in->len && input_word_char(in->text[pos])) {
  856. while (pos < in->len && input_word_char(in->text[pos]))
  857. pos++;
  858. while (pos < in->len && !input_word_char(in->text[pos]))
  859. pos++;
  860. return pos;
  861. }
  862. while (pos < in->len && !input_word_char(in->text[pos]))
  863. pos++;
  864. while (pos < in->len && input_word_char(in->text[pos]))
  865. pos++;
  866. return pos;
  867. }
  868. static void input_sel_bounds(const struct ngui_input *in,
  869. unsigned int *a, unsigned int *b)
  870. {
  871. if (in->anchor < in->cursor) {
  872. *a = in->anchor;
  873. *b = in->cursor;
  874. } else {
  875. *a = in->cursor;
  876. *b = in->anchor;
  877. }
  878. }
  879. /* Remove [a, b) and keep cursor/anchor consistent. Returns 1 when the text
  880. * changed. */
  881. static int input_delete_range(struct ngui_input *in, unsigned int a,
  882. unsigned int b)
  883. {
  884. unsigned int i;
  885. if (a >= b || b > in->len)
  886. return 0;
  887. for (i = b; i < in->len; i++)
  888. in->text[i - (b - a)] = in->text[i];
  889. in->len -= b - a;
  890. in->text[in->len] = '\0';
  891. if (in->cursor >= b)
  892. in->cursor -= b - a;
  893. else if (in->cursor > a)
  894. in->cursor = a;
  895. if (in->anchor >= b)
  896. in->anchor -= b - a;
  897. else if (in->anchor > a)
  898. in->anchor = a;
  899. return 1;
  900. }
  901. static int input_delete_selection(struct ngui_input *in)
  902. {
  903. unsigned int a, b;
  904. input_sel_bounds(in, &a, &b);
  905. return input_delete_range(in, a, b);
  906. }
  907. static void input_insert(struct ngui_input *in, char c)
  908. {
  909. unsigned int i;
  910. input_delete_selection(in);
  911. if (in->len + 1 >= NGUI_INPUT_MAX)
  912. return;
  913. i = in->len;
  914. while (i > in->cursor) {
  915. in->text[i] = in->text[i - 1];
  916. i--;
  917. }
  918. in->text[in->cursor] = c;
  919. in->cursor++;
  920. in->len++;
  921. in->text[in->len] = '\0';
  922. in->anchor = in->cursor;
  923. }
  924. static void input_delete(struct ngui_input *in, int back)
  925. {
  926. unsigned int pos;
  927. if (back) {
  928. if (in->cursor == 0)
  929. return;
  930. pos = in->cursor - 1;
  931. } else {
  932. if (in->cursor >= in->len)
  933. return;
  934. pos = in->cursor;
  935. }
  936. input_delete_range(in, pos, pos + 1);
  937. }
  938. static void input_move_to(struct ngui_input *in, unsigned int pos)
  939. {
  940. if (pos > in->len)
  941. pos = in->len;
  942. in->cursor = pos;
  943. in->anchor = pos;
  944. }
  945. /* Two presses in the same field within this window select all of its text. */
  946. #define NGUI_DOUBLE_CLICK_MS 500
  947. /* Character position under a content-relative x, clamped to the visible
  948. * part of the field. */
  949. static unsigned int input_pos_at(const struct ngui_input *in, int inner_x,
  950. int cols, int x)
  951. {
  952. int col = (x - inner_x) / 8;
  953. unsigned int pos;
  954. if (col < 0)
  955. col = 0;
  956. if (cols > 0 && col > cols)
  957. col = cols;
  958. pos = in->scroll + (unsigned int)col;
  959. if (pos > in->len)
  960. pos = in->len;
  961. return pos;
  962. }
  963. static void input_draw_selection(struct ngui *ui, const struct ngui_input *in,
  964. struct ngui_rect inner, int cols)
  965. {
  966. unsigned int a, b, va, vb;
  967. input_sel_bounds(in, &a, &b);
  968. if (a == b || cols <= 0)
  969. return;
  970. va = a < in->scroll ? in->scroll : a;
  971. vb = b > in->scroll + (unsigned int)cols
  972. ? in->scroll + (unsigned int)cols : b;
  973. if (va < vb)
  974. ngui_fill_rect(ui, rect_make(inner.x + (int)(va - in->scroll) * 8,
  975. inner.y, (int)(vb - va) * 8, 16),
  976. ui->theme.selection);
  977. }
  978. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  979. struct ngui_input *in)
  980. {
  981. struct ngui_rect inner;
  982. int focused = (ui->focus == id);
  983. int result = 0;
  984. int cols, k;
  985. int changed = 0;
  986. inner = rect_make(r.x + 4, r.y + (r.h - 16) / 2, r.w - 8, 16);
  987. if (inner.w < 0)
  988. inner.w = 0;
  989. cols = inner.w / 8;
  990. if (cols < 0)
  991. cols = 0;
  992. if (ui->pressed &&
  993. ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
  994. unsigned int now = u_ticks_ms();
  995. int dbl = in->last_click_ms &&
  996. (unsigned int)(now - in->last_click_ms) <=
  997. NGUI_DOUBLE_CLICK_MS;
  998. if (dbl) {
  999. /* Double click: select the whole text and keep it
  1000. * selected while the second press is held. */
  1001. in->anchor = 0;
  1002. in->cursor = in->len;
  1003. in->dragging = 0;
  1004. } else {
  1005. unsigned int pos = input_pos_at(in, inner.x, cols,
  1006. ui->press_x);
  1007. in->cursor = pos;
  1008. in->anchor = pos;
  1009. in->dragging = 1;
  1010. }
  1011. in->last_click_ms = now;
  1012. ui->focus = id;
  1013. ui->focus_claimed = 1;
  1014. ui->last_blink = now;
  1015. ui->caret_on = 1;
  1016. focused = 1;
  1017. } else if (ui->pressed) {
  1018. in->dragging = 0;
  1019. }
  1020. /* Extend the selection while the button that started in this field is
  1021. * still held. Sampled coordinates may be outside the canvas. The
  1022. * release *event* is authoritative: the sampled button state can lag
  1023. * the event queue by a frame. */
  1024. if (in->dragging && (ui->buttons & 1) && !ui->released) {
  1025. in->cursor = input_pos_at(in, inner.x, cols, ui->mouse_x);
  1026. /* Keep redrawing when the pointer leaves the window. */
  1027. ngui_invalidate(ui);
  1028. }
  1029. if (ui->released)
  1030. in->dragging = 0;
  1031. if (focused) {
  1032. while ((k = ngui_key(ui)) != 0) {
  1033. if (k == '\n' || k == '\r') {
  1034. result |= NGUI_INPUT_SUBMIT;
  1035. break;
  1036. }
  1037. if (k == NGUI_KEY_ESC || k == NGUI_KEY_TAB) {
  1038. result |= NGUI_INPUT_CANCEL;
  1039. ui->focus = 0;
  1040. break;
  1041. }
  1042. if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
  1043. if (!input_delete_selection(in))
  1044. input_delete(in, 1);
  1045. changed = 1;
  1046. } else if (k == NGUI_KEY_DELETE) {
  1047. if (!input_delete_selection(in))
  1048. input_delete(in, 0);
  1049. changed = 1;
  1050. } else if (k == NGUI_KEY_CTRL_BACKSPACE) {
  1051. if (input_delete_selection(in))
  1052. changed = 1;
  1053. else
  1054. changed = input_delete_range(in,
  1055. input_word_prev(in, in->cursor),
  1056. in->cursor);
  1057. } else if (k == NGUI_KEY_CTRL_DELETE) {
  1058. if (input_delete_selection(in)) {
  1059. changed = 1;
  1060. } else {
  1061. unsigned int a = in->cursor;
  1062. unsigned int b = input_word_delete_end(in,
  1063. a);
  1064. /* At the end of the line, delete the word
  1065. * before the caret instead of doing
  1066. * nothing. */
  1067. if (b == a) {
  1068. a = input_word_prev(in, in->cursor);
  1069. b = in->cursor;
  1070. }
  1071. changed = input_delete_range(in, a, b);
  1072. }
  1073. } else if (k == NGUI_KEY_LEFT) {
  1074. input_move_to(in, in->cursor > 0 ? in->cursor - 1 : 0);
  1075. } else if (k == NGUI_KEY_RIGHT) {
  1076. input_move_to(in, in->cursor < in->len
  1077. ? in->cursor + 1 : in->len);
  1078. } else if (k == NGUI_KEY_CTRL_LEFT) {
  1079. input_move_to(in, input_word_prev(in, in->cursor));
  1080. } else if (k == NGUI_KEY_CTRL_RIGHT) {
  1081. input_move_to(in, input_word_next(in, in->cursor));
  1082. } else if (k == NGUI_KEY_HOME) {
  1083. input_move_to(in, 0);
  1084. } else if (k == NGUI_KEY_END) {
  1085. input_move_to(in, in->len);
  1086. } else if (k == 0x01) { /* Ctrl+A: select all */
  1087. in->anchor = 0;
  1088. in->cursor = in->len;
  1089. } else if (k == 0x15) { /* Ctrl+U */
  1090. in->len = 0;
  1091. in->cursor = 0;
  1092. in->anchor = 0;
  1093. in->text[0] = '\0';
  1094. changed = 1;
  1095. } else if (k >= 0x20 && k <= 0x7E) {
  1096. input_insert(in, (char)k);
  1097. changed = 1;
  1098. }
  1099. }
  1100. }
  1101. ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
  1102. ngui_frame(ui, r, focused ? ui->theme.accent : ui->theme.border);
  1103. if (in->scroll > in->len)
  1104. in->scroll = in->len;
  1105. if (in->cursor < in->scroll)
  1106. in->scroll = in->cursor;
  1107. if (cols > 0 && in->cursor >= in->scroll + (unsigned int)cols)
  1108. in->scroll = in->cursor - (unsigned int)cols + 1;
  1109. if (in->scroll > in->len)
  1110. in->scroll = in->len;
  1111. ngui_push_clip(ui, inner);
  1112. if (in->len == 0 && in->placeholder) {
  1113. ngui_text(ui, inner.x, inner.y, in->placeholder,
  1114. ui->theme.text_dim);
  1115. } else {
  1116. if (focused)
  1117. input_draw_selection(ui, in, inner, cols);
  1118. ngui_text(ui, inner.x, inner.y, in->text + in->scroll,
  1119. ui->theme.text);
  1120. }
  1121. if (focused && ui->caret_on) {
  1122. int col = (int)in->cursor - (int)in->scroll;
  1123. if (col >= 0 && col <= cols)
  1124. ngui_fill_rect(ui, rect_make(inner.x + col * 8, inner.y,
  1125. 1, 16), ui->theme.accent);
  1126. }
  1127. ngui_pop_clip(ui);
  1128. if (changed)
  1129. result |= NGUI_INPUT_CHANGED;
  1130. return result;
  1131. }
  1132. static int scrollable(const struct ngui_scroll *s)
  1133. {
  1134. return s->content_h > s->view_h && s->view_h > 0;
  1135. }
  1136. static int scroll_thumb_h(const struct ngui_rect *r,
  1137. const struct ngui_scroll *s)
  1138. {
  1139. int h = 0;
  1140. if (s->content_h > 0)
  1141. h = r->h * s->view_h / s->content_h;
  1142. if (h < 12)
  1143. h = 12;
  1144. if (h > r->h)
  1145. h = r->h;
  1146. return h;
  1147. }
  1148. static int scroll_thumb_y(const struct ngui_rect *r,
  1149. const struct ngui_scroll *s)
  1150. {
  1151. int th = scroll_thumb_h(r, s);
  1152. int avail = r->h - th;
  1153. int max = s->content_h - s->view_h;
  1154. if (avail <= 0 || max <= 0)
  1155. return r->y;
  1156. return r->y + s->offset * avail / max;
  1157. }
  1158. static void scroll_clamp(struct ngui_scroll *s)
  1159. {
  1160. int max = s->content_h - s->view_h;
  1161. if (max < 0)
  1162. max = 0;
  1163. if (s->offset > max)
  1164. s->offset = max;
  1165. if (s->offset < 0)
  1166. s->offset = 0;
  1167. }
  1168. void ngui_scroll_to(struct ngui_scroll *s, int offset)
  1169. {
  1170. s->offset = offset;
  1171. scroll_clamp(s);
  1172. }
  1173. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
  1174. {
  1175. if (y < s->offset)
  1176. s->offset = y;
  1177. else if (y + h > s->offset + s->view_h)
  1178. s->offset = y + h - s->view_h;
  1179. scroll_clamp(s);
  1180. }
  1181. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  1182. struct ngui_scroll *s)
  1183. {
  1184. struct ngui_rect bar;
  1185. int scrolls = scrollable(s);
  1186. s->view_h = r.h;
  1187. scroll_clamp(s);
  1188. if (!ui->buttons)
  1189. s->dragging = 0;
  1190. if (scrolls) {
  1191. int th = scroll_thumb_h(&r, s);
  1192. int ty = scroll_thumb_y(&r, s);
  1193. bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1194. if (ui->pressed &&
  1195. ngui_rect_contains(&bar, ui->press_x, ui->press_y)) {
  1196. if (ui->press_y >= ty && ui->press_y < ty + th) {
  1197. s->dragging = 1;
  1198. s->drag_dy = ui->press_y - ty;
  1199. } else {
  1200. int rel = ui->press_y - r.y - th / 2;
  1201. int avail = r.h - th;
  1202. int max = s->content_h - s->view_h;
  1203. if (rel < 0)
  1204. rel = 0;
  1205. if (rel > avail)
  1206. rel = avail;
  1207. if (avail > 0)
  1208. s->offset = rel * max / avail;
  1209. s->dragging = 1;
  1210. s->drag_dy = th / 2;
  1211. scroll_clamp(s);
  1212. }
  1213. }
  1214. /* Sampled buttons are authoritative while held: some event paths
  1215. * deliver the press late or coalesce it with the release. Start
  1216. * the drag from the current pointer position as well. */
  1217. if (!s->dragging && (ui->buttons & 1) &&
  1218. ngui_rect_contains(&bar, ui->mouse_x, ui->mouse_y)) {
  1219. if (ui->mouse_y >= ty && ui->mouse_y < ty + th) {
  1220. s->dragging = 1;
  1221. s->drag_dy = ui->mouse_y - ty;
  1222. } else {
  1223. int rel = ui->mouse_y - r.y - th / 2;
  1224. int avail = r.h - th;
  1225. int max = s->content_h - s->view_h;
  1226. if (rel < 0)
  1227. rel = 0;
  1228. if (rel > avail)
  1229. rel = avail;
  1230. if (avail > 0)
  1231. s->offset = rel * max / avail;
  1232. s->dragging = 1;
  1233. s->drag_dy = th / 2;
  1234. scroll_clamp(s);
  1235. }
  1236. }
  1237. if (s->dragging && (ui->buttons & 1)) {
  1238. int rel = ui->mouse_y - r.y - s->drag_dy;
  1239. int avail = r.h - th;
  1240. int max = s->content_h - s->view_h;
  1241. if (rel < 0)
  1242. rel = 0;
  1243. if (rel > avail)
  1244. rel = avail;
  1245. if (avail > 0)
  1246. s->offset = rel * max / avail;
  1247. scroll_clamp(s);
  1248. }
  1249. if (ui->released)
  1250. s->dragging = 0;
  1251. }
  1252. ngui_push_clip(ui, r);
  1253. }
  1254. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  1255. struct ngui_scroll *s)
  1256. {
  1257. ngui_pop_clip(ui);
  1258. if (!scrollable(s))
  1259. return;
  1260. {
  1261. struct ngui_rect bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1262. int th = scroll_thumb_h(&r, s);
  1263. int ty = scroll_thumb_y(&r, s);
  1264. struct ngui_rect thumb = rect_make(bar.x, ty, 14, th);
  1265. ngui_fill_rect(ui, bar, ui->theme.panel_alt);
  1266. ngui_fill_rect(ui, thumb, s->dragging ?
  1267. ui->theme.accent : ui->theme.text_dim);
  1268. }
  1269. }
  1270. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s)
  1271. {
  1272. int k = ngui_key_peek(ui);
  1273. int page;
  1274. if (k != NGUI_KEY_PAGE_UP && k != NGUI_KEY_PAGE_DOWN)
  1275. return 0;
  1276. page = s->view_h - 22;
  1277. if (page < 22)
  1278. page = 22;
  1279. ngui_key(ui);
  1280. if (k == NGUI_KEY_PAGE_UP)
  1281. s->offset -= page;
  1282. else
  1283. s->offset += page;
  1284. scroll_clamp(s);
  1285. return 1;
  1286. }