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