0001 from WidgetRedirector import WidgetRedirector 0002 from Delegator import Delegator 0003 0004 class Percolator: 0005 0006 def __init__(self, text): 0007 # XXX would be nice to inherit from Delegator 0008 self.text = text 0009 self.redir = WidgetRedirector(text) 0010 self.top = self.bottom = Delegator(text) 0011 self.bottom.insert = self.redir.register("insert", self.insert) 0012 self.bottom.delete = self.redir.register("delete", self.delete) 0013 self.filters = [] 0014 0015 def close(self): 0016 while self.top is not self.bottom: 0017 self.removefilter(self.top) 0018 self.top = None 0019 self.bottom.setdelegate(None); self.bottom = None 0020 self.redir.close(); self.redir = None 0021 self.text = None 0022 0023 def insert(self, index, chars, tags=None): 0024 # Could go away if inheriting from Delegator 0025 self.top.insert(index, chars, tags) 0026 0027 def delete(self, index1, index2=None): 0028 # Could go away if inheriting from Delegator 0029 self.top.delete(index1, index2) 0030 0031 def insertfilter(self, filter): 0032 # Perhaps rename to pushfilter()? 0033 assert isinstance(filter, Delegator) 0034 assert filter.delegate is None 0035 filter.setdelegate(self.top) 0036 self.top = filter 0037 0038 def removefilter(self, filter): 0039 # XXX Perhaps should only support popfilter()? 0040 assert isinstance(filter, Delegator) 0041 assert filter.delegate is not None 0042 f = self.top 0043 if f is filter: 0044 self.top = filter.delegate 0045 filter.setdelegate(None) 0046 else: 0047 while f.delegate is not filter: 0048 assert f is not self.bottom 0049 f.resetcache() 0050 f = f.delegate 0051 f.setdelegate(filter.delegate) 0052 filter.setdelegate(None) 0053 0054 0055 def main(): 0056 class Tracer(Delegator): 0057 def __init__(self, name): 0058 self.name = name 0059 Delegator.__init__(self, None) 0060 def insert(self, *args): 0061 print self.name, ": insert", args 0062 self.delegate.insert(*args) 0063 def delete(self, *args): 0064 print self.name, ": delete", args 0065 self.delegate.delete(*args) 0066 root = Tk() 0067 root.wm_protocol("WM_DELETE_WINDOW", root.quit) 0068 text = Text() 0069 text.pack() 0070 text.focus_set() 0071 p = Percolator(text) 0072 t1 = Tracer("t1") 0073 t2 = Tracer("t2") 0074 p.insertfilter(t1) 0075 p.insertfilter(t2) 0076 root.mainloop() 0077 p.removefilter(t2) 0078 root.mainloop() 0079 p.insertfilter(t2) 0080 p.removefilter(t1) 0081 root.mainloop() 0082 0083 if __name__ == "__main__": 0084 from Tkinter import * 0085 main() 0086
Generated by PyXR 0.9.4