a
    äIþfö  ã                   @   s’   d Z ddlmZmZmZmZ ddlmZ dZdZ	dZ
dZdZd	ZG d
d„ deƒ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 )zvA call-tip window class for Tkinter/IDLE.

After tooltip.py, which uses ideas gleaned from PySol.
Used by calltip.py.
é    )ÚLabelÚLEFTÚSOLIDÚTclError)ÚTooltipBasez<<calltipwindow-hide>>)z<Key-Escape>z
<FocusOut>z<<calltipwindow-checkhide>>)z<KeyRelease>z<ButtonRelease>éd   Zcalltipwindowregion_rightc                       sv   e Zd ZdZ‡ fdd„Zdd„ Z‡ fdd„Z‡ fdd	„Zd
d„ Zddd„Z	dd„ Z
‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚCalltipWindowz+A call-tip widget for tkinter text widgets.c                    sD   t t| ƒ |¡ d | _| _d | _ | _| _d | _| _	d| _
dS )z}Create a call-tip; shown by showtip().

        text_widget: a Text widget with code for which call-tips are desired
        N)Úsuperr   Ú__init__ÚlabelÚtextÚ	parenlineÚparencolÚlastlineÚhideidÚcheckhideidÚcheckhide_after_id)ÚselfZtext_widget©Ú	__class__© ú"lib/python3.9/idlelib/calltip_w.pyr
      s
    zCalltipWindow.__init__c                 C   sŠ   t | j d¡ d¡d ƒ}|| jkr2| j| jf}n|df}| j d| ¡}|snt| j d¡ƒ}d|d< d|d< |d d |d |d  fS )z$Choose the position of the call-tip.ÚinsertÚ.r   z%d.%dé   é   é   )ÚintÚanchor_widgetÚindexÚsplitr   r   ZbboxÚlist)r   ÚcurlineZanchor_indexZboxr   r   r   Úget_position#   s    
zCalltipWindow.get_positionc                    sL   t | j d¡ d¡d ƒ}|| jkr(dS || _| j d¡ tt| ƒ ¡  dS )z Reposition the window if needed.r   r   r   N)	r   r   r   r    r   Zseer	   r   Úposition_window)r   r"   r   r   r   r$   2   s    
zCalltipWindow.position_windowc                    s^   || _ | js| j sdS | j t|¡ tt| j |¡ d¡ƒ\| _	| _
tt| ƒ ¡  |  ¡  dS )a_  Show the call-tip, bind events which will close it and reposition it.

        text: the text to display in the call-tip
        parenleft: index of the opening parenthesis in the text widget
        parenright: index of the closing parenthesis in the text widget,
                    or the end of the line if there is no closing parenthesis
        Nr   )r   Ú	tipwindowr   Zmark_setÚ
MARK_RIGHTÚmapr   r   r    r   r   r	   r   ÚshowtipÚ_bind_events)r   r   Z	parenleftZ
parenrightr   r   r   r(   ;   s    	ÿzCalltipWindow.showtipc              
   C   s2   t | j| jtddtd| jd d| _| j ¡  dS )zCreate the call-tip widget.z#ffffd0Zblackr   Úfont)r   ZjustifyZ
backgroundZ
foregroundZreliefZborderwidthr*   N)r   r%   r   r   r   r   r   Úpack©r   r   r   r   ÚshowcontentsP   s    ýzCalltipWindow.showcontentsNc                 C   s–   | j s
dS tt| j d¡ d¡ƒ\}}|| jk sT|| jkrD|| jksT| j ddt	¡r`|  
¡  dS |  ¡  | jdur€| j | j¡ | j t| j¡| _dS )z4Handle CHECK_HIDE_EVENT: call hidetip or reschedule.Nr   r   ú>Úbreak)r%   r'   r   r   r   r    r   r   Zcomparer&   Úhidetipr$   r   Zafter_cancelÚafterÚCHECKHIDE_TIMEÚcheckhide_event)r   Úeventr"   Zcurcolr   r   r   r3   X   s$    
ÿÿþ
ÿzCalltipWindow.checkhide_eventc                 C   s   | j s
dS |  ¡  dS )z%Handle HIDE_EVENT by calling hidetip.Nr/   )r%   r0   )r   r4   r   r   r   Ú
hide_events   s    zCalltipWindow.hide_eventc              	      sž   | j s
dS z| j ¡  W n ty*   Y n0 d| _d | _ | _| _z| j t	¡ W n tyf   Y n0 z|  
¡  W n ttfyŠ   Y n0 tt| ƒ ¡  dS )zHide the call-tip.N)r%   r   Zdestroyr   r   r   r   r   Z
mark_unsetr&   Ú_unbind_eventsÚ
ValueErrorr	   r   r0   r,   r   r   r   r0   {   s"    zCalltipWindow.hidetipc                 C   sh   | j  t| j¡| _tD ]}| j  t|¡ q| j  t| j¡ | j  t	| j
¡| _tD ]}| j  t	|¡ qPdS )zBind event handlers.N)r   ÚbindÚCHECKHIDE_EVENTr3   r   ÚCHECKHIDE_SEQUENCESÚ	event_addr1   r2   Ú
HIDE_EVENTr5   r   ÚHIDE_SEQUENCES©r   Úseqr   r   r   r)   ”   s    ÿÿzCalltipWindow._bind_eventsc                 C   s`   t D ]}| j t|¡ q| j t| j¡ d| _tD ]}| j t|¡ q2| j t| j¡ d| _dS )zUnbind event handlers.N)	r:   r   Zevent_deleter9   Zunbindr   r=   r<   r   r>   r   r   r   r6       s    zCalltipWindow._unbind_events)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r#   r$   r(   r-   r3   r5   r0   r)   r6   Ú__classcell__r   r   r   r   r      s   	
r   c                    sì   ddl m}m}m}m} || ƒ}| d¡ tt|  ¡  	d¡dd … ƒ\}}| d|d |d f ¡ ||ƒ}|j
||dd	 | d
d¡ | ¡  t|ƒ‰ ‡ fdd„}	‡ fdd„}
| dd¡ | dd¡ | d|	¡ | d|
¡ | ¡  d S )Nr   )ÚToplevelÚTextr   ÚBOTHzTest call-tipsú+r   z250x100+%d+%dé¯   é–   )ZsideZfillÚexpandr   zstring.splitc                    s   ˆ   ddd¡ d S )Nz(s='Hello world')r   Úend)r(   ©r4   ©Zcalltipr   r   Úcalltip_show¹   s    z%_calltip_window.<locals>.calltip_showc                    s   ˆ   ¡  d S )N)r0   rM   rN   r   r   Úcalltip_hide»   s    z%_calltip_window.<locals>.calltip_hidez<<calltip-show>>ú(z<<calltip-hide>>ú))ÚtkinterrE   rF   r   rG   Útitler'   r   Zgeometryr    r+   r   Úupdater   r;   r8   Z	focus_set)ÚparentrE   rF   r   rG   ÚtopÚxÚyr   rO   rP   r   rN   r   Ú_calltip_window¬   s"    
 rZ   Ú__main__)Úmainz idlelib.idle_test.test_calltip_wr   F)Ú	verbosityÚexit)ÚrunN)rC   rS   r   r   r   r   Zidlelib.tooltipr   r<   r=   r9   r:   r2   r&   r   rZ   r@   Zunittestr\   Zidlelib.idle_test.htestr_   r   r   r   r   Ú<module>   s     