ngui.c 61 KB

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