o
    ÈQEcò?  ã                   @   s0  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y6   e	ZY nw e j
 e¡r@eZ	ne j
 e	¡sMe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 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 ]E}tj tj |¡¡d }t	||d}	| 
|	¡ t||	ddd}
|
j||d t||d	}
|
j|d |d |d }|d
kre|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"   úLib\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ButtonPressZ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d*d$d%„Zd*d&d'„Zd*d(d)„ZdS )+ÚTreeNodec                 C   s:   || _ || _|| _d| _d| _g | _d  | _| _i | _d S )NÚ	collapsedF)	ÚcanvasÚparentÚitemÚstateÚselectedÚchildrenÚxÚyÚ
iconimages)Úselfr1   r2   r3   r"   r"   r#   Ú__init__V   s   
zTreeNode.__init__c                 C   s2   | j d d … D ]}| j  |¡ | ¡  qd | _d S ©N)r6   ÚremoveÚdestroyr2   )r:   Úcr"   r"   r#   r>   `   s   

zTreeNode.destroyc                 C   sf   z| j | W S  ty   Y nw tj |¡\}}|pd}tj t|| ¡}t| j|d}|| j |< |S )Nz.gif)r   r   )	r9   ÚKeyErrorr   r   r   r   ÚICONDIRr   r1   )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)r5   Údeselectallr1   Ú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)r5   r1   rF   rG   rH   rI   rJ   r"   r"   r#   Údeselect{   s   zTreeNode.deselectc                 C   s    | j r
| j  ¡  d S |  ¡  d S r<   )r2   rE   Údeselecttree©r:   r"   r"   r#   rE   ƒ   s   zTreeNode.deselectallc                 C   s&   | j r|  ¡  | jD ]}| ¡  q
d S r<   )r5   rL   r6   rM   )r:   Úchildr"   r"   r#   rM   ‰   s
   

ÿzTreeNode.deselecttreec                 C   s*   | j dkr
|  ¡  n|  ¡  | j ¡  dS )NÚexpandedr(   )r4   ÚcollapseÚexpandr3   ÚOnDoubleClickrJ   r"   r"   r#   Úflip   s
   


zTreeNode.flipc                 C   s6   | j  ¡ sd S | jdkrd| _|  ¡  |  ¡  d S d S )NrP   )r3   Ú_IsExpandabler4   ÚupdateÚviewrJ   r"   r"   r#   rR   —   s   

ýzTreeNode.expandc                 C   s    | j dkrd| _ |  ¡  d S d S )Nr0   )r4   rV   rJ   r"   r"   r#   rQ   Ÿ   s   
þzTreeNode.collapsec                 C   s®   | j d }|  ¡ j d }|| }| j d¡}| j ¡ }| j |¡}||kr+||kr+d S | j | jd ¡\}}}	}
||krG||krG|| | }n|}t|ƒ|
 }| j |¡ d S )Nr   é   r   Úscrollregion)r8   Úlastvisiblechildr1   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#   rW   ¤   s   

