a
    åzeò?  ã                   @   s.  d dl Z d dlT d dlmZmZ d dlmZ d dlmZ dZ	ze j
 e j
 e¡e	¡ZW n eyl   e	ZY n0 e j
 e¡r€eZ	ne j
 e	¡sšede	f ƒ‚e	fdd	„Zdd
d„ZG dd„ dƒZG dd„ dƒZG dd„ deƒZG dd„ dƒZdd„ Zedkr*d dlmZ edddd d dlmZ eeƒ dS )é    N)Ú*)ÚFrameÚ	Scrollbar)ÚidleConf)Ú
zoomheightZIconszcan't find icon directory (%r)c                 C   sÖ   t ƒ }ddl}| tj | | ¡d¡¡}| ¡  g }d }}|D ]Š}tj tj |¡¡d }t	||d}	| 
|	¡ t||	ddd}
|
j||d t||d	}
|
j|d |d |d }|d
kr@|d }d}q@||_dS )z'Utility to display the available icons.r   Nz*.gif)ÚfileÚmasteré   Zraised)ÚimageÚbdZrelief)ÚrowÚcolumn)Útexté
   é   )ZTkÚglobÚosÚpathÚjoinÚescapeÚsortÚsplitextÚbasenameÚ
PhotoImageÚappendÚLabelÚgridÚimages)ZicondirÚrootr   Úlistr   r   r   r   Únamer
   Úlabel© r"   ú"/usr/lib/python3.9/idlelib/tree.pyÚ	listicons%   s&    
r$   c                 C   sR   t j| jdkt j| jdki}|| j r*dnd}|du r<| jn|}| t|d¡ dS )a6  Handle scrollwheel event.

    For wheel up, event.delta = 120*n on Windows, -1*n on darwin,
    where n can be > 1 if one scrolls fast.  Flicking the wheel
    generates up to maybe 20 events with n up to 10 or more 1.
    Macs use wheel down (delta = 1*n) to scroll up, so positive
    delta means to scroll up on both systems.

    X-11 sends Control-Button-4,5 events instead.

    The widget parameter is needed so browser label bindings can pass
    the underlying canvas.

    This function depends on widget.yview to not be overridden by
    a subclass.
    r   é   éûÿÿÿé   NZunitsÚbreak)	Z	EventTypeZ
MouseWheelZdeltaZButtonPressÚnumÚtypeÚwidgetÚyviewZSCROLL)Úeventr+   ZupÚlinesr"   r"   r#   Úwheel_event;   s    ÿr/   c                   @   s¾   e Zd Zdd„ Zdd„ Zdd„ Zd*dd	„Zd+d
d„Zdd„ Zdd„ Z	d,dd„Z
d-dd„Zd.dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd/d"d#„Zd0d$d%„Zd1d&d'„Zd2d(d)„ZdS )3ÚTreeNodec                 C   s:   || _ || _|| _d| _d| _g | _d  | _| _i | _d S )NÚ	collapsedF)	ÚcanvasÚparentÚitemÚstateÚselectedÚchildrenÚxÚyÚ
iconimages)Úselfr2   r3   r4   r"   r"   r#   Ú__init__V   s    zTreeNode.__init__c                 C   s2   | j d d … D ]}| j  |¡ | ¡  qd | _d S ©N)r7   ÚremoveÚdestroyr3   )r;   Úcr"   r"   r#   r?   `   s    
zTreeNode.destroyc                 C   sf   z| j | W S  ty   Y n0 tj |¡\}}|p6d}tj t|| ¡}t| j|d}|| j |< |S )Nz.gif)r   r   )	r:   ÚKeyErrorr   r   r   r   ÚICONDIRr   r2   )r;   r    r   ÚextÚfullnamer
   r"   r"   r#   Úgeticonimagef   s    
