ngui.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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. /* Brighten (step > 0) or darken (step < 0) an RGB565 color for the
  438. * one-pixel bevel. Green has six bits, red and blue five, so the green
  439. * component moves twice as far to keep the shade visually even. */
  440. static unsigned short shade565(unsigned short c, int step)
  441. {
  442. int r = (int)((c >> 11) & 0x1F) + step;
  443. int g = (int)((c >> 5) & 0x3F) + step * 2;
  444. int b = (int)(c & 0x1F) + step;
  445. if (r < 0)
  446. r = 0;
  447. if (r > 0x1F)
  448. r = 0x1F;
  449. if (g < 0)
  450. g = 0;
  451. if (g > 0x3F)
  452. g = 0x3F;
  453. if (b < 0)
  454. b = 0;
  455. if (b > 0x1F)
  456. b = 0x1F;
  457. return (unsigned short)((r << 11) | (g << 5) | b);
  458. }
  459. /* A one-pixel two-tone edge: `tl` along the top and left, `br` along the
  460. * bottom and right. A raised widget lights its top/left and shades its
  461. * bottom/right; an inset one (a text field) swaps the tones. */
  462. static void bevel_rect(struct ngui *ui, struct ngui_rect r,
  463. unsigned short tl, unsigned short br)
  464. {
  465. if (r.w <= 0 || r.h <= 0)
  466. return;
  467. ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), tl);
  468. ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), br);
  469. if (r.h > 2) {
  470. ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), tl);
  471. ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
  472. r.h - 2), br);
  473. }
  474. }
  475. /* Raised button edge: lit on the top/left, shaded on the bottom/right. A
  476. * pressed button inverts the bevel, so it reads as pushed in. The hovered
  477. * button keeps its accent on the lit edge. */
  478. static void button_bevel(struct ngui *ui, struct ngui_rect r,
  479. unsigned short bg, int hot, int down)
  480. {
  481. if (down)
  482. bevel_rect(ui, r, shade565(bg, -4), shade565(bg, 4));
  483. else if (hot)
  484. bevel_rect(ui, r, ui->theme.accent, shade565(bg, -4));
  485. else
  486. bevel_rect(ui, r, shade565(bg, 4), shade565(bg, -4));
  487. }
  488. static int ngui_isqrt(int v)
  489. {
  490. int r = 0, bit = 1 << 30;
  491. if (v <= 0)
  492. return 0;
  493. /* Classic shift-and-subtract sqrt: bit must be the highest power
  494. * of four that fits in v, and each step consumes two bits. */
  495. while (bit > v)
  496. bit >>= 2;
  497. while (bit) {
  498. if (v >= r + bit) {
  499. v -= r + bit;
  500. r = (r >> 1) + bit;
  501. } else {
  502. r >>= 1;
  503. }
  504. bit >>= 2;
  505. }
  506. return r;
  507. }
  508. /* Horizontal inset of one scanline of a corner with the given radius. */
  509. static int round_inset(int y, int h, int r)
  510. {
  511. int dy, d2;
  512. if (r <= 0)
  513. return 0;
  514. if (y < r)
  515. dy = r - y - 1;
  516. else if (y >= h - r)
  517. dy = y - (h - r);
  518. else
  519. return 0;
  520. d2 = r * r - dy * dy;
  521. if (d2 < 0)
  522. d2 = 0;
  523. return r - ngui_isqrt(d2);
  524. }
  525. void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
  526. int tl, int tr, int br, int bl,
  527. unsigned short color)
  528. {
  529. int y, half;
  530. if (rect_empty(&r))
  531. return;
  532. half = r.w < r.h ? r.w / 2 : r.h / 2;
  533. if (tl > half) tl = half;
  534. if (tr > half) tr = half;
  535. if (br > half) br = half;
  536. if (bl > half) bl = half;
  537. if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
  538. ngui_fill_rect(ui, r, color);
  539. return;
  540. }
  541. for (y = 0; y < r.h; y++) {
  542. int li = 0, ri = 0;
  543. if (y < tl)
  544. li = round_inset(y, r.h, tl);
  545. if (y < tr)
  546. ri = round_inset(y, r.h, tr);
  547. if (y >= r.h - bl) {
  548. int i = round_inset(y, r.h, bl);
  549. if (i > li)
  550. li = i;
  551. }
  552. if (y >= r.h - br) {
  553. int i = round_inset(y, r.h, br);
  554. if (i > ri)
  555. ri = i;
  556. }
  557. if (r.w - li - ri > 0)
  558. ngui_fill_rect(ui, rect_make(r.x + li, r.y + y,
  559. r.w - li - ri, 1), color);
  560. }
  561. }
  562. void ngui_frame_round(struct ngui *ui, struct ngui_rect r,
  563. int tl, int tr, int br, int bl, int thickness,
  564. unsigned short color)
  565. {
  566. int y, half;
  567. if (rect_empty(&r))
  568. return;
  569. if (thickness < 1)
  570. thickness = 1;
  571. half = r.w < r.h ? r.w / 2 : r.h / 2;
  572. if (tl > half) tl = half;
  573. if (tr > half) tr = half;
  574. if (br > half) br = half;
  575. if (bl > half) bl = half;
  576. if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
  577. ngui_frame(ui, r, color);
  578. return;
  579. }
  580. for (y = 0; y < r.h; y++) {
  581. int li = 0, ri = 0;
  582. int x0, x1;
  583. if (y < tl)
  584. li = round_inset(y, r.h, tl);
  585. if (y < tr)
  586. ri = round_inset(y, r.h, tr);
  587. if (y >= r.h - bl) {
  588. int i = round_inset(y, r.h, bl);
  589. if (i > li)
  590. li = i;
  591. }
  592. if (y >= r.h - br) {
  593. int i = round_inset(y, r.h, br);
  594. if (i > ri)
  595. ri = i;
  596. }
  597. x0 = r.x + li;
  598. x1 = r.x + r.w - 1 - ri;
  599. if (x0 > x1)
  600. continue;
  601. if (y < thickness || y >= r.h - thickness) {
  602. ngui_fill_rect(ui, rect_make(x0, r.y + y,
  603. x1 - x0 + 1, 1), color);
  604. } else {
  605. ngui_fill_rect(ui, rect_make(x0, r.y + y,
  606. thickness, 1), color);
  607. ngui_fill_rect(ui, rect_make(x1 - thickness + 1,
  608. r.y + y, thickness, 1), color);
  609. }
  610. }
  611. }
  612. static void ngui_pixel(struct ngui *ui, int x, int y, unsigned short color)
  613. {
  614. if (pixel_visible(ui, x, y))
  615. ui->px[y * ui->w + x] = color;
  616. }
  617. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  618. unsigned short color)
  619. {
  620. int dx = x1 - x0;
  621. int dy = y1 - y0;
  622. int sx = dx < 0 ? -1 : 1;
  623. int sy = dy < 0 ? -1 : 1;
  624. int err, e2;
  625. if (dx < 0)
  626. dx = -dx;
  627. if (dy < 0)
  628. dy = -dy;
  629. dy = -dy;
  630. err = dx + dy;
  631. for (;;) {
  632. ngui_pixel(ui, x0, y0, color);
  633. if (x0 == x1 && y0 == y1)
  634. break;
  635. e2 = err * 2;
  636. if (e2 >= dy) {
  637. err += dy;
  638. x0 += sx;
  639. }
  640. if (e2 <= dx) {
  641. err += dx;
  642. y0 += sy;
  643. }
  644. }
  645. }
  646. static void ngui_bitmap_glyph(struct ngui *ui, int x, int y, unsigned char c,
  647. unsigned short color, int scale, int bold)
  648. {
  649. const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  650. int row, col, sx, sy;
  651. if (scale < 1)
  652. scale = 1;
  653. for (row = 0; row < FONT_8X16_ROWS; row++) {
  654. unsigned char bits = g[row];
  655. if (!bits)
  656. continue;
  657. for (col = 0; col < 8; col++) {
  658. if (!(bits & (0x80u >> col)))
  659. continue;
  660. for (sy = 0; sy < scale; sy++) {
  661. for (sx = 0; sx < scale; sx++) {
  662. ngui_pixel(ui,
  663. x + col * scale + sx +
  664. (bold && scale == 1 ? 1 : 0),
  665. y + row * scale + sy,
  666. color);
  667. }
  668. }
  669. if (bold && scale > 1)
  670. ngui_pixel(ui, x + col * scale + scale,
  671. y + row * scale, color);
  672. }
  673. }
  674. }
  675. static struct ufont_face *ngui_face(struct ngui *ui, int bold, int italic,
  676. int mono)
  677. {
  678. struct ufont_face *f;
  679. if (!ui)
  680. return 0;
  681. if (mono && ui->font_mono)
  682. f = ui->font_mono;
  683. else if (bold && italic)
  684. f = ui->font_bold_italic;
  685. else if (bold)
  686. f = ui->font_bold;
  687. else if (italic)
  688. f = ui->font_italic;
  689. else
  690. f = ui->font;
  691. if (!f)
  692. f = ui->font;
  693. return f;
  694. }
  695. static void ngui_clip_bounds(struct ngui *ui, int *cx, int *cy,
  696. int *cw, int *ch)
  697. {
  698. struct ngui_rect r = { 0, 0, ui->w, ui->h };
  699. int i;
  700. for (i = 0; i < ui->nclip; i++) {
  701. struct ngui_rect out;
  702. if (!rect_intersect(&r, &ui->clip[i], &out)) {
  703. r.w = r.h = 0;
  704. break;
  705. }
  706. r = out;
  707. }
  708. *cx = r.x;
  709. *cy = r.y;
  710. *cw = r.w;
  711. *ch = r.h;
  712. }
  713. int ngui_text_ascent_face(struct ngui *ui, struct ufont_face *face, int px,
  714. int mono)
  715. {
  716. struct ufont_face *f = face ? face : ngui_face(ui, 0, 0, mono);
  717. struct ufont_metrics m;
  718. if (f) {
  719. ufont_metrics(f, px, &m);
  720. return m.ascent;
  721. }
  722. return 16 * (px >= 24 ? 2 : 1);
  723. }
  724. int ngui_text_ascent(struct ngui *ui, int px, int mono)
  725. {
  726. return ngui_text_ascent_face(ui, 0, px, mono);
  727. }
  728. int ngui_text_line_height_face(struct ngui *ui, struct ufont_face *face,
  729. int px, int mono)
  730. {
  731. struct ufont_face *f = face ? face : ngui_face(ui, 0, 0, mono);
  732. struct ufont_metrics m;
  733. if (f) {
  734. ufont_metrics(f, px, &m);
  735. return m.line_height;
  736. }
  737. return 16 * (px >= 24 ? 2 : 1);
  738. }
  739. int ngui_text_line_height(struct ngui *ui, int px, int mono)
  740. {
  741. return ngui_text_line_height_face(ui, 0, px, mono);
  742. }
  743. unsigned int ngui_text_width_face(struct ngui *ui, struct ufont_face *face,
  744. const char *s, unsigned int n, int px,
  745. int bold, int italic, int mono)
  746. {
  747. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  748. int scale;
  749. if (f)
  750. return (unsigned int)ufont_text_width(f, px, s, (int)n, 0);
  751. scale = px >= 24 ? 2 : 1;
  752. return n * 8u * (unsigned int)scale;
  753. }
  754. unsigned int ngui_text_width_px(struct ngui *ui, const char *s, unsigned int n,
  755. int px, int bold, int italic, int mono)
  756. {
  757. return ngui_text_width_face(ui, 0, s, n, px, bold, italic, mono);
  758. }
  759. unsigned int ngui_text_width_ui(struct ngui *ui, const char *s)
  760. {
  761. return ngui_text_width_px(ui, s, slen(s),
  762. ui ? ui->font_px : 14, 0, 0, 0);
  763. }
  764. unsigned int ngui_text_width(const char *s)
  765. {
  766. return slen(s) * 8;
  767. }
  768. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale)
  769. {
  770. (void)s;
  771. if (scale < 1)
  772. scale = 1;
  773. return n * 8u * (unsigned int)scale;
  774. }
  775. static void ngui_text_px_common(struct ngui *ui, struct ufont_face *face,
  776. int x, int y, const char *s, unsigned int n,
  777. unsigned short color, int px, int bold,
  778. int italic, int mono)
  779. {
  780. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  781. int scale, i, pen = 0, ascent;
  782. int cx, cy, cw, ch;
  783. if (!ui || !ui->px || !s)
  784. return;
  785. if (!f) {
  786. scale = px >= 24 ? 2 : 1;
  787. for (i = 0; i < (int)n && s[i]; i++)
  788. ngui_bitmap_glyph(ui, x + i * 8 * scale, y,
  789. (unsigned char)s[i], color,
  790. scale, bold);
  791. return;
  792. }
  793. ascent = ngui_text_ascent_face(ui, face, px, mono);
  794. ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
  795. for (i = 0; i < (int)n && s[i]; i++) {
  796. const struct ufont_glyph *g =
  797. ufont_glyph(f, px, (unsigned char)s[i]);
  798. if (!g) {
  799. pen += 8 << 6;
  800. continue;
  801. }
  802. ufont_blit_rgb565(ui->px, ui->w, ui->h,
  803. x + (pen >> 6), y + ascent, g, color,
  804. cx, cy, cw, ch);
  805. pen += g->advance;
  806. }
  807. }
  808. void ngui_text_px(struct ngui *ui, int x, int y, const char *s,
  809. unsigned int n, unsigned short color, int px, int bold,
  810. int italic, int mono)
  811. {
  812. ngui_text_px_common(ui, 0, x, y, s, n, color, px, bold, italic, mono);
  813. }
  814. /* ---- UTF-8 text with an emoji fallback face --------------------------- */
  815. static int utf8_skip_cp(unsigned int cp)
  816. {
  817. return cp == 0x200B || cp == 0x200C || cp == 0x200D ||
  818. cp == 0xFE0E || cp == 0xFE0F;
  819. }
  820. /* Advance of one codepoint in 26.6 pixels, choosing the primary face, then
  821. * the lazily loaded emoji face, then '?'. */
  822. static int ngui_utf8_advance(struct ngui *ui, struct ufont_face *f,
  823. int px, unsigned int cp)
  824. {
  825. struct ufont_face *emoji;
  826. if (utf8_skip_cp(cp) || !f)
  827. return 0;
  828. if (ufont_has_glyph(f, cp))
  829. return ufont_advance_cp(f, px, cp);
  830. emoji = ngui_emoji_face(ui);
  831. if (emoji && ufont_has_glyph(emoji, cp))
  832. return ufont_advance_cp(emoji, px, cp);
  833. return ufont_advance_cp(f, px, '?');
  834. }
  835. static unsigned int ngui_text_width_face_utf8(struct ngui *ui,
  836. struct ufont_face *face,
  837. const char *s, unsigned int n,
  838. int px, int bold, int italic,
  839. int mono)
  840. {
  841. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  842. int i = 0, total = 0;
  843. if (!f)
  844. return n * 8u * (px >= 24 ? 2u : 1u);
  845. while (i < (int)n) {
  846. unsigned int cp = ufont_utf8_next(s, &i, (int)n);
  847. total += ngui_utf8_advance(ui, f, px, cp);
  848. }
  849. return (unsigned int)((total + 32) >> 6);
  850. }
  851. static const struct ufont_glyph *ngui_utf8_glyph(struct ngui *ui,
  852. struct ufont_face *f,
  853. int px, unsigned int cp,
  854. struct ufont_face **used)
  855. {
  856. struct ufont_face *emoji;
  857. const struct ufont_glyph *g;
  858. *used = f;
  859. if (utf8_skip_cp(cp) || !f)
  860. return 0;
  861. if (ufont_has_glyph(f, cp)) {
  862. g = ufont_glyph(f, px, cp);
  863. if (g)
  864. return g;
  865. }
  866. emoji = ngui_emoji_face(ui);
  867. if (emoji && ufont_has_glyph(emoji, cp)) {
  868. g = ufont_glyph(emoji, px, cp);
  869. if (g) {
  870. *used = emoji;
  871. return g;
  872. }
  873. }
  874. *used = f;
  875. return ufont_glyph(f, px, '?');
  876. }
  877. static void ngui_text_px_utf8_common(struct ngui *ui, struct ufont_face *face,
  878. int x, int y, const char *s,
  879. unsigned int n, unsigned short color,
  880. int px, int bold, int italic, int mono)
  881. {
  882. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  883. int i = 0, pen = 0, ascent;
  884. int cx, cy, cw, ch;
  885. if (!ui || !ui->px || !s)
  886. return;
  887. if (!f) {
  888. ngui_text_px_common(ui, face, x, y, s, n, color, px, bold,
  889. italic, mono);
  890. return;
  891. }
  892. ascent = ngui_text_ascent_face(ui, face, px, mono);
  893. ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
  894. while (i < (int)n) {
  895. unsigned int cp = ufont_utf8_next(s, &i, (int)n);
  896. struct ufont_face *used;
  897. const struct ufont_glyph *g;
  898. if (utf8_skip_cp(cp))
  899. continue;
  900. g = ngui_utf8_glyph(ui, f, px, cp, &used);
  901. if (!g) {
  902. pen += 8 << 6;
  903. continue;
  904. }
  905. ufont_blit_rgb565(ui->px, ui->w, ui->h,
  906. x + (pen >> 6), y + ascent, g, color,
  907. cx, cy, cw, ch);
  908. pen += g->advance;
  909. }
  910. }
  911. void ngui_text_n_utf8(struct ngui *ui, int x, int y, const char *s,
  912. unsigned int n, unsigned short color)
  913. {
  914. ngui_text_px_utf8_common(ui, 0, x, y, s, n, color,
  915. ui ? ui->font_px : 14, 0, 0, 0);
  916. }
  917. void ngui_text_utf8(struct ngui *ui, int x, int y, const char *s,
  918. unsigned short color)
  919. {
  920. ngui_text_n_utf8(ui, x, y, s, s ? slen(s) : 0, color);
  921. }
  922. unsigned int ngui_text_width_utf8(struct ngui *ui, const char *s)
  923. {
  924. return ngui_text_width_face_utf8(ui, 0, s, s ? slen(s) : 0,
  925. ui ? ui->font_px : 14, 0, 0, 0);
  926. }
  927. void ngui_text_ellipsis_utf8(struct ngui *ui, struct ngui_rect r,
  928. int x, int y, const char *s,
  929. unsigned short color)
  930. {
  931. unsigned int n = s ? slen(s) : 0;
  932. unsigned int end = 0, i = 0;
  933. int avail = r.x + r.w - x;
  934. int dots, width;
  935. if (!ui || !s)
  936. return;
  937. if (avail <= 0)
  938. return;
  939. width = (int)ngui_text_width_utf8(ui, s);
  940. if (width <= avail) {
  941. ngui_text_utf8(ui, x, y, s, color);
  942. return;
  943. }
  944. dots = (int)ngui_text_width_utf8(ui, "...");
  945. if (dots >= avail)
  946. return;
  947. while (i < n) {
  948. int next = i;
  949. (void)ufont_utf8_next(s, &next, (int)n);
  950. if ((int)ngui_text_width_face_utf8(ui, 0, s, (unsigned int)next,
  951. ui->font_px, 0, 0, 0) +
  952. dots > avail)
  953. break;
  954. i = (unsigned int)next;
  955. end = i;
  956. }
  957. ngui_text_n_utf8(ui, x, y, s, end, color);
  958. ngui_text_utf8(ui, x + (int)ngui_text_width_face_utf8(
  959. ui, 0, s, end, ui->font_px, 0, 0, 0),
  960. y, "...", color);
  961. }
  962. void ngui_text_px_face(struct ngui *ui, struct ufont_face *face, int x,
  963. int y, const char *s, unsigned int n,
  964. unsigned short color, int px, int bold, int italic,
  965. int mono)
  966. {
  967. ngui_text_px_common(ui, face, x, y, s, n, color, px, bold, italic,
  968. mono);
  969. }
  970. void ngui_text_n(struct ngui *ui, int x, int y, const char *s, unsigned int n,
  971. unsigned short color)
  972. {
  973. ngui_text_px(ui, x, y, s, n, color,
  974. ui ? ui->font_px : 14, 0, 0, 0);
  975. }
  976. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  977. unsigned short color)
  978. {
  979. ngui_text_n(ui, x, y, s, slen(s), color);
  980. }
  981. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  982. unsigned int n, unsigned short color, int scale, int bold)
  983. {
  984. if (scale < 1)
  985. scale = 1;
  986. if (ui && ui->font) {
  987. ngui_text_px(ui, x, y, s, n, color,
  988. ui->font_px * scale, bold, 0, 0);
  989. return;
  990. }
  991. {
  992. unsigned int i;
  993. for (i = 0; i < n && s && s[i]; i++)
  994. ngui_bitmap_glyph(ui, x + (int)i * 8 * scale, y,
  995. (unsigned char)s[i], color,
  996. scale, bold);
  997. }
  998. }
  999. /* Draw a string upside down. The whole n-character block is rotated, so the
  1000. * first character ends up on the right; used for the mirrored corner index on
  1001. * a playing card. */
  1002. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  1003. unsigned int n, unsigned short color)
  1004. {
  1005. unsigned int i;
  1006. int row, col;
  1007. for (i = 0; i < n && s && s[i]; i++) {
  1008. const unsigned char *g =
  1009. &font_8x16[(unsigned int)(unsigned char)s[i] *
  1010. FONT_8X16_ROWS];
  1011. for (row = 0; row < FONT_8X16_ROWS; row++) {
  1012. unsigned char bits = g[row];
  1013. if (!bits)
  1014. continue;
  1015. for (col = 0; col < 8; col++) {
  1016. if (!(bits & (0x80u >> col)))
  1017. continue;
  1018. ngui_pixel(ui,
  1019. x + (int)(n - 1 - i) * 8 + 7 - col,
  1020. y + FONT_8X16_ROWS - 1 - row,
  1021. color);
  1022. }
  1023. }
  1024. }
  1025. }
  1026. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  1027. const char *s, unsigned short color)
  1028. {
  1029. unsigned int n = slen(s);
  1030. int avail = r.x + r.w - x;
  1031. unsigned int i, best = 0;
  1032. int dots;
  1033. if (avail <= 0)
  1034. return;
  1035. if (ui && ui->font) {
  1036. dots = (int)ngui_text_width_px(ui, "...", 3, ui->font_px,
  1037. 0, 0, 0);
  1038. for (i = 0; i <= n; i++) {
  1039. int w = (int)ngui_text_width_px(ui, s, i, ui->font_px,
  1040. 0, 0, 0);
  1041. if (w > avail)
  1042. break;
  1043. best = i;
  1044. }
  1045. if (best == n) {
  1046. ngui_text_n(ui, x, y, s, n, color);
  1047. return;
  1048. }
  1049. while (best > 0 &&
  1050. (int)ngui_text_width_px(ui, s, best, ui->font_px,
  1051. 0, 0, 0) + dots > avail)
  1052. best--;
  1053. ngui_text_n(ui, x, y, s, best, color);
  1054. ngui_text_n(ui, x + (int)ngui_text_width_px(ui, s, best,
  1055. ui->font_px, 0, 0, 0),
  1056. y, "...", 3, color);
  1057. return;
  1058. }
  1059. {
  1060. int max = avail / 8;
  1061. if (max <= 0)
  1062. return;
  1063. if (n <= (unsigned int)max || max < 4) {
  1064. for (i = 0; i < n && (int)i < max; i++)
  1065. ngui_bitmap_glyph(ui, x + (int)i * 8, y,
  1066. (unsigned char)s[i], color, 1, 0);
  1067. return;
  1068. }
  1069. for (i = 0; i < (unsigned int)(max - 3); i++)
  1070. ngui_bitmap_glyph(ui, x + (int)i * 8, y,
  1071. (unsigned char)s[i], color, 1, 0);
  1072. ngui_bitmap_glyph(ui, x + (max - 3) * 8, y, '.', color, 1, 0);
  1073. ngui_bitmap_glyph(ui, x + (max - 2) * 8, y, '.', color, 1, 0);
  1074. ngui_bitmap_glyph(ui, x + (max - 1) * 8, y, '.', color, 1, 0);
  1075. }
  1076. }
  1077. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  1078. unsigned short color)
  1079. {
  1080. int x = r.x + r.w / 2 - 8;
  1081. int y = r.y + r.h / 2 - 8;
  1082. switch (icon) {
  1083. case NGUI_ICON_FOLDER:
  1084. ngui_fill_rect(ui, rect_make(x + 1, y + 3, 7, 4), color);
  1085. ngui_fill_rect(ui, rect_make(x, y + 6, 16, 9), color);
  1086. break;
  1087. case NGUI_ICON_FILE:
  1088. ngui_frame(ui, rect_make(x + 2, y, 12, 16), color);
  1089. ngui_line(ui, x + 10, y, x + 13, y + 3, color);
  1090. ngui_fill_rect(ui, rect_make(x + 4, y + 7, 8, 1), color);
  1091. ngui_fill_rect(ui, rect_make(x + 4, y + 10, 8, 1), color);
  1092. break;
  1093. case NGUI_ICON_DISK:
  1094. ngui_frame(ui, rect_make(x, y + 2, 16, 12), color);
  1095. ngui_fill_rect(ui, rect_make(x + 2, y + 5, 12, 2), color);
  1096. ngui_fill_rect(ui, rect_make(x + 11, y + 9, 3, 3), color);
  1097. break;
  1098. case NGUI_ICON_UP:
  1099. ngui_line(ui, x + 8, y + 2, x + 3, y + 8, color);
  1100. ngui_line(ui, x + 8, y + 2, x + 13, y + 8, color);
  1101. ngui_fill_rect(ui, rect_make(x + 6, y + 7, 5, 7), color);
  1102. break;
  1103. case NGUI_ICON_HOME:
  1104. ngui_line(ui, x + 2, y + 8, x + 8, y + 2, color);
  1105. ngui_line(ui, x + 8, y + 2, x + 14, y + 8, color);
  1106. ngui_frame(ui, rect_make(x + 3, y + 8, 10, 7), color);
  1107. ngui_fill_rect(ui, rect_make(x + 7, y + 11, 3, 4), color);
  1108. break;
  1109. case NGUI_ICON_REFRESH:
  1110. ngui_line(ui, x + 3, y + 8, x + 6, y + 4, color);
  1111. ngui_line(ui, x + 6, y + 4, x + 10, y + 3, color);
  1112. ngui_line(ui, x + 10, y + 3, x + 13, y + 6, color);
  1113. ngui_line(ui, x + 13, y + 6, x + 13, y + 9, color);
  1114. ngui_fill_rect(ui, rect_make(x + 11, y + 1, 4, 4), color);
  1115. break;
  1116. case NGUI_ICON_PLUS:
  1117. ngui_fill_rect(ui, rect_make(x + 7, y + 3, 2, 10), color);
  1118. ngui_fill_rect(ui, rect_make(x + 3, y + 7, 10, 2), color);
  1119. break;
  1120. case NGUI_ICON_CHEVRON_LEFT:
  1121. ngui_line(ui, x + 10, y + 3, x + 6, y + 8, color);
  1122. ngui_line(ui, x + 6, y + 8, x + 10, y + 13, color);
  1123. break;
  1124. case NGUI_ICON_CHEVRON_RIGHT:
  1125. ngui_line(ui, x + 6, y + 3, x + 10, y + 8, color);
  1126. ngui_line(ui, x + 10, y + 8, x + 6, y + 13, color);
  1127. break;
  1128. default:
  1129. break;
  1130. }
  1131. }
  1132. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1133. const char *label)
  1134. {
  1135. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1136. int press_hot = ui->pressed &&
  1137. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1138. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1139. int fired = 0;
  1140. unsigned short bg;
  1141. int tw, tx, ty;
  1142. if (press_hot)
  1143. ui->active = id;
  1144. if (ui->pressed && ui->released && press_hot && release_hot)
  1145. fired = 1;
  1146. if (ui->released && ui->active == id) {
  1147. if (release_hot)
  1148. fired = 1;
  1149. ui->active = 0;
  1150. }
  1151. if (ui->active == id)
  1152. bg = ui->theme.button_down;
  1153. else if (hot)
  1154. bg = ui->theme.button_hot;
  1155. else
  1156. bg = ui->theme.button;
  1157. ngui_fill_rect(ui, r, bg);
  1158. button_bevel(ui, r, bg, hot, ui->active == id);
  1159. if (label && label[0]) {
  1160. tw = (int)ngui_text_width_ui(ui, label);
  1161. tx = r.x + (r.w - tw) / 2;
  1162. ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
  1163. if (ty < r.y)
  1164. ty = r.y;
  1165. ngui_push_clip(ui, r);
  1166. ngui_text(ui, tx, ty, label, ui->theme.text);
  1167. ngui_pop_clip(ui);
  1168. }
  1169. if (hot)
  1170. ui->hot = id;
  1171. return fired;
  1172. }
  1173. /* One row of a drop-down menu: left-aligned label, optional check mark and
  1174. * right-aligned shortcut. Uses the same press/release/active semantics as
  1175. * ngui_button so a menu and its caller never disagree about a click. */
  1176. static int ngui_menu_item_common(struct ngui *ui, unsigned int id,
  1177. struct ngui_rect r, const char *label,
  1178. const char *shortcut, int checked, int utf8)
  1179. {
  1180. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1181. int press_hot = ui->pressed &&
  1182. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1183. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1184. int fired = 0;
  1185. int selected;
  1186. unsigned short bg, fg;
  1187. int ty;
  1188. if (press_hot)
  1189. ui->active = id;
  1190. if (ui->pressed && ui->released && press_hot && release_hot)
  1191. fired = 1;
  1192. if (ui->released && ui->active == id) {
  1193. if (release_hot)
  1194. fired = 1;
  1195. ui->active = 0;
  1196. }
  1197. selected = (ui->active == id) || hot;
  1198. bg = selected ? ui->theme.selection : ui->theme.panel;
  1199. fg = selected ? NGUI_RGB(255, 255, 255) : ui->theme.text;
  1200. ngui_fill_rect(ui, r, bg);
  1201. ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
  1202. if (ty < r.y)
  1203. ty = r.y;
  1204. ngui_push_clip(ui, r);
  1205. if (checked) {
  1206. ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
  1207. ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
  1208. }
  1209. if (label) {
  1210. if (utf8)
  1211. ngui_text_utf8(ui, r.x + 22, ty, label, fg);
  1212. else
  1213. ngui_text(ui, r.x + 22, ty, label, fg);
  1214. }
  1215. if (shortcut) {
  1216. int sw = (int)(utf8 ? ngui_text_width_utf8(ui, shortcut) :
  1217. ngui_text_width_ui(ui, shortcut));
  1218. if (utf8)
  1219. ngui_text_utf8(ui, r.x + r.w - 8 - sw, ty, shortcut,
  1220. fg);
  1221. else
  1222. ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
  1223. }
  1224. ngui_pop_clip(ui);
  1225. if (hot)
  1226. ui->hot = id;
  1227. return fired;
  1228. }
  1229. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1230. const char *label, const char *shortcut, int checked)
  1231. {
  1232. return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 0);
  1233. }
  1234. int ngui_menu_item_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1235. const char *label, const char *shortcut, int checked)
  1236. {
  1237. return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 1);
  1238. }
  1239. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1240. enum ngui_icon icon){
  1241. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1242. int press_hot = ui->pressed &&
  1243. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1244. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1245. int fired = 0;
  1246. unsigned short bg;
  1247. if (press_hot)
  1248. ui->active = id;
  1249. if (ui->pressed && ui->released && press_hot && release_hot)
  1250. fired = 1;
  1251. if (ui->released && ui->active == id) {
  1252. if (release_hot)
  1253. fired = 1;
  1254. ui->active = 0;
  1255. }
  1256. if (ui->active == id)
  1257. bg = ui->theme.button_down;
  1258. else if (hot)
  1259. bg = ui->theme.button_hot;
  1260. else
  1261. bg = ui->theme.button;
  1262. ngui_fill_rect(ui, r, bg);
  1263. button_bevel(ui, r, bg, hot, ui->active == id);
  1264. ngui_icon(ui, r, icon, ui->theme.text);
  1265. if (hot)
  1266. ui->hot = id;
  1267. return fired;
  1268. }
  1269. void ngui_input_set(struct ngui_input *in, const char *s)
  1270. {
  1271. scopy(in->text, s, NGUI_INPUT_MAX);
  1272. in->len = slen(in->text);
  1273. in->cursor = in->len;
  1274. in->scroll = 0;
  1275. in->anchor = in->cursor;
  1276. in->dragging = 0;
  1277. in->last_click_ms = 0;
  1278. }
  1279. void ngui_input_value(const struct ngui_input *in, char *out,
  1280. unsigned int max)
  1281. {
  1282. unsigned int i;
  1283. if (!in || !out || !max)
  1284. return;
  1285. /* ngui_text_input() unmasks a secret field again after drawing, so
  1286. * in->text is the real value between calls. */
  1287. for (i = 0; in->text[i] && i + 1 < max; i++)
  1288. out[i] = in->text[i];
  1289. out[i] = '\0';
  1290. }
  1291. static int input_word_char(char c)
  1292. {
  1293. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  1294. (c >= '0' && c <= '9') || c == '_';
  1295. }
  1296. /* Next word start after `pos` (Windows edit-control semantics). */
  1297. static unsigned int input_word_next(const struct ngui_input *in,
  1298. unsigned int pos)
  1299. {
  1300. while (pos < in->len && input_word_char(in->text[pos]))
  1301. pos++;
  1302. while (pos < in->len && !input_word_char(in->text[pos]))
  1303. pos++;
  1304. return pos;
  1305. }
  1306. /* Start of the previous word before `pos`. */
  1307. static unsigned int input_word_prev(const struct ngui_input *in,
  1308. unsigned int pos)
  1309. {
  1310. while (pos > 0 && !input_word_char(in->text[pos - 1]))
  1311. pos--;
  1312. while (pos > 0 && input_word_char(in->text[pos - 1]))
  1313. pos--;
  1314. return pos;
  1315. }
  1316. /* End of the word to the right of `pos`. A caret inside a word takes the
  1317. * rest of that word and its trailing separators; a caret on separators takes
  1318. * them plus the next word. */
  1319. static unsigned int input_word_delete_end(const struct ngui_input *in,
  1320. unsigned int pos)
  1321. {
  1322. if (pos < in->len && input_word_char(in->text[pos])) {
  1323. while (pos < in->len && input_word_char(in->text[pos]))
  1324. pos++;
  1325. while (pos < in->len && !input_word_char(in->text[pos]))
  1326. pos++;
  1327. return pos;
  1328. }
  1329. while (pos < in->len && !input_word_char(in->text[pos]))
  1330. pos++;
  1331. while (pos < in->len && input_word_char(in->text[pos]))
  1332. pos++;
  1333. return pos;
  1334. }
  1335. static void input_sel_bounds(const struct ngui_input *in,
  1336. unsigned int *a, unsigned int *b)
  1337. {
  1338. if (in->anchor < in->cursor) {
  1339. *a = in->anchor;
  1340. *b = in->cursor;
  1341. } else {
  1342. *a = in->cursor;
  1343. *b = in->anchor;
  1344. }
  1345. }
  1346. /* Remove [a, b) and keep cursor/anchor consistent. Returns 1 when the text
  1347. * changed. */
  1348. static int input_delete_range(struct ngui_input *in, unsigned int a,
  1349. unsigned int b)
  1350. {
  1351. unsigned int i;
  1352. if (a >= b || b > in->len)
  1353. return 0;
  1354. for (i = b; i < in->len; i++)
  1355. in->text[i - (b - a)] = in->text[i];
  1356. in->len -= b - a;
  1357. in->text[in->len] = '\0';
  1358. if (in->cursor >= b)
  1359. in->cursor -= b - a;
  1360. else if (in->cursor > a)
  1361. in->cursor = a;
  1362. if (in->anchor >= b)
  1363. in->anchor -= b - a;
  1364. else if (in->anchor > a)
  1365. in->anchor = a;
  1366. return 1;
  1367. }
  1368. static int input_delete_selection(struct ngui_input *in)
  1369. {
  1370. unsigned int a, b;
  1371. input_sel_bounds(in, &a, &b);
  1372. return input_delete_range(in, a, b);
  1373. }
  1374. static void input_insert(struct ngui_input *in, char c)
  1375. {
  1376. unsigned int i;
  1377. input_delete_selection(in);
  1378. if (in->len + 1 >= NGUI_INPUT_MAX)
  1379. return;
  1380. i = in->len;
  1381. while (i > in->cursor) {
  1382. in->text[i] = in->text[i - 1];
  1383. i--;
  1384. }
  1385. in->text[in->cursor] = c;
  1386. in->cursor++;
  1387. in->len++;
  1388. in->text[in->len] = '\0';
  1389. in->anchor = in->cursor;
  1390. }
  1391. static void input_delete(struct ngui_input *in, int back)
  1392. {
  1393. unsigned int pos;
  1394. if (back) {
  1395. if (in->cursor == 0)
  1396. return;
  1397. pos = in->cursor - 1;
  1398. } else {
  1399. if (in->cursor >= in->len)
  1400. return;
  1401. pos = in->cursor;
  1402. }
  1403. input_delete_range(in, pos, pos + 1);
  1404. }
  1405. static void input_move_to(struct ngui_input *in, unsigned int pos)
  1406. {
  1407. if (pos > in->len)
  1408. pos = in->len;
  1409. in->cursor = pos;
  1410. in->anchor = pos;
  1411. }
  1412. /* Two presses in the same field within this window select all of its text. */
  1413. #define NGUI_DOUBLE_CLICK_MS 500
  1414. static int input_text_w(struct ngui *ui, const char *s, unsigned int n)
  1415. {
  1416. return (int)ngui_text_width_px(ui, s, n, ui->font_px, 0, 0, 0);
  1417. }
  1418. /* Character position under a content-relative x, clamped to the visible
  1419. * part of the field. Proportional text means the prefix must be measured. */
  1420. static unsigned int input_pos_at(struct ngui *ui, const struct ngui_input *in,
  1421. int inner_x, int x)
  1422. {
  1423. unsigned int i, n, best = 0;
  1424. int rel = x - inner_x;
  1425. if (rel <= 0 || in->scroll >= in->len)
  1426. return in->scroll;
  1427. n = in->len - in->scroll;
  1428. for (i = 1; i <= n; i++) {
  1429. if (input_text_w(ui, in->text + in->scroll, i) > rel)
  1430. break;
  1431. best = i;
  1432. }
  1433. return in->scroll + best;
  1434. }
  1435. static void input_draw_selection(struct ngui *ui, const struct ngui_input *in,
  1436. struct ngui_rect inner)
  1437. {
  1438. unsigned int a, b, va, vb;
  1439. int xa, xb;
  1440. input_sel_bounds(in, &a, &b);
  1441. if (a == b || in->scroll >= in->len)
  1442. return;
  1443. va = a < in->scroll ? in->scroll : a;
  1444. vb = b < in->len ? b : in->len;
  1445. if (va >= vb)
  1446. return;
  1447. xa = input_text_w(ui, in->text + in->scroll, va - in->scroll);
  1448. xb = input_text_w(ui, in->text + in->scroll, vb - in->scroll);
  1449. if (xb > xa)
  1450. ngui_fill_rect(ui, rect_make(inner.x + xa, inner.y,
  1451. xb - xa,
  1452. ngui_text_line_height(ui,
  1453. ui->font_px,
  1454. 0)),
  1455. ui->theme.selection);
  1456. }
  1457. /* Scroll the first-visible-character index until the caret is visible. */
  1458. static void input_scroll_to_cursor(struct ngui *ui, struct ngui_input *in,
  1459. int inner_w)
  1460. {
  1461. if (in->scroll > in->len)
  1462. in->scroll = in->len;
  1463. if (in->cursor < in->scroll)
  1464. in->scroll = in->cursor;
  1465. if (inner_w <= 0) {
  1466. in->scroll = in->len;
  1467. return;
  1468. }
  1469. while (in->scroll < in->cursor &&
  1470. input_text_w(ui, in->text + in->scroll,
  1471. in->cursor - in->scroll) > inner_w)
  1472. in->scroll++;
  1473. if (in->scroll > in->len)
  1474. in->scroll = in->len;
  1475. }
  1476. /* Secret fields are measured and painted from a '*' mask: the real text is
  1477. * swapped out for everything except the edit loop, so the caret, selection
  1478. * and click positions all follow the masked width. */
  1479. static char secret_save[NGUI_INPUT_MAX];
  1480. static void secret_mask(struct ngui_input *in)
  1481. {
  1482. unsigned int i;
  1483. for (i = 0; i <= in->len && i < NGUI_INPUT_MAX; i++) {
  1484. secret_save[i] = in->text[i];
  1485. in->text[i] = i < in->len ? '*' : '\0';
  1486. }
  1487. }
  1488. static void secret_unmask(struct ngui_input *in)
  1489. {
  1490. unsigned int i;
  1491. for (i = 0; i <= in->len && i < NGUI_INPUT_MAX; i++) {
  1492. in->text[i] = secret_save[i];
  1493. secret_save[i] = 0;
  1494. }
  1495. }
  1496. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1497. struct ngui_input *in)
  1498. {
  1499. struct ngui_rect inner;
  1500. int focused = !in->disabled && (ui->focus == id);
  1501. int result = 0;
  1502. int avail, line_h, k;
  1503. int changed = 0;
  1504. line_h = ngui_text_line_height(ui, ui->font_px, 0);
  1505. if (line_h < 1)
  1506. line_h = 16;
  1507. inner = rect_make(r.x + 4, r.y + (r.h - line_h) / 2, r.w - 8,
  1508. line_h);
  1509. if (inner.w < 0)
  1510. inner.w = 0;
  1511. if (inner.h < 1)
  1512. inner.h = 1;
  1513. avail = inner.w;
  1514. if (in->secret)
  1515. secret_mask(in);
  1516. if (in->disabled) {
  1517. /* A field that became disabled while focused gives the focus
  1518. * back, and a press cannot start a drag or a selection. */
  1519. if (ui->focus == id)
  1520. ngui_set_focus(ui, 0);
  1521. in->dragging = 0;
  1522. } else if (ui->pressed &&
  1523. ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
  1524. unsigned int now = u_ticks_ms();
  1525. int dbl = in->last_click_ms &&
  1526. (unsigned int)(now - in->last_click_ms) <=
  1527. NGUI_DOUBLE_CLICK_MS;
  1528. if (dbl) {
  1529. /* Double click: select the whole text and keep it
  1530. * selected while the second press is held. */
  1531. in->anchor = 0;
  1532. in->cursor = in->len;
  1533. in->dragging = 0;
  1534. } else {
  1535. unsigned int pos = input_pos_at(ui, in, inner.x,
  1536. ui->press_x);
  1537. in->cursor = pos;
  1538. in->anchor = pos;
  1539. in->dragging = 1;
  1540. }
  1541. in->last_click_ms = now;
  1542. ui->focus = id;
  1543. ui->focus_claimed = 1;
  1544. ui->last_blink = now;
  1545. ui->caret_on = 1;
  1546. focused = 1;
  1547. } else if (ui->pressed) {
  1548. in->dragging = 0;
  1549. }
  1550. /* Extend the selection while the button that started in this field is
  1551. * still held. Sampled coordinates may be outside the canvas. The
  1552. * release *event* is authoritative: the sampled button state can lag
  1553. * the event queue by a frame. */
  1554. if (!in->disabled && in->dragging && (ui->buttons & 1) &&
  1555. !ui->released) {
  1556. in->cursor = input_pos_at(ui, in, inner.x, ui->mouse_x);
  1557. /* Keep redrawing when the pointer leaves the window. */
  1558. ngui_invalidate(ui);
  1559. }
  1560. if (ui->released)
  1561. in->dragging = 0;
  1562. if (focused) {
  1563. if (in->secret)
  1564. secret_unmask(in);
  1565. while ((k = ngui_key(ui)) != 0) {
  1566. if (in->secret) {
  1567. /* No word boundaries inside a password. */
  1568. if (k == NGUI_KEY_CTRL_LEFT)
  1569. k = NGUI_KEY_HOME;
  1570. else if (k == NGUI_KEY_CTRL_RIGHT)
  1571. k = NGUI_KEY_END;
  1572. }
  1573. if (k == '\n' || k == '\r') {
  1574. result |= NGUI_INPUT_SUBMIT;
  1575. break;
  1576. }
  1577. if (k == NGUI_KEY_ESC || k == NGUI_KEY_TAB) {
  1578. result |= NGUI_INPUT_CANCEL;
  1579. if (k == NGUI_KEY_TAB)
  1580. result |= NGUI_INPUT_NEXT;
  1581. ui->focus = 0;
  1582. break;
  1583. }
  1584. if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
  1585. if (!input_delete_selection(in))
  1586. input_delete(in, 1);
  1587. changed = 1;
  1588. } else if (k == NGUI_KEY_DELETE) {
  1589. if (!input_delete_selection(in))
  1590. input_delete(in, 0);
  1591. changed = 1;
  1592. } else if (k == NGUI_KEY_CTRL_BACKSPACE) {
  1593. if (input_delete_selection(in))
  1594. changed = 1;
  1595. else
  1596. changed = input_delete_range(in,
  1597. input_word_prev(in, in->cursor),
  1598. in->cursor);
  1599. } else if (k == NGUI_KEY_CTRL_DELETE) {
  1600. if (input_delete_selection(in)) {
  1601. changed = 1;
  1602. } else {
  1603. unsigned int a = in->cursor;
  1604. unsigned int b = input_word_delete_end(in,
  1605. a);
  1606. /* At the end of the line, delete the word
  1607. * before the caret instead of doing
  1608. * nothing. */
  1609. if (b == a) {
  1610. a = input_word_prev(in, in->cursor);
  1611. b = in->cursor;
  1612. }
  1613. changed = input_delete_range(in, a, b);
  1614. }
  1615. } else if (k == NGUI_KEY_LEFT) {
  1616. input_move_to(in, in->cursor > 0 ? in->cursor - 1 : 0);
  1617. } else if (k == NGUI_KEY_RIGHT) {
  1618. input_move_to(in, in->cursor < in->len
  1619. ? in->cursor + 1 : in->len);
  1620. } else if (k == NGUI_KEY_CTRL_LEFT) {
  1621. input_move_to(in, input_word_prev(in, in->cursor));
  1622. } else if (k == NGUI_KEY_CTRL_RIGHT) {
  1623. input_move_to(in, input_word_next(in, in->cursor));
  1624. } else if (k == NGUI_KEY_HOME) {
  1625. input_move_to(in, 0);
  1626. } else if (k == NGUI_KEY_END) {
  1627. input_move_to(in, in->len);
  1628. } else if (k == 0x01) { /* Ctrl+A: select all */
  1629. in->anchor = 0;
  1630. in->cursor = in->len;
  1631. } else if (k == 0x15) { /* Ctrl+U */
  1632. in->len = 0;
  1633. in->cursor = 0;
  1634. in->anchor = 0;
  1635. in->text[0] = '\0';
  1636. changed = 1;
  1637. } else if (k >= 0x20 && k <= 0x7E) {
  1638. input_insert(in, (char)k);
  1639. changed = 1;
  1640. }
  1641. }
  1642. if (in->secret)
  1643. secret_mask(in);
  1644. }
  1645. ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
  1646. /* Inset edge: the shadow sits on the top/left, the light on the
  1647. * bottom/right. A focused field keeps the shape in accent blue. */
  1648. if (focused)
  1649. bevel_rect(ui, r, ui->theme.accent,
  1650. shade565(ui->theme.accent, 4));
  1651. else
  1652. bevel_rect(ui, r, shade565(ui->theme.panel, -4),
  1653. shade565(ui->theme.panel, 4));
  1654. if (!in->disabled)
  1655. input_scroll_to_cursor(ui, in, avail);
  1656. ngui_push_clip(ui, inner);
  1657. if (in->len == 0 && in->placeholder) {
  1658. ngui_text_px(ui, inner.x, inner.y, in->placeholder,
  1659. slen(in->placeholder), ui->theme.text_dim,
  1660. ui->font_px, 0, 0, 0);
  1661. } else {
  1662. if (focused)
  1663. input_draw_selection(ui, in, inner);
  1664. ngui_text_px(ui, inner.x, inner.y, in->text + in->scroll,
  1665. slen(in->text + in->scroll),
  1666. in->disabled ? ui->theme.text_dim :
  1667. ui->theme.text,
  1668. ui->font_px, 0, 0, 0);
  1669. }
  1670. if (focused && ui->caret_on) {
  1671. int cx = 0;
  1672. if (in->cursor > in->scroll)
  1673. cx = input_text_w(ui, in->text + in->scroll,
  1674. in->cursor - in->scroll);
  1675. if (cx < 0)
  1676. cx = 0;
  1677. if (avail > 0 && cx >= avail)
  1678. cx = avail - 1;
  1679. if (avail > 0 && cx < avail)
  1680. ngui_fill_rect(ui, rect_make(inner.x + cx, inner.y,
  1681. 1, inner.h),
  1682. ui->theme.accent);
  1683. }
  1684. ngui_pop_clip(ui);
  1685. if (in->secret)
  1686. secret_unmask(in);
  1687. if (changed)
  1688. result |= NGUI_INPUT_CHANGED;
  1689. return result;
  1690. }
  1691. static int scrollable(const struct ngui_scroll *s)
  1692. {
  1693. return s->content_h > s->view_h && s->view_h > 0;
  1694. }
  1695. static int scroll_thumb_h(const struct ngui_rect *r,
  1696. const struct ngui_scroll *s)
  1697. {
  1698. int h = 0;
  1699. if (s->content_h > 0)
  1700. h = r->h * s->view_h / s->content_h;
  1701. if (h < 12)
  1702. h = 12;
  1703. if (h > r->h)
  1704. h = r->h;
  1705. return h;
  1706. }
  1707. static int scroll_thumb_y(const struct ngui_rect *r,
  1708. const struct ngui_scroll *s)
  1709. {
  1710. int th = scroll_thumb_h(r, s);
  1711. int avail = r->h - th;
  1712. int max = s->content_h - s->view_h;
  1713. if (avail <= 0 || max <= 0)
  1714. return r->y;
  1715. return r->y + s->offset * avail / max;
  1716. }
  1717. static void scroll_clamp(struct ngui_scroll *s)
  1718. {
  1719. int max = s->content_h - s->view_h;
  1720. if (max < 0)
  1721. max = 0;
  1722. if (s->offset > max)
  1723. s->offset = max;
  1724. if (s->offset < 0)
  1725. s->offset = 0;
  1726. }
  1727. void ngui_scroll_to(struct ngui_scroll *s, int offset)
  1728. {
  1729. s->offset = offset;
  1730. scroll_clamp(s);
  1731. }
  1732. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
  1733. {
  1734. if (y < s->offset)
  1735. s->offset = y;
  1736. else if (y + h > s->offset + s->view_h)
  1737. s->offset = y + h - s->view_h;
  1738. scroll_clamp(s);
  1739. }
  1740. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  1741. struct ngui_scroll *s)
  1742. {
  1743. struct ngui_rect bar;
  1744. int scrolls = scrollable(s);
  1745. s->view_h = r.h;
  1746. scroll_clamp(s);
  1747. if (!ui->buttons)
  1748. s->dragging = 0;
  1749. if (scrolls) {
  1750. int th = scroll_thumb_h(&r, s);
  1751. int ty = scroll_thumb_y(&r, s);
  1752. bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1753. if (ui->pressed &&
  1754. ngui_rect_contains(&bar, ui->press_x, ui->press_y)) {
  1755. if (ui->press_y >= ty && ui->press_y < ty + th) {
  1756. s->dragging = 1;
  1757. s->drag_dy = ui->press_y - ty;
  1758. } else {
  1759. int rel = ui->press_y - r.y - th / 2;
  1760. int avail = r.h - th;
  1761. int max = s->content_h - s->view_h;
  1762. if (rel < 0)
  1763. rel = 0;
  1764. if (rel > avail)
  1765. rel = avail;
  1766. if (avail > 0)
  1767. s->offset = rel * max / avail;
  1768. s->dragging = 1;
  1769. s->drag_dy = th / 2;
  1770. scroll_clamp(s);
  1771. }
  1772. }
  1773. /* Sampled buttons are authoritative while held: some event paths
  1774. * deliver the press late or coalesce it with the release. Start
  1775. * the drag from the current pointer position as well. */
  1776. if (!s->dragging && (ui->buttons & 1) &&
  1777. ngui_rect_contains(&bar, ui->mouse_x, ui->mouse_y)) {
  1778. if (ui->mouse_y >= ty && ui->mouse_y < ty + th) {
  1779. s->dragging = 1;
  1780. s->drag_dy = ui->mouse_y - ty;
  1781. } else {
  1782. int rel = ui->mouse_y - r.y - th / 2;
  1783. int avail = r.h - th;
  1784. int max = s->content_h - s->view_h;
  1785. if (rel < 0)
  1786. rel = 0;
  1787. if (rel > avail)
  1788. rel = avail;
  1789. if (avail > 0)
  1790. s->offset = rel * max / avail;
  1791. s->dragging = 1;
  1792. s->drag_dy = th / 2;
  1793. scroll_clamp(s);
  1794. }
  1795. }
  1796. if (s->dragging && (ui->buttons & 1)) {
  1797. int rel = ui->mouse_y - r.y - s->drag_dy;
  1798. int avail = r.h - th;
  1799. int max = s->content_h - s->view_h;
  1800. if (rel < 0)
  1801. rel = 0;
  1802. if (rel > avail)
  1803. rel = avail;
  1804. if (avail > 0)
  1805. s->offset = rel * max / avail;
  1806. scroll_clamp(s);
  1807. }
  1808. if (ui->released)
  1809. s->dragging = 0;
  1810. }
  1811. ngui_push_clip(ui, r);
  1812. }
  1813. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  1814. struct ngui_scroll *s)
  1815. {
  1816. ngui_pop_clip(ui);
  1817. if (!scrollable(s))
  1818. return;
  1819. {
  1820. struct ngui_rect bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1821. int th = scroll_thumb_h(&r, s);
  1822. int ty = scroll_thumb_y(&r, s);
  1823. struct ngui_rect thumb = rect_make(bar.x, ty, 14, th);
  1824. ngui_fill_rect(ui, bar, ui->theme.panel_alt);
  1825. ngui_fill_rect(ui, thumb, s->dragging ?
  1826. ui->theme.accent : ui->theme.text_dim);
  1827. }
  1828. }
  1829. int ngui_scroll_wheel(struct ngui *ui, struct ngui_rect r,
  1830. struct ngui_scroll *s)
  1831. {
  1832. if (!ui->scroll_y ||
  1833. !ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y))
  1834. return 0;
  1835. ngui_scroll_to(s, s->offset + ui->scroll_y);
  1836. ui->scroll_y = 0;
  1837. return 1;
  1838. }
  1839. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s)
  1840. {
  1841. int k = ngui_key_peek(ui);
  1842. int page;
  1843. if (k != NGUI_KEY_PAGE_UP && k != NGUI_KEY_PAGE_DOWN)
  1844. return 0;
  1845. page = s->view_h - 22;
  1846. if (page < 22)
  1847. page = 22;
  1848. ngui_key(ui);
  1849. if (k == NGUI_KEY_PAGE_UP)
  1850. s->offset -= page;
  1851. else
  1852. s->offset += page;
  1853. scroll_clamp(s);
  1854. return 1;
  1855. }