ngui.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  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(struct ngui *ui, int px, int mono)
  639. {
  640. struct ufont_face *f = ngui_face(ui, 0, 0, mono);
  641. struct ufont_metrics m;
  642. if (f) {
  643. ufont_metrics(f, px, &m);
  644. return m.ascent;
  645. }
  646. return 16 * (px >= 24 ? 2 : 1);
  647. }
  648. int ngui_text_line_height(struct ngui *ui, int px, int mono)
  649. {
  650. struct ufont_face *f = ngui_face(ui, 0, 0, mono);
  651. struct ufont_metrics m;
  652. if (f) {
  653. ufont_metrics(f, px, &m);
  654. return m.line_height;
  655. }
  656. return 16 * (px >= 24 ? 2 : 1);
  657. }
  658. unsigned int ngui_text_width_px(struct ngui *ui, const char *s, unsigned int n,
  659. int px, int bold, int italic, int mono)
  660. {
  661. struct ufont_face *f = ngui_face(ui, bold, italic, mono);
  662. int scale;
  663. if (f)
  664. return (unsigned int)ufont_text_width(f, px, s, (int)n, 0);
  665. scale = px >= 24 ? 2 : 1;
  666. return n * 8u * (unsigned int)scale;
  667. }
  668. unsigned int ngui_text_width_ui(struct ngui *ui, const char *s)
  669. {
  670. return ngui_text_width_px(ui, s, slen(s),
  671. ui ? ui->font_px : 14, 0, 0, 0);
  672. }
  673. unsigned int ngui_text_width(const char *s)
  674. {
  675. return slen(s) * 8;
  676. }
  677. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale)
  678. {
  679. (void)s;
  680. if (scale < 1)
  681. scale = 1;
  682. return n * 8u * (unsigned int)scale;
  683. }
  684. void ngui_text_px(struct ngui *ui, int x, int y, const char *s,
  685. unsigned int n, unsigned short color, int px, int bold,
  686. int italic, int mono)
  687. {
  688. struct ufont_face *f = ngui_face(ui, bold, italic, mono);
  689. int scale, i, pen = 0, ascent;
  690. int cx, cy, cw, ch;
  691. if (!ui || !ui->px || !s)
  692. return;
  693. if (!f) {
  694. scale = px >= 24 ? 2 : 1;
  695. for (i = 0; i < (int)n && s[i]; i++)
  696. ngui_bitmap_glyph(ui, x + i * 8 * scale, y,
  697. (unsigned char)s[i], color,
  698. scale, bold);
  699. return;
  700. }
  701. ascent = ngui_text_ascent(ui, px, mono);
  702. ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
  703. for (i = 0; i < (int)n && s[i]; i++) {
  704. const struct ufont_glyph *g =
  705. ufont_glyph(f, px, (unsigned char)s[i]);
  706. if (!g) {
  707. pen += 8 << 6;
  708. continue;
  709. }
  710. ufont_blit_rgb565(ui->px, ui->w, ui->h,
  711. x + (pen >> 6), y + ascent, g, color,
  712. cx, cy, cw, ch);
  713. pen += g->advance;
  714. }
  715. }
  716. void ngui_text_n(struct ngui *ui, int x, int y, const char *s, unsigned int n,
  717. unsigned short color)
  718. {
  719. ngui_text_px(ui, x, y, s, n, color,
  720. ui ? ui->font_px : 14, 0, 0, 0);
  721. }
  722. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  723. unsigned short color)
  724. {
  725. ngui_text_n(ui, x, y, s, slen(s), color);
  726. }
  727. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  728. unsigned int n, unsigned short color, int scale, int bold)
  729. {
  730. if (scale < 1)
  731. scale = 1;
  732. if (ui && ui->font) {
  733. ngui_text_px(ui, x, y, s, n, color,
  734. ui->font_px * scale, bold, 0, 0);
  735. return;
  736. }
  737. {
  738. unsigned int i;
  739. for (i = 0; i < n && s && s[i]; i++)
  740. ngui_bitmap_glyph(ui, x + (int)i * 8 * scale, y,
  741. (unsigned char)s[i], color,
  742. scale, bold);
  743. }
  744. }
  745. /* Draw a string upside down. The whole n-character block is rotated, so the
  746. * first character ends up on the right; used for the mirrored corner index on
  747. * a playing card. */
  748. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  749. unsigned int n, unsigned short color)
  750. {
  751. unsigned int i;
  752. int row, col;
  753. for (i = 0; i < n && s && s[i]; i++) {
  754. const unsigned char *g =
  755. &font_8x16[(unsigned int)(unsigned char)s[i] *
  756. FONT_8X16_ROWS];
  757. for (row = 0; row < FONT_8X16_ROWS; row++) {
  758. unsigned char bits = g[row];
  759. if (!bits)
  760. continue;
  761. for (col = 0; col < 8; col++) {
  762. if (!(bits & (0x80u >> col)))
  763. continue;
  764. ngui_pixel(ui,
  765. x + (int)(n - 1 - i) * 8 + 7 - col,
  766. y + FONT_8X16_ROWS - 1 - row,
  767. color);
  768. }
  769. }
  770. }
  771. }
  772. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  773. const char *s, unsigned short color)
  774. {
  775. unsigned int n = slen(s);
  776. int avail = r.x + r.w - x;
  777. unsigned int i, best = 0;
  778. int dots;
  779. if (avail <= 0)
  780. return;
  781. if (ui && ui->font) {
  782. dots = (int)ngui_text_width_px(ui, "...", 3, ui->font_px,
  783. 0, 0, 0);
  784. for (i = 0; i <= n; i++) {
  785. int w = (int)ngui_text_width_px(ui, s, i, ui->font_px,
  786. 0, 0, 0);
  787. if (w > avail)
  788. break;
  789. best = i;
  790. }
  791. if (best == n) {
  792. ngui_text_n(ui, x, y, s, n, color);
  793. return;
  794. }
  795. while (best > 0 &&
  796. (int)ngui_text_width_px(ui, s, best, ui->font_px,
  797. 0, 0, 0) + dots > avail)
  798. best--;
  799. ngui_text_n(ui, x, y, s, best, color);
  800. ngui_text_n(ui, x + (int)ngui_text_width_px(ui, s, best,
  801. ui->font_px, 0, 0, 0),
  802. y, "...", 3, color);
  803. return;
  804. }
  805. {
  806. int max = avail / 8;
  807. if (max <= 0)
  808. return;
  809. if (n <= (unsigned int)max || max < 4) {
  810. for (i = 0; i < n && (int)i < max; i++)
  811. ngui_bitmap_glyph(ui, x + (int)i * 8, y,
  812. (unsigned char)s[i], color, 1, 0);
  813. return;
  814. }
  815. for (i = 0; i < (unsigned int)(max - 3); i++)
  816. ngui_bitmap_glyph(ui, x + (int)i * 8, y,
  817. (unsigned char)s[i], color, 1, 0);
  818. ngui_bitmap_glyph(ui, x + (max - 3) * 8, y, '.', color, 1, 0);
  819. ngui_bitmap_glyph(ui, x + (max - 2) * 8, y, '.', color, 1, 0);
  820. ngui_bitmap_glyph(ui, x + (max - 1) * 8, y, '.', color, 1, 0);
  821. }
  822. }
  823. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  824. unsigned short color)
  825. {
  826. int x = r.x + r.w / 2 - 8;
  827. int y = r.y + r.h / 2 - 8;
  828. switch (icon) {
  829. case NGUI_ICON_FOLDER:
  830. ngui_fill_rect(ui, rect_make(x + 1, y + 3, 7, 4), color);
  831. ngui_fill_rect(ui, rect_make(x, y + 6, 16, 9), color);
  832. break;
  833. case NGUI_ICON_FILE:
  834. ngui_frame(ui, rect_make(x + 2, y, 12, 16), color);
  835. ngui_line(ui, x + 10, y, x + 13, y + 3, color);
  836. ngui_fill_rect(ui, rect_make(x + 4, y + 7, 8, 1), color);
  837. ngui_fill_rect(ui, rect_make(x + 4, y + 10, 8, 1), color);
  838. break;
  839. case NGUI_ICON_DISK:
  840. ngui_frame(ui, rect_make(x, y + 2, 16, 12), color);
  841. ngui_fill_rect(ui, rect_make(x + 2, y + 5, 12, 2), color);
  842. ngui_fill_rect(ui, rect_make(x + 11, y + 9, 3, 3), color);
  843. break;
  844. case NGUI_ICON_UP:
  845. ngui_line(ui, x + 8, y + 2, x + 3, y + 8, color);
  846. ngui_line(ui, x + 8, y + 2, x + 13, y + 8, color);
  847. ngui_fill_rect(ui, rect_make(x + 6, y + 7, 5, 7), color);
  848. break;
  849. case NGUI_ICON_HOME:
  850. ngui_line(ui, x + 2, y + 8, x + 8, y + 2, color);
  851. ngui_line(ui, x + 8, y + 2, x + 14, y + 8, color);
  852. ngui_frame(ui, rect_make(x + 3, y + 8, 10, 7), color);
  853. ngui_fill_rect(ui, rect_make(x + 7, y + 11, 3, 4), color);
  854. break;
  855. case NGUI_ICON_REFRESH:
  856. ngui_line(ui, x + 3, y + 8, x + 6, y + 4, color);
  857. ngui_line(ui, x + 6, y + 4, x + 10, y + 3, color);
  858. ngui_line(ui, x + 10, y + 3, x + 13, y + 6, color);
  859. ngui_line(ui, x + 13, y + 6, x + 13, y + 9, color);
  860. ngui_fill_rect(ui, rect_make(x + 11, y + 1, 4, 4), color);
  861. break;
  862. case NGUI_ICON_PLUS:
  863. ngui_fill_rect(ui, rect_make(x + 7, y + 3, 2, 10), color);
  864. ngui_fill_rect(ui, rect_make(x + 3, y + 7, 10, 2), color);
  865. break;
  866. case NGUI_ICON_CHEVRON_LEFT:
  867. ngui_line(ui, x + 10, y + 3, x + 6, y + 8, color);
  868. ngui_line(ui, x + 6, y + 8, x + 10, y + 13, color);
  869. break;
  870. case NGUI_ICON_CHEVRON_RIGHT:
  871. ngui_line(ui, x + 6, y + 3, x + 10, y + 8, color);
  872. ngui_line(ui, x + 10, y + 8, x + 6, y + 13, color);
  873. break;
  874. default:
  875. break;
  876. }
  877. }
  878. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  879. const char *label)
  880. {
  881. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  882. int press_hot = ui->pressed &&
  883. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  884. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  885. int fired = 0;
  886. unsigned short bg;
  887. int tw, tx, ty;
  888. if (press_hot)
  889. ui->active = id;
  890. if (ui->pressed && ui->released && press_hot && release_hot)
  891. fired = 1;
  892. if (ui->released && ui->active == id) {
  893. if (release_hot)
  894. fired = 1;
  895. ui->active = 0;
  896. }
  897. if (ui->active == id)
  898. bg = ui->theme.button_down;
  899. else if (hot)
  900. bg = ui->theme.button_hot;
  901. else
  902. bg = ui->theme.button;
  903. ngui_fill_rect(ui, r, bg);
  904. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  905. if (label && label[0]) {
  906. tw = (int)ngui_text_width_ui(ui, label);
  907. tx = r.x + (r.w - tw) / 2;
  908. ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
  909. if (ty < r.y)
  910. ty = r.y;
  911. ngui_push_clip(ui, r);
  912. ngui_text(ui, tx, ty, label, ui->theme.text);
  913. ngui_pop_clip(ui);
  914. }
  915. if (hot)
  916. ui->hot = id;
  917. return fired;
  918. }
  919. /* One row of a drop-down menu: left-aligned label, optional check mark and
  920. * right-aligned shortcut. Uses the same press/release/active semantics as
  921. * ngui_button so a menu and its caller never disagree about a click. */
  922. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  923. const char *label, const char *shortcut, int checked)
  924. {
  925. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  926. int press_hot = ui->pressed &&
  927. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  928. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  929. int fired = 0;
  930. int selected;
  931. unsigned short bg, fg;
  932. int ty;
  933. if (press_hot)
  934. ui->active = id;
  935. if (ui->pressed && ui->released && press_hot && release_hot)
  936. fired = 1;
  937. if (ui->released && ui->active == id) {
  938. if (release_hot)
  939. fired = 1;
  940. ui->active = 0;
  941. }
  942. selected = (ui->active == id) || hot;
  943. bg = selected ? ui->theme.selection : ui->theme.panel;
  944. fg = selected ? NGUI_RGB(255, 255, 255) : ui->theme.text;
  945. ngui_fill_rect(ui, r, bg);
  946. ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
  947. if (ty < r.y)
  948. ty = r.y;
  949. ngui_push_clip(ui, r);
  950. if (checked) {
  951. ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
  952. ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
  953. }
  954. if (label)
  955. ngui_text(ui, r.x + 22, ty, label, fg);
  956. if (shortcut) {
  957. int sw = (int)ngui_text_width_ui(ui, shortcut);
  958. ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
  959. }
  960. ngui_pop_clip(ui);
  961. if (hot)
  962. ui->hot = id;
  963. return fired;
  964. }
  965. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  966. enum ngui_icon icon){
  967. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  968. int press_hot = ui->pressed &&
  969. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  970. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  971. int fired = 0;
  972. unsigned short bg;
  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. if (ui->active == id)
  983. bg = ui->theme.button_down;
  984. else if (hot)
  985. bg = ui->theme.button_hot;
  986. else
  987. bg = ui->theme.button;
  988. ngui_fill_rect(ui, r, bg);
  989. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  990. ngui_icon(ui, r, icon, ui->theme.text);
  991. if (hot)
  992. ui->hot = id;
  993. return fired;
  994. }
  995. void ngui_input_set(struct ngui_input *in, const char *s)
  996. {
  997. scopy(in->text, s, NGUI_INPUT_MAX);
  998. in->len = slen(in->text);
  999. in->cursor = in->len;
  1000. in->scroll = 0;
  1001. in->anchor = in->cursor;
  1002. in->dragging = 0;
  1003. in->last_click_ms = 0;
  1004. }
  1005. static int input_word_char(char c)
  1006. {
  1007. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  1008. (c >= '0' && c <= '9') || c == '_';
  1009. }
  1010. /* Next word start after `pos` (Windows edit-control semantics). */
  1011. static unsigned int input_word_next(const struct ngui_input *in,
  1012. unsigned int pos)
  1013. {
  1014. while (pos < in->len && input_word_char(in->text[pos]))
  1015. pos++;
  1016. while (pos < in->len && !input_word_char(in->text[pos]))
  1017. pos++;
  1018. return pos;
  1019. }
  1020. /* Start of the previous word before `pos`. */
  1021. static unsigned int input_word_prev(const struct ngui_input *in,
  1022. unsigned int pos)
  1023. {
  1024. while (pos > 0 && !input_word_char(in->text[pos - 1]))
  1025. pos--;
  1026. while (pos > 0 && input_word_char(in->text[pos - 1]))
  1027. pos--;
  1028. return pos;
  1029. }
  1030. /* End of the word to the right of `pos`. A caret inside a word takes the
  1031. * rest of that word and its trailing separators; a caret on separators takes
  1032. * them plus the next word. */
  1033. static unsigned int input_word_delete_end(const struct ngui_input *in,
  1034. unsigned int pos)
  1035. {
  1036. if (pos < in->len && input_word_char(in->text[pos])) {
  1037. while (pos < in->len && input_word_char(in->text[pos]))
  1038. pos++;
  1039. while (pos < in->len && !input_word_char(in->text[pos]))
  1040. pos++;
  1041. return pos;
  1042. }
  1043. while (pos < in->len && !input_word_char(in->text[pos]))
  1044. pos++;
  1045. while (pos < in->len && input_word_char(in->text[pos]))
  1046. pos++;
  1047. return pos;
  1048. }
  1049. static void input_sel_bounds(const struct ngui_input *in,
  1050. unsigned int *a, unsigned int *b)
  1051. {
  1052. if (in->anchor < in->cursor) {
  1053. *a = in->anchor;
  1054. *b = in->cursor;
  1055. } else {
  1056. *a = in->cursor;
  1057. *b = in->anchor;
  1058. }
  1059. }
  1060. /* Remove [a, b) and keep cursor/anchor consistent. Returns 1 when the text
  1061. * changed. */
  1062. static int input_delete_range(struct ngui_input *in, unsigned int a,
  1063. unsigned int b)
  1064. {
  1065. unsigned int i;
  1066. if (a >= b || b > in->len)
  1067. return 0;
  1068. for (i = b; i < in->len; i++)
  1069. in->text[i - (b - a)] = in->text[i];
  1070. in->len -= b - a;
  1071. in->text[in->len] = '\0';
  1072. if (in->cursor >= b)
  1073. in->cursor -= b - a;
  1074. else if (in->cursor > a)
  1075. in->cursor = a;
  1076. if (in->anchor >= b)
  1077. in->anchor -= b - a;
  1078. else if (in->anchor > a)
  1079. in->anchor = a;
  1080. return 1;
  1081. }
  1082. static int input_delete_selection(struct ngui_input *in)
  1083. {
  1084. unsigned int a, b;
  1085. input_sel_bounds(in, &a, &b);
  1086. return input_delete_range(in, a, b);
  1087. }
  1088. static void input_insert(struct ngui_input *in, char c)
  1089. {
  1090. unsigned int i;
  1091. input_delete_selection(in);
  1092. if (in->len + 1 >= NGUI_INPUT_MAX)
  1093. return;
  1094. i = in->len;
  1095. while (i > in->cursor) {
  1096. in->text[i] = in->text[i - 1];
  1097. i--;
  1098. }
  1099. in->text[in->cursor] = c;
  1100. in->cursor++;
  1101. in->len++;
  1102. in->text[in->len] = '\0';
  1103. in->anchor = in->cursor;
  1104. }
  1105. static void input_delete(struct ngui_input *in, int back)
  1106. {
  1107. unsigned int pos;
  1108. if (back) {
  1109. if (in->cursor == 0)
  1110. return;
  1111. pos = in->cursor - 1;
  1112. } else {
  1113. if (in->cursor >= in->len)
  1114. return;
  1115. pos = in->cursor;
  1116. }
  1117. input_delete_range(in, pos, pos + 1);
  1118. }
  1119. static void input_move_to(struct ngui_input *in, unsigned int pos)
  1120. {
  1121. if (pos > in->len)
  1122. pos = in->len;
  1123. in->cursor = pos;
  1124. in->anchor = pos;
  1125. }
  1126. /* Two presses in the same field within this window select all of its text. */
  1127. #define NGUI_DOUBLE_CLICK_MS 500
  1128. static int input_text_w(struct ngui *ui, const char *s, unsigned int n)
  1129. {
  1130. return (int)ngui_text_width_px(ui, s, n, ui->font_px, 0, 0, 0);
  1131. }
  1132. /* Character position under a content-relative x, clamped to the visible
  1133. * part of the field. Proportional text means the prefix must be measured. */
  1134. static unsigned int input_pos_at(struct ngui *ui, const struct ngui_input *in,
  1135. int inner_x, int x)
  1136. {
  1137. unsigned int i, n, best = 0;
  1138. int rel = x - inner_x;
  1139. if (rel <= 0 || in->scroll >= in->len)
  1140. return in->scroll;
  1141. n = in->len - in->scroll;
  1142. for (i = 1; i <= n; i++) {
  1143. if (input_text_w(ui, in->text + in->scroll, i) > rel)
  1144. break;
  1145. best = i;
  1146. }
  1147. return in->scroll + best;
  1148. }
  1149. static void input_draw_selection(struct ngui *ui, const struct ngui_input *in,
  1150. struct ngui_rect inner)
  1151. {
  1152. unsigned int a, b, va, vb;
  1153. int xa, xb;
  1154. input_sel_bounds(in, &a, &b);
  1155. if (a == b || in->scroll >= in->len)
  1156. return;
  1157. va = a < in->scroll ? in->scroll : a;
  1158. vb = b < in->len ? b : in->len;
  1159. if (va >= vb)
  1160. return;
  1161. xa = input_text_w(ui, in->text + in->scroll, va - in->scroll);
  1162. xb = input_text_w(ui, in->text + in->scroll, vb - in->scroll);
  1163. if (xb > xa)
  1164. ngui_fill_rect(ui, rect_make(inner.x + xa, inner.y,
  1165. xb - xa,
  1166. ngui_text_line_height(ui,
  1167. ui->font_px,
  1168. 0)),
  1169. ui->theme.selection);
  1170. }
  1171. /* Scroll the first-visible-character index until the caret is visible. */
  1172. static void input_scroll_to_cursor(struct ngui *ui, struct ngui_input *in,
  1173. int inner_w)
  1174. {
  1175. if (in->scroll > in->len)
  1176. in->scroll = in->len;
  1177. if (in->cursor < in->scroll)
  1178. in->scroll = in->cursor;
  1179. if (inner_w <= 0) {
  1180. in->scroll = in->len;
  1181. return;
  1182. }
  1183. while (in->scroll < in->cursor &&
  1184. input_text_w(ui, in->text + in->scroll,
  1185. in->cursor - in->scroll) > inner_w)
  1186. in->scroll++;
  1187. if (in->scroll > in->len)
  1188. in->scroll = in->len;
  1189. }
  1190. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1191. struct ngui_input *in)
  1192. {
  1193. struct ngui_rect inner;
  1194. int focused = !in->disabled && (ui->focus == id);
  1195. int result = 0;
  1196. int avail, line_h, k;
  1197. int changed = 0;
  1198. line_h = ngui_text_line_height(ui, ui->font_px, 0);
  1199. if (line_h < 1)
  1200. line_h = 16;
  1201. inner = rect_make(r.x + 4, r.y + (r.h - line_h) / 2, r.w - 8,
  1202. line_h);
  1203. if (inner.w < 0)
  1204. inner.w = 0;
  1205. if (inner.h < 1)
  1206. inner.h = 1;
  1207. avail = inner.w;
  1208. if (in->disabled) {
  1209. /* A field that became disabled while focused gives the focus
  1210. * back, and a press cannot start a drag or a selection. */
  1211. if (ui->focus == id)
  1212. ngui_set_focus(ui, 0);
  1213. in->dragging = 0;
  1214. } else if (ui->pressed &&
  1215. ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
  1216. unsigned int now = u_ticks_ms();
  1217. int dbl = in->last_click_ms &&
  1218. (unsigned int)(now - in->last_click_ms) <=
  1219. NGUI_DOUBLE_CLICK_MS;
  1220. if (dbl) {
  1221. /* Double click: select the whole text and keep it
  1222. * selected while the second press is held. */
  1223. in->anchor = 0;
  1224. in->cursor = in->len;
  1225. in->dragging = 0;
  1226. } else {
  1227. unsigned int pos = input_pos_at(ui, in, inner.x,
  1228. ui->press_x);
  1229. in->cursor = pos;
  1230. in->anchor = pos;
  1231. in->dragging = 1;
  1232. }
  1233. in->last_click_ms = now;
  1234. ui->focus = id;
  1235. ui->focus_claimed = 1;
  1236. ui->last_blink = now;
  1237. ui->caret_on = 1;
  1238. focused = 1;
  1239. } else if (ui->pressed) {
  1240. in->dragging = 0;
  1241. }
  1242. /* Extend the selection while the button that started in this field is
  1243. * still held. Sampled coordinates may be outside the canvas. The
  1244. * release *event* is authoritative: the sampled button state can lag
  1245. * the event queue by a frame. */
  1246. if (!in->disabled && in->dragging && (ui->buttons & 1) &&
  1247. !ui->released) {
  1248. in->cursor = input_pos_at(ui, in, inner.x, ui->mouse_x);
  1249. /* Keep redrawing when the pointer leaves the window. */
  1250. ngui_invalidate(ui);
  1251. }
  1252. if (ui->released)
  1253. in->dragging = 0;
  1254. if (focused) {
  1255. while ((k = ngui_key(ui)) != 0) {
  1256. if (k == '\n' || k == '\r') {
  1257. result |= NGUI_INPUT_SUBMIT;
  1258. break;
  1259. }
  1260. if (k == NGUI_KEY_ESC || k == NGUI_KEY_TAB) {
  1261. result |= NGUI_INPUT_CANCEL;
  1262. ui->focus = 0;
  1263. break;
  1264. }
  1265. if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
  1266. if (!input_delete_selection(in))
  1267. input_delete(in, 1);
  1268. changed = 1;
  1269. } else if (k == NGUI_KEY_DELETE) {
  1270. if (!input_delete_selection(in))
  1271. input_delete(in, 0);
  1272. changed = 1;
  1273. } else if (k == NGUI_KEY_CTRL_BACKSPACE) {
  1274. if (input_delete_selection(in))
  1275. changed = 1;
  1276. else
  1277. changed = input_delete_range(in,
  1278. input_word_prev(in, in->cursor),
  1279. in->cursor);
  1280. } else if (k == NGUI_KEY_CTRL_DELETE) {
  1281. if (input_delete_selection(in)) {
  1282. changed = 1;
  1283. } else {
  1284. unsigned int a = in->cursor;
  1285. unsigned int b = input_word_delete_end(in,
  1286. a);
  1287. /* At the end of the line, delete the word
  1288. * before the caret instead of doing
  1289. * nothing. */
  1290. if (b == a) {
  1291. a = input_word_prev(in, in->cursor);
  1292. b = in->cursor;
  1293. }
  1294. changed = input_delete_range(in, a, b);
  1295. }
  1296. } else if (k == NGUI_KEY_LEFT) {
  1297. input_move_to(in, in->cursor > 0 ? in->cursor - 1 : 0);
  1298. } else if (k == NGUI_KEY_RIGHT) {
  1299. input_move_to(in, in->cursor < in->len
  1300. ? in->cursor + 1 : in->len);
  1301. } else if (k == NGUI_KEY_CTRL_LEFT) {
  1302. input_move_to(in, input_word_prev(in, in->cursor));
  1303. } else if (k == NGUI_KEY_CTRL_RIGHT) {
  1304. input_move_to(in, input_word_next(in, in->cursor));
  1305. } else if (k == NGUI_KEY_HOME) {
  1306. input_move_to(in, 0);
  1307. } else if (k == NGUI_KEY_END) {
  1308. input_move_to(in, in->len);
  1309. } else if (k == 0x01) { /* Ctrl+A: select all */
  1310. in->anchor = 0;
  1311. in->cursor = in->len;
  1312. } else if (k == 0x15) { /* Ctrl+U */
  1313. in->len = 0;
  1314. in->cursor = 0;
  1315. in->anchor = 0;
  1316. in->text[0] = '\0';
  1317. changed = 1;
  1318. } else if (k >= 0x20 && k <= 0x7E) {
  1319. input_insert(in, (char)k);
  1320. changed = 1;
  1321. }
  1322. }
  1323. }
  1324. ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
  1325. ngui_frame(ui, r, focused ? ui->theme.accent : ui->theme.border);
  1326. if (!in->disabled)
  1327. input_scroll_to_cursor(ui, in, avail);
  1328. ngui_push_clip(ui, inner);
  1329. if (in->len == 0 && in->placeholder) {
  1330. ngui_text_px(ui, inner.x, inner.y, in->placeholder,
  1331. slen(in->placeholder), ui->theme.text_dim,
  1332. ui->font_px, 0, 0, 0);
  1333. } else {
  1334. if (focused)
  1335. input_draw_selection(ui, in, inner);
  1336. ngui_text_px(ui, inner.x, inner.y, in->text + in->scroll,
  1337. slen(in->text + in->scroll),
  1338. in->disabled ? ui->theme.text_dim :
  1339. ui->theme.text,
  1340. ui->font_px, 0, 0, 0);
  1341. }
  1342. if (focused && ui->caret_on) {
  1343. int cx = 0;
  1344. if (in->cursor > in->scroll)
  1345. cx = input_text_w(ui, in->text + in->scroll,
  1346. in->cursor - in->scroll);
  1347. if (cx < 0)
  1348. cx = 0;
  1349. if (avail > 0 && cx >= avail)
  1350. cx = avail - 1;
  1351. if (avail > 0 && cx < avail)
  1352. ngui_fill_rect(ui, rect_make(inner.x + cx, inner.y,
  1353. 1, inner.h),
  1354. ui->theme.accent);
  1355. }
  1356. ngui_pop_clip(ui);
  1357. if (changed)
  1358. result |= NGUI_INPUT_CHANGED;
  1359. return result;
  1360. }
  1361. static int scrollable(const struct ngui_scroll *s)
  1362. {
  1363. return s->content_h > s->view_h && s->view_h > 0;
  1364. }
  1365. static int scroll_thumb_h(const struct ngui_rect *r,
  1366. const struct ngui_scroll *s)
  1367. {
  1368. int h = 0;
  1369. if (s->content_h > 0)
  1370. h = r->h * s->view_h / s->content_h;
  1371. if (h < 12)
  1372. h = 12;
  1373. if (h > r->h)
  1374. h = r->h;
  1375. return h;
  1376. }
  1377. static int scroll_thumb_y(const struct ngui_rect *r,
  1378. const struct ngui_scroll *s)
  1379. {
  1380. int th = scroll_thumb_h(r, s);
  1381. int avail = r->h - th;
  1382. int max = s->content_h - s->view_h;
  1383. if (avail <= 0 || max <= 0)
  1384. return r->y;
  1385. return r->y + s->offset * avail / max;
  1386. }
  1387. static void scroll_clamp(struct ngui_scroll *s)
  1388. {
  1389. int max = s->content_h - s->view_h;
  1390. if (max < 0)
  1391. max = 0;
  1392. if (s->offset > max)
  1393. s->offset = max;
  1394. if (s->offset < 0)
  1395. s->offset = 0;
  1396. }
  1397. void ngui_scroll_to(struct ngui_scroll *s, int offset)
  1398. {
  1399. s->offset = offset;
  1400. scroll_clamp(s);
  1401. }
  1402. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
  1403. {
  1404. if (y < s->offset)
  1405. s->offset = y;
  1406. else if (y + h > s->offset + s->view_h)
  1407. s->offset = y + h - s->view_h;
  1408. scroll_clamp(s);
  1409. }
  1410. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  1411. struct ngui_scroll *s)
  1412. {
  1413. struct ngui_rect bar;
  1414. int scrolls = scrollable(s);
  1415. s->view_h = r.h;
  1416. scroll_clamp(s);
  1417. if (!ui->buttons)
  1418. s->dragging = 0;
  1419. if (scrolls) {
  1420. int th = scroll_thumb_h(&r, s);
  1421. int ty = scroll_thumb_y(&r, s);
  1422. bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1423. if (ui->pressed &&
  1424. ngui_rect_contains(&bar, ui->press_x, ui->press_y)) {
  1425. if (ui->press_y >= ty && ui->press_y < ty + th) {
  1426. s->dragging = 1;
  1427. s->drag_dy = ui->press_y - ty;
  1428. } else {
  1429. int rel = ui->press_y - r.y - th / 2;
  1430. int avail = r.h - th;
  1431. int max = s->content_h - s->view_h;
  1432. if (rel < 0)
  1433. rel = 0;
  1434. if (rel > avail)
  1435. rel = avail;
  1436. if (avail > 0)
  1437. s->offset = rel * max / avail;
  1438. s->dragging = 1;
  1439. s->drag_dy = th / 2;
  1440. scroll_clamp(s);
  1441. }
  1442. }
  1443. /* Sampled buttons are authoritative while held: some event paths
  1444. * deliver the press late or coalesce it with the release. Start
  1445. * the drag from the current pointer position as well. */
  1446. if (!s->dragging && (ui->buttons & 1) &&
  1447. ngui_rect_contains(&bar, ui->mouse_x, ui->mouse_y)) {
  1448. if (ui->mouse_y >= ty && ui->mouse_y < ty + th) {
  1449. s->dragging = 1;
  1450. s->drag_dy = ui->mouse_y - ty;
  1451. } else {
  1452. int rel = ui->mouse_y - r.y - th / 2;
  1453. int avail = r.h - th;
  1454. int max = s->content_h - s->view_h;
  1455. if (rel < 0)
  1456. rel = 0;
  1457. if (rel > avail)
  1458. rel = avail;
  1459. if (avail > 0)
  1460. s->offset = rel * max / avail;
  1461. s->dragging = 1;
  1462. s->drag_dy = th / 2;
  1463. scroll_clamp(s);
  1464. }
  1465. }
  1466. if (s->dragging && (ui->buttons & 1)) {
  1467. int rel = ui->mouse_y - r.y - s->drag_dy;
  1468. int avail = r.h - th;
  1469. int max = s->content_h - s->view_h;
  1470. if (rel < 0)
  1471. rel = 0;
  1472. if (rel > avail)
  1473. rel = avail;
  1474. if (avail > 0)
  1475. s->offset = rel * max / avail;
  1476. scroll_clamp(s);
  1477. }
  1478. if (ui->released)
  1479. s->dragging = 0;
  1480. }
  1481. ngui_push_clip(ui, r);
  1482. }
  1483. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  1484. struct ngui_scroll *s)
  1485. {
  1486. ngui_pop_clip(ui);
  1487. if (!scrollable(s))
  1488. return;
  1489. {
  1490. struct ngui_rect bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1491. int th = scroll_thumb_h(&r, s);
  1492. int ty = scroll_thumb_y(&r, s);
  1493. struct ngui_rect thumb = rect_make(bar.x, ty, 14, th);
  1494. ngui_fill_rect(ui, bar, ui->theme.panel_alt);
  1495. ngui_fill_rect(ui, thumb, s->dragging ?
  1496. ui->theme.accent : ui->theme.text_dim);
  1497. }
  1498. }
  1499. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s)
  1500. {
  1501. int k = ngui_key_peek(ui);
  1502. int page;
  1503. if (k != NGUI_KEY_PAGE_UP && k != NGUI_KEY_PAGE_DOWN)
  1504. return 0;
  1505. page = s->view_h - 22;
  1506. if (page < 22)
  1507. page = 22;
  1508. ngui_key(ui);
  1509. if (k == NGUI_KEY_PAGE_UP)
  1510. s->offset -= page;
  1511. else
  1512. s->offset += page;
  1513. scroll_clamp(s);
  1514. return 1;
  1515. }