ngui.c 47 KB

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