ngui.c 33 KB

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