ngui.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  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. #include "ufont.h"
  6. const struct ngui_theme ngui_theme_dark = {
  7. .bg = NGUI_RGB(24, 28, 40),
  8. .panel = NGUI_RGB(38, 43, 58),
  9. .panel_alt = NGUI_RGB(50, 56, 74),
  10. .border = NGUI_RGB(92, 99, 124),
  11. .text = NGUI_RGB(228, 230, 240),
  12. .text_dim = NGUI_RGB(145, 151, 170),
  13. .accent = NGUI_RGB(84, 132, 224),
  14. .selection = NGUI_RGB(54, 92, 166),
  15. .button = NGUI_RGB(54, 61, 80),
  16. .button_hot = NGUI_RGB(70, 81, 108),
  17. .button_down = NGUI_RGB(48, 112, 204),
  18. .icon_folder = NGUI_RGB(238, 198, 88),
  19. .icon_file = NGUI_RGB(204, 210, 226),
  20. .icon_disk = NGUI_RGB(112, 174, 232),
  21. .error = NGUI_RGB(238, 116, 116),
  22. .success = NGUI_RGB(110, 210, 130),
  23. .warning = NGUI_RGB(238, 198, 88),
  24. };
  25. static unsigned int slen(const char *s)
  26. {
  27. unsigned int n = 0;
  28. while (s && s[n])
  29. n++;
  30. return n;
  31. }
  32. static void scopy(char *dst, const char *src, unsigned int cap)
  33. {
  34. unsigned int i = 0;
  35. if (!cap)
  36. return;
  37. while (src && src[i] && i + 1 < cap) {
  38. dst[i] = src[i];
  39. i++;
  40. }
  41. dst[i] = '\0';
  42. }
  43. static int rect_empty(const struct ngui_rect *r)
  44. {
  45. return r->w <= 0 || r->h <= 0;
  46. }
  47. static int rect_intersect(const struct ngui_rect *a, const struct ngui_rect *b,
  48. struct ngui_rect *out)
  49. {
  50. int x0 = a->x > b->x ? a->x : b->x;
  51. int y0 = a->y > b->y ? a->y : b->y;
  52. int x1 = a->x + a->w < b->x + b->w ? a->x + a->w : b->x + b->w;
  53. int y1 = a->y + a->h < b->y + b->h ? a->y + a->h : b->y + b->h;
  54. out->x = x0;
  55. out->y = y0;
  56. out->w = x1 - x0;
  57. out->h = y1 - y0;
  58. return !rect_empty(out);
  59. }
  60. static struct ngui_rect rect_make(int x, int y, int w, int h)
  61. {
  62. struct ngui_rect r;
  63. r.x = x;
  64. r.y = y;
  65. r.w = w;
  66. r.h = h;
  67. return r;
  68. }
  69. int ngui_rect_contains(const struct ngui_rect *r, int x, int y)
  70. {
  71. return x >= r->x && y >= r->y && x < r->x + r->w && y < r->y + r->h;
  72. }
  73. void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h)
  74. {
  75. unsigned int i;
  76. for (i = 0; i < sizeof(*ui); i++)
  77. ((unsigned char *)ui)[i] = 0;
  78. ui->px = fb;
  79. ui->w = w;
  80. ui->h = h;
  81. ui->theme = ngui_theme_dark;
  82. ui->font_px = 14;
  83. ui->nclip = 0;
  84. ui->dirty = 1;
  85. ui->caret_on = 1;
  86. ui->last_blink = u_ticks_ms();
  87. ui->motion_ms = ui->last_blink;
  88. ui->last_mouse_x = -1;
  89. ui->last_mouse_y = -1;
  90. }
  91. void ngui_font_attach(struct ngui *ui)
  92. {
  93. if (!ui || ui->font)
  94. return;
  95. ui->font_px = 14;
  96. ui->font = ufont_face_open_path("/ramfs/fonts/sans.ttf");
  97. if (!ui->font)
  98. return;
  99. ui->font_bold = ufont_face_open_path("/ramfs/fonts/sans-b.ttf");
  100. ui->font_italic = ufont_face_open_path("/ramfs/fonts/sans-i.ttf");
  101. ui->font_bold_italic = ufont_face_open_path("/ramfs/fonts/sans-bi.ttf");
  102. ui->font_mono = ufont_face_open_path("/ramfs/fonts/mono.ttf");
  103. if (!ui->font_bold)
  104. ui->font_bold = ui->font;
  105. if (!ui->font_italic)
  106. ui->font_italic = ui->font;
  107. if (!ui->font_bold_italic)
  108. ui->font_bold_italic = ui->font_bold;
  109. }
  110. void ngui_font_detach(struct ngui *ui)
  111. {
  112. if (!ui)
  113. return;
  114. ui->font = 0;
  115. ui->font_bold = 0;
  116. ui->font_italic = 0;
  117. ui->font_bold_italic = 0;
  118. ui->font_mono = 0;
  119. ui->font_emoji = 0;
  120. ui->emoji_loaded = 0;
  121. ufont_reset();
  122. }
  123. /* Emoji fallback is opened on first use so apps that never draw UTF-8 text
  124. * do not pay for it. ufont_face_open_path() may clear the glyph cache, which
  125. * is safe here: callers never keep a glyph pointer across this call. */
  126. static struct ufont_face *ngui_emoji_face(struct ngui *ui)
  127. {
  128. if (!ui)
  129. return 0;
  130. if (!ui->emoji_loaded) {
  131. ui->emoji_loaded = 1;
  132. ui->font_emoji = ufont_face_open_path("/ramfs/fonts/emoji.ttf");
  133. }
  134. return ui->font_emoji;
  135. }
  136. void ngui_invalidate(struct ngui *ui)
  137. {
  138. ui->dirty = 1;
  139. }
  140. void ngui_set_focus(struct ngui *ui, unsigned int id)
  141. {
  142. if (ui->focus != id) {
  143. ui->focus = id;
  144. ui->last_blink = u_ticks_ms();
  145. ui->caret_on = 1;
  146. ui->dirty = 1;
  147. }
  148. }
  149. unsigned int ngui_focus(struct ngui *ui)
  150. {
  151. return ui->focus;
  152. }
  153. static void key_push(struct ngui *ui, int k)
  154. {
  155. unsigned int next = (ui->key_head + 1) % 32;
  156. if (next == ui->key_tail)
  157. return;
  158. ui->keys[ui->key_head] = k;
  159. ui->key_head = next;
  160. }
  161. int ngui_key_peek(struct ngui *ui)
  162. {
  163. if (ui->key_tail == ui->key_head)
  164. return 0;
  165. return ui->keys[ui->key_tail];
  166. }
  167. int ngui_key(struct ngui *ui)
  168. {
  169. int k = ngui_key_peek(ui);
  170. if (!k)
  171. return 0;
  172. ui->key_tail = (ui->key_tail + 1) % 32;
  173. return k;
  174. }
  175. int ngui_begin(struct ngui *ui)
  176. {
  177. struct win_info wi;
  178. struct win_pointer wp;
  179. struct win_scroll ws;
  180. struct win_event ev;
  181. unsigned int now;
  182. int want = ui->dirty;
  183. int i, k;
  184. ui->dirty = 0;
  185. ui->pressed = ui->released = 0;
  186. ui->clicked = 0;
  187. ui->focus_claimed = 0;
  188. ui->hot = 0;
  189. if (u_win_info(&wi) == 0) {
  190. if (wi.width != (unsigned int)ui->w ||
  191. wi.height != (unsigned int)ui->h) {
  192. if (u_win_ack_resize() == 0) {
  193. ui->px = (unsigned short *)wi.fb;
  194. ui->w = (int)wi.width;
  195. ui->h = (int)wi.height;
  196. }
  197. want = 1;
  198. } else if (wi.fb && wi.fb != (void *)ui->px) {
  199. ui->px = (unsigned short *)wi.fb;
  200. }
  201. }
  202. if (u_win_pointer(&wp) == 0) {
  203. ui->mouse_x = wp.x;
  204. ui->mouse_y = wp.y;
  205. ui->buttons = wp.buttons;
  206. ui->inside = wp.inside ? 1 : 0;
  207. ui->focused = wp.focused ? 1 : 0;
  208. } else {
  209. ui->mouse_x = ui->mouse_y = 0;
  210. ui->buttons = 0;
  211. ui->inside = 0;
  212. ui->focused = 0;
  213. }
  214. if (u_win_scroll(&ws) == 1) {
  215. ui->scroll_x = ws.dx;
  216. ui->scroll_y = ws.dy;
  217. if (ws.dx || ws.dy)
  218. want = 1;
  219. } else {
  220. ui->scroll_x = ui->scroll_y = 0;
  221. }
  222. for (i = 0; i < 16; i++) {
  223. int r = u_win_event(&ev);
  224. if (r != 1)
  225. break;
  226. if (ev.type == WIN_EV_POINTER_DOWN) {
  227. ui->pressed |= 1;
  228. ui->press_x = ev.x;
  229. ui->press_y = ev.y;
  230. ui->clicked = 1;
  231. } else if (ev.type == WIN_EV_POINTER_UP) {
  232. ui->released |= 1;
  233. ui->release_x = ev.x;
  234. ui->release_y = ev.y;
  235. }
  236. }
  237. for (i = 0; i < 32; i++) {
  238. k = u_key_poll();
  239. if (!k)
  240. break;
  241. key_push(ui, k);
  242. want = 1;
  243. }
  244. now = u_ticks_ms();
  245. if (ui->focus) {
  246. if (now - ui->last_blink >= 500) {
  247. ui->last_blink = now;
  248. ui->caret_on = !ui->caret_on;
  249. want = 1;
  250. }
  251. } else {
  252. ui->caret_on = 1;
  253. }
  254. if (ui->inside != ui->last_inside) {
  255. want = 1;
  256. ui->motion_pending = 0;
  257. ui->motion_ms = now;
  258. } else if (ui->inside &&
  259. (ui->mouse_x != ui->last_mouse_x ||
  260. ui->mouse_y != ui->last_mouse_y)) {
  261. /* Hover feedback needs a repaint, but not one per mouse
  262. * report: while the pointer keeps moving, redraw at most every
  263. * 33 ms and remember that a final frame is still due. A held
  264. * button is a drag, which stays at full rate. */
  265. if (ui->buttons) {
  266. ui->motion_ms = now;
  267. ui->motion_pending = 0;
  268. want = 1;
  269. } else {
  270. ui->motion_pending = 1;
  271. if ((unsigned int)(now - ui->motion_ms) >=
  272. NGUI_MOTION_REDRAW_MS) {
  273. ui->motion_ms = now;
  274. ui->motion_pending = 0;
  275. want = 1;
  276. }
  277. }
  278. } else if (ui->motion_pending &&
  279. (unsigned int)(now - ui->motion_ms) >= NGUI_MOTION_REDRAW_MS) {
  280. ui->motion_ms = now;
  281. ui->motion_pending = 0;
  282. want = 1;
  283. }
  284. if (ui->focused != ui->last_focused)
  285. want = 1;
  286. ui->last_mouse_x = ui->mouse_x;
  287. ui->last_mouse_y = ui->mouse_y;
  288. ui->last_inside = ui->inside;
  289. ui->last_focused = ui->focused;
  290. if (ui->pressed || ui->released)
  291. want = 1;
  292. if (want)
  293. ui->motion_pending = 0;
  294. ui->cursor_frame = want;
  295. if (want)
  296. ui->cursor = CURSOR_DEFAULT;
  297. return want;
  298. }
  299. void ngui_set_cursor(struct ngui *ui, int shape)
  300. {
  301. ui->cursor = shape;
  302. }
  303. int ngui_cursor_rect(struct ngui *ui, struct ngui_rect r, int shape)
  304. {
  305. if (!ui->inside || !ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y))
  306. return 0;
  307. if (ui->nclip > 0 &&
  308. !ngui_rect_contains(&ui->clip[ui->nclip - 1], ui->mouse_x,
  309. ui->mouse_y))
  310. return 0;
  311. ui->cursor = shape;
  312. return 1;
  313. }
  314. void ngui_end_ex(struct ngui *ui, int present)
  315. {
  316. if (present && u_win_present() != 0)
  317. ui->dirty = 1;
  318. /* Only a redrawn frame has evaluated every widget's hover state. */
  319. if (ui->cursor_frame && ui->cursor != ui->cursor_sent &&
  320. u_win_set_cursor(ui->cursor) == 0)
  321. ui->cursor_sent = ui->cursor;
  322. ui->cursor_frame = 0;
  323. if (ui->clicked && !ui->focus_claimed)
  324. ui->focus = 0;
  325. /* Only the release edge ends a press. The sampled button state is not
  326. * synchronized with the event queue: it can read 0 for a frame before
  327. * the release event is delivered. Clearing active on "!buttons" made
  328. * clicks fire only when the two happened to land in the same frame. */
  329. if (ui->released)
  330. ui->active = 0;
  331. ui->pressed = ui->released = 0;
  332. ui->clicked = 0;
  333. ui->focus_claimed = 0;
  334. ui->press_x = ui->press_y = 0;
  335. ui->release_x = ui->release_y = 0;
  336. }
  337. void ngui_end(struct ngui *ui)
  338. {
  339. ngui_end_ex(ui, 1);
  340. }
  341. struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h)
  342. {
  343. struct ngui_rect out = *r;
  344. if (h < 0)
  345. h = 0;
  346. if (h > r->h)
  347. h = r->h;
  348. out.h = h;
  349. r->y += h;
  350. r->h -= h;
  351. return out;
  352. }
  353. struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h)
  354. {
  355. struct ngui_rect out = *r;
  356. if (h < 0)
  357. h = 0;
  358. if (h > r->h)
  359. h = r->h;
  360. out.y = r->y + r->h - h;
  361. out.h = h;
  362. r->h -= h;
  363. return out;
  364. }
  365. struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w)
  366. {
  367. struct ngui_rect out = *r;
  368. if (w < 0)
  369. w = 0;
  370. if (w > r->w)
  371. w = r->w;
  372. out.w = w;
  373. r->x += w;
  374. r->w -= w;
  375. return out;
  376. }
  377. struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w)
  378. {
  379. struct ngui_rect out = *r;
  380. if (w < 0)
  381. w = 0;
  382. if (w > r->w)
  383. w = r->w;
  384. out.x = r->x + r->w - w;
  385. out.w = w;
  386. r->w -= w;
  387. return out;
  388. }
  389. void ngui_inset(struct ngui_rect *r, int n)
  390. {
  391. r->x += n;
  392. r->y += n;
  393. r->w -= n * 2;
  394. r->h -= n * 2;
  395. if (r->w < 0)
  396. r->w = 0;
  397. if (r->h < 0)
  398. r->h = 0;
  399. }
  400. void ngui_push_clip(struct ngui *ui, struct ngui_rect r)
  401. {
  402. struct ngui_rect out;
  403. struct ngui_rect canvas;
  404. if (ui->nclip >= 4)
  405. return;
  406. canvas = rect_make(0, 0, ui->w, ui->h);
  407. if (!rect_intersect(&r, &canvas, &out))
  408. out = rect_make(0, 0, 0, 0);
  409. if (ui->nclip > 0) {
  410. struct ngui_rect clipped = out;
  411. if (!rect_intersect(&clipped, &ui->clip[ui->nclip - 1], &out))
  412. out = rect_make(0, 0, 0, 0);
  413. }
  414. ui->clip[ui->nclip++] = out;
  415. }
  416. void ngui_pop_clip(struct ngui *ui)
  417. {
  418. if (ui->nclip > 0)
  419. ui->nclip--;
  420. }
  421. static int pixel_visible(struct ngui *ui, int x, int y)
  422. {
  423. struct ngui_rect *c;
  424. if (x < 0 || y < 0 || x >= ui->w || y >= ui->h)
  425. return 0;
  426. if (ui->nclip <= 0)
  427. return 1;
  428. c = &ui->clip[ui->nclip - 1];
  429. return x >= c->x && y >= c->y &&
  430. x < c->x + c->w && y < c->y + c->h;
  431. }
  432. void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color)
  433. {
  434. struct ngui_rect canvas;
  435. int x, y;
  436. if (rect_empty(&r))
  437. return;
  438. canvas = rect_make(0, 0, ui->w, ui->h);
  439. if (!rect_intersect(&r, &canvas, &r))
  440. return;
  441. if (ui->nclip > 0 &&
  442. !rect_intersect(&r, &ui->clip[ui->nclip - 1], &r))
  443. return;
  444. for (y = r.y; y < r.y + r.h; y++) {
  445. unsigned short *p = ui->px + y * ui->w + r.x;
  446. for (x = 0; x < r.w; x++)
  447. p[x] = color;
  448. }
  449. }
  450. void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color)
  451. {
  452. if (r.w <= 0 || r.h <= 0)
  453. return;
  454. ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), color);
  455. ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), color);
  456. if (r.h > 2) {
  457. ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), color);
  458. ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
  459. r.h - 2), color);
  460. }
  461. }
  462. /* Brighten (step > 0) or darken (step < 0) an RGB565 color for the
  463. * one-pixel bevel. Green has six bits, red and blue five, so the green
  464. * component moves twice as far to keep the shade visually even. */
  465. static unsigned short shade565(unsigned short c, int step)
  466. {
  467. int r = (int)((c >> 11) & 0x1F) + step;
  468. int g = (int)((c >> 5) & 0x3F) + step * 2;
  469. int b = (int)(c & 0x1F) + step;
  470. if (r < 0)
  471. r = 0;
  472. if (r > 0x1F)
  473. r = 0x1F;
  474. if (g < 0)
  475. g = 0;
  476. if (g > 0x3F)
  477. g = 0x3F;
  478. if (b < 0)
  479. b = 0;
  480. if (b > 0x1F)
  481. b = 0x1F;
  482. return (unsigned short)((r << 11) | (g << 5) | b);
  483. }
  484. /* A one-pixel two-tone edge: `tl` along the top and left, `br` along the
  485. * bottom and right. A raised widget lights its top/left and shades its
  486. * bottom/right; an inset one (a text field) swaps the tones. */
  487. static void bevel_rect(struct ngui *ui, struct ngui_rect r,
  488. unsigned short tl, unsigned short br)
  489. {
  490. if (r.w <= 0 || r.h <= 0)
  491. return;
  492. ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), tl);
  493. ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), br);
  494. if (r.h > 2) {
  495. ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), tl);
  496. ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
  497. r.h - 2), br);
  498. }
  499. }
  500. /* Raised button edge: lit on the top/left, shaded on the bottom/right. A
  501. * pressed button inverts the bevel, so it reads as pushed in. The hovered
  502. * button keeps its accent on the lit edge. */
  503. static void button_bevel(struct ngui *ui, struct ngui_rect r,
  504. unsigned short bg, int hot, int down)
  505. {
  506. if (down)
  507. bevel_rect(ui, r, shade565(bg, -4), shade565(bg, 4));
  508. else if (hot)
  509. bevel_rect(ui, r, ui->theme.accent, shade565(bg, -4));
  510. else
  511. bevel_rect(ui, r, shade565(bg, 4), shade565(bg, -4));
  512. }
  513. static int ngui_isqrt(int v)
  514. {
  515. int r = 0, bit = 1 << 30;
  516. if (v <= 0)
  517. return 0;
  518. /* Classic shift-and-subtract sqrt: bit must be the highest power
  519. * of four that fits in v, and each step consumes two bits. */
  520. while (bit > v)
  521. bit >>= 2;
  522. while (bit) {
  523. if (v >= r + bit) {
  524. v -= r + bit;
  525. r = (r >> 1) + bit;
  526. } else {
  527. r >>= 1;
  528. }
  529. bit >>= 2;
  530. }
  531. return r;
  532. }
  533. /* Horizontal inset of one scanline of a corner with the given radius.
  534. * Samples the pixel centre, so a corner is as wide as it is tall. */
  535. static int round_inset(int y, int h, int r)
  536. {
  537. int dy2, d2;
  538. if (r <= 0)
  539. return 0;
  540. if (y < r)
  541. dy2 = 2 * r - 2 * y - 1;
  542. else if (y >= h - r)
  543. dy2 = 2 * (y - (h - r)) + 1;
  544. else
  545. return 0;
  546. /* d2 = (2r)^2 - dy2^2 = 4 * (r^2 - dy^2). The inset is
  547. * r - floor(sqrt(d2) / 2 + 0.5), i.e. r - (isqrt(d2) + 1) / 2.
  548. * d2 is 3 or 7 mod 8, never a perfect square, so the rounded
  549. * square root is exact. */
  550. d2 = 4 * r * r - dy2 * dy2;
  551. if (d2 < 0)
  552. d2 = 0;
  553. return r - (ngui_isqrt(d2) + 1) / 2;
  554. }
  555. void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
  556. int tl, int tr, int br, int bl,
  557. unsigned short color)
  558. {
  559. int y, half;
  560. if (rect_empty(&r))
  561. return;
  562. half = r.w < r.h ? r.w / 2 : r.h / 2;
  563. if (tl > half) tl = half;
  564. if (tr > half) tr = half;
  565. if (br > half) br = half;
  566. if (bl > half) bl = half;
  567. if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
  568. ngui_fill_rect(ui, r, color);
  569. return;
  570. }
  571. for (y = 0; y < r.h; y++) {
  572. int li = 0, ri = 0;
  573. if (y < tl)
  574. li = round_inset(y, r.h, tl);
  575. if (y < tr)
  576. ri = round_inset(y, r.h, tr);
  577. if (y >= r.h - bl) {
  578. int i = round_inset(y, r.h, bl);
  579. if (i > li)
  580. li = i;
  581. }
  582. if (y >= r.h - br) {
  583. int i = round_inset(y, r.h, br);
  584. if (i > ri)
  585. ri = i;
  586. }
  587. if (r.w - li - ri > 0)
  588. ngui_fill_rect(ui, rect_make(r.x + li, r.y + y,
  589. r.w - li - ri, 1), color);
  590. }
  591. }
  592. void ngui_frame_round(struct ngui *ui, struct ngui_rect r,
  593. int tl, int tr, int br, int bl, int thickness,
  594. unsigned short color)
  595. {
  596. int y, half;
  597. if (rect_empty(&r))
  598. return;
  599. if (thickness < 1)
  600. thickness = 1;
  601. half = r.w < r.h ? r.w / 2 : r.h / 2;
  602. if (tl > half) tl = half;
  603. if (tr > half) tr = half;
  604. if (br > half) br = half;
  605. if (bl > half) bl = half;
  606. if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
  607. ngui_frame(ui, r, color);
  608. return;
  609. }
  610. for (y = 0; y < r.h; y++) {
  611. int li = 0, ri = 0;
  612. int x0, x1;
  613. if (y < tl)
  614. li = round_inset(y, r.h, tl);
  615. if (y < tr)
  616. ri = round_inset(y, r.h, tr);
  617. if (y >= r.h - bl) {
  618. int i = round_inset(y, r.h, bl);
  619. if (i > li)
  620. li = i;
  621. }
  622. if (y >= r.h - br) {
  623. int i = round_inset(y, r.h, br);
  624. if (i > ri)
  625. ri = i;
  626. }
  627. x0 = r.x + li;
  628. x1 = r.x + r.w - 1 - ri;
  629. if (x0 > x1)
  630. continue;
  631. if (y < thickness || y >= r.h - thickness) {
  632. ngui_fill_rect(ui, rect_make(x0, r.y + y,
  633. x1 - x0 + 1, 1), color);
  634. } else {
  635. ngui_fill_rect(ui, rect_make(x0, r.y + y,
  636. thickness, 1), color);
  637. ngui_fill_rect(ui, rect_make(x1 - thickness + 1,
  638. r.y + y, thickness, 1), color);
  639. }
  640. }
  641. }
  642. static void ngui_pixel(struct ngui *ui, int x, int y, unsigned short color)
  643. {
  644. if (pixel_visible(ui, x, y))
  645. ui->px[y * ui->w + x] = color;
  646. }
  647. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  648. unsigned short color)
  649. {
  650. int dx = x1 - x0;
  651. int dy = y1 - y0;
  652. int sx = dx < 0 ? -1 : 1;
  653. int sy = dy < 0 ? -1 : 1;
  654. int err, e2;
  655. if (dx < 0)
  656. dx = -dx;
  657. if (dy < 0)
  658. dy = -dy;
  659. dy = -dy;
  660. err = dx + dy;
  661. for (;;) {
  662. ngui_pixel(ui, x0, y0, color);
  663. if (x0 == x1 && y0 == y1)
  664. break;
  665. e2 = err * 2;
  666. if (e2 >= dy) {
  667. err += dy;
  668. x0 += sx;
  669. }
  670. if (e2 <= dx) {
  671. err += dx;
  672. y0 += sy;
  673. }
  674. }
  675. }
  676. static void ngui_bitmap_glyph(struct ngui *ui, int x, int y, unsigned char c,
  677. unsigned short color, int scale, int bold)
  678. {
  679. const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  680. int row, col, sx, sy;
  681. if (scale < 1)
  682. scale = 1;
  683. for (row = 0; row < FONT_8X16_ROWS; row++) {
  684. unsigned char bits = g[row];
  685. if (!bits)
  686. continue;
  687. for (col = 0; col < 8; col++) {
  688. if (!(bits & (0x80u >> col)))
  689. continue;
  690. for (sy = 0; sy < scale; sy++) {
  691. for (sx = 0; sx < scale; sx++) {
  692. ngui_pixel(ui,
  693. x + col * scale + sx +
  694. (bold && scale == 1 ? 1 : 0),
  695. y + row * scale + sy,
  696. color);
  697. }
  698. }
  699. if (bold && scale > 1)
  700. ngui_pixel(ui, x + col * scale + scale,
  701. y + row * scale, color);
  702. }
  703. }
  704. }
  705. static struct ufont_face *ngui_face(struct ngui *ui, int bold, int italic,
  706. int mono)
  707. {
  708. struct ufont_face *f;
  709. if (!ui)
  710. return 0;
  711. if (mono && ui->font_mono)
  712. f = ui->font_mono;
  713. else if (bold && italic)
  714. f = ui->font_bold_italic;
  715. else if (bold)
  716. f = ui->font_bold;
  717. else if (italic)
  718. f = ui->font_italic;
  719. else
  720. f = ui->font;
  721. if (!f)
  722. f = ui->font;
  723. return f;
  724. }
  725. static void ngui_clip_bounds(struct ngui *ui, int *cx, int *cy,
  726. int *cw, int *ch)
  727. {
  728. struct ngui_rect r = { 0, 0, ui->w, ui->h };
  729. int i;
  730. for (i = 0; i < ui->nclip; i++) {
  731. struct ngui_rect out;
  732. if (!rect_intersect(&r, &ui->clip[i], &out)) {
  733. r.w = r.h = 0;
  734. break;
  735. }
  736. r = out;
  737. }
  738. *cx = r.x;
  739. *cy = r.y;
  740. *cw = r.w;
  741. *ch = r.h;
  742. }
  743. int ngui_text_ascent_face(struct ngui *ui, struct ufont_face *face, int px,
  744. int mono)
  745. {
  746. struct ufont_face *f = face ? face : ngui_face(ui, 0, 0, mono);
  747. struct ufont_metrics m;
  748. if (f) {
  749. ufont_metrics(f, px, &m);
  750. return m.ascent;
  751. }
  752. return 16 * (px >= 24 ? 2 : 1);
  753. }
  754. int ngui_text_ascent(struct ngui *ui, int px, int mono)
  755. {
  756. return ngui_text_ascent_face(ui, 0, px, mono);
  757. }
  758. int ngui_text_line_height_face(struct ngui *ui, struct ufont_face *face,
  759. int px, int mono)
  760. {
  761. struct ufont_face *f = face ? face : ngui_face(ui, 0, 0, mono);
  762. struct ufont_metrics m;
  763. if (f) {
  764. ufont_metrics(f, px, &m);
  765. return m.line_height;
  766. }
  767. return 16 * (px >= 24 ? 2 : 1);
  768. }
  769. int ngui_text_line_height(struct ngui *ui, int px, int mono)
  770. {
  771. return ngui_text_line_height_face(ui, 0, px, mono);
  772. }
  773. unsigned int ngui_text_width_face(struct ngui *ui, struct ufont_face *face,
  774. const char *s, unsigned int n, int px,
  775. int bold, int italic, int mono)
  776. {
  777. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  778. int scale;
  779. if (f)
  780. return (unsigned int)ufont_text_width(f, px, s, (int)n, 0);
  781. scale = px >= 24 ? 2 : 1;
  782. return n * 8u * (unsigned int)scale;
  783. }
  784. unsigned int ngui_text_width_px(struct ngui *ui, const char *s, unsigned int n,
  785. int px, int bold, int italic, int mono)
  786. {
  787. return ngui_text_width_face(ui, 0, s, n, px, bold, italic, mono);
  788. }
  789. unsigned int ngui_text_width_ui(struct ngui *ui, const char *s)
  790. {
  791. return ngui_text_width_px(ui, s, slen(s),
  792. ui ? ui->font_px : 14, 0, 0, 0);
  793. }
  794. unsigned int ngui_text_width(const char *s)
  795. {
  796. return slen(s) * 8;
  797. }
  798. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale)
  799. {
  800. (void)s;
  801. if (scale < 1)
  802. scale = 1;
  803. return n * 8u * (unsigned int)scale;
  804. }
  805. static void ngui_text_px_common(struct ngui *ui, struct ufont_face *face,
  806. int x, int y, const char *s, unsigned int n,
  807. unsigned short color, int px, int bold,
  808. int italic, int mono)
  809. {
  810. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  811. int scale, i, pen = 0, ascent;
  812. int cx, cy, cw, ch;
  813. if (!ui || !ui->px || !s)
  814. return;
  815. if (!f) {
  816. scale = px >= 24 ? 2 : 1;
  817. for (i = 0; i < (int)n && s[i]; i++)
  818. ngui_bitmap_glyph(ui, x + i * 8 * scale, y,
  819. (unsigned char)s[i], color,
  820. scale, bold);
  821. return;
  822. }
  823. ascent = ngui_text_ascent_face(ui, face, px, mono);
  824. ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
  825. for (i = 0; i < (int)n && s[i]; i++) {
  826. const struct ufont_glyph *g =
  827. ufont_glyph(f, px, (unsigned char)s[i]);
  828. if (!g) {
  829. pen += 8 << 6;
  830. continue;
  831. }
  832. ufont_blit_rgb565(ui->px, ui->w, ui->h,
  833. x + (pen >> 6), y + ascent, g, color,
  834. cx, cy, cw, ch);
  835. pen += g->advance;
  836. }
  837. }
  838. void ngui_text_px(struct ngui *ui, int x, int y, const char *s,
  839. unsigned int n, unsigned short color, int px, int bold,
  840. int italic, int mono)
  841. {
  842. ngui_text_px_common(ui, 0, x, y, s, n, color, px, bold, italic, mono);
  843. }
  844. /* ---- UTF-8 text with an emoji fallback face --------------------------- */
  845. static int utf8_skip_cp(unsigned int cp)
  846. {
  847. return cp == 0x200B || cp == 0x200C || cp == 0x200D ||
  848. cp == 0xFE0E || cp == 0xFE0F;
  849. }
  850. /* Advance of one codepoint in 26.6 pixels, choosing the primary face, then
  851. * the lazily loaded emoji face, then '?'. */
  852. static int ngui_utf8_advance(struct ngui *ui, struct ufont_face *f,
  853. int px, unsigned int cp)
  854. {
  855. struct ufont_face *emoji;
  856. if (utf8_skip_cp(cp) || !f)
  857. return 0;
  858. if (ufont_has_glyph(f, cp))
  859. return ufont_advance_cp(f, px, cp);
  860. emoji = ngui_emoji_face(ui);
  861. if (emoji && ufont_has_glyph(emoji, cp))
  862. return ufont_advance_cp(emoji, px, cp);
  863. return ufont_advance_cp(f, px, '?');
  864. }
  865. static unsigned int ngui_text_width_face_utf8(struct ngui *ui,
  866. struct ufont_face *face,
  867. const char *s, unsigned int n,
  868. int px, int bold, int italic,
  869. int mono)
  870. {
  871. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  872. int i = 0, total = 0;
  873. if (!f)
  874. return n * 8u * (px >= 24 ? 2u : 1u);
  875. while (i < (int)n) {
  876. unsigned int cp = ufont_utf8_next(s, &i, (int)n);
  877. total += ngui_utf8_advance(ui, f, px, cp);
  878. }
  879. return (unsigned int)((total + 32) >> 6);
  880. }
  881. static const struct ufont_glyph *ngui_utf8_glyph(struct ngui *ui,
  882. struct ufont_face *f,
  883. int px, unsigned int cp,
  884. struct ufont_face **used)
  885. {
  886. struct ufont_face *emoji;
  887. const struct ufont_glyph *g;
  888. *used = f;
  889. if (utf8_skip_cp(cp) || !f)
  890. return 0;
  891. if (ufont_has_glyph(f, cp)) {
  892. g = ufont_glyph(f, px, cp);
  893. if (g)
  894. return g;
  895. }
  896. emoji = ngui_emoji_face(ui);
  897. if (emoji && ufont_has_glyph(emoji, cp)) {
  898. g = ufont_glyph(emoji, px, cp);
  899. if (g) {
  900. *used = emoji;
  901. return g;
  902. }
  903. }
  904. *used = f;
  905. return ufont_glyph(f, px, '?');
  906. }
  907. static void ngui_text_px_utf8_common(struct ngui *ui, struct ufont_face *face,
  908. int x, int y, const char *s,
  909. unsigned int n, unsigned short color,
  910. int px, int bold, int italic, int mono)
  911. {
  912. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  913. int i = 0, pen = 0, ascent;
  914. int cx, cy, cw, ch;
  915. if (!ui || !ui->px || !s)
  916. return;
  917. if (!f) {
  918. ngui_text_px_common(ui, face, x, y, s, n, color, px, bold,
  919. italic, mono);
  920. return;
  921. }
  922. ascent = ngui_text_ascent_face(ui, face, px, mono);
  923. ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
  924. while (i < (int)n) {
  925. unsigned int cp = ufont_utf8_next(s, &i, (int)n);
  926. struct ufont_face *used;
  927. const struct ufont_glyph *g;
  928. if (utf8_skip_cp(cp))
  929. continue;
  930. g = ngui_utf8_glyph(ui, f, px, cp, &used);
  931. if (!g) {
  932. pen += 8 << 6;
  933. continue;
  934. }
  935. ufont_blit_rgb565(ui->px, ui->w, ui->h,
  936. x + (pen >> 6), y + ascent, g, color,
  937. cx, cy, cw, ch);
  938. pen += g->advance;
  939. }
  940. }
  941. void ngui_text_n_utf8(struct ngui *ui, int x, int y, const char *s,
  942. unsigned int n, unsigned short color)
  943. {
  944. ngui_text_px_utf8_common(ui, 0, x, y, s, n, color,
  945. ui ? ui->font_px : 14, 0, 0, 0);
  946. }
  947. void ngui_text_utf8(struct ngui *ui, int x, int y, const char *s,
  948. unsigned short color)
  949. {
  950. ngui_text_n_utf8(ui, x, y, s, s ? slen(s) : 0, color);
  951. }
  952. unsigned int ngui_text_width_utf8(struct ngui *ui, const char *s)
  953. {
  954. return ngui_text_width_face_utf8(ui, 0, s, s ? slen(s) : 0,
  955. ui ? ui->font_px : 14, 0, 0, 0);
  956. }
  957. void ngui_text_ellipsis_utf8(struct ngui *ui, struct ngui_rect r,
  958. int x, int y, const char *s,
  959. unsigned short color)
  960. {
  961. unsigned int n = s ? slen(s) : 0;
  962. unsigned int end = 0, i = 0;
  963. int avail = r.x + r.w - x;
  964. int dots, width;
  965. if (!ui || !s)
  966. return;
  967. if (avail <= 0)
  968. return;
  969. width = (int)ngui_text_width_utf8(ui, s);
  970. if (width <= avail) {
  971. ngui_text_utf8(ui, x, y, s, color);
  972. return;
  973. }
  974. dots = (int)ngui_text_width_utf8(ui, "...");
  975. if (dots >= avail)
  976. return;
  977. while (i < n) {
  978. int next = i;
  979. (void)ufont_utf8_next(s, &next, (int)n);
  980. if ((int)ngui_text_width_face_utf8(ui, 0, s, (unsigned int)next,
  981. ui->font_px, 0, 0, 0) +
  982. dots > avail)
  983. break;
  984. i = (unsigned int)next;
  985. end = i;
  986. }
  987. ngui_text_n_utf8(ui, x, y, s, end, color);
  988. ngui_text_utf8(ui, x + (int)ngui_text_width_face_utf8(
  989. ui, 0, s, end, ui->font_px, 0, 0, 0),
  990. y, "...", color);
  991. }
  992. void ngui_text_px_face(struct ngui *ui, struct ufont_face *face, int x,
  993. int y, const char *s, unsigned int n,
  994. unsigned short color, int px, int bold, int italic,
  995. int mono)
  996. {
  997. ngui_text_px_common(ui, face, x, y, s, n, color, px, bold, italic,
  998. mono);
  999. }
  1000. void ngui_text_n(struct ngui *ui, int x, int y, const char *s, unsigned int n,
  1001. unsigned short color)
  1002. {
  1003. ngui_text_px(ui, x, y, s, n, color,
  1004. ui ? ui->font_px : 14, 0, 0, 0);
  1005. }
  1006. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  1007. unsigned short color)
  1008. {
  1009. ngui_text_n(ui, x, y, s, slen(s), color);
  1010. }
  1011. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  1012. unsigned int n, unsigned short color, int scale, int bold)
  1013. {
  1014. if (scale < 1)
  1015. scale = 1;
  1016. if (ui && ui->font) {
  1017. ngui_text_px(ui, x, y, s, n, color,
  1018. ui->font_px * scale, bold, 0, 0);
  1019. return;
  1020. }
  1021. {
  1022. unsigned int i;
  1023. for (i = 0; i < n && s && s[i]; i++)
  1024. ngui_bitmap_glyph(ui, x + (int)i * 8 * scale, y,
  1025. (unsigned char)s[i], color,
  1026. scale, bold);
  1027. }
  1028. }
  1029. /* Draw a string upside down. The whole n-character block is rotated, so the
  1030. * first character ends up on the right; used for the mirrored corner index on
  1031. * a playing card. */
  1032. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  1033. unsigned int n, unsigned short color)
  1034. {
  1035. unsigned int i;
  1036. int row, col;
  1037. for (i = 0; i < n && s && s[i]; i++) {
  1038. const unsigned char *g =
  1039. &font_8x16[(unsigned int)(unsigned char)s[i] *
  1040. FONT_8X16_ROWS];
  1041. for (row = 0; row < FONT_8X16_ROWS; row++) {
  1042. unsigned char bits = g[row];
  1043. if (!bits)
  1044. continue;
  1045. for (col = 0; col < 8; col++) {
  1046. if (!(bits & (0x80u >> col)))
  1047. continue;
  1048. ngui_pixel(ui,
  1049. x + (int)(n - 1 - i) * 8 + 7 - col,
  1050. y + FONT_8X16_ROWS - 1 - row,
  1051. color);
  1052. }
  1053. }
  1054. }
  1055. }
  1056. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  1057. const char *s, unsigned short color)
  1058. {
  1059. unsigned int n = slen(s);
  1060. int avail = r.x + r.w - x;
  1061. unsigned int i, best = 0;
  1062. int dots;
  1063. if (avail <= 0)
  1064. return;
  1065. if (ui && ui->font) {
  1066. dots = (int)ngui_text_width_px(ui, "...", 3, ui->font_px,
  1067. 0, 0, 0);
  1068. for (i = 0; i <= n; i++) {
  1069. int w = (int)ngui_text_width_px(ui, s, i, ui->font_px,
  1070. 0, 0, 0);
  1071. if (w > avail)
  1072. break;
  1073. best = i;
  1074. }
  1075. if (best == n) {
  1076. ngui_text_n(ui, x, y, s, n, color);
  1077. return;
  1078. }
  1079. while (best > 0 &&
  1080. (int)ngui_text_width_px(ui, s, best, ui->font_px,
  1081. 0, 0, 0) + dots > avail)
  1082. best--;
  1083. ngui_text_n(ui, x, y, s, best, color);
  1084. ngui_text_n(ui, x + (int)ngui_text_width_px(ui, s, best,
  1085. ui->font_px, 0, 0, 0),
  1086. y, "...", 3, color);
  1087. return;
  1088. }
  1089. {
  1090. int max = avail / 8;
  1091. if (max <= 0)
  1092. return;
  1093. if (n <= (unsigned int)max || max < 4) {
  1094. for (i = 0; i < n && (int)i < max; i++)
  1095. ngui_bitmap_glyph(ui, x + (int)i * 8, y,
  1096. (unsigned char)s[i], color, 1, 0);
  1097. return;
  1098. }
  1099. for (i = 0; i < (unsigned int)(max - 3); i++)
  1100. ngui_bitmap_glyph(ui, x + (int)i * 8, y,
  1101. (unsigned char)s[i], color, 1, 0);
  1102. ngui_bitmap_glyph(ui, x + (max - 3) * 8, y, '.', color, 1, 0);
  1103. ngui_bitmap_glyph(ui, x + (max - 2) * 8, y, '.', color, 1, 0);
  1104. ngui_bitmap_glyph(ui, x + (max - 1) * 8, y, '.', color, 1, 0);
  1105. }
  1106. }
  1107. /* ---- Icons ------------------------------------------------------------
  1108. * The Adwaita symbolic masks are read once per process into BSS (not the
  1109. * libc heap, so the browser's heap_reset() cannot free them). The read is
  1110. * capped at the icons this build knows: a newer bundle with appended icons
  1111. * still works, an older one leaves the missing ids blank. */
  1112. #define NGUI_ICON_PX 16
  1113. #define NGUI_ICON_BYTES (NGUI_ICON_PX * NGUI_ICON_PX)
  1114. static unsigned char icon_data[sizeof(struct icon_file_hdr) +
  1115. NGUI_ICON_COUNT * NGUI_ICON_BYTES + 512];
  1116. static int icon_state; /* 0 untried, 1 loaded, -1 unavailable */
  1117. static unsigned int icon_count;
  1118. #define NGUI_ICON_NAME_(e, name, file) name,
  1119. static const char *const icon_names[NGUI_ICON_COUNT] = {
  1120. ICON_LIST(NGUI_ICON_NAME_)
  1121. };
  1122. #undef NGUI_ICON_NAME_
  1123. const char *ngui_icon_name(enum ngui_icon icon)
  1124. {
  1125. if ((unsigned int)icon >= NGUI_ICON_COUNT)
  1126. return "?";
  1127. return icon_names[icon];
  1128. }
  1129. static void icon_load(void)
  1130. {
  1131. const struct icon_file_hdr *h = (const struct icon_file_hdr *)icon_data;
  1132. unsigned int max = sizeof(icon_data) - 512, have;
  1133. int n, i;
  1134. icon_state = -1;
  1135. /* The FAT path may write a final partial sector past `max`; the
  1136. * buffer keeps 512 bytes of slack for it. */
  1137. n = u_syscall3(SYS_READ_FILE, (int)NGUI_ICON_PATH, (int)icon_data,
  1138. (int)max);
  1139. if (n < (int)sizeof(*h))
  1140. return;
  1141. for (i = 0; i < 8; i++)
  1142. if (h->magic[i] != ICON_FILE_MAGIC[i])
  1143. return;
  1144. if (h->size != NGUI_ICON_PX)
  1145. return;
  1146. have = ((unsigned int)n - sizeof(*h)) / NGUI_ICON_BYTES;
  1147. if (have > h->count)
  1148. have = h->count;
  1149. if (have > NGUI_ICON_COUNT)
  1150. have = NGUI_ICON_COUNT;
  1151. icon_count = have;
  1152. icon_state = 1;
  1153. }
  1154. static const unsigned char *icon_mask(enum ngui_icon icon)
  1155. {
  1156. if (icon_state == 0)
  1157. icon_load();
  1158. if (icon_state != 1 || (unsigned int)icon >= icon_count)
  1159. return 0;
  1160. return icon_data + sizeof(struct icon_file_hdr) +
  1161. (unsigned int)icon * NGUI_ICON_BYTES;
  1162. }
  1163. /* Tint a coverage mask with `color` and blend it over the canvas. */
  1164. static void icon_blit(struct ngui *ui, int x, int y,
  1165. const unsigned char *m, unsigned short color)
  1166. {
  1167. int cx, cy, cw, ch, i, j;
  1168. int sr = color >> 11, sg = (color >> 5) & 63, sb = color & 31;
  1169. ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
  1170. for (j = 0; j < NGUI_ICON_PX; j++) {
  1171. int py = y + j;
  1172. unsigned short *row;
  1173. if (py < cy || py >= cy + ch)
  1174. continue;
  1175. row = ui->px + py * ui->w;
  1176. for (i = 0; i < NGUI_ICON_PX; i++) {
  1177. int px = x + i, a = m[j * NGUI_ICON_PX + i];
  1178. unsigned short d;
  1179. int dr, dg, db;
  1180. if (!a || px < cx || px >= cx + cw)
  1181. continue;
  1182. if (a == 255) {
  1183. row[px] = color;
  1184. continue;
  1185. }
  1186. d = row[px];
  1187. dr = d >> 11;
  1188. dg = (d >> 5) & 63;
  1189. db = d & 31;
  1190. dr += ((sr - dr) * a + 127) / 255;
  1191. dg += ((sg - dg) * a + 127) / 255;
  1192. db += ((sb - db) * a + 127) / 255;
  1193. row[px] = (unsigned short)((dr << 11) | (dg << 5) | db);
  1194. }
  1195. }
  1196. }
  1197. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  1198. unsigned short color)
  1199. {
  1200. int x = r.x + r.w / 2 - 8;
  1201. int y = r.y + r.h / 2 - 8;
  1202. const unsigned char *m;
  1203. if (!ui || !ui->px)
  1204. return;
  1205. /* Without the bundle (a stale /ramfs or a host run without
  1206. * icons/) the icon is simply left out. */
  1207. m = icon_mask(icon);
  1208. if (m)
  1209. icon_blit(ui, x, y, m, color);
  1210. }
  1211. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1212. const char *label)
  1213. {
  1214. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1215. int press_hot = ui->pressed &&
  1216. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1217. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1218. int fired = 0;
  1219. unsigned short bg;
  1220. int tw, tx, ty;
  1221. if (press_hot)
  1222. ui->active = id;
  1223. if (ui->pressed && ui->released && press_hot && release_hot)
  1224. fired = 1;
  1225. if (ui->released && ui->active == id) {
  1226. if (release_hot)
  1227. fired = 1;
  1228. ui->active = 0;
  1229. }
  1230. if (ui->active == id)
  1231. bg = ui->theme.button_down;
  1232. else if (hot)
  1233. bg = ui->theme.button_hot;
  1234. else
  1235. bg = ui->theme.button;
  1236. ngui_fill_rect(ui, r, bg);
  1237. button_bevel(ui, r, bg, hot, ui->active == id);
  1238. if (label && label[0]) {
  1239. tw = (int)ngui_text_width_ui(ui, label);
  1240. tx = r.x + (r.w - tw) / 2;
  1241. ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
  1242. if (ty < r.y)
  1243. ty = r.y;
  1244. ngui_push_clip(ui, r);
  1245. ngui_text(ui, tx, ty, label, ui->theme.text);
  1246. ngui_pop_clip(ui);
  1247. }
  1248. if (hot)
  1249. ui->hot = id;
  1250. return fired;
  1251. }
  1252. /* One row of a drop-down menu: left-aligned label, optional check mark and
  1253. * right-aligned shortcut. Uses the same press/release/active semantics as
  1254. * ngui_button so a menu and its caller never disagree about a click. */
  1255. static int ngui_menu_item_common(struct ngui *ui, unsigned int id,
  1256. struct ngui_rect r, const char *label,
  1257. const char *shortcut, int checked, int utf8)
  1258. {
  1259. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1260. int press_hot = ui->pressed &&
  1261. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1262. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1263. int fired = 0;
  1264. int selected;
  1265. unsigned short bg, fg;
  1266. int ty;
  1267. if (press_hot)
  1268. ui->active = id;
  1269. if (ui->pressed && ui->released && press_hot && release_hot)
  1270. fired = 1;
  1271. if (ui->released && ui->active == id) {
  1272. if (release_hot)
  1273. fired = 1;
  1274. ui->active = 0;
  1275. }
  1276. selected = (ui->active == id) || hot;
  1277. bg = selected ? ui->theme.selection : ui->theme.panel;
  1278. fg = selected ? NGUI_RGB(255, 255, 255) : ui->theme.text;
  1279. ngui_fill_rect(ui, r, bg);
  1280. ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
  1281. if (ty < r.y)
  1282. ty = r.y;
  1283. ngui_push_clip(ui, r);
  1284. if (checked) {
  1285. ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
  1286. ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
  1287. }
  1288. if (label) {
  1289. if (utf8)
  1290. ngui_text_utf8(ui, r.x + 22, ty, label, fg);
  1291. else
  1292. ngui_text(ui, r.x + 22, ty, label, fg);
  1293. }
  1294. if (shortcut) {
  1295. int sw = (int)(utf8 ? ngui_text_width_utf8(ui, shortcut) :
  1296. ngui_text_width_ui(ui, shortcut));
  1297. if (utf8)
  1298. ngui_text_utf8(ui, r.x + r.w - 8 - sw, ty, shortcut,
  1299. fg);
  1300. else
  1301. ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
  1302. }
  1303. ngui_pop_clip(ui);
  1304. if (hot)
  1305. ui->hot = id;
  1306. return fired;
  1307. }
  1308. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1309. const char *label, const char *shortcut, int checked)
  1310. {
  1311. return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 0);
  1312. }
  1313. int ngui_menu_item_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1314. const char *label, const char *shortcut, int checked)
  1315. {
  1316. return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 1);
  1317. }
  1318. void ngui_popover_begin(struct ngui *ui, struct ngui_popover *p)
  1319. {
  1320. struct ngui_rect all = { 0, 0, ui->w, ui->h };
  1321. ngui_fill_rect(ui, all, ui->theme.bg);
  1322. p->body = rect_make(12, 12, ui->w - 24, ui->h - 12 - 34);
  1323. p->footer = rect_make(12, ui->h - 30, ui->w - 24, 18);
  1324. }
  1325. void ngui_popover_footer(struct ngui *ui, const struct ngui_popover *p,
  1326. const char *text, unsigned short color)
  1327. {
  1328. if (!p || !text || !text[0])
  1329. return;
  1330. ngui_push_clip(ui, p->footer);
  1331. ngui_text_utf8(ui, p->footer.x, p->footer.y, text, color);
  1332. ngui_pop_clip(ui);
  1333. }
  1334. int ngui_section(struct ngui *ui, struct ngui_rect r, const char *caption,
  1335. unsigned int action_id, const char *action)
  1336. {
  1337. struct ngui_rect br;
  1338. int fired = 0;
  1339. int y = r.y + (r.h - 17) / 2;
  1340. if (y < r.y)
  1341. y = r.y;
  1342. ngui_text_px(ui, r.x, y, caption, slen(caption), ui->theme.text_dim,
  1343. 14, 1, 0, 0);
  1344. if (action_id && action && action[0]) {
  1345. int tw = (int)ngui_text_width_ui(ui, action);
  1346. int bw = tw + 24;
  1347. if (bw < 58)
  1348. bw = 58;
  1349. br = rect_make(r.x + r.w - bw, r.y + (r.h - 24) / 2, bw, 24);
  1350. if (ngui_button(ui, action_id, br, action))
  1351. fired = 1;
  1352. }
  1353. return fired;
  1354. }
  1355. /* Shared by the byte and UTF-8 variants: one hover/selection row with an
  1356. * optional signal icon and right-aligned text. */
  1357. static int ngui_list_row_common(struct ngui *ui, unsigned int id,
  1358. struct ngui_rect r,
  1359. const struct ngui_row *row, int utf8)
  1360. {
  1361. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1362. int press_hot = ui->pressed &&
  1363. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1364. int release_hot = ui->released &&
  1365. ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1366. int fired = 0;
  1367. int left = r.x + 10, right = r.x + r.w - 10;
  1368. int title_h, block_h, ty;
  1369. unsigned short bg = 0, fg, sub_fg, trail_fg;
  1370. if (press_hot)
  1371. ui->active = id;
  1372. if (ui->pressed && ui->released && press_hot && release_hot)
  1373. fired = 1;
  1374. if (ui->released && ui->active == id) {
  1375. if (release_hot)
  1376. fired = 1;
  1377. ui->active = 0;
  1378. }
  1379. if (row->selected)
  1380. bg = ui->theme.selection;
  1381. else if (hot || ui->active == id)
  1382. bg = ui->theme.panel_alt;
  1383. if (bg)
  1384. ngui_fill_round_rect(ui, r, NGUI_RADIUS, NGUI_RADIUS,
  1385. NGUI_RADIUS, NGUI_RADIUS, bg);
  1386. title_h = ngui_text_line_height(ui, ui->font_px, 0);
  1387. fg = ui->theme.text;
  1388. sub_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
  1389. trail_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
  1390. if (row->trailing && row->trailing[0]) {
  1391. int tw = (int)ngui_text_width_ui(ui, row->trailing);
  1392. right -= tw + 8;
  1393. if (utf8)
  1394. ngui_text_utf8(ui, right + 8,
  1395. r.y + (r.h - title_h) / 2,
  1396. row->trailing, trail_fg);
  1397. else
  1398. ngui_text(ui, right + 8,
  1399. r.y + (r.h - title_h) / 2,
  1400. row->trailing, trail_fg);
  1401. }
  1402. if (row->bars >= 0) {
  1403. struct ngui_rect br;
  1404. right -= 26;
  1405. br.x = right + 6;
  1406. br.y = r.y + (r.h - 14) / 2;
  1407. br.w = 18;
  1408. br.h = 14;
  1409. ngui_signal_bars(ui, br, row->bars,
  1410. row->selected ? ui->theme.text :
  1411. ui->theme.accent);
  1412. }
  1413. if (right <= left)
  1414. return fired;
  1415. block_h = row->subtitle && row->subtitle[0] ? title_h * 2 - 2 :
  1416. title_h;
  1417. ty = r.y + (r.h - block_h) / 2;
  1418. if (ty < r.y)
  1419. ty = r.y;
  1420. ngui_push_clip(ui, rect_make(left, r.y, right - left, r.h));
  1421. if (utf8)
  1422. ngui_text_utf8(ui, left, ty, row->title, fg);
  1423. else
  1424. ngui_text(ui, left, ty, row->title, fg);
  1425. if (row->subtitle && row->subtitle[0]) {
  1426. if (utf8)
  1427. ngui_text_utf8(ui, left, ty + title_h - 2,
  1428. row->subtitle, sub_fg);
  1429. else
  1430. ngui_text(ui, left, ty + title_h - 2, row->subtitle,
  1431. sub_fg);
  1432. }
  1433. ngui_pop_clip(ui);
  1434. if (hot)
  1435. ui->hot = id;
  1436. return fired;
  1437. }
  1438. int ngui_list_row(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1439. const struct ngui_row *row)
  1440. {
  1441. return ngui_list_row_common(ui, id, r, row, 0);
  1442. }
  1443. int ngui_list_row_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1444. const struct ngui_row *row)
  1445. {
  1446. return ngui_list_row_common(ui, id, r, row, 1);
  1447. }
  1448. void ngui_signal_bars(struct ngui *ui, struct ngui_rect r, int bars,
  1449. unsigned short color)
  1450. {
  1451. int i;
  1452. int base = r.y + r.h - 1;
  1453. for (i = 0; i < 4; i++) {
  1454. int h = 3 + i * 3;
  1455. struct ngui_rect br;
  1456. br.x = r.x + i * 5;
  1457. br.y = base - h;
  1458. br.w = 3;
  1459. br.h = h;
  1460. ngui_fill_rect(ui, br, i < bars ? color : ui->theme.button);
  1461. }
  1462. }
  1463. int ngui_tile(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1464. const char *title, const char *subtitle, int on)
  1465. {
  1466. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1467. int press_hot = ui->pressed &&
  1468. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1469. int release_hot = ui->released &&
  1470. ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1471. int fired = 0;
  1472. unsigned short bg, fg, sub_fg;
  1473. if (press_hot)
  1474. ui->active = id;
  1475. if (ui->pressed && ui->released && press_hot && release_hot)
  1476. fired = 1;
  1477. if (ui->released && ui->active == id) {
  1478. if (release_hot)
  1479. fired = 1;
  1480. ui->active = 0;
  1481. }
  1482. if (on)
  1483. bg = ui->theme.selection;
  1484. else if (hot || ui->active == id)
  1485. bg = ui->theme.button_hot;
  1486. else
  1487. bg = ui->theme.panel_alt;
  1488. ngui_fill_round_rect(ui, r, NGUI_RADIUS, NGUI_RADIUS,
  1489. NGUI_RADIUS, NGUI_RADIUS, bg);
  1490. fg = ui->theme.text;
  1491. sub_fg = on ? ui->theme.text : ui->theme.text_dim;
  1492. ngui_push_clip(ui, r);
  1493. ngui_text_px(ui, r.x + 14, r.y + 8, title, slen(title), fg,
  1494. 14, 1, 0, 0);
  1495. if (subtitle && subtitle[0])
  1496. ngui_text_utf8(ui, r.x + 14, r.y + 27, subtitle, sub_fg);
  1497. ngui_pop_clip(ui);
  1498. if (hot)
  1499. ui->hot = id;
  1500. return fired;
  1501. }
  1502. int ngui_slider(struct ngui *ui, unsigned int id, struct ngui_slider *s,
  1503. struct ngui_rect r, int *value, int min, int max)
  1504. {
  1505. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1506. int press_hot = ui->pressed &&
  1507. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1508. int changed = 0;
  1509. int track_y = r.y + r.h / 2 - 2;
  1510. int knob_x, fill_w;
  1511. if (!s || !value || max <= min)
  1512. return 0;
  1513. if (*value < min)
  1514. *value = min;
  1515. if (*value > max)
  1516. *value = max;
  1517. if (press_hot) {
  1518. s->dragging = 1;
  1519. ui->active = id;
  1520. }
  1521. if (s->dragging && (ui->buttons & 1)) {
  1522. int rel = ui->mouse_x - r.x;
  1523. int v;
  1524. if (rel < 0)
  1525. rel = 0;
  1526. if (rel >= r.w)
  1527. rel = r.w - 1;
  1528. v = min + rel * (max - min) / (r.w > 1 ? r.w - 1 : 1);
  1529. if (v != *value) {
  1530. *value = v;
  1531. changed = 1;
  1532. }
  1533. }
  1534. if (ui->released) {
  1535. s->dragging = 0;
  1536. if (ui->active == id)
  1537. ui->active = 0;
  1538. }
  1539. if (hot)
  1540. ui->hot = id;
  1541. ngui_fill_rect(ui, rect_make(r.x, track_y, r.w, 4),
  1542. ui->theme.button);
  1543. fill_w = (*value - min) * r.w / (max - min);
  1544. if (fill_w > 0)
  1545. ngui_fill_rect(ui, rect_make(r.x, track_y, fill_w, 4),
  1546. ui->theme.accent);
  1547. knob_x = r.x + (*value - min) * (r.w - 1) / (max - min);
  1548. ngui_fill_round_rect(ui,
  1549. rect_make(knob_x - 7, r.y + r.h / 2 - 7, 14, 14),
  1550. 7, 7, 7, 7, ui->theme.text);
  1551. return changed;
  1552. }
  1553. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1554. enum ngui_icon icon){
  1555. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1556. int press_hot = ui->pressed &&
  1557. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1558. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1559. int fired = 0;
  1560. unsigned short bg;
  1561. if (press_hot)
  1562. ui->active = id;
  1563. if (ui->pressed && ui->released && press_hot && release_hot)
  1564. fired = 1;
  1565. if (ui->released && ui->active == id) {
  1566. if (release_hot)
  1567. fired = 1;
  1568. ui->active = 0;
  1569. }
  1570. if (ui->active == id)
  1571. bg = ui->theme.button_down;
  1572. else if (hot)
  1573. bg = ui->theme.button_hot;
  1574. else
  1575. bg = ui->theme.button;
  1576. ngui_fill_rect(ui, r, bg);
  1577. button_bevel(ui, r, bg, hot, ui->active == id);
  1578. ngui_icon(ui, r, icon, ui->theme.text);
  1579. if (hot)
  1580. ui->hot = id;
  1581. return fired;
  1582. }
  1583. void ngui_input_set(struct ngui_input *in, const char *s)
  1584. {
  1585. scopy(in->text, s, NGUI_INPUT_MAX);
  1586. in->len = slen(in->text);
  1587. in->cursor = in->len;
  1588. in->scroll = 0;
  1589. in->anchor = in->cursor;
  1590. in->dragging = 0;
  1591. in->last_click_ms = 0;
  1592. }
  1593. void ngui_input_value(const struct ngui_input *in, char *out,
  1594. unsigned int max)
  1595. {
  1596. unsigned int i;
  1597. if (!in || !out || !max)
  1598. return;
  1599. /* ngui_text_input() unmasks a secret field again after drawing, so
  1600. * in->text is the real value between calls. */
  1601. for (i = 0; in->text[i] && i + 1 < max; i++)
  1602. out[i] = in->text[i];
  1603. out[i] = '\0';
  1604. }
  1605. static int input_word_char(char c)
  1606. {
  1607. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  1608. (c >= '0' && c <= '9') || c == '_';
  1609. }
  1610. /* Next word start after `pos` (Windows edit-control semantics). */
  1611. static unsigned int input_word_next(const struct ngui_input *in,
  1612. unsigned int pos)
  1613. {
  1614. while (pos < in->len && input_word_char(in->text[pos]))
  1615. pos++;
  1616. while (pos < in->len && !input_word_char(in->text[pos]))
  1617. pos++;
  1618. return pos;
  1619. }
  1620. /* Start of the previous word before `pos`. */
  1621. static unsigned int input_word_prev(const struct ngui_input *in,
  1622. unsigned int pos)
  1623. {
  1624. while (pos > 0 && !input_word_char(in->text[pos - 1]))
  1625. pos--;
  1626. while (pos > 0 && input_word_char(in->text[pos - 1]))
  1627. pos--;
  1628. return pos;
  1629. }
  1630. /* End of the word to the right of `pos`. A caret inside a word takes the
  1631. * rest of that word and its trailing separators; a caret on separators takes
  1632. * them plus the next word. */
  1633. static unsigned int input_word_delete_end(const struct ngui_input *in,
  1634. unsigned int pos)
  1635. {
  1636. if (pos < in->len && input_word_char(in->text[pos])) {
  1637. while (pos < in->len && input_word_char(in->text[pos]))
  1638. pos++;
  1639. while (pos < in->len && !input_word_char(in->text[pos]))
  1640. pos++;
  1641. return pos;
  1642. }
  1643. while (pos < in->len && !input_word_char(in->text[pos]))
  1644. pos++;
  1645. while (pos < in->len && input_word_char(in->text[pos]))
  1646. pos++;
  1647. return pos;
  1648. }
  1649. static void input_sel_bounds(const struct ngui_input *in,
  1650. unsigned int *a, unsigned int *b)
  1651. {
  1652. if (in->anchor < in->cursor) {
  1653. *a = in->anchor;
  1654. *b = in->cursor;
  1655. } else {
  1656. *a = in->cursor;
  1657. *b = in->anchor;
  1658. }
  1659. }
  1660. /* Remove [a, b) and keep cursor/anchor consistent. Returns 1 when the text
  1661. * changed. */
  1662. static int input_delete_range(struct ngui_input *in, unsigned int a,
  1663. unsigned int b)
  1664. {
  1665. unsigned int i;
  1666. if (a >= b || b > in->len)
  1667. return 0;
  1668. for (i = b; i < in->len; i++)
  1669. in->text[i - (b - a)] = in->text[i];
  1670. in->len -= b - a;
  1671. in->text[in->len] = '\0';
  1672. if (in->cursor >= b)
  1673. in->cursor -= b - a;
  1674. else if (in->cursor > a)
  1675. in->cursor = a;
  1676. if (in->anchor >= b)
  1677. in->anchor -= b - a;
  1678. else if (in->anchor > a)
  1679. in->anchor = a;
  1680. return 1;
  1681. }
  1682. static int input_delete_selection(struct ngui_input *in)
  1683. {
  1684. unsigned int a, b;
  1685. input_sel_bounds(in, &a, &b);
  1686. return input_delete_range(in, a, b);
  1687. }
  1688. static void input_insert(struct ngui_input *in, char c)
  1689. {
  1690. unsigned int i;
  1691. input_delete_selection(in);
  1692. if (in->len + 1 >= NGUI_INPUT_MAX)
  1693. return;
  1694. i = in->len;
  1695. while (i > in->cursor) {
  1696. in->text[i] = in->text[i - 1];
  1697. i--;
  1698. }
  1699. in->text[in->cursor] = c;
  1700. in->cursor++;
  1701. in->len++;
  1702. in->text[in->len] = '\0';
  1703. in->anchor = in->cursor;
  1704. }
  1705. static void input_delete(struct ngui_input *in, int back)
  1706. {
  1707. unsigned int pos;
  1708. if (back) {
  1709. if (in->cursor == 0)
  1710. return;
  1711. pos = in->cursor - 1;
  1712. } else {
  1713. if (in->cursor >= in->len)
  1714. return;
  1715. pos = in->cursor;
  1716. }
  1717. input_delete_range(in, pos, pos + 1);
  1718. }
  1719. static void input_move_to(struct ngui_input *in, unsigned int pos)
  1720. {
  1721. if (pos > in->len)
  1722. pos = in->len;
  1723. in->cursor = pos;
  1724. in->anchor = pos;
  1725. }
  1726. /* Two presses in the same field within this window select all of its text. */
  1727. #define NGUI_DOUBLE_CLICK_MS 500
  1728. static int input_text_w(struct ngui *ui, const char *s, unsigned int n)
  1729. {
  1730. return (int)ngui_text_width_px(ui, s, n, ui->font_px, 0, 0, 0);
  1731. }
  1732. /* Character position under a content-relative x, clamped to the visible
  1733. * part of the field. Proportional text means the prefix must be measured. */
  1734. static unsigned int input_pos_at(struct ngui *ui, const struct ngui_input *in,
  1735. int inner_x, int x)
  1736. {
  1737. unsigned int i, n, best = 0;
  1738. int rel = x - inner_x;
  1739. if (rel <= 0 || in->scroll >= in->len)
  1740. return in->scroll;
  1741. n = in->len - in->scroll;
  1742. for (i = 1; i <= n; i++) {
  1743. if (input_text_w(ui, in->text + in->scroll, i) > rel)
  1744. break;
  1745. best = i;
  1746. }
  1747. return in->scroll + best;
  1748. }
  1749. static void input_draw_selection(struct ngui *ui, const struct ngui_input *in,
  1750. struct ngui_rect inner)
  1751. {
  1752. unsigned int a, b, va, vb;
  1753. int xa, xb;
  1754. input_sel_bounds(in, &a, &b);
  1755. if (a == b || in->scroll >= in->len)
  1756. return;
  1757. va = a < in->scroll ? in->scroll : a;
  1758. vb = b < in->len ? b : in->len;
  1759. if (va >= vb)
  1760. return;
  1761. xa = input_text_w(ui, in->text + in->scroll, va - in->scroll);
  1762. xb = input_text_w(ui, in->text + in->scroll, vb - in->scroll);
  1763. if (xb > xa)
  1764. ngui_fill_rect(ui, rect_make(inner.x + xa, inner.y,
  1765. xb - xa,
  1766. ngui_text_line_height(ui,
  1767. ui->font_px,
  1768. 0)),
  1769. ui->theme.selection);
  1770. }
  1771. /* Scroll the first-visible-character index until the caret is visible. */
  1772. static void input_scroll_to_cursor(struct ngui *ui, struct ngui_input *in,
  1773. int inner_w)
  1774. {
  1775. if (in->scroll > in->len)
  1776. in->scroll = in->len;
  1777. if (in->cursor < in->scroll)
  1778. in->scroll = in->cursor;
  1779. if (inner_w <= 0) {
  1780. in->scroll = in->len;
  1781. return;
  1782. }
  1783. while (in->scroll < in->cursor &&
  1784. input_text_w(ui, in->text + in->scroll,
  1785. in->cursor - in->scroll) > inner_w)
  1786. in->scroll++;
  1787. if (in->scroll > in->len)
  1788. in->scroll = in->len;
  1789. }
  1790. /* Secret fields are measured and painted from a '*' mask: the real text is
  1791. * swapped out for everything except the edit loop, so the caret, selection
  1792. * and click positions all follow the masked width. */
  1793. static char secret_save[NGUI_INPUT_MAX];
  1794. static void secret_mask(struct ngui_input *in)
  1795. {
  1796. unsigned int i;
  1797. for (i = 0; i <= in->len && i < NGUI_INPUT_MAX; i++) {
  1798. secret_save[i] = in->text[i];
  1799. in->text[i] = i < in->len ? '*' : '\0';
  1800. }
  1801. }
  1802. static void secret_unmask(struct ngui_input *in)
  1803. {
  1804. unsigned int i;
  1805. for (i = 0; i <= in->len && i < NGUI_INPUT_MAX; i++) {
  1806. in->text[i] = secret_save[i];
  1807. secret_save[i] = 0;
  1808. }
  1809. }
  1810. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1811. struct ngui_input *in)
  1812. {
  1813. struct ngui_rect inner;
  1814. int focused = !in->disabled && (ui->focus == id);
  1815. int result = 0;
  1816. int avail, line_h, k;
  1817. int changed = 0;
  1818. line_h = ngui_text_line_height(ui, ui->font_px, 0);
  1819. if (line_h < 1)
  1820. line_h = 16;
  1821. inner = rect_make(r.x + 4, r.y + (r.h - line_h) / 2, r.w - 8,
  1822. line_h);
  1823. if (inner.w < 0)
  1824. inner.w = 0;
  1825. if (inner.h < 1)
  1826. inner.h = 1;
  1827. avail = inner.w;
  1828. if (in->secret)
  1829. secret_mask(in);
  1830. ngui_cursor_rect(ui, r, in->disabled ? CURSOR_NOT_ALLOWED : CURSOR_TEXT);
  1831. if (in->disabled) {
  1832. /* A field that became disabled while focused gives the focus
  1833. * back, and a press cannot start a drag or a selection. */
  1834. if (ui->focus == id)
  1835. ngui_set_focus(ui, 0);
  1836. in->dragging = 0;
  1837. } else if (ui->pressed &&
  1838. ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
  1839. unsigned int now = u_ticks_ms();
  1840. int dbl = in->last_click_ms &&
  1841. (unsigned int)(now - in->last_click_ms) <=
  1842. NGUI_DOUBLE_CLICK_MS;
  1843. if (dbl) {
  1844. /* Double click: select the whole text and keep it
  1845. * selected while the second press is held. */
  1846. in->anchor = 0;
  1847. in->cursor = in->len;
  1848. in->dragging = 0;
  1849. } else {
  1850. unsigned int pos = input_pos_at(ui, in, inner.x,
  1851. ui->press_x);
  1852. in->cursor = pos;
  1853. in->anchor = pos;
  1854. in->dragging = 1;
  1855. }
  1856. in->last_click_ms = now;
  1857. ui->focus = id;
  1858. ui->focus_claimed = 1;
  1859. ui->last_blink = now;
  1860. ui->caret_on = 1;
  1861. focused = 1;
  1862. } else if (ui->pressed) {
  1863. in->dragging = 0;
  1864. }
  1865. /* Extend the selection while the button that started in this field is
  1866. * still held. Sampled coordinates may be outside the canvas. The
  1867. * release *event* is authoritative: the sampled button state can lag
  1868. * the event queue by a frame. */
  1869. if (!in->disabled && in->dragging && (ui->buttons & 1) &&
  1870. !ui->released) {
  1871. in->cursor = input_pos_at(ui, in, inner.x, ui->mouse_x);
  1872. /* Keep redrawing when the pointer leaves the window. */
  1873. ngui_invalidate(ui);
  1874. }
  1875. if (ui->released)
  1876. in->dragging = 0;
  1877. if (focused) {
  1878. if (in->secret)
  1879. secret_unmask(in);
  1880. while ((k = ngui_key(ui)) != 0) {
  1881. if (in->secret) {
  1882. /* No word boundaries inside a password. */
  1883. if (k == NGUI_KEY_CTRL_LEFT)
  1884. k = NGUI_KEY_HOME;
  1885. else if (k == NGUI_KEY_CTRL_RIGHT)
  1886. k = NGUI_KEY_END;
  1887. }
  1888. if (k == '\n' || k == '\r') {
  1889. result |= NGUI_INPUT_SUBMIT;
  1890. break;
  1891. }
  1892. if (k == NGUI_KEY_ESC || k == NGUI_KEY_TAB) {
  1893. result |= NGUI_INPUT_CANCEL;
  1894. if (k == NGUI_KEY_TAB)
  1895. result |= NGUI_INPUT_NEXT;
  1896. ui->focus = 0;
  1897. break;
  1898. }
  1899. if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
  1900. if (!input_delete_selection(in))
  1901. input_delete(in, 1);
  1902. changed = 1;
  1903. } else if (k == NGUI_KEY_DELETE) {
  1904. if (!input_delete_selection(in))
  1905. input_delete(in, 0);
  1906. changed = 1;
  1907. } else if (k == NGUI_KEY_CTRL_BACKSPACE) {
  1908. if (input_delete_selection(in))
  1909. changed = 1;
  1910. else
  1911. changed = input_delete_range(in,
  1912. input_word_prev(in, in->cursor),
  1913. in->cursor);
  1914. } else if (k == NGUI_KEY_CTRL_DELETE) {
  1915. if (input_delete_selection(in)) {
  1916. changed = 1;
  1917. } else {
  1918. unsigned int a = in->cursor;
  1919. unsigned int b = input_word_delete_end(in,
  1920. a);
  1921. /* At the end of the line, delete the word
  1922. * before the caret instead of doing
  1923. * nothing. */
  1924. if (b == a) {
  1925. a = input_word_prev(in, in->cursor);
  1926. b = in->cursor;
  1927. }
  1928. changed = input_delete_range(in, a, b);
  1929. }
  1930. } else if (k == NGUI_KEY_LEFT) {
  1931. input_move_to(in, in->cursor > 0 ? in->cursor - 1 : 0);
  1932. } else if (k == NGUI_KEY_RIGHT) {
  1933. input_move_to(in, in->cursor < in->len
  1934. ? in->cursor + 1 : in->len);
  1935. } else if (k == NGUI_KEY_CTRL_LEFT) {
  1936. input_move_to(in, input_word_prev(in, in->cursor));
  1937. } else if (k == NGUI_KEY_CTRL_RIGHT) {
  1938. input_move_to(in, input_word_next(in, in->cursor));
  1939. } else if (k == NGUI_KEY_HOME) {
  1940. input_move_to(in, 0);
  1941. } else if (k == NGUI_KEY_END) {
  1942. input_move_to(in, in->len);
  1943. } else if (k == 0x01) { /* Ctrl+A: select all */
  1944. in->anchor = 0;
  1945. in->cursor = in->len;
  1946. } else if (k == 0x15) { /* Ctrl+U */
  1947. in->len = 0;
  1948. in->cursor = 0;
  1949. in->anchor = 0;
  1950. in->text[0] = '\0';
  1951. changed = 1;
  1952. } else if (k >= 0x20 && k <= 0x7E) {
  1953. input_insert(in, (char)k);
  1954. changed = 1;
  1955. }
  1956. }
  1957. if (in->secret)
  1958. secret_mask(in);
  1959. }
  1960. ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
  1961. /* Inset edge: the shadow sits on the top/left, the light on the
  1962. * bottom/right. A focused field keeps the shape in accent blue. */
  1963. if (focused)
  1964. bevel_rect(ui, r, ui->theme.accent,
  1965. shade565(ui->theme.accent, 4));
  1966. else
  1967. bevel_rect(ui, r, shade565(ui->theme.panel, -4),
  1968. shade565(ui->theme.panel, 4));
  1969. if (!in->disabled)
  1970. input_scroll_to_cursor(ui, in, avail);
  1971. ngui_push_clip(ui, inner);
  1972. if (in->len == 0 && in->placeholder) {
  1973. ngui_text_px(ui, inner.x, inner.y, in->placeholder,
  1974. slen(in->placeholder), ui->theme.text_dim,
  1975. ui->font_px, 0, 0, 0);
  1976. } else {
  1977. if (focused)
  1978. input_draw_selection(ui, in, inner);
  1979. ngui_text_px(ui, inner.x, inner.y, in->text + in->scroll,
  1980. slen(in->text + in->scroll),
  1981. in->disabled ? ui->theme.text_dim :
  1982. ui->theme.text,
  1983. ui->font_px, 0, 0, 0);
  1984. }
  1985. if (focused && ui->caret_on) {
  1986. int cx = 0;
  1987. if (in->cursor > in->scroll)
  1988. cx = input_text_w(ui, in->text + in->scroll,
  1989. in->cursor - in->scroll);
  1990. if (cx < 0)
  1991. cx = 0;
  1992. if (avail > 0 && cx >= avail)
  1993. cx = avail - 1;
  1994. if (avail > 0 && cx < avail)
  1995. ngui_fill_rect(ui, rect_make(inner.x + cx, inner.y,
  1996. 1, inner.h),
  1997. ui->theme.accent);
  1998. }
  1999. ngui_pop_clip(ui);
  2000. if (in->secret)
  2001. secret_unmask(in);
  2002. if (changed)
  2003. result |= NGUI_INPUT_CHANGED;
  2004. return result;
  2005. }
  2006. static int scrollable(const struct ngui_scroll *s)
  2007. {
  2008. return s->content_h > s->view_h && s->view_h > 0;
  2009. }
  2010. static int scroll_thumb_h(const struct ngui_rect *r,
  2011. const struct ngui_scroll *s)
  2012. {
  2013. int h = 0;
  2014. if (s->content_h > 0)
  2015. h = r->h * s->view_h / s->content_h;
  2016. if (h < 12)
  2017. h = 12;
  2018. if (h > r->h)
  2019. h = r->h;
  2020. return h;
  2021. }
  2022. static int scroll_thumb_y(const struct ngui_rect *r,
  2023. const struct ngui_scroll *s)
  2024. {
  2025. int th = scroll_thumb_h(r, s);
  2026. int avail = r->h - th;
  2027. int max = s->content_h - s->view_h;
  2028. if (avail <= 0 || max <= 0)
  2029. return r->y;
  2030. return r->y + s->offset * avail / max;
  2031. }
  2032. static void scroll_clamp(struct ngui_scroll *s)
  2033. {
  2034. int max = s->content_h - s->view_h;
  2035. if (max < 0)
  2036. max = 0;
  2037. if (s->offset > max)
  2038. s->offset = max;
  2039. if (s->offset < 0)
  2040. s->offset = 0;
  2041. }
  2042. void ngui_scroll_to(struct ngui_scroll *s, int offset)
  2043. {
  2044. s->offset = offset;
  2045. scroll_clamp(s);
  2046. }
  2047. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
  2048. {
  2049. if (y < s->offset)
  2050. s->offset = y;
  2051. else if (y + h > s->offset + s->view_h)
  2052. s->offset = y + h - s->view_h;
  2053. scroll_clamp(s);
  2054. }
  2055. #define SCROLLBAR_W 8
  2056. #define SCROLLBAR_INSET 2
  2057. #define SCROLLBAR_HIT_W 14
  2058. #define SCROLLBAR_STEPS 8
  2059. #define SCROLLBAR_STEP_MS 14
  2060. static struct ngui_rect scroll_track_rect(const struct ngui_rect *r)
  2061. {
  2062. struct ngui_rect t;
  2063. int inset = SCROLLBAR_INSET;
  2064. if (r->w < SCROLLBAR_W + 2 * inset || r->h <= 2 * inset)
  2065. inset = 0;
  2066. t.x = r->x + r->w - SCROLLBAR_W - inset;
  2067. t.y = r->y + inset;
  2068. t.w = SCROLLBAR_W;
  2069. t.h = r->h - 2 * inset;
  2070. return t;
  2071. }
  2072. static struct ngui_rect scroll_hit_rect(const struct ngui_rect *r)
  2073. {
  2074. struct ngui_rect h = *r;
  2075. int w = SCROLLBAR_HIT_W;
  2076. if (w > r->w)
  2077. w = r->w;
  2078. h.x = r->x + r->w - w;
  2079. h.w = w;
  2080. return h;
  2081. }
  2082. static int pointer_in_rect(struct ngui *ui, const struct ngui_rect *r)
  2083. {
  2084. if (!ui->inside || !ngui_rect_contains(r, ui->mouse_x, ui->mouse_y))
  2085. return 0;
  2086. if (ui->nclip > 0 &&
  2087. !ngui_rect_contains(&ui->clip[ui->nclip - 1], ui->mouse_x,
  2088. ui->mouse_y))
  2089. return 0;
  2090. return 1;
  2091. }
  2092. /* Width of the pill for one animation step. The bar grows into the pane
  2093. * from its right edge, so the animation needs no alpha compositing. */
  2094. static int scroll_bar_w(int vis)
  2095. {
  2096. if (vis <= 0)
  2097. return 0;
  2098. if (vis >= SCROLLBAR_STEPS)
  2099. return SCROLLBAR_W;
  2100. return 1 + (vis - 1) * (SCROLLBAR_W - 1) / (SCROLLBAR_STEPS - 1);
  2101. }
  2102. /* Advance the overlay bar animation. The bar stays hidden while the pointer
  2103. * is outside the pane and slides in on hover; a drag pins it open. Setting
  2104. * ui->dirty while steps remain makes the app draw the next frame. */
  2105. static void scroll_bar_update(struct ngui *ui, const struct ngui_rect *r,
  2106. struct ngui_scroll *s)
  2107. {
  2108. int hover, target, steps;
  2109. unsigned int now, due;
  2110. if (s->hide_bar || !scrollable(s)) {
  2111. s->dragging = 0;
  2112. s->bar_vis = 0;
  2113. s->bar_target = 0;
  2114. s->bar_hot = 0;
  2115. return;
  2116. }
  2117. {
  2118. struct ngui_rect hit = scroll_hit_rect(r);
  2119. hover = pointer_in_rect(ui, r);
  2120. s->bar_hot = hover &&
  2121. ngui_rect_contains(&hit, ui->mouse_x, ui->mouse_y);
  2122. }
  2123. target = (hover || s->dragging) ? SCROLLBAR_STEPS : 0;
  2124. now = u_ticks_ms();
  2125. if (target != s->bar_target) {
  2126. s->bar_target = target;
  2127. s->bar_ms = now;
  2128. if (s->bar_vis != target)
  2129. ui->dirty = 1;
  2130. return;
  2131. }
  2132. if (s->bar_vis == target)
  2133. return;
  2134. due = now - s->bar_ms;
  2135. steps = (int)(due / SCROLLBAR_STEP_MS);
  2136. if (steps > 0) {
  2137. if (target > s->bar_vis) {
  2138. s->bar_vis += steps;
  2139. if (s->bar_vis > target)
  2140. s->bar_vis = target;
  2141. } else {
  2142. s->bar_vis -= steps;
  2143. if (s->bar_vis < target)
  2144. s->bar_vis = target;
  2145. }
  2146. s->bar_ms += (unsigned int)steps * SCROLLBAR_STEP_MS;
  2147. }
  2148. if (s->bar_vis != target)
  2149. ui->dirty = 1;
  2150. }
  2151. static void scroll_bar_press(struct ngui_scroll *s,
  2152. const struct ngui_rect *track, int th, int y)
  2153. {
  2154. int ty = scroll_thumb_y(track, s);
  2155. if (y >= ty && y < ty + th) {
  2156. s->dragging = 1;
  2157. s->drag_dy = y - ty;
  2158. } else {
  2159. int rel = y - track->y - th / 2;
  2160. int avail = track->h - th;
  2161. int max = s->content_h - s->view_h;
  2162. if (rel < 0)
  2163. rel = 0;
  2164. if (rel > avail)
  2165. rel = avail;
  2166. if (avail > 0)
  2167. s->offset = rel * max / avail;
  2168. s->dragging = 1;
  2169. s->drag_dy = th / 2;
  2170. scroll_clamp(s);
  2171. }
  2172. }
  2173. static void scroll_bar_drag(struct ngui_scroll *s,
  2174. const struct ngui_rect *track, int th, int y)
  2175. {
  2176. int rel = y - track->y - s->drag_dy;
  2177. int avail = track->h - th;
  2178. int max = s->content_h - s->view_h;
  2179. if (rel < 0)
  2180. rel = 0;
  2181. if (rel > avail)
  2182. rel = avail;
  2183. if (avail > 0)
  2184. s->offset = rel * max / avail;
  2185. scroll_clamp(s);
  2186. }
  2187. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  2188. struct ngui_scroll *s)
  2189. {
  2190. struct ngui_rect track, hit;
  2191. int th;
  2192. s->view_h = r.h;
  2193. scroll_clamp(s);
  2194. if (!ui->buttons)
  2195. s->dragging = 0;
  2196. scroll_bar_update(ui, &r, s);
  2197. if (!s->hide_bar && scrollable(s)) {
  2198. track = scroll_track_rect(&r);
  2199. hit = scroll_hit_rect(&r);
  2200. th = scroll_thumb_h(&track, s);
  2201. if (ui->pressed &&
  2202. ngui_rect_contains(&hit, ui->press_x, ui->press_y)) {
  2203. scroll_bar_press(s, &track, th, ui->press_y);
  2204. /* The bar is on top of the content: a drag must not
  2205. * also activate a row or button below it. */
  2206. ui->pressed = 0;
  2207. ui->clicked = 0;
  2208. }
  2209. /* Sampled buttons are authoritative while held: some event
  2210. * paths deliver the press late or coalesce it with the
  2211. * release. Start the drag from the current pointer position
  2212. * as well, but only once the bar is fully visible. */
  2213. if (!s->dragging && (ui->buttons & 1) &&
  2214. s->bar_hot && s->bar_vis == SCROLLBAR_STEPS)
  2215. scroll_bar_press(s, &track, th, ui->mouse_y);
  2216. if (s->dragging && (ui->buttons & 1))
  2217. scroll_bar_drag(s, &track, th, ui->mouse_y);
  2218. if (ui->released)
  2219. s->dragging = 0;
  2220. }
  2221. ngui_push_clip(ui, r);
  2222. }
  2223. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  2224. struct ngui_scroll *s)
  2225. {
  2226. struct ngui_rect track, thumb;
  2227. int bw, th, ty;
  2228. unsigned short color;
  2229. ngui_pop_clip(ui);
  2230. /* content_h is often known only after drawing, so advance the state
  2231. * here as well; a new target requests a follow-up frame. */
  2232. scroll_bar_update(ui, &r, s);
  2233. if (s->hide_bar || !scrollable(s) || s->bar_vis <= 0)
  2234. return;
  2235. track = scroll_track_rect(&r);
  2236. bw = scroll_bar_w(s->bar_vis);
  2237. if (bw <= 0)
  2238. return;
  2239. th = scroll_thumb_h(&track, s);
  2240. ty = scroll_thumb_y(&track, s);
  2241. thumb = rect_make(r.x + r.w - SCROLLBAR_INSET - bw, ty, bw, th);
  2242. color = s->dragging ? ui->theme.accent :
  2243. s->bar_hot ? ui->theme.text_dim : ui->theme.border;
  2244. ngui_fill_round_rect(ui, thumb, bw / 2, bw / 2, bw / 2, bw / 2,
  2245. color);
  2246. if (s->bar_hot)
  2247. ngui_set_cursor(ui, CURSOR_DEFAULT);
  2248. }
  2249. int ngui_scroll_wheel(struct ngui *ui, struct ngui_rect r,
  2250. struct ngui_scroll *s)
  2251. {
  2252. if (!ui->scroll_y ||
  2253. !ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y))
  2254. return 0;
  2255. ngui_scroll_to(s, s->offset + ui->scroll_y);
  2256. ui->scroll_y = 0;
  2257. return 1;
  2258. }
  2259. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s)
  2260. {
  2261. int k = ngui_key_peek(ui);
  2262. int page;
  2263. if (k != NGUI_KEY_PAGE_UP && k != NGUI_KEY_PAGE_DOWN)
  2264. return 0;
  2265. page = s->view_h - 22;
  2266. if (page < 22)
  2267. page = 22;
  2268. ngui_key(ui);
  2269. if (k == NGUI_KEY_PAGE_UP)
  2270. s->offset -= page;
  2271. else
  2272. s->offset += page;
  2273. scroll_clamp(s);
  2274. return 1;
  2275. }