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