Blob


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