
O(Oc           @   s   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 Z
 d Z d Z d Z d Z d Z d Z d Z d Z d d d     YZ d S(   iNt   UTF8_STRINGt   CTD_RICHt	   CTD_TABLEt   CTD_CODEBOXs	   text/htmls   text/uri-listt   COMPOUND_TEXTt   STRINGt   TEXTs	   image/pngs
   image/jpegs	   image/bmps
   image/tiffs   image/x-MS-bmps   image/x-bmpt   ClipboardHandlerc           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d e d  Z d d  Z RS(   s   Handler of Clipboardc         C   s%   | |  _  t j   |  _ t |  _ d S(   s   Clipboard Handler bootN(   t   dadt   gtkt   clipboard_gett	   clipboardt   Falset   force_plain_text(   t   selfR   (    (    s   modules/clipboard.pyt   __init__'   s    	c         C   s=   | j  d  |  j j j   s# d S|  j |  j j |  d S(   s   Copy to Clipboards   copy-clipboardN(   t   stop_emissionR   t   curr_buffert   get_has_selectiont   selection_to_clipboard(   R   t
   sourceview(    (    s   modules/clipboard.pyt   copy-   s     c         C   s   | j  d  |  j j j   s# d S|  j |  j j |  |  j j |  j j d s |  j j j t | j	    |  j j
 j   n  d S(   s   Cut to Clipboards   cut-clipboardNi   (   R   R   R   R   R   t	   treestoret   curr_tree_itert   delete_selectiont   Truet   get_editableR   t
   grab_focus(   R   R   (    (    s   modules/clipboard.pyt   cut3   s     c         C   sc   |  j  j j |  } |  j j g  t t f D] } | d d f ^ q+ |  j |  j | d | f  d S(   s+   Put the Selected Table Row to the Clipboardi    N(
   R   t   html_handlert   table_export_to_htmlR   t   set_with_datat   TARGET_CTD_TABLEt   TARGET_HTMLt   get_funct
   clear_funct   None(   R   t
   table_dictt	   html_textt   t(    (    s   modules/clipboard.pyt   table_row_to_clipboard<   s
    .c         C   sF   |  j  j   } | s t St | k rB |  j  j t |  j |  t St S(   s"   Paste Table Row from the Clipboard(   R   t   wait_for_targetsR   R    t   request_contentst   to_tableR   (   R   t   model_n_itert   targets(    (    s   modules/clipboard.pyt   table_row_pasteD   s     c         C   s  | j    \ } } | j   | j   } | d k r| j   } | rt |  } d | k r |  j j g  t D] } | d d f ^ qn |  j |  j | j	  d Sd | k r$|  j
 j j |  }	 |  j
 j j |	  }
 |  j j g  t t f D] } | d d f ^ q |  j |  j |	 d |
 f  d Sd | k r|  j
 j j | d d } |  j
 j j | d d } |  j
 j j |  }
 |  j j g  t t f D] } | d d f ^ q|  j |  j | d |
 f  d Sqn  t j j t j  st j t j  n  |  j
 j j | | | |  j
 j  }
 |  j
 j t j k r| j | |  } |  j | | |  } |  j j g  t  t! t f D] } | d d f ^ qp|  j |  j | | |
 f  n\ | j | |  } |  j j g  t  t f D] } | d d f ^ q|  j |  j | d |
 f  d S(	   s+   Write the Selected Content to the Clipboardi   t   pixbufi    Nt	   liststoret   sourcebuffert	   for_printi   ("   t   get_selection_boundst
   get_offsett   get_child_anchort   dirR   R   t   TARGETS_IMAGESR"   R#   R/   R   t   state_machinet   table_to_dictR   R   R    R!   R$   t   codebox_to_dictt   codebox_export_to_htmlt   TARGET_CTD_CODEBOXt   ost   patht   isdirt   const
   TMP_FOLDERt   mkdirt   selection_export_to_htmlt   syntax_highlightingt   CUSTOM_COLORS_IDt   get_textt(   rich_text_get_from_text_buffer_selectiont   TARGET_CTD_PLAIN_TEXTt   TARGET_CTD_RICH_TEXT(   R   t   text_bufferR   t   iter_sel_startt   iter_sel_endt	   num_charst   anchort
   anchor_dirR'   R%   R&   t   codebox_dictt   codebox_dict_htmlt
   plain_textt	   rich_text(    (    s   modules/clipboard.pyR   M   sX    (
..
 $1.c         C   sX  | j    } | t k r2 | j d d | d  n"| t k rX | j d d | d  n | t k r~ | j d d | d  n | t k r t j j j	   } |  j
 j j d | d d g |  | j d d | j    ny | t k r8t j j j	   } |  j
 j j d | d d g |  | j d d | j    n | t k rT| j |  n  d S(   s&   Connected with clipboard.set_with_dataR    i   i    i   i   t   leftN(   t
   get_targetRH   t   setRI   R!   R<   t   xmlt   domt   minidomt   DocumentR   t   xml_handlert   codebox_element_to_xmlt   toxmlR    t   table_element_to_xmlR7   t
   set_pixbuf(   R   R   t   selectiondatat   infot   datat   targetRX   (    (    s   modules/clipboard.pyR"   |   s"       ## c         C   s   ~ d S(   s&   Connected with clipboard.set_with_dataN(    (   R   R   Rb   (    (    s   modules/clipboard.pyR#      s    c      
   C   s]  | j  d  |  j j |  j j d r+ d S|  j j   } | sD d S|  j j j t | j	    |  j
 r t |  _
 x7 t D], } | | k ry |  j j | |  j  d Sqy Wn  t | k r |  j j t j k r |  j j t |  j  d St | k r"|  j j t j k r"|  j j t |  j  d St | k r`|  j j t j k r`|  j j t |  j d  d St | k r|  j j t j k r|  j j t |  j  d St | k r|  j j t j k r|  j j t |  j  d SxI t D]A } | | k r|  j j t j k r|  j j | |  j  d SqWx4 t D], } | | k r)|  j j | |  j  Pq)q)Wd S(   s   Paste from Clipboards   paste-clipboardi   N(   R   R   R   R   R   R)   R   R   R   R   R   R   t   TARGETS_PLAIN_TEXTR*   t   to_plain_textRI   RD   R@   RE   t   to_rich_textR<   t
   to_codeboxR    R+   R$   R!   t   to_htmlt   TARGET_URI_LISTt   to_uri_listR7   t   to_image(   R   R   R-   Rc   (    (    s   modules/clipboard.pyt   paste   sH      		!!!!!!c      	   C   s2  t  j t j d | j  } | j t j  } x| D]} t |  d k r4 |  j j	 j
 |  j j	 j    } | d d !d k s | d d !d k r d | } n | d d !d	 k r| d j d
 t j  }	 t j |	  d }
 t |
  d k ry|
 d d !d k ryt j j |	  ry|  j j | t j j |	   |  j j	 j
 |  j j	 j    } |  j j	 j | d t j  q4 qd t j |	  } n d } | j   } |  j j	 j | | d t j  | r*|  j j	 j |  } |  j j	 j | t |   } |  j j	 j |  j j d |  | |  q*q4 q4 Wd S(   s   From Clipboard to URI listt    i   i    i   t   httpi   t   ftps   webs s   file://s   %20i   i   s   image/s   file %st   linkN(   t   ret   subR@   t	   BAD_CHARSRb   t   splitt   CHAR_NEWLINEt   lenR   R   t   get_iter_at_markt
   get_insertt   replacet
   CHAR_SPACEt	   mimetypest
   guess_typeR=   R>   t   isfilet   image_insertR	   t   gdkt   pixbuf_new_from_filet   insertt   base64t	   b64encodeR$   R4   t   get_iter_at_offsett   apply_tag_by_namet   apply_tag_exist_or_create(   R   R   R`   Rb   t   selection_datat   uri_listt   elementt   iter_insertt   property_valuet	   file_patht   mimetypet   start_offsetRK   RL   (    (    s   modules/clipboard.pyRj      s0    !& 7!!c         C   s   t  | j d  d k rJ t  | j d  d
 k rJ | j j d d  } n< t j d | j  } | r} | j j d d  } n	 | j } t j t j d |  } t j	 |  j
  } | j |  } |  j |  d	 S(   s   From Clipboard to HTML Texti    i   i   i   s   utf-16t   ignores   .* \w \w \w.*Rm   N(   i   i   (   t   ordRb   t   decodeRq   t   matchRr   R@   Rs   t   importst   HTMLFromClipboardHandlerR   t   get_clipboard_selection_xmlt   from_xml_string_to_buffer(   R   R   R`   Rb   R   R   t   html_importt
   xml_string(    (    s   modules/clipboard.pyRh      s     	c         C   s&  | j    } |  j j j |  j j j    } | j   } |  j j j | |  |  j j t j	 k r"x t
 j |  D] } |  j j j | | d  } |  j j j | | d  }	 | | d | d !}
 |
 d d !d
 k r d |
 }
 n  d |
 } |  j j j |  j j d |  | |	  qt Wn  d	 S(   s   From Clipboard to Plain Texti    i   i   t   httRo   s   http://s   webs Rp   N(   R   s   ftp(   RF   R   R   Rw   Rx   R4   R   RD   R@   RE   R   t%   get_web_links_offsets_from_plain_textR   R   R   (   R   R   R`   Rb   RR   R   R   t   offsetsRK   RL   t   link_urlR   (    (    s   modules/clipboard.pyRe      s    ! 
c         C   s   |  j  | j    d S(   s   From Clipboard to Rich TextN(   R   RF   (   R   R   R`   Rb   (    (    s   modules/clipboard.pyRf      s    c         C   s  t  j j j |  } | j } | j d k r6 d GHd S| j } x | d k r| j d k r| j } x | d k r | j d k r |  j |  n` | j d k r |  j |  nA | j d k r |  j	 | d  n | j d k r |  j
 |  n  | j } qi Wn  | j } qB Wd S(	   s   From XML String to Text Buffert   roots   rich text from clipboard errorNt   slotRS   t   encoded_pngt   tablet   codebox(   RW   RX   RY   t   parseStringt
   firstChildt   nodeNameR$   t   dom_node_to_rich_textt   dom_node_to_imaget   dom_node_to_tablet   dom_node_to_codeboxt   nextSibling(   R   R   RX   t   dom_nodet   child_dom_itert   nephew_dom_iter(    (    s   modules/clipboard.pyR      s(    			c         C   s  | j  r | j  j } n d } g  } xX t j D]M } | j |  r. | j | j } | r{ | j |  j j	 | |   q{ q. q. Wt
 |  } | d k r |  j j j |  j j j |  j j j    |  n4 |  j j j |  j j j |  j j j    | |  d S(   s   From dom_node to Rich TextRm   i    N(   R   Rb   R@   t   TAG_PROPERTIESt   hasAttributet
   attributest   valuet   appendR   R   Rv   R   R   Rw   Rx   t   insert_with_tags_by_name(   R   R   t   textt	   tag_namest   tag_propertyR   t   tags_num(    (    s   modules/clipboard.pyR     s     	  &*
*c         C   s>   | j    } |  j j |  j j j |  j j j    |  d S(   s   From Clipboard to ImageN(   t
   get_pixbufR   R~   R   Rw   Rx   (   R   R   R`   Rb   R/   (    (    s   modules/clipboard.pyRk   "  s    c         C   s   | j  d  r" | j d j } n d } | j  d  rb t j j t j  } | j d j | _ n t	 j
 | j j  } | r |  j j |  j j j |  j j j    | d | d |  j j n  d S(   s   From dom_node to Imaget   justificationRT   RN   t   image_justificationRJ   N(   R   R   R   R	   R   R   R@   t   ANCHOR_CHARRN   t   machinest   get_pixbuf_from_encoded_bufferR   Rb   R   R~   R   Rw   Rx   (   R   R   R   R/   (    (    s   modules/clipboard.pyR   '  s     'c         C   sM   t  j j j | j    } | j } | j d k r< d GHd S|  j |  d S(   s   From Clipboard to CodeBoxR   s   codebox from clipboard errorN(   RW   RX   RY   R   RF   R   R   R   (   R   R   R`   Rb   RX   R   (    (    s   modules/clipboard.pyRg   5  s    	c      
   C   s?  | j  d  r | j d j n d } i t | j d j  d 6t | j d j  d 6| j  d  ox | j d j d k d 6| j d j d 6| j  d  o | j d j d k d 6| j  d	  o | j d	 j d k d	 6| j r | j j n d
 d 6} |  j j j |  j j	 j
 |  j j	 j    | d | d |  j j	 d S(   s   From dom_node to CodeBoxR   RT   t   frame_widtht   frame_heightt   width_in_pixelsR   RD   t   highlight_bracketst   show_line_numbersRm   t	   fill_textt   codebox_justificationRJ   N(   R   R   R   t   intR   Rb   R   t   codeboxes_handlert   codebox_insertR   Rw   Rx   (   R   R   R   RP   (    (    s   modules/clipboard.pyR   >  s    %&&&*c         C   sP   t  j j j | j    } | j } | j d k r< d GHd S|  j | |  d S(   s   From Clipboard to TableR   s   table from clipboard errorN(   RW   RX   RY   R   RF   R   R   R   (   R   R   R`   t   table_model_n_iterRX   R   (    (    s   modules/clipboard.pyR+   O  s    	c         C   s+  | j  d  r" | j d j } n d } i g  d 6t | j d j  d 6t | j d j  d 6} | j } x | d k r#| j d k r| d j g   | j } xp | d k r| j d k r| j d k r | d d j | j j  q| d d j d	  n  | j	 } q Wn  | j	 } qo W| sp|  j
 j j |  j
 j j |  j
 j j    | d
 | d |  j
 j n | \ } } | j   }	 x t | d  D] \ }
 } |
 t | d  d k  rt |  |	 k r| |	  } n0 t |  |	 k  r| d	 g |	 t |  } n  | j | |  } qqWd S(   s   From dom_node to TableR   RT   t   matrixt   col_mint   col_maxt   rowt   celliRm   t   table_justificationRJ   i   N(   R   R   R   R   R   R$   R   R   Rb   R   R   t   tables_handlert   table_insertR   Rw   Rx   t   get_n_columnst	   enumerateRv   t   insert_after(   R   R   R   R   R   R   R   t   modelt   itert   num_columnst   it	   table_row(    (    s   modules/clipboard.pyR   X  s@     
		 *  t   nc      	   C   s:  | j    } | j    } | r+ | d 8} n  | | f } |  j j j | d d d | }	 t j j j   }
 |
 j d  } |
 j	 |  d } | j    } xp |	 D]h } | d d } | t
 |	  k  r |	 | } n d } |  j |
 | | | | | |  | d 7} | } q W|  j |
 | | | j    | d |  |
 j   S(   s:   Given text_buffer and selection, returns the rich text xmli   R2   i    t	   sel_rangeR   N(   R4   R   R8   t%   get_embedded_pixbufs_tables_codeboxesRW   RX   RY   RZ   t   createElementt   appendChildRv   R$   t   rich_text_process_slotR]   (   R   RJ   RK   RL   t   change_caset   exclude_iter_sel_endt   iter_sel_start_offsett   iter_sel_end_offsett   iter_sel_ranget   pixbuf_table_codebox_vectorRX   R   t   obj_posR   t   end_offset_elementt
   end_offsett   obj_element(    (    s   modules/clipboard.pyRG   z  s.     	 

%c      	   C   s2  | j  d  } | j |  | j |  }	 i  |  _ x t j D] }
 d |  j |
 <q> W|	 j   } |  j j j	 | |  j  | j
 d
  } x| rK| j   | k r | j |  } n  |  j j j | |	 | |  j d | d | | j   } | | k r Pq |  j j j	 | |  j  |	 j |  | j
 d
  } | j   | k r Pq q W| j   | k rp| j |  } n  |  j j j | |	 | |  j d | d | | r.| d d k r|  j j j | d | |  q.| d d k r|  j j j | d |  q.| d d	 k r.|  j j j | d |  q.n  d
 S(   s   Process a Single Pango SlotR   Rm   R   RX   i    R/   i   R   R   N(   R   R   R   t   curr_attributesR@   R   R   R   R[   t   rich_text_attributes_updatet   forward_to_tag_toggleR$   R4   t   rich_txt_serializet
   set_offsett   pixbuf_element_to_xmlR^   R\   (   R   RX   R   R   R   RJ   R   R   t   dom_itert
   start_iterR   t	   curr_itert	   tag_foundt
   offset_old(    (    s   modules/clipboard.pyR     sB    	 	        (   t   __name__t
   __module__t   __doc__R   R   R   R(   R.   R   R"   R#   Rl   Rj   Rh   Re   Rf   R   R   Rk   R   Rg   R   R+   R   R   RG   R   (    (    (    s   modules/clipboard.pyR   $   s0   								/			&														#(   s   UTF8_STRINGR   s   STRINGR   (   s	   image/pngs
   image/jpegs	   image/bmps
   image/tiffs   image/x-MS-bmps   image/x-bmp(    (   R	   R=   t   xml.dom.minidomRW   Rq   R   R{   R@   R   t   exportsR   RH   RI   R    R<   R!   Ri   Rd   R7   R   (    (    (    s   modules/clipboard.pyt   <module>   s   H0