summaryrefslogtreecommitdiff
path: root/appl/wm/smenu.b
blob: 6b06754f2a8100d40b4bdbb01b10755d1b58e4f8 (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
implement Smenu;

include "sys.m";
	sys: Sys;
include "draw.m";
include "tk.m";
	tk: Tk;
include "smenu.m";

Scrollmenu.new(t: ref Tk->Toplevel, name: string, labs: array of string, e: int, o: int): ref Scrollmenu
{
	if(sys == nil)
		sys = load Sys Sys->PATH;
	if(tk == nil)
		tk = load Tk Tk->PATH;
	m := ref Scrollmenu;
	n := len labs;
	if(n < e)
		e = n;
	if(o > n-e)
		o = n-e;
	l := 0;
	for(i := 0; i < n; i++){
		if(len labs[i] > l)
			l = len labs[i];
		i++;
	}
	nlabs := array[n] of string;
	sp := string array[l] of { * => byte ' ' };
	for(i = 0; i < n; i++)
		nlabs[i] = labs[i] + sp[0: l - len labs[i]];
	sch := cname(name);
	cmd(t, "menu " + name);
	for(i = 0; i < e; i++){
		cmd(t, name + " add command -label {" + nlabs[o+i] + "} -command {send " + sch + " " + string i + "}");
	}
	# cmd(t, "bind " + name + " <ButtonPress-1> +{send " + sch + " b}");
	# cmd(t, "bind " + name + " <ButtonRelease-1> +{send " + sch + " b}");
	cmd(t, "bind " + name + " <Motion> +{send " + sch + " M %x %y}");
	cmd(t, "bind " + name + " <Map> +{send " + sch + " m}");
	cmd(t, "bind " + name + " <Unmap> +{send " + sch + " u}");
	cmd(t, "update");
	m.name = name;
	m.labs = nlabs;
	m.c = nil;
	m.t = t;
	m.m = e;
	m.n = n;
	m.o = o;
	m.timer = 1;
	return m;
}

Scrollmenu.post(m: self ref Scrollmenu, x: int, y: int, resc: chan of string, prefix: string)
{
	sync := chan of int;
	spawn listen(m, sync, resc, prefix);
	<- sync;
	cmd(m.t, m.name + " post " + string x + " " + string y);
	cmd(m.t, "update");
}

Scrollmenu.destroy(m: self ref Scrollmenu)
{
	if(m.c != nil){
		m.c <-= "u";	# fake unmap message
		m.c = nil;
	}
	m.name = nil;
	m.labs = nil;
	m.t = nil;
}

timer(t: int, sync: chan of int, c: chan of int)
{
	sync <-= 0;
	for(;;){
		alt{
			c <-= 0 =>
				sys->sleep(t);
			<- sync =>
				exit;
		}
	}
}

TINT: con 100;
SEC: con 1000/TINT;
		
listen(m: ref Scrollmenu, sync: chan of int, resc: chan of string, prefix: string)
{
	timerc := chan of int;
	cmdc := chan of string;
	m.c = cmdc;
	tk->namechan(m.t, cmdc, cname(m.name));
	sync <-= 0;
	x := y := ly := w := h := -1;
	for(;;){
		alt{
			<- timerc =>
				if(x > 0 && x < w){
					if(y < 0 && y > -h/m.m)
						menudir(m, -1);
					else if(y > 0+h && y < h+h/m.m)
						menudir(m, 1);
				}
			s := <- cmdc =>
				(nil, toks) := sys->tokenize(s, " ");
				case hd toks{
					"M" =>
						x = int hd tl toks;
						y = int hd tl tl toks;
						if(!m.timer && x > 0 && x < w){
							mv := 0;
							if(y < ly && y < 0)
								mv = y/(h/m.m)-1;
							else if(y > ly && y > h)
								mv = (y-h)/(h/m.m)+1;
							if(mv != 0)
								menudirs(m, mv);
							ly = y;
						}
					"m" =>
						w = int cmd(m.t, m.name + " cget -actwidth");
						h = int cmd(m.t, m.name + " cget -actheight");
						ly = -1;
						if(m.timer){
							spawn timer(TINT, sync, timerc);
							<- sync;
						}
					"u" =>
						if(m.timer)
							sync <-= 0;
						m.c = nil;
						exit;
					* =>
						# do not block
						res := prefix + string (int hd toks + m.o);
						for(t := 0; t < SEC; ){
							if(m.timer)
								alt{
									resc <-=  res =>
										t = SEC;
									<- timerc =>
										t++;
								}
							else
								alt{
									resc <-= res =>
										t = SEC;
									* =>
										sys->sleep(TINT);
										t++;
								}
						}
				}
		}
	}
}

menudirs(sm: ref Scrollmenu, n: int)
{
	if(n < 0)
		(a, d) := (-n, -1);
	else
		(a, d) = (n, 1);
	for(i := 0; i < a; i++)
		menudir(sm, d);
}

menudir(sm: ref Scrollmenu, d: int)
{
	o := sm.o;
	n := sm.n;
	m := sm.m;
	if(d == -1){
		if(o == 0)
			return;
		for(i := 0; i < m; i++)
			cmd(sm.t, sm.name + " entryconfigure " + string i + " -label {" + sm.labs[o-1+i] + "}");
		sm.o = o-1;
	}
	else{
		if(o+m == n)
			return;
		for(i := 0; i < m; i++)
			cmd(sm.t, sm.name + " entryconfigure " + string i + " -label {" + sm.labs[o+1+i] + "}");
		sm.o = o+1;
	}
	cmd(sm.t, "update");	
}

cname(s: string): string
{
	return "sm_" + s + "_sm";
}

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