o
   ٌM                   @   s   d Z ddl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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r{ddlm Z  e dddd ddl!m"Z" e"e dS dS )z6
Dialog for building Tkinter accelerator key bindings
    )ToplevelListbox	StringVarTclError)FrameButtonCheckbuttonEntryLabel	Scrollbar)
messagebox)_setup_dialogN)ZF1ZF2ZF3ZF4ZF5ZF6ZF7ZF8ZF9ZF10ZF11ZF12z~!@#%^&*()_-+={}[]|;:,.<>/?)TabSpaceZReturn)Z	BackSpaceZDeleteZInsert)ZHomeZEndPage Up	Page Down
Left ArrowRight ArrowUp Arrow
Down Arrowc                 C   s  i 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"i d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdEi}| | | } dF|v r| tjv r|  } dG|  S )Hz3Translate from keycap symbol to the Tkinter keysym.r   space~Z
asciitilde!Zexclam@Zat#Z
numbersign%percent^Zasciicircum&Z	ampersand*Zasterisk(Z	parenleft)Z
parenright_Z
underscore-Zminus+Zplus=Zequal{Z	braceleft}Z
braceright[Zbracketleft]Zbracketright|Zbar;Z	semicolon:colon,Zcomma.Zperiod<Zless>Zgreater/Zslash?Zquestionr   ZPriorr   ZNextr   ZLeftr   ZRightr   ZUpr   ZDownr   ShiftzKey-)getstringascii_lowercaseupper)key	modifiersmapping r<   8/app/.heroku/python/lib/python3.10/idlelib/config_key.pytranslate_key   s   		


r>   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   sJ  t | | |   | jdd | jddd | | | | t|  |   | 	d| j
 || _|| _|| _d| _t| | _| jd |   g | _| jD ]}t| }|d | j| qQd| _|   |   | d| | d |  d   | |s| d |  d  nd	 f  |s|    | !  d
S 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	transientr   Zgrab_setprotocolcancelparentactioncurrent_key_sequencesresultr   
key_stringsetset_modifiers_for_platformmodifier_varsr:   appendadvancedcreate_widgetsZupdate_idletasksZgeometryZwinfo_rootxZwinfo_widthZwinfo_reqwidthZwinfo_rootyZwinfo_heightZwinfo_reqheightZ	deiconifyZwait_window)	selfrM   rJ   rN   rO   r@   rA   modifiervariabler<   r<   r=   rI   1   sP   




zGetKeysDialog.__init__c                 O   s   t j|i | d S N)r   	showerror)rX   argskwargsr<   r<   r=   r\   b   s   zGetKeysDialog.showerrorc              	   C   s.  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}t| j| jD ])\}}| j||}t| j| j|||d!d"}	|	jd|ddd# |	| j|< |d7 }qt
| jdd$d%}
|
jddd&ddd' t| jd(d)d*d+| _| jjd,gtR   | 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 )BNrG   Zsunken)rC   relieftopTZboth)sideexpandfillZbottomx)ra   rc   ZOK   )textrE   commandr   rB   )rowcolumnpadxpadyZCancel   Zkeyseq_basic)nameZnsew)rh   ri   stickyrj   rk   zNew keys for 'z' :rf   w)anchorleftZgroove)justifytextvariabler_   rC   )ZipadxZipadyrc   )rh   ri   rn   rj   rF   )rg   rf   rZ   ZonvalueZoffvalue)rh   ri   rj   rn   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.))rs   rf      )rh   ri   
columnspanrj   rn      
   Zsingle)rE   rD   Z
selectmodeendz<ButtonRelease-1>ns)rh   ri   Zrowspanrn   Zvertical)Zorientrg   )Zyscrollcommandz
Clear Keys)rf   rg   )rh   ri   rv   Zkeyseq_advancedzEnter new binding(s) for 'z' :
z2(These bindings will not be checked for validity!))rt   )rc   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>.)rh   ri   rn   << Basic Key Binding Entry)rg   rf   Zew)rh   ri   Zstickrj   rk   )(r   framepackr   okZ	button_okZgridrL   Zbutton_cancelframe_keyseq_basicr
   rN   rQ   frame_controls_basicZmodifier_checkbuttonszipr:   rT   modifier_labelr5   r   build_key_stringr   list_keys_finalinsertAVAILABLE_KEYSbindfinal_key_selectedr   yviewconfigrR   clear_key_seqZbutton_clearframe_keyseq_advancedr	   advanced_keysframe_help_advancedtoggle_levelbutton_level)rX   r|   Zframe_buttonsZbasic_titleZ
basic_keysri   rY   rZ   ZlabelcheckZ
help_basicZscroll_keys_finalZadvanced_titleZhelp_advancedr<   r<   r=   rW   f   s   




