root/tags/gajim-0.11.4/src/gtkspellmodule.c

Revision 5665, 6.2 kB (checked in by asterix, 3 years ago)

[gjc] gtkspellmodule upstream update. Fixes #1700

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