]> git.saurik.com Git - wxWidgets.git/blame - wxPython/src/helpers.cpp
added --enable-official-build and --enable-vendor
[wxWidgets.git] / wxPython / src / helpers.cpp
CommitLineData
7bf85405
RD
1/////////////////////////////////////////////////////////////////////////////
2// Name: helpers.cpp
03e9bead 3// Purpose: Helper functions/classes for the wxPython extension module
7bf85405
RD
4//
5// Author: Robin Dunn
6//
7// Created: 7/1/97
8// RCS-ID: $Id$
9// Copyright: (c) 1998 by Total Control Software
10// Licence: wxWindows license
11/////////////////////////////////////////////////////////////////////////////
12
08127323 13
7bf85405
RD
14#undef DEBUG
15#include <Python.h>
16#include "helpers.h"
cbf60e09 17#include "pyistream.h"
694759cf 18
af309447
RD
19#ifdef __WXMSW__
20#include <wx/msw/private.h>
78b57918 21#include <wx/msw/winundef.h>
4268f798 22#include <wx/msw/msvcrt.h>
af309447 23#endif
694759cf
RD
24
25#ifdef __WXGTK__
26#include <gtk/gtk.h>
54b96882
RD
27#include <gdk/gdkprivate.h>
28#include <wx/gtk/win_gtk.h>
694759cf 29#endif
7bf85405 30
dd116e73
RD
31#include <wx/clipbrd.h>
32#include <wx/mimetype.h>
d84a9306 33#include <wx/image.h>
dd116e73 34
c8bc7bb8
RD
35//----------------------------------------------------------------------
36
37#if PYTHON_API_VERSION <= 1007 && wxUSE_UNICODE
38#error Python must support Unicode to use wxWindows Unicode
39#endif
40
4268f798
RD
41//----------------------------------------------------------------------
42
4268f798 43wxPyApp* wxPythonApp = NULL; // Global instance of application object
e3dbf593 44bool wxPyDoCleanup = FALSE;
1e4a197e 45bool wxPyDoingCleanup = FALSE;
4268f798
RD
46
47
48#ifdef WXP_WITH_THREAD
49struct wxPyThreadState {
50 unsigned long tid;
51 PyThreadState* tstate;
52
53 wxPyThreadState(unsigned long _tid=0, PyThreadState* _tstate=NULL)
54 : tid(_tid), tstate(_tstate) {}
55};
56
57#include <wx/dynarray.h>
58WX_DECLARE_OBJARRAY(wxPyThreadState, wxPyThreadStateArray);
59#include <wx/arrimpl.cpp>
60WX_DEFINE_OBJARRAY(wxPyThreadStateArray);
61
62wxPyThreadStateArray* wxPyTStates = NULL;
63wxMutex* wxPyTMutex = NULL;
64#endif
7bf85405
RD
65
66
1e4a197e
RD
67static PyObject* wxPython_dict = NULL;
68static PyObject* wxPyPtrTypeMap = NULL;
69static PyObject* wxPyAssertionError = NULL;
70
71
21f4bf45 72#ifdef __WXMSW__ // If building for win32...
21f4bf45
RD
73//----------------------------------------------------------------------
74// This gets run when the DLL is loaded. We just need to save a handle.
75//----------------------------------------------------------------------
9c039d08 76
21f4bf45
RD
77BOOL WINAPI DllMain(
78 HINSTANCE hinstDLL, // handle to DLL module
79 DWORD fdwReason, // reason for calling function
80 LPVOID lpvReserved // reserved
81 )
82{
a2426843
RD
83 // If wxPython is embedded in another wxWindows app then
84 // the inatance has already been set.
85 if (! wxGetInstance())
86 wxSetInstance(hinstDLL);
87 return TRUE;
21f4bf45
RD
88}
89#endif
90
7bf85405 91//----------------------------------------------------------------------
4268f798 92// Classes for implementing the wxp main application shell.
7bf85405
RD
93//----------------------------------------------------------------------
94
1e4a197e
RD
95IMPLEMENT_ABSTRACT_CLASS(wxPyApp, wxApp);
96
7bf85405 97
cf694132 98wxPyApp::wxPyApp() {
1e4a197e 99 m_assertMode = wxPYAPP_ASSERT_EXCEPTION;
cf694132
RD
100}
101
1e4a197e 102
cf694132 103wxPyApp::~wxPyApp() {
cf694132
RD
104}
105
106
1e4a197e 107// This one isn't acutally called... We fake it with __wxStart()
4268f798 108bool wxPyApp::OnInit() {
6999b0d8 109 return FALSE;
7bf85405
RD
110}
111
4268f798
RD
112
113int wxPyApp::MainLoop() {
7ff49f0c 114 int retval = 0;
af309447 115
7ff49f0c 116 DeletePendingObjects();
4268f798 117 bool initialized = wxTopLevelWindows.GetCount() != 0;
7ff49f0c 118#ifdef __WXGTK__
4268f798 119 m_initialized = initialized;
7ff49f0c 120#endif
7bf85405 121
4268f798 122 if (initialized) {
098be78d
RD
123 if ( m_exitOnFrameDelete == Later ) {
124 m_exitOnFrameDelete = Yes;
125 }
126
7ff49f0c 127 retval = wxApp::MainLoop();
4268f798 128 OnExit();
7ff49f0c
RD
129 }
130 return retval;
131}
7bf85405
RD
132
133
1e4a197e
RD
134bool wxPyApp::OnInitGui() {
135 bool rval=TRUE;
136 wxApp::OnInitGui(); // in this case always call the base class version
137 // wxPyBeginBlockThreads(); *** only called from within __wxStart so we already have the GIL
138 if (wxPyCBH_findCallback(m_myInst, "OnInitGui"))
139 rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
140 // wxPyEndBlockThreads(); ***
141 return rval;
142}
143
144
145int wxPyApp::OnExit() {
146 int rval=0;
147 wxPyBeginBlockThreads();
148 if (wxPyCBH_findCallback(m_myInst, "OnExit"))
149 rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
150 wxPyEndBlockThreads();
151 wxApp::OnExit(); // in this case always call the base class version
152 return rval;
153}
154
155
156#ifdef __WXDEBUG__
157void wxPyApp::OnAssert(const wxChar *file,
158 int line,
159 const wxChar *cond,
160 const wxChar *msg) {
161
162 // If the OnAssert is overloaded in the Python class then call it...
163 bool found;
164 wxPyBeginBlockThreads();
165 if ((found = wxPyCBH_findCallback(m_myInst, "OnAssert"))) {
166 PyObject* fso = wx2PyString(file);
167 PyObject* cso = wx2PyString(file);
168 PyObject* mso;
169 if (msg != NULL)
170 mso = wx2PyString(file);
171 else {
172 mso = Py_None; Py_INCREF(Py_None);
173 }
174 wxPyCBH_callCallback(m_myInst, Py_BuildValue("(OiOO)", fso, line, cso, mso));
175 Py_DECREF(fso);
176 Py_DECREF(cso);
177 Py_DECREF(mso);
178 }
179 wxPyEndBlockThreads();
180
181 // ...otherwise do our own thing with it
182 if (! found) {
183 // ignore it?
184 if (m_assertMode & wxPYAPP_ASSERT_SUPPRESS)
185 return;
186
187 // turn it into a Python exception?
188 if (m_assertMode & wxPYAPP_ASSERT_EXCEPTION) {
189 wxString buf;
190 buf.Alloc(4096);
191 buf.Printf(wxT("C++ assertion \"%s\" failed in %s(%d)"), cond, file, line);
192 if (msg != NULL) {
193 buf += wxT(": ");
194 buf += msg;
195 }
196
197 // set the exception
198 wxPyBeginBlockThreads();
199 PyObject* s = wx2PyString(buf);
200 PyErr_SetObject(wxPyAssertionError, s);
201 Py_DECREF(s);
202 wxPyEndBlockThreads();
203
204 // Now when control returns to whatever API wrapper was called from
205 // Python it should detect that an exception is set and will return
206 // NULL, signalling the exception to Python.
207 }
208
209 // Send it to the normal log destination, but only if
210 // not _DIALOG because it will call this too
211 if ( (m_assertMode & wxPYAPP_ASSERT_LOG) && !(m_assertMode & wxPYAPP_ASSERT_DIALOG)) {
212 wxString buf;
213 buf.Alloc(4096);
214 buf.Printf(wxT("%s(%d): assert \"%s\" failed"),
215 file, line, cond);
216 if (msg != NULL) {
217 buf += wxT(": ");
218 buf += msg;
219 }
220 wxLogDebug(buf);
221 }
222
223 // do the normal wx assert dialog?
224 if (m_assertMode & wxPYAPP_ASSERT_DIALOG)
225 wxApp::OnAssert(file, line, cond, msg);
226 }
227}
228#endif
229
230
231/*static*/
232bool wxPyApp::GetMacDefaultEncodingIsPC() {
233#ifdef __WXMAC__
234 return s_macDefaultEncodingIsPC;
235#else
236 return 0;
237#endif
238}
239
240/*static*/
241bool wxPyApp::GetMacSupportPCMenuShortcuts() {
242#ifdef __WXMAC__
243 return s_macSupportPCMenuShortcuts;
244#else
245 return 0;
246#endif
247}
248
249/*static*/
250long wxPyApp::GetMacAboutMenuItemId() {
251#ifdef __WXMAC__
252 return s_macAboutMenuItemId;
253#else
254 return 0;
255#endif
256}
257
258/*static*/
259long wxPyApp::GetMacPreferencesMenuItemId() {
260#ifdef __WXMAC__
261 return s_macPreferencesMenuItemId;
262#else
263 return 0;
264#endif
265}
266
267/*static*/
268long wxPyApp::GetMacExitMenuItemId() {
269#ifdef __WXMAC__
270 return s_macExitMenuItemId;
271#else
272 return 0;
273#endif
274}
275
276/*static*/
277wxString wxPyApp::GetMacHelpMenuTitleName() {
278#ifdef __WXMAC__
279 return s_macHelpMenuTitleName;
280#else
281 return wxEmptyString;
282#endif
283}
284
285/*static*/
286void wxPyApp::SetMacDefaultEncodingIsPC(bool val) {
287#ifdef __WXMAC__
288 s_macDefaultEncodingIsPC = val;
289#endif
290}
291
292/*static*/
293void wxPyApp::SetMacSupportPCMenuShortcuts(bool val) {
294#ifdef __WXMAC__
295 s_macSupportPCMenuShortcuts = val;
296#endif
297}
298
299/*static*/
300void wxPyApp::SetMacAboutMenuItemId(long val) {
301#ifdef __WXMAC__
302 s_macAboutMenuItemId = val;
303#endif
304}
305
306/*static*/
307void wxPyApp::SetMacPreferencesMenuItemId(long val) {
308#ifdef __WXMAC__
309 s_macPreferencesMenuItemId = val;
310#endif
311}
312
313/*static*/
314void wxPyApp::SetMacExitMenuItemId(long val) {
315#ifdef __WXMAC__
316 s_macExitMenuItemId = val;
317#endif
318}
319
320/*static*/
321void wxPyApp::SetMacHelpMenuTitleName(const wxString& val) {
322#ifdef __WXMAC__
323 s_macHelpMenuTitleName = val;
324#endif
325}
326
327
4268f798 328
fb5e0af0 329//---------------------------------------------------------------------
7bf85405
RD
330//----------------------------------------------------------------------
331
a541c325 332
dd116e73 333#if 0
a541c325
RD
334static char* wxPyCopyCString(const wxChar* src)
335{
336 wxWX2MBbuf buff = (wxWX2MBbuf)wxConvCurrent->cWX2MB(src);
337 size_t len = strlen(buff);
338 char* dest = new char[len+1];
339 strcpy(dest, buff);
340 return dest;
341}
342
c8bc7bb8 343#if wxUSE_UNICODE
a541c325 344static char* wxPyCopyCString(const char* src) // we need a char version too
c8bc7bb8 345{
a541c325
RD
346 size_t len = strlen(src);
347 char* dest = new char[len+1];
348 strcpy(dest, src);
349 return dest;
c8bc7bb8 350}
a541c325 351#endif
c8bc7bb8 352
a541c325 353static wxChar* wxPyCopyWString(const char *src)
c8bc7bb8 354{
a541c325
RD
355 //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
356 wxString str(src, *wxConvCurrent);
357 return copystring(str);
c8bc7bb8
RD
358}
359
a541c325
RD
360#if wxUSE_UNICODE
361static wxChar* wxPyCopyWString(const wxChar *src)
c8bc7bb8 362{
a541c325 363 return copystring(src);
c8bc7bb8
RD
364}
365#endif
dd116e73 366#endif
f6bcfd97 367
a541c325
RD
368
369//----------------------------------------------------------------------
370
dd116e73
RD
371// This function is called when the wxc module is imported to do some initial
372// setup. (Before there is a wxApp object.)
1e4a197e 373void __wxPreStart(PyObject* moduleDict)
7bf85405 374{
de20db99
RD
375
376#ifdef __WXMSW__
44f68505 377// wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
de20db99
RD
378#endif
379
9d8bd15f 380#ifdef WXP_WITH_THREAD
1112b0c6 381 PyEval_InitThreads();
4268f798
RD
382 wxPyTStates = new wxPyThreadStateArray;
383 wxPyTMutex = new wxMutex;
9d8bd15f
RD
384#endif
385
dd116e73 386 // Ensure that the build options in the DLL (or whatever) match this build
0b85cc38
RD
387 wxApp::CheckBuildOptions(wxBuildOptions());
388
dd116e73 389 // Create an exception object to use for wxASSERTions
1e4a197e
RD
390 wxPyAssertionError = PyErr_NewException("wxPython.wxc.wxPyAssertionError",
391 PyExc_AssertionError, NULL);
392 PyDict_SetItemString(moduleDict, "wxPyAssertionError", wxPyAssertionError);
0d6f9504
RD
393}
394
395
dd116e73
RD
396
397// Initialize wxWindows and bootstrap the user application by calling the
398// wxApp's OnInit, which is a parameter to this funciton. See wxApp.__init__
399// in _extras.py to learn how the bootstrap is started.
0d6f9504
RD
400PyObject* __wxStart(PyObject* /* self */, PyObject* args)
401{
402 PyObject* onInitFunc = NULL;
dd116e73
RD
403 PyObject* arglist= NULL;
404 PyObject* result = NULL;
405 PyObject* pyint = NULL;
0d6f9504
RD
406 long bResult;
407
0d6f9504
RD
408 if (!PyArg_ParseTuple(args, "O", &onInitFunc))
409 return NULL;
410
dd116e73 411 // Get any command-line args passed to this program from the sys module
9416aa89 412 int argc = 0;
dd116e73 413 char** argv = NULL;
f6bcfd97 414 PyObject* sysargv = PySys_GetObject("argv");
9416aa89
RD
415 if (sysargv != NULL) {
416 argc = PyList_Size(sysargv);
dd116e73 417 argv = new char*[argc+1];
9416aa89 418 int x;
c8bc7bb8
RD
419 for(x=0; x<argc; x++) {
420 PyObject *pyArg = PyList_GetItem(sysargv, x);
dd116e73 421 argv[x] = PyString_AsString(pyArg);
c8bc7bb8 422 }
9416aa89
RD
423 argv[argc] = NULL;
424 }
c8bc7bb8 425
dd116e73
RD
426 if (! wxEntryStart(argc, argv) ) {
427 PyErr_SetString(PyExc_SystemError, // is this the right one?
428 "wxEntryStart failed!");
429 goto error;
430 }
431 delete [] argv;
432
0d6f9504 433
dd116e73
RD
434 // The stock objects were all NULL when they were loaded into
435 // SWIG generated proxies, so re-init those now...
436 wxPy_ReinitStockObjects();
7bf85405 437
dd116e73
RD
438
439 // Call the Python wxApp's OnInit function
7bf85405
RD
440 arglist = PyTuple_New(0);
441 result = PyEval_CallObject(onInitFunc, arglist);
1e4a197e 442 Py_DECREF(arglist);
8bf5d46e
RD
443 if (!result) { // an exception was raised.
444 return NULL;
7bf85405
RD
445 }
446
dd116e73 447 pyint = PyNumber_Int(result);
1e4a197e 448 if (! pyint) {
7bf85405 449 PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value");
1e4a197e 450 goto error;
7bf85405 451 }
1e4a197e 452 bResult = PyInt_AS_LONG(pyint);
7bf85405 453 if (! bResult) {
194fa2ac 454 PyErr_SetString(PyExc_SystemExit, "OnInit returned FALSE, exiting...");
1e4a197e 455 goto error;
7bf85405
RD
456 }
457
13dfc243 458#ifdef __WXGTK__
7ff49f0c 459 wxTheApp->m_initialized = (wxTopLevelWindows.GetCount() > 0);
13dfc243 460#endif
fb5e0af0 461
1e4a197e
RD
462 Py_DECREF(result);
463 Py_DECREF(pyint);
7bf85405
RD
464 Py_INCREF(Py_None);
465 return Py_None;
1e4a197e
RD
466
467 error:
468 Py_XDECREF(result);
469 Py_XDECREF(pyint);
470 return NULL;
7bf85405
RD
471}
472
4268f798 473
dd116e73 474
7ff49f0c 475void __wxCleanup() {
1e4a197e 476 wxPyDoingCleanup = TRUE;
e3dbf593
RD
477 if (wxPyDoCleanup)
478 wxEntryCleanup();
7faa9591 479#ifdef WXP_WITH_THREAD
4268f798
RD
480 delete wxPyTMutex;
481 wxPyTMutex = NULL;
482 wxPyTStates->Empty();
483 delete wxPyTStates;
484 wxPyTStates = NULL;
7faa9591 485#endif
7ff49f0c 486}
7bf85405
RD
487
488
489
4acff284 490
7bf85405
RD
491PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args)
492{
493
494 if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
495 return NULL;
496
497 if (!PyDict_Check(wxPython_dict)) {
498 PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!");
499 return NULL;
500 }
9416aa89
RD
501
502 if (! wxPyPtrTypeMap)
503 wxPyPtrTypeMap = PyDict_New();
504 PyDict_SetItemString(wxPython_dict, "__wxPyPtrTypeMap", wxPyPtrTypeMap);
505
506
7bf85405 507#ifdef __WXMOTIF__
21f4bf45
RD
508#define wxPlatform "__WXMOTIF__"
509#endif
be43cc44
RD
510#ifdef __WXX11__
511#define wxPlatform "__WXX11__"
7bf85405
RD
512#endif
513#ifdef __WXGTK__
21f4bf45 514#define wxPlatform "__WXGTK__"
7bf85405
RD
515#endif
516#if defined(__WIN32__) || defined(__WXMSW__)
fb5e0af0 517#define wxPlatform "__WXMSW__"
7bf85405
RD
518#endif
519#ifdef __WXMAC__
21f4bf45 520#define wxPlatform "__WXMAC__"
7bf85405
RD
521#endif
522
249a57f4
RD
523#ifdef __WXDEBUG__
524 int wxdebug = 1;
525#else
526 int wxdebug = 0;
527#endif
528
7bf85405 529 PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform));
c8bc7bb8 530 PyDict_SetItemString(wxPython_dict, "wxUSE_UNICODE", PyInt_FromLong(wxUSE_UNICODE));
249a57f4 531 PyDict_SetItemString(wxPython_dict, "__WXDEBUG__", PyInt_FromLong(wxdebug));
c8bc7bb8 532
7bf85405
RD
533 Py_INCREF(Py_None);
534 return Py_None;
535}
536
dd116e73
RD
537
538//---------------------------------------------------------------------------
539
540// The stock objects are no longer created when the wxc module is imported, but
541// only after the app object has been created. This function will be called before
542// OnInit is called so we can hack the new pointer values into the obj.this attributes.
543
544void wxPy_ReinitStockObjects()
545{
546 char ptrbuf[128];
547 PyObject* obj;
548 PyObject* ptrobj;
549
550
551
552#define REINITOBJ(name, type) \
553 obj = PyDict_GetItemString(wxPython_dict, #name); \
554 wxASSERT_MSG(obj != NULL, wxT("Unable to find stock object for " #name)); \
555 SWIG_MakePtr(ptrbuf, (char *) name, "_" #type "_p"); \
556 ptrobj = PyString_FromString(ptrbuf); \
557 PyObject_SetAttrString(obj, "this", ptrobj); \
558 Py_DECREF(ptrobj)
559
560#define REINITOBJ2(name, type) \
561 obj = PyDict_GetItemString(wxPython_dict, #name); \
562 wxASSERT_MSG(obj != NULL, wxT("Unable to find stock object for " #name)); \
563 SWIG_MakePtr(ptrbuf, (char *) &name, "_" #type "_p"); \
564 ptrobj = PyString_FromString(ptrbuf); \
565 PyObject_SetAttrString(obj, "this", ptrobj); \
566 Py_DECREF(ptrobj)
567
568
569 REINITOBJ(wxNORMAL_FONT, wxFont);
570 REINITOBJ(wxSMALL_FONT, wxFont);
571 REINITOBJ(wxITALIC_FONT, wxFont);
572 REINITOBJ(wxSWISS_FONT, wxFont);
573
574 REINITOBJ(wxRED_PEN, wxPen);
575 REINITOBJ(wxCYAN_PEN, wxPen);
576 REINITOBJ(wxGREEN_PEN, wxPen);
577 REINITOBJ(wxBLACK_PEN, wxPen);
578 REINITOBJ(wxWHITE_PEN, wxPen);
579 REINITOBJ(wxTRANSPARENT_PEN, wxPen);
580 REINITOBJ(wxBLACK_DASHED_PEN, wxPen);
581 REINITOBJ(wxGREY_PEN, wxPen);
582 REINITOBJ(wxMEDIUM_GREY_PEN, wxPen);
583 REINITOBJ(wxLIGHT_GREY_PEN, wxPen);
584
585 REINITOBJ(wxBLUE_BRUSH, wxBrush);
586 REINITOBJ(wxGREEN_BRUSH, wxBrush);
587 REINITOBJ(wxWHITE_BRUSH, wxBrush);
588 REINITOBJ(wxBLACK_BRUSH, wxBrush);
589 REINITOBJ(wxTRANSPARENT_BRUSH, wxBrush);
590 REINITOBJ(wxCYAN_BRUSH, wxBrush);
591 REINITOBJ(wxRED_BRUSH, wxBrush);
592 REINITOBJ(wxGREY_BRUSH, wxBrush);
593 REINITOBJ(wxMEDIUM_GREY_BRUSH, wxBrush);
594 REINITOBJ(wxLIGHT_GREY_BRUSH, wxBrush);
595
596 REINITOBJ(wxBLACK, wxColour);
597 REINITOBJ(wxWHITE, wxColour);
598 REINITOBJ(wxRED, wxColour);
599 REINITOBJ(wxBLUE, wxColour);
600 REINITOBJ(wxGREEN, wxColour);
601 REINITOBJ(wxCYAN, wxColour);
602 REINITOBJ(wxLIGHT_GREY, wxColour);
603
604 REINITOBJ(wxSTANDARD_CURSOR, wxCursor);
605 REINITOBJ(wxHOURGLASS_CURSOR, wxCursor);
606 REINITOBJ(wxCROSS_CURSOR, wxCursor);
607
608 REINITOBJ2(wxNullBitmap, wxBitmap);
609 REINITOBJ2(wxNullIcon, wxIcon);
610 REINITOBJ2(wxNullCursor, wxCursor);
611 REINITOBJ2(wxNullPen, wxPen);
612 REINITOBJ2(wxNullBrush, wxBrush);
613 REINITOBJ2(wxNullPalette, wxPalette);
614 REINITOBJ2(wxNullFont, wxFont);
615 REINITOBJ2(wxNullColour, wxColour);
616
617 REINITOBJ(wxTheFontList, wxFontList);
618 REINITOBJ(wxThePenList, wxPenList);
619 REINITOBJ(wxTheBrushList, wxBrushList);
620 REINITOBJ(wxTheColourDatabase, wxColourDatabase);
621
622
623 REINITOBJ(wxTheClipboard, wxClipboard);
624 REINITOBJ(wxTheMimeTypesManager, wxMimeTypesManager);
625 REINITOBJ2(wxDefaultValidator, wxValidator);
626 REINITOBJ2(wxNullImage, wxImage);
627 REINITOBJ2(wxNullAcceleratorTable, wxAcceleratorTable);
628
629#undef REINITOBJ
630#undef REINITOBJ2
631}
632
4acff284
RD
633//---------------------------------------------------------------------------
634
635void wxPyClientData_dtor(wxPyClientData* self) {
1e4a197e
RD
636 if (! wxPyDoingCleanup) { // Don't do it during cleanup as Python
637 // may have already garbage collected the object...
638 wxPyBeginBlockThreads();
639 Py_DECREF(self->m_obj);
640 wxPyEndBlockThreads();
641 }
4acff284
RD
642}
643
644void wxPyUserData_dtor(wxPyUserData* self) {
1e4a197e
RD
645 if (! wxPyDoingCleanup) {
646 wxPyBeginBlockThreads();
647 Py_DECREF(self->m_obj);
648 wxPyEndBlockThreads();
649 }
4acff284
RD
650}
651
652
653// This is called when an OOR controled object is being destroyed. Although
654// the C++ object is going away there is no way to force the Python object
655// (and all references to it) to die too. This causes problems (crashes) in
656// wxPython when a python shadow object attempts to call a C++ method using
657// the now bogus pointer... So to try and prevent this we'll do a little black
658// magic and change the class of the python instance to a class that will
659// raise an exception for any attempt to call methods with it. See
660// _wxPyDeadObject in _extras.py for the implementation of this class.
661void wxPyOORClientData_dtor(wxPyOORClientData* self) {
662
663 static PyObject* deadObjectClass = NULL;
664
665 wxPyBeginBlockThreads();
666 if (deadObjectClass == NULL) {
667 deadObjectClass = PyDict_GetItemString(wxPython_dict, "_wxPyDeadObject");
668 wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!"));
669 Py_INCREF(deadObjectClass);
670 }
671
1e4a197e 672
1fded56b
RD
673 // Only if there is more than one reference to the object
674 if ( !wxPyDoingCleanup && self->m_obj->ob_refcnt > 1 ) {
4acff284 675 wxASSERT_MSG(PyInstance_Check(self->m_obj), wxT("m_obj not an instance!?!?!"));
1fded56b
RD
676
677 // Call __del__, if there is one.
678 PyObject* func = PyObject_GetAttrString(self->m_obj, "__del__");
679 if (func) {
680 PyObject* rv = PyObject_CallMethod(self->m_obj, "__del__", NULL);
681 Py_XDECREF(rv);
682 Py_DECREF(func);
683 }
684 if (PyErr_Occurred())
685 PyErr_Clear(); // just ignore it for now
686
687 // Clear the instance's dictionary
4acff284
RD
688 PyInstanceObject* inst = (PyInstanceObject*)self->m_obj;
689 PyDict_Clear(inst->in_dict);
1fded56b
RD
690
691 // put the name of the old class into the instance, and then reset the
692 // class to be the dead class.
4acff284
RD
693 PyDict_SetItemString(inst->in_dict, "_name", inst->in_class->cl_name);
694 inst->in_class = (PyClassObject*)deadObjectClass;
695 Py_INCREF(deadObjectClass);
696 }
1fded56b
RD
697
698 // m_obj is DECREF's in the base class dtor...
4acff284
RD
699 wxPyEndBlockThreads();
700}
7bf85405 701
1fded56b 702
9416aa89
RD
703//---------------------------------------------------------------------------
704// Stuff used by OOR to find the right wxPython class type to return and to
705// build it.
706
707
708// The pointer type map is used when the "pointer" type name generated by SWIG
709// is not the same as the shadow class name, for example wxPyTreeCtrl
710// vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
711// so we'll just make it a Python dictionary in the wx module's namespace.
4acff284 712// (See __wxSetDictionary)
9416aa89
RD
713void wxPyPtrTypeMap_Add(const char* commonName, const char* ptrName) {
714 if (! wxPyPtrTypeMap)
715 wxPyPtrTypeMap = PyDict_New();
9416aa89
RD
716 PyDict_SetItemString(wxPyPtrTypeMap,
717 (char*)commonName,
718 PyString_FromString((char*)ptrName));
719}
720
721
722
a541c325 723PyObject* wxPyClassExists(const wxString& className) {
9416aa89 724
1e4a197e
RD
725 PyObject* item;
726 wxString name(className);
727 char buff[64]; // should always be big enough...
728
9416aa89
RD
729 if (!className)
730 return NULL;
731
1e4a197e
RD
732 // Try the name as-is first
733 sprintf(buff, "%sPtr", (const char*)name.mbc_str());
9416aa89
RD
734 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
735
1e4a197e
RD
736 // if not found see if there is a mapped name for it
737 if ( ! classobj) {
738 if ((item = PyDict_GetItemString(wxPyPtrTypeMap, (char*)(const char*)name.mbc_str())) != NULL) {
739 name = wxString(PyString_AsString(item), *wxConvCurrent);
740 sprintf(buff, "%sPtr", (const char*)name.mbc_str());
741 classobj = PyDict_GetItemString(wxPython_dict, buff);
742 }
743 }
744
9416aa89
RD
745 return classobj; // returns NULL if not found
746}
747
748
2f4e9287 749PyObject* wxPyMake_wxObject(wxObject* source, bool checkEvtHandler) {
0122b7e3
RD
750 PyObject* target = NULL;
751 bool isEvtHandler = FALSE;
9416aa89
RD
752
753 if (source) {
2aab8f16 754 // If it's derived from wxEvtHandler then there may
2f4e9287
RD
755 // already be a pointer to a Python object that we can use
756 // in the OOR data.
757 if (checkEvtHandler && wxIsKindOf(source, wxEvtHandler)) {
758 isEvtHandler = TRUE;
0122b7e3 759 wxEvtHandler* eh = (wxEvtHandler*)source;
4acff284 760 wxPyOORClientData* data = (wxPyOORClientData*)eh->GetClientObject();
0122b7e3
RD
761 if (data) {
762 target = data->m_obj;
763 Py_INCREF(target);
764 }
9416aa89 765 }
0122b7e3
RD
766
767 if (! target) {
2aab8f16
RD
768 // Otherwise make it the old fashioned way by making a
769 // new shadow object and putting this pointer in it.
1e4a197e
RD
770 wxClassInfo* info = source->GetClassInfo();
771 wxString name = info->GetClassName();
0122b7e3
RD
772 PyObject* klass = wxPyClassExists(name);
773 while (info && !klass) {
774 name = (wxChar*)info->GetBaseClassName1();
775 info = wxClassInfo::FindClass(name);
776 klass = wxPyClassExists(name);
777 }
778 if (info) {
779 target = wxPyConstructObject(source, name, klass, FALSE);
780 if (target && isEvtHandler)
4acff284 781 ((wxEvtHandler*)source)->SetClientObject(new wxPyOORClientData(target));
0122b7e3 782 } else {
4acff284 783 wxString msg(wxT("wxPython class not found for "));
0122b7e3 784 msg += source->GetClassInfo()->GetClassName();
a541c325 785 PyErr_SetString(PyExc_NameError, msg.mbc_str());
0122b7e3
RD
786 target = NULL;
787 }
9416aa89
RD
788 }
789 } else { // source was NULL so return None.
790 Py_INCREF(Py_None); target = Py_None;
791 }
792 return target;
793}
794
0122b7e3 795
2f4e9287
RD
796PyObject* wxPyMake_wxSizer(wxSizer* source) {
797 PyObject* target = NULL;
798
799 if (source && wxIsKindOf(source, wxSizer)) {
800 // If it's derived from wxSizer then there may
801 // already be a pointer to a Python object that we can use
802 // in the OOR data.
803 wxSizer* sz = (wxSizer*)source;
4acff284 804 wxPyOORClientData* data = (wxPyOORClientData*)sz->GetClientObject();
2f4e9287
RD
805 if (data) {
806 target = data->m_obj;
807 Py_INCREF(target);
808 }
809 }
810 if (! target) {
811 target = wxPyMake_wxObject(source, FALSE);
812 if (target != Py_None)
4acff284 813 ((wxSizer*)source)->SetClientObject(new wxPyOORClientData(target));
2f4e9287
RD
814 }
815 return target;
816}
817
818
819
7bf85405
RD
820//---------------------------------------------------------------------------
821
c368d904 822PyObject* wxPyConstructObject(void* ptr,
a541c325 823 const wxString& className,
9416aa89 824 PyObject* klass,
1e7ecb7b 825 int setThisOwn) {
9416aa89 826
33510773
RD
827 PyObject* obj;
828 PyObject* arg;
9416aa89 829 PyObject* item;
a541c325 830 wxString name(className);
9416aa89
RD
831 char swigptr[64]; // should always be big enough...
832 char buff[64];
833
a541c325
RD
834 if ((item = PyDict_GetItemString(wxPyPtrTypeMap, (char*)(const char*)name.mbc_str())) != NULL) {
835 name = wxString(PyString_AsString(item), *wxConvCurrent);
9416aa89 836 }
a541c325 837 sprintf(buff, "_%s_p", (const char*)name.mbc_str());
9416aa89
RD
838 SWIG_MakePtr(swigptr, ptr, buff);
839
840 arg = Py_BuildValue("(s)", swigptr);
841 obj = PyInstance_New(klass, arg, NULL);
842 Py_DECREF(arg);
843
844 if (setThisOwn) {
845 PyObject* one = PyInt_FromLong(1);
846 PyObject_SetAttrString(obj, "thisown", one);
847 Py_DECREF(one);
848 }
849
850 return obj;
851}
852
853
854PyObject* wxPyConstructObject(void* ptr,
a541c325 855 const wxString& className,
9416aa89 856 int setThisOwn) {
33510773
RD
857 if (!ptr) {
858 Py_INCREF(Py_None);
859 return Py_None;
860 }
861
9c039d08 862 char buff[64]; // should always be big enough...
a541c325 863 sprintf(buff, "%sPtr", (const char*)className.mbc_str());
c5943253 864
a541c325 865 wxASSERT_MSG(wxPython_dict, wxT("wxPython_dict is not set yet!!"));
c5943253 866
7bf85405
RD
867 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
868 if (! classobj) {
e3dbf593
RD
869 wxString msg(wxT("wxPython class not found for "));
870 msg += className;
871 PyErr_SetString(PyExc_NameError, msg.mbc_str());
872 return NULL;
7bf85405
RD
873 }
874
9416aa89 875 return wxPyConstructObject(ptr, className, classobj, setThisOwn);
7bf85405
RD
876}
877
a541c325 878
d559219f 879//---------------------------------------------------------------------------
7bf85405 880
4958ea8f 881
cf694132 882#ifdef WXP_WITH_THREAD
4268f798
RD
883inline
884unsigned long wxPyGetCurrentThreadId() {
4958ea8f 885 return wxThread::GetCurrentId();
4268f798
RD
886}
887
be43cc44 888static PyThreadState* gs_shutdownTState;
4268f798
RD
889static
890PyThreadState* wxPyGetThreadState() {
be43cc44
RD
891 if (wxPyTMutex == NULL) // Python is shutting down...
892 return gs_shutdownTState;
893
4268f798
RD
894 unsigned long ctid = wxPyGetCurrentThreadId();
895 PyThreadState* tstate = NULL;
896
897 wxPyTMutex->Lock();
898 for(size_t i=0; i < wxPyTStates->GetCount(); i++) {
899 wxPyThreadState& info = wxPyTStates->Item(i);
900 if (info.tid == ctid) {
901 tstate = info.tstate;
902 break;
903 }
904 }
905 wxPyTMutex->Unlock();
a541c325 906 wxASSERT_MSG(tstate, wxT("PyThreadState should not be NULL!"));
4268f798
RD
907 return tstate;
908}
909
910static
911void wxPySaveThreadState(PyThreadState* tstate) {
be43cc44
RD
912 if (wxPyTMutex == NULL) { // Python is shutting down, assume a single thread...
913 gs_shutdownTState = tstate;
914 return;
915 }
4268f798
RD
916 unsigned long ctid = wxPyGetCurrentThreadId();
917 wxPyTMutex->Lock();
918 for(size_t i=0; i < wxPyTStates->GetCount(); i++) {
919 wxPyThreadState& info = wxPyTStates->Item(i);
920 if (info.tid == ctid) {
763d71e4
RD
921#if 0
922 if (info.tstate != tstate)
923 wxLogMessage("*** tstate mismatch!???");
924#endif
46e10fde 925 // info.tstate = tstate; *** DO NOT update existing ones???
763d71e4
RD
926 // Normally it will never change, but apparently COM callbacks
927 // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient
46e10fde 928 // tstate which will then be garbage the next time we try to use
763d71e4 929 // it...
4268f798
RD
930 wxPyTMutex->Unlock();
931 return;
932 }
1112b0c6 933 }
4268f798
RD
934 // not found, so add it...
935 wxPyTStates->Add(new wxPyThreadState(ctid, tstate));
936 wxPyTMutex->Unlock();
937}
938
939#endif
940
941
942// Calls from Python to wxWindows code are wrapped in calls to these
943// functions:
944
945PyThreadState* wxPyBeginAllowThreads() {
946#ifdef WXP_WITH_THREAD
947 PyThreadState* saved = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
948 wxPySaveThreadState(saved);
949 return saved;
950#else
951 return NULL;
952#endif
953}
954
955void wxPyEndAllowThreads(PyThreadState* saved) {
956#ifdef WXP_WITH_THREAD
957 PyEval_RestoreThread(saved); // Py_END_ALLOW_THREADS;
cf694132 958#endif
d559219f 959}
cf694132 960
cf694132 961
4268f798
RD
962
963// Calls from wxWindows back to Python code, or even any PyObject
964// manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
965
966void wxPyBeginBlockThreads() {
cf694132 967#ifdef WXP_WITH_THREAD
4268f798
RD
968 PyThreadState* tstate = wxPyGetThreadState();
969 PyEval_RestoreThread(tstate);
970#endif
971}
972
973
974void wxPyEndBlockThreads() {
975#ifdef WXP_WITH_THREAD
4268f798 976 // Is there any need to save it again?
c4770edd
RD
977 // PyThreadState* tstate =
978 PyEval_SaveThread();
1112b0c6 979#endif
cf694132
RD
980}
981
19a97bd6 982
d559219f 983//---------------------------------------------------------------------------
f74ff5ef
RD
984// wxPyInputStream and wxPyCBInputStream methods
985
f74ff5ef
RD
986
987void wxPyInputStream::close() {
a541c325 988 /* do nothing for now */
f74ff5ef
RD
989}
990
991void wxPyInputStream::flush() {
a541c325 992 /* do nothing for now */
f74ff5ef
RD
993}
994
995bool wxPyInputStream::eof() {
996 if (m_wxis)
997 return m_wxis->Eof();
998 else
999 return TRUE;
1000}
1001
1002wxPyInputStream::~wxPyInputStream() {
1003 /* do nothing */
1004}
1005
a541c325
RD
1006
1007
1008
1009PyObject* wxPyInputStream::read(int size) {
1010 PyObject* obj = NULL;
1011 wxMemoryBuffer buf;
f74ff5ef
RD
1012 const int BUFSIZE = 1024;
1013
1014 // check if we have a real wxInputStream to work with
1015 if (!m_wxis) {
1e4a197e 1016 wxPyBeginBlockThreads();
f74ff5ef 1017 PyErr_SetString(PyExc_IOError, "no valid C-wxInputStream");
1e4a197e 1018 wxPyEndBlockThreads();
f74ff5ef
RD
1019 return NULL;
1020 }
1021
1022 if (size < 0) {
1e4a197e
RD
1023 // read while bytes are available on the stream
1024 while ( m_wxis->CanRead() ) {
a541c325
RD
1025 m_wxis->Read(buf.GetAppendBuf(BUFSIZE), BUFSIZE);
1026 buf.UngetAppendBuf(m_wxis->LastRead());
f74ff5ef
RD
1027 }
1028
1029 } else { // Read only size number of characters
a541c325
RD
1030 m_wxis->Read(buf.GetWriteBuf(size), size);
1031 buf.UngetWriteBuf(m_wxis->LastRead());
1032 }
f74ff5ef 1033
a541c325 1034 // error check
1e4a197e
RD
1035 wxPyBeginBlockThreads();
1036 wxStreamError err = m_wxis->GetLastError();
1037 if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) {
a541c325
RD
1038 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
1039 }
1040 else {
1041 // We use only strings for the streams, not unicode
1042 obj = PyString_FromStringAndSize(buf, buf.GetDataLen());
f74ff5ef 1043 }
1e4a197e 1044 wxPyEndBlockThreads();
a541c325 1045 return obj;
f74ff5ef
RD
1046}
1047
1048
a541c325
RD
1049PyObject* wxPyInputStream::readline(int size) {
1050 PyObject* obj = NULL;
1051 wxMemoryBuffer buf;
1052 int i;
1053 char ch;
1054
f74ff5ef
RD
1055 // check if we have a real wxInputStream to work with
1056 if (!m_wxis) {
1e4a197e 1057 wxPyBeginBlockThreads();
f74ff5ef 1058 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream");
1e4a197e 1059 wxPyEndBlockThreads();
f74ff5ef
RD
1060 return NULL;
1061 }
1062
f74ff5ef 1063 // read until \n or byte limit reached
1e4a197e 1064 for (i=ch=0; (ch != '\n') && (m_wxis->CanRead()) && ((size < 0) || (i < size)); i++) {
a541c325
RD
1065 ch = m_wxis->GetC();
1066 buf.AppendByte(ch);
f74ff5ef
RD
1067 }
1068
1069 // errorcheck
1e4a197e
RD
1070 wxPyBeginBlockThreads();
1071 wxStreamError err = m_wxis->GetLastError();
1072 if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) {
f74ff5ef 1073 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
f74ff5ef 1074 }
a541c325
RD
1075 else {
1076 // We use only strings for the streams, not unicode
1077 obj = PyString_FromStringAndSize((char*)buf.GetData(), buf.GetDataLen());
1078 }
1e4a197e 1079 wxPyEndBlockThreads();
a541c325 1080 return obj;
f74ff5ef
RD
1081}
1082
1083
a541c325
RD
1084PyObject* wxPyInputStream::readlines(int sizehint) {
1085 PyObject* pylist;
1086
f74ff5ef
RD
1087 // check if we have a real wxInputStream to work with
1088 if (!m_wxis) {
1e4a197e
RD
1089 wxPyBeginBlockThreads();
1090 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream");
1091 wxPyEndBlockThreads();
f74ff5ef
RD
1092 return NULL;
1093 }
1094
1095 // init list
1e4a197e 1096 wxPyBeginBlockThreads();
a541c325
RD
1097 pylist = PyList_New(0);
1098 if (!pylist) {
1e4a197e 1099 wxPyBeginBlockThreads();
f74ff5ef 1100 PyErr_NoMemory();
1e4a197e 1101 wxPyEndBlockThreads();
f74ff5ef
RD
1102 return NULL;
1103 }
1104
1105 // read sizehint bytes or until EOF
1106 int i;
1e4a197e 1107 for (i=0; (m_wxis->CanRead()) && ((sizehint < 0) || (i < sizehint));) {
a541c325 1108 PyObject* s = this->readline();
f74ff5ef 1109 if (s == NULL) {
1e4a197e 1110 wxPyBeginBlockThreads();
a541c325 1111 Py_DECREF(pylist);
1e4a197e 1112 wxPyEndBlockThreads();
f74ff5ef
RD
1113 return NULL;
1114 }
1e4a197e 1115 wxPyBeginBlockThreads();
a541c325
RD
1116 PyList_Append(pylist, s);
1117 i += PyString_Size(s);
1e4a197e 1118 wxPyEndBlockThreads();
f74ff5ef
RD
1119 }
1120
1121 // error check
1e4a197e
RD
1122 wxStreamError err = m_wxis->GetLastError();
1123 if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) {
1124 wxPyBeginBlockThreads();
a541c325 1125 Py_DECREF(pylist);
f74ff5ef 1126 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
1e4a197e 1127 wxPyEndBlockThreads();
f74ff5ef
RD
1128 return NULL;
1129 }
a541c325
RD
1130
1131 return pylist;
f74ff5ef
RD
1132}
1133
1134
1135void wxPyInputStream::seek(int offset, int whence) {
1136 if (m_wxis)
1137 m_wxis->SeekI(offset, wxSeekMode(whence));
1138}
1139
1140int wxPyInputStream::tell(){
1141 if (m_wxis)
1142 return m_wxis->TellI();
1143 else return 0;
1144}
1145
1146
1147
1148
1149wxPyCBInputStream::wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block)
1150 : wxInputStream(), m_read(r), m_seek(s), m_tell(t), m_block(block)
1151{}
1152
1153
1154wxPyCBInputStream::~wxPyCBInputStream() {
1155 if (m_block) wxPyBeginBlockThreads();
1156 Py_XDECREF(m_read);
1157 Py_XDECREF(m_seek);
1158 Py_XDECREF(m_tell);
1159 if (m_block) wxPyEndBlockThreads();
1160}
1161
1162
1163wxPyCBInputStream* wxPyCBInputStream::create(PyObject *py, bool block) {
1164 if (block) wxPyBeginBlockThreads();
1165
1166 PyObject* read = getMethod(py, "read");
1167 PyObject* seek = getMethod(py, "seek");
1168 PyObject* tell = getMethod(py, "tell");
1169
1170 if (!read) {
1171 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
1172 Py_XDECREF(read);
1173 Py_XDECREF(seek);
1174 Py_XDECREF(tell);
1175 if (block) wxPyEndBlockThreads();
1176 return NULL;
1177 }
1178
1179 if (block) wxPyEndBlockThreads();
1180 return new wxPyCBInputStream(read, seek, tell, block);
1181}
1182
249a57f4
RD
1183
1184wxPyCBInputStream* wxPyCBInputStream_create(PyObject *py, bool block) {
1185 return wxPyCBInputStream::create(py, block);
1186}
1187
f74ff5ef
RD
1188PyObject* wxPyCBInputStream::getMethod(PyObject* py, char* name) {
1189 if (!PyObject_HasAttrString(py, name))
1190 return NULL;
1191 PyObject* o = PyObject_GetAttrString(py, name);
1192 if (!PyMethod_Check(o) && !PyCFunction_Check(o)) {
1193 Py_DECREF(o);
1194 return NULL;
1195 }
1196 return o;
1197}
1198
1199
1200size_t wxPyCBInputStream::GetSize() const {
1201 wxPyCBInputStream* self = (wxPyCBInputStream*)this; // cast off const
1202 if (m_seek && m_tell) {
1203 off_t temp = self->OnSysTell();
1204 off_t ret = self->OnSysSeek(0, wxFromEnd);
1205 self->OnSysSeek(temp, wxFromStart);
1206 return ret;
1207 }
1208 else
1209 return 0;
1210}
1211
1212
1213size_t wxPyCBInputStream::OnSysRead(void *buffer, size_t bufsize) {
1214 if (bufsize == 0)
1215 return 0;
1216
1217 wxPyBeginBlockThreads();
1218 PyObject* arglist = Py_BuildValue("(i)", bufsize);
1219 PyObject* result = PyEval_CallObject(m_read, arglist);
1220 Py_DECREF(arglist);
1221
1222 size_t o = 0;
a541c325 1223 if ((result != NULL) && PyString_Check(result)) {
f74ff5ef
RD
1224 o = PyString_Size(result);
1225 if (o == 0)
1226 m_lasterror = wxSTREAM_EOF;
1227 if (o > bufsize)
1228 o = bufsize;
a541c325 1229 memcpy((char*)buffer, PyString_AsString(result), o); // strings only, not unicode...
f74ff5ef
RD
1230 Py_DECREF(result);
1231
1232 }
1233 else
1234 m_lasterror = wxSTREAM_READ_ERROR;
1235 wxPyEndBlockThreads();
f74ff5ef
RD
1236 return o;
1237}
1238
1239size_t wxPyCBInputStream::OnSysWrite(const void *buffer, size_t bufsize) {
1240 m_lasterror = wxSTREAM_WRITE_ERROR;
1241 return 0;
1242}
1243
1244off_t wxPyCBInputStream::OnSysSeek(off_t off, wxSeekMode mode) {
1245 wxPyBeginBlockThreads();
1fded56b
RD
1246#ifdef _LARGE_FILES
1247 // off_t is a 64-bit value...
1248 PyObject* arglist = Py_BuildValue("(Li)", off, mode);
1249#else
f74ff5ef 1250 PyObject* arglist = Py_BuildValue("(ii)", off, mode);
1fded56b 1251#endif
f74ff5ef
RD
1252 PyObject* result = PyEval_CallObject(m_seek, arglist);
1253 Py_DECREF(arglist);
1254 Py_XDECREF(result);
1255 wxPyEndBlockThreads();
1256 return OnSysTell();
1257}
1258
1fded56b 1259
f74ff5ef
RD
1260off_t wxPyCBInputStream::OnSysTell() const {
1261 wxPyBeginBlockThreads();
1262 PyObject* arglist = Py_BuildValue("()");
1263 PyObject* result = PyEval_CallObject(m_tell, arglist);
1264 Py_DECREF(arglist);
1265 off_t o = 0;
1266 if (result != NULL) {
1fded56b
RD
1267#ifdef _LARGE_FILES
1268 if (PyLong_Check(result))
1269 o = PyLong_AsLongLong(result);
1270 else
1271#else
f74ff5ef 1272 o = PyInt_AsLong(result);
1fded56b 1273#endif
f74ff5ef
RD
1274 Py_DECREF(result);
1275 };
1276 wxPyEndBlockThreads();
1277 return o;
1278}
1279
1280//----------------------------------------------------------------------
d559219f 1281
2f90df85
RD
1282IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject);
1283
d559219f
RD
1284wxPyCallback::wxPyCallback(PyObject* func) {
1285 m_func = func;
1286 Py_INCREF(m_func);
1287}
1288
2f90df85
RD
1289wxPyCallback::wxPyCallback(const wxPyCallback& other) {
1290 m_func = other.m_func;
1291 Py_INCREF(m_func);
1292}
1293
d559219f 1294wxPyCallback::~wxPyCallback() {
4268f798 1295 wxPyBeginBlockThreads();
d559219f 1296 Py_DECREF(m_func);
4268f798 1297 wxPyEndBlockThreads();
d559219f
RD
1298}
1299
cf694132
RD
1300
1301
7bf85405
RD
1302// This function is used for all events destined for Python event handlers.
1303void wxPyCallback::EventThunker(wxEvent& event) {
1304 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
1305 PyObject* func = cb->m_func;
1306 PyObject* result;
1307 PyObject* arg;
1308 PyObject* tuple;
1309
cf694132 1310
4268f798 1311 wxPyBeginBlockThreads();
65dd82cb
RD
1312 wxString className = event.GetClassInfo()->GetClassName();
1313
1e4a197e 1314 if (className == wxT("wxPyEvent"))
65dd82cb 1315 arg = ((wxPyEvent*)&event)->GetSelf();
1e4a197e 1316 else if (className == wxT("wxPyCommandEvent"))
65dd82cb 1317 arg = ((wxPyCommandEvent*)&event)->GetSelf();
c8bc7bb8 1318 else {
a541c325 1319 arg = wxPyConstructObject((void*)&event, className);
c8bc7bb8 1320 }
7bf85405
RD
1321
1322 tuple = PyTuple_New(1);
1323 PyTuple_SET_ITEM(tuple, 0, arg);
1324 result = PyEval_CallObject(func, tuple);
7bf85405
RD
1325 Py_DECREF(tuple);
1326 if (result) {
1327 Py_DECREF(result);
ac346f50 1328 PyErr_Clear(); // Just in case...
7bf85405
RD
1329 } else {
1330 PyErr_Print();
1331 }
4268f798 1332 wxPyEndBlockThreads();
7bf85405
RD
1333}
1334
1335
d559219f 1336//----------------------------------------------------------------------
7bf85405 1337
2f90df85
RD
1338wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) {
1339 m_lastFound = NULL;
1340 m_self = other.m_self;
f6bcfd97
BP
1341 m_class = other.m_class;
1342 if (m_self) {
2f90df85 1343 Py_INCREF(m_self);
f6bcfd97
BP
1344 Py_INCREF(m_class);
1345 }
2f90df85
RD
1346}
1347
1348
33510773 1349void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) {
d559219f 1350 m_self = self;
33510773 1351 m_class = klass;
b7312675 1352 m_incRef = incref;
f6bcfd97 1353 if (incref) {
2f90df85 1354 Py_INCREF(m_self);
f6bcfd97
BP
1355 Py_INCREF(m_class);
1356 }
d559219f 1357}
8bf5d46e 1358
8bf5d46e 1359
78b57918
RD
1360#if PYTHON_API_VERSION >= 1011
1361
1362// Prior to Python 2.2 PyMethod_GetClass returned the class object
1363// in which the method was defined. Starting with 2.2 it returns
1364// "class that asked for the method" which seems totally bogus to me
4152e8b9 1365// but apprently it fixes some obscure problem waiting to happen in
78b57918
RD
1366// Python. Since the API was not documented Guido and the gang felt
1367// safe in changing it. Needless to say that totally screwed up the
1368// logic below in wxPyCallbackHelper::findCallback, hence this icky
4152e8b9 1369// code to find the class where the method is actually defined...
78b57918
RD
1370
1371static
1372PyObject* PyFindClassWithAttr(PyObject *klass, PyObject *name)
1373{
1374 int i, n;
1375
1376 if (PyType_Check(klass)) { // new style classes
1377 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
4a98c806 1378 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
78b57918
RD
1379 PyTypeObject* type = (PyTypeObject*)klass;
1380 PyObject *mro, *res, *base, *dict;
1381 /* Look in tp_dict of types in MRO */
1382 mro = type->tp_mro;
1383 assert(PyTuple_Check(mro));
1384 n = PyTuple_GET_SIZE(mro);
1385 for (i = 0; i < n; i++) {
1386 base = PyTuple_GET_ITEM(mro, i);
1387 if (PyClass_Check(base))
1388 dict = ((PyClassObject *)base)->cl_dict;
1389 else {
1390 assert(PyType_Check(base));
1391 dict = ((PyTypeObject *)base)->tp_dict;
1392 }
1393 assert(dict && PyDict_Check(dict));
1394 res = PyDict_GetItem(dict, name);
1395 if (res != NULL)
4a98c806 1396 return base;
78b57918
RD
1397 }
1398 return NULL;
1399 }
1400
1401 else if (PyClass_Check(klass)) { // old style classes
1402 // This code is borrowed/adapted from class_lookup in classobject.c
1403 PyClassObject* cp = (PyClassObject*)klass;
1404 PyObject *value = PyDict_GetItem(cp->cl_dict, name);
1405 if (value != NULL) {
1406 return (PyObject*)cp;
1407 }
1408 n = PyTuple_Size(cp->cl_bases);
1409 for (i = 0; i < n; i++) {
1410 PyObject* base = PyTuple_GetItem(cp->cl_bases, i);
1411 PyObject *v = PyFindClassWithAttr(base, name);
1412 if (v != NULL)
1413 return v;
1414 }
1415 return NULL;
1416 }
4152e8b9 1417 return NULL;
78b57918
RD
1418}
1419#endif
1420
1421
1422static
1423PyObject* PyMethod_GetDefiningClass(PyObject* method, const char* name)
1424{
1425 PyObject* mgc = PyMethod_GET_CLASS(method);
1426
1427#if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1428 return mgc;
1429#else // 2.2 and after, the hard way...
1430
1431 PyObject* nameo = PyString_FromString(name);
1432 PyObject* klass = PyFindClassWithAttr(mgc, nameo);
1433 Py_DECREF(nameo);
1434 return klass;
1435#endif
1436}
1437
1438
1439
de20db99 1440bool wxPyCallbackHelper::findCallback(const char* name) const {
f6bcfd97
BP
1441 wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const
1442 self->m_lastFound = NULL;
78b57918
RD
1443
1444 // If the object (m_self) has an attibute of the given name...
de20db99 1445 if (m_self && PyObject_HasAttrString(m_self, (char*)name)) {
78b57918 1446 PyObject *method, *klass;
de20db99 1447 method = PyObject_GetAttrString(m_self, (char*)name);
f6bcfd97 1448
78b57918
RD
1449 // ...and if that attribute is a method, and if that method's class is
1450 // not from a base class...
f6bcfd97 1451 if (PyMethod_Check(method) &&
78b57918
RD
1452 (klass = PyMethod_GetDefiningClass(method, (char*)name)) != NULL &&
1453 ((klass == m_class) || PyClass_IsSubclass(klass, m_class))) {
8bf5d46e 1454
78b57918 1455 // ...then we'll save a pointer to the method so callCallback can call it.
f6bcfd97
BP
1456 self->m_lastFound = method;
1457 }
de20db99
RD
1458 else {
1459 Py_DECREF(method);
1460 }
f6bcfd97 1461 }
d559219f
RD
1462 return m_lastFound != NULL;
1463}
8bf5d46e 1464
d559219f 1465
f6bcfd97 1466int wxPyCallbackHelper::callCallback(PyObject* argTuple) const {
d559219f
RD
1467 PyObject* result;
1468 int retval = FALSE;
1469
1470 result = callCallbackObj(argTuple);
1471 if (result) { // Assumes an integer return type...
1472 retval = PyInt_AsLong(result);
1473 Py_DECREF(result);
1474 PyErr_Clear(); // forget about it if it's not...
1475 }
1476 return retval;
1477}
1478
1479// Invoke the Python callable object, returning the raw PyObject return
1480// value. Caller should DECREF the return value and also call PyEval_SaveThread.
f6bcfd97 1481PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const {
de20db99 1482 PyObject* result;
d559219f 1483
de20db99
RD
1484 // Save a copy of the pointer in case the callback generates another
1485 // callback. In that case m_lastFound will have a different value when
1486 // it gets back here...
1487 PyObject* method = m_lastFound;
1488
1489 result = PyEval_CallObject(method, argTuple);
d559219f 1490 Py_DECREF(argTuple);
de20db99 1491 Py_DECREF(method);
d559219f
RD
1492 if (!result) {
1493 PyErr_Print();
1494 }
1495 return result;
1496}
714e6a9e 1497
7bf85405 1498
0122b7e3 1499void wxPyCBH_setCallbackInfo(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) {
1e7ecb7b
RD
1500 cbh.setSelf(self, klass, incref);
1501}
1502
1503bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) {
1504 return cbh.findCallback(name);
1505}
1506
1507int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
1508 return cbh.callCallback(argTuple);
1509}
1510
1511PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
1512 return cbh.callCallbackObj(argTuple);
1513}
1514
1515
1516void wxPyCBH_delete(wxPyCallbackHelper* cbh) {
1e7ecb7b 1517 if (cbh->m_incRef) {
4268f798 1518 wxPyBeginBlockThreads();
1e7ecb7b
RD
1519 Py_XDECREF(cbh->m_self);
1520 Py_XDECREF(cbh->m_class);
4268f798 1521 wxPyEndBlockThreads();
1e7ecb7b 1522 }
1e7ecb7b 1523}
d559219f 1524
65dd82cb
RD
1525//---------------------------------------------------------------------------
1526//---------------------------------------------------------------------------
2aab8f16 1527// These event classes can be derived from in Python and passed through the event
c368d904 1528// system without losing anything. They do this by keeping a reference to
65dd82cb
RD
1529// themselves and some special case handling in wxPyCallback::EventThunker.
1530
1531
e19b7164 1532wxPyEvtSelfRef::wxPyEvtSelfRef() {
65dd82cb
RD
1533 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1534 //Py_INCREF(m_self); // circular loops...
194fa2ac 1535 m_cloned = FALSE;
65dd82cb
RD
1536}
1537
e19b7164 1538wxPyEvtSelfRef::~wxPyEvtSelfRef() {
4268f798 1539 wxPyBeginBlockThreads();
65dd82cb
RD
1540 if (m_cloned)
1541 Py_DECREF(m_self);
4268f798 1542 wxPyEndBlockThreads();
65dd82cb
RD
1543}
1544
e19b7164 1545void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) {
4268f798 1546 wxPyBeginBlockThreads();
65dd82cb
RD
1547 if (m_cloned)
1548 Py_DECREF(m_self);
1549 m_self = self;
1550 if (clone) {
1551 Py_INCREF(m_self);
1552 m_cloned = TRUE;
1553 }
4268f798 1554 wxPyEndBlockThreads();
65dd82cb
RD
1555}
1556
e19b7164 1557PyObject* wxPyEvtSelfRef::GetSelf() const {
65dd82cb
RD
1558 Py_INCREF(m_self);
1559 return m_self;
1560}
1561
1562
07b2e1cd
RD
1563IMPLEMENT_ABSTRACT_CLASS(wxPyEvent, wxEvent);
1564IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent, wxCommandEvent);
1565
1566
3ef86e32
RD
1567wxPyEvent::wxPyEvent(int winid, wxEventType commandType)
1568 : wxEvent(winid, commandType) {
65dd82cb
RD
1569}
1570
65dd82cb 1571
07b2e1cd
RD
1572wxPyEvent::wxPyEvent(const wxPyEvent& evt)
1573 : wxEvent(evt)
1574{
1575 SetSelf(evt.m_self, TRUE);
65dd82cb
RD
1576}
1577
1578
07b2e1cd
RD
1579wxPyEvent::~wxPyEvent() {
1580}
65dd82cb
RD
1581
1582
1583wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id)
1584 : wxCommandEvent(commandType, id) {
1585}
1586
65dd82cb 1587
07b2e1cd
RD
1588wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent& evt)
1589 : wxCommandEvent(evt)
1590{
1591 SetSelf(evt.m_self, TRUE);
65dd82cb
RD
1592}
1593
1594
07b2e1cd
RD
1595wxPyCommandEvent::~wxPyCommandEvent() {
1596}
1597
65dd82cb
RD
1598
1599
1600
d559219f 1601//---------------------------------------------------------------------------
7bf85405
RD
1602//---------------------------------------------------------------------------
1603
d559219f 1604
7bf85405
RD
1605wxPyTimer::wxPyTimer(PyObject* callback) {
1606 func = callback;
1607 Py_INCREF(func);
1608}
1609
1610wxPyTimer::~wxPyTimer() {
4268f798 1611 wxPyBeginBlockThreads();
7bf85405 1612 Py_DECREF(func);
4268f798 1613 wxPyEndBlockThreads();
7bf85405
RD
1614}
1615
1616void wxPyTimer::Notify() {
185d7c3e
RD
1617 if (!func || func == Py_None) {
1618 wxTimer::Notify();
1619 }
1620 else {
4268f798 1621 wxPyBeginBlockThreads();
185d7c3e
RD
1622
1623 PyObject* result;
1624 PyObject* args = Py_BuildValue("()");
1625
1626 result = PyEval_CallObject(func, args);
1627 Py_DECREF(args);
1628 if (result) {
1629 Py_DECREF(result);
1630 PyErr_Clear();
1631 } else {
1632 PyErr_Print();
1633 }
7bf85405 1634
4268f798 1635 wxPyEndBlockThreads();
7bf85405
RD
1636 }
1637}
1638
1639
cf694132 1640
2f90df85 1641//---------------------------------------------------------------------------
389c5527
RD
1642//---------------------------------------------------------------------------
1643// Convert a wxList to a Python List
1644
1e4a197e
RD
1645PyObject* wxPy_ConvertList(wxListBase* listbase, const char* className) {
1646 wxList* list = (wxList*)listbase; // this is probably bad...
389c5527
RD
1647 PyObject* pyList;
1648 PyObject* pyObj;
1649 wxObject* wxObj;
1e4a197e 1650 wxNode* node = list->GetFirst();
389c5527 1651
4268f798 1652 wxPyBeginBlockThreads();
389c5527
RD
1653 pyList = PyList_New(0);
1654 while (node) {
1e4a197e 1655 wxObj = node->GetData();
9416aa89 1656 pyObj = wxPyMake_wxObject(wxObj); //wxPyConstructObject(wxObj, className);
389c5527 1657 PyList_Append(pyList, pyObj);
1e4a197e 1658 node = node->GetNext();
389c5527 1659 }
4268f798 1660 wxPyEndBlockThreads();
389c5527
RD
1661 return pyList;
1662}
1663
54b96882
RD
1664//----------------------------------------------------------------------
1665
1666long wxPyGetWinHandle(wxWindow* win) {
1667#ifdef __WXMSW__
1668 return (long)win->GetHandle();
1669#endif
1670
1671 // Find and return the actual X-Window.
1672#ifdef __WXGTK__
1673 if (win->m_wxwindow) {
1e4a197e
RD
1674#ifdef __WXGTK20__
1675 return (long) GDK_WINDOW_XWINDOW(GTK_PIZZA(win->m_wxwindow)->bin_window);
1676#else
54b96882
RD
1677 GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window;
1678 if (bwin) {
1679 return (long)bwin->xwindow;
1680 }
1e4a197e 1681#endif
54b96882
RD
1682 }
1683#endif
1684 return 0;
1685}
1686
7bf85405
RD
1687//----------------------------------------------------------------------
1688// Some helper functions for typemaps in my_typemaps.i, so they won't be
c8bc7bb8
RD
1689// included in every file over and over again...
1690
1691#if PYTHON_API_VERSION >= 1009
1692 static char* wxStringErrorMsg = "String or Unicode type required";
1693#else
1694 static char* wxStringErrorMsg = "String type required";
1695#endif
1696
1697
1698wxString* wxString_in_helper(PyObject* source) {
1699 wxString* target;
1700#if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1701 if (!PyString_Check(source) && !PyUnicode_Check(source)) {
1702 PyErr_SetString(PyExc_TypeError, wxStringErrorMsg);
1703 return NULL;
1704 }
1705#if wxUSE_UNICODE
1706 if (PyUnicode_Check(source)) {
1e4a197e
RD
1707 target = new wxString();
1708 size_t len = PyUnicode_GET_SIZE(source);
1709 if (len) {
1710 PyUnicode_AsWideChar((PyUnicodeObject*)source, target->GetWriteBuf(len), len);
1711 target->UngetWriteBuf();
1712 }
c8bc7bb8 1713 } else {
a541c325
RD
1714 // It is a string, get pointers to it and transform to unicode
1715 char* tmpPtr; int tmpSize;
1716 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1717 target = new wxString(tmpPtr, *wxConvCurrent, tmpSize);
c8bc7bb8
RD
1718 }
1719#else
1720 char* tmpPtr; int tmpSize;
1721 if (PyString_AsStringAndSize(source, &tmpPtr, &tmpSize) == -1) {
1722 PyErr_SetString(PyExc_TypeError, "Unable to convert string");
1723 return NULL;
1724 }
1725 target = new wxString(tmpPtr, tmpSize);
1726#endif // wxUSE_UNICODE
1727
1728#else // No Python unicode API (1.5.2)
1729 if (!PyString_Check(source)) {
1730 PyErr_SetString(PyExc_TypeError, wxStringErrorMsg);
1731 return NULL;
1732 }
1733 target = new wxString(PyString_AS_STRING(source), PyString_GET_SIZE(source));
1734#endif
1735 return target;
1736}
1737
7bf85405 1738
a541c325
RD
1739// Similar to above except doesn't use "new" and doesn't set an exception
1740wxString Py2wxString(PyObject* source)
1741{
1742 wxString target;
1743 bool doDecRef = FALSE;
1744
1745#if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1746 if (!PyString_Check(source) && !PyUnicode_Check(source)) {
1747 // Convert to String if not one already... (TODO: Unicode too?)
1748 source = PyObject_Str(source);
1749 doDecRef = TRUE;
1750 }
1751
1752#if wxUSE_UNICODE
1753 if (PyUnicode_Check(source)) {
1e4a197e
RD
1754 size_t len = PyUnicode_GET_SIZE(source);
1755 if (len) {
1756 PyUnicode_AsWideChar((PyUnicodeObject*)source, target.GetWriteBuf(len), len);
1757 target.UngetWriteBuf();
1758 }
a541c325
RD
1759 } else {
1760 // It is a string, get pointers to it and transform to unicode
1761 char* tmpPtr; int tmpSize;
1762 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1763 target = wxString(tmpPtr, *wxConvCurrent, tmpSize);
1764 }
1765#else
1766 char* tmpPtr; int tmpSize;
1767 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1768 target = wxString(tmpPtr, tmpSize);
1769#endif // wxUSE_UNICODE
1770
1771#else // No Python unicode API (1.5.2)
1772 if (!PyString_Check(source)) {
1773 // Convert to String if not one already...
1774 source = PyObject_Str(source);
1775 doDecRef = TRUE;
1776 }
1777 target = wxString(PyString_AS_STRING(source), PyString_GET_SIZE(source));
1778#endif
1779
1780 if (doDecRef)
1781 Py_DECREF(source);
1782 return target;
1783}
1784
1785
1786// Make either a Python String or Unicode object, depending on build mode
1787PyObject* wx2PyString(const wxString& src)
1788{
1789 PyObject* str;
1790#if wxUSE_UNICODE
1e4a197e 1791 str = PyUnicode_FromWideChar(src.c_str(), src.Len());
a541c325
RD
1792#else
1793 str = PyString_FromStringAndSize(src.c_str(), src.Len());
1794#endif
1795 return str;
1796}
1797
1798
1799//----------------------------------------------------------------------
1800
7bf85405 1801
2f90df85 1802byte* byte_LIST_helper(PyObject* source) {
b639c3c5
RD
1803 if (!PyList_Check(source)) {
1804 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1805 return NULL;
1806 }
1807 int count = PyList_Size(source);
1808 byte* temp = new byte[count];
1809 if (! temp) {
1810 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1811 return NULL;
1812 }
1813 for (int x=0; x<count; x++) {
1814 PyObject* o = PyList_GetItem(source, x);
1815 if (! PyInt_Check(o)) {
1816 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1817 return NULL;
1818 }
1819 temp[x] = (byte)PyInt_AsLong(o);
1820 }
1821 return temp;
1822}
1823
1824
2f90df85 1825int* int_LIST_helper(PyObject* source) {
7bf85405
RD
1826 if (!PyList_Check(source)) {
1827 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1828 return NULL;
1829 }
1830 int count = PyList_Size(source);
1831 int* temp = new int[count];
1832 if (! temp) {
1833 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1834 return NULL;
1835 }
1836 for (int x=0; x<count; x++) {
1837 PyObject* o = PyList_GetItem(source, x);
1838 if (! PyInt_Check(o)) {
1839 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1840 return NULL;
1841 }
1842 temp[x] = PyInt_AsLong(o);
1843 }
1844 return temp;
1845}
1846
1847
2f90df85 1848long* long_LIST_helper(PyObject* source) {
7bf85405
RD
1849 if (!PyList_Check(source)) {
1850 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1851 return NULL;
1852 }
1853 int count = PyList_Size(source);
1854 long* temp = new long[count];
1855 if (! temp) {
1856 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1857 return NULL;
1858 }
1859 for (int x=0; x<count; x++) {
1860 PyObject* o = PyList_GetItem(source, x);
1861 if (! PyInt_Check(o)) {
1862 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1863 return NULL;
1864 }
1865 temp[x] = PyInt_AsLong(o);
1866 }
1867 return temp;
1868}
1869
1870
2f90df85 1871char** string_LIST_helper(PyObject* source) {
7bf85405
RD
1872 if (!PyList_Check(source)) {
1873 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1874 return NULL;
1875 }
1876 int count = PyList_Size(source);
1877 char** temp = new char*[count];
1878 if (! temp) {
1879 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1880 return NULL;
1881 }
1882 for (int x=0; x<count; x++) {
1883 PyObject* o = PyList_GetItem(source, x);
1884 if (! PyString_Check(o)) {
1885 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
1886 return NULL;
1887 }
1888 temp[x] = PyString_AsString(o);
1889 }
1890 return temp;
1891}
1892
e0672e2f
RD
1893//--------------------------------
1894// Part of patch from Tim Hochberg
1895static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) {
1896 if (PyInt_Check(o1) && PyInt_Check(o2)) {
1897 point->x = PyInt_AS_LONG(o1);
1898 point->y = PyInt_AS_LONG(o2);
1899 return true;
1900 }
1901 if (PyFloat_Check(o1) && PyFloat_Check(o2)) {
1902 point->x = (int)PyFloat_AS_DOUBLE(o1);
1903 point->y = (int)PyFloat_AS_DOUBLE(o2);
1904 return true;
1905 }
1906 if (PyInstance_Check(o1) || PyInstance_Check(o2)) {
1907 // Disallow instances because they can cause havok
1908 return false;
1909 }
1910 if (PyNumber_Check(o1) && PyNumber_Check(o2)) {
1911 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1912 point->x = PyInt_AsLong(o1);
1913 point->y = PyInt_AsLong(o2);
1914 return true;
1915 }
1916 return false;
1917}
7bf85405
RD
1918
1919
e0672e2f
RD
1920wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) {
1921 // Putting all of the declarations here allows
1922 // us to put the error handling all in one place.
1923 int x;
1924 wxPoint* temp;
1925 PyObject *o, *o1, *o2;
9d37f964 1926 bool isFast = PyList_Check(source) || PyTuple_Check(source);
e0672e2f 1927
e0672e2f
RD
1928 if (!PySequence_Check(source)) {
1929 goto error0;
7bf85405 1930 }
9d37f964
RD
1931
1932 // The length of the sequence is returned in count.
e0672e2f
RD
1933 *count = PySequence_Length(source);
1934 if (*count < 0) {
1935 goto error0;
1936 }
1937
1938 temp = new wxPoint[*count];
1939 if (!temp) {
7bf85405
RD
1940 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1941 return NULL;
1942 }
e0672e2f
RD
1943 for (x=0; x<*count; x++) {
1944 // Get an item: try fast way first.
1945 if (isFast) {
1946 o = PySequence_Fast_GET_ITEM(source, x);
1947 }
1948 else {
1949 o = PySequence_GetItem(source, x);
1950 if (o == NULL) {
1951 goto error1;
1952 }
1953 }
7bf85405 1954
e0672e2f
RD
1955 // Convert o to wxPoint.
1956 if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) ||
1957 (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) {
1958 o1 = PySequence_Fast_GET_ITEM(o, 0);
1959 o2 = PySequence_Fast_GET_ITEM(o, 1);
1960 if (!wxPointFromObjects(o1, o2, &temp[x])) {
1961 goto error2;
1962 }
7bf85405 1963 }
d559219f
RD
1964 else if (PyInstance_Check(o)) {
1965 wxPoint* pt;
e0672e2f
RD
1966 if (SWIG_GetPtrObj(o, (void **)&pt, "_wxPoint_p")) {
1967 goto error2;
d559219f
RD
1968 }
1969 temp[x] = *pt;
1970 }
e0672e2f
RD
1971 else if (PySequence_Check(o) && PySequence_Length(o) == 2) {
1972 o1 = PySequence_GetItem(o, 0);
1973 o2 = PySequence_GetItem(o, 1);
1974 if (!wxPointFromObjects(o1, o2, &temp[x])) {
1975 goto error3;
1976 }
1977 Py_DECREF(o1);
1978 Py_DECREF(o2);
1979 }
7bf85405 1980 else {
e0672e2f 1981 goto error2;
7bf85405 1982 }
e0672e2f
RD
1983 // Clean up.
1984 if (!isFast)
1985 Py_DECREF(o);
7bf85405
RD
1986 }
1987 return temp;
e0672e2f
RD
1988
1989error3:
1990 Py_DECREF(o1);
1991 Py_DECREF(o2);
1992error2:
1993 if (!isFast)
1994 Py_DECREF(o);
1995error1:
1e4a197e 1996 delete [] temp;
e0672e2f
RD
1997error0:
1998 PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints.");
1999 return NULL;
7bf85405 2000}
e0672e2f
RD
2001// end of patch
2002//------------------------------
7bf85405
RD
2003
2004
2f90df85 2005wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
7bf85405
RD
2006 if (!PyList_Check(source)) {
2007 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2008 return NULL;
2009 }
2010 int count = PyList_Size(source);
2011 wxBitmap** temp = new wxBitmap*[count];
2012 if (! temp) {
2013 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2014 return NULL;
2015 }
2016 for (int x=0; x<count; x++) {
2017 PyObject* o = PyList_GetItem(source, x);
e166644c 2018 if (PyInstance_Check(o)) {
7bf85405 2019 wxBitmap* pt;
e166644c 2020 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) {
7bf85405
RD
2021 PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p.");
2022 return NULL;
2023 }
2024 temp[x] = pt;
2025 }
2026 else {
2027 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
2028 return NULL;
2029 }
2030 }
2031 return temp;
2032}
2033
2034
2035
2f90df85 2036wxString* wxString_LIST_helper(PyObject* source) {
7bf85405
RD
2037 if (!PyList_Check(source)) {
2038 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2039 return NULL;
2040 }
2041 int count = PyList_Size(source);
2042 wxString* temp = new wxString[count];
2043 if (! temp) {
2044 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2045 return NULL;
2046 }
2047 for (int x=0; x<count; x++) {
2048 PyObject* o = PyList_GetItem(source, x);
ecc08ead
RD
2049#if PYTHON_API_VERSION >= 1009
2050 if (! PyString_Check(o) && ! PyUnicode_Check(o)) {
2051 PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects.");
2052 return NULL;
2053 }
ecc08ead 2054#else
7bf85405
RD
2055 if (! PyString_Check(o)) {
2056 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
2057 return NULL;
2058 }
ecc08ead 2059#endif
a541c325
RD
2060
2061 wxString* pStr = wxString_in_helper(o);
2062 temp[x] = *pStr;
2063 delete pStr;
7bf85405
RD
2064 }
2065 return temp;
2066}
2067
2068
2f90df85 2069wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
7bf85405
RD
2070 if (!PyList_Check(source)) {
2071 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2072 return NULL;
2073 }
2074 int count = PyList_Size(source);
2075 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
2076 if (! temp) {
2077 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2078 return NULL;
2079 }
2080 for (int x=0; x<count; x++) {
2081 PyObject* o = PyList_GetItem(source, x);
e166644c 2082 if (PyInstance_Check(o)) {
7bf85405 2083 wxAcceleratorEntry* ae;
e166644c 2084 if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) {
7bf85405
RD
2085 PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p.");
2086 return NULL;
2087 }
2088 temp[x] = *ae;
2089 }
2090 else if (PyTuple_Check(o)) {
2091 PyObject* o1 = PyTuple_GetItem(o, 0);
2092 PyObject* o2 = PyTuple_GetItem(o, 1);
2093 PyObject* o3 = PyTuple_GetItem(o, 2);
0adbc166 2094 temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
7bf85405
RD
2095 }
2096 else {
2097 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
2098 return NULL;
2099 }
2100 }
2101 return temp;
2102}
2103
bb0054cd 2104
9d37f964
RD
2105wxPen** wxPen_LIST_helper(PyObject* source) {
2106 if (!PyList_Check(source)) {
2107 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2108 return NULL;
2109 }
2110 int count = PyList_Size(source);
2111 wxPen** temp = new wxPen*[count];
2112 if (!temp) {
2113 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2114 return NULL;
2115 }
2116 for (int x=0; x<count; x++) {
2117 PyObject* o = PyList_GetItem(source, x);
2118 if (PyInstance_Check(o)) {
2119 wxPen* pt;
2120 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxPen_p")) {
2121 delete temp;
2122 PyErr_SetString(PyExc_TypeError,"Expected _wxPen_p.");
2123 return NULL;
2124 }
2125 temp[x] = pt;
2126 }
2127 else {
2128 delete temp;
2129 PyErr_SetString(PyExc_TypeError, "Expected a list of wxPens.");
2130 return NULL;
2131 }
2132 }
2133 return temp;
2134}
2135
2136
1e4a197e 2137bool wxPy2int_seq_helper(PyObject* source, int* i1, int* i2) {
9d37f964
RD
2138 bool isFast = PyList_Check(source) || PyTuple_Check(source);
2139 PyObject *o1, *o2;
2140
2141 if (!PySequence_Check(source) || PySequence_Length(source) != 2)
2142 return FALSE;
2143
2144 if (isFast) {
2145 o1 = PySequence_Fast_GET_ITEM(source, 0);
2146 o2 = PySequence_Fast_GET_ITEM(source, 1);
2147 }
2148 else {
2149 o1 = PySequence_GetItem(source, 0);
2150 o2 = PySequence_GetItem(source, 1);
2151 }
2152
2153 *i1 = PyInt_AsLong(o1);
2154 *i2 = PyInt_AsLong(o2);
2155
2156 if (! isFast) {
2157 Py_DECREF(o1);
2158 Py_DECREF(o2);
2159 }
2160 return TRUE;
2161}
2162
2163
1e4a197e 2164bool wxPy4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4) {
9d37f964
RD
2165 bool isFast = PyList_Check(source) || PyTuple_Check(source);
2166 PyObject *o1, *o2, *o3, *o4;
2167
2168 if (!PySequence_Check(source) || PySequence_Length(source) != 4)
2169 return FALSE;
2170
2171 if (isFast) {
2172 o1 = PySequence_Fast_GET_ITEM(source, 0);
2173 o2 = PySequence_Fast_GET_ITEM(source, 1);
2174 o3 = PySequence_Fast_GET_ITEM(source, 2);
2175 o4 = PySequence_Fast_GET_ITEM(source, 3);
2176 }
2177 else {
2178 o1 = PySequence_GetItem(source, 0);
2179 o2 = PySequence_GetItem(source, 1);
2180 o3 = PySequence_GetItem(source, 2);
2181 o4 = PySequence_GetItem(source, 3);
2182 }
2183
2184 *i1 = PyInt_AsLong(o1);
2185 *i2 = PyInt_AsLong(o2);
2186 *i3 = PyInt_AsLong(o3);
2187 *i4 = PyInt_AsLong(o4);
2188
2189 if (! isFast) {
2190 Py_DECREF(o1);
2191 Py_DECREF(o2);
2192 Py_DECREF(o3);
2193 Py_DECREF(o4);
2194 }
2195 return TRUE;
2196}
2197
bb0054cd
RD
2198
2199//----------------------------------------------------------------------
bb0054cd 2200
2f90df85
RD
2201bool wxSize_helper(PyObject* source, wxSize** obj) {
2202
2203 // If source is an object instance then it may already be the right type
2204 if (PyInstance_Check(source)) {
2205 wxSize* ptr;
2206 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p"))
2207 goto error;
2208 *obj = ptr;
2209 return TRUE;
2210 }
2211 // otherwise a 2-tuple of integers is expected
2212 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
2213 PyObject* o1 = PySequence_GetItem(source, 0);
2214 PyObject* o2 = PySequence_GetItem(source, 1);
db0ff83e
RD
2215 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
2216 Py_DECREF(o1);
2217 Py_DECREF(o2);
2218 goto error;
2219 }
2f90df85 2220 **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2));
db0ff83e
RD
2221 Py_DECREF(o1);
2222 Py_DECREF(o2);
2f90df85
RD
2223 return TRUE;
2224 }
2225
2226 error:
2227 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object.");
2228 return FALSE;
2229}
2230
1e4a197e 2231
2f90df85
RD
2232bool wxPoint_helper(PyObject* source, wxPoint** obj) {
2233
2234 // If source is an object instance then it may already be the right type
2235 if (PyInstance_Check(source)) {
2236 wxPoint* ptr;
2237 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p"))
2238 goto error;
2239 *obj = ptr;
2240 return TRUE;
2241 }
e0672e2f
RD
2242 // otherwise a length-2 sequence of integers is expected
2243 if (PySequence_Check(source) && PySequence_Length(source) == 2) {
2f90df85
RD
2244 PyObject* o1 = PySequence_GetItem(source, 0);
2245 PyObject* o2 = PySequence_GetItem(source, 1);
db0ff83e
RD
2246 // This should really check for integers, not numbers -- but that would break code.
2247 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
2248 Py_DECREF(o1);
2249 Py_DECREF(o2);
2250 goto error;
2251 }
2252 **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2));
2253 Py_DECREF(o1);
2254 Py_DECREF(o2);
2f90df85
RD
2255 return TRUE;
2256 }
2f90df85
RD
2257 error:
2258 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object.");
2259 return FALSE;
2260}
2261
2262
2263
2264bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) {
2265
2266 // If source is an object instance then it may already be the right type
2267 if (PyInstance_Check(source)) {
2268 wxRealPoint* ptr;
2269 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p"))
2270 goto error;
2271 *obj = ptr;
2272 return TRUE;
2273 }
2274 // otherwise a 2-tuple of floats is expected
2275 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
2276 PyObject* o1 = PySequence_GetItem(source, 0);
2277 PyObject* o2 = PySequence_GetItem(source, 1);
db0ff83e
RD
2278 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
2279 Py_DECREF(o1);
2280 Py_DECREF(o2);
2281 goto error;
2282 }
2f90df85 2283 **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
db0ff83e
RD
2284 Py_DECREF(o1);
2285 Py_DECREF(o2);
2f90df85
RD
2286 return TRUE;
2287 }
2288
2289 error:
2290 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object.");
2291 return FALSE;
2292}
2293
2294
2295
2296
2297bool wxRect_helper(PyObject* source, wxRect** obj) {
2298
2299 // If source is an object instance then it may already be the right type
2300 if (PyInstance_Check(source)) {
2301 wxRect* ptr;
2302 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p"))
2303 goto error;
2304 *obj = ptr;
2305 return TRUE;
2306 }
2307 // otherwise a 4-tuple of integers is expected
2308 else if (PySequence_Check(source) && PyObject_Length(source) == 4) {
2309 PyObject* o1 = PySequence_GetItem(source, 0);
2310 PyObject* o2 = PySequence_GetItem(source, 1);
2311 PyObject* o3 = PySequence_GetItem(source, 2);
2312 PyObject* o4 = PySequence_GetItem(source, 3);
db0ff83e
RD
2313 if (!PyNumber_Check(o1) || !PyNumber_Check(o2) ||
2314 !PyNumber_Check(o3) || !PyNumber_Check(o4)) {
2315 Py_DECREF(o1);
2316 Py_DECREF(o2);
2317 Py_DECREF(o3);
2318 Py_DECREF(o4);
2319 goto error;
2320 }
2f90df85 2321 **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2),
db0ff83e
RD
2322 PyInt_AsLong(o3), PyInt_AsLong(o4));
2323 Py_DECREF(o1);
2324 Py_DECREF(o2);
2325 Py_DECREF(o3);
2326 Py_DECREF(o4);
2f90df85
RD
2327 return TRUE;
2328 }
2329
2330 error:
2331 PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object.");
2332 return FALSE;
2333}
2334
2335
2336
f6bcfd97
BP
2337bool wxColour_helper(PyObject* source, wxColour** obj) {
2338
2339 // If source is an object instance then it may already be the right type
2340 if (PyInstance_Check(source)) {
2341 wxColour* ptr;
2342 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p"))
2343 goto error;
2344 *obj = ptr;
2345 return TRUE;
2346 }
1e4a197e
RD
2347 // otherwise check for a string
2348 else if (PyString_Check(source) || PyUnicode_Check(source)) {
2349 wxString spec = Py2wxString(source);
a541c325
RD
2350 if (spec.GetChar(0) == '#' && spec.Length() == 7) { // It's #RRGGBB
2351 long red, green, blue;
2352 red = green = blue = 0;
a541c325
RD
2353 spec.Mid(1,2).ToLong(&red, 16);
2354 spec.Mid(3,2).ToLong(&green, 16);
2355 spec.Mid(5,2).ToLong(&blue, 16);
2356
f6bcfd97
BP
2357 **obj = wxColour(red, green, blue);
2358 return TRUE;
2359 }
2360 else { // it's a colour name
2361 **obj = wxColour(spec);
2362 return TRUE;
2363 }
2364 }
1e4a197e
RD
2365 // last chance: 3-tuple of integers is expected
2366 else if (PySequence_Check(source) && PyObject_Length(source) == 3) {
2367 PyObject* o1 = PySequence_GetItem(source, 0);
2368 PyObject* o2 = PySequence_GetItem(source, 1);
2369 PyObject* o3 = PySequence_GetItem(source, 2);
2370 if (!PyNumber_Check(o1) || !PyNumber_Check(o2) || !PyNumber_Check(o3)) {
2371 Py_DECREF(o1);
2372 Py_DECREF(o2);
2373 Py_DECREF(o3);
2374 goto error;
2375 }
2376 **obj = wxColour(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
2377 Py_DECREF(o1);
2378 Py_DECREF(o2);
2379 Py_DECREF(o3);
2380 return TRUE;
2381 }
f6bcfd97
BP
2382
2383 error:
a541c325 2384 PyErr_SetString(PyExc_TypeError,
1e4a197e
RD
2385 "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
2386 return FALSE;
2387}
2388
2389
2390
2391bool wxPoint2DDouble_helper(PyObject* source, wxPoint2DDouble** obj) {
2392 // If source is an object instance then it may already be the right type
2393 if (PyInstance_Check(source)) {
2394 wxPoint2DDouble* ptr;
2395 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint2DDouble_p"))
2396 goto error;
2397 *obj = ptr;
2398 return TRUE;
2399 }
2400 // otherwise a length-2 sequence of floats is expected
2401 if (PySequence_Check(source) && PySequence_Length(source) == 2) {
2402 PyObject* o1 = PySequence_GetItem(source, 0);
2403 PyObject* o2 = PySequence_GetItem(source, 1);
2404 // This should really check for integers, not numbers -- but that would break code.
2405 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
2406 Py_DECREF(o1);
2407 Py_DECREF(o2);
2408 goto error;
2409 }
2410 **obj = wxPoint2DDouble(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
2411 Py_DECREF(o1);
2412 Py_DECREF(o2);
2413 return TRUE;
2414 }
2415 error:
2416 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxPoint2DDouble object.");
f6bcfd97
BP
2417 return FALSE;
2418}
2419
2420
1e4a197e 2421
bb0054cd 2422//----------------------------------------------------------------------
b37c7e1d
RD
2423
2424PyObject* wxArrayString2PyList_helper(const wxArrayString& arr) {
2425
2426 PyObject* list = PyList_New(0);
2427 for (size_t i=0; i < arr.GetCount(); i++) {
c8bc7bb8 2428#if wxUSE_UNICODE
1e4a197e 2429 PyObject* str = PyUnicode_FromWideChar(arr[i].c_str(), arr[i].Len());
c8bc7bb8
RD
2430#else
2431 PyObject* str = PyString_FromStringAndSize(arr[i].c_str(), arr[i].Len());
2432#endif
b37c7e1d 2433 PyList_Append(list, str);
8af26133 2434 Py_DECREF(str);
b37c7e1d
RD
2435 }
2436 return list;
2437}
2438
2439
293a0a86
RD
2440PyObject* wxArrayInt2PyList_helper(const wxArrayInt& arr) {
2441
2442 PyObject* list = PyList_New(0);
2443 for (size_t i=0; i < arr.GetCount(); i++) {
2444 PyObject* number = PyInt_FromLong(arr[i]);
2445 PyList_Append(list, number);
2446 Py_DECREF(number);
2447 }
2448 return list;
2449}
2450
2451
bb0054cd 2452//----------------------------------------------------------------------
7bf85405 2453//----------------------------------------------------------------------
7bf85405 2454
7bf85405 2455
7bf85405 2456
de20db99 2457