4
* Python extension to grab m_macPort out of a wxDC.
6
* This module itself doesn't really use any features of C++, but it links
7
* against wx.h (which is definitely C++).
16
void initmacport(void);
19
/* This converts a string of hex digits into an unsigned long. It reads
20
This code is a modified version of SWIG_UnpackData from weave/swigptr2.py,
21
and which I believe originated from the SWIG sources. */
22
unsigned long hexstr_to_long(const char *c, char bytesize = 4) {
23
unsigned long retval = 0;
25
unsigned char *u = reinterpret_cast<unsigned char*>(&retval);
26
const unsigned char *eu = u + bytesize;
27
for (; u != eu; ++u) {
28
register int d = *(c++);
29
register unsigned char uu = 0;
30
if ((d >= '0') && (d <= '9'))
31
uu = ((d - '0') << 4);
32
else if ((d >= 'a') && (d <= 'f'))
33
uu = ((d - ('a'-10)) << 4);
37
if ((d >= '0') && (d <= '9'))
39
else if ((d >= 'a') && (d <= 'f'))
48
PyObject* get_macport(PyObject *self, PyObject *args)
50
const char err_string[] = "get_macport() requires a SWIG 'this' string.";
52
// the string representing the address embedded in the SWIG this ptr
53
char *dc_addr_str = NULL;
57
wxGraphicsContext *p_gc = NULL;
59
err = PyArg_ParseTuple(args, "s#", &dc_addr_str, &length);
62
PyErr_SetString(PyExc_ValueError, err_string);
67
PyErr_SetString(PyExc_ValueError, err_string);
72
p_dc = reinterpret_cast<wxDC*>(hexstr_to_long(dc_addr_str+1, 4));
73
p_gc = reinterpret_cast<wxGraphicsContext*>(p_dc->GetGraphicsContext());
74
unsigned long tmp = reinterpret_cast<unsigned long>(p_gc->GetNativeContext());
75
return Py_BuildValue("k", tmp);
79
static PyMethodDef macport_methods[] = {
80
{"get_macport", get_macport, METH_VARARGS,
81
"get_macport(dc.this) -> Returns the pointer (as an unsigned long) of the CGContextRef of a wxDC.this SWIG pointer"},
85
void initmacport(void)
87
Py_InitModule("macport", macport_methods);