3
8ßL]ÇI  ã               @   sÊ   d Z ddlmZ ddlZddlmZmZmZmZm	Z	 ddl
mZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZmZ eeƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ dejƒZdS )z? Defines file editors for the wxPython user interface toolkit.
é    )Úabsolute_importN)ÚabspathÚsplitÚsplitextÚisfileÚexists)ÚListÚStrÚEventÚAnyÚon_trait_changeÚ
TraitError)ÚToolkitEditorFactoryé   )ÚSimpleEditor)ÚTraitsUIPanelÚPopupControlc                   s°   e Zd ZdZeZeZdd„ Z‡ fdd„Ze	dƒdd„ ƒZ
d	d
„ Zdd„ Zdd„ Zdd„ Ze	dƒdd„ ƒZe	dƒdd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Z‡  ZS )#r   zÇ Simple style of file editor, consisting of a text field and a **Browse**
        button that opens a file-selection dialog box. The user can also drag
        and drop a file onto this control.
    c       
      C   sv  t |dƒ | _}tjtjƒ}| j}|jdkrvddlm} ||j|j	d�| _
| j
j|ƒ}d}tj|ddtjddƒd�}nŒ|jr¦tj|dd	tjd
�}tj||jƒ | jƒ ntj|dd	ƒ}tj|| jƒ |j	rÜtj||jƒ | jƒ tjjtjdd�}	tj|d|	d�}d}|| _|j|dtjtjB ƒ |j|dtjtjB |ƒ tj||jƒ | j ƒ |j!t"| ƒƒ |j#|ƒ || _$| j%|ƒ dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        r   r   )ÚHistoryControl)ÚentriesÚauto_seté   z...é   )ÚsizeÚ )Ústyleé   )Zbitmapé   Néÿÿÿÿr   r   r   r   )r   r   r   )&r   ÚcontrolÚwxZBoxSizerZ
HORIZONTALÚfactoryr   Zhistory_controlr   r   ÚhistoryÚcreate_controlZButtonÚSizeZ	enter_setZTextCtrlZTE_PROCESS_ENTERÚEVT_TEXT_ENTERÚGetIdÚupdate_objectÚEVT_KILL_FOCUSÚEVT_TEXTZArtProviderZ	GetBitmapZART_FOLDER_OPENZBitmapButtonÚ
_file_nameÚAddZEXPANDZALIGN_CENTERÚLEFTÚ
EVT_BUTTONÚshow_file_dialogZSetDropTargetÚFileDropTargetZSetSizerAndFitÚ_buttonÚset_tooltip)
ÚselfÚparentÚpanelZsizerr    r   r   ÚpadÚbuttonZbmp© r6   ú=/tmp/pip-install-5sebvq2j/traitsui/traitsui/wx/file_editor.pyÚinitK   s>    





zSimpleEditor.initc                sž   | j }|jtj| jjƒ dƒ d| _| jdk	r>| jjƒ  d| _nN| j}| j	d }| _	|jtj
dƒ |jtjd|jƒ d� |jtjd|jƒ d� tt| ƒjƒ  dS )z0 Disposes of the contents of an editor.
        N)Úid)r   ZBindr   r,   r/   r%   r!   Údisposer    r)   r'   r$   r(   Úsuperr   )r1   r3   r    r   )Ú	__class__r6   r7   r:   y   s    

zSimpleEditor.disposezhistory:valuec             C   s   | j s| j|ƒ dS )z: Handles the history 'value' trait being changed.
        N)Ú
_no_updateÚ_update)r1   Úvaluer6   r6   r7   Ú_history_value_changed�   s    z#SimpleEditor._history_value_changedc             C   s(   t |tjƒr|jƒ  | j| jjƒ ƒ dS )zE Handles the user changing the contents of the edit control.
        N)Ú
isinstancer   Z
FocusEventZSkipr>   r)   ÚGetValue)r1   Úeventr6   r6   r7   r&   ›   s    zSimpleEditor.update_objectc             C   s4   | j dk	r"d| _| j| j _d| _n| jj| jƒ dS )z` Updates the editor when the object trait changes externally to the
            editor.
        NTF)r!   r=   Ú	str_valuer?   r)   ZSetValue)r1   r6   r6   r7   Úupdate_editor¦   s
    

zSimpleEditor.update_editorc             C   sj   | j dk	r| jƒ | _nP| jƒ }|jƒ tjk}t|jƒ ƒ}|j	ƒ  |rf| j
jrXt|ƒd }|| _| jƒ  dS )z* Displays the pop-up file dialog.
        Nr   )r!   Ú_create_file_popupÚpopupÚ_create_file_dialogZ	ShowModalr   ZID_OKr   ÚGetPathZDestroyr    Útruncate_extr   r?   rE   )r1   rC   ÚdlgÚrcÚ	file_namer6   r6   r7   r-   µ   s    
