File: macros.i

package info (click to toggle)
pyfltk 1.4.4.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,556 kB
  • sloc: python: 7,769; cpp: 356; makefile: 24
file content (172 lines) | stat: -rw-r--r-- 4,017 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
%include <std_string.i>
%include <typemaps.i>

// macro to delegate the ownership of a class to C++
%define CHANGE_OWNERSHIP(name)
#include "CallbackStruct.h"
%pythonappend name##::##name %{
if len(args) == 5:          
    # retain reference to label
    self.my_label = args[-1]
if self.parent() != None:   
    # delegate ownership to C++
    self.this.disown()
self.init_type("name")
#print("Adding type: ", name)
%}
%extend name {
  void init_type(char* name) {
    CallbackStruct *cb = new CallbackStruct( 0, 0, 0, 0 );
    memcpy(cb->type_name, name, strlen(name));
    self->user_data(cb);
  }
 }
%enddef

// macro to revert the ownership
%define REVERT_OWNERSHIP(name)
%pythonappend name %{
#self = args[0]
if self.parent() != None:   
    #delegate ownership to C++
    self.this.disown()
else:                       
    #give ownership back to Python
    self.this.own() 
%}
%enddef

%define DEFINE_CALLBACK(name)
#include "CallbackStruct.h"
%{
#include "CallbackStruct.h"
#include <FL/Fl_Button.H>

  //static PyObject *my_pycallback = NULL;
  static void name ## _PythonCallBack(name *widget, void *clientdata)
    {
      PyObject *func, *arglist;
      PyObject *result;
      PyObject *obj = 0;
      CallbackStruct* cb = (CallbackStruct*)clientdata;

      // This is the function .... 
      func = cb->func;

      if (cb->widget != 0) {
        // the parent widget
        obj = (PyObject *)( ((CallbackStruct *)clientdata)->widget);
      }
      else if (cb->type != 0) {
        // this is the type of widget
        swig_type_info *descr = (swig_type_info *)cb->type;
        if (descr != 0) {
          //printf("success\n");
          obj = SWIG_NewPointerObj(widget, (swig_type_info *)descr, 0);
        }
      }
      if (obj == 0) {
        // generic fallback
        obj = SWIG_NewPointerObj(widget, SWIGTYPE_p_ ## name, 0);
      }

      if (((CallbackStruct *)clientdata)->data)
	{
	  arglist = Py_BuildValue("(OO)", obj, (PyObject *)(((CallbackStruct *)clientdata)->data) ); 
	}
      else
	{
	  arglist = Py_BuildValue("(O)", obj ); 
	}

      result =  PyObject_CallObject(func, arglist);
   
      //Py_XDECREF(arglist);                           // Trash arglist
      Py_XDECREF(result);
      PyObject *error = PyErr_Occurred();
      {
	if (error != NULL) {
	  throw Swig::DirectorMethodException();
	}
      }
      //if (PyErr_Occurred())
      //{
      //  PyErr_Print();
      //}
   
      return /*void*/;
    }
  %}
%enddef

%define ADD_CALLBACK(name)

%extend name {
  void
    callback(PyObject *PyFunc, PyObject *PyWidget, PyObject *PyData = 0)
    {
      //CallbackStruct *cb = 0;
      CallbackStruct *cb = (CallbackStruct*)self->user_data();

      if (cb) {
	cb->func = PyFunc;
	cb->widget = PyWidget;
	if (PyData) {
	  cb->data = PyData;
	}
	cb->widget = PyWidget;
      }
      else
	cb = new CallbackStruct( PyFunc, PyData, PyWidget );

      // Add a reference to new callback
      Py_INCREF(PyFunc);			
      Py_XINCREF(PyData);
      Py_XINCREF(PyWidget);

      self->callback(name ## _PythonCallBack, (void *)cb);

    
    }
}
%enddef


%define ADD_USERDATA(name)
#include "CallbackStruct.h"
%extend name {
   void
    user_data(PyObject *PyData)
    {
      // Add a reference to new callback
      Py_XINCREF(PyData);
	
      CallbackStruct *cb = (CallbackStruct*)self->user_data();
      if (cb == NULL) {
	cb = new CallbackStruct(0, PyData, (PyObject*)0);
	self->user_data((void *)cb);
      }
      else {
	if (cb->data != NULL)
	  Py_XDECREF(cb->data);
	cb->data = PyData;
      }
    
    }

  PyObject* user_data() {
    PyObject *obj = 0;
    CallbackStruct *cb = (CallbackStruct*)self->user_data();
    if (cb) {
      if (cb->data) {
	obj = (PyObject*)cb->data;

	Py_XINCREF(obj);
	return obj;
      }
    }
    // nothing found
    Py_RETURN_NONE;
  }
}
%enddef