summaryrefslogtreecommitdiff
path: root/sys/src/cmd/mothra/libpanel/textview.c
blob: 27b8bc5adfb2223a069c6486971ef4a8a28d293b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Fonted text viewer, calls out to code in rtext.c
 *
 * Should redo this to copy the already-visible parts on scrolling & only
 * update the newly appearing stuff -- then the offscreen assembly bitmap can go away.
 */
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include <panel.h>
#include "pldefs.h"
#include "rtext.h"

typedef struct Textview Textview;
struct Textview{
	void (*hit)(Panel *, int, Rtext *); /* call back to user on hit */
	Rtext *text;			/* text */
	int yoffs;			/* offset of top of screen */
	Rtext *hitword;			/* text to hilite */
	Rtext *hitfirst;		/* first word in range select */
	Image *hitsave;			/* for restoring hilit text */
	int twid;			/* text width */
	int thgt;			/* text height */
	Point minsize;			/* smallest acceptible window size */
	int buttons;
	char *snarf;
	char *esnarf;
};

/*
 * Highlight a range of words from s to e.
 */
void pl_hilitewords(Panel *p, Rtext *s, Rtext *e, int on)
{
	Point ul, size;
	Rectangle r;
	Textview *tp;
	Rtext *t;

	if(s==0 || e==0)
		return;
	for(t=s; t!=0 && t!=e; t=t->next)
		;
	if(t==e){
		r=s->r;
		for(t=s; t!=e; t=t->next)
			combinerect(&r, t->r);
	}else{
		r=e->r;
		for(t=e; t!=s; t=t->next)
			combinerect(&r, t->r);
	}
	combinerect(&r, t->r);

	tp=p->data;
	ul=p->r.min;
	size=subpt(p->r.max, p->r.min);
	pl_interior(UP, &ul, &size);
	ul.y-=tp->yoffs;
	r=rectaddpt(r, ul);
	if(rectclip(&r, p->r)==0)
		return;

	if(on){
		if(tp->hitsave) freeimage(tp->hitsave);
		tp->hitsave = allocimage(display, r, screen->chan, 0, DNofill);
		if(tp->hitsave) draw(tp->hitsave, r, p->b, 0, r.min);
		if(t==e){
			for(t=s; t!=e; t=t->next){
				if(t->p!=0) continue;
				r=rectaddpt(t->r, ul);
				if(rectclip(&r, p->r))
					pl_highlight(p->b, r);
			}
		}else{
			for(t=e; t!=s; t=t->next){
				if(t->p!=0) continue;
				r=rectaddpt(t->r, ul);
				if(rectclip(&r, p->r))
					pl_highlight(p->b, r);
			}
		}
		if(t->p==0){
			r=rectaddpt(t->r, ul);
			if(rectclip(&r, p->r))
				pl_highlight(p->b, r);
		}
	} else {
		if(tp->hitsave){
			draw(p->b, r, tp->hitsave, 0, r.min);
			freeimage(tp->hitsave);
			tp->hitsave = 0;
		}
	}
}

void pl_stuffbitmap(Panel *p, Image *b){
	p->b=b;
	for(p=p->child;p;p=p->next)
		pl_stuffbitmap(p, b);
}
/*
 * If we draw the text in a backup bitmap and copy it onto the screen,
 * the bitmap pointers in all the subpanels point to the wrong bitmap.
 * This code fixes them.
 */
void pl_drawnon(Rtext *rp, Image *b){
	for(;rp!=0;rp=rp->next)
		if(rp->b==0 && rp->p!=0)
			pl_stuffbitmap(rp->p, b);
}
/*
 * Mark the hilite and update the scroll bar
 */
