a
    §ˆcD  ã                   @   sÀ   d dl mZ d dlmZ d dlZd dlmZmZmZ d dl	Z	d dl
mZ d dlmZmZ e e¡Ze	 d¡Ze	 e	j¡Ze	 e	j¡Ze	jZedd	„ ƒZeddd„ƒZeeƒG dd„ deƒƒZdS )é    )Úcontextmanager)ÚBytesION)ÚdumpsÚloadÚloads)Úprovides)Ú
IClipboardÚBaseClipboardZPythonObjectc               	   c   sD   t  ¡ } | r:t  d¡ zdV  W t  d¡ q@t  d¡ 0 ndV  dS )aR   Ensure use of X11 clipboard rather than primary selection on X11.

    X11 allows pasting from either the clipboard or the primary selection.
    This context manager ensures that the clipboard is always used for Pyface,
    no matter what the wider application state is currently using.

    On non-X11 platforms this does nothing.
    FNT)ÚcbZIsUsingPrimarySelectionZUsePrimarySelection)Zusing_primary© r   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/pyface/ui/wx/clipboard.pyÚ_ensure_clipboard"   s    

r   Fc                 c   s:   zdV  W t  ¡  | r6t  ¡  nt  ¡  | r4t  ¡  0 dS )zÖ Ensures clipboard is closed and (optionally) flushed.

    Parameters
    ----------
    flush : bool
        Whether or not to flush the clipboard.  Should be true when setting
        data to the clipboard.
    N)r
   ÚCloseZFlush©Úflushr   r   r   Ú_close_clipboard7   s    

þr   c                   @   sp   e Zd Z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dd„ Zdd„ Zdd„ ZdS )Ú	ClipboardaY   WxPython implementation of the IClipboard interface.

    Python object data is transmitted as bytes consisting of the pickled class
    object followed by the corresponding pickled instance object.  This means
    that copy/paste of Python objects may not work unless compatible Python
    libraries are available at the pasting location.
    c              	   C   sz   d}t ƒ �\ t ¡ rXtƒ �. t t¡p8t t¡p8t t¡}W d   ƒ n1 sN0    Y  W d   ƒ n1 sl0    Y  |S ©NF)r   r
   ÚOpenr   ÚIsSupportedÚ
TextFormatÚ
FileFormatÚPythonObjectFormat)ÚselfÚresultr   r   r   Ú_get_has_dataW   s    
ÿý>zClipboard._get_has_datac              	   C   s–   d }t ƒ �x t ¡ rttƒ �J t t¡rVt t¡}t |¡rVt	| ¡ ƒ}t
|ƒ}t
|ƒ}W d   ƒ n1 sj0    Y  W d   ƒ n1 sˆ0    Y  |S ©N)r   r
   r   r   r   r   ÚwxÚCustomDataObjectÚGetDatar   r   )r   r   ÚcdoÚfileÚ_r   r   r   Ú_get_object_datag   s    


DzClipboard._get_object_datac              	   C   sˆ   t ƒ �n t ¡ rftdd��< t t¡}| t|j	ƒt|ƒ ¡ t |¡ W d   ƒ n1 s\0    Y  W d   ƒ n1 sz0    Y  d S ©NTr   )
r   r
   r   r   r   r   r   ÚSetDatar   Ú	__class__)r   Údatar    r   r   r   Ú_set_object_datat   s    
zClipboard._set_object_datac                 C   s
   |   t¡S r   )Ú_has_this_datar   ©r   r   r   r   Ú_get_has_object_data   s    zClipboard._get_has_object_datac              
   C   s¨   d}t ƒ �Š t ¡ r†tƒ �\ t t¡rht t¡}t |¡rhzt	| ¡ ƒ}W n t
yf   t d¡ Y n0 W d   ƒ n1 s|0    Y  W d   ƒ n1 sš0    Y  |S )NÚ z Cannot load data from clipboard.)r   r
   r   r   r   r   r   r   r   r   Ú	ExceptionÚloggerÚ	exception)r   r   r    r   r   r   Ú_get_object_type‚   s    


LzClipboard._get_object_typec              	   C   s„   d}t ƒ �f t ¡ r^tƒ �4 t t¡r@t ¡ }t |¡r@| 	¡ }W d   ƒ n1 sT0    Y  |W  d   ƒ S 1 sv0    Y  d S )Nr,   )
r   r
   r   r   r   r   r   ÚTextDataObjectr   ZGetText)r   r   Ztdor   r   r   Ú_get_text_data•   s    

&zClipboard._get_text_datac              	   C   sp   t ƒ �V t ¡ rNtdd��$ t t t|ƒ¡¡ W d   ƒ n1 sD0    Y  W d   ƒ n1 sb0    Y  d S r$   )r   r
   r   r   r%   r   r1   Ústr)r   r'   r   r   r   Ú_set_text_data    s    zClipboard._set_text_datac                 C   s
   |   t¡S r   )r)   r   r*   r   r   r   Ú_get_has_text_data¦   s    zClipboard._get_has_text_datac              	   C   s„   t ƒ �j g }t ¡ r^tƒ �4 t t¡r@t ¡ }t |¡r@| 	¡ }W d   ƒ n1 sT0    Y  |W  d   ƒ S 1 sv0    Y  d S r   )
r   r
   r   r   r   r   r   ÚFileDataObjectr   ZGetFilenames)r   r   Útfor   r   r   Ú_get_file_data­   s    

&zClipboard._get_file_datac              	   C   s˜   t ƒ �~ t ¡ rvtdd��L t ¡ }t|tƒr:| |¡ n|D ]}| |¡ q>t 	|¡ W d   ƒ n1 sl0    Y  W d   ƒ n1 sŠ0    Y  d S r$   )
r   r
   r   r   r   r6   Ú
isinstancer3   ZAddFiler%   )r   r'   r7   Úfilenamer   r   r   Ú_set_file_data¸   s    
zClipboard._set_file_datac                 C   s
   |   t¡S r   )r)   r   r*   r   r   r   Ú_get_has_file_dataÄ   s    zClipboard._get_has_file_datac              	   C   sf   d}t ƒ �H t ¡ rDtƒ � t |¡}W d   ƒ n1 s:0    Y  W d   ƒ n1 sX0    Y  |S r   )r   r
   r   r   r   )r   Úformatr   r   r   r   r)   Ë   s    FzClipboard._has_this_dataN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r(   r+   r0   r2   r4   r5   r8   r;   r<   r)   r   r   r   r   r   I   s   r   )F)Ú
contextlibr   Úior   ÚloggingÚpickler   r   r   r   Z
traits.apir   Zpyface.i_clipboardr   r	   Ú	getLoggerr>   r.   Z
DataFormatr   ZDF_TEXTr   ZDF_FILENAMEr   ZTheClipboardr
   r   r   r   r   r   r   r   Ú<module>   s"   