zTreeNode.geticonimageNc                 C   s:   | j r
d S |  ¡  d| _ | j | j¡ |  ¡  |  ¡  d S )NT)r6   Údeselectallr2   ÚdeleteÚimage_idÚdrawiconÚdrawtext©r;   r-   r"   r"   r#   Úselectr   s    zTreeNode.selectc                 C   s2   | j s
d S d| _ | j | j¡ |  ¡  |  ¡  d S )NF)r6   r2   rG   rH   rI   rJ   rK   r"   r"   r#   Údeselect{   s    zTreeNode.deselectc                 C   s   | j r| j  ¡  n|  ¡  d S r=   )r3   rF   Údeselecttree©r;   r"   r"   r#   rF   ƒ   s    zTreeNode.deselectallc                 C   s&   | j r|  ¡  | jD ]}| ¡  qd S r=   )r6   rM   r7   rN   )r;   Úchildr"   r"   r#   rN   ‰   s    
zTreeNode.deselecttreec                 C   s*   | j dkr|  ¡  n|  ¡  | j ¡  dS )NÚexpandedr(   )r5   ÚcollapseÚexpandr4   ÚOnDoubleClickrK   r"   r"   r#   Úflip   s
    


zTreeNode.flipc                 C   s2   | j  ¡ sd S | jdkr.d| _|  ¡  |  ¡  d S )NrQ   )r4   Ú_IsExpandabler5   ÚupdateÚviewrK   r"   r"   r#   rS   —   s    

zTreeNode.expandc                 C   s   | j dkrd| _ |  ¡  d S )Nr1   )r5   rW   rK   r"   r"   r#   rR   Ÿ   s    
zTreeNode.collapsec                 C   s®   | j d }|  ¡ j d }|| }| j d¡}| j ¡ }| j |¡}||krV||krVd S | j | jd ¡\}}}	}
||krŽ||krŽ|| | }n|}t|ƒ|
 }| j |¡ d S )Nr   é   r   Úscrollregion)r9   Úlastvisiblechildr2   ZcanvasyZwinfo_heightZ_getintsÚfloatZyview_moveto)r;   ÚtopZbottomZheightZvisible_topZvisible_heightZvisible_bottomÚx0Úy0Úx1Úy1Zfractionr"   r"   r#   rX   ¤   s    

zTreeNode.viewc                 C   s&   | j r| jdkr| j d  ¡ S | S d S )NrQ   éÿÿÿÿ)r7   r5   r[   rO   r"   r"   r#   r[   µ   s    zTreeNode.lastvisiblechildc                 C   s€   | j r| j  ¡  nj| jd }d| jd< | j ¡  | j t¡ |  dd¡ | j t¡\}}}}| jjdd||fd || jd< d S )NZcursorZwatché   r   r   )rZ   )r3   rW   r2   rG   ZALLÚdrawÚbboxÚ	configure)r;   Z	oldcursorr^   r_   r`   ra   r"   r"   r#   rW   »   s    


zTreeNode.updatec                 C   sp  d}|| | _ | _|  ¡  |  ¡  | jdkr4|| S | jsv| j ¡ }|sP|d S |D ] }|  | j	| |¡}| j 
|¡ qT|d }|| }d}	| jD ]¨}|}	| j	j|d |d ||d dd | ||¡}|j ¡ r|jdkræd	}
|j}n
d
}
|j}|  |
¡}| j	j|d |	d |d}| j	 |d|¡ | j	 |ddd„ ¡ q| j	j|d |d |d |	d dd}| j	 |¡ |S )Né   rQ   rY   r   é	   rc   Zgray50)ÚfillZ	minusnodeZplusnode)r
   ú<1>ú
<Double-1>c                 S   s   d S r=   r"   )r8   r"   r"   r#   Ú<lambda>ë   ó    zTreeNode.draw.<locals>.<lambda>r   )r8   r9   rI   rJ   r5   r7   r4   Ú_GetSubListÚ	__class__r2   r   Zcreate_linerd   rV   rR   rS   rE   Úcreate_imageÚtag_bindZ	tag_lower)r;   r8   r9   ZdyÚsublistr4   rP   ZcxÚcyZcylastZiconnameÚcallbackr
   Úidr"   r"   r#   rd   È   sF    


