~ubuntu-branches/ubuntu/trusty/python-enable/trusty

« back to all changes in this revision

Viewing changes to enthought/kiva/mac/macport28.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-04-06 19:28:09 UTC
  • mfrom: (8.2.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110406192809-20ikzqm4uj9qxwuu
Tags: 3.4.1-2
d/rules: fix pyshared directory path

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * macport.cpp
3
 
 *
4
 
 * Python extension to grab m_macPort out of a wxDC.
5
 
 *
6
 
 * This module itself doesn't really use any features of C++, but it links
7
 
 * against wx.h (which is definitely C++).
8
 
 */
9
 
 
10
 
#include "wx/wx.h"
11
 
 
12
 
#include "Python.h"
13
 
 
14
 
extern "C"
15
 
{
16
 
    void initmacport(void);
17
 
}
18
 
 
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;
24
 
    
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);
34
 
        else 
35
 
            return 0;
36
 
        d = *(c++);
37
 
        if ((d >= '0') && (d <= '9'))
38
 
            uu |= (d - '0');
39
 
        else if ((d >= 'a') && (d <= 'f'))
40
 
            uu |= (d - ('a'-10));
41
 
        else 
42
 
            return 0;
43
 
        *u = uu;
44
 
    }
45
 
    return retval;
46
 
}
47
 
 
48
 
PyObject* get_macport(PyObject *self, PyObject *args)
49
 
{
50
 
    const char err_string[] = "get_macport() requires a SWIG 'this' string.";
51
 
    
52
 
    // the string representing the address embedded in the SWIG this ptr
53
 
    char *dc_addr_str = NULL;
54
 
    int length = 0;
55
 
    int err = 0;
56
 
    wxDC *p_dc = NULL;
57
 
    wxGraphicsContext *p_gc = NULL;
58
 
 
59
 
    err = PyArg_ParseTuple(args, "s#", &dc_addr_str, &length);
60
 
    if (err != 1)
61
 
    {
62
 
        PyErr_SetString(PyExc_ValueError, err_string);
63
 
        return NULL;
64
 
    }
65
 
    else if (length < 10)
66
 
    {
67
 
        PyErr_SetString(PyExc_ValueError, err_string);
68
 
        return NULL;
69
 
    }
70
 
    else
71
 
    {
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);
76
 
    }
77
 
}
78
 
 
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"},
82
 
    {NULL, NULL}
83
 
};
84
 
 
85
 
void initmacport(void)
86
 
{
87
 
    Py_InitModule("macport", macport_methods);
88
 
}