1 /* -*- Mode: C; c-basic-offset: 4 -*- */ 2 #ifndef _PYGTK_H_ 3 #define _PYGTK_H_ 4 5 #define NO_IMPORT_PYGOBJECT 6 #include <pygobject.h> 7 #include <Python.h> 8 #include <gtk/gtk.h> 9 10 struct _PyGtk_FunctionStruct { 11 char *pygtkVersion; 12 13 PyTypeObject *gdkAtom_type; 14 PyObject *(* gdkAtom_new)(GdkAtom atom); 15 gboolean (* rectangle_from_pyobject)(PyObject *object, 16 GdkRectangle *rectangle); 17 PyObject * (*tree_path_to_pyobject) (GtkTreePath *path); 18 GtkTreePath * (*tree_path_from_pyobject) (PyObject *object); 19 }; 20 21 /* structure definitions for the various object types in PyGTK */ 22 typedef struct { 23 PyObject_HEAD 24 gchar *name; 25 GdkAtom atom; 26 } PyGdkAtom_Object; 27 28 /* routines to get the C object value out of the PyObject wrapper */ 29 #define PyGdkAtom_Get(v) (((PyGdkAtom_Object *)(v))->atom) 30 31 /* this section is dependent on whether we are being included from gtkmodule.c 32 * or not. A similar source level interface should be provided in both 33 * instances. */ 34 #ifndef _INSIDE_PYGTK_ 35 36 /* for multi file extensions, define one of these in all but the main file 37 * of the module */ 38 #if defined(NO_IMPORT) || defined(NO_IMPORT_PYGTK) 39 extern struct _PyGtk_FunctionStruct *_PyGtk_API; 40 #else 41 struct _PyGtk_FunctionStruct *_PyGtk_API; 42 #endif 43 44 /* type objects */ 45 #define PyGdkAtom_Type *(_PyGtk_API->gdkAtom_type) 46 47 /* type checking routines */ 48 #define PyGdkAtom_Check(v) ((v)->ob_type == _PyGtk_API->gdkAtom_type) 49 50 /* type objects */ 51 #define PyGdkAtom_New (_PyGtk_API->gdkAtom_new) 52 53 /* some variables */ 54 #define PYGTK_VERSION (_PyGtk_API->pygtkVersion) 55 56 /* public functions */ 57 #define pygdk_rectangle_from_pyobject (_PyGtk_API->rectangle_from_pyobject) 58 #define pygtk_tree_path_to_pyobject (_PyGtk_API->tree_path_to_pyobject) 59 #define pygtk_tree_path_from_pyobject (_PyGtk_API->tree_path_from_pyobject) 60 61 62 /* a function to initialise the pygtk functions */ 63 64 /* Python 2.7 introduced the PyCapsule API and deprecated the CObject API */ 65 #if PY_VERSION_HEX >= 0x02070000 66 #define init_pygtk() G_STMT_START { \ 67 void *capsule = PyCapsule_Import("gtk._gtk._PyGtk_API", 0); \ 68 if (!capsule) { \ 69 return; \ 70 } \ 71 _PyGtk_API = (struct _PyGtk_FunctionStruct*)capsule; \ 72 } G_STMT_END 73 #else /* PY_VERSION_HEX */ 74 /* Python 2.6 and earlier use the CObject API */ 75 #define init_pygtk() G_STMT_START { \ 76 PyObject *pygtk = PyImport_ImportModule("gtk"); \ 77 if (pygtk != NULL) { \ 78 PyObject *module_dict = PyModule_GetDict(pygtk); \ 79 PyObject *cobject = PyDict_GetItemString(module_dict, "_PyGtk_API"); \ 80 if (PyCObject_Check(cobject)) \ 81 _PyGtk_API = (struct _PyGtk_FunctionStruct*) \ 82 PyCObject_AsVoidPtr(cobject); \ 83 else { \ 84 PyErr_SetString(PyExc_RuntimeError, \ 85 "could not find _PyGtk_API object"); \ 86 return; \ 87 } \ 88 } else { \ 89 PyErr_SetString(PyExc_ImportError, \ 90 "could not import gtk"); \ 91 return; \ 92 } \ 93 } G_STMT_END 94 #endif /* PY_VERSION_HEX */ 95 96 #endif 97 98 #endif /* !_PYGTK_H_ */ 99