ngui.c 40 KB

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