3
8Ad]À^  ã               @   s^  d Z ddlmZ ddlmZ ddlZddlZddlZddlm	Z	m
Z
mZ ddlmZmZmZmZmZmZmZmZmZmZ ddlmZmZ ddlmZ d	d
lmZmZmZ d	dl m!Z! ddl"Z"eddƒZ#i Z$da%da&dHdd„Z'dd„ Z(dd„ Z)dIdd„Z*dd„ Z+dd„ Z,dJdd„Z-dd„ Z.dd „ Z/d!d"„ Z0d#d$„ Z1G d%d&„ d&ej2ƒZ3ej4d'k �r`G d(d)„ d)e5ƒZ6nG d*d)„ d)ej7ƒZ6G d+d,„ d,ej8j9j:ƒZ;ej<d-fej=d.fej>d/fej?d0fej@d1fejAd2fejBd3fejCd4fejDd5fejEd6fejFd7fejGd8fejHd9fejId:fejJd;fejKd<ffaLdKd>d?„ZMG d@dA„ dAe!ƒZNG dBdC„ dCeƒZOG dDdE„ dEejPƒZQG dFdG„ dGejRƒZRdS )Lzr Defines helper functions and classes used to define wxPython-based trait
    editors and trait editor factories.
é    )Úabsolute_import)Ú
itemgetterN)ÚjoinÚdirnameÚabspath)
ÚHasPrivateTraitsÚEnumÚCTraitÚInstanceÚAnyÚIntÚEventÚBoolÚBaseTraitHandlerÚ
TraitError)Úconvert_imageÚSequenceTypes)Údo_lateré   )Ústandard_bitmap_widthÚ	screen_dxÚ	screen_dy)ÚEditorÚ
horizontalZverticalc             C   sš  | dd… dkr8t | jddƒjƒ ƒ}|dk	r8|jƒ jƒ S |dkrhtdkrbddl}tt|j	j
ƒdƒat}n*|dkr’tdkrŽtttjd ƒd	dƒat}tt|| jddƒjƒ d
 ƒƒ}tj|d| d…  ƒ}|dk	rÔ|S t	jt	j|ƒƒ }}|t|< |jƒ }|tk �r€|jƒ }	t	jt|	ƒ}t	jƒ }
t	jƒ }|
j|ƒ |j|ƒ |
jt	jƒ |
jt	jƒ |
jddt|	ƒ |
jt| d d||	|ddƒ |t|d < |�r–|S |S )z5 Converts an image file name to a cached bitmap.
    Nr   ú@ú Ú_r   ÚimagesÚ z..z.gifÚ*é   )r   ÚreplaceÚlowerZcreate_imageZConvertToBitmapÚtraits_pathZtraitsui.wxr   r   ÚwxÚ__file__Úapp_pathÚsysÚargvr   Ú_bitmap_cacheÚgetZBitmapFromImageZImageÚGetWidthr   Ú	GetHeightÚEmptyBitmapÚMemoryDCÚSelectObjectÚSetPenZTRANSPARENT_PENÚSetBrushZWHITE_BRUSHZDrawRectangleÚBlit)ÚnameZstandard_sizeÚpathÚimageÚtraitsuiÚfilenameÚbitmapZ
std_bitmapÚdxÚdyZdc1Zdc2© r;   ún/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/traitsui-6.1.2-py3.6.egg/traitsui/wx/helper.pyÚbitmap_cacheO   sH    


r=   c             C   s   t dd„ | D ƒƒd S )zk Returns an appropriate width for a wxChoice widget based upon the list
        of values it contains:
    c             S   s   g | ]}t |ƒ‘qS r;   )Úlen)Ú.0Úxr;   r;   r<   ú
<listcomp>Š   s    z choice_width.<locals>.<listcomp>é   )Úmax)Úvaluesr;   r;   r<   Úchoice_width†   s    rE   c             C   s    | j }| j|jƒ |jƒ  ƒ dS )z9 Saves the user preference items for a specified UI.
    N)ÚcontrolZ
