a
    §ˆc§&  ã                   @   s  d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
 zddlmZ W n eyf   dZY n0 zddlmZ W n eyŽ   dZY n0 ddlmZ d	d
lmZ edƒ ¡ Zedƒ ¡ Zedƒ ¡ Zeef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 )a0   Defines the various editors for a drag-and-drop editor,
    for the wxPython user interface toolkit. A drag-and-drop editor represents
    its value as a simple image which, depending upon the editor style, can be
    a drag source only, a drop target only, or both a drag source and a drop
    target.
é    N)Úload)ÚBool)ÚPythonDropSourceÚPythonDropTargetÚ	clipboard)ÚFile)ÚBinding)ÚImageResourceé   )ÚEditorÚfileÚobjectZinactivec                       s€   e Zd ZdZedƒZedƒ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dd„ Z‡  ZS )ÚSimpleEditorzjSimply style of editor for a drag-and-drop editor, which is both a drag
    source and a drop target.
    Tc                 C   sæ  | j }t|tƒ| _t|tƒp:| jo:t|ƒdko:t|d tƒ| _| jj}|durp| 	¡ }| jj
}|dur‚| 	¡ }nt}t}| jr‚t}| ¡ | _|dur | ¡ | _n¾t t | ¡ tj¡d¡t g d¢g¡ }|dd…df |dd…df  |dd…df  }| |dd…df<  |dd…df< |dd…df< | t | tj¡¡ ¡ ¡ | ddd¡ | ¡ | _tj|dt | ¡ |  ¡ ¡d� | _!}|  "¡  | j#�r¢| $t%| ƒ¡ | &tj'| j(¡ | &tj)| j*¡ | &tj+| j,¡ | &tj-| j.¡ dS )	z\Finishes initializing the editor by creating the underlying toolkit
        widget.
        r   N)éÿÿÿÿé   )g5^ºIÓ?g+‡Ùâ?gÉv¾Ÿ/½?r
   é   r   )Úsize)/ÚvalueÚ
isinstanceÚlistZ_is_listÚstring_typeÚlenÚ_is_fileÚfactoryÚimageÚcreate_imageÚdisabled_imageÚinactive_imageÚobject_imageÚ
file_imageZConvertToBitmapÚ_imageÚ_disabled_imageÚnumpyZreshapeÚ
fromstringZGetDataZuint8ÚarrayÚSetDataZravelZastypeÚtostringZSetMaskColourÚwxZWindowÚSizeÚGetWidthÚ	GetHeightÚcontrolZset_tooltipÚdrop_targetZSetDropTargetr   ZBindÚEVT_LEFT_DOWNÚ
_left_downÚEVT_LEFT_UPÚ_left_upÚ
EVT_MOTIONÚ_mouse_moveÚ	EVT_PAINTÚ	_on_paint)ÚselfÚparentr   r   r   ÚdataÚgr+   © r9   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/wx/dnd_editor.pyÚinitP   sR    

ÿý

ÿþ00
ÿzSimpleEditor.initc                    sD   | j }| tj¡ | tj¡ | tj¡ | tj¡ tƒ  ¡  dS )z&Disposes of the contents of an editor.N)	r+   ZUnbindr'   r-   r/   r1   r3   ÚsuperÚdispose)r5   r+   ©Ú	__class__r9   r:   r=   …   s    zSimpleEditor.disposec                 C   s   dS )z[Updates the editor when the object trait changes externally to the
        editor.
        Nr9   )r5   r9   r9   r:   Úupdate_editor�   s    zSimpleEditor.update_editorc                 C   sØ   t |tƒrˆtdur.t |d tƒr.dd„ |D ƒ}tdurÔt |d tƒrÔdd„ |D ƒ}| jsÔg }|D ] }|  |¡}|dur`| |¡ q`|}nLtdur t |tƒr |j}tdurÔt |tƒrÔ|j}| jsÔ|  |¡}|durÔ|}|S )z7Returns the processed version of a drag request's data.Nr   c                 S   s   g | ]
}|j ‘qS r9   )Úobj©Ú.0Úitemr9   r9   r:   Ú
<listcomp>œ   ó    z/SimpleEditor._get_drag_data.<locals>.<listcomp>c                 S   s   g | ]
}|j ‘qS r9   )Úabsolute_pathrB   r9   r9   r:   rE   Ÿ   rF   )	r   r   r   r   r   Ú	_unpickleÚappendrA   rG   )r5   r7   Úresultr   rD   r   r9   r9   r:   Ú_get_drag_data—   s*    


