B
     S]8                 @   s   d Z ddlmZmZmZmZmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZ ddlZddlZdZeejej ZedZdZd	Zd
Zee e e e e Zdd ZG dd deZedkrddlmZ edddd ddl m!Z! e!e dS )z6
Dialog for building Tkinter accelerator key bindings
    )ToplevelListboxText	StringVarTclError)FrameButtonCheckbuttonEntryLabel	Scrollbar)
messageboxN)ZF1ZF2ZF3ZF4ZF5ZF6ZF7ZF8ZF9ZF10ZF11ZF12z~!@#%^&*()_-+={}[]|;:,.<>/?)TabSpaceZReturn)Z	BackSpaceZDeleteZInsert)ZHomeZEndzPage Upz	Page Downz
Left ArrowzRight ArrowzUp Arrowz
Down Arrowc          $   C   s|   ddddddddd	d
dddddddddddddddddddddd d!d"d#d$#}| | | } d%|krr| tjkrr|  } d&|  S )'z3Translate from keycap symbol to the Tkinter keysym.ZspaceZ
asciitildeZexclamZatZ
numbersignZpercentZasciicircumZ	ampersandZasteriskZ	parenleftZ
parenrightZ
underscoreZminusZplusZequalZ	braceleftZ
bracerightZbracketleftZbracketrightZbarZ	semicolonZcolonZcommaZperiodZlessZgreaterZslashZquestionZPriorZNextZLeftZRightZUpZDownr   )#r   ~!@#%^&*()_-+={}[]|;:,.<>/?zPage Upz	Page Downz
Left ArrowzRight ArrowzUp Arrowz
Down Arrowr   ShiftzKey-)getstringascii_lowercaseupper)key	modifiersmapping r3   #lib/python3.7/idlelib/config_key.pytranslate_key   s     r5   c               @   s   e Zd ZdZdddddZdd Zdd	 Zd
d Zdd Zdd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S )"GetKeysDialogzKey Sequence ErrorF)_htest_utestc      	      C   sF  t | | |   | jdd | jddd | | | | |   | d| j	 || _
|| _|| _d| _t| | _| jd |   g | _x,| jD ]"}t| }|d | j| qW d| _|   |   | d| | d |  d   | |s | d |  d  nd	 f  |sB|   |    d
S )a  
        parent - parent of this dialog
        title - string which is the title of the popup dialog
        action - string, the name of the virtual event these keys will be
                 mapped to
        current_key_sequences - list, a list of all key sequence lists
                 currently mapped to virtual events, for overlap checking
        _htest - bool, change box location when running htest
        _utest - bool, do not wait when running unittest
           )borderwidthF)heightwidthZWM_DELETE_WINDOW z+%d+%d      N)!r   __init__ZwithdrawZ	configureZ	resizabletitleZ	transientZgrab_setZprotocolcancelparentactioncurrent_key_sequencesresultr   
key_stringsetset_modifiers_for_platformmodifier_varsr1   appendadvancedcreate_widgetsZupdate_idletasksZgeometryZwinfo_rootxZwinfo_widthZwinfo_reqwidthZwinfo_rootyZwinfo_heightZwinfo_reqheightZ	deiconifyZwait_window)	selfrC   rA   rD   rE   r7   r8   modifiervariabler3   r3   r4   r@   0   s@    