void pl_fixtextview(Panel *p, Textview *tp, Rectangle r){
	Panel *sb;
	int lo, hi;
	pl_hilitewords(p, tp->hitword, tp->hitfirst, 1);
	lo=tp->yoffs;
	hi=lo+r.max.y-r.min.y;	/* wrong? */
	sb=p->yscroller;
	if(sb && sb->setscrollbar) sb->setscrollbar(sb, lo, hi, tp->thgt);
}
void pl_drawtextview(Panel *p){
	int twid;
	Rectangle r;
	Textview *tp;
	Image *b;
	tp=p->data;
	b=allocimage(display, p->r, screen->chan, 0, DNofill);
	if(b==0) b=p->b;
	r=pl_outline(b, p->r, p->state);
	twid=r.max.x-r.min.x;
	if(twid!=tp->twid){
		tp->twid=twid;
		tp->thgt=pl_rtfmt(tp->text, tp->twid);
		p->scr.size.y=tp->thgt;
	}
	p->scr.pos.y=tp->yoffs;
	pl_rtdraw(b, r, tp->text, tp->yoffs);
	if(b!=p->b){
		draw(p->b, p->r, b, 0, b->r.min);
		freeimage(b);
		pl_drawnon(tp->text, p->b);
	}
	pl_fixtextview(p, tp, r);
}
/*
 * If t is a panel word, pass the mouse event on to it
 */
void pl_passon(Rtext *t, Mouse *m){
	if(t && t->b==0 && t->p!=0) plmouse(t->p, m);
}
int pl_hittextview(Panel *p, Mouse *m){
	Rtext *oldhitword, *oldhitfirst;
	int hitme, oldstate;
	Point ul, size;
	Textview *tp;

	tp=p->data;

	hitme=0;
	oldstate=p->state;
	oldhitword=tp->hitword;
	oldhitfirst=tp->hitfirst;
	if(oldhitword==oldhitfirst)
		pl_passon(oldhitword, m);
	if(m->buttons&OUT)
		p->state=UP;
	else if(m->buttons&7){
		tp->buttons=m->buttons;
		p->state=DOWN;

		ul=p->r.min;
		size=subpt(p->r.max, p->r.min);
		pl_interior(p->state, &ul, &size);
		tp->hitword=pl_rthit(tp->text, tp->yoffs, m->xy, ul);
		if(tp->hitword==0)
			if(oldhitword!=0 && oldstate==DOWN)
				tp->hitword=oldhitword;
			else
				tp->hitfirst=0;
		if(tp->hitword!=0 && oldstate!=DOWN)
			tp->hitfirst=tp->hitword;
	}
	else{
		if(p->state==DOWN) hitme=1;
		p->state=UP;
	}
	if(tp->hitfirst!=oldhitfirst || tp->hitword!=oldhitword){
		pl_hilitewords(p, oldhitword, oldhitfirst, 0);
		pl_hilitewords(p, tp->hitword, tp->hitfirst, 1);
		if(tp->hitword==tp->hitfirst)
			pl_passon(tp->hitword, m);
	}
	if(hitme && tp->hit && tp->hitword!=0 && tp->hitword==tp->hitfirst){
		pl_hilitewords(p, tp->hitword, tp->hitfirst, 0);
		tp->hit(p, tp->buttons, tp->hitword);
		tp->hitword=0;
		tp->hitfirst=0;
	}
	return 0;
}
void pl_scrolltextview(Panel *p, int dir, int buttons, int num, int den){
	int yoffs;
	Point ul, size;
	Textview *tp;
	Rectangle r;
	if(dir!=VERT) return;

	tp=p->data;
	ul=p->r.min;
	size=subpt(p->r.max, p->r.min);
	pl_interior(p->state, &ul, &size);
	switch(buttons){
	default:
		SET(yoffs);
		break;
	case 1:		/* left -- top moves to pointer */
		yoffs=(vlong)tp->yoffs-num*size.y/den;
		if(yoffs<0) yoffs=0;
		break;
	case 2:		/* middle -- absolute index of file */
		yoffs=(vlong)tp->thgt*num/den;
		break;
	case 4:		/* right -- line pointed at moves to top */
		yoffs=tp->yoffs+(vlong)num*size.y/den;
		if(yoffs>tp->thgt) yoffs=tp->thgt;
		break;
	}
	if(yoffs!=tp->yoffs){
		pl_hilitewords(p, tp->hitword, tp->hitfirst, 0);
		r=pl_outline(p->b, p->r, p->state);
		pl_rtredraw(p->b, r, tp->text, yoffs, tp->yoffs);
		p->scr.pos.y=tp->yoffs=yoffs;
		pl_fixtextview(p, tp, r);
	}
}
void pl_typetextview(Panel *g, Rune c){
	USED(g, c);
}
Point pl_getsizetextview(Panel *p, Point children){
	USED(children);
	return pl_boxsize(((Textview *)p->data)->minsize, p->state);
}
void pl_childspacetextview(Panel *g, Point *ul, Point *size){
	USED(g, ul, size);
}
/*
 * Priority depends on what thing inside the panel we're pointing at.
 */
