
ddc           @` s,  d  Z  d d l m Z m Z m Z m Z d d l Td d l Z d d l Z d d l	 Z	 e	 j
 d  d d l Z d d l Z d d l m Z d d	 l m Z d
 d d g Z d e j f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ e j e  d S(   uv   Widget holding an array of GUI elements.

The widget is used as the default GUI for `setting.ArraySetting` instances.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literals(   t   *Nu   2.0i   (   t   utilsi   (   t   drag_and_drop_contextu   ItemBoxu   ArrayBoxu   ItemBoxItemt   ItemBoxc           B` s   e  Z d  Z d Z d Z e d  Z e d    Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z RS(   u   
  This base class defines a scrollable box holding a vertical list of items.
  Each item is an instance of `ItemBoxItem` class or one of its subclasses.
  i   c         O` s   t    j | |   | |  _ t j   |  _ g  |  _ t j d t	  |  _
 |  j
 j |  j  t j d t	  |  _ |  j j |  j  |  j j |  j
 d t	 d t	 |  j t j t j  |  j |  j  |  j   j t j  d  S(   Nt   homogeneoust   expandt   fill(   t   supert   __init__t   _item_spacingt   drag_and_drop_context_t   DragAndDropContextt   _drag_and_drop_contextt   _itemst   gtkt   VBoxt   Falset   _vbox_itemst   set_spacingt   _vboxt   VBOX_SPACINGt
   pack_startt
   set_policyt   POLICY_AUTOMATICt   add_with_viewportt	   get_childt   set_shadow_typet   SHADOW_NONE(   t   selft   item_spacingt   argst   kwargs(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   '   s    		c         C` s   |  j  S(   N(   R   (   R    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyt   items:   s    c         C` sr   |  j  j | j d t d t | j j d |  j |  | j j d |  j |  |  j |  |  j	 j
 |  | S(   NR	   R
   u   clickedu   key-press-event(   R   R   t   widgetR   t   button_removet   connectt   _on_item_button_remove_clickedt   _on_item_widget_key_press_eventt   _setup_dragR   t   append(   R    t   item(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyt   add_item>   s    c         C` sk   t  t | d  t |  j  d  } |  j j |  j |   |  j j | |  |  j j | j	 |  | S(   Ni    i   (
   t   mint   maxt   lenR   t   popt   _get_item_positiont   insertR   t   reorder_childR%   (   R    R,   t   positiont   new_position(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyt   reorder_itemJ   s
    %c         C` sz   |  j  |  } | t |  j  d k  rI | d } |  j | j j   n  |  j j | j  | j   |  j j |  d  S(   Ni   (	   R2   R0   R   t   item_widgett
   grab_focusR   t   removeR%   t   remove_item_widget(   R    R,   t   item_positiont   next_item_position(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyt   remove_itemT   s    

c         C` s8   x1 t  t |  j   D] } |  j |  j d  q Wd  S(   Ni    (   t   rangeR0   R   R>   (   R    t   unused_(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyt   clear_   s    c         C` s2   |  j  j | j |  j |  j | g | g |   d  S(   N(   R   t
   setup_dragR8   t   _get_drag_datat   _on_drag_data_received(   R    R,   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR*   c   s    	c         C` s   t  |  j j |   S(   N(   t   strR   t   index(   R    t   dragged_item(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyRC   l   s    c         C` s0   |  j  t |  } |  j | |  j |   d  S(   N(   R   t   intR7   R2   (   R    t   dragged_item_index_strt   destination_itemRG   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyRD   o   s    c         C` s   | j  t j j @r t j j | j  } | d k rT |  j | |  j |  d  q | d k r |  j | |  j |  d  q n  d  S(   Nu   Upu   KP_Upi   u   Downu   KP_Down(   u   Upu   KP_Up(   u   Downu   KP_Down(   t   stateR   t   gdkt	   MOD1_MASKt   keyval_namet   keyvalR7   R2   (   R    R%   t   eventR,   t   key_name(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR)   s   s    c         C` s   |  j  |  d  S(   N(   R>   (   R    t   buttonR,   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR(   }   s    c         C` s   |  j  j |  S(   N(   R   RF   (   R    R,   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR2      s    (   t   __name__t
   __module__t   __doc__t   ITEM_SPACINGR   R   t   propertyR$   R-   R7   R>   RA   R*   RC   RD   R)   R(   R2   (    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR      s   		
							
	t   ItemBoxItemc           B` s   e  Z d  Z d  Z d   Z e d    Z e d    Z e d    Z d   Z	 d d  Z d d  Z d   Z d	   Z d
   Z d   Z d   Z RS(   i   c         C` s  | |  _  t j d t  |  _ |  j j |  j  t j d t  |  _ |  j j |  j  t j	   |  _
 |  j
 j |  j  t j d t  |  _ |  j j |  j  t j	   |  _ |  j j |  j  |  j j |  j
 d t d t |  j j |  j  d t d t |  j j |  j d t d t t j	   |  _ |  j j |  j  t |  _ t j   |  _ |  j |  j t j  |  j j d |  j  |  j j d |  j  t |  _ d  |  _ |  j j d |  j  |  j j d |  j  |  j j   |  j j t  |  j j t  d  S(   NR   R	   R
   u   enter-notify-eventu   leave-notify-eventu   size-allocate(    t   _item_widgetR   t   HBoxR   t   _hboxR   t   _HBOX_SPACINGt   _hbox_indicator_buttonst   _HBOX_BUTTONS_SPACINGt   EventBoxt   _event_box_indicator_buttonst   addt   _hbox_buttonst   _event_box_buttonsR   t   Truet
   _event_boxt   _has_hbox_buttons_focust   Buttont   _button_removet   _setup_item_buttont   STOCK_CLOSER'   t    _on_event_box_enter_notify_eventt    _on_event_box_leave_notify_eventt   _is_event_box_allocated_sizet   Nonet   _buttons_allocationt   _on_event_box_size_allocatet#   _on_event_box_buttons_size_allocatet   show_allt   set_no_show_all(   R    R8   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR      s8    				c         C` s   |  j  S(   N(   Re   (   R    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR%      s    c         C` s   |  j  S(   N(   RY   (   R    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR8      s    c         C` s   |  j  S(   N(   Rh   (   R    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR&      s    c         C` s   |  j  j |  j  d  S(   N(   R[   R:   RY   (   R    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR;      s    c         C` s   |  j  | | | |  j  d  S(   N(   t   _setup_buttonRb   (   R    RR   t   iconR5   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyRi      s    c         C` s   |  j  | | | |  j  d  S(   N(   Rt   R]   (   R    RR   Ru   R5   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyt   _setup_item_indicator_button   s    c         C` s   | j  t j  t j | j | t j   } | j |  | j | d t d t | d  k	 rs | j
 | |  n  | j   d  S(   NR	   R
   (   t
   set_reliefR   t   RELIEF_NONEt   image_new_from_pixbuft   render_icont   ICON_SIZE_MENURa   R   R   Rn   R4   Rr   (   R    RR   Ru   R5   t   hboxt   button_icon(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyRt      s    c         C` s)   | j  t j j k r% |  j j   n  d  S(   N(   t   detailR   RL   t   NOTIFY_INFERIORRb   t   show(   R    t	   event_boxRP   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyRk      s    c         C` s)   | j  t j j k r% |  j j   n  d  S(   N(   R~   R   RL   R   Rb   t   hide(   R    R   RP   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyRl      s    c         C` sW   |  j  rS |  j d  k	 rS t |  _  |  j j | j k rS |  j j d | j  qS n  d  S(   Nu   height-request(   Rm   Ro   Rn   Rd   t   heightR[   t   set_property(   R    R   t
   allocation(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyRp      s    	c         C` sc   |  j  d  k r_ | j d k r_ | j d k r_ | |  _  |  j j d |  j  j  |  j j   n  d  S(   Ni   u   width-request(   Ro   Rn   t   widthR   Rc   R   Rb   R   (   R    R   R   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyRq      s    -	N(   RS   RT   R^   R\   R   RW   R%   R8   R&   R;   Rn   Ri   Rv   Rt   Rk   Rl   Rp   Rq   (    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyRX      s   	+					t   ArrayBoxc           B` s   e  Z d  Z i e j d d f d 6e j d d f d 6Z d Z d d e j	 d d d  Z
 d   Z d d d  Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z e d    Z RS(   u  
  This class can be used to edit `setting.ArraySetting` instances interactively.
  
  Signals:
  
  * `'array-box-changed'` - An item was added, reordered or removed by the user.
  * `'array-box-item-changed'` - The contents of an item was modified by the
    user. Currently, this signal is not invoked in this widget and can only be
    invoked explicitly by calling `ArrayBox.emit('array-box-item-changed')`.
  s   array-box-changeds   array-box-item-changedi   i    c   	      O` s   t    j d | | |  | |  _ | d k r4 | n d |  _ | d k rU d |  _ n | | k rg | n | |  _ | |  _ | |  _ t j	 |  _
 t j	 |  _ t j	 |  _ d |  _ d |  _ i  |  _ t   |  _ |  j   d S(   u9  
    Parameters:
    
    * `new_item_default_value` - default value for new items.
    
    * `min_size` - minimum number of elements.
    
    * `max_size` - maximum number of elements. If `None`, the number of elements
      is unlimited.
    
    * `item_spacing` - vertical spacing in pixels between items.
    
    * `max_width` - maximum width of the array box before the horizontal
      scrollbar is displayed. The array box will resize automatically until the
      maximum width is reached. If `max_width` is `None`, the width is fixed
      to whatever width is provided by `gtk.ScrolledWindow`. If `max_width` is
      zero or negative, the width is unlimited.
    
    * `max_height` - maximum height of the array box before the vertical
      scrollbar is displayed. For more information, see `max_width`.
    R!   i    i   i    NI       (   R   R   t   _new_item_default_valuet	   _min_sizeRn   t	   _max_sizet	   max_widtht
   max_heightt   pgutilst
   empty_funct   on_add_itemt   on_reorder_itemt   on_remove_itemt   _items_total_widtht   _items_total_heightt   _items_allocationst   _ActionLockert   _lockert	   _init_gui(	   R    t   new_item_default_valuet   min_sizet   max_sizeR!   R   R   R"   R#   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR     s     						c         C` s)  t  j t  j d d d |  j d |  j d d d d  d	 d |  _ |  j j t  |  j j d  t  j	 t
 d
   |  _ t  j   |  _ |  j j |  j  |  j j |  j d t d t |  j j |  j d t d t |  j j |  j d t d t |  j j |  j d  |  j j d |  j  d  S(   Nt   valuei    t   lowert   uppert	   step_incri   t	   page_incri
   t   digitsu   SizeR	   R
   u   value-changed(   R   t
   SpinButtont
   AdjustmentR   R   t   _size_spin_buttont   set_numericRd   t	   set_valuet   Labelt   _t   _size_spin_button_labelRZ   t
   _size_hboxR   t   _SIZE_HBOX_SPACINGR   R   R   R4   R'   t"   _on_size_spin_button_value_changed(   R    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   <  s&    					c      	   C` s  | d  k r |  j } n  |  j | |  } t |  } t   j |  | j j d |  j |  | d  k r | j	 j
 |  j t |  j    n  | d  k	 r |  j j d   |  j | |  Wd  QXn  |  j j d  r|  j j d   |  j j t j d d Wd  QXn  | S(   Nu   size-allocateu!   emit_array_box_changed_on_reorderu   update_spin_buttonu#   emit_size_spin_button_value_changedt	   incrementi   (   Rn   R   R   t   _ArrayBoxItemR   R-   R%   R'   t   _on_item_widget_size_allocatet   labelt	   set_labelt   _get_item_nameR0   R   R   t	   lock_tempR7   t   is_unlockedR   t   spinR   t   SPIN_STEP_FORWARD(   R    t
   item_valueRF   R8   R,   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR-   W  s    %"c         C` sp   |  j  |  } t   j | |  } |  j | |  |  j t | |   |  j j d  rl |  j d  n  d  S(   Nu!   emit_array_box_changed_on_reorderu   array-box-changed(	   R2   R   R7   R   t   _rename_item_namesR.   R   R   t   emit(   R    R,   R6   t   orig_positiont   processed_new_position(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR7   p  s    c         C` s   |  j  j d  r. t |  j  |  j k r. d  S|  j  j d  ru |  j  j d   |  j j t j	 d d Wd  QXn  |  j
 |  } t   j |  | |  j k r |  j |  j | j |  j  |  j | =n  |  j |  |  j |  d  S(   Nu   prevent_removal_below_min_sizeu   update_spin_buttonu#   emit_size_spin_button_value_changedR   i   (   R   R   R0   R   R   R   R   R   R   t   SPIN_STEP_BACKWARDR2   R   R>   R   t   _update_heightR   R   R   R   (   R    R,   R<   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR>   {  s    "c         C` s   |  j  j d  |  j  j d  |  j } t j |  _ |  j   |  j d d  x* t |  D] \ } } |  j | |  q\ W| |  _ |  j	 j
 t |   |  j  j d  |  j  j d  d  S(   Nu#   emit_size_spin_button_value_changedu   prevent_removal_below_min_sizeu   height-requesti(   R   t   lockR   R   R   RA   R   t	   enumerateR-   R   R   R0   t   unlock(   R    t   valuest   orig_on_remove_itemRF   R   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyt
   set_values  s    	
	c         C` s2   |  j  j | j |  j |  j | g | g |   d  S(   N(   R   RB   R%   RC   RD   (   R    R,   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR*     s    	c         C` s   |  j  j d  r |  j  j d  | j   } | t |  j  k rz | t |  j  } xw t |  D] } |  j   qc WnV | t |  j  k  r t |  j  | } x+ t |  D] } |  j |  j d  q Wn  |  j	 d  |  j  j
 d  n  d  S(   Nu#   emit_size_spin_button_value_changedu   update_spin_buttoniu   array-box-changed(   R   R   R   t   get_value_as_intR0   R   R?   R-   R>   R   R   (   R    t   size_spin_buttont   new_sizet   num_elements_to_addR@   t   num_elements_to_remove(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR     s    c         C` sw   |  j  j d  t |  j  |  j k p7 |  j  j d  } t   j | |  | rc |  j d  n  |  j  j	 d  d  S(   Nu#   emit_size_spin_button_value_changedu   prevent_removal_below_min_sizeu   array-box-changed(
   R   R   R0   R   R   t	   is_lockedR   R(   R   R   (   R    RR   R,   t   should_emit_signal(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR(     s    c         C` s   | |  j  k rN |  j | j |  j  | j  |  j | j |  j  | j  n' |  j | j  |  j | j |  j  | |  j  | <d  S(   N(   R   t   _update_widthR   R   R   R   (   R    R8   R   R,   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR     s    !c         C` sc   |  j  d  k r$ |  j   j |  _  n  | d k r_ |  j | |  j  |  j d  |  j  | |  _  n  d  S(   Ni    u   width-request(   R   Rn   t   get_allocationR   t   _update_dimensionR   (   R    t
   width_diff(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR     s    c         C` sc   |  j  d  k r$ |  j   j |  _  n  | d k r_ |  j | |  j  |  j d  |  j  | |  _  n  d  S(   Ni    u   height-request(   R   Rn   R   R   R   R   (   R    t   height_diff(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR     s    c         C` s   | d  k r t } n | d k } | s9 t | |  } n | } | sa | | | k rn | | k  rn | | } nD | | k r | d k  r | | | k  r | | } q | } n | } | d  k	 r |  j | |  n  d  S(   Ni    (   Rn   Rd   R.   R   (   R    t	   size_difft
   total_sizet   max_visible_sizet   dimension_request_propertyt   is_max_visible_size_unlimitedt   visible_sizeR   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR     s"    		c         C` sI   xB t  |  j |  D]- \ } } | j j |  j | d |   q Wd  S(   Ni   (   R   R   R   R   R   (   R    t   start_indexRF   R,   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR     s     c         C` s   t  d  d t |   S(   Nu   Elementu    (   R   RE   (   RF   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR     s    N(    (    (   RS   RT   RU   t   gobjectt   SIGNAL_RUN_FIRSTRn   t   __gsignals__R   R   RV   R   R   R-   R7   R>   R   R*   R   R(   R   R   R   R   R   t   staticmethodR   (    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR      s0   
0								
				R   c           B` s    e  Z d    Z e d    Z RS(   c         C` se   t    j |  t j   |  _ |  j j   |  j j |  j d t d t |  j j	 |  j d  d  S(   NR	   R
   i    (
   R   R   R   R   t   _labelR   R[   R   R   R4   (   R    R8   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   #  s
    c         C` s   |  j  S(   N(   R   (   R    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   ,  s    (   RS   RT   R   RW   R   (    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   !  s   		R   c           B` sG   e  Z d    Z e j d    Z d   Z d   Z d   Z d   Z	 RS(   c         C` s   t  j t  |  _ d  S(   N(   t   collectionst   defaultdictRH   t   _tokens(   R    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   3  s    c         c` s+   |  j  |  z	 d  VWd  |  j |  Xd  S(   N(   R   R   (   R    t   key(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   6  s    	c         C` s   |  j  | c d 7<d  S(   Ni   (   R   (   R    R   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   >  s    c         C` s-   |  j  | d k r) |  j  | c d 8<n  d  S(   Ni    i   (   R   (   R    R   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   A  s    c         C` s   |  j  | d k S(   Ni    (   R   (   R    R   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   E  s    c         C` s   |  j  | d k S(   Ni    (   R   (   R    R   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   H  s    (
   RS   RT   R   t
   contextlibt   contextmanagerR   R   R   R   R   (    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyR   1  s   				(   RU   t
   __future__R    R   R   R   t   future.builtinsR   R   t   pygtkt   requireR   R   t    R   R   R   R   t   __all__t   ScrolledWindowR   t   objectRX   R   R   R   t   type_register(    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/item_box.pyt   <module>   s(   "
	fo /                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            # -*- coding: utf-8 -*-

"""Dialog prompt for handling conflicting files (overwrite, skip, etc.)."""

from __future__ import absolute_import, division, print_function, unicode_literals
from future.builtins import *

import os

import pygtk
pygtk.require('2.0')
import gtk
import gobject

import gimpui

from .. import overwrite as pgoverwrite

__all__ = [
  'GtkDialogOverwriteChooser',
]


class GtkDialogOverwriteChooser(pgoverwrite.InteractiveOverwriteChooser):
  """
  This class is used to display a GTK dialog prompt in an interactive
  environment when a file about to be saved has the same name as an already
  existing file.
  """
  
  _DIALOG_BORDER_WIDTH = 8
  _DIALOG_HBOX_CONTENTS_SPACING = 10
  _DIALOG_VBOX_SPACING = 5
  _DIALOG_ACTION_AREA_SPACING = 8
  
  def __init__(
        self,
        values_and_display_names,
        default_value,
        default_response,
        title='',
        parent=None):
    
    super().__init__(values_and_display_names, default_value, default_response)
    
    self._title = title
    self._parent = parent
    
    self._init_gui()
  
  def _init_gui(self):
    self._dialog = gimpui.Dialog(
      title='',
      role=None,
      parent=self._parent,
      flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT)
    self._dialog.set_transient_for(self._parent)
    self._dialog.set_title(self._title)
    self._dialog.set_border_width(self._DIALOG_BORDER_WIDTH)
    self._dialog.set_resizable(False)
    
    self._dialog_icon = gtk.Image()
    self._dialog_icon.set_from_stock(gtk.STOCK_DIALOG_QUESTION, gtk.ICON_SIZE_DIALOG)
    
    self._dialog_text = gtk.Label('')
    self._dialog_text.set_line_wrap(True)
    self._dialog_text.set_use_markup(True)
    
    self._dialog_text_event_box = gtk.EventBox()
    self._dialog_text_event_box.add(self._dialog_text)
    
    self._hbox_dialog_contents = gtk.HBox(homogeneous=False)
    self._hbox_dialog_contents.set_spacing(self._DIALOG_HBOX_CONTENTS_SPACING)
    self._hbox_dialog_contents.pack_start(self._dialog_icon, expand=False, fill=False)
    self._hbox_dialog_contents.pack_start(
      self._dialog_text_event_box, expand=False, fill=False)
    
    self._checkbutton_apply_to_all = gtk.CheckButton(
      label=_('_Apply action to all files'))
    self._checkbutton_apply_to_all.set_use_underline(True)
    
    self._dialog.vbox.set_spacing(self._DIALOG_VBOX_SPACING)
    self._dialog.vbox.pack_start(self._hbox_dialog_contents, expand=False, fill=False)
    self._dialog.vbox.pack_start(self._checkbutton_apply_to_all, expand=False, fill=False)
    
    self._buttons = {}
    for value, display_name in self.values_and_display_names:
      self._buttons[value] = self._dialog.add_button(display_name, value)
    
    self._dialog.action_area.set_spacing(self._DIALOG_ACTION_AREA_SPACING)
    
    self._checkbutton_apply_to_all.connect(
      'toggled', self._on_checkbutton_apply_to_all_toggled)
    
    self._is_dialog_text_allocated_size = False
    self._dialog_text_event_box.connect(
      'size-allocate', self._on_dialog_text_event_box_size_allocate)
    
    self._dialog.set_focus(self._buttons[self.default_value])
  
  def _choose(self, filepath):
    if filepath is not None:
      dirpath, filename = os.path.split(filepath)
      if dirpath:
        text_choose = (
          _('A file named "{}" already exists in "{}". ').format(
            filename, os.path.basename(dirpath)))
      else:
        text_choose = _('A file named "{}" already exists.\n').format(filename)
    else:
      text_choose = _('A file with the same name already exists.\n')
    
    text_choose += _('What would you like to do?')
    
    self._dialog_text.set_markup(
      '<span font_size="large"><b>{}</b></span>'.format(
        gobject.markup_escape_text(text_choose)))
    
    self._dialog.show_all()
    
    self._overwrite_mode = self._dialog.run()
    
    if self._overwrite_mode not in self._values:
      self._overwrite_mode = self.default_response
    
    self._dialog.hide()
    
    return self._overwrite_mode
  
  def _on_checkbutton_apply_to_all_toggled(self, checkbutton):
    self._is_apply_to_all = self._checkbutton_apply_to_all.get_active()
  
  def _on_dialog_text_event_box_size_allocate(self, dialog_text_event_box, allocation):
    if not self._is_dialog_text_allocated_size:
      self._is_dialog_text_allocated_size = True
      
      # Make sure the label uses as much width as possible in the dialog.
      dialog_text_allocation = dialog_text_event_box.get_allocation()
      dialog_vbox_allocation = self._dialog.vbox.get_allocation()
      self._dialog_text.set_property(
        'width-request', dialog_vbox_allocation.width - dialog_text_allocation.x)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          