save_prefsZGetPositionTupleÚGetSizeTuple)ÚuirF   r;   r;   r<   Úsave_window‘   s    rI   Fc             C   sˆ   | j ƒ }|dk	r„|\}}}}t||ƒ}t|||||ƒ\}}}}|rRt| j||ƒ n2||fdkrr| jj||ddƒ n| jj||||ƒ dS )z< Restores the user preference items for a specified UI.
    Nr   r   )r   r   éÿÿÿÿrJ   )Zrestore_prefsÚfind_closest_displayÚget_position_for_displayÚposition_windowrF   ÚSetDimensions)rH   Zis_popupZprefsr@   Úyr9   r:   Úclosestr;   r;   r<   Úrestore_windowœ   s    
rQ   c             C   s\   d}xRt tjjƒ ƒD ]@}tj|ƒ}|dkr0|}qdd„ }|| ||ƒ|| ||ƒk r|}qW |S )a   For a virtual screen position, find the closest display.
        There are a few reasons to use this function:
            * the number of displays changed
            * the size of the displays changed
            * the orientation of one or more displays changed.
    Nc       	      S   s@   |j ƒ \}}}}||d  }||d  }| | d || d  S )Nr    )ÚGetGeometry)	r@   rO   ÚdisplayÚdis_xÚdis_yÚdis_wÚdis_hZ	dis_mid_xZ	dis_mid_yr;   r;   r<   Ú	_distanceÁ   s    z'find_closest_display.<locals>._distance)Úranger$   ZDisplayÚGetCount)r@   rO   rP   Zdisplay_numrS   rX   r;   r;   r<   rK   ´   s    
rK   c       	      C   sh   |j ƒ \}}}}t||ƒ}t||ƒ}| | || ks<| |k r@|} || || ksX||k r\|}| |||fS )zW calculates a valid position and size for a window to fit
        inside a display
    )rR   Úmin)	r@   rO   r9   r:   rS   rT   rU   rV   rW   r;   r;   r<   rL   Î   s    

rL   c             C   sø   | j ƒ \}}|p|}|p|}|dkr*| j}|dkrV| jt| d t| d ||ƒ dS t|tjƒr€|jddƒ\}}|j ƒ \}}	n|\}}}}	t	| ddƒdk}
t
t||ƒtƒ}t
|tƒ}t||ƒ}|
rÌ||	7 }t|||||ƒ\}}}}| j||||ƒ dS )zŽ Positions a window on the screen with a specified width and height so
        that the window completely fits on the screen if possible.
    Nr    r   Ú_kindÚpopup)rG   Z_parentrN   r   r   Ú
isinstancer$   ÚWindowÚClientToScreenXYÚgetattrr[   rC   rK   rL   )ÚwindowÚwidthÚheightÚparentr9   r:   r@   rO   Z	parent_dxZ	parent_dyÚadjacentrP   r;   r;   r<   rM   â   s*    

rM   c             C   s&   | j ƒ }x|dk	r |} | j ƒ }q
W | S )z; Returns the top-level window for a specified control.
    N)Ú	GetParent)rF   re   r;   r;   r<   Útop_level_window_for  s
    
rh   c                s*  t | tƒrjdd„ | jƒ D ƒ}t|ƒdkræ|jtdƒd� |d d jdƒd ‰ ˆ dkræ‡ fdd„|D ƒ}n|t | tƒsØ| }t |tƒrˆ|j	}t |t
ƒsštdƒ‚|jrÆd	d„ |jjƒ D ƒ}|jtdƒd� qæd
d„ |jD ƒ}ndd„ | D ƒ}dd„ |D ƒ}i }i }x"|D ]\}}|||< |||< �qW |||fS )zB Recomputes the mappings for a new set of enumeration values.
    c             S   s   g | ]\}}t j|ƒ|f‘qS r;   )ÚsixÚ	text_type)r?   ÚnÚvr;   r;   r<   rA   "  s    z'enum_values_changed.<locals>.<listcomp>r   )Úkeyú:r   c                s    g | ]\}}|ˆ d … |f‘qS )Nr;   )r?   rk   rl   )Úcolr;   r<   rA   '  s    z$Invalid value for 'values' specifiedc             S   s   g | ]}t j|ƒ|f‘qS r;   )ri   rj   )r?   rk   r;   r;   r<   rA   /  s    c             S   s   g | ]}t j|ƒ|f‘qS r;   )ri   rj   )r?   rl   r;   r;   r<   rA   2  s    c             S   s   g | ]}t j|ƒ|f‘qS r;   )ri   rj   )r?   rl   r;   r;   r<   rA   4  s    c             S   s   g | ]}|d  ‘qS )r   r;   )r?   r@   r;   r;   r<   rA   6  s    )r^   ÚdictÚitemsr>   Úsortr   Úfindr   r	   Úhandlerr   r   Z	is_mappedÚmapÚkeysrD   )rD   Údatart   ÚnamesÚmappingZinverse_mappingr3   Úvaluer;   )ro   r<   Úenum_values_changed  s2    