zSimpleEditor._get_drag_datac              	   C   sR   t |dƒ�4}zt|ƒ}W n ty.   d}Y n0 W d  ƒ n1 sD0    Y  |S )z@Returns the unpickled version of a specified file (if possible).ÚrbN)Úopenr   Ú	Exception)r5   Ú	file_nameÚfhr   r9   r9   r:   rH   µ   s    (zSimpleEditor._unpicklec                 C   sX   | j }| j}| ¡ s| j}| ¡ \}}t |¡ ||| ¡  d || 	¡  d d¡ dS )z)Called when the control needs repainting.r   TN)
r    r+   Ú	IsEnabledr!   ZGetClientSizer'   ZPaintDCZ
DrawBitmapr)   r*   )r5   Úeventr   r+   ZwdxZwdyr9   r9   r:   r4   Á   s    
üzSimpleEditor._on_paintc                 C   s<   | j  ¡ r0| jr0| ¡ | ¡  | _| _| j  ¡  | ¡  dS )z,Handles the left mouse button being pressed.N)	r+   rQ   Údrag_sourceÚGetXÚGetYÚ_xÚ_yZCaptureMouseÚSkip©r5   rR   r9   r9   r:   r.   Ð   s    
zSimpleEditor._left_downc                 C   s&   | j durd| _ | j ¡  | ¡  dS )z-Handles the left mouse button being released.N)rV   r+   ÚReleaseMouserX   rY   r9   r9   r:   r0   Ø   s    

zSimpleEditor._left_upc                 C   sr   | j durft| j | ¡  ƒt| j| ¡  ƒ dkrf| j ¡  d| _ | jrXt| j| j	ƒ nt
| j| j	ƒ | ¡  dS )zHandles the mouse being moved.Nr   )rV   ÚabsrT   rW   rU   r+   rZ   r   ÚFileDropSourcer   r   rX   rY   r9   r9   r:   r2   à   s    
"þ
zSimpleEditor._mouse_movec                 C   s*   z|   |¡| _|W S    tj Y S 0 dS )z2Handles a Python object being dropped on the tree.N)rK   r   r'   ÚDragNone©r5   ÚxÚyr7   Údrag_resultr9   r9   r:   Úwx_dropped_onñ   s
    zSimpleEditor.wx_dropped_onc                 C   s@   z(| j  | j¡ | j | j|  |¡¡ |W S    tj Y S 0 dS )z4Handles a Python object being dragged over the tree.N)r   Z
base_traitÚnameÚvalidaterK   r'   r]   r^   r9   r9   r:   Úwx_drag_overù   s    ÿzSimpleEditor.wx_drag_over)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r,   rS   r;   r=   r@   rK   rH   r4   r.   r0   r2   rb   re   Ú__classcell__r9   r9   r>   r:   r   A   s   	5
r   c                   @   s   e Zd ZdZdZdS )ÚCustomEditorzACustom style of drag-and-drop editor, which is not a drag source.FN)rf   rg   rh   ri   rS   r9   r9   r9   r:   rk     s   rk   c                   @   s   e Zd ZdZdZdS )ÚReadonlyEditorzDRead-only style of drag-and-drop editor, which is not a drop target.FN)rf   rg   rh   ri   r,   r9   r9   r9   r:   rl     s   rl   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )r\   zRepresents a draggable file.c                    sp   d| _ d| _|t_|t_| t_t ¡ }t|t	ƒr6|g}|D ]}| 
|¡ q:tƒ  |¡ |  |¡ |  d¡| _dS )zInitializes the object.NT)ÚhandlerZ
allow_mover   r7   ÚsourceZdrop_sourcer'   ZFileDataObjectr   r   ZAddFiler<   Ú__init__r%   Z
DoDragDroprJ   )r5   rn   ÚfilesZdata_objectr   r>   r9   r:   ro     s    

zFileDropSource.__init__c                 C   s   dS )z&Called when the data has been dropped.Nr9   )r5   ra   r9   r9   r:   Ú
on_dropped3  s    zFileDropSource.on_dropped)rf   rg   rh   ri   ro   rq   rj   r9   r9   r>   r:   r\     s   r\   )ri   r'   r"   Úpickler   Z
traits.apir   Zpyface.wx.drag_and_dropr   r   r   Zapptools.ior   ÚImportErrorZapptools.naming.apir   Zpyface.image_resourcer	   Zeditorr   r   r   r   r   Ústrr   r   rk   rl   Z
DropSourcer\   r9   r9   r9   r:   Ú<module>   s0   

 D