a
    ze5                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ dd Zdd Z	G dd	 d	Z
G d
d deZG dd de
Zdd Zedkrd dlmZ edddd d dlmZ ee dS )    N)idleConf)	Delegatorc                 C   s   t t| dS )Nzend-1c)intfloatindex)text r   %/usr/lib/python3.9/idlelib/sidebar.pyget_end_linenumber   s    r
   c                 C   s   |   }|dkr|  }n |dkr,|  }ntd| tt| jj|d | d| dg}tt| jj|d | d| dg}||fS )NpackgridzUnsupported geometry manager: padxZborderpady)	Zwinfo_managerZ	pack_infoZ	grid_info
ValueErrorsummaptkZgetintZcget)Zwidgetmanagerinfor   r   r   r   r	   get_widget_padding   s"    



r   c                   @   sd   e 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S )BaseSideBarc              	   C   sx   || _ |j| _|j| _t| j\}}tj| jdtjd|ddd| _| jj	tj
d | j| jd< |   |   d| _d S )N      r   )widthwrapr   r   ZborderwidthZhighlightthicknessstateZyscrollcommandF)editwin
text_frameparentr   r   r   TextNONEsidebar_textconfigDISABLEDredirect_yscroll_eventupdate_fontupdate_colorsis_shown)selfr   Z_padxr   r   r   r	   __init__/   s    zBaseSideBar.__init__c                 C   s   t | jdd}| | d S )NmainEditorWindow)r   GetFontr   _update_fontr)   fontr   r   r	   r&   ?   s    zBaseSideBar.update_fontc                 C   s   || j d< d S )Nr0   )r"   r/   r   r   r	   r.   D   s    zBaseSideBar._update_fontc                 C   s*   t t  d}| j|d |d d d S )NZnormal
foreground
backgroundr1   r2   r   ZGetHighlightZCurrentTheme_update_colorsr)   Zcolorsr   r   r	   r'   G   s    
zBaseSideBar.update_colorsc                 C   s   | j j|||||d d S )N)ZfgbgZselectforegroundZselectbackgroundZinactiveselectbackground)r"   r#   )r)   r1   r2   r   r   r	   r5   M   s
    zBaseSideBar._update_colorsc                 C   s$   | j s | jjddtjd d| _ d S )Nr   r   rowcolumnZstickyT)r(   r"   r   r   NSEWr)   r   r   r	   show_sidebarT   s    zBaseSideBar.show_sidebarc                 C   s   | j r| j  d| _ d S )NF)r(   r"   Zgrid_forgetr<   r   r   r	   hide_sidebarY   s    
zBaseSideBar.hide_sidebarc                 O   s"   | j jj|  | j|d  dS )Nr   break)r   vbarsetr"   yview_moveto)r)   argskwargsr   r   r	   r%   ^   s    z"BaseSideBar.redirect_yscroll_eventc                 C   s   | j   dS )Nr?   )r   	focus_setr)   eventr   r   r	   redirect_focusin_eventg   s    
z"BaseSideBar.redirect_focusin_eventc                 C   s"   | j   | j j|d|jd dS )Nr   )xyr?   )r   rE   event_generaterJ   )r)   rG   
event_namer   r   r	   redirect_mousebutton_eventl   s    
z&BaseSideBar.redirect_mousebutton_eventc                 C   s   | j jdd|j|jd dS )N<MouseWheel>r   )rI   rJ   deltar?   )r   rK   rJ   rO   rF   r   r   r	   redirect_mousewheel_eventr   s    
z%BaseSideBar.redirect_mousewheel_eventN)__name__
__module____qualname__r*   r&   r.   r'   r5   r=   r>   r%   rH   rM   rP   r   r   r   r	   r   +   s   	r   c                   @   s(   e Zd Zdd ZdddZd	ddZdS )
EndLineDelegatorc                 C   s   t |  || _d S N)r   r*   changed_callback)r)   rV   r   r   r	   r*   |   s    
zEndLineDelegator.__init__Nc                 C   s$   | j ||| | t| j  d S rU   )delegateinsertrV   r
   )r)   r   charstagsr   r   r	   rX      s    zEndLineDelegator.insertc                 C   s"   | j || | t| j  d S rU   )rW   deleterV   r
   )r)   Zindex1index2r   r   r	   r[      s    zEndLineDelegator.delete)N)N)rQ   rR   rS   r*   rX   r[   r   r   r   r	   rT   y   s   	
rT   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
LineNumbersc                 C   s   t | | d| _t| jd | _| jjtjd | j	ddd | jjtj
d | jjddd | jjdtjd	 |   t| j}| | t| j}|| jjj | jj| | jjj}||ur|  |j}qd| _d S )
Nr   r   r   rX   1
linenumberF)Z	takefocusZexportselection)Zjustify)r   r*   prev_endtyper"   _sidebar_width_typer#   r   NORMALrX   r$   Z
tag_configZRIGHTbind_eventsr
   r   update_sidebar_textrT   Zsetdelegater   ZundorW   ZpertopZ
