3
8ßL]”G  ã               @   sÎ   d Z ddlmZ ddlZddljZddlmZ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 ddlmZ d	d
lmZ d	dlmZmZ dZd	ZdZG dd„ deƒZe Z Z Z!G dd„ deƒZ"dS )zo Defines a source code editor for the wxPython user interface toolkit,
    useful for tools such as debuggers.
é    )Úabsolute_importN)ÚStrÚListÚIntÚEventÚBoolÚ
TraitErrorÚon_trait_change)ÚSequenceTypes)ÚToolkitEditorFactory)ÚPythonEditor)Úfacesé   )ÚEditor)ÚOKColorÚ
ErrorColoré   c                   s  e Zd ZdZdZedƒZeZe	Z
eeƒZeZeZeZeZeeƒZe	ZedƒZeeƒZe	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d„ Z#dd „ Z$e%d!ƒd"d#„ ƒZ&d$d%„ Z'd&d'„ Z(‡ fd(d)„Z)d*d+„ Z*d,d-„ Z+‡  Z,S ).ÚSourceEditorzC Editor for source code, which displays a PyFace PythonEditor.
    TFé   c             C   s�  | j }t||jd� | _}|j | _}|jtjddƒƒ |jt	dƒt
jƒ |jt	dƒt
jƒ tj|| jƒ t
j||jƒ | jƒ |jrœ|jdkrœtj|| jƒ |jr´|j| jddd	� |jd
k	rÐ|j| jddd	� | jrà|jdƒ |jt
jƒ |jt
j| j ƒ yt!t
d| j j"j#ƒ  ƒ| _"W n t$k
�r6   t
j%| _"Y nX |j&t't
j(|j)d� |j&t*t
j(|j+d� |j&t,t
j(|j-d� | j.ƒ  | j/|j0dddd� | j/|jddƒ | j/|j1ddƒ | j/|j2dƒ | j/|j3dƒ | j/|j4dƒ | j/|j5dddd� | j j6dk�rd| _6n| j/|j6ddƒ | j/|j7dddd� |j8dk�r<d| _8n| j/|j8ddƒ |j2dk�sp|j3dk�sp|j1dk�r„t
j9||jƒ | j:ƒ | j;ƒ  d
S )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        )Úshow_line_numbersi,  é|   ÚBÚNÚ ÚchangedÚui)ÚdispatchNÚkey_pressedTZSTC_LEX_)Z
backgroundÚ
mark_linesÚfrom)Zis_listÚselected_lineÚselected_textZtoÚlineÚcolumnÚcalltip_clickedÚ	dim_linesz	dark greyÚ	dim_colorÚsquiggle_linesZredÚsquiggle_color)<Úfactoryr   r   Ú_editorÚcontrolZSetSizeÚwxÚSizeZCmdKeyClearÚordÚstcZSTC_SCMOD_CTRLÚEVT_KILL_FOCUSÚwx_update_objectZEVT_STC_CALLTIP_CLICKZGetIdÚ_calltip_clickedÚauto_scrollr    ZEVT_SIZEZ_update_selected_lineÚauto_setr	   Úupdate_objectÚkey_bindingsr   ÚreadonlyÚSetReadOnlyÚSetLexerÚSTC_LEX_CONTAINERZBindZEVT_STC_STYLENEEDEDÚ_style_neededÚgetattrÚlexerÚupperÚAttributeErrorÚSTC_LEX_NULLZMarkerDefineÚMARK_MARKERZSTC_MARK_BACKGROUNDZmark_color_ÚSEARCH_MARKERZsearch_color_ÚSELECTED_MARKERZselected_color_Úupdate_editorZ
sync_valuer   r!   r"   r#   r$   r%   r&   r'   r(   ZEVT_STC_UPDATEUIÚ_position_changedZset_tooltip)ÚselfÚparentr)   Úeditorr+   © rI   ú=/tmp/pip-install-5sebvq2j/traitsui/traitsui/wx/code_editor.pyÚinit{   sl    









zSourceEditor.initc             C   s   | j ƒ  |jƒ  dS )zC Handles the user entering input data in the edit control.
        N)r5   ZSkip)rF   ÚeventrI   rI   rJ   r1   Û   s    zSourceEditor.wx_update_objectc             C   sl   | j shy>| jjƒ }t| jtƒr&|jƒ }|| _| jjtƒ | jj	ƒ  W n" t
