Wrap gnt_util_get_text_bound.
[python-gnt.git] / gnt.override
blob9c537c720a47b46e11e4b4f65f566e6d3fe77b7d
1 /**
2  * pygnt- Python bindings for the GNT toolkit.
3  * Copyright (C) 2007 Sadrul Habib Chowdhury <sadrul@pidgin.im>
4  *
5  *   gnt.override: Some generic wrappers.
6  *                 Support for specifying custom bindings.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library 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 GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301
21  * USA
22  */
24 headers
25 #include <Python.h>
26 #include "pygobject.h"
27 #include "gnt.h"
28 #include "gntbindable.h"
29 #include "gntwidget.h"
30 #include "gntbox.h"
31 #include "gntbutton.h"
32 #include "gntcheckbox.h"
33 #include "gntcolors.h"
34 #include "gntcombobox.h"
35 #include "gntentry.h"
36 #include "gntfilesel.h"
37 #include "gntkeys.h"
38 #include "gntlabel.h"
39 #include "gntline.h"
40 #include "gntmenu.h"
41 #include "gntmenuitem.h"
42 #include "gntmenuitemcheck.h"
43 #include "gntslider.h"
44 #include "gntstyle.h"
45 #include "gnttextview.h"
46 #include "gnttree.h"
47 #include "gntutils.h"
48 #include "gntwindow.h"
49 #include "gntwm.h"
50 #include "gntws.h"
51 #include "common.h"
53 #define gnt_menu_item_new   gnt_menuitem_new
54 #define gnt_menu_item_set_submenu  gnt_menuitem_set_submenu
55 #define gnt_menu_item_set_trigger  gnt_menuitem_set_trigger
56 #define gnt_menu_item_get_trigger  gnt_menuitem_get_trigger
58 #define gnt_menu_item_check_new   gnt_menuitem_check_new
59 #define gnt_menu_item_check_get_checked  gnt_menuitem_check_get_checked
60 #define gnt_menu_item_check_set_checked  gnt_menuitem_check_set_checked
62 /* This will keep track of the callbacks for different bindings for custom
63    pygnt widgets. */
64 GHashTable *bindings;
67 include
68  gntbindable.override
69  gntbox.override
70  gntfilesel.override
71  gntmenu.override
72  gnttree.override
73  gntwidget.override
75 modulename gnt
77 import gobject.GObject as PyGObject_Type
79 ignore-glob
80         *_get_gtype
82 define set_flag
83 static PyObject *
84 _wrap_set_flag(PyGObject *self, PyObject *args, PyObject *kwargs)
86         static char *kwlist[] = {"flags", NULL};
87         PyGObject *widget;
88         int flags;
90         if (!PyArg_ParseTuple(args, "O!i:gnt.set_flag", &PyGntWidget_Type, &widget,
91                                 &flags)) {
92                 return NULL;
93         }
95         GNT_WIDGET_SET_FLAGS(widget->obj, flags);
97         Py_INCREF(Py_None);
98         return Py_None;
101 define unset_flag
102 static PyObject *
103 _wrap_unset_flag(PyGObject *self, PyObject *args, PyObject *kwargs)
105         static char *kwlist[] = {"flags", NULL};
106         PyGObject *widget;
107         int flags;
109         if (!PyArg_ParseTuple(args, "O!i:gnt.unset_flag", &PyGntWidget_Type, &widget,
110                                 &flags)) {
111                 return NULL;
112         }
114         GNT_WIDGET_UNSET_FLAGS(widget->obj, flags);
116         Py_INCREF(Py_None);
117         return Py_None;
120 define screen_size noargs
121 static PyObject *
122 _wrap_screen_size(PyObject *self)
124         PyObject *list = PyList_New(0);
126         if (list == NULL)
127                 return NULL;
129         PyList_Append(list, PyInt_FromLong((long)getmaxx(stdscr)));
130         PyList_Append(list, PyInt_FromLong((long)getmaxy(stdscr)));
132         return list;
135 override gnt_register_action
136 static GHashTable *actions;
140 static PyObject *
141 _wrap_gnt_register_action(PyGObject *self, PyObject *args, PyObject *kwargs)
143         static char *kwlist[] = {"name", "callback", NULL};
144         PyGObject *callback;
145         GClosure *closure;
146         char *name;
148         if (!PyArg_ParseTuple(args, "sO:gnt.gnt_register_action", &name, &callback)) {
149                 return NULL;
150         }
152         if (!PyCallable_Check(callback)) {
153                 PyErr_SetString(PyExc_TypeError, "the callback must be callable ... doh!");
154                 return NULL;
155         }
157         gnt_register_action(name, callback->obj);
159         Py_INCREF(Py_None);
160         return Py_None;
163 define register_bindings
165 static gboolean
166 pygnt_binding_callback(GntBindable *bindable, GList *list)
168         PyObject *wrapper = pygobject_new(G_OBJECT(bindable));
169         if (list)
170                 PyObject_CallMethod(wrapper, list->data, "O", Py_None);
171         Py_DECREF(wrapper);
172         return TRUE;
175 static PyObject *
176 _wrap_register_bindings(PyObject *self, PyObject *args)
178         PyTypeObject *class;
179         int pos = 0;
180         PyObject *key, *value, *gbindings;
181         GntBindableClass *bindable;
183         if (!PyArg_ParseTuple(args, "O!:gnt.register_bindings",
184                                 &PyType_Type, &class)) {
185                 /* Make sure it's a GntBindableClass subclass */
186                 PyErr_SetString(PyExc_TypeError,
187                                 "argument must be a GntBindable subclass");
188                 return NULL;
189         }
191         gbindings = PyDict_GetItemString(class->tp_dict, "__gntbindings__");
192         if (!gbindings)
193                 goto end;
195         if (!PyDict_Check(gbindings)) {
196                 PyErr_SetString(PyExc_TypeError,
197                                 "__gntbindings__ attribute not a dict!");
198                 return NULL;
199         }
201         bindable = g_type_class_ref(pyg_type_from_object((PyObject *)class));
202         while (PyDict_Next(gbindings, &pos, &key, &value)) {
203                 const char *trigger, *callback, *name;
204                 GList *list = NULL;
205                 char *cbname;
207                 if (!PyString_Check(key)) {
208                         PyErr_SetString(PyExc_TypeError,
209                                         "__gntbindings__ keys must be strings");
210                         g_type_class_unref(bindable);
211                         return NULL;
212                 }
213                 name = PyString_AsString(key);
215                 if (!PyTuple_Check(value) ||
216                                 !PyArg_ParseTuple(value, "ss", &callback, &trigger)) {
217                         PyErr_SetString(PyExc_TypeError,
218                                         "__gntbindings__ values must be (callback, trigger) tupples");
219                         g_type_class_unref(bindable);
220                         return NULL;
221                 }
223                 gnt_bindable_class_register_action(bindable, name, pygnt_binding_callback,
224                                 trigger, g_strdup(callback), NULL);
225                 cbname = g_strdup_printf("%s::%s", class->tp_name, name);
226                 g_hash_table_replace(bindings, cbname, g_strdup(callback));
227                 /* Do not free cbname here. */
228         }
229         if (gbindings)
230                 PyDict_DelItemString(class->tp_dict, "__gntbindings__");
231         g_type_class_unref(bindable);
233 end:
234         Py_INCREF(Py_None);
235         return Py_None;
238 define show_menu
239 static PyObject *
240 _wrap_show_menu(PyGObject *self, PyObject *args, PyObject *kwargs)
242         PyGObject *menu;
244         if (!PyArg_ParseTuple(args, "O!:gnt.show_menu", &PyGntMenu_Type, &menu)) {
245                 return NULL;
246         }
248         gnt_screen_menu_show(GNT_MENU(menu->obj));
249         Py_INCREF(menu);
251         Py_INCREF(Py_None);
252         return Py_None;
255 ignore
256 gnt_util_get_text_bound
258 define get_text_bound
259 static PyObject *
260 _wrap_get_text_bound(PyGObject *self, PyObject *args)
262         PyObject *list;
263         int w = 0, h = 0;
264         char *text;
266     if (!PyArg_ParseTuple(args, "s:gnt.get_text_bound", &text)) {
267                 return NULL;
268         }
270         gnt_util_get_text_bound(text, &w, &h);
272         list = PyList_New(0);
273         PyList_Append(list, PyInt_FromLong((long)w));
274         PyList_Append(list, PyInt_FromLong((long)h));
275         return list;