B
    Ç S]ö  ã               @   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.7/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
parenright)r   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k	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   r1   X   s    

zCalltipWindow.checkhide_eventc             C   s   | j s
dS |  ¡  dS )z%Handle HIDE_EVENT by calling hidetip.Nr-   )r$   r.   )r   r2   r   r   r   Ú
hide_events   s    zCalltipWindow.hide_eventc          	      s¤   | j s
dS y| j ¡  W n tk
r,   Y nX d| _d | _ | _| _y| j t	¡ W n tk
rj   Y nX y|  
¡  W n ttfk
r   Y nX 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   r.   )r   )r   r   r   r.   {   s"    zCalltipWindow.hidetipc             C   sp   | j  t| j¡| _xtD ]}| j  t|¡ qW | j  t| j¡ | j  t	| j
¡| _xtD ]}| j  t	|¡ qVW dS )zBind event handlers.N)r   ÚbindÚCHECKHIDE_EVENTr1   r   ÚCHECKHIDE_SEQUENCESÚ	event_addr/   r0   Ú
HIDE_EVENTr3   r   ÚHIDE_SEQUENCES)r   Úseqr   r   r   r(   ”   s    



zCalltipWindow._bind_eventsc             C   sh   xt D ]}| j t|¡ qW | j t| j¡ d| _xtD ]}| j t|¡ q8W | j t| j¡ d| _dS )zUnbind event handlers.N)	r8   r   Zevent_deleter7   Zunbindr   r;   r:   r   )r   r<   r   r   r   r4       s    

zCalltipWindow._unbind_events)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r"   r#   r'   r+   r1   r3   r.   r(   r4   Ú__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'   )r2   )Úcalltipr   r   Úcalltip_show¹   s    z%_calltip_window.<locals>.calltip_showc                s   ˆ   ¡  d S )N)r.   )r2   )rJ   r   r   Úcalltip_hide»   s    z%_calltip_window.<locals>.calltip_hidez<<calltip-show>>ú(z<<calltip-hide>>ú))ÚtkinterrB   rC   r   rD   Útitler&   r   Zgeometryr   r*   r   Úupdater   r9   r6   Z	focus_set)ÚparentrB   rC   r   rD   ÚtopÚxÚyr   rK   rL   r   )rJ   r   Ú_calltip_window¬   s"    
 rV   Ú__main__)Úmainz idlelib.idle_test.test_calltip_wr   F)Ú	verbosityÚexit)ÚrunN)r@   rO   r   r   r   r   Zidlelib.tooltipr   r:   r;   r7   r8   r0   r%   r   rV   r=   ZunittestrX   Zidlelib.idle_test.htestr[   r   r   r   r   Ú<module>   s     