ngui.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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 void ngui_bitmap_text(struct ngui *ui, int x, int y, const char *s,
  601. unsigned short color)
  602. {
  603. unsigned int i;
  604. for (i = 0; s && s[i]; i++)
  605. ngui_bitmap_glyph(ui, x + (int)i * 8, y,
  606. (unsigned char)s[i], color, 1, 0);
  607. }
  608. static struct ufont_face *ngui_face(struct ngui *ui, int bold, int italic,
  609. int mono)
  610. {
  611. struct ufont_face *f;
  612. if (!ui)
  613. return 0;
  614. if (mono && ui->font_mono)
  615. f = ui->font_mono;
  616. else if (bold && italic)
  617. f = ui->font_bold_italic;
  618. else if (bold)
  619. f = ui->font_bold;
  620. else if (italic)
  621. f = ui->font_italic;
  622. else
  623. f = ui->font;
  624. if (!f)
  625. f = ui->font;
  626. return f;
  627. }
  628. static void ngui_clip_bounds(struct ngui *ui, int *cx, int *cy,
  629. int *cw, int *ch)
  630. {
  631. struct ngui_rect r = { 0, 0, ui->w, ui->h };
  632. int i;
  633. for (i = 0; i < ui->nclip; i++) {
  634. struct ngui_rect out;
  635. if (!rect_intersect(&r, &ui->clip[i], &out)) {
  636. r.w = r.h = 0;
  637. break;
  638. }
  639. r = out;
  640. }
  641. *cx = r.x;
  642. *cy = r.y;
  643. *cw = r.w;
  644. *ch = r.h;
  645. }
  646. int ngui_text_ascent(struct ngui *ui, int px, int mono)
  647. {
  648. struct ufont_face *f = ngui_face(ui, 0, 0, mono);
  649. struct ufont_metrics m;
  650. if (f) {
  651. ufont_metrics(f, px, &m);
  652. return m.ascent;
  653. }
  654. return 16 * (px >= 24 ? 2 : 1);
  655. }
  656. int ngui_text_line_height(struct ngui *ui, int px, int mono)
  657. {
  658. struct ufont_face *f = ngui_face(ui, 0, 0, mono);
  659. struct ufont_metrics m;
  660. if (f) {
  661. ufont_metrics(f, px, &m);
  662. return m.line_height;
  663. }
  664. return 16 * (px >= 24 ? 2 : 1);
  665. }
  666. unsigned int ngui_text_width_px(struct ngui *ui, const char *s, unsigned int n,
  667. int px, int bold, int italic, int mono)
  668. {
  669. struct ufont_face *f = ngui_face(ui, bold, italic, mono);
  670. int scale;
  671. if (f)
  672. return (unsigned int)ufont_text_width(f, px, s, (int)n, 0);
  673. scale = px >= 24 ? 2 : 1;
  674. return n * 8u * (unsigned int)scale;
  675. }
  676. unsigned int ngui_text_width_ui(struct ngui *ui, const char *s)
  677. {
  678. return ngui_text_width_px(ui, s, slen(s),
  679. ui ? ui->font_px : 14, 0, 0, 0);
  680. }
  681. unsigned int ngui_text_width(const char *s)
  682. {
  683. return slen(s) * 8;
  684. }
  685. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale)
  686. {
  687. (void)s;
  688. if (scale < 1)
  689. scale = 1;
  690. return n * 8u * (unsigned int)scale;
  691. }
  692. void ngui_text_px(struct ngui *ui, int x, int y, const char *s,
  693. unsigned int n, unsigned short color, int px, int bold,
  694. int italic, int mono)
  695. {
  696. struct ufont_face *f = ngui_face(ui, bold, italic, mono);
  697. int scale, i, pen = 0, ascent;
  698. int cx, cy, cw, ch;
  699. if (!ui || !ui->px || !s)
  700. return;
  701. if (!f) {
  702. scale = px >= 24 ? 2 : 1;
  703. for (i = 0; i < (int)n && s[i]; i++)
  704. ngui_bitmap_glyph(ui, x + i * 8 * scale, y,
  705. (unsigned char)s[i], color,
  706. scale, bold);
  707. return;
  708. }
  709. ascent = ngui_text_ascent(ui, px, mono);
  710. ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
  711. for (i = 0; i < (int)n && s[i]; i++) {
  712. const struct ufont_glyph *g =
  713. ufont_glyph(f, px, (unsigned char)s[i]);
  714. if (!g) {
  715. pen += 8 << 6;
  716. continue;
  717. }
  718. ufont_blit_rgb565(ui->px, ui->w, ui->h,
  719. x + (pen >> 6), y + ascent, g, color,
  720. cx, cy, cw, ch);
  721. pen += g->advance;
  722. }
  723. }
  724. void ngui_text_n(struct ngui *ui, int x, int y, const char *s, unsigned int n,
  725. unsigned short color)
  726. {
  727. ngui_text_px(ui, x, y, s, n, color,
  728. ui ? ui->font_px : 14, 0, 0, 0);
  729. }
  730. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  731. unsigned short color)
  732. {
  733. ngui_text_n(ui, x, y, s, slen(s), color);
  734. }
  735. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  736. unsigned int n, unsigned short color, int scale, int bold)
  737. {
  738. if (scale < 1)
  739. scale = 1;
  740. if (ui && ui->font) {
  741. ngui_text_px(ui, x, y, s, n, color,
  742. ui->font_px * scale, bold, 0, 0);
  743. return;
  744. }
  745. {
  746. unsigned int i;
  747. for (i = 0; i < n && s && s[i]; i++)
  748. ngui_bitmap_glyph(ui, x + (int)i * 8 * scale, y,
  749. (unsigned char)s[i], color,
  750. scale, bold);
  751. }
  752. }
  753. /* Draw a string upside down. The whole n-character block is rotated, so the
  754. * first character ends up on the right; used for the mirrored corner index on
  755. * a playing card. */
  756. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  757. unsigned int n, unsigned short color)
  758. {
  759. unsigned int i;
  760. int row, col;
  761. for (i = 0; i < n && s && s[i]; i++) {
  762. const unsigned char *g =
  763. &font_8x16[(unsigned int)(unsigned char)s[i] *
  764. FONT_8X16_ROWS];
  765. for (row = 0; row < FONT_8X16_ROWS; row++) {
  766. unsigned char bits = g[row];
  767. if (!bits)
  768. continue;
  769. for (col = 0; col < 8; col++) {
  770. if (!(bits & (0x80u >> col)))
  771. continue;
  772. ngui_pixel(ui,
  773. x + (int)(n - 1 - i) * 8 + 7 - col,
  774. y + FONT_8X16_ROWS - 1 - row,
  775. color);
  776. }
  777. }
  778. }
  779. }
  780. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  781. const char *s, unsigned short color)
  782. {
  783. unsigned int n = slen(s);
  784. int avail = r.x + r.w - x;
  785. unsigned int i, best = 0;
  786. int dots;
  787. if (avail <= 0)
  788. return;
  789. if (ui && ui->font) {
  790. dots = (int)ngui_text_width_px(ui, "...", 3, ui->font_px,
  791. 0, 0, 0);
  792. for (i = 0; i <= n; i++) {
  793. int w = (int)ngui_text_width_px(ui, s, i, ui->font_px,
  794. 0, 0, 0);
  795. if (w > avail)
  796. break;
  797. best = i;
  798. }
  799. if (best == n) {
  800. ngui_text_n(ui, x, y, s, n, color);
  801. return;
  802. }
  803. while (best > 0 &&
  804. (int)ngui_text_width_px(ui, s, best, ui->font_px,
  805. 0, 0, 0) + dots > avail)
  806. best--;
  807. ngui_text_n(ui, x, y, s, best, color);
  808. ngui_text_n(ui, x + (int)ngui_text_width_px(ui, s, best,
  809. ui->font_px, 0, 0, 0),
  810. y, "...", 3, color);
  811. return;
  812. }
  813. {
  814. int max = avail / 8;
  815. if (max <= 0)
  816. return;
  817. if (n <= (unsigned int)max || max < 4) {
  818. for (i = 0; i < n && (int)i < max; i++)
  819. ngui_bitmap_glyph(ui, x + (int)i * 8, y,
  820. (unsigned char)s[i], color, 1, 0);
  821. return;
  822. }
  823. for (i = 0; i < (unsigned int)(max - 3); i++)
  824. ngui_bitmap_glyph(ui, x + (int)i * 8, y,
  825. (unsigned char)s[i], color, 1, 0);
  826. ngui_bitmap_glyph(ui, x + (max - 3) * 8, y, '.', color, 1, 0);
  827. ngui_bitmap_glyph(ui, x + (max - 2) * 8, y, '.', color, 1, 0);
  828. ngui_bitmap_glyph(ui, x + (max - 1) * 8, y, '.', color, 1, 0);
  829. }
  830. }
  831. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  832. unsigned short color)
  833. {
  834. int x = r.x + r.w / 2 - 8;
  835. int y = r.y + r.h / 2 - 8;
  836. switch (icon) {
  837. case NGUI_ICON_FOLDER:
  838. ngui_fill_rect(ui, rect_make(x + 1, y + 3, 7, 4), color);
  839. ngui_fill_rect(ui, rect_make(x, y + 6, 16, 9), color);
  840. break;
  841. case NGUI_ICON_FILE:
  842. ngui_frame(ui, rect_make(x + 2, y, 12, 16), color);
  843. ngui_line(ui, x + 10, y, x + 13, y + 3, color);
  844. ngui_fill_rect(ui, rect_make(x + 4, y + 7, 8, 1), color);
  845. ngui_fill_rect(ui, rect_make(x + 4, y + 10, 8, 1), color);
  846. break;
  847. case NGUI_ICON_DISK:
  848. ngui_frame(ui, rect_make(x, y + 2, 16, 12), color);
  849. ngui_fill_rect(ui, rect_make(x + 2, y + 5, 12, 2), color);
  850. ngui_fill_rect(ui, rect_make(x + 11, y + 9, 3, 3), color);
  851. break;
  852. case NGUI_ICON_UP:
  853. ngui_line(ui, x + 8, y + 2, x + 3, y + 8, color);
  854. ngui_line(ui, x + 8, y + 2, x + 13, y + 8, color);
  855. ngui_fill_rect(ui, rect_make(x + 6, y + 7, 5, 7), color);
  856. break;
  857. case NGUI_ICON_HOME:
  858. ngui_line(ui, x + 2, y + 8, x + 8, y + 2, color);
  859. ngui_line(ui, x + 8, y + 2, x + 14, y + 8, color);
  860. ngui_frame(ui, rect_make(x + 3, y + 8, 10, 7), color);
  861. ngui_fill_rect(ui, rect_make(x + 7, y + 11, 3, 4), color);
  862. break;
  863. case NGUI_ICON_REFRESH:
  864. ngui_line(ui, x + 3, y + 8, x + 6, y + 4, color);
  865. ngui_line(ui, x + 6, y + 4, x + 10, y + 3, color);
  866. ngui_line(ui, x + 10, y + 3, x + 13, y + 6, color);
  867. ngui_line(ui, x + 13, y + 6, x + 13, y + 9, color);
  868. ngui_fill_rect(ui, rect_make(x + 11, y + 1, 4, 4), color);
  869. break;
  870. case NGUI_ICON_PLUS:
  871. ngui_fill_rect(ui, rect_make(x + 7, y + 3, 2, 10), color);
  872. ngui_fill_rect(ui, rect_make(x + 3, y + 7, 10, 2), color);
  873. break;
  874. case NGUI_ICON_CHEVRON_LEFT:
  875. ngui_line(ui, x + 10, y + 3, x + 6, y + 8, color);
  876. ngui_line(ui, x + 6, y + 8, x + 10, y + 13, color);
  877. break;
  878. case NGUI_ICON_CHEVRON_RIGHT:
  879. ngui_line(ui, x + 6, y + 3, x + 10, y + 8, color);
  880. ngui_line(ui, x + 10, y + 8, x + 6, y + 13, color);
  881. break;
  882. default:
  883. break;
  884. }
  885. }
  886. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  887. const char *label)
  888. {
  889. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  890. int press_hot = ui->pressed &&
  891. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  892. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  893. int fired = 0;
  894. unsigned short bg;
  895. int tw, tx, ty;
  896. if (press_hot)
  897. ui->active = id;
  898. if (ui->pressed && ui->released && press_hot && release_hot)
  899. fired = 1;
  900. if (ui->released && ui->active == id) {
  901. if (release_hot)
  902. fired = 1;
  903. ui->active = 0;
  904. }
  905. if (ui->active == id)
  906. bg = ui->theme.button_down;
  907. else if (hot)
  908. bg = ui->theme.button_hot;
  909. else
  910. bg = ui->theme.button;
  911. ngui_fill_rect(ui, r, bg);
  912. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  913. if (label && label[0]) {
  914. tw = (int)ngui_text_width_ui(ui, label);
  915. tx = r.x + (r.w - tw) / 2;
  916. ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
  917. if (ty < r.y)
  918. ty = r.y;
  919. ngui_push_clip(ui, r);
  920. ngui_text(ui, tx, ty, label, ui->theme.text);
  921. ngui_pop_clip(ui);
  922. }
  923. if (hot)
  924. ui->hot = id;
  925. return fired;
  926. }
  927. /* One row of a drop-down menu: left-aligned label, optional check mark and
  928. * right-aligned shortcut. Uses the same press/release/active semantics as
  929. * ngui_button so a menu and its caller never disagree about a click. */
  930. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  931. const char *label, const char *shortcut, int checked)
  932. {
  933. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  934. int press_hot = ui->pressed &&
  935. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  936. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  937. int fired = 0;
  938. int selected;
  939. unsigned short bg, fg;
  940. int ty;
  941. if (press_hot)
  942. ui->active = id;
  943. if (ui->pressed && ui->released && press_hot && release_hot)
  944. fired = 1;
  945. if (ui->released && ui->active == id) {
  946. if (release_hot)
  947. fired = 1;
  948. ui->active = 0;
  949. }
  950. selected = (ui->active == id) || hot;
  951. bg = selected ? ui->theme.selection : ui->theme.panel;
  952. fg = selected ? NGUI_RGB(255, 255, 255) : ui->theme.text;
  953. ngui_fill_rect(ui, r, bg);
  954. ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
  955. if (ty < r.y)
  956. ty = r.y;
  957. ngui_push_clip(ui, r);
  958. if (checked) {
  959. ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
  960. ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
  961. }
  962. if (label)
  963. ngui_text(ui, r.x + 22, ty, label, fg);
  964. if (shortcut) {
  965. int sw = (int)ngui_text_width_ui(ui, shortcut);
  966. ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
  967. }
  968. ngui_pop_clip(ui);
  969. if (hot)
  970. ui->hot = id;
  971. return fired;
  972. }
  973. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  974. enum ngui_icon icon){
  975. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  976. int press_hot = ui->pressed &&
  977. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  978. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  979. int fired = 0;
  980. unsigned short bg;
  981. if (press_hot)
  982. ui->active = id;
  983. if (ui->pressed && ui->released && press_hot && release_hot)
  984. fired = 1;
  985. if (ui->released && ui->active == id) {
  986. if (release_hot)
  987. fired = 1;
  988. ui->active = 0;
  989. }
  990. if (ui->active == id)
  991. bg = ui->theme.button_down;
  992. else if (hot)
  993. bg = ui->theme.button_hot;
  994. else
  995. bg = ui->theme.button;
  996. ngui_fill_rect(ui, r, bg);
  997. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  998. ngui_icon(ui, r, icon, ui->theme.text);
  999. if (hot)
  1000. ui->hot = id;
  1001. return fired;
  1002. }
  1003. void ngui_input_set(struct ngui_input *in, const char *s)
  1004. {
  1005. scopy(in->text, s, NGUI_INPUT_MAX);
  1006. in->len = slen(in->text);
  1007. in->cursor = in->len;
  1008. in->scroll = 0;
  1009. in->anchor = in->cursor;
  1010. in->dragging = 0;
  1011. in->last_click_ms = 0;
  1012. }
  1013. static int input_word_char(char c)
  1014. {
  1015. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  1016. (c >= '0' && c <= '9') || c == '_';
  1017. }
  1018. /* Next word start after `pos` (Windows edit-control semantics). */
  1019. static unsigned int input_word_next(const struct ngui_input *in,
  1020. unsigned int pos)
  1021. {
  1022. while (pos < in->len && input_word_char(in->text[pos]))
  1023. pos++;
  1024. while (pos < in->len && !input_word_char(in->text[pos]))
  1025. pos++;
  1026. return pos;
  1027. }
  1028. /* Start of the previous word before `pos`. */
  1029. static unsigned int input_word_prev(const struct ngui_input *in,
  1030. unsigned int pos)
  1031. {
  1032. while (pos > 0 && !input_word_char(in->text[pos - 1]))
  1033. pos--;
  1034. while (pos > 0 && input_word_char(in->text[pos - 1]))
  1035. pos--;
  1036. return pos;
  1037. }
  1038. /* End of the word to the right of `pos`. A caret inside a word takes the
  1039. * rest of that word and its trailing separators; a caret on separators takes
  1040. * them plus the next word. */
  1041. static unsigned int input_word_delete_end(const struct ngui_input *in,
  1042. unsigned int pos)
  1043. {
  1044. if (pos < in->len && input_word_char(in->text[pos])) {
  1045. while (pos < in->len && input_word_char(in->text[pos]))
  1046. pos++;
  1047. while (pos < in->len && !input_word_char(in->text[pos]))
  1048. pos++;
  1049. return pos;
  1050. }
  1051. while (pos < in->len && !input_word_char(in->text[pos]))
  1052. pos++;
  1053. while (pos < in->len && input_word_char(in->text[pos]))
  1054. pos++;
  1055. return pos;
  1056. }
  1057. static void input_sel_bounds(const struct ngui_input *in,
  1058. unsigned int *a, unsigned int *b)
  1059. {
  1060. if (in->anchor < in->cursor) {
  1061. *a = in->anchor;
  1062. *b = in->cursor;
  1063. } else {
  1064. *a = in->cursor;
  1065. *b = in->anchor;
  1066. }
  1067. }
  1068. /* Remove [a, b) and keep cursor/anchor consistent. Returns 1 when the text
  1069. * changed. */
  1070. static int input_delete_range(struct ngui_input *in, unsigned int a,
  1071. unsigned int b)
  1072. {
  1073. unsigned int i;
  1074. if (a >= b || b > in->len)
  1075. return 0;
  1076. for (i = b; i < in->len; i++)
  1077. in->text[i - (b - a)] = in->text[i];
  1078. in->len -= b - a;
  1079. in->text[in->len] = '\0';
  1080. if (in->cursor >= b)
  1081. in->cursor -= b - a;
  1082. else if (in->cursor > a)
  1083. in->cursor = a;
  1084. if (in->anchor >= b)
  1085. in->anchor -= b - a;
  1086. else if (in->anchor > a)
  1087. in->anchor = a;
  1088. return 1;
  1089. }
  1090. static int input_delete_selection(struct ngui_input *in)
  1091. {
  1092. unsigned int a, b;
  1093. input_sel_bounds(in, &a, &b);
  1094. return input_delete_range(in, a, b);
  1095. }
  1096. static void input_insert(struct ngui_input *in, char c)
  1097. {
  1098. unsigned int i;
  1099. input_delete_selection(in);
  1100. if (in->len + 1 >= NGUI_INPUT_MAX)
  1101. return;
  1102. i = in->len;
  1103. while (i > in->cursor) {
  1104. in->text[i] = in->text[i - 1];
  1105. i--;
  1106. }
  1107. in->text[in->cursor] = c;
  1108. in->cursor++;
  1109. in->len++;
  1110. in->text[in->len] = '\0';
  1111. in->anchor = in->cursor;
  1112. }
  1113. static void input_delete(struct ngui_input *in, int back)
  1114. {
  1115. unsigned int pos;
  1116. if (back) {
  1117. if (in->cursor == 0)
  1118. return;
  1119. pos = in->cursor - 1;
  1120. } else {
  1121. if (in->cursor >= in->len)
  1122. return;
  1123. pos = in->cursor;
  1124. }
  1125. input_delete_range(in, pos, pos + 1);
  1126. }
  1127. static void input_move_to(struct ngui_input *in, unsigned int pos)
  1128. {
  1129. if (pos > in->len)
  1130. pos = in->len;
  1131. in->cursor = pos;
  1132. in->anchor = pos;
  1133. }
  1134. /* Two presses in the same field within this window select all of its text. */
  1135. #define NGUI_DOUBLE_CLICK_MS 500
  1136. /* Character position under a content-relative x, clamped to the visible
  1137. * part of the field. */
  1138. static unsigned int input_pos_at(const struct ngui_input *in, int inner_x,
  1139. int cols, int x)
  1140. {
  1141. int col = (x - inner_x) / 8;
  1142. unsigned int pos;
  1143. if (col < 0)
  1144. col = 0;
  1145. if (cols > 0 && col > cols)
  1146. col = cols;
  1147. pos = in->scroll + (unsigned int)col;
  1148. if (pos > in->len)
  1149. pos = in->len;
  1150. return pos;
  1151. }
  1152. static void input_draw_selection(struct ngui *ui, const struct ngui_input *in,
  1153. struct ngui_rect inner, int cols)
  1154. {
  1155. unsigned int a, b, va, vb;
  1156. input_sel_bounds(in, &a, &b);
  1157. if (a == b || cols <= 0)
  1158. return;
  1159. va = a < in->scroll ? in->scroll : a;
  1160. vb = b > in->scroll + (unsigned int)cols
  1161. ? in->scroll + (unsigned int)cols : b;
  1162. if (va < vb)
  1163. ngui_fill_rect(ui, rect_make(inner.x + (int)(va - in->scroll) * 8,
  1164. inner.y, (int)(vb - va) * 8, 16),
  1165. ui->theme.selection);
  1166. }
  1167. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1168. struct ngui_input *in)
  1169. {
  1170. struct ngui_rect inner;
  1171. int focused = (ui->focus == id);
  1172. int result = 0;
  1173. int cols, k;
  1174. int changed = 0;
  1175. inner = rect_make(r.x + 4, r.y + (r.h - 16) / 2, r.w - 8, 16);
  1176. if (inner.w < 0)
  1177. inner.w = 0;
  1178. cols = inner.w / 8;
  1179. if (cols < 0)
  1180. cols = 0;
  1181. if (ui->pressed &&
  1182. ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
  1183. unsigned int now = u_ticks_ms();
  1184. int dbl = in->last_click_ms &&
  1185. (unsigned int)(now - in->last_click_ms) <=
  1186. NGUI_DOUBLE_CLICK_MS;
  1187. if (dbl) {
  1188. /* Double click: select the whole text and keep it
  1189. * selected while the second press is held. */
  1190. in->anchor = 0;
  1191. in->cursor = in->len;
  1192. in->dragging = 0;
  1193. } else {
  1194. unsigned int pos = input_pos_at(in, inner.x, cols,
  1195. ui->press_x);
  1196. in->cursor = pos;
  1197. in->anchor = pos;
  1198. in->dragging = 1;
  1199. }
  1200. in->last_click_ms = now;
  1201. ui->focus = id;
  1202. ui->focus_claimed = 1;
  1203. ui->last_blink = now;
  1204. ui->caret_on = 1;
  1205. focused = 1;
  1206. } else if (ui->pressed) {
  1207. in->dragging = 0;
  1208. }
  1209. /* Extend the selection while the button that started in this field is
  1210. * still held. Sampled coordinates may be outside the canvas. The
  1211. * release *event* is authoritative: the sampled button state can lag
  1212. * the event queue by a frame. */
  1213. if (in->dragging && (ui->buttons & 1) && !ui->released) {
  1214. in->cursor = input_pos_at(in, inner.x, cols, ui->mouse_x);
  1215. /* Keep redrawing when the pointer leaves the window. */
  1216. ngui_invalidate(ui);
  1217. }
  1218. if (ui->released)
  1219. in->dragging = 0;
  1220. if (focused) {
  1221. while ((k = ngui_key(ui)) != 0) {
  1222. if (k == '\n' || k == '\r') {
  1223. result |= NGUI_INPUT_SUBMIT;
  1224. break;
  1225. }
  1226. if (k == NGUI_KEY_ESC || k == NGUI_KEY_TAB) {
  1227. result |= NGUI_INPUT_CANCEL;
  1228. ui->focus = 0;
  1229. break;
  1230. }
  1231. if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
  1232. if (!input_delete_selection(in))
  1233. input_delete(in, 1);
  1234. changed = 1;
  1235. } else if (k == NGUI_KEY_DELETE) {
  1236. if (!input_delete_selection(in))
  1237. input_delete(in, 0);
  1238. changed = 1;
  1239. } else if (k == NGUI_KEY_CTRL_BACKSPACE) {
  1240. if (input_delete_selection(in))
  1241. changed = 1;
  1242. else
  1243. changed = input_delete_range(in,
  1244. input_word_prev(in, in->cursor),
  1245. in->cursor);
  1246. } else if (k == NGUI_KEY_CTRL_DELETE) {
  1247. if (input_delete_selection(in)) {
  1248. changed = 1;
  1249. } else {
  1250. unsigned int a = in->cursor;
  1251. unsigned int b = input_word_delete_end(in,
  1252. a);
  1253. /* At the end of the line, delete the word
  1254. * before the caret instead of doing
  1255. * nothing. */
  1256. if (b == a) {
  1257. a = input_word_prev(in, in->cursor);
  1258. b = in->cursor;
  1259. }
  1260. changed = input_delete_range(in, a, b);
  1261. }
  1262. } else if (k == NGUI_KEY_LEFT) {
  1263. input_move_to(in, in->cursor > 0 ? in->cursor - 1 : 0);
  1264. } else if (k == NGUI_KEY_RIGHT) {
  1265. input_move_to(in, in->cursor < in->len
  1266. ? in->cursor + 1 : in->len);
  1267. } else if (k == NGUI_KEY_CTRL_LEFT) {
  1268. input_move_to(in, input_word_prev(in, in->cursor));
  1269. } else if (k == NGUI_KEY_CTRL_RIGHT) {
  1270. input_move_to(in, input_word_next(in, in->cursor));
  1271. } else if (k == NGUI_KEY_HOME) {
  1272. input_move_to(in, 0);
  1273. } else if (k == NGUI_KEY_END) {
  1274. input_move_to(in, in->len);
  1275. } else if (k == 0x01) { /* Ctrl+A: select all */
  1276. in->anchor = 0;
  1277. in->cursor = in->len;
  1278. } else if (k == 0x15) { /* Ctrl+U */
  1279. in->len = 0;
  1280. in->cursor = 0;
  1281. in->anchor = 0;
  1282. in->text[0] = '\0';
  1283. changed = 1;
  1284. } else if (k >= 0x20 && k <= 0x7E) {
  1285. input_insert(in, (char)k);
  1286. changed = 1;
  1287. }
  1288. }
  1289. }
  1290. ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
  1291. ngui_frame(ui, r, focused ? ui->theme.accent : ui->theme.border);
  1292. if (in->scroll > in->len)
  1293. in->scroll = in->len;
  1294. if (in->cursor < in->scroll)
  1295. in->scroll = in->cursor;
  1296. if (cols > 0 && in->cursor >= in->scroll + (unsigned int)cols)
  1297. in->scroll = in->cursor - (unsigned int)cols + 1;
  1298. if (in->scroll > in->len)
  1299. in->scroll = in->len;
  1300. ngui_push_clip(ui, inner);
  1301. if (in->len == 0 && in->placeholder) {
  1302. ngui_bitmap_text(ui, inner.x, inner.y, in->placeholder,
  1303. ui->theme.text_dim);
  1304. } else {
  1305. if (focused)
  1306. input_draw_selection(ui, in, inner, cols);
  1307. ngui_bitmap_text(ui, inner.x, inner.y, in->text + in->scroll,
  1308. ui->theme.text);
  1309. }
  1310. if (focused && ui->caret_on) {
  1311. int col = (int)in->cursor - (int)in->scroll;
  1312. if (col >= 0 && col <= cols)
  1313. ngui_fill_rect(ui, rect_make(inner.x + col * 8, inner.y,
  1314. 1, 16), ui->theme.accent);
  1315. }
  1316. ngui_pop_clip(ui);
  1317. if (changed)
  1318. result |= NGUI_INPUT_CHANGED;
  1319. return result;
  1320. }
  1321. static int scrollable(const struct ngui_scroll *s)
  1322. {
  1323. return s->content_h > s->view_h && s->view_h > 0;
  1324. }
  1325. static int scroll_thumb_h(const struct ngui_rect *r,
  1326. const struct ngui_scroll *s)
  1327. {
  1328. int h = 0;
  1329. if (s->content_h > 0)
  1330. h = r->h * s->view_h / s->content_h;
  1331. if (h < 12)
  1332. h = 12;
  1333. if (h > r->h)
  1334. h = r->h;
  1335. return h;
  1336. }
  1337. static int scroll_thumb_y(const struct ngui_rect *r,
  1338. const struct ngui_scroll *s)
  1339. {
  1340. int th = scroll_thumb_h(r, s);
  1341. int avail = r->h - th;
  1342. int max = s->content_h - s->view_h;
  1343. if (avail <= 0 || max <= 0)
  1344. return r->y;
  1345. return r->y + s->offset * avail / max;
  1346. }
  1347. static void scroll_clamp(struct ngui_scroll *s)
  1348. {
  1349. int max = s->content_h - s->view_h;
  1350. if (max < 0)
  1351. max = 0;
  1352. if (s->offset > max)
  1353. s->offset = max;
  1354. if (s->offset < 0)
  1355. s->offset = 0;
  1356. }
  1357. void ngui_scroll_to(struct ngui_scroll *s, int offset)
  1358. {
  1359. s->offset = offset;
  1360. scroll_clamp(s);
  1361. }
  1362. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
  1363. {
  1364. if (y < s->offset)
  1365. s->offset = y;
  1366. else if (y + h > s->offset + s->view_h)
  1367. s->offset = y + h - s->view_h;
  1368. scroll_clamp(s);
  1369. }
  1370. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  1371. struct ngui_scroll *s)
  1372. {
  1373. struct ngui_rect bar;
  1374. int scrolls = scrollable(s);
  1375. s->view_h = r.h;
  1376. scroll_clamp(s);
  1377. if (!ui->buttons)
  1378. s->dragging = 0;
  1379. if (scrolls) {
  1380. int th = scroll_thumb_h(&r, s);
  1381. int ty = scroll_thumb_y(&r, s);
  1382. bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1383. if (ui->pressed &&
  1384. ngui_rect_contains(&bar, ui->press_x, ui->press_y)) {
  1385. if (ui->press_y >= ty && ui->press_y < ty + th) {
  1386. s->dragging = 1;
  1387. s->drag_dy = ui->press_y - ty;
  1388. } else {
  1389. int rel = ui->press_y - r.y - th / 2;
  1390. int avail = r.h - th;
  1391. int max = s->content_h - s->view_h;
  1392. if (rel < 0)
  1393. rel = 0;
  1394. if (rel > avail)
  1395. rel = avail;
  1396. if (avail > 0)
  1397. s->offset = rel * max / avail;
  1398. s->dragging = 1;
  1399. s->drag_dy = th / 2;
  1400. scroll_clamp(s);
  1401. }
  1402. }
  1403. /* Sampled buttons are authoritative while held: some event paths
  1404. * deliver the press late or coalesce it with the release. Start
  1405. * the drag from the current pointer position as well. */
  1406. if (!s->dragging && (ui->buttons & 1) &&
  1407. ngui_rect_contains(&bar, ui->mouse_x, ui->mouse_y)) {
  1408. if (ui->mouse_y >= ty && ui->mouse_y < ty + th) {
  1409. s->dragging = 1;
  1410. s->drag_dy = ui->mouse_y - ty;
  1411. } else {
  1412. int rel = ui->mouse_y - r.y - th / 2;
  1413. int avail = r.h - th;
  1414. int max = s->content_h - s->view_h;
  1415. if (rel < 0)
  1416. rel = 0;
  1417. if (rel > avail)
  1418. rel = avail;
  1419. if (avail > 0)
  1420. s->offset = rel * max / avail;
  1421. s->dragging = 1;
  1422. s->drag_dy = th / 2;
  1423. scroll_clamp(s);
  1424. }
  1425. }
  1426. if (s->dragging && (ui->buttons & 1)) {
  1427. int rel = ui->mouse_y - r.y - s->drag_dy;
  1428. int avail = r.h - th;
  1429. int max = s->content_h - s->view_h;
  1430. if (rel < 0)
  1431. rel = 0;
  1432. if (rel > avail)
  1433. rel = avail;
  1434. if (avail > 0)
  1435. s->offset = rel * max / avail;
  1436. scroll_clamp(s);
  1437. }
  1438. if (ui->released)
  1439. s->dragging = 0;
  1440. }
  1441. ngui_push_clip(ui, r);
  1442. }
  1443. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  1444. struct ngui_scroll *s)
  1445. {
  1446. ngui_pop_clip(ui);
  1447. if (!scrollable(s))
  1448. return;
  1449. {
  1450. struct ngui_rect bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1451. int th = scroll_thumb_h(&r, s);
  1452. int ty = scroll_thumb_y(&r, s);
  1453. struct ngui_rect thumb = rect_make(bar.x, ty, 14, th);
  1454. ngui_fill_rect(ui, bar, ui->theme.panel_alt);
  1455. ngui_fill_rect(ui, thumb, s->dragging ?
  1456. ui->theme.accent : ui->theme.text_dim);
  1457. }
  1458. }
  1459. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s)
  1460. {
  1461. int k = ngui_key_peek(ui);
  1462. int page;
  1463. if (k != NGUI_KEY_PAGE_UP && k != NGUI_KEY_PAGE_DOWN)
  1464. return 0;
  1465. page = s->view_h - 22;
  1466. if (page < 22)
  1467. page = 22;
  1468. ngui_key(ui);
  1469. if (k == NGUI_KEY_PAGE_UP)
  1470. s->offset -= page;
  1471. else
  1472. s->offset += page;
  1473. scroll_clamp(s);
  1474. return 1;
  1475. }