ó
âdc           @` s   d  Z  d d l m Z m Z m Z m Z d d l Td d l Z e j d  d d l	 Z	 d d l
 Z
 d g Z d e	 j f d     YZ e
 j e  d S(	   u   Custom GTK cell renderers.i    (   t   absolute_importt   divisiont   print_functiont   unicode_literals(   t   *Nu   2.0u   CellRendererTextListt   CellRendererTextListc           B` s   e  Z d  Z i e j d d e j f d 6e j d d e j f d 6e j d d d	 e j f d
 6Z d   Z	 d   Z
 d   Z d   Z e d    Z RS(   uR   
  This is a custom text-based cell renderer that can accept a list of strings.
  s   list of stringsu   List of strings to renders	   text-lists   list of strings in markupu%   List of strings with markup to renders   markup-lists   separator for list of stringsuQ   Text separator for the list of strings ("text-list" and "markup-list" properties)u   , s   text-list-separatorc         C` s/   t  j j |   d  |  _ d  |  _ d |  _ d  S(   Nu   , (   t   gtkt   CellRendererTextt   __init__t   Nonet	   text_listt   markup_listt   text_list_separator(   t   self(    (    sT   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/cell_renderers.pyR   -   s    		c         C` sH   |  j  | j  } t |  |  r. t |  |  St j j |  | j  Sd  S(   N(   t   _property_name_to_attrt   namet   hasattrt   getattrR   R   t   get_property(   R   t	   property_t	   attr_name(    (    sT   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/cell_renderers.pyt   do_get_property4   s    c         C` s   |  j  | j  } t |  |  r | j d k r^ t | t  pK t | t  r^ t d   n  t |  | |  |  j | j  n  d  S(   Nu	   text-listu   markup-listu   not a list or tuple(   u	   text-listu   markup-list(	   R   R   R   t
   isinstancet   listt   tuplet   AttributeErrort   setattrt   _evaluate_text_property(   R   R   t   valueR   (    (    sT   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/cell_renderers.pyt   do_set_property;   s    c         ` sĦ     f d   }   f d   } | d k r= |   d   _ n` | d k r\ |   d   _ nA | d k r   j d k	 r |   q   j d k	 r |   q n  d S(   u   
    Change the 'text' or 'markup' property according to the value of
    'text-list', 'markup-list' and 'text-list-separator' properties.
    c          ` s/     j  j   j  }  t j j   d |   d  S(   Nu   text(   R   t   joinR
   R   R   t   set_property(   t   new_text(   R   (    sT   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/cell_renderers.pyt	   _set_textK   s    c          ` s/     j  j   j  }  t j j   d |   d  S(   Nu   markup(   R   R   R   R   R   R   (   R    (   R   (    sT   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/cell_renderers.pyt   _set_markupO   s    u	   text-listu   markup-listu   text-list-separatorN(   R	   R   R
   (   R   t   property_nameR!   R"   (    (   R   sT   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/cell_renderers.pyR   F   s    
c         C` s   |  j  d d  S(   Nu   -u   _(   t   replace(   R#   (    (    sT   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/cell_renderers.pyR   _   s    (   t   __name__t
   __module__t   __doc__t   gobjectt   TYPE_PYOBJECTt   PARAM_READWRITEt   PARAM_WRITABLEt   TYPE_STRINGt   __gproperties__R   R   R   R   t   staticmethodR   (    (    (    sT   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/cell_renderers.pyR      s(   				(   R'   t
   __future__R    R   R   R   t   future.builtinst   pygtkt   requireR   R(   t   __all__R   R   t   type_register(    (    (    sT   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/gui/cell_renderers.pyt   <module>   s   "
	R                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           # -*- coding: utf-8 -*-

"""Class providing drag-and-drop capability to any GTK widget."""

from __future__ import absolute_import, division, print_function, unicode_literals
from future.builtins import *

import pygtk
pygtk.require('2.0')
import gtk

__all__ = [
  'DragAndDropContext',
]


class DragAndDropContext(object):
  """
  This class adds drag-and-drop capability to the specified GTK widget.
  """
  
  def __init__(self):
    self._drag_type = self._get_unique_drag_type()
    self._last_widget_dest_drag = None
  
  def setup_drag(
        self,
        widget,
        get_drag_data_func,
        drag_data_receive_func,
        get_drag_data_args=None,
        drag_data_receive_args=None,
        scrolled_window=None):
    """
    Enable dragging for the specified `widget`.
    
    `get_drag_data_func` is a function that returns data as a string describing
    the dragged widget.
    
    `drag_data_receive_func` is a function that processes the data returned by
    `get_drag_data_func`.
    
    `get_drag_data_args` and `drag_data_receive_args` are optional arguments for
    `get_drag_data_func` and `drag_data_receive_func`, respectively.
    
    The displayed `widget` is used as the drag icon. If the item box is wrapped
    in a scrolled window, specify the `scrolled_window` instance so that the
    default drag icon is assigned if `widget` is partially hidden inside the
    scrolled window.
    """
    if get_drag_data_args is None:
      get_drag_data_args = ()
    
    if drag_data_receive_args is None:
      drag_data_receive_args = ()
    
    widget.connect(
      'drag-data-get',
      self._on_widget_drag_data_get,
      get_drag_data_func,
      get_drag_data_args)
    widget.drag_source_set(
      gtk.gdk.BUTTON1_MASK, [(self._drag_type, 0, 0)], gtk.gdk.ACTION_MOVE)
    
    widget.connect(
      'drag-data-received',
      self._on_widget_drag_data_received,
      drag_data_receive_func,
      *drag_data_receive_args)
    widget.drag_dest_set(
      gtk.DEST_DEFAULT_ALL, [(self._drag_type, 0, 0)], gtk.gdk.ACTION_MOVE)
    
    widget.connect(
      'drag-begin', self._on_widget_drag_begin, scrolled_window)
    widget.connect('drag-motion', self._on_widget_drag_motion)
    widget.connect('drag-failed', self._on_widget_drag_failed)
  
  def _get_unique_drag_type(self):
    return str('{}_{}'.format(self.__class__.__name__, id(self)))
  
  def _on_widget_drag_data_get(
        self,
        widget,
        drag_context,
        selection_data,
        info,
        timestamp,
        get_drag_data_func,
        get_drag_data_args):
    selection_data.set(selection_data.target, 8, get_drag_data_func(*get_drag_data_args))
  
  def _on_widget_drag_data_received(
        self,
        widget,
        drag_context,
        drop_x,
        drop_y,
        selection_data,
        info,
        timestamp,
        drag_data_receive_func,
        *drag_data_receive_args):
    drag_data_receive_func(selection_data.data, *drag_data_receive_args)
  
  def _on_widget_drag_begin(self, widget, drag_context, scrolled_window):
    drag_icon_pixbuf = self._get_drag_icon_pixbuf(widget, scrolled_window)
    if drag_icon_pixbuf is not None:
      drag_context.set_icon_pixbuf(drag_icon_pixbuf, 0, 0)
  
  def _on_widget_drag_motion(
        self, widget, drag_context, drop_x, drop_y, timestamp):
    self._last_widget_dest_drag = widget
  
  def _on_widget_drag_failed(self, widget, drag_context, result):
    if self._last_widget_dest_drag is not None:
      self._last_widget_dest_drag.drag_unhighlight()
      self._last_widget_dest_drag = None
  
  def _get_drag_icon_pixbuf(self, widget, scrolled_window):
    if widget.get_window() is None:
      return
    
    if (scrolled_window is not None
        and self._are_items_partially_hidden_because_of_visible_horizontal_scrollbar(
              scrolled_window)):
      return None
    
    self._setup_widget_to_add_border_to_drag_icon(widget)
    
    while gtk.events_pending():
      gtk.main_iteration()
    
    widget_allocation = widget.get_allocation()
    
    pixbuf = gtk.gdk.Pixbuf(
      gtk.gdk.COLORSPACE_RGB,
      False,
      8,
      widget_allocation.width,
      widget_allocation.height)
    
    drag_icon_pixbuf = pixbuf.get_from_drawable(
      widget.get_window(),
      widget.get_colormap(),
      0,
      0,
      0,
      0,
      widget_allocation.width,
      widget_allocation.height)
    
    self._restore_widget_after_creating_drag_icon(widget)
    
    return drag_icon_pixbuf
  
  @staticmethod
  def _are_items_partially_hidden_because_of_visible_horizontal_scrollbar(
        scrolled_window):
    return (
      scrolled_window.get_hscrollbar() is not None
      and scrolled_window.get_hscrollbar().get_mapped())
  
  def _setup_widget_to_add_border_to_drag_icon(self, widget):
    self._remove_focus_outline(widget)
    self._add_border(widget)
  
  @staticmethod
  def _remove_focus_outline(widget):
    if widget.has_focus():
      widget.set_can_focus(False)
  
  @staticmethod
  def _add_border(widget):
    widget.drag_highlight()
  
  def _restore_widget_after_creating_drag_icon(self, widget):
    self._add_focus_outline(widget)
    self._remove_border(widget)
  
  @staticmethod
  def _add_focus_outline(widget):
    widget.set_can_focus(True)
  
  @staticmethod
  def _remove_border(widget):
    widget.drag_unhighlight()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        