blob: d18daf05863c183d6a0e6c42b9518541e74f3cd8 [file] [log] [blame]
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -04001from idlelib.delegator import Delegator
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -04002from idlelib.redirector import WidgetRedirector
David Scherer7aced172000-08-15 01:13:23 +00003
Terry Jan Reedy092473e2016-05-17 22:48:22 -04004
David Scherer7aced172000-08-15 01:13:23 +00005class Percolator:
6
7 def __init__(self, text):
8 # XXX would be nice to inherit from Delegator
9 self.text = text
10 self.redir = WidgetRedirector(text)
11 self.top = self.bottom = Delegator(text)
12 self.bottom.insert = self.redir.register("insert", self.insert)
13 self.bottom.delete = self.redir.register("delete", self.delete)
14 self.filters = []
15
16 def close(self):
17 while self.top is not self.bottom:
18 self.removefilter(self.top)
19 self.top = None
Terry Jan Reedy092473e2016-05-17 22:48:22 -040020 self.bottom.setdelegate(None)
21 self.bottom = None
22 self.redir.close()
23 self.redir = None
David Scherer7aced172000-08-15 01:13:23 +000024 self.text = None
25
26 def insert(self, index, chars, tags=None):
27 # Could go away if inheriting from Delegator
28 self.top.insert(index, chars, tags)
29
30 def delete(self, index1, index2=None):
31 # Could go away if inheriting from Delegator
32 self.top.delete(index1, index2)
33
34 def insertfilter(self, filter):
35 # Perhaps rename to pushfilter()?
36 assert isinstance(filter, Delegator)
37 assert filter.delegate is None
38 filter.setdelegate(self.top)
39 self.top = filter
40
41 def removefilter(self, filter):
42 # XXX Perhaps should only support popfilter()?
43 assert isinstance(filter, Delegator)
44 assert filter.delegate is not None
45 f = self.top
46 if f is filter:
47 self.top = filter.delegate
48 filter.setdelegate(None)
49 else:
50 while f.delegate is not filter:
51 assert f is not self.bottom
52 f.resetcache()
53 f = f.delegate
54 f.setdelegate(filter.delegate)
55 filter.setdelegate(None)
56
Terry Jan Reedy092473e2016-05-17 22:48:22 -040057
58def _percolator(parent): # htest #
Terry Jan Reedya2fc99e2014-05-25 18:44:05 -040059 import tkinter as tk
Terry Jan Reedy092473e2016-05-17 22:48:22 -040060
David Scherer7aced172000-08-15 01:13:23 +000061 class Tracer(Delegator):
62 def __init__(self, name):
63 self.name = name
64 Delegator.__init__(self, None)
Terry Jan Reedy092473e2016-05-17 22:48:22 -040065
David Scherer7aced172000-08-15 01:13:23 +000066 def insert(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000067 print(self.name, ": insert", args)
Raymond Hettinger931237e2003-07-09 18:48:24 +000068 self.delegate.insert(*args)
Terry Jan Reedy092473e2016-05-17 22:48:22 -040069
David Scherer7aced172000-08-15 01:13:23 +000070 def delete(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000071 print(self.name, ": delete", args)
Raymond Hettinger931237e2003-07-09 18:48:24 +000072 self.delegate.delete(*args)
Terry Jan Reedy092473e2016-05-17 22:48:22 -040073
74 box = tk.Toplevel(parent)
75 box.title("Test Percolator")
Terry Jan Reedya7480322016-07-10 17:28:10 -040076 x, y = map(int, parent.geometry().split('+')[1:])
77 box.geometry("+%d+%d" % (x, y + 175))
Terry Jan Reedy092473e2016-05-17 22:48:22 -040078 text = tk.Text(box)
David Scherer7aced172000-08-15 01:13:23 +000079 p = Percolator(text)
Terry Jan Reedy092473e2016-05-17 22:48:22 -040080 pin = p.insertfilter
81 pout = p.removefilter
David Scherer7aced172000-08-15 01:13:23 +000082 t1 = Tracer("t1")
83 t2 = Tracer("t2")
Terry Jan Reedya2fc99e2014-05-25 18:44:05 -040084
85 def toggle1():
Terry Jan Reedy092473e2016-05-17 22:48:22 -040086 (pin if var1.get() else pout)(t1)
Terry Jan Reedya2fc99e2014-05-25 18:44:05 -040087 def toggle2():
Terry Jan Reedy092473e2016-05-17 22:48:22 -040088 (pin if var2.get() else pout)(t2)
Terry Jan Reedya2fc99e2014-05-25 18:44:05 -040089
90 text.pack()
Terry Jan Reedyb60adc52016-06-21 18:41:38 -040091 var1 = tk.IntVar(parent)
Terry Jan Reedy092473e2016-05-17 22:48:22 -040092 cb1 = tk.Checkbutton(box, text="Tracer1", command=toggle1, variable=var1)
Terry Jan Reedya2fc99e2014-05-25 18:44:05 -040093 cb1.pack()
Terry Jan Reedyb60adc52016-06-21 18:41:38 -040094 var2 = tk.IntVar(parent)
Terry Jan Reedy092473e2016-05-17 22:48:22 -040095 cb2 = tk.Checkbutton(box, text="Tracer2", command=toggle2, variable=var2)
Terry Jan Reedya2fc99e2014-05-25 18:44:05 -040096 cb2.pack()
97
David Scherer7aced172000-08-15 01:13:23 +000098if __name__ == "__main__":
Terry Jan Reedy092473e2016-05-17 22:48:22 -040099 import unittest
100 unittest.main('idlelib.idle_test.test_percolator', verbosity=2,
101 exit=False)
102
Terry Jan Reedya2fc99e2014-05-25 18:44:05 -0400103 from idlelib.idle_test.htest import run
104 run(_percolator)