k
rf } zW Y dd}~X nX dS )zC Handles the user entering input data in the edit control.
        N)Ú_lockedr+   ÚGetTextÚ
isinstanceÚvaluer
   ÚsplitÚSetBackgroundColourr   ÚRefreshr   )rF   rP   ÚexcprI   rI   rJ   r5   á   s    
zSourceEditor.update_objectc             C   s¨   d| _ | j}t|tƒr*djdd„ |D ƒƒ}| j}|jƒ |krž|jƒ }|jdƒ |j	ƒ }|j
ƒ }|j|ƒ |j|ƒ |j|ƒ |j|ƒ | jƒ  | jƒ  | jƒ  d| _ dS )z` Updates the editor when the object trait changes externally to the
            editor.
        TÚ
c             S   s   g | ]}|j ƒ ‘qS rI   )Úrstrip)Ú.0r"   rI   rI   rJ   ú
<listcomp>ú   s    z.SourceEditor.update_editor.<locals>.<listcomp>FN)rM   rP   rO   r
   Újoinr+   rN   ZGetReadOnlyr8   ZGetFirstVisibleLineÚGetCurrentPosZSetTextÚGotoPosÚScrollToLineÚ_mark_lines_changedÚ_selected_line_changedÚ_style_document)rF   Z	new_valuer+   r7   Úl1ÚposrI   rI   rJ   rD   ó   s$    





zSourceEditor.update_editorc             C   s
   d| _ d S )NT)r$   )rF   rL   rI   rI   rJ   r2     s    zSourceEditor._calltip_clickedc             C   s`   | j }| j}|jƒ }|jtƒ x4|D ],}d|  k o:|kn  r$|j|d tƒ q$W |jƒ  dS )z8 Handles the set of marked lines being changed.
        r   r   N)r   r+   ÚGetLineCountÚMarkerDeleteAllrA   Ú	MarkerAddrS   )rF   Úlinesr+   Úlcr"   rI   rI   rJ   r]     s    

z SourceEditor._mark_lines_changedc             C   s   | j ƒ  d S )N)r]   )rF   rI   rI   rJ   Ú_mark_lines_items_changed$  s    z&SourceEditor._mark_lines_items_changedc             C   sn   | j }| j}tdt|jƒ |ƒƒd }|jtƒ |j|tƒ |j|ƒ | j	j
rb|j||jƒ d  ƒ |jƒ  dS )z? Handles a change in which line is currently selected.
        r   r   N)r    r+   ÚmaxÚminrb   rc   rC   rd   ÚGotoLiner)   r3   r\   ZLinesOnScreenrS   )rF   r"   r+   rI   rI   rJ   r^   +  s    

z#SourceEditor._selected_line_changedc             C   s   | j s| jj|d ƒ d S )Nr   )rM   r+   rj   )rF   r"   rI   rI   rJ   Ú_line_changed=  s    zSourceEditor._line_changedc             C   s6   | j s2| j}|j|jƒ ƒ}|j|j|ƒ| d ƒ d S )Nr   )rM   r+   ÚLineFromPositionrZ   r[   ÚPositionFromLine)rF   r#   r+   r"   rI   rI   rJ   Ú_column_changedE  s    zSourceEditor._column_changedc             C   sP   | j }|jƒ }|j|ƒ}d| _|d | _||j|ƒ d | _d| _|jƒ | _dS )z4 Handles the cursor position being changed.
        Tr   FN)	r+   rZ   rl   rM   r"   rm   r#   ZGetSelectedTextr!   )rF   rL   r+   ra   r"   rI   rI   rJ   rE   O  s    

