use introspection instead of ctype to use gtkspell
This commit is contained in:
parent
f69ade130c
commit
512ffc7c0a
|
@ -1,6 +1,7 @@
|
||||||
## src/gtkspell.py
|
## src/gtkspell.py
|
||||||
##
|
##
|
||||||
## (C) 2008 Thorsten P. 'dGhvcnN0ZW5wIEFUIHltYWlsIGNvbQ==\n'.decode("base64")
|
## (C) 2008 Thorsten P. 'dGhvcnN0ZW5wIEFUIHltYWlsIGNvbQ==\n'.decode("base64")
|
||||||
|
## (C) 2015 Yann Leboulanger <asterix AT lagaule.org>
|
||||||
##
|
##
|
||||||
## This file is part of Gajim.
|
## This file is part of Gajim.
|
||||||
##
|
##
|
||||||
|
@ -16,40 +17,8 @@
|
||||||
## You should have received a copy of the GNU General Public License
|
## You should have received a copy of the GNU General Public License
|
||||||
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import ctypes
|
|
||||||
import ctypes.util
|
|
||||||
|
|
||||||
from gi.repository import Gtk
|
from gi.repository import Gtk
|
||||||
|
from gi.repository import GtkSpell
|
||||||
|
|
||||||
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]
|
|
||||||
|
|
||||||
|
|
||||||
def ensure_attached(func):
|
def ensure_attached(func):
|
||||||
def f(self, *args, **kwargs):
|
def f(self, *args, **kwargs):
|
||||||
|
@ -65,16 +34,18 @@ class Spell(object):
|
||||||
def __init__(self, textview, language=None, create=True):
|
def __init__(self, textview, language=None, create=True):
|
||||||
if not isinstance(textview, Gtk.TextView):
|
if not isinstance(textview, Gtk.TextView):
|
||||||
raise TypeError("Textview must be derived from Gtk.TextView")
|
raise TypeError("Textview must be derived from Gtk.TextView")
|
||||||
tv = PyGObject.from_address(id(textview)).obj
|
spell = GtkSpell.Checker.get_from_text_view(textview)
|
||||||
spell = libgtkspell.gtkspell_get_from_text_view(tv)
|
|
||||||
if create:
|
if create:
|
||||||
if spell:
|
if spell:
|
||||||
raise RuntimeError("Textview has already a Spell obj attached")
|
raise RuntimeError("Textview has already a Spell obj attached")
|
||||||
self.spell = libgtkspell.gtkspell_new_attach(tv, language.encode(
|
self.spell = GtkSpell.Checker.new()
|
||||||
'utf-8'), None)
|
|
||||||
if not self.spell:
|
if not self.spell:
|
||||||
raise OSError("Unable to attach spell object. "
|
raise OSError("Unable to create spell object.")
|
||||||
"Language: '%s'" % str(language))
|
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:
|
else:
|
||||||
if spell:
|
if spell:
|
||||||
self.spell = spell
|
self.spell = spell
|
||||||
|
@ -83,17 +54,16 @@ class Spell(object):
|
||||||
|
|
||||||
@ensure_attached
|
@ensure_attached
|
||||||
def set_language(self, language):
|
def set_language(self, language):
|
||||||
if libgtkspell.gtkspell_set_language(self.spell, language.encode(
|
if not self.spell.set_language(language):
|
||||||
'utf-8'), None) == 0:
|
raise OSError("Unable to set language: '%s'" % language)
|
||||||
raise OSError("Unable to set language '%s'" % str(language))
|
|
||||||
|
|
||||||
@ensure_attached
|
@ensure_attached
|
||||||
def recheck_all(self):
|
def recheck_all(self):
|
||||||
libgtkspell.gtkspell_recheck_all(self.spell)
|
self.spell.recheck_all()
|
||||||
|
|
||||||
@ensure_attached
|
@ensure_attached
|
||||||
def detach(self):
|
def detach(self):
|
||||||
libgtkspell.gtkspell_detach(self.spell)
|
self.spell.detach()
|
||||||
self.spell = None
|
self.spell = None
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue