ó
âdc           @` 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 e j	 d ƒ d d l
 Z
 d g Z d e f d „  ƒ  YZ d S(	   u"   Undo context for GTK text entries.i    (   t   absolute_importt   divisiont   print_functiont   unicode_literals(   t   *Nu   2.0u   EntryUndoContextt   EntryUndoContextc           B` s­   e  Z d  Z e j d d d d g ƒ Z d d g Z d „  Z d „  Z d	 „  Z	 d
 „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z RS(   u¾   
  This class adds undo/redo capabilities to a `gtk.Entry` object.
  
  Attributes:
  
  * `undo_enabled` - If `True`, add user actions (insert text, delete text) to
    the undo history.
  u   _ActionDatau   action_typeu   positionu   textu   insertu   deletec         C` s›   | |  _  t |  _ g  |  _ g  |  _ g  |  _ d  |  _ t |  _	 |  j  j
 d |  j ƒ |  j  j
 d |  j ƒ |  j  j
 d |  j ƒ |  j  j
 d |  j ƒ d  S(   Nu   insert-textu   delete-textu   notify::cursor-positionu   key-press-event(   t   _entryt   Truet   undo_enabledt   _undo_stackt   _redo_stackt   _last_action_groupt   Nonet   _last_action_typet   Falset   _cursor_changed_by_actiont   connectt   _on_entry_insert_textt   _on_entry_delete_textt    _on_entry_notify_cursor_positiont   _on_entry_key_press_event(   t   selft   entry(    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyt   __init__!   s    							c      	   ` s`   ˆ  j  ˆ  j ˆ  j d i ‡  f d †  d 6‡  f d †  d 6d i d „  d 6d „  d 6d	 t ƒd  S(
   Nt   action_handlersc         ` s&   ˆ  j  j |  j |  j t |  j ƒ ƒ S(   N(   R   t   delete_textt   positiont   lent   text(   t   action_data(   R   (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyt   <lambda>8   s   	u   insertc         ` s   ˆ  j  j |  j |  j ƒ S(   N(   R   t   insert_textR   R   (   R   (   R   (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR   :   s   	u   deletet#   action_handlers_get_cursor_positionc         S` s   |  j  S(   N(   R   (   t   last_action_data(    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR   =   t    c         S` s   |  j  t |  j ƒ S(   N(   R   R   R   (   R!   (    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR   >   s    t   actions_iterator(   t
   _undo_redoR	   R
   t   reversed(   R   (    (   R   sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyt   undo3   s    	
c      	   ` sZ   ˆ  j  ˆ  j ˆ  j d i ‡  f d †  d 6‡  f d †  d 6d i d „  d 6d „  d 6ƒd  S(	   NR   c         ` s   ˆ  j  j |  j |  j ƒ S(   N(   R   R   R   R   (   R   (   R   (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR   G   s   	u   insertc         ` s&   ˆ  j  j |  j |  j t |  j ƒ ƒ S(   N(   R   R   R   R   R   (   R   (   R   (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR   I   s   	u   deleteR    c         S` s   |  j  t |  j ƒ S(   N(   R   R   R   (   R!   (    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR   L   s    c         S` s   |  j  S(   N(   R   (   R!   (    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR   N   R"   (   R$   R
   R	   (   R   (    (   R   sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyt   redoB   s    	
c         C` s   g  |  _  |  j ƒ  x] | D]U \ } } } | |  j k rP t d j | ƒ ƒ ‚ n  |  j j |  j | | | ƒ ƒ q W|  j ƒ  d S(   uJ  
    Manually add changes to the undo history. The changes are treated as one
    undo group (i.e. a single `undo()` call will undo all specified changes at
    once).
    
    If there are pending changes not yet added to the undo history, they are
    added first (as a separate undo group), and then the changes specified in
    this method.
    
    Calling this method completely removes the redo history.
    
    Parameters:
    
    * `undo_push_list` - List of `(action_type, position, text)` tuples to add
      as one undo action. `action_type` can be 'insert' for text insertion or
      'delete' for text deletion (other values raise `ValueError`). `position`
      is the starting entry cursor position of the changed text. `text` is the
      changed text.
    
    Raises:
    
    * `ValueError` - invalid `action_type`.
    u   invalid action type "{0}"N(   R
   t   _undo_stack_pusht   _ACTION_TYPESt
   ValueErrort   formatR   t   appendt   _ActionData(   R   t   undo_push_listt   action_typeR   R   (    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyt	   undo_pushP   s    	
#c         C` s   t  |  j ƒ S(   N(   t   boolR	   (   R   (    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyt   can_undos   s    c         C` s   t  |  j ƒ S(   N(   R1   R
   (   R   (    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyt   can_redov   s    c         C` s/   |  j  r+ | r+ |  j | j ƒ  | d ƒ n  d  S(   Nu   insert(   R   t   _on_entry_actiont   get_position(   R   R   t   new_textt   new_text_lengthR   (    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR   y   s    c         C` s?   |  j  r; | j ƒ  | | !} | r; |  j | | d ƒ q; n  d  S(   Nu   delete(   R   t   get_textR4   (   R   R   t   startt   endt   text_to_delete(    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR   }   s    	c         C` s#   |  j  r t |  _  n
 |  j ƒ  d  S(   N(   R   R   R(   (   R   R   t   property_spec(    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR   ƒ   s    	c         C` s{   | j  t j ƒ  @t j j k rw t j j t j j | j ƒ ƒ } | d k rZ |  j ƒ  t	 S| d k rw |  j
 ƒ  t	 Sn  d  S(   Nu   zu   y(   t   statet   gtkt    accelerator_get_default_mod_maskt   gdkt   CONTROL_MASKt   keyval_namet   keyval_to_lowert   keyvalR&   R   R'   (   R   R   t   eventt   key_name(    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR   ‰   s    !

c         C` sZ   g  |  _  |  j | k r% |  j ƒ  n  |  j j |  j | | | ƒ ƒ | |  _ t |  _ d  S(   N(   R
   R   R(   R   R,   R-   R   R   (   R   R   R   R/   (    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR4   “   s    		c   	      C` s±   |  j  ƒ  | s d  S| j ƒ  } | d  k r5 | } n t | | ƒ ƒ } | j | ƒ t |  _ x | D] } | | j | ƒ qd W|  j j	 | | d j | d ƒ ƒ t
 |  _ d  S(   Niÿÿÿÿ(   R(   t   popR   t   listR,   R   R   R/   R   t   set_positionR   (	   R   t   stack_to_pop_fromt   stack_to_push_toR   R    R#   t   actionst   action_listt   action(    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR$   ž   s    
			c         C` s,   |  j  r( |  j j |  j  ƒ g  |  _  n  d  S(   N(   R   R	   R,   (   R   (    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR(   ½   s    	N(   t   __name__t
   __module__t   __doc__t   collectionst
   namedtupleR-   R)   R   R&   R'   R0   R2   R3   R   R   R   R   R4   R   R$   R(   (    (    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyR      s    				#						
	(   RQ   t
   __future__R    R   R   R   t   future.builtinsRR   t   pygtkt   requireR>   t   __all__t   objectR   (    (    (    sX   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/entry_undo_context.pyt   <module>   s   "
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         # -*- coding: utf-8 -*-

"""Combo box widget for `gimp.Item` objects."""

from __future__ import absolute_import, division, print_function, unicode_literals
from future.builtins import *

import collections

import pygtk
pygtk.require('2.0')
import gtk
import gobject

import gimp
import gimpui

from .. import utils as pgutils

__all__ = [
  'GimpItemComboBox',
]


class GimpItemComboBox(gtk.VBox):
  """
  This class defines a GTK widget for `gimp.Item` objects acting as an
  abstraction over GIMP objects - layers, channels and vectors.
  
  Signals:
  
  * `'changed'` - The user changed the selection either in the combo box
    containing available item types or in the combo box for the selected item
    type.
    
    Arguments:
    
    * `selected_item` - The currently selected `gimp.Item` object.
  """
  
  __gsignals__ = {b'changed': (gobject.SIGNAL_RUN_FIRST, None, (gobject.TYPE_PYOBJECT,))}
  
  _GimpItemComboBox = collections.namedtuple(
    '_GimpItemComboBox',
    ['name', 'widget', 'get_active_item_func', 'set_active_item_func', 'gimp_item_type'])
  
  _COMBO_BOX_SPACING = 4
  
  def __init__(self, constraint=None, data=None, **kwargs):
    super().__init__(homogeneous=False, spacing=self._COMBO_BOX_SPACING, **kwargs)
    
    self._displayed_item_combo_box = None
    
    self._layer_combo_box = gimpui.LayerComboBox(constraint=constraint, data=data)
    self._channel_combo_box = gimpui.ChannelComboBox(constraint=constraint, data=data)
    self._vectors_combo_box = gimpui.VectorsComboBox(constraint=constraint, data=data)
    
    self._item_combo_boxes = [
      self._GimpItemComboBox(
        _('Layer'),
        self._layer_combo_box,
        self._layer_combo_box.get_active_layer,
        self._layer_combo_box.set_active_layer,
        gimp.Layer),
      self._GimpItemComboBox(
        _('Channel'),
        self._channel_combo_box,
        self._channel_combo_box.get_active_channel,
        self._channel_combo_box.set_active_channel,
        gimp.Channel),
      self._GimpItemComboBox(
        _('Vectors'),
        self._vectors_combo_box,
        self._vectors_combo_box.get_active_vectors,
        self._vectors_combo_box.set_active_vectors,
        gimp.Vectors)]
    
    self._item_types_combo_box = gtk.combo_box_new_text()
    
    self.pack_start(self._item_types_combo_box, expand=True, fill=True)
    
    for combo_box in self._item_combo_boxes:
      combo_box.widget.show_all()
      combo_box.widget.hide()
      combo_box.widget.set_no_show_all(True)
      
      self._item_types_combo_box.append_text(pgutils.safe_encode_gtk(combo_box.name))
      
      self.pack_start(combo_box.widget, expand=True, fill=True)
      
      combo_box.widget.connect('changed', self._on_combo_box_changed)
    
    self._item_types_combo_box.connect('changed', self._on_item_types_combo_box_changed)
    
    self._item_types_combo_box.set_active(0)
  
  def get_active_item(self):
    if self._displayed_item_combo_box is not None:
      return self._displayed_item_combo_box.get_active_item_func()
    else:
      return None
  
  def set_active_item(self, item):
    for index, combo_box in enumerate(self._item_combo_boxes):
      if isinstance(item, combo_box.gimp_item_type):
        matching_combo_box = combo_box
        matching_index = index
        break
    else:
      matching_combo_box = None
    
    if matching_combo_box is None:
      raise TypeError(
        'argument must be one of the following types: {}'.format(
          ', '.join(
            str(combo_box.gimp_item_type) for combo_box in self._item_combo_boxes)))
    
    matching_combo_box.set_active_item_func(item)
    self._item_types_combo_box.set_active(matching_index)
  
  def _on_combo_box_changed(self, *args, **kwargs):
    self.emit('changed', self.get_active_item())
  
  def _on_item_types_combo_box_changed(self, combo_box):
    if self._displayed_item_combo_box is not None:
      self._displayed_item_combo_box.widget.hide()
    
    index = self._item_types_combo_box.get_active()
    self._item_combo_boxes[index].widget.show()
    
    self._displayed_item_combo_box = self._item_combo_boxes[index]
    
    self.emit('changed', self.get_active_item())


gobject.type_register(GimpItemComboBox)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   