Colorful details window.
[python-gnt.git] / gnt.override
blobe7dc8cd1fd4a7d7bbffbf8e5472458fbe2c7cb03
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_get_submenu       gnt_menuitem_get_submenu
56 #define gnt_menu_item_set_trigger  gnt_menuitem_set_trigger
57 #define gnt_menu_item_get_trigger  gnt_menuitem_get_trigger
58 #define gnt_menu_item_set_id    gnt_menuitem_set_id
59 #define gnt_menu_item_get_id    gnt_menuitem_get_id
60 #define gnt_menu_item_activate  gnt_menuitem_activate
62 #define gnt_menu_item_check_new   gnt_menuitem_check_new
63 #define gnt_menu_item_check_get_checked  gnt_menuitem_check_get_checked
64 #define gnt_menu_item_check_set_checked  gnt_menuitem_check_set_checked
66 /* This will keep track of the callbacks for different bindings for custom
67    pygnt widgets. */
68 GHashTable *bindings;
71 include
72  gntbindable.override
73  gntbox.override
74  gntcombobox.override
75  gntfilesel.override
76  gntmenu.override
77  gnttree.override
78  gntwidget.override
79  gntwindow.override
81 modulename gnt
83 import gobject.GObject as PyGObject_Type
85 ignore-glob
86         *_get_gtype
88 define set_flag
89 static PyObject *
90 _wrap_set_flag(PyGObject *self, PyObject *args, PyObject *kwargs)
92         static char *kwlist[] = {"flags", NULL};
93         PyGObject *widget;
94         int flags;
96         if (!PyArg_ParseTuple(args, "O!i:gnt.set_flag", &PyGntWidget_Type, &widget,
97                                 &flags)) {
98                 return NULL;
99         }
101         GNT_WIDGET_SET_FLAGS(widget->obj, flags);
103         Py_INCREF(Py_None);
104         return Py_None;
107 define unset_flag
108 static PyObject *
109 _wrap_unset_flag(PyGObject *self, PyObject *args, PyObject *kwargs)
111         static char *kwlist[] = {"flags", NULL};
112         PyGObject *widget;
113         int flags;
115         if (!PyArg_ParseTuple(args, "O!i:gnt.unset_flag", &PyGntWidget_Type, &widget,
116                                 &flags)) {
117                 return NULL;
118         }
120         GNT_WIDGET_UNSET_FLAGS(widget->obj, flags);
122         Py_INCREF(Py_None);
123         return Py_None;
126 define screen_size noargs
127 static PyObject *
128 _wrap_screen_size(PyObject *self)
130         PyObject *list = PyList_New(0);
132         if (list == NULL)
133                 return NULL;
135         PyList_Append(list, PyInt_FromLong((long)getmaxx(stdscr)));
136         PyList_Append(list, PyInt_FromLong((long)getmaxy(stdscr)));
138         return list;
141 override gnt_register_action
142 static GHashTable *actions;
146 static PyObject *
147 _wrap_gnt_register_action(PyGObject *self, PyObject *args, PyObject *kwargs)
149         static char *kwlist[] = {"name", "callback", NULL};
150         PyGObject *callback;
151         GClosure *closure;
152         char *name;
154         if (!PyArg_ParseTuple(args, "sO:gnt.gnt_register_action", &name, &callback)) {
155                 return NULL;
156         }
158         if (!PyCallable_Check(callback)) {
159                 PyErr_SetString(PyExc_TypeError, "the callback must be callable ... doh!");
160                 return NULL;
161         }
163         gnt_register_action(name, callback->obj);
165         Py_INCREF(Py_None);
166         return Py_None;
169 define register_bindings
171 static gboolean
172 pygnt_binding_callback(GntBindable *bindable, GList *list)
174         PyGILState_STATE state;
175         state = pyg_gil_state_ensure();
176         PyObject *wrapper = pygobject_new(G_OBJECT(bindable));
177         if (list)
178                 PyObject_CallMethod(wrapper, list->data, "O", Py_None);
179         Py_DECREF(wrapper);
180         pyg_gil_state_release(state);
181         return TRUE;
184 static PyObject *
185 _wrap_register_bindings(PyObject *self, PyObject *args)
187         PyTypeObject *class;
188         int pos = 0;
189         PyObject *key, *value, *gbindings;
190         GntBindableClass *bindable;
192         if (!PyArg_ParseTuple(args, "O!:gnt.register_bindings",
193                                 &PyType_Type, &class)) {
194                 /* Make sure it's a GntBindableClass subclass */
195                 PyErr_SetString(PyExc_TypeError,
196                                 "argument must be a GntBindable subclass");
197                 return NULL;
198         }
200         gbindings = PyDict_GetItemString(class->tp_dict, "__gntbindings__");
201         if (!gbindings)
202                 goto end;
204         if (!PyDict_Check(gbindings)) {
205                 PyErr_SetString(PyExc_TypeError,
206                                 "__gntbindings__ attribute not a dict!");
207                 return NULL;
208         }
210         bindable = g_type_class_ref(pyg_type_from_object((PyObject *)class));
211         while (PyDict_Next(gbindings, &pos, &key, &value)) {
212                 const char *trigger, *callback, *name;
213                 GList *list = NULL;
214                 char *cbname;
216                 if (!PyString_Check(key)) {
217                         PyErr_SetString(PyExc_TypeError,
218                                         "__gntbindings__ keys must be strings");
219                         g_type_class_unref(bindable);
220                         return NULL;
221                 }
222                 name = PyString_AsString(key);
224                 if (!PyTuple_Check(value) ||
225                                 !PyArg_ParseTuple(value, "ss", &callback, &trigger)) {
226                         PyErr_SetString(PyExc_TypeError,
227                                         "__gntbindings__ values must be (callback, trigger) tupples");
228                         g_type_class_unref(bindable);
229                         return NULL;
230                 }
232                 gnt_bindable_class_register_action(bindable, name, pygnt_binding_callback,
233                                 trigger, g_strdup(callback), NULL);
234                 cbname = g_strdup_printf("%s::%s", class->tp_name, name);
235                 g_hash_table_replace(bindings, cbname, g_strdup(callback));
236                 /* Do not free cbname here. */
237         }
238         if (gbindings)
239                 PyDict_DelItemString(class->tp_dict, "__gntbindings__");
240         g_type_class_unref(bindable);
242 end:
243         Py_INCREF(Py_None);
244         return Py_None;
247 define show_menu
248 static PyObject *
249 _wrap_show_menu(PyGObject *self, PyObject *args, PyObject *kwargs)
251         PyGObject *menu;
253         if (!PyArg_ParseTuple(args, "O!:gnt.show_menu", &PyGntMenu_Type, &menu)) {
254                 return NULL;
255         }
257         gnt_screen_menu_show(GNT_MENU(menu->obj));
258         Py_INCREF(menu);
260         Py_INCREF(Py_None);
261         return Py_None;
264 ignore
265 gnt_util_get_text_bound
267 define get_text_bound
268 static PyObject *
269 _wrap_get_text_bound(PyGObject *self, PyObject *args)
271         PyObject *list;
272         int w = 0, h = 0;
273         char *text;
275     if (!PyArg_ParseTuple(args, "s:gnt.get_text_bound", &text)) {
276                 return NULL;
277         }
279         gnt_util_get_text_bound(text, &w, &h);
281         list = PyList_New(0);
282         PyList_Append(list, PyInt_FromLong((long)w));
283         PyList_Append(list, PyInt_FromLong((long)h));
284         return list;
287 override gnt_main noargs
288 static PyObject *
289 _wrap_gnt_main(PyObject *self)
291         if (pyg_threads_enabled)
292                 pyg_enable_threads();
293         
294         pyg_begin_allow_threads;
295         gnt_main();
296         pyg_end_allow_threads;
298         Py_INCREF(Py_None);
299         return Py_None;