| 1 | #include <Python.h> |
|---|
| 2 | #include <gtk/gtk.h> |
|---|
| 3 | #include <gtkspell/gtkspell.h> |
|---|
| 4 | #include "pygobject.h" |
|---|
| 5 | |
|---|
| 6 | typedef struct { |
|---|
| 7 | PyObject_HEAD |
|---|
| 8 | GtkSpell *spell; |
|---|
| 9 | } gtkspell_SpellObject; |
|---|
| 10 | |
|---|
| 11 | extern PyTypeObject gtkspell_SpellType; |
|---|
| 12 | |
|---|
| 13 | static PyTypeObject *_PyGtkTextView_Type; |
|---|
| 14 | #define PyGtkTextView_Type (*_PyGtkTextView_Type) |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | static PyObject * |
|---|
| 18 | _wrap_gtkspell_new_attach (PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 19 | { |
|---|
| 20 | gtkspell_SpellObject *self; |
|---|
| 21 | PyObject *pytextview; |
|---|
| 22 | GtkTextView *textview; |
|---|
| 23 | GtkSpell *spell; |
|---|
| 24 | char *language = NULL; |
|---|
| 25 | GError *error = NULL; |
|---|
| 26 | |
|---|
| 27 | if (!PyArg_ParseTuple(args, "O!|z:gtkspell.Spell.__new__", |
|---|
| 28 | &PyGtkTextView_Type, &pytextview, &language)) |
|---|
| 29 | return NULL; |
|---|
| 30 | |
|---|
| 31 | textview = GTK_TEXT_VIEW(((PyGObject *)pytextview)->obj); |
|---|
| 32 | spell = gtkspell_new_attach(textview, language, &error); |
|---|
| 33 | |
|---|
| 34 | if (pyg_error_check(&error)) |
|---|
| 35 | return NULL; |
|---|
| 36 | if (!spell) { |
|---|
| 37 | PyErr_SetString(PyExc_RuntimeError, "unable to create and attach a Spell object"); |
|---|
| 38 | return NULL; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | self = (gtkspell_SpellObject *)type->tp_alloc(type, 0); |
|---|
| 42 | self->spell = spell; |
|---|
| 43 | return (PyObject *)self; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | static PyObject * |
|---|
| 47 | _wrap_gtkspell_set_language (gtkspell_SpellObject *self, PyObject *args, PyObject *kwds) |
|---|
| 48 | { |
|---|
| 49 | gchar *lang; |
|---|
| 50 | gboolean result; |
|---|
| 51 | |
|---|
| 52 | char *argnames[] = {"language", NULL}; |
|---|
| 53 | PyArg_ParseTupleAndKeywords (args, kwds, "s", argnames, &lang); |
|---|
| 54 | |
|---|
| 55 | result = gtkspell_set_language (self->spell, lang, NULL); |
|---|
| 56 | |
|---|
| 57 | if (!result) { |
|---|
| 58 | /*there are no specific errors in GtkSpell yet*/ |
|---|
| 59 | PyErr_SetString(PyExc_RuntimeError, "Error setting language"); |
|---|
| 60 | return NULL; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | Py_INCREF(Py_None); |
|---|
| 64 | return Py_None; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | static PyObject * |
|---|
| 68 | _wrap_gtkspell_recheck_all (gtkspell_SpellObject *self) |
|---|
| 69 | { |
|---|
| 70 | gtkspell_recheck_all ((GtkSpell *)self->spell); |
|---|
| 71 | |
|---|
| 72 | Py_INCREF(Py_None); |
|---|
| 73 | return Py_None; |
|---|
| 74 | |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | static PyObject * |
|---|
| 78 | _wrap_gtkspell_get_from_text_view (PyObject *junk, PyObject *args, PyObject *kwds) |
|---|
| 79 | { |
|---|
| 80 | PyObject *pytextview; |
|---|
| 81 | GtkTextView *textview; |
|---|
| 82 | gtkspell_SpellObject *self; |
|---|
| 83 | |
|---|
| 84 | char *argnames[] = {"textview", NULL}; |
|---|
| 85 | PyArg_ParseTupleAndKeywords (args, kwds, "O", argnames, &pytextview); |
|---|
| 86 | |
|---|
| 87 | textview = GTK_TEXT_VIEW(((PyGObject *)pytextview)->obj); |
|---|
| 88 | |
|---|
| 89 | self = (gtkspell_SpellObject *)PyType_GenericAlloc((PyTypeObject *)>kspell_SpellType, 1); |
|---|
| 90 | if (self != NULL) { |
|---|
| 91 | self->spell = gtkspell_get_from_text_view(textview); |
|---|
| 92 | if (self->spell == NULL) { |
|---|
| 93 | Py_DECREF(self); |
|---|
| 94 | return NULL; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | return (PyObject *)self; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | static PyObject * |
|---|
| 101 | _wrap_gtkspell_detach (gtkspell_SpellObject *self) |
|---|
| 102 | { |
|---|
| 103 | gtkspell_detach(self->spell); |
|---|
| 104 | self->spell = NULL; |
|---|
| 105 | Py_INCREF(Py_None); |
|---|
| 106 | return Py_None; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | static PyMethodDef gtkspell_methods[] = { |
|---|
| 111 | {"set_language", (PyCFunction)_wrap_gtkspell_set_language, |
|---|
| 112 | METH_KEYWORDS, "Set the language"}, |
|---|
| 113 | {"recheck_all", (PyCFunction)_wrap_gtkspell_recheck_all, |
|---|
| 114 | METH_NOARGS, "Recheck the spelling in the entire buffer"}, |
|---|
| 115 | {"detach", (PyCFunction)_wrap_gtkspell_detach, |
|---|
| 116 | METH_NOARGS, "Detaches a Spell object from a TextView"}, |
|---|
| 117 | { NULL, NULL, 0 } |
|---|
| 118 | }; |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | PyTypeObject gtkspell_SpellType = { |
|---|
| 122 | PyObject_HEAD_INIT(NULL) |
|---|
| 123 | 0, /*ob_size*/ |
|---|
| 124 | "gtkspell.Spell", /*tp_name*/ |
|---|
| 125 | sizeof(gtkspell_SpellObject), /*tp_basicsize*/ |
|---|
| 126 | 0, /*tp_itemsize*/ |
|---|
| 127 | 0, /*tp_dealloc*/ |
|---|
| 128 | 0, /*tp_print*/ |
|---|
| 129 | 0, /*tp_getattr*/ |
|---|
| 130 | 0, /*tp_setattr*/ |
|---|
| 131 | 0, /*tp_compare*/ |
|---|
| 132 | 0, /*tp_repr*/ |
|---|
| 133 | 0, /*tp_as_number*/ |
|---|
| 134 | 0, /*tp_as_sequence*/ |
|---|
| 135 | 0, /*tp_as_mapping*/ |
|---|
| 136 | 0, /*tp_hash */ |
|---|
| 137 | 0, /*tp_call*/ |
|---|
| 138 | 0, /*tp_str*/ |
|---|
| 139 | 0, /*tp_getattro*/ |
|---|
| 140 | 0, /*tp_setattro*/ |
|---|
| 141 | 0, /*tp_as_buffer*/ |
|---|
| 142 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
|---|
| 143 | "GtkSpell object", /* tp_doc */ |
|---|
| 144 | 0, /* tp_traverse */ |
|---|
| 145 | 0, /* tp_clear */ |
|---|
| 146 | 0, /* tp_richcompare */ |
|---|
| 147 | 0, /* tp_weaklistoffset */ |
|---|
| 148 | 0, /* tp_iter */ |
|---|
| 149 | 0, /* tp_iternext */ |
|---|
| 150 | gtkspell_methods, /* tp_methods */ |
|---|
| 151 | 0, /* tp_members */ |
|---|
| 152 | 0, /* tp_getset */ |
|---|
| 153 | 0, /* tp_base */ |
|---|
| 154 | 0, /* tp_dict */ |
|---|
| 155 | 0, /* tp_descr_get */ |
|---|
| 156 | 0, /* tp_descr_set */ |
|---|
| 157 | 0, /* tp_dictoffset */ |
|---|
| 158 | 0, /* tp_init */ |
|---|
| 159 | 0, /* tp_alloc */ |
|---|
| 160 | _wrap_gtkspell_new_attach, /* tp_new */ |
|---|
| 161 | }; |
|---|
| 162 | |
|---|
| 163 | static PyMethodDef gtkspell_functions[] = { |
|---|
| 164 | {"get_from_text_view", (PyCFunction)_wrap_gtkspell_get_from_text_view, |
|---|
| 165 | METH_KEYWORDS, "Retrieves the Spell object attach"}, |
|---|
| 166 | { NULL, NULL, 0, NULL } |
|---|
| 167 | }; |
|---|
| 168 | |
|---|
| 169 | DL_EXPORT(void) |
|---|
| 170 | initgtkspell(void) |
|---|
| 171 | { |
|---|
| 172 | PyObject *m, *module; |
|---|
| 173 | |
|---|
| 174 | init_pygobject(); |
|---|
| 175 | |
|---|
| 176 | if ((module = PyImport_ImportModule("gtk")) != NULL) { |
|---|
| 177 | PyObject *moddict = PyModule_GetDict(module); |
|---|
| 178 | |
|---|
| 179 | _PyGtkTextView_Type = (PyTypeObject *)PyDict_GetItemString(moddict, "TextView"); |
|---|
| 180 | if (_PyGtkTextView_Type == NULL) { |
|---|
| 181 | PyErr_SetString(PyExc_ImportError, |
|---|
| 182 | "cannot import name TextView from gtk"); |
|---|
| 183 | return; |
|---|
| 184 | } |
|---|
| 185 | } else { |
|---|
| 186 | PyErr_SetString(PyExc_ImportError, |
|---|
| 187 | "could not import gtk"); |
|---|
| 188 | return; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | m = Py_InitModule3 ("gtkspell", gtkspell_functions, "GtkSpell bindings"); |
|---|
| 193 | |
|---|
| 194 | if (PyType_Ready(>kspell_SpellType) < 0) |
|---|
| 195 | return; |
|---|
| 196 | |
|---|
| 197 | Py_INCREF(>kspell_SpellType); |
|---|
| 198 | PyModule_AddObject(m, "Spell", (PyObject *)>kspell_SpellType); |
|---|
| 199 | |
|---|
| 200 | if (PyErr_Occurred ()) { |
|---|
| 201 | PyErr_Print(); |
|---|
| 202 | Py_FatalError ("can't initialise module gtkspell"); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|