3866 3976 3977: Two 'spell' entries; remove Enchant dependency; Gramps does spellcheck on/off menu, gtkspell does the different languages. Cleaned-up code to reflect fact that spellcheck can only be on or off (see 3866).

svn: r17669
This commit is contained in:
Tim G L Lyons 2011-06-02 22:57:32 +00:00
parent b86b1ae8ae
commit 9a497b9264
2 changed files with 44 additions and 48 deletions

View File

@ -82,62 +82,57 @@ import config
class Spell(object):
"""Attach a gtkspell instance to the passed TextView instance.
"""
_spellcheck_options = {'off': _('Off')}
if HAVE_GTKSPELL:
_spellcheck_options['on'] = _('On')
_spellcheck_options = {False: _('Off'), True: _('On') }
def __init__(self, textview):
self.textview = textview
if HAVE_GTKSPELL and config.get('behavior.spellcheck'):
self.spellcheck = 'on'
self.spellcheck = True
else:
self.spellcheck = 'off'
self.spellcheck = False
self._active_spellcheck = 'off'
self._previous_spellcheck = False
self.__real_set_active_spellcheck(self.spellcheck)
# Private
def __real_set_active_spellcheck(self, spellcheck_code):
def __real_set_active_spellcheck(self, next_spellcheck):
"""Set active spellcheck by its code."""
if self._active_spellcheck == 'off':
if spellcheck_code == 'off':
return
else:
try:
gtkspell_spell = gtkspell.Spell(self.textview)
self._active_spellcheck = spellcheck_code
except:
import traceback
print traceback.print_exc()
# attaching the spellchecker will fail if
# the language does not exist
# and presumably if there is no dictionary
pass
else:
if spellcheck_code == 'on':
return
else:
if self._previous_spellcheck == next_spellcheck:
return
elif self._previous_spellcheck == False and next_spellcheck == True:
try:
gtkspell_spell = gtkspell.Spell(self.textview)
self._previous_spellcheck = next_spellcheck
except:
import traceback
print traceback.print_exc()
# attaching the spellchecker will fail if
# the language does not exist
# and presumably if there is no dictionary
pass
elif self._previous_spellcheck == True and next_spellcheck == False:
gtkspell_spell = gtkspell.get_from_text_view(self.textview)
gtkspell_spell.detach()
self._active_spellcheck = spellcheck_code
self._previous_spellcheck = next_spellcheck
else:
assert False, "spellcheck flags are not boolean -- shouldn't get here"
# Public API
def get_all_spellchecks(self):
def get_spellcheck_options(self):
"""Get the list of installed spellcheck names."""
return self._spellcheck_options.values()
def set_active_spellcheck(self, spellcheck):
def set_spellcheck_state(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)
return
def get_active_spellcheck(self):
def get_spellcheck_state(self):
"""Get the name of the active spellcheck."""
return self._spellcheck_options[self._active_spellcheck]
return self._spellcheck_options[self._previous_spellcheck]

View File

@ -57,7 +57,7 @@ from gui.widgets.undoablestyledbuffer import UndoableStyledBuffer
from gui.widgets.valueaction import ValueAction
from gui.widgets.toolcomboentry import ToolComboEntry
from gui.widgets.springseparator import SpringSeparatorAction
from Spell import Spell
from Spell import Spell, HAVE_GTKSPELL
from GrampsDisplay import url as display_url
import config
from constfunc import has_display
@ -364,10 +364,11 @@ class StyledTextEditor(gtk.TextView):
"""
# spell checker submenu
spell_menu = gtk.MenuItem(_('Spellcheck'))
spell_menu.set_submenu(self._create_spell_menu())
spell_menu.show_all()
menu.prepend(spell_menu)
if HAVE_GTKSPELL:
spell_menu = gtk.MenuItem(_('Spellcheck'))
spell_menu.set_submenu(self._create_spell_menu())
spell_menu.show_all()
menu.prepend(spell_menu)
search_menu = gtk.MenuItem(_("Search selection on web"))
search_menu.connect('activate', self.search_web)
@ -548,23 +549,23 @@ class StyledTextEditor(gtk.TextView):
"[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+", MAIL)
def _create_spell_menu(self):
"""Create a menu with all the installed spellchecks.
"""Create a menu with the spellcheck option Off and On.
It is called each time the popup menu is opened. Each spellcheck
forms a radio menu item, and the selected spellcheck is set as active.
It is called each time the popup menu is opened. Each option
forms a radio menu item, and the selected option is set as active.
@returns: menu containing all the installed spellchecks.
@returns: menu containing the spellcheck options.
@returntype: gtk.Menu
"""
active_spellcheck = self.spellcheck.get_active_spellcheck()
spellcheck_state = self.spellcheck.get_spellcheck_state()
menu = gtk.Menu()
group = None
for lang in self.spellcheck.get_all_spellchecks():
menuitem = gtk.RadioMenuItem(group, lang)
menuitem.set_active(lang == active_spellcheck)
menuitem.connect('activate', self._spell_change_cb, lang)
for spellcheck in self.spellcheck.get_spellcheck_options():
menuitem = gtk.RadioMenuItem(group, spellcheck)
menuitem.set_active(spellcheck == spellcheck_state)
menuitem.connect('activate', self._spell_change_cb, spellcheck)
menu.append(menuitem)
if group is None:
@ -706,8 +707,8 @@ class StyledTextEditor(gtk.TextView):
self._internal_style_change = False
def _spell_change_cb(self, menuitem, spellcheck):
"""Set spell checker spellcheck according to user selection."""
self.spellcheck.set_active_spellcheck(spellcheck)
"""Set spell checker option according to user selection."""
self.spellcheck.set_spellcheck_state(spellcheck)
def _open_url_cb(self, menuitem, url, flavor):
"""Open the URL in a browser."""