zTreeNode.viewc                 C   s"   | j r| jdkr| j d  ¡ S | S )NrP   éÿÿÿÿ)r6   r4   rZ   rN   r"   r"   r#   rZ   µ   s   zTreeNode.lastvisiblechildc                 C   s‚   | j r
| j  ¡  d S | 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   )rY   )r2   rV   r1   rF   ZALLÚdrawÚbboxÚ	configure)r:   Z	oldcursorr]   r^   r_   r`   r"   r"   r#   rV   »   s   


zTreeNode.updatec                 C   sp  d}||| _ | _|  ¡  |  ¡  | jdkr|| S | js;| j ¡ }|s(|d S |D ]}|  | j	| |¡}| j 
|¡ q*|d }|| }d}	| jD ]T}|}	| j	j|d |d ||d dd | ||¡}|j ¡ rœ|jdkrsd	}
|j}nd
}
|j}|  |
¡}| j	j|d |	d |d}| j	 |d|¡ | j	 |ddd„ ¡ qH| j	j|d |d |d |	d dd}| j	 |¡ |S )Né   rP   rX   r   é	   rb   Zgray50)ÚfillZ	minusnodeZplusnode)r
   ú<1>ú
<Double-1>c                 S   s   d S r<   r"   )r7   r"   r"   r#   Ú<lambda>ë   ó    zTreeNode.draw.<locals>.<lambda>r   )r7   r8   rH   rI   r4   r6   r3   Ú_GetSubListÚ	__class__r1   r   Zcreate_linerc   rU   rQ   rR   rD   Úcreate_imageÚtag_bindZ	tag_lower)r:   r7   r8   ZdyÚsublistr3   rO   ZcxÚcyZcylastZiconnameÚcallbackr
   Úidr"   r"   r#   rc   È   sH   


"


€þ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
   ri   rj   )r5   r3   ÚGetSelectedIconNameÚGetIconNamerD   r1   ro   r7   r8   rG   rp   rK   rT   )r:   Z	imagenamer
   rt   r"   r"   r#   rH   ò   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 ¡ pFd
}	zˆ j W n	 tyU   Y nw ˆ  ¡  zˆ j W n tys   tˆ j|	ddddˆ _Y nw 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 )Nrf   r	   r%   ru   )rv   r   ri   rj   éÈ   r   z	<no text>r   r   )r   r   ZpadxZpadyZhiliteZnormal)rv   Zwindowú<MouseWheel>c                    ó   t | ˆ jƒS r<   ©r.   r1   ©ÚerN   r"   r#   rk     ó    z#TreeNode.drawtext.<locals>.<lambda>ú
<Button-4>c                    r{   r<   r|   r}   rN   r"   r#   rk      r   ú
<Button-5>c                    r{   r<   r|   r}   rN   r"   r#   rk   !  r   )r7   r8   r3   ÚGetLabelTextr1   Zcreate_textrp   rK   rT   rd   ÚmaxÚGetTextÚentryÚAttributeErrorÚedit_finishr!   r   r   ZCurrentThemer5   re   ZGetHighlightZcreate_windowÚbindÚselect_or_editZtext_id)r:   ZtextxZtextyZ	labeltextrt   r]   r^   r_   r`   r   Zthemer"   rN   r#   rI   ÿ   sH   

