forked from wxWidgets/Phoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdc_ex.cpp
517 lines (435 loc) · 13.6 KB
/
dc_ex.cpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//--------------------------------------------------------------------------
// Name: src/dc_ex.h
// Purpose: Functions that can quickly draw lists of items on a DC
//
// Author: Robin Dunn
//
// Created: 18-Aug-2012
// Copyright: (c) 2012-2020 by Total Control Software
// Licence: wxWindows license
//--------------------------------------------------------------------------
typedef bool (*wxPyDrawListOp_t)(wxDC& dc, PyObject* coords);
PyObject* wxPyDrawXXXList(wxDC& dc, wxPyDrawListOp_t doDraw,
PyObject* pyCoords, PyObject* pyPens, PyObject* pyBrushes);
bool wxPyDrawXXXPoint(wxDC& dc, PyObject* coords);
bool wxPyDrawXXXLine(wxDC& dc, PyObject* coords);
bool wxPyDrawXXXRectangle(wxDC& dc, PyObject* coords);
bool wxPyDrawXXXEllipse(wxDC& dc, PyObject* coords);
bool wxPyDrawXXXPolygon(wxDC& dc, PyObject* coords);
PyObject* wxPyDrawTextList(wxDC& dc, PyObject* textList, PyObject* pyPoints,
PyObject* foregroundList, PyObject* backgroundList);
//--------------------------------------------------------------------------
PyObject* wxPyDrawXXXList(wxDC& dc, wxPyDrawListOp_t doDraw,
PyObject* pyCoords, PyObject* pyPens, PyObject* pyBrushes)
{
wxPyBlock_t blocked = wxPyBeginBlockThreads();
bool isFastSeq = PyList_Check(pyCoords) || PyTuple_Check(pyCoords);
bool isFastPens = PyList_Check(pyPens) || PyTuple_Check(pyPens);
bool isFastBrushes = PyList_Check(pyBrushes) || PyTuple_Check(pyBrushes);
int numObjs = 0;
int numPens = 0;
int numBrushes = 0;
wxPen* pen;
wxBrush* brush;
PyObject* obj;
PyObject* coords;
int i = 0;
PyObject* retval;
if (!PySequence_Check(pyCoords)) {
goto err0;
}
if (!PySequence_Check(pyPens)) {
goto err1;
}
if (!PySequence_Check(pyBrushes)) {
goto err2;
}
numObjs = PySequence_Length(pyCoords);
numPens = PySequence_Length(pyPens);
numBrushes = PySequence_Length(pyBrushes);
for (i = 0; i < numObjs; i++) {
// Use a new pen?
if (i < numPens) {
if (isFastPens) {
obj = PySequence_Fast_GET_ITEM(pyPens, i);
}
else {
obj = PySequence_GetItem(pyPens, i);
}
if (! wxPyConvertWrappedPtr(obj, (void **) &pen, "wxPen")) {
if (!isFastPens)
Py_DECREF(obj);
goto err1;
}
dc.SetPen(*pen);
if (!isFastPens)
Py_DECREF(obj);
}
// Use a new brush?
if (i < numBrushes) {
if (isFastBrushes) {
obj = PySequence_Fast_GET_ITEM(pyBrushes, i);
}
else {
obj = PySequence_GetItem(pyBrushes, i);
}
if (!wxPyConvertWrappedPtr(obj, (void **) &brush, "wxBrush")) {
if (!isFastBrushes)
Py_DECREF(obj);
goto err2;
}
dc.SetBrush(*brush);
if (!isFastBrushes)
Py_DECREF(obj);
}
// Get the Coordinates
if (isFastSeq) {
coords = PySequence_Fast_GET_ITEM(pyCoords, i);
}
else {
coords = PySequence_GetItem(pyCoords, i);
}
// call the drawOp
bool success = doDraw(dc, coords);
if (!isFastSeq)
Py_DECREF(coords);
if (! success) {
retval = NULL;
goto exit;
}
} // end of main for loop
Py_INCREF(Py_None);
retval = Py_None;
goto exit;
err0:
PyErr_SetString(PyExc_TypeError, "Expected a sequence of coordinates");
retval = NULL;
goto exit;
err1:
PyErr_SetString(PyExc_TypeError, "Expected a sequence of wxPens");
retval = NULL;
goto exit;
err2:
PyErr_SetString(PyExc_TypeError, "Expected a sequence of wxBrushes");
retval = NULL;
goto exit;
exit:
wxPyEndBlockThreads(blocked);
return retval;
}
bool wxPyDrawXXXPoint(wxDC& dc, PyObject* coords)
{
int x, y;
if (! wxPy2int_seq_helper(coords, &x, &y)) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of (x,y) sequences.");
return false;
}
dc.DrawPoint(x, y);
return true;
}
bool wxPyDrawXXXLine(wxDC& dc, PyObject* coords)
{
int x1, y1, x2, y2;
if (! wxPy4int_seq_helper(coords, &x1, &y1, &x2, &y2)) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of (x1,y1, x1,y2) sequences.");
return false;
}
dc.DrawLine(x1,y1, x2,y2);
return true;
}
bool wxPyDrawXXXRectangle(wxDC& dc, PyObject* coords)
{
int x, y, w, h;
if (! wxPy4int_seq_helper(coords, &x, &y, &w, &h)) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of (x,y, w,h) sequences.");
return false;
}
dc.DrawRectangle(x, y, w, h);
return true;
}
bool wxPyDrawXXXEllipse(wxDC& dc, PyObject* coords)
{
int x, y, w, h;
if (! wxPy4int_seq_helper(coords, &x, &y, &w, &h)) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of (x,y, w,h) sequences.");
return false;
}
dc.DrawEllipse(x, y, w, h);
return true;
}
wxPoint* wxPoint_LIST_helper(PyObject* source, int *count);
bool wxPyDrawXXXPolygon(wxDC& dc, PyObject* coords)
{
wxPoint* points;
int numPoints;
points = wxPoint_LIST_helper(coords, &numPoints);
if (! points) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of sequences of (x,y) sequences.");
return false;
}
dc.DrawPolygon(numPoints, points);
delete [] points;
return true;
}
//---------------------------------------------------------------------------
PyObject* wxPyDrawTextList(wxDC& dc, PyObject* textList, PyObject* pyPoints, PyObject* foregroundList, PyObject* backgroundList)
{
wxPyBlock_t blocked = wxPyBeginBlockThreads();
bool isFastSeq = PyList_Check(pyPoints) || PyTuple_Check(pyPoints);
bool isFastText = PyList_Check(textList) || PyTuple_Check(textList);
bool isFastForeground = PyList_Check(foregroundList) || PyTuple_Check(foregroundList);
bool isFastBackground = PyList_Check(backgroundList) || PyTuple_Check(backgroundList);
int numText = 0;
int numPoints = 0;
int numForeground = 0;
int numBackground = 0;
PyObject* obj;
int x1, y1;
int i = 0;
wxColor* color;
PyObject* retval;
wxString string;
if (!PySequence_Check(pyPoints)) {
goto err0;
}
if (!PySequence_Check(textList)) {
goto err1;
}
if (!PySequence_Check(foregroundList)) {
goto err2;
}
if (!PySequence_Check(backgroundList)) {
goto err3;
}
numPoints = PySequence_Length(pyPoints);
numText = PySequence_Length(textList);
numForeground = PySequence_Length(foregroundList);
numBackground = PySequence_Length(backgroundList);
for (i = 0; i < numPoints; i++) {
// Use a new string ?
if (i < numText) {
if ( isFastText ) {
obj = PySequence_Fast_GET_ITEM(textList, i);
}
else {
obj = PySequence_GetItem(textList, i);
}
if (! PyBytes_Check(obj) && !PyUnicode_Check(obj) ) {
Py_DECREF(obj);
goto err1;
}
string = Py2wxString(obj);
if ( !isFastText )
Py_DECREF(obj);
}
if (i < numForeground) {
// Use a new foreground ?
if ( isFastForeground ) {
obj = PySequence_Fast_GET_ITEM(foregroundList, i);
}
else {
obj = PySequence_GetItem(foregroundList, i);
}
if (! wxPyConvertWrappedPtr(obj, (void **) &color, "wxColour")) {
if (!isFastForeground)
Py_DECREF(obj);
goto err2;
}
dc.SetTextForeground(*color);
if ( !isFastForeground )
Py_DECREF(obj);
}
if (i < numBackground) {
// Use a new background ?
if ( isFastBackground ) {
obj = PySequence_Fast_GET_ITEM(backgroundList, i);
}
else {
obj = PySequence_GetItem(backgroundList, i);
}
if (! wxPyConvertWrappedPtr(obj, (void **) &color, "wxColour")) {
if (!isFastBackground)
Py_DECREF(obj);
goto err3;
}
dc.SetTextBackground(*color);
if ( !isFastBackground )
Py_DECREF(obj);
}
// Get the point coordinates
if (isFastSeq) {
obj = PySequence_Fast_GET_ITEM(pyPoints, i);
}
else {
obj = PySequence_GetItem(pyPoints, i);
}
if (! wxPy2int_seq_helper(obj, &x1, &y1)) {
if (! isFastSeq)
Py_DECREF(obj);
goto err0;
}
if (!isFastText)
Py_DECREF(obj);
if (PyErr_Occurred()) {
retval = NULL;
goto exit;
}
// Now draw the text
dc.DrawText(string, x1, y1);
}
Py_INCREF(Py_None);
retval = Py_None;
goto exit;
err0:
PyErr_SetString(PyExc_TypeError, "Expected a sequence of (x,y) sequences.");
retval = NULL;
goto exit;
err1:
PyErr_SetString(PyExc_TypeError, "Expected a sequence of strings");
retval = NULL;
goto exit;
err2:
PyErr_SetString(PyExc_TypeError, "Expected a sequence of wxColours for foregrounds");
retval = NULL;
goto exit;
err3:
PyErr_SetString(PyExc_TypeError, "Expected a sequence of wxColours for backgrounds");
retval = NULL;
goto exit;
exit:
wxPyEndBlockThreads(blocked);
return retval;
}
//---------------------------------------------------------------------------
bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point)
{
// get the x value
if (wxPyInt_Check(o1))
point->x = (int)wxPyInt_AS_LONG(o1);
else if (PyFloat_Check(o1))
point->x = (int)PyFloat_AS_DOUBLE(o1);
else if (PyNumber_Check(o1))
point->x = (int)wxPyInt_AsLong(o1);
else
return false;
// get the y value
if (wxPyInt_Check(o2))
point->y = (int)wxPyInt_AS_LONG(o2);
else if (PyFloat_Check(o2))
point->y = (int)PyFloat_AS_DOUBLE(o2);
else if (PyNumber_Check(o2))
point->y = (int)wxPyInt_AsLong(o2);
else
return false;
return true;
}
wxPoint* wxPoint_LIST_helper(PyObject* source, int *count)
{
int idx;
wxPoint* temp;
PyObject *o, *o1, *o2;
bool isFast = PyList_Check(source) || PyTuple_Check(source);
if (!PySequence_Check(source)) {
goto error0;
}
// The length of the sequence is returned in count.
*count = PySequence_Length(source);
if (*count < 0) {
goto error0;
}
temp = new wxPoint[*count];
if (!temp) {
PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
return NULL;
}
for (idx=0; idx<*count; idx++) {
// Get an item: try fast way first.
if (isFast) {
o = PySequence_Fast_GET_ITEM(source, idx);
}
else {
o = PySequence_GetItem(source, idx);
if (o == NULL) {
goto error1;
}
}
// Convert o to wxPoint.
if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) ||
(PyList_Check(o) && PyList_GET_SIZE(o) == 2)) {
o1 = PySequence_Fast_GET_ITEM(o, 0);
o2 = PySequence_Fast_GET_ITEM(o, 1);
if (!wxPointFromObjects(o1, o2, &temp[idx])) {
goto error2;
}
}
else if (wxPyWrappedPtr_Check(o)) {
wxPoint* pt;
if (! wxPyConvertWrappedPtr(o, (void **)&pt, "wxPoint")) {
goto error2;
}
temp[idx] = *pt;
}
else if (PySequence_Check(o) && PySequence_Length(o) == 2) {
o1 = PySequence_GetItem(o, 0);
o2 = PySequence_GetItem(o, 1);
if (!wxPointFromObjects(o1, o2, &temp[idx])) {
goto error3;
}
Py_DECREF(o1);
Py_DECREF(o2);
}
else {
goto error2;
}
// Clean up.
if (!isFast)
Py_DECREF(o);
}
return temp;
error3:
Py_DECREF(o1);
Py_DECREF(o2);
error2:
if (!isFast)
Py_DECREF(o);
error1:
delete [] temp;
error0:
PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wx.Points.");
return NULL;
}
PyObject* wxPyDrawLinesFromBuffer(wxDC& dc, PyObject* pyBuff)
{
wxPyBlock_t blocked = wxPyBeginBlockThreads();
Py_buffer view;
PyObject* retval;
if (!PyObject_CheckBuffer(pyBuff)) {
goto err0;
}
if (PyObject_GetBuffer(pyBuff, &view, PyBUF_CONTIG) < 0) {
goto err1;
}
if (view.itemsize * 2 != sizeof(wxPoint)) {
goto err2;
}
dc.DrawLines(view.len / view.itemsize / 2, (wxPoint *)view.buf);
PyBuffer_Release(&view);
Py_INCREF(Py_None);
retval = Py_None;
goto exit;
err0:
PyErr_SetString(PyExc_TypeError, "Expected a buffer object");
retval = NULL;
goto exit;
err1:
// PyObject_GetBuffer raises exception already
retval = NULL;
goto exit;
err2:
PyErr_SetString(PyExc_TypeError, "Item size does not match wxPoint size");
retval = NULL;
goto exit;
exit:
wxPyEndBlockThreads(blocked);
return retval;
}