blob: fcfa56b54d9a8882273fcf093bf4a8889910dc49 [file] [log] [blame]
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +00001# Module 'panel'
2#
3# Support for the Panel library.
4# Uses built-in module 'pnl'.
Jeremy Hyltona05e2932000-06-28 14:48:01 +00005# Applications should use 'panel.function' instead of 'pnl.function';
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +00006# most 'pnl' functions are transparently exported by 'panel',
7# but dopanel() is overridden and you have to use this version
8# if you want to use callbacks.
Brett Cannon7e375862008-05-15 03:46:27 +00009from warnings import warnpy3k
10warnpy3k("the panel module has been removed in Python 3.0", stacklevel=2)
11del warnpy3k
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000012
13
14import pnl
15
16
17debug = 0
18
19
20# Test if an object is a list.
21#
22def is_list(x):
Tim Peters182b5ac2004-07-18 06:16:08 +000023 return type(x) == type([])
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000024
25
26# Reverse a list.
27#
28def reverse(list):
Tim Peters182b5ac2004-07-18 06:16:08 +000029 res = []
30 for item in list:
31 res.insert(0, item)
32 return res
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000033
34
35# Get an attribute of a list, which may itself be another list.
36# Don't use 'prop' for name.
37#
38def getattrlist(list, name):
Tim Peters182b5ac2004-07-18 06:16:08 +000039 for item in list:
40 if item and is_list(item) and item[0] == name:
41 return item[1:]
42 return []
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000043
44
45# Get a property of a list, which may itself be another list.
46#
47def getproplist(list, name):
Tim Peters182b5ac2004-07-18 06:16:08 +000048 for item in list:
49 if item and is_list(item) and item[0] == 'prop':
50 if len(item) > 1 and item[1] == name:
51 return item[2:]
52 return []
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000053
54
55# Test if an actuator description contains the property 'end-of-group'
56#
57def is_endgroup(list):
Tim Peters182b5ac2004-07-18 06:16:08 +000058 x = getproplist(list, 'end-of-group')
59 return (x and x[0] == '#t')
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000060
61
62# Neatly display an actuator definition given as S-expression
63# the prefix string is printed before each line.
64#
65def show_actuator(prefix, a):
Tim Peters182b5ac2004-07-18 06:16:08 +000066 for item in a:
67 if not is_list(item):
68 print prefix, item
69 elif item and item[0] == 'al':
70 print prefix, 'Subactuator list:'
71 for a in item[1:]:
72 show_actuator(prefix + ' ', a)
73 elif len(item) == 2:
74 print prefix, item[0], '=>', item[1]
75 elif len(item) == 3 and item[0] == 'prop':
76 print prefix, 'Prop', item[1], '=>',
77 print item[2]
78 else:
79 print prefix, '?', item
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000080
81
82# Neatly display a panel.
83#
84def show_panel(prefix, p):
Tim Peters182b5ac2004-07-18 06:16:08 +000085 for item in p:
86 if not is_list(item):
87 print prefix, item
88 elif item and item[0] == 'al':
89 print prefix, 'Actuator list:'
90 for a in item[1:]:
91 show_actuator(prefix + ' ', a)
92 elif len(item) == 2:
93 print prefix, item[0], '=>', item[1]
94 elif len(item) == 3 and item[0] == 'prop':
95 print prefix, 'Prop', item[1], '=>',
96 print item[2]
97 else:
98 print prefix, '?', item
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000099
100
101# Exception raised by build_actuator or build_panel.
102#
103panel_error = 'panel error'
104
105
106# Dummy callback used to initialize the callbacks.
107#
108def dummy_callback(arg):
Tim Peters182b5ac2004-07-18 06:16:08 +0000109 pass
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +0000110
111
112# Assign attributes to members of the target.
113# Attribute names in exclist are ignored.
114# The member name is the attribute name prefixed with the prefix.
115#
116def assign_members(target, attrlist, exclist, prefix):
Tim Peters182b5ac2004-07-18 06:16:08 +0000117 for item in attrlist:
118 if is_list(item) and len(item) == 2 and item[0] not in exclist:
119 name, value = item[0], item[1]
120 ok = 1
121 if value[0] in '-0123456789':
122 value = eval(value)
123 elif value[0] == '"':
124 value = value[1:-1]
125 elif value == 'move-then-resize':
126 # Strange default set by Panel Editor...
127 ok = 0
128 else:
129 print 'unknown value', value, 'for', name
130 ok = 0
131 if ok:
132 lhs = 'target.' + prefix + name
133 stmt = lhs + '=' + repr(value)
134 if debug: print 'exec', stmt
135 try:
136 exec stmt + '\n'
137 except KeyboardInterrupt: # Don't catch this!
138 raise KeyboardInterrupt
139 except:
140 print 'assign failed:', stmt
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +0000141
142
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000143# Build a real actuator from an actuator description.
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +0000144# Return a pair (actuator, name).
145#
146def build_actuator(descr):
Tim Peters182b5ac2004-07-18 06:16:08 +0000147 namelist = getattrlist(descr, 'name')
148 if namelist:
149 # Assume it is a string
150 actuatorname = namelist[0][1:-1]
151 else:
152 actuatorname = ''
153 type = descr[0]
154 if type[:4] == 'pnl_': type = type[4:]
155 act = pnl.mkact(type)
156 act.downfunc = act.activefunc = act.upfunc = dummy_callback
157 #
158 assign_members(act, descr[1:], ['al', 'data', 'name'], '')
159 #
160 # Treat actuator-specific data
161 #
162 datalist = getattrlist(descr, 'data')
163 prefix = ''
164 if type[-4:] == 'puck':
165 prefix = 'puck_'
166 elif type == 'mouse':
167 prefix = 'mouse_'
168 assign_members(act, datalist, [], prefix)
169 #
170 return act, actuatorname
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +0000171
172
173# Build all sub-actuators and add them to the super-actuator.
174# The super-actuator must already have been added to the panel.
175# Sub-actuators with defined names are added as members to the panel
176# so they can be referenced as p.name.
177#
178# Note: I have no idea how panel.endgroup() works when applied
179# to a sub-actuator.
180#
181def build_subactuators(panel, super_act, al):
Tim Peters182b5ac2004-07-18 06:16:08 +0000182 #
183 # This is nearly the same loop as below in build_panel(),
184 # except a call is made to addsubact() instead of addact().
185 #
186 for a in al:
187 act, name = build_actuator(a)
188 act.addsubact(super_act)
189 if name:
190 stmt = 'panel.' + name + ' = act'
191 if debug: print 'exec', stmt
192 exec stmt + '\n'
193 if is_endgroup(a):
194 panel.endgroup()
195 sub_al = getattrlist(a, 'al')
196 if sub_al:
197 build_subactuators(panel, act, sub_al)
198 #
199 # Fix the actuator to which whe just added subactuators.
200 # This can't hurt (I hope) and is needed for the scroll actuator.
201 #
202 super_act.fixact()
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +0000203
204
205# Build a real panel from a panel definition.
206# Return a panel object p, where for each named actuator a, p.name is a
207# reference to a.
208#
209def build_panel(descr):
Tim Peters182b5ac2004-07-18 06:16:08 +0000210 #
211 # Sanity check
212 #
213 if (not descr) or descr[0] != 'panel':
214 raise panel_error, 'panel description must start with "panel"'
215 #
216 if debug: show_panel('', descr)
217 #
218 # Create an empty panel
219 #
220 panel = pnl.mkpanel()
221 #
222 # Assign panel attributes
223 #
224 assign_members(panel, descr[1:], ['al'], '')
225 #
226 # Look for actuator list
227 #
228 al = getattrlist(descr, 'al')
229 #
230 # The order in which actuators are created is important
231 # because of the endgroup() operator.
232 # Unfortunately the Panel Editor outputs the actuator list
233 # in reverse order, so we reverse it here.
234 #
235 al = reverse(al)
236 #
237 for a in al:
238 act, name = build_actuator(a)
239 act.addact(panel)
240 if name:
241 stmt = 'panel.' + name + ' = act'
242 exec stmt + '\n'
243 if is_endgroup(a):
244 panel.endgroup()
245 sub_al = getattrlist(a, 'al')
246 if sub_al:
247 build_subactuators(panel, act, sub_al)
248 #
249 return panel
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +0000250
251
252# Wrapper around pnl.dopanel() which calls call-back functions.
253#
254def my_dopanel():
Tim Peters182b5ac2004-07-18 06:16:08 +0000255 # Extract only the first 4 elements to allow for future expansion
256 a, down, active, up = pnl.dopanel()[:4]
257 if down:
258 down.downfunc(down)
259 if active:
260 active.activefunc(active)
261 if up:
262 up.upfunc(up)
263 return a
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +0000264
265
266# Create one or more panels from a description file (S-expressions)
267# generated by the Panel Editor.
Tim Peters182b5ac2004-07-18 06:16:08 +0000268#
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +0000269def defpanellist(file):
Tim Peters182b5ac2004-07-18 06:16:08 +0000270 import panelparser
271 descrlist = panelparser.parse_file(open(file, 'r'))
272 panellist = []
273 for descr in descrlist:
274 panellist.append(build_panel(descr))
275 return panellist
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +0000276
277
278# Import everything from built-in method pnl, so the user can always
279# use panel.foo() instead of pnl.foo().
280# This gives *no* performance penalty once this module is imported.
281#
Tim Peters182b5ac2004-07-18 06:16:08 +0000282from pnl import * # for export
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +0000283
Tim Peters182b5ac2004-07-18 06:16:08 +0000284dopanel = my_dopanel # override pnl.dopanel