int pl_pritextview(Panel *p, Point xy){
	Point ul, size;
	Textview *tp;
	Rtext *h;
	tp=p->data;
	ul=p->r.min;
	size=subpt(p->r.max, p->r.min);
	pl_interior(p->state, &ul, &size);
	h=pl_rthit(tp->text, tp->yoffs, xy, ul);
	if(h && h->b==0 && h->p!=0){
		p=pl_ptinpanel(xy, h->p);
		if(p) return p->pri(p, xy);
	}
	return PRI_NORMAL;
}
void plinittextview(Panel *v, int flags, Point minsize, Rtext *t, void (*hit)(Panel *, int, Rtext *)){
	Textview *tp;
	tp=v->data;
	v->flags=flags|LEAF;
	v->state=UP;
	v->draw=pl_drawtextview;
	v->hit=pl_hittextview;
	v->type=pl_typetextview;
	v->getsize=pl_getsizetextview;
	v->childspace=pl_childspacetextview;
	v->kind="textview";
	v->pri=pl_pritextview;
	tp->hit=hit;
	tp->minsize=minsize;
	tp->text=t;
	tp->yoffs=0;
	tp->hitfirst=0;
	tp->hitword=0;
	tp->esnarf=tp->snarf=0;
	v->scroll=pl_scrolltextview;
	tp->twid=-1;
	v->scr.pos=Pt(0,0);
	v->scr.size=Pt(0,1);
}
Panel *pltextview(Panel *parent, int flags, Point minsize, Rtext *t, void (*hit)(Panel *, int, Rtext *)){
	Panel *v;
	v=pl_newpanel(parent, sizeof(Textview));
	plinittextview(v, flags, minsize, t, hit);
	return v;
}
int plgetpostextview(Panel *p){
	return ((Textview *)p->data)->yoffs;
}
void plsetpostextview(Panel *p, int yoffs){
	((Textview *)p->data)->yoffs=yoffs;
	pldraw(p, p->b);
}

static void
snarfword(Textview *tp, Rtext *w){
	char *b;
	int n;
	if(w->b!=0 || w->p!=0 || w->text==0)
		return;
	n = strlen(w->text)+4;
	if((b = realloc(tp->snarf, (tp->esnarf+n) - tp->snarf)) == nil)
		return;
	tp->esnarf = (tp->esnarf - tp->snarf) + b;
	tp->snarf = b;
	if(w->space == 0)
		tp->esnarf += sprint(tp->esnarf, "%s", w->text);
	else if(w->space > 0)
		tp->esnarf += sprint(tp->esnarf, " %s", w->text);
	else if(PL_OP(w->space) == PL_TAB)
		tp->esnarf += sprint(tp->esnarf, "\t%s", w->text);
	if(w->nextline == w->next)
		tp->esnarf += sprint(tp->esnarf, "\n");
}

char *plsnarftext(Panel *p){
	Rtext *t, *s, *e;
	Textview *tp;
	tp=p->data;
	free(tp->snarf);
	tp->snarf=tp->esnarf=0;
	s = tp->hitfirst;
	e = tp->hitword;
	if(s==0 || e==0)
		return nil;
	for(t=s; t!=0 && t!=e; t=t->next)
		;
	if(t==e){
		for(t=s; t!=e; t=t->next)
			snarfword(tp, t);
	}else{
		for(t=e; t!=s; t=t->next)
			snarfword(tp, t);
	}
	snarfword(tp, t);
	return tp->snarf;
}