use introspection instead of ctype to use gtkspell

This commit is contained in:
Yann Leboulanger 2015-08-16 14:22:04 +02:00
parent f69ade130c
commit 512ffc7c0a
1 changed files with 14 additions and 44 deletions

View File

@ -1,6 +1,7 @@
## src/gtkspell.py
##
## (C) 2008 Thorsten P. 'dGhvcnN0ZW5wIEFUIHltYWlsIGNvbQ==\n'.decode("base64")
## (C) 2015 Yann Leboulanger <asterix AT lagaule.org>
##
## This file is part of Gajim.
##
@ -16,40 +17,8 @@
## You should have received a copy of the GNU General Public License
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
import ctypes
import ctypes.util
from gi.repository import Gtk
gboolean = ctypes.c_int
gchar_p = ctypes.c_char_p
gerror_p = ctypes.c_void_p
gobject_p = ctypes.c_void_p
gtkspell_p = ctypes.c_void_p
gtktextview_p = ctypes.c_void_p
class PyGObject(ctypes.Structure):
_fields_ = [
("PyObject_HEAD", ctypes.c_byte * object.__basicsize__),
("obj", gobject_p)
]
libgtkspell_path = ctypes.util.find_library("gtkspell")
if libgtkspell_path == None:
raise ImportError("libgtkspell not found")
libgtkspell = ctypes.cdll.LoadLibrary(libgtkspell_path)
libgtkspell.gtkspell_new_attach.restype = gtkspell_p
libgtkspell.gtkspell_new_attach.argtypes = [gtktextview_p, gchar_p, gerror_p]
libgtkspell.gtkspell_set_language.restype = gboolean
libgtkspell.gtkspell_set_language.argtypes = [gtkspell_p, gchar_p, gerror_p]
libgtkspell.gtkspell_recheck_all.argtypes = [gtkspell_p]
libgtkspell.gtkspell_get_from_text_view.restype = gtkspell_p
libgtkspell.gtkspell_get_from_text_view.argtypes = [gtktextview_p]
libgtkspell.gtkspell_detach.argtypes = [gtkspell_p]
from gi.repository import GtkSpell
def ensure_attached(func):
def f(self, *args, **kwargs):
@ -65,16 +34,18 @@ class Spell(object):
def __init__(self, textview, language=None, create=True):
if not isinstance(textview, Gtk.TextView):
raise TypeError("Textview must be derived from Gtk.TextView")
tv = PyGObject.from_address(id(textview)).obj
spell = libgtkspell.gtkspell_get_from_text_view(tv)
spell = GtkSpell.Checker.get_from_text_view(textview)
if create:
if spell:
raise RuntimeError("Textview has already a Spell obj attached")
self.spell = libgtkspell.gtkspell_new_attach(tv, language.encode(
'utf-8'), None)
self.spell = GtkSpell.Checker.new()
if not self.spell:
raise OSError("Unable to attach spell object. "
"Language: '%s'" % str(language))
raise OSError("Unable to create spell object.")
if not self.spell.attach(textview):
raise OSError("Unable to attach spell object.")
if not self.spell.set_language(language):
raise OSError("Unable to set language: '%s'" % language)
else:
if spell:
self.spell = spell
@ -83,17 +54,16 @@ class Spell(object):
@ensure_attached
def set_language(self, language):
if libgtkspell.gtkspell_set_language(self.spell, language.encode(
'utf-8'), None) == 0:
raise OSError("Unable to set language '%s'" % str(language))
if not self.spell.set_language(language):
raise OSError("Unable to set language: '%s'" % language)
@ensure_attached
def recheck_all(self):
libgtkspell.gtkspell_recheck_all(self.spell)
self.spell.recheck_all()
@ensure_attached
def detach(self):
libgtkspell.gtkspell_detach(self.spell)
self.spell.detach()
self.spell = None