Imported Upstream version 2.28.6
[platform/upstream/pygobject2.git] / gobject / pyginterface.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * pygtk- Python bindings for the GTK toolkit.
3  * Copyright (C) 1998-2003  James Henstridge
4  *               2004-2008  Johan Dahlin
5  *   pyginterface.c: wrapper for the gobject library.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <Python.h>
28 #include "pyglib.h"
29 #include "pygobject-private.h"
30
31 GQuark pyginterface_type_key;
32 GQuark pyginterface_info_key;
33
34 PYGLIB_DEFINE_TYPE("gobject.GInterface", PyGInterface_Type, PyObject)
35
36 static int
37 pyg_interface_init(PyObject *self, PyObject *args, PyObject *kwargs)
38 {
39     gchar buf[512];
40
41     if (!PyArg_ParseTuple(args, ":GInterface.__init__"))
42         return -1;
43
44     g_snprintf(buf, sizeof(buf), "%s can not be constructed",
45                Py_TYPE(self)->tp_name);
46     PyErr_SetString(PyExc_NotImplementedError, buf);
47     return -1;
48 }
49
50 static void
51 pyg_interface_free(PyObject *op)
52 {
53     PyObject_FREE(op);
54 }
55
56 /**
57  * pyg_register_interface:
58  * @dict: a module dictionary.
59  * @class_name: the class name for the wrapper class.
60  * @gtype: the GType of the interface.
61  * @type: the wrapper class for the interface.
62  *
63  * Registers a Python class as the wrapper for a GInterface.  As a
64  * convenience it will also place a reference to the wrapper class in
65  * the provided module dictionary.
66  */
67 void
68 pyg_register_interface(PyObject *dict, const gchar *class_name,
69                        GType gtype, PyTypeObject *type)
70 {
71     PyObject *o;
72
73     Py_TYPE(type) = &PyType_Type;
74     type->tp_base = &PyGInterface_Type;
75
76     if (PyType_Ready(type) < 0) {
77         g_warning("could not ready `%s'", type->tp_name);
78         return;
79     }
80
81     if (gtype) {
82         o = pyg_type_wrapper_new(gtype);
83         PyDict_SetItemString(type->tp_dict, "__gtype__", o);
84         Py_DECREF(o);
85     }
86
87     g_type_set_qdata(gtype, pyginterface_type_key, type);
88     
89     PyDict_SetItemString(dict, (char *)class_name, (PyObject *)type);
90     
91 }
92
93 void
94 pyg_register_interface_info(GType gtype, const GInterfaceInfo *info)
95 {
96     g_type_set_qdata(gtype, pyginterface_info_key, (gpointer) info);
97 }
98
99 const GInterfaceInfo *
100 pyg_lookup_interface_info(GType gtype)
101 {
102     return g_type_get_qdata(gtype, pyginterface_info_key);
103 }
104
105 void
106 pygobject_interface_register_types(PyObject *d)
107 {
108   pyginterface_type_key = g_quark_from_static_string("PyGInterface::type");
109   pyginterface_info_key = g_quark_from_static_string("PyGInterface::info");
110
111   PyGInterface_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
112   PyGInterface_Type.tp_init = (initproc)pyg_interface_init;
113   PyGInterface_Type.tp_free = (freefunc)pyg_interface_free;
114
115   PYGOBJECT_REGISTER_GTYPE(d, PyGInterface_Type, "GInterface", G_TYPE_INTERFACE)
116
117   PyDict_SetItemString(PyGInterface_Type.tp_dict, "__doc__",
118                        pyg_object_descr_doc_get());
119   PyDict_SetItemString(PyGInterface_Type.tp_dict, "__gdoc__",
120                        pyg_object_descr_doc_get());
121   
122 }