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 frdrawsel(Frame *f, Point pt, ulong p0, ulong p1, int issel)
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);
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 frdrawsel0(Frame *f, Point pt, ulong p0, ulong p1, Image *back, Image *text)
60 {
61 Frbox *b;
62 int nb, nr, w, x, trim;
63 Point qt;
64 uint p;
65 char *ptr;
67 p = 0;
68 b = f->box;
69 trim = 0;
70 for(nb=0; nb<f->nbox && p<p1; nb++){
71 nr = b->nrune;
72 if(nr < 0)
73 nr = 1;
74 if(p+nr <= p0)
75 goto Continue;
76 if(p >= p0){
77 qt = pt;
78 _frcklinewrap(f, &pt, b);
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 stringn(f->b, pt, text, ZP, f->font, ptr, nr);
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);
117 void
118 frtick(Frame *f, Point pt, int ticked)
120 Rectangle r;
122 if(f->ticked==ticked || f->tick==0 || !ptinrect(pt, f->r))
123 return;
124 pt.x--; /* looks best just left of where requested */
125 r = Rect(pt.x, pt.y, pt.x+FRTICKW, pt.y+f->font->height);
126 if(ticked){
127 draw(f->tickback, f->tickback->r, f->b, nil, pt);
128 draw(f->b, r, f->tick, nil, ZP);
129 }else
130 draw(f->b, r, f->tickback, nil, ZP);
131 f->ticked = ticked;
134 Point
135 _frdraw(Frame *f, Point pt)
137 Frbox *b;
138 int nb, n;
140 for(b=f->box,nb=0; nb<f->nbox; nb++, b++){
141 _frcklinewrap0(f, &pt, b);
142 if(pt.y == f->r.max.y){
143 f->nchars -= _frstrlen(f, nb);
144 _frdelbox(f, nb, f->nbox-1);
145 break;
147 if(b->nrune > 0){
148 n = _frcanfit(f, pt, b);
149 if(n == 0)
150 drawerror(f->display, "_frcanfit==0");
151 if(n != b->nrune){
152 _frsplitbox(f, nb, n);
153 b = &f->box[nb];
155 pt.x += b->wid;
156 }else{
157 if(b->bc == '\n'){
158 pt.x = f->r.min.x;
159 pt.y+=f->font->height;
160 }else
161 pt.x += _frnewwid(f, pt, b);
164 return pt;
167 int
168 _frstrlen(Frame *f, int nb)
170 int n;
172 for(n=0; nb<f->nbox; nb++)
173 n += NRUNE(&f->box[nb]);
174 return n;