
    "ebL                        d Z ddlZddlZdZdZdZdZdZdZd	Z	d
Z
dZdZdZd Zd Z G d de      Z e       Zej&                  Zej(                  Zej*                  Zej,                  Zej.                  Zej0                  Zej2                  Zej4                  Zej6                  Zej8                  Zej:                  Zej<                  Zej>                  Zej@                  Z ejB                  Z!ejD                  Z"ejF                  Z#ejH                  Z$ejJ                  Z%ejL                  Z&ejN                  Z'ejP                  Z(ejR                  Z)ejT                  Z+ejX                  Z,ejZ                  Z-ej\                  Z.ddZ/g dZ0y)z6
Inputhook management for GUI event loop integration.
    Nwxqtqt4qt5gtktkosxglutpygletgtk3nonec                       y)z Ignore CTRL+C (not implemented).N r       9/usr/lib/python3/dist-packages/pydev_ipython/inputhook.pyignore_CTRL_Cr   *       r   c                       y)z+Take CTRL+C into account (not implemented).Nr   r   r   r   allow_CTRL_Cr   /   r   r   c                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d d
Zd dZd dZd Zd dZd dZd Zd dZd Zd dZd Zd dZd Zd dZd Zd dZd Zd dZd Zd dZd Zd Z y	)!InputHookManagerzManage PyOS_InputHook for different GUI toolkits.

    This class installs various hooks under ``PyOSInputHook`` to handle
    GUI event loop integration.
    c                 N    d | _         i | _        | j                          d| _        y NF)_return_control_callback_apps_resetpyplot_importedselfs    r   __init__zInputHookManager.__init__?   s"    (,%
$r   c                 .    d | _         d | _        d | _        y N)_callback_pyfunctype	_callback_current_guir   s    r   r   zInputHookManager._resetE   s    $(! r   c                     || _         y r"   r   )r   return_control_callbacks     r   set_return_control_callbackz,InputHookManager.set_return_control_callbackJ   s
    (?%r   c                     | j                   S r"   r'   r   s    r   get_return_control_callbackz,InputHookManager.get_return_control_callbackM   s    ,,,r   c                 "    | j                         S r"   r'   r   s    r   return_controlzInputHookManager.return_controlP   s    ,,..r   c                     | j                   S r"   r$   r   s    r   get_inputhookzInputHookManager.get_inputhookS   s    ~~r   c                     || _         y)zSet inputhook to callback.Nr/   )r   callbacks     r   set_inputhookzInputHookManager.set_inputhookV   s     "r   Nc                 $    | j                          y)a  Clear input hook.

        Parameters
        ----------
        app : optional, ignored
          This parameter is allowed only so that clear_inputhook() can be
          called with a similar interface as all the ``enable_*`` methods.  But
          the actual value of the parameter is ignored.  This uniform interface
          makes it easier to have user-level entry points in the main IPython
          app like :meth:`enable_gui`.N)r   )r   apps     r   clear_inputhookz InputHookManager.clear_inputhook\   s     	r   c                 P    |i | _         y|| j                   v r| j                   |= yy)a{  Clear IPython's internal reference to an application instance.

        Whenever we create an app for a user on qt4 or wx, we hold a
        reference to the app.  This is needed because in some cases bad things
        can happen if a user doesn't hold a reference themselves.  This
        method is provided to clear the references we are holding.

        Parameters
        ----------
        gui : None or str
            If None, clear all app references.  If ('wx', 'qt4') clear
            the app for that toolkit.  References are not held for gtk or tk
            as those toolkits don't have the notion of an app.
        N)r   )r   guis     r   clear_app_refszInputHookManager.clear_app_refsi   s,     ;DJDJJ

3 r   c                 V   ddl }ddlm}  ||j                        j                  }|ddgk  rt        d|j                  z        ddlm} | j                  |       t        | _
        ||j                         }||j                  dd	      }d
|_        || j                  t        <   |S )a  Enable event loop integration with wxPython.

        Parameters
        ----------
        app : WX Application, optional.
            Running application to use.  If not given, we probe WX for an
            existing application object, and create a new one if none is found.

        Notes
        -----
        This methods sets the ``PyOS_InputHook`` for wxPython, which allows
        the wxPython to integrate with terminal based applications like
        IPython.

        If ``app`` is not given we probe for an existing one, and return it if
        found.  If no existing app is found, we create an :class:`wx.App` as
        follows::

            import wx
            app = wx.App(redirect=False, clearSigInt=False)
        r   N)LooseVersion      z)requires wxPython >= 2.8, but you have %s)inputhook_wxF)redirectclearSigIntT)r   distutils.versionr;   __version__version
