Do some little improvements on the per-second update.
[python-gnt.git] / gnt.override
blob97ca6e82777166c5628564fbcff9435ccb649b48
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
74  gntwindow.override
76 modulename gnt
78 import gobject.GObject as PyGObject_Type
80 ignore-glob
81         *_get_gtype
83 define set_flag
84 static PyObject *
85 _wrap_set_flag(PyGObject *self, PyObject *args, PyObject *kwargs)
87         static char *kwlist[] = {"flags", NULL};
88         PyGObject *widget;
89         int flags;
91         if (!PyArg_ParseTuple(args, "O!i:gnt.set_flag", &PyGntWidget_Type, &widget,
92                                 &flags)) {
93                 return NULL;
94         }
96         GNT_WIDGET_SET_FLAGS(widget->obj, flags);
98         Py_INCREF(Py_None);
99         return Py_None;
102 define unset_flag
103 static PyObject *
104 _wrap_unset_flag(PyGObject *self, PyObject *args, PyObject *kwargs)
106         static char *kwlist[] = {"flags", NULL};
107         PyGObject *widget;
108         int flags;
110         if (!PyArg_ParseTuple(args, "O!i:gnt.unset_flag", &PyGntWidget_Type, &widget,
111                                 &flags)) {
112                 return NULL;
113         }
115         GNT_WIDGET_UNSET_FLAGS(widget->obj, flags);
117         Py_INCREF(Py_None);
118         return Py_None;
121 define screen_size noargs
122 static PyObject *
123 _wrap_screen_size(PyObject *self)
125         PyObject *list = PyList_New(0);
127         if (list == NULL)
128                 return NULL;
130         PyList_Append(list, PyInt_FromLong((long)getmaxx(stdscr)));
131         PyList_Append(list, PyInt_FromLong((long)getmaxy(stdscr)));
133         return list;
136 override gnt_register_action
137 static GHashTable *actions;
141 static PyObject *
142 _wrap_gnt_register_action(PyGObject *self, PyObject *args, PyObject *kwargs)
144         static char *kwlist[] = {"name", "callback", NULL};
145         PyGObject *callback;
146         GClosure *closure;
147         char *name;
149         if (!PyArg_ParseTuple(args, "sO:gnt.gnt_register_action", &name, &callback)) {
150                 return NULL;
151         }
153         if (!PyCallable_Check(callback)) {
154                 PyErr_SetString(PyExc_TypeError, "the callback must be callable ... doh!");
155                 return NULL;
156         }
158         gnt_register_action(name, callback->obj);
160         Py_INCREF(Py_None);
161         return Py_None;
164 define register_bindings
166 static gboolean
167 pygnt_binding_callback(GntBindable *bindable, GList *list)
169         PyObject *wrapper = pygobject_new(G_OBJECT(bindable));
170         if (list)
171                 PyObject_CallMethod(wrapper, list->data, "O", Py_None);
172         Py_DECREF(wrapper);
173         return TRUE;
176 static PyObject *
177 _wrap_register_bindings(PyObject *self, PyObject *args)
179         PyTypeObject *class;
180         int pos = 0;
181         PyObject *key, *value, *gbindings;
182         GntBindableClass *bindable;
184         if (!PyArg_ParseTuple(args, "O!:gnt.register_bindings",
185                                 &PyType_Type, &class)) {
186                 /* Make sure it's a GntBindableClass subclass */
187                 PyErr_SetString(PyExc_TypeError,
188                                 "argument must be a GntBindable subclass");
189                 return NULL;
190         }
192         gbindings = PyDict_GetItemString(class->tp_dict, "__gntbindings__");
193         if (!gbindings)
194                 goto end;
196         if (!PyDict_Check(gbindings)) {
197                 PyErr_SetString(PyExc_TypeError,
198                                 "__gntbindings__ attribute not a dict!");
199                 return NULL;
200         }
202         bindable = g_type_class_ref(pyg_type_from_object((PyObject *)class));
203         while (PyDict_Next(gbindings, &pos, &key, &value)) {
204                 const char *trigger, *callback, *name;
205                 GList *list = NULL;
206                 char *cbname;
208                 if (!PyString_Check(key)) {
209                         PyErr_SetString(PyExc_TypeError,
210                                         "__gntbindings__ keys must be strings");
211                         g_type_class_unref(bindable);
212                         return NULL;
213                 }
214                 name = PyString_AsString(key);
216                 if (!PyTuple_Check(value) ||
217                                 !PyArg_ParseTuple(value, "ss", &callback, &trigger)) {
218                         PyErr_SetString(PyExc_TypeError,
219                                         "__gntbindings__ values must be (callback, trigger) tupples");
220                         g_type_class_unref(bindable);
221                         return NULL;
222                 }
224                 gnt_bindable_class_register_action(bindable, name, pygnt_binding_callback,
225                                 trigger, g_strdup(callback), NULL);
226                 cbname = g_strdup_printf("%s::%s", class->tp_name, name);
227                 g_hash_table_replace(bindings, cbname, g_strdup(callback));
228                 /* Do not free cbname here. */
229         }
230         if (gbindings)
231                 PyDict_DelItemString(class->tp_dict, "__gntbindings__");
232         g_type_class_unref(bindable);
234 end:
235         Py_INCREF(Py_None);
236         return Py_None;
239 define show_menu
240 static PyObject *
241 _wrap_show_menu(PyGObject *self, PyObject *args, PyObject *kwargs)
243         PyGObject *menu;
245         if (!PyArg_ParseTuple(args, "O!:gnt.show_menu", &PyGntMenu_Type, &menu)) {
246                 return NULL;
247         }
249         gnt_screen_menu_show(GNT_MENU(menu->obj));
250         Py_INCREF(menu);
252         Py_INCREF(Py_None);
253         return Py_None;
256 ignore
257 gnt_util_get_text_bound
259 define get_text_bound
260 static PyObject *
261 _wrap_get_text_bound(PyGObject *self, PyObject *args)
263         PyObject *list;
264         int w = 0, h = 0;
265         char *text;
267     if (!PyArg_ParseTuple(args, "s:gnt.get_text_bound", &text)) {
268                 return NULL;
269         }
271         gnt_util_get_text_bound(text, &w, &h);
273         list = PyList_New(0);
274         PyList_Append(list, PyInt_FromLong((long)w));
275         PyList_Append(list, PyInt_FromLong((long)h));
276         return list;