Use the gobject property to create labels with text format.
[python-gnt.git] / gnt.override
blobd6b489f75eb1dd515dce647a0d040bdce86eeb8c
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  gntcombobox.override
71  gntfilesel.override
72  gntmenu.override
73  gnttree.override
74  gntwidget.override
75  gntwindow.override
77 modulename gnt
79 import gobject.GObject as PyGObject_Type
81 ignore-glob
82         *_get_gtype
84 define set_flag
85 static PyObject *
86 _wrap_set_flag(PyGObject *self, PyObject *args, PyObject *kwargs)
88         static char *kwlist[] = {"flags", NULL};
89         PyGObject *widget;
90         int flags;
92         if (!PyArg_ParseTuple(args, "O!i:gnt.set_flag", &PyGntWidget_Type, &widget,
93                                 &flags)) {
94                 return NULL;
95         }
97         GNT_WIDGET_SET_FLAGS(widget->obj, flags);
99         Py_INCREF(Py_None);
100         return Py_None;
103 define unset_flag
104 static PyObject *
105 _wrap_unset_flag(PyGObject *self, PyObject *args, PyObject *kwargs)
107         static char *kwlist[] = {"flags", NULL};
108         PyGObject *widget;
109         int flags;
111         if (!PyArg_ParseTuple(args, "O!i:gnt.unset_flag", &PyGntWidget_Type, &widget,
112                                 &flags)) {
113                 return NULL;
114         }
116         GNT_WIDGET_UNSET_FLAGS(widget->obj, flags);
118         Py_INCREF(Py_None);
119         return Py_None;
122 define screen_size noargs
123 static PyObject *
124 _wrap_screen_size(PyObject *self)
126         PyObject *list = PyList_New(0);
128         if (list == NULL)
129                 return NULL;
131         PyList_Append(list, PyInt_FromLong((long)getmaxx(stdscr)));
132         PyList_Append(list, PyInt_FromLong((long)getmaxy(stdscr)));
134         return list;
137 override gnt_register_action
138 static GHashTable *actions;
142 static PyObject *
143 _wrap_gnt_register_action(PyGObject *self, PyObject *args, PyObject *kwargs)
145         static char *kwlist[] = {"name", "callback", NULL};
146         PyGObject *callback;
147         GClosure *closure;
148         char *name;
150         if (!PyArg_ParseTuple(args, "sO:gnt.gnt_register_action", &name, &callback)) {
151                 return NULL;
152         }
154         if (!PyCallable_Check(callback)) {
155                 PyErr_SetString(PyExc_TypeError, "the callback must be callable ... doh!");
156                 return NULL;
157         }
159         gnt_register_action(name, callback->obj);
161         Py_INCREF(Py_None);
162         return Py_None;
165 define register_bindings
167 static gboolean
168 pygnt_binding_callback(GntBindable *bindable, GList *list)
170         PyObject *wrapper = pygobject_new(G_OBJECT(bindable));
171         if (list)
172                 PyObject_CallMethod(wrapper, list->data, "O", Py_None);
173         Py_DECREF(wrapper);
174         return TRUE;
177 static PyObject *
178 _wrap_register_bindings(PyObject *self, PyObject *args)
180         PyTypeObject *class;
181         int pos = 0;
182         PyObject *key, *value, *gbindings;
183         GntBindableClass *bindable;
185         if (!PyArg_ParseTuple(args, "O!:gnt.register_bindings",
186                                 &PyType_Type, &class)) {
187                 /* Make sure it's a GntBindableClass subclass */
188                 PyErr_SetString(PyExc_TypeError,
189                                 "argument must be a GntBindable subclass");
190                 return NULL;
191         }
193         gbindings = PyDict_GetItemString(class->tp_dict, "__gntbindings__");
194         if (!gbindings)
195                 goto end;
197         if (!PyDict_Check(gbindings)) {
198                 PyErr_SetString(PyExc_TypeError,
199                                 "__gntbindings__ attribute not a dict!");
200                 return NULL;
201         }
203         bindable = g_type_class_ref(pyg_type_from_object((PyObject *)class));
204         while (PyDict_Next(gbindings, &pos, &key, &value)) {
205                 const char *trigger, *callback, *name;
206                 GList *list = NULL;
207                 char *cbname;
209                 if (!PyString_Check(key)) {
210                         PyErr_SetString(PyExc_TypeError,
211                                         "__gntbindings__ keys must be strings");
212                         g_type_class_unref(bindable);
213                         return NULL;
214                 }
215                 name = PyString_AsString(key);
217                 if (!PyTuple_Check(value) ||
218                                 !PyArg_ParseTuple(value, "ss", &callback, &trigger)) {
219                         PyErr_SetString(PyExc_TypeError,
220                                         "__gntbindings__ values must be (callback, trigger) tupples");
221                         g_type_class_unref(bindable);
222                         return NULL;
223                 }
225                 gnt_bindable_class_register_action(bindable, name, pygnt_binding_callback,
226                                 trigger, g_strdup(callback), NULL);
227                 cbname = g_strdup_printf("%s::%s", class->tp_name, name);
228                 g_hash_table_replace(bindings, cbname, g_strdup(callback));
229                 /* Do not free cbname here. */
230         }
231         if (gbindings)
232                 PyDict_DelItemString(class->tp_dict, "__gntbindings__");
233         g_type_class_unref(bindable);
235 end:
236         Py_INCREF(Py_None);
237         return Py_None;
240 define show_menu
241 static PyObject *
242 _wrap_show_menu(PyGObject *self, PyObject *args, PyObject *kwargs)
244         PyGObject *menu;
246         if (!PyArg_ParseTuple(args, "O!:gnt.show_menu", &PyGntMenu_Type, &menu)) {
247                 return NULL;
248         }
250         gnt_screen_menu_show(GNT_MENU(menu->obj));
251         Py_INCREF(menu);
253         Py_INCREF(Py_None);
254         return Py_None;
257 ignore
258 gnt_util_get_text_bound
260 define get_text_bound
261 static PyObject *
262 _wrap_get_text_bound(PyGObject *self, PyObject *args)
264         PyObject *list;
265         int w = 0, h = 0;
266         char *text;
268     if (!PyArg_ParseTuple(args, "s:gnt.get_text_bound", &text)) {
269                 return NULL;
270         }
272         gnt_util_get_text_bound(text, &w, &h);
274         list = PyList_New(0);
275         PyList_Append(list, PyInt_FromLong((long)w));
276         PyList_Append(list, PyInt_FromLong((long)h));
277         return list;