*zGetKeysDialog.__init__c             O   s   t j|| d S )N)r   	showerror)rN   argskwargsr3   r3   r4   rQ   `   s    zGetKeysDialog.showerrorc          	   C   s0  t | ddd | _}|jdddd t | }|jdd	d
 t|dd| jd| _| jjddddd t|dd| jd| _| jjddddd t |dd| _	| j	jdddddd t
| j	d| j dd}|jdd t
| j	d| jddd}|jddd	d t || _| jjddddd  i | _d}xft| j| jD ]T\}}| j||}t| j| j|||d!d"}	|	jd|ddd# |	| j|< |d7 }q0W t
| jdd$d%}
|
jddd&ddd' t| jd(d)d*d+| _| jjdBt  | jd-| j | jjdd&d&d.d/ t| jd0| jjd1}| jj|jd2 |jddd&d.d/ t| jd3| j d4| _!| j!jddd&d5 t |d6d| _"| j"jdddddd t
| j"dd7| j d8d9 d%}|jdd t#| j"| jd:| _$| j$jd	d; t || _%| j%jddddd  t
| j%dd<d%}|jdddd= t|| j&d>d?| _'| j'jddd@dddA | &  d S )CNr>   Zsunken)r:   relieftopTZboth)sideexpandfillZbottomx)rV   rX   ZOK   )textr<   commandr   r9   )rowcolumnpadxpadyZCancel   Zkeyseq_basic)nameZnsew)r]   r^   stickyr_   r`   zNew keys for 'z' :)r[   w)anchorleftZgroove)justifytextvariablerT   r:   )ZipadxZipadyrX   )r]   r^   rc   r_   r=   )r\   r[   rP   ZonvalueZoffvalue)r]   r^   r_   rc   zSelect the desired modifier keys
above, and the final key from the
list on the right.

Use upper case Symbols when using
the Shift modifier.  (Letters will be
converted automatically.))rg   r[      )r]   r^   
columnspanr_   rc      
   Zsingle)r<   r;   Z
selectmodeendz<ButtonRelease-1>ns)r]   r^   Zrowspanrc   Zvertical)Zorientr\   )Zyscrollcommandz
Clear Keys)r[   r\   )r]   r^   rj   Zkeyseq_advancedzEnter new binding(s) for 'z' :
z2(These bindings will not be checked for validity!))rh   )rX   a  Key bindings are specified using Tkinter keysyms as
in these samples: <Control-f>, <Shift-F2>, <F12>,
<Control-space>, <Meta-less>, <Control-Alt-Shift-X>.
Upper case is used when the Shift modifier is present!

'Emacs style' multi-keystroke bindings are specified as
follows: <Control-x><Control-y>, where the first key
is the 'do-nothing' keybinding.

