2005-08-18 11:28:28 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-05-02 02:41:26 +05:30
|
|
|
# Copyright (C) 2005-2006 Donald N. Allingham
|
2005-08-18 11:28:28 +05:30
|
|
|
#
|
|
|
|
# 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$
|
|
|
|
|
|
|
|
"""
|
|
|
|
Provide an interface to the gtkspell interface. This requires
|
|
|
|
python-gnome-extras package. If the gtkspell package is not
|
|
|
|
present, we default to no spell checking.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2007-07-17 19:10:29 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Python classes
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2010-01-18 10:12:17 +05:30
|
|
|
from gen.ggettext import gettext as _
|
2007-07-17 19:10:29 +05:30
|
|
|
import locale
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2007-07-17 19:10:29 +05:30
|
|
|
#-------------------------------------------------------------------------
|
2005-12-06 12:08:09 +05:30
|
|
|
#
|
2007-07-17 19:10:29 +05:30
|
|
|
# Set up logging
|
2005-12-06 12:08:09 +05:30
|
|
|
#
|
2007-07-17 19:10:29 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import logging
|
2007-07-23 02:33:52 +05:30
|
|
|
LOG = logging.getLogger(".Spell")
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2007-07-17 19:10:29 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK libraries
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import gtk
|
2007-09-08 09:50:59 +05:30
|
|
|
|
2005-12-06 12:08:09 +05:30
|
|
|
try:
|
|
|
|
import gtkspell
|
2007-07-17 19:10:29 +05:30
|
|
|
HAVE_GTKSPELL = True
|
|
|
|
except ImportError:
|
|
|
|
HAVE_GTKSPELL = False
|
2009-03-05 17:40:47 +05:30
|
|
|
|
2007-07-17 19:10:29 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS classes
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-10-08 06:42:51 +05:30
|
|
|
import config
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2007-07-17 19:10:29 +05:30
|
|
|
#-------------------------------------------------------------------------
|
2005-12-06 12:08:09 +05:30
|
|
|
#
|
2007-07-17 19:10:29 +05:30
|
|
|
# Constants
|
2005-12-06 12:08:09 +05:30
|
|
|
#
|
2007-07-17 19:10:29 +05:30
|
|
|
#-------------------------------------------------------------------------
|
2007-07-18 01:53:48 +05:30
|
|
|
|
2009-05-21 22:49:50 +05:30
|
|
|
class Spell(object):
|
2007-07-17 19:10:29 +05:30
|
|
|
"""Attach a gtkspell instance to the passed TextView instance.
|
|
|
|
"""
|
2011-05-26 13:35:04 +05:30
|
|
|
_spellcheck_options = {'off': _('Off')}
|
|
|
|
|
|
|
|
if HAVE_GTKSPELL:
|
|
|
|
_spellcheck_options['on'] = _('On')
|
2009-03-05 17:40:47 +05:30
|
|
|
|
2007-07-17 19:10:29 +05:30
|
|
|
def __init__(self, textview):
|
|
|
|
self.textview = textview
|
|
|
|
|
2011-05-26 13:35:04 +05:30
|
|
|
if HAVE_GTKSPELL and config.get('behavior.spellcheck'):
|
|
|
|
self.spellcheck = 'on'
|
2007-07-17 19:10:29 +05:30
|
|
|
else:
|
2011-05-26 13:35:04 +05:30
|
|
|
self.spellcheck = 'off'
|
2007-07-17 19:10:29 +05:30
|
|
|
|
2011-05-26 13:35:04 +05:30
|
|
|
self._active_spellcheck = 'off'
|
|
|
|
self.__real_set_active_spellcheck(self.spellcheck)
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2007-07-27 15:54:09 +05:30
|
|
|
# Private
|
|
|
|
|
2011-05-26 13:35:04 +05:30
|
|
|
def __real_set_active_spellcheck(self, spellcheck_code):
|
|
|
|
"""Set active spellcheck by its code."""
|
|
|
|
if self._active_spellcheck == 'off':
|
|
|
|
if spellcheck_code == 'off':
|
2007-07-17 19:10:29 +05:30
|
|
|
return
|
|
|
|
else:
|
2011-05-26 13:35:04 +05:30
|
|
|
try:
|
|
|
|
gtkspell_spell = gtkspell.Spell(self.textview)
|
|
|
|
self._active_spellcheck = spellcheck_code
|
|
|
|
except:
|
|
|
|
# attaching the spellchecker will fail if
|
|
|
|
# the language does not exist
|
|
|
|
# and presumably if there is no dictionary
|
|
|
|
pass
|
2007-07-17 19:10:29 +05:30
|
|
|
else:
|
2011-05-26 13:35:04 +05:30
|
|
|
if spellcheck_code == 'on':
|
2007-07-17 19:10:29 +05:30
|
|
|
return
|
2011-05-26 13:35:04 +05:30
|
|
|
else:
|
|
|
|
gtkspell_spell = gtkspell.get_from_text_view(self.textview)
|
|
|
|
gtkspell_spell.detach()
|
|
|
|
self._active_spellcheck = spellcheck_code
|
|
|
|
|
2007-07-27 15:54:09 +05:30
|
|
|
# Public API
|
|
|
|
|
2011-05-26 13:35:04 +05:30
|
|
|
def get_all_spellchecks(self):
|
|
|
|
"""Get the list of installed spellcheck names."""
|
|
|
|
return self._spellcheck_options.values()
|
2007-07-17 19:10:29 +05:30
|
|
|
|
2011-05-26 13:35:04 +05:30
|
|
|
def set_active_spellcheck(self, spellcheck):
|
|
|
|
"""Set active spellcheck by it's name."""
|
|
|
|
for code, name in self._spellcheck_options.items():
|
|
|
|
if name == spellcheck:
|
|
|
|
self.__real_set_active_spellcheck(code)
|
2007-07-17 19:10:29 +05:30
|
|
|
return
|
|
|
|
|
2011-05-26 13:35:04 +05:30
|
|
|
def get_active_spellcheck(self):
|
|
|
|
"""Get the name of the active spellcheck."""
|
|
|
|
return self._spellcheck_options[self._active_spellcheck]
|
2007-09-08 09:50:59 +05:30
|
|
|
|