|
From: Kevin A. <ka...@us...> - 2004-05-04 20:49:51
|
Update of /cvsroot/pythoncard/PythonCard In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18372 Modified Files: menu.py widget.py Log Message: switched to new style event binding and dispatch Index: widget.py =================================================================== RCS file: /cvsroot/pythoncard/PythonCard/widget.py,v retrieving revision 1.124 retrieving revision 1.125 diff -C2 -d -r1.124 -r1.125 *** widget.py 3 May 2004 02:56:33 -0000 1.124 --- widget.py 4 May 2004 20:49:11 -0000 1.125 *************** *** 378,382 **** event.EventLog.getInstance().log(eventName, self.name, True) if 0: ! print "dispatching", handler._name # make a lowercase alias aWxEvent.skip = aWxEvent.Skip --- 378,382 ---- event.EventLog.getInstance().log(eventName, self.name, True) if 0: ! print "dispatching", handler.__name__ # make a lowercase alias aWxEvent.skip = aWxEvent.Skip Index: menu.py =================================================================== RCS file: /cvsroot/pythoncard/PythonCard/menu.py,v retrieving revision 1.31 retrieving revision 1.32 diff -C2 -d -r1.31 -r1.32 *** menu.py 1 May 2004 18:47:50 -0000 1.31 --- menu.py 4 May 2004 20:49:11 -0000 1.32 *************** *** 32,35 **** --- 32,45 ---- """ + class MenuSelectEvent(event.SelectEvent): + binding = wx.EVT_MENU + id = wx.wxEVT_COMMAND_MENU_SELECTED + + def decorate(self, aWxEvent, source): + aWxEvent = event.Event.decorate(self, aWxEvent, source) + aWxEvent.checked = aWxEvent.IsChecked() + return aWxEvent + + class MenuItem(wx.MenuItem, component.Component): """ *************** *** 69,82 **** wx.CallAfter(self.Enable, aResource.enabled) ! # events are bound to the parent wxFrame ! # aka aParent.parent.parent ! wx.EVT_MENU(aScriptable, id, aParent.parent.doMenu) component.Component.__init__( self ) ! event.EventDispatch(self, aScriptable) aParent.parent.id2itemMap[id] = self def _getName( self ) : --- 79,211 ---- wx.CallAfter(self.Enable, aResource.enabled) ! ## # events are bound to the parent wxFrame ! ## # aka aParent.parent.parent ! ## wx.EVT_MENU(aScriptable, id, aParent.parent.doMenu) component.Component.__init__( self ) ! ## event.EventDispatch(self, aScriptable) ! self._bindEvents((MenuSelectEvent,), aScriptable, id) aParent.parent.id2itemMap[id] = self + def _bindEvents(self, eventList, aScriptable, menuId): + ## background = wx.GetTopLevelParent(self) + background = aScriptable + + # where should this check go? + # should we just set a "global" in the app instance such as + # self.stack.app.bindUnusedEvents + # this kind of thing isn't going to work for Rowland's compound + # components + if wx.GetApp()._showDebugMenu: + bindUnusedEvents = True + else: + bindUnusedEvents = False + + # helper variable to simplify test for whether to bind InsteadOfTypeEvents + # there is a good chance we will need to know + # which events are bound, if we want to dynamically add or remove + # events later, so go ahead and keep a reference to the list + self.boundEvents = {} + + self.eventIdToHandler = {} + self.wxEventIdMap = {} + + if 0: + print "\nBINDING...", self.name + + for eventClass in eventList: + self.wxEventIdMap[eventClass.id] = eventClass + if issubclass(eventClass, event.CommandTypeEvent) and self.command: + handler = background.findHandler('on_' + self.command + '_command') + if not handler: + handler = background.findHandler('on_' + self.name + '_' + eventClass.name) + else: + handler = background.findHandler('on_' + self.name + '_' + eventClass.name) + if not handler: + handler = background.findHandler('on_' + eventClass.name) + if handler or bindUnusedEvents: + if not self.boundEvents.get(eventClass.binding, None): + ## self.Bind(eventClass.binding, self._dispatch) + # KEA 2004-05-04 + # not sure yet if there is a different way to handle the bind + # apparently we have to bind the event to the parent frame + # and I think we also need the id, need to check with Robin + background.Bind(eventClass.binding, self._dispatch, id=menuId) + self.boundEvents[eventClass.binding] = eventClass.name + if handler: + if 0: + print " binding", self.name, eventClass.name, handler._name, eventClass.id + self.eventIdToHandler[eventClass.id] = handler.getFunction() + + if 0: + print "\n boundEvents:" + for name in self.boundEvents.values(): + print " ", name + print "\n\n" + print "\n self.eventIdToHandler:" + for id in self.eventIdToHandler: + # KEA 2004-05-02 + # change to just using the method directly, Handler class not needed + #print " ", id, self.eventIdToHandler[id]._function + print " ", id, self.eventIdToHandler[id] + print "\n\n" + + def _dispatch(self, aWxEvent): + eventType = aWxEvent.GetEventType() + # KEA 2004-05-04 + # for a menu item this event will always be the same + # so this is redundant unless there is some binding other than + # EVT_MENU that we're going to support?! + eventClass = self.wxEventIdMap[eventType] + + eventClassInstance = eventClass(self) + # decorate will add the relevant event attributes + aWxEvent = eventClassInstance.decorate(aWxEvent, self) + + if self.command and isinstance(eventClassInstance, event.CommandTypeEvent): + # need to report the name for the handler if it exists + eventName = 'command' + else: + if isinstance(eventClassInstance, event.InsteadOfTypeEvent): + # changes eventType if needed + # e.g. mouseDrag instead of mouseMove + eventType = eventClassInstance.translateEventType(aWxEvent) + eventName = self.wxEventIdMap[eventType].name + + # cleanup + eventClass = None + eventClassInstance = None + + handler = self.eventIdToHandler.get(eventType, None) + if handler: + event.EventLog.getInstance().log(eventName, self.name, True) + if 0: + print "dispatching", handler.__name__ + # make a lowercase alias + aWxEvent.skip = aWxEvent.Skip + + # KEA 2004-05-04 + # the menu events are always bound to the parent frame + # so the target of the event will be the "background" + background = aWxEvent.GetEventObject() + ## background = wx.GetTopLevelParent(self) + + handler(background, aWxEvent) + + # do we have to clean up this alias? + aWxEvent.skip = None + # how about this local reference to handler? + handler = None + background = None + else: + event.EventLog.getInstance().log(eventName, self.name, False) + # hopefully this is all we need to do for "unused events" + aWxEvent.Skip() + + # cleanup + aWxEvent.target = aWxEvent.eventObject = None + def _getName( self ) : |