ó
t®¸Qc           @   sS   d  d l  Td d	 d „  ƒ  YZ d d
 d „  ƒ  YZ d „  Z e d k rO e ƒ  n  d S(   iÿÿÿÿ(   t   *t   WidgetRedirectorc           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sä  Support for redirecting arbitrary widget subcommands.

    Some Tk operations don't normally pass through Tkinter.  For example, if a
    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
    processes the insert without calling back into Tkinter.

    Although a binding to <Key> could be made via Tkinter, what we really want
    to do is to hook the Tk 'insert' operation itself.

    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
    operation.

    In IDLE, 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.

    c         C   se   i  |  _  | |  _ | j |  _ } | j } | d |  _ | j d | |  j ƒ | j | |  j ƒ d  S(   Nt   _origt   rename(   t   _operationst   widgett   tkt   _wt   origt   callt   createcommandt   dispatch(   t   selfR   R   t   w(    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyt   __init__   s    			c         C   s   d |  j  j j |  j  j f S(   Ns   WidgetRedirector(%s<%s>)(   R   t	   __class__t   __name__R   (   R   (    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyt   __repr__&   s    c         C   s{   x$ t  |  j ƒ D] } |  j | ƒ q W|  j } |  ` |  j } |  ` | j } | j } | j | ƒ | j d | | ƒ d  S(   NR   (	   t   listR   t
   unregisterR   R   R   R   t   deletecommandR	   (   R   t	   operationR   R   R   R   (    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyt   close*   s    	 	 		c         C   s-   | |  j  | <t |  j | | ƒ t |  | ƒ S(   N(   R   t   setattrR   t   OriginalCommand(   R   R   t   function(    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyt   register5   s    c         C   sW   | |  j  k rO |  j  | } |  j  | =t |  j | ƒ rK t |  j | ƒ n  | Sd  Sd  S(   N(   R   t   hasattrR   t   delattrt   None(   R   R   R   (    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyR   :   s    
c         G   s\   |  j  j | ƒ } y1 | r% | | Œ  S|  j j |  j | f | ƒ SWn t k
 rW d SXd S(   s  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()
        to *args to accomplish that.  For an example, see ColorDelegator.py.

        t    N(   R   t   getR   R	   R   t   TclError(   R   R   t   argst   m(    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyR   D   s    
!(	   R   t
   __module__t   __doc__R   R   R   R   R   R   (    (    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyR      s   					
R   c           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   sR   | |  _  | |  _ | j |  _ | j |  _ |  j j |  _ |  j |  j f |  _ d  S(   N(   t   redirR   R   R   R	   t   tk_callt   orig_and_operation(   R   R%   R   (    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyR   \   s    		c         C   s   d |  j  |  j f S(   Ns   OriginalCommand(%r, %r)(   R%   R   (   R   (    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyR   d   s    c         G   s   |  j  |  j | ƒ S(   N(   R&   R'   (   R   R!   (    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyt   __call__g   s    (   R   R#   R   R   R(   (    (    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyR   Z   s   		c          C   s™   t  ƒ  }  |  j d |  j ƒ t ƒ  } | j ƒ  | j ƒ  t | ƒ } d „  } | j d | ƒ a |  j	 ƒ  | j
 d ƒ | j ƒ  |  j	 ƒ  |  j ƒ  d  S(   Nt   WM_DELETE_WINDOWc          W   s   d G|  GHt  |  Œ  d  S(   Nt   insert(   t   previous_tcl_fcn(   R!   (    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyt	   my_inserts   s    	R*   (   t   Tkt   wm_protocolt   quitt   Textt   packt	   focus_setR   R   R+   t   mainloopR   R   t   destroy(   t   roott   textR%   R,   (    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyt   maink   s    		

	


t   __main__N(    (    (   t   TkinterR   R   R7   R   (    (    (    s.   /sys/lib/python2.7/idlelib/WidgetRedirector.pyt   <module>   s
   
W	