1990-10-13 20:23:40 +01:00
|
|
|
# Module 'panel'
|
|
|
|
#
|
|
|
|
# Support for the Panel library.
|
|
|
|
# Uses built-in module 'pnl'.
|
|
|
|
# Applciations 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):
|
1992-01-01 20:35:13 +01:00
|
|
|
return type(x) == type([])
|
1990-10-13 20:23:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
# 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:
|
1992-01-01 20:35:13 +01:00
|
|
|
if item and is_list(item) and item[0] == name:
|
1990-10-13 20:23:40 +01:00
|
|
|
return item[1:]
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
# Get a property of a list, which may itself be another list.
|
|
|
|
#
|
|
|
|
def getproplist(list, name):
|
|
|
|
for item in list:
|
1992-01-01 20:35:13 +01:00
|
|
|
if item and is_list(item) and item[0] == 'prop':
|
|
|
|
if len(item) > 1 and item[1] == name:
|
1990-10-13 20:23:40 +01:00
|
|
|
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')
|
1992-01-01 20:35:13 +01:00
|
|
|
return (x and x[0] == '#t')
|
1990-10-13 20:23:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
# 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
|
1992-01-01 20:35:13 +01:00
|
|
|
elif item and item[0] == 'al':
|
1990-10-13 20:23:40 +01:00
|
|
|
print prefix, 'Subactuator list:'
|
|
|
|
for a in item[1:]:
|
|
|
|
show_actuator(prefix + ' ', a)
|
1992-01-01 20:35:13 +01:00
|
|
|
elif len(item) == 2:
|
1990-10-13 20:23:40 +01:00
|
|
|
print prefix, item[0], '=>', item[1]
|
1992-01-01 20:35:13 +01:00
|
|
|
elif len(item) == 3 and item[0] == 'prop':
|
1990-10-13 20:23:40 +01:00
|
|
|
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
|
1992-01-01 20:35:13 +01:00
|
|
|
elif item and item[0] == 'al':
|
1990-10-13 20:23:40 +01:00
|
|
|
print prefix, 'Actuator list:'
|
|
|
|
for a in item[1:]:
|
|
|
|
show_actuator(prefix + ' ', a)
|
1992-01-01 20:35:13 +01:00
|
|
|
elif len(item) == 2:
|
1990-10-13 20:23:40 +01:00
|
|
|
print prefix, item[0], '=>', item[1]
|
1992-01-01 20:35:13 +01:00
|
|
|
elif len(item) == 3 and item[0] == 'prop':
|
1990-10-13 20:23:40 +01:00
|
|
|
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:
|
1992-01-01 20:35:13 +01:00
|
|
|
if is_list(item) and len(item) == 2 and item[0] not in exclist:
|
1990-10-13 20:23:40 +01:00
|
|
|
name, value = item[0], item[1]
|
|
|
|
ok = 1
|
|
|
|
if value[0] in '-0123456789':
|
|
|
|
value = eval(value)
|
1992-01-01 20:35:13 +01:00
|
|
|
elif value[0] == '"':
|
1990-10-13 20:23:40 +01:00
|
|
|
value = value[1:-1]
|
1992-01-01 20:35:13 +01:00
|
|
|
elif value == 'move-then-resize':
|
1990-10-13 20:23:40 +01:00
|
|
|
# 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 + '=' + `value`
|
|
|
|
if debug: print 'exec', stmt
|
|
|
|
try:
|
1995-09-19 00:00:37 +02:00
|
|
|
exec stmt + '\n'
|
1990-10-13 20:23:40 +01:00
|
|
|
except KeyboardInterrupt: # Don't catch this!
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
except:
|
|
|
|
print 'assign failed:', stmt
|
|
|
|
|
|
|
|
|
|
|
|
# Build a real actuator from an actuator descriptior.
|
|
|
|
# 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]
|
1992-01-01 20:35:13 +01:00
|
|
|
if type[:4] == 'pnl_': type = type[4:]
|
1990-10-13 20:23:40 +01:00
|
|
|
act = pnl.mkact(type)
|
|
|
|
act.downfunc = act.activefunc = act.upfunc = dummy_callback
|
|
|
|
#
|
1991-01-23 14:45:17 +01:00
|
|
|
assign_members(act, descr[1:], ['al', 'data', 'name'], '')
|
1990-10-13 20:23:40 +01:00
|
|
|
#
|
|
|
|
# Treat actuator-specific data
|
|
|
|
#
|
|
|
|
datalist = getattrlist(descr, 'data')
|
|
|
|
prefix = ''
|
1992-01-01 20:35:13 +01:00
|
|
|
if type[-4:] == 'puck':
|
1990-10-13 20:23:40 +01:00
|
|
|
prefix = 'puck_'
|
1992-01-01 20:35:13 +01:00
|
|
|
elif type == 'mouse':
|
1990-10-13 20:23:40 +01:00
|
|
|
prefix = 'mouse_'
|
1991-01-23 14:45:17 +01:00
|
|
|
assign_members(act, datalist, [], prefix)
|
1990-10-13 20:23:40 +01:00
|
|
|
#
|
|
|
|
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
|
1995-09-19 00:00:37 +02:00
|
|
|
exec stmt + '\n'
|
1990-10-13 20:23:40 +01:00
|
|
|
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
|
|
|
|
#
|
1991-01-23 14:45:17 +01:00
|
|
|
assign_members(panel, descr[1:], ['al'], '')
|
1990-10-13 20:23:40 +01:00
|
|
|
#
|
|
|
|
# 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'
|
1995-09-19 00:00:37 +02:00
|
|
|
exec stmt + '\n'
|
1990-10-13 20:23:40 +01:00
|
|
|
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):
|
1991-01-23 14:45:17 +01:00
|
|
|
import panelparser
|
|
|
|
descrlist = panelparser.parse_file(open(file, 'r'))
|
1990-10-13 20:23:40 +01:00
|
|
|
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
|