3 # Support for the Panel library.
4 # Uses built-in module 'pnl'.
5 # Applciations should use 'panel.function' instead of 'pnl.function';
6 # 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.
17 # Test if an object is a list.
20 return type(x
) == type([])
32 # Get an attribute of a list, which may itself be another list.
33 # Don't use 'prop' for name.
35 def getattrlist(list, name
):
37 if item
and is_list(item
) and item
[0] == name
:
42 # Get a property of a list, which may itself be another list.
44 def getproplist(list, name
):
46 if item
and is_list(item
) and item
[0] == 'prop':
47 if len(item
) > 1 and item
[1] == name
:
52 # Test if an actuator description contains the property 'end-of-group'
54 def is_endgroup(list):
55 x
= getproplist(list, 'end-of-group')
56 return (x
and x
[0] == '#t')
59 # Neatly display an actuator definition given as S-expression
60 # the prefix string is printed before each line.
62 def show_actuator(prefix
, a
):
66 elif item
and item
[0] == 'al':
67 print prefix
, 'Subactuator list:'
69 show_actuator(prefix
+ ' ', a
)
71 print prefix
, item
[0], '=>', item
[1]
72 elif len(item
) == 3 and item
[0] == 'prop':
73 print prefix
, 'Prop', item
[1], '=>',
76 print prefix
, '?', item
79 # Neatly display a panel.
81 def show_panel(prefix
, p
):
85 elif item
and item
[0] == 'al':
86 print prefix
, 'Actuator list:'
88 show_actuator(prefix
+ ' ', a
)
90 print prefix
, item
[0], '=>', item
[1]
91 elif len(item
) == 3 and item
[0] == 'prop':
92 print prefix
, 'Prop', item
[1], '=>',
95 print prefix
, '?', item
98 # Exception raised by build_actuator or build_panel.
100 panel_error
= 'panel error'
103 # Dummy callback used to initialize the callbacks.
105 def dummy_callback(arg
):
109 # Assign attributes to members of the target.
110 # Attribute names in exclist are ignored.
111 # The member name is the attribute name prefixed with the prefix.
113 def assign_members(target
, attrlist
, exclist
, prefix
):
114 for item
in attrlist
:
115 if is_list(item
) and len(item
) == 2 and item
[0] not in exclist
:
116 name
, value
= item
[0], item
[1]
118 if value
[0] in '-0123456789':
120 elif value
[0] == '"':
122 elif value
== 'move-then-resize':
123 # Strange default set by Panel Editor...
126 print 'unknown value', value
, 'for', name
129 lhs
= 'target.' + prefix
+ name
130 stmt
= lhs
+ '=' + `value`
131 if debug
: print 'exec', stmt
134 except KeyboardInterrupt: # Don't catch this!
135 raise KeyboardInterrupt
137 print 'assign failed:', stmt
140 # Build a real actuator from an actuator descriptior.
141 # Return a pair (actuator, name).
143 def build_actuator(descr
):
144 namelist
= getattrlist(descr
, 'name')
146 # Assume it is a string
147 actuatorname
= namelist
[0][1:-1]
151 if type[:4] == 'pnl_': type = type[4:]
152 act
= pnl
.mkact(type)
153 act
.downfunc
= act
.activefunc
= act
.upfunc
= dummy_callback
155 assign_members(act
, descr
[1:], ['al', 'data', 'name'], '')
157 # Treat actuator-specific data
159 datalist
= getattrlist(descr
, 'data')
161 if type[-4:] == 'puck':
163 elif type == 'mouse':
165 assign_members(act
, datalist
, [], prefix
)
167 return act
, actuatorname
170 # Build all sub-actuators and add them to the super-actuator.
171 # The super-actuator must already have been added to the panel.
172 # Sub-actuators with defined names are added as members to the panel
173 # so they can be referenced as p.name.
175 # Note: I have no idea how panel.endgroup() works when applied
178 def build_subactuators(panel
, super_act
, al
):
180 # This is nearly the same loop as below in build_panel(),
181 # except a call is made to addsubact() instead of addact().
184 act
, name
= build_actuator(a
)
185 act
.addsubact(super_act
)
187 stmt
= 'panel.' + name
+ ' = act'
188 if debug
: print 'exec', stmt
192 sub_al
= getattrlist(a
, 'al')
194 build_subactuators(panel
, act
, sub_al
)
196 # Fix the actuator to which whe just added subactuators.
197 # This can't hurt (I hope) and is needed for the scroll actuator.
202 # Build a real panel from a panel definition.
203 # Return a panel object p, where for each named actuator a, p.name is a
206 def build_panel(descr
):
210 if (not descr
) or descr
[0] <> 'panel':
211 raise panel_error
, 'panel description must start with "panel"'
213 if debug
: show_panel('', descr
)
215 # Create an empty panel
217 panel
= pnl
.mkpanel()
219 # Assign panel attributes
221 assign_members(panel
, descr
[1:], ['al'], '')
223 # Look for actuator list
225 al
= getattrlist(descr
, 'al')
227 # The order in which actuators are created is important
228 # because of the endgroup() operator.
229 # Unfortunately the Panel Editor outputs the actuator list
230 # in reverse order, so we reverse it here.
235 act
, name
= build_actuator(a
)
238 stmt
= 'panel.' + name
+ ' = act'
242 sub_al
= getattrlist(a
, 'al')
244 build_subactuators(panel
, act
, sub_al
)
249 # Wrapper around pnl.dopanel() which calls call-back functions.
252 # Extract only the first 4 elements to allow for future expansion
253 a
, down
, active
, up
= pnl
.dopanel()[:4]
257 active
.activefunc(active
)
263 # Create one or more panels from a description file (S-expressions)
264 # generated by the Panel Editor.
266 def defpanellist(file):
268 descrlist
= panelparser
.parse_file(open(file, 'r'))
270 for descr
in descrlist
:
271 panellist
.append(build_panel(descr
))
275 # Import everything from built-in method pnl, so the user can always
276 # use panel.foo() instead of pnl.foo().
277 # This gives *no* performance penalty once this module is imported.
279 from pnl
import * # for export
281 dopanel
= my_dopanel
# override pnl.dopanel