ngui.c 38 KB

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