You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
8.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 Miguel Angel Ajo <miguelangel@nbee.es>
  5. * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file wx.i
  26. * @brief wx wrappers for basic things, wxString, wxPoint, wxRect, etc..
  27. * all the wx objects are very complex, and we don't want to pull
  28. * and swig all depending objects, so we just define the methods
  29. * we want to wrap.
  30. */
  31. %{
  32. #include <wx_python_helpers.h>
  33. %}
  34. // encoding setup, ascii by default ///////////////////////////////////////////
  35. void wxSetDefaultPyEncoding(const char* encoding);
  36. const char* wxGetDefaultPyEncoding();
  37. // wxRect class wrapper ///////////////////////////////////////////////////////
  38. class wxRect
  39. {
  40. public:
  41. wxRect() : x(0), y(0), width(0), height(0) { }
  42. wxRect(int xx, int yy, int ww, int hh): x(xx), y(yy), width(ww), height(hh) { }
  43. wxRect(const wxPoint& topLeft, const wxPoint& bottomRight);
  44. wxRect(const wxPoint& pt, const wxSize& size)
  45. : x(pt.x), y(pt.y), width(size.x), height(size.y) { }
  46. wxRect(const wxSize& size): x(0), y(0), width(size.x), height(size.y) { }
  47. int GetX() const { return x; }
  48. void SetX(int xx) { x = xx; }
  49. int GetY() const { return y; }
  50. void SetY(int yy) { y = yy; }
  51. int GetWidth() const { return width; }
  52. void SetWidth(int w) { width = w; }
  53. int GetHeight() const { return height; }
  54. void SetHeight(int h) { height = h; }
  55. wxPoint GetPosition() const { return wxPoint(x, y); }
  56. void SetPosition( const wxPoint &p ) { x = p.x; y = p.y; }
  57. int x, y, width, height;
  58. %extend
  59. {
  60. /* extend the wxRect object so it can be converted into a tuple */
  61. PyObject* Get()
  62. {
  63. PyObject* res = PyTuple_New(4);
  64. PyTuple_SET_ITEM(res, 0, PyInt_FromLong(self->x));
  65. PyTuple_SET_ITEM(res, 1, PyInt_FromLong(self->y));
  66. PyTuple_SET_ITEM(res, 2, PyInt_FromLong(self->width));
  67. PyTuple_SET_ITEM(res, 3, PyInt_FromLong(self->height));
  68. return res;
  69. }
  70. }
  71. %pythoncode
  72. {
  73. def __eq__(self,other):
  74. return self.x==other.x and self.y==other.y and self.width==other.width and self.height==other.height
  75. def __str__(self): return str(self.Get())
  76. def __repr__(self): return 'wxRect'+str(self.Get())
  77. def __len__(self): return len(self.Get())
  78. def __getitem__(self, index): return self.Get()[index]
  79. def __setitem__(self, index, val):
  80. if index == 0: self.SetX(val)
  81. elif index == 1: self.SetY(val)
  82. elif index == 2: self.SetWidth(val)
  83. elif index == 3: self.SetHeight(val)
  84. else: raise IndexError
  85. def __nonzero__(self): return self.Get() != (0,0,0,0)
  86. __safe_for_unpickling__ = True
  87. }
  88. };
  89. // wxSize class wrapper ///////////////////////////////////////////////////////
  90. class wxSize
  91. {
  92. public:
  93. int x,y;
  94. wxSize(int xx, int yy) : x(xx), y(yy) { }
  95. wxSize(double xx, double yy) : x(xx), y(yy) {}
  96. %extend
  97. {
  98. PyObject* Get()
  99. {
  100. PyObject* res = PyTuple_New(2);
  101. PyTuple_SET_ITEM(res, 0, PyInt_FromLong(self->x));
  102. PyTuple_SET_ITEM(res, 1, PyInt_FromLong(self->y));
  103. return res;
  104. }
  105. }
  106. ~wxSize();
  107. void SetWidth(int w);
  108. void SetHeight(int h);
  109. int GetWidth() const;
  110. int GetHeight() const;
  111. %pythoncode
  112. {
  113. def Scale(self,xscale,yscale):
  114. return wxSize(self.x*xscale,self.y*yscale)
  115. def __eq__(self,other):
  116. return self.GetWidth()==other.GetWidth() and self.GetHeight()==other.GetHeight()
  117. def __str__(self): return str(self.Get())
  118. def __repr__(self): return 'wxSize'+str(self.Get())
  119. def __len__(self): return len(self.Get())
  120. def __getitem__(self, index): return self.Get()[index]
  121. def __setitem__(self, index, val):
  122. if index == 0: self.SetWidth(val)
  123. elif index == 1: self.SetHeight(val)
  124. else: raise IndexError
  125. def __nonzero__(self): return self.Get() != (0,0)
  126. __safe_for_unpickling__ = True
  127. }
  128. };
  129. // wxPoint class wrapper to (xx,yy) tuple /////////////////////////////////////
  130. class wxPoint
  131. {
  132. public:
  133. int x, y;
  134. wxPoint(int xx, int yy);
  135. wxPoint(double xx, double yy) : x(xx), y(yy) {}
  136. ~wxPoint();
  137. %extend {
  138. wxPoint __add__(const wxPoint& pt) { return *self + pt; }
  139. wxPoint __sub__(const wxPoint& pt) { return *self - pt; }
  140. void Set(long x, long y) { self->x = x; self->y = y; }
  141. PyObject* Get()
  142. {
  143. PyObject* tup = PyTuple_New(2);
  144. PyTuple_SET_ITEM(tup, 0, PyInt_FromLong(self->x));
  145. PyTuple_SET_ITEM(tup, 1, PyInt_FromLong(self->y));
  146. return tup;
  147. }
  148. }
  149. %pythoncode {
  150. def __eq__(self,other): return (self.x==other.x and self.y==other.y)
  151. def __ne__(self,other): return not (self==other)
  152. def __str__(self): return str(self.Get())
  153. def __repr__(self): return 'wxPoint'+str(self.Get())
  154. def __len__(self): return len(self.Get())
  155. def __getitem__(self, index): return self.Get()[index]
  156. def __setitem__(self, index, val):
  157. if index == 0:
  158. self.x = val
  159. elif index == 1:
  160. self.y = val
  161. else:
  162. raise IndexError
  163. def __nonzero__(self): return self.Get() != (0,0)
  164. }
  165. };
  166. // wxChar typemaps ///////////////////////////////////////////////////////////
  167. /* they handle the conversion from/to strings */
  168. %typemap(in) wxChar { wxString str = Py2wxString($input); $1 = str[0]; }
  169. %typemap(out) wxChar { wxString str($1); $result = wx2PyString(str); }
  170. // wxString wrappers /////////////////////////////////////////////////////////
  171. %typemap(out) wxString&
  172. {
  173. %#if wxUSE_UNICODE
  174. $result = PyUnicode_FromWideChar($1->c_str(), $1->Len());
  175. %#else
  176. $result = PyString_FromStringAndSize($1->c_str(), $1->Len());
  177. %#endif
  178. }
  179. %apply wxString& { wxString* }
  180. %typemap(out) wxString
  181. {
  182. %#if wxUSE_UNICODE
  183. $result = PyUnicode_FromWideChar($1.c_str(), $1.Len());
  184. %#else
  185. $result = PyString_FromStringAndSize($1.c_str(), $1.Len());
  186. %#endif
  187. }
  188. %typemap(varout) wxString
  189. {
  190. %#if wxUSE_UNICODE
  191. $result = PyUnicode_FromWideChar($1.c_str(), $1.Len());
  192. %#else
  193. $result = PyString_FromStringAndSize($1.c_str(), $1.Len());
  194. %#endif
  195. }
  196. %typemap(in) wxString& (bool temp=false)
  197. {
  198. $1 = newWxStringFromPy($input);
  199. if ($1 == NULL) SWIG_fail;
  200. temp = true;
  201. }
  202. %typemap(freearg) wxString&
  203. {
  204. if (temp$argnum)
  205. delete $1;
  206. }
  207. %typemap(in) wxString {
  208. wxString* sptr = newWxStringFromPy($input);
  209. if (sptr == NULL) SWIG_fail;
  210. $1 = *sptr;
  211. delete sptr;
  212. }
  213. %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxString& {
  214. $1 = PyString_Check($input) || PyUnicode_Check($input);
  215. }
  216. // wxArrayString wrappers //////////////////////////////////////////////////////
  217. %typemap(in) wxArrayString& (bool temp=false) {
  218. if (!PySequence_Check($input))
  219. {
  220. PyErr_SetString(PyExc_TypeError, "Not a sequence of strings");
  221. SWIG_fail;
  222. }
  223. $1 = new wxArrayString;
  224. temp = true;
  225. int last=PySequence_Length($input);
  226. for (int i=0; i<last; i++)
  227. {
  228. PyObject* pyStr = PySequence_GetItem($input, i);
  229. wxString* wxS = newWxStringFromPy(pyStr);
  230. if (PyErr_Occurred())
  231. SWIG_fail;
  232. $1->Add(*wxS);
  233. delete wxS;
  234. Py_DECREF(pyStr);
  235. }
  236. }
  237. %typemap(freearg) wxArrayString&
  238. {
  239. if (temp$argnum)
  240. delete $1;
  241. }
  242. %typemap(out) wxArrayString&
  243. {
  244. $result = wxArrayString2PyList(*$1);
  245. }
  246. %typemap(out) wxArrayString
  247. {
  248. $result = wxArrayString2PyList($1);
  249. }
  250. %template(wxPoint_Vector) std::vector<wxPoint>;