a
    §ˆc¥N  ã                   @   s(  d 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 d	d
lmZ d	dlmZ eddƒZ i Z!da"da#dDdd„Z$dd„ Z%dd„ Z&dEdd„Z'dd„ Z(dd„ Z)dFdd„Z*dd„ Z+dd „ Z,d!d"„ Z-G d#d$„ d$ej.ƒZ/G d%d&„ d&ej0ƒZ1G d'd(„ d(ej2j3j4ƒZ5ej6d)fej7d*fej8d+fej9d,fej:d-fej;d.fej<d/fej=d0fej>d1fej?d2fej@d3fejAd4fejBd5fejCd6fejDd7fejEd8ffaFdGd:d;„ZGG d<d=„ d=eƒZHG d>d?„ d?eƒZIG d@dA„ dAejJƒZKG dBdC„ dCejLƒZLdS )Hzr Defines helper functions and classes used to define wxPython-based trait
    editors and trait editor factories.
é    )Ú
itemgetterN)ÚjoinÚdirnameÚabspath)
ÚHasPrivateTraitsÚEnumÚCTraitÚInstanceÚAnyÚIntÚEventÚBoolÚBaseTraitHandlerÚ
TraitError)Úconvert_imageÚSequenceTypes)ÚSystemMetrics)Údo_lateré   )Ústandard_bitmap_width)ÚEditorZ
horizontalZverticalc              	   C   sš  | dd… dkr8t |  dd¡ ¡ ƒ}|dur8| ¡  ¡ S |du rhtdu rbddl}tt|j	j
ƒdƒat}n*|dkr’tdu rŽtttjd ƒd	dƒat}tt||  dd¡ ¡ d
 ƒƒ}t |d| d…  ¡}|durÔ|S t	 t	 |¡¡ }}|t|< | ¡ }|tk �r€| ¡ }	t	 t|	¡}t	 ¡ }
t	 ¡ }|
 |¡ | |¡ |
 t	j¡ |
 t	j¡ |
 ddt|	¡ |
 t| d d||	|dd¡ |t|d < |�r–|S |S )z/Converts an image file name to a cached bitmap.Nr   ú@ú Ú_r   ZimagesÚ z..z.gifÚ*é   )r   ÚreplaceÚlowerZcreate_imageZConvertToBitmapÚtraits_pathZtraitsui.wxr   r   ÚwxÚ__file__Úapp_pathÚsysÚargvr   Ú_bitmap_cacheÚgetÚBitmapZImageÚGetWidthr   Ú	GetHeightÚMemoryDCÚSelectObjectZSetPenZTRANSPARENT_PENZSetBrushZWHITE_BRUSHZDrawRectangleÚBlit)ÚnameZstandard_sizeÚpathÚimageZtraitsuiÚfilenameÚbitmapZ
std_bitmapÚdxÚdyZdc1Zdc2© r4   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/wx/helper.pyÚbitmap_cacheE   sF    


r6   c                 C   s   t dd„ | D ƒƒd S )zfReturns an appropriate width for a wxChoice widget based upon the list
    of values it contains:
    c                 S   s   g | ]}t |ƒ‘qS r4   )Úlen)Ú.0Úxr4   r4   r5   Ú
