ngui.c 42 KB

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