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