[PATCH] Pygoocanvas C API



Hello,

This provides a C API for pygoocanvas so that it is possible to build
python bindings for libraries which use goocanvas.

The implementation was adapted from py2cairo.



Using the pygoocanvas C API in your extension module.

1. Add the following to configure.in:

PYGOOCANVAS_DEFSDIR=`pkg-config --variable=defsdir pygoocanvas`
AC_SUBST(PYGOOCANVAS_DEFSDIR)

2. Add the following argument to the gobject codegen command line in
   Makefile.am:

--register @PYGOOCANVAS_DEFSDIR@/goocanvas.defs

3. Include the API in your module.c:

#include <pygoocanvas.h>
PyGooCanvas_CAPI_t *PyGooCanvas_CAPI;

4. Import pygoocanvas in the module init function:

    PyGooCanvas_IMPORT;
    if (PyGooCanvas_CAPI == NULL)
        return;

5. Add the following to the %% headers section of your override file:

#include <pygoocanvas.h>
extern PyGooCanvas_CAPI_t *PyGooCanvas_CAPI;



Cheers,

Rodney
---
 Makefile.am        |    6 ++++
 goocanvas.override |    7 +----
 goocanvasmodule.c  |   16 +++++++++++-
 private.h          |   47 ++++++++++++++++++++++++++++++++++++
 pygoocanvas.h      |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 pygoocanvas.pc.in  |    6 ++++-
 6 files changed, 143 insertions(+), 7 deletions(-)
 create mode 100644 private.h
 create mode 100644 pygoocanvas.h

diff --git a/Makefile.am b/Makefile.am
index 107b565..3f611b3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -44,6 +44,12 @@ pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = pygoocanvas.pc
 EXTRA_DIST += pygoocanvas.pc
 
+# C API
+pkgincludedir = $(includedir)/pygoocanvas
+pkginclude_HEADERS = pygoocanvas.h
+defsdir = $(datadir)/pygtk/2.0/defs
+defs_DATA = goocanvas.defs
+
 if PLATFORM_WIN32
 # Python .pyd modules are simply DLLs, but they have to be called .pyd for
 # python to find them, and libtool only creates .dll.
diff --git a/goocanvas.override b/goocanvas.override
index c60c957..4b84675 100644
--- a/goocanvas.override
+++ b/goocanvas.override
@@ -7,6 +7,8 @@ headers
 #include <pygtk/pygtk.h>
 
 #include <pycairo.h>
+#include "private.h"
+
 
 #include "config.h"
 
@@ -63,11 +65,6 @@ _gslist_to_pylist_strs (GSList *source)
 
 #endif
 
-typedef struct {
-    PyObject_HEAD
-    GooCanvasBounds bounds;
-} PyGooCanvasBounds;
-
 #define bounds_getter(field)                                                    \
 static PyObject *                                                               \
 _pygoo_canvas_bounds_get_##field(PyGooCanvasBounds *self, void *closure)        \
diff --git a/goocanvasmodule.c b/goocanvasmodule.c
index bf19f2b..07ec46d 100644
--- a/goocanvasmodule.c
+++ b/goocanvasmodule.c
@@ -2,6 +2,8 @@
 #include "config.h"
 #endif
 
+#include "private.h"
+
 #include <pygobject.h>
 #define NO_IMPORT_PYGTK
 #include <pygtk/pygtk.h>
@@ -14,6 +16,8 @@ Pycairo_CAPI_t *Pycairo_CAPI;
 void pygoocanvas_register_classes (PyObject *d); 
 void pygoocanvas_add_constants(PyObject *module, const gchar *strip_prefix);
 
+extern PyTypeObject PyGooCanvasBounds_Type;
+
 extern PyMethodDef pygoocanvas_functions[];
  
 static PyObject *
@@ -53,6 +57,14 @@ _cairo_pattern_to_gvalue(GValue *value, PyObject *obj)
     return 0;
 }
 
+/* C API.  Clients get at this via PyGooCanvas_IMPORT, defined in pygoocanvas.h.
+ */
+static PyGooCanvas_CAPI_t CAPI = {
+  &PyGooCanvasBounds_Type,
+  pygoo_canvas_bounds_new,
+};
+
+
 DL_EXPORT (void)
 initgoocanvas (void)
 {
@@ -84,7 +96,9 @@ initgoocanvas (void)
                                      PYGOOCANVAS_MAJOR_VERSION,
                                      PYGOOCANVAS_MINOR_VERSION,
                                      PYGOOCANVAS_MICRO_VERSION));
-    
+
+    PyModule_AddObject(m, "CAPI", PyCObject_FromVoidPtr(&CAPI, NULL));
+
     if (PyErr_Occurred ())
         Py_FatalError ("can't initialise module goocanvas");
 }
