3
w]7                 @   s   dZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZm	Z	 d	Z
G d
d  d ejZG dd dejZedkreddZedejjedejjfZeje ejd ejd eej  eej  dS )KeyboardShortcutSelectionKeyboardShortcutSelectionDialog    )Gdk)GObject)Gtk)IBus)Pango)_N_   c                   s   e Zd Zd& fdd	Z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 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  ZS )(r   Nc                s,   t t| jtjjd | j  | j| d S )N)orientation)superr   __init__r   OrientationZVERTICAL#_KeyboardShortcutSelection__init_uiset_shortcuts)self	shortcuts)	__class__ )/usr/share/ibus/setup/keyboardshortcut.pyr   '   s    
z"KeyboardShortcutSelection.__init__c       
      C   s@  t jt jtjd| _t j }t jtd|dd}| jj	| | jj
d| j t j }|jt jjt jj |jd |j| j |jt jj | j|ddd t jt jjd	}t jtd
d}|jt jj |jdd |j|ddd t j | _| jj
d| j  |j| jddd t j!dd| _"| j"j
d| j# |j| j"ddd | j|ddd t jt jjd	}t jtdd}|jt jj |jdd |j|ddd t j$ddd}g | _%| j%j&dt j'j(dt)j*j+f | j%j&dt j'j(dt)j*j,f | j%j&dt j'j(dt)j*j-f | j%j&dt j'j(dt)j*j.f | j%j&dt j'j(dt)j*j/f | j%j&dt j'j(d t)j*j0f x&| j%D ]\}}}	|j
d!| j1| qnW |j2| j%d d" dd"dd" |j2| j%d" d" d"ddd" |j2| j%d d" dd#dd" |j2| j%d# d" dd"d"d |j2| j%d d" d"dd"d |j2| j%d$ d" dd#d"d |j|ddd | j|ddd t jt jjd	}t j!td%dd&| _3| j3j4d | j3j
d| j5 |j| j3ddd t j!td'dd&| _6| j6j4d | j6j
d| j7 |j| j6ddd t j!td(dd&| _8| j8j4d | j8j
d| j9 |j| j8ddd | j|ddd d S ))N)modelzKeyboard shortcutsr   )textzcursor-changedd   T   )r   z	Key code:)labelg        g      ?Fznotify::textz...Zclickedz
Modifiers:   )Zn_rowsZ	n_columnsZControlZ_ControlZAltZA_ltZShiftZ_ShiftZMetaZ_MetaZSuperZS_uperZHyperZ_HyperZtoggled         Z_Add)r   Zuse_underlineZ_ApplyZ_Delete):r   TreeView	ListStorer   ZTYPE_STRING)_KeyboardShortcutSelection__shortcut_viewZCellRendererTextTreeViewColumnr	   append_columnconnect;_KeyboardShortcutSelection__shortcut_view_cursor_changed_cbScrolledWindowZ
set_policyZ
PolicyTypeZ	AUTOMATICset_min_content_heightaddZset_shadow_typeZ
ShadowTypeIN
pack_startBoxr   Z
HORIZONTALZLabelZset_justifyZJustificationZLEFTZset_alignmentZEntry)_KeyboardShortcutSelection__keycode_entry3_KeyboardShortcutSelection__keycode_entry_notify_cbZButtonZ*_KeyboardShortcutSelection__keycode_button5_KeyboardShortcutSelection__keycode_button_clicked_cbZTable,_KeyboardShortcutSelection__modifier_buttonsappendZCheckButtonZnew_with_mnemonicr   ZModifierTypeZCONTROL_MASKZ	MOD1_MASKZ
SHIFT_MASKZ	META_MASKZ
SUPER_MASKZ
HYPER_MASK6_KeyboardShortcutSelection__modifier_button_toggled_cbZattach&_KeyboardShortcutSelection__add_buttonset_sensitive1_KeyboardShortcutSelection__add_button_clicked_cb(_KeyboardShortcutSelection__apply_button3_KeyboardShortcutSelection__apply_button_clicked_cb)_KeyboardShortcutSelection__delete_button4_KeyboardShortcutSelection__delete_button_clicked_cb)
r   renderercolumnZscrolledwindowZhboxr   tablenamebuttonmaskr   r   r   Z	__init_ui-   s    