<listcomp>€   ó    z choice_width.<locals>.<listcomp>é   )Úmax)Úvaluesr4   r4   r5   Úchoice_width|   s    r?   c                 C   s    | j }|  | ¡ | ¡  ¡ dS )z3Saves the user preference items for a specified UI.N)ÚcontrolZ
save_prefsZGetPositionÚGetSize)Úuir@   r4   r4   r5   Úsave_windowƒ   s    rC   Fc                 C   sˆ   |   ¡ }|dur„|\}}}}t||ƒ}t|||||ƒ\}}}}|rRt| j||ƒ n2||fdkrr| j ||dd¡ n| j ||||¡ dS )z6Restores the user preference items for a specified UI.N)r   r   éÿÿÿÿ)Zrestore_prefsÚfind_closest_displayÚget_position_for_displayÚposition_windowr@   ÚSetSize)rB   Zis_popupZprefsr9   Úyr2   r3   Úclosestr4   r4   r5   Úrestore_window‰   s    
rK   c                 C   sX   d}t tj ¡ ƒD ]@}t |¡}|du r.|}qdd„ }|| ||ƒ|| ||ƒk r|}q|S )zþ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@   |  ¡ \}}}}||d  }||d  }| | d || d  S )Nr   )ÚGetGeometry)	r9   rI   ÚdisplayÚdis_xÚdis_yÚdis_wÚdis_hZ	dis_mid_xZ	dis_mid_yr4   r4   r5   Ú	_distance®   s    z'find_closest_display.<locals>._distance)Úranger    ZDisplayZGetCount)r9   rI   rJ   Zdisplay_numrM   rR   r4   r4   r5   rE       s    
rE   c           	      C   sh   |  ¡ \}}}}t||ƒ}t||ƒ}| | || ks<| |k r@|} || || ksX||k r\|}| |||fS )zRcalculates a valid position and size for a window to fit
    inside a display
    )rL   Úmin)	r9   rI   r2   r3   rM   rN   rO   rP   rQ   r4   r4   r5   rF   »   s    

rF   c                 C   s  |   ¡ \}}|p|}|p|}|du r*| j}|du r^|  tƒ j| d tƒ j| d ||¡ dS t|tjƒrˆ| 	dd¡\}}|  ¡ \}}	n|\}}}}	t
| ddƒdk}
tt||ƒtƒ jƒ}t|tƒ jƒ}t||ƒ}|
rÜ||	7 }t|||||ƒ\}}}}|  ||||¡ 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)rA   Ú_parentrH   r   Zscreen_widthÚscreen_heightÚ
isinstancer    ÚWindowÚClientToScreenÚgetattrrT   r=   rE   rF   )ÚwindowÚwidthÚheightÚparentr2   r3   r9   rI   Z	parent_dxZ	parent_dyÚadjacentrJ   r4   r4   r5   rG   Ð   s2    ü
rG   c                 C   s"   |   ¡ }|dur|} |   ¡ }q| S )z5Returns the top-level window for a specified control.N)Ú	GetParent)r@   r`   r4   r4   r5   Útop_level_window_forü   s
    