zSourceEditor._position_changedc             C   s    | j jj|j| jj| jjƒ dS )z8 Handles a key being pressed within the editor.
        N)r)   r6   ZdorL   r   ÚhandlerÚinfo)rF   rL   rI   rI   rJ   r   _  s    zSourceEditor.key_pressedc             C   sD   | j j| j| jƒ | j j| jdƒ | j j| jtd ƒ | j jƒ  d S )Nzcourier newÚsize)r+   ZStyleSetForegroundÚ_dim_style_numberr&   ZStyleSetFaceNameZStyleSetSizer   rS   )rF   rI   rI   rJ   Ú_dim_color_changedi  s    zSourceEditor._dim_color_changedc             C   s.   | j jdtjƒ | j jd| jƒ | j jƒ  d S )Nr   )r+   ZIndicatorSetStyler/   ZSTC_INDIC_SQUIGGLEZIndicatorSetForegroundr(   rS   )rF   rI   rI   rJ   Ú_squiggle_color_changedo  s    z$SourceEditor._squiggle_color_changedzdim_lines, squiggle_linesc             C   s&   | j jƒ  | j jddƒ | j jƒ  dS )z\ Force the STC to fire a STC_STYLENEEDED event for the entire
            document.
        r   r   Néÿÿÿÿ)r+   ZClearDocumentStyleÚ	ColouriserS   )rF   rI   rI   rJ   r_   t  s    
zSourceEditor._style_documentc             C   sT  | j jƒ }| j j|ƒ}|jƒ }| j j|ƒ}|| jkr<|d8 }| jtjkrr| j j| jƒ | j j	||ƒ | j jtj
ƒ xÜt||d ƒD ]Ê}| j j|ƒ}| j j|ƒ| }|d | jkrÐ| j j|dƒ | j j|| jƒ n*| jtjkrú| j j|dƒ | j j|tjƒ |d | jk�r,| j j|tjƒ | j j|tjƒ q‚| j j|tjƒ | j j|tjƒ q‚W dS )z; Handles an STC request for styling for some area.
        r   é   N)r+   ZGetEndStyledrl   ZGetPositionr%   r=   r/   r@   r9   rv   r:   Úrangerm   ZGetLineEndPositionZStartStylingZ
SetStylingrr   ZSTC_STYLE_DEFAULTr'   ZSTC_INDIC2_MASK)rF   rL   ÚpositionZ
start_lineÚendZend_liner"   Zstyle_lengthrI   rI   rJ   r;   }  s0    

zSourceEditor._style_neededc             C   s   | j jtƒ | j jƒ  dS )zN Handles an error that occurs while setting the object's trait value.
        N)r+   rR   r   rS   )rF   rT   rI   rI   rJ   Úerror«  s    zSourceEditor.errorc                s\   | j jr| jj| jddd� | j jdk	r<| jj| jddd� tj| j	dƒ t
t| ƒjƒ  dS )z0 Disposes of the contents of an editor.
        r   T)ÚremoveNr   )r)   r4   r*   r	   r5   r6   r   r,   r0   r+   Úsuperr   Údispose)rF   )Ú	__class__rI   rJ   r~   µ  s    zSourceEditor.disposec             C   s0   | j jdk	r,|jdƒ}|dk	r,| j jj|ƒ dS )z` Restores any saved user preference information associated with the
            editor.
        Nr6   )r)   r6   ÚgetÚmerge)rF   Zprefsr6   rI   rI   rJ   Úrestore_prefsÊ  s    
zSourceEditor.restore_prefsc             C   s   d| j jiS )zM Returns any user preference information associated with the editor.
        r6   )r)   r6   )rF   rI   rI   rJ   Ú
save_prefs×  s    zSourceEditor.save_prefs)-Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z
scrollabler   r7   r   r    r   r!   r   r   r   r"   r#   r$   r=   r%   r&   rr   r'   r(   rK   r1   r5   rD   r2   r]   rg   r^   rk   rn   rE   r   rs   rt   r	   r_   r;   r{   r~   r‚   rƒ   Ú__classcell__rI   rI   )r   rJ   r   J   sF   `

	.
r   c               @   s   e Zd ZdZdS )ÚReadonlyEditorTN)r„   r…   r†   r7   rI   rI   rI   rJ   r‰   â  s   r‰   )#r‡   Ú
__future__r   r,   Zwx.stcr/   Z
traits.apir   r   r   r   r   r   r	   Ztraits.trait_baser
   Ztraitsui.editors.code_editorr   Z
pyface.apir   Zpyface.util.python_stcr   rH   r   Ú	constantsr   r   rA   rB   rC   r   ZCustomEditorZSimpleEditorZ
TextEditorr‰   rI   rI   rI   rJ   Ú<module>   s&   
$
   