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