ValueErrorpydev_ipython.inputhookwxr>   r3   GUI_WXr%   GetAppApp_in_event_loopr   )r   r5   r   V
wx_versionr>   s         r   	enable_wxzInputHookManager.enable_wx}   s    , 	7r~~&..
AH2>>YZZ:<(";))+C;&&%U&;C! 

6
r   c                 x    t         | j                  v rd| j                  t            _        | j                          y)zhDisable event loop integration with wxPython.

        This merely sets PyOS_InputHook to NULL.
        FN)rF   r   rI   r6   r   s    r   
disable_wxzInputHookManager.disable_wx   s-    
 TZZ05DJJv-r   c                 d    ddl m}m} ||k(  r| j                  |       y | j	                  |       y )Nr   )QT_APIQT_API_PYQT5)pydev_ipython.qt_for_kernelrP   rQ   
enable_qt5
enable_qt4)r   r5   rP   rQ   s       r   	enable_qtzInputHookManager.enable_qt   s%    D\!OOC OOC r   c                     ddl m}  || |      \  }}| j                  |       t        | _        d|_        || j                  t        <   |S )a  Enable event loop integration with PyQt4.

        Parameters
        ----------
        app : Qt Application, optional.
            Running application to use.  If not given, we probe Qt for an
            existing application object, and create a new one if none is found.

        Notes
        -----
        This methods sets the PyOS_InputHook for PyQt4, which allows
        the PyQt4 to integrate with terminal based applications like
        IPython.

        If ``app`` is not given we probe for an existing one, and return it if
        found.  If no existing app is found, we create an :class:`QApplication`
        as follows::

            from PyQt4 import QtCore
            app = QtGui.QApplication(sys.argv)
        r   )create_inputhook_qt4T)pydev_ipython.inputhookqt4rW   r3   GUI_QT4r%   rI   r   )r   r5   rW   inputhook_qt4s       r   rT   zInputHookManager.enable_qt4   sJ    , 	D1$<]=)#!!

7
r   c                 x    t         | j                  v rd| j                  t            _        | j                          y)zeDisable event loop integration with PyQt4.

        This merely sets PyOS_InputHook to NULL.
        FN)rY   r   rI   r6   r   s    r   disable_qt4zInputHookManager.disable_qt4   s-    
 djj 16DJJw.r   c                     ddl m}  || |      \  }}| j                  |       t        | _        d|_        || j                  t        <   |S )Nr   )create_inputhook_qt5T)pydev_ipython.inputhookqt5r^   r3   GUI_QT5r%   rI   r   )r   r5   r^   inputhook_qt5s       r   rS   zInputHookManager.enable_qt5   sG    C1$<]=)#!!

