ó
[³XMc           @   sÍ   d  Z  d d l Z y  d d l m Z m Z m Z Wn- e	 k
 ra d d l
 m Z m Z m Z n Xd d l m Z m Z d d l m Z d e f d „  ƒ  YZ d	 e j f d
 „  ƒ  YZ d „  Z d g Z d S(   sa  
This module provides wxPython event loop support for Twisted.

In order to use this support, simply do the following::

    |  from twisted.internet import wxreactor
    |  wxreactor.install()

Then, when your root wxApp has been created::

    | from twisted.internet import reactor
    | reactor.registerWxApp(yourApp)
    | reactor.run()

Then use twisted.internet APIs as usual. Stop the event loop using
reactor.stop(), not yourApp.ExitMainLoop().

IMPORTANT: tests will fail when run under this reactor. This is
expected and probably does not reflect on the reactor's ability to run
real applications.
iÿÿÿÿN(   t   PySimpleAppt	   CallAftert   Timer(   t   wxPySimpleAppt   wxCallAftert   wxTimer(   t   logt   runtime(   t   _threadedselectt   ProcessEventsTimerc           B   s    e  Z d  Z d „  Z d „  Z RS(   sÊ   
    Timer that tells wx to process pending events.

    This is necessary on OS X, probably due to a bug in wx, if we want
    wxCallAfters to be handled when modal dialogs, menus, etc.  are open.
    c         C   s   t  j |  ƒ | |  _ d  S(   N(   R   t   __init__t   wxapp(   t   selfR   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyR
   -   s    c         C   s   |  j  j ƒ  d S(   s5   
        Called repeatedly by wx event loop.
        N(   R   t   ProcessPendingEvents(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyt   Notify2   s    (   t   __name__t
   __module__t   __doc__R
   R   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyR	   &   s   	t	   WxReactorc           B   sM   e  Z d  Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z	 e
 d „ Z RS(   s[   
    wxPython reactor.

    wxPython drives the event loop, select() runs in a thread.
    c         C   s   | |  _  d S(   s;   
        Register wxApp instance with the reactor.
        N(   R   (   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyt   registerWxAppC   s    c         C   sI   y& d d l  } | j  | j | j ƒ Wn t k
 r: d SX|  j ƒ  d S(   sO   
        wx sometimes removes our own signal handlers, so re-add them.
        iÿÿÿÿN(   t   signalt   SIGINTt   default_int_handlert   ImportErrort   _handleSignals(   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyt   _installSignalHandlersAgainJ   s    c         C   s*   |  j  r d St |  _  t j j |  ƒ d S(   s#   
        Stop the reactor.
        N(   t	   _stoppingt   TrueR   t   ThreadedSelectReactort   stop(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyR   W   s    		c         C   s0   t  |  d ƒ r t | ƒ n |  j j | ƒ d S(   sm   
        Schedule function to run in main wx/Twisted thread.

        Called by the select() thread.
        R   N(   t   hasattrR   t
   _postQueuet   put(   R   t   f(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyt   _runInMainThreada   s    c         C   s#   t  |  d ƒ r |  j j ƒ  n  d S(   s   
        Stop the wx event loop if it hasn't already been stopped.

        Called during Twisted event loop shutdown.
        R   N(   R   R   t   ExitMainLoop(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyt   _stopWxn   s    c            sx  t  j  ƒ  ˆ  _ t ˆ  d ƒ s> t j d ƒ ˆ  j t ƒ  ƒ n  ˆ  j ˆ  j d | ƒ| rp ˆ  j	 d ˆ  j
 ƒ n  ˆ  j d d ˆ  j ƒ ˆ  j d d ‡  f d †  ƒ t j j ƒ  rÐ t ˆ  j ƒ } | j d ƒ n  ˆ  j j ƒ  ˆ  j } ˆ  ` ˆ  j stˆ  j ƒ  | j ƒ  xh y ˆ  j j d	 d
 ƒ } Wn t  j k
 r>qqX| d k rOPn  y | ƒ  Wqt j ƒ  qXqn  d S(   s$   
        Start the reactor.
        R   sM   registerWxApp() was not called on reactor, registering my own wxApp instance.t   installSignalHandlersi    t   aftert   shutdownc              s   ˆ  j  j d  ƒ S(   N(   R   R    t   None(    (   R   (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyt   <lambda>‹   s    i   t   timeoutg{®Gáz„?N(   t   QueueR   R   R   t   msgR   R   t
   interleaveR"   t	   callLaterR   t   addSystemEventTriggerR$   R   t   platformt   isMacOSXR	   R   t   Startt   MainLoopR   R   R   t   gett   EmptyR(   t   err(   R   R%   t   tR   R!   (    (   R   s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyt   runx   s>    		

(   R   R   R   t   FalseR   R   R   R   R"   R$   R   R8   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyR   :   s   			
		
c          C   s'   t  ƒ  }  d d l m } | |  ƒ |  S(   sP   
    Configure the twisted mainloop to be run inside the wxPython mainloop.
    iÿÿÿÿ(   t   installReactor(   R   t   twisted.internet.mainR:   (   t   reactorR:   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyt   install®   s    	
R=   (   R   R+   t   wxR    R   R   R   R   R   R   t   wxPython.wxt   twisted.pythonR   R   t   twisted.internetR   R	   R   R   R=   t   __all__(    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/wxreactor.pyt   <module>   s     t	
