aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/samterm/flayer.c
blob: a8e70d0c044da354f7a4b5402521f99cf4ba6eba (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <mouse.h>
#include <cursor.h>
#include <keyboard.h>
#include <frame.h>
#include "flayer.h"
#include "samterm.h"

#define	DELTA	10

static Flayer	**llist;	/* front to back */
static int	nllist;
static int	nlalloc;
static Rectangle lDrect;

Vis		visibility(Flayer *);
void		newvisibilities(int);
void		llinsert(Flayer*);
void		lldelete(Flayer*);

Image	*maincols[NCOL];
Image	*cmdcols[NCOL];

void
flstart(Rectangle r)
{
	lDrect = r;

	/* Main text is yellowish */
	maincols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
	maincols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
	maincols[BORD] = allocimage(display, Rect(0,0,2,2), screen->chan, 1, DYellowgreen);
	maincols[TEXT] = display->black;
	maincols[HTEXT] = display->black;

	/* Command text is blueish */
	cmdcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
	cmdcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
	cmdcols[BORD] = allocimage(display, Rect(0,0,2,2), screen->chan, 1, DPurpleblue);
	cmdcols[TEXT] = display->black;
	cmdcols[HTEXT] = display->black;
}

void
flnew(Flayer *l, Rune *(*fn)(Flayer*, long, ulong*), int u0, void *u1)
{
	if(nllist == nlalloc){
		nlalloc += DELTA;
		llist = realloc(llist, nlalloc*sizeof(Flayer**));
		if(llist == 0)
			panic("flnew");
	}
	l->textfn = fn;
	l->user0 = u0;
	l->user1 = u1;
	l->lastsr = ZR;
	llinsert(l);
}

Rectangle
flrect(Flayer *l, Rectangle r)
{
	rectclip(&r, lDrect);
	l->entire = r;
	l->scroll = insetrect(r, FLMARGIN(l));
	r.min.x =
	 l->scroll.max.x = r.min.x+FLMARGIN(l)+FLSCROLLWID(l)+(FLGAP(l)-FLMARGIN(l));
	return r;
}

void
flinit(Flayer *l, Rectangle r, Font *ft, Image **cols)
{
	lldelete(l);
	llinsert(l);
	l->visible = All;
	l->origin = l->p0 = l->p1 = 0;
	l->f.display = display; // for FLMARGIN
	frinit(&l->f, insetrect(flrect(l, r), FLMARGIN(l)), ft, screen, cols);
	l->f.maxtab = maxtab*stringwidth(ft, "0");
	newvisibilities(1);
	draw(screen, l->entire, l->f.cols[BACK], nil, ZP);
	scrdraw(l, 0L);
	flborder(l, 0);
}

void
flclose(Flayer *l)
{
	if(l->visible == All)
		draw(screen, l->entire, display->white, nil, ZP);
	else if(l->visible == Some){
		if(l->f.b == 0)
			l->f.b = allocimage(display, l->entire, screen->chan, 0, DNofill);
		if(l->f.b){
			draw(l->f.b, l->entire, display->white, nil, ZP);
			flrefresh(l, l->entire, 0);
		}
	}
	frclear(&l->f, 1);
	lldelete(l);
	if(l->f.b && l->visible!=All)
		freeimage(l->f.b);
	l->textfn = 0;
	newvisibilities(1);
}

void
flborder(Flayer *l, int wide)
{
	if(flprepare(l)){
		border(l->f.b, l->entire, FLMARGIN(l), l->f.cols[BACK], ZP);
		border(l->f.b, l->entire, wide? FLMARGIN(l) : 1, l->f.cols[BORD], ZP);
		if(l->visible==Some)
			flrefresh(l, l->entire, 0);
	}
}

Flayer *
flwhich(Point p)
{
	int i;

	if(p.x==0 && p.y==0)
		return nllist? llist[0] : 0;
	for(i=0; i<nllist; i++)
		if(ptinrect(p, llist[i]->entire))
			return llist[i];
	return 0;
}

void
flupfront(Flayer *l)
{
	int v = l->visible;

	lldelete(l);
	llinsert(l);
	if(v!=All)
		newvisibilities(0);
}

void
newvisibilities(int redraw)
	/* if redraw false, we know it's a flupfront, and needn't
	 * redraw anyone becoming partially covered */
{
	int i;
	Vis ov;
	Flayer *l;

	for(i = 0; i<nllist; i++){
		l = llist[i];
		l->lastsr = ZR;	/* make sure scroll bar gets redrawn */
		ov = l->visible;
		l->visible = visibility(l);
#define	V(a, b)	(((a)<<2)|((b)))
		switch(V(ov, l->visible)){
		case V(Some, None):
			if(l->f.b)
				freeimage(l->f.b);
		case V(All, None):
		case V(All, Some):
			l->f.b = 0;
			frclear(&l->f, 0);
			break;

		case V(Some, Some):
		case V(None, Some):
			if(ov == None || (l->f.b==0 && redraw))
				flprepare(l);
			if(l->f.b && redraw){
				flrefresh(l, l->entire, 0);
				freeimage(l->f.b);
				l->f.b = 0;
				frclear(&l->f, 0);
			}
		case V(None, None):
		case V(All, All):
			break;

		case V(Some, All):
			if(l->f.b){
				draw(screen, l->entire, l->f.b, nil, l->entire.min);
				freeimage(l->f.b);
				l->f.b = screen;
				break;
			}
		case V(None, All):
			flprepare(l);
			break;
		}
		if(ov==None && l->visible!=None)
			flnewlyvisible(l);
	}
}

void
llinsert(Flayer *l)
{
	int i;
	for(i=nllist; i>0; --i)
		llist[i]=llist[i-1];
	llist[0]=l;
	nllist++;
}

void
lldelete(Flayer *l)
{
	int i;

	for(i=0; i<nllist; i++)
		if(llist[i]==l){
			--nllist;
			for(; i<nllist; i++)
				llist[i] = llist[i+1];
			return;
		}
	panic("lldelete");
}

void
flinsert(Flayer *l, Rune *sp, Rune *ep, long p0)
{
	if(flprepare(l)){
		frinsert(&l->f, sp, ep, p0-l->origin);
		scrdraw(l, scrtotal(l));
		if(l->visible==Some)
			flrefresh(l, l->entire, 0);
	}
}

void
fldelete(Flayer *l, long p0, long p1)
{
	if(flprepare(l)){
		p0 -= l->origin;
		if(p0 < 0)
			p0 = 0;
		p1 -= l->origin;
		if(p1<0)
			p1 = 0;
		frdelete(&l->f, p0, p1);
		scrdraw(l, scrtotal(l));
		if(l->visible==Some)
			flrefresh(l, l->entire, 0);
	}
}

int
flselect(Flayer *l)
{
	int ret;
	if(l->visible!=All)
		flupfront(l);
	frselect(&l->f, mousectl);
	ret = 0;
	if(l->f.p0==l->f.p1){
		if(mousep->msec-l->click<Clicktime && l->f.p0+l->origin==l->p0){
			ret = 1;
			l->click = 0;
		}else
			l->click = mousep->msec;
	}else
		l->click = 0;
	l->p0 = l->f.p0+l->origin, l->p1 = l->f.p1+l->origin;
	return ret;
}

void
flsetselect(Flayer *l, long p0, long p1)
{
	ulong fp0, fp1;
	int ticked;

	l->click = 0;
	if(l->visible==None || !flprepare(l)){
		l->p0 = p0, l->p1 = p1;
		return;
	}
	l->p0 = p0, l->p1 = p1;
	flfp0p1(l, &fp0, &fp1, &ticked);
	if(fp0==l->f.p0 && fp1==l->f.p1){
		if(l->f.ticked != ticked)
			frtick(&l->f, frptofchar(&l->f, fp0), ticked);
		return;
	}

	if(fp1<=l->f.p0 || fp0>=l->f.p1 || l->f.p0==l->f.p1 || fp0==fp1){
		/* no overlap or trivial repainting */
		frdrawsel(&l->f, frptofchar(&l->f, l->f.p0), l->f.p0, l->f.p1, 0);
		if(fp0 != fp1 || ticked)
			frdrawsel(&l->f, frptofchar(&l->f, fp0), fp0, fp1, 1);
		goto Refresh;
	}
	/* the current selection and the desired selection overlap and are both non-empty */
	if(fp0 < l->f.p0){
		/* extend selection backwards */
		frdrawsel(&l->f, frptofchar(&l->f, fp0), fp0, l->f.p0, 1);
	}else if(fp0 > l->f.p0){
		/* trim first part of selection */
		frdrawsel(&l->f, frptofchar(&l->f, l->f.p0), l->f.p0, fp0, 0);
	}
	if(fp1 > l->f.p1){
		/* extend selection forwards */
		frdrawsel(&l->f, frptofchar(&l->f, l->f.p1), l->f.p1, fp1, 1);
	}else if(fp1 < l->f.p1){
		/* trim last part of selection */
		frdrawsel(&l->f, frptofchar(&l->f, fp1), fp1, l->f.p1, 0);
	}

    Refresh:
	l->f.p0 = fp0;
	l->f.p1 = fp1;
	if(l->visible==Some)
		flrefresh(l, l->entire, 0);
}

void
flfp0p1(Flayer *l, ulong *pp0, ulong *pp1, int *ticked)
{
	long p0 = l->p0-l->origin, p1 = l->p1-l->origin;

	*ticked = p0 == p1;
	if(p0 < 0){
		*ticked = 0;
		p0 = 0;
	}
	if(p1 < 0)
		p1 = 0;
	if(p0 > l->f.nchars)
		p0 = l->f.nchars;
	if(p1 > l->f.nchars){
		*ticked = 0;
		p1 = l->f.nchars;
	}
	*pp0 = p0;
	*pp1 = p1;
}

Rectangle
rscale(Rectangle r, Point old, Point new)
{
	r.min.x = r.min.x*new.x/old.x;
	r.min.y = r.min.y*new.y/old.y;
	r.max.x = r.max.x*new.x/old.x;
	r.max.y = r.max.y*new.y/old.y;
	return r;
}

void
flresize(Rectangle dr)
{
	int i;
	Flayer *l;
	Frame *f;
	Rectangle r, olDrect;
	int move;

	olDrect = lDrect;
	lDrect = dr;
	move = 0;
	/* no moving on rio; must repaint */
	if(0 && Dx(dr)==Dx(olDrect) && Dy(dr)==Dy(olDrect))
		move = 1;
	else
		draw(screen, lDrect, display->white, nil, ZP);
	for(i=0; i<nllist; i++){
		l = llist[i];
		l->lastsr = ZR;
		f = &l->f;
		if(move)
			r = rectaddpt(rectsubpt(l->entire, olDrect.min), dr.min);
		else{
			r = rectaddpt(rscale(rectsubpt(l->entire, olDrect.min),
				subpt(olDrect.max, olDrect.min),
				subpt(dr.max, dr.min)), dr.min);
			if(l->visible==Some && f->b){
				freeimage(f->b);
				frclear(f, 0);
			}
			f->b = 0;
			if(l->visible!=None)
				frclear(f, 0);
		}
		if(!rectclip(&r, dr))
			panic("flresize");
		if(r.max.x-r.min.x<100)
			r.min.x = dr.min.x;
		if(r.max.x-r.min.x<100)
			r.max.x = dr.max.x;
		if(r.max.y-r.min.y<2*FLMARGIN(l)+f->font->height)
			r.min.y = dr.min.y;
		if(r.max.y-r.min.y<2*FLMARGIN(l)+f->font->height)
			r.max.y = dr.max.y;
		if(!move)
			l->visible = None;
		frsetrects(f, insetrect(flrect(l, r), FLMARGIN(l)), f->b);
		if(!move && f->b)
			scrdraw(l, scrtotal(l));
	}
	newvisibilities(1);
}

int
flprepare(Flayer *l)
{
	Frame *f;
	ulong n;
	Rune *r;
	int ticked;

	if(l->visible == None)
		return 0;
	f = &l->f;
	if(f->b == 0){
		if(l->visible == All)
			f->b = screen;
		else if((f->b = allocimage(display, l->entire, screen->chan, 0, 0))==0)
			return 0;
		draw(f->b, l->entire, f->cols[BACK], nil, ZP);
		border(f->b, l->entire, l==llist[0]? FLMARGIN(l) : 1, f->cols[BORD], ZP);
		n = f->nchars;
		frinit(f, f->entire, f->font, f->b, 0);
		f->maxtab = maxtab*stringwidth(f->font, "0");
		r = (*l->textfn)(l, n, &n);
		frinsert(f, r, r+n, (ulong)0);
		frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0);
		flfp0p1(l, &f->p0, &f->p1, &ticked);
		if(f->p0 != f->p1 || ticked)
			frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 1);
		l->lastsr = ZR;
		scrdraw(l, scrtotal(l));
	}
	return 1;
}

