redirector.py 6.73 KB
Newer Older
1
from tkinter import TclError
David Scherer's avatar
David Scherer committed
2 3

class WidgetRedirector:
4 5
    """Support for redirecting arbitrary widget subcommands.

6
    Some Tk operations don't normally pass through tkinter.  For example, if a
7 8
    character is inserted into a Text widget by pressing a key, a default Tk
    binding to the widget's 'insert' operation is activated, and the Tk library
9
    processes the insert without calling back into tkinter.
10

11 12 13
    Although a binding to <Key> could be made via tkinter, what we really want
    to do is to hook the Tk 'insert' operation itself.  For one thing, we want
    a text.insert call in idle code to have the same effect as a key press.
David Scherer's avatar
David Scherer committed
14

15 16 17 18
    When a widget is instantiated, a Tcl command is created whose name is the
    same as the pathname widget._w.  This command is used to invoke the various
    widget operations, e.g. insert (for a Text widget). We are going to hook
    this command and provide a facility ('register') to intercept the widget
19 20
    operation.  We will also intercept method calls on the tkinter class
    instance that represents the tk widget.
21

22 23 24 25
    In IDLE, WidgetRedirector is used in Percolator to intercept Text
    commands.  The function being registered provides access to the top
    of a Percolator chain.  At the bottom of the chain is a call to the
    original Tk widget operation.
26
    """
David Scherer's avatar
David Scherer committed
27
    def __init__(self, widget):
28 29 30 31 32 33 34 35 36 37
        '''Initialize attributes and setup redirection.

        _operations: dict mapping operation name to new function.
        widget: the widget whose tcl command is to be intercepted.
        tk: widget.tk, a convenience attribute, probably not needed.
        orig: new name of the original tcl command.

        Since renaming to orig fails with TclError when orig already
        exists, only one WidgetDirector can exist for a given widget.
        '''
38 39 40 41
        self._operations = {}
        self.widget = widget            # widget instance
        self.tk = tk = widget.tk        # widget's root
        w = widget._w                   # widget's (full) Tk pathname
David Scherer's avatar
David Scherer committed
42
        self.orig = w + "_orig"
43
        # Rename the Tcl command within Tcl:
David Scherer's avatar
David Scherer committed
44
        tk.call("rename", w, self.orig)
45 46
        # Create a new Tcl command whose name is the widget's pathname, and
        # whose action is to dispatch on the operation passed to the widget:
David Scherer's avatar
David Scherer committed
47 48 49
        tk.createcommand(w, self.dispatch)

    def __repr__(self):
50 51 52
        return "%s(%s<%s>)" % (self.__class__.__name__,
                               self.widget.__class__.__name__,
                               self.widget._w)
David Scherer's avatar
David Scherer committed
53 54

    def close(self):
55
        "Unregister operations and revert redirection created by .__init__."
56 57
        for operation in list(self._operations):
            self.unregister(operation)
58
        widget = self.widget
David Scherer's avatar
David Scherer committed
59 60
        tk = widget.tk
        w = widget._w
61
        # Restore the original widget Tcl command.
David Scherer's avatar
David Scherer committed
62
        tk.deletecommand(w)
63 64 65
        tk.call("rename", self.orig, w)
        del self.widget, self.tk  # Should not be needed
        # if instance is deleted after close, as in Percolator.
David Scherer's avatar
David Scherer committed
66

67
    def register(self, operation, function):
68 69
        '''Return OriginalCommand(operation) after registering function.

70
        Registration adds an operation: function pair to ._operations.
71
        It also adds a widget function attribute that masks the tkinter
72 73 74 75 76
        class instance method.  Method masking operates independently
        from command dispatch.

        If a second function is registered for the same operation, the
        first function is replaced in both places.
77
        '''
78 79 80 81 82
        self._operations[operation] = function
        setattr(self.widget, operation, function)
        return OriginalCommand(self, operation)

    def unregister(self, operation):
83 84 85 86
        '''Return the function for the operation, or None.

        Deleting the instance attribute unmasks the class attribute.
        '''
87 88 89
        if operation in self._operations:
            function = self._operations[operation]
            del self._operations[operation]
90
            try:
91
                delattr(self.widget, operation)
92 93
            except AttributeError:
                pass
David Scherer's avatar
David Scherer committed
94 95 96 97
            return function
        else:
            return None

98 99 100 101 102 103 104 105 106
    def dispatch(self, operation, *args):
        '''Callback from Tcl which runs when the widget is referenced.

        If an operation has been registered in self._operations, apply the
        associated function to the args passed into Tcl. Otherwise, pass the
        operation through to Tk via the original Tcl function.

        Note that if a registered function is called, the operation is not
        passed through to Tk.  Apply the function returned by self.register()
107
        to *args to accomplish that.  For an example, see colorizer.py.
108 109 110

        '''
        m = self._operations.get(operation)
David Scherer's avatar
David Scherer committed
111 112
        try:
            if m:
113
                return m(*args)
David Scherer's avatar
David Scherer committed
114
            else:
115
                return self.tk.call((self.orig, operation) + args)
David Scherer's avatar
David Scherer committed
116 117 118 119 120
        except TclError:
            return ""


class OriginalCommand:
121 122 123 124 125 126 127 128 129
    '''Callable for original tk command that has been redirected.

    Returned by .register; can be used in the function registered.
    redir = WidgetRedirector(text)
    def my_insert(*args):
        print("insert", args)
        original_insert(*args)
    original_insert = redir.register("insert", my_insert)
    '''
David Scherer's avatar
David Scherer committed
130

131
    def __init__(self, redir, operation):
132 133 134 135 136
        '''Create .tk_call and .orig_and_operation for .__call__ method.

        .redir and .operation store the input args for __repr__.
        .tk and .orig copy attributes of .redir (probably not needed).
        '''
David Scherer's avatar
David Scherer committed
137
        self.redir = redir
138
        self.operation = operation
139 140 141 142 143
        self.tk = redir.tk  # redundant with self.redir
        self.orig = redir.orig  # redundant with self.redir
        # These two could be deleted after checking recipient code.
        self.tk_call = redir.tk.call
        self.orig_and_operation = (redir.orig, operation)
David Scherer's avatar
David Scherer committed
144 145

    def __repr__(self):
146 147
        return "%s(%r, %r)" % (self.__class__.__name__,
                               self.redir, self.operation)
David Scherer's avatar
David Scherer committed
148 149

    def __call__(self, *args):
150
        return self.tk_call(self.orig_and_operation + args)
David Scherer's avatar
David Scherer committed
151 152


153
def _widget_redirector(parent):  # htest #
154
    from tkinter import Toplevel, Text
155

156 157
    top = Toplevel(parent)
    top.title("Test WidgetRedirector")
158 159
    x, y = map(int, parent.geometry().split('+')[1:])
    top.geometry("+%d+%d" % (x, y + 175))
160
    text = Text(top)
David Scherer's avatar
David Scherer committed
161 162 163 164
    text.pack()
    text.focus_set()
    redir = WidgetRedirector(text)
    def my_insert(*args):
165
        print("insert", args)
166 167
        original_insert(*args)
    original_insert = redir.register("insert", my_insert)
David Scherer's avatar
David Scherer committed
168 169

if __name__ == "__main__":
170
    import unittest
171
    unittest.main('idlelib.idle_test.test_redirector',
172
                  verbosity=2, exit=False)
173

174 175
    from idlelib.idle_test.htest import run
    run(_widget_redirector)