ngui.c 56 KB

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