7
r   c                 x    t         | j                  v rd| j                  t            _        | j                          y r   )r`   r   rI   r6   r   s    r   disable_qt5zInputHookManager.disable_qt5   s+    djj 16DJJw.r   c                 h    ddl m} | j                   || j                               t        | _        y)a  Enable event loop integration with PyGTK.

        Parameters
        ----------
        app : ignored
           Ignored, it's only a placeholder to keep the call signature of all
           gui activation methods consistent, which simplifies the logic of
           supporting magics.

        Notes
        -----
        This methods sets the PyOS_InputHook for PyGTK, which allows
        the PyGTK to integrate with terminal based applications like
        IPython.
        r   )create_inputhook_gtkN)pydev_ipython.inputhookgtkre   r3   _stdin_fileGUI_GTKr%   )r   r5   re   s      r   
enable_gtkzInputHookManager.enable_gtk   s*      	D/0@0@AB#r   c                 $    | j                          yzeDisable event loop integration with PyGTK.

        This merely sets PyOS_InputHook to NULL.
        Nr6   r   s    r   disable_gtkzInputHookManager.disable_gtk      
 	r   c                     t         | _        |8	 ddl}|j	                         }|j                          || j                  t         <   ddlm} | j                   ||             |S #  ddl}Y ZxY w)a1  Enable event loop integration with Tk.

        Parameters
        ----------
        app : toplevel :class:`Tkinter.Tk` widget, optional.
            Running toplevel widget to use.  If not given, we probe Tk for an
            existing one, and create a new one if none is found.

        Notes
        -----
        If you have already created a :class:`Tkinter.Tk` object, the only
        thing done by this method is to register with the
        :class:`InputHookManager`, since creating that object automatically
        sets ``PyOS_InputHook``.
        Nr   )create_inputhook_tk)
GUI_TKr%   TkintertkinterTkwithdrawr   pydev_ipython.inputhooktkrp   r3   )r   r5   _TKrp   s       r   	enable_tkzInputHookManager.enable_tk  sc      #;&% &&(CLLN!$DJJvA.s34
&%s   A% %A-c                 $    | j                          y)zgDisable event loop integration with Tkinter.

        This merely sets PyOS_InputHook to NULL.
        Nrl   r   s    r   
disable_tkzInputHookManager.disable_tk'  rn   r   c                    ddl m} ddlm}m}m}m}m} t        | j                  vrt        t        dg       }|j                  |       |j                  |       t        |j                        r&|j                  |j                   |j"                         |j%                  t'        |      dkD  r|d   nd       |j)                  dd       |j+                          |j-                  |       |j/                  |       |j1                  |       n3|j-                  |       |j/                  |       |j1                  |       | j3                  |       t        | _        d| j                  t        <   y)aR   Enable event loop integration with GLUT.

        Parameters
        ----------

        app : ignored
            Ignored, it's only a placeholder to keep the call signature of all
            gui activation methods consistent, which simplifies the logic of
            supporting magics.

        Notes
        -----

        This methods sets the PyOS_InputHook for GLUT, which allows the GLUT to
        integrate with terminal based applications like IPython. Due to GLUT
        limitations, it is currently not possible to start the event loop
        without first creating a window. You should thus not create another
        window but use instead the created one. See 'gui-glut.py' in the
        docs/examples/lib directory.

        The default screen mode is set to:
        glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH
        r   N)glut_display_mode
glut_closeglut_display	glut_idleinputhook_glutargv    T)OpenGL.GLUTGLUTpydev_ipython.inputhookglutr|   r}   r~   r   r   GUI_GLUTr   getattrsysglutInitglutInitDisplayModeboolglutSetOptionGLUT_ACTION_ON_WINDOW_CLOSE GLUT_ACTION_GLUTMAINLOOP_RETURNSglutCreateWindowlenglutReshapeWindowglutHideWindowglutWMCloseFuncglutDisplayFuncglutIdleFuncr3   r%   )	r   r5   r
   r|   r}   r~   r   r   r   s	            r   enable_glutzInputHookManager.enable_glut.  s*   2 	#	H 	H 4::%3+DMM$$$%67D&&'""4#C#C$($I$IK!!SY]$q'C""1a(!  ,  .i(  ,  .i(>*$#

8r   c                 j    ddl m} ddlm} |j	                           |        | j                          y)zDisable event loop integration with glut.

        This sets PyOS_InputHook to NULL and set the display function to a
        dummy one and set the timer to a dummy timer that will be triggered
        very far in the future.
        r   N)glutMainLoopEvent)r   r   glut_supportr   r   r6   )r   r
   r   s      r   disable_glutzInputHookManager.disable_glutb  s(     	#2r   c                 J    ddl m} | j                  |       t        | _        |S )a  Enable event loop integration with pyglet.

        Parameters
        ----------
        app : ignored
           Ignored, it's only a placeholder to keep the call signature of all
           gui activation methods consistent, which simplifies the logic of
           supporting magics.

        Notes
        -----
        This methods sets the ``PyOS_InputHook`` for pyglet, which allows
        pyglet to integrate with terminal based applications like
        IPython.

        r   )inputhook_pyglet)pydev_ipython.inputhookpygletr   r3   
GUI_PYGLETr%   )r   r5   r   s      r   enable_pygletzInputHookManager.enable_pygletp  s$    " 	C+,&
r   c                 $    | j                          y)zfDisable event loop integration with pyglet.

        This merely sets PyOS_InputHook to NULL.
        Nrl   r   s    r   disable_pygletzInputHookManager.disable_pyglet  rn   r   c                 h    ddl m} | j                   || j                               t        | _        y)a  Enable event loop integration with Gtk3 (gir bindings).

        Parameters
        ----------
        app : ignored
           Ignored, it's only a placeholder to keep the call signature of all
           gui activation methods consistent, which simplifies the logic of
           supporting magics.

        Notes
        -----
        This methods sets the PyOS_InputHook for Gtk3, which allows
        the Gtk3 to integrate with terminal based applications like
        IPython.
        r   )create_inputhook_gtk3N)pydev_ipython.inputhookgtk3r   r3   rg   rh   r%   )r   r5   r   s      r   enable_gtk3zInputHookManager.enable_gtk3  s*      	F01A1ABC#r   c                 $    | j                          yrk   rl   r   s    r   disable_gtk3zInputHookManager.disable_gtk3  rn   r   c                 J     d fd	} j                  |       t         _        y)a   Enable event loop integration with MacOSX.

        We call function pyplot.pause, which updates and displays active
        figure during pause. It's not MacOSX-specific, but it enables to
        avoid inputhooks in native MacOSX backend.
        Also we shouldn't import pyplot, until user does it. Cause it's
        possible to choose backend before importing pyplot for the first
        time only.
        Nc                     j                   r&t        j                  d   }	 |j                  d       y dt        j                  v rd_         y y #  Y y xY w)Nzmatplotlib.pyplotg{Gz?T)r   r   modulespause)r5   pyplotr   s     r   inputhook_macz2InputHookManager.enable_mac.<locals>.inputhook_mac  sQ    ##%89LL& '#++5+/D( 6s   A Ar"   )r3   GUI_OSXr%   )r   r5   r   s   `  r   
