summaryrefslogtreecommitdiffstats
path: root/Lib/plat-irix6/panel.py
blob: 3aa744865389fc3b2a15ce9bd9aaf91f143d8e4a (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
# Module 'panel'
#
# Support for the Panel library.
# Uses built-in module 'pnl'.
# Applications should use 'panel.function' instead of 'pnl.function';
# most 'pnl' functions are transparently exported by 'panel',
# but dopanel() is overridden and you have to use this version
# if you want to use callbacks.


import pnl


debug = 0


# Test if an object is a list.
#
def is_list(x):
	return type(x) == type([])


# Reverse a list.
#
def reverse(list):
	res = []
	for item in list:
		res.insert(0, item)
	return res


# Get an attribute of a list, which may itself be another list.
# Don't use 'prop' for name.
#
def getattrlist(list, name):
	for item in list:
		if item and is_list(item) and item[0] == name:
			return item[1:]
	return []


# Get a property of a list, which may itself be another list.
#
def getproplist(list, name):
	for item in list:
		if item and is_list(item) and item[0] == 'prop':
			if len(item) > 1 and item[1] == name:
				return item[2:]
	return []


# Test if an actuator description contains the property 'end-of-group'
#
def is_endgroup(list):
	x = getproplist(list, 'end-of-group')
	return (x and x[0] == '#t')


# Neatly display an actuator definition given as S-expression
# the prefix string is printed before each line.
#
def show_actuator(prefix, a):
	for item in a:
		if not is_list(item):
			print prefix, item
		elif item and item[0] == 'al':
			print prefix, 'Subactuator list:'
			for a in item[1:]:
				show_actuator(prefix + '    ', a)
		elif len(item) == 2:
			print prefix, item[0], '=>', item[1]
		elif len(item) == 3 and item[0] == 'prop':
			print prefix, 'Prop', item[1], '=>',
			print item[2]
		else:
			print prefix, '?', item


# Neatly display a panel.
#
def show_panel(prefix, p):
	for item in p:
		if not is_list(item):
			print prefix, item
		elif item and item[0] == 'al':
			print prefix, 'Actuator list:'
			for a in item[1:]:
				show_actuator(prefix + '    ', a)
		elif len(item) == 2:
			print prefix, item[0], '=>', item[1]
		elif len(item) == 3 and item[0] == 'prop':
			print prefix, 'Prop', item[1], '=>',
			print item[2]
		else:
			print prefix, '?', item


# Exception raised by build_actuator or build_panel.
#
panel_error = 'panel error'


# Dummy callback used to initialize the callbacks.
#
def dummy_callback(arg):
	pass


# Assign attributes to members of the target.
# Attribute names in exclist are ignored.
# The member name is the attribute name prefixed with the prefix.
#
def assign_members(target, attrlist, exclist, prefix):
	for item in attrlist:
		if is_list(item) and len(item) == 2 and item[0] not in exclist:
			name, value = item[0], item[1]
			ok = 1
			if value[0] in '-0123456789':
				value = eval(value)
			elif value[0] == '"':
				value = value[1:-1]
			elif value == 'move-then-resize':
				# Strange default set by Panel Editor...
				ok = 0
			else:
				print 'unknown value', value, 'for', name
				ok = 0
			if ok:
				lhs = 'target.' + prefix + name
				stmt = lhs + '=' + repr(value)
				if debug: print 'exec', stmt
				try:
					exec stmt + '\n'
				except KeyboardInterrupt: # Don't catch this!
					raise KeyboardInterrupt
				except:
					print 'assign failed:', stmt


# Build a real actuator from an actuator description.
# Return a pair (actuator, name).
#
def build_actuator(descr):
	namelist = getattrlist(descr, 'name')
	if namelist:
		# Assume it is a string
		actuatorname = namelist[0][1:-1]
	else:
		actuatorname = ''
	type = descr[0]
	if type[:4] == 'pnl_': type = type[4:]
	act = pnl.mkact(type)
	act.downfunc = act.activefunc = act.upfunc = dummy_callback
	#
	assign_members(act, descr[1:], ['al', 'data', 'name'], '')
	#
	# Treat actuator-specific data
	#
	datalist = getattrlist(descr, 'data')
	prefix = ''
	if type[-4:] == 'puck':
		prefix = 'puck_'
	elif type == 'mouse':
		prefix = 'mouse_'
	assign_members(act, datalist, [], prefix)
	#
	return act, actuatorname


# Build all sub-actuators and add them to the super-actuator.
# The super-actuator must already have been added to the panel.
# Sub-actuators with defined names are added as members to the panel
# so they can be referenced as p.name.
#
# Note: I have no idea how panel.endgroup() works when applied
# to a sub-actuator.
#
def build_subactuators(panel, super_act, al):
	#
	# This is nearly the same loop as below in build_panel(),
	# except a call is made to addsubact() instead of addact().
	#
	for a in al:
		act, name = build_actuator(a)
		act.addsubact(super_act)
		if name:
			stmt = 'panel.' + name + ' = act'
			if debug: print 'exec', stmt
			exec stmt + '\n'
		if is_endgroup(a):
			panel.endgroup()
		sub_al = getattrlist(a, 'al')
		if sub_al:
			build_subactuators(panel, act, sub_al)
	#
	# Fix the actuator to which whe just added subactuators.
	# This can't hurt (I hope) and is needed for the scroll actuator.
	#
	super_act.fixact()


# Build a real panel from a panel definition.
# Return a panel object p, where for each named actuator a, p.name is a
# reference to a.
#
def build_panel(descr):
	#
	# Sanity check
	#
	if (not descr) or descr[0] != 'panel':
		raise panel_error, 'panel description must start with "panel"'
	#
	if debug: show_panel('', descr)
	#
	# Create an empty panel
	#
	panel = pnl.mkpanel()
	#
	# Assign panel attributes
	#
	assign_members(panel, descr[1:], ['al'], '')
	#
	# Look for actuator list
	#
	al = getattrlist(descr, 'al')
	#
	# The order in which actuators are created is important
	# because of the endgroup() operator.
	# Unfortunately the Panel Editor outputs the actuator list
	# in reverse order, so we reverse it here.
	#
	al = reverse(al)
	#
	for a in al:
		act, name = build_actuator(a)
		act.addact(panel)
		if name:
			stmt = 'panel.' + name + ' = act'
			exec stmt + '\n'
		if is_endgroup(a):
			panel.endgroup()
		sub_al = getattrlist(a, 'al')
		if sub_al:
			build_subactuators(panel, act, sub_al)
	#
	return panel


# Wrapper around pnl.dopanel() which calls call-back functions.
#
def my_dopanel():
	# Extract only the first 4 elements to allow for future expansion
	a, down, active, up = pnl.dopanel()[:4]
	if down:
		down.downfunc(down)
	if active:
		active.activefunc(active)
	if up:
		up.upfunc(up)
	return a


# Create one or more panels from a description file (S-expressions)
# generated by the Panel Editor.
# 
def defpanellist(file):
	import panelparser
	descrlist = panelparser.parse_file(open(file, 'r'))
	panellist = []
	for descr in descrlist:
		panellist.append(build_panel(descr))
	return panellist


# Import everything from built-in method pnl, so the user can always
# use panel.foo() instead of pnl.foo().
# This gives *no* performance penalty once this module is imported.
#
from pnl import *			# for export

dopanel = my_dopanel			# override pnl.dopanel