]>
Commit | Line | Data |
---|---|---|
1e4a197e RD |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: drawlist.cpp | |
3 | // Purpose: Helper functions for optimized list drawing on a wxDC | |
4 | // | |
d14a1e28 | 5 | // Author: Robin Dunn, Chris Barker |
1e4a197e RD |
6 | // |
7 | // Created: | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 2003 by Total Control Software | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | ||
14 | #undef DEBUG | |
15 | #include <Python.h> | |
d14a1e28 RD |
16 | #include "wx/wxPython/wxPython.h" |
17 | #include "wx/wxPython/pydrawxxx.h" | |
1e4a197e RD |
18 | |
19 | ||
20 | //---------------------------------------------------------------------- | |
21 | ||
22 | ||
23 | ||
24 | PyObject* wxPyDrawXXXList(wxDC& dc, wxPyDrawListOp_t doDraw, | |
25 | PyObject* pyCoords, PyObject* pyPens, PyObject* pyBrushes) { | |
26 | ||
d14a1e28 | 27 | wxPyBeginBlockThreads(); |
1e4a197e | 28 | |
d14a1e28 RD |
29 | if ( !wxPyCoreAPIPtr) |
30 | wxPyCoreAPI_IMPORT(); | |
31 | ||
1e4a197e RD |
32 | bool isFastSeq = PyList_Check(pyCoords) || PyTuple_Check(pyCoords); |
33 | bool isFastPens = PyList_Check(pyPens) || PyTuple_Check(pyPens); | |
34 | bool isFastBrushes = PyList_Check(pyBrushes) || PyTuple_Check(pyBrushes); | |
35 | int numObjs = 0; | |
36 | int numPens = 0; | |
37 | int numBrushes = 0; | |
38 | wxPen* pen; | |
39 | wxBrush* brush; | |
40 | PyObject* obj; | |
41 | PyObject* coords; | |
1e4a197e RD |
42 | int i = 0; |
43 | PyObject* retval; | |
44 | ||
45 | if (!PySequence_Check(pyCoords)) { | |
46 | goto err0; | |
47 | } | |
48 | if (!PySequence_Check(pyPens)) { | |
49 | goto err1; | |
50 | } | |
51 | if (!PySequence_Check(pyBrushes)) { | |
52 | goto err2; | |
53 | } | |
54 | numObjs = PySequence_Length(pyCoords); | |
55 | numPens = PySequence_Length(pyPens); | |
56 | numBrushes = PySequence_Length(pyBrushes); | |
57 | for (i = 0; i < numObjs; i++) { | |
58 | // Use a new pen? | |
59 | if (i < numPens) { | |
60 | if (isFastPens) { | |
61 | obj = PySequence_Fast_GET_ITEM(pyPens, i); | |
62 | } | |
63 | else { | |
64 | obj = PySequence_GetItem(pyPens, i); | |
65 | } | |
d14a1e28 | 66 | if (! wxPyConvertSwigPtr(obj, (void **) &pen, wxT("wxPen"))) { |
1e4a197e RD |
67 | if (!isFastPens) |
68 | Py_DECREF(obj); | |
69 | goto err1; | |
70 | } | |
71 | ||
72 | dc.SetPen(*pen); | |
73 | if (!isFastPens) | |
74 | Py_DECREF(obj); | |
75 | } | |
76 | // Use a new brush? | |
77 | if (i < numBrushes) { | |
78 | if (isFastBrushes) { | |
79 | obj = PySequence_Fast_GET_ITEM(pyBrushes, i); | |
80 | } | |
81 | else { | |
82 | obj = PySequence_GetItem(pyBrushes, i); | |
83 | } | |
d14a1e28 | 84 | if (!wxPyConvertSwigPtr(obj, (void **) &brush, wxT("wxBrush"))) { |
1e4a197e RD |
85 | if (!isFastBrushes) |
86 | Py_DECREF(obj); | |
87 | goto err2; | |
88 | } | |
89 | ||
90 | dc.SetBrush(*brush); | |
91 | if (!isFastBrushes) | |
92 | Py_DECREF(obj); | |
93 | } | |
94 | ||
95 | // Get the Coordinates | |
96 | if (isFastSeq) { | |
97 | coords = PySequence_Fast_GET_ITEM(pyCoords, i); | |
98 | } | |
99 | else { | |
100 | coords = PySequence_GetItem(pyCoords, i); | |
101 | } | |
102 | ||
103 | ||
104 | // call the drawOp | |
105 | bool success = doDraw(dc, coords); | |
106 | if (!isFastSeq) | |
107 | Py_DECREF(coords); | |
108 | ||
109 | if (! success) { | |
110 | retval = NULL; | |
111 | goto exit; | |
112 | } | |
113 | ||
114 | } // end of main for loop | |
115 | ||
116 | Py_INCREF(Py_None); | |
117 | retval = Py_None; | |
118 | goto exit; | |
119 | ||
120 | ||
121 | err0: | |
122 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of coordinates"); | |
123 | retval = NULL; | |
124 | goto exit; | |
125 | ||
126 | err1: | |
127 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of wxPens"); | |
128 | retval = NULL; | |
129 | goto exit; | |
130 | ||
131 | err2: | |
132 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of wxBrushes"); | |
133 | retval = NULL; | |
134 | goto exit; | |
135 | ||
136 | ||
137 | exit: | |
138 | wxPyEndBlockThreads(); | |
139 | return retval; | |
140 | } | |
141 | ||
142 | ||
143 | ||
144 | bool wxPyDrawXXXPoint(wxDC& dc, PyObject* coords) { | |
145 | int x, y; | |
146 | ||
147 | if (! wxPy2int_seq_helper(coords, &x, &y)) { | |
148 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of (x,y) sequences."); | |
dd9f7fea | 149 | return False; |
1e4a197e RD |
150 | } |
151 | dc.DrawPoint(x, y); | |
dd9f7fea | 152 | return True; |
1e4a197e RD |
153 | } |
154 | ||
155 | bool wxPyDrawXXXLine(wxDC& dc, PyObject* coords) { | |
156 | int x1, y1, x2, y2; | |
157 | ||
158 | if (! wxPy4int_seq_helper(coords, &x1, &y1, &x2, &y2)) { | |
159 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of (x1,y1, x1,y2) sequences."); | |
dd9f7fea | 160 | return False; |
1e4a197e RD |
161 | } |
162 | dc.DrawLine(x1,y1, x2,y2); | |
dd9f7fea | 163 | return True; |
1e4a197e RD |
164 | } |
165 | ||
166 | bool wxPyDrawXXXRectangle(wxDC& dc, PyObject* coords) { | |
167 | int x, y, w, h; | |
168 | ||
169 | if (! wxPy4int_seq_helper(coords, &x, &y, &w, &h)) { | |
170 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of (x,y, w,h) sequences."); | |
dd9f7fea | 171 | return False; |
1e4a197e RD |
172 | } |
173 | dc.DrawRectangle(x, y, w, h); | |
dd9f7fea | 174 | return True; |
1e4a197e RD |
175 | } |
176 | ||
177 | bool wxPyDrawXXXEllipse(wxDC& dc, PyObject* coords) { | |
178 | int x, y, w, h; | |
179 | ||
180 | if (! wxPy4int_seq_helper(coords, &x, &y, &w, &h)) { | |
181 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of (x,y, w,h) sequences."); | |
dd9f7fea | 182 | return False; |
1e4a197e RD |
183 | } |
184 | dc.DrawEllipse(x, y, w, h); | |
dd9f7fea | 185 | return True; |
1e4a197e RD |
186 | } |
187 | ||
188 | ||
189 | bool wxPyDrawXXXPolygon(wxDC& dc, PyObject* coords) { | |
190 | wxPoint* points; | |
191 | int numPoints; | |
192 | ||
193 | points = wxPoint_LIST_helper(coords, &numPoints); | |
194 | if (! points) { | |
195 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of sequences of (x,y) sequences."); | |
dd9f7fea | 196 | return False; |
1e4a197e RD |
197 | } |
198 | dc.DrawPolygon(numPoints, points); | |
dd9f7fea | 199 | return True; |
1e4a197e RD |
200 | } |
201 | ||
202 | ||
203 | //---------------------------------------------------------------------- | |
204 | ||
205 | ||
206 | ||
207 | PyObject* wxPyDrawTextList(wxDC& dc, PyObject* textList, PyObject* pyPoints, PyObject* foregroundList, PyObject* backgroundList) { | |
208 | wxPyBeginBlockThreads(); | |
209 | ||
d14a1e28 RD |
210 | if ( !wxPyCoreAPIPtr) |
211 | wxPyCoreAPI_IMPORT(); | |
212 | ||
1e4a197e RD |
213 | bool isFastSeq = PyList_Check(pyPoints) || PyTuple_Check(pyPoints); |
214 | bool isFastText = PyList_Check(textList) || PyTuple_Check(textList); | |
215 | bool isFastForeground = PyList_Check(foregroundList) || PyTuple_Check(foregroundList); | |
216 | bool isFastBackground = PyList_Check(backgroundList) || PyTuple_Check(backgroundList); | |
217 | int numText = 0; | |
218 | int numPoints = 0; | |
219 | int numForeground = 0; | |
220 | int numBackground = 0; | |
221 | PyObject* obj; | |
222 | int x1, y1; | |
223 | int i = 0; | |
224 | wxColor* color; | |
225 | PyObject* retval; | |
226 | wxString string; | |
227 | ||
228 | if (!PySequence_Check(pyPoints)) { | |
229 | goto err0; | |
230 | } | |
231 | if (!PySequence_Check(textList)) { | |
232 | goto err1; | |
233 | } | |
234 | if (!PySequence_Check(foregroundList)) { | |
235 | goto err2; | |
236 | } | |
237 | if (!PySequence_Check(backgroundList)) { | |
238 | goto err3; | |
239 | } | |
240 | numPoints = PySequence_Length(pyPoints); | |
241 | numText = PySequence_Length(textList); | |
242 | numForeground = PySequence_Length(foregroundList); | |
243 | numBackground = PySequence_Length(backgroundList); | |
244 | ||
245 | for (i = 0; i < numPoints; i++) { | |
246 | // Use a new string ? | |
247 | if (i < numText) { | |
248 | if ( isFastText ) { | |
249 | obj = PySequence_Fast_GET_ITEM(textList, i); | |
250 | } | |
251 | else { | |
252 | obj = PySequence_GetItem(textList, i); | |
253 | } | |
254 | if (! PyString_Check(obj) ) { | |
255 | Py_DECREF(obj); | |
256 | goto err1; | |
257 | } | |
258 | string = Py2wxString(obj); | |
259 | if ( !isFastText ) | |
260 | Py_DECREF(obj); | |
261 | } | |
262 | ||
263 | if (i < numForeground) { | |
264 | // Use a new foreground ? | |
265 | if ( isFastForeground ) { | |
266 | obj = PySequence_Fast_GET_ITEM(foregroundList, i); | |
267 | } | |
268 | else { | |
269 | obj = PySequence_GetItem(foregroundList, i); | |
270 | } | |
d14a1e28 | 271 | if (! wxPyConvertSwigPtr(obj, (void **) &color, wxT("wxColour_p"))) { |
1e4a197e RD |
272 | if (!isFastForeground) |
273 | Py_DECREF(obj); | |
274 | goto err2; | |
275 | } | |
276 | dc.SetTextForeground(*color); | |
277 | if ( !isFastForeground ) | |
278 | Py_DECREF(obj); | |
279 | } | |
280 | ||
281 | if (i < numBackground) { | |
282 | // Use a new background ? | |
283 | if ( isFastBackground ) { | |
284 | obj = PySequence_Fast_GET_ITEM(backgroundList, i); | |
285 | } | |
286 | else { | |
287 | obj = PySequence_GetItem(backgroundList, i); | |
288 | } | |
d14a1e28 | 289 | if (! wxPyConvertSwigPtr(obj, (void **) &color, wxT("wxColour"))) { |
1e4a197e RD |
290 | if (!isFastBackground) |
291 | Py_DECREF(obj); | |
292 | goto err3; | |
293 | } | |
294 | dc.SetTextBackground(*color); | |
295 | if ( !isFastBackground ) | |
296 | Py_DECREF(obj); | |
297 | } | |
298 | ||
299 | // Get the point coordinates | |
300 | if (isFastSeq) { | |
301 | obj = PySequence_Fast_GET_ITEM(pyPoints, i); | |
302 | } | |
303 | else { | |
304 | obj = PySequence_GetItem(pyPoints, i); | |
305 | } | |
306 | if (! wxPy2int_seq_helper(obj, &x1, &y1)) { | |
307 | if (! isFastSeq) | |
308 | Py_DECREF(obj); | |
309 | goto err0; | |
310 | } | |
311 | if (PyErr_Occurred()) { | |
312 | retval = NULL; | |
313 | if (!isFastSeq) | |
314 | Py_DECREF(obj); | |
315 | goto exit; | |
316 | } | |
317 | ||
318 | ||
319 | // Now draw the text | |
320 | dc.DrawText(string, x1, y1); | |
321 | ||
322 | if (!isFastText) | |
323 | Py_DECREF(obj); | |
324 | } | |
325 | ||
326 | Py_INCREF(Py_None); | |
327 | retval = Py_None; | |
328 | goto exit; | |
329 | ||
330 | err0: | |
331 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of (x,y) sequences."); | |
332 | retval = NULL; | |
333 | goto exit; | |
334 | err1: | |
335 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of strings"); | |
336 | retval = NULL; | |
337 | goto exit; | |
338 | ||
339 | err2: | |
340 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of wxColours for foregrounds"); | |
341 | retval = NULL; | |
342 | goto exit; | |
343 | ||
344 | err3: | |
345 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of wxColours for backgrounds"); | |
346 | retval = NULL; | |
347 | goto exit; | |
348 | ||
349 | exit: | |
350 | wxPyEndBlockThreads(); | |
351 | return retval; | |
352 | } | |
353 | ||
354 | ||
355 | ||
356 | //---------------------------------------------------------------------- |