zGetKeysDialog.create_widgetsc                 C   s.   t jdkrg d| _ng d| _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.
        darwin)r4   ControlZOptionZCommand)r   ZAltr4   r   ZCtrlN)sysplatformr:   r   rX   r<   r<   r=   rS      s   

z(GetKeysDialog.set_modifiers_for_platformc                 C   s   | j ddr(|   | j jdd | j  | j  | j	  d| _
dS |   | j jdd | j  | j  d| _
dS )	z'Toggle between basic and advanced keys.rf   ZAdvancedr{   ro   TzAdvanced Key Binding Entry >>FN)r   Zcget
startswithr   r   r   Zliftr   r   Z	focus_setrV   r   r   r   r<   r<   r=   r      s   






zGetKeysDialog.toggle_levelNc                 C   s   |    dS )z3Handler for clicking on key in basic settings list.N)r   rX   eventr<   r<   r=   r      s   z GetKeysDialog.final_key_selectedc                 C   sN   |    }}| jd}|rt||}|| | jdd| d dS )z2Create formatted string of modifiers plus the key.rq   r0   r#   r1   N)get_modifiersr   r5   r>   rU   rQ   rR   join)rX   keylistr:   	final_keyr<   r<   r=   r      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 r<   )r5   ).0rZ   r<   r<   r=   
<listcomp>       z/GetKeysDialog.get_modifiers.<locals>.<listcomp>c                 S   s   g | ]}|r|qS r<   r<   )r   modr<   r<   r=   r      r   )rT   )rX   Zmod_listr<   r<   r=   r      s   zGetKeysDialog.get_modifiersc                 C   sB   | j dd | j dd | jD ]}|d q| jd dS )z#Clear modifiers and keys selection.r   ry   Zmovetoz0.0rF   N)r   Zselect_clearr   rT   rR   rQ   )rX   rZ   r<   r<   r=   r      s
   
zGetKeysDialog.clear_key_seqc                 C   s\   | j   }|s| j| j| dd d S | js| |r$| |r$|| _| 	  | 
  d S )NzNo key specified.rJ   rM   message)rQ   r5   stripr\   keyerror_titlerV   keys_okbind_okrP   grab_releasedestroy)rX   r   keysr<   r<   r=   r~     s   
zGetKeysDialog.okc                 C   s   d| _ |   |   d S )NrF   )rP   r   r   r   r<   r<   r=   rL     s   zGetKeysDialog.cancelc                 C   s   | j d}|  }| j}dd | jD }|ds$| j|| dd dS |s6|tt vr6| j|| dd dS |d	gkrO|tt d
 vrOd}| j|| |d dS ||v r_d}| j|| |d 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.
        rq   c                 S   s   g | ]	}|D ]}|qqS r<   r<   )r   r   r9   r<   r<   r=   r     s
    
z)GetKeysDialog.keys_ok.<locals>.<listcomp>r1   zMissing the final Key)rM   r   zNo modifier key(s) specified.r   r4   )r   r   zBThe shift modifier by itself may not be used with this key symbol.z'This key combination is already in use.TF)	r   r5   r   r   rO   endswithr\   FUNCTION_KEYS	MOVE_KEYS)rX   r   r   r:   rJ   Zkey_sequencesmsgr<   r<   r=   r     s:   



zGetKeysDialog.keys_okc              
   C   sd   z
|  |dd }W n ty) } z| j| j| d| d W Y d}~dS d}~ww | || dS )z:Return True if Tcl accepts the new keys else show message.c                   S   s   d S r[   r<   r<   r<   r<   r=   <lambda>6  s    z'GetKeysDialog.bind_ok.<locals>.<lambda>z2The entered key sequence is not accepted.

Error: r   NFT)r   r   r\   r   Zunbind)rX   r   Zbindingerrr<   r<   r=   r   3  s   zGetKeysDialog.bind_okr[   )__name__
__module____qualname__r   rI   r\   rW   rS   r   r   r   r   r   r~   rL   r   r   r<   r<   r<   r=   r?   ,   s     1f
	

r?   __main__)mainz!idlelib.idle_test.test_config_keyrG   F)	verbosityexit)run)#__doc__tkinterr   r   r   r   Ztkinter.ttkr   r   r   r	   r
   r   r   Ztkinter.simpledialogr   r6   r   r   tupler7   digitsZALPHANUM_KEYSZPUNCTUATION_KEYSZWHITESPACE_KEYSZ	EDIT_KEYSr   r   r>   r?   r   Zunittestr   Zidlelib.idle_test.htestr   r<   r<   r<   r=   <module>   s<     
  