| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428 |
- /* ngui: immediate-mode widgets for RGB565 canvas windows. See ngui.h. */
- #include "ngui.h"
- #include "ucmd_lib.h"
- #include "font_8x16.h"
- #include "ufont.h"
- const struct ngui_theme ngui_theme_dark = {
- .bg = NGUI_RGB(24, 28, 40),
- .panel = NGUI_RGB(38, 43, 58),
- .panel_alt = NGUI_RGB(50, 56, 74),
- .border = NGUI_RGB(92, 99, 124),
- .text = NGUI_RGB(228, 230, 240),
- .text_dim = NGUI_RGB(145, 151, 170),
- .accent = NGUI_RGB(84, 132, 224),
- .selection = NGUI_RGB(54, 92, 166),
- .button = NGUI_RGB(54, 61, 80),
- .button_hot = NGUI_RGB(70, 81, 108),
- .button_down = NGUI_RGB(48, 112, 204),
- .icon_folder = NGUI_RGB(238, 198, 88),
- .icon_file = NGUI_RGB(204, 210, 226),
- .icon_disk = NGUI_RGB(112, 174, 232),
- .error = NGUI_RGB(238, 116, 116),
- .success = NGUI_RGB(110, 210, 130),
- .warning = NGUI_RGB(238, 198, 88),
- };
- static unsigned int slen(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static void scopy(char *dst, const char *src, unsigned int cap)
- {
- unsigned int i = 0;
- if (!cap)
- return;
- while (src && src[i] && i + 1 < cap) {
- dst[i] = src[i];
- i++;
- }
- dst[i] = '\0';
- }
- static int rect_empty(const struct ngui_rect *r)
- {
- return r->w <= 0 || r->h <= 0;
- }
- static int rect_intersect(const struct ngui_rect *a, const struct ngui_rect *b,
- struct ngui_rect *out)
- {
- int x0 = a->x > b->x ? a->x : b->x;
- int y0 = a->y > b->y ? a->y : b->y;
- int x1 = a->x + a->w < b->x + b->w ? a->x + a->w : b->x + b->w;
- int y1 = a->y + a->h < b->y + b->h ? a->y + a->h : b->y + b->h;
- out->x = x0;
- out->y = y0;
- out->w = x1 - x0;
- out->h = y1 - y0;
- return !rect_empty(out);
- }
- static struct ngui_rect rect_make(int x, int y, int w, int h)
- {
- struct ngui_rect r;
- r.x = x;
- r.y = y;
- r.w = w;
- r.h = h;
- return r;
- }
- int ngui_rect_contains(const struct ngui_rect *r, int x, int y)
- {
- return x >= r->x && y >= r->y && x < r->x + r->w && y < r->y + r->h;
- }
- void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h)
- {
- unsigned int i;
- for (i = 0; i < sizeof(*ui); i++)
- ((unsigned char *)ui)[i] = 0;
- ui->px = fb;
- ui->w = w;
- ui->h = h;
- ui->theme = ngui_theme_dark;
- ui->font_px = 14;
- ui->nclip = 0;
- ui->dirty = 1;
- ui->caret_on = 1;
- ui->last_blink = u_ticks_ms();
- ui->motion_ms = ui->last_blink;
- ui->last_mouse_x = -1;
- ui->last_mouse_y = -1;
- }
- void ngui_font_attach(struct ngui *ui)
- {
- if (!ui || ui->font)
- return;
- ui->font_px = 14;
- ui->font = ufont_face_open_path("/ramfs/fonts/sans.ttf");
- if (!ui->font)
- return;
- ui->font_bold = ufont_face_open_path("/ramfs/fonts/sans-b.ttf");
- ui->font_italic = ufont_face_open_path("/ramfs/fonts/sans-i.ttf");
- ui->font_bold_italic = ufont_face_open_path("/ramfs/fonts/sans-bi.ttf");
- ui->font_mono = ufont_face_open_path("/ramfs/fonts/mono.ttf");
- if (!ui->font_bold)
- ui->font_bold = ui->font;
- if (!ui->font_italic)
- ui->font_italic = ui->font;
- if (!ui->font_bold_italic)
- ui->font_bold_italic = ui->font_bold;
- }
- void ngui_font_detach(struct ngui *ui)
- {
- if (!ui)
- return;
- ui->font = 0;
- ui->font_bold = 0;
- ui->font_italic = 0;
- ui->font_bold_italic = 0;
- ui->font_mono = 0;
- ui->font_emoji = 0;
- ui->emoji_loaded = 0;
- ufont_reset();
- }
- /* Emoji fallback is opened on first use so apps that never draw UTF-8 text
- * do not pay for it. ufont_face_open_path() may clear the glyph cache, which
- * is safe here: callers never keep a glyph pointer across this call. */
- static struct ufont_face *ngui_emoji_face(struct ngui *ui)
- {
- if (!ui)
- return 0;
- if (!ui->emoji_loaded) {
- ui->emoji_loaded = 1;
- ui->font_emoji = ufont_face_open_path("/ramfs/fonts/emoji.ttf");
- }
- return ui->font_emoji;
- }
- void ngui_invalidate(struct ngui *ui)
- {
- ui->dirty = 1;
- }
- void ngui_set_focus(struct ngui *ui, unsigned int id)
- {
- if (ui->focus != id) {
- ui->focus = id;
- ui->last_blink = u_ticks_ms();
- ui->caret_on = 1;
- ui->dirty = 1;
- }
- }
- unsigned int ngui_focus(struct ngui *ui)
- {
- return ui->focus;
- }
- static void key_push(struct ngui *ui, int k)
- {
- unsigned int next = (ui->key_head + 1) % 32;
- if (next == ui->key_tail)
- return;
- ui->keys[ui->key_head] = k;
- ui->key_head = next;
- }
- int ngui_key_peek(struct ngui *ui)
- {
- if (ui->key_tail == ui->key_head)
- return 0;
- return ui->keys[ui->key_tail];
- }
- int ngui_key(struct ngui *ui)
- {
- int k = ngui_key_peek(ui);
- if (!k)
- return 0;
- ui->key_tail = (ui->key_tail + 1) % 32;
- return k;
- }
- int ngui_begin(struct ngui *ui)
- {
- struct win_info wi;
- struct win_pointer wp;
- struct win_scroll ws;
- struct win_event ev;
- unsigned int now;
- int want = ui->dirty;
- int i, k;
- ui->dirty = 0;
- ui->pressed = ui->released = 0;
- ui->clicked = 0;
- ui->focus_claimed = 0;
- ui->hot = 0;
- if (u_win_info(&wi) == 0) {
- if (wi.width != (unsigned int)ui->w ||
- wi.height != (unsigned int)ui->h) {
- if (u_win_ack_resize() == 0) {
- ui->px = (unsigned short *)wi.fb;
- ui->w = (int)wi.width;
- ui->h = (int)wi.height;
- }
- want = 1;
- } else if (wi.fb && wi.fb != (void *)ui->px) {
- ui->px = (unsigned short *)wi.fb;
- }
- }
- if (u_win_pointer(&wp) == 0) {
- ui->mouse_x = wp.x;
- ui->mouse_y = wp.y;
- ui->buttons = wp.buttons;
- ui->inside = wp.inside ? 1 : 0;
- ui->focused = wp.focused ? 1 : 0;
- } else {
- ui->mouse_x = ui->mouse_y = 0;
- ui->buttons = 0;
- ui->inside = 0;
- ui->focused = 0;
- }
- if (u_win_scroll(&ws) == 1) {
- ui->scroll_x = ws.dx;
- ui->scroll_y = ws.dy;
- if (ws.dx || ws.dy)
- want = 1;
- } else {
- ui->scroll_x = ui->scroll_y = 0;
- }
- for (i = 0; i < 16; i++) {
- int r = u_win_event(&ev);
- if (r != 1)
- break;
- if (ev.type == WIN_EV_POINTER_DOWN) {
- ui->pressed |= 1;
- ui->press_x = ev.x;
- ui->press_y = ev.y;
- ui->clicked = 1;
- } else if (ev.type == WIN_EV_POINTER_UP) {
- ui->released |= 1;
- ui->release_x = ev.x;
- ui->release_y = ev.y;
- }
- }
- for (i = 0; i < 32; i++) {
- k = u_key_poll();
- if (!k)
- break;
- key_push(ui, k);
- want = 1;
- }
- now = u_ticks_ms();
- if (ui->focus) {
- if (now - ui->last_blink >= 500) {
- ui->last_blink = now;
- ui->caret_on = !ui->caret_on;
- want = 1;
- }
- } else {
- ui->caret_on = 1;
- }
- if (ui->inside != ui->last_inside) {
- want = 1;
- ui->motion_pending = 0;
- ui->motion_ms = now;
- } else if (ui->inside &&
- (ui->mouse_x != ui->last_mouse_x ||
- ui->mouse_y != ui->last_mouse_y)) {
- /* Hover feedback needs a repaint, but not one per mouse
- * report: while the pointer keeps moving, redraw at most every
- * 33 ms and remember that a final frame is still due. A held
- * button is a drag, which stays at full rate. */
- if (ui->buttons) {
- ui->motion_ms = now;
- ui->motion_pending = 0;
- want = 1;
- } else {
- ui->motion_pending = 1;
- if ((unsigned int)(now - ui->motion_ms) >=
- NGUI_MOTION_REDRAW_MS) {
- ui->motion_ms = now;
- ui->motion_pending = 0;
- want = 1;
- }
- }
- } else if (ui->motion_pending &&
- (unsigned int)(now - ui->motion_ms) >= NGUI_MOTION_REDRAW_MS) {
- ui->motion_ms = now;
- ui->motion_pending = 0;
- want = 1;
- }
- if (ui->focused != ui->last_focused)
- want = 1;
- ui->last_mouse_x = ui->mouse_x;
- ui->last_mouse_y = ui->mouse_y;
- ui->last_inside = ui->inside;
- ui->last_focused = ui->focused;
- if (ui->pressed || ui->released)
- want = 1;
- if (want)
- ui->motion_pending = 0;
- ui->cursor_frame = want;
- if (want)
- ui->cursor = CURSOR_DEFAULT;
- return want;
- }
- void ngui_set_cursor(struct ngui *ui, int shape)
- {
- ui->cursor = shape;
- }
- int ngui_cursor_rect(struct ngui *ui, struct ngui_rect r, int shape)
- {
- if (!ui->inside || !ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y))
- return 0;
- if (ui->nclip > 0 &&
- !ngui_rect_contains(&ui->clip[ui->nclip - 1], ui->mouse_x,
- ui->mouse_y))
- return 0;
- ui->cursor = shape;
- return 1;
- }
- void ngui_end_ex(struct ngui *ui, int present)
- {
- if (present && u_win_present() != 0)
- ui->dirty = 1;
- /* Only a redrawn frame has evaluated every widget's hover state. */
- if (ui->cursor_frame && ui->cursor != ui->cursor_sent &&
- u_win_set_cursor(ui->cursor) == 0)
- ui->cursor_sent = ui->cursor;
- ui->cursor_frame = 0;
- if (ui->clicked && !ui->focus_claimed)
- ui->focus = 0;
- /* Only the release edge ends a press. The sampled button state is not
- * synchronized with the event queue: it can read 0 for a frame before
- * the release event is delivered. Clearing active on "!buttons" made
- * clicks fire only when the two happened to land in the same frame. */
- if (ui->released)
- ui->active = 0;
- ui->pressed = ui->released = 0;
- ui->clicked = 0;
- ui->focus_claimed = 0;
- ui->press_x = ui->press_y = 0;
- ui->release_x = ui->release_y = 0;
- }
- void ngui_end(struct ngui *ui)
- {
- ngui_end_ex(ui, 1);
- }
- struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h)
- {
- struct ngui_rect out = *r;
- if (h < 0)
- h = 0;
- if (h > r->h)
- h = r->h;
- out.h = h;
- r->y += h;
- r->h -= h;
- return out;
- }
- struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h)
- {
- struct ngui_rect out = *r;
- if (h < 0)
- h = 0;
- if (h > r->h)
- h = r->h;
- out.y = r->y + r->h - h;
- out.h = h;
- r->h -= h;
- return out;
- }
- struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w)
- {
- struct ngui_rect out = *r;
- if (w < 0)
- w = 0;
- if (w > r->w)
- w = r->w;
- out.w = w;
- r->x += w;
- r->w -= w;
- return out;
- }
- struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w)
- {
- struct ngui_rect out = *r;
- if (w < 0)
- w = 0;
- if (w > r->w)
- w = r->w;
- out.x = r->x + r->w - w;
- out.w = w;
- r->w -= w;
- return out;
- }
- void ngui_inset(struct ngui_rect *r, int n)
- {
- r->x += n;
- r->y += n;
- r->w -= n * 2;
- r->h -= n * 2;
- if (r->w < 0)
- r->w = 0;
- if (r->h < 0)
- r->h = 0;
- }
- void ngui_push_clip(struct ngui *ui, struct ngui_rect r)
- {
- struct ngui_rect out;
- struct ngui_rect canvas;
- if (ui->nclip >= 4)
- return;
- canvas = rect_make(0, 0, ui->w, ui->h);
- if (!rect_intersect(&r, &canvas, &out))
- out = rect_make(0, 0, 0, 0);
- if (ui->nclip > 0) {
- struct ngui_rect clipped = out;
- if (!rect_intersect(&clipped, &ui->clip[ui->nclip - 1], &out))
- out = rect_make(0, 0, 0, 0);
- }
- ui->clip[ui->nclip++] = out;
- }
- void ngui_pop_clip(struct ngui *ui)
- {
- if (ui->nclip > 0)
- ui->nclip--;
- }
- static int pixel_visible(struct ngui *ui, int x, int y)
- {
- struct ngui_rect *c;
- if (x < 0 || y < 0 || x >= ui->w || y >= ui->h)
- return 0;
- if (ui->nclip <= 0)
- return 1;
- c = &ui->clip[ui->nclip - 1];
- return x >= c->x && y >= c->y &&
- x < c->x + c->w && y < c->y + c->h;
- }
- void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color)
- {
- struct ngui_rect canvas;
- int x, y;
- if (rect_empty(&r))
- return;
- canvas = rect_make(0, 0, ui->w, ui->h);
- if (!rect_intersect(&r, &canvas, &r))
- return;
- if (ui->nclip > 0 &&
- !rect_intersect(&r, &ui->clip[ui->nclip - 1], &r))
- return;
- for (y = r.y; y < r.y + r.h; y++) {
- unsigned short *p = ui->px + y * ui->w + r.x;
- for (x = 0; x < r.w; x++)
- p[x] = color;
- }
- }
- void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color)
- {
- if (r.w <= 0 || r.h <= 0)
- return;
- ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), color);
- ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), color);
- if (r.h > 2) {
- ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), color);
- ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
- r.h - 2), color);
- }
- }
- /* Brighten (step > 0) or darken (step < 0) an RGB565 color for the
- * one-pixel bevel. Green has six bits, red and blue five, so the green
- * component moves twice as far to keep the shade visually even. */
- static unsigned short shade565(unsigned short c, int step)
- {
- int r = (int)((c >> 11) & 0x1F) + step;
- int g = (int)((c >> 5) & 0x3F) + step * 2;
- int b = (int)(c & 0x1F) + step;
- if (r < 0)
- r = 0;
- if (r > 0x1F)
- r = 0x1F;
- if (g < 0)
- g = 0;
- if (g > 0x3F)
- g = 0x3F;
- if (b < 0)
- b = 0;
- if (b > 0x1F)
- b = 0x1F;
- return (unsigned short)((r << 11) | (g << 5) | b);
- }
- /* A one-pixel two-tone edge: `tl` along the top and left, `br` along the
- * bottom and right. A raised widget lights its top/left and shades its
- * bottom/right; an inset one (a text field) swaps the tones. */
- static void bevel_rect(struct ngui *ui, struct ngui_rect r,
- unsigned short tl, unsigned short br)
- {
- if (r.w <= 0 || r.h <= 0)
- return;
- ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), tl);
- ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), br);
- if (r.h > 2) {
- ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), tl);
- ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
- r.h - 2), br);
- }
- }
- /* Raised button edge: lit on the top/left, shaded on the bottom/right. A
- * pressed button inverts the bevel, so it reads as pushed in. The hovered
- * button keeps its accent on the lit edge. */
- static void button_bevel(struct ngui *ui, struct ngui_rect r,
- unsigned short bg, int hot, int down)
- {
- if (down)
- bevel_rect(ui, r, shade565(bg, -4), shade565(bg, 4));
- else if (hot)
- bevel_rect(ui, r, ui->theme.accent, shade565(bg, -4));
- else
- bevel_rect(ui, r, shade565(bg, 4), shade565(bg, -4));
- }
- static int ngui_isqrt(int v)
- {
- int r = 0, bit = 1 << 30;
- if (v <= 0)
- return 0;
- /* Classic shift-and-subtract sqrt: bit must be the highest power
- * of four that fits in v, and each step consumes two bits. */
- while (bit > v)
- bit >>= 2;
- while (bit) {
- if (v >= r + bit) {
- v -= r + bit;
- r = (r >> 1) + bit;
- } else {
- r >>= 1;
- }
- bit >>= 2;
- }
- return r;
- }
- /* Horizontal inset of one scanline of a corner with the given radius. */
- static int round_inset(int y, int h, int r)
- {
- int dy, d2;
- if (r <= 0)
- return 0;
- if (y < r)
- dy = r - y - 1;
- else if (y >= h - r)
- dy = y - (h - r);
- else
- return 0;
- d2 = r * r - dy * dy;
- if (d2 < 0)
- d2 = 0;
- return r - ngui_isqrt(d2);
- }
- void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
- int tl, int tr, int br, int bl,
- unsigned short color)
- {
- int y, half;
- if (rect_empty(&r))
- return;
- half = r.w < r.h ? r.w / 2 : r.h / 2;
- if (tl > half) tl = half;
- if (tr > half) tr = half;
- if (br > half) br = half;
- if (bl > half) bl = half;
- if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
- ngui_fill_rect(ui, r, color);
- return;
- }
- for (y = 0; y < r.h; y++) {
- int li = 0, ri = 0;
- if (y < tl)
- li = round_inset(y, r.h, tl);
- if (y < tr)
- ri = round_inset(y, r.h, tr);
- if (y >= r.h - bl) {
- int i = round_inset(y, r.h, bl);
- if (i > li)
- li = i;
- }
- if (y >= r.h - br) {
- int i = round_inset(y, r.h, br);
- if (i > ri)
- ri = i;
- }
- if (r.w - li - ri > 0)
- ngui_fill_rect(ui, rect_make(r.x + li, r.y + y,
- r.w - li - ri, 1), color);
- }
- }
- void ngui_frame_round(struct ngui *ui, struct ngui_rect r,
- int tl, int tr, int br, int bl, int thickness,
- unsigned short color)
- {
- int y, half;
- if (rect_empty(&r))
- return;
- if (thickness < 1)
- thickness = 1;
- half = r.w < r.h ? r.w / 2 : r.h / 2;
- if (tl > half) tl = half;
- if (tr > half) tr = half;
- if (br > half) br = half;
- if (bl > half) bl = half;
- if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
- ngui_frame(ui, r, color);
- return;
- }
- for (y = 0; y < r.h; y++) {
- int li = 0, ri = 0;
- int x0, x1;
- if (y < tl)
- li = round_inset(y, r.h, tl);
- if (y < tr)
- ri = round_inset(y, r.h, tr);
- if (y >= r.h - bl) {
- int i = round_inset(y, r.h, bl);
- if (i > li)
- li = i;
- }
- if (y >= r.h - br) {
- int i = round_inset(y, r.h, br);
- if (i > ri)
- ri = i;
- }
- x0 = r.x + li;
- x1 = r.x + r.w - 1 - ri;
- if (x0 > x1)
- continue;
- if (y < thickness || y >= r.h - thickness) {
- ngui_fill_rect(ui, rect_make(x0, r.y + y,
- x1 - x0 + 1, 1), color);
- } else {
- ngui_fill_rect(ui, rect_make(x0, r.y + y,
- thickness, 1), color);
- ngui_fill_rect(ui, rect_make(x1 - thickness + 1,
- r.y + y, thickness, 1), color);
- }
- }
- }
- static void ngui_pixel(struct ngui *ui, int x, int y, unsigned short color)
- {
- if (pixel_visible(ui, x, y))
- ui->px[y * ui->w + x] = color;
- }
- void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
- unsigned short color)
- {
- int dx = x1 - x0;
- int dy = y1 - y0;
- int sx = dx < 0 ? -1 : 1;
- int sy = dy < 0 ? -1 : 1;
- int err, e2;
- if (dx < 0)
- dx = -dx;
- if (dy < 0)
- dy = -dy;
- dy = -dy;
- err = dx + dy;
- for (;;) {
- ngui_pixel(ui, x0, y0, color);
- if (x0 == x1 && y0 == y1)
- break;
- e2 = err * 2;
- if (e2 >= dy) {
- err += dy;
- x0 += sx;
- }
- if (e2 <= dx) {
- err += dx;
- y0 += sy;
- }
- }
- }
- static void ngui_bitmap_glyph(struct ngui *ui, int x, int y, unsigned char c,
- unsigned short color, int scale, int bold)
- {
- const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
- int row, col, sx, sy;
- if (scale < 1)
- scale = 1;
- for (row = 0; row < FONT_8X16_ROWS; row++) {
- unsigned char bits = g[row];
- if (!bits)
- continue;
- for (col = 0; col < 8; col++) {
- if (!(bits & (0x80u >> col)))
- continue;
- for (sy = 0; sy < scale; sy++) {
- for (sx = 0; sx < scale; sx++) {
- ngui_pixel(ui,
- x + col * scale + sx +
- (bold && scale == 1 ? 1 : 0),
- y + row * scale + sy,
- color);
- }
- }
- if (bold && scale > 1)
- ngui_pixel(ui, x + col * scale + scale,
- y + row * scale, color);
- }
- }
- }
- static struct ufont_face *ngui_face(struct ngui *ui, int bold, int italic,
- int mono)
- {
- struct ufont_face *f;
- if (!ui)
- return 0;
- if (mono && ui->font_mono)
- f = ui->font_mono;
- else if (bold && italic)
- f = ui->font_bold_italic;
- else if (bold)
- f = ui->font_bold;
- else if (italic)
- f = ui->font_italic;
- else
- f = ui->font;
- if (!f)
- f = ui->font;
- return f;
- }
- static void ngui_clip_bounds(struct ngui *ui, int *cx, int *cy,
- int *cw, int *ch)
- {
- struct ngui_rect r = { 0, 0, ui->w, ui->h };
- int i;
- for (i = 0; i < ui->nclip; i++) {
- struct ngui_rect out;
- if (!rect_intersect(&r, &ui->clip[i], &out)) {
- r.w = r.h = 0;
- break;
- }
- r = out;
- }
- *cx = r.x;
- *cy = r.y;
- *cw = r.w;
- *ch = r.h;
- }
- int ngui_text_ascent_face(struct ngui *ui, struct ufont_face *face, int px,
- int mono)
- {
- struct ufont_face *f = face ? face : ngui_face(ui, 0, 0, mono);
- struct ufont_metrics m;
- if (f) {
- ufont_metrics(f, px, &m);
- return m.ascent;
- }
- return 16 * (px >= 24 ? 2 : 1);
- }
- int ngui_text_ascent(struct ngui *ui, int px, int mono)
- {
- return ngui_text_ascent_face(ui, 0, px, mono);
- }
- int ngui_text_line_height_face(struct ngui *ui, struct ufont_face *face,
- int px, int mono)
- {
- struct ufont_face *f = face ? face : ngui_face(ui, 0, 0, mono);
- struct ufont_metrics m;
- if (f) {
- ufont_metrics(f, px, &m);
- return m.line_height;
- }
- return 16 * (px >= 24 ? 2 : 1);
- }
- int ngui_text_line_height(struct ngui *ui, int px, int mono)
- {
- return ngui_text_line_height_face(ui, 0, px, mono);
- }
- unsigned int ngui_text_width_face(struct ngui *ui, struct ufont_face *face,
- const char *s, unsigned int n, int px,
- int bold, int italic, int mono)
- {
- struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
- int scale;
- if (f)
- return (unsigned int)ufont_text_width(f, px, s, (int)n, 0);
- scale = px >= 24 ? 2 : 1;
- return n * 8u * (unsigned int)scale;
- }
- unsigned int ngui_text_width_px(struct ngui *ui, const char *s, unsigned int n,
- int px, int bold, int italic, int mono)
- {
- return ngui_text_width_face(ui, 0, s, n, px, bold, italic, mono);
- }
- unsigned int ngui_text_width_ui(struct ngui *ui, const char *s)
- {
- return ngui_text_width_px(ui, s, slen(s),
- ui ? ui->font_px : 14, 0, 0, 0);
- }
- unsigned int ngui_text_width(const char *s)
- {
- return slen(s) * 8;
- }
- unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale)
- {
- (void)s;
- if (scale < 1)
- scale = 1;
- return n * 8u * (unsigned int)scale;
- }
- static void ngui_text_px_common(struct ngui *ui, struct ufont_face *face,
- int x, int y, const char *s, unsigned int n,
- unsigned short color, int px, int bold,
- int italic, int mono)
- {
- struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
- int scale, i, pen = 0, ascent;
- int cx, cy, cw, ch;
- if (!ui || !ui->px || !s)
- return;
- if (!f) {
- scale = px >= 24 ? 2 : 1;
- for (i = 0; i < (int)n && s[i]; i++)
- ngui_bitmap_glyph(ui, x + i * 8 * scale, y,
- (unsigned char)s[i], color,
- scale, bold);
- return;
- }
- ascent = ngui_text_ascent_face(ui, face, px, mono);
- ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
- for (i = 0; i < (int)n && s[i]; i++) {
- const struct ufont_glyph *g =
- ufont_glyph(f, px, (unsigned char)s[i]);
- if (!g) {
- pen += 8 << 6;
- continue;
- }
- ufont_blit_rgb565(ui->px, ui->w, ui->h,
- x + (pen >> 6), y + ascent, g, color,
- cx, cy, cw, ch);
- pen += g->advance;
- }
- }
- void ngui_text_px(struct ngui *ui, int x, int y, const char *s,
- unsigned int n, unsigned short color, int px, int bold,
- int italic, int mono)
- {
- ngui_text_px_common(ui, 0, x, y, s, n, color, px, bold, italic, mono);
- }
- /* ---- UTF-8 text with an emoji fallback face --------------------------- */
- static int utf8_skip_cp(unsigned int cp)
- {
- return cp == 0x200B || cp == 0x200C || cp == 0x200D ||
- cp == 0xFE0E || cp == 0xFE0F;
- }
- /* Advance of one codepoint in 26.6 pixels, choosing the primary face, then
- * the lazily loaded emoji face, then '?'. */
- static int ngui_utf8_advance(struct ngui *ui, struct ufont_face *f,
- int px, unsigned int cp)
- {
- struct ufont_face *emoji;
- if (utf8_skip_cp(cp) || !f)
- return 0;
- if (ufont_has_glyph(f, cp))
- return ufont_advance_cp(f, px, cp);
- emoji = ngui_emoji_face(ui);
- if (emoji && ufont_has_glyph(emoji, cp))
- return ufont_advance_cp(emoji, px, cp);
- return ufont_advance_cp(f, px, '?');
- }
- static unsigned int ngui_text_width_face_utf8(struct ngui *ui,
- struct ufont_face *face,
- const char *s, unsigned int n,
- int px, int bold, int italic,
- int mono)
- {
- struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
- int i = 0, total = 0;
- if (!f)
- return n * 8u * (px >= 24 ? 2u : 1u);
- while (i < (int)n) {
- unsigned int cp = ufont_utf8_next(s, &i, (int)n);
- total += ngui_utf8_advance(ui, f, px, cp);
- }
- return (unsigned int)((total + 32) >> 6);
- }
- static const struct ufont_glyph *ngui_utf8_glyph(struct ngui *ui,
- struct ufont_face *f,
- int px, unsigned int cp,
- struct ufont_face **used)
- {
- struct ufont_face *emoji;
- const struct ufont_glyph *g;
- *used = f;
- if (utf8_skip_cp(cp) || !f)
- return 0;
- if (ufont_has_glyph(f, cp)) {
- g = ufont_glyph(f, px, cp);
- if (g)
- return g;
- }
- emoji = ngui_emoji_face(ui);
- if (emoji && ufont_has_glyph(emoji, cp)) {
- g = ufont_glyph(emoji, px, cp);
- if (g) {
- *used = emoji;
- return g;
- }
- }
- *used = f;
- return ufont_glyph(f, px, '?');
- }
- static void ngui_text_px_utf8_common(struct ngui *ui, struct ufont_face *face,
- int x, int y, const char *s,
- unsigned int n, unsigned short color,
- int px, int bold, int italic, int mono)
- {
- struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
- int i = 0, pen = 0, ascent;
- int cx, cy, cw, ch;
- if (!ui || !ui->px || !s)
- return;
- if (!f) {
- ngui_text_px_common(ui, face, x, y, s, n, color, px, bold,
- italic, mono);
- return;
- }
- ascent = ngui_text_ascent_face(ui, face, px, mono);
- ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
- while (i < (int)n) {
- unsigned int cp = ufont_utf8_next(s, &i, (int)n);
- struct ufont_face *used;
- const struct ufont_glyph *g;
- if (utf8_skip_cp(cp))
- continue;
- g = ngui_utf8_glyph(ui, f, px, cp, &used);
- if (!g) {
- pen += 8 << 6;
- continue;
- }
- ufont_blit_rgb565(ui->px, ui->w, ui->h,
- x + (pen >> 6), y + ascent, g, color,
- cx, cy, cw, ch);
- pen += g->advance;
- }
- }
- void ngui_text_n_utf8(struct ngui *ui, int x, int y, const char *s,
- unsigned int n, unsigned short color)
- {
- ngui_text_px_utf8_common(ui, 0, x, y, s, n, color,
- ui ? ui->font_px : 14, 0, 0, 0);
- }
- void ngui_text_utf8(struct ngui *ui, int x, int y, const char *s,
- unsigned short color)
- {
- ngui_text_n_utf8(ui, x, y, s, s ? slen(s) : 0, color);
- }
- unsigned int ngui_text_width_utf8(struct ngui *ui, const char *s)
- {
- return ngui_text_width_face_utf8(ui, 0, s, s ? slen(s) : 0,
- ui ? ui->font_px : 14, 0, 0, 0);
- }
- void ngui_text_ellipsis_utf8(struct ngui *ui, struct ngui_rect r,
- int x, int y, const char *s,
- unsigned short color)
- {
- unsigned int n = s ? slen(s) : 0;
- unsigned int end = 0, i = 0;
- int avail = r.x + r.w - x;
- int dots, width;
- if (!ui || !s)
- return;
- if (avail <= 0)
- return;
- width = (int)ngui_text_width_utf8(ui, s);
- if (width <= avail) {
- ngui_text_utf8(ui, x, y, s, color);
- return;
- }
- dots = (int)ngui_text_width_utf8(ui, "...");
- if (dots >= avail)
- return;
- while (i < n) {
- int next = i;
- (void)ufont_utf8_next(s, &next, (int)n);
- if ((int)ngui_text_width_face_utf8(ui, 0, s, (unsigned int)next,
- ui->font_px, 0, 0, 0) +
- dots > avail)
- break;
- i = (unsigned int)next;
- end = i;
- }
- ngui_text_n_utf8(ui, x, y, s, end, color);
- ngui_text_utf8(ui, x + (int)ngui_text_width_face_utf8(
- ui, 0, s, end, ui->font_px, 0, 0, 0),
- y, "...", color);
- }
- void ngui_text_px_face(struct ngui *ui, struct ufont_face *face, int x,
- int y, const char *s, unsigned int n,
- unsigned short color, int px, int bold, int italic,
- int mono)
- {
- ngui_text_px_common(ui, face, x, y, s, n, color, px, bold, italic,
- mono);
- }
- void ngui_text_n(struct ngui *ui, int x, int y, const char *s, unsigned int n,
- unsigned short color)
- {
- ngui_text_px(ui, x, y, s, n, color,
- ui ? ui->font_px : 14, 0, 0, 0);
- }
- void ngui_text(struct ngui *ui, int x, int y, const char *s,
- unsigned short color)
- {
- ngui_text_n(ui, x, y, s, slen(s), color);
- }
- void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
- unsigned int n, unsigned short color, int scale, int bold)
- {
- if (scale < 1)
- scale = 1;
- if (ui && ui->font) {
- ngui_text_px(ui, x, y, s, n, color,
- ui->font_px * scale, bold, 0, 0);
- return;
- }
- {
- unsigned int i;
- for (i = 0; i < n && s && s[i]; i++)
- ngui_bitmap_glyph(ui, x + (int)i * 8 * scale, y,
- (unsigned char)s[i], color,
- scale, bold);
- }
- }
- /* Draw a string upside down. The whole n-character block is rotated, so the
- * first character ends up on the right; used for the mirrored corner index on
- * a playing card. */
- void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
- unsigned int n, unsigned short color)
- {
- unsigned int i;
- int row, col;
- for (i = 0; i < n && s && s[i]; i++) {
- const unsigned char *g =
- &font_8x16[(unsigned int)(unsigned char)s[i] *
- FONT_8X16_ROWS];
- for (row = 0; row < FONT_8X16_ROWS; row++) {
- unsigned char bits = g[row];
- if (!bits)
- continue;
- for (col = 0; col < 8; col++) {
- if (!(bits & (0x80u >> col)))
- continue;
- ngui_pixel(ui,
- x + (int)(n - 1 - i) * 8 + 7 - col,
- y + FONT_8X16_ROWS - 1 - row,
- color);
- }
- }
- }
- }
- void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
- const char *s, unsigned short color)
- {
- unsigned int n = slen(s);
- int avail = r.x + r.w - x;
- unsigned int i, best = 0;
- int dots;
- if (avail <= 0)
- return;
- if (ui && ui->font) {
- dots = (int)ngui_text_width_px(ui, "...", 3, ui->font_px,
- 0, 0, 0);
- for (i = 0; i <= n; i++) {
- int w = (int)ngui_text_width_px(ui, s, i, ui->font_px,
- 0, 0, 0);
- if (w > avail)
- break;
- best = i;
- }
- if (best == n) {
- ngui_text_n(ui, x, y, s, n, color);
- return;
- }
- while (best > 0 &&
- (int)ngui_text_width_px(ui, s, best, ui->font_px,
- 0, 0, 0) + dots > avail)
- best--;
- ngui_text_n(ui, x, y, s, best, color);
- ngui_text_n(ui, x + (int)ngui_text_width_px(ui, s, best,
- ui->font_px, 0, 0, 0),
- y, "...", 3, color);
- return;
- }
- {
- int max = avail / 8;
- if (max <= 0)
- return;
- if (n <= (unsigned int)max || max < 4) {
- for (i = 0; i < n && (int)i < max; i++)
- ngui_bitmap_glyph(ui, x + (int)i * 8, y,
- (unsigned char)s[i], color, 1, 0);
- return;
- }
- for (i = 0; i < (unsigned int)(max - 3); i++)
- ngui_bitmap_glyph(ui, x + (int)i * 8, y,
- (unsigned char)s[i], color, 1, 0);
- ngui_bitmap_glyph(ui, x + (max - 3) * 8, y, '.', color, 1, 0);
- ngui_bitmap_glyph(ui, x + (max - 2) * 8, y, '.', color, 1, 0);
- ngui_bitmap_glyph(ui, x + (max - 1) * 8, y, '.', color, 1, 0);
- }
- }
- /* ---- Icons ------------------------------------------------------------
- * The Adwaita symbolic masks are read once per process into BSS (not the
- * libc heap, so the browser's heap_reset() cannot free them). The read is
- * capped at the icons this build knows: a newer bundle with appended icons
- * still works, an older one leaves the missing ids blank. */
- #define NGUI_ICON_PX 16
- #define NGUI_ICON_BYTES (NGUI_ICON_PX * NGUI_ICON_PX)
- static unsigned char icon_data[sizeof(struct icon_file_hdr) +
- NGUI_ICON_COUNT * NGUI_ICON_BYTES + 512];
- static int icon_state; /* 0 untried, 1 loaded, -1 unavailable */
- static unsigned int icon_count;
- #define NGUI_ICON_NAME_(e, name, file) name,
- static const char *const icon_names[NGUI_ICON_COUNT] = {
- ICON_LIST(NGUI_ICON_NAME_)
- };
- #undef NGUI_ICON_NAME_
- const char *ngui_icon_name(enum ngui_icon icon)
- {
- if ((unsigned int)icon >= NGUI_ICON_COUNT)
- return "?";
- return icon_names[icon];
- }
- static void icon_load(void)
- {
- const struct icon_file_hdr *h = (const struct icon_file_hdr *)icon_data;
- unsigned int max = sizeof(icon_data) - 512, have;
- int n, i;
- icon_state = -1;
- /* The FAT path may write a final partial sector past `max`; the
- * buffer keeps 512 bytes of slack for it. */
- n = u_syscall3(SYS_READ_FILE, (int)NGUI_ICON_PATH, (int)icon_data,
- (int)max);
- if (n < (int)sizeof(*h))
- return;
- for (i = 0; i < 8; i++)
- if (h->magic[i] != ICON_FILE_MAGIC[i])
- return;
- if (h->size != NGUI_ICON_PX)
- return;
- have = ((unsigned int)n - sizeof(*h)) / NGUI_ICON_BYTES;
- if (have > h->count)
- have = h->count;
- if (have > NGUI_ICON_COUNT)
- have = NGUI_ICON_COUNT;
- icon_count = have;
- icon_state = 1;
- }
- static const unsigned char *icon_mask(enum ngui_icon icon)
- {
- if (icon_state == 0)
- icon_load();
- if (icon_state != 1 || (unsigned int)icon >= icon_count)
- return 0;
- return icon_data + sizeof(struct icon_file_hdr) +
- (unsigned int)icon * NGUI_ICON_BYTES;
- }
- /* Tint a coverage mask with `color` and blend it over the canvas. */
- static void icon_blit(struct ngui *ui, int x, int y,
- const unsigned char *m, unsigned short color)
- {
- int cx, cy, cw, ch, i, j;
- int sr = color >> 11, sg = (color >> 5) & 63, sb = color & 31;
- ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
- for (j = 0; j < NGUI_ICON_PX; j++) {
- int py = y + j;
- unsigned short *row;
- if (py < cy || py >= cy + ch)
- continue;
- row = ui->px + py * ui->w;
- for (i = 0; i < NGUI_ICON_PX; i++) {
- int px = x + i, a = m[j * NGUI_ICON_PX + i];
- unsigned short d;
- int dr, dg, db;
- if (!a || px < cx || px >= cx + cw)
- continue;
- if (a == 255) {
- row[px] = color;
- continue;
- }
- d = row[px];
- dr = d >> 11;
- dg = (d >> 5) & 63;
- db = d & 31;
- dr += ((sr - dr) * a + 127) / 255;
- dg += ((sg - dg) * a + 127) / 255;
- db += ((sb - db) * a + 127) / 255;
- row[px] = (unsigned short)((dr << 11) | (dg << 5) | db);
- }
- }
- }
- void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
- unsigned short color)
- {
- int x = r.x + r.w / 2 - 8;
- int y = r.y + r.h / 2 - 8;
- const unsigned char *m;
- if (!ui || !ui->px)
- return;
- /* Without the bundle (a stale /ramfs or a host run without
- * icons/) the icon is simply left out. */
- m = icon_mask(icon);
- if (m)
- icon_blit(ui, x, y, m, color);
- }
- int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
- const char *label)
- {
- int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
- int press_hot = ui->pressed &&
- ngui_rect_contains(&r, ui->press_x, ui->press_y);
- int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
- int fired = 0;
- unsigned short bg;
- int tw, tx, ty;
- if (press_hot)
- ui->active = id;
- if (ui->pressed && ui->released && press_hot && release_hot)
- fired = 1;
- if (ui->released && ui->active == id) {
- if (release_hot)
- fired = 1;
- ui->active = 0;
- }
- if (ui->active == id)
- bg = ui->theme.button_down;
- else if (hot)
- bg = ui->theme.button_hot;
- else
- bg = ui->theme.button;
- ngui_fill_rect(ui, r, bg);
- button_bevel(ui, r, bg, hot, ui->active == id);
- if (label && label[0]) {
- tw = (int)ngui_text_width_ui(ui, label);
- tx = r.x + (r.w - tw) / 2;
- ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
- if (ty < r.y)
- ty = r.y;
- ngui_push_clip(ui, r);
- ngui_text(ui, tx, ty, label, ui->theme.text);
- ngui_pop_clip(ui);
- }
- if (hot)
- ui->hot = id;
- return fired;
- }
- /* One row of a drop-down menu: left-aligned label, optional check mark and
- * right-aligned shortcut. Uses the same press/release/active semantics as
- * ngui_button so a menu and its caller never disagree about a click. */
- static int ngui_menu_item_common(struct ngui *ui, unsigned int id,
- struct ngui_rect r, const char *label,
- const char *shortcut, int checked, int utf8)
- {
- int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
- int press_hot = ui->pressed &&
- ngui_rect_contains(&r, ui->press_x, ui->press_y);
- int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
- int fired = 0;
- int selected;
- unsigned short bg, fg;
- int ty;
- if (press_hot)
- ui->active = id;
- if (ui->pressed && ui->released && press_hot && release_hot)
- fired = 1;
- if (ui->released && ui->active == id) {
- if (release_hot)
- fired = 1;
- ui->active = 0;
- }
- selected = (ui->active == id) || hot;
- bg = selected ? ui->theme.selection : ui->theme.panel;
- fg = selected ? NGUI_RGB(255, 255, 255) : ui->theme.text;
- ngui_fill_rect(ui, r, bg);
- ty = r.y + (r.h - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
- if (ty < r.y)
- ty = r.y;
- ngui_push_clip(ui, r);
- if (checked) {
- ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
- ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
- }
- if (label) {
- if (utf8)
- ngui_text_utf8(ui, r.x + 22, ty, label, fg);
- else
- ngui_text(ui, r.x + 22, ty, label, fg);
- }
- if (shortcut) {
- int sw = (int)(utf8 ? ngui_text_width_utf8(ui, shortcut) :
- ngui_text_width_ui(ui, shortcut));
- if (utf8)
- ngui_text_utf8(ui, r.x + r.w - 8 - sw, ty, shortcut,
- fg);
- else
- ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
- }
- ngui_pop_clip(ui);
- if (hot)
- ui->hot = id;
- return fired;
- }
- int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
- const char *label, const char *shortcut, int checked)
- {
- return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 0);
- }
- int ngui_menu_item_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
- const char *label, const char *shortcut, int checked)
- {
- return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 1);
- }
- void ngui_popover_begin(struct ngui *ui, struct ngui_popover *p)
- {
- struct ngui_rect all = { 0, 0, ui->w, ui->h };
- ngui_fill_rect(ui, all, ui->theme.bg);
- p->body = rect_make(12, 12, ui->w - 24, ui->h - 12 - 34);
- p->footer = rect_make(12, ui->h - 30, ui->w - 24, 18);
- }
- void ngui_popover_footer(struct ngui *ui, const struct ngui_popover *p,
- const char *text, unsigned short color)
- {
- if (!p || !text || !text[0])
- return;
- ngui_push_clip(ui, p->footer);
- ngui_text_utf8(ui, p->footer.x, p->footer.y, text, color);
- ngui_pop_clip(ui);
- }
- int ngui_section(struct ngui *ui, struct ngui_rect r, const char *caption,
- unsigned int action_id, const char *action)
- {
- struct ngui_rect br;
- int fired = 0;
- int y = r.y + (r.h - 17) / 2;
- if (y < r.y)
- y = r.y;
- ngui_text_px(ui, r.x, y, caption, slen(caption), ui->theme.text_dim,
- 14, 1, 0, 0);
- if (action_id && action && action[0]) {
- int tw = (int)ngui_text_width_ui(ui, action);
- int bw = tw + 24;
- if (bw < 58)
- bw = 58;
- br = rect_make(r.x + r.w - bw, r.y + (r.h - 24) / 2, bw, 24);
- if (ngui_button(ui, action_id, br, action))
- fired = 1;
- }
- return fired;
- }
- /* Shared by the byte and UTF-8 variants: one hover/selection row with an
- * optional signal icon and right-aligned text. */
- static int ngui_list_row_common(struct ngui *ui, unsigned int id,
- struct ngui_rect r,
- const struct ngui_row *row, int utf8)
- {
- int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
- int press_hot = ui->pressed &&
- ngui_rect_contains(&r, ui->press_x, ui->press_y);
- int release_hot = ui->released &&
- ngui_rect_contains(&r, ui->release_x, ui->release_y);
- int fired = 0;
- int left = r.x + 10, right = r.x + r.w - 10;
- int title_h, block_h, ty;
- unsigned short bg = 0, fg, sub_fg, trail_fg;
- if (press_hot)
- ui->active = id;
- if (ui->pressed && ui->released && press_hot && release_hot)
- fired = 1;
- if (ui->released && ui->active == id) {
- if (release_hot)
- fired = 1;
- ui->active = 0;
- }
- if (row->selected)
- bg = ui->theme.selection;
- else if (hot || ui->active == id)
- bg = ui->theme.panel_alt;
- if (bg)
- ngui_fill_round_rect(ui, r, 6, 6, 6, 6, bg);
- title_h = ngui_text_line_height(ui, ui->font_px, 0);
- fg = ui->theme.text;
- sub_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
- trail_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
- if (row->selected) {
- int cy = r.y + r.h / 2;
- ngui_line(ui, left, cy, left + 3, cy + 4, fg);
- ngui_line(ui, left + 3, cy + 4, left + 11, cy - 5, fg);
- left += 17;
- }
- if (row->trailing && row->trailing[0]) {
- int tw = (int)ngui_text_width_ui(ui, row->trailing);
- right -= tw + 8;
- if (utf8)
- ngui_text_utf8(ui, right + 8,
- r.y + (r.h - title_h) / 2,
- row->trailing, trail_fg);
- else
- ngui_text(ui, right + 8,
- r.y + (r.h - title_h) / 2,
- row->trailing, trail_fg);
- }
- if (row->bars >= 0) {
- struct ngui_rect br;
- right -= 26;
- br.x = right + 6;
- br.y = r.y + (r.h - 14) / 2;
- br.w = 18;
- br.h = 14;
- ngui_signal_bars(ui, br, row->bars,
- row->selected ? ui->theme.text :
- ui->theme.accent);
- }
- if (right <= left)
- return fired;
- block_h = row->subtitle && row->subtitle[0] ? title_h * 2 - 2 :
- title_h;
- ty = r.y + (r.h - block_h) / 2;
- if (ty < r.y)
- ty = r.y;
- ngui_push_clip(ui, rect_make(left, r.y, right - left, r.h));
- if (utf8)
- ngui_text_utf8(ui, left, ty, row->title, fg);
- else
- ngui_text(ui, left, ty, row->title, fg);
- if (row->subtitle && row->subtitle[0]) {
- if (utf8)
- ngui_text_utf8(ui, left, ty + title_h - 2,
- row->subtitle, sub_fg);
- else
- ngui_text(ui, left, ty + title_h - 2, row->subtitle,
- sub_fg);
- }
- ngui_pop_clip(ui);
- if (hot)
- ui->hot = id;
- return fired;
- }
- int ngui_list_row(struct ngui *ui, unsigned int id, struct ngui_rect r,
- const struct ngui_row *row)
- {
- return ngui_list_row_common(ui, id, r, row, 0);
- }
- int ngui_list_row_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
- const struct ngui_row *row)
- {
- return ngui_list_row_common(ui, id, r, row, 1);
- }
- void ngui_signal_bars(struct ngui *ui, struct ngui_rect r, int bars,
- unsigned short color)
- {
- int i;
- int base = r.y + r.h - 1;
- for (i = 0; i < 4; i++) {
- int h = 3 + i * 3;
- struct ngui_rect br;
- br.x = r.x + i * 5;
- br.y = base - h;
- br.w = 3;
- br.h = h;
- ngui_fill_rect(ui, br, i < bars ? color : ui->theme.button);
- }
- }
- int ngui_tile(struct ngui *ui, unsigned int id, struct ngui_rect r,
- const char *title, const char *subtitle, int on)
- {
- int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
- int press_hot = ui->pressed &&
- ngui_rect_contains(&r, ui->press_x, ui->press_y);
- int release_hot = ui->released &&
- ngui_rect_contains(&r, ui->release_x, ui->release_y);
- int fired = 0;
- unsigned short bg, fg, sub_fg;
- if (press_hot)
- ui->active = id;
- if (ui->pressed && ui->released && press_hot && release_hot)
- fired = 1;
- if (ui->released && ui->active == id) {
- if (release_hot)
- fired = 1;
- ui->active = 0;
- }
- if (on)
- bg = ui->theme.selection;
- else if (hot || ui->active == id)
- bg = ui->theme.button_hot;
- else
- bg = ui->theme.panel_alt;
- ngui_fill_round_rect(ui, r, 10, 10, 10, 10, bg);
- fg = ui->theme.text;
- sub_fg = on ? ui->theme.text : ui->theme.text_dim;
- ngui_push_clip(ui, r);
- ngui_text_px(ui, r.x + 14, r.y + 8, title, slen(title), fg,
- 14, 1, 0, 0);
- if (subtitle && subtitle[0])
- ngui_text_utf8(ui, r.x + 14, r.y + 27, subtitle, sub_fg);
- ngui_pop_clip(ui);
- if (hot)
- ui->hot = id;
- return fired;
- }
- int ngui_slider(struct ngui *ui, unsigned int id, struct ngui_slider *s,
- struct ngui_rect r, int *value, int min, int max)
- {
- int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
- int press_hot = ui->pressed &&
- ngui_rect_contains(&r, ui->press_x, ui->press_y);
- int changed = 0;
- int track_y = r.y + r.h / 2 - 2;
- int knob_x, fill_w;
- if (!s || !value || max <= min)
- return 0;
- if (*value < min)
- *value = min;
- if (*value > max)
- *value = max;
- if (press_hot) {
- s->dragging = 1;
- ui->active = id;
- }
- if (s->dragging && (ui->buttons & 1)) {
- int rel = ui->mouse_x - r.x;
- int v;
- if (rel < 0)
- rel = 0;
- if (rel >= r.w)
- rel = r.w - 1;
- v = min + rel * (max - min) / (r.w > 1 ? r.w - 1 : 1);
- if (v != *value) {
- *value = v;
- changed = 1;
- }
- }
- if (ui->released) {
- s->dragging = 0;
- if (ui->active == id)
- ui->active = 0;
- }
- if (hot)
- ui->hot = id;
- ngui_fill_rect(ui, rect_make(r.x, track_y, r.w, 4),
- ui->theme.button);
- fill_w = (*value - min) * r.w / (max - min);
- if (fill_w > 0)
- ngui_fill_rect(ui, rect_make(r.x, track_y, fill_w, 4),
- ui->theme.accent);
- knob_x = r.x + (*value - min) * (r.w - 1) / (max - min);
- ngui_fill_round_rect(ui,
- rect_make(knob_x - 7, r.y + r.h / 2 - 7, 14, 14),
- 7, 7, 7, 7, ui->theme.text);
- return changed;
- }
- int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
- enum ngui_icon icon){
- int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
- int press_hot = ui->pressed &&
- ngui_rect_contains(&r, ui->press_x, ui->press_y);
- int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
- int fired = 0;
- unsigned short bg;
- if (press_hot)
- ui->active = id;
- if (ui->pressed && ui->released && press_hot && release_hot)
- fired = 1;
- if (ui->released && ui->active == id) {
- if (release_hot)
- fired = 1;
- ui->active = 0;
- }
- if (ui->active == id)
- bg = ui->theme.button_down;
- else if (hot)
- bg = ui->theme.button_hot;
- else
- bg = ui->theme.button;
- ngui_fill_rect(ui, r, bg);
- button_bevel(ui, r, bg, hot, ui->active == id);
- ngui_icon(ui, r, icon, ui->theme.text);
- if (hot)
- ui->hot = id;
- return fired;
- }
- void ngui_input_set(struct ngui_input *in, const char *s)
- {
- scopy(in->text, s, NGUI_INPUT_MAX);
- in->len = slen(in->text);
- in->cursor = in->len;
- in->scroll = 0;
- in->anchor = in->cursor;
- in->dragging = 0;
- in->last_click_ms = 0;
- }
- void ngui_input_value(const struct ngui_input *in, char *out,
- unsigned int max)
- {
- unsigned int i;
- if (!in || !out || !max)
- return;
- /* ngui_text_input() unmasks a secret field again after drawing, so
- * in->text is the real value between calls. */
- for (i = 0; in->text[i] && i + 1 < max; i++)
- out[i] = in->text[i];
- out[i] = '\0';
- }
- static int input_word_char(char c)
- {
- return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') || c == '_';
- }
- /* Next word start after `pos` (Windows edit-control semantics). */
- static unsigned int input_word_next(const struct ngui_input *in,
- unsigned int pos)
- {
- while (pos < in->len && input_word_char(in->text[pos]))
- pos++;
- while (pos < in->len && !input_word_char(in->text[pos]))
- pos++;
- return pos;
- }
- /* Start of the previous word before `pos`. */
- static unsigned int input_word_prev(const struct ngui_input *in,
- unsigned int pos)
- {
- while (pos > 0 && !input_word_char(in->text[pos - 1]))
- pos--;
- while (pos > 0 && input_word_char(in->text[pos - 1]))
- pos--;
- return pos;
- }
- /* End of the word to the right of `pos`. A caret inside a word takes the
- * rest of that word and its trailing separators; a caret on separators takes
- * them plus the next word. */
- static unsigned int input_word_delete_end(const struct ngui_input *in,
- unsigned int pos)
- {
- if (pos < in->len && input_word_char(in->text[pos])) {
- while (pos < in->len && input_word_char(in->text[pos]))
- pos++;
- while (pos < in->len && !input_word_char(in->text[pos]))
- pos++;
- return pos;
- }
- while (pos < in->len && !input_word_char(in->text[pos]))
- pos++;
- while (pos < in->len && input_word_char(in->text[pos]))
- pos++;
- return pos;
- }
- static void input_sel_bounds(const struct ngui_input *in,
- unsigned int *a, unsigned int *b)
- {
- if (in->anchor < in->cursor) {
- *a = in->anchor;
- *b = in->cursor;
- } else {
- *a = in->cursor;
- *b = in->anchor;
- }
- }
- /* Remove [a, b) and keep cursor/anchor consistent. Returns 1 when the text
- * changed. */
- static int input_delete_range(struct ngui_input *in, unsigned int a,
- unsigned int b)
- {
- unsigned int i;
- if (a >= b || b > in->len)
- return 0;
- for (i = b; i < in->len; i++)
- in->text[i - (b - a)] = in->text[i];
- in->len -= b - a;
- in->text[in->len] = '\0';
- if (in->cursor >= b)
- in->cursor -= b - a;
- else if (in->cursor > a)
- in->cursor = a;
- if (in->anchor >= b)
- in->anchor -= b - a;
- else if (in->anchor > a)
- in->anchor = a;
- return 1;
- }
- static int input_delete_selection(struct ngui_input *in)
- {
- unsigned int a, b;
- input_sel_bounds(in, &a, &b);
- return input_delete_range(in, a, b);
- }
- static void input_insert(struct ngui_input *in, char c)
- {
- unsigned int i;
- input_delete_selection(in);
- if (in->len + 1 >= NGUI_INPUT_MAX)
- return;
- i = in->len;
- while (i > in->cursor) {
- in->text[i] = in->text[i - 1];
- i--;
- }
- in->text[in->cursor] = c;
- in->cursor++;
- in->len++;
- in->text[in->len] = '\0';
- in->anchor = in->cursor;
- }
- static void input_delete(struct ngui_input *in, int back)
- {
- unsigned int pos;
- if (back) {
- if (in->cursor == 0)
- return;
- pos = in->cursor - 1;
- } else {
- if (in->cursor >= in->len)
- return;
- pos = in->cursor;
- }
- input_delete_range(in, pos, pos + 1);
- }
- static void input_move_to(struct ngui_input *in, unsigned int pos)
- {
- if (pos > in->len)
- pos = in->len;
- in->cursor = pos;
- in->anchor = pos;
- }
- /* Two presses in the same field within this window select all of its text. */
- #define NGUI_DOUBLE_CLICK_MS 500
- static int input_text_w(struct ngui *ui, const char *s, unsigned int n)
- {
- return (int)ngui_text_width_px(ui, s, n, ui->font_px, 0, 0, 0);
- }
- /* Character position under a content-relative x, clamped to the visible
- * part of the field. Proportional text means the prefix must be measured. */
- static unsigned int input_pos_at(struct ngui *ui, const struct ngui_input *in,
- int inner_x, int x)
- {
- unsigned int i, n, best = 0;
- int rel = x - inner_x;
- if (rel <= 0 || in->scroll >= in->len)
- return in->scroll;
- n = in->len - in->scroll;
- for (i = 1; i <= n; i++) {
- if (input_text_w(ui, in->text + in->scroll, i) > rel)
- break;
- best = i;
- }
- return in->scroll + best;
- }
- static void input_draw_selection(struct ngui *ui, const struct ngui_input *in,
- struct ngui_rect inner)
- {
- unsigned int a, b, va, vb;
- int xa, xb;
- input_sel_bounds(in, &a, &b);
- if (a == b || in->scroll >= in->len)
- return;
- va = a < in->scroll ? in->scroll : a;
- vb = b < in->len ? b : in->len;
- if (va >= vb)
- return;
- xa = input_text_w(ui, in->text + in->scroll, va - in->scroll);
- xb = input_text_w(ui, in->text + in->scroll, vb - in->scroll);
- if (xb > xa)
- ngui_fill_rect(ui, rect_make(inner.x + xa, inner.y,
- xb - xa,
- ngui_text_line_height(ui,
- ui->font_px,
- 0)),
- ui->theme.selection);
- }
- /* Scroll the first-visible-character index until the caret is visible. */
- static void input_scroll_to_cursor(struct ngui *ui, struct ngui_input *in,
- int inner_w)
- {
- if (in->scroll > in->len)
- in->scroll = in->len;
- if (in->cursor < in->scroll)
- in->scroll = in->cursor;
- if (inner_w <= 0) {
- in->scroll = in->len;
- return;
- }
- while (in->scroll < in->cursor &&
- input_text_w(ui, in->text + in->scroll,
- in->cursor - in->scroll) > inner_w)
- in->scroll++;
- if (in->scroll > in->len)
- in->scroll = in->len;
- }
- /* Secret fields are measured and painted from a '*' mask: the real text is
- * swapped out for everything except the edit loop, so the caret, selection
- * and click positions all follow the masked width. */
- static char secret_save[NGUI_INPUT_MAX];
- static void secret_mask(struct ngui_input *in)
- {
- unsigned int i;
- for (i = 0; i <= in->len && i < NGUI_INPUT_MAX; i++) {
- secret_save[i] = in->text[i];
- in->text[i] = i < in->len ? '*' : '\0';
- }
- }
- static void secret_unmask(struct ngui_input *in)
- {
- unsigned int i;
- for (i = 0; i <= in->len && i < NGUI_INPUT_MAX; i++) {
- in->text[i] = secret_save[i];
- secret_save[i] = 0;
- }
- }
- int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
- struct ngui_input *in)
- {
- struct ngui_rect inner;
- int focused = !in->disabled && (ui->focus == id);
- int result = 0;
- int avail, line_h, k;
- int changed = 0;
- line_h = ngui_text_line_height(ui, ui->font_px, 0);
- if (line_h < 1)
- line_h = 16;
- inner = rect_make(r.x + 4, r.y + (r.h - line_h) / 2, r.w - 8,
- line_h);
- if (inner.w < 0)
- inner.w = 0;
- if (inner.h < 1)
- inner.h = 1;
- avail = inner.w;
- if (in->secret)
- secret_mask(in);
- ngui_cursor_rect(ui, r, in->disabled ? CURSOR_NOT_ALLOWED : CURSOR_TEXT);
- if (in->disabled) {
- /* A field that became disabled while focused gives the focus
- * back, and a press cannot start a drag or a selection. */
- if (ui->focus == id)
- ngui_set_focus(ui, 0);
- in->dragging = 0;
- } else if (ui->pressed &&
- ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
- unsigned int now = u_ticks_ms();
- int dbl = in->last_click_ms &&
- (unsigned int)(now - in->last_click_ms) <=
- NGUI_DOUBLE_CLICK_MS;
- if (dbl) {
- /* Double click: select the whole text and keep it
- * selected while the second press is held. */
- in->anchor = 0;
- in->cursor = in->len;
- in->dragging = 0;
- } else {
- unsigned int pos = input_pos_at(ui, in, inner.x,
- ui->press_x);
- in->cursor = pos;
- in->anchor = pos;
- in->dragging = 1;
- }
- in->last_click_ms = now;
- ui->focus = id;
- ui->focus_claimed = 1;
- ui->last_blink = now;
- ui->caret_on = 1;
- focused = 1;
- } else if (ui->pressed) {
- in->dragging = 0;
- }
- /* Extend the selection while the button that started in this field is
- * still held. Sampled coordinates may be outside the canvas. The
- * release *event* is authoritative: the sampled button state can lag
- * the event queue by a frame. */
- if (!in->disabled && in->dragging && (ui->buttons & 1) &&
- !ui->released) {
- in->cursor = input_pos_at(ui, in, inner.x, ui->mouse_x);
- /* Keep redrawing when the pointer leaves the window. */
- ngui_invalidate(ui);
- }
- if (ui->released)
- in->dragging = 0;
- if (focused) {
- if (in->secret)
- secret_unmask(in);
- while ((k = ngui_key(ui)) != 0) {
- if (in->secret) {
- /* No word boundaries inside a password. */
- if (k == NGUI_KEY_CTRL_LEFT)
- k = NGUI_KEY_HOME;
- else if (k == NGUI_KEY_CTRL_RIGHT)
- k = NGUI_KEY_END;
- }
- if (k == '\n' || k == '\r') {
- result |= NGUI_INPUT_SUBMIT;
- break;
- }
- if (k == NGUI_KEY_ESC || k == NGUI_KEY_TAB) {
- result |= NGUI_INPUT_CANCEL;
- if (k == NGUI_KEY_TAB)
- result |= NGUI_INPUT_NEXT;
- ui->focus = 0;
- break;
- }
- if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
- if (!input_delete_selection(in))
- input_delete(in, 1);
- changed = 1;
- } else if (k == NGUI_KEY_DELETE) {
- if (!input_delete_selection(in))
- input_delete(in, 0);
- changed = 1;
- } else if (k == NGUI_KEY_CTRL_BACKSPACE) {
- if (input_delete_selection(in))
- changed = 1;
- else
- changed = input_delete_range(in,
- input_word_prev(in, in->cursor),
- in->cursor);
- } else if (k == NGUI_KEY_CTRL_DELETE) {
- if (input_delete_selection(in)) {
- changed = 1;
- } else {
- unsigned int a = in->cursor;
- unsigned int b = input_word_delete_end(in,
- a);
- /* At the end of the line, delete the word
- * before the caret instead of doing
- * nothing. */
- if (b == a) {
- a = input_word_prev(in, in->cursor);
- b = in->cursor;
- }
- changed = input_delete_range(in, a, b);
- }
- } else if (k == NGUI_KEY_LEFT) {
- input_move_to(in, in->cursor > 0 ? in->cursor - 1 : 0);
- } else if (k == NGUI_KEY_RIGHT) {
- input_move_to(in, in->cursor < in->len
- ? in->cursor + 1 : in->len);
- } else if (k == NGUI_KEY_CTRL_LEFT) {
- input_move_to(in, input_word_prev(in, in->cursor));
- } else if (k == NGUI_KEY_CTRL_RIGHT) {
- input_move_to(in, input_word_next(in, in->cursor));
- } else if (k == NGUI_KEY_HOME) {
- input_move_to(in, 0);
- } else if (k == NGUI_KEY_END) {
- input_move_to(in, in->len);
- } else if (k == 0x01) { /* Ctrl+A: select all */
- in->anchor = 0;
- in->cursor = in->len;
- } else if (k == 0x15) { /* Ctrl+U */
- in->len = 0;
- in->cursor = 0;
- in->anchor = 0;
- in->text[0] = '\0';
- changed = 1;
- } else if (k >= 0x20 && k <= 0x7E) {
- input_insert(in, (char)k);
- changed = 1;
- }
- }
- if (in->secret)
- secret_mask(in);
- }
- ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
- /* Inset edge: the shadow sits on the top/left, the light on the
- * bottom/right. A focused field keeps the shape in accent blue. */
- if (focused)
- bevel_rect(ui, r, ui->theme.accent,
- shade565(ui->theme.accent, 4));
- else
- bevel_rect(ui, r, shade565(ui->theme.panel, -4),
- shade565(ui->theme.panel, 4));
- if (!in->disabled)
- input_scroll_to_cursor(ui, in, avail);
- ngui_push_clip(ui, inner);
- if (in->len == 0 && in->placeholder) {
- ngui_text_px(ui, inner.x, inner.y, in->placeholder,
- slen(in->placeholder), ui->theme.text_dim,
- ui->font_px, 0, 0, 0);
- } else {
- if (focused)
- input_draw_selection(ui, in, inner);
- ngui_text_px(ui, inner.x, inner.y, in->text + in->scroll,
- slen(in->text + in->scroll),
- in->disabled ? ui->theme.text_dim :
- ui->theme.text,
- ui->font_px, 0, 0, 0);
- }
- if (focused && ui->caret_on) {
- int cx = 0;
- if (in->cursor > in->scroll)
- cx = input_text_w(ui, in->text + in->scroll,
- in->cursor - in->scroll);
- if (cx < 0)
- cx = 0;
- if (avail > 0 && cx >= avail)
- cx = avail - 1;
- if (avail > 0 && cx < avail)
- ngui_fill_rect(ui, rect_make(inner.x + cx, inner.y,
- 1, inner.h),
- ui->theme.accent);
- }
- ngui_pop_clip(ui);
- if (in->secret)
- secret_unmask(in);
- if (changed)
- result |= NGUI_INPUT_CHANGED;
- return result;
- }
- static int scrollable(const struct ngui_scroll *s)
- {
- return s->content_h > s->view_h && s->view_h > 0;
- }
- static int scroll_thumb_h(const struct ngui_rect *r,
- const struct ngui_scroll *s)
- {
- int h = 0;
- if (s->content_h > 0)
- h = r->h * s->view_h / s->content_h;
- if (h < 12)
- h = 12;
- if (h > r->h)
- h = r->h;
- return h;
- }
- static int scroll_thumb_y(const struct ngui_rect *r,
- const struct ngui_scroll *s)
- {
- int th = scroll_thumb_h(r, s);
- int avail = r->h - th;
- int max = s->content_h - s->view_h;
- if (avail <= 0 || max <= 0)
- return r->y;
- return r->y + s->offset * avail / max;
- }
- static void scroll_clamp(struct ngui_scroll *s)
- {
- int max = s->content_h - s->view_h;
- if (max < 0)
- max = 0;
- if (s->offset > max)
- s->offset = max;
- if (s->offset < 0)
- s->offset = 0;
- }
- void ngui_scroll_to(struct ngui_scroll *s, int offset)
- {
- s->offset = offset;
- scroll_clamp(s);
- }
- void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
- {
- if (y < s->offset)
- s->offset = y;
- else if (y + h > s->offset + s->view_h)
- s->offset = y + h - s->view_h;
- scroll_clamp(s);
- }
- void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
- struct ngui_scroll *s)
- {
- struct ngui_rect bar;
- int scrolls = scrollable(s);
- s->view_h = r.h;
- scroll_clamp(s);
- if (!ui->buttons)
- s->dragging = 0;
- if (s->hide_bar)
- s->dragging = 0;
- if (!s->hide_bar && scrolls) {
- int th = scroll_thumb_h(&r, s);
- int ty = scroll_thumb_y(&r, s);
- bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
- if (ui->pressed &&
- ngui_rect_contains(&bar, ui->press_x, ui->press_y)) {
- if (ui->press_y >= ty && ui->press_y < ty + th) {
- s->dragging = 1;
- s->drag_dy = ui->press_y - ty;
- } else {
- int rel = ui->press_y - r.y - th / 2;
- int avail = r.h - th;
- int max = s->content_h - s->view_h;
- if (rel < 0)
- rel = 0;
- if (rel > avail)
- rel = avail;
- if (avail > 0)
- s->offset = rel * max / avail;
- s->dragging = 1;
- s->drag_dy = th / 2;
- scroll_clamp(s);
- }
- }
- /* Sampled buttons are authoritative while held: some event paths
- * deliver the press late or coalesce it with the release. Start
- * the drag from the current pointer position as well. */
- if (!s->dragging && (ui->buttons & 1) &&
- ngui_rect_contains(&bar, ui->mouse_x, ui->mouse_y)) {
- if (ui->mouse_y >= ty && ui->mouse_y < ty + th) {
- s->dragging = 1;
- s->drag_dy = ui->mouse_y - ty;
- } else {
- int rel = ui->mouse_y - r.y - th / 2;
- int avail = r.h - th;
- int max = s->content_h - s->view_h;
- if (rel < 0)
- rel = 0;
- if (rel > avail)
- rel = avail;
- if (avail > 0)
- s->offset = rel * max / avail;
- s->dragging = 1;
- s->drag_dy = th / 2;
- scroll_clamp(s);
- }
- }
- if (s->dragging && (ui->buttons & 1)) {
- int rel = ui->mouse_y - r.y - s->drag_dy;
- int avail = r.h - th;
- int max = s->content_h - s->view_h;
- if (rel < 0)
- rel = 0;
- if (rel > avail)
- rel = avail;
- if (avail > 0)
- s->offset = rel * max / avail;
- scroll_clamp(s);
- }
- if (ui->released)
- s->dragging = 0;
- }
- ngui_push_clip(ui, r);
- }
- void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
- struct ngui_scroll *s)
- {
- ngui_pop_clip(ui);
- if (s->hide_bar || !scrollable(s))
- return;
- {
- struct ngui_rect bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
- int th = scroll_thumb_h(&r, s);
- int ty = scroll_thumb_y(&r, s);
- struct ngui_rect thumb = rect_make(bar.x, ty, 14, th);
- ngui_fill_rect(ui, bar, ui->theme.panel_alt);
- ngui_fill_rect(ui, thumb, s->dragging ?
- ui->theme.accent : ui->theme.text_dim);
- }
- }
- int ngui_scroll_wheel(struct ngui *ui, struct ngui_rect r,
- struct ngui_scroll *s)
- {
- if (!ui->scroll_y ||
- !ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y))
- return 0;
- ngui_scroll_to(s, s->offset + ui->scroll_y);
- ui->scroll_y = 0;
- return 1;
- }
- int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s)
- {
- int k = ngui_key_peek(ui);
- int page;
- if (k != NGUI_KEY_PAGE_UP && k != NGUI_KEY_PAGE_DOWN)
- return 0;
- page = s->view_h - 22;
- if (page < 22)
- page = 22;
- ngui_key(ui);
- if (k == NGUI_KEY_PAGE_UP)
- s->offset -= page;
- else
- s->offset += page;
- scroll_clamp(s);
- return 1;
- }
|