2 * pygnt- Python bindings for the GNT toolkit.
3 * Copyright (C) 2007 Sadrul Habib Chowdhury <sadrul@pidgin.im>
5 * gnt.override: Some generic wrappers.
6 * Support for specifying custom bindings.
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.
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.
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
26 #include "pygobject.h"
28 #include "gntbindable.h"
29 #include "gntwidget.h"
31 #include "gntbutton.h"
32 #include "gntcheckbox.h"
33 #include "gntcolors.h"
34 #include "gntcombobox.h"
36 #include "gntfilesel.h"
41 #include "gntmenuitem.h"
42 #include "gntmenuitemcheck.h"
43 #include "gntslider.h"
45 #include "gnttextview.h"
48 #include "gntwindow.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
78 import gobject.GObject as PyGObject_Type
85 _wrap_set_flag(PyGObject *self, PyObject *args, PyObject *kwargs)
87 static char *kwlist[] = {"flags", NULL};
91 if (!PyArg_ParseTuple(args, "O!i:gnt.set_flag", &PyGntWidget_Type, &widget,
96 GNT_WIDGET_SET_FLAGS(widget->obj, flags);
104 _wrap_unset_flag(PyGObject *self, PyObject *args, PyObject *kwargs)
106 static char *kwlist[] = {"flags", NULL};
110 if (!PyArg_ParseTuple(args, "O!i:gnt.unset_flag", &PyGntWidget_Type, &widget,
115 GNT_WIDGET_UNSET_FLAGS(widget->obj, flags);
121 define screen_size noargs
123 _wrap_screen_size(PyObject *self)
125 PyObject *list = PyList_New(0);
130 PyList_Append(list, PyInt_FromLong((long)getmaxx(stdscr)));
131 PyList_Append(list, PyInt_FromLong((long)getmaxy(stdscr)));
136 override gnt_register_action
137 static GHashTable *actions;
142 _wrap_gnt_register_action(PyGObject *self, PyObject *args, PyObject *kwargs)
144 static char *kwlist[] = {"name", "callback", NULL};
149 if (!PyArg_ParseTuple(args, "sO:gnt.gnt_register_action", &name, &callback)) {
153 if (!PyCallable_Check(callback)) {
154 PyErr_SetString(PyExc_TypeError, "the callback must be callable ... doh!");
158 gnt_register_action(name, callback->obj);
164 define register_bindings
167 pygnt_binding_callback(GntBindable *bindable, GList *list)
169 PyObject *wrapper = pygobject_new(G_OBJECT(bindable));
171 PyObject_CallMethod(wrapper, list->data, "O", Py_None);
177 _wrap_register_bindings(PyObject *self, PyObject *args)
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");
192 gbindings = PyDict_GetItemString(class->tp_dict, "__gntbindings__");
196 if (!PyDict_Check(gbindings)) {
197 PyErr_SetString(PyExc_TypeError,
198 "__gntbindings__ attribute not a dict!");
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;
208 if (!PyString_Check(key)) {
209 PyErr_SetString(PyExc_TypeError,
210 "__gntbindings__ keys must be strings");
211 g_type_class_unref(bindable);
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);
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. */
231 PyDict_DelItemString(class->tp_dict, "__gntbindings__");
232 g_type_class_unref(bindable);
241 _wrap_show_menu(PyGObject *self, PyObject *args, PyObject *kwargs)
245 if (!PyArg_ParseTuple(args, "O!:gnt.show_menu", &PyGntMenu_Type, &menu)) {
249 gnt_screen_menu_show(GNT_MENU(menu->obj));
257 gnt_util_get_text_bound
259 define get_text_bound
261 _wrap_get_text_bound(PyGObject *self, PyObject *args)
267 if (!PyArg_ParseTuple(args, "s:gnt.get_text_bound", &text)) {
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));