ÿ
ÿ
þ
ÿ
zTreeNode.drawtextc                 C   s,   | j r| j ¡ r|  |¡ d S |  |¡ d S r<   )r5   r3   Ú
IsEditableÚeditrK   rJ   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_cancelrJ   r"   r"   r#   r‹   *  s   
zTreeNode.editc                 C   s|   z| j }| ` W n
 ty   Y d S w | ¡ }| ¡  |r)|| j ¡ kr)| j |¡ | j ¡ }|| jd< |  ¡  | j	 
¡  d S )Nr   )r…   r†   Úgetr>   r3   r„   ÚSetTextr!   rI   r1   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 w | ¡  |  ¡  | j ¡  d S r<   )r…   r†   r>   rI   r1   r   )r:   r,   r…   r"   r"   r#   r‘   B  s   ÿzTreeNode.edit_cancelr<   )Ú__name__Ú
__module__Ú__qualname__r;   r>   rD   rK   rL   rE   rM   rT   rR   rQ   rW   rZ   rV   rc   rH   rI   r‰   r‹   r‡   r‘   r"   r"   r"   r#   r/   T   s*    


	


*
%

	r/   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   ó   dS )z)Constructor.  Do whatever you need to do.Nr"   rN   r"   r"   r#   r;   V  rl   zTreeItem.__init__c                 C   r˜   )zReturn text string to display.Nr"   rN   r"   r"   r#   r„   Y  rl   zTreeItem.GetTextc                 C   r˜   )z>Return label text string to display in front of text (if any).Nr"   rN   r"   r"   r#   r‚   \  rl   zTreeItem.GetLabelTextNc                 C   s   | j du r
|  ¡ | _ | j S )ú%Do not override!  Called by TreeNode.N)Ú
expandableÚIsExpandablerN   r"   r"   r#   rU   a  s   

zTreeItem._IsExpandablec                 C   r˜   )z"Return whether there are subitems.r	   r"   rN   r"   r"   r#   r›   g  s   zTreeItem.IsExpandablec                 C   s"   |   ¡ sg S |  ¡ }|sd| _|S )r™   r   )r›   Ú
GetSubListrš   )r:   rq   r"   r"   r#   rm   k  s   zTreeItem._GetSubListc                 C   r˜   )z-Return whether the item's text may be edited.Nr"   rN   r"   r"   r#   rŠ   t  rl   zTreeItem.IsEditablec                 C   r˜   )z+Change the item's text (if it is editable).Nr"   )r:   r   r"   r"   r#   r“   w  rl   zTreeItem.SetTextc                 C   r˜   )z-Return name of icon to be displayed normally.Nr"   rN   r"   r"   r#   rx   z  rl   zTreeItem.GetIconNamec                 C   r˜   )z2Return name of icon to be displayed when selected.Nr"   rN   r"   r"   r#   rw   }  rl   zTreeItem.GetSelectedIconNamec                 C   r˜   )z%Return list of items forming sublist.Nr"   rN   r"   r"   r#   rœ   €  rl   zTreeItem.GetSubListc                 C   r˜   )z%Called on a double-click on the item.Nr"   rN   r"   r"   r#   rS   ƒ  rl   zTreeItem.OnDoubleClick)r”   r•   r–   Ú__doc__r;   r„   r‚   rš   rU   r›   rm   rŠ   r“   rx   rw   rœ   rS   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   rN   r"   r"   r#   r„     s   zFileTreeItem.GetTextc                 C   s   t j | j¡dkS )NÚ rŸ   rN   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 d S  ty4   Y d S w 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›   rN   r"   r"   r#   rx   ¡  s   ÿzFileTreeItem.GetIconNamec                 C   s   t j | j¡S r<   )r   r   ÚisdirrN   r"   r"   r#   r›   ¥  s   zFileTreeItem.IsExpandablec                 C   sh   zt  | j¡}W n ty   g  Y S w |jt jjd g }|D ]}tt j | j|¡ƒ}| |¡ q |S )N)Úkey)	r   Úlistdirr   r£   r   Únormcaserž   r   r   )r:   Únamesrq   r    r3   r"   r"   r#   rœ   ¨  s   ÿzFileTreeItem.GetSubListN)r”   r•   r–   r   r;   r„   rŠ   r“   rx   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yscrollincrementrX   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>rz   r€   r   z<Alt-Key-2>)r   r   ÚframeZrowconfigureZcolumnconfigureZCanvasr1   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   ó   | j  dd¡ dS )Nra   Úpager(   ©r1   Zyview_scrollrJ   r"   r"   r#   r¯   Ô  ó   zScrolledCanvas.page_upc                 C   r´   )Nr	   rµ   r(   r¶   rJ   r"   r"   r#   r°   ×  r·   zScrolledCanvas.page_downc                 C   r´   )Nra   Úunitr(   r¶   rJ   r"   r"   r#   r±   Ú  r·   zScrolledCanvas.unit_upc                 C   r´   )Nr	   r¸   r(   r¶   rJ   r"   r"   r#   r²   Ý  r·   zScrolledCanvas.unit_downc                 C   s   t  | j¡ dS )Nr(   )r   r³   r   rJ   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)rR   rh   Zside)ZToplevelÚmapÚintZgeometryÚsplitrª   r­   r   ZLEFTrž   rA   r/   r1   rR   )r2   r\   r7   r8   Zscr3   Znoder"   r"   r#   Ú_tree_widgetå  s    rÀ   Ú__main__)Úmainzidlelib.idle_test.test_treer   F)Ú	verbosityÚexit)Úrunr<   )r   ZtkinterZtkinter.ttkr   r   Zidlelib.configr   Zidlelibr   rA   r   r   r¡   Ú__file__Z_icondirÚ	NameErrorr¥   ÚRuntimeErrorr$   r.   r/   r—   rž   rª   rÀ   r”   ZunittestrÂ   Zidlelib.idle_test.htestrÅ   r"   r"   r"   r#   Ú<module>   s:   ÿ
 z<..
û