U
    Pex                  	   @   s  d Z dZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ dd ZG dd deZG dd deZG dd deZedkrddlmZ ddlmZ G dd deZe Ze, ed eddZee edW 5 Q R X ee dS )a  
Sandbox
=======

.. versionadded:: 1.8.0

.. warning::

    This is experimental and subject to change as long as this warning notice
    is present.

This is a widget that runs itself and all of its children in a Sandbox. That
means if a child raises an Exception, it will be caught. The Sandbox
itself runs its own Clock, Cache, etc.

The SandBox widget is still experimental and required for the Kivy designer.
When the user designs their own widget, if they do something wrong (wrong size
value, invalid python code), it will be caught correctly without breaking
the whole application. Because it has been designed that way, we are still
enhancing this widget and the :mod:`kivy.context` module.
Don't use it unless you know what you are doing.

)Sandbox    wraps)Context)ExceptionManagerBase)Clock)Widget)FloatLayout)RelativeLayout)Builderc                    s   t   fdd}|S )Nc              	      s(   d }|   | f||}W 5 Q R X |S N )selfargskwargsretfr   4/tmp/pip-unpacked-wheel-xzebddm3/kivy/uix/sandbox.py_f2&   s    zsandbox.<locals>._f2r   )r   r   r   r   r   sandbox%   s    r   c                   @   s   e Zd Zdd Zdd ZdS )SandboxExceptionManagerc                 C   s   t |  || _d S r   )r   __init__r   )r   r   r   r   r   r   1   s    
z SandboxExceptionManager.__init__c                 C   s   | j |stjS tjS r   )r   on_exceptionr   ZRAISEZPASS)r   er   r   r   handle_exception5   s    z(SandboxExceptionManager.handle_exceptionN)__name__
__module____qualname__r   r   r   r   r   r   r   /   s   r   c                   @   s   e Zd ZdS )SandboxContentN)r   r   r   r   r   r   r   r   ;   s   r   c                       s   e Zd ZdZ fddZdd Zdd Zdd	 ZdddZe	e
jZe	e
jZe	e
jZe	e
jZe	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Zdd Z  ZS )r   zQSandbox widget, used to trap all the exceptions raised by child
    widgets.
    c                    s   t dd| _t| | jd< | | j_| j  |   d | _tt| j	f | t
| j| jd| _tt| | j | j  t| jd t| jd ttd| _d S )NT)initZExceptionManager)sizeposr   _obj)r   _contextr   r   pushon_context_created
_containersuperr   r   r   r!   r"   
add_widgetpopr   Zschedule_interval_clock_sandboxschedule_once_clock_sandbox_drawobject__getattribute__
main_clock)r   r   	__class__r   r   r   D   s    

zSandbox.__init__c                 C   s   | j   d S r   )r%   r&   r   r   r   r   	__enter__U   s    zSandbox.__enter__c                 C   s$   | j   |d k	r | j||dS d S )N)
_traceback)r%   r+   r   )r   _typevalue	tracebackr   r   r   __exit__X   s    
zSandbox.__exit__c                 C   s   dS )zwOverride this method in order to load your kv file or do anything
        else with the newly created context.
        Nr   r4   r   r   r   r'   ]   s    zSandbox.on_context_createdNc                 C   s   ddl }|| dS )zOverride this method in order to catch all the exceptions from
        children.

        If you return True, it will not reraise the exception.
        If you return False, the exception will be raised to the parent.
        r   NT)r9   print_tb)r   	exceptionr6   r9   r   r   r   r   c   s    
zSandbox.on_exceptionc                 O   s   | j j|| d S r   )r(   r*   r   r   r   r   r   r   r*   s   s    zSandbox.add_widgetc                 O   s   | j j|| d S r   )r(   remove_widgetr=   r   r   r   r>   w   s    zSandbox.remove_widgetc                 O   s   | j j|| d S r   )r(   clear_widgetsr=   r   r   r   r?   {   s    zSandbox.clear_widgetsc                 G   s   | j r| j| j _d S r   )r(   r!   r   r   r   r   r   on_size   s    zSandbox.on_sizec                 G   s   | j r| j| j _d S r   )r(   r"   r@   r   r   r   on_pos   s    zSandbox.on_posc                 C   s   t   t  d S r   )r   Ztickr   syncr   dtr   r   r   r,      s    zSandbox._clock_sandboxc                 C   s$   t   t  | j| jd d S )Nr   )r   Z	tick_drawr   rC   r1   r-   
_call_drawrD   r   r   r   r.      s    zSandbox._clock_sandbox_drawc                 C   s   | j | jd d S )Nr#   )r1   r-   r.   rD   r   r   r   rF      s    zSandbox._call_draw)N)r   r   r   __doc__r   r5   r:   r'   r   r   r   Z	on_motionon_touch_downZon_touch_moveon_touch_upr*   r>   r?   rA   rB   r,   r.   rF   __classcell__r   r   r2   r   r   ?   s2   











r   __main__)runTouchApp)Buttonc                       s(   e Zd Z fddZ fddZ  ZS )
TestButtonc                    s   t t| |S r   )r)   rN   rI   r   touchr2   r   r   rI      s    zTestButton.on_touch_upc                    s   t t| |S r   )r)   rN   rH   rO   r2   r   r   rH      s    zTestButton.on_touch_down)r   r   r   rI   rH   rJ   r   r   r2   r   rN      s   rN   aC  
<TestButton>:
    canvas:
        Color:
            rgb: (.3, .2, 0) if self.state == 'normal' else (.7, .7, 0)
        Rectangle:
            pos: self.pos
            size: self.size
        Color:
            rgb: 1, 1, 1
        Rectangle:
            size: self.texture_size
            pos: self.center_x - self.texture_size[0] / 2.,                 self.center_y - self.texture_size[1] / 2.
            texture: self.texture

    # invalid... for testing.
    # on_touch_up: root.d()
    # on_touch_down: root.f()
    on_release: root.args()
    # on_press: root.args()
zHello World)textZhelloN) rG   __all__	functoolsr   Zkivy.contextr   Z	kivy.baser   Z
kivy.clockr   Zkivy.uix.widgetr   Zkivy.uix.floatlayoutr	   Zkivy.uix.relativelayoutr
   Z	kivy.langr   r   r   r   r   r   rL   Zkivy.uix.buttonrM   rN   sload_stringbr*   	Exceptionr   r   r   r   <module>   s0   
Z




