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) 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 string(f->b, pt, f->cols[TEXT], ZP, f->font, (char *)b->ptr); 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 if(pt.y > qt.y) 82 draw(f->b, Rect(qt.x, qt.y, f->r.max.x, pt.y), back, nil, qt); 83 } 84 ptr = (char*)b->ptr; 85 if(p < p0){ /* beginning of region: advance into box */ 86 ptr += nbytes(ptr, p0-p); 87 nr -= (p0-p); 88 p = p0; 89 } 90 trim = 0; 91 if(p+nr > p1){ /* end of region: trim box */ 92 nr -= (p+nr)-p1; 93 trim = 1; 94 } 95 if(b->nrune<0 || nr==b->nrune) 96 w = b->wid; 97 else 98 w = stringnwidth(f->font, ptr, nr); 99 x = pt.x+w; 100 if(x > f->r.max.x) 101 x = f->r.max.x; 102 draw(f->b, Rect(pt.x, pt.y, x, pt.y+f->font->height), back, nil, pt); 103 if(b->nrune >= 0) 104 stringn(f->b, pt, text, ZP, f->font, ptr, nr); 105 pt.x += w; 106 Continue: 107 b++; 108 p += nr; 109 } 110 /* if this is end of last plain text box on wrapped line, fill to end of line */ 111 if(p1>p0 && b>f->box && b<f->box+f->nbox && b[-1].nrune>0 && !trim){ 112 qt = pt; 113 _frcklinewrap(f, &pt, b); 114 if(pt.y > qt.y) 115 draw(f->b, Rect(qt.x, qt.y, f->r.max.x, pt.y), back, nil, qt); 116 } 117 } 118 119 void 120 frtick(Frame *f, Point pt, int ticked) 121 { 122 Rectangle r; 123 124 if(f->ticked==ticked || f->tick==0 || !ptinrect(pt, f->r)) 125 return; 126 pt.x--; /* looks best just left of where requested */ 127 r = Rect(pt.x, pt.y, pt.x+FRTICKW, pt.y+f->font->height); 128 if(ticked){ 129 draw(f->tickback, f->tickback->r, f->b, nil, pt); 130 draw(f->b, r, f->tick, nil, ZP); 131 }else 132 draw(f->b, r, f->tickback, nil, ZP); 133 f->ticked = ticked; 134 } 135 136 Point 137 _frdraw(Frame *f, Point pt) 138 { 139 Frbox *b; 140 int nb, n; 141 142 for(b=f->box,nb=0; nb<f->nbox; nb++, b++){ 143 _frcklinewrap0(f, &pt, b); 144 if(pt.y == f->r.max.y){ 145 f->nchars -= _frstrlen(f, nb); 146 _frdelbox(f, nb, f->nbox-1); 147 break; 148 } 149 if(b->nrune > 0){ 150 n = _frcanfit(f, pt, b); 151 if(n == 0) 152 drawerror(f->display, "_frcanfit==0"); 153 if(n != b->nrune){ 154 _frsplitbox(f, nb, n); 155 b = &f->box[nb]; 156 } 157 pt.x += b->wid; 158 }else{ 159 if(b->bc == '\n'){ 160 pt.x = f->r.min.x; 161 pt.y+=f->font->height; 162 }else 163 pt.x += _frnewwid(f, pt, b); 164 } 165 } 166 return pt; 167 } 168 169 int 170 _frstrlen(Frame *f, int nb) 171 { 172 int n; 173 174 for(n=0; nb<f->nbox; nb++) 175 n += NRUNE(&f->box[nb]); 176 return n; 177 } 178