Multiple separate bindings for one action should be
separated by a space, eg., <Alt-v> <Meta-v>.)r]   r^   rc   z<< Basic Key Binding Entry)r\   r[   Zew)r]   r^   Zstickr_   r`   )rm   )(r   frameZpackr   okZ	button_okZgridrB   Zbutton_cancelframe_keyseq_basicr   rD   rG   frame_controls_basicZmodifier_checkbuttonszipr1   rJ   modifier_labelr,   r	   build_key_stringr   list_keys_finalinsertAVAILABLE_KEYSbindfinal_key_selectedr   yviewconfigrH   clear_key_seqZbutton_clearframe_keyseq_advancedr
   advanced_keysframe_help_advancedtoggle_levelbutton_level)rN   ro   Zframe_buttonsZbasic_titleZ
basic_keysr^   rO   rP   ZlabelZcheckZ
help_basicZscroll_keys_finalZadvanced_titleZhelp_advancedr3   r3   r4   rM   d   s    






zGetKeysDialog.create_widgetsc             C   s4   t jdkrddddg| _ndddg| _ddi| _dS )	aV  Determine list of names of key modifiers for this platform.

        The names are used to build Tk bindings -- it doesn't matter if the
        keyboard has these keys; it matters if Tk understands them.  The
        order is also important: key binding equality depends on it, so
        config-keys.def must use the same ordering.
        darwinr+   ZControlZOptionZCommandZAltZCtrlN)sysplatformr1   rt   )rN   r3   r3   r4   rI      s    
z(GetKeysDialog.set_modifiers_for_platformc             C   s   | j ddrN|   | j jdd | j  | j  | j	  d| _
n0|   | j jdd | j  | j  d| _
dS )	z'Toggle between basic and advanced keys.r[   ZAdvancedz<< Basic Key Binding Entry)r[   TzAdvanced Key Binding Entry >>FN)r   Zcget
startswithr}   r|   r~   Zliftr   r   Z	focus_setrL   rq   rr   )rN   r3   r3   r4   r      s    




zGetKeysDialog.toggle_levelNc             C   s   |    dS )z3Handler for clicking on key in basic settings list.N)ru   )rN   eventr3   r3   r4   rz      s    z GetKeysDialog.final_key_selectedc             C   sN   |    }}| jd}|r0t||}|| | jdd| d dS )z2Create formatted string of modifiers plus the key.re   r'   r   r(   N)get_modifiersrv   r,   r5   rK   rG   rH   join)rN   keylistr1   	final_keyr3   r3   r4   ru      s    

zGetKeysDialog.build_key_stringc             C   s   dd | j D }dd |D S )z9Return ordered list of modifiers that have been selected.c             S   s   g | ]}|  qS r3   )r,   ).0rP   r3   r3   r4   
<listcomp>   s    z/GetKeysDialog.get_modifiers.<locals>.<listcomp>c             S   s   g | ]}|r|qS r3   r3   )r   modr3   r3   r4   r      s    )rJ   )rN   Zmod_listr3   r3   r4   r      s    zGetKeysDialog.get_modifiersc             C   sF   | j dd | j dd x| jD ]}|d q$W | jd dS )z#Clear modifiers and keys selection.r   rm   Zmovetoz0.0r=   N)rv   Zselect_clearr{   rJ   rH   rG   )rN   rP   r3   r3   r4   r}      s
    zGetKeysDialog.clear_key_seqc             C   s\   | j   }|s(| j| j| dd d S | js8| |rH| |rH|| _| 	  | 
  d S )NzNo key specified.)rA   rC   message)rG   r,   striprQ   keyerror_titlerL   keys_okbind_okrF   grab_releasedestroy)rN   r   keysr3   r3   r4   rp     s    
zGetKeysDialog.okc             C   s   d| _ |   |   d S )Nr=   )rF   r   r   )rN   r   r3   r3   r4   rB     s    zGetKeysDialog.cancelc             C   s   | j d}|  }| j}dd | jD }|dsF| j|| dd nt|sh|tt krh| j|| dd nR|d	gkr|tt d
 krd}| j|| |d n"||krd}| j|| |d ndS dS )zValidity check on user's 'basic' keybinding selection.

        Doesn't check the string produced by the advanced dialog because
        'modifiers' isn't set.
        re   c             S   s   g | ]}|D ]}|qqS r3   r3   )r   r   r0   r3   r3   r4   r     s    z)GetKeysDialog.keys_ok.<locals>.<listcomp>r(   zMissing the final Key)rC   r   zNo modifier key(s) specified.)rA   rC   r   r+   )r   r   zBThe shift modifier by itself may not be used with this key symbol.z'This key combination is already in use.TF)	rv   r,   r   r   rE   endswithrQ   FUNCTION_KEYS	MOVE_KEYS)rN   r   r   r1   rA   Zkey_sequencesmsgr3   r3   r4   r     s*    



zGetKeysDialog.keys_okc          
   C   sd   y|  |dd }W n: tk
rN } z| j| j| d| d dS d}~X Y nX | || dS dS )z:Return True if Tcl accepts the new keys else show message.c               S   s   d S )Nr3   r3   r3   r3   r4   <lambda>4      z'GetKeysDialog.bind_ok.<locals>.<lambda>z2The entered key sequence is not accepted.

Error: )rA   rC   r   FNT)ry   r   rQ   r   Zunbind)rN   r   Zbindingerrr3   r3   r4   r   1  s    zGetKeysDialog.bind_ok)N)N)N)__name__
__module____qualname__r   r@   rQ   rM   rI   r   rz   ru   r   r}   rp   rB   r   r   r3   r3   r3   r4   r6   +   s   /f
	

r6   __main__)mainz!idlelib.idle_test.test_config_keyr>   F)	verbosityexit)run)"__doc__Ztkinterr   r   r   r   r   Ztkinter.ttkr   r   r	   r
   r   r   r   r-   r   r   tupler.   ZdigitsZALPHANUM_KEYSZPUNCTUATION_KEYSZWHITESPACE_KEYSZ	EDIT_KEYSr   rx   r5   r6   r   Zunittestr   Zidlelib.idle_test.htestr   r3   r3   r3   r4   <module>   s*      