summaryrefslogtreecommitdiff
path: root/appl/lib/dividers.b
blob: aaf3b08e51357fc63dc5ebff1f3dccce95704d3a (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
implement Dividers;

include "sys.m";
	sys: Sys;
include "draw.m";
	draw: Draw;
	Point, Rect: import draw;
include "tk.m";
	tk: Tk;
include "dividers.m";

Lay: adt {
	d: int;
	x: fn(l: self Lay, p: Point): int;
	y: fn(l: self Lay, p: Point): int;
	mkr: fn(l: self Lay, r: Rect): Rect;
	mkpt: fn(l: self Lay, p: Point): Point;
};

DIVHEIGHT: con 6;

init()
{
	sys = load Sys Sys->PATH;
	draw = load Draw Draw->PATH;
	tk = load Tk Tk->PATH;
}

# dir is direction in which to stack widgets (NS or EW)
Divider.new(win: ref Tk->Toplevel, w: string, wl: list of string, dir: int): (ref Divider, chan of string)
{
	lay := Lay(dir);
	n := len wl;
	d := ref Divider(win, w, nil, dir, array[n] of {* => ref DWidget}, (0, 0));
	p := Point(0, 0);
	for (i := 0; wl != nil; (wl, i) = (tl wl, i+1)) {
		sz := lay.mkpt(wsize(win, hd wl));
		*d.widgets[i] = (hd wl, (p, p.add(sz)), sz);
		if (sz.x > d.canvsize.x)
			d.canvsize.x = sz.x;
		p.y += sz.y + DIVHEIGHT;
	}
	d.canvsize.y = p.y - DIVHEIGHT;
	cmd(win, "canvas " + d.w + " -width " + string lay.x(d.canvsize) +
			" -height " + string lay.y(d.canvsize));
	ech := chan of string;
	echname := "dw" + d.w;
	tk->namechan(win, ech, echname);
	for (i = 0; i < n; i++) {
		dw := d.widgets[i];
		dw.r.max.x = d.canvsize.x + dw.r.min.x;
		sz := dxy(dw.r);
		cmd(win, d.w + " create window " + p2s(lay.mkpt(dw.r.min)) +
			" -window " + dw.w +
			" -tags w" + string i + " -anchor nw" +
			" -width " + string lay.x(sz) +
			" -height " + string lay.y(sz));
		cmd(win, "pack propagate " + dw.w + " 0");
		if (i < n - 1) {
			r := lay.mkr(((dw.r.min.x, dw.r.max.y),
					(dw.r.max.x, dw.r.max.y + DIVHEIGHT)));
			cmd(win, d.w + " create rectangle " + r2s(r) +
				" -fill red" +
				" -tags d" + string i);
			cmd(win, d.w + " bind d" + string i + " <Button-1>" +
				" {send " + echname + " but " + string i + " %x %y}");
			cmd(win, d.w + " bind d" + string i + " <Motion-Button-1> {}");
			cmd(win, d.w + " bind d" + string i + " <ButtonRelease-1>" +
				" {send " + echname + " up x %x %y}");
		}
	}
	cmd(win, d.w + " create rectangle -2 -2 -1 -1 -tags grab");
	cmd(win, d.w + " bind grab <Button-1> {send " + echname + " drag x %x %y}");
	cmd(win, d.w + " bind grab <ButtonRelease-1> {send " + echname + " up x %x %y}");
	cmd(win, "bind " + d.w + " <Configure> {send " + echname + " config x x x}");
	return (d, ech);
}

Divider.event(d: self ref Divider, e: string)
{
	(n, toks) := sys->tokenize(e, " ");
	if (n != 4) {
		sys->print("dividers: invalid event %s\n", e);
		return;
	}
	lay := Lay(d.dir);
	p := lay.mkpt((int hd tl tl toks, int hd tl tl tl toks));
	t := hd toks;
	if (t == "but" && d.state != nil)
		t = "drag";
	case t {
	"but" =>
		if (d.state != nil) {
			sys->print("dividers: event '%s' received in drag mode\n", e);
			return;
		}
		div := int hd tl toks;
		d.state = ref DState;
		d.state.dragdiv = div;
		d.state.dy = p.y - d.widgets[div].r.max.y;
		d.state.maxy = d.widgets[div+1].r.max.y - DIVHEIGHT;
		d.state.miny = d.widgets[div].r.min.y;
		cmd(d.win, d.w + " itemconfigure d" + string div + " -fill orange");
		cmd(d.win, d.w + " raise d" + string div);
		cmd(d.win, d.w + " coords grab -10000 -10000 10000 10000");
		cmd(d.win, "grab set " + d.w);
		cmd(d.win, "update");
	"drag" =>
		if (d.state == nil) {
			sys->print("dividers: event '%s' received in non-drag mode\n", e);
			return;
		}
		div := d.state.dragdiv;
		ypos := p.y - d.state.dy;
		if (ypos > d.state.maxy)
			ypos = d.state.maxy;
		else if (ypos < d.state.miny)
			ypos = d.state.miny;
		r := Rect((0, ypos), (d.canvsize.x, ypos + DIVHEIGHT));
		cmd(d.win, d.w + " coords d" + string div + " " + r2s(lay.mkr(r)));
		d.widgets[div].r.max.y = ypos;
		d.widgets[div+1].r.min.y = ypos + DIVHEIGHT;
		relayout(d);
		cmd(d.win, "update");
	"up" =>
		if (d.state == nil) {
			sys->print("dividers: event '%s' received in non-drag mode\n", e);
			return;
		}
		div := d.state.dragdiv;
		cmd(d.win, d.w + " itemconfigure d" + string div + " -fill red");
		cmd(d.win, d.w + " coords grab -2 -2 -1 -1");
		cmd(d.win, "grab release " + d.w);
		cmd(d.win, "update");
		d.state = nil;
	"config" =>
		resize(d);
		cmd(d.win, "update");
	}
}

# lay out widgets according to rectangles that have been already specified.
relayout(d: ref Divider)
{
	lay := Lay(d.dir);
	for (i := 0; i < len d.widgets; i++) {
		dw := d.widgets[i];
		sz := dxy(dw.r);
		szs := " -width " + string lay.x(sz) + " -height " + string lay.y(sz);
		cmd(d.win, d.w + " coords w" + string i + " " + p2s(lay.mkpt(dw.r.min)));
		cmd(d.win, d.w + " itemconfigure w" + string i + szs);
		cmd(d.win, dw.w + " configure" + szs);
		if (i < len d.widgets - 1) {
			r := lay.mkr(((dw.r.min.x, dw.r.max.y),
					(dw.r.max.x, dw.r.max.y + DIVHEIGHT)));
			cmd(d.win, d.w + " coords d" + string i + " " + r2s(r));
		}
	}
}

# resize based on current actual size of canvas;
# sections resize proportionate to their previously occupied space.
# strange things will happen if we're resizing in the middle of a drag...
resize(d: ref Divider)
{
	lay := Lay(d.dir);
	sz := lay.mkpt((int cmd(d.win, d.w + " cget -actwidth"), 
			int cmd(d.win, d.w + " cget -actheight")));

	wspace := (len d.widgets - 1) * DIVHEIGHT;
	y := 0;
	for (i := 0; i < len d.widgets; i++) {
		dw := d.widgets[i];
		prop := real dw.r.dy() / real (d.canvsize.y - wspace);
		dw.r = ((0, y), (sz.x, y + int (prop * real (sz.y - wspace))));
		y = dw.r.max.y + DIVHEIGHT;
	}
	y -= DIVHEIGHT;
	# compensate for rounding errors
	d.widgets[i - 1].r.max.y -= y - sz.y;
	d.canvsize = sz;
	relayout(d);
}

wsize(win: ref Tk->Toplevel, w: string): Point
{
	bw := int cmd(win, w + " cget -borderwidth");
	return Point(int cmd(win, w + " cget -width") + bw*2,
			int cmd(win, w + " cget -height") + bw*2);
}

dxy(r: Rect): Point
{
	return r.max.sub(r.min);
}

p2s(p: Point): string
{
	return string p.x + " " + string p.y;
}

r2s(r: Rect): string
{
	return string r.min.x + " " + string r.min.y + " " +
			string r.max.x + " " + string r.max.y;
}

Lay.x(l: self Lay, p: Point): int
{
	if (l.d == NS)
		return p.x;
	return p.y;
}

Lay.y(l: self Lay, p: Point): int
{
	if (l.d == NS)
		return p.y;
	return p.x;
}

Lay.mkr(l: self Lay, r: Rect): Rect
{
	if (l.d == NS)
		return r;
	return ((r.min.y, r.min.x), (r.max.y, r.max.x));
}

Lay.mkpt(l: self Lay, p: Point): Point
{
	if (l.d == NS)
		return p;
	return (p.y, p.x);
}

cmd(top: ref Tk->Toplevel, s: string): string
{
	e := tk->cmd(top, s);
	if (e != nil && e[0] == '!')
		sys->print("dividers: tk error %s on '%s'\n", e, s);
	return e;
}