ngui.c 40 KB

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