zSimpleEditor.show_file_dialogc             C   s   | j S )zC Returns the editor's control for indicating error status.
        )r)   )r1   r6   r6   r7   Úget_error_controlÊ   s    zSimpleEditor.get_error_controlzpopup:valuec             C   s8   | j jrt|ƒd }|| _d| _| jj| jƒ d| _dS )z0 Handles the popup value being changed.
        r   TFN)r    rJ   r   r?   r=   r!   Ú	set_valuerD   )r1   rM   r6   r6   r7   Ú_popup_value_changedÑ   s    z!SimpleEditor._popup_value_changedzpopup:closedc             C   s
   d| _ dS )z1 Handles the popup control being closed.
        N)rG   )r1   r6   r6   r7   Ú_popup_closed_changedÝ   s    z"SimpleEditor._popup_closed_changedc             C   s*   | j dk	r&|jdg ƒd| jj… | j _ dS )z` Restores any saved user preference information associated with the
            editor.
        Nr!   )r!   Úgetr    r   )r1   Zprefsr6   r6   r7   Úrestore_prefså   s    
zSimpleEditor.restore_prefsc             C   s"   | j dk	rd| j j dd… iS dS )zM Returns any user preference information associated with the editor.
        Nr!   )r!   )r1   r6   r6   r7   Ú
save_prefsí   s    
zSimpleEditor.save_prefsc             C   s†   t | jjƒdkr(dj| jjdd… ƒ}nd}| jjdkr@tj}n| jjdkrTtj}ntj}t	| j
ƒ ƒ\}}tj| j||d||d�}|S )	z2 Creates the correct type of file dialog.
        r   ú|NzAll Files (*.*)|*.*ÚsaveÚopenzSelect a File)Z
defaultDirZdefaultFileÚmessageÚwildcardr   )Úlenr    ÚfilterÚjoinZdialog_styler   ZFD_SAVEZFD_OPENZFD_DEFAULT_STYLEr   Ú
_get_valueÚ
FileDialogr   )r1   rY   r   Ú	directoryÚfilenamerK   r6   r6   r7   rH   ÷   s"    z SimpleEditor._create_file_dialogc             C   s   t | j| j| jjdd�S )z1 Creates the correct type of file popup.
        i,  )r   rM   r[   Úheight)Ú	PopupFiler   rD   r    r[   )r1   r6   r6   r7   rF     s    zSimpleEditor._create_file_popupc             C   sF   y| j jrt|ƒd }|| _W n" tk
r@ } zW Y dd}~X nX dS )z> Updates the editor value with a specified file name.
        r   N)r    rJ   r   r?   r   )r1   rM   Zexcpr6   r6   r7   r>     s    
zSimpleEditor._updatec             C   s   | j dk	r| j jS | jjƒ S )z> Returns the current file name from the edit control.
        N)r!   r?   r)   rB   )r1   r6   r6   r7   r]   &  s    
zSimpleEditor._get_value)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   rG   r8   r:   r   r@   r&   rE   r-   rN   rP   rQ   rS   rT   rH   rF   r>   r]   Ú__classcell__r6   r6   )r<   r7   r   :   s$   .
r   c                   sx   e Zd ZdZdZeZeZeZ	dd„ Z
‡ fdd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚCustomEditorzI Custom style of file editor, consisting of a file system tree view.
    Tc             C   sÐ   | j ƒ }| j}t|jƒdks&|jdkr0|tjO }tj||d�| _| jj	ƒ  | _
}|jƒ }tj||| jƒ tj||| jƒ tj||| jƒ |j| _| j|jdddd� | j|jddƒ | j|jd	d
ƒ | jƒ  dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        r   r   )r   r[   ÚfromT)Zis_listÚreloadÚdclickZtoN)Ú	get_styler    rZ   r[   Zfilter_namer   ÚDIRCTRL_SHOW_FILTERSÚGenericDirCtrlr   ÚGetTreeCtrlÚ_treer%   ÚEVT_TREE_SEL_CHANGEDr&   ÚEVT_TREE_ITEM_ACTIVATEDÚ
_on_dclickZEVT_TREE_ITEM_GETTOOLTIPÚ_on_tooltipZ
sync_valueZreload_nameZdclick_namer0   )r1   r2   r   r    Útreer9   r6   r6   r7   r8   H  s    
zCustomEditor.initc                sD   | j d }| _ |jƒ }tj||dƒ tj||dƒ tt| ƒjƒ  dS )z0 Disposes of the contents of an editor.
        N)rp   r%   r   rq   rr   r;   rh   r:   )r1   ru   r9   )r<   r6   r7   r:   _  s
    zCustomEditor.disposec             C   sB   | j dk	r>| j jƒ }| jjs$t|ƒr>| jjr8t|ƒd }|| _dS )zE Handles the user changing the contents of the edit control.
        Nr   )r   rI   r    Z	allow_dirr   rJ   r   r?   )r1   rC   Úpathr6   r6   r7   r&   n  s    

