Blob


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