ngui.c 61 KB

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