r{   c             G   s&   | j ƒ }x|D ]}|| |dƒ qW dS )z@ Disconnects a wx event handle from its associated control.
    N)ÚGetId)rF   ÚeventsÚidÚeventr;   r;   r<   Ú
disconnectD  s    
r€   c             G   s   x|D ]}|| dƒ qW dS )z@ Disconnects a wx event handle from its associated control.
    Nr;   )rF   r}   r   r;   r;   r<   Údisconnect_no_idL  s    
r�   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTraitsUIPanelc             O   sX   |j ddƒ}tjj| |f|ž|Ž tj| | jƒ |r@| j|ƒ n| jƒ }| j|jƒ dS )zp Creates a wx.Panel that correctly sets its background color to be
            the same as its parents.
        Úbg_colorN)	Úpopr$   ÚPanelÚ__init__ÚEVT_CHILD_FOCUSÚOnChildFocusÚSetBackgroundColourZGetDefaultAttributesZcolBg)Úselfre   ÚargsÚkwrƒ   Úattrr;   r;   r<   r†   Z  s    zTraitsUIPanel.__init__c             C   s   |j ƒ | jƒ kr|jƒ  dS )a>   If the ChildFocusEvent contains one of the Panel's direct children,
            then we will Skip it to let it pass up the widget hierarchy.

            Otherwise, we consume the event to make sure it doesn't go any
            farther. This works around a problem in wx 2.8.8.1 where each Panel
            in a nested hierarchy generates many events that might consume too
            many resources. We do, however, let one event bubble up to the top
            so that it may inform a top-level ScrolledPanel that a descendant
            has acquired focus.
        N)Ú	GetWindowÚGetChildrenZSkip)rŠ   r   r;   r;   r<   rˆ   j  s    zTraitsUIPanel.OnChildFocusN)Ú__name__Ú
__module__Ú__qualname__r†   rˆ   r;   r;   r;   r<   r‚   X  s   r‚   z2.8.8c               @   s   e Zd Zdd„ ZdS )ÚChildFocusOverridec             C   s   |j tj|jƒ d S )N)ÚBindr$   r‡   rˆ   )rŠ   rb   r;   r;   r<   r†   ‚  s    zChildFocusOverride.__init__N)r�   r‘   r’   r†   r;   r;   r;   r<   r“   €  s   r“   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r“   a   Override the scroll-to-focus behaviour in wx 2.8.8's ScrolledWindow
            C++ implementation for ScrolledPanel.

            Instantiating this class with the ScrolledPanel will register the
            new instance as the event handler for the panel.
        c             C   s    || _ tjj| ƒ |j| ƒ d S )N)rb   r$   ÚPyEvtHandlerr†   ZPushEventHandler)rŠ   rb   r;   r;   r<   r†   �  s    zChildFocusOverride.__init__c             C   s.   t |tjƒr| jj|ƒS | jƒ j|ƒ}|S d S )N)r^   r$   ZChildFocusEventrb   rˆ   ZGetNextHandlerÚProcessEvent)rŠ   r   Úresultr;   r;   r<   r–   —  s    zChildFocusOverride.ProcessEventN)r�   r‘   r’   Ú__doc__r†   r–   r;   r;   r;   r<   r“   ˆ  s   c               @   s6   e Zd Zd
ejejejdfdd„Zdd„ Zdd„ Z	d	S )ÚTraitsUIScrolledPanelr   Úscrolledpanelc          	   C   s>   t jj| ||||||d� | j|ƒ | j|jƒ ƒ t| ƒ d S )N)ÚposÚsizeÚstyler3   )r$   ZPyScrolledWindowr†   ÚSetSizer‰   ZGetBackgroundColourr“   )rŠ   re   r~   r›   rœ   r�   r3   r;   r;   r<   r†   ¨  s
    

