| 1 | n/a | from tkinter import TclError |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | class WidgetRedirector: |
|---|
| 4 | n/a | """Support for redirecting arbitrary widget subcommands. |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | Some Tk operations don't normally pass through tkinter. For example, if a |
|---|
| 7 | n/a | character is inserted into a Text widget by pressing a key, a default Tk |
|---|
| 8 | n/a | binding to the widget's 'insert' operation is activated, and the Tk library |
|---|
| 9 | n/a | processes the insert without calling back into tkinter. |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | Although a binding to <Key> could be made via tkinter, what we really want |
|---|
| 12 | n/a | to do is to hook the Tk 'insert' operation itself. For one thing, we want |
|---|
| 13 | n/a | a text.insert call in idle code to have the same effect as a key press. |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | When a widget is instantiated, a Tcl command is created whose name is the |
|---|
| 16 | n/a | same as the pathname widget._w. This command is used to invoke the various |
|---|
| 17 | n/a | widget operations, e.g. insert (for a Text widget). We are going to hook |
|---|
| 18 | n/a | this command and provide a facility ('register') to intercept the widget |
|---|
| 19 | n/a | operation. We will also intercept method calls on the tkinter class |
|---|
| 20 | n/a | instance that represents the tk widget. |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | In IDLE, WidgetRedirector is used in Percolator to intercept Text |
|---|
| 23 | n/a | commands. The function being registered provides access to the top |
|---|
| 24 | n/a | of a Percolator chain. At the bottom of the chain is a call to the |
|---|
| 25 | n/a | original Tk widget operation. |
|---|
| 26 | n/a | """ |
|---|
| 27 | n/a | def __init__(self, widget): |
|---|
| 28 | n/a | '''Initialize attributes and setup redirection. |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | _operations: dict mapping operation name to new function. |
|---|
| 31 | n/a | widget: the widget whose tcl command is to be intercepted. |
|---|
| 32 | n/a | tk: widget.tk, a convenience attribute, probably not needed. |
|---|
| 33 | n/a | orig: new name of the original tcl command. |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | Since renaming to orig fails with TclError when orig already |
|---|
| 36 | n/a | exists, only one WidgetDirector can exist for a given widget. |
|---|
| 37 | n/a | ''' |
|---|
| 38 | n/a | self._operations = {} |
|---|
| 39 | n/a | self.widget = widget # widget instance |
|---|
| 40 | n/a | self.tk = tk = widget.tk # widget's root |
|---|
| 41 | n/a | w = widget._w # widget's (full) Tk pathname |
|---|
| 42 | n/a | self.orig = w + "_orig" |
|---|
| 43 | n/a | # Rename the Tcl command within Tcl: |
|---|
| 44 | n/a | tk.call("rename", w, self.orig) |
|---|
| 45 | n/a | # Create a new Tcl command whose name is the widget's pathname, and |
|---|
| 46 | n/a | # whose action is to dispatch on the operation passed to the widget: |
|---|
| 47 | n/a | tk.createcommand(w, self.dispatch) |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | def __repr__(self): |
|---|
| 50 | n/a | return "%s(%s<%s>)" % (self.__class__.__name__, |
|---|
| 51 | n/a | self.widget.__class__.__name__, |
|---|
| 52 | n/a | self.widget._w) |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | def close(self): |
|---|
| 55 | n/a | "Unregister operations and revert redirection created by .__init__." |
|---|
| 56 | n/a | for operation in list(self._operations): |
|---|
| 57 | n/a | self.unregister(operation) |
|---|
| 58 | n/a | widget = self.widget |
|---|
| 59 | n/a | tk = widget.tk |
|---|
| 60 | n/a | w = widget._w |
|---|
| 61 | n/a | # Restore the original widget Tcl command. |
|---|
| 62 | n/a | tk.deletecommand(w) |
|---|
| 63 | n/a | tk.call("rename", self.orig, w) |
|---|
| 64 | n/a | del self.widget, self.tk # Should not be needed |
|---|
| 65 | n/a | # if instance is deleted after close, as in Percolator. |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | def register(self, operation, function): |
|---|
| 68 | n/a | '''Return OriginalCommand(operation) after registering function. |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | Registration adds an operation: function pair to ._operations. |
|---|
| 71 | n/a | It also adds a widget function attribute that masks the tkinter |
|---|
| 72 | n/a | class instance method. Method masking operates independently |
|---|
| 73 | n/a | from command dispatch. |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | If a second function is registered for the same operation, the |
|---|
| 76 | n/a | first function is replaced in both places. |
|---|
| 77 | n/a | ''' |
|---|
| 78 | n/a | self._operations[operation] = function |
|---|
| 79 | n/a | setattr(self.widget, operation, function) |
|---|
| 80 | n/a | return OriginalCommand(self, operation) |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | def unregister(self, operation): |
|---|
| 83 | n/a | '''Return the function for the operation, or None. |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | Deleting the instance attribute unmasks the class attribute. |
|---|
| 86 | n/a | ''' |
|---|
| 87 | n/a | if operation in self._operations: |
|---|
| 88 | n/a | function = self._operations[operation] |
|---|
| 89 | n/a | del self._operations[operation] |
|---|
| 90 | n/a | try: |
|---|
| 91 | n/a | delattr(self.widget, operation) |
|---|
| 92 | n/a | except AttributeError: |
|---|
| 93 | n/a | pass |
|---|
| 94 | n/a | return function |
|---|
| 95 | n/a | else: |
|---|
| 96 | n/a | return None |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | def dispatch(self, operation, *args): |
|---|
| 99 | n/a | '''Callback from Tcl which runs when the widget is referenced. |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | If an operation has been registered in self._operations, apply the |
|---|
| 102 | n/a | associated function to the args passed into Tcl. Otherwise, pass the |
|---|
| 103 | n/a | operation through to Tk via the original Tcl function. |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | Note that if a registered function is called, the operation is not |
|---|
| 106 | n/a | passed through to Tk. Apply the function returned by self.register() |
|---|
| 107 | n/a | to *args to accomplish that. For an example, see colorizer.py. |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | ''' |
|---|
| 110 | n/a | m = self._operations.get(operation) |
|---|
| 111 | n/a | try: |
|---|
| 112 | n/a | if m: |
|---|
| 113 | n/a | return m(*args) |
|---|
| 114 | n/a | else: |
|---|
| 115 | n/a | return self.tk.call((self.orig, operation) + args) |
|---|
| 116 | n/a | except TclError: |
|---|
| 117 | n/a | return "" |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | class OriginalCommand: |
|---|
| 121 | n/a | '''Callable for original tk command that has been redirected. |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | Returned by .register; can be used in the function registered. |
|---|
| 124 | n/a | redir = WidgetRedirector(text) |
|---|
| 125 | n/a | def my_insert(*args): |
|---|
| 126 | n/a | print("insert", args) |
|---|
| 127 | n/a | original_insert(*args) |
|---|
| 128 | n/a | original_insert = redir.register("insert", my_insert) |
|---|
| 129 | n/a | ''' |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def __init__(self, redir, operation): |
|---|
| 132 | n/a | '''Create .tk_call and .orig_and_operation for .__call__ method. |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | .redir and .operation store the input args for __repr__. |
|---|
| 135 | n/a | .tk and .orig copy attributes of .redir (probably not needed). |
|---|
| 136 | n/a | ''' |
|---|
| 137 | n/a | self.redir = redir |
|---|
| 138 | n/a | self.operation = operation |
|---|
| 139 | n/a | self.tk = redir.tk # redundant with self.redir |
|---|
| 140 | n/a | self.orig = redir.orig # redundant with self.redir |
|---|
| 141 | n/a | # These two could be deleted after checking recipient code. |
|---|
| 142 | n/a | self.tk_call = redir.tk.call |
|---|
| 143 | n/a | self.orig_and_operation = (redir.orig, operation) |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | def __repr__(self): |
|---|
| 146 | n/a | return "%s(%r, %r)" % (self.__class__.__name__, |
|---|
| 147 | n/a | self.redir, self.operation) |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | def __call__(self, *args): |
|---|
| 150 | n/a | return self.tk_call(self.orig_and_operation + args) |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | def _widget_redirector(parent): # htest # |
|---|
| 154 | n/a | from tkinter import Toplevel, Text |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | top = Toplevel(parent) |
|---|
| 157 | n/a | top.title("Test WidgetRedirector") |
|---|
| 158 | n/a | x, y = map(int, parent.geometry().split('+')[1:]) |
|---|
| 159 | n/a | top.geometry("+%d+%d" % (x, y + 175)) |
|---|
| 160 | n/a | text = Text(top) |
|---|
| 161 | n/a | text.pack() |
|---|
| 162 | n/a | text.focus_set() |
|---|
| 163 | n/a | redir = WidgetRedirector(text) |
|---|
| 164 | n/a | def my_insert(*args): |
|---|
| 165 | n/a | print("insert", args) |
|---|
| 166 | n/a | original_insert(*args) |
|---|
| 167 | n/a | original_insert = redir.register("insert", my_insert) |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | if __name__ == "__main__": |
|---|
| 170 | n/a | import unittest |
|---|
| 171 | n/a | unittest.main('idlelib.idle_test.test_redirector', |
|---|
| 172 | n/a | verbosity=2, exit=False) |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | from idlelib.idle_test.htest import run |
|---|
| 175 | n/a | run(_widget_redirector) |
|---|