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