1 """The Bindings module manages an infrastructure for key bindings."""
3 # Event = (context, sequence ...) wobei alle elsts strings sind
5 # gebuynbden wird ein Action object. Jede Action hat einen namen unter
6 # der sie registriert wird. die funktionen zum definieren eines
7 # keymnap und zum binden von keys nehmen diesen namen als argument =>
8 # alle Aktionen m"ussen vorher registriert werden
10 # KeyStack => KeyMap => Binding => Action
12 # Das Binding Objekt enthaelt das Event unter welchem die Action
13 # gebunden ist. Jedes Binding aht auch einen label zum anzeigen des
16 import Node, Actions, Events, Logger
18 class Binding(Node.Node):
20 action = Node.NodeReference()
22 def __init__(self, event, label, action):
23 Node.Node.__init__(self)
32 Logger.log('binding', 'execute: %s' % self.action.name)
36 class KeyMap(Node.Node):
40 def __init__(self, name=None):
41 Node.Node.__init__(self)
44 def add(self, binding):
45 if self.owner : raise RuntimeError, 'changing an active KeyMap not supported'
46 self._map[binding.event] = binding
48 def unbind(self, event):
49 if self.owner : raise RuntimeError, 'changing an active KeyMap not supported'
56 if self.owner : return self.owner.indexOf(self)
62 def lookup(self, event):
63 return self._map.get(event,None)
69 class KeyList(Node.Node):
71 _list = Node.NodeList()
74 Node.Node.__init__(self)
77 def indexOf(self, map):
78 return self._list.index(map)
80 def append(self, map):
81 self._list.append(map)
84 def prepend(self, map):
85 self._list[:0] = [map]
88 def removeMap(self, map):
89 for i in (range(len(self._list))):
90 if self._list[i] is map:
91 self._list[i:i+1] = []
94 def replaceFromIndex(self, index, maps):
95 self._list[index:] = maps
98 def lookup(self, event):
99 for i in range(len(self._list)-1,-1,-1):
100 binding = self._list[i].lookup(event)
101 if binding is not None : return binding
106 for map in self._list:
107 bindings.update(map.bindings())
110 def bindingsByContext(self):
112 for map in self._list:
113 for binding in map.bindings().itervalues():
114 bindings.setdefault(binding.event.context,{})
115 bindings[binding.event.context][binding.event] = binding
118 def registerCallback(self, cb):
119 self._callbacks.append(cb)
121 def unregisterCallback(self, cb):
122 self._callbacks.remove(cb)
124 def _callCallbacks(self):
125 for cb in self._callbacks : cb(self)
128 return ' / '.join([ map.name() for map in self._list if map.name() ])