zCustomEditor.update_objectc             C   s   t | jƒr| jj| jƒ dS )z` Updates the editor when the object trait changes externally to the
            editor.
        N)r   rD   r   ÚSetPath)r1   r6   r6   r7   rE   }  s    
zCustomEditor.update_editorc             C   s   t jS )z9 Returns the basic style to use for the control.
        )r   ÚDIRCTRL_EDIT_LABELS)r1   r6   r6   r7   rl   ˆ  s    zCustomEditor.get_stylec             C   s   | j S )zC Returns the editor's control for indicating error status.
        )rp   )r1   r6   r6   r7   rN   ‘  s    zCustomEditor.get_error_controlc             C   s    | j jdj| jdd… ƒƒ dS )z3 Handles the 'filter' trait being changed.
        rU   N)r   Z	SetFilterr\   r[   )r1   r6   r6   r7   Ú_filter_changedš  s    zCustomEditor._filter_changedc             C   s   | j jƒ | _dS )z: Handles the user double-clicking on a file name.
        N)r   rI   rk   )r1   rC   r6   r6   r7   rs   £  s    zCustomEditor._on_dclickc             C   s   | j j|jƒ ƒ}|j|ƒ dS )zA Handles the user hovering on a file name for a tooltip.
        N)rp   ZGetItemTextZGetItemZ
SetToolTip)r1   rC   Útextr6   r6   r7   rt   ¬  s    zCustomEditor._on_tooltipc             C   s   | j jƒ  dS )z3 Handles the 'reload' trait being changed.
        N)r   ZReCreateTree)r1   r6   r6   r7   Ú_reload_changed¶  s    zCustomEditor._reload_changed)rc   rd   re   rf   Z
scrollableÚfilter_traitr[   r
   rj   rk   r8   r:   r&   rE   rl   rN   ry   rs   rt   r{   rg   r6   r6   )r<   r7   rh   3  s   				
rh   c               @   s@   e Zd ZeZeZdZdd„ Zdd„ Z	dd„ Z
dd	„ Zd
d„ ZdS )rb   Tc             C   sp   | j ƒ }t| jƒdkr |tjO }tj||dj| jƒd� | _}|j| j	ƒ |j
ƒ  | _}tj||jƒ | jƒ dS )z= Creates the file control and gets it ready for use.
        r   rU   )r   r[   N)rl   rZ   r[   r   rm   rn   r\   Ú_filesrw   rM   ro   rp   rq   r%   Ú_select_file)r1   r2   r   Úfilesru   r6   r6   r7   r"   Í  s    
zPopupFile.create_controlc             C   s&   t j| j| jjƒ d ƒ d  | _| _d S )N)r   rq   rp   r%   r}   )r1   r6   r6   r7   r:   Ú  s    zPopupFile.disposec             C   s   t jS )z8 Returns the base style for this type of popup.
        )r   rx   )r1   r6   r6   r7   rl   Þ  s    zPopupFile.get_stylec             C   s   t |ƒS )z3 Returns whether or not the path is valid.
        )r   )r1   rv   r6   r6   r7   Úis_validã  s    zPopupFile.is_validc             C   s<   | j jƒ }|t| jƒkr8|dd… dkr8| j|ƒr8|| _dS )z< Handles a file being selected in the file control.
        r   Nz/\r   )r}   rI   r   rM   r€   r?   )r1   rC   rv   r6   r6   r7   r~   ê  s
    

zPopupFile._select_fileN)rc   rd   re   r	   rM   r|   r[   Z	resizabler"   r:   rl   r€   r~   r6   r6   r6   r7   rb   À  s   rb   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r.   zC A target for a drag and drop operation, which accepts a file.
    c             C   s   t jj| ƒ || _d S )N)r   r.   Ú__init__Úeditor)r1   r‚   r6   r6   r7   r�     s    zFileDropTarget.__init__c             C   s   |d | j _| j jƒ  dS )Nr   Tr   )r‚   r?   rE   )r1   ÚxÚyZ
file_namesr6   r6   r7   ÚOnDropFiles  s    
zFileDropTarget.OnDropFilesN)rc   rd   re   rf   r�   r…   r6   r6   r6   r7   r.     s   r.   )rf   Ú
__future__r   r   Úos.pathr   r   r   r   r   Z
traits.apir   r	   r
   r   r   r   Ztraitsui.editors.file_editorr   Ztext_editorr   ZSimpleTextEditorÚhelperr   r   r|   rh   rb   r.   r6   r6   r6   r7   Ú<module>   s     z C