ngui.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. const struct ngui_theme ngui_theme_dark = {
  6. .bg = NGUI_RGB(24, 28, 40),
  7. .panel = NGUI_RGB(38, 43, 58),
  8. .panel_alt = NGUI_RGB(50, 56, 74),
  9. .border = NGUI_RGB(92, 99, 124),
  10. .text = NGUI_RGB(228, 230, 240),
  11. .text_dim = NGUI_RGB(145, 151, 170),
  12. .accent = NGUI_RGB(84, 132, 224),
  13. .selection = NGUI_RGB(54, 92, 166),
  14. .button = NGUI_RGB(54, 61, 80),
  15. .button_hot = NGUI_RGB(70, 81, 108),
  16. .button_down = NGUI_RGB(48, 112, 204),
  17. .icon_folder = NGUI_RGB(238, 198, 88),
  18. .icon_file = NGUI_RGB(204, 210, 226),
  19. .icon_disk = NGUI_RGB(112, 174, 232),
  20. .error = NGUI_RGB(238, 116, 116),
  21. };
  22. static unsigned int slen(const char *s)
  23. {
  24. unsigned int n = 0;
  25. while (s && s[n])
  26. n++;
  27. return n;
  28. }
  29. static void scopy(char *dst, const char *src, unsigned int cap)
  30. {
  31. unsigned int i = 0;
  32. if (!cap)
  33. return;
  34. while (src && src[i] && i + 1 < cap) {
  35. dst[i] = src[i];
  36. i++;
  37. }
  38. dst[i] = '\0';
  39. }
  40. static int rect_empty(const struct ngui_rect *r)
  41. {
  42. return r->w <= 0 || r->h <= 0;
  43. }
  44. static int rect_intersect(const struct ngui_rect *a, const struct ngui_rect *b,
  45. struct ngui_rect *out)
  46. {
  47. int x0 = a->x > b->x ? a->x : b->x;
  48. int y0 = a->y > b->y ? a->y : b->y;
  49. int x1 = a->x + a->w < b->x + b->w ? a->x + a->w : b->x + b->w;
  50. int y1 = a->y + a->h < b->y + b->h ? a->y + a->h : b->y + b->h;
  51. out->x = x0;
  52. out->y = y0;
  53. out->w = x1 - x0;
  54. out->h = y1 - y0;
  55. return !rect_empty(out);
  56. }
  57. static struct ngui_rect rect_make(int x, int y, int w, int h)
  58. {
  59. struct ngui_rect r;
  60. r.x = x;
  61. r.y = y;
  62. r.w = w;
  63. r.h = h;
  64. return r;
  65. }
  66. int ngui_rect_contains(const struct ngui_rect *r, int x, int y)
  67. {
  68. return x >= r->x && y >= r->y && x < r->x + r->w && y < r->y + r->h;
  69. }
  70. void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h)
  71. {
  72. unsigned int i;
  73. for (i = 0; i < sizeof(*ui); i++)
  74. ((unsigned char *)ui)[i] = 0;
  75. ui->px = fb;
  76. ui->w = w;
  77. ui->h = h;
  78. ui->theme = ngui_theme_dark;
  79. ui->nclip = 0;
  80. ui->dirty = 1;
  81. ui->caret_on = 1;
  82. ui->last_blink = u_ticks_ms();
  83. ui->last_mouse_x = -1;
  84. ui->last_mouse_y = -1;
  85. }
  86. void ngui_invalidate(struct ngui *ui)
  87. {
  88. ui->dirty = 1;
  89. }
  90. void ngui_set_focus(struct ngui *ui, unsigned int id)
  91. {
  92. if (ui->focus != id) {
  93. ui->focus = id;
  94. ui->last_blink = u_ticks_ms();
  95. ui->caret_on = 1;
  96. ui->dirty = 1;
  97. }
  98. }
  99. unsigned int ngui_focus(struct ngui *ui)
  100. {
  101. return ui->focus;
  102. }
  103. static void key_push(struct ngui *ui, int k)
  104. {
  105. unsigned int next = (ui->key_head + 1) % 32;
  106. if (next == ui->key_tail)
  107. return;
  108. ui->keys[ui->key_head] = k;
  109. ui->key_head = next;
  110. }
  111. int ngui_key_peek(struct ngui *ui)
  112. {
  113. if (ui->key_tail == ui->key_head)
  114. return 0;
  115. return ui->keys[ui->key_tail];
  116. }
  117. int ngui_key(struct ngui *ui)
  118. {
  119. int k = ngui_key_peek(ui);
  120. if (!k)
  121. return 0;
  122. ui->key_tail = (ui->key_tail + 1) % 32;
  123. return k;
  124. }
  125. int ngui_begin(struct ngui *ui)
  126. {
  127. struct win_info wi;
  128. struct win_pointer wp;
  129. struct win_event ev;
  130. unsigned int now;
  131. int want = ui->dirty;
  132. int i, k;
  133. ui->dirty = 0;
  134. ui->pressed = ui->released = 0;
  135. ui->clicked = 0;
  136. ui->focus_claimed = 0;
  137. ui->hot = 0;
  138. if (u_win_info(&wi) == 0) {
  139. if (wi.width != (unsigned int)ui->w ||
  140. wi.height != (unsigned int)ui->h) {
  141. if (u_win_ack_resize() == 0) {
  142. ui->px = (unsigned short *)wi.fb;
  143. ui->w = (int)wi.width;
  144. ui->h = (int)wi.height;
  145. }
  146. want = 1;
  147. } else if (wi.fb && wi.fb != (void *)ui->px) {
  148. ui->px = (unsigned short *)wi.fb;
  149. }
  150. }
  151. if (u_win_pointer(&wp) == 0) {
  152. ui->mouse_x = wp.x;
  153. ui->mouse_y = wp.y;
  154. ui->buttons = wp.buttons;
  155. ui->inside = wp.inside ? 1 : 0;
  156. ui->focused = wp.focused ? 1 : 0;
  157. } else {
  158. ui->mouse_x = ui->mouse_y = 0;
  159. ui->buttons = 0;
  160. ui->inside = 0;
  161. ui->focused = 0;
  162. }
  163. for (i = 0; i < 16; i++) {
  164. int r = u_win_event(&ev);
  165. if (r != 1)
  166. break;
  167. if (ev.type == WIN_EV_POINTER_DOWN) {
  168. ui->pressed |= 1;
  169. ui->press_x = ev.x;
  170. ui->press_y = ev.y;
  171. ui->clicked = 1;
  172. } else if (ev.type == WIN_EV_POINTER_UP) {
  173. ui->released |= 1;
  174. ui->release_x = ev.x;
  175. ui->release_y = ev.y;
  176. }
  177. }
  178. for (i = 0; i < 32; i++) {
  179. k = u_key_poll();
  180. if (!k)
  181. break;
  182. key_push(ui, k);
  183. want = 1;
  184. }
  185. now = u_ticks_ms();
  186. if (ui->focus) {
  187. if (now - ui->last_blink >= 500) {
  188. ui->last_blink = now;
  189. ui->caret_on = !ui->caret_on;
  190. want = 1;
  191. }
  192. } else {
  193. ui->caret_on = 1;
  194. }
  195. if (ui->inside != ui->last_inside) {
  196. want = 1;
  197. } else if (ui->inside &&
  198. (ui->mouse_x != ui->last_mouse_x ||
  199. ui->mouse_y != ui->last_mouse_y)) {
  200. want = 1;
  201. }
  202. if (ui->focused != ui->last_focused)
  203. want = 1;
  204. ui->last_mouse_x = ui->mouse_x;
  205. ui->last_mouse_y = ui->mouse_y;
  206. ui->last_inside = ui->inside;
  207. ui->last_focused = ui->focused;
  208. if (ui->pressed || ui->released)
  209. want = 1;
  210. return want;
  211. }
  212. void ngui_end(struct ngui *ui)
  213. {
  214. if (u_win_present() != 0)
  215. ui->dirty = 1;
  216. if (ui->clicked && !ui->focus_claimed)
  217. ui->focus = 0;
  218. if (ui->released || !ui->buttons)
  219. ui->active = 0;
  220. ui->pressed = ui->released = 0;
  221. ui->clicked = 0;
  222. ui->focus_claimed = 0;
  223. ui->press_x = ui->press_y = 0;
  224. ui->release_x = ui->release_y = 0;
  225. }
  226. struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h)
  227. {
  228. struct ngui_rect out = *r;
  229. if (h < 0)
  230. h = 0;
  231. if (h > r->h)
  232. h = r->h;
  233. out.h = h;
  234. r->y += h;
  235. r->h -= h;
  236. return out;
  237. }
  238. struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h)
  239. {
  240. struct ngui_rect out = *r;
  241. if (h < 0)
  242. h = 0;
  243. if (h > r->h)
  244. h = r->h;
  245. out.y = r->y + r->h - h;
  246. out.h = h;
  247. r->h -= h;
  248. return out;
  249. }
  250. struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w)
  251. {
  252. struct ngui_rect out = *r;
  253. if (w < 0)
  254. w = 0;
  255. if (w > r->w)
  256. w = r->w;
  257. out.w = w;
  258. r->x += w;
  259. r->w -= w;
  260. return out;
  261. }
  262. struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w)
  263. {
  264. struct ngui_rect out = *r;
  265. if (w < 0)
  266. w = 0;
  267. if (w > r->w)
  268. w = r->w;
  269. out.x = r->x + r->w - w;
  270. out.w = w;
  271. r->w -= w;
  272. return out;
  273. }
  274. void ngui_inset(struct ngui_rect *r, int n)
  275. {
  276. r->x += n;
  277. r->y += n;
  278. r->w -= n * 2;
  279. r->h -= n * 2;
  280. if (r->w < 0)
  281. r->w = 0;
  282. if (r->h < 0)
  283. r->h = 0;
  284. }
  285. void ngui_push_clip(struct ngui *ui, struct ngui_rect r)
  286. {
  287. struct ngui_rect out;
  288. struct ngui_rect canvas;
  289. if (ui->nclip >= 4)
  290. return;
  291. canvas = rect_make(0, 0, ui->w, ui->h);
  292. if (!rect_intersect(&r, &canvas, &out))
  293. out = rect_make(0, 0, 0, 0);
  294. if (ui->nclip > 0) {
  295. struct ngui_rect clipped = out;
  296. if (!rect_intersect(&clipped, &ui->clip[ui->nclip - 1], &out))
  297. out = rect_make(0, 0, 0, 0);
  298. }
  299. ui->clip[ui->nclip++] = out;
  300. }
  301. void ngui_pop_clip(struct ngui *ui)
  302. {
  303. if (ui->nclip > 0)
  304. ui->nclip--;
  305. }
  306. static int pixel_visible(struct ngui *ui, int x, int y)
  307. {
  308. struct ngui_rect *c;
  309. if (x < 0 || y < 0 || x >= ui->w || y >= ui->h)
  310. return 0;
  311. if (ui->nclip <= 0)
  312. return 1;
  313. c = &ui->clip[ui->nclip - 1];
  314. return x >= c->x && y >= c->y &&
  315. x < c->x + c->w && y < c->y + c->h;
  316. }
  317. void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color)
  318. {
  319. struct ngui_rect canvas;
  320. int x, y;
  321. if (rect_empty(&r))
  322. return;
  323. canvas = rect_make(0, 0, ui->w, ui->h);
  324. if (!rect_intersect(&r, &canvas, &r))
  325. return;
  326. if (ui->nclip > 0 &&
  327. !rect_intersect(&r, &ui->clip[ui->nclip - 1], &r))
  328. return;
  329. for (y = r.y; y < r.y + r.h; y++) {
  330. unsigned short *p = ui->px + y * ui->w + r.x;
  331. for (x = 0; x < r.w; x++)
  332. p[x] = color;
  333. }
  334. }
  335. void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color)
  336. {
  337. if (r.w <= 0 || r.h <= 0)
  338. return;
  339. ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), color);
  340. ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), color);
  341. if (r.h > 2) {
  342. ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), color);
  343. ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
  344. r.h - 2), color);
  345. }
  346. }
  347. static void ngui_pixel(struct ngui *ui, int x, int y, unsigned short color)
  348. {
  349. if (pixel_visible(ui, x, y))
  350. ui->px[y * ui->w + x] = color;
  351. }
  352. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  353. unsigned short color)
  354. {
  355. int dx = x1 - x0;
  356. int dy = y1 - y0;
  357. int sx = dx < 0 ? -1 : 1;
  358. int sy = dy < 0 ? -1 : 1;
  359. int err, e2;
  360. if (dx < 0)
  361. dx = -dx;
  362. if (dy < 0)
  363. dy = -dy;
  364. dy = -dy;
  365. err = dx + dy;
  366. for (;;) {
  367. ngui_pixel(ui, x0, y0, color);
  368. if (x0 == x1 && y0 == y1)
  369. break;
  370. e2 = err * 2;
  371. if (e2 >= dy) {
  372. err += dy;
  373. x0 += sx;
  374. }
  375. if (e2 <= dx) {
  376. err += dx;
  377. y0 += sy;
  378. }
  379. }
  380. }
  381. static void ngui_glyph(struct ngui *ui, int x, int y, unsigned char c,
  382. unsigned short color)
  383. {
  384. const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  385. int row, col;
  386. for (row = 0; row < FONT_8X16_ROWS; row++) {
  387. unsigned char bits = g[row];
  388. if (!bits)
  389. continue;
  390. for (col = 0; col < 8; col++)
  391. if (bits & (0x80u >> col))
  392. ngui_pixel(ui, x + col, y + row, color);
  393. }
  394. }
  395. unsigned int ngui_text_width(const char *s)
  396. {
  397. return slen(s) * 8;
  398. }
  399. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  400. unsigned short color)
  401. {
  402. int i;
  403. for (i = 0; s && s[i]; i++)
  404. ngui_glyph(ui, x + i * 8, y, (unsigned char)s[i], color);
  405. }
  406. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  407. const char *s, unsigned short color)
  408. {
  409. unsigned int n = slen(s);
  410. int max = (r.x + r.w - x) / 8;
  411. unsigned int i;
  412. if (max <= 0)
  413. return;
  414. if (n <= (unsigned int)max || max < 4) {
  415. for (i = 0; i < n && (int)i < max; i++)
  416. ngui_glyph(ui, x + (int)i * 8, y,
  417. (unsigned char)s[i], color);
  418. return;
  419. }
  420. for (i = 0; i < (unsigned int)(max - 3); i++)
  421. ngui_glyph(ui, x + (int)i * 8, y, (unsigned char)s[i], color);
  422. ngui_glyph(ui, x + (max - 3) * 8, y, '.', color);
  423. ngui_glyph(ui, x + (max - 2) * 8, y, '.', color);
  424. ngui_glyph(ui, x + (max - 1) * 8, y, '.', color);
  425. }
  426. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  427. unsigned short color)
  428. {
  429. int x = r.x + r.w / 2 - 8;
  430. int y = r.y + r.h / 2 - 8;
  431. switch (icon) {
  432. case NGUI_ICON_FOLDER:
  433. ngui_fill_rect(ui, rect_make(x + 1, y + 3, 7, 4), color);
  434. ngui_fill_rect(ui, rect_make(x, y + 6, 16, 9), color);
  435. break;
  436. case NGUI_ICON_FILE:
  437. ngui_frame(ui, rect_make(x + 2, y, 12, 16), color);
  438. ngui_line(ui, x + 10, y, x + 13, y + 3, color);
  439. ngui_fill_rect(ui, rect_make(x + 4, y + 7, 8, 1), color);
  440. ngui_fill_rect(ui, rect_make(x + 4, y + 10, 8, 1), color);
  441. break;
  442. case NGUI_ICON_DISK:
  443. ngui_frame(ui, rect_make(x, y + 2, 16, 12), color);
  444. ngui_fill_rect(ui, rect_make(x + 2, y + 5, 12, 2), color);
  445. ngui_fill_rect(ui, rect_make(x + 11, y + 9, 3, 3), color);
  446. break;
  447. case NGUI_ICON_UP:
  448. ngui_line(ui, x + 8, y + 2, x + 3, y + 8, color);
  449. ngui_line(ui, x + 8, y + 2, x + 13, y + 8, color);
  450. ngui_fill_rect(ui, rect_make(x + 6, y + 7, 5, 7), color);
  451. break;
  452. case NGUI_ICON_HOME:
  453. ngui_line(ui, x + 2, y + 8, x + 8, y + 2, color);
  454. ngui_line(ui, x + 8, y + 2, x + 14, y + 8, color);
  455. ngui_frame(ui, rect_make(x + 3, y + 8, 10, 7), color);
  456. ngui_fill_rect(ui, rect_make(x + 7, y + 11, 3, 4), color);
  457. break;
  458. case NGUI_ICON_REFRESH:
  459. ngui_line(ui, x + 3, y + 8, x + 6, y + 4, color);
  460. ngui_line(ui, x + 6, y + 4, x + 10, y + 3, color);
  461. ngui_line(ui, x + 10, y + 3, x + 13, y + 6, color);
  462. ngui_line(ui, x + 13, y + 6, x + 13, y + 9, color);
  463. ngui_fill_rect(ui, rect_make(x + 11, y + 1, 4, 4), color);
  464. break;
  465. case NGUI_ICON_PLUS:
  466. ngui_fill_rect(ui, rect_make(x + 7, y + 3, 2, 10), color);
  467. ngui_fill_rect(ui, rect_make(x + 3, y + 7, 10, 2), color);
  468. break;
  469. case NGUI_ICON_CHEVRON_LEFT:
  470. ngui_line(ui, x + 10, y + 3, x + 6, y + 8, color);
  471. ngui_line(ui, x + 6, y + 8, x + 10, y + 13, color);
  472. break;
  473. case NGUI_ICON_CHEVRON_RIGHT:
  474. ngui_line(ui, x + 6, y + 3, x + 10, y + 8, color);
  475. ngui_line(ui, x + 10, y + 8, x + 6, y + 13, color);
  476. break;
  477. default:
  478. break;
  479. }
  480. }
  481. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  482. const char *label)
  483. {
  484. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  485. int press_hot = ui->pressed &&
  486. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  487. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  488. int fired = 0;
  489. unsigned short bg;
  490. int tw, tx, ty;
  491. if (press_hot)
  492. ui->active = id;
  493. if (ui->pressed && ui->released && press_hot && release_hot)
  494. fired = 1;
  495. if (ui->released && ui->active == id) {
  496. if (release_hot)
  497. fired = 1;
  498. ui->active = 0;
  499. }
  500. if (ui->active == id)
  501. bg = ui->theme.button_down;
  502. else if (hot)
  503. bg = ui->theme.button_hot;
  504. else
  505. bg = ui->theme.button;
  506. ngui_fill_rect(ui, r, bg);
  507. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  508. if (label && label[0]) {
  509. tw = (int)ngui_text_width(label);
  510. tx = r.x + (r.w - tw) / 2;
  511. ty = r.y + (r.h - 16) / 2;
  512. ngui_push_clip(ui, r);
  513. ngui_text(ui, tx, ty, label, ui->theme.text);
  514. ngui_pop_clip(ui);
  515. }
  516. if (hot)
  517. ui->hot = id;
  518. return fired;
  519. }
  520. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  521. enum ngui_icon icon)
  522. {
  523. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  524. int press_hot = ui->pressed &&
  525. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  526. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  527. int fired = 0;
  528. unsigned short bg;
  529. if (press_hot)
  530. ui->active = id;
  531. if (ui->pressed && ui->released && press_hot && release_hot)
  532. fired = 1;
  533. if (ui->released && ui->active == id) {
  534. if (release_hot)
  535. fired = 1;
  536. ui->active = 0;
  537. }
  538. if (ui->active == id)
  539. bg = ui->theme.button_down;
  540. else if (hot)
  541. bg = ui->theme.button_hot;
  542. else
  543. bg = ui->theme.button;
  544. ngui_fill_rect(ui, r, bg);
  545. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  546. ngui_icon(ui, r, icon, ui->theme.text);
  547. if (hot)
  548. ui->hot = id;
  549. return fired;
  550. }
  551. void ngui_input_set(struct ngui_input *in, const char *s)
  552. {
  553. scopy(in->text, s, NGUI_INPUT_MAX);
  554. in->len = slen(in->text);
  555. in->cursor = in->len;
  556. in->scroll = 0;
  557. }
  558. static void input_insert(struct ngui_input *in, char c)
  559. {
  560. unsigned int i;
  561. if (in->len + 1 >= NGUI_INPUT_MAX)
  562. return;
  563. i = in->len;
  564. while (i > in->cursor) {
  565. in->text[i] = in->text[i - 1];
  566. i--;
  567. }
  568. in->text[in->cursor] = c;
  569. in->cursor++;
  570. in->len++;
  571. in->text[in->len] = '\0';
  572. }
  573. static void input_delete(struct ngui_input *in, int back)
  574. {
  575. unsigned int i, pos;
  576. if (back) {
  577. if (in->cursor == 0)
  578. return;
  579. pos = in->cursor - 1;
  580. } else {
  581. if (in->cursor >= in->len)
  582. return;
  583. pos = in->cursor;
  584. }
  585. i = pos;
  586. while (i + 1 < in->len) {
  587. in->text[i] = in->text[i + 1];
  588. i++;
  589. }
  590. in->len--;
  591. if (back)
  592. in->cursor--;
  593. in->text[in->len] = '\0';
  594. }
  595. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  596. struct ngui_input *in)
  597. {
  598. struct ngui_rect inner;
  599. int focused = (ui->focus == id);
  600. int result = 0;
  601. int cols, k;
  602. int changed = 0;
  603. if (ui->pressed &&
  604. ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
  605. int col = (ui->press_x - (r.x + 4)) / 8;
  606. unsigned int pos;
  607. if (col < 0)
  608. col = 0;
  609. pos = in->scroll + (unsigned int)col;
  610. if (pos > in->len)
  611. pos = in->len;
  612. in->cursor = pos;
  613. ui->focus = id;
  614. ui->focus_claimed = 1;
  615. ui->last_blink = u_ticks_ms();
  616. ui->caret_on = 1;
  617. focused = 1;
  618. }
  619. if (focused) {
  620. while ((k = ngui_key(ui)) != 0) {
  621. if (k == '\n' || k == '\r') {
  622. result |= NGUI_INPUT_SUBMIT;
  623. break;
  624. }
  625. if (k == NGUI_KEY_ESC || k == NGUI_KEY_TAB) {
  626. result |= NGUI_INPUT_CANCEL;
  627. ui->focus = 0;
  628. break;
  629. }
  630. if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
  631. input_delete(in, 1);
  632. changed = 1;
  633. } else if (k == NGUI_KEY_DELETE) {
  634. input_delete(in, 0);
  635. changed = 1;
  636. } else if (k == NGUI_KEY_LEFT) {
  637. if (in->cursor > 0)
  638. in->cursor--;
  639. } else if (k == NGUI_KEY_RIGHT) {
  640. if (in->cursor < in->len)
  641. in->cursor++;
  642. } else if (k == NGUI_KEY_HOME) {
  643. in->cursor = 0;
  644. } else if (k == NGUI_KEY_END) {
  645. in->cursor = in->len;
  646. } else if (k == 0x15) { /* Ctrl+U */
  647. in->len = 0;
  648. in->cursor = 0;
  649. in->text[0] = '\0';
  650. changed = 1;
  651. } else if (k >= 0x20 && k <= 0x7E) {
  652. input_insert(in, (char)k);
  653. changed = 1;
  654. }
  655. }
  656. }
  657. inner = rect_make(r.x + 4, r.y + (r.h - 16) / 2, r.w - 8, 16);
  658. if (inner.w < 0)
  659. inner.w = 0;
  660. ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
  661. ngui_frame(ui, r, focused ? ui->theme.accent : ui->theme.border);
  662. cols = inner.w / 8;
  663. if (cols < 0)
  664. cols = 0;
  665. if (in->scroll > in->len)
  666. in->scroll = in->len;
  667. if (in->cursor < in->scroll)
  668. in->scroll = in->cursor;
  669. if (cols > 0 && in->cursor >= in->scroll + (unsigned int)cols)
  670. in->scroll = in->cursor - (unsigned int)cols + 1;
  671. if (in->scroll > in->len)
  672. in->scroll = in->len;
  673. ngui_push_clip(ui, inner);
  674. if (in->len == 0 && in->placeholder)
  675. ngui_text(ui, inner.x, inner.y, in->placeholder,
  676. ui->theme.text_dim);
  677. else
  678. ngui_text(ui, inner.x, inner.y, in->text + in->scroll,
  679. ui->theme.text);
  680. if (focused && ui->caret_on) {
  681. int col = (int)in->cursor - (int)in->scroll;
  682. if (col >= 0 && col <= cols)
  683. ngui_fill_rect(ui, rect_make(inner.x + col * 8, inner.y,
  684. 1, 16), ui->theme.accent);
  685. }
  686. ngui_pop_clip(ui);
  687. if (changed)
  688. result |= NGUI_INPUT_CHANGED;
  689. return result;
  690. }
  691. static int scrollable(const struct ngui_scroll *s)
  692. {
  693. return s->content_h > s->view_h && s->view_h > 0;
  694. }
  695. static int scroll_thumb_h(const struct ngui_rect *r,
  696. const struct ngui_scroll *s)
  697. {
  698. int h = 0;
  699. if (s->content_h > 0)
  700. h = r->h * s->view_h / s->content_h;
  701. if (h < 12)
  702. h = 12;
  703. if (h > r->h)
  704. h = r->h;
  705. return h;
  706. }
  707. static int scroll_thumb_y(const struct ngui_rect *r,
  708. const struct ngui_scroll *s)
  709. {
  710. int th = scroll_thumb_h(r, s);
  711. int avail = r->h - th;
  712. int max = s->content_h - s->view_h;
  713. if (avail <= 0 || max <= 0)
  714. return r->y;
  715. return r->y + s->offset * avail / max;
  716. }
  717. static void scroll_clamp(struct ngui_scroll *s)
  718. {
  719. int max = s->content_h - s->view_h;
  720. if (max < 0)
  721. max = 0;
  722. if (s->offset > max)
  723. s->offset = max;
  724. if (s->offset < 0)
  725. s->offset = 0;
  726. }
  727. void ngui_scroll_to(struct ngui_scroll *s, int offset)
  728. {
  729. s->offset = offset;
  730. scroll_clamp(s);
  731. }
  732. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
  733. {
  734. if (y < s->offset)
  735. s->offset = y;
  736. else if (y + h > s->offset + s->view_h)
  737. s->offset = y + h - s->view_h;
  738. scroll_clamp(s);
  739. }
  740. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  741. struct ngui_scroll *s)
  742. {
  743. struct ngui_rect bar;
  744. int scrolls = scrollable(s);
  745. s->view_h = r.h;
  746. scroll_clamp(s);
  747. if (!ui->buttons)
  748. s->dragging = 0;
  749. if (scrolls) {
  750. int th = scroll_thumb_h(&r, s);
  751. int ty = scroll_thumb_y(&r, s);
  752. bar = rect_make(r.x + r.w - 9, r.y, 8, r.h);
  753. if (ui->pressed &&
  754. ngui_rect_contains(&bar, ui->press_x, ui->press_y)) {
  755. if (ui->press_y >= ty && ui->press_y < ty + th) {
  756. s->dragging = 1;
  757. s->drag_dy = ui->press_y - ty;
  758. } else {
  759. int rel = ui->press_y - r.y - th / 2;
  760. int avail = r.h - th;
  761. int max = s->content_h - s->view_h;
  762. if (rel < 0)
  763. rel = 0;
  764. if (rel > avail)
  765. rel = avail;
  766. if (avail > 0)
  767. s->offset = rel * max / avail;
  768. s->dragging = 1;
  769. s->drag_dy = th / 2;
  770. scroll_clamp(s);
  771. }
  772. }
  773. if (s->dragging && (ui->buttons & 1)) {
  774. int rel = ui->mouse_y - r.y - s->drag_dy;
  775. int avail = r.h - th;
  776. int max = s->content_h - s->view_h;
  777. if (rel < 0)
  778. rel = 0;
  779. if (rel > avail)
  780. rel = avail;
  781. if (avail > 0)
  782. s->offset = rel * max / avail;
  783. scroll_clamp(s);
  784. }
  785. if (ui->released)
  786. s->dragging = 0;
  787. }
  788. ngui_push_clip(ui, r);
  789. }
  790. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  791. struct ngui_scroll *s)
  792. {
  793. ngui_pop_clip(ui);
  794. if (!scrollable(s))
  795. return;
  796. {
  797. struct ngui_rect bar = rect_make(r.x + r.w - 9, r.y, 8, r.h);
  798. int th = scroll_thumb_h(&r, s);
  799. int ty = scroll_thumb_y(&r, s);
  800. struct ngui_rect thumb = rect_make(bar.x, ty, 8, th);
  801. ngui_fill_rect(ui, bar, ui->theme.panel_alt);
  802. ngui_fill_rect(ui, thumb, s->dragging ?
  803. ui->theme.accent : ui->theme.text_dim);
  804. }
  805. }
  806. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s)
  807. {
  808. int k = ngui_key_peek(ui);
  809. int page;
  810. if (k != NGUI_KEY_PAGE_UP && k != NGUI_KEY_PAGE_DOWN)
  811. return 0;
  812. page = s->view_h - 22;
  813. if (page < 22)
  814. page = 22;
  815. ngui_key(ui);
  816. if (k == NGUI_KEY_PAGE_UP)
  817. s->offset -= page;
  818. else
  819. s->offset += page;
  820. scroll_clamp(s);
  821. return 1;
  822. }