blob: ba5251ff71f48c8bed3b834e1c26f2ef4a916f53 [file] [log] [blame]
Georg Brandl14fc4272008-05-17 18:39:55 +00001from tkinter import *
David Scherer7aced172000-08-15 01:13:23 +00002
David Scherer7aced172000-08-15 01:13:23 +00003class WidgetRedirector:
4
Guido van Rossum8ce8a782007-11-01 19:42:39 +00005 """Support for redirecting arbitrary widget subcommands.
David Scherer7aced172000-08-15 01:13:23 +00006
Guido van Rossum8ce8a782007-11-01 19:42:39 +00007 Some Tk operations don't normally pass through Tkinter. For example, if a
8 character is inserted into a Text widget by pressing a key, a default Tk
9 binding to the widget's 'insert' operation is activated, and the Tk library
10 processes the insert without calling back into Tkinter.
11
12 Although a binding to <Key> could be made via Tkinter, what we really want
13 to do is to hook the Tk 'insert' operation itself.
14
15 When a widget is instantiated, a Tcl command is created whose name is the
16 same as the pathname widget._w. This command is used to invoke the various
17 widget operations, e.g. insert (for a Text widget). We are going to hook
18 this command and provide a facility ('register') to intercept the widget
19 operation.
20
21 In IDLE, the function being registered provides access to the top of a
22 Percolator chain. At the bottom of the chain is a call to the original
23 Tk widget operation.
24
25 """
David Scherer7aced172000-08-15 01:13:23 +000026 def __init__(self, widget):
Guido van Rossum8ce8a782007-11-01 19:42:39 +000027 self._operations = {}
28 self.widget = widget # widget instance
29 self.tk = tk = widget.tk # widget's root
30 w = widget._w # widget's (full) Tk pathname
David Scherer7aced172000-08-15 01:13:23 +000031 self.orig = w + "_orig"
Guido van Rossum8ce8a782007-11-01 19:42:39 +000032 # Rename the Tcl command within Tcl:
David Scherer7aced172000-08-15 01:13:23 +000033 tk.call("rename", w, self.orig)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000034 # Create a new Tcl command whose name is the widget's pathname, and
35 # whose action is to dispatch on the operation passed to the widget:
David Scherer7aced172000-08-15 01:13:23 +000036 tk.createcommand(w, self.dispatch)
37
38 def __repr__(self):
39 return "WidgetRedirector(%s<%s>)" % (self.widget.__class__.__name__,
40 self.widget._w)
41
42 def close(self):
Guido van Rossum8ce8a782007-11-01 19:42:39 +000043 for operation in list(self._operations):
44 self.unregister(operation)
David Scherer7aced172000-08-15 01:13:23 +000045 widget = self.widget; del self.widget
46 orig = self.orig; del self.orig
47 tk = widget.tk
48 w = widget._w
49 tk.deletecommand(w)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000050 # restore the original widget Tcl command:
David Scherer7aced172000-08-15 01:13:23 +000051 tk.call("rename", orig, w)
52
Guido van Rossum8ce8a782007-11-01 19:42:39 +000053 def register(self, operation, function):
54 self._operations[operation] = function
55 setattr(self.widget, operation, function)
56 return OriginalCommand(self, operation)
David Scherer7aced172000-08-15 01:13:23 +000057
Guido van Rossum8ce8a782007-11-01 19:42:39 +000058 def unregister(self, operation):
59 if operation in self._operations:
60 function = self._operations[operation]
61 del self._operations[operation]
62 if hasattr(self.widget, operation):
63 delattr(self.widget, operation)
David Scherer7aced172000-08-15 01:13:23 +000064 return function
65 else:
66 return None
67
Guido van Rossum8ce8a782007-11-01 19:42:39 +000068 def dispatch(self, operation, *args):
69 '''Callback from Tcl which runs when the widget is referenced.
70
71 If an operation has been registered in self._operations, apply the
72 associated function to the args passed into Tcl. Otherwise, pass the
73 operation through to Tk via the original Tcl function.
74
75 Note that if a registered function is called, the operation is not
76 passed through to Tk. Apply the function returned by self.register()
77 to *args to accomplish that. For an example, see ColorDelegator.py.
78
79 '''
80 m = self._operations.get(operation)
David Scherer7aced172000-08-15 01:13:23 +000081 try:
82 if m:
Raymond Hettinger931237e2003-07-09 18:48:24 +000083 return m(*args)
David Scherer7aced172000-08-15 01:13:23 +000084 else:
Guido van Rossum8ce8a782007-11-01 19:42:39 +000085 return self.tk.call((self.orig, operation) + args)
David Scherer7aced172000-08-15 01:13:23 +000086 except TclError:
87 return ""
88
89
90class OriginalCommand:
91
Guido van Rossum8ce8a782007-11-01 19:42:39 +000092 def __init__(self, redir, operation):
David Scherer7aced172000-08-15 01:13:23 +000093 self.redir = redir
Guido van Rossum8ce8a782007-11-01 19:42:39 +000094 self.operation = operation
David Scherer7aced172000-08-15 01:13:23 +000095 self.tk = redir.tk
96 self.orig = redir.orig
97 self.tk_call = self.tk.call
Guido van Rossum8ce8a782007-11-01 19:42:39 +000098 self.orig_and_operation = (self.orig, self.operation)
David Scherer7aced172000-08-15 01:13:23 +000099
100 def __repr__(self):
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000101 return "OriginalCommand(%r, %r)" % (self.redir, self.operation)
David Scherer7aced172000-08-15 01:13:23 +0000102
103 def __call__(self, *args):
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000104 return self.tk_call(self.orig_and_operation + args)
David Scherer7aced172000-08-15 01:13:23 +0000105
106
107def main():
108 root = Tk()
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000109 root.wm_protocol("WM_DELETE_WINDOW", root.quit)
David Scherer7aced172000-08-15 01:13:23 +0000110 text = Text()
111 text.pack()
112 text.focus_set()
113 redir = WidgetRedirector(text)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000114 global previous_tcl_fcn
David Scherer7aced172000-08-15 01:13:23 +0000115 def my_insert(*args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000116 print("insert", args)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000117 previous_tcl_fcn(*args)
118 previous_tcl_fcn = redir.register("insert", my_insert)
David Scherer7aced172000-08-15 01:13:23 +0000119 root.mainloop()
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000120 redir.unregister("insert") # runs after first 'close window'
121 redir.close()
122 root.mainloop()
123 root.destroy()
David Scherer7aced172000-08-15 01:13:23 +0000124
125if __name__ == "__main__":
126 main()