zTraitsUIScrolledPanel.__init__c             C   s   | j | jƒ ƒ dS )zh Handle a ChildFocusEvent.

        Returns a boolean so it can be used as a library call, too.
        T)ÚScrollChildIntoViewZ	FindFocus)rŠ   r   r;   r;   r<   rˆ   ¸  s    z"TraitsUIScrolledPanel.OnChildFocusc             C   s®  |dkrdS | j ƒ \}}| jƒ \}}|jƒ \}}}}	|jƒ }
x>|
| dgkrz|
jƒ dd… \}}|| ||  }}|
jƒ }
q>W tj||||	ƒ}| jƒ }d\}}|jdk r¼|dkr¼||j|  }|jdk rÜ|dkrÜ||j|  }|j	|j
koî|dk�r2|j	|j
 | }|j||  dk�r$|| d }n||j|  }|j|jk�rŠ|dk�rŠ|j|j | }|j||  dk�r||| d }n||j|  }|dk�sž|d	k�rª| j||ƒ dS )
zÉ Scrolls the panel such that the specified child window is in view.
            This method overrides the original in the base class so that
            nested subpanels are handled correctly.
        Nr    r   r   rJ   rJ   )rJ   rJ   rJ   rJ   )ZGetScrollPixelsPerUnitZGetViewStartÚGetRectrg   r$   ZRectÚGetClientSizer@   rO   Úrightrc   Úbottomrd   ZScroll)rŠ   ÚchildZsppuxZsppuyZvsxZvsyZcrxZcryZcrdxZcrdyZ	subwindowZpwxZpwyZcrZclient_sizeZnew_vsxZnew_vsyÚdiffr;   r;   r<   rŸ   Á  s:    z)TraitsUIScrolledPanel.ScrollChildIntoViewNrJ   )
r�   r‘   r’   r$   ZDefaultPositionZDefaultSizeZTAB_TRAVERSALr†   rˆ   rŸ   r;   r;   r;   r<   r™   ¦  s
   	r™   Ú_erase_backgroundZ_paintÚ_sizeZ
_left_downZ_left_upZ_left_dclickZ_middle_downZ
_middle_upZ_middle_dclickZ_right_downZ	_right_upZ_right_dclickZ_motionÚ_enterZ_leaveZ_wheelr   c             C   s8   x2t D ]*\}}t||| dƒ}|dk	r|| |ƒ qW dS )zz Initializes a standard set of wx event handlers for a specified control
        and object using a specified prefix.
    N)Úhandlersra   )rF   ÚobjectÚprefixrt   r3   Úmethodr;   r;   r<   Úinit_wx_handlers  s    r­   c               @   s   e Zd Zdd„ ZdS )ÚGroupEditorc             K   s   | j f |Ž dS )z! Initializes the object.
        N)Z	trait_set)rŠ   Útraitsr;   r;   r<   r†   )  s    zGroupEditor.__init__N)r�   r‘   r’   r†   r;   r;   r;   r<   r®   #  s   r®   c                   sx   e Zd ZeejƒZeZeZ	e
dƒZeZeZeejƒZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z‡  ZS )ÚPopupControlFc                sh   t t| ƒjf |Ž tj}| jr$tj}tjddd|d� | _}tj	|| j
ƒ | j|ƒ | jƒ  |jƒ  dS )z! Initializes the object.
        Nr   r   )r�   rJ   )Úsuperr°   r†   r$   ZSIMPLE_BORDERÚ	resizableZRESIZE_BORDERÚFramer]   ÚEVT_ACTIVATEÚ_on_close_popupÚcreate_controlÚ_position_controlZShow)rŠ   r¯   r�   r]   )Ú	__class__r;   r<   r†   R  s    