"


þzTreeNode.drawc                 C   s~   | j r | j ¡ p| j ¡ pd}n| j ¡ p,d}|  |¡}| jj| j| jd|d}|| _	| j 
|d| j¡ | j 
|d| j¡ d S )NZ
openfolderZfolderÚnw)Úanchorr
   rj   rk   )r6   r4   ÚGetSelectedIconNameÚGetIconNamerE   r2   rp   r8   r9   rH   rq   rL   rU   )r;   Z	imagenamer
   ru   r"   r"   r#   rI   ò   s    
ÿþ
zTreeNode.drawiconc              
      s¤  ˆ j d d }ˆ jd }ˆ j ¡ }|r€ˆ jj||d|d}ˆ j |dˆ j¡ ˆ j |dˆ j¡ ˆ j 	|¡\}}}}t
|dƒd	 }ˆ j ¡ pŒd
}	z
ˆ j W n tyª   Y n
0 ˆ  ¡  z
ˆ j W n( tyæ   tˆ j|	ddddˆ _Y n0 t ¡ }
ˆ jrˆ j t |
d¡¡ nˆ j t |
d¡¡ ˆ jj||dˆ jd}ˆ j dˆ j¡ ˆ j dˆ j¡ ˆ j d‡ fdd„¡ ˆ j d‡ fdd„¡ ˆ j d‡ fdd„¡ |ˆ _d S )Nrg   r	   r%   rv   )rw   r   rj   rk   éÈ   r   z	<no text>r   r   )r   r   ZpadxZpadyZhiliteZnormal)rw   Zwindowú<MouseWheel>c                    s   t | ˆ jƒS r=   ©r/   r2   ©ÚerO   r"   r#   rl     rm   z#TreeNode.drawtext.<locals>.<lambda>ú
<Button-4>c                    s   t | ˆ jƒS r=   r|   r}   rO   r"   r#   rl      rm   ú
<Button-5>c                    s   t | ˆ jƒS r=   r|   r}   rO   r"   r#   rl   !  rm   )r8   r9   r4   ÚGetLabelTextr2   Zcreate_textrq   rL   rU   re   ÚmaxÚGetTextÚentryÚAttributeErrorÚedit_finishr!   r   r   ZCurrentThemer6   rf   ZGetHighlightZcreate_windowÚbindÚselect_or_editZtext_id)r;   ZtextxZtextyZ	labeltextru   r^   r_   r`   ra   r   Zthemer"   rO   r#   rJ   ÿ   sD    

ÿ


ÿzTreeNode.drawtextc                 C   s*   | j r| j ¡ r|  |¡ n
|  |¡ d S r=   )r6   r4   Ú
IsEditableÚeditrL   rK   r"   r"   r#   rˆ   $  s    zTreeNode.select_or_editc                 C   sr   t | jdddd| _| j d| jd ¡ | j dt¡ | jjdd | j ¡  | j d| j	¡ | j d| j
¡ d S )	Nr   r	   )r   ÚhighlightthicknessÚwidthr   r'   )Zipadxz<Return>z<Escape>)ZEntryr!   r„   ÚinsertZselection_rangeZENDÚpackÚ	focus_setr‡   r†   Úedit_cancelrK   r"   r"   r#   rŠ   *  s    
zTreeNode.editc                 C   s|   z| j }| ` W n ty"   Y d S 0 | ¡ }| ¡  |rR|| j ¡ krR| j |¡ | j ¡ }|| jd< |  ¡  | j	 
¡  d S )Nr   )r„   r…   Úgetr?   r4   rƒ   ÚSetTextr!   rJ   r2   r   )r;   r-   r„   r   r"   r"   r#   r†   3  s    

zTreeNode.edit_finishc                 C   sB   z| j }| ` W n ty"   Y d S 0 | ¡  |  ¡  | j ¡  d S r=   )r„   r…   r?   rJ   r2   r   )r;   r-   r„   r"   r"   r#   r   B  s    zTreeNode.edit_cancel)N)N)N)N)N)N)N)N)N)Ú__name__Ú
__module__Ú__qualname__r<   r?   rE   rL   rM   rF   rN   rU   rS   rR   rX   r[   rW   rd   rI   rJ   rˆ   rŠ   r†   r   r"   r"   r"   r#   r0   T   s(   

	



*%

	
r0   c                   @   st   e Zd ZdZdd„ Zdd„ Zd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S )ÚTreeItemz‚Abstract class representing tree items.

    Methods should typically be overridden, otherwise a default action
    is used.

    c                 C   s   dS )z)Constructor.  Do whatever you need to do.Nr"   rO   r"   r"   r#   r<   V  s    zTreeItem.__init__c                 C   s   dS )zReturn text string to display.Nr"   rO   r"   r"   r#   rƒ   Y  s    zTreeItem.GetTextc                 C   s   dS )z>Return label text string to display in front of text (if any).Nr"   rO   r"   r"   r#   r   \  s    zTreeItem.GetLabelTextNc                 C   s   | j du r|  ¡ | _ | j S )ú%Do not override!  Called by TreeNode.N)Ú
