# HG changeset patch # User spq@33b003aa-7bff-0310-803a-e67f0ece8222 # Date 1224082624 0 # Node ID 5b04a7d3ded6f51cccda64218543c55a3dbf7eee # Parent 31718fa356f8dc507796705b974b55819e92d72e typo fix some missing event (de)registrations ability to capture one event multiple times (instead of only having one slot, we have groups and each group has an own slot, the default group is "default" so we are backward compatible diff -r 31718fa356f8 -r 5b04a7d3ded6 engine/extensions/pychan/events.py --- a/engine/extensions/pychan/events.py Wed Oct 15 09:13:59 2008 +0000 +++ b/engine/extensions/pychan/events.py Wed Oct 15 14:57:04 2008 +0000 @@ -42,7 +42,7 @@ if "mouse" in name: return MOUSE_EVENT if "key" in name: - return MOUSE_EVENT + return KEY_EVENT return ACTION_EVENT @@ -81,7 +81,8 @@ event = self.translateEvent(getEventType(name), event) if name in self.events: if self.debug: print "-"*self.indent, name - self.events[name]( event ) + for f in self.events[name].itervalues(): + f( event ) self.indent -= 4 def translateEvent(self,event_type,event): @@ -143,6 +144,7 @@ if not self.listener.events: return if self.debug: print "Attach:",self + self.widget.real_widget.addKeyListener( self.listener ) self.widget.real_widget.addMouseListener( self.listener ) self.widget.real_widget.addActionListener( self.listener ) self.is_attached = True @@ -156,23 +158,22 @@ if not self.is_attached: return if self.debug: print "Detach:",self + self.widget.real_widget.removeKeyListener( self.listener ) self.widget.real_widget.removeMouseListener( self.listener ) self.widget.real_widget.removeActionListener( self.listener ) self.is_attached = False - def capture(self,event_name,callback): + def capture(self,event_name,callback,group_name): if event_name not in EVENTS: raise exceptions.RuntimeError("Unknown eventname: " + event_name) - if callback is None and not self.isCaptured(event_name): - if self.debug: - print CALLBACK_NONE_MESSAGE % str(self.widget) - return - if callback is None: - del self.listener.events[event_name] - if not self.listener.events: - self.detach() + if self.isCaptured(event_name): + del self.listener.events[event_name][group_name] + if not self.listener.events: + self.detach() + elif self.debug: + print CALLBACK_NONE_MESSAGE % str(self.widget) return if not callable(callback): @@ -181,11 +182,14 @@ def captured_f(event): tools.applyOnlySuitable(callback,event=event,widget=self.widget) - self.listener.events[event_name] = captured_f + if event_name not in self.listener.events: + self.listener.events[event_name] = {group_name : captured_f} + else: + self.listener.events[event_name][group_name] = captured_f self.attach() - def isCaptured(self,event_name): - return event_name in self.listener.events + def isCaptured(self,event_name,group_name): + return event_name in self.listener.events and group_name in self.listener.events[event_name] def getCapturedEvents(self): return self.listener.events.keys() @@ -194,11 +198,13 @@ def splitEventDescriptor(name): """ Utility function to split "widgetName/eventName" descriptions into tuples. """ L = name.split("/") + if len(L) not in (1,2,3): + raise exceptions.RuntimeError("Invalid widgetname / eventname combination: " + name) if len(L) == 1: L = L[0],"action" - if len(L) != 2: - raise exceptions.RuntimeError("Invalid widgetname / eventname combination: " + name) - if L[1] not in EVENTS: + elif L[1] not in EVENTS: raise exceptions.RuntimeError("Unknown event name: " + name) + if len(L) == 2: + L = L[0],L[1],"default" return L diff -r 31718fa356f8 -r 5b04a7d3ded6 engine/extensions/pychan/widgets.py --- a/engine/extensions/pychan/widgets.py Wed Oct 15 09:13:59 2008 +0000 +++ b/engine/extensions/pychan/widgets.py Wed Oct 15 14:57:04 2008 +0000 @@ -166,7 +166,7 @@ return False return True - def capture(self, callback, event_name="action"): + def capture(self, callback, event_name="action", group_name="default"): """ Add a callback to be executed when the widget event occurs on this widget. @@ -180,7 +180,7 @@ @param callback: Event callback - may accept keyword arguments event and widget. @paran event_name: The event to capture - may be one of L{events.EVENTS} and defaults to "action" """ - self.event_mapper.capture( event_name, callback ) + self.event_mapper.capture( event_name, callback, group_name ) def isCaptured(self): """ @@ -340,11 +340,11 @@ """ for descr,func in eventMap.items(): - name, event_name = events.splitEventDescriptor(descr) - print name, event_name + name, event_name, group_name = events.splitEventDescriptor(descr) + print name, event_name, group_name widget = self.findChild(name=name) if widget: - widget.capture( func, event_name = event_name ) + widget.capture( func, event_name = event_name, group_name = group_name ) elif not ignoreMissing: raise RuntimeError("No widget with the name: %s" % name)