| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ License |
|---|
| 4 |
##~ |
|---|
| 5 |
##- The RuneBlade Foundation library is intended to ease some |
|---|
| 6 |
##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.) |
|---|
| 7 |
##- applications, while providing the flexibility to modularly change the |
|---|
| 8 |
##- architecture. Enjoy. |
|---|
| 9 |
##~ |
|---|
| 10 |
##~ Copyright (C) 2002 TechGame Networks, LLC. |
|---|
| 11 |
##~ |
|---|
| 12 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 13 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 14 |
##~ LICENSE file included with this distribution. |
|---|
| 15 |
##~ |
|---|
| 16 |
##~ TechGame Networks, LLC can be reached at: |
|---|
| 17 |
##~ 3578 E. Hartsel Drive #211 |
|---|
| 18 |
##~ Colorado Springs, Colorado, USA, 80920 |
|---|
| 19 |
##~ |
|---|
| 20 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 21 |
|
|---|
| 22 |
"""A subject/observer implementation of a wxEvtHandler""" |
|---|
| 23 |
|
|---|
| 24 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 25 |
#~ Imports |
|---|
| 26 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 27 |
|
|---|
| 28 |
from wxPython import wx |
|---|
| 29 |
|
|---|
| 30 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 31 |
#~ Definitions |
|---|
| 32 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 33 |
|
|---|
| 34 |
class wxEventSubject(object): |
|---|
| 35 |
__slots__ = ['observers'] |
|---|
| 36 |
|
|---|
| 37 |
def __init__(self): |
|---|
| 38 |
self.observers = [] |
|---|
| 39 |
|
|---|
| 40 |
def Connect(self, function, *args): |
|---|
| 41 |
self.observers.insert(0, (function, args)) |
|---|
| 42 |
|
|---|
| 43 |
def Disconnect(self, function, *args): |
|---|
| 44 |
self.observers = [x for x in self.observers if x[0] != function] |
|---|
| 45 |
|
|---|
| 46 |
def Notify(self, evt, *args): |
|---|
| 47 |
result = NotImplemented |
|---|
| 48 |
for observer, obsargs in self.observers: |
|---|
| 49 |
if evt.GetSkipped(): |
|---|
| 50 |
# Restore it to not skipped |
|---|
| 51 |
evt.Skip(False) |
|---|
| 52 |
result = observer(evt, *(args+obsargs)) |
|---|
| 53 |
|
|---|
| 54 |
if result is NotImplemented: |
|---|
| 55 |
evt.Skip() |
|---|
| 56 |
|
|---|
| 57 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 58 |
|
|---|
| 59 |
class wxSubObsEvtHandler(wx.wxEvtHandler): |
|---|
| 60 |
__slots__ = ['_subject_map'] |
|---|
| 61 |
|
|---|
| 62 |
def ReplaceEvtHandlerOf(self, eventhandler): |
|---|
| 63 |
eventhandler.Connect = self.Connect |
|---|
| 64 |
eventhandler.Disconnect = self.Disconnect |
|---|
| 65 |
try: |
|---|
| 66 |
# Push the event handler |
|---|
| 67 |
eventhandler.PushEventHandler(self) |
|---|
| 68 |
except AttributeError: |
|---|
| 69 |
# Replace the event handler |
|---|
| 70 |
next = eventhandler.GetNextHandler() |
|---|
| 71 |
if next is not None: |
|---|
| 72 |
next.SetPreviousHandler(self) |
|---|
| 73 |
self.SetNextHandler(next) |
|---|
| 74 |
|
|---|
| 75 |
eventhandler.SetNextHandler(self) |
|---|
| 76 |
self.SetPreviousHandler(eventhandler) |
|---|
| 77 |
|
|---|
| 78 |
def Connect(self, id, lastid, eventType, function, *args, **kw): |
|---|
| 79 |
if lastid < 0: wxidrange = (id,) |
|---|
| 80 |
else: wxidrange = range(id, lastid) |
|---|
| 81 |
|
|---|
| 82 |
# Iterate through the id range manually |
|---|
| 83 |
for wxid in wxidrange: |
|---|
| 84 |
key = (wxid, -1, eventType) |
|---|
| 85 |
try: subject = self.SubjectMap[key] |
|---|
| 86 |
except KeyError: |
|---|
| 87 |
subject = wxEventSubject() |
|---|
| 88 |
self.SubjectMap[key] = subject |
|---|
| 89 |
# Connect the subject to the event |
|---|
| 90 |
subjectargs = key + (subject.Notify,) |
|---|
| 91 |
wx.wxEvtHandler.Connect(self, *subjectargs) |
|---|
| 92 |
|
|---|
| 93 |
#connect the function to the subject |
|---|
| 94 |
subject.Connect(function, *args, **kw) |
|---|
| 95 |
|
|---|
| 96 |
def Disconnect(self, id, lastid, eventType, function, *args): |
|---|
| 97 |
if lastid < 0: wxidrange = (id,) |
|---|
| 98 |
else: wxidrange = range(id, lastid) |
|---|
| 99 |
|
|---|
| 100 |
# Iterate through the id range manually |
|---|
| 101 |
for wxid in wxidrange: |
|---|
| 102 |
key = (wxid, -1, eventType) |
|---|
| 103 |
try: subject = self.SubjectMap[key] |
|---|
| 104 |
except KeyError: pass |
|---|
| 105 |
else: subject.Disconnect(function, *args) |
|---|
| 106 |
|
|---|
| 107 |
def _GetSubjectMap(self): |
|---|
| 108 |
try: |
|---|
| 109 |
return self._subject_map |
|---|
| 110 |
except AttributeError: |
|---|
| 111 |
self._subject_map = {} |
|---|
| 112 |
return self._subject_map |
|---|
| 113 |
SubjectMap = property(_GetSubjectMap) |
|---|
| 114 |
|
|---|