| 1 |
%include <std_string.i> |
| 2 |
%include <typemaps.i> |
| 3 |
|
| 4 |
%define SET_FORMAT_STRING_IN_TYPEMAP(type, name) |
| 5 |
%typemap(directorin) (type name) { |
| 6 |
// deliberately keep this empty in order to ignore the input parameter |
| 7 |
} |
| 8 |
%enddef |
| 9 |
|
| 10 |
%define SET_FORMAT_STRING_OUT_TYPEMAP(type, name, inp_name) |
| 11 |
%typemap(directorout) (type name) { |
| 12 |
Py_ssize_t psize; |
| 13 |
psize = 0; |
| 14 |
const char* chars = PyUnicode_AsUTF8AndSize(result, &psize); |
| 15 |
// Fl_Valuator::format() writes 128-bytes only. The output buffer may not have |
| 16 |
// room for more. I leave 1 byte for the trailing '\0' |
| 17 |
c_result = psize; |
| 18 |
if(c_result > 127) c_result = 127; |
| 19 |
strncpy(inp_name, chars, c_result); |
| 20 |
inp_name[c_result] = '\0'; |
| 21 |
} |
| 22 |
%enddef |
| 23 |
|
| 24 |
%define SET_FORMAT_STRING_TYPEMAP_NEW(name1, name2) |
| 25 |
%typemap(directorin) (char* name1) { |
| 26 |
// deliberately keep this empty in order to ignore the input parameter |
| 27 |
} |
| 28 |
%typemap(directorout) (int name2) { |
| 29 |
Py_ssize_t psize; |
| 30 |
psize = 0; |
| 31 |
const char* chars = PyUnicode_AsUTF8AndSize(result, &psize); |
| 32 |
// Fl_Valuator::format() writes 128-bytes only. The output buffer may not have |
| 33 |
// room for more. I leave 1 byte for the trailing '\0' |
| 34 |
c_result = psize; |
| 35 |
if(c_result > 127) c_result = 127; |
| 36 |
strncpy(name1, chars, c_result); |
| 37 |
name1[c_result] = '\0'; |
| 38 |
} |
| 39 |
%enddef |
| 40 |
|
| 41 |
%define CLEAR_FORMAT_STRING_OUT_TYPEMAP(type, name) |
| 42 |
%typemap(directorout) (type name) {} |
| 43 |
%enddef |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
// macro to delegate the ownership of a class to C++ |
| 48 |
%define CHANGE_OWNERSHIP(name) |
| 49 |
#include "CallbackStruct.h" |
| 50 |
%pythonappend name##::##name %{ |
| 51 |
if len(args) == 5: |
| 52 |
# retain reference to label |
| 53 |
self.my_label = args[-1] |
| 54 |
if self.parent() != None: |
| 55 |
# delegate ownership to C++ |
| 56 |
self.this.disown() |
| 57 |
self.init_type("name") |
| 58 |
#print("Adding type: ", name) |
| 59 |
%} |
| 60 |
%extend name { |
| 61 |
void init_type(char* name) { |
| 62 |
CallbackStruct *cb = new CallbackStruct( 0, 0, 0, 0 ); |
| 63 |
memcpy(cb->type_name, name, strlen(name)); |
| 64 |
self->user_data(cb); |
| 65 |
} |
| 66 |
} |
| 67 |
%enddef |
| 68 |
|
| 69 |
// macro to revert the ownership |
| 70 |
%define REVERT_OWNERSHIP(name) |
| 71 |
%pythonappend name %{ |
| 72 |
#self = args[0] |
| 73 |
if self.parent() != None: |
| 74 |
#delegate ownership to C++ |
| 75 |
self.this.disown() |
| 76 |
else: |
| 77 |
#give ownership back to Python |
| 78 |
self.this.own() |
| 79 |
%} |
| 80 |
%enddef |
| 81 |
|
| 82 |
%define DEFINE_CALLBACK(name) |
| 83 |
#include "CallbackStruct.h" |
| 84 |
%{ |
| 85 |
#include "CallbackStruct.h" |
| 86 |
#include <FL/Fl_Button.H> |
| 87 |
|
| 88 |
//static PyObject *my_pycallback = NULL; |
| 89 |
static void name ## _PythonCallBack(name *widget, void *clientdata) |
| 90 |
{ |
| 91 |
PyObject *func, *arglist; |
| 92 |
PyObject *result; |
| 93 |
PyObject *obj = 0; |
| 94 |
CallbackStruct* cb = (CallbackStruct*)clientdata; |
| 95 |
|
| 96 |
// This is the function .... |
| 97 |
func = cb->func; |
| 98 |
|
| 99 |
if (cb->widget != 0) { |
| 100 |
// the parent widget |
| 101 |
obj = (PyObject *)( ((CallbackStruct *)clientdata)->widget); |
| 102 |
} |
| 103 |
else if (cb->type != 0) { |
| 104 |
// this is the type of widget |
| 105 |
swig_type_info *descr = (swig_type_info *)cb->type; |
| 106 |
if (descr != 0) { |
| 107 |
//printf("success\n"); |
| 108 |
obj = SWIG_NewPointerObj(widget, (swig_type_info *)descr, 0); |
| 109 |
} |
| 110 |
} |
| 111 |
if (obj == 0) { |
| 112 |
// generic fallback |
| 113 |
obj = SWIG_NewPointerObj(widget, SWIGTYPE_p_ ## name, 0); |
| 114 |
} |
| 115 |
|
| 116 |
if (((CallbackStruct *)clientdata)->data) |
| 117 |
{ |
| 118 |
arglist = Py_BuildValue("(OO)", obj, (PyObject *)(((CallbackStruct *)clientdata)->data) ); |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
arglist = Py_BuildValue("(O)", obj ); |
| 123 |
} |
| 124 |
|
| 125 |
result = PyObject_CallObject(func, arglist); |
| 126 |
|
| 127 |
//Py_XDECREF(arglist); // Trash arglist |
| 128 |
Py_XDECREF(result); |
| 129 |
PyObject *error = PyErr_Occurred(); |
| 130 |
{ |
| 131 |
if (error != NULL) { |
| 132 |
throw Swig::DirectorMethodException(); |
| 133 |
} |
| 134 |
} |
| 135 |
//if (PyErr_Occurred()) |
| 136 |
//{ |
| 137 |
// PyErr_Print(); |
| 138 |
//} |
| 139 |
|
| 140 |
return /*void*/; |
| 141 |
} |
| 142 |
%} |
| 143 |
%enddef |
| 144 |
|
| 145 |
%define ADD_CALLBACK(name) |
| 146 |
|
| 147 |
%extend name { |
| 148 |
void |
| 149 |
callback(PyObject *PyFunc, PyObject *PyWidget, PyObject *PyData = 0) |
| 150 |
{ |
| 151 |
//CallbackStruct *cb = 0; |
| 152 |
CallbackStruct *cb = (CallbackStruct*)self->user_data(); |
| 153 |
|
| 154 |
if (cb) { |
| 155 |
cb->func = PyFunc; |
| 156 |
cb->widget = PyWidget; |
| 157 |
if (PyData) { |
| 158 |
cb->data = PyData; |
| 159 |
} |
| 160 |
cb->widget = PyWidget; |
| 161 |
} |
| 162 |
else |
| 163 |
cb = new CallbackStruct( PyFunc, PyData, PyWidget ); |
| 164 |
|
| 165 |
// Add a reference to new callback |
| 166 |
Py_INCREF(PyFunc); |
| 167 |
Py_XINCREF(PyData); |
| 168 |
Py_XINCREF(PyWidget); |
| 169 |
|
| 170 |
self->callback(name ## _PythonCallBack, (void *)cb); |
| 171 |
|
| 172 |
|
| 173 |
} |
| 174 |
} |
| 175 |
%enddef |
| 176 |
|
| 177 |
|
| 178 |
%define ADD_USERDATA(name) |
| 179 |
#include "CallbackStruct.h" |
| 180 |
%extend name { |
| 181 |
void |
| 182 |
user_data(PyObject *PyData) |
| 183 |
{ |
| 184 |
// Add a reference to new callback |
| 185 |
Py_XINCREF(PyData); |
| 186 |
|
| 187 |
CallbackStruct *cb = (CallbackStruct*)self->user_data(); |
| 188 |
if (cb == NULL) { |
| 189 |
cb = new CallbackStruct(0, PyData, (PyObject*)0); |
| 190 |
self->user_data((void *)cb); |
| 191 |
} |
| 192 |
else { |
| 193 |
if (cb->data != NULL) |
| 194 |
Py_XDECREF(cb->data); |
| 195 |
cb->data = PyData; |
| 196 |
} |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
PyObject* user_data() { |
| 201 |
PyObject *obj = 0; |
| 202 |
CallbackStruct *cb = (CallbackStruct*)self->user_data(); |
| 203 |
if (cb) { |
| 204 |
if (cb->data) { |
| 205 |
obj = (PyObject*)cb->data; |
| 206 |
|
| 207 |
Py_XINCREF(obj); |
| 208 |
return obj; |
| 209 |
} |
| 210 |
} |
| 211 |
// nothing found |
| 212 |
Py_RETURN_NONE; |
| 213 |
} |
| 214 |
} |
| 215 |
%enddef |