zPopupControl.__init__c             C   s   t ‚dS )zM Creates the control.

            Must be overridden by a subclass.
        N)ÚNotImplementedError)rŠ   r;   r;   r<   r¶   a  s    zPopupControl.create_controlc             C   s   dS )z{ Called when the popup is being closed to allow any custom clean-up.

            Can be overridden by a subclass.
        Nr;   )rŠ   r;   r;   r<   Údisposeh  s    zPopupControl.disposec             C   s   t | jƒ dS )z, Handles the 'value' being changed.
        N)r   Ú_close_popup)rŠ   rz   r;   r;   r<   Ú_value_changedq  s    zPopupControl._value_changedc       	      C   sª   | j jddƒ\}}| j jƒ \}}| jjƒ \}}t||| jƒt|| jƒ }}|| }|| tkr”|| dk rŒt| }||kr†d| }}q”|}n|| }| jj||||ƒ dS )zD Initializes the popup control's initial position and size.
        r   N)	rF   r`   rG   r]   rC   rc   rd   r   rN   )	rŠ   ZpxÚcyZcdxZcdyZpdxZpdyÚpyr£   r;   r;   r<   r·   x  s    zPopupControl._position_controlc             C   s   |j ƒ s| jƒ  dS )z: Closes the popup control when it is deactivated.
        N)Z	GetActiver»   )rŠ   r   r;   r;   r<   rµ   �  s    zPopupControl._on_close_popupc             C   s6   t j| jdƒ | jƒ  d| _| jjƒ  d | _| _dS )z Closes the dialog.
        NT)r$   r´   r]   rº   ÚclosedZDestroyrF   )rŠ   r;   r;   r<   r»   –  s
    
zPopupControl._close_popup)r�   r‘   r’   r
   r$   r_   rF   r   rc   rd   r   r²   r   rz   r   r¿   r]   r†   r¶   rº   r¼   r·   rµ   r»   Ú__classcell__r;   r;   )r¸   r<   r°   3  s   

	r°   c               @   s$   e Zd ZdZddd„Zd	dd„ZdS )
ÚBufferDCzÌ An off-screen buffer class.

        This class implements a off-screen output buffer. Data is meant to
        be drawn in the buffer and then blitted directly to the output device
        context.
    Nc             C   s\   t jj| ƒ |dkr*|jƒ \}}t j|ƒ}|| _t j||ƒ| _| j| jƒ | j	|j
ƒ ƒ dS )zInitializes the buffer.N)r$   r.   r†   r¡   ZPaintDCÚdcr-   r8   r/   ZSetFontZGetFont)rŠ   rÂ   rc   rd   r;   r;   r<   r†   ¬  s    
zBufferDC.__init__r   c             C   s(   | j j||| jjƒ | jjƒ | ddƒ dS )zh Performs the blit of the buffer contents to the specified device
            context location.
        r   N)rÂ   r2   r8   r+   r,   )rŠ   r@   rO   r;   r;   r<   Úcopy½  s    zBufferDC.copy)NN)r   r   )r�   r‘   r’   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‡  ZS )ÚSliderz´ This is a 'fixed' version of the wx.Slider control which does not
        erase its background, which can cause a lot of update flicker and is
        completely unnecessary.
    c                s$   t t| ƒj||Ž tj| | jƒ d S )N)r±   rÄ   r†   r$   ÚEVT_ERASE_BACKGROUNDr¦   )rŠ   r‹   rŒ   )r¸   r;   r<   r†   Ï  s    zSlider.__init__c             C   s   d S )Nr;   )rŠ   r   r;   r;   r<   r¦   Ô  s    zSlider._erase_background)r�   r‘   r’   r˜   r†   r¦   rÀ   r;   r;   )r¸   r<   rÄ   É  s   rÄ   )N)F)NNN)r   )Sr˜   Ú
__future__r   Úoperatorr   r$   Zwx.lib.scrolledpanelr'   Úos.pathr   r   r   Z
traits.apir   r   r	   r
   r   r   r   r   r   r   Ztraitsui.ui_traitsr   r   Zpyface.timer.apir   Ú	constantsr   r   r   Zeditorr   ri   ZOrientationr)   r&   r#   r=   rE   rI   rQ   rK   rL   rM   rh   r{   r€   r�   r…   r‚   Ú__version__rª   r“   r•   Úlibrš   ZScrolledPanelr™   rÅ   Z	EVT_PAINTZEVT_SIZEZEVT_LEFT_DOWNZEVT_LEFT_UPZEVT_LEFT_DCLICKZEVT_MIDDLE_DOWNZEVT_MIDDLE_UPZEVT_MIDDLE_DCLICKZEVT_RIGHT_DOWNZEVT_RIGHT_UPZEVT_RIGHT_DCLICKZ
EVT_MOTIONZEVT_ENTER_WINDOWZEVT_LEAVE_WINDOWZEVT_MOUSEWHEELr©   r­   r®   r°   r.   rÁ   rÄ   r;   r;   r;   r<   Ú<module>   sj   0

7

,'&Z
q%