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