| 1 | |
| 2 | |
| 3 | #--------------------------------------------------------------------------- |
| 4 | |
| 5 | class PyEventBinder(object): |
| 6 | """ |
| 7 | Instances of this class are used to bind specific events to event |
| 8 | handlers. |
| 9 | """ |
| 10 | def __init__(self, evtType, expectedIDs=0): |
| 11 | if expectedIDs not in [0, 1, 2]: |
| 12 | raise ValueError, "Invalid number of expectedIDs" |
| 13 | self.expectedIDs = expectedIDs |
| 14 | |
| 15 | if type(evtType) == list or type(evtType) == tuple: |
| 16 | self.evtType = evtType |
| 17 | else: |
| 18 | self.evtType = [evtType] |
| 19 | |
| 20 | |
| 21 | def Bind(self, target, id1, id2, function): |
| 22 | """Bind this set of event types to target.""" |
| 23 | for et in self.evtType: |
| 24 | target.Connect(id1, id2, et, function) |
| 25 | |
| 26 | |
| 27 | def Unbind(self, target, id1, id2): |
| 28 | """Remove an event binding.""" |
| 29 | success = 0 |
| 30 | for et in self.evtType: |
| 31 | success += target.Disconnect(id1, id2, et) |
| 32 | return success != 0 |
| 33 | |
| 34 | |
| 35 | def __call__(self, *args): |
| 36 | """ |
| 37 | For backwards compatibility with the old EVT_* functions. |
| 38 | Should be called with either (window, func), (window, ID, |
| 39 | func) or (window, ID1, ID2, func) parameters depending on the |
| 40 | type of the event. |
| 41 | """ |
| 42 | assert len(args) == 2 + self.expectedIDs |
| 43 | id1 = wx.ID_ANY |
| 44 | id2 = wx.ID_ANY |
| 45 | target = args[0] |
| 46 | if self.expectedIDs == 0: |
| 47 | func = args[1] |
| 48 | elif self.expectedIDs == 1: |
| 49 | id1 = args[1] |
| 50 | func = args[2] |
| 51 | elif self.expectedIDs == 2: |
| 52 | id1 = args[1] |
| 53 | id2 = args[2] |
| 54 | func = args[3] |
| 55 | else: |
| 56 | raise ValueError, "Unexpected number of IDs" |
| 57 | |
| 58 | self.Bind(target, id1, id2, func) |
| 59 | |
| 60 | |
| 61 | # These two are square pegs that don't fit the PyEventBinder hole... |
| 62 | def EVT_COMMAND(win, id, cmd, func): |
| 63 | win.Connect(id, -1, cmd, func) |
| 64 | def EVT_COMMAND_RANGE(win, id1, id2, cmd, func): |
| 65 | win.Connect(id1, id2, cmd, func) |
| 66 | |
| 67 | |
| 68 | #--------------------------------------------------------------------------- |