diff options
author | rsc <devnull@localhost> | 2003-09-30 17:47:43 +0000 |
---|---|---|
committer | rsc <devnull@localhost> | 2003-09-30 17:47:43 +0000 |
commit | b5990708483a1fa452b3d4b6ed2bfa998deeeb39 (patch) | |
tree | 9d2413286307f7e29a1906ebe4d1b59477ff886d /src/libframe | |
parent | 76193d7cb0457807b2f0b95f909ab5de19480cd7 (diff) | |
download | plan9port-b5990708483a1fa452b3d4b6ed2bfa998deeeb39.tar.gz plan9port-b5990708483a1fa452b3d4b6ed2bfa998deeeb39.tar.bz2 plan9port-b5990708483a1fa452b3d4b6ed2bfa998deeeb39.zip |
Initial import.
Diffstat (limited to 'src/libframe')
-rw-r--r-- | src/libframe/frdelete.c | 122 | ||||
-rw-r--r-- | src/libframe/frinit.c | 83 | ||||
-rw-r--r-- | src/libframe/frinsert.c | 285 | ||||
-rw-r--r-- | src/libframe/frptofchar.c | 115 | ||||
-rw-r--r-- | src/libframe/frselect.c | 132 | ||||
-rw-r--r-- | src/libframe/frstr.c | 37 | ||||
-rw-r--r-- | src/libframe/frutil.c | 111 |
7 files changed, 885 insertions, 0 deletions
diff --git a/src/libframe/frdelete.c b/src/libframe/frdelete.c new file mode 100644 index 00000000..04ef6a09 --- /dev/null +++ b/src/libframe/frdelete.c @@ -0,0 +1,122 @@ +#include <u.h> +#include <libc.h> +#include <draw.h> +#include <mouse.h> +#include <frame.h> + +int +frdelete(Frame *f, ulong p0, ulong p1) +{ + Point pt0, pt1, ppt0; + Frbox *b; + int n0, n1, n; + ulong cn1; + Rectangle r; + int nn0; + Image *col; + + if(p0>=f->nchars || p0==p1 || f->b==nil) + return 0; + if(p1 > f->nchars) + p1 = f->nchars; + n0 = _frfindbox(f, 0, 0, p0); + if(n0 == f->nbox) + drawerror(f->display, "off end in frdelete"); + n1 = _frfindbox(f, n0, p0, p1); + pt0 = _frptofcharnb(f, p0, n0); + pt1 = frptofchar(f, p1); + if(f->p0 == f->p1) + frtick(f, frptofchar(f, f->p0), 0); + nn0 = n0; + ppt0 = pt0; + _frfreebox(f, n0, n1-1); + f->modified = 1; + + /* + * Invariants: + * - pt0 points to beginning, pt1 points to end + * - n0 is box containing beginning of stuff being deleted + * - n1, b are box containing beginning of stuff to be kept after deletion + * - cn1 is char position of n1 + * - f->p0 and f->p1 are not adjusted until after all deletion is done + */ + b = &f->box[n1]; + cn1 = p1; + while(pt1.x!=pt0.x && n1<f->nbox){ + _frcklinewrap0(f, &pt0, b); + _frcklinewrap(f, &pt1, b); + n = _frcanfit(f, pt0, b); + if(n==0) + drawerror(f->display, "_frcanfit==0"); + r.min = pt0; + r.max = pt0; + r.max.y += f->font->height; + if(b->nrune > 0){ + if(n != b->nrune){ + _frsplitbox(f, n1, n); + b = &f->box[n1]; + } + r.max.x += b->wid; + draw(f->b, r, f->b, nil, pt1); + cn1 += b->nrune; + }else{ + r.max.x += _frnewwid0(f, pt0, b); + if(r.max.x > f->r.max.x) + r.max.x = f->r.max.x; + col = f->cols[BACK]; + if(f->p0<=cn1 && cn1<f->p1) + col = f->cols[HIGH]; + draw(f->b, r, col, nil, pt0); + cn1++; + } + _fradvance(f, &pt1, b); + pt0.x += _frnewwid(f, pt0, b); + f->box[n0++] = f->box[n1++]; + b++; + } + if(n1==f->nbox && pt0.x!=pt1.x) /* deleting last thing in window; must clean up */ + frselectpaint(f, pt0, pt1, f->cols[BACK]); + if(pt1.y != pt0.y){ + Point pt2; + + pt2 = _frptofcharptb(f, 32767, pt1, n1); + if(pt2.y > f->r.max.y) + drawerror(f->display, "frptofchar in frdelete"); + if(n1 < f->nbox){ + int q0, q1, q2; + + q0 = pt0.y+f->font->height; + q1 = pt1.y+f->font->height; + q2 = pt2.y+f->font->height; + if(q2 > f->r.max.y) + q2 = f->r.max.y; + draw(f->b, Rect(pt0.x, pt0.y, pt0.x+(f->r.max.x-pt1.x), q0), + f->b, nil, pt1); + draw(f->b, Rect(f->r.min.x, q0, f->r.max.x, q0+(q2-q1)), + f->b, nil, Pt(f->r.min.x, q1)); + frselectpaint(f, Pt(pt2.x, pt2.y-(pt1.y-pt0.y)), pt2, f->cols[BACK]); + }else + frselectpaint(f, pt0, pt2, f->cols[BACK]); + } + _frclosebox(f, n0, n1-1); + if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=(int)f->r.min.x){ + --nn0; + ppt0.x -= f->box[nn0].wid; + } + _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0); + if(f->p1 > p1) + f->p1 -= p1-p0; + else if(f->p1 > p0) + f->p1 = p0; + if(f->p0 > p1) + f->p0 -= p1-p0; + else if(f->p0 > p0) + f->p0 = p0; + f->nchars -= p1-p0; + if(f->p0 == f->p1) + frtick(f, frptofchar(f, f->p0), 1); + pt0 = frptofchar(f, f->nchars); + n = f->nlines; + f->nlines = (pt0.y-f->r.min.y)/f->font->height+(pt0.x>f->r.min.x); + return n - f->nlines; +} diff --git a/src/libframe/frinit.c b/src/libframe/frinit.c new file mode 100644 index 00000000..c178ede1 --- /dev/null +++ b/src/libframe/frinit.c @@ -0,0 +1,83 @@ +#include <u.h> +#include <libc.h> +#include <draw.h> +#include <mouse.h> +#include <frame.h> + +void +frinit(Frame *f, Rectangle r, Font *ft, Image *b, Image *cols[NCOL]) +{ + f->font = ft; + f->display = b->display; + f->maxtab = 8*stringwidth(ft, "0"); + f->nbox = 0; + f->nalloc = 0; + f->nchars = 0; + f->nlines = 0; + f->p0 = 0; + f->p1 = 0; + f->box = 0; + f->lastlinefull = 0; + if(cols != 0) + memmove(f->cols, cols, sizeof f->cols); + frsetrects(f, r, b); + if(f->tick==nil && f->cols[BACK]!=0) + frinittick(f); +} + +void +frinittick(Frame *f) +{ + Image *b; + Font *ft; + + b = f->display->screenimage; + ft = f->font; + if(f->tick) + freeimage(f->tick); + f->tick = allocimage(f->display, Rect(0, 0, FRTICKW, ft->height), b->chan, 0, DWhite); + if(f->tick == nil) + return; + if(f->tickback) + freeimage(f->tickback); + f->tickback = allocimage(f->display, f->tick->r, b->chan, 0, DWhite); + if(f->tickback == 0){ + freeimage(f->tick); + f->tick = 0; + return; + } + /* background color */ + draw(f->tick, f->tick->r, f->cols[BACK], nil, ZP); + /* vertical line */ + draw(f->tick, Rect(FRTICKW/2, 0, FRTICKW/2+1, ft->height), f->display->black, nil, ZP); + /* box on each end */ + draw(f->tick, Rect(0, 0, FRTICKW, FRTICKW), f->cols[TEXT], nil, ZP); + draw(f->tick, Rect(0, ft->height-FRTICKW, FRTICKW, ft->height), f->cols[TEXT], nil, ZP); +} + +void +frsetrects(Frame *f, Rectangle r, Image *b) +{ + f->b = b; + f->entire = r; + f->r = r; + f->r.max.y -= (r.max.y-r.min.y)%f->font->height; + f->maxlines = (r.max.y-r.min.y)/f->font->height; +} + +void +frclear(Frame *f, int freeall) +{ + if(f->nbox) + _frdelbox(f, 0, f->nbox-1); + if(f->box) + free(f->box); + if(freeall){ + freeimage(f->tick); + freeimage(f->tickback); + f->tick = 0; + f->tickback = 0; + } + f->box = 0; + f->ticked = 0; +} diff --git a/src/libframe/frinsert.c b/src/libframe/frinsert.c new file mode 100644 index 00000000..8ca39fcf --- /dev/null +++ b/src/libframe/frinsert.c @@ -0,0 +1,285 @@ +#include <u.h> +#include <libc.h> +#include <draw.h> +#include <mouse.h> +#include <frame.h> + +#define DELTA 25 +#define TMPSIZE 256 +static Frame frame; + +static +Point +bxscan(Frame *f, Rune *sp, Rune *ep, Point *ppt) +{ + int w, c, nb, delta, nl, nr, rw; + Frbox *b; + char *s, tmp[TMPSIZE+3]; /* +3 for rune overflow */ + uchar *p; + + frame.r = f->r; + frame.b = f->b; + frame.font = f->font; + frame.maxtab = f->maxtab; + frame.nbox = 0; + frame.nchars = 0; + memmove(frame.cols, f->cols, sizeof frame.cols); + delta = DELTA; + nl = 0; + for(nb=0; sp<ep && nl<=f->maxlines; nb++,frame.nbox++){ + if(nb == frame.nalloc){ + _frgrowbox(&frame, delta); + if(delta < 10000) + delta *= 2; + } + b = &frame.box[nb]; + c = *sp; + if(c=='\t' || c=='\n'){ + b->bc = c; + b->wid = 5000; + b->minwid = (c=='\n')? 0 : stringwidth(frame.font, " "); + b->nrune = -1; + if(c=='\n') + nl++; + frame.nchars++; + sp++; + }else{ + s = tmp; + nr = 0; + w = 0; + while(sp < ep){ + c = *sp; + if(c=='\t' || c=='\n') + break; + rw = runetochar(s, sp); + if(s+rw >= tmp+TMPSIZE) + break; + w += runestringnwidth(frame.font, sp, 1); + sp++; + s += rw; + nr++; + } + *s++ = 0; + p = _frallocstr(f, s-tmp); + b = &frame.box[nb]; + b->ptr = p; + memmove(p, tmp, s-tmp); + b->wid = w; + b->nrune = nr; + frame.nchars += nr; + } + } + _frcklinewrap0(f, ppt, &frame.box[0]); + return _frdraw(&frame, *ppt); +} + +static +void +chopframe(Frame *f, Point pt, ulong p, int bn) +{ + Frbox *b; + + for(b = &f->box[bn]; ; b++){ + if(b >= &f->box[f->nbox]) + drawerror(f->display, "endofframe"); + _frcklinewrap(f, &pt, b); + if(pt.y >= f->r.max.y) + break; + p += NRUNE(b); + _fradvance(f, &pt, b); + } + f->nchars = p; + f->nlines = f->maxlines; + if(b<&f->box[f->nbox]) /* BUG */ + _frdelbox(f, (int)(b-f->box), f->nbox-1); +} + +void +frinsert(Frame *f, Rune *sp, Rune *ep, ulong p0) +{ + Point pt0, pt1, opt0, ppt0, ppt1, pt; + Frbox *b; + int n, n0, nn0, y; + ulong cn0; + Image *col; + Rectangle r; + static struct{ + Point pt0, pt1; + }*pts; + static int nalloc=0; + int npts; + + if(p0>f->nchars || sp==ep || f->b==nil) + return; + n0 = _frfindbox(f, 0, 0, p0); + cn0 = p0; + nn0 = n0; + pt0 = _frptofcharnb(f, p0, n0); + ppt0 = pt0; + opt0 = pt0; + pt1 = bxscan(f, sp, ep, &ppt0); + ppt1 = pt1; + if(n0 < f->nbox){ + _frcklinewrap(f, &pt0, b = &f->box[n0]); /* for frdrawsel() */ + _frcklinewrap0(f, &ppt1, b); + } + f->modified = 1; + /* + * ppt0 and ppt1 are start and end of insertion as they will appear when + * insertion is complete. pt0 is current location of insertion position + * (p0); pt1 is terminal point (without line wrap) of insertion. + */ + if(f->p0 == f->p1) + frtick(f, frptofchar(f, f->p0), 0); + + /* + * Find point where old and new x's line up + * Invariants: + * pt0 is where the next box (b, n0) is now + * pt1 is where it will be after the insertion + * If pt1 goes off the rectangle, we can toss everything from there on + */ + for(b = &f->box[n0],npts=0; + pt1.x!=pt0.x && pt1.y!=f->r.max.y && n0<f->nbox; b++,n0++,npts++){ + _frcklinewrap(f, &pt0, b); + _frcklinewrap0(f, &pt1, b); + if(b->nrune > 0){ + n = _frcanfit(f, pt1, b); + if(n == 0) + drawerror(f->display, "_frcanfit==0"); + if(n != b->nrune){ + _frsplitbox(f, n0, n); + b = &f->box[n0]; + } + } + if(npts == nalloc){ + pts = realloc(pts, (npts+DELTA)*sizeof(pts[0])); + nalloc += DELTA; + b = &f->box[n0]; + } + pts[npts].pt0 = pt0; + pts[npts].pt1 = pt1; + /* has a text box overflowed off the frame? */ + if(pt1.y == f->r.max.y) + break; + _fradvance(f, &pt0, b); + pt1.x += _frnewwid(f, pt1, b); + cn0 += NRUNE(b); + } + if(pt1.y > f->r.max.y) + drawerror(f->display, "frinsert pt1 too far"); + if(pt1.y==f->r.max.y && n0<f->nbox){ + f->nchars -= _frstrlen(f, n0); + _frdelbox(f, n0, f->nbox-1); + } + if(n0 == f->nbox) + f->nlines = (pt1.y-f->r.min.y)/f->font->height+(pt1.x>f->r.min.x); + else if(pt1.y!=pt0.y){ + int q0, q1; + + y = f->r.max.y; + q0 = pt0.y+f->font->height; + q1 = pt1.y+f->font->height; + f->nlines += (q1-q0)/f->font->height; + if(f->nlines > f->maxlines) + chopframe(f, ppt1, p0, nn0); + if(pt1.y < y){ + r = f->r; + r.min.y = q1; + r.max.y = y; + if(q1 < y) + draw(f->b, r, f->b, nil, Pt(f->r.min.x, q0)); + r.min = pt1; + r.max.x = pt1.x+(f->r.max.x-pt0.x); + r.max.y = q1; + draw(f->b, r, f->b, nil, pt0); + } + } + /* + * Move the old stuff down to make room. The loop will move the stuff + * between the insertion and the point where the x's lined up. + * The draw()s above moved everything down after the point they lined up. + */ + for((y=pt1.y==f->r.max.y?pt1.y:0),b = &f->box[n0-1]; --npts>=0; --b){ + pt = pts[npts].pt1; + if(b->nrune > 0){ + r.min = pt; + r.max = r.min; + r.max.x += b->wid; + r.max.y += f->font->height; + draw(f->b, r, f->b, nil, pts[npts].pt0); + /* clear bit hanging off right */ + if(npts==0 && pt.y>pt0.y){ + /* + * first new char is bigger than first char we're + * displacing, causing line wrap. ugly special case. + */ + r.min = opt0; + r.max = opt0; + r.max.x = f->r.max.x; + r.max.y += f->font->height; + if(f->p0<=cn0 && cn0<f->p1) /* b+1 is inside selection */ + col = f->cols[HIGH]; + else + col = f->cols[BACK]; + draw(f->b, r, col, nil, r.min); + }else if(pt.y < y){ + r.min = pt; + r.max = pt; + r.min.x += b->wid; + r.max.x = f->r.max.x; + r.max.y += f->font->height; + if(f->p0<=cn0 && cn0<f->p1) /* b+1 is inside selection */ + col = f->cols[HIGH]; + else + col = f->cols[BACK]; + draw(f->b, r, col, nil, r.min); + } + y = pt.y; + cn0 -= b->nrune; + }else{ + r.min = pt; + r.max = pt; + r.max.x += b->wid; + r.max.y += f->font->height; + if(r.max.x >= f->r.max.x) + r.max.x = f->r.max.x; + cn0--; + if(f->p0<=cn0 && cn0<f->p1) /* b is inside selection */ + col = f->cols[HIGH]; + else + col = f->cols[BACK]; + draw(f->b, r, col, nil, r.min); + y = 0; + if(pt.x == f->r.min.x) + y = pt.y; + } + } + /* insertion can extend the selection, so the condition here is different */ + if(f->p0<p0 && p0<=f->p1) + col = f->cols[HIGH]; + else + col = f->cols[BACK]; + frselectpaint(f, ppt0, ppt1, col); + _frredraw(&frame, ppt0); + _fraddbox(f, nn0, frame.nbox); + for(n=0; n<frame.nbox; n++) + f->box[nn0+n] = frame.box[n]; + if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=f->r.min.x){ + --nn0; + ppt0.x -= f->box[nn0].wid; + } + n0 += frame.nbox; + _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0); + f->nchars += frame.nchars; + if(f->p0 >= p0) + f->p0 += frame.nchars; + if(f->p0 > f->nchars) + f->p0 = f->nchars; + if(f->p1 >= p0) + f->p1 += frame.nchars; + if(f->p1 > f->nchars) + f->p1 = f->nchars; + if(f->p0 == f->p1) + frtick(f, frptofchar(f, f->p0), 1); +} diff --git a/src/libframe/frptofchar.c b/src/libframe/frptofchar.c new file mode 100644 index 00000000..58536ed9 --- /dev/null +++ b/src/libframe/frptofchar.c @@ -0,0 +1,115 @@ +#include <u.h> +#include <libc.h> +#include <draw.h> +#include <mouse.h> +#include <frame.h> + +Point +_frptofcharptb(Frame *f, ulong p, Point pt, int bn) +{ + uchar *s; + Frbox *b; + int w, l; + Rune r; + + for(b = &f->box[bn]; bn<f->nbox; bn++,b++){ + _frcklinewrap(f, &pt, b); + if(p < (l=NRUNE(b))){ + if(b->nrune > 0) + for(s=b->ptr; p>0; s+=w, p--){ + if((r = *s) < Runeself) + w = 1; + else + w = chartorune(&r, (char*)s); + pt.x += stringnwidth(f->font, (char*)s, 1); + if(r==0 || pt.x>f->r.max.x) + drawerror(f->display, "frptofchar"); + } + break; + } + p -= l; + _fradvance(f, &pt, b); + } + return pt; +} + +Point +frptofchar(Frame *f, ulong p) +{ + return _frptofcharptb(f, p, f->r.min, 0); +} + +Point +_frptofcharnb(Frame *f, ulong p, int nb) /* doesn't do final _fradvance to next line */ +{ + Point pt; + int nbox; + + nbox = f->nbox; + f->nbox = nb; + pt = _frptofcharptb(f, p, f->r.min, 0); + f->nbox = nbox; + return pt; +} + +static +Point +_frgrid(Frame *f, Point p) +{ + p.y -= f->r.min.y; + p.y -= p.y%f->font->height; + p.y += f->r.min.y; + if(p.x > f->r.max.x) + p.x = f->r.max.x; + return p; +} + +ulong +frcharofpt(Frame *f, Point pt) +{ + Point qt; + int w, bn; + uchar *s; + Frbox *b; + ulong p; + Rune r; + + pt = _frgrid(f, pt); + qt = f->r.min; + for(b=f->box,bn=0,p=0; bn<f->nbox && qt.y<pt.y; bn++,b++){ + _frcklinewrap(f, &qt, b); + if(qt.y >= pt.y) + break; + _fradvance(f, &qt, b); + p += NRUNE(b); + } + for(; bn<f->nbox && qt.x<=pt.x; bn++,b++){ + _frcklinewrap(f, &qt, b); + if(qt.y > pt.y) + break; + if(qt.x+b->wid > pt.x){ + if(b->nrune < 0) + _fradvance(f, &qt, b); + else{ + s = b->ptr; + for(;;){ + if((r = *s) < Runeself) + w = 1; + else + w = chartorune(&r, (char*)s); + if(r == 0) + drawerror(f->display, "end of string in frcharofpt"); + qt.x += stringnwidth(f->font, (char*)s, 1); + s += w; + if(qt.x > pt.x) + break; + p++; + } + } + }else{ + p += NRUNE(b); + _fradvance(f, &qt, b); + } + } + return p; +} diff --git a/src/libframe/frselect.c b/src/libframe/frselect.c new file mode 100644 index 00000000..b319c576 --- /dev/null +++ b/src/libframe/frselect.c @@ -0,0 +1,132 @@ +#include <u.h> +#include <libc.h> +#include <draw.h> +#include <mouse.h> +#include <frame.h> + +static +int +region(int a, int b) +{ + if(a < b) + return -1; + if(a == b) + return 0; + return 1; +} + +void +frselect(Frame *f, Mousectl *mc) /* when called, button 1 is down */ +{ + ulong p0, p1, q; + Point mp, pt0, pt1, qt; + int reg, b, scrled; + + mp = mc->m.xy; + b = mc->m.buttons; + + f->modified = 0; + frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0); + p0 = p1 = frcharofpt(f, mp); + f->p0 = p0; + f->p1 = p1; + pt0 = frptofchar(f, p0); + pt1 = frptofchar(f, p1); + frdrawsel(f, pt0, p0, p1, 1); + reg = 0; + do{ + scrled = 0; + if(f->scroll){ + if(mp.y < f->r.min.y){ + (*f->scroll)(f, -(f->r.min.y-mp.y)/(int)f->font->height-1); + p0 = f->p1; + p1 = f->p0; + scrled = 1; + }else if(mp.y > f->r.max.y){ + (*f->scroll)(f, (mp.y-f->r.max.y)/(int)f->font->height+1); + p0 = f->p0; + p1 = f->p1; + scrled = 1; + } + if(scrled){ + if(reg != region(p1, p0)) + q = p0, p0 = p1, p1 = q; /* undo the swap that will happen below */ + pt0 = frptofchar(f, p0); + pt1 = frptofchar(f, p1); + reg = region(p1, p0); + } + } + q = frcharofpt(f, mp); + if(p1 != q){ + if(reg != region(q, p0)){ /* crossed starting point; reset */ + if(reg > 0) + frdrawsel(f, pt0, p0, p1, 0); + else if(reg < 0) + frdrawsel(f, pt1, p1, p0, 0); + p1 = p0; + pt1 = pt0; + reg = region(q, p0); + if(reg == 0) + frdrawsel(f, pt0, p0, p1, 1); + } + qt = frptofchar(f, q); + if(reg > 0){ + if(q > p1) + frdrawsel(f, pt1, p1, q, 1); + else if(q < p1) + frdrawsel(f, qt, q, p1, 0); + }else if(reg < 0){ + if(q > p1) + frdrawsel(f, pt1, p1, q, 0); + else + frdrawsel(f, qt, q, p1, 1); + } + p1 = q; + pt1 = qt; + } + f->modified = 0; + if(p0 < p1) { + f->p0 = p0; + f->p1 = p1; + } + else { + f->p0 = p1; + f->p1 = p0; + } + if(scrled) + (*f->scroll)(f, 0); + flushimage(f->display, 1); + if(!scrled) + readmouse(mc); + mp = mc->m.xy; + }while(mc->m.buttons == b); +} + +void +frselectpaint(Frame *f, Point p0, Point p1, Image *col) +{ + int n; + Point q0, q1; + + q0 = p0; + q1 = p1; + q0.y += f->font->height; + q1.y += f->font->height; + n = (p1.y-p0.y)/f->font->height; + if(f->b == nil) + drawerror(f->display, "frselectpaint b==0"); + if(p0.y == f->r.max.y) + return; + if(n == 0) + draw(f->b, Rpt(p0, q1), col, nil, ZP); + else{ + if(p0.x >= f->r.max.x) + p0.x = f->r.max.x-1; + draw(f->b, Rect(p0.x, p0.y, f->r.max.x, q0.y), col, nil, ZP); + if(n > 1) + draw(f->b, Rect(f->r.min.x, q0.y, f->r.max.x, p1.y), + col, nil, ZP); + draw(f->b, Rect(f->r.min.x, p1.y, q1.x, q1.y), + col, nil, ZP); + } +} diff --git a/src/libframe/frstr.c b/src/libframe/frstr.c new file mode 100644 index 00000000..950b3e21 --- /dev/null +++ b/src/libframe/frstr.c @@ -0,0 +1,37 @@ +#include <u.h> +#include <libc.h> +#include <draw.h> +#include <mouse.h> +#include <frame.h> + +#define CHUNK 16 +#define ROUNDUP(n) ((n+CHUNK)&~(CHUNK-1)) + +uchar * +_frallocstr(Frame *f, unsigned n) +{ + uchar *p; + + p = malloc(ROUNDUP(n)); + if(p == 0) + drawerror(f->display, "out of memory"); + return p; +} + +void +_frinsure(Frame *f, int bn, unsigned n) +{ + Frbox *b; + uchar *p; + + b = &f->box[bn]; + if(b->nrune < 0) + drawerror(f->display, "_frinsure"); + if(ROUNDUP(b->nrune) > n) /* > guarantees room for terminal NUL */ + return; + p = _frallocstr(f, n); + b = &f->box[bn]; + memmove(p, b->ptr, NBYTE(b)+1); + free(b->ptr); + b->ptr = p; +} diff --git a/src/libframe/frutil.c b/src/libframe/frutil.c new file mode 100644 index 00000000..c8e3c043 --- /dev/null +++ b/src/libframe/frutil.c @@ -0,0 +1,111 @@ +#include <u.h> +#include <libc.h> +#include <draw.h> +#include <mouse.h> +#include <frame.h> + +int +_frcanfit(Frame *f, Point pt, Frbox *b) +{ + int left, w, nr; + uchar *p; + Rune r; + + left = f->r.max.x-pt.x; + if(b->nrune < 0) + return b->minwid <= left; + if(left >= b->wid) + return b->nrune; + for(nr=0,p=b->ptr; *p; p+=w,nr++){ + r = *p; + if(r < Runeself) + w = 1; + else + w = chartorune(&r, (char*)p); + left -= stringnwidth(f->font, (char*)p, 1); + if(left < 0) + return nr; + } + drawerror(f->display, "_frcanfit can't"); + return 0; +} + +void +_frcklinewrap(Frame *f, Point *p, Frbox *b) +{ + if((b->nrune<0? b->minwid : b->wid) > f->r.max.x-p->x){ + p->x = f->r.min.x; + p->y += f->font->height; + } +} + +void +_frcklinewrap0(Frame *f, Point *p, Frbox *b) +{ + if(_frcanfit(f, *p, b) == 0){ + p->x = f->r.min.x; + p->y += f->font->height; + } +} + +void +_fradvance(Frame *f, Point *p, Frbox *b) +{ + if(b->nrune<0 && b->bc=='\n'){ + p->x = f->r.min.x; + p->y += f->font->height; + }else + p->x += b->wid; +} + +int +_frnewwid(Frame *f, Point pt, Frbox *b) +{ + b->wid = _frnewwid0(f, pt, b); + return b->wid; +} + +int +_frnewwid0(Frame *f, Point pt, Frbox *b) +{ + int c, x; + + c = f->r.max.x; + x = pt.x; + if(b->nrune>=0 || b->bc!='\t') + return b->wid; + if(x+b->minwid > c) + x = pt.x = f->r.min.x; + x += f->maxtab; + x -= (x-f->r.min.x)%f->maxtab; + if(x-pt.x<b->minwid || x>c) + x = pt.x+b->minwid; + return x-pt.x; +} + +void +_frclean(Frame *f, Point pt, int n0, int n1) /* look for mergeable boxes */ +{ + Frbox *b; + int nb, c; + + c = f->r.max.x; + for(nb=n0; nb<n1-1; nb++){ + b = &f->box[nb]; + _frcklinewrap(f, &pt, b); + while(b[0].nrune>=0 && nb<n1-1 && b[1].nrune>=0 && pt.x+b[0].wid+b[1].wid<c){ + _frmergebox(f, nb); + n1--; + b = &f->box[nb]; + } + _fradvance(f, &pt, &f->box[nb]); + } + for(; nb<f->nbox; nb++){ + b = &f->box[nb]; + _frcklinewrap(f, &pt, b); + _fradvance(f, &pt, &f->box[nb]); + } + f->lastlinefull = 0; + if(pt.y >= f->r.max.y) + f->lastlinefull = 1; +} |