enable_maczInputHookManager.enable_mac  s     		0 	=)#r   c                 $    | j                          y r"   rl   r   s    r   disable_maczInputHookManager.disable_mac  s    r   c                     | j                   S )z<Return a string indicating the currently active GUI or None.)r%   r   s    r   current_guizInputHookManager.current_gui  s       r   r"   )!__name__
__module____qualname____doc__r    r   r)   r+   r-   r0   r3   r6   r9   rL   rN   rU   rT   r\   rS   rc   ri   rm   rx   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   8   s    %!
@-/" ('R!>
$(>2$h,$($2!r   r   c                    t               t        d      t        t        t        t
        t        t        t        t        t        t        t        t        t        t        t         t"        t$        t&        t(        t*        t,        t.        i}	 ||    } ||      S # t0        $ r; | | dk(  rt        }n*d| dt3        |j5                               }t        |      Y Kw xY w)ay  Switch amongst GUI input hooks by name.

    This is just a utility wrapper around the methods of the InputHookManager
    object.

    Parameters
    ----------
    gui : optional, string or None
      If None (or 'none'), clears input hook, otherwise it must be one
      of the recognized GUI names (see ``GUI_*`` constants in module).

    app : optional, existing application object.
      For toolkits that have the concept of a global app, you can supply an
      existing one.  If not given, the toolkit will be probed for one, and if
      none is found, a new one will be created.  Note that GTK does not have
      this concept, and passing an app if ``gui=="GTK"`` will raise an error.

    Returns
    -------
    The output of the underlying gui switch routine, typically the actual
    PyOS_InputHook wrapper object or the GUI toolkit app created, if there was
    one.
    zUA return_control_callback must be supplied as a reference before a gui can be enabledr   zInvalid GUI request z, valid ones are:)r+   rD   GUI_NONEr6   r   r   rq   rx   rh   ri   rF   rL   GUI_QTrU   rY   rT   r`   rS   r   r   r   r   GUI_GTK3r   KeyErrorlistkeys)r8   r5   guisgui_hookes        r   
enable_guir     s    2 #$,pqqoZIZIIZZkkD 9 C=   ;#)&H $ @CDDUVAQ-  s   B ACC)+rF   r   rY   r`   rh   rq   r   r   r   r   r   r   r   r   inputhook_managerrL   rN   rU   rT   r\   rS   rc   ri   rm   rx   rz   r   r   r   r   r   r   r   r   r6   r3   r   r9   stdin_readyr)   r+   r0   r   )NN)1r   r   selectrF   r   rY   r`   rh   rq   r   r   r   r   r   r   r   objectr   r   rL   rN   rU   rT   r\   rS   rc   ri   rm   rx   rz   r   r   r   r   r   r   r   r   r6   r3   r   r9   r-   r   r)   r+   r0   r   __all__r   r   r   <module>r      s     
	


	

	
	N!v N!b %& ''	))
''	))
++))
++))
++''	))
++ --!//"11++ --))
++#33!//++"11  ../KK /KK !//0f1r   