File: vshow.c

package info (click to toggle)
wily 0.13.33-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,500 kB
  • ctags: 1,720
  • sloc: ansic: 12,830; sh: 245; makefile: 188
file content (212 lines) | stat: -rw-r--r-- 4,343 bytes parent folder | download | duplicates (2)
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
#include "wily.h"
#include "view.h"

void
view_linesdown(View *v, int n, Bool down) {
	Mouse	m;
	Rectangle	r;

	if(! (v = tile_body(view_win(v))) )
		return;

	r = v->r;
	m.xy.x = r.min.x;
	m.xy.y = r.min.y + v->f.font->height * n;
	m.buttons = down? RIGHT : LEFT;
	view_scroll(v, &m);
}

void
view_pagedown(View *v, Bool down) {
	Mouse	m;
	Rectangle	r;

	if(! (v = tile_body(view_win(v))) )
		return;

	r = v->r;
	m.xy.x = r.min.x;
	m.xy.y = (r.min.y + r.max.y) /2;
	m.buttons = down? RIGHT : LEFT;
	view_scroll(v, &m);
}

/* Make 'n' the first rune displayed in 'v' */
static void
view_set(View*v, ulong n) {
	ulong	ndelete;

	assert(view_invariants(v));
	assert(v->scroll);
	assert(n<= text_length(v->t));

	if(n == v->visible.p0)
		return;

	ndelete = RCONTAINS(v->visible, n) ? 
			n - v->visible.p0 : 
			v->f.nchars;
	v->visible.p0 = n;
	if (ISVISIBLE(v)) {
		frdelete(&v->f, 0, ndelete);
		fill(v);
	}

	assert(view_invariants(v));
}

static Bool
tag_isfull(Frame*f) {
	Point	pt = frptofchar(f, f->nchars);
	Point	max = f->r.max;
	return max.x - pt.x < 30;
}

/* Try to get a tag to show as much as possible of 'r' */
static void
tag_show(View*v, Range r) {
	Frame	*f = &v->f;

	assert(ISTAG(v));
	assert(r.p1 <= text_length(v->t));

	/* If we're seeing all of r,
	 *  or if all that we're seeing is in r, we're happy
	 */
	if(RISSUBSET(r, v->visible) || RISSUBSET( v->visible,r))
		goto done;

	/* Otherwise, it must be the case that EITHER
	 * there's some of 'r' invisible off to the right or the left
	 */
	assert(r.p1 > v->visible.p1 || r.p0 < v->visible.p0);

	if (r.p1 > v->visible.p1) {
		/* remove stuff until we can see r.p1 */
		while(r.p1 > v->visible.p1) {
			frdelete(f, 0, f->nchars);
			v->visible.p0 += 5;
			fill(v);
		}

		assert(r.p1 <= v->visible.p1);
	} else {
		/* add stuff until we can see r.p0 */
		frdelete(f, 0, f->nchars);
		v->visible.p0 = r.p0;
		fill(v);

		assert(r.p0 >= v->visible.p0);
	}

done:
	while (!tag_isfull(f) && v->visible.p0) {
		frdelete(f, 0, f->nchars);
		v->visible.p0--;
		fill(v);
	}
}

/* Make sure 'v' is showing at least some of 'r' */
void
view_show(View *v, Range r) {
	assert(view_invariants(v));
	assert(ROK(r));
	assert(r.p1 <= text_length(v->t));

	if (v->scroll) {
		tile_show(v->tile);	
	}
	assert(ISVISIBLE(v));
	if(!v->scroll) {
		tag_show(v,r);
		return;
	}

	if(!RISSUBSET(r, v->visible)) {
		ulong	dest;

		/* try to position range in the middle of the screen */
		dest = text_nl(v->t, r.p0, -((int)v->f.maxlines)/2);
		view_set(v, dest);

		if (!RINTERSECT(r, v->visible)) {
			/* lines are too long, quit being fancy, start with line contain 'r' */
			dest = text_nl(v->t, r.p0, 0);
			view_set(v,dest);
		}
		if (!RINTERSECT(r, v->visible)) {
			/* goddamm!  we can't even fit a whole line.  no more mr nice guy */
			view_set(v,r.p0);
			/* Now _that_ oughta do it! */
		}
	}

	assert(RINTERSECT(r, v->visible));
	assert(view_invariants(v));
	assert(ISVISIBLE(v));
}

/* Handle a mouse-click in 'v's scrollbar */
void
view_scroll(View *v, Mouse *m) {
	int	y;
	ulong n,base = v->visible.p0;
	int	lineheight = v->f.font->height;
	ulong	runepos;

	assert(m->buttons);
	
	y = clip(m->xy.y - v->f.r.min.y, 0, Dy(v->f.r));
	if (y < v->f.font->height && m->buttons != MIDDLE){
		y = lineheight;
		m->xy.y += lineheight;
	}

	runepos = frcharofpt(&v->f, m->xy);
	/*
	 * With left and middle buttons, we try to scroll back such that
	 * we start at the start of a line.  Sometimes that isn't possible,
	 * if lines are too big, so we jump again to some fallback position.
	 * TODO:  work this stuff out offscreen.
	 */
	switch(m->buttons){
	case RIGHT:
		view_set(v, base + runepos);
		break;
	case LEFT:
		n = back_height(v->t, v->visible.p0,  v->f.font, Dx(v->f.r), m->xy.y - v->f.r.min.y);
		view_set(v, n);
		break;
	default:
		runepos = (text_length(v->t) * y) / Dy(v->f.r);
		n = text_nl(v->t, runepos, 0);
		view_set(v, n);
		if (runepos > v->visible.p1) {
			view_set(v, runepos);
		}
	}
}

/* Scroll 'v' to the left or right a bit, if possible */
void
view_hscroll(View*v, Bool left) {
	ulong	old = v->visible.p0;
	assert(!v->scroll);

	assert(ISTAG(v));
	
	if(left){
		if(v->visible.p0) 
			v->visible.p0--;
	} else {
		if(v->visible.p1 < text_length(v->t))
			v->visible.p0++;
	}

	if (old != v->visible.p0){
		frdelete(&v->f, 0, v->f.nchars);
		fill(v);
	}
}