static	int	somevis, someinvis, justvis;

Vis
visibility(Flayer *l)
{
	somevis = someinvis = 0;
	justvis = 1;
	flrefresh(l, l->entire, 0);
	justvis = 0;
	if(somevis==0)
		return None;
	if(someinvis==0)
		return All;
	return Some;
}

void
flrefresh(Flayer *l, Rectangle r, int i)
{
	Flayer *t;
	Rectangle s;

    Top:
	if((t=llist[i++]) == l){
		if(!justvis)
			draw(screen, r, l->f.b, nil, r.min);
		somevis = 1;
	}else{
		if(!rectXrect(t->entire, r))
			goto Top;	/* avoid stacking unnecessarily */
		if(t->entire.min.x>r.min.x){
			s = r;
			s.max.x = t->entire.min.x;
			flrefresh(l, s, i);
			r.min.x = t->entire.min.x;
		}
		if(t->entire.min.y>r.min.y){
			s = r;
			s.max.y = t->entire.min.y;
			flrefresh(l, s, i);
			r.min.y = t->entire.min.y;
		}
		if(t->entire.max.x<r.max.x){
			s = r;
			s.min.x = t->entire.max.x;
			flrefresh(l, s, i);
			r.max.x = t->entire.max.x;
		}
		if(t->entire.max.y<r.max.y){
			s = r;
			s.min.y = t->entire.max.y;
			flrefresh(l, s, i);
			r.max.y = t->entire.max.y;
		}
		/* remaining piece of r is blocked by t; forget about it */
		someinvis = 1;
	}
}

int
flscale(Flayer *l, int n)
{
	if(l == nil)
		return n;
	return scalesize(l->f.display, n);
}