hi, this is a WIP patch for mothra. it provides better editing of URL and text entries: tick, moving around, selecting with the mouse, etc. -- Sigrid diff -r 02e3059af5bc sys/src/cmd/mothra/libpanel/draw.c --- a/sys/src/cmd/mothra/libpanel/draw.c Thu Feb 11 09:37:36 2021 +0100 +++ b/sys/src/cmd/mothra/libpanel/draw.c Fri Feb 12 16:19:10 2021 +0100 @@ -13,8 +13,8 @@ #define CKWID 1 /* width of frame around check mark */ #define CKINSET 1 /* space around check mark frame */ #define CKBORDER 2 /* space around X inside frame */ -static Image *pl_white, *pl_light, *pl_dark, *pl_black, *pl_hilit; -Image *pl_blue; +static Image *pl_light, *pl_dark; +Image *pl_blue, *pl_white, *pl_black, *pl_tick, *pl_hilit; int pl_drawinit(void){ pl_white=allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xFFFFFFFF); pl_light=allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xFFFFFFFF); @@ -22,7 +22,13 @@ pl_black=allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x000000FF); pl_hilit=allocimage(display, Rect(0,0,1,1), CHAN1(CAlpha,8), 1, 0x80); pl_blue=allocimage(display, Rect(0,0,1,1), RGB24, 1, 0x0000FFFF); - if(pl_white==0 || pl_light==0 || pl_black==0 || pl_dark==0 || pl_blue==0) sysfatal("allocimage: %r"); + if((pl_tick = allocimage(display, Rect(0, 0, TICKW, font->height), screen->chan, 0, DNofill)) != nil){ + draw(pl_tick, pl_tick->r, pl_white, nil, ZP); + draw(pl_tick, Rect(TICKW/2, 0, TICKW/2+1, font->height), pl_black, nil, ZP); + draw(pl_tick, Rect(0, 0, TICKW, TICKW), pl_black, nil, ZP); + draw(pl_tick, Rect(0, font->height-TICKW, TICKW, font->height), pl_black, nil, ZP); + } + if(pl_white==0 || pl_light==0 || pl_black==0 || pl_dark==0 || pl_blue==0 || pl_tick==0) sysfatal("allocimage: %r"); return 1; } Rectangle pl_boxoutline(Image *b, Rectangle r, int style, int fill){ @@ -227,6 +233,9 @@ void pl_highlight(Image *b, Rectangle r){ draw(b, r, pl_dark, pl_hilit, ZP); } +void pl_drawtick(Image *b, Rectangle r){ + draw(b, r, pl_tick, nil, ZP); +} void pl_clr(Image *b, Rectangle r){ draw(b, r, display->white, 0, ZP); } diff -r 02e3059af5bc sys/src/cmd/mothra/libpanel/entry.c --- a/sys/src/cmd/mothra/libpanel/entry.c Thu Feb 11 09:37:36 2021 +0100 +++ b/sys/src/cmd/mothra/libpanel/entry.c Fri Feb 12 16:19:10 2021 +0100 @@ -9,12 +9,24 @@ typedef struct Entry Entry; struct Entry{ char *entry; - char *entp; - char *eent; + int sz; + int n; + int a; + int b; + int off; void (*hit)(Panel *, char *); Point minsize; }; #define SLACK 7 /* enough for one extra rune and ◀ and a nul */ +void pl_cutentry(Panel *p){ + Entry *ep; + + ep=p->data; + memmove(ep->entry+ep->a, ep->entry+ep->b, ep->n-ep->b); + ep->n -= ep->b-ep->a; + ep->entry[ep->n]=0; + ep->b=ep->a; +} char *pl_snarfentry(Panel *p){ Entry *ep; int n; @@ -22,27 +34,74 @@ if(p->flags&USERFL) /* no snarfing from password entry */ return nil; ep=p->data; - n=utfnlen(ep->entry, ep->entp-ep->entry); + n=utfnlen(ep->entry+ep->a, ep->b-ep->a); if(n<1) return nil; - return smprint("%.*s", n, ep->entry); + return smprint("%.*s", n, ep->entry+ep->a); } void pl_pasteentry(Panel *p, char *s){ Entry *ep; - char *e; - int n, m; + int m; ep=p->data; - n=ep->entp-ep->entry; m=strlen(s); - e=pl_erealloc(ep->entry,n+m+SLACK); - ep->entry=e; - e+=n; - strncpy(e, s, m); - e+=m; - *e='\0'; - ep->entp=ep->eent=e; + ep->sz=ep->n+m+100+SLACK; + ep->entry=pl_erealloc(ep->entry,ep->sz); + memmove(ep->entry+ep->a+m, ep->entry+ep->b, ep->n-ep->b); + memmove(ep->entry+ep->a, s, m); + ep->n+=m-(ep->b-ep->a); + ep->a+=m; + ep->b=ep->a; + ep->entry[ep->n]=0; pldraw(p, p->b); } +static void drawentry(Panel *p, Rectangle r, char *s){ + Rectangle save; + Point ul; + Entry *ep; + Image *b; + char c; + int toff, offs; + + ep=p->data; + b=p->b; + ul=r.min; + offs=Dx(r) - stringwidth(font, s); + if(offs > 0) + offs = 0; + ul.x += offs; + toff = 0; + if(plkbfocus == p){ + c = s[ep->a]; + s[ep->a] = 0; + toff = stringwidth(font, s); + s[ep->a] = c; + } + if(ul.x+toff+TICKW > r.max.x) + ul.x -= TICKW; + if(ul.x+toff < r.min.x) + ul.x = r.min.x - toff; + if(ep->off+toff < r.min.x || ep->off+toff > r.max.x-TICKW) + ep->off = ul.x; + else + ul.x = ep->off; + save=b->clipr; + if(!rectclip(&r, save)) + return; + replclipr(b, b->repl, r); + string(b, ul, pl_black, ZP, font, s); + if(plkbfocus == p){ + r.min.x = ep->off+toff; + if(ep->a != ep->b){ + c = s[ep->b]; + s[ep->b] = 0; + r.max.x = ep->off+stringwidth(font, s); + s[ep->b] = c; + pl_highlight(b, r); + }else + pl_drawtick(b, r); + } + replclipr(b, b->repl, save); +} void pl_drawentry(Panel *p){ Rectangle r; Entry *ep; @@ -57,18 +116,24 @@ for(p=s; *p; p++) *p='*'; } - if(stringwidth(font, s)<=r.max.x-r.min.x) - pl_drawicon(p->b, r, PLACEW, 0, s); - else - pl_drawicon(p->b, r, PLACEE, 0, s); + drawentry(p, r, s); if(s != ep->entry) free(s); } int pl_hitentry(Panel *p, Mouse *m){ - if((m->buttons&7)==1){ + Entry *ep; + int i, n, oldb; + oldb = m->buttons&7; + if(oldb==1){ + if(plkbfocus != p) + p->state=DOWN; plgrabkb(p); - - p->state=DOWN; + ep = p->data; + for(i = 1; i <= ep->n; i++) + if(stringnwidth(font, ep->entry, i) > m->xy.x-ep->off) + break; + n = i-1; + ep->a = ep->b = n; pldraw(p, p->b); while(m->buttons&1){ int old; @@ -76,21 +141,32 @@ if(display->bufp > display->buf) flushimage(display, 1); *m=emouse(); + p->state=UP; if((old&7)==1){ if((m->buttons&7)==3){ - Entry *ep; - plsnarf(p); - - /* cut */ - ep=p->data; - ep->entp=ep->entry; - *ep->entp='\0'; + pl_cutentry(p); + pldraw(p, p->b); + ep->b = n = ep->a; + } + if(oldb==1 && (m->buttons&7)==1){ + p->state=UP; + for(i = 0; i < ep->n; i++) + if(stringnwidth(font, ep->entry, i)+TICKW*2 > m->xy.x-ep->off) + break; + if(i <= n){ + ep->b = n; + ep->a = i; + }else if(i >= n){ + ep->a = n; + ep->b = i; + } pldraw(p, p->b); } if((m->buttons&7)==5) plpaste(p); - } + }else + oldb=old&7; } p->state=UP; pldraw(p, p->b); @@ -100,43 +176,74 @@ void pl_typeentry(Panel *p, Rune c){ int n; Entry *ep; + char s[UTFmax]; ep=p->data; switch(c){ case '\n': case '\r': - *ep->entp='\0'; if(ep->hit) ep->hit(p, ep->entry); return; + case Kleft: + while(ep->a!=0 && !pl_rune1st(ep->entry[ep->a-1])) + --ep->a; + if(ep->a!=0) + --ep->a; + ep->b=ep->a; + break; + case Kright: + if(ep->an) + ep->b = (ep->a += chartorune(&c, ep->entry+ep->a)); + break; + case Ksoh: + ep->a=ep->b=0; + break; + case Kenq: + ep->a=ep->b=ep->n; + break; case Kesc: + ep->a=0; + ep->b=ep->n; plsnarf(p); /* no break */ case Kdel: /* clear */ + ep->a = ep->b = ep->n = 0; + *ep->entry = 0; + break; case Knack: /* ^U: erase line */ - ep->entp=ep->entry; - *ep->entp='\0'; + ep->a = 0; + pl_cutentry(p); break; case Kbs: /* ^H: erase character */ - while(ep->entp!=ep->entry && !pl_rune1st(ep->entp[-1])) *--ep->entp='\0'; - if(ep->entp!=ep->entry) *--ep->entp='\0'; - break; + if(ep->a==ep->b){ + while(ep->a>0 && !pl_rune1st(ep->entry[ep->a-1])) + --ep->a; + if(ep->a>0) + --ep->a; + } + /* wet floor */ + if(0){ case Ketb: /* ^W: erase word */ - while(ep->entp!=ep->entry && !pl_idchar(ep->entp[-1])) - --ep->entp; - while(ep->entp!=ep->entry && pl_idchar(ep->entp[-1])) - --ep->entp; - *ep->entp='\0'; + while(ep->a>0 && !pl_idchar(ep->entry[ep->a-1])) + --ep->a; + while(ep->a>0 && pl_idchar(ep->entry[ep->a-1])) + --ep->a; + } + pl_cutentry(p); break; default: if(c < 0x20 || (c & 0xFF00) == KF || (c & 0xFF00) == Spec) break; - ep->entp+=runetochar(ep->entp, &c); - if(ep->entp>ep->eent){ - n=ep->entp-ep->entry; - ep->entry=pl_erealloc(ep->entry, n+100+SLACK); - ep->entp=ep->entry+n; - ep->eent=ep->entp+100; + n=runetochar(s, &c); + memmove(ep->entry+ep->a+n, ep->entry+ep->b, ep->n-ep->b); + memmove(ep->entry+ep->a, s, n); + ep->n+=n-(ep->b-ep->a); + ep->a+=n; + ep->b=ep->a; + if(ep->n>ep->sz){ + ep->sz = ep->n+100; + ep->entry=pl_erealloc(ep->entry, ep->sz+SLACK); } - *ep->entp='\0'; + ep->entry[ep->n]=0; break; } pldraw(p, p->b); @@ -152,10 +259,9 @@ Entry *ep; ep = p->data; free(ep->entry); - ep->entry = ep->eent = ep->entp = 0; + ep->entry = nil; } void plinitentry(Panel *v, int flags, int wid, char *str, void (*hit)(Panel *, char *)){ - int elen; Entry *ep; ep=v->data; v->flags=flags|LEAF; @@ -169,12 +275,11 @@ v->free=pl_freeentry; v->snarf=pl_snarfentry; v->paste=pl_pasteentry; - elen=100; - if(str) elen+=strlen(str); - ep->entry=pl_erealloc(ep->entry, elen+SLACK); - ep->eent=ep->entry+elen; - strecpy(ep->entry, ep->eent, str ? str : ""); - ep->entp=ep->entry+strlen(ep->entry); + ep->sz=100; + if(str) ep->sz+=strlen(str); + ep->entry=pl_erealloc(ep->entry, ep->sz+SLACK); + strecpy(ep->entry, ep->entry+ep->sz, str ? str : ""); + ep->n=ep->b=ep->a=strlen(ep->entry); ep->hit=hit; v->kind="entry"; } @@ -187,6 +292,5 @@ char *plentryval(Panel *p){ Entry *ep; ep=p->data; - *ep->entp='\0'; return ep->entry; } diff -r 02e3059af5bc sys/src/cmd/mothra/libpanel/event.c --- a/sys/src/cmd/mothra/libpanel/event.c Thu Feb 11 09:37:36 2021 +0100 +++ b/sys/src/cmd/mothra/libpanel/event.c Fri Feb 12 16:19:10 2021 +0100 @@ -6,6 +6,11 @@ #include "pldefs.h" void plgrabkb(Panel *g){ + Panel *o; + o=plkbfocus; + plkbfocus=nil; + if(o && o!=g) /* redraw if lost focus */ + pldraw(o, o->b); plkbfocus=g; } void plkeyboard(Rune c){ diff -r 02e3059af5bc sys/src/cmd/mothra/libpanel/pldefs.h --- a/sys/src/cmd/mothra/libpanel/pldefs.h Thu Feb 11 09:37:36 2021 +0100 +++ b/sys/src/cmd/mothra/libpanel/pldefs.h Fri Feb 12 16:19:10 2021 +0100 @@ -13,6 +13,7 @@ #define LEAF 0x10000 /* newpanel will refuse to attach children */ #define INVIS 0x20000 /* don't draw this */ #define REMOUSE 0x40000 /* send next mouse event here, even if not inside */ +#define TICKW 3 /* tick width */ /* * States, also styles */ @@ -40,7 +41,7 @@ SCROLLABSX, }; -extern Image *pl_blue; +extern Image *pl_blue, *pl_white, *pl_black; /* * Scrollbar, slider orientations @@ -58,6 +59,7 @@ * Drawing primitives */ int pl_drawinit(void); +void pl_entry(Panel *, Rectangle, int, char *, int, int); Rectangle pl_box(Image *, Rectangle, int); Rectangle pl_outline(Image *, Rectangle, int); Point pl_boxsize(Point, int); @@ -70,6 +72,7 @@ void pl_invis(Panel *, int); Point pl_iconsize(int, Icon *); void pl_highlight(Image *, Rectangle); +void pl_drawtick(Image *, Rectangle); void pl_clr(Image *, Rectangle); void pl_fill(Image *, Rectangle); void pl_cpy(Image *, Point, Rectangle); diff -r 02e3059af5bc sys/src/cmd/mothra/mothra.c --- a/sys/src/cmd/mothra/mothra.c Thu Feb 11 09:37:36 2021 +0100 +++ b/sys/src/cmd/mothra/mothra.c Fri Feb 12 16:19:10 2021 +0100 @@ -399,6 +399,7 @@ case Ekeyboard: switch(e.kbdc){ default: +Plkey: adjkb(); plkeyboard(e.kbdc); break; @@ -424,9 +425,13 @@ search(); break; case Kright: + if(plkbfocus) + goto Plkey; sidescroll(text->size.x/4, 1); break; case Kleft: + if(plkbfocus) + goto Plkey; sidescroll(-text->size.x/4, 1); break; } @@ -441,6 +446,8 @@ break; } plmouse(root, &mouse); + if(mouse.buttons == 1 && root->lastmouse == root) + plgrabkb(nil); break; case Eplumb: pm=e.v; diff -r 02e3059af5bc sys/src/cmd/mothra/rdhtml.c --- a/sys/src/cmd/mothra/rdhtml.c Thu Feb 11 09:37:36 2021 +0100 +++ b/sys/src/cmd/mothra/rdhtml.c Fri Feb 12 16:19:10 2021 +0100 @@ -374,7 +374,7 @@ /* * Skip over white space */ -char *pl_white(char *s){ +char *pl_whitespace(char *s){ while(*s==' ' || *s=='\t' || *s=='\n' || *s=='\r') s++; return s; } @@ -431,19 +431,19 @@ g->tag=tagp-tag; if(g->tag==Tag_end) htmlerror(g->name, g->lineno, "no tag %s", name); for(;;){ - s=pl_white(s); + s=pl_whitespace(s); if(*s=='\0'){ ap->name=0; return; } ap->name=s; s=pl_word(s); - t=pl_white(s); + t=pl_whitespace(s); c=*t; *s='\0'; for(s=ap->name;*s;s++) if('A'<=*s && *s<='Z') *s+='a'-'A'; if(c=='='){ - s=pl_white(t+1); + s=pl_whitespace(t+1); if(*s=='\'' || *s=='"'){ ap->value=s+1; s=pl_quote(s);