Go to the documentation of this file.00001
00002
00003
00004
00005 class PyEventBinder(object):
00006 """
00007 Instances of this class are used to bind specific events to event
00008 handlers.
00009 """
00010 def __init__(self, evtType, expectedIDs=0):
00011 if expectedIDs not in [0, 1, 2]:
00012 raise ValueError, "Invalid number of expectedIDs"
00013 self.expectedIDs = expectedIDs
00014
00015 if type(evtType) == list or type(evtType) == tuple:
00016 self.evtType = evtType
00017 else:
00018 self.evtType = [evtType]
00019
00020
00021 def Bind(self, target, id1, id2, function):
00022 """Bind this set of event types to target."""
00023 for et in self.evtType:
00024 target.Connect(id1, id2, et, function)
00025
00026
00027 def Unbind(self, target, id1, id2):
00028 """Remove an event binding."""
00029 success = 0
00030 for et in self.evtType:
00031 success += target.Disconnect(id1, id2, et)
00032 return success != 0
00033
00034 def _getEvtType(self):
00035 """
00036 Make it easy to get to the default wxEventType typeID for this
00037 event binder.
00038 """
00039 return self.evtType[0]
00040
00041 typeId = property(_getEvtType)
00042
00043
00044 def __call__(self, *args):
00045 """
00046 For backwards compatibility with the old EVT_* functions.
00047 Should be called with either (window, func), (window, ID,
00048 func) or (window, ID1, ID2, func) parameters depending on the
00049 type of the event.
00050 """
00051 assert len(args) == 2 + self.expectedIDs
00052 id1 = wx.ID_ANY
00053 id2 = wx.ID_ANY
00054 target = args[0]
00055 if self.expectedIDs == 0:
00056 func = args[1]
00057 elif self.expectedIDs == 1:
00058 id1 = args[1]
00059 func = args[2]
00060 elif self.expectedIDs == 2:
00061 id1 = args[1]
00062 id2 = args[2]
00063 func = args[3]
00064 else:
00065 raise ValueError, "Unexpected number of IDs"
00066
00067 self.Bind(target, id1, id2, func)
00068
00069
00070
00071 def EVT_COMMAND(win, id, cmd, func):
00072 win.Connect(id, -1, cmd, func)
00073 def EVT_COMMAND_RANGE(win, id1, id2, cmd, func):
00074 win.Connect(id1, id2, cmd, func)
00075
00076
00077