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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2012 Miguel Angel Ajo <miguelangel@nbee.es>
* Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file wx.i
* @brief wx wrappers for basic things, wxString, wxPoint, wxRect, etc..
* all the wx objects are very complex, and we don't want to pull
* and swig all depending objects, so we just define the methods
* we want to wrap.
*/
%{
#include <wx_python_helpers.h>
%}
// encoding setup, ascii by default ///////////////////////////////////////////
void wxSetDefaultPyEncoding(const char* encoding);
const char* wxGetDefaultPyEncoding();
// wxRect class wrapper ///////////////////////////////////////////////////////
class wxRect
{
public:
wxRect() : x(0), y(0), width(0), height(0) { }
wxRect(int xx, int yy, int ww, int hh): x(xx), y(yy), width(ww), height(hh) { }
wxRect(const wxPoint& topLeft, const wxPoint& bottomRight);
wxRect(const wxPoint& pt, const wxSize& size)
: x(pt.x), y(pt.y), width(size.x), height(size.y) { }
wxRect(const wxSize& size): x(0), y(0), width(size.x), height(size.y) { }
int GetX() const { return x; }
void SetX(int xx) { x = xx; }
int GetY() const { return y; }
void SetY(int yy) { y = yy; }
int GetWidth() const { return width; }
void SetWidth(int w) { width = w; }
int GetHeight() const { return height; }
void SetHeight(int h) { height = h; }
wxPoint GetPosition() const { return wxPoint(x, y); }
void SetPosition( const wxPoint &p ) { x = p.x; y = p.y; }
int x, y, width, height;
%extend
{
/* extend the wxRect object so it can be converted into a tuple */
PyObject* Get()
{
PyObject* res = PyTuple_New(4);
PyTuple_SET_ITEM(res, 0, PyInt_FromLong(self->x));
PyTuple_SET_ITEM(res, 1, PyInt_FromLong(self->y));
PyTuple_SET_ITEM(res, 2, PyInt_FromLong(self->width));
PyTuple_SET_ITEM(res, 3, PyInt_FromLong(self->height));
return res;
}
}
%pythoncode
{
def __eq__(self,other):
return self.x==other.x and self.y==other.y and self.width==other.width and self.height==other.height
def __str__(self): return str(self.Get())
def __repr__(self): return 'wxRect'+str(self.Get())
def __len__(self): return len(self.Get())
def __getitem__(self, index): return self.Get()[index]
def __setitem__(self, index, val):
if index == 0: self.SetX(val)
elif index == 1: self.SetY(val)
elif index == 2: self.SetWidth(val)
elif index == 3: self.SetHeight(val)
else: raise IndexError
def __nonzero__(self): return self.Get() != (0,0,0,0)
__safe_for_unpickling__ = True
}
};
// wxSize class wrapper ///////////////////////////////////////////////////////
class wxSize
{
public:
int x,y;
wxSize(int xx, int yy) : x(xx), y(yy) { }
wxSize(double xx, double yy) : x(xx), y(yy) {}
%extend
{
PyObject* Get()
{
PyObject* res = PyTuple_New(2);
PyTuple_SET_ITEM(res, 0, PyInt_FromLong(self->x));
PyTuple_SET_ITEM(res, 1, PyInt_FromLong(self->y));
return res;
}
}
~wxSize();
void SetWidth(int w);
void SetHeight(int h);
int GetWidth() const;
int GetHeight() const;
%pythoncode
{
def Scale(self,xscale,yscale):
return wxSize(self.x*xscale,self.y*yscale)
def __eq__(self,other):
return self.GetWidth()==other.GetWidth() and self.GetHeight()==other.GetHeight()
def __str__(self): return str(self.Get())
def __repr__(self): return 'wxSize'+str(self.Get())
def __len__(self): return len(self.Get())
def __getitem__(self, index): return self.Get()[index]
def __setitem__(self, index, val):
if index == 0: self.SetWidth(val)
elif index == 1: self.SetHeight(val)
else: raise IndexError
def __nonzero__(self): return self.Get() != (0,0)
__safe_for_unpickling__ = True
}
};
// wxPoint class wrapper to (xx,yy) tuple /////////////////////////////////////
class wxPoint
{
public:
int x, y;
wxPoint(int xx, int yy);
wxPoint(double xx, double yy) : x(xx), y(yy) {}
~wxPoint();
%extend {
wxPoint __add__(const wxPoint& pt) { return *self + pt; }
wxPoint __sub__(const wxPoint& pt) { return *self - pt; }
void Set(long x, long y) { self->x = x; self->y = y; }
PyObject* Get()
{
PyObject* tup = PyTuple_New(2);
PyTuple_SET_ITEM(tup, 0, PyInt_FromLong(self->x));
PyTuple_SET_ITEM(tup, 1, PyInt_FromLong(self->y));
return tup;
}
}
%pythoncode {
def __eq__(self,other): return (self.x==other.x and self.y==other.y)
def __ne__(self,other): return not (self==other)
def __str__(self): return str(self.Get())
def __repr__(self): return 'wxPoint'+str(self.Get())
def __len__(self): return len(self.Get())
def __getitem__(self, index): return self.Get()[index]
def __setitem__(self, index, val):
if index == 0:
self.x = val
elif index == 1:
self.y = val
else:
raise IndexError
def __nonzero__(self): return self.Get() != (0,0)
}
};
// wxChar typemaps ///////////////////////////////////////////////////////////
/* they handle the conversion from/to strings */
%typemap(in) wxChar { wxString str = Py2wxString($input); $1 = str[0]; }
%typemap(out) wxChar { wxString str($1); $result = wx2PyString(str); }
// wxString wrappers /////////////////////////////////////////////////////////
%typemap(out) wxString&
{
%#if wxUSE_UNICODE
$result = PyUnicode_FromWideChar($1->c_str(), $1->Len());
%#else
$result = PyString_FromStringAndSize($1->c_str(), $1->Len());
%#endif
}
%apply wxString& { wxString* }
%typemap(out) wxString
{
%#if wxUSE_UNICODE
$result = PyUnicode_FromWideChar($1.c_str(), $1.Len());
%#else
$result = PyString_FromStringAndSize($1.c_str(), $1.Len());
%#endif
}
%typemap(varout) wxString
{
%#if wxUSE_UNICODE
$result = PyUnicode_FromWideChar($1.c_str(), $1.Len());
%#else
$result = PyString_FromStringAndSize($1.c_str(), $1.Len());
%#endif
}
%typemap(in) wxString& (bool temp=false)
{
$1 = newWxStringFromPy($input);
if ($1 == NULL) SWIG_fail;
temp = true;
}
%typemap(freearg) wxString&
{
if (temp$argnum)
delete $1;
}
%typemap(in) wxString {
wxString* sptr = newWxStringFromPy($input);
if (sptr == NULL) SWIG_fail;
$1 = *sptr;
delete sptr;
}
%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxString& {
$1 = PyString_Check($input) || PyUnicode_Check($input);
}
// wxArrayString wrappers //////////////////////////////////////////////////////
%typemap(in) wxArrayString& (bool temp=false) {
if (!PySequence_Check($input))
{
PyErr_SetString(PyExc_TypeError, "Not a sequence of strings");
SWIG_fail;
}
$1 = new wxArrayString;
temp = true;
int last=PySequence_Length($input);
for (int i=0; i<last; i++)
{
PyObject* pyStr = PySequence_GetItem($input, i);
wxString* wxS = newWxStringFromPy(pyStr);
if (PyErr_Occurred())
SWIG_fail;
$1->Add(*wxS);
delete wxS;
Py_DECREF(pyStr);
}
}
%typemap(freearg) wxArrayString&
{
if (temp$argnum)
delete $1;
}
%typemap(out) wxArrayString&
{
$result = wxArrayString2PyList(*$1);
}
%typemap(out) wxArrayString
{
$result = wxArrayString2PyList($1);
}
%template(wxPoint_Vector) std::vector<wxPoint>;
|