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:
|
|
|
|
"""Represents the fields needed by a drag and drop traget."""
|
|
|
|
|
|
|
|
_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):
|
|
|
|
"""return the next available app_id."""
|
|
|
|
|
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):
|
|
|
|
"""return the full target information in the format
|
|
|
|
required by the 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 = {}
|
|
|
|
|
2007-07-12 09:54:44 +05:30
|
|
|
self.URL = _DdType(self, 'url')
|
|
|
|
self.EVENT = _DdType(self, 'pevent')
|
|
|
|
self.EVENTREF = _DdType(self, 'eventref')
|
|
|
|
self.ATTRIBUTE = _DdType(self, 'pattr')
|
|
|
|
self.ADDRESS = _DdType(self, 'paddr')
|
|
|
|
self.LOCATION = _DdType(self, 'location')
|
|
|
|
self.SOURCEREF = _DdType(self, 'srcref')
|
|
|
|
self.REPOREF = _DdType(self, 'reporef')
|
|
|
|
self.REPO_LINK = _DdType(self, 'repo-link')
|
|
|
|
self.PLACE_LINK = _DdType(self, 'place-link')
|
|
|
|
self.NAME = _DdType(self, 'name')
|
|
|
|
self.MEDIAOBJ = _DdType(self, 'mediaobj')
|
|
|
|
self.MEDIAREF = _DdType(self, 'mediaref')
|
|
|
|
self.DATA = _DdType(self, 'data_tuple')
|
|
|
|
self.NOTE_LINK = _DdType(self, 'note-link')
|
|
|
|
|
|
|
|
self.PERSON_LINK = _DdType(self, 'person-link')
|
|
|
|
self.PERSON_LINK_LIST = _DdType(self, 'person-link-list')
|
|
|
|
self.PERSONREF = _DdType(self, 'personref')
|
|
|
|
|
|
|
|
self.SOURCE_LINK = _DdType(self, 'source-link')
|
|
|
|
|
|
|
|
self.FAMILY_EVENT = _DdType(self, 'fevent')
|
|
|
|
self.FAMILY_ATTRIBUTE = _DdType(self, 'fattr')
|
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.URL,
|
|
|
|
self.EVENT,
|
|
|
|
self.ATTRIBUTE,
|
|
|
|
self.ADDRESS,
|
|
|
|
self.LOCATION,
|
|
|
|
self.SOURCEREF,
|
|
|
|
self.EVENTREF,
|
|
|
|
self.NAME,
|
|
|
|
self.REPOREF,
|
|
|
|
self.MEDIAOBJ,
|
|
|
|
self.MEDIAREF,
|
|
|
|
self.REPO_LINK,
|
|
|
|
self.PLACE_LINK,
|
|
|
|
self.SOURCE_LINK,
|
|
|
|
self.PERSON_LINK,
|
|
|
|
self.PERSON_LINK_LIST,
|
|
|
|
self.PERSONREF,
|
|
|
|
self.NOTE_LINK,
|
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 = _DdType(self, 'TEXT', 0, 1)
|
|
|
|
self.TEXT_MIME = _DdType(self, 'text/plain', 0, 0)
|
|
|
|
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
|
|
|
|
2005-04-06 14:58:54 +05:30
|
|
|
# List of all the test types. These are types
|
|
|
|
# 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):
|
2005-03-24 19:14:54 +05:30
|
|
|
"""Add a target to the lookup lists. These lists are
|
|
|
|
used purely for performance reasons."""
|
|
|
|
|
|
|
|
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):
|
|
|
|
"""return a list of all the type names that could be
|
|
|
|
used as the type of a string."""
|
|
|
|
|
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):
|
|
|
|
"""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):
|
|
|
|
"""return a list of all the targets that could be used
|
|
|
|
for text."""
|
|
|
|
|
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):
|
|
|
|
"""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):
|
|
|
|
"""return a list of all the known targets."""
|
|
|
|
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')
|