]> git.saurik.com Git - wxWidgets.git/blob - wxPython/src/helpers.cpp
embedded sample compiles again
[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
727 REINITOBJ(wxNORMAL_FONT, wxFont);
728 REINITOBJ(wxSMALL_FONT, wxFont);
729 REINITOBJ(wxITALIC_FONT, wxFont);
730 REINITOBJ(wxSWISS_FONT, wxFont);
731
732 REINITOBJ(wxRED_PEN, wxPen);
733 REINITOBJ(wxCYAN_PEN, wxPen);
734 REINITOBJ(wxGREEN_PEN, wxPen);
735 REINITOBJ(wxBLACK_PEN, wxPen);
736 REINITOBJ(wxWHITE_PEN, wxPen);
737 REINITOBJ(wxTRANSPARENT_PEN, wxPen);
738 REINITOBJ(wxBLACK_DASHED_PEN, wxPen);
739 REINITOBJ(wxGREY_PEN, wxPen);
740 REINITOBJ(wxMEDIUM_GREY_PEN, wxPen);
741 REINITOBJ(wxLIGHT_GREY_PEN, wxPen);
742
743 REINITOBJ(wxBLUE_BRUSH, wxBrush);
744 REINITOBJ(wxGREEN_BRUSH, wxBrush);
745 REINITOBJ(wxWHITE_BRUSH, wxBrush);
746 REINITOBJ(wxBLACK_BRUSH, wxBrush);
747 REINITOBJ(wxTRANSPARENT_BRUSH, wxBrush);
748 REINITOBJ(wxCYAN_BRUSH, wxBrush);
749 REINITOBJ(wxRED_BRUSH, wxBrush);
750 REINITOBJ(wxGREY_BRUSH, wxBrush);
751 REINITOBJ(wxMEDIUM_GREY_BRUSH, wxBrush);
752 REINITOBJ(wxLIGHT_GREY_BRUSH, wxBrush);
753
754 REINITOBJ(wxBLACK, wxColour);
755 REINITOBJ(wxWHITE, wxColour);
756 REINITOBJ(wxRED, wxColour);
757 REINITOBJ(wxBLUE, wxColour);
758 REINITOBJ(wxGREEN, wxColour);
759 REINITOBJ(wxCYAN, wxColour);
760 REINITOBJ(wxLIGHT_GREY, wxColour);
761
762 REINITOBJ(wxSTANDARD_CURSOR, wxCursor);
763 REINITOBJ(wxHOURGLASS_CURSOR, wxCursor);
764 REINITOBJ(wxCROSS_CURSOR, wxCursor);
765
766 REINITOBJ2(wxNullBitmap, wxBitmap);
767 REINITOBJ2(wxNullIcon, wxIcon);
768 REINITOBJ2(wxNullCursor, wxCursor);
769 REINITOBJ2(wxNullPen, wxPen);
770 REINITOBJ2(wxNullBrush, wxBrush);
771 REINITOBJ2(wxNullPalette, wxPalette);
772 REINITOBJ2(wxNullFont, wxFont);
773 REINITOBJ2(wxNullColour, wxColour);
774
775 REINITOBJ(wxTheFontList, wxFontList);
776 REINITOBJ(wxThePenList, wxPenList);
777 REINITOBJ(wxTheBrushList, wxBrushList);
778 REINITOBJ(wxTheColourDatabase, wxColourDatabase);
779
780
781 REINITOBJ(wxTheClipboard, wxClipboard);
782 REINITOBJ2(wxDefaultValidator, wxValidator);
783 REINITOBJ2(wxNullImage, wxImage);
784 REINITOBJ2(wxNullAcceleratorTable, wxAcceleratorTable);
785
786 #undef REINITOBJ
787 #undef REINITOBJ2
788 }
789
790 //---------------------------------------------------------------------------
791
792 void wxPyClientData_dtor(wxPyClientData* self) {
793 if (! wxPyDoingCleanup) { // Don't do it during cleanup as Python
794 // may have already garbage collected the object...
795 bool blocked = wxPyBeginBlockThreads();
796 Py_DECREF(self->m_obj);
797 self->m_obj = NULL;
798 wxPyEndBlockThreads(blocked);
799 }
800 }
801
802 void wxPyUserData_dtor(wxPyUserData* self) {
803 if (! wxPyDoingCleanup) {
804 bool blocked = wxPyBeginBlockThreads();
805 Py_DECREF(self->m_obj);
806 self->m_obj = NULL;
807 wxPyEndBlockThreads(blocked);
808 }
809 }
810
811
812 // This is called when an OOR controled object is being destroyed. Although
813 // the C++ object is going away there is no way to force the Python object
814 // (and all references to it) to die too. This causes problems (crashes) in
815 // wxPython when a python shadow object attempts to call a C++ method using
816 // the now bogus pointer... So to try and prevent this we'll do a little black
817 // magic and change the class of the python instance to a class that will
818 // raise an exception for any attempt to call methods with it. See
819 // _wxPyDeadObject in _core_ex.py for the implementation of this class.
820 void wxPyOORClientData_dtor(wxPyOORClientData* self) {
821
822 static PyObject* deadObjectClass = NULL;
823
824 bool blocked = wxPyBeginBlockThreads();
825 if (deadObjectClass == NULL) {
826 deadObjectClass = PyDict_GetItemString(wxPython_dict, "_wxPyDeadObject");
827 // TODO: Can not wxASSERT here because inside a wxPyBeginBlock Threads,
828 // will lead to a deadlock when it tries to aquire the GIL again.
829 //wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!"));
830 Py_INCREF(deadObjectClass);
831 }
832
833
834 // Only if there is more than one reference to the object
835 if ( !wxPyDoingCleanup && self->m_obj->ob_refcnt > 1 ) {
836 // bool isInstance = wxPyInstance_Check(self->m_obj);
837 // TODO same here
838 //wxASSERT_MSG(isInstance, wxT("m_obj not an instance!?!?!"));
839
840 // Call __del__, if there is one.
841 PyObject* func = PyObject_GetAttrString(self->m_obj, "__del__");
842 if (func) {
843 PyObject* rv = PyObject_CallMethod(self->m_obj, "__del__", NULL);
844 Py_XDECREF(rv);
845 Py_DECREF(func);
846 }
847 if (PyErr_Occurred())
848 PyErr_Clear(); // just ignore it for now
849
850
851 PyObject* dict = PyObject_GetAttrString(self->m_obj, "__dict__");
852 if (dict) {
853 // Clear the instance's dictionary
854 PyDict_Clear(dict);
855
856 // put the name of the old class into the instance, and then reset the
857 // class to be the dead class.
858 PyObject* klass = PyObject_GetAttrString(self->m_obj, "__class__");
859 PyObject* name = PyObject_GetAttrString(klass, "__name__");
860 PyDict_SetItemString(dict, "_name", name);
861 PyObject_SetAttrString(self->m_obj, "__class__", deadObjectClass);
862 //Py_INCREF(deadObjectClass);
863 Py_DECREF(klass);
864 Py_DECREF(name);
865 }
866 }
867
868 // m_obj is DECREF'd in the base class dtor...
869 wxPyEndBlockThreads(blocked);
870 }
871
872
873 //---------------------------------------------------------------------------
874 // Stuff used by OOR to find the right wxPython class type to return and to
875 // build it.
876
877
878 // The pointer type map is used when the "pointer" type name generated by SWIG
879 // is not the same as the shadow class name, for example wxPyTreeCtrl
880 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
881 // so we'll just make it a Python dictionary in the wx module's namespace.
882 // (See __wxSetDictionary)
883 void wxPyPtrTypeMap_Add(const char* commonName, const char* ptrName) {
884 if (! wxPyPtrTypeMap)
885 wxPyPtrTypeMap = PyDict_New();
886 PyDict_SetItemString(wxPyPtrTypeMap,
887 (char*)commonName,
888 PyString_FromString((char*)ptrName));
889 }
890
891
892
893
894 PyObject* wxPyMake_wxObject(wxObject* source, bool checkEvtHandler) {
895 PyObject* target = NULL;
896 bool isEvtHandler = False;
897
898 if (source) {
899 // If it's derived from wxEvtHandler then there may
900 // already be a pointer to a Python object that we can use
901 // in the OOR data.
902 if (checkEvtHandler && wxIsKindOf(source, wxEvtHandler)) {
903 isEvtHandler = True;
904 wxEvtHandler* eh = (wxEvtHandler*)source;
905 wxPyOORClientData* data = (wxPyOORClientData*)eh->GetClientObject();
906 if (data) {
907 target = data->m_obj;
908 if (target)
909 Py_INCREF(target);
910 }
911 }
912
913 if (! target) {
914 // Otherwise make it the old fashioned way by making a new shadow
915 // object and putting this pointer in it. Look up the class
916 // heirarchy until we find a class name that is located in the
917 // python module.
918 const wxClassInfo* info = source->GetClassInfo();
919 wxString name = info->GetClassName();
920 bool exists = wxPyCheckSwigType(name);
921 while (info && !exists) {
922 info = info->GetBaseClass1();
923 name = info->GetClassName();
924 exists = wxPyCheckSwigType(name);
925 }
926 if (info) {
927 target = wxPyConstructObject((void*)source, name, False);
928 if (target && isEvtHandler)
929 ((wxEvtHandler*)source)->SetClientObject(new wxPyOORClientData(target));
930 } else {
931 wxString msg(wxT("wxPython class not found for "));
932 msg += source->GetClassInfo()->GetClassName();
933 PyErr_SetString(PyExc_NameError, msg.mbc_str());
934 target = NULL;
935 }
936 }
937 } else { // source was NULL so return None.
938 Py_INCREF(Py_None); target = Py_None;
939 }
940 return target;
941 }
942
943
944 PyObject* wxPyMake_wxSizer(wxSizer* source) {
945 PyObject* target = NULL;
946
947 if (source && wxIsKindOf(source, wxSizer)) {
948 // If it's derived from wxSizer then there may already be a pointer to
949 // a Python object that we can use in the OOR data.
950 wxSizer* sz = (wxSizer*)source;
951 wxPyOORClientData* data = (wxPyOORClientData*)sz->GetClientObject();
952 if (data) {
953 target = data->m_obj;
954 if (target)
955 Py_INCREF(target);
956 }
957 }
958 if (! target) {
959 target = wxPyMake_wxObject(source, False);
960 if (target != Py_None)
961 ((wxSizer*)source)->SetClientObject(new wxPyOORClientData(target));
962 }
963 return target;
964 }
965
966
967 //---------------------------------------------------------------------------
968
969
970 #ifdef WXP_WITH_THREAD
971 inline
972 unsigned long wxPyGetCurrentThreadId() {
973 return wxThread::GetCurrentId();
974 }
975
976 static wxPyThreadState gs_shutdownTState;
977
978 static
979 wxPyThreadState* wxPyGetThreadState() {
980 if (wxPyTMutex == NULL) // Python is shutting down...
981 return &gs_shutdownTState;
982
983 unsigned long ctid = wxPyGetCurrentThreadId();
984 wxPyThreadState* tstate = NULL;
985
986 wxPyTMutex->Lock();
987 for(size_t i=0; i < wxPyTStates->GetCount(); i++) {
988 wxPyThreadState& info = wxPyTStates->Item(i);
989 if (info.tid == ctid) {
990 tstate = &info;
991 break;
992 }
993 }
994 wxPyTMutex->Unlock();
995 wxASSERT_MSG(tstate, wxT("PyThreadState should not be NULL!"));
996 return tstate;
997 }
998
999
1000 static
1001 void wxPySaveThreadState(PyThreadState* tstate) {
1002 if (wxPyTMutex == NULL) { // Python is shutting down, assume a single thread...
1003 gs_shutdownTState.tstate = tstate;
1004 return;
1005 }
1006 unsigned long ctid = wxPyGetCurrentThreadId();
1007 wxPyTMutex->Lock();
1008 for(size_t i=0; i < wxPyTStates->GetCount(); i++) {
1009 wxPyThreadState& info = wxPyTStates->Item(i);
1010 if (info.tid == ctid) {
1011 #if 0
1012 if (info.tstate != tstate)
1013 wxLogMessage("*** tstate mismatch!???");
1014 #endif
1015 // info.tstate = tstate; *** DO NOT update existing ones???
1016 // Normally it will never change, but apparently COM callbacks
1017 // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient
1018 // tstate which will then be garbage the next time we try to use
1019 // it...
1020 wxPyTMutex->Unlock();
1021 return;
1022 }
1023 }
1024 // not found, so add it...
1025 wxPyTStates->Add(new wxPyThreadState(ctid, tstate));
1026 wxPyTMutex->Unlock();
1027 }
1028
1029 #endif
1030
1031
1032
1033 // Calls from Python to wxWindows code are wrapped in calls to these
1034 // functions:
1035
1036 PyThreadState* wxPyBeginAllowThreads() {
1037 #ifdef WXP_WITH_THREAD
1038 PyThreadState* saved = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
1039 wxPySaveThreadState(saved);
1040 return saved;
1041 #else
1042 return NULL;
1043 #endif
1044 }
1045
1046 void wxPyEndAllowThreads(PyThreadState* saved) {
1047 #ifdef WXP_WITH_THREAD
1048 PyEval_RestoreThread(saved); // Py_END_ALLOW_THREADS;
1049 #endif
1050 }
1051
1052
1053
1054 // Calls from wxWindows back to Python code, or even any PyObject
1055 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
1056
1057 bool wxPyBeginBlockThreads() {
1058 #ifdef WXP_WITH_THREAD
1059 // This works in for 2.3, maybe a good alternative to find the needed tstate?
1060 // PyThreadState *check = PyGILState_GetThisThreadState();
1061
1062 PyThreadState *current = _PyThreadState_Current;
1063
1064 // Only block if there wasn't already a tstate, or if the current one is
1065 // not the one we are wanting to change to. This should prevent deadlock
1066 // if there are nested calls to wxPyBeginBlockThreads
1067 bool blocked = false;
1068 wxPyThreadState* tstate = wxPyGetThreadState();
1069 if (current != tstate->tstate) {
1070 PyEval_RestoreThread(tstate->tstate);
1071 blocked = true;
1072 }
1073 return blocked;
1074 #endif
1075 }
1076
1077
1078 void wxPyEndBlockThreads(bool blocked) {
1079 #ifdef WXP_WITH_THREAD
1080 // Only unblock if we blocked in the last call to wxPyBeginBlockThreads.
1081 // The value of blocked passed in needs to be the same as that returned
1082 // from wxPyBeginBlockThreads at the same nesting level.
1083 if ( blocked ) {
1084 PyEval_SaveThread();
1085 }
1086 #endif
1087 }
1088
1089
1090 //---------------------------------------------------------------------------
1091 // wxPyInputStream and wxPyCBInputStream methods
1092
1093
1094 void wxPyInputStream::close() {
1095 /* do nothing for now */
1096 }
1097
1098 void wxPyInputStream::flush() {
1099 /* do nothing for now */
1100 }
1101
1102 bool wxPyInputStream::eof() {
1103 if (m_wxis)
1104 return m_wxis->Eof();
1105 else
1106 return True;
1107 }
1108
1109 wxPyInputStream::~wxPyInputStream() {
1110 /* do nothing */
1111 }
1112
1113
1114
1115
1116 PyObject* wxPyInputStream::read(int size) {
1117 PyObject* obj = NULL;
1118 wxMemoryBuffer buf;
1119 const int BUFSIZE = 1024;
1120
1121 // check if we have a real wxInputStream to work with
1122 if (!m_wxis) {
1123 bool blocked = wxPyBeginBlockThreads();
1124 PyErr_SetString(PyExc_IOError, "no valid C-wxInputStream");
1125 wxPyEndBlockThreads(blocked);
1126 return NULL;
1127 }
1128
1129 if (size < 0) {
1130 // read while bytes are available on the stream
1131 while ( m_wxis->CanRead() ) {
1132 m_wxis->Read(buf.GetAppendBuf(BUFSIZE), BUFSIZE);
1133 buf.UngetAppendBuf(m_wxis->LastRead());
1134 }
1135
1136 } else { // Read only size number of characters
1137 m_wxis->Read(buf.GetWriteBuf(size), size);
1138 buf.UngetWriteBuf(m_wxis->LastRead());
1139 }
1140
1141 // error check
1142 bool blocked = wxPyBeginBlockThreads();
1143 wxStreamError err = m_wxis->GetLastError();
1144 if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) {
1145 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
1146 }
1147 else {
1148 // We use only strings for the streams, not unicode
1149 obj = PyString_FromStringAndSize(buf, buf.GetDataLen());
1150 }
1151 wxPyEndBlockThreads(blocked);
1152 return obj;
1153 }
1154
1155
1156 PyObject* wxPyInputStream::readline(int size) {
1157 PyObject* obj = NULL;
1158 wxMemoryBuffer buf;
1159 int i;
1160 char ch;
1161
1162 // check if we have a real wxInputStream to work with
1163 if (!m_wxis) {
1164 bool blocked = wxPyBeginBlockThreads();
1165 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream");
1166 wxPyEndBlockThreads(blocked);
1167 return NULL;
1168 }
1169
1170 // read until \n or byte limit reached
1171 for (i=ch=0; (ch != '\n') && (m_wxis->CanRead()) && ((size < 0) || (i < size)); i++) {
1172 ch = m_wxis->GetC();
1173 buf.AppendByte(ch);
1174 }
1175
1176 // errorcheck
1177 bool blocked = wxPyBeginBlockThreads();
1178 wxStreamError err = m_wxis->GetLastError();
1179 if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) {
1180 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
1181 }
1182 else {
1183 // We use only strings for the streams, not unicode
1184 obj = PyString_FromStringAndSize((char*)buf.GetData(), buf.GetDataLen());
1185 }
1186 wxPyEndBlockThreads(blocked);
1187 return obj;
1188 }
1189
1190
1191 PyObject* wxPyInputStream::readlines(int sizehint) {
1192 PyObject* pylist;
1193
1194 // check if we have a real wxInputStream to work with
1195 if (!m_wxis) {
1196 bool blocked = wxPyBeginBlockThreads();
1197 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream");
1198 wxPyEndBlockThreads(blocked);
1199 return NULL;
1200 }
1201
1202 // init list
1203 bool blocked = wxPyBeginBlockThreads();
1204 pylist = PyList_New(0);
1205 wxPyEndBlockThreads(blocked);
1206
1207 if (!pylist) {
1208 bool blocked = wxPyBeginBlockThreads();
1209 PyErr_NoMemory();
1210 wxPyEndBlockThreads(blocked);
1211 return NULL;
1212 }
1213
1214 // read sizehint bytes or until EOF
1215 int i;
1216 for (i=0; (m_wxis->CanRead()) && ((sizehint < 0) || (i < sizehint));) {
1217 PyObject* s = this->readline();
1218 if (s == NULL) {
1219 bool blocked = wxPyBeginBlockThreads();
1220 Py_DECREF(pylist);
1221 wxPyEndBlockThreads(blocked);
1222 return NULL;
1223 }
1224 bool blocked = wxPyBeginBlockThreads();
1225 PyList_Append(pylist, s);
1226 i += PyString_Size(s);
1227 wxPyEndBlockThreads(blocked);
1228 }
1229
1230 // error check
1231 wxStreamError err = m_wxis->GetLastError();
1232 if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) {
1233 bool blocked = wxPyBeginBlockThreads();
1234 Py_DECREF(pylist);
1235 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
1236 wxPyEndBlockThreads(blocked);
1237 return NULL;
1238 }
1239
1240 return pylist;
1241 }
1242
1243
1244 void wxPyInputStream::seek(int offset, int whence) {
1245 if (m_wxis)
1246 m_wxis->SeekI(offset, wxSeekMode(whence));
1247 }
1248
1249 int wxPyInputStream::tell(){
1250 if (m_wxis)
1251 return m_wxis->TellI();
1252 else return 0;
1253 }
1254
1255
1256
1257
1258 wxPyCBInputStream::wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block)
1259 : wxInputStream(), m_read(r), m_seek(s), m_tell(t), m_block(block)
1260 {}
1261
1262
1263 wxPyCBInputStream::~wxPyCBInputStream() {
1264 bool blocked;
1265 if (m_block) blocked = wxPyBeginBlockThreads();
1266 Py_XDECREF(m_read);
1267 Py_XDECREF(m_seek);
1268 Py_XDECREF(m_tell);
1269 if (m_block) wxPyEndBlockThreads(blocked);
1270 }
1271
1272
1273 wxPyCBInputStream* wxPyCBInputStream::create(PyObject *py, bool block) {
1274 bool blocked;
1275 if (block) blocked = wxPyBeginBlockThreads();
1276
1277 PyObject* read = getMethod(py, "read");
1278 PyObject* seek = getMethod(py, "seek");
1279 PyObject* tell = getMethod(py, "tell");
1280
1281 if (!read) {
1282 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
1283 Py_XDECREF(read);
1284 Py_XDECREF(seek);
1285 Py_XDECREF(tell);
1286 if (block) wxPyEndBlockThreads(blocked);
1287 return NULL;
1288 }
1289
1290 if (block) wxPyEndBlockThreads(blocked);
1291 return new wxPyCBInputStream(read, seek, tell, block);
1292 }
1293
1294
1295 wxPyCBInputStream* wxPyCBInputStream_create(PyObject *py, bool block) {
1296 return wxPyCBInputStream::create(py, block);
1297 }
1298
1299 PyObject* wxPyCBInputStream::getMethod(PyObject* py, char* name) {
1300 if (!PyObject_HasAttrString(py, name))
1301 return NULL;
1302 PyObject* o = PyObject_GetAttrString(py, name);
1303 if (!PyMethod_Check(o) && !PyCFunction_Check(o)) {
1304 Py_DECREF(o);
1305 return NULL;
1306 }
1307 return o;
1308 }
1309
1310
1311 size_t wxPyCBInputStream::GetSize() const {
1312 wxPyCBInputStream* self = (wxPyCBInputStream*)this; // cast off const
1313 if (m_seek && m_tell) {
1314 off_t temp = self->OnSysTell();
1315 off_t ret = self->OnSysSeek(0, wxFromEnd);
1316 self->OnSysSeek(temp, wxFromStart);
1317 return ret;
1318 }
1319 else
1320 return 0;
1321 }
1322
1323
1324 size_t wxPyCBInputStream::OnSysRead(void *buffer, size_t bufsize) {
1325 if (bufsize == 0)
1326 return 0;
1327
1328 bool blocked = wxPyBeginBlockThreads();
1329 PyObject* arglist = Py_BuildValue("(i)", bufsize);
1330 PyObject* result = PyEval_CallObject(m_read, arglist);
1331 Py_DECREF(arglist);
1332
1333 size_t o = 0;
1334 if ((result != NULL) && PyString_Check(result)) {
1335 o = PyString_Size(result);
1336 if (o == 0)
1337 m_lasterror = wxSTREAM_EOF;
1338 if (o > bufsize)
1339 o = bufsize;
1340 memcpy((char*)buffer, PyString_AsString(result), o); // strings only, not unicode...
1341 Py_DECREF(result);
1342
1343 }
1344 else
1345 m_lasterror = wxSTREAM_READ_ERROR;
1346 wxPyEndBlockThreads(blocked);
1347 return o;
1348 }
1349
1350 size_t wxPyCBInputStream::OnSysWrite(const void *buffer, size_t bufsize) {
1351 m_lasterror = wxSTREAM_WRITE_ERROR;
1352 return 0;
1353 }
1354
1355 off_t wxPyCBInputStream::OnSysSeek(off_t off, wxSeekMode mode) {
1356 bool blocked = wxPyBeginBlockThreads();
1357 #ifdef _LARGE_FILES
1358 // off_t is a 64-bit value...
1359 PyObject* arglist = Py_BuildValue("(Li)", off, mode);
1360 #else
1361 PyObject* arglist = Py_BuildValue("(ii)", off, mode);
1362 #endif
1363 PyObject* result = PyEval_CallObject(m_seek, arglist);
1364 Py_DECREF(arglist);
1365 Py_XDECREF(result);
1366 wxPyEndBlockThreads(blocked);
1367 return OnSysTell();
1368 }
1369
1370
1371 off_t wxPyCBInputStream::OnSysTell() const {
1372 bool blocked = wxPyBeginBlockThreads();
1373 PyObject* arglist = Py_BuildValue("()");
1374 PyObject* result = PyEval_CallObject(m_tell, arglist);
1375 Py_DECREF(arglist);
1376 off_t o = 0;
1377 if (result != NULL) {
1378 #ifdef _LARGE_FILES
1379 if (PyLong_Check(result))
1380 o = PyLong_AsLongLong(result);
1381 else
1382 #endif
1383 o = PyInt_AsLong(result);
1384 Py_DECREF(result);
1385 };
1386 wxPyEndBlockThreads(blocked);
1387 return o;
1388 }
1389
1390 //----------------------------------------------------------------------
1391
1392 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject);
1393
1394 wxPyCallback::wxPyCallback(PyObject* func) {
1395 m_func = func;
1396 Py_INCREF(m_func);
1397 }
1398
1399 wxPyCallback::wxPyCallback(const wxPyCallback& other) {
1400 m_func = other.m_func;
1401 Py_INCREF(m_func);
1402 }
1403
1404 wxPyCallback::~wxPyCallback() {
1405 bool blocked = wxPyBeginBlockThreads();
1406 Py_DECREF(m_func);
1407 wxPyEndBlockThreads(blocked);
1408 }
1409
1410
1411 #define wxPy_PRECALLINIT "_preCallInit"
1412 #define wxPy_POSTCALLCLEANUP "_postCallCleanup"
1413
1414 // This function is used for all events destined for Python event handlers.
1415 void wxPyCallback::EventThunker(wxEvent& event) {
1416 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
1417 PyObject* func = cb->m_func;
1418 PyObject* result;
1419 PyObject* arg;
1420 PyObject* tuple;
1421 bool checkSkip = False;
1422
1423 bool blocked = wxPyBeginBlockThreads();
1424 wxString className = event.GetClassInfo()->GetClassName();
1425
1426 // If the event is one of these types then pass the original
1427 // event object instead of the one passed to us.
1428 if ( className == wxT("wxPyEvent") ) {
1429 arg = ((wxPyEvent*)&event)->GetSelf();
1430 checkSkip = ((wxPyEvent*)&event)->GetCloned();
1431 }
1432 else if ( className == wxT("wxPyCommandEvent") ) {
1433 arg = ((wxPyCommandEvent*)&event)->GetSelf();
1434 checkSkip = ((wxPyCommandEvent*)&event)->GetCloned();
1435 }
1436 else {
1437 arg = wxPyConstructObject((void*)&event, className);
1438 }
1439
1440 if (!arg) {
1441 PyErr_Print();
1442 } else {
1443 // "intern" the pre/post method names to speed up the HasAttr
1444 static PyObject* s_preName = NULL;
1445 static PyObject* s_postName = NULL;
1446 if (s_preName == NULL) {
1447 s_preName = PyString_FromString(wxPy_PRECALLINIT);
1448 s_postName = PyString_FromString(wxPy_POSTCALLCLEANUP);
1449 }
1450
1451 // Check if the event object needs some preinitialization
1452 if (PyObject_HasAttr(arg, s_preName)) {
1453 result = PyObject_CallMethodObjArgs(arg, s_preName, arg, NULL);
1454 if ( result ) {
1455 Py_DECREF(result); // result is ignored, but we still need to decref it
1456 PyErr_Clear(); // Just in case...
1457 } else {
1458 PyErr_Print();
1459 }
1460 }
1461
1462 // Call the event handler, passing the event object
1463 tuple = PyTuple_New(1);
1464 PyTuple_SET_ITEM(tuple, 0, arg); // steals ref to arg
1465 result = PyEval_CallObject(func, tuple);
1466 if ( result ) {
1467 Py_DECREF(result); // result is ignored, but we still need to decref it
1468 PyErr_Clear(); // Just in case...
1469 } else {
1470 PyErr_Print();
1471 }
1472
1473 // Check if the event object needs some post cleanup
1474 if (PyObject_HasAttr(arg, s_postName)) {
1475 result = PyObject_CallMethodObjArgs(arg, s_postName, arg, NULL);
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
1484 if ( checkSkip ) {
1485 // if the event object was one of our special types and
1486 // it had been cloned, then we need to extract the Skipped
1487 // value from the original and set it in the clone.
1488 result = PyObject_CallMethod(arg, "GetSkipped", "");
1489 if ( result ) {
1490 event.Skip(PyInt_AsLong(result));
1491 Py_DECREF(result);
1492 } else {
1493 PyErr_Print();
1494 }
1495 }
1496 Py_DECREF(tuple);
1497 }
1498 wxPyEndBlockThreads(blocked);
1499 }
1500
1501
1502 //----------------------------------------------------------------------
1503
1504 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) {
1505 m_lastFound = NULL;
1506 m_self = other.m_self;
1507 m_class = other.m_class;
1508 if (m_self) {
1509 Py_INCREF(m_self);
1510 Py_INCREF(m_class);
1511 }
1512 }
1513
1514
1515 void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) {
1516 m_self = self;
1517 m_class = klass;
1518 m_incRef = incref;
1519 if (incref) {
1520 Py_INCREF(m_self);
1521 Py_INCREF(m_class);
1522 }
1523 }
1524
1525
1526 #if PYTHON_API_VERSION >= 1011
1527
1528 // Prior to Python 2.2 PyMethod_GetClass returned the class object
1529 // in which the method was defined. Starting with 2.2 it returns
1530 // "class that asked for the method" which seems totally bogus to me
1531 // but apprently it fixes some obscure problem waiting to happen in
1532 // Python. Since the API was not documented Guido and the gang felt
1533 // safe in changing it. Needless to say that totally screwed up the
1534 // logic below in wxPyCallbackHelper::findCallback, hence this icky
1535 // code to find the class where the method is actually defined...
1536
1537 static
1538 PyObject* PyFindClassWithAttr(PyObject *klass, PyObject *name)
1539 {
1540 int i, n;
1541
1542 if (PyType_Check(klass)) { // new style classes
1543 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1544 PyTypeObject* type = (PyTypeObject*)klass;
1545 PyObject *mro, *res, *base, *dict;
1546 /* Look in tp_dict of types in MRO */
1547 mro = type->tp_mro;
1548 assert(PyTuple_Check(mro));
1549 n = PyTuple_GET_SIZE(mro);
1550 for (i = 0; i < n; i++) {
1551 base = PyTuple_GET_ITEM(mro, i);
1552 if (PyClass_Check(base))
1553 dict = ((PyClassObject *)base)->cl_dict;
1554 else {
1555 assert(PyType_Check(base));
1556 dict = ((PyTypeObject *)base)->tp_dict;
1557 }
1558 assert(dict && PyDict_Check(dict));
1559 res = PyDict_GetItem(dict, name);
1560 if (res != NULL)
1561 return base;
1562 }
1563 return NULL;
1564 }
1565
1566 else if (PyClass_Check(klass)) { // old style classes
1567 // This code is borrowed/adapted from class_lookup in classobject.c
1568 PyClassObject* cp = (PyClassObject*)klass;
1569 PyObject *value = PyDict_GetItem(cp->cl_dict, name);
1570 if (value != NULL) {
1571 return (PyObject*)cp;
1572 }
1573 n = PyTuple_Size(cp->cl_bases);
1574 for (i = 0; i < n; i++) {
1575 PyObject* base = PyTuple_GetItem(cp->cl_bases, i);
1576 PyObject *v = PyFindClassWithAttr(base, name);
1577 if (v != NULL)
1578 return v;
1579 }
1580 return NULL;
1581 }
1582 return NULL;
1583 }
1584 #endif
1585
1586
1587 static
1588 PyObject* PyMethod_GetDefiningClass(PyObject* method, const char* name)
1589 {
1590 PyObject* mgc = PyMethod_GET_CLASS(method);
1591
1592 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1593 return mgc;
1594 #else // 2.2 and after, the hard way...
1595
1596 PyObject* nameo = PyString_FromString(name);
1597 PyObject* klass = PyFindClassWithAttr(mgc, nameo);
1598 Py_DECREF(nameo);
1599 return klass;
1600 #endif
1601 }
1602
1603
1604
1605 bool wxPyCallbackHelper::findCallback(const char* name) const {
1606 wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const
1607 self->m_lastFound = NULL;
1608
1609 // If the object (m_self) has an attibute of the given name...
1610 if (m_self && PyObject_HasAttrString(m_self, (char*)name)) {
1611 PyObject *method, *klass;
1612 method = PyObject_GetAttrString(m_self, (char*)name);
1613
1614 // ...and if that attribute is a method, and if that method's class is
1615 // not from a base class...
1616 if (PyMethod_Check(method) &&
1617 (klass = PyMethod_GetDefiningClass(method, (char*)name)) != NULL &&
1618 ((klass == m_class) || PyObject_IsSubclass(klass, m_class))) {
1619
1620 // ...then we'll save a pointer to the method so callCallback can call it.
1621 self->m_lastFound = method;
1622 }
1623 else {
1624 Py_DECREF(method);
1625 }
1626 }
1627 return m_lastFound != NULL;
1628 }
1629
1630
1631 int wxPyCallbackHelper::callCallback(PyObject* argTuple) const {
1632 PyObject* result;
1633 int retval = False;
1634
1635 result = callCallbackObj(argTuple);
1636 if (result) { // Assumes an integer return type...
1637 retval = PyInt_AsLong(result);
1638 Py_DECREF(result);
1639 PyErr_Clear(); // forget about it if it's not...
1640 }
1641 return retval;
1642 }
1643
1644 // Invoke the Python callable object, returning the raw PyObject return
1645 // value. Caller should DECREF the return value and also manage the GIL.
1646 PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const {
1647 PyObject* result;
1648
1649 // Save a copy of the pointer in case the callback generates another
1650 // callback. In that case m_lastFound will have a different value when
1651 // it gets back here...
1652 PyObject* method = m_lastFound;
1653
1654 result = PyEval_CallObject(method, argTuple);
1655 Py_DECREF(argTuple);
1656 Py_DECREF(method);
1657 if (!result) {
1658 PyErr_Print();
1659 }
1660 return result;
1661 }
1662
1663
1664 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) {
1665 cbh.setSelf(self, klass, incref);
1666 }
1667
1668 bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) {
1669 return cbh.findCallback(name);
1670 }
1671
1672 int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
1673 return cbh.callCallback(argTuple);
1674 }
1675
1676 PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
1677 return cbh.callCallbackObj(argTuple);
1678 }
1679
1680
1681 void wxPyCBH_delete(wxPyCallbackHelper* cbh) {
1682 if (cbh->m_incRef) {
1683 bool blocked = wxPyBeginBlockThreads();
1684 Py_XDECREF(cbh->m_self);
1685 Py_XDECREF(cbh->m_class);
1686 wxPyEndBlockThreads(blocked);
1687 }
1688 }
1689
1690 //---------------------------------------------------------------------------
1691 //---------------------------------------------------------------------------
1692 // These event classes can be derived from in Python and passed through the event
1693 // system without losing anything. They do this by keeping a reference to
1694 // themselves and some special case handling in wxPyCallback::EventThunker.
1695
1696
1697 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1698 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1699 //Py_INCREF(m_self); // circular loops...
1700 m_cloned = False;
1701 }
1702
1703 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1704 bool blocked = wxPyBeginBlockThreads();
1705 if (m_cloned)
1706 Py_DECREF(m_self);
1707 wxPyEndBlockThreads(blocked);
1708 }
1709
1710 void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) {
1711 bool blocked = wxPyBeginBlockThreads();
1712 if (m_cloned)
1713 Py_DECREF(m_self);
1714 m_self = self;
1715 if (clone) {
1716 Py_INCREF(m_self);
1717 m_cloned = True;
1718 }
1719 wxPyEndBlockThreads(blocked);
1720 }
1721
1722 PyObject* wxPyEvtSelfRef::GetSelf() const {
1723 Py_INCREF(m_self);
1724 return m_self;
1725 }
1726
1727
1728 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent, wxEvent);
1729 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent, wxCommandEvent);
1730
1731
1732 wxPyEvent::wxPyEvent(int winid, wxEventType commandType)
1733 : wxEvent(winid, commandType) {
1734 }
1735
1736
1737 wxPyEvent::wxPyEvent(const wxPyEvent& evt)
1738 : wxEvent(evt)
1739 {
1740 SetSelf(evt.m_self, True);
1741 }
1742
1743
1744 wxPyEvent::~wxPyEvent() {
1745 }
1746
1747
1748 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id)
1749 : wxCommandEvent(commandType, id) {
1750 }
1751
1752
1753 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent& evt)
1754 : wxCommandEvent(evt)
1755 {
1756 SetSelf(evt.m_self, True);
1757 }
1758
1759
1760 wxPyCommandEvent::~wxPyCommandEvent() {
1761 }
1762
1763
1764
1765
1766
1767 //---------------------------------------------------------------------------
1768 //---------------------------------------------------------------------------
1769 // Convert a wxList to a Python List, only works for lists of wxObjects
1770
1771 PyObject* wxPy_ConvertList(wxListBase* listbase) {
1772 wxList* list = (wxList*)listbase; // this is probably bad...
1773 PyObject* pyList;
1774 PyObject* pyObj;
1775 wxObject* wxObj;
1776 wxNode* node = list->GetFirst();
1777
1778 bool blocked = wxPyBeginBlockThreads();
1779 pyList = PyList_New(0);
1780 while (node) {
1781 wxObj = node->GetData();
1782 pyObj = wxPyMake_wxObject(wxObj);
1783 PyList_Append(pyList, pyObj);
1784 node = node->GetNext();
1785 }
1786 wxPyEndBlockThreads(blocked);
1787 return pyList;
1788 }
1789
1790 //----------------------------------------------------------------------
1791
1792 long wxPyGetWinHandle(wxWindow* win) {
1793
1794 #ifdef __WXMSW__
1795 return (long)win->GetHandle();
1796 #endif
1797
1798 // Find and return the actual X-Window.
1799 #ifdef __WXGTK__
1800 if (win->m_wxwindow) {
1801 #ifdef __WXGTK20__
1802 return (long) GDK_WINDOW_XWINDOW(GTK_PIZZA(win->m_wxwindow)->bin_window);
1803 #else
1804 GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window;
1805 if (bwin) {
1806 return (long)bwin->xwindow;
1807 }
1808 #endif
1809 }
1810 #endif
1811
1812 #ifdef __WXMAC__
1813 return (long)MAC_WXHWND(win->MacGetRootWindow());
1814 #endif
1815
1816 return 0;
1817 }
1818
1819 //----------------------------------------------------------------------
1820 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1821 // included in every file over and over again...
1822
1823 #if PYTHON_API_VERSION >= 1009
1824 static char* wxStringErrorMsg = "String or Unicode type required";
1825 #else
1826 static char* wxStringErrorMsg = "String type required";
1827 #endif
1828
1829
1830 wxString* wxString_in_helper(PyObject* source) {
1831 wxString* target;
1832 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1833 if (!PyString_Check(source) && !PyUnicode_Check(source)) {
1834 PyErr_SetString(PyExc_TypeError, wxStringErrorMsg);
1835 return NULL;
1836 }
1837 #if wxUSE_UNICODE
1838 if (PyUnicode_Check(source)) {
1839 target = new wxString();
1840 size_t len = PyUnicode_GET_SIZE(source);
1841 if (len) {
1842 PyUnicode_AsWideChar((PyUnicodeObject*)source, target->GetWriteBuf(len), len);
1843 target->UngetWriteBuf();
1844 }
1845 } else {
1846 // It is a string, get pointers to it and transform to unicode
1847 char* tmpPtr; int tmpSize;
1848 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1849 target = new wxString(tmpPtr, *wxConvCurrent, tmpSize);
1850 }
1851 #else
1852 char* tmpPtr; int tmpSize;
1853 if (PyString_AsStringAndSize(source, &tmpPtr, &tmpSize) == -1) {
1854 PyErr_SetString(PyExc_TypeError, "Unable to convert string");
1855 return NULL;
1856 }
1857 target = new wxString(tmpPtr, tmpSize);
1858 #endif // wxUSE_UNICODE
1859
1860 #else // No Python unicode API (1.5.2)
1861 if (!PyString_Check(source)) {
1862 PyErr_SetString(PyExc_TypeError, wxStringErrorMsg);
1863 return NULL;
1864 }
1865 target = new wxString(PyString_AS_STRING(source), PyString_GET_SIZE(source));
1866 #endif
1867 return target;
1868 }
1869
1870
1871 // Similar to above except doesn't use "new" and doesn't set an exception
1872 wxString Py2wxString(PyObject* source)
1873 {
1874 wxString target;
1875 bool doDecRef = False;
1876
1877 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1878 if (!PyString_Check(source) && !PyUnicode_Check(source)) {
1879 // Convert to String if not one already... (TODO: Unicode too?)
1880 source = PyObject_Str(source);
1881 doDecRef = True;
1882 }
1883
1884 #if wxUSE_UNICODE
1885 if (PyUnicode_Check(source)) {
1886 size_t len = PyUnicode_GET_SIZE(source);
1887 if (len) {
1888 PyUnicode_AsWideChar((PyUnicodeObject*)source, target.GetWriteBuf(len), len);
1889 target.UngetWriteBuf();
1890 }
1891 } else {
1892 // It is a string, get pointers to it and transform to unicode
1893 char* tmpPtr; int tmpSize;
1894 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1895 target = wxString(tmpPtr, *wxConvCurrent, tmpSize);
1896 }
1897 #else
1898 char* tmpPtr; int tmpSize;
1899 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1900 target = wxString(tmpPtr, tmpSize);
1901 #endif // wxUSE_UNICODE
1902
1903 #else // No Python unicode API (1.5.2)
1904 if (!PyString_Check(source)) {
1905 // Convert to String if not one already...
1906 source = PyObject_Str(source);
1907 doDecRef = True;
1908 }
1909 target = wxString(PyString_AS_STRING(source), PyString_GET_SIZE(source));
1910 #endif
1911
1912 if (doDecRef)
1913 Py_DECREF(source);
1914 return target;
1915 }
1916
1917
1918 // Make either a Python String or Unicode object, depending on build mode
1919 PyObject* wx2PyString(const wxString& src)
1920 {
1921 PyObject* str;
1922 #if wxUSE_UNICODE
1923 str = PyUnicode_FromWideChar(src.c_str(), src.Len());
1924 #else
1925 str = PyString_FromStringAndSize(src.c_str(), src.Len());
1926 #endif
1927 return str;
1928 }
1929
1930
1931 //----------------------------------------------------------------------
1932
1933
1934 byte* byte_LIST_helper(PyObject* source) {
1935 if (!PyList_Check(source)) {
1936 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1937 return NULL;
1938 }
1939 int count = PyList_Size(source);
1940 byte* temp = new byte[count];
1941 if (! temp) {
1942 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1943 return NULL;
1944 }
1945 for (int x=0; x<count; x++) {
1946 PyObject* o = PyList_GetItem(source, x);
1947 if (! PyInt_Check(o)) {
1948 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1949 return NULL;
1950 }
1951 temp[x] = (byte)PyInt_AsLong(o);
1952 }
1953 return temp;
1954 }
1955
1956
1957 int* int_LIST_helper(PyObject* source) {
1958 if (!PyList_Check(source)) {
1959 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1960 return NULL;
1961 }
1962 int count = PyList_Size(source);
1963 int* temp = new int[count];
1964 if (! temp) {
1965 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1966 return NULL;
1967 }
1968 for (int x=0; x<count; x++) {
1969 PyObject* o = PyList_GetItem(source, x);
1970 if (! PyInt_Check(o)) {
1971 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1972 return NULL;
1973 }
1974 temp[x] = PyInt_AsLong(o);
1975 }
1976 return temp;
1977 }
1978
1979
1980 long* long_LIST_helper(PyObject* source) {
1981 if (!PyList_Check(source)) {
1982 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1983 return NULL;
1984 }
1985 int count = PyList_Size(source);
1986 long* temp = new long[count];
1987 if (! temp) {
1988 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1989 return NULL;
1990 }
1991 for (int x=0; x<count; x++) {
1992 PyObject* o = PyList_GetItem(source, x);
1993 if (! PyInt_Check(o)) {
1994 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1995 return NULL;
1996 }
1997 temp[x] = PyInt_AsLong(o);
1998 }
1999 return temp;
2000 }
2001
2002
2003 char** string_LIST_helper(PyObject* source) {
2004 if (!PyList_Check(source)) {
2005 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2006 return NULL;
2007 }
2008 int count = PyList_Size(source);
2009 char** temp = new char*[count];
2010 if (! temp) {
2011 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2012 return NULL;
2013 }
2014 for (int x=0; x<count; x++) {
2015 PyObject* o = PyList_GetItem(source, x);
2016 if (! PyString_Check(o)) {
2017 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
2018 return NULL;
2019 }
2020 temp[x] = PyString_AsString(o);
2021 }
2022 return temp;
2023 }
2024
2025 //--------------------------------
2026 // Part of patch from Tim Hochberg
2027 static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) {
2028 if (PyInt_Check(o1) && PyInt_Check(o2)) {
2029 point->x = PyInt_AS_LONG(o1);
2030 point->y = PyInt_AS_LONG(o2);
2031 return True;
2032 }
2033 if (PyFloat_Check(o1) && PyFloat_Check(o2)) {
2034 point->x = (int)PyFloat_AS_DOUBLE(o1);
2035 point->y = (int)PyFloat_AS_DOUBLE(o2);
2036 return True;
2037 }
2038 if (wxPySwigInstance_Check(o1) || wxPySwigInstance_Check(o2)) { // TODO: Why???
2039 // Disallow instances because they can cause havok
2040 return False;
2041 }
2042 if (PyNumber_Check(o1) && PyNumber_Check(o2)) {
2043 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
2044 point->x = PyInt_AsLong(o1);
2045 point->y = PyInt_AsLong(o2);
2046 return True;
2047 }
2048 return False;
2049 }
2050
2051
2052 wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) {
2053 // Putting all of the declarations here allows
2054 // us to put the error handling all in one place.
2055 int x;
2056 wxPoint* temp;
2057 PyObject *o, *o1, *o2;
2058 bool isFast = PyList_Check(source) || PyTuple_Check(source);
2059
2060 if (!PySequence_Check(source)) {
2061 goto error0;
2062 }
2063
2064 // The length of the sequence is returned in count.
2065 *count = PySequence_Length(source);
2066 if (*count < 0) {
2067 goto error0;
2068 }
2069
2070 temp = new wxPoint[*count];
2071 if (!temp) {
2072 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2073 return NULL;
2074 }
2075 for (x=0; x<*count; x++) {
2076 // Get an item: try fast way first.
2077 if (isFast) {
2078 o = PySequence_Fast_GET_ITEM(source, x);
2079 }
2080 else {
2081 o = PySequence_GetItem(source, x);
2082 if (o == NULL) {
2083 goto error1;
2084 }
2085 }
2086
2087 // Convert o to wxPoint.
2088 if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) ||
2089 (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) {
2090 o1 = PySequence_Fast_GET_ITEM(o, 0);
2091 o2 = PySequence_Fast_GET_ITEM(o, 1);
2092 if (!wxPointFromObjects(o1, o2, &temp[x])) {
2093 goto error2;
2094 }
2095 }
2096 else if (wxPySwigInstance_Check(o)) {
2097 wxPoint* pt;
2098 if (! wxPyConvertSwigPtr(o, (void **)&pt, wxT("wxPoint"))) {
2099 goto error2;
2100 }
2101 temp[x] = *pt;
2102 }
2103 else if (PySequence_Check(o) && PySequence_Length(o) == 2) {
2104 o1 = PySequence_GetItem(o, 0);
2105 o2 = PySequence_GetItem(o, 1);
2106 if (!wxPointFromObjects(o1, o2, &temp[x])) {
2107 goto error3;
2108 }
2109 Py_DECREF(o1);
2110 Py_DECREF(o2);
2111 }
2112 else {
2113 goto error2;
2114 }
2115 // Clean up.
2116 if (!isFast)
2117 Py_DECREF(o);
2118 }
2119 return temp;
2120
2121 error3:
2122 Py_DECREF(o1);
2123 Py_DECREF(o2);
2124 error2:
2125 if (!isFast)
2126 Py_DECREF(o);
2127 error1:
2128 delete [] temp;
2129 error0:
2130 PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints.");
2131 return NULL;
2132 }
2133 // end of patch
2134 //------------------------------
2135
2136
2137 wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
2138 if (!PyList_Check(source)) {
2139 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2140 return NULL;
2141 }
2142 int count = PyList_Size(source);
2143 wxBitmap** temp = new wxBitmap*[count];
2144 if (! temp) {
2145 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2146 return NULL;
2147 }
2148 for (int x=0; x<count; x++) {
2149 PyObject* o = PyList_GetItem(source, x);
2150 if (wxPySwigInstance_Check(o)) {
2151 wxBitmap* pt;
2152 if (! wxPyConvertSwigPtr(o, (void **) &pt, wxT("wxBitmap"))) {
2153 PyErr_SetString(PyExc_TypeError,"Expected wxBitmap.");
2154 return NULL;
2155 }
2156 temp[x] = pt;
2157 }
2158 else {
2159 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
2160 return NULL;
2161 }
2162 }
2163 return temp;
2164 }
2165
2166
2167
2168 wxString* wxString_LIST_helper(PyObject* source) {
2169 if (!PyList_Check(source)) {
2170 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2171 return NULL;
2172 }
2173 int count = PyList_Size(source);
2174 wxString* temp = new wxString[count];
2175 if (! temp) {
2176 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2177 return NULL;
2178 }
2179 for (int x=0; x<count; x++) {
2180 PyObject* o = PyList_GetItem(source, x);
2181 #if PYTHON_API_VERSION >= 1009
2182 if (! PyString_Check(o) && ! PyUnicode_Check(o)) {
2183 PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects.");
2184 return NULL;
2185 }
2186 #else
2187 if (! PyString_Check(o)) {
2188 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
2189 return NULL;
2190 }
2191 #endif
2192
2193 wxString* pStr = wxString_in_helper(o);
2194 temp[x] = *pStr;
2195 delete pStr;
2196 }
2197 return temp;
2198 }
2199
2200
2201 wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
2202 if (!PyList_Check(source)) {
2203 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2204 return NULL;
2205 }
2206 int count = PyList_Size(source);
2207 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
2208 if (! temp) {
2209 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2210 return NULL;
2211 }
2212 for (int x=0; x<count; x++) {
2213 PyObject* o = PyList_GetItem(source, x);
2214 if (wxPySwigInstance_Check(o)) {
2215 wxAcceleratorEntry* ae;
2216 if (! wxPyConvertSwigPtr(o, (void **) &ae, wxT("wxAcceleratorEntry"))) {
2217 PyErr_SetString(PyExc_TypeError,"Expected wxAcceleratorEntry.");
2218 return NULL;
2219 }
2220 temp[x] = *ae;
2221 }
2222 else if (PyTuple_Check(o)) {
2223 PyObject* o1 = PyTuple_GetItem(o, 0);
2224 PyObject* o2 = PyTuple_GetItem(o, 1);
2225 PyObject* o3 = PyTuple_GetItem(o, 2);
2226 temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
2227 }
2228 else {
2229 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
2230 return NULL;
2231 }
2232 }
2233 return temp;
2234 }
2235
2236
2237 wxPen** wxPen_LIST_helper(PyObject* source) {
2238 if (!PyList_Check(source)) {
2239 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2240 return NULL;
2241 }
2242 int count = PyList_Size(source);
2243 wxPen** temp = new wxPen*[count];
2244 if (!temp) {
2245 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2246 return NULL;
2247 }
2248 for (int x=0; x<count; x++) {
2249 PyObject* o = PyList_GetItem(source, x);
2250 if (wxPySwigInstance_Check(o)) {
2251 wxPen* pt;
2252 if (! wxPyConvertSwigPtr(o, (void **)&pt, wxT("wxPen"))) {
2253 delete temp;
2254 PyErr_SetString(PyExc_TypeError,"Expected wxPen.");
2255 return NULL;
2256 }
2257 temp[x] = pt;
2258 }
2259 else {
2260 delete temp;
2261 PyErr_SetString(PyExc_TypeError, "Expected a list of wxPens.");
2262 return NULL;
2263 }
2264 }
2265 return temp;
2266 }
2267
2268
2269 bool wxPy2int_seq_helper(PyObject* source, int* i1, int* i2) {
2270 bool isFast = PyList_Check(source) || PyTuple_Check(source);
2271 PyObject *o1, *o2;
2272
2273 if (!PySequence_Check(source) || PySequence_Length(source) != 2)
2274 return False;
2275
2276 if (isFast) {
2277 o1 = PySequence_Fast_GET_ITEM(source, 0);
2278 o2 = PySequence_Fast_GET_ITEM(source, 1);
2279 }
2280 else {
2281 o1 = PySequence_GetItem(source, 0);
2282 o2 = PySequence_GetItem(source, 1);
2283 }
2284
2285 *i1 = PyInt_AsLong(o1);
2286 *i2 = PyInt_AsLong(o2);
2287
2288 if (! isFast) {
2289 Py_DECREF(o1);
2290 Py_DECREF(o2);
2291 }
2292 return True;
2293 }
2294
2295
2296 bool wxPy4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4) {
2297 bool isFast = PyList_Check(source) || PyTuple_Check(source);
2298 PyObject *o1, *o2, *o3, *o4;
2299
2300 if (!PySequence_Check(source) || PySequence_Length(source) != 4)
2301 return False;
2302
2303 if (isFast) {
2304 o1 = PySequence_Fast_GET_ITEM(source, 0);
2305 o2 = PySequence_Fast_GET_ITEM(source, 1);
2306 o3 = PySequence_Fast_GET_ITEM(source, 2);
2307 o4 = PySequence_Fast_GET_ITEM(source, 3);
2308 }
2309 else {
2310 o1 = PySequence_GetItem(source, 0);
2311 o2 = PySequence_GetItem(source, 1);
2312 o3 = PySequence_GetItem(source, 2);
2313 o4 = PySequence_GetItem(source, 3);
2314 }
2315
2316 *i1 = PyInt_AsLong(o1);
2317 *i2 = PyInt_AsLong(o2);
2318 *i3 = PyInt_AsLong(o3);
2319 *i4 = PyInt_AsLong(o4);
2320
2321 if (! isFast) {
2322 Py_DECREF(o1);
2323 Py_DECREF(o2);
2324 Py_DECREF(o3);
2325 Py_DECREF(o4);
2326 }
2327 return True;
2328 }
2329
2330
2331 //----------------------------------------------------------------------
2332
2333 bool wxPySimple_typecheck(PyObject* source, const wxChar* classname, int seqLen)
2334 {
2335 void* ptr;
2336
2337 if (wxPySwigInstance_Check(source) &&
2338 wxPyConvertSwigPtr(source, (void **)&ptr, classname))
2339 return True;
2340
2341 PyErr_Clear();
2342 if (PySequence_Check(source) && PySequence_Length(source) == seqLen)
2343 return True;
2344
2345 return False;
2346 }
2347
2348 bool wxSize_helper(PyObject* source, wxSize** obj)
2349 {
2350 if (source == Py_None) {
2351 **obj = wxSize(-1,-1);
2352 return True;
2353 }
2354 return wxPyTwoIntItem_helper(source, obj, wxT("wxSize"));
2355 }
2356
2357
2358 bool wxPoint_helper(PyObject* source, wxPoint** obj)
2359 {
2360 if (source == Py_None) {
2361 **obj = wxPoint(-1,-1);
2362 return True;
2363 }
2364 return wxPyTwoIntItem_helper(source, obj, wxT("wxPoint"));
2365 }
2366
2367
2368
2369 bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) {
2370
2371 if (source == Py_None) {
2372 **obj = wxRealPoint(-1,-1);
2373 return True;
2374 }
2375
2376 // If source is an object instance then it may already be the right type
2377 if (wxPySwigInstance_Check(source)) {
2378 wxRealPoint* ptr;
2379 if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxRealPoint")))
2380 goto error;
2381 *obj = ptr;
2382 return True;
2383 }
2384 // otherwise a 2-tuple of floats is expected
2385 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
2386 PyObject* o1 = PySequence_GetItem(source, 0);
2387 PyObject* o2 = PySequence_GetItem(source, 1);
2388 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
2389 Py_DECREF(o1);
2390 Py_DECREF(o2);
2391 goto error;
2392 }
2393 **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
2394 Py_DECREF(o1);
2395 Py_DECREF(o2);
2396 return True;
2397 }
2398
2399 error:
2400 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object.");
2401 return False;
2402 }
2403
2404
2405
2406 bool wxRect_helper(PyObject* source, wxRect** obj) {
2407
2408 if (source == Py_None) {
2409 **obj = wxRect(-1,-1,-1,-1);
2410 return True;
2411 }
2412
2413 // If source is an object instance then it may already be the right type
2414 if (wxPySwigInstance_Check(source)) {
2415 wxRect* ptr;
2416 if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxRect")))
2417 goto error;
2418 *obj = ptr;
2419 return True;
2420 }
2421 // otherwise a 4-tuple of integers is expected
2422 else if (PySequence_Check(source) && PyObject_Length(source) == 4) {
2423 PyObject* o1 = PySequence_GetItem(source, 0);
2424 PyObject* o2 = PySequence_GetItem(source, 1);
2425 PyObject* o3 = PySequence_GetItem(source, 2);
2426 PyObject* o4 = PySequence_GetItem(source, 3);
2427 if (!PyNumber_Check(o1) || !PyNumber_Check(o2) ||
2428 !PyNumber_Check(o3) || !PyNumber_Check(o4)) {
2429 Py_DECREF(o1);
2430 Py_DECREF(o2);
2431 Py_DECREF(o3);
2432 Py_DECREF(o4);
2433 goto error;
2434 }
2435 **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2),
2436 PyInt_AsLong(o3), PyInt_AsLong(o4));
2437 Py_DECREF(o1);
2438 Py_DECREF(o2);
2439 Py_DECREF(o3);
2440 Py_DECREF(o4);
2441 return True;
2442 }
2443
2444 error:
2445 PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object.");
2446 return False;
2447 }
2448
2449
2450
2451 bool wxColour_helper(PyObject* source, wxColour** obj) {
2452
2453 if (source == Py_None) {
2454 **obj = wxNullColour;
2455 return True;
2456 }
2457
2458 // If source is an object instance then it may already be the right type
2459 if (wxPySwigInstance_Check(source)) {
2460 wxColour* ptr;
2461 if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxColour")))
2462 goto error;
2463 *obj = ptr;
2464 return True;
2465 }
2466 // otherwise check for a string
2467 else if (PyString_Check(source) || PyUnicode_Check(source)) {
2468 wxString spec = Py2wxString(source);
2469 if (spec.GetChar(0) == '#' && spec.Length() == 7) { // It's #RRGGBB
2470 long red, green, blue;
2471 red = green = blue = 0;
2472 spec.Mid(1,2).ToLong(&red, 16);
2473 spec.Mid(3,2).ToLong(&green, 16);
2474 spec.Mid(5,2).ToLong(&blue, 16);
2475
2476 **obj = wxColour(red, green, blue);
2477 return True;
2478 }
2479 else { // it's a colour name
2480 **obj = wxColour(spec);
2481 return True;
2482 }
2483 }
2484 // last chance: 3-tuple of integers is expected
2485 else if (PySequence_Check(source) && PyObject_Length(source) == 3) {
2486 PyObject* o1 = PySequence_GetItem(source, 0);
2487 PyObject* o2 = PySequence_GetItem(source, 1);
2488 PyObject* o3 = PySequence_GetItem(source, 2);
2489 if (!PyNumber_Check(o1) || !PyNumber_Check(o2) || !PyNumber_Check(o3)) {
2490 Py_DECREF(o1);
2491 Py_DECREF(o2);
2492 Py_DECREF(o3);
2493 goto error;
2494 }
2495 **obj = wxColour(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
2496 Py_DECREF(o1);
2497 Py_DECREF(o2);
2498 Py_DECREF(o3);
2499 return True;
2500 }
2501
2502 error:
2503 PyErr_SetString(PyExc_TypeError,
2504 "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
2505 return False;
2506 }
2507
2508
2509 bool wxColour_typecheck(PyObject* source) {
2510
2511 if (wxPySimple_typecheck(source, wxT("wxColour"), 3))
2512 return True;
2513
2514 if (PyString_Check(source) || PyUnicode_Check(source))
2515 return True;
2516
2517 return False;
2518 }
2519
2520
2521
2522 bool wxPoint2D_helper(PyObject* source, wxPoint2D** obj) {
2523
2524 if (source == Py_None) {
2525 **obj = wxPoint2D(-1,-1);
2526 return True;
2527 }
2528
2529 // If source is an object instance then it may already be the right type
2530 if (wxPySwigInstance_Check(source)) {
2531 wxPoint2D* ptr;
2532 if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxPoint2D")))
2533 goto error;
2534 *obj = ptr;
2535 return True;
2536 }
2537 // otherwise a length-2 sequence of floats is expected
2538 if (PySequence_Check(source) && PySequence_Length(source) == 2) {
2539 PyObject* o1 = PySequence_GetItem(source, 0);
2540 PyObject* o2 = PySequence_GetItem(source, 1);
2541 // This should really check for floats, not numbers -- but that would break code.
2542 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
2543 Py_DECREF(o1);
2544 Py_DECREF(o2);
2545 goto error;
2546 }
2547 **obj = wxPoint2D(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
2548 Py_DECREF(o1);
2549 Py_DECREF(o2);
2550 return True;
2551 }
2552 error:
2553 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxPoint2D object.");
2554 return False;
2555 }
2556
2557
2558 //----------------------------------------------------------------------
2559
2560 PyObject* wxArrayString2PyList_helper(const wxArrayString& arr) {
2561
2562 PyObject* list = PyList_New(0);
2563 for (size_t i=0; i < arr.GetCount(); i++) {
2564 #if wxUSE_UNICODE
2565 PyObject* str = PyUnicode_FromWideChar(arr[i].c_str(), arr[i].Len());
2566 #else
2567 PyObject* str = PyString_FromStringAndSize(arr[i].c_str(), arr[i].Len());
2568 #endif
2569 PyList_Append(list, str);
2570 Py_DECREF(str);
2571 }
2572 return list;
2573 }
2574
2575
2576 PyObject* wxArrayInt2PyList_helper(const wxArrayInt& arr) {
2577
2578 PyObject* list = PyList_New(0);
2579 for (size_t i=0; i < arr.GetCount(); i++) {
2580 PyObject* number = PyInt_FromLong(arr[i]);
2581 PyList_Append(list, number);
2582 Py_DECREF(number);
2583 }
2584 return list;
2585 }
2586
2587
2588 //----------------------------------------------------------------------
2589 //----------------------------------------------------------------------
2590
2591
2592
2593