resetcacher(   )r)   r   endZend_line_delegatorZ	delegatorr   r   r	   r*      s&    



zLineNumbers.__init__c                    s4  j dj j dj fdd}dD ]j}d| dd| dd	| d
fD ]}|||d qVd| dd| dfD ]}||d| dd qq0d d  fdd}j d| fdd}j d| fdd  fdd}j d|  fdd}j d| d S )Nz	<FocusIn>rN   c                    s"   t j j|d} j| | d S )N)rL   )	functoolspartialrM   r"   bind)rL   target_event_namehandlerr<   r   r	   bind_mouse_event   s    z1LineNumbers.bind_events.<locals>.bind_mouse_event)r            z<Button->z<ButtonRelease-z<Bz-Motion>)rk   z<Double-Button-z<Triple-Button-c                    sl   t t jd| j } jddd  jd| d|d  d  jd|d  d |d S Nz@0,Zsel1.0rg   z.0r   rX   )	r   r   r"   r   rJ   r   
tag_removetag_addmark_set)rG   linenor)   
start_liner   r	   b1_mousedown_handler   s
     z5LineNumbers.bind_events.<locals>.b1_mousedown_handlerz
<Button-1>c                    s   d d  d d S rU   r   )rG   )last_y
last_yviewry   r   r	   b1_mouseup_handler   s    z3LineNumbers.bind_events.<locals>.b1_mouseup_handlerz<ButtonRelease-1>c                    s   t t jd|  }t|g\}} jddd  jd| d|d  d  jd||krn|n|d  d d S rr   )	r   r   r"   r   sortedr   rt   ru   rv   )Zy_coordrw   abrx   r   r	   %drag_update_selection_and_insert_mark   s     zFLineNumbers.bind_events.<locals>.drag_update_selection_and_insert_markc                    sJ   | j j d  kr*j ks<n jd   | j  d S Nr   )rJ   r"   yviewZwinfo_heightr   rB   )rG   rC   r   r{   r|   r)   r   r	   b1_drag_handler   s
    
z0LineNumbers.bind_events.<locals>.b1_drag_handlerz<B1-Motion>c                    s:   d u rd S j  }|kr6j|d    d S r   )r"   r   r   rB   )rG   r   r   r   r	   selection_handler  s    
z2LineNumbers.bind_events.<locals>.selection_handlerz<<Selection>>)r"   rj   rH   rP   )r)   rm   ZbuttonrL   rz   r}   r   r   r   )r   r{   r|   r)   ry   r	   rd      s4    







	zLineNumbers.bind_eventsc                 C   s*   t t  d}| j|d |d d d S )Nr_   r1   r2   r3   r4   r6   r   r   r	   r'     s    
zLineNumbers.update_colorsc                 C   s   || j krd S tt|tt| j  }|rVtt| jd }|| }| || jd< | jjtj	d || j krd
tdgttt| j d |d }| jd|d n| j|d  dd | jjtjd || _ d S )	Nr   r   
 r   zend -1cr_   z.0 -1c)r`   lenstrr   r   r"   rb   r#   r   rc   join	itertoolschainr   rangerX   r[   r$   )r)   rg   Zwidth_differenceZ	cur_widthZ	new_widthZnew_textr   r   r	   re     s"    

zLineNumbers.update_sidebar_textN)rQ   rR   rS   r*   rd   r'   re   r   r   r   r	   r]      s   jr]   c              	   C   s   ddl m} t| }t|}|jtjtjdd |jddd |j	ddd t
|dd}tj|d	d
tj|d}|jddtjd ||}t||_t|}|  |dddd tddD  d S )Nr   )Dummy_editwinT)Zsidefillexpandr   )Zweightr+   r,   P      )r   Zheightr   r0   r8   rs   r   c                 s   s   | ]}d | V  qdS )r   Nr   ).0ir   r   r	   	<genexpr>M      z._linenumbers_drag_scrolling.<locals>.<genexpr>e   )Zidlelib.idle_test.test_sidebarr   r   ZToplevelZFramer   ZLEFTZBOTHZrowconfigureZcolumnconfigurer   r-   r    r!   r   r;   Z	Scrollbarr@   r]   r=   rX   r   r   )r   r   Ztoplevelr   r0   r   r   Zlinenumbersr   r   r	   _linenumbers_drag_scrolling:  s    

r   __main__)r+   zidlelib.idle_test.test_sidebarr   F)	verbosityexit)run)rh   r   tkinterr   Zidlelib.configr   Zidlelib.delegatorr   r
   r   r   rT   r]   r   rQ   Zunittestr+   Zidlelib.idle_test.htestr   r   r   r   r	   <module>   s    N -