rc   c                 G   s$   |   ¡ }|D ]}| j||d� qdS )ú:Disconnects a wx event handle from its associated control.)ÚidN)ZGetIdÚUnbind)r@   Úeventsre   Úeventr4   r4   r5   Ú
disconnect  s    ri   c                 G   s   |D ]}|   |¡ qdS )rd   N)rf   )r@   rg   rh   r4   r4   r5   Údisconnect_no_id  s    rj   c                       s,   e Zd Zdd„ Zdd„ Z‡ fdd„Z‡  ZS )ÚTraitsUIPanelc                 O   s`   |  dd¡}tjj| |g|¢R i |¤Ž |  tj| j¡ |rH|  |¡ n|  ¡ }|  |j	¡ dS )zkCreates a wx.Panel that correctly sets its background color to be
        the same as its parents.
        Úbg_colorN)
Úpopr    ÚPanelÚ__init__ÚBindÚEVT_CHILD_FOCUSÚOnChildFocusÚSetBackgroundColourZGetDefaultAttributesZcolBg)Úselfr`   ÚargsÚkwrl   Úattrr4   r4   r5   ro     s    zTraitsUIPanel.__init__c                 C   s   |  ¡ |  ¡ v r| ¡  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)Z	GetWindowZGetChildrenZSkip©rt   rh   r4   r4   r5   rr   *  s    zTraitsUIPanel.OnChildFocusc                    s   |   tj¡ tƒ  ¡  d S ©N)rf   r    rq   ÚsuperÚDestroy©rt   ©Ú	__class__r4   r5   r{   8  s    zTraitsUIPanel.Destroy)Ú__name__Ú
__module__Ú__qualname__ro   rr   r{   Ú__classcell__r4   r4   r}   r5   rk     s   rk   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚChildFocusOverridezî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                    s   || _ tƒ  ¡  | | ¡ d S ry   )r]   rz   ro   ZPushEventHandler)rt   r]   r}   r4   r5   ro   J  s    
zChildFocusOverride.__init__c                 C   s.   t |tjƒr| j |¡S |  ¡  |¡}|S d S ry   )rY   r    ZChildFocusEventr]   rr   ZGetNextHandlerÚProcessEvent)rt   rh   Úresultr4   r4   r5   r„   Q  s    zChildFocusOverride.ProcessEvent)r   r€   r�   Ú__doc__ro   r„   r‚   r4   r4   r}   r5   rƒ   B  s   rƒ   c                       sF   e Zd Zdejejejdfdd„Z‡ fdd„Zdd„ Z	d	d
„ Z
‡  ZS )ÚTraitsUIScrolledPanelrD   Úscrolledpanelc              	   C   s>   t jj| ||||||d� |  |¡ |  | ¡ ¡ t| ƒ d S )N)ÚposÚsizeÚstyler-   )r    ZScrolledWindowro   rH   rs   ZGetBackgroundColourrƒ   )rt   r`   re   r‰   rŠ   r‹   r-   r4   r4   r5   ro   ]  s    
ÿ
zTraitsUIScrolledPanel.__init__c                    s$   ddl m} || tƒ tƒ  ¡  d S )Nr   )Ú_popEventHandlers)Ztraitsui.wx.toolkitrŒ   rƒ   rz   r{   )rt   rŒ   r}   r4   r5   r{   t  s    
zTraitsUIScrolledPanel.Destroyc                 C   s   |   |  ¡ ¡ dS )zgHandle a ChildFocusEvent.

        Returns a boolean so it can be used as a library call, too.
        T)ÚScrollChildIntoViewZ	FindFocusrx   r4   r4   r5   rr   z  s    z"TraitsUIScrolledPanel.OnChildFocusc                 C   s¬  |du rdS |   ¡ \}}|  ¡ \}}| ¡ \}}}}	| ¡ }
|
| dfvrx|
 ¡ dd… \}}|| ||  }}|
 ¡ }
q<t ||||	¡}|  ¡ }d\}}|jdk r¸|dkr¸||j|  }|jdk rØ|dkrØ||j|  }|j	|j
k�r0|dk�r0|j	|j
 | }|j||  dk�r"|| d }n||j|  }|j|jk�rˆ|dk�rˆ|j|j | }|j||  dk�rz|| d }n||j|  }|dk�sœ|dk�r¨|  ||¡ 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   )rD   rD   r   r   rD   )ZGetScrollPixelsPerUnitZGetViewStartZGetRectrb   r    ZRectÚGetClientSizer9   rI   Úrightr^   Úbottomr_   ZScroll)rt   ÚchildZsppuxZsppuyZvsxZvsyZcrxZcryZcrdxZcrdyZ	subwindowZpwxZpwyZcrZclient_sizeZnew_vsxZnew_vsyÚdiffr4   r4   r5   r�   ƒ  s:    
z)TraitsUIScrolledPanel.ScrollChildIntoView)r   r€   r�   r    ZDefaultPositionZDefaultSizeZTAB_TRAVERSALro   r{   rr   r�   r‚   r4   r4   r}   r5   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   s4   t D ]*\}}t||| dƒ}|dur|| |ƒ qdS )zuInitializes a standard set of wx event handlers for a specified control
    and object using a specified prefix.
    N)Úhandlersr\   )r@   ÚobjectÚprefixÚhandlerr-   Úmethodr4   r4   r5   Úinit_wx_handlersÖ  s    r›   c                   @   s   e Zd Zdd„ ZdS )ÚGroupEditorc                 K   s   | j f i |¤Ž dS )úInitializes the object.N)Z	trait_set)rt   Útraitsr4   r4   r5   ro   ã  s    zGroupEditor.__init__N)r   r€   r�   ro   r4   r4   r4   r5   rœ   â  s   rœ   c                       s€   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                    sj   t ƒ jf i |¤Ž tj}| jr$tj}tjddd|d� | _}| tj	| j
¡ |  |¡ |  ¡  | ¡  dS )r�   NrD   r   )r‹   )rz   ro   r    ZSIMPLE_BORDERÚ	resizableZRESIZE_BORDERZFramerV   rp   ÚEVT_ACTIVATEÚ_on_close_popupÚcreate_controlÚ_position_controlZShow)rt   rž   r‹   rV   r}   r4   r5   ro     s    
zPopupControl.__init__c                 C   s   t ‚dS )zHCreates the control.

        Must be overridden by a subclass.
        N)ÚNotImplementedErrorr|   r4   r4   r5   r£     s    zPopupControl.create_controlc                 C   s   dS )zvCalled when the popup is being closed to allow any custom clean-up.

        Can be overridden by a subclass.
        Nr4   r|   r4   r4   r5   Údispose  s    zPopupControl.disposec                 C   s   t | jƒ dS )z"Handles the 'value' being changed.N)r   Ú_close_popup)rt   Úvaluer4   r4   r5   Ú_value_changed%  s    zPopupControl._value_changedc           	      C   s²   | j  dd¡\}}| j  ¡ \}}| j ¡ \}}t||| jƒt|| jƒ }}|| }|| tƒ jkrœ|| dk r”tƒ j| }||krŽd| }}qœ|}n|| }| j 	||||¡ dS )z:Initializes the popup control's initial position and size.r   N)
r@   r[   rA   rV   r=   r^   r_   r   rX   rH   )	rt   ZpxÚcyZcdxZcdyZpdxZpdyÚpyr�   r4   r4   r5   r¤   +  s    zPopupControl._position_controlc                 C   s   |  ¡ s|  ¡  dS )z0Closes the popup control when it is deactivated.N)Z	GetActiver§   rx   r4   r4   r5   r¢   B  s    zPopupControl._on_close_popupc                 C   s6   | j  tj¡ |  ¡  d| _| j  ¡  d | _ | _dS )zCloses the dialog.TN)rV   rf   r    r¡   r¦   Úclosedr{   r@   r|   r4   r4   r5   r§   G  s
    
zPopupControl._close_popup)r   r€   r�   r	   r    rZ   r@   r   r^   r_   r   r    r
   r¨   r   r¬   rV   ro   r£   r¦   r©   r¤   r¢   r§   r‚   r4   r4   r}   r5   rŸ   è  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 | ¡ |du r*| ¡ \}}t  |¡}|| _t  ||¡| _|  | j¡ |  	| 
¡ ¡ dS )zInitializes the buffer.N)r    r*   ro   rŽ   ZPaintDCÚdcr'   r1   r+   ZSetFontZGetFont)rt   r®   r^   r_   r4   r4   r5   ro   X  s    
zBufferDC.__init__r   c              	   C   s(   | j  ||| j ¡ | j ¡ | dd¡ dS )zcPerforms the blit of the buffer contents to the specified device
        context location.
        r   N)r®   r,   r1   r(   r)   )rt   r9   rI   r4   r4   r5   Úcopyi  s    ÿzBufferDC.copy)NN)r   r   )r   r€   r�   r†   ro   r¯   r4   r4   r4   r5   r­   P  s   
r­   c                       s4   e Zd ZdZ‡ fdd„Zdd„ Z‡ f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 ƒ j|i |¤Ž |  tj| j¡ d S ry   )rz   ro   rp   r    ÚEVT_ERASE_BACKGROUNDr“   )rt   ru   rv   r}   r4   r5   ro   x  s    zSlider.__init__c                 C   s   d S ry   r4   rx   r4   r4   r5   r“   }  s    zSlider._erase_backgroundc                    s   |   tj¡ tƒ  ¡  d S ry   )rf   r    r±   rz   r{   r|   r}   r4   r5   r{   €  s    zSlider.Destroy)r   r€   r�   r†   ro   r“   r{   r‚   r4   r4   r}   r5   r°   r  s   r°   )N)F)NNN)r   )Mr†   Ú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.apir   Zpyface.timer.apir   Ú	constantsr   Zeditorr   ZOrientationr%   r"   r   r6   r?   rC   rK   rE   rF   rG   rc   ri   rj   rn   rk   Z
EvtHandlerrƒ   Ú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°   r4   r4   r4   r5   Ú<module>   sd   0

7

,
)gð
h"