|
1 | | -/* Helper functions for converting an agg image buffer to a gtk drawable |
2 | | - efficiently |
3 | | -*/ |
4 | | -#include <Python.h> |
5 | | -#include <pygobject.h> |
6 | | -#include <pygtk/pygtk.h> |
7 | 1 | #include <cstring> |
8 | 2 | #include <cerrno> |
9 | 3 | #include <cstdio> |
| 4 | +#include <iostream> |
| 5 | +#include <cmath> |
| 6 | +#include <utility> |
10 | 7 |
|
11 | | -#include "_backend_agg.h" |
| 8 | +#include <pygobject.h> |
| 9 | +#include <pygtk/pygtk.h> |
12 | 10 |
|
13 | | -static PyObject *ErrorObject; |
14 | | -static PyTypeObject *PyGObject_Type=NULL; |
| 11 | +#include "_backend_agg.h" |
15 | 12 |
|
| 13 | +// the extension module |
| 14 | +class _gtkagg_module : public Py::ExtensionModule<_gtkagg_module> |
| 15 | +{ |
| 16 | +public: |
| 17 | + _gtkagg_module() |
| 18 | + : Py::ExtensionModule<_gtkagg_module>( "_gtkagg" ) |
| 19 | + { |
| 20 | + add_varargs_method("agg_to_gtk_drawable", |
| 21 | + &_gtkagg_module::agg_to_gtk_drawable, |
| 22 | + "Draw to a gtk drawable from a agg buffer."); |
| 23 | + initialize( "The _gtkagg module" ); |
| 24 | + } |
| 25 | + |
| 26 | + virtual ~_gtkagg_module() {} |
| 27 | + |
| 28 | +private: |
| 29 | + |
| 30 | + Py::Object agg_to_gtk_drawable(const Py::Tuple &args) { |
16 | 31 |
|
17 | | -static PyObject * |
18 | | -_agg_to_gtk_drawable(PyObject *self, PyObject *args) { |
| 32 | + args.verify_length(2); |
19 | 33 |
|
20 | | - PyGObject *py_drawable = NULL; |
21 | | - GdkDrawable *drawable = NULL; |
22 | | - GdkGC* gc = NULL; |
23 | 34 |
|
24 | | - PyObject* aggo; |
25 | | - |
| 35 | + PyGObject *py_drawable = (PyGObject *)(args[0].ptr()); |
| 36 | + RendererAgg* aggRenderer = static_cast<RendererAgg*>(args[1].ptr()); |
26 | 37 |
|
27 | | - if (!PyArg_ParseTuple(args, "O!O", PyGObject_Type, |
28 | | - &py_drawable, &aggo)) |
29 | | - return NULL; |
| 38 | + |
| 39 | + GdkDrawable *drawable = GDK_DRAWABLE(py_drawable->obj); |
| 40 | + GdkGC* gc = gdk_gc_new(drawable); |
30 | 41 |
|
| 42 | + unsigned int width = aggRenderer->get_width(); |
| 43 | + unsigned int height = aggRenderer->get_height(); |
31 | 44 |
|
32 | | - RendererAgg* aggRenderer = (RendererAgg*)aggo; |
| 45 | + gdk_draw_rgb_32_image(drawable, gc, 0, 0, |
| 46 | + width, |
| 47 | + height, |
| 48 | + GDK_RGB_DITHER_NORMAL, |
| 49 | + aggRenderer->pixBuffer, |
| 50 | + width*4); |
33 | 51 |
|
34 | | - drawable = GDK_DRAWABLE(py_drawable->obj); |
35 | | - gc = gdk_gc_new(drawable); |
| 52 | + return Py::Object(); |
36 | 53 |
|
37 | | - unsigned int width = aggRenderer->get_width(); |
38 | | - unsigned int height = aggRenderer->get_height(); |
| 54 | + } |
| 55 | +}; |
39 | 56 |
|
40 | | - gdk_draw_rgb_32_image(drawable, gc, 0, 0, |
41 | | - width, |
42 | | - height, |
43 | | - GDK_RGB_DITHER_NORMAL, |
44 | | - aggRenderer->pixBuffer, |
45 | | - width*4); |
46 | | - |
47 | | - Py_INCREF(Py_None); |
48 | | - return Py_None; |
49 | | -} |
50 | | - |
51 | 57 |
|
| 58 | +extern "C" |
| 59 | +DL_EXPORT(void) |
| 60 | + init_gtkagg(void) |
| 61 | +{ |
| 62 | + init_pygobject(); |
| 63 | + init_pygtk(); |
| 64 | + //suppress unused warning by creating in two lines |
| 65 | + static _gtkagg_module* _gtkagg = NULL; |
| 66 | + _gtkagg = new _gtkagg_module; |
52 | 67 |
|
| 68 | +}; |
53 | 69 |
|
54 | 70 |
|
55 | | -static struct PyMethodDef _gtkagg_methods[] = { |
56 | | - {"agg_to_gtk_drawable", (PyCFunction)_agg_to_gtk_drawable, METH_VARARGS, |
57 | | - "Draw to a gtk drawable from a agg buffer."}, |
58 | | - {NULL, NULL} /* sentinel */ |
59 | | -}; |
60 | 71 |
|
61 | 72 |
|
62 | | -extern "C" |
63 | | -DL_EXPORT(void) init_gtkagg(void) |
64 | | -{ |
65 | | - PyObject *module, *d; |
66 | | - |
67 | 73 |
|
68 | 74 |
|
69 | | - init_pygobject(); |
70 | | - init_pygtk(); |
71 | | - /* Create the module and add the functions */ |
72 | | - Py_InitModule("_gtkagg", _gtkagg_methods); |
73 | | - module = PyImport_ImportModule("gobject"); |
74 | | - if (module) { |
75 | | - PyGObject_Type = |
76 | | - (PyTypeObject*)PyObject_GetAttrString(module, "GObject"); |
77 | | - Py_DECREF(module); |
78 | | - } |
79 | | - /* Add some symbolic constants to the module */ |
80 | | - d = PyModule_GetDict(module); |
81 | | - ErrorObject = PyString_FromString("_gtkagg.error"); |
82 | | - PyDict_SetItemString(d, "error", ErrorObject); |
83 | | - |
84 | | - /* Check for errors */ |
85 | | - if (PyErr_Occurred()) |
86 | | - Py_FatalError("can't initialize module _gtkagg"); |
87 | | -} |
0 commit comments