lighter import
[gajim.git] / src / gtkspell.py
blobaa1adaec7d5845d3e8ff616cdf811c350ad6cae1
1 ## src/gtkspell.py
2 ##
3 ## (C) 2008 Thorsten P. 'dGhvcnN0ZW5wIEFUIHltYWlsIGNvbQ==\n'.decode("base64")
4 ##
5 ## This file is part of Gajim.
6 ##
7 ## Gajim is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published
9 ## by the Free Software Foundation; version 3 only.
11 ## Gajim is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
19 import ctypes
20 import ctypes.util
22 import gtk
25 gboolean = ctypes.c_int
26 gchar_p = ctypes.c_char_p
27 gerror_p = ctypes.c_void_p
28 gobject_p = ctypes.c_void_p
29 gtkspell_p = ctypes.c_void_p
30 gtktextview_p = ctypes.c_void_p
33 class PyGObject(ctypes.Structure):
34 _fields_ = [
35 ("PyObject_HEAD", ctypes.c_byte * object.__basicsize__),
36 ("obj", gobject_p)
40 libgtkspell_path = ctypes.util.find_library("gtkspell")
41 if libgtkspell_path == None:
42 raise ImportError("libgtkspell not found")
43 libgtkspell = ctypes.cdll.LoadLibrary(libgtkspell_path)
44 libgtkspell.gtkspell_new_attach.restype = gtkspell_p
45 libgtkspell.gtkspell_new_attach.argtypes = [gtktextview_p, gchar_p, gerror_p]
46 libgtkspell.gtkspell_set_language.restype = gboolean
47 libgtkspell.gtkspell_set_language.argtypes = [gtkspell_p, gchar_p, gerror_p]
48 libgtkspell.gtkspell_recheck_all.argtypes = [gtkspell_p]
49 libgtkspell.gtkspell_get_from_text_view.restype = gtkspell_p
50 libgtkspell.gtkspell_get_from_text_view.argtypes = [gtktextview_p]
51 libgtkspell.gtkspell_detach.argtypes = [gtkspell_p]
54 def ensure_attached(func):
55 def f(self, *args, **kwargs):
56 if self.spell:
57 func(self, *args, **kwargs)
58 else:
59 raise RuntimeError("Spell object is already detached")
60 return f
63 class Spell(object):
65 def __init__(self, textview, language=None, create=True):
66 if not isinstance(textview, gtk.TextView):
67 raise TypeError("Textview must be derived from gtk.TextView")
68 tv = PyGObject.from_address(id(textview)).obj
69 spell = libgtkspell.gtkspell_get_from_text_view(tv)
70 if create:
71 if spell:
72 raise RuntimeError("Textview has already a Spell obj attached")
73 self.spell = libgtkspell.gtkspell_new_attach(tv, language, None)
74 if not self.spell:
75 raise OSError("Unable to attach spell object. "
76 "Language: '%s'" % str(language))
77 else:
78 if spell:
79 self.spell = spell
80 else:
81 raise RuntimeError("Textview has no Spell object attached")
83 @ensure_attached
84 def set_language(self, language):
85 if libgtkspell.gtkspell_set_language(self.spell, language, None) == 0:
86 raise OSError("Unable to set language '%s'" % str(language))
88 @ensure_attached
89 def recheck_all(self):
90 libgtkspell.gtkspell_recheck_all(self.spell)
92 @ensure_attached
93 def detach(self):
94 libgtkspell.gtkspell_detach(self.spell)
95 self.spell = None
98 def get_from_text_view(textview):
99 return Spell(textview, create=False)