expandableÚIsExpandablerO   r"   r"   r#   rV   a  s    

zTreeItem._IsExpandablec                 C   s   dS )z"Return whether there are subitems.r	   r"   rO   r"   r"   r#   r™   g  s    zTreeItem.IsExpandablec                 C   s"   |   ¡ sg S |  ¡ }|sd| _|S )r—   r   )r™   Ú
GetSubListr˜   )r;   rr   r"   r"   r#   rn   k  s    zTreeItem._GetSubListc                 C   s   dS )z-Return whether the item's text may be edited.Nr"   rO   r"   r"   r#   r‰   t  s    zTreeItem.IsEditablec                 C   s   dS )z+Change the item's text (if it is editable).Nr"   )r;   r   r"   r"   r#   r’   w  s    zTreeItem.SetTextc                 C   s   dS )z-Return name of icon to be displayed normally.Nr"   rO   r"   r"   r#   ry   z  s    zTreeItem.GetIconNamec                 C   s   dS )z2Return name of icon to be displayed when selected.Nr"   rO   r"   r"   r#   rx   }  s    zTreeItem.GetSelectedIconNamec                 C   s   dS )z%Return list of items forming sublist.Nr"   rO   r"   r"   r#   rš   €  s    zTreeItem.GetSubListc                 C   s   dS )z%Called on a double-click on the item.Nr"   rO   r"   r"   r#   rT   ƒ  s    zTreeItem.OnDoubleClick)r“   r”   r•   Ú__doc__r<   rƒ   r   r˜   rV   r™   rn   r‰   r’   ry   rx   rš   rT   r"   r"   r"   r#   r–   M  s   	r–   c                   @   sH   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S )ÚFileTreeItemz4Example TreeItem subclass -- browse the file system.c                 C   s
   || _ d S r=   )r   )r;   r   r"   r"   r#   r<     s    zFileTreeItem.__init__c                 C   s   t j | j¡p| jS r=   ©r   r   r   rO   r"   r"   r#   rƒ     s    zFileTreeItem.GetTextc                 C   s   t j | j¡dkS )NÚ r   rO   r"   r"   r#   r‰   “  s    zFileTreeItem.IsEditablec                 C   sj   t j | j¡}t j ||¡}t j |¡t j | j¡kr:d S zt  | j|¡ || _W n tyd   Y n0 d S r=   )r   r   Údirnamer   ÚrenameÚOSError)r;   r   Únewpathr"   r"   r#   r’   –  s    
zFileTreeItem.SetTextc                 C   s   |   ¡ sdS d S )NZpython)r™   rO   r"   r"   r#   ry   ¡  s    zFileTreeItem.GetIconNamec                 C   s   t j | j¡S r=   )r   r   ÚisdirrO   r"   r"   r#   r™   ¥  s    zFileTreeItem.IsExpandablec                 C   sh   zt  | j¡}W n ty&   g  Y S 0 |jt jjd g }|D ]"}tt j | j|¡ƒ}| |¡ q@|S )N)Úkey)	r   Úlistdirr   r¡   r   Únormcaserœ   r   r   )r;   Únamesrr   r    r4   r"   r"   r#   rš   ¨  s    
zFileTreeItem.GetSubListN)r“   r”   r•   r›   r<   rƒ   r‰   r’   ry   r™   rš   r"   r"   r"   r#   rœ   ‰  s   rœ   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚScrolledCanvasc                 K   sl  d|vrd|d< || _ t|ƒ| _| jjddd | jjddd t| jfi |¤Ž| _| jjdddd t| jdd	| _	| j	jddd
d t| jddd| _
| j
jdddd | j	j| jd< | jj| j	d< | j
j| jd< | jj| j
d< | j d| j¡ | j d| j¡ | j d| j¡ | j d| j¡ | j dt¡ | j dt¡ | j dt¡ | j d| j¡ | j ¡  d S )NZyscrollincrementrY   r   r	   )ZweightZnsew)r   r   ZstickyÚvbar)r    ZnseÚhbarZ
horizontal)r    ZorientZewsZyscrollcommandZcommandZxscrollcommandz<Key-Prior>z
<Key-Next>z<Key-Up>z
<Key-Down>r{   r   r€   z<Alt-Key-2>)r   r   ÚframeZrowconfigureZcolumnconfigureZCanvasr2   r   r   r©   rª   Úsetr,   Zxviewr‡   Úpage_upÚ	page_downÚunit_upÚ	unit_downr/   Úzoom_heightr   )r;   r   Zoptsr"   r"   r#   r<   ¹  s2    
zScrolledCanvas.__init__c                 C   s   | j  dd¡ dS )Nrb   Úpager(   ©r2   Zyview_scrollrK   r"   r"   r#   r­   Ô  s    zScrolledCanvas.page_upc                 C   s   | j  dd¡ dS )Nr	   r²   r(   r³   rK   r"   r"   r#   r®   ×  s    zScrolledCanvas.page_downc                 C   s   | j  dd¡ dS )Nrb   Úunitr(   r³   rK   r"   r"   r#   r¯   Ú  s    zScrolledCanvas.unit_upc                 C   s   | j  dd¡ dS )Nr	   r´   r(   r³   rK   r"   r"   r#   r°   Ý  s    zScrolledCanvas.unit_downc                 C   s   t  | j¡ dS )Nr(   )r   r±   r   rK   r"   r"   r#   r±   à  s    zScrolledCanvas.zoom_heightN)	r“   r”   r•   r<   r­   r®   r¯   r°   r±   r"   r"   r"   r#   r¨   ·  s   r¨   c                 C   s†   t | ƒ}tt|  ¡  d¡dd … ƒ\}}| d|d |d f ¡ t|dddd}|jjdd	td
 t	t
ƒ}t|jd |ƒ}| ¡  d S )Nú+r	   z+%d+%dé2   é¯   Zwhiter   )Úbgr‹   Z	takefocusZboth)rS   ri   Zside)ZToplevelÚmapÚintZgeometryÚsplitr¨   r«   rŽ   ZLEFTrœ   rB   r0   r2   rS   )r3   r]   r8   r9   Zscr4   Znoder"   r"   r#   Ú_tree_widgetå  s     r¼   Ú__main__)Úmainzidlelib.idle_test.test_treer   F)Ú	verbosityÚexit)Úrun)N)r   ÚtkinterZtkinter.ttkr   r   Zidlelib.configr   Zidlelibr   rB   r   r   rŸ   Ú__file__Z_icondirÚ	NameErrorr£   ÚRuntimeErrorr$   r/   r0   r–   rœ   r¨   r¼   r“   Zunittestr¾   Zidlelib.idle_test.htestrÁ   r"   r"   r"   r#   Ú<module>   s4   

 z<..

