| 1 | /* |
|---|
| 2 | * src/gtkspellmodule.c |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2005 Nikos Kouremenos <kourem AT gmail.com> |
|---|
| 5 | * Copyright (C) 2006 Yann Leboulanger <asterix AT lagaule.org> |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Gajim. |
|---|
| 8 | * |
|---|
| 9 | * Gajim is free software; you can redistribute it and/or modify |
|---|
| 10 | * it under the terms of the GNU General Public License as published |
|---|
| 11 | * by the Free Software Foundation; version 3 only. |
|---|
| 12 | * |
|---|
| 13 | * Gajim is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with Gajim. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <Python.h> |
|---|
| 24 | #include <gtk/gtk.h> |
|---|
| 25 | #include <gtkspell/gtkspell.h> |
|---|
| 26 | #include "pygobject.h" |
|---|
| 27 | |
|---|
| 28 | typedef struct { |
|---|
| 29 | PyObject_HEAD |
|---|
| 30 | GtkSpell *spell; |
|---|
| 31 | } gtkspell_SpellObject; |
|---|
| 32 | |
|---|
| 33 | extern PyTypeObject gtkspell_SpellType; |
|---|
| 34 | |
|---|
| 35 | static PyTypeObject *_PyGtkTextView_Type; |
|---|
| 36 | #define PyGtkTextView_Type (*_PyGtkTextView_Type) |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | static PyObject * |
|---|
| 40 | _wrap_gtkspell_new_attach (PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 41 | { |
|---|
| 42 | gtkspell_SpellObject *self; |
|---|
| 43 | PyObject *pytextview; |
|---|
| 44 | GtkTextView *textview; |
|---|
| 45 | GtkSpell *spell; |
|---|
| 46 | char *language = NULL; |
|---|
| 47 | GError *error = NULL; |
|---|
| 48 | |
|---|
| 49 | if (!PyArg_ParseTuple(args, "O!|z:gtkspell.Spell.__new__", |
|---|
| 50 | &PyGtkTextView_Type, &pytextview, &language)) |
|---|
| 51 | return NULL; |
|---|
| 52 | |
|---|
| 53 | textview = GTK_TEXT_VIEW(((PyGObject *)pytextview)->obj); |
|---|
| 54 | spell = gtkspell_new_attach(textview, language, &error); |
|---|
| 55 | |
|---|
| 56 | if (pyg_error_check(&error)) |
|---|
| 57 | return NULL; |
|---|
| 58 | if (!spell) { |
|---|
| 59 | PyErr_SetString(PyExc_RuntimeError, "unable to create and attach a Spell object"); |
|---|
| 60 | return NULL; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | self = (gtkspell_SpellObject *)type->tp_alloc(type, 0); |
|---|
| 64 | self->spell = spell; |
|---|
| 65 | return (PyObject *)self; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | static PyObject * |
|---|
| 69 | _wrap_gtkspell_set_language (gtkspell_SpellObject *self, PyObject *args, PyObject *kwds) |
|---|
| 70 | { |
|---|
| 71 | gchar *lang = NULL; |
|---|
| 72 | gboolean result; |
|---|
| 73 | char *argnames[] = {"language", NULL}; |
|---|
| 74 | |
|---|
| 75 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "z", argnames, &lang)) |
|---|
| 76 | return NULL; |
|---|
| 77 | |
|---|
| 78 | result = gtkspell_set_language (self->spell, lang, NULL); |
|---|
| 79 | |
|---|
| 80 | if (!result) { |
|---|
| 81 | /*there are no specific errors in GtkSpell yet*/ |
|---|
| 82 | PyErr_SetString(PyExc_RuntimeError, "Error setting language"); |
|---|
| 83 | return NULL; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | Py_INCREF(Py_None); |
|---|
| 87 | return Py_None; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | static PyObject * |
|---|
| 91 | _wrap_gtkspell_recheck_all (gtkspell_SpellObject *self) |
|---|
| 92 | { |
|---|
| 93 | gtkspell_recheck_all ((GtkSpell *)self->spell); |
|---|
| 94 | |
|---|
| 95 | Py_INCREF(Py_None); |
|---|
| 96 | return Py_None; |
|---|
| 97 | |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | static PyObject * |
|---|
| 101 | _wrap_gtkspell_get_from_text_view (PyObject *junk, PyObject *args, PyObject *kwds) |
|---|
| 102 | { |
|---|
| 103 | PyObject *pytextview; |
|---|
| 104 | GtkTextView *textview; |
|---|
| 105 | gtkspell_SpellObject *self; |
|---|
| 106 | char *argnames[] = {"textview", NULL}; |
|---|
| 107 | |
|---|
| 108 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "O", argnames, &pytextview)) |
|---|
| 109 | return NULL; |
|---|
| 110 | |
|---|
| 111 | textview = GTK_TEXT_VIEW(((PyGObject *)pytextview)->obj); |
|---|
| 112 | |
|---|
| 113 | self = (gtkspell_SpellObject *)PyType_GenericAlloc((PyTypeObject *)>kspell_SpellType, 1); |
|---|
| 114 | if (self != NULL) { |
|---|
| 115 | self->spell = gtkspell_get_from_text_view(textview); |
|---|
| 116 | if (self->spell == NULL) { |
|---|
| 117 | Py_DECREF(self); |
|---|
| 118 | return NULL; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | return (PyObject *)self; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | static PyObject * |
|---|
| 125 | _wrap_gtkspell_detach (gtkspell_SpellObject *self) |
|---|
| 126 | { |
|---|
| 127 | gtkspell_detach(self->spell); |
|---|
| 128 | self->spell = NULL; |
|---|
| 129 | Py_INCREF(Py_None); |
|---|
| 130 | return Py_None; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | static PyMethodDef gtkspell_methods[] = { |
|---|
| 135 | {"set_language", (PyCFunction)_wrap_gtkspell_set_language, |
|---|
| 136 | METH_KEYWORDS, "Set the language"}, |
|---|
| 137 | {"recheck_all", (PyCFunction)_wrap_gtkspell_recheck_all, |
|---|
| 138 | METH_NOARGS, "Recheck the spelling in the entire buffer"}, |
|---|
| 139 | {"detach", (PyCFunction)_wrap_gtkspell_detach, |
|---|
| 140 | METH_NOARGS, "Detaches a Spell object from a TextView"}, |
|---|
| 141 | { NULL, NULL, 0 } |
|---|
| 142 | }; |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | PyTypeObject gtkspell_SpellType = { |
|---|
| 146 | PyObject_HEAD_INIT(NULL) |
|---|
| 147 | 0, /*ob_size*/ |
|---|
| 148 | "gtkspell.Spell", /*tp_name*/ |
|---|
| 149 | sizeof(gtkspell_SpellObject), /*tp_basicsize*/ |
|---|
| 150 | 0, /*tp_itemsize*/ |
|---|
| 151 | 0, /*tp_dealloc*/ |
|---|
| 152 | 0, /*tp_print*/ |
|---|
| 153 | 0, /*tp_getattr*/ |
|---|
| 154 | 0, /*tp_setattr*/ |
|---|
| 155 | 0, /*tp_compare*/ |
|---|
| 156 | 0, /*tp_repr*/ |
|---|
| 157 | 0, /*tp_as_number*/ |
|---|
| 158 | 0, /*tp_as_sequence*/ |
|---|
| 159 | 0, /*tp_as_mapping*/ |
|---|
| 160 | 0, /*tp_hash */ |
|---|
| 161 | 0, /*tp_call*/ |
|---|
| 162 | 0, /*tp_str*/ |
|---|
| 163 | 0, /*tp_getattro*/ |
|---|
| 164 | 0, /*tp_setattro*/ |
|---|
| 165 | 0, /*tp_as_buffer*/ |
|---|
| 166 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
|---|
| 167 | "GtkSpell object", /* tp_doc */ |
|---|
| 168 | 0, /* tp_traverse */ |
|---|
| 169 | 0, /* tp_clear */ |
|---|
| 170 | 0, /* tp_richcompare */ |
|---|
| 171 | 0, /* tp_weaklistoffset */ |
|---|
| 172 | 0, /* tp_iter */ |
|---|
| 173 | 0, /* tp_iternext */ |
|---|
| 174 | gtkspell_methods, /* tp_methods */ |
|---|
| 175 | 0, /* tp_members */ |
|---|
| 176 | 0, /* tp_getset */ |
|---|
| 177 | 0, /* tp_base */ |
|---|
| 178 | 0, /* tp_dict */ |
|---|
| 179 | 0, /* tp_descr_get */ |
|---|
| 180 | 0, /* tp_descr_set */ |
|---|
| 181 | 0, /* tp_dictoffset */ |
|---|
| 182 | 0, /* tp_init */ |
|---|
| 183 | 0, /* tp_alloc */ |
|---|
| 184 | _wrap_gtkspell_new_attach, /* tp_new */ |
|---|
| 185 | }; |
|---|
| 186 | |
|---|
| 187 | static PyMethodDef gtkspell_functions[] = { |
|---|
| 188 | {"get_from_text_view", (PyCFunction)_wrap_gtkspell_get_from_text_view, |
|---|
| 189 | METH_KEYWORDS, "Retrieves the Spell object attach"}, |
|---|
| 190 | { NULL, NULL, 0, NULL } |
|---|
| 191 | }; |
|---|
| 192 | |
|---|
| 193 | DL_EXPORT(void) |
|---|
| 194 | initgtkspell(void) |
|---|
| 195 | { |
|---|
| 196 | PyObject *m, *module; |
|---|
| 197 | |
|---|
| 198 | init_pygobject(); |
|---|
| 199 | |
|---|
| 200 | if ((module = PyImport_ImportModule("gtk")) != NULL) { |
|---|
| 201 | PyObject *moddict = PyModule_GetDict(module); |
|---|
| 202 | |
|---|
| 203 | _PyGtkTextView_Type = (PyTypeObject *)PyDict_GetItemString(moddict, "TextView"); |
|---|
| 204 | if (_PyGtkTextView_Type == NULL) { |
|---|
| 205 | PyErr_SetString(PyExc_ImportError, |
|---|
| 206 | "cannot import name TextView from gtk"); |
|---|
| 207 | return; |
|---|
| 208 | } |
|---|
| 209 | } else { |
|---|
| 210 | PyErr_SetString(PyExc_ImportError, |
|---|
| 211 | "could not import gtk"); |
|---|
| 212 | return; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | m = Py_InitModule3 ("gtkspell", gtkspell_functions, "GtkSpell bindings"); |
|---|
| 217 | |
|---|
| 218 | if (PyType_Ready(>kspell_SpellType) < 0) |
|---|
| 219 | return; |
|---|
| 220 | |
|---|
| 221 | Py_INCREF(>kspell_SpellType); |
|---|
| 222 | PyModule_AddObject(m, "Spell", (PyObject *)>kspell_SpellType); |
|---|
| 223 | |
|---|
| 224 | if (PyErr_Occurred ()) { |
|---|
| 225 | PyErr_Print(); |
|---|
| 226 | Py_FatalError ("can't initialise module gtkspell"); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|