2005-03-24 19:14:54 +05:30
|
|
|
# -*- python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000-2005 Donald N. Allingham
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
|
|
|
# $Id$
|
|
|
|
|
|
|
|
"""
|
|
|
|
This module provides management of all the drag and drop target information
|
|
|
|
required by gramps widgets.
|
|
|
|
|
|
|
|
Adding a new drag and drop target.
|
|
|
|
==================================
|
|
|
|
|
|
|
|
To add a new target: add a new _DdType to in _DdTargets.__init__ and
|
|
|
|
then add this new type to the list returned from either all_text_types()
|
|
|
|
and all_text_targets() or or all_gramps_targets() and all_gramps_types().
|
|
|
|
|
|
|
|
Usage
|
|
|
|
=====
|
|
|
|
|
|
|
|
The module defines a singleton instance of _DdTargets called DdTargets.
|
|
|
|
|
|
|
|
from DdTargets import DdTargets
|
|
|
|
|
|
|
|
drag_dest_set(gtk.DEST_DEFAULT_ALL,
|
|
|
|
DdTargets.all_targets(),
|
|
|
|
ACTION_COPY)
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2006-03-05 10:15:44 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# set up logging
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(".DdTargets")
|
|
|
|
|
|
|
|
|
2005-03-24 19:14:54 +05:30
|
|
|
class _DdType:
|
2009-08-12 15:31:28 +05:30
|
|
|
"""Represents the fields needed by a drag and drop target."""
|
2005-03-24 19:14:54 +05:30
|
|
|
|
|
|
|
_APP_ID_OFFSET = 40 # Starting value of app_ids
|
|
|
|
|
2007-07-12 09:54:44 +05:30
|
|
|
def __init__(self, container, drag_type, target_flags=0, app_id=None):
|
2005-03-24 19:14:54 +05:30
|
|
|
"""Create a new DdType:
|
|
|
|
|
|
|
|
drag_type: string holding the name of the type.
|
|
|
|
target_flags: int value that will be passed to drop target.
|
|
|
|
app_id: integer target id passed to drop target.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.drag_type = drag_type
|
|
|
|
self.target_flags = target_flags
|
|
|
|
self.app_id = app_id or self._calculate_id()
|
|
|
|
container.insert(self)
|
|
|
|
|
|
|
|
def _calculate_id(self):
|
2008-01-22 03:33:43 +05:30
|
|
|
"""Return the next available app_id."""
|
2005-03-24 19:14:54 +05:30
|
|
|
|
2005-04-09 08:41:03 +05:30
|
|
|
idval = _DdType._APP_ID_OFFSET
|
2005-03-24 19:14:54 +05:30
|
|
|
_DdType._APP_ID_OFFSET += 1
|
2005-04-09 08:41:03 +05:30
|
|
|
return idval
|
2005-03-24 19:14:54 +05:30
|
|
|
|
|
|
|
def target(self):
|
2009-08-12 15:31:28 +05:30
|
|
|
"""
|
|
|
|
Return the full target information in the format required by the
|
2008-01-22 03:33:43 +05:30
|
|
|
Gtk functions.
|
|
|
|
|
|
|
|
"""
|
2007-07-12 09:54:44 +05:30
|
|
|
return (self.drag_type, self.target_flags, self.app_id)
|
2005-03-24 19:14:54 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _DdTargets(object):
|
|
|
|
"""A single class that manages all the drag and drop targets."""
|
|
|
|
|
|
|
|
_instance = None # Singleton instance
|
|
|
|
|
|
|
|
def __new__(cls):
|
|
|
|
"""Ensure that we never have more than one instance."""
|
|
|
|
|
2006-03-23 04:33:57 +05:30
|
|
|
if _DdTargets._instance:
|
2005-03-24 19:14:54 +05:30
|
|
|
return _DdTargets._instance
|
|
|
|
_DdTargets._instance = object.__new__(cls)
|
|
|
|
return _DdTargets._instance
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Set up the drag and drop targets."""
|
|
|
|
|
|
|
|
self._type_map = {}
|
|
|
|
self._app_id_map = {}
|
|
|
|
|
2009-08-12 15:31:28 +05:30
|
|
|
self.ADDRESS = _DdType(self, 'paddr')
|
|
|
|
self.ATTRIBUTE = _DdType(self, 'pattr')
|
|
|
|
self.DATA = _DdType(self, 'data_tuple')
|
|
|
|
self.EVENT = _DdType(self, 'pevent')
|
|
|
|
self.EVENTREF = _DdType(self, 'eventref')
|
|
|
|
self.FAMILY_ATTRIBUTE = _DdType(self, 'fattr')
|
|
|
|
self.FAMILY_EVENT = _DdType(self, 'fevent')
|
|
|
|
self.LOCATION = _DdType(self, 'location')
|
|
|
|
self.MEDIAOBJ = _DdType(self, 'mediaobj')
|
|
|
|
self.MEDIAREF = _DdType(self, 'mediaref')
|
|
|
|
self.NAME = _DdType(self, 'name')
|
|
|
|
self.NOTE_LINK = _DdType(self, 'note-link')
|
|
|
|
self.PLACE_LINK = _DdType(self, 'place-link')
|
|
|
|
self.REPO_LINK = _DdType(self, 'repo-link')
|
|
|
|
self.REPOREF = _DdType(self, 'reporef')
|
|
|
|
self.PERSON_LINK = _DdType(self, 'person-link')
|
2010-07-29 13:27:43 +05:30
|
|
|
self.LINK_LIST = _DdType(self, 'link-list')
|
|
|
|
self.RAW_LIST = _DdType(self, 'raw-list')
|
2010-07-30 06:48:49 +05:30
|
|
|
self.HANDLE_LIST = _DdType(self, 'handle-list')
|
2009-08-12 15:31:28 +05:30
|
|
|
self.PERSONREF = _DdType(self, 'personref')
|
|
|
|
self.SOURCEREF = _DdType(self, 'srcref')
|
|
|
|
self.SOURCE_LINK = _DdType(self, 'source-link')
|
|
|
|
self.URL = _DdType(self, 'url')
|
2005-04-06 14:58:54 +05:30
|
|
|
|
|
|
|
# List of all types that are used between
|
|
|
|
# gramps widgets but should not be exported
|
|
|
|
# to non gramps widgets.
|
2007-03-28 21:57:45 +05:30
|
|
|
self._all_gramps_types = [
|
2007-07-12 09:54:44 +05:30
|
|
|
self.ADDRESS,
|
2009-08-12 15:31:28 +05:30
|
|
|
self.ATTRIBUTE,
|
|
|
|
self.EVENT,
|
2007-07-12 09:54:44 +05:30
|
|
|
self.EVENTREF,
|
2009-08-12 15:31:28 +05:30
|
|
|
self.LOCATION,
|
2007-07-12 09:54:44 +05:30
|
|
|
self.MEDIAOBJ,
|
|
|
|
self.MEDIAREF,
|
2009-08-12 15:31:28 +05:30
|
|
|
self.NAME,
|
|
|
|
self.NOTE_LINK,
|
2007-07-12 09:54:44 +05:30
|
|
|
self.PLACE_LINK,
|
|
|
|
self.PERSON_LINK,
|
2010-07-29 13:27:43 +05:30
|
|
|
self.LINK_LIST,
|
|
|
|
self.RAW_LIST,
|
2010-07-30 06:48:49 +05:30
|
|
|
self.HANDLE_LIST,
|
2007-07-12 09:54:44 +05:30
|
|
|
self.PERSONREF,
|
2009-08-12 15:31:28 +05:30
|
|
|
self.REPO_LINK,
|
|
|
|
self.REPOREF,
|
|
|
|
self.SOURCEREF,
|
|
|
|
self.SOURCE_LINK,
|
|
|
|
self.URL,
|
2007-03-28 21:57:45 +05:30
|
|
|
]
|
2005-03-26 02:35:09 +05:30
|
|
|
|
2007-07-12 09:54:44 +05:30
|
|
|
self.CHILD = _DdType(self, 'child')
|
|
|
|
self.SPOUSE = _DdType(self, 'spouse')
|
|
|
|
self.TEXT_MIME = _DdType(self, 'text/plain', 0, 0)
|
2009-08-12 15:31:28 +05:30
|
|
|
self.TEXT = _DdType(self, 'TEXT', 0, 1)
|
2007-07-12 09:54:44 +05:30
|
|
|
self.STRING = _DdType(self, 'STRING', 0, 2)
|
|
|
|
self.COMPOUND_TEXT = _DdType(self, 'COMPOUND_TEXT', 0, 3)
|
|
|
|
self.UTF8_STRING = _DdType(self, 'UTF8_STRING', 0, 4)
|
|
|
|
self.URI_LIST = _DdType(self, 'text/uri-list', 0, 5)
|
|
|
|
self.APP_ROOT = _DdType(self, 'application/x-rootwin-drop', 0, 6)
|
2005-03-24 19:14:54 +05:30
|
|
|
|
2009-08-12 15:31:28 +05:30
|
|
|
# List of all the text types. These are types
|
2005-04-06 14:58:54 +05:30
|
|
|
# that can be interpreted as text.
|
2007-07-12 09:54:44 +05:30
|
|
|
self._all_text_types = (self.UTF8_STRING,
|
|
|
|
self.TEXT,
|
|
|
|
self.TEXT_MIME,
|
|
|
|
self.STRING,
|
2005-12-06 12:08:09 +05:30
|
|
|
self.COMPOUND_TEXT)
|
2005-04-06 14:58:54 +05:30
|
|
|
|
2007-07-12 09:54:44 +05:30
|
|
|
def insert(self, dd_type):
|
2009-08-12 15:31:28 +05:30
|
|
|
"""
|
|
|
|
Add a target to the lookup lists. These lists are
|
|
|
|
used purely for performance reasons.
|
|
|
|
"""
|
2005-03-24 19:14:54 +05:30
|
|
|
|
|
|
|
self._type_map[dd_type.drag_type] = dd_type
|
|
|
|
self._app_id_map[dd_type.app_id] = dd_type
|
|
|
|
|
2007-07-12 09:54:44 +05:30
|
|
|
def get_dd_type_from_type_name(self, type_name):
|
|
|
|
return self._type_map.get(type_name, None)
|
2005-03-24 19:14:54 +05:30
|
|
|
|
2007-07-12 09:54:44 +05:30
|
|
|
def get_dd_type_from_app_id(self, app_id):
|
|
|
|
return self._app_id_map.get(app_id, None)
|
2005-03-24 19:14:54 +05:30
|
|
|
|
2007-07-12 09:54:44 +05:30
|
|
|
def is_text_type(self, type_name):
|
2005-03-24 19:14:54 +05:30
|
|
|
return type_name in self.all_text_types()
|
|
|
|
|
2005-03-31 18:46:20 +05:30
|
|
|
def all_text(self):
|
2005-04-06 14:58:54 +05:30
|
|
|
return self._all_text_types
|
2005-03-31 18:46:20 +05:30
|
|
|
|
2005-03-24 19:14:54 +05:30
|
|
|
def all_text_types(self):
|
2009-08-12 15:31:28 +05:30
|
|
|
"""
|
|
|
|
Return a list of all the type names that could be
|
|
|
|
used as the type of a string.
|
|
|
|
"""
|
2005-03-24 19:14:54 +05:30
|
|
|
|
2005-04-06 14:58:54 +05:30
|
|
|
return tuple([t.drag_type for t in self._all_text_types])
|
2005-03-24 19:14:54 +05:30
|
|
|
|
2007-07-12 09:54:44 +05:30
|
|
|
def is_gramps_type(self, type_name):
|
2005-03-24 19:14:54 +05:30
|
|
|
return type_name in self.all_gramps_types()
|
|
|
|
|
|
|
|
def all_gramps_types(self):
|
2009-08-12 15:31:28 +05:30
|
|
|
"""Return a list of all the type names that are internal to gramps."""
|
2005-04-06 14:58:54 +05:30
|
|
|
|
|
|
|
return tuple([t.drag_type for t in self._all_gramps_types])
|
2005-03-24 19:14:54 +05:30
|
|
|
|
|
|
|
def all_text_targets(self):
|
2009-08-12 15:31:28 +05:30
|
|
|
"""Return a list of all the targets that could be used for text."""
|
2005-03-24 19:14:54 +05:30
|
|
|
|
2005-04-06 14:58:54 +05:30
|
|
|
return tuple([t.target() for t in self._all_text_types])
|
2005-03-24 19:14:54 +05:30
|
|
|
|
|
|
|
|
|
|
|
def all_gramps_targets(self):
|
2009-08-12 15:31:28 +05:30
|
|
|
"""Return a list off the internal gramps targets."""
|
2005-04-06 14:58:54 +05:30
|
|
|
|
|
|
|
return tuple([t.target() for t in self._all_gramps_types])
|
2005-03-24 19:14:54 +05:30
|
|
|
|
|
|
|
def all_targets(self):
|
2009-08-12 15:31:28 +05:30
|
|
|
"""Return a list of all the known targets."""
|
2005-03-24 19:14:54 +05:30
|
|
|
return self.all_gramps_targets() + self.all_text_targets()
|
|
|
|
|
|
|
|
# Create the singleton instance.
|
|
|
|
|
|
|
|
DdTargets = _DdTargets()
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Below here is test code.
|
|
|
|
#
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
print repr(DdTargets.all_text_types())
|
|
|
|
print repr(DdTargets.URL)
|
|
|
|
print DdTargets.is_gramps_type('pevent')
|