1 #include <u.h> 2 #include <libc.h> 3 #include <draw.h> 4 #include <thread.h> 5 #include <mouse.h> 6 #include <frame.h> 7 8 void 9 _frredraw(Frame *f, Point pt, Image *text, Image *back) 10 { 11 Frbox *b; 12 int nb; 13 static int x; 14 15 for(nb=0,b=f->box; nb<f->nbox; nb++, b++){ 16 _frcklinewrap(f, &pt, b); 17 if(b->nrune >= 0){ 18 stringbg(f->b, pt, text, ZP, f->font, (char *)b->ptr, back, ZP); 19 } 20 pt.x += b->wid; 21 } 22 } 23 24 static int 25 nbytes(char *s0, int nr) 26 { 27 char *s; 28 Rune r; 29 30 s = s0; 31 while(--nr >= 0) 32 s += chartorune(&r, s); 33 return s-s0; 34 } 35 36 void 37 frdrawsel(Frame *f, Point pt, ulong p0, ulong p1, int issel) 38 { 39 Image *back, *text; 40 41 if(f->ticked) 42 frtick(f, frptofchar(f, f->p0), 0); 43 44 if(p0 == p1){ 45 frtick(f, pt, issel); 46 return; 47 } 48 49 if(issel){ 50 back = f->cols[HIGH]; 51 text = f->cols[HTEXT]; 52 }else{ 53 back = f->cols[BACK]; 54 text = f->cols[TEXT]; 55 } 56 57 frdrawsel0(f, pt, p0, p1, back, text); 58 } 59 60 void 61 frdrawsel0(Frame *f, Point pt, ulong p0, ulong p1, Image *back, Image *text) 62 { 63 Frbox *b; 64 int nb, nr, w, x, trim; 65 Point qt; 66 uint p; 67 char *ptr; 68 69 p = 0; 70 b = f->box; 71 trim = 0; 72 for(nb=0; nb<f->nbox && p<p1; nb++){ 73 nr = b->nrune; 74 if(nr < 0) 75 nr = 1; 76 if(p+nr <= p0) 77 goto Continue; 78 if(p >= p0){ 79 qt = pt; 80 _frcklinewrap(f, &pt, b); 81 // fill in the end of a wrapped line 82 if(pt.y > qt.y) 83 draw(f->b, Rect(qt.x, qt.y, f->r.max.x, pt.y), back, nil, qt); 84 } 85 ptr = (char*)b->ptr; 86 if(p < p0){ /* beginning of region: advance into box */ 87 ptr += nbytes(ptr, p0-p); 88 nr -= (p0-p); 89 p = p0; 90 } 91 trim = 0; 92 if(p+nr > p1){ /* end of region: trim box */ 93 nr -= (p+nr)-p1; 94 trim = 1; 95 } 96 if(b->nrune<0 || nr==b->nrune) 97 w = b->wid; 98 else 99 w = stringnwidth(f->font, ptr, nr); 100 x = pt.x+w; 101 if(x > f->r.max.x) 102 x = f->r.max.x; 103 draw(f->b, Rect(pt.x, pt.y, x, pt.y+f->font->height), back, nil, pt); 104 if(b->nrune >= 0) 105 stringnbg(f->b, pt, text, ZP, f->font, ptr, nr, back, ZP); 106 pt.x += w; 107 Continue: 108 b++; 109 p += nr; 110 } 111 /* if this is end of last plain text box on wrapped line, fill to end of line */ 112 if(p1>p0 && b>f->box && b<f->box+f->nbox && b[-1].nrune>0 && !trim){ 113 qt = pt; 114 _frcklinewrap(f, &pt, b); 115 if(pt.y > qt.y) 116 draw(f->b, Rect(qt.x, qt.y, f->r.max.x, pt.y), back, nil, qt); 117 } 118 } 119 120 void 121 frtick(Frame *f, Point pt, int ticked) 122 { 123 Rectangle r; 124 125 if(f->ticked==ticked || f->tick==0 || !ptinrect(pt, f->r)) 126 return; 127 pt.x--; /* looks best just left of where requested */ 128 r = Rect(pt.x, pt.y, pt.x+FRTICKW, pt.y+f->font->height); 129 /* can go into left border but not right */ 130 if(r.max.x > f->r.max.x) 131 r.max.x = f->r.max.x; 132 if(ticked){ 133 draw(f->tickback, f->tickback->r, f->b, nil, pt); 134 draw(f->b, r, f->tick, nil, ZP); 135 }else 136 draw(f->b, r, f->tickback, nil, ZP); 137 f->ticked = ticked; 138 } 139 140 Point 141 _frdraw(Frame *f, Point pt) 142 { 143 Frbox *b; 144 int nb, n; 145 146 for(b=f->box,nb=0; nb<f->nbox; nb++, b++){ 147 _frcklinewrap0(f, &pt, b); 148 if(pt.y == f->r.max.y){ 149 f->nchars -= _frstrlen(f, nb); 150 _frdelbox(f, nb, f->nbox-1); 151 break; 152 } 153 if(b->nrune > 0){ 154 n = _frcanfit(f, pt, b); 155 if(n == 0) 156 drawerror(f->display, "_frcanfit==0"); 157 if(n != b->nrune){ 158 _frsplitbox(f, nb, n); 159 b = &f->box[nb]; 160 } 161 pt.x += b->wid; 162 }else{ 163 if(b->bc == '\n'){ 164 pt.x = f->r.min.x; 165 pt.y+=f->font->height; 166 }else 167 pt.x += _frnewwid(f, pt, b); 168 } 169 } 170 return pt; 171 } 172 173 int 174 _frstrlen(Frame *f, int nb) 175 { 176 int n; 177 178 for(n=0; nb<f->nbox; nb++) 179 n += NRUNE(&f->box[nb]); 180 return n; 181 } 182