ngui.c 42 KB

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