xref: /plan9/sys/src/libframe/frdraw.c (revision 0d601874851962e88c6c60fdd2e637bba04e13c2)
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 	/* can go into left border but not right */
129 	if(r.max.x > f->r.max.x)
130 		r.max.x = f->r.max.x;
131 	if(ticked){
132 		draw(f->tickback, f->tickback->r, f->b, nil, pt);
133 		draw(f->b, r, f->tick, nil, ZP);
134 	}else
135 		draw(f->b, r, f->tickback, nil, ZP);
136 	f->ticked = ticked;
137 }
138 
139 Point
140 _frdraw(Frame *f, Point pt)
141 {
142 	Frbox *b;
143 	int nb, n;
144 
145 	for(b=f->box,nb=0; nb<f->nbox; nb++, b++){
146 		_frcklinewrap0(f, &pt, b);
147 		if(pt.y == f->r.max.y){
148 			f->nchars -= _frstrlen(f, nb);
149 			_frdelbox(f, nb, f->nbox-1);
150 			break;
151 		}
152 		if(b->nrune > 0){
153 			n = _frcanfit(f, pt, b);
154 			if(n == 0)
155 				drawerror(f->display, "_frcanfit==0");
156 			if(n != b->nrune){
157 				_frsplitbox(f, nb, n);
158 				b = &f->box[nb];
159 			}
160 			pt.x += b->wid;
161 		}else{
162 			if(b->bc == '\n'){
163 				pt.x = f->r.min.x;
164 				pt.y+=f->font->height;
165 			}else
166 				pt.x += _frnewwid(f, pt, b);
167 		}
168 	}
169 	return pt;
170 }
171 
172 int
173 _frstrlen(Frame *f, int nb)
174 {
175 	int n;
176 
177 	for(n=0; nb<f->nbox; nb++)
178 		n += NRUNE(&f->box[nb]);
179 	return n;
180 }
181