diff --git a/private.h b/private.h
new file mode 100644
index 0000000..701aa05
--- /dev/null
+++ b/private.h
@@ -0,0 +1,47 @@
+/* -*- mode: C; c-basic-offset: 4 -*-
+ *
+ * Pygoocanvas - Python bindings for goocanvas
+ *
+ * Copyright © 2003 James Henstridge, Steven Chaplin
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it either under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation
+ * (the "LGPL") or, at your option, under the terms of the Mozilla
+ * Public License Version 1.1 (the "MPL"). If you do not alter this
+ * notice, a recipient may use your version of this file under either
+ * the MPL or the LGPL.
+ *
+ * You should have received a copy of the LGPL along with this library
+ * in the file COPYING-LGPL-2.1; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * You should have received a copy of the MPL along with this library
+ * in the file COPYING-MPL-1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
+ * OF ANY KIND, either express or implied. See the LGPL or the MPL for
+ * the specific language governing rights and limitations.
+ */
+
+#ifndef _PYGOOCANVAS_PRIVATE_H_
+#define _PYGOOCANVAS_PRIVATE_H_
+
+#ifdef _PYGOOCANVAS_H_
+#  error "don't include pygoocanvas.h and pygoocanvas-private.h together"
+#endif
+
+#define _INSIDE_PYGOOCANVAS_
+#include <Python.h>
+
+#include "pygoocanvas.h"
+
+
+extern PyTypeObject PyGooCanvasBounds_Type;
+PyObject *pygoo_canvas_bounds_new(const GooCanvasBounds *bounds);
+
+#endif /* _PYGOOCANVAS_PRIVATE_H_ */
diff --git a/pygoocanvas.h b/pygoocanvas.h
new file mode 100644
index 0000000..b4d85ed
--- /dev/null
+++ b/pygoocanvas.h
@@ -0,0 +1,68 @@
+/* -*- mode: C; c-basic-offset: 4 -*-
+ *
+ * Pygoocanvas - Python bindings for goocanvas
+ *
+ * Copyright © 2003 James Henstridge, Steven Chaplin
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it either under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation
+ * (the "LGPL") or, at your option, under the terms of the Mozilla
+ * Public License Version 1.1 (the "MPL"). If you do not alter this
+ * notice, a recipient may use your version of this file under either
+ * the MPL or the LGPL.
+ *
+ * You should have received a copy of the LGPL along with this library
+ * in the file COPYING-LGPL-2.1; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * You should have received a copy of the MPL along with this library
+ * in the file COPYING-MPL-1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
+ * OF ANY KIND, either express or implied. See the LGPL or the MPL for
+ * the specific language governing rights and limitations.
+ */
+
+#ifndef _PYGOOCANVAS_H_
+#define _PYGOOCANVAS_H_
+
+#include <Python.h>
+
+#include <goocanvas.h>
+
+typedef struct {
+    PyObject_HEAD
+    GooCanvasBounds bounds;
+} PyGooCanvasBounds;
+
+
+/* Define structure for C API. */
+typedef struct {
+  /* (type object, constructor) pairs */
+  PyTypeObject *Bounds_Type;
+  PyObject *(*Bounds_FromBounds)(const GooCanvasBounds *);
+} PyGooCanvas_CAPI_t;
+
+
+#ifndef _INSIDE_PYGOOCANVAS_
+
+/* Macros for accessing the C API */
+#define PyGooCanvasBounds_Type         *(PyGooCanvas_CAPI->Bounds_Type)
+#define PyGooCanvasBounds_FromBounds   (PyGooCanvas_CAPI->Bounds_FromBounds)
+
+/* To access the PyGooCanvas C API, edit the client module file to:
+ * 1) Add the following line to define a global variable for the C API
+ *    static PyGooCanvas_CAPI_t *PyGooCanvas_CAPI;
+ * 2) Add 'PyGooCanvas_IMPORT;' to the init<module> function
+ */
+#define PyGooCanvas_IMPORT \
+        PyGooCanvas_CAPI = (PyGooCanvas_CAPI_t*) PyCObject_Import("goocanvas", "CAPI")
+
+#endif /* ifndef _INSIDE_PYGOOCANVAS_ */
+
+#endif /* ifndef _PYGOOCANVAS_H_ */
diff --git a/pygoocanvas.pc.in b/pygoocanvas.pc.in
index c20e2d7..43a0cd7 100644
--- a/pygoocanvas.pc.in
+++ b/pygoocanvas.pc.in
@@ -2,10 +2,14 @@ prefix= prefix@
 exec_prefix= exec_prefix@
 libdir= libdir@
 includedir= includedir@
+datarootdir= datarootdir@
+datadir= datadir@
 
+defsdir=${datadir}/pygtk/2.0/defs
 
 Name: PyGooCanvas
 Description: Python bindings for GooCanvas
 Version: @VERSION@
 Requires: pygtk-2.0
-
+Cflags: -I includedir@/pygoocanvas
+Libs:
-- 
1.6.2.5

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]