ngui.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359
  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->right_pressed = ui->right_released = 0;
  187. ui->clicked = 0;
  188. ui->focus_claimed = 0;
  189. ui->hot = 0;
  190. if (u_win_info(&wi) == 0) {
  191. if (wi.width != (unsigned int)ui->w ||
  192. wi.height != (unsigned int)ui->h) {
  193. if (u_win_ack_resize() == 0) {
  194. ui->px = (unsigned short *)wi.fb;
  195. ui->w = (int)wi.width;
  196. ui->h = (int)wi.height;
  197. }
  198. want = 1;
  199. } else if (wi.fb && wi.fb != (void *)ui->px) {
  200. ui->px = (unsigned short *)wi.fb;
  201. }
  202. }
  203. if (u_win_pointer(&wp) == 0) {
  204. ui->mouse_x = wp.x;
  205. ui->mouse_y = wp.y;
  206. ui->buttons = wp.buttons;
  207. ui->inside = wp.inside ? 1 : 0;
  208. ui->focused = wp.focused ? 1 : 0;
  209. } else {
  210. ui->mouse_x = ui->mouse_y = 0;
  211. ui->buttons = 0;
  212. ui->inside = 0;
  213. ui->focused = 0;
  214. }
  215. if (u_win_scroll(&ws) == 1) {
  216. ui->scroll_x = ws.dx;
  217. ui->scroll_y = ws.dy;
  218. if (ws.dx || ws.dy)
  219. want = 1;
  220. } else {
  221. ui->scroll_x = ui->scroll_y = 0;
  222. }
  223. for (i = 0; i < 16; i++) {
  224. int r = u_win_event(&ev);
  225. if (r != 1)
  226. break;
  227. if (ev.type == WIN_EV_POINTER_DOWN) {
  228. unsigned int b = ev.button ? ev.button : ev.buttons;
  229. if (b & 1) {
  230. ui->pressed |= 1;
  231. ui->press_x = ev.x;
  232. ui->press_y = ev.y;
  233. ui->clicked = 1;
  234. }
  235. if (b & 2) {
  236. ui->right_pressed |= 1;
  237. ui->right_press_x = ev.x;
  238. ui->right_press_y = ev.y;
  239. }
  240. } else if (ev.type == WIN_EV_POINTER_UP) {
  241. unsigned int b = ev.button ? ev.button :
  242. (~ev.buttons & 3);
  243. if (b & 1) {
  244. ui->released |= 1;
  245. ui->release_x = ev.x;
  246. ui->release_y = ev.y;
  247. }
  248. if (b & 2) {
  249. ui->right_released |= 1;
  250. ui->right_release_x = ev.x;
  251. ui->right_release_y = ev.y;
  252. }
  253. }
  254. }
  255. for (i = 0; i < 32; i++) {
  256. k = u_key_poll();
  257. if (!k)
  258. break;
  259. key_push(ui, k);
  260. want = 1;
  261. }
  262. now = u_ticks_ms();
  263. if (ui->focus) {
  264. if (now - ui->last_blink >= 500) {
  265. ui->last_blink = now;
  266. ui->caret_on = !ui->caret_on;
  267. want = 1;
  268. }
  269. } else {
  270. ui->caret_on = 1;
  271. }
  272. if (ui->inside != ui->last_inside) {
  273. want = 1;
  274. ui->motion_pending = 0;
  275. ui->motion_ms = now;
  276. } else if (ui->inside &&
  277. (ui->mouse_x != ui->last_mouse_x ||
  278. ui->mouse_y != ui->last_mouse_y)) {
  279. /* Hover feedback needs a repaint, but not one per mouse
  280. * report: while the pointer keeps moving, redraw at most every
  281. * 33 ms and remember that a final frame is still due. A held
  282. * button is a drag, which stays at full rate. */
  283. if (ui->buttons) {
  284. ui->motion_ms = now;
  285. ui->motion_pending = 0;
  286. want = 1;
  287. } else {
  288. ui->motion_pending = 1;
  289. if ((unsigned int)(now - ui->motion_ms) >=
  290. NGUI_MOTION_REDRAW_MS) {
  291. ui->motion_ms = now;
  292. ui->motion_pending = 0;
  293. want = 1;
  294. }
  295. }
  296. } else if (ui->motion_pending &&
  297. (unsigned int)(now - ui->motion_ms) >= NGUI_MOTION_REDRAW_MS) {
  298. ui->motion_ms = now;
  299. ui->motion_pending = 0;
  300. want = 1;
  301. }
  302. if (ui->focused != ui->last_focused)
  303. want = 1;
  304. ui->last_mouse_x = ui->mouse_x;
  305. ui->last_mouse_y = ui->mouse_y;
  306. ui->last_inside = ui->inside;
  307. ui->last_focused = ui->focused;
  308. if (ui->pressed || ui->released || ui->right_pressed ||
  309. ui->right_released)
  310. want = 1;
  311. if (want)
  312. ui->motion_pending = 0;
  313. ui->cursor_frame = want;
  314. if (want)
  315. ui->cursor = CURSOR_DEFAULT;
  316. return want;
  317. }
  318. void ngui_set_cursor(struct ngui *ui, int shape)
  319. {
  320. ui->cursor = shape;
  321. }
  322. int ngui_cursor_rect(struct ngui *ui, struct ngui_rect r, int shape)
  323. {
  324. if (!ui->inside || !ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y))
  325. return 0;
  326. if (ui->nclip > 0 &&
  327. !ngui_rect_contains(&ui->clip[ui->nclip - 1], ui->mouse_x,
  328. ui->mouse_y))
  329. return 0;
  330. ui->cursor = shape;
  331. return 1;
  332. }
  333. void ngui_end_ex(struct ngui *ui, int present)
  334. {
  335. if (present && u_win_present() != 0)
  336. ui->dirty = 1;
  337. /* Only a redrawn frame has evaluated every widget's hover state. */
  338. if (ui->cursor_frame && ui->cursor != ui->cursor_sent &&
  339. u_win_set_cursor(ui->cursor) == 0)
  340. ui->cursor_sent = ui->cursor;
  341. ui->cursor_frame = 0;
  342. if (ui->clicked && !ui->focus_claimed)
  343. ui->focus = 0;
  344. /* Only the release edge ends a press. The sampled button state is not
  345. * synchronized with the event queue: it can read 0 for a frame before
  346. * the release event is delivered. Clearing active on "!buttons" made
  347. * clicks fire only when the two happened to land in the same frame. */
  348. if (ui->released)
  349. ui->active = 0;
  350. ui->pressed = ui->released = 0;
  351. ui->clicked = 0;
  352. ui->focus_claimed = 0;
  353. ui->press_x = ui->press_y = 0;
  354. ui->release_x = ui->release_y = 0;
  355. }
  356. void ngui_end(struct ngui *ui)
  357. {
  358. ngui_end_ex(ui, 1);
  359. }
  360. struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h)
  361. {
  362. struct ngui_rect out = *r;
  363. if (h < 0)
  364. h = 0;
  365. if (h > r->h)
  366. h = r->h;
  367. out.h = h;
  368. r->y += h;
  369. r->h -= h;
  370. return out;
  371. }
  372. struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h)
  373. {
  374. struct ngui_rect out = *r;
  375. if (h < 0)
  376. h = 0;
  377. if (h > r->h)
  378. h = r->h;
  379. out.y = r->y + r->h - h;
  380. out.h = h;
  381. r->h -= h;
  382. return out;
  383. }
  384. struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w)
  385. {
  386. struct ngui_rect out = *r;
  387. if (w < 0)
  388. w = 0;
  389. if (w > r->w)
  390. w = r->w;
  391. out.w = w;
  392. r->x += w;
  393. r->w -= w;
  394. return out;
  395. }
  396. struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w)
  397. {
  398. struct ngui_rect out = *r;
  399. if (w < 0)
  400. w = 0;
  401. if (w > r->w)
  402. w = r->w;
  403. out.x = r->x + r->w - w;
  404. out.w = w;
  405. r->w -= w;
  406. return out;
  407. }
  408. void ngui_inset(struct ngui_rect *r, int n)
  409. {
  410. r->x += n;
  411. r->y += n;
  412. r->w -= n * 2;
  413. r->h -= n * 2;
  414. if (r->w < 0)
  415. r->w = 0;
  416. if (r->h < 0)
  417. r->h = 0;
  418. }
  419. void ngui_push_clip(struct ngui *ui, struct ngui_rect r)
  420. {
  421. struct ngui_rect out;
  422. struct ngui_rect canvas;
  423. if (ui->nclip >= 4)
  424. return;
  425. canvas = rect_make(0, 0, ui->w, ui->h);
  426. if (!rect_intersect(&r, &canvas, &out))
  427. out = rect_make(0, 0, 0, 0);
  428. if (ui->nclip > 0) {
  429. struct ngui_rect clipped = out;
  430. if (!rect_intersect(&clipped, &ui->clip[ui->nclip - 1], &out))
  431. out = rect_make(0, 0, 0, 0);
  432. }
  433. ui->clip[ui->nclip++] = out;
  434. }
  435. void ngui_pop_clip(struct ngui *ui)
  436. {
  437. if (ui->nclip > 0)
  438. ui->nclip--;
  439. }
  440. static int pixel_visible(struct ngui *ui, int x, int y)
  441. {
  442. struct ngui_rect *c;
  443. if (x < 0 || y < 0 || x >= ui->w || y >= ui->h)
  444. return 0;
  445. if (ui->nclip <= 0)
  446. return 1;
  447. c = &ui->clip[ui->nclip - 1];
  448. return x >= c->x && y >= c->y &&
  449. x < c->x + c->w && y < c->y + c->h;
  450. }
  451. void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color)
  452. {
  453. struct ngui_rect canvas;
  454. int x, y;
  455. if (rect_empty(&r))
  456. return;
  457. canvas = rect_make(0, 0, ui->w, ui->h);
  458. if (!rect_intersect(&r, &canvas, &r))
  459. return;
  460. if (ui->nclip > 0 &&
  461. !rect_intersect(&r, &ui->clip[ui->nclip - 1], &r))
  462. return;
  463. for (y = r.y; y < r.y + r.h; y++) {
  464. unsigned short *p = ui->px + y * ui->w + r.x;
  465. for (x = 0; x < r.w; x++)
  466. p[x] = color;
  467. }
  468. }
  469. void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color)
  470. {
  471. if (r.w <= 0 || r.h <= 0)
  472. return;
  473. ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), color);
  474. ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), color);
  475. if (r.h > 2) {
  476. ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), color);
  477. ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
  478. r.h - 2), color);
  479. }
  480. }
  481. /* Brighten (step > 0) or darken (step < 0) an RGB565 color for the
  482. * one-pixel bevel. Green has six bits, red and blue five, so the green
  483. * component moves twice as far to keep the shade visually even. */
  484. static unsigned short shade565(unsigned short c, int step)
  485. {
  486. int r = (int)((c >> 11) & 0x1F) + step;
  487. int g = (int)((c >> 5) & 0x3F) + step * 2;
  488. int b = (int)(c & 0x1F) + step;
  489. if (r < 0)
  490. r = 0;
  491. if (r > 0x1F)
  492. r = 0x1F;
  493. if (g < 0)
  494. g = 0;
  495. if (g > 0x3F)
  496. g = 0x3F;
  497. if (b < 0)
  498. b = 0;
  499. if (b > 0x1F)
  500. b = 0x1F;
  501. return (unsigned short)((r << 11) | (g << 5) | b);
  502. }
  503. /* A one-pixel two-tone edge: `tl` along the top and left, `br` along the
  504. * bottom and right. A raised widget lights its top/left and shades its
  505. * bottom/right; an inset one (a text field) swaps the tones. */
  506. static void bevel_rect(struct ngui *ui, struct ngui_rect r,
  507. unsigned short tl, unsigned short br)
  508. {
  509. if (r.w <= 0 || r.h <= 0)
  510. return;
  511. ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), tl);
  512. ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), br);
  513. if (r.h > 2) {
  514. ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), tl);
  515. ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
  516. r.h - 2), br);
  517. }
  518. }
  519. /* Raised button edge: lit on the top/left, shaded on the bottom/right. A
  520. * pressed button inverts the bevel, so it reads as pushed in. The hovered
  521. * button keeps its accent on the lit edge. */
  522. static void button_bevel(struct ngui *ui, struct ngui_rect r,
  523. unsigned short bg, int hot, int down)
  524. {
  525. if (down)
  526. bevel_rect(ui, r, shade565(bg, -4), shade565(bg, 4));
  527. else if (hot)
  528. bevel_rect(ui, r, ui->theme.accent, shade565(bg, -4));
  529. else
  530. bevel_rect(ui, r, shade565(bg, 4), shade565(bg, -4));
  531. }
  532. static int ngui_isqrt(int v)
  533. {
  534. int r = 0, bit = 1 << 30;
  535. if (v <= 0)
  536. return 0;
  537. /* Classic shift-and-subtract sqrt: bit must be the highest power
  538. * of four that fits in v, and each step consumes two bits. */
  539. while (bit > v)
  540. bit >>= 2;
  541. while (bit) {
  542. if (v >= r + bit) {
  543. v -= r + bit;
  544. r = (r >> 1) + bit;
  545. } else {
  546. r >>= 1;
  547. }
  548. bit >>= 2;
  549. }
  550. return r;
  551. }
  552. /* Horizontal inset of one scanline of a corner with the given radius.
  553. * Samples the pixel centre, so a corner is as wide as it is tall. */
  554. static int round_inset(int y, int h, int r)
  555. {
  556. int dy2, d2;
  557. if (r <= 0)
  558. return 0;
  559. if (y < r)
  560. dy2 = 2 * r - 2 * y - 1;
  561. else if (y >= h - r)
  562. dy2 = 2 * (y - (h - r)) + 1;
  563. else
  564. return 0;
  565. /* d2 = (2r)^2 - dy2^2 = 4 * (r^2 - dy^2). The inset is
  566. * r - floor(sqrt(d2) / 2 + 0.5), i.e. r - (isqrt(d2) + 1) / 2.
  567. * d2 is 3 or 7 mod 8, never a perfect square, so the rounded
  568. * square root is exact. */
  569. d2 = 4 * r * r - dy2 * dy2;
  570. if (d2 < 0)
  571. d2 = 0;
  572. return r - (ngui_isqrt(d2) + 1) / 2;
  573. }
  574. void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
  575. int tl, int tr, int br, int bl,
  576. unsigned short color)
  577. {
  578. int y, half;
  579. if (rect_empty(&r))
  580. return;
  581. half = r.w < r.h ? r.w / 2 : r.h / 2;
  582. if (tl > half) tl = half;
  583. if (tr > half) tr = half;
  584. if (br > half) br = half;
  585. if (bl > half) bl = half;
  586. if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
  587. ngui_fill_rect(ui, r, color);
  588. return;
  589. }
  590. for (y = 0; y < r.h; y++) {
  591. int li = 0, ri = 0;
  592. if (y < tl)
  593. li = round_inset(y, r.h, tl);
  594. if (y < tr)
  595. ri = round_inset(y, r.h, tr);
  596. if (y >= r.h - bl) {
  597. int i = round_inset(y, r.h, bl);
  598. if (i > li)
  599. li = i;
  600. }
  601. if (y >= r.h - br) {
  602. int i = round_inset(y, r.h, br);
  603. if (i > ri)
  604. ri = i;
  605. }
  606. if (r.w - li - ri > 0)
  607. ngui_fill_rect(ui, rect_make(r.x + li, r.y + y,
  608. r.w - li - ri, 1), color);
  609. }
  610. }
  611. void ngui_frame_round(struct ngui *ui, struct ngui_rect r,
  612. int tl, int tr, int br, int bl, int thickness,
  613. unsigned short color)
  614. {
  615. int y, half;
  616. if (rect_empty(&r))
  617. return;
  618. if (thickness < 1)
  619. thickness = 1;
  620. half = r.w < r.h ? r.w / 2 : r.h / 2;
  621. if (tl > half) tl = half;
  622. if (tr > half) tr = half;
  623. if (br > half) br = half;
  624. if (bl > half) bl = half;
  625. if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
  626. ngui_frame(ui, r, color);
  627. return;
  628. }
  629. for (y = 0; y < r.h; y++) {
  630. int li = 0, ri = 0;
  631. int x0, x1;
  632. if (y < tl)
  633. li = round_inset(y, r.h, tl);
  634. if (y < tr)
  635. ri = round_inset(y, r.h, tr);
  636. if (y >= r.h - bl) {
  637. int i = round_inset(y, r.h, bl);
  638. if (i > li)
  639. li = i;
  640. }
  641. if (y >= r.h - br) {
  642. int i = round_inset(y, r.h, br);
  643. if (i > ri)
  644. ri = i;
  645. }
  646. x0 = r.x + li;
  647. x1 = r.x + r.w - 1 - ri;
  648. if (x0 > x1)
  649. continue;
  650. if (y < thickness || y >= r.h - thickness) {
  651. ngui_fill_rect(ui, rect_make(x0, r.y + y,
  652. x1 - x0 + 1, 1), color);
  653. } else {
  654. ngui_fill_rect(ui, rect_make(x0, r.y + y,
  655. thickness, 1), color);
  656. ngui_fill_rect(ui, rect_make(x1 - thickness + 1,
  657. r.y + y, thickness, 1), color);
  658. }
  659. }
  660. }
  661. static void ngui_pixel(struct ngui *ui, int x, int y, unsigned short color)
  662. {
  663. if (pixel_visible(ui, x, y))
  664. ui->px[y * ui->w + x] = color;
  665. }
  666. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  667. unsigned short color)
  668. {
  669. int dx = x1 - x0;
  670. int dy = y1 - y0;
  671. int sx = dx < 0 ? -1 : 1;
  672. int sy = dy < 0 ? -1 : 1;
  673. int err, e2;
  674. if (dx < 0)
  675. dx = -dx;
  676. if (dy < 0)
  677. dy = -dy;
  678. dy = -dy;
  679. err = dx + dy;
  680. for (;;) {
  681. ngui_pixel(ui, x0, y0, color);
  682. if (x0 == x1 && y0 == y1)
  683. break;
  684. e2 = err * 2;
  685. if (e2 >= dy) {
  686. err += dy;
  687. x0 += sx;
  688. }
  689. if (e2 <= dx) {
  690. err += dx;
  691. y0 += sy;
  692. }
  693. }
  694. }
  695. static void ngui_bitmap_glyph(struct ngui *ui, int x, int y, unsigned char c,
  696. unsigned short color, int scale, int bold)
  697. {
  698. const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  699. int row, col, sx, sy;
  700. if (scale < 1)
  701. scale = 1;
  702. for (row = 0; row < FONT_8X16_ROWS; row++) {
  703. unsigned char bits = g[row];
  704. if (!bits)
  705. continue;
  706. for (col = 0; col < 8; col++) {
  707. if (!(bits & (0x80u >> col)))
  708. continue;
  709. for (sy = 0; sy < scale; sy++) {
  710. for (sx = 0; sx < scale; sx++) {
  711. ngui_pixel(ui,
  712. x + col * scale + sx +
  713. (bold && scale == 1 ? 1 : 0),
  714. y + row * scale + sy,
  715. color);
  716. }
  717. }
  718. if (bold && scale > 1)
  719. ngui_pixel(ui, x + col * scale + scale,
  720. y + row * scale, color);
  721. }
  722. }
  723. }
  724. static struct ufont_face *ngui_face(struct ngui *ui, int bold, int italic,
  725. int mono)
  726. {
  727. struct ufont_face *f;
  728. if (!ui)
  729. return 0;
  730. if (mono && ui->font_mono)
  731. f = ui->font_mono;
  732. else if (bold && italic)
  733. f = ui->font_bold_italic;
  734. else if (bold)
  735. f = ui->font_bold;
  736. else if (italic)
  737. f = ui->font_italic;
  738. else
  739. f = ui->font;
  740. if (!f)
  741. f = ui->font;
  742. return f;
  743. }
  744. static void ngui_clip_bounds(struct ngui *ui, int *cx, int *cy,
  745. int *cw, int *ch)
  746. {
  747. struct ngui_rect r = { 0, 0, ui->w, ui->h };
  748. int i;
  749. for (i = 0; i < ui->nclip; i++) {
  750. struct ngui_rect out;
  751. if (!rect_intersect(&r, &ui->clip[i], &out)) {
  752. r.w = r.h = 0;
  753. break;
  754. }
  755. r = out;
  756. }
  757. *cx = r.x;
  758. *cy = r.y;
  759. *cw = r.w;
  760. *ch = r.h;
  761. }
  762. int ngui_text_ascent_face(struct ngui *ui, struct ufont_face *face, int px,
  763. int mono)
  764. {
  765. struct ufont_face *f = face ? face : ngui_face(ui, 0, 0, mono);
  766. struct ufont_metrics m;
  767. if (f) {
  768. ufont_metrics(f, px, &m);
  769. return m.ascent;
  770. }
  771. return 16 * (px >= 24 ? 2 : 1);
  772. }
  773. int ngui_text_ascent(struct ngui *ui, int px, int mono)
  774. {
  775. return ngui_text_ascent_face(ui, 0, px, mono);
  776. }
  777. int ngui_text_line_height_face(struct ngui *ui, struct ufont_face *face,
  778. int px, int mono)
  779. {
  780. struct ufont_face *f = face ? face : ngui_face(ui, 0, 0, mono);
  781. struct ufont_metrics m;
  782. if (f) {
  783. ufont_metrics(f, px, &m);
  784. return m.line_height;
  785. }
  786. return 16 * (px >= 24 ? 2 : 1);
  787. }
  788. int ngui_text_line_height(struct ngui *ui, int px, int mono)
  789. {
  790. return ngui_text_line_height_face(ui, 0, px, mono);
  791. }
  792. unsigned int ngui_text_width_face(struct ngui *ui, struct ufont_face *face,
  793. const char *s, unsigned int n, int px,
  794. int bold, int italic, int mono)
  795. {
  796. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  797. int scale;
  798. if (f)
  799. return (unsigned int)ufont_text_width(f, px, s, (int)n, 0);
  800. scale = px >= 24 ? 2 : 1;
  801. return n * 8u * (unsigned int)scale;
  802. }
  803. unsigned int ngui_text_width_px(struct ngui *ui, const char *s, unsigned int n,
  804. int px, int bold, int italic, int mono)
  805. {
  806. return ngui_text_width_face(ui, 0, s, n, px, bold, italic, mono);
  807. }
  808. unsigned int ngui_text_width_ui(struct ngui *ui, const char *s)
  809. {
  810. return ngui_text_width_px(ui, s, slen(s),
  811. ui ? ui->font_px : 14, 0, 0, 0);
  812. }
  813. unsigned int ngui_text_width(const char *s)
  814. {
  815. return slen(s) * 8;
  816. }
  817. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale)
  818. {
  819. (void)s;
  820. if (scale < 1)
  821. scale = 1;
  822. return n * 8u * (unsigned int)scale;
  823. }
  824. static void ngui_text_px_common(struct ngui *ui, struct ufont_face *face,
  825. int x, int y, const char *s, unsigned int n,
  826. unsigned short color, int px, int bold,
  827. int italic, int mono)
  828. {
  829. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  830. int scale, i, pen = 0, ascent;
  831. int cx, cy, cw, ch;
  832. if (!ui || !ui->px || !s)
  833. return;
  834. if (!f) {
  835. scale = px >= 24 ? 2 : 1;
  836. for (i = 0; i < (int)n && s[i]; i++)
  837. ngui_bitmap_glyph(ui, x + i * 8 * scale, y,
  838. (unsigned char)s[i], color,
  839. scale, bold);
  840. return;
  841. }
  842. ascent = ngui_text_ascent_face(ui, face, px, mono);
  843. ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
  844. for (i = 0; i < (int)n && s[i]; i++) {
  845. const struct ufont_glyph *g =
  846. ufont_glyph(f, px, (unsigned char)s[i]);
  847. if (!g) {
  848. pen += 8 << 6;
  849. continue;
  850. }
  851. ufont_blit_rgb565(ui->px, ui->w, ui->h,
  852. x + (pen >> 6), y + ascent, g, color,
  853. cx, cy, cw, ch);
  854. pen += g->advance;
  855. }
  856. }
  857. void ngui_text_px(struct ngui *ui, int x, int y, const char *s,
  858. unsigned int n, unsigned short color, int px, int bold,
  859. int italic, int mono)
  860. {
  861. ngui_text_px_common(ui, 0, x, y, s, n, color, px, bold, italic, mono);
  862. }
  863. /* ---- UTF-8 text with an emoji fallback face --------------------------- */
  864. static int utf8_skip_cp(unsigned int cp)
  865. {
  866. return cp == 0x200B || cp == 0x200C || cp == 0x200D ||
  867. cp == 0xFE0E || cp == 0xFE0F;
  868. }
  869. /* Advance of one codepoint in 26.6 pixels, choosing the primary face, then
  870. * the lazily loaded emoji face, then '?'. */
  871. static int ngui_utf8_advance(struct ngui *ui, struct ufont_face *f,
  872. int px, unsigned int cp)
  873. {
  874. struct ufont_face *emoji;
  875. if (utf8_skip_cp(cp) || !f)
  876. return 0;
  877. if (ufont_has_glyph(f, cp))
  878. return ufont_advance_cp(f, px, cp);
  879. emoji = ngui_emoji_face(ui);
  880. if (emoji && ufont_has_glyph(emoji, cp))
  881. return ufont_advance_cp(emoji, px, cp);
  882. return ufont_advance_cp(f, px, '?');
  883. }
  884. static unsigned int ngui_text_width_face_utf8(struct ngui *ui,
  885. struct ufont_face *face,
  886. const char *s, unsigned int n,
  887. int px, int bold, int italic,
  888. int mono)
  889. {
  890. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  891. int i = 0, total = 0;
  892. if (!f)
  893. return n * 8u * (px >= 24 ? 2u : 1u);
  894. while (i < (int)n) {
  895. unsigned int cp = ufont_utf8_next(s, &i, (int)n);
  896. total += ngui_utf8_advance(ui, f, px, cp);
  897. }
  898. return (unsigned int)((total + 32) >> 6);
  899. }
  900. static const struct ufont_glyph *ngui_utf8_glyph(struct ngui *ui,
  901. struct ufont_face *f,
  902. int px, unsigned int cp,
  903. struct ufont_face **used)
  904. {
  905. struct ufont_face *emoji;
  906. const struct ufont_glyph *g;
  907. *used = f;
  908. if (utf8_skip_cp(cp) || !f)
  909. return 0;
  910. if (ufont_has_glyph(f, cp)) {
  911. g = ufont_glyph(f, px, cp);
  912. if (g)
  913. return g;
  914. }
  915. emoji = ngui_emoji_face(ui);
  916. if (emoji && ufont_has_glyph(emoji, cp)) {
  917. g = ufont_glyph(emoji, px, cp);
  918. if (g) {
  919. *used = emoji;
  920. return g;
  921. }
  922. }
  923. *used = f;
  924. return ufont_glyph(f, px, '?');
  925. }
  926. static void ngui_text_px_utf8_common(struct ngui *ui, struct ufont_face *face,
  927. int x, int y, const char *s,
  928. unsigned int n, unsigned short color,
  929. int px, int bold, int italic, int mono)
  930. {
  931. struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
  932. int i = 0, pen = 0, ascent;
  933. int cx, cy, cw, ch;
  934. if (!ui || !ui->px || !s)
  935. return;
  936. if (!f) {
  937. ngui_text_px_common(ui, face, x, y, s, n, color, px, bold,
  938. italic, mono);
  939. return;
  940. }
  941. ascent = ngui_text_ascent_face(ui, face, px, mono);
  942. ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
  943. while (i < (int)n) {
  944. unsigned int cp = ufont_utf8_next(s, &i, (int)n);
  945. struct ufont_face *used;
  946. const struct ufont_glyph *g;
  947. if (utf8_skip_cp(cp))
  948. continue;
  949. g = ngui_utf8_glyph(ui, f, px, cp, &used);
  950. if (!g) {
  951. pen += 8 << 6;
  952. continue;
  953. }
  954. ufont_blit_rgb565(ui->px, ui->w, ui->h,
  955. x + (pen >> 6), y + ascent, g, color,
  956. cx, cy, cw, ch);
  957. pen += g->advance;
  958. }
  959. }
  960. void ngui_text_n_utf8(struct ngui *ui, int x, int y, const char *s,
  961. unsigned int n, unsigned short color)
  962. {
  963. ngui_text_px_utf8_common(ui, 0, x, y, s, n, color,
  964. ui ? ui->font_px : 14, 0, 0, 0);
  965. }
  966. void ngui_text_utf8(struct ngui *ui, int x, int y, const char *s,
  967. unsigned short color)
  968. {
  969. ngui_text_n_utf8(ui, x, y, s, s ? slen(s) : 0, color);
  970. }
  971. unsigned int ngui_text_width_utf8(struct ngui *ui, const char *s)
  972. {
  973. return ngui_text_width_face_utf8(ui, 0, s, s ? slen(s) : 0,
  974. ui ? ui->font_px : 14, 0, 0, 0);
  975. }
  976. void ngui_text_ellipsis_utf8(struct ngui *ui, struct ngui_rect r,
  977. int x, int y, const char *s,
  978. unsigned short color)
  979. {
  980. unsigned int n = s ? slen(s) : 0;
  981. unsigned int end = 0, i = 0;
  982. int avail = r.x + r.w - x;
  983. int dots, width;
  984. if (!ui || !s)
  985. return;
  986. if (avail <= 0)
  987. return;
  988. width = (int)ngui_text_width_utf8(ui, s);
  989. if (width <= avail) {
  990. ngui_text_utf8(ui, x, y, s, color);
  991. return;
  992. }
  993. dots = (int)ngui_text_width_utf8(ui, "...");
  994. if (dots >= avail)
  995. return;
  996. while (i < n) {
  997. int next = i;
  998. (void)ufont_utf8_next(s, &next, (int)n);
  999. if ((int)ngui_text_width_face_utf8(ui, 0, s, (unsigned int)next,
  1000. ui->font_px, 0, 0, 0) +
  1001. dots > avail)
  1002. break;
  1003. i = (unsigned int)next;
  1004. end = i;
  1005. }
  1006. ngui_text_n_utf8(ui, x, y, s, end, color);
  1007. ngui_text_utf8(ui, x + (int)ngui_text_width_face_utf8(
  1008. ui, 0, s, end, ui->font_px, 0, 0, 0),
  1009. y, "...", color);
  1010. }
  1011. void ngui_text_px_face(struct ngui *ui, struct ufont_face *face, int x,
  1012. int y, const char *s, unsigned int n,
  1013. unsigned short color, int px, int bold, int italic,
  1014. int mono)
  1015. {
  1016. ngui_text_px_common(ui, face, x, y, s, n, color, px, bold, italic,
  1017. mono);
  1018. }
  1019. void ngui_text_n(struct ngui *ui, int x, int y, const char *s, unsigned int n,
  1020. unsigned short color)
  1021. {
  1022. ngui_text_px(ui, x, y, s, n, color,
  1023. ui ? ui->font_px : 14, 0, 0, 0);
  1024. }
  1025. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  1026. unsigned short color)
  1027. {
  1028. ngui_text_n(ui, x, y, s, slen(s), color);
  1029. }
  1030. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  1031. unsigned int n, unsigned short color, int scale, int bold)
  1032. {
  1033. if (scale < 1)
  1034. scale = 1;
  1035. if (ui && ui->font) {
  1036. ngui_text_px(ui, x, y, s, n, color,
  1037. ui->font_px * scale, bold, 0, 0);
  1038. return;
  1039. }
  1040. {
  1041. unsigned int i;
  1042. for (i = 0; i < n && s && s[i]; i++)
  1043. ngui_bitmap_glyph(ui, x + (int)i * 8 * scale, y,
  1044. (unsigned char)s[i], color,
  1045. scale, bold);
  1046. }
  1047. }
  1048. /* Draw a string upside down. The whole n-character block is rotated, so the
  1049. * first character ends up on the right; used for the mirrored corner index on
  1050. * a playing card. */
  1051. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  1052. unsigned int n, unsigned short color)
  1053. {
  1054. unsigned int i;
  1055. int row, col;
  1056. for (i = 0; i < n && s && s[i]; i++) {
  1057. const unsigned char *g =
  1058. &font_8x16[(unsigned int)(unsigned char)s[i] *
  1059. FONT_8X16_ROWS];
  1060. for (row = 0; row < FONT_8X16_ROWS; row++) {
  1061. unsigned char bits = g[row];
  1062. if (!bits)
  1063. continue;
  1064. for (col = 0; col < 8; col++) {
  1065. if (!(bits & (0x80u >> col)))
  1066. continue;
  1067. ngui_pixel(ui,
  1068. x + (int)(n - 1 - i) * 8 + 7 - col,
  1069. y + FONT_8X16_ROWS - 1 - row,
  1070. color);
  1071. }
  1072. }
  1073. }
  1074. }
  1075. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  1076. const char *s, unsigned short color)
  1077. {
  1078. unsigned int n = slen(s);
  1079. int avail = r.x + r.w - x;
  1080. unsigned int i, best = 0;
  1081. int dots;
  1082. if (avail <= 0)
  1083. return;
  1084. if (ui && ui->font) {
  1085. dots = (int)ngui_text_width_px(ui, "...", 3, ui->font_px,
  1086. 0, 0, 0);
  1087. for (i = 0; i <= n; i++) {
  1088. int w = (int)ngui_text_width_px(ui, s, i, ui->font_px,
  1089. 0, 0, 0);
  1090. if (w > avail)
  1091. break;
  1092. best = i;
  1093. }
  1094. if (best == n) {
  1095. ngui_text_n(ui, x, y, s, n, color);
  1096. return;
  1097. }
  1098. while (best > 0 &&
  1099. (int)ngui_text_width_px(ui, s, best, ui->font_px,
  1100. 0, 0, 0) + dots > avail)
  1101. best--;
  1102. ngui_text_n(ui, x, y, s, best, color);
  1103. ngui_text_n(ui, x + (int)ngui_text_width_px(ui, s, best,
  1104. ui->font_px, 0, 0, 0),
  1105. y, "...", 3, color);
  1106. return;
  1107. }
  1108. {
  1109. int max = avail / 8;
  1110. if (max <= 0)
  1111. return;
  1112. if (n <= (unsigned int)max || max < 4) {
  1113. for (i = 0; i < n && (int)i < max; i++)
  1114. ngui_bitmap_glyph(ui, x + (int)i * 8, y,
  1115. (unsigned char)s[i], color, 1, 0);
  1116. return;
  1117. }
  1118. for (i = 0; i < (unsigned int)(max - 3); i++)
  1119. ngui_bitmap_glyph(ui, x + (int)i * 8, y,
  1120. (unsigned char)s[i], color, 1, 0);
  1121. ngui_bitmap_glyph(ui, x + (max - 3) * 8, y, '.', color, 1, 0);
  1122. ngui_bitmap_glyph(ui, x + (max - 2) * 8, y, '.', color, 1, 0);
  1123. ngui_bitmap_glyph(ui, x + (max - 1) * 8, y, '.', color, 1, 0);
  1124. }
  1125. }
  1126. /* ---- Icons ------------------------------------------------------------
  1127. * The Adwaita symbolic masks are read once per process into BSS (not the
  1128. * libc heap, so the browser's heap_reset() cannot free them). The read is
  1129. * capped at the icons this build knows: a newer bundle with appended icons
  1130. * still works, an older one leaves the missing ids blank. */
  1131. #define NGUI_ICON_PX 16
  1132. #define NGUI_ICON_BYTES (NGUI_ICON_PX * NGUI_ICON_PX)
  1133. static unsigned char icon_data[sizeof(struct icon_file_hdr) +
  1134. NGUI_ICON_COUNT * NGUI_ICON_BYTES + 512];
  1135. static int icon_state; /* 0 untried, 1 loaded, -1 unavailable */
  1136. static unsigned int icon_count;
  1137. #define NGUI_ICON_NAME_(e, name, file) name,
  1138. static const char *const icon_names[NGUI_ICON_COUNT] = {
  1139. ICON_LIST(NGUI_ICON_NAME_)
  1140. };
  1141. #undef NGUI_ICON_NAME_
  1142. const char *ngui_icon_name(enum ngui_icon icon)
  1143. {
  1144. if ((unsigned int)icon >= NGUI_ICON_COUNT)
  1145. return "?";
  1146. return icon_names[icon];
  1147. }
  1148. static void icon_load(void)
  1149. {
  1150. const struct icon_file_hdr *h = (const struct icon_file_hdr *)icon_data;
  1151. unsigned int max = sizeof(icon_data) - 512, have;
  1152. int n, i;
  1153. icon_state = -1;
  1154. /* The FAT path may write a final partial sector past `max`; the
  1155. * buffer keeps 512 bytes of slack for it. */
  1156. n = u_syscall3(SYS_READ_FILE, (int)NGUI_ICON_PATH, (int)icon_data,
  1157. (int)max);
  1158. if (n < (int)sizeof(*h))
  1159. return;
  1160. for (i = 0; i < 8; i++)
  1161. if (h->magic[i] != ICON_FILE_MAGIC[i])
  1162. return;
  1163. if (h->size != NGUI_ICON_PX)
  1164. return;
  1165. have = ((unsigned int)n - sizeof(*h)) / NGUI_ICON_BYTES;
  1166. if (have > h->count)
  1167. have = h->count;
  1168. if (have > NGUI_ICON_COUNT)
  1169. have = NGUI_ICON_COUNT;
  1170. icon_count = have;
  1171. icon_state = 1;
  1172. }
  1173. static const unsigned char *icon_mask(enum ngui_icon icon)
  1174. {
  1175. if (icon_state == 0)
  1176. icon_load();
  1177. if (icon_state != 1 || (unsigned int)icon >= icon_count)
  1178. return 0;
  1179. return icon_data + sizeof(struct icon_file_hdr) +
  1180. (unsigned int)icon * NGUI_ICON_BYTES;
  1181. }
  1182. /* Tint a coverage mask with `color` and blend it over the canvas. */
  1183. static void icon_blit(struct ngui *ui, int x, int y,
  1184. const unsigned char *m, unsigned short color)
  1185. {
  1186. int cx, cy, cw, ch, i, j;
  1187. int sr = color >> 11, sg = (color >> 5) & 63, sb = color & 31;
  1188. ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
  1189. for (j = 0; j < NGUI_ICON_PX; j++) {
  1190. int py = y + j;
  1191. unsigned short *row;
  1192. if (py < cy || py >= cy + ch)
  1193. continue;
  1194. row = ui->px + py * ui->w;
  1195. for (i = 0; i < NGUI_ICON_PX; i++) {
  1196. int px = x + i, a = m[j * NGUI_ICON_PX + i];
  1197. unsigned short d;
  1198. int dr, dg, db;
  1199. if (!a || px < cx || px >= cx + cw)
  1200. continue;
  1201. if (a == 255) {
  1202. row[px] = color;
  1203. continue;
  1204. }
  1205. d = row[px];
  1206. dr = d >> 11;
  1207. dg = (d >> 5) & 63;
  1208. db = d & 31;
  1209. dr += ((sr - dr) * a + 127) / 255;
  1210. dg += ((sg - dg) * a + 127) / 255;
  1211. db += ((sb - db) * a + 127) / 255;
  1212. row[px] = (unsigned short)((dr << 11) | (dg << 5) | db);
  1213. }
  1214. }
  1215. }
  1216. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  1217. unsigned short color)
  1218. {
  1219. int x = r.x + r.w / 2 - 8;
  1220. int y = r.y + r.h / 2 - 8;
  1221. const unsigned char *m;
  1222. if (!ui || !ui->px)
  1223. return;
  1224. /* Without the bundle (a stale /ramfs or a host run without
  1225. * icons/) the icon is simply left out. */
  1226. m = icon_mask(icon);
  1227. if (m)
  1228. icon_blit(ui, x, y, m, color);
  1229. }
  1230. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1231. const char *label)
  1232. {
  1233. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1234. int press_hot = ui->pressed &&
  1235. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1236. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1237. int fired = 0;
  1238. unsigned short bg;
  1239. int tw, tx, ty;
  1240. if (press_hot)
  1241. ui->active = id;
  1242. if (ui->pressed && ui->released && press_hot && release_hot)
  1243. fired = 1;
  1244. if (ui->released && ui->active == id) {
  1245. if (release_hot)
  1246. fired = 1;
  1247. ui->active = 0;
  1248. }
  1249. if (ui->active == id)
  1250. bg = ui->theme.button_down;
  1251. else if (hot)
  1252. bg = ui->theme.button_hot;
  1253. else
  1254. bg = ui->theme.button;
  1255. ngui_fill_rect(ui, r, bg);
  1256. button_bevel(ui, r, bg, hot, ui->active == id);
  1257. if (label && label[0]) {
  1258. tw = (int)ngui_text_width_ui(ui, label);
  1259. tx = r.x + (r.w - tw) / 2;
  1260. ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
  1261. if (ty < r.y)
  1262. ty = r.y;
  1263. ngui_push_clip(ui, r);
  1264. ngui_text(ui, tx, ty, label, ui->theme.text);
  1265. ngui_pop_clip(ui);
  1266. }
  1267. if (hot)
  1268. ui->hot = id;
  1269. return fired;
  1270. }
  1271. /* One row of a drop-down menu: left-aligned label, optional check mark and
  1272. * right-aligned shortcut. Uses the same press/release/active semantics as
  1273. * ngui_button so a menu and its caller never disagree about a click. */
  1274. static int ngui_menu_item_common(struct ngui *ui, unsigned int id,
  1275. struct ngui_rect r, const char *label,
  1276. const char *shortcut, int checked, int utf8)
  1277. {
  1278. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1279. int press_hot = ui->pressed &&
  1280. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1281. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1282. int fired = 0;
  1283. int selected;
  1284. unsigned short bg, fg;
  1285. int ty;
  1286. if (press_hot)
  1287. ui->active = id;
  1288. if (ui->pressed && ui->released && press_hot && release_hot)
  1289. fired = 1;
  1290. if (ui->released && ui->active == id) {
  1291. if (release_hot)
  1292. fired = 1;
  1293. ui->active = 0;
  1294. }
  1295. selected = (ui->active == id) || hot;
  1296. bg = selected ? ui->theme.selection : ui->theme.panel;
  1297. fg = selected ? NGUI_RGB(255, 255, 255) : ui->theme.text;
  1298. ngui_fill_rect(ui, r, bg);
  1299. ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
  1300. if (ty < r.y)
  1301. ty = r.y;
  1302. ngui_push_clip(ui, r);
  1303. if (checked) {
  1304. ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
  1305. ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
  1306. }
  1307. if (label) {
  1308. if (utf8)
  1309. ngui_text_utf8(ui, r.x + 22, ty, label, fg);
  1310. else
  1311. ngui_text(ui, r.x + 22, ty, label, fg);
  1312. }
  1313. if (shortcut) {
  1314. int sw = (int)(utf8 ? ngui_text_width_utf8(ui, shortcut) :
  1315. ngui_text_width_ui(ui, shortcut));
  1316. if (utf8)
  1317. ngui_text_utf8(ui, r.x + r.w - 8 - sw, ty, shortcut,
  1318. fg);
  1319. else
  1320. ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
  1321. }
  1322. ngui_pop_clip(ui);
  1323. if (hot)
  1324. ui->hot = id;
  1325. return fired;
  1326. }
  1327. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1328. const char *label, const char *shortcut, int checked)
  1329. {
  1330. return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 0);
  1331. }
  1332. int ngui_menu_item_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1333. const char *label, const char *shortcut, int checked)
  1334. {
  1335. return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 1);
  1336. }
  1337. void ngui_popover_begin(struct ngui *ui, struct ngui_popover *p)
  1338. {
  1339. struct ngui_rect all = { 0, 0, ui->w, ui->h };
  1340. ngui_fill_rect(ui, all, ui->theme.bg);
  1341. p->body = rect_make(12, 12, ui->w - 24, ui->h - 12 - 34);
  1342. p->footer = rect_make(12, ui->h - 30, ui->w - 24, 18);
  1343. }
  1344. void ngui_popover_footer(struct ngui *ui, const struct ngui_popover *p,
  1345. const char *text, unsigned short color)
  1346. {
  1347. if (!p || !text || !text[0])
  1348. return;
  1349. ngui_push_clip(ui, p->footer);
  1350. ngui_text_utf8(ui, p->footer.x, p->footer.y, text, color);
  1351. ngui_pop_clip(ui);
  1352. }
  1353. int ngui_section(struct ngui *ui, struct ngui_rect r, const char *caption,
  1354. unsigned int action_id, const char *action)
  1355. {
  1356. struct ngui_rect br;
  1357. int fired = 0;
  1358. int y = r.y + (r.h - 17) / 2;
  1359. if (y < r.y)
  1360. y = r.y;
  1361. ngui_text_px(ui, r.x, y, caption, slen(caption), ui->theme.text_dim,
  1362. 14, 1, 0, 0);
  1363. if (action_id && action && action[0]) {
  1364. int tw = (int)ngui_text_width_ui(ui, action);
  1365. int bw = tw + 24;
  1366. if (bw < 58)
  1367. bw = 58;
  1368. br = rect_make(r.x + r.w - bw, r.y + (r.h - 24) / 2, bw, 24);
  1369. if (ngui_button(ui, action_id, br, action))
  1370. fired = 1;
  1371. }
  1372. return fired;
  1373. }
  1374. /* Width of one tab: the label plus 10 px on each side and room for the
  1375. * fresh-output dot. Reserved even without a dot, so the row does not move
  1376. * when a tab becomes fresh. */
  1377. static int ngui_tab_width(struct ngui *ui, const struct ngui_tab *tab)
  1378. {
  1379. return (int)ngui_text_width_ui(ui, tab->label ? tab->label : "") + 30;
  1380. }
  1381. static int ngui_tab_at(struct ngui *ui, struct ngui_rect r,
  1382. const struct ngui_tab *tabs, int count, int x, int y)
  1383. {
  1384. int i, tx = r.x;
  1385. if (!ngui_rect_contains(&r, x, y))
  1386. return -1;
  1387. for (i = 0; i < count && tx < r.x + r.w; i++) {
  1388. int w = ngui_tab_width(ui, &tabs[i]);
  1389. if (x >= tx && x < tx + w)
  1390. return i;
  1391. tx += w + 2;
  1392. }
  1393. return -1;
  1394. }
  1395. int ngui_tabs(struct ngui *ui, struct ngui_tabs *t, struct ngui_rect r,
  1396. const struct ngui_tab *tabs, int count)
  1397. {
  1398. int i, x, changed = 0;
  1399. int lh, ty;
  1400. if (!ui || !t || !tabs || count <= 0)
  1401. return 0;
  1402. if (t->active < 0 || t->active >= count)
  1403. t->active = 0;
  1404. if (!ui->pressed && !ui->released && !ui->buttons)
  1405. t->press = 0;
  1406. if (ui->pressed)
  1407. t->press = ngui_tab_at(ui, r, tabs, count,
  1408. ui->press_x, ui->press_y) + 1;
  1409. if (ui->released) {
  1410. int hit = ngui_tab_at(ui, r, tabs, count,
  1411. ui->release_x, ui->release_y);
  1412. if (hit >= 0 && hit + 1 == t->press && hit != t->active) {
  1413. t->active = hit;
  1414. changed = 1;
  1415. }
  1416. t->press = 0;
  1417. }
  1418. lh = ngui_text_line_height(ui, ui->font_px, 0);
  1419. ty = r.y + (r.h - lh) / 2;
  1420. if (ty < r.y)
  1421. ty = r.y;
  1422. ngui_push_clip(ui, r);
  1423. x = r.x;
  1424. for (i = 0; i < count && x < r.x + r.w; i++) {
  1425. const char *label = tabs[i].label ? tabs[i].label : "";
  1426. int w = ngui_tab_width(ui, &tabs[i]);
  1427. struct ngui_rect tr = rect_make(x, r.y, w, r.h);
  1428. int hot = ui->inside &&
  1429. ngui_rect_contains(&tr, ui->mouse_x, ui->mouse_y);
  1430. unsigned short color;
  1431. if (i == t->active)
  1432. ngui_fill_rect(ui, tr, ui->theme.panel_alt);
  1433. else if (hot || t->press == i + 1)
  1434. ngui_fill_rect(ui, tr, ui->theme.button);
  1435. color = (i == t->active || hot) ? ui->theme.text :
  1436. ui->theme.text_dim;
  1437. ngui_text(ui, x + 10, ty, label, color);
  1438. if (i == t->active)
  1439. ngui_fill_rect(ui, rect_make(x, r.y + r.h - 2, w, 2),
  1440. ui->theme.accent);
  1441. if (tabs[i].fresh && i != t->active)
  1442. ngui_fill_round_rect(ui,
  1443. rect_make(x + w - 12,
  1444. r.y + r.h / 2 - 2, 4, 4),
  1445. 2, 2, 2, 2, ui->theme.accent);
  1446. x += w + 2;
  1447. }
  1448. ngui_pop_clip(ui);
  1449. return changed;
  1450. }
  1451. /* Shared by the byte and UTF-8 variants: one hover/selection row with an
  1452. * optional signal icon and right-aligned text. */
  1453. static int ngui_list_row_common(struct ngui *ui, unsigned int id,
  1454. struct ngui_rect r,
  1455. const struct ngui_row *row, int utf8)
  1456. {
  1457. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1458. int press_hot = ui->pressed &&
  1459. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1460. int release_hot = ui->released &&
  1461. ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1462. int fired = 0;
  1463. int left = r.x + 10, right = r.x + r.w - 10;
  1464. int title_h, block_h, ty;
  1465. unsigned short bg = 0, fg, sub_fg, trail_fg;
  1466. if (press_hot)
  1467. ui->active = id;
  1468. if (ui->pressed && ui->released && press_hot && release_hot)
  1469. fired = 1;
  1470. if (ui->released && ui->active == id) {
  1471. if (release_hot)
  1472. fired = 1;
  1473. ui->active = 0;
  1474. }
  1475. if (row->selected)
  1476. bg = ui->theme.selection;
  1477. else if (hot || ui->active == id)
  1478. bg = ui->theme.panel_alt;
  1479. if (bg)
  1480. ngui_fill_round_rect(ui, r, NGUI_RADIUS, NGUI_RADIUS,
  1481. NGUI_RADIUS, NGUI_RADIUS, bg);
  1482. title_h = ngui_text_line_height(ui, ui->font_px, 0);
  1483. fg = ui->theme.text;
  1484. sub_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
  1485. trail_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
  1486. if (row->trailing && row->trailing[0]) {
  1487. int tw = (int)ngui_text_width_ui(ui, row->trailing);
  1488. right -= tw + 8;
  1489. if (utf8)
  1490. ngui_text_utf8(ui, right + 8,
  1491. r.y + (r.h - title_h) / 2,
  1492. row->trailing, trail_fg);
  1493. else
  1494. ngui_text(ui, right + 8,
  1495. r.y + (r.h - title_h) / 2,
  1496. row->trailing, trail_fg);
  1497. }
  1498. if (row->bars >= 0) {
  1499. struct ngui_rect br;
  1500. right -= 26;
  1501. br.x = right + 6;
  1502. br.y = r.y + (r.h - 14) / 2;
  1503. br.w = 18;
  1504. br.h = 14;
  1505. ngui_signal_bars(ui, br, row->bars,
  1506. row->selected ? ui->theme.text :
  1507. ui->theme.accent);
  1508. }
  1509. if (right <= left)
  1510. return fired;
  1511. block_h = row->subtitle && row->subtitle[0] ? title_h * 2 - 2 :
  1512. title_h;
  1513. ty = r.y + (r.h - block_h) / 2;
  1514. if (ty < r.y)
  1515. ty = r.y;
  1516. ngui_push_clip(ui, rect_make(left, r.y, right - left, r.h));
  1517. if (utf8)
  1518. ngui_text_utf8(ui, left, ty, row->title, fg);
  1519. else
  1520. ngui_text(ui, left, ty, row->title, fg);
  1521. if (row->subtitle && row->subtitle[0]) {
  1522. if (utf8)
  1523. ngui_text_utf8(ui, left, ty + title_h - 2,
  1524. row->subtitle, sub_fg);
  1525. else
  1526. ngui_text(ui, left, ty + title_h - 2, row->subtitle,
  1527. sub_fg);
  1528. }
  1529. ngui_pop_clip(ui);
  1530. if (hot)
  1531. ui->hot = id;
  1532. return fired;
  1533. }
  1534. int ngui_list_row(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1535. const struct ngui_row *row)
  1536. {
  1537. return ngui_list_row_common(ui, id, r, row, 0);
  1538. }
  1539. int ngui_list_row_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1540. const struct ngui_row *row)
  1541. {
  1542. return ngui_list_row_common(ui, id, r, row, 1);
  1543. }
  1544. void ngui_signal_bars(struct ngui *ui, struct ngui_rect r, int bars,
  1545. unsigned short color)
  1546. {
  1547. int i;
  1548. int base = r.y + r.h - 1;
  1549. for (i = 0; i < 4; i++) {
  1550. int h = 3 + i * 3;
  1551. struct ngui_rect br;
  1552. br.x = r.x + i * 5;
  1553. br.y = base - h;
  1554. br.w = 3;
  1555. br.h = h;
  1556. ngui_fill_rect(ui, br, i < bars ? color : ui->theme.button);
  1557. }
  1558. }
  1559. int ngui_tile(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1560. const char *title, const char *subtitle, int on)
  1561. {
  1562. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1563. int press_hot = ui->pressed &&
  1564. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1565. int release_hot = ui->released &&
  1566. ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1567. int fired = 0;
  1568. unsigned short bg, fg, sub_fg;
  1569. if (press_hot)
  1570. ui->active = id;
  1571. if (ui->pressed && ui->released && press_hot && release_hot)
  1572. fired = 1;
  1573. if (ui->released && ui->active == id) {
  1574. if (release_hot)
  1575. fired = 1;
  1576. ui->active = 0;
  1577. }
  1578. if (on)
  1579. bg = ui->theme.selection;
  1580. else if (hot || ui->active == id)
  1581. bg = ui->theme.button_hot;
  1582. else
  1583. bg = ui->theme.panel_alt;
  1584. ngui_fill_round_rect(ui, r, NGUI_RADIUS, NGUI_RADIUS,
  1585. NGUI_RADIUS, NGUI_RADIUS, bg);
  1586. fg = ui->theme.text;
  1587. sub_fg = on ? ui->theme.text : ui->theme.text_dim;
  1588. ngui_push_clip(ui, r);
  1589. ngui_text_px(ui, r.x + 14, r.y + 8, title, slen(title), fg,
  1590. 14, 1, 0, 0);
  1591. if (subtitle && subtitle[0])
  1592. ngui_text_utf8(ui, r.x + 14, r.y + 27, subtitle, sub_fg);
  1593. ngui_pop_clip(ui);
  1594. if (hot)
  1595. ui->hot = id;
  1596. return fired;
  1597. }
  1598. int ngui_slider(struct ngui *ui, unsigned int id, struct ngui_slider *s,
  1599. struct ngui_rect r, int *value, int min, int max)
  1600. {
  1601. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1602. int press_hot = ui->pressed &&
  1603. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1604. int changed = 0;
  1605. int track_y = r.y + r.h / 2 - 2;
  1606. int knob_x, fill_w;
  1607. if (!s || !value || max <= min)
  1608. return 0;
  1609. if (*value < min)
  1610. *value = min;
  1611. if (*value > max)
  1612. *value = max;
  1613. if (press_hot) {
  1614. s->dragging = 1;
  1615. ui->active = id;
  1616. }
  1617. if (s->dragging && (ui->buttons & 1)) {
  1618. int rel = ui->mouse_x - r.x;
  1619. int v;
  1620. if (rel < 0)
  1621. rel = 0;
  1622. if (rel >= r.w)
  1623. rel = r.w - 1;
  1624. v = min + rel * (max - min) / (r.w > 1 ? r.w - 1 : 1);
  1625. if (v != *value) {
  1626. *value = v;
  1627. changed = 1;
  1628. }
  1629. }
  1630. if (ui->released) {
  1631. s->dragging = 0;
  1632. if (ui->active == id)
  1633. ui->active = 0;
  1634. }
  1635. if (hot)
  1636. ui->hot = id;
  1637. ngui_fill_rect(ui, rect_make(r.x, track_y, r.w, 4),
  1638. ui->theme.button);
  1639. fill_w = (*value - min) * r.w / (max - min);
  1640. if (fill_w > 0)
  1641. ngui_fill_rect(ui, rect_make(r.x, track_y, fill_w, 4),
  1642. ui->theme.accent);
  1643. knob_x = r.x + (*value - min) * (r.w - 1) / (max - min);
  1644. ngui_fill_round_rect(ui,
  1645. rect_make(knob_x - 7, r.y + r.h / 2 - 7, 14, 14),
  1646. 7, 7, 7, 7, ui->theme.text);
  1647. return changed;
  1648. }
  1649. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1650. enum ngui_icon icon){
  1651. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  1652. int press_hot = ui->pressed &&
  1653. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  1654. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  1655. int fired = 0;
  1656. unsigned short bg;
  1657. if (press_hot)
  1658. ui->active = id;
  1659. if (ui->pressed && ui->released && press_hot && release_hot)
  1660. fired = 1;
  1661. if (ui->released && ui->active == id) {
  1662. if (release_hot)
  1663. fired = 1;
  1664. ui->active = 0;
  1665. }
  1666. if (ui->active == id)
  1667. bg = ui->theme.button_down;
  1668. else if (hot)
  1669. bg = ui->theme.button_hot;
  1670. else
  1671. bg = ui->theme.button;
  1672. ngui_fill_rect(ui, r, bg);
  1673. button_bevel(ui, r, bg, hot, ui->active == id);
  1674. ngui_icon(ui, r, icon, ui->theme.text);
  1675. if (hot)
  1676. ui->hot = id;
  1677. return fired;
  1678. }
  1679. void ngui_input_set(struct ngui_input *in, const char *s)
  1680. {
  1681. scopy(in->text, s, NGUI_INPUT_MAX);
  1682. in->len = slen(in->text);
  1683. in->cursor = in->len;
  1684. in->scroll = 0;
  1685. in->anchor = in->cursor;
  1686. in->dragging = 0;
  1687. in->last_click_ms = 0;
  1688. }
  1689. void ngui_input_value(const struct ngui_input *in, char *out,
  1690. unsigned int max)
  1691. {
  1692. unsigned int i;
  1693. if (!in || !out || !max)
  1694. return;
  1695. /* ngui_text_input() unmasks a secret field again after drawing, so
  1696. * in->text is the real value between calls. */
  1697. for (i = 0; in->text[i] && i + 1 < max; i++)
  1698. out[i] = in->text[i];
  1699. out[i] = '\0';
  1700. }
  1701. static int input_word_char(char c)
  1702. {
  1703. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  1704. (c >= '0' && c <= '9') || c == '_';
  1705. }
  1706. /* Next word start after `pos` (Windows edit-control semantics). */
  1707. static unsigned int input_word_next(const struct ngui_input *in,
  1708. unsigned int pos)
  1709. {
  1710. while (pos < in->len && input_word_char(in->text[pos]))
  1711. pos++;
  1712. while (pos < in->len && !input_word_char(in->text[pos]))
  1713. pos++;
  1714. return pos;
  1715. }
  1716. /* Start of the previous word before `pos`. */
  1717. static unsigned int input_word_prev(const struct ngui_input *in,
  1718. unsigned int pos)
  1719. {
  1720. while (pos > 0 && !input_word_char(in->text[pos - 1]))
  1721. pos--;
  1722. while (pos > 0 && input_word_char(in->text[pos - 1]))
  1723. pos--;
  1724. return pos;
  1725. }
  1726. /* End of the word to the right of `pos`. A caret inside a word takes the
  1727. * rest of that word and its trailing separators; a caret on separators takes
  1728. * them plus the next word. */
  1729. static unsigned int input_word_delete_end(const struct ngui_input *in,
  1730. unsigned int pos)
  1731. {
  1732. if (pos < in->len && input_word_char(in->text[pos])) {
  1733. while (pos < in->len && input_word_char(in->text[pos]))
  1734. pos++;
  1735. while (pos < in->len && !input_word_char(in->text[pos]))
  1736. pos++;
  1737. return pos;
  1738. }
  1739. while (pos < in->len && !input_word_char(in->text[pos]))
  1740. pos++;
  1741. while (pos < in->len && input_word_char(in->text[pos]))
  1742. pos++;
  1743. return pos;
  1744. }
  1745. static void input_sel_bounds(const struct ngui_input *in,
  1746. unsigned int *a, unsigned int *b)
  1747. {
  1748. if (in->anchor < in->cursor) {
  1749. *a = in->anchor;
  1750. *b = in->cursor;
  1751. } else {
  1752. *a = in->cursor;
  1753. *b = in->anchor;
  1754. }
  1755. }
  1756. /* Remove [a, b) and keep cursor/anchor consistent. Returns 1 when the text
  1757. * changed. */
  1758. static int input_delete_range(struct ngui_input *in, unsigned int a,
  1759. unsigned int b)
  1760. {
  1761. unsigned int i;
  1762. if (a >= b || b > in->len)
  1763. return 0;
  1764. for (i = b; i < in->len; i++)
  1765. in->text[i - (b - a)] = in->text[i];
  1766. in->len -= b - a;
  1767. in->text[in->len] = '\0';
  1768. if (in->cursor >= b)
  1769. in->cursor -= b - a;
  1770. else if (in->cursor > a)
  1771. in->cursor = a;
  1772. if (in->anchor >= b)
  1773. in->anchor -= b - a;
  1774. else if (in->anchor > a)
  1775. in->anchor = a;
  1776. return 1;
  1777. }
  1778. static int input_delete_selection(struct ngui_input *in)
  1779. {
  1780. unsigned int a, b;
  1781. input_sel_bounds(in, &a, &b);
  1782. return input_delete_range(in, a, b);
  1783. }
  1784. static void input_insert(struct ngui_input *in, char c)
  1785. {
  1786. unsigned int i;
  1787. input_delete_selection(in);
  1788. if (in->len + 1 >= NGUI_INPUT_MAX)
  1789. return;
  1790. i = in->len;
  1791. while (i > in->cursor) {
  1792. in->text[i] = in->text[i - 1];
  1793. i--;
  1794. }
  1795. in->text[in->cursor] = c;
  1796. in->cursor++;
  1797. in->len++;
  1798. in->text[in->len] = '\0';
  1799. in->anchor = in->cursor;
  1800. }
  1801. static void input_delete(struct ngui_input *in, int back)
  1802. {
  1803. unsigned int pos;
  1804. if (back) {
  1805. if (in->cursor == 0)
  1806. return;
  1807. pos = in->cursor - 1;
  1808. } else {
  1809. if (in->cursor >= in->len)
  1810. return;
  1811. pos = in->cursor;
  1812. }
  1813. input_delete_range(in, pos, pos + 1);
  1814. }
  1815. static void input_move_to(struct ngui_input *in, unsigned int pos)
  1816. {
  1817. if (pos > in->len)
  1818. pos = in->len;
  1819. in->cursor = pos;
  1820. in->anchor = pos;
  1821. }
  1822. /* Two presses in the same field within this window select all of its text. */
  1823. #define NGUI_DOUBLE_CLICK_MS 500
  1824. static int input_text_w(struct ngui *ui, const char *s, unsigned int n)
  1825. {
  1826. return (int)ngui_text_width_px(ui, s, n, ui->font_px, 0, 0, 0);
  1827. }
  1828. /* Character position under a content-relative x, clamped to the visible
  1829. * part of the field. Proportional text means the prefix must be measured. */
  1830. static unsigned int input_pos_at(struct ngui *ui, const struct ngui_input *in,
  1831. int inner_x, int x)
  1832. {
  1833. unsigned int i, n, best = 0;
  1834. int rel = x - inner_x;
  1835. if (rel <= 0 || in->scroll >= in->len)
  1836. return in->scroll;
  1837. n = in->len - in->scroll;
  1838. for (i = 1; i <= n; i++) {
  1839. if (input_text_w(ui, in->text + in->scroll, i) > rel)
  1840. break;
  1841. best = i;
  1842. }
  1843. return in->scroll + best;
  1844. }
  1845. static void input_draw_selection(struct ngui *ui, const struct ngui_input *in,
  1846. struct ngui_rect inner)
  1847. {
  1848. unsigned int a, b, va, vb;
  1849. int xa, xb;
  1850. input_sel_bounds(in, &a, &b);
  1851. if (a == b || in->scroll >= in->len)
  1852. return;
  1853. va = a < in->scroll ? in->scroll : a;
  1854. vb = b < in->len ? b : in->len;
  1855. if (va >= vb)
  1856. return;
  1857. xa = input_text_w(ui, in->text + in->scroll, va - in->scroll);
  1858. xb = input_text_w(ui, in->text + in->scroll, vb - in->scroll);
  1859. if (xb > xa)
  1860. ngui_fill_rect(ui, rect_make(inner.x + xa, inner.y,
  1861. xb - xa,
  1862. ngui_text_line_height(ui,
  1863. ui->font_px,
  1864. 0)),
  1865. ui->theme.selection);
  1866. }
  1867. /* Scroll the first-visible-character index until the caret is visible. */
  1868. static void input_scroll_to_cursor(struct ngui *ui, struct ngui_input *in,
  1869. int inner_w)
  1870. {
  1871. if (in->scroll > in->len)
  1872. in->scroll = in->len;
  1873. if (in->cursor < in->scroll)
  1874. in->scroll = in->cursor;
  1875. if (inner_w <= 0) {
  1876. in->scroll = in->len;
  1877. return;
  1878. }
  1879. while (in->scroll < in->cursor &&
  1880. input_text_w(ui, in->text + in->scroll,
  1881. in->cursor - in->scroll) > inner_w)
  1882. in->scroll++;
  1883. if (in->scroll > in->len)
  1884. in->scroll = in->len;
  1885. }
  1886. /* Secret fields are measured and painted from a '*' mask: the real text is
  1887. * swapped out for everything except the edit loop, so the caret, selection
  1888. * and click positions all follow the masked width. */
  1889. static char secret_save[NGUI_INPUT_MAX];
  1890. static void secret_mask(struct ngui_input *in)
  1891. {
  1892. unsigned int i;
  1893. for (i = 0; i <= in->len && i < NGUI_INPUT_MAX; i++) {
  1894. secret_save[i] = in->text[i];
  1895. in->text[i] = i < in->len ? '*' : '\0';
  1896. }
  1897. }
  1898. static void secret_unmask(struct ngui_input *in)
  1899. {
  1900. unsigned int i;
  1901. for (i = 0; i <= in->len && i < NGUI_INPUT_MAX; i++) {
  1902. in->text[i] = secret_save[i];
  1903. secret_save[i] = 0;
  1904. }
  1905. }
  1906. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  1907. struct ngui_input *in)
  1908. {
  1909. struct ngui_rect inner;
  1910. int focused = !in->disabled && (ui->focus == id);
  1911. int result = 0;
  1912. int avail, line_h, k;
  1913. int changed = 0;
  1914. line_h = ngui_text_line_height(ui, ui->font_px, 0);
  1915. if (line_h < 1)
  1916. line_h = 16;
  1917. inner = rect_make(r.x + 4, r.y + (r.h - line_h) / 2, r.w - 8,
  1918. line_h);
  1919. if (inner.w < 0)
  1920. inner.w = 0;
  1921. if (inner.h < 1)
  1922. inner.h = 1;
  1923. avail = inner.w;
  1924. if (in->secret)
  1925. secret_mask(in);
  1926. ngui_cursor_rect(ui, r, in->disabled ? CURSOR_NOT_ALLOWED : CURSOR_TEXT);
  1927. if (in->disabled) {
  1928. /* A field that became disabled while focused gives the focus
  1929. * back, and a press cannot start a drag or a selection. */
  1930. if (ui->focus == id)
  1931. ngui_set_focus(ui, 0);
  1932. in->dragging = 0;
  1933. } else if (ui->pressed &&
  1934. ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
  1935. unsigned int now = u_ticks_ms();
  1936. int dbl = in->last_click_ms &&
  1937. (unsigned int)(now - in->last_click_ms) <=
  1938. NGUI_DOUBLE_CLICK_MS;
  1939. if (dbl) {
  1940. /* Double click: select the whole text and keep it
  1941. * selected while the second press is held. */
  1942. in->anchor = 0;
  1943. in->cursor = in->len;
  1944. in->dragging = 0;
  1945. } else {
  1946. unsigned int pos = input_pos_at(ui, in, inner.x,
  1947. ui->press_x);
  1948. in->cursor = pos;
  1949. in->anchor = pos;
  1950. in->dragging = 1;
  1951. }
  1952. in->last_click_ms = now;
  1953. ui->focus = id;
  1954. ui->focus_claimed = 1;
  1955. ui->last_blink = now;
  1956. ui->caret_on = 1;
  1957. focused = 1;
  1958. } else if (ui->pressed) {
  1959. in->dragging = 0;
  1960. }
  1961. /* Extend the selection while the button that started in this field is
  1962. * still held. Sampled coordinates may be outside the canvas. The
  1963. * release *event* is authoritative: the sampled button state can lag
  1964. * the event queue by a frame. */
  1965. if (!in->disabled && in->dragging && (ui->buttons & 1) &&
  1966. !ui->released) {
  1967. in->cursor = input_pos_at(ui, in, inner.x, ui->mouse_x);
  1968. /* Keep redrawing when the pointer leaves the window. */
  1969. ngui_invalidate(ui);
  1970. }
  1971. if (ui->released)
  1972. in->dragging = 0;
  1973. if (focused) {
  1974. if (in->secret)
  1975. secret_unmask(in);
  1976. while ((k = ngui_key(ui)) != 0) {
  1977. if (in->secret) {
  1978. /* No word boundaries inside a password. */
  1979. if (k == NGUI_KEY_CTRL_LEFT)
  1980. k = NGUI_KEY_HOME;
  1981. else if (k == NGUI_KEY_CTRL_RIGHT)
  1982. k = NGUI_KEY_END;
  1983. }
  1984. if (k == '\n' || k == '\r') {
  1985. result |= NGUI_INPUT_SUBMIT;
  1986. break;
  1987. }
  1988. if (k == NGUI_KEY_ESC || k == NGUI_KEY_TAB) {
  1989. result |= NGUI_INPUT_CANCEL;
  1990. if (k == NGUI_KEY_TAB)
  1991. result |= NGUI_INPUT_NEXT;
  1992. ui->focus = 0;
  1993. break;
  1994. }
  1995. if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
  1996. if (!input_delete_selection(in))
  1997. input_delete(in, 1);
  1998. changed = 1;
  1999. } else if (k == NGUI_KEY_DELETE) {
  2000. if (!input_delete_selection(in))
  2001. input_delete(in, 0);
  2002. changed = 1;
  2003. } else if (k == NGUI_KEY_CTRL_BACKSPACE) {
  2004. if (input_delete_selection(in))
  2005. changed = 1;
  2006. else
  2007. changed = input_delete_range(in,
  2008. input_word_prev(in, in->cursor),
  2009. in->cursor);
  2010. } else if (k == NGUI_KEY_CTRL_DELETE) {
  2011. if (input_delete_selection(in)) {
  2012. changed = 1;
  2013. } else {
  2014. unsigned int a = in->cursor;
  2015. unsigned int b = input_word_delete_end(in,
  2016. a);
  2017. /* At the end of the line, delete the word
  2018. * before the caret instead of doing
  2019. * nothing. */
  2020. if (b == a) {
  2021. a = input_word_prev(in, in->cursor);
  2022. b = in->cursor;
  2023. }
  2024. changed = input_delete_range(in, a, b);
  2025. }
  2026. } else if (k == NGUI_KEY_LEFT) {
  2027. input_move_to(in, in->cursor > 0 ? in->cursor - 1 : 0);
  2028. } else if (k == NGUI_KEY_RIGHT) {
  2029. input_move_to(in, in->cursor < in->len
  2030. ? in->cursor + 1 : in->len);
  2031. } else if (k == NGUI_KEY_CTRL_LEFT) {
  2032. input_move_to(in, input_word_prev(in, in->cursor));
  2033. } else if (k == NGUI_KEY_CTRL_RIGHT) {
  2034. input_move_to(in, input_word_next(in, in->cursor));
  2035. } else if (k == NGUI_KEY_HOME) {
  2036. input_move_to(in, 0);
  2037. } else if (k == NGUI_KEY_END) {
  2038. input_move_to(in, in->len);
  2039. } else if (k == 0x01) { /* Ctrl+A: select all */
  2040. in->anchor = 0;
  2041. in->cursor = in->len;
  2042. } else if (k == 0x15) { /* Ctrl+U */
  2043. in->len = 0;
  2044. in->cursor = 0;
  2045. in->anchor = 0;
  2046. in->text[0] = '\0';
  2047. changed = 1;
  2048. } else if (k >= 0x20 && k <= 0x7E) {
  2049. input_insert(in, (char)k);
  2050. changed = 1;
  2051. }
  2052. }
  2053. if (in->secret)
  2054. secret_mask(in);
  2055. }
  2056. ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
  2057. /* Inset edge: the shadow sits on the top/left, the light on the
  2058. * bottom/right. A focused field keeps the shape in accent blue. */
  2059. if (focused)
  2060. bevel_rect(ui, r, ui->theme.accent,
  2061. shade565(ui->theme.accent, 4));
  2062. else
  2063. bevel_rect(ui, r, shade565(ui->theme.panel, -4),
  2064. shade565(ui->theme.panel, 4));
  2065. if (!in->disabled)
  2066. input_scroll_to_cursor(ui, in, avail);
  2067. ngui_push_clip(ui, inner);
  2068. if (in->len == 0 && in->placeholder) {
  2069. ngui_text_px(ui, inner.x, inner.y, in->placeholder,
  2070. slen(in->placeholder), ui->theme.text_dim,
  2071. ui->font_px, 0, 0, 0);
  2072. } else {
  2073. if (focused)
  2074. input_draw_selection(ui, in, inner);
  2075. ngui_text_px(ui, inner.x, inner.y, in->text + in->scroll,
  2076. slen(in->text + in->scroll),
  2077. in->disabled ? ui->theme.text_dim :
  2078. ui->theme.text,
  2079. ui->font_px, 0, 0, 0);
  2080. }
  2081. if (focused && ui->caret_on) {
  2082. int cx = 0;
  2083. if (in->cursor > in->scroll)
  2084. cx = input_text_w(ui, in->text + in->scroll,
  2085. in->cursor - in->scroll);
  2086. if (cx < 0)
  2087. cx = 0;
  2088. if (avail > 0 && cx >= avail)
  2089. cx = avail - 1;
  2090. if (avail > 0 && cx < avail)
  2091. ngui_fill_rect(ui, rect_make(inner.x + cx, inner.y,
  2092. 1, inner.h),
  2093. ui->theme.accent);
  2094. }
  2095. ngui_pop_clip(ui);
  2096. if (in->secret)
  2097. secret_unmask(in);
  2098. if (changed)
  2099. result |= NGUI_INPUT_CHANGED;
  2100. return result;
  2101. }
  2102. static int scrollable(const struct ngui_scroll *s)
  2103. {
  2104. return s->content_h > s->view_h && s->view_h > 0;
  2105. }
  2106. static int scroll_thumb_h(const struct ngui_rect *r,
  2107. const struct ngui_scroll *s)
  2108. {
  2109. int h = 0;
  2110. if (s->content_h > 0)
  2111. h = r->h * s->view_h / s->content_h;
  2112. if (h < 12)
  2113. h = 12;
  2114. if (h > r->h)
  2115. h = r->h;
  2116. return h;
  2117. }
  2118. static int scroll_thumb_y(const struct ngui_rect *r,
  2119. const struct ngui_scroll *s)
  2120. {
  2121. int th = scroll_thumb_h(r, s);
  2122. int avail = r->h - th;
  2123. int max = s->content_h - s->view_h;
  2124. if (avail <= 0 || max <= 0)
  2125. return r->y;
  2126. return r->y + s->offset * avail / max;
  2127. }
  2128. static void scroll_clamp(struct ngui_scroll *s)
  2129. {
  2130. int max = s->content_h - s->view_h;
  2131. if (max < 0)
  2132. max = 0;
  2133. if (s->offset > max)
  2134. s->offset = max;
  2135. if (s->offset < 0)
  2136. s->offset = 0;
  2137. }
  2138. void ngui_scroll_to(struct ngui_scroll *s, int offset)
  2139. {
  2140. s->offset = offset;
  2141. scroll_clamp(s);
  2142. }
  2143. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
  2144. {
  2145. if (y < s->offset)
  2146. s->offset = y;
  2147. else if (y + h > s->offset + s->view_h)
  2148. s->offset = y + h - s->view_h;
  2149. scroll_clamp(s);
  2150. }
  2151. #define SCROLLBAR_W 8
  2152. #define SCROLLBAR_INSET 2
  2153. #define SCROLLBAR_HIT_W 14
  2154. #define SCROLLBAR_STEPS 8
  2155. #define SCROLLBAR_STEP_MS 14
  2156. static struct ngui_rect scroll_track_rect(const struct ngui_rect *r)
  2157. {
  2158. struct ngui_rect t;
  2159. int inset = SCROLLBAR_INSET;
  2160. if (r->w < SCROLLBAR_W + 2 * inset || r->h <= 2 * inset)
  2161. inset = 0;
  2162. t.x = r->x + r->w - SCROLLBAR_W - inset;
  2163. t.y = r->y + inset;
  2164. t.w = SCROLLBAR_W;
  2165. t.h = r->h - 2 * inset;
  2166. return t;
  2167. }
  2168. static struct ngui_rect scroll_hit_rect(const struct ngui_rect *r)
  2169. {
  2170. struct ngui_rect h = *r;
  2171. int w = SCROLLBAR_HIT_W;
  2172. if (w > r->w)
  2173. w = r->w;
  2174. h.x = r->x + r->w - w;
  2175. h.w = w;
  2176. return h;
  2177. }
  2178. static int pointer_in_rect(struct ngui *ui, const struct ngui_rect *r)
  2179. {
  2180. if (!ui->inside || !ngui_rect_contains(r, ui->mouse_x, ui->mouse_y))
  2181. return 0;
  2182. if (ui->nclip > 0 &&
  2183. !ngui_rect_contains(&ui->clip[ui->nclip - 1], ui->mouse_x,
  2184. ui->mouse_y))
  2185. return 0;
  2186. return 1;
  2187. }
  2188. /* Width of the pill for one animation step. The bar grows into the pane
  2189. * from its right edge, so the animation needs no alpha compositing. */
  2190. static int scroll_bar_w(int vis)
  2191. {
  2192. if (vis <= 0)
  2193. return 0;
  2194. if (vis >= SCROLLBAR_STEPS)
  2195. return SCROLLBAR_W;
  2196. return 1 + (vis - 1) * (SCROLLBAR_W - 1) / (SCROLLBAR_STEPS - 1);
  2197. }
  2198. /* Advance the overlay bar animation. The bar stays hidden while the pointer
  2199. * is outside the pane and slides in on hover; a drag pins it open. Setting
  2200. * ui->dirty while steps remain makes the app draw the next frame. */
  2201. static void scroll_bar_update(struct ngui *ui, const struct ngui_rect *r,
  2202. struct ngui_scroll *s)
  2203. {
  2204. int hover, target, steps;
  2205. unsigned int now, due;
  2206. if (s->hide_bar || !scrollable(s)) {
  2207. s->dragging = 0;
  2208. s->bar_vis = 0;
  2209. s->bar_target = 0;
  2210. s->bar_hot = 0;
  2211. return;
  2212. }
  2213. {
  2214. struct ngui_rect hit = scroll_hit_rect(r);
  2215. hover = pointer_in_rect(ui, r);
  2216. s->bar_hot = hover &&
  2217. ngui_rect_contains(&hit, ui->mouse_x, ui->mouse_y);
  2218. }
  2219. target = (hover || s->dragging) ? SCROLLBAR_STEPS : 0;
  2220. now = u_ticks_ms();
  2221. if (target != s->bar_target) {
  2222. s->bar_target = target;
  2223. s->bar_ms = now;
  2224. if (s->bar_vis != target)
  2225. ui->dirty = 1;
  2226. return;
  2227. }
  2228. if (s->bar_vis == target)
  2229. return;
  2230. due = now - s->bar_ms;
  2231. steps = (int)(due / SCROLLBAR_STEP_MS);
  2232. if (steps > 0) {
  2233. if (target > s->bar_vis) {
  2234. s->bar_vis += steps;
  2235. if (s->bar_vis > target)
  2236. s->bar_vis = target;
  2237. } else {
  2238. s->bar_vis -= steps;
  2239. if (s->bar_vis < target)
  2240. s->bar_vis = target;
  2241. }
  2242. s->bar_ms += (unsigned int)steps * SCROLLBAR_STEP_MS;
  2243. }
  2244. if (s->bar_vis != target)
  2245. ui->dirty = 1;
  2246. }
  2247. static void scroll_bar_press(struct ngui_scroll *s,
  2248. const struct ngui_rect *track, int th, int y)
  2249. {
  2250. int ty = scroll_thumb_y(track, s);
  2251. if (y >= ty && y < ty + th) {
  2252. s->dragging = 1;
  2253. s->drag_dy = y - ty;
  2254. } else {
  2255. int rel = y - track->y - th / 2;
  2256. int avail = track->h - th;
  2257. int max = s->content_h - s->view_h;
  2258. if (rel < 0)
  2259. rel = 0;
  2260. if (rel > avail)
  2261. rel = avail;
  2262. if (avail > 0)
  2263. s->offset = rel * max / avail;
  2264. s->dragging = 1;
  2265. s->drag_dy = th / 2;
  2266. scroll_clamp(s);
  2267. }
  2268. }
  2269. static void scroll_bar_drag(struct ngui_scroll *s,
  2270. const struct ngui_rect *track, int th, int y)
  2271. {
  2272. int rel = y - track->y - s->drag_dy;
  2273. int avail = track->h - th;
  2274. int max = s->content_h - s->view_h;
  2275. if (rel < 0)
  2276. rel = 0;
  2277. if (rel > avail)
  2278. rel = avail;
  2279. if (avail > 0)
  2280. s->offset = rel * max / avail;
  2281. scroll_clamp(s);
  2282. }
  2283. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  2284. struct ngui_scroll *s)
  2285. {
  2286. struct ngui_rect track, hit;
  2287. int th;
  2288. s->view_h = r.h;
  2289. scroll_clamp(s);
  2290. if (!ui->buttons)
  2291. s->dragging = 0;
  2292. scroll_bar_update(ui, &r, s);
  2293. if (!s->hide_bar && scrollable(s)) {
  2294. track = scroll_track_rect(&r);
  2295. hit = scroll_hit_rect(&r);
  2296. th = scroll_thumb_h(&track, s);
  2297. if (ui->pressed &&
  2298. ngui_rect_contains(&hit, ui->press_x, ui->press_y)) {
  2299. scroll_bar_press(s, &track, th, ui->press_y);
  2300. /* The bar is on top of the content: a drag must not
  2301. * also activate a row or button below it. */
  2302. ui->pressed = 0;
  2303. ui->clicked = 0;
  2304. }
  2305. /* Sampled buttons are authoritative while held: some event
  2306. * paths deliver the press late or coalesce it with the
  2307. * release. Start the drag from the current pointer position
  2308. * as well, but only once the bar is fully visible. */
  2309. if (!s->dragging && (ui->buttons & 1) &&
  2310. s->bar_hot && s->bar_vis == SCROLLBAR_STEPS)
  2311. scroll_bar_press(s, &track, th, ui->mouse_y);
  2312. if (s->dragging && (ui->buttons & 1))
  2313. scroll_bar_drag(s, &track, th, ui->mouse_y);
  2314. if (ui->released)
  2315. s->dragging = 0;
  2316. }
  2317. ngui_push_clip(ui, r);
  2318. }
  2319. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  2320. struct ngui_scroll *s)
  2321. {
  2322. struct ngui_rect track, thumb;
  2323. int bw, th, ty;
  2324. unsigned short color;
  2325. ngui_pop_clip(ui);
  2326. /* content_h is often known only after drawing, so advance the state
  2327. * here as well; a new target requests a follow-up frame. */
  2328. scroll_bar_update(ui, &r, s);
  2329. if (s->hide_bar || !scrollable(s) || s->bar_vis <= 0)
  2330. return;
  2331. track = scroll_track_rect(&r);
  2332. bw = scroll_bar_w(s->bar_vis);
  2333. if (bw <= 0)
  2334. return;
  2335. th = scroll_thumb_h(&track, s);
  2336. ty = scroll_thumb_y(&track, s);
  2337. thumb = rect_make(r.x + r.w - SCROLLBAR_INSET - bw, ty, bw, th);
  2338. color = s->dragging ? ui->theme.accent :
  2339. s->bar_hot ? ui->theme.text_dim : ui->theme.border;
  2340. ngui_fill_round_rect(ui, thumb, bw / 2, bw / 2, bw / 2, bw / 2,
  2341. color);
  2342. if (s->bar_hot)
  2343. ngui_set_cursor(ui, CURSOR_DEFAULT);
  2344. }
  2345. int ngui_scroll_wheel(struct ngui *ui, struct ngui_rect r,
  2346. struct ngui_scroll *s)
  2347. {
  2348. if (!ui->scroll_y ||
  2349. !ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y))
  2350. return 0;
  2351. ngui_scroll_to(s, s->offset + ui->scroll_y);
  2352. ui->scroll_y = 0;
  2353. return 1;
  2354. }
  2355. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s)
  2356. {
  2357. int k = ngui_key_peek(ui);
  2358. int page;
  2359. if (k != NGUI_KEY_PAGE_UP && k != NGUI_KEY_PAGE_DOWN)
  2360. return 0;
  2361. page = s->view_h - 22;
  2362. if (page < 22)
  2363. page = 22;
  2364. ngui_key(ui);
  2365. if (k == NGUI_KEY_PAGE_UP)
  2366. s->offset -= page;
  2367. else
  2368. s->offset += page;
  2369. scroll_clamp(s);
  2370. return 1;
  2371. }
  2372. /* ---- multi-line text area ---------------------------------------------- */
  2373. #define TA_PAD_X 6
  2374. #define TA_PAD_Y 4
  2375. #define TA_TAB_COLS 4
  2376. #define TA_SCROLL_RESERVE 10
  2377. struct ta_metrics {
  2378. int rows;
  2379. int caret_row;
  2380. int caret_pen; /* 26.6 pixels from the visual-line start */
  2381. unsigned int seg_start;
  2382. unsigned int seg_end;
  2383. };
  2384. static int ta_word_char(unsigned char c)
  2385. {
  2386. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  2387. (c >= '0' && c <= '9') || c == '_' || c >= 0x80;
  2388. }
  2389. /* Advance of one character in 26.6 pixels, matching ngui_text_px()'s
  2390. * mono face (or its 8 px bitmap fallback). Tabs are handled separately. */
  2391. static int ta_char_advance(struct ngui *ui, unsigned char c)
  2392. {
  2393. struct ufont_face *f = ngui_face(ui, 0, 0, 1);
  2394. int a;
  2395. if (!f)
  2396. return 8 << 6;
  2397. a = ufont_advance_cp(f, ui ? ui->font_px : 14, c);
  2398. return a > 0 ? a : (8 << 6);
  2399. }
  2400. static int ta_tab_advance(struct ngui *ui, int pen)
  2401. {
  2402. int tab = ta_char_advance(ui, ' ') * TA_TAB_COLS;
  2403. if (tab < 1)
  2404. tab = 8 << 6;
  2405. return tab - pen % tab;
  2406. }
  2407. static int ta_pen_at(struct ngui *ui, const struct ngui_text_area *ta,
  2408. unsigned int start, unsigned int pos)
  2409. {
  2410. int pen = 0;
  2411. unsigned int p;
  2412. if (pos > ta->len)
  2413. pos = ta->len;
  2414. for (p = start; p < pos; p++) {
  2415. if (ta->text[p] == '\t')
  2416. pen += ta_tab_advance(ui, pen);
  2417. else
  2418. pen += ta_char_advance(ui, (unsigned char)ta->text[p]);
  2419. }
  2420. return pen;
  2421. }
  2422. static unsigned int ta_line_end(const struct ngui_text_area *ta,
  2423. unsigned int pos)
  2424. {
  2425. while (pos < ta->len && ta->text[pos] != '\n')
  2426. pos++;
  2427. return pos;
  2428. }
  2429. /* End of the visual segment starting at s, wrapping at `avail` (26.6
  2430. * pixels). Breaks after the last space/tab that fits; otherwise breaks
  2431. * before the character that would overflow. Always consumes >= 1 char when
  2432. * s < e, so callers cannot loop. */
  2433. static unsigned int ta_seg_end(struct ngui *ui,
  2434. const struct ngui_text_area *ta,
  2435. unsigned int s, unsigned int e, int avail,
  2436. int *used)
  2437. {
  2438. unsigned int p = s, last_break = s;
  2439. int pen = 0, last_pen = 0;
  2440. if (avail < 1)
  2441. avail = 1;
  2442. while (p < e) {
  2443. unsigned char c = (unsigned char)ta->text[p];
  2444. int w;
  2445. if (c == '\t')
  2446. w = ta_tab_advance(ui, pen);
  2447. else
  2448. w = ta_char_advance(ui, c);
  2449. if (pen + w > avail && p > s) {
  2450. if (last_break > s) {
  2451. *used = last_pen;
  2452. return last_break;
  2453. }
  2454. *used = pen;
  2455. return p;
  2456. }
  2457. pen += w;
  2458. p++;
  2459. if (c == ' ' || c == '\t') {
  2460. last_break = p;
  2461. last_pen = pen;
  2462. }
  2463. if (pen >= avail && p < e) {
  2464. *used = pen;
  2465. return p;
  2466. }
  2467. }
  2468. *used = pen;
  2469. return e;
  2470. }
  2471. static void ta_sel_bounds(const struct ngui_text_area *ta,
  2472. unsigned int *a, unsigned int *b)
  2473. {
  2474. if (ta->anchor < ta->cursor) {
  2475. *a = ta->anchor;
  2476. *b = ta->cursor;
  2477. } else {
  2478. *a = ta->cursor;
  2479. *b = ta->anchor;
  2480. }
  2481. }
  2482. static int ta_delete_range(struct ngui_text_area *ta, unsigned int a,
  2483. unsigned int b)
  2484. {
  2485. unsigned int i;
  2486. if (a >= b || b > ta->len)
  2487. return 0;
  2488. for (i = b; i < ta->len; i++)
  2489. ta->text[i - (b - a)] = ta->text[i];
  2490. ta->len -= b - a;
  2491. ta->text[ta->len] = '\0';
  2492. if (ta->cursor >= b)
  2493. ta->cursor -= b - a;
  2494. else if (ta->cursor > a)
  2495. ta->cursor = a;
  2496. if (ta->anchor >= b)
  2497. ta->anchor -= b - a;
  2498. else if (ta->anchor > a)
  2499. ta->anchor = a;
  2500. return 1;
  2501. }
  2502. static int ta_delete_selection(struct ngui_text_area *ta)
  2503. {
  2504. unsigned int a, b;
  2505. ta_sel_bounds(ta, &a, &b);
  2506. return ta_delete_range(ta, a, b);
  2507. }
  2508. static int ta_insert_char(struct ngui_text_area *ta, char c)
  2509. {
  2510. unsigned int i;
  2511. ta_delete_selection(ta);
  2512. if (ta->len + 1 >= ta->cap)
  2513. return 0;
  2514. i = ta->len + 1;
  2515. while (i > ta->cursor) {
  2516. ta->text[i] = ta->text[i - 1];
  2517. i--;
  2518. }
  2519. ta->text[ta->cursor] = c;
  2520. ta->cursor++;
  2521. ta->len++;
  2522. ta->anchor = ta->cursor;
  2523. return 1;
  2524. }
  2525. static unsigned int ta_word_next(const struct ngui_text_area *ta,
  2526. unsigned int pos)
  2527. {
  2528. while (pos < ta->len && ta_word_char((unsigned char)ta->text[pos]))
  2529. pos++;
  2530. while (pos < ta->len && !ta_word_char((unsigned char)ta->text[pos]))
  2531. pos++;
  2532. return pos;
  2533. }
  2534. static unsigned int ta_word_prev(const struct ngui_text_area *ta,
  2535. unsigned int pos)
  2536. {
  2537. while (pos > 0 && !ta_word_char((unsigned char)ta->text[pos - 1]))
  2538. pos--;
  2539. while (pos > 0 && ta_word_char((unsigned char)ta->text[pos - 1]))
  2540. pos--;
  2541. return pos;
  2542. }
  2543. static unsigned int ta_word_delete_end(const struct ngui_text_area *ta,
  2544. unsigned int pos)
  2545. {
  2546. if (pos < ta->len && ta_word_char((unsigned char)ta->text[pos])) {
  2547. while (pos < ta->len &&
  2548. ta_word_char((unsigned char)ta->text[pos]))
  2549. pos++;
  2550. while (pos < ta->len &&
  2551. !ta_word_char((unsigned char)ta->text[pos]))
  2552. pos++;
  2553. return pos;
  2554. }
  2555. while (pos < ta->len && !ta_word_char((unsigned char)ta->text[pos]))
  2556. pos++;
  2557. while (pos < ta->len && ta_word_char((unsigned char)ta->text[pos]))
  2558. pos++;
  2559. return pos;
  2560. }
  2561. static void ta_measure(struct ngui *ui, const struct ngui_text_area *ta,
  2562. int avail, struct ta_metrics *m,
  2563. unsigned int *line_out, unsigned int *column_out)
  2564. {
  2565. unsigned int pos = 0, line_start = 0, line_no = 1;
  2566. int row = 0, found = 0;
  2567. m->rows = 0;
  2568. m->caret_row = 0;
  2569. m->caret_pen = 0;
  2570. m->seg_start = 0;
  2571. m->seg_end = 0;
  2572. *line_out = 1;
  2573. *column_out = 1;
  2574. for (;;) {
  2575. unsigned int le = ta_line_end(ta, pos);
  2576. unsigned int s = pos;
  2577. for (;;) {
  2578. unsigned int e;
  2579. int used = 0;
  2580. if (s >= le) {
  2581. e = le;
  2582. } else {
  2583. e = ta_seg_end(ui, ta, s, le, avail, &used);
  2584. }
  2585. if (!found && ta->cursor >= s && ta->cursor <= e) {
  2586. m->caret_row = row;
  2587. m->caret_pen = ta_pen_at(ui, ta, s,
  2588. ta->cursor);
  2589. m->seg_start = s;
  2590. m->seg_end = e;
  2591. found = 1;
  2592. }
  2593. row++;
  2594. if (e >= le)
  2595. break;
  2596. s = e;
  2597. }
  2598. if (ta->cursor >= line_start && ta->cursor <= le) {
  2599. *line_out = line_no;
  2600. *column_out = ta->cursor - line_start + 1;
  2601. }
  2602. if (le >= ta->len)
  2603. break;
  2604. pos = le + 1;
  2605. line_start = pos;
  2606. line_no++;
  2607. }
  2608. m->rows = row;
  2609. }
  2610. /* Position at a 26.6 x offset inside [s, e), rounded to the nearest character
  2611. * boundary. */
  2612. static unsigned int ta_pos_at_pen(struct ngui *ui,
  2613. const struct ngui_text_area *ta,
  2614. unsigned int s, unsigned int e, int target)
  2615. {
  2616. unsigned int p = s;
  2617. int pen = 0;
  2618. if (target < 0)
  2619. target = 0;
  2620. while (p < e) {
  2621. int w = ta->text[p] == '\t' ? ta_tab_advance(ui, pen) :
  2622. ta_char_advance(ui, (unsigned char)ta->text[p]);
  2623. if (target < pen + (w + 1) / 2)
  2624. return p;
  2625. pen += w;
  2626. p++;
  2627. }
  2628. return e;
  2629. }
  2630. /* The position on visual row `target_row` closest to `target_pen`. */
  2631. static unsigned int ta_pos_at_row(struct ngui *ui,
  2632. const struct ngui_text_area *ta,
  2633. int avail, int target_row, int target_pen)
  2634. {
  2635. unsigned int pos = 0;
  2636. int row = 0;
  2637. if (target_row < 0)
  2638. target_row = 0;
  2639. for (;;) {
  2640. unsigned int le = ta_line_end(ta, pos);
  2641. unsigned int s = pos;
  2642. for (;;) {
  2643. unsigned int e;
  2644. int used = 0;
  2645. if (s >= le)
  2646. e = le;
  2647. else
  2648. e = ta_seg_end(ui, ta, s, le, avail, &used);
  2649. if (row == target_row)
  2650. return ta_pos_at_pen(ui, ta, s, e, target_pen);
  2651. row++;
  2652. if (e >= le)
  2653. break;
  2654. s = e;
  2655. }
  2656. if (le >= ta->len)
  2657. break;
  2658. pos = le + 1;
  2659. }
  2660. return ta->len;
  2661. }
  2662. static unsigned int ta_pos_at_xy(struct ngui *ui,
  2663. const struct ngui_text_area *ta,
  2664. int avail, const struct ngui_rect *view,
  2665. int x, int y)
  2666. {
  2667. int line_h = ngui_text_line_height(ui, ui->font_px, 1);
  2668. int row, pen;
  2669. if (line_h < 1)
  2670. line_h = 16;
  2671. if (y < view->y)
  2672. y = view->y;
  2673. if (y >= view->y + view->h)
  2674. y = view->y + view->h - 1;
  2675. row = (y - view->y - TA_PAD_Y + ta->scroll.offset) / line_h;
  2676. if (row < 0)
  2677. row = 0;
  2678. pen = ((x - (view->x + TA_PAD_X)) << 6);
  2679. if (pen < 0)
  2680. pen = 0;
  2681. return ta_pos_at_row(ui, ta, avail, row, pen);
  2682. }
  2683. static void ta_move_vertical(struct ngui *ui, struct ngui_text_area *ta,
  2684. int avail, const struct ta_metrics *m,
  2685. const struct ngui_rect *view, int delta)
  2686. {
  2687. int line_h = ngui_text_line_height(ui, ui->font_px, 1);
  2688. int target;
  2689. if (line_h < 1)
  2690. line_h = 16;
  2691. target = m->caret_row + delta;
  2692. if (target < 0)
  2693. target = 0;
  2694. if (target >= m->rows)
  2695. target = m->rows > 0 ? m->rows - 1 : 0;
  2696. ta->cursor = ta_pos_at_row(ui, ta, avail, target, m->caret_pen);
  2697. ta->anchor = ta->cursor;
  2698. ngui_scroll_ensure_visible(&ta->scroll,
  2699. TA_PAD_Y + target * line_h, line_h);
  2700. (void)view;
  2701. }
  2702. /* Selected text gets the theme colour; a drag over a tab keeps its cells
  2703. * highlighted. Draws one visual segment, splitting the run at tabs so the
  2704. * glyph positions match the pen used for wrapping and hit-testing. */
  2705. static void ta_draw_segment(struct ngui *ui,
  2706. const struct ngui_text_area *ta,
  2707. unsigned int s, unsigned int e, int x0, int y,
  2708. unsigned int sel_a, unsigned int sel_b)
  2709. {
  2710. int pen = 0, run_pen = 0;
  2711. unsigned int p, run_start = s;
  2712. if (sel_b > sel_a) {
  2713. unsigned int sa = sel_a > s ? sel_a : s;
  2714. unsigned int sb = sel_b < e ? sel_b : e;
  2715. if (sa < sb) {
  2716. int pa = ta_pen_at(ui, ta, s, sa);
  2717. int pb = ta_pen_at(ui, ta, s, sb);
  2718. struct ngui_rect hr;
  2719. hr.x = x0 + (pa >> 6);
  2720. hr.y = y;
  2721. hr.w = ((pb + 32) >> 6) - (pa >> 6);
  2722. if (hr.w < 1)
  2723. hr.w = 1;
  2724. hr.h = ngui_text_line_height(ui, ui->font_px, 1);
  2725. ngui_fill_rect(ui, hr, ui->theme.selection);
  2726. }
  2727. }
  2728. for (p = s; p < e; p++) {
  2729. if (ta->text[p] == '\t') {
  2730. if (p > run_start)
  2731. ngui_text_px(ui, x0 + (run_pen >> 6), y,
  2732. ta->text + run_start,
  2733. p - run_start, ui->theme.text,
  2734. ui->font_px, 0, 0, 1);
  2735. pen += ta_tab_advance(ui, pen);
  2736. run_start = p + 1;
  2737. run_pen = pen;
  2738. } else {
  2739. pen += ta_char_advance(ui,
  2740. (unsigned char)ta->text[p]);
  2741. }
  2742. }
  2743. if (e > run_start)
  2744. ngui_text_px(ui, x0 + (run_pen >> 6), y, ta->text + run_start,
  2745. e - run_start, ui->theme.text, ui->font_px,
  2746. 0, 0, 1);
  2747. }
  2748. int ngui_text_area(struct ngui *ui, unsigned int id, struct ngui_rect r,
  2749. struct ngui_text_area *ta)
  2750. {
  2751. struct ngui_rect view;
  2752. struct ta_metrics m;
  2753. unsigned int line, column, sel_a, sel_b;
  2754. int focused, avail, line_h, result = 0, k, caret_moved = 0;
  2755. if (!ui || !ta || !ta->text)
  2756. return 0;
  2757. if (ta->cap < 1)
  2758. return 0;
  2759. if (ta->len >= ta->cap) {
  2760. ta->len = ta->cap - 1;
  2761. ta->text[ta->len] = '\0';
  2762. }
  2763. if (ta->cursor > ta->len)
  2764. ta->cursor = ta->len;
  2765. if (ta->anchor > ta->len)
  2766. ta->anchor = ta->len;
  2767. focused = ui->focus == id;
  2768. line_h = ngui_text_line_height(ui, ui->font_px, 1);
  2769. if (line_h < 1)
  2770. line_h = 16;
  2771. view.x = r.x + 1;
  2772. view.y = r.y + 1;
  2773. view.w = r.w - 2;
  2774. view.h = r.h - 2;
  2775. if (view.w < 1)
  2776. view.w = 1;
  2777. if (view.h < 1)
  2778. view.h = 1;
  2779. avail = (view.w - 2 * TA_PAD_X - TA_SCROLL_RESERVE) << 6;
  2780. if (avail < (8 << 6))
  2781. avail = 8 << 6;
  2782. ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
  2783. ngui_fill_rect(ui, view, ui->theme.bg);
  2784. ngui_cursor_rect(ui, view, CURSOR_TEXT);
  2785. ta_measure(ui, ta, avail, &m, &line, &column);
  2786. ta->scroll.view_h = view.h;
  2787. ta->scroll.content_h = m.rows * line_h + 2 * TA_PAD_Y;
  2788. ngui_scroll_begin(ui, view, &ta->scroll);
  2789. if (ngui_scroll_wheel(ui, view, &ta->scroll))
  2790. ui->dirty = 1;
  2791. if (ui->pressed && ngui_rect_contains(&view, ui->press_x,
  2792. ui->press_y)) {
  2793. unsigned int pos = ta_pos_at_xy(ui, ta, avail, &view,
  2794. ui->press_x, ui->press_y);
  2795. unsigned int now = u_ticks_ms();
  2796. int dbl = ta->last_click_ms &&
  2797. (unsigned int)(now - ta->last_click_ms) <= 500;
  2798. ta->cursor = pos;
  2799. if (dbl && pos < ta->len &&
  2800. ta_word_char((unsigned char)ta->text[pos])) {
  2801. unsigned int a = pos, b = pos;
  2802. while (a > 0 &&
  2803. ta_word_char((unsigned char)ta->text[a - 1]))
  2804. a--;
  2805. while (b < ta->len &&
  2806. ta_word_char((unsigned char)ta->text[b]))
  2807. b++;
  2808. ta->anchor = a;
  2809. ta->cursor = b;
  2810. ta->dragging = 0;
  2811. } else {
  2812. ta->anchor = pos;
  2813. ta->dragging = 1;
  2814. }
  2815. ta->last_click_ms = now;
  2816. ngui_set_focus(ui, id);
  2817. ui->focus_claimed = 1;
  2818. ui->last_blink = now;
  2819. ui->caret_on = 1;
  2820. focused = 1;
  2821. caret_moved = 1;
  2822. } else if (ui->pressed) {
  2823. ta->dragging = 0;
  2824. }
  2825. if (ta->dragging && (ui->buttons & 1) && !ui->released) {
  2826. ta->cursor = ta_pos_at_xy(ui, ta, avail, &view,
  2827. ui->mouse_x, ui->mouse_y);
  2828. ngui_invalidate(ui);
  2829. caret_moved = 1;
  2830. }
  2831. if (ui->released)
  2832. ta->dragging = 0;
  2833. if (ui->focus == id) {
  2834. while ((k = ngui_key_peek(ui)) != 0) {
  2835. int changed = 0;
  2836. int handled = 1;
  2837. if (k >= 0x20 && k <= 0x7E) {
  2838. if (!ta->read_only)
  2839. changed = ta_insert_char(ta, (char)k);
  2840. } else if (k == '\n' || k == '\r') {
  2841. if (!ta->read_only)
  2842. changed = ta_insert_char(ta, '\n');
  2843. } else if (k == '\t') {
  2844. if (!ta->read_only)
  2845. changed = ta_insert_char(ta, '\t');
  2846. } else if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
  2847. if (!ta->read_only) {
  2848. if (ta_delete_selection(ta))
  2849. changed = 1;
  2850. else if (ta->cursor > 0)
  2851. changed = ta_delete_range(
  2852. ta, ta->cursor - 1,
  2853. ta->cursor);
  2854. }
  2855. } else if (k == NGUI_KEY_DELETE) {
  2856. if (!ta->read_only) {
  2857. if (ta_delete_selection(ta))
  2858. changed = 1;
  2859. else if (ta->cursor < ta->len)
  2860. changed = ta_delete_range(
  2861. ta, ta->cursor,
  2862. ta->cursor + 1);
  2863. }
  2864. } else if (k == NGUI_KEY_CTRL_BACKSPACE) {
  2865. if (!ta->read_only) {
  2866. if (ta_delete_selection(ta))
  2867. changed = 1;
  2868. else
  2869. changed = ta_delete_range(
  2870. ta,
  2871. ta_word_prev(ta,
  2872. ta->cursor),
  2873. ta->cursor);
  2874. }
  2875. } else if (k == NGUI_KEY_CTRL_DELETE) {
  2876. if (!ta->read_only) {
  2877. if (ta_delete_selection(ta)) {
  2878. changed = 1;
  2879. } else {
  2880. unsigned int a = ta->cursor;
  2881. unsigned int b =
  2882. ta_word_delete_end(ta, a);
  2883. if (b == a) {
  2884. a = ta_word_prev(ta,
  2885. ta->cursor);
  2886. b = ta->cursor;
  2887. }
  2888. changed = ta_delete_range(ta,
  2889. a, b);
  2890. }
  2891. }
  2892. } else if (k == NGUI_KEY_LEFT) {
  2893. if (ta->cursor > 0)
  2894. ta->cursor--;
  2895. ta->anchor = ta->cursor;
  2896. } else if (k == NGUI_KEY_RIGHT) {
  2897. if (ta->cursor < ta->len)
  2898. ta->cursor++;
  2899. ta->anchor = ta->cursor;
  2900. } else if (k == NGUI_KEY_CTRL_LEFT) {
  2901. ta->cursor = ta_word_prev(ta, ta->cursor);
  2902. ta->anchor = ta->cursor;
  2903. } else if (k == NGUI_KEY_CTRL_RIGHT) {
  2904. ta->cursor = ta_word_next(ta, ta->cursor);
  2905. ta->anchor = ta->cursor;
  2906. } else if (k == NGUI_KEY_HOME) {
  2907. ta->cursor = m.seg_start;
  2908. ta->anchor = ta->cursor;
  2909. } else if (k == NGUI_KEY_END) {
  2910. ta->cursor = m.seg_end;
  2911. ta->anchor = ta->cursor;
  2912. } else if (k == NGUI_KEY_UP) {
  2913. ta_move_vertical(ui, ta, avail, &m, &view, -1);
  2914. } else if (k == NGUI_KEY_DOWN) {
  2915. ta_move_vertical(ui, ta, avail, &m, &view, 1);
  2916. } else if (k == NGUI_KEY_PAGE_UP) {
  2917. int page = view.h / line_h - 1;
  2918. if (page < 1)
  2919. page = 1;
  2920. ta_move_vertical(ui, ta, avail, &m, &view,
  2921. -page);
  2922. } else if (k == NGUI_KEY_PAGE_DOWN) {
  2923. int page = view.h / line_h - 1;
  2924. if (page < 1)
  2925. page = 1;
  2926. ta_move_vertical(ui, ta, avail, &m, &view,
  2927. page);
  2928. } else if (k == 0x01) { /* Ctrl+A: select all */
  2929. ta->anchor = 0;
  2930. ta->cursor = ta->len;
  2931. ta->dragging = 0;
  2932. } else if (k == NGUI_KEY_ESC) {
  2933. ui->focus = 0;
  2934. result |= NGUI_TEXTAREA_CANCEL;
  2935. } else {
  2936. handled = 0;
  2937. }
  2938. if (!handled)
  2939. break;
  2940. ngui_key(ui);
  2941. if (changed)
  2942. result |= NGUI_TEXTAREA_CHANGED;
  2943. if (k != NGUI_KEY_ESC) {
  2944. caret_moved = 1;
  2945. ta_measure(ui, ta, avail, &m, &line, &column);
  2946. }
  2947. }
  2948. }
  2949. /* The edit keys can change both the wrap and the caret row. */
  2950. ta_measure(ui, ta, avail, &m, &line, &column);
  2951. ta->line = line;
  2952. ta->column = column;
  2953. ta->scroll.view_h = view.h;
  2954. ta->scroll.content_h = m.rows * line_h + 2 * TA_PAD_Y;
  2955. if (caret_moved)
  2956. ngui_scroll_ensure_visible(&ta->scroll,
  2957. TA_PAD_Y + m.caret_row * line_h,
  2958. line_h);
  2959. ta_sel_bounds(ta, &sel_a, &sel_b);
  2960. {
  2961. unsigned int pos = 0;
  2962. int row = 0;
  2963. for (;;) {
  2964. unsigned int le = ta_line_end(ta, pos);
  2965. unsigned int s = pos;
  2966. for (;;) {
  2967. unsigned int e;
  2968. int used = 0;
  2969. int y;
  2970. if (s >= le)
  2971. e = le;
  2972. else
  2973. e = ta_seg_end(ui, ta, s, le, avail,
  2974. &used);
  2975. y = view.y + TA_PAD_Y + row * line_h -
  2976. ta->scroll.offset;
  2977. if (y + line_h > view.y &&
  2978. y < view.y + view.h)
  2979. ta_draw_segment(ui, ta, s, e,
  2980. view.x + TA_PAD_X, y,
  2981. sel_a, sel_b);
  2982. if (row == m.caret_row && ui->focus == id &&
  2983. ui->caret_on) {
  2984. int cx = view.x + TA_PAD_X +
  2985. (m.caret_pen >> 6);
  2986. if (cx < view.x)
  2987. cx = view.x;
  2988. if (cx >= view.x + view.w)
  2989. cx = view.x + view.w - 1;
  2990. ngui_fill_rect(ui,
  2991. rect_make(cx, y, 1, line_h),
  2992. ui->theme.accent);
  2993. }
  2994. row++;
  2995. if (e >= le)
  2996. break;
  2997. s = e;
  2998. }
  2999. if (le >= ta->len)
  3000. break;
  3001. pos = le + 1;
  3002. }
  3003. }
  3004. ngui_scroll_end(ui, view, &ta->scroll);
  3005. ngui_frame(ui, r, ui->focus == id ? ui->theme.accent :
  3006. ui->theme.border);
  3007. if (ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y))
  3008. ui->hot = id;
  3009. return result;
  3010. }