z#KeyboardShortcutSelection.__init_uic             C   s\   |d krg }| j j }|j  g }x4|D ],}||kr(|jd}||| d< |j| q(W d S )Nr   )r"   	get_modelclearinsertr1   )r   r   r   Zaddedshortcutitr   r   r   r      s    


z'KeyboardShortcutSelection.set_shortcutsc          	   C   s(   | j j }ydd |D S    g S d S )Nc             S   s   g | ]}|d  qS )r   r   ).0ir   r   r   
<listcomp>   s    z;KeyboardShortcutSelection.get_shortcuts.<locals>.<listcomp>)r"   r@   )r   r   r   r   r   get_shortcuts   s
    
z'KeyboardShortcutSelection.get_shortcutsc             C   sj   | j j }t|tkrd S || j kr*d S |jd}||| d< | jjd |j|}| j j	|d d d S )Nr   F)
r"   r@   len
MAX_HOTKEYrH   rB   r3   r4   get_pathZ
set_cursor)r   rC   r   rD   pathr   r   r   add_shortcut   s    


z&KeyboardShortcutSelection.add_shortcutc             C   s~   g }| j j }tj|dkr d S x&| jD ]\}}}|j r(|j| q(W |jdr^|dd  }djdd |D }||7 }|S )Nr   r	   r    c             S   s   g | ]}d | d qS )<>r   )rE   mr   r   r   rG      s    zIKeyboardShortcutSelection.__get_shortcut_from_buttons.<locals>.<listcomp>)	r-   Zget_textr   Zkeyval_from_namer0   Z
get_activer1   
startswithjoin)r   Z	modifierskeycoder=   r>   r?   rC   r   r   r   Z__get_shortcut_from_buttons   s    

z5KeyboardShortcutSelection.__get_shortcut_from_buttonsc             C   sr   t j|\}}|dkr"|dkr"d S x2| jD ](\}}}||@ rH|jd q*|jd q*W | jj|jddd  d S )Nr   TFrP   r   )r   Zaccelerator_parser0   
set_activer-   set_textrsplit)r   rC   keyvalstater=   r>   r?   r   r   r   Z__set_shortcut_to_buttons   s    z3KeyboardShortcutSelection.__set_shortcut_to_buttonsc             C   s8   | j j }| j j \}}|d kr$d S ||j d  d S )Nr   )r"   r@   
get_cursorZget_indices)r   r   rL   r;   r   r   r   Z__get_selected_shortcut   s
    
z1KeyboardShortcutSelection.__get_selected_shortcutc             C   s4   | j j }| j j \}}|||d  d< | j  d S )Nr   )r"   r@   r[   8_KeyboardShortcutSelection__update_add_and_apply_buttons)r   rC   r   rL   r;   r   r   r   Z__set_selected_shortcut   s    
z1KeyboardShortcutSelection.__set_selected_shortcutc             C   s4   | j j }| j j \}}|j|j| | j  d S )N)r"   r@   r[   removeZget_iterr\   )r   r   rL   r;   r   r   r   Z__del_selected_shortcut   s    
z1KeyboardShortcutSelection.__del_selected_shortcutc             C   s8   | j  }|d kr(| j| | jjd n| jjd d S )NTF)1_KeyboardShortcutSelection__get_selected_shortcut3_KeyboardShortcutSelection__set_shortcut_to_buttonsr8   r4   )r   ZtreeviewrC   r   r   r   Z!__shortcut_view_cursor_changed_cb   s
    
z;KeyboardShortcutSelection.__shortcut_view_cursor_changed_cbc             C   sp   | j  }| j }| j }|d ko2||ko2t|tk }| jj| ||ko^|d ko^|d ko^||k}| jj| d S )N)5_KeyboardShortcutSelection__get_shortcut_from_buttonsr^   rH   rI   rJ   r3   r4   r6   )r   rC   Zselected_shortcutr   Zcan_addZ	can_applyr   r   r   Z__update_add_and_apply_buttons   s    z8KeyboardShortcutSelection.__update_add_and_apply_buttonsc             C   s   | j   d S )N)r\   )r   r>   r=   r   r   r   Z__modifier_button_toggled_cb   s    z6KeyboardShortcutSelection.__modifier_button_toggled_cbc             C   s   | j   d S )N)r\   )r   entryargr   r   r   Z__keycode_entry_notify_cb   s    z3KeyboardShortcutSelection.__keycode_entry_notify_cbc                s  g t j| j t jjd td} j|  jtd t j } fdd}t j	t
jt
jt
j}t j|d}|jd |j| |jd t j }t jt jjd	d
}|jd| |j|d	 |j|dd |j|dd |j|dd |j| |jd }	 j }
|
j|d	d	d |j   j } j  |t jj ksJt!dk rNd S d }d }d }x6| j"D ],\}}}||@ r|j#d	 n
|j#d qnW t j$d |||}|j%dd}| j&j'|j(ddd  d S )N)transient_forbuttonsz^Please press a key (or a key combination).
The dialog will be closed when the key is released.z)Please press a key (or a key combination)c                s0   j | j | j |  jtjj d S )N)r1   Zresponser   ResponseTypeOK)crL   rY   rZ   rT   )dlgoutr   r   Z__accel_edited_cb  s    


zPKeyboardShortcutSelection.__keycode_button_clicked_cb.<locals>.__accel_edited_cb)r   F   T)Z
accel_modeZeditablezaccel-editedz
accel-modsr   z	accel-keyr   rT   r   r   z	<Primary>z	<Control>rP   rU   ))r   ZMessageDialogZget_toplevelZButtonsTypeZCLOSEr	   Z
set_markupZ	set_titler'   r!   r   ZTYPE_INTZ	TYPE_UINTr    Zset_headers_visibler)   r(   r#   ZCellRendererAccelZCellRendererAccelModeZOTHERr%   r+   Zadd_attributer$   r1   Zget_message_areaZpack_endshow_allrunZdestroyre   rf   rI   r0   rV   Zaccelerator_name_with_keycodereplacer-   rW   rX   )r   r>   messageswZ+_KeyboardShortcutSelection__accel_edited_cbr   Z
accel_viewr;   r:   rD   ZareaidrY   rZ   rT   r=   r?   rC   r   )rh   ri   r   Z__keycode_button_clicked_cb   sZ    








z5KeyboardShortcutSelection.__keycode_button_clicked_cbc             C   s   | j  }| j| d S )N)r`   rM   )r   r>   rC   r   r   r   Z__add_button_clicked_cb1  s    z1KeyboardShortcutSelection.__add_button_clicked_cbc             C   s   | j  }| j| d S )N)r`   1_KeyboardShortcutSelection__set_selected_shortcut)r   r>   rC   r   r   r   Z__apply_button_clicked_cb5  s    z3KeyboardShortcutSelection.__apply_button_clicked_cbc             C   s$   | j   | jjd | jjd d S )NF)1_KeyboardShortcutSelection__del_selected_shortcutr8   r4   r6   )r   r>   r   r   r   Z__delete_button_clicked_cb9  s    z4KeyboardShortcutSelection.__delete_button_clicked_cb)N)N)__name__
__module____qualname__r   r   r   rH   rM   r`   r_   r^   rq   rr   r&   r\   r2   r.   r/   r5   r7   r9   __classcell__r   r   )r   r   r   &   s$   b
9c                   s8   e Zd Zd fdd	ZdddZdd Zd	d
 Z  ZS )r   Nr   c                s@   t t| j|||d t | _| jj| jddd | jj  d S )N)titlerc   flagsFTr   )r   r   r   r   0_KeyboardShortcutSelectionDialog__selection_viewZvboxr+   rk   )r   rw   rc   rx   )r   r   r   r   ?  s
    
z(KeyboardShortcutSelectionDialog.__init__c             C   s   | j j| d S )N)ry   r   )r   Z	shotrcutsr   r   r   r   F  s    z-KeyboardShortcutSelectionDialog.set_shortcutsc             C   s   | j j| d S )N)ry   rM   )r   Zshotrcutr   r   r   rM   I  s    z,KeyboardShortcutSelectionDialog.add_shortcutc             C   s
   | j j S )N)ry   rH   )r   r   r   r   rH   L  s    z-KeyboardShortcutSelectionDialog.get_shortcuts)NNr   )N)rs   rt   ru   r   r   rM   rH   rv   r   r   )r   r   r   >  s   
__main__zSelect test)rw   Z_CancelZ_OKzControl+Shift+spaceN)r   r   )__all__Zgi.repositoryr   r   r   r   r   Zi18nr	   r
   rJ   r,   r   ZDialogr   rs   rh   re   ZCANCELrf   rd   Zadd_buttonsrM   r   printrl   rH   r   r   r   r   <module>   s*      



