1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     Helper functions/classes for the wxPython extension module 
   7 // Created:     1-July-1997 
   9 // Copyright:   (c) 1998 by Total Control Software 
  10 // Licence:     wxWindows license 
  11 ///////////////////////////////////////////////////////////////////////////// 
  16 #include "wx/wxPython/wxPython_int.h" 
  17 #include "wx/wxPython/pyistream.h" 
  20 #include <wx/msw/private.h> 
  21 #include <wx/msw/winundef.h> 
  22 #include <wx/msw/msvcrt.h> 
  29 #include <gdk/gdkprivate.h> 
  30 #include <wx/gtk/win_gtk.h> 
  31 #define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \ 
  32                               GDK_WINDOW_XWINDOW(GTK_PIZZA((wxwin)->m_wxwindow)->bin_window) : \ 
  33                               GDK_WINDOW_XWINDOW((wxwin)->m_widget->window) 
  38 #include "wx/x11/privx.h" 
  39 #define GetXWindow(wxwin)   ((Window)(wxwin)->GetHandle()) 
  43 #include <wx/mac/private.h> 
  46 #include <wx/clipbrd.h> 
  47 #include <wx/mimetype.h> 
  50 //---------------------------------------------------------------------- 
  52 #if PYTHON_API_VERSION < 1009 && wxUSE_UNICODE 
  53 #error Python must support Unicode to use wxWindows Unicode 
  56 //---------------------------------------------------------------------- 
  58 wxPyApp
* wxPythonApp 
= NULL
;  // Global instance of application object 
  59 bool wxPyDoCleanup 
= false; 
  60 bool wxPyDoingCleanup 
= false; 
  63 #ifdef WXP_WITH_THREAD 
  64 #if !wxPyUSE_GIL_STATE 
  65 struct wxPyThreadState 
{ 
  67     PyThreadState
* tstate
; 
  69     wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
) 
  70         : tid(_tid
), tstate(_tstate
) {} 
  73 #include <wx/dynarray.h> 
  74 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
); 
  75 #include <wx/arrimpl.cpp> 
  76 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
); 
  78 wxPyThreadStateArray
* wxPyTStates 
= NULL
; 
  79 wxMutex
*              wxPyTMutex 
= NULL
; 
  85 #define DEFAULTENCODING_SIZE 64 
  86 static char wxPyDefaultEncoding
[DEFAULTENCODING_SIZE
] = "ascii"; 
  88 static PyObject
* wxPython_dict 
= NULL
; 
  89 static PyObject
* wxPyAssertionError 
= NULL
; 
  90 static PyObject
* wxPyNoAppError 
= NULL
; 
  92 PyObject
* wxPyPtrTypeMap 
= NULL
; 
  95 #ifdef __WXMSW__             // If building for win32... 
  96 //---------------------------------------------------------------------- 
  97 // This gets run when the DLL is loaded.  We just need to save a handle. 
  98 //---------------------------------------------------------------------- 
 101     HINSTANCE   hinstDLL
,    // handle to DLL module 
 102     DWORD       fdwReason
,   // reason for calling function 
 103     LPVOID      lpvReserved  
// reserved 
 106     // If wxPython is embedded in another wxWidgets app then 
 107     // the instance has already been set. 
 108     if (! wxGetInstance()) 
 109         wxSetInstance(hinstDLL
); 
 114 //---------------------------------------------------------------------- 
 115 // Classes for implementing the wxp main application shell. 
 116 //---------------------------------------------------------------------- 
 118 IMPLEMENT_ABSTRACT_CLASS(wxPyApp
, wxApp
); 
 122     m_assertMode 
= wxPYAPP_ASSERT_EXCEPTION
; 
 123     m_startupComplete 
= false; 
 127 wxPyApp::~wxPyApp() { 
 131 // This one isn't acutally called...  We fake it with _BootstrapApp 
 132 bool wxPyApp::OnInit() { 
 137 int  wxPyApp::MainLoop() { 
 140     DeletePendingObjects(); 
 141     bool initialized 
= wxTopLevelWindows
.GetCount() != 0; 
 143         if ( m_exitOnFrameDelete 
== Later 
) { 
 144             m_exitOnFrameDelete 
= Yes
; 
 147         retval 
= wxApp::MainLoop(); 
 154 bool wxPyApp::OnInitGui() { 
 156     wxApp::OnInitGui();  // in this case always call the base class version 
 157     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
 158     if (wxPyCBH_findCallback(m_myInst
, "OnInitGui")) 
 159         rval 
= wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()")); 
 160     wxPyEndBlockThreads(blocked
); 
 165 int wxPyApp::OnExit() { 
 167     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
 168     if (wxPyCBH_findCallback(m_myInst
, "OnExit")) 
 169         rval 
= wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()")); 
 170     wxPyEndBlockThreads(blocked
); 
 171     wxApp::OnExit();  // in this case always call the base class version 
 177 void wxPyApp::OnAssert(const wxChar 
*file
, 
 182     // if we're not fully initialized then just log the error 
 183     if (! m_startupComplete
) { 
 186         buf
.Printf(wxT("%s(%d): assert \"%s\" failed"), 
 196     // If the OnAssert is overloaded in the Python class then call it... 
 198     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
 199     if ((found 
= wxPyCBH_findCallback(m_myInst
, "OnAssert"))) { 
 200         PyObject
* fso 
= wx2PyString(file
); 
 201         PyObject
* cso 
= wx2PyString(file
); 
 204             mso 
= wx2PyString(file
); 
 206             mso 
= Py_None
; Py_INCREF(Py_None
); 
 208         wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(OiOO)", fso
, line
, cso
, mso
)); 
 213     wxPyEndBlockThreads(blocked
); 
 215     // ...otherwise do our own thing with it 
 218         if (m_assertMode 
& wxPYAPP_ASSERT_SUPPRESS
) 
 221         // turn it into a Python exception? 
 222         if (m_assertMode 
& wxPYAPP_ASSERT_EXCEPTION
) { 
 225             buf
.Printf(wxT("C++ assertion \"%s\" failed in %s(%d)"), cond
, file
, line
); 
 232             wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
 233             PyObject
* s 
= wx2PyString(buf
); 
 234             PyErr_SetObject(wxPyAssertionError
, s
); 
 236             wxPyEndBlockThreads(blocked
); 
 238             // Now when control returns to whatever API wrapper was called from 
 239             // Python it should detect that an exception is set and will return 
 240             // NULL, signalling the exception to Python. 
 243         // Send it to the normal log destination, but only if 
 244         // not _DIALOG because it will call this too 
 245         if ( (m_assertMode 
& wxPYAPP_ASSERT_LOG
) && !(m_assertMode 
& wxPYAPP_ASSERT_DIALOG
)) { 
 248             buf
.Printf(wxT("%s(%d): assert \"%s\" failed"), 
 257         // do the normal wx assert dialog? 
 258         if (m_assertMode 
& wxPYAPP_ASSERT_DIALOG
) 
 259             wxApp::OnAssert(file
, line
, cond
, msg
); 
 264     // For catching Apple Events 
 265 void wxPyApp::MacOpenFile(const wxString 
&fileName
) 
 267     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
 268     if (wxPyCBH_findCallback(m_myInst
, "MacOpenFile")) { 
 269         PyObject
* s 
= wx2PyString(fileName
); 
 270         wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(O)", s
)); 
 273     wxPyEndBlockThreads(blocked
); 
 276 void wxPyApp::MacPrintFile(const wxString 
&fileName
) 
 278     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
 279     if (wxPyCBH_findCallback(m_myInst
, "MacPrintFile")) { 
 280         PyObject
* s 
= wx2PyString(fileName
); 
 281         wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(O)", s
)); 
 284     wxPyEndBlockThreads(blocked
); 
 287 void wxPyApp::MacNewFile() 
 289     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
 290     if (wxPyCBH_findCallback(m_myInst
, "MacNewFile")) 
 291         wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()")); 
 292     wxPyEndBlockThreads(blocked
); 
 295 void wxPyApp::MacReopenApp() 
 297     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
 298     if (wxPyCBH_findCallback(m_myInst
, "MacReopenApp")) 
 299         wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()")); 
 300     wxPyEndBlockThreads(blocked
); 
 305 bool wxPyApp::GetMacSupportPCMenuShortcuts() { 
 307     return s_macSupportPCMenuShortcuts
; 
 314 long wxPyApp::GetMacAboutMenuItemId() { 
 316     return s_macAboutMenuItemId
; 
 323 long wxPyApp::GetMacPreferencesMenuItemId() { 
 325     return s_macPreferencesMenuItemId
; 
 332 long wxPyApp::GetMacExitMenuItemId() { 
 334     return s_macExitMenuItemId
; 
 341 wxString 
wxPyApp::GetMacHelpMenuTitleName() { 
 343     return s_macHelpMenuTitleName
; 
 345     return wxEmptyString
; 
 350 void wxPyApp::SetMacSupportPCMenuShortcuts(bool val
) { 
 352     s_macSupportPCMenuShortcuts 
= val
; 
 357 void wxPyApp::SetMacAboutMenuItemId(long val
) { 
 359     s_macAboutMenuItemId 
= val
; 
 364 void wxPyApp::SetMacPreferencesMenuItemId(long val
) { 
 366     s_macPreferencesMenuItemId 
= val
; 
 371 void wxPyApp::SetMacExitMenuItemId(long val
) { 
 373     s_macExitMenuItemId 
= val
; 
 378 void wxPyApp::SetMacHelpMenuTitleName(const wxString
& val
) { 
 380     s_macHelpMenuTitleName 
= val
; 
 385 // This finishes the initialization of wxWindows and then calls the OnInit 
 386 // that should be present in the derived (Python) class. 
 387 void wxPyApp::_BootstrapApp() 
 389     static      bool haveInitialized 
= false; 
 392     PyObject
*   retval 
= NULL
; 
 393     PyObject
*   pyint  
= NULL
; 
 396     // Only initialize wxWidgets once 
 397     if (! haveInitialized
) { 
 399         // Get any command-line args passed to this program from the sys module 
 402         blocked 
= wxPyBeginBlockThreads(); 
 404         PyObject
* sysargv 
= PySys_GetObject("argv"); 
 405         PyObject
* executable 
= PySys_GetObject("executable"); 
 407         if (sysargv 
!= NULL 
&& executable 
!= NULL
) { 
 408             argc 
= PyList_Size(sysargv
) + 1; 
 409             argv 
= new char*[argc
+1]; 
 410             argv
[0] = PyString_AsString(executable
); 
 412             for(x
=1; x
<argc
; x
++) { 
 413                 PyObject 
*pyArg 
= PyList_GetItem(sysargv
, x
-1); 
 414                 argv
[x
] = PyString_AsString(pyArg
); 
 418         wxPyEndBlockThreads(blocked
); 
 420         // Initialize wxWidgets 
 421         result 
= wxEntryStart(argc
, argv
); 
 424         blocked 
= wxPyBeginBlockThreads(); 
 426             PyErr_SetString(PyExc_SystemError
, 
 427                             "wxEntryStart failed, unable to initialize wxWidgets!" 
 429                             "  (Is DISPLAY set properly?)" 
 435         // On wxGTK the locale will be changed to match the system settings, 
 436         // but Python before 2.4 needs to have LC_NUMERIC set to "C" in order 
 437         // for the floating point conversions and such to work right. 
 438 #if defined(__WXGTK__) && PY_VERSION_HEX < 0x02040000 
 439         setlocale(LC_NUMERIC
, "C"); 
 442         // The stock objects were all NULL when they were loaded into 
 443         // SWIG generated proxies, so re-init those now... 
 444         wxPy_ReinitStockObjects(3); 
 446         wxPyEndBlockThreads(blocked
); 
 447         haveInitialized 
= true; 
 450     // It's now ok to generate exceptions for assertion errors. 
 451     wxPythonApp
->SetStartupComplete(true); 
 453     // Call the Python wxApp's OnInit function 
 454     blocked 
= wxPyBeginBlockThreads(); 
 455     if (wxPyCBH_findCallback(m_myInst
, "OnInit")) { 
 457         PyObject
* method 
= m_myInst
.GetLastFound(); 
 458         PyObject
* argTuple 
= PyTuple_New(0); 
 459         retval 
= PyEval_CallObject(method
, argTuple
); 
 465         pyint 
= PyNumber_Int(retval
); 
 467             PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value"); 
 470         result 
= PyInt_AS_LONG(pyint
); 
 473         // Is it okay if there is no OnInit?  Probably so... 
 478         PyErr_SetString(PyExc_SystemExit
, "OnInit returned false, exiting..."); 
 485     wxPyEndBlockThreads(blocked
); 
 488 //--------------------------------------------------------------------- 
 489 //---------------------------------------------------------------------- 
 493 static char* wxPyCopyCString(const wxChar
* src
) 
 495     wxWX2MBbuf buff 
= (wxWX2MBbuf
)wxConvCurrent
->cWX2MB(src
); 
 496     size_t len 
= strlen(buff
); 
 497     char*  dest 
= new char[len
+1]; 
 503 static char* wxPyCopyCString(const char* src
)   // we need a char version too 
 505     size_t len 
= strlen(src
); 
 506     char*  dest 
= new char[len
+1]; 
 512 static wxChar
* wxPyCopyWString(const char *src
) 
 514     //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src); 
 515     wxString 
str(src
, *wxConvCurrent
); 
 516     return copystring(str
); 
 520 static wxChar
* wxPyCopyWString(const wxChar 
*src
) 
 522     return copystring(src
); 
 528 inline const char* dropwx(const char* name
) { 
 529     if (name
[0] == 'w' && name
[1] == 'x') 
 535 //---------------------------------------------------------------------- 
 537 // This function is called when the wx._core_ module is imported to do some 
 538 // initial setup.  (Before there is a wxApp object.)  The rest happens in 
 539 // wxPyApp::_BootstrapApp 
 540 void __wxPyPreStart(PyObject
* moduleDict
) 
 544 //     wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF 
 545 //                     | _CRTDBG_CHECK_ALWAYS_DF 
 546 //                     | _CRTDBG_DELAY_FREE_MEM_DF 
 550 #ifdef WXP_WITH_THREAD 
 551 #if wxPyUSE_GIL_STATE 
 552     PyEval_InitThreads(); 
 554     PyEval_InitThreads(); 
 555     wxPyTStates 
= new wxPyThreadStateArray
; 
 556     wxPyTMutex 
= new wxMutex
; 
 558     // Save the current (main) thread state in our array 
 559     PyThreadState
* tstate 
= wxPyBeginAllowThreads(); 
 560     wxPyEndAllowThreads(tstate
); 
 564     // Ensure that the build options in the DLL (or whatever) match this build 
 565     wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "wxPython"); 
 567     // Init the stock objects to a non-NULL value so SWIG doesn't create them as None 
 568     wxPy_ReinitStockObjects(1); 
 570     wxInitAllImageHandlers(); 
 575 void __wxPyCleanup() { 
 576     wxPyDoingCleanup 
= true; 
 578         wxPyDoCleanup 
= false; 
 581 #ifdef WXP_WITH_THREAD 
 582 #if !wxPyUSE_GIL_STATE 
 585     wxPyTStates
->Empty(); 
 593 // Save a reference to the dictionary of the wx._core module, and inject 
 594 // a few more things into it. 
 595 PyObject
* __wxPySetDictionary(PyObject
* /* self */, PyObject
* args
) 
 598     if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
)) 
 601     if (!PyDict_Check(wxPython_dict
)) { 
 602         PyErr_SetString(PyExc_TypeError
, 
 603                         "_wxPySetDictionary must have dictionary object!"); 
 607     if (! wxPyPtrTypeMap
) 
 608         wxPyPtrTypeMap 
= PyDict_New(); 
 609     PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
); 
 611     // Create an exception object to use for wxASSERTions 
 612     wxPyAssertionError 
= PyErr_NewException("wx._core.PyAssertionError", 
 613                                             PyExc_AssertionError
, NULL
); 
 614     PyDict_SetItemString(wxPython_dict
, "PyAssertionError", wxPyAssertionError
); 
 616     // Create an exception object to use when the app object hasn't been created yet 
 617     wxPyNoAppError 
= PyErr_NewException("wx._core.PyNoAppError", 
 618                                         PyExc_RuntimeError
, NULL
); 
 619     PyDict_SetItemString(wxPython_dict
, "PyNoAppError", wxPyNoAppError
); 
 624 #define wxPlatform "__WXMOTIF__" 
 625 #define wxPlatName "wxMotif" 
 628 #define wxPlatform "__WXX11__" 
 629 #define wxPlatName "wxX11" 
 632 #define wxPlatform "__WXGTK__" 
 633 #define wxPlatName "wxGTK" 
 636 #define wxPlatform "__WXMSW__" 
 637 #define wxPlatName "wxMSW" 
 640 #define wxPlatform "__WXMAC__" 
 641 #define wxPlatName "wxMac" 
 650     // These should be deprecated in favor of the PlatformInfo tuple built below... 
 651     PyDict_SetItemString(wxPython_dict
, "Platform", PyString_FromString(wxPlatform
)); 
 652     PyDict_SetItemString(wxPython_dict
, "USE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
)); 
 653     PyDict_SetItemString(wxPython_dict
, "__WXDEBUG__", PyInt_FromLong(wxdebug
)); 
 655     // Make a tuple of strings that gives more info about the platform. 
 656     PyObject
* PlatInfo 
= PyList_New(0); 
 659 #define _AddInfoString(st) \ 
 660     obj = PyString_FromString(st); \ 
 661     PyList_Append(PlatInfo, obj); \ 
 664     _AddInfoString(wxPlatform
); 
 665     _AddInfoString(wxPlatName
); 
 667     _AddInfoString("unicode"); 
 669     _AddInfoString("ansi"); 
 673     _AddInfoString("gtk2"); 
 675     _AddInfoString("gtk1"); 
 679     _AddInfoString("wx-assertions-on"); 
 681     _AddInfoString("wx-assertions-off"); 
 684 #undef _AddInfoString 
 686     PyObject
* PlatInfoTuple 
= PyList_AsTuple(PlatInfo
); 
 688     PyDict_SetItemString(wxPython_dict
, "PlatformInfo", PlatInfoTuple
); 
 694 //--------------------------------------------------------------------------- 
 696 // Python's PyInstance_Check does not return True for instances of new-style 
 697 // classes.  This should get close enough for both new and old classes but I 
 698 // should re-evaluate the need for doing instance checks... 
 699 bool wxPyInstance_Check(PyObject
* obj
) { 
 700     return PyObject_HasAttrString(obj
, "__class__") != 0; 
 704 // This one checks if the object is an instance of a SWIG proxy class (it has 
 705 // a .this attribute) 
 706 bool wxPySwigInstance_Check(PyObject
* obj
) { 
 707     return PyObject_HasAttrString(obj
, "this") != 0; 
 710 //--------------------------------------------------------------------------- 
 712 // The stock objects are no longer created when the wx._core_ module is 
 713 // imported, but only after the app object has been created.  The 
 714 // wxPy_ReinitStockObjects function will be called 3 times to pass the stock 
 715 // objects though various stages of evolution: 
 717 //   pass 1: Set all the pointers to a non-NULL value so the Python proxy 
 718 //           object will be created (otherwise SWIG will just use None.) 
 720 //   pass 2: After the module has been imported and the python proxys have 
 721 //           been created, then set the __class__ to be _wxPyUnbornObject so 
 722 //           it will catch any access to the object and will raise an exception. 
 724 //   pass 3: Finally, from BootstrapApp patch things up so the stock objects 
 728 PyObject
* __wxPyFixStockObjects(PyObject
* /* self */, PyObject
* args
) 
 730     wxPy_ReinitStockObjects(2); 
 735 static void rsoPass2(const char* name
) 
 737     static PyObject
* unbornObjectClass 
= NULL
; 
 740     if (unbornObjectClass 
== NULL
) { 
 741         unbornObjectClass 
= PyDict_GetItemString(wxPython_dict
, "_wxPyUnbornObject"); 
 742         Py_INCREF(unbornObjectClass
); 
 745     // Find the object instance 
 746     obj 
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(name
)); 
 747     wxCHECK_RET(obj 
!= NULL
, wxT("Unable to find stock object")); 
 748     wxCHECK_RET(wxPySwigInstance_Check(obj
), wxT("Not a swig instance")); 
 751     PyObject_SetAttrString(obj
, "__class__",  unbornObjectClass
); 
 755 static void rsoPass3(const char* name
, const char* classname
, void* ptr
) 
 761     // Find the object instance 
 762     obj 
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(name
)); 
 763     wxCHECK_RET(obj 
!= NULL
, wxT("Unable to find stock object")); 
 764     wxCHECK_RET(wxPySwigInstance_Check(obj
), wxT("Not a swig instance")); 
 766     // Find the class object and put it back in the instance 
 767     classobj 
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(classname
)); 
 768     wxCHECK_RET(classobj 
!= NULL
, wxT("Unable to find stock class object")); 
 769     PyObject_SetAttrString(obj
, "__class__",  classobj
); 
 771     // Rebuild the .this swigified pointer with the new value of the C++ pointer 
 772     ptrobj 
= wxPyMakeSwigPtr(ptr
, wxString(classname
, *wxConvCurrent
)); 
 773     PyObject_SetAttrString(obj
, "this", ptrobj
); 
 779 void wxPy_ReinitStockObjects(int pass
) 
 782     // If there is already an App object then wxPython is probably embedded in 
 783     // a wx C++ application, so there is no need to do all this. 
 784     static bool embedded 
= false; 
 785     if ((pass 
== 1 || pass 
== 2) && wxTheApp
) { 
 789     if (pass 
== 3 && embedded
) 
 793 #define REINITOBJ(name, classname) \ 
 794     if (pass == 1) { name = (classname*)0xC0C0C0C0; } \ 
 795     else if (pass == 2) { rsoPass2(#name); } \ 
 796     else if (pass == 3) { rsoPass3(#name, #classname, (void*)name); } 
 799 #define REINITOBJ2(name, classname) \ 
 801     else if (pass == 2) { rsoPass2(#name); } \ 
 802     else if (pass == 3) { rsoPass3(#name, #classname, (void*)&name); } 
 805     REINITOBJ(wxNORMAL_FONT
, wxFont
); 
 806     REINITOBJ(wxSMALL_FONT
, wxFont
); 
 807     REINITOBJ(wxITALIC_FONT
, wxFont
); 
 808     REINITOBJ(wxSWISS_FONT
, wxFont
); 
 810     REINITOBJ(wxRED_PEN
, wxPen
); 
 811     REINITOBJ(wxCYAN_PEN
, wxPen
); 
 812     REINITOBJ(wxGREEN_PEN
, wxPen
); 
 813     REINITOBJ(wxBLACK_PEN
, wxPen
); 
 814     REINITOBJ(wxWHITE_PEN
, wxPen
); 
 815     REINITOBJ(wxTRANSPARENT_PEN
, wxPen
); 
 816     REINITOBJ(wxBLACK_DASHED_PEN
, wxPen
); 
 817     REINITOBJ(wxGREY_PEN
, wxPen
); 
 818     REINITOBJ(wxMEDIUM_GREY_PEN
, wxPen
); 
 819     REINITOBJ(wxLIGHT_GREY_PEN
, wxPen
); 
 821     REINITOBJ(wxBLUE_BRUSH
, wxBrush
); 
 822     REINITOBJ(wxGREEN_BRUSH
, wxBrush
); 
 823     REINITOBJ(wxWHITE_BRUSH
, wxBrush
); 
 824     REINITOBJ(wxBLACK_BRUSH
, wxBrush
); 
 825     REINITOBJ(wxTRANSPARENT_BRUSH
, wxBrush
); 
 826     REINITOBJ(wxCYAN_BRUSH
, wxBrush
); 
 827     REINITOBJ(wxRED_BRUSH
, wxBrush
); 
 828     REINITOBJ(wxGREY_BRUSH
, wxBrush
); 
 829     REINITOBJ(wxMEDIUM_GREY_BRUSH
, wxBrush
); 
 830     REINITOBJ(wxLIGHT_GREY_BRUSH
, wxBrush
); 
 832     REINITOBJ(wxBLACK
, wxColour
); 
 833     REINITOBJ(wxWHITE
, wxColour
); 
 834     REINITOBJ(wxRED
, wxColour
); 
 835     REINITOBJ(wxBLUE
, wxColour
); 
 836     REINITOBJ(wxGREEN
, wxColour
); 
 837     REINITOBJ(wxCYAN
, wxColour
); 
 838     REINITOBJ(wxLIGHT_GREY
, wxColour
); 
 840     REINITOBJ(wxSTANDARD_CURSOR
, wxCursor
); 
 841     REINITOBJ(wxHOURGLASS_CURSOR
, wxCursor
); 
 842     REINITOBJ(wxCROSS_CURSOR
, wxCursor
); 
 844     REINITOBJ2(wxNullBitmap
, wxBitmap
); 
 845     REINITOBJ2(wxNullIcon
, wxIcon
); 
 846     REINITOBJ2(wxNullCursor
, wxCursor
); 
 847     REINITOBJ2(wxNullPen
, wxPen
); 
 848     REINITOBJ2(wxNullBrush
, wxBrush
); 
 849     REINITOBJ2(wxNullPalette
, wxPalette
); 
 850     REINITOBJ2(wxNullFont
, wxFont
); 
 851     REINITOBJ2(wxNullColour
, wxColour
); 
 853     REINITOBJ(wxTheFontList
, wxFontList
); 
 854     REINITOBJ(wxThePenList
, wxPenList
); 
 855     REINITOBJ(wxTheBrushList
, wxBrushList
); 
 856     REINITOBJ(wxTheColourDatabase
, wxColourDatabase
); 
 859     REINITOBJ2(wxDefaultValidator
, wxValidator
); 
 860     REINITOBJ2(wxNullImage
, wxImage
); 
 861     REINITOBJ2(wxNullAcceleratorTable
, wxAcceleratorTable
); 
 867 //--------------------------------------------------------------------------- 
 869 // Check for existence of a wxApp, setting an exception if there isn't one. 
 870 // This doesn't need to aquire the GIL because it should only be called from 
 871 // an %exception before the lock is released. 
 873 bool wxPyCheckForApp() { 
 874     if (wxTheApp 
!= NULL
) 
 877         PyErr_SetString(wxPyNoAppError
, "The wx.App object must be created first!"); 
 882 //--------------------------------------------------------------------------- 
 884 void wxPyUserData_dtor(wxPyUserData
* self
) { 
 885     if (! wxPyDoingCleanup
) { 
 886         wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
 887         Py_DECREF(self
->m_obj
); 
 889         wxPyEndBlockThreads(blocked
); 
 894 void wxPyClientData_dtor(wxPyClientData
* self
) { 
 895     if (! wxPyDoingCleanup
) {           // Don't do it during cleanup as Python 
 896                                         // may have already garbage collected the object... 
 897         if (self
->m_incRef
) { 
 898             wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
 899             Py_DECREF(self
->m_obj
); 
 900             wxPyEndBlockThreads(blocked
); 
 908 // This is called when an OOR controled object is being destroyed.  Although 
 909 // the C++ object is going away there is no way to force the Python object 
 910 // (and all references to it) to die too.  This causes problems (crashes) in 
 911 // wxPython when a python shadow object attempts to call a C++ method using 
 912 // the now bogus pointer... So to try and prevent this we'll do a little black 
 913 // magic and change the class of the python instance to a class that will 
 914 // raise an exception for any attempt to call methods with it.  See 
 915 // _wxPyDeadObject in _core_ex.py for the implementation of this class. 
 916 void wxPyOORClientData_dtor(wxPyOORClientData
* self
) { 
 918     static PyObject
* deadObjectClass 
= NULL
; 
 920     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
 921     if (deadObjectClass 
== NULL
) { 
 922         deadObjectClass 
= PyDict_GetItemString(wxPython_dict
, "_wxPyDeadObject"); 
 923         // TODO:  Can not wxASSERT here because inside a wxPyBeginBlock Threads, 
 924         // will lead to a deadlock when it tries to aquire the GIL again. 
 925         //wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!")); 
 926         Py_INCREF(deadObjectClass
); 
 930     // Only if there is more than one reference to the object and we are 
 931     // holding the OOR reference: 
 932     if ( !wxPyDoingCleanup 
&& self
->m_obj
->ob_refcnt 
> 1 && self
->m_incRef
) { 
 933         // bool isInstance = wxPyInstance_Check(self->m_obj); 
 935         //wxASSERT_MSG(isInstance, wxT("m_obj not an instance!?!?!")); 
 937         // Call __del__, if there is one. 
 938         PyObject
* func 
= PyObject_GetAttrString(self
->m_obj
, "__del__"); 
 940             PyObject
* rv 
= PyObject_CallMethod(self
->m_obj
, "__del__", NULL
); 
 944         if (PyErr_Occurred()) 
 945             PyErr_Clear();      // just ignore it for now 
 948         PyObject
* dict 
= PyObject_GetAttrString(self
->m_obj
, "__dict__"); 
 950             // Clear the instance's dictionary 
 953             // put the name of the old class into the instance, and then reset the 
 954             // class to be the dead class. 
 955             PyObject
* klass 
= PyObject_GetAttrString(self
->m_obj
, "__class__"); 
 956             PyObject
* name 
=  PyObject_GetAttrString(klass
, "__name__"); 
 957             PyDict_SetItemString(dict
, "_name", name
); 
 958             PyObject_SetAttrString(self
->m_obj
, "__class__",  deadObjectClass
); 
 959             //Py_INCREF(deadObjectClass); 
 965     // m_obj is DECREF'd in the base class dtor... 
 966     wxPyEndBlockThreads(blocked
); 
 970 //--------------------------------------------------------------------------- 
 971 // Stuff used by OOR to find the right wxPython class type to return and to 
 975 // The pointer type map is used when the "pointer" type name generated by SWIG 
 976 // is not the same as the shadow class name, for example wxPyTreeCtrl 
 977 // vs. wxTreeCtrl.  It needs to be referenced in Python as well as from C++, 
 978 // so we'll just make it a Python dictionary in the wx module's namespace. 
 979 // (See __wxSetDictionary) 
 980 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) { 
 981     if (! wxPyPtrTypeMap
) 
 982         wxPyPtrTypeMap 
= PyDict_New(); 
 983     PyDict_SetItemString(wxPyPtrTypeMap
, 
 985                          PyString_FromString((char*)ptrName
)); 
 991 PyObject
*  wxPyMake_wxObject(wxObject
* source
, bool setThisOwn
, bool checkEvtHandler
) { 
 992     PyObject
* target 
= NULL
; 
 993     bool      isEvtHandler 
= false; 
 996         // If it's derived from wxEvtHandler then there may 
 997         // already be a pointer to a Python object that we can use 
 999         if (checkEvtHandler 
&& wxIsKindOf(source
, wxEvtHandler
)) { 
1000             isEvtHandler 
= true; 
1001             wxEvtHandler
* eh 
= (wxEvtHandler
*)source
; 
1002             wxPyOORClientData
* data 
= (wxPyOORClientData
*)eh
->GetClientObject(); 
1004                 target 
= data
->m_obj
; 
1011             // Otherwise make it the old fashioned way by making a new shadow 
1012             // object and putting this pointer in it.  Look up the class 
1013             // heirarchy until we find a class name that is located in the 
1015             const wxClassInfo
* info   
= source
->GetClassInfo(); 
1016             wxString           name   
= info
->GetClassName(); 
1017             bool               exists 
= wxPyCheckSwigType(name
); 
1018             while (info 
&& !exists
) { 
1019                 info 
= info
->GetBaseClass1(); 
1020                 name 
= info
->GetClassName(); 
1021                 exists 
= wxPyCheckSwigType(name
); 
1024                 target 
= wxPyConstructObject((void*)source
, name
, setThisOwn
); 
1025                 if (target 
&& isEvtHandler
) 
1026                     ((wxEvtHandler
*)source
)->SetClientObject(new wxPyOORClientData(target
)); 
1028                 wxString 
msg(wxT("wxPython class not found for ")); 
1029                 msg 
+= source
->GetClassInfo()->GetClassName(); 
1030                 PyErr_SetString(PyExc_NameError
, msg
.mbc_str()); 
1034     } else {  // source was NULL so return None. 
1035         Py_INCREF(Py_None
); target 
= Py_None
; 
1041 PyObject
*  wxPyMake_wxSizer(wxSizer
* source
, bool setThisOwn
) { 
1042     PyObject
* target 
= NULL
; 
1044     if (source 
&& wxIsKindOf(source
, wxSizer
)) { 
1045         // If it's derived from wxSizer then there may already be a pointer to 
1046         // a Python object that we can use in the OOR data. 
1047         wxSizer
* sz 
= (wxSizer
*)source
; 
1048         wxPyOORClientData
* data 
= (wxPyOORClientData
*)sz
->GetClientObject(); 
1050             target 
= data
->m_obj
; 
1056         target 
= wxPyMake_wxObject(source
, setThisOwn
, false); 
1057         if (target 
!= Py_None
) 
1058             ((wxSizer
*)source
)->SetClientObject(new wxPyOORClientData(target
)); 
1064 //--------------------------------------------------------------------------- 
1067 #ifdef WXP_WITH_THREAD 
1068 #if !wxPyUSE_GIL_STATE 
1071 unsigned long wxPyGetCurrentThreadId() { 
1072     return wxThread::GetCurrentId(); 
1075 static wxPyThreadState gs_shutdownTState
; 
1078 wxPyThreadState
* wxPyGetThreadState() { 
1079     if (wxPyTMutex 
== NULL
) // Python is shutting down... 
1080         return &gs_shutdownTState
; 
1082     unsigned long ctid 
= wxPyGetCurrentThreadId(); 
1083     wxPyThreadState
* tstate 
= NULL
; 
1086     for(size_t i
=0; i 
< wxPyTStates
->GetCount(); i
++) { 
1087         wxPyThreadState
& info 
= wxPyTStates
->Item(i
); 
1088         if (info
.tid 
== ctid
) { 
1093     wxPyTMutex
->Unlock(); 
1094     wxASSERT_MSG(tstate
, wxT("PyThreadState should not be NULL!")); 
1100 void wxPySaveThreadState(PyThreadState
* tstate
) { 
1101     if (wxPyTMutex 
== NULL
) { // Python is shutting down, assume a single thread... 
1102         gs_shutdownTState
.tstate 
= tstate
; 
1105     unsigned long ctid 
= wxPyGetCurrentThreadId(); 
1107     for(size_t i
=0; i 
< wxPyTStates
->GetCount(); i
++) { 
1108         wxPyThreadState
& info 
= wxPyTStates
->Item(i
); 
1109         if (info
.tid 
== ctid
) { 
1111             if (info
.tstate 
!= tstate
) 
1112                 wxLogMessage("*** tstate mismatch!???"); 
1114             info
.tstate 
= tstate
;    // allow for transient tstates 
1115             // Normally it will never change, but apparently COM callbacks 
1116             // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient 
1117             // tstate which will then be garbage the next time we try to use 
1120             wxPyTMutex
->Unlock(); 
1124     // not found, so add it... 
1125     wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
)); 
1126     wxPyTMutex
->Unlock(); 
1134 // Calls from Python to wxWindows code are wrapped in calls to these 
1137 PyThreadState
* wxPyBeginAllowThreads() { 
1138 #ifdef WXP_WITH_THREAD 
1139     PyThreadState
* saved 
= PyEval_SaveThread();  // Py_BEGIN_ALLOW_THREADS; 
1140 #if !wxPyUSE_GIL_STATE 
1141     wxPySaveThreadState(saved
); 
1149 void wxPyEndAllowThreads(PyThreadState
* saved
) { 
1150 #ifdef WXP_WITH_THREAD 
1151     PyEval_RestoreThread(saved
);   // Py_END_ALLOW_THREADS; 
1157 // Calls from wxWindows back to Python code, or even any PyObject 
1158 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions: 
1160 wxPyBlock_t 
wxPyBeginBlockThreads() { 
1161 #ifdef WXP_WITH_THREAD 
1162 #if wxPyUSE_GIL_STATE 
1163     PyGILState_STATE state 
= PyGILState_Ensure(); 
1166     PyThreadState 
*current 
= _PyThreadState_Current
; 
1168     // Only block if there wasn't already a tstate, or if the current one is 
1169     // not the one we are wanting to change to.  This should prevent deadlock 
1170     // if there are nested calls to wxPyBeginBlockThreads 
1171     wxPyBlock_t blocked 
= false; 
1172     wxPyThreadState
* tstate 
= wxPyGetThreadState(); 
1173     if (current 
!= tstate
->tstate
) { 
1174         PyEval_RestoreThread(tstate
->tstate
); 
1185 void wxPyEndBlockThreads(wxPyBlock_t blocked
) { 
1186 #ifdef WXP_WITH_THREAD 
1187 #if wxPyUSE_GIL_STATE 
1188     PyGILState_Release(blocked
); 
1190     // Only unblock if we blocked in the last call to wxPyBeginBlockThreads. 
1191     // The value of blocked passed in needs to be the same as that returned 
1192     // from wxPyBeginBlockThreads at the same nesting level. 
1194         PyEval_SaveThread(); 
1201 //--------------------------------------------------------------------------- 
1202 // wxPyInputStream and wxPyCBInputStream methods 
1205 void wxPyInputStream::close() { 
1206     /* do nothing for now */ 
1209 void wxPyInputStream::flush() { 
1210     /* do nothing for now */ 
1213 bool wxPyInputStream::eof() { 
1215         return m_wxis
->Eof(); 
1220 wxPyInputStream::~wxPyInputStream() { 
1228 PyObject
* wxPyInputStream::read(int size
) { 
1229     PyObject
* obj 
= NULL
; 
1231     const int BUFSIZE 
= 1024; 
1233     // check if we have a real wxInputStream to work with 
1235         wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1236         PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream"); 
1237         wxPyEndBlockThreads(blocked
); 
1242         // read while bytes are available on the stream 
1243         while ( m_wxis
->CanRead() ) { 
1244             m_wxis
->Read(buf
.GetAppendBuf(BUFSIZE
), BUFSIZE
); 
1245             buf
.UngetAppendBuf(m_wxis
->LastRead()); 
1248     } else {  // Read only size number of characters 
1249         m_wxis
->Read(buf
.GetWriteBuf(size
), size
); 
1250         buf
.UngetWriteBuf(m_wxis
->LastRead()); 
1254     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1255     wxStreamError err 
= m_wxis
->GetLastError(); 
1256     if (err 
!= wxSTREAM_NO_ERROR 
&& err 
!= wxSTREAM_EOF
) { 
1257         PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream"); 
1260         // We use only strings for the streams, not unicode 
1261         obj 
= PyString_FromStringAndSize(buf
, buf
.GetDataLen()); 
1263     wxPyEndBlockThreads(blocked
); 
1268 PyObject
* wxPyInputStream::readline(int size
) { 
1269     PyObject
* obj 
= NULL
; 
1274     // check if we have a real wxInputStream to work with 
1276         wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1277         PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream"); 
1278         wxPyEndBlockThreads(blocked
); 
1282     // read until \n or byte limit reached 
1283     for (i
=ch
=0; (ch 
!= '\n') && (m_wxis
->CanRead()) && ((size 
< 0) || (i 
< size
)); i
++) { 
1284         ch 
= m_wxis
->GetC(); 
1289     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1290     wxStreamError err 
= m_wxis
->GetLastError(); 
1291     if (err 
!= wxSTREAM_NO_ERROR 
&& err 
!= wxSTREAM_EOF
) { 
1292         PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream"); 
1295         // We use only strings for the streams, not unicode 
1296         obj 
= PyString_FromStringAndSize((char*)buf
.GetData(), buf
.GetDataLen()); 
1298     wxPyEndBlockThreads(blocked
); 
1303 PyObject
* wxPyInputStream::readlines(int sizehint
) { 
1306     // check if we have a real wxInputStream to work with 
1308         wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1309         PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream"); 
1310         wxPyEndBlockThreads(blocked
); 
1315     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1316     pylist 
= PyList_New(0); 
1317     wxPyEndBlockThreads(blocked
); 
1320         wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1322         wxPyEndBlockThreads(blocked
); 
1326     // read sizehint bytes or until EOF 
1328     for (i
=0; (m_wxis
->CanRead()) && ((sizehint 
< 0) || (i 
< sizehint
));) { 
1329         PyObject
* s 
= this->readline(); 
1331             wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1333             wxPyEndBlockThreads(blocked
); 
1336         wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1337         PyList_Append(pylist
, s
); 
1338         i 
+= PyString_Size(s
); 
1339         wxPyEndBlockThreads(blocked
); 
1343     wxStreamError err 
= m_wxis
->GetLastError(); 
1344     if (err 
!= wxSTREAM_NO_ERROR 
&& err 
!= wxSTREAM_EOF
) { 
1345         wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1347         PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream"); 
1348         wxPyEndBlockThreads(blocked
); 
1356 void wxPyInputStream::seek(int offset
, int whence
) { 
1358         m_wxis
->SeekI(offset
, wxSeekMode(whence
)); 
1361 int wxPyInputStream::tell(){ 
1363         return m_wxis
->TellI(); 
1370 wxPyCBInputStream::wxPyCBInputStream(PyObject 
*r
, PyObject 
*s
, PyObject 
*t
, bool block
) 
1371     : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
) 
1374 wxPyCBInputStream::wxPyCBInputStream(const wxPyCBInputStream
& other
) 
1376     m_read  
= other
.m_read
; 
1377     m_seek  
= other
.m_seek
; 
1378     m_tell  
= other
.m_tell
; 
1379     m_block 
= other
.m_block
; 
1386 wxPyCBInputStream::~wxPyCBInputStream() { 
1387     wxPyBlock_t blocked
; 
1388     if (m_block
) blocked 
= wxPyBeginBlockThreads(); 
1392     if (m_block
) wxPyEndBlockThreads(blocked
); 
1396 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject 
*py
, bool block
) { 
1397     wxPyBlock_t blocked
; 
1398     if (block
) blocked 
= wxPyBeginBlockThreads(); 
1400     PyObject
* read 
= getMethod(py
, "read"); 
1401     PyObject
* seek 
= getMethod(py
, "seek"); 
1402     PyObject
* tell 
= getMethod(py
, "tell"); 
1405         PyErr_SetString(PyExc_TypeError
, "Not a file-like object"); 
1409         if (block
) wxPyEndBlockThreads(blocked
); 
1413     if (block
) wxPyEndBlockThreads(blocked
); 
1414     return new wxPyCBInputStream(read
, seek
, tell
, block
); 
1418 wxPyCBInputStream
* wxPyCBInputStream_create(PyObject 
*py
, bool block
) { 
1419     return wxPyCBInputStream::create(py
, block
); 
1422 wxPyCBInputStream
* wxPyCBInputStream_copy(wxPyCBInputStream
* other
) { 
1423     return new wxPyCBInputStream(*other
); 
1426 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) { 
1427     if (!PyObject_HasAttrString(py
, name
)) 
1429     PyObject
* o 
= PyObject_GetAttrString(py
, name
); 
1430     if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) { 
1438 wxFileOffset 
wxPyCBInputStream::GetLength() const { 
1439     wxPyCBInputStream
* self 
= (wxPyCBInputStream
*)this; // cast off const 
1440     if (m_seek 
&& m_tell
) { 
1441         wxFileOffset temp 
= self
->OnSysTell(); 
1442         wxFileOffset ret 
= self
->OnSysSeek(0, wxFromEnd
); 
1443         self
->OnSysSeek(temp
, wxFromStart
); 
1447         return wxInvalidOffset
; 
1451 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) { 
1455     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1456     PyObject
* arglist 
= Py_BuildValue("(i)", bufsize
); 
1457     PyObject
* result 
= PyEval_CallObject(m_read
, arglist
); 
1461     if ((result 
!= NULL
) && PyString_Check(result
)) { 
1462         o 
= PyString_Size(result
); 
1464             m_lasterror 
= wxSTREAM_EOF
; 
1467         memcpy((char*)buffer
, PyString_AsString(result
), o
);  // strings only, not unicode... 
1472         m_lasterror 
= wxSTREAM_READ_ERROR
; 
1473     wxPyEndBlockThreads(blocked
); 
1477 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) { 
1478     m_lasterror 
= wxSTREAM_WRITE_ERROR
; 
1483 wxFileOffset 
wxPyCBInputStream::OnSysSeek(wxFileOffset off
, wxSeekMode mode
) { 
1484     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1485     PyObject
* arglist 
= PyTuple_New(2); 
1487     if (sizeof(wxFileOffset
) > sizeof(long)) 
1488         // wxFileOffset is a 64-bit value... 
1489         PyTuple_SET_ITEM(arglist
, 0, PyLong_FromLongLong(off
)); 
1491         PyTuple_SET_ITEM(arglist
, 0, PyInt_FromLong(off
)); 
1493     PyTuple_SET_ITEM(arglist
, 1, PyInt_FromLong(mode
)); 
1496     PyObject
* result 
= PyEval_CallObject(m_seek
, arglist
); 
1499     wxPyEndBlockThreads(blocked
); 
1504 wxFileOffset 
wxPyCBInputStream::OnSysTell() const { 
1505     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1506     PyObject
* arglist 
= Py_BuildValue("()"); 
1507     PyObject
* result 
= PyEval_CallObject(m_tell
, arglist
); 
1510     if (result 
!= NULL
) { 
1511         if (PyLong_Check(result
)) 
1512             o 
= PyLong_AsLongLong(result
); 
1514             o 
= PyInt_AsLong(result
); 
1517     wxPyEndBlockThreads(blocked
); 
1521 //---------------------------------------------------------------------- 
1523 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
); 
1525 wxPyCallback::wxPyCallback(PyObject
* func
) { 
1530 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) { 
1531     m_func 
= other
.m_func
; 
1535 wxPyCallback::~wxPyCallback() { 
1536     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1538     wxPyEndBlockThreads(blocked
); 
1542 #define wxPy_PRECALLINIT     "_preCallInit" 
1543 #define wxPy_POSTCALLCLEANUP "_postCallCleanup" 
1545 // This function is used for all events destined for Python event handlers. 
1546 void wxPyCallback::EventThunker(wxEvent
& event
) { 
1547     wxPyCallback
*   cb 
= (wxPyCallback
*)event
.m_callbackUserData
; 
1548     PyObject
*       func 
= cb
->m_func
; 
1552     bool            checkSkip 
= false; 
1554     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1555     wxString className 
= event
.GetClassInfo()->GetClassName(); 
1557     // If the event is one of these types then pass the original 
1558     // event object instead of the one passed to us. 
1559     if ( className 
== wxT("wxPyEvent") ) { 
1560         arg 
=       ((wxPyEvent
*)&event
)->GetSelf(); 
1561         checkSkip 
= ((wxPyEvent
*)&event
)->GetCloned(); 
1563     else if ( className 
== wxT("wxPyCommandEvent") ) { 
1564         arg 
=       ((wxPyCommandEvent
*)&event
)->GetSelf(); 
1565         checkSkip 
= ((wxPyCommandEvent
*)&event
)->GetCloned(); 
1568         arg 
= wxPyConstructObject((void*)&event
, className
); 
1574         // "intern" the pre/post method names to speed up the HasAttr 
1575         static PyObject
* s_preName  
= NULL
; 
1576         static PyObject
* s_postName 
= NULL
; 
1577         if (s_preName 
== NULL
) { 
1578             s_preName  
= PyString_FromString(wxPy_PRECALLINIT
); 
1579             s_postName 
= PyString_FromString(wxPy_POSTCALLCLEANUP
); 
1582         // Check if the event object needs some preinitialization 
1583         if (PyObject_HasAttr(arg
, s_preName
)) { 
1584             result 
= PyObject_CallMethodObjArgs(arg
, s_preName
, arg
, NULL
); 
1586                 Py_DECREF(result
);   // result is ignored, but we still need to decref it 
1587                 PyErr_Clear();       // Just in case... 
1593         // Call the event handler, passing the event object 
1594         tuple 
= PyTuple_New(1); 
1595         PyTuple_SET_ITEM(tuple
, 0, arg
);  // steals ref to arg 
1596         result 
= PyEval_CallObject(func
, tuple
); 
1598             Py_DECREF(result
);   // result is ignored, but we still need to decref it 
1599             PyErr_Clear();       // Just in case... 
1604         // Check if the event object needs some post cleanup 
1605         if (PyObject_HasAttr(arg
, s_postName
)) { 
1606             result 
= PyObject_CallMethodObjArgs(arg
, s_postName
, arg
, NULL
); 
1608                 Py_DECREF(result
);   // result is ignored, but we still need to decref it 
1609                 PyErr_Clear();       // Just in case... 
1616             // if the event object was one of our special types and 
1617             // it had been cloned, then we need to extract the Skipped 
1618             // value from the original and set it in the clone. 
1619             result 
= PyObject_CallMethod(arg
, "GetSkipped", ""); 
1621                 event
.Skip(PyInt_AsLong(result
)); 
1629     wxPyEndBlockThreads(blocked
); 
1633 //---------------------------------------------------------------------- 
1635 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) { 
1637       m_self 
= other
.m_self
; 
1638       m_class 
= other
.m_class
; 
1646 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) { 
1657 #if PYTHON_API_VERSION >= 1011 
1659 // Prior to Python 2.2 PyMethod_GetClass returned the class object 
1660 // in which the method was defined.  Starting with 2.2 it returns 
1661 // "class that asked for the method" which seems totally bogus to me 
1662 // but apprently it fixes some obscure problem waiting to happen in 
1663 // Python.  Since the API was not documented Guido and the gang felt 
1664 // safe in changing it.  Needless to say that totally screwed up the 
1665 // logic below in wxPyCallbackHelper::findCallback, hence this icky 
1666 // code to find the class where the method is actually defined... 
1669 PyObject
* PyFindClassWithAttr(PyObject 
*klass
, PyObject 
*name
) 
1673     if (PyType_Check(klass
)) {      // new style classes 
1674         // This code is borrowed/adapted from _PyType_Lookup in typeobject.c 
1675         PyTypeObject
* type 
= (PyTypeObject
*)klass
; 
1676         PyObject 
*mro
, *res
, *base
, *dict
; 
1677         /* Look in tp_dict of types in MRO */ 
1679         assert(PyTuple_Check(mro
)); 
1680         n 
= PyTuple_GET_SIZE(mro
); 
1681         for (i 
= 0; i 
< n
; i
++) { 
1682             base 
= PyTuple_GET_ITEM(mro
, i
); 
1683             if (PyClass_Check(base
)) 
1684                 dict 
= ((PyClassObject 
*)base
)->cl_dict
; 
1686                 assert(PyType_Check(base
)); 
1687                 dict 
= ((PyTypeObject 
*)base
)->tp_dict
; 
1689             assert(dict 
&& PyDict_Check(dict
)); 
1690             res 
= PyDict_GetItem(dict
, name
); 
1697     else if (PyClass_Check(klass
)) { // old style classes 
1698         // This code is borrowed/adapted from class_lookup in classobject.c 
1699         PyClassObject
* cp 
= (PyClassObject
*)klass
; 
1700         PyObject 
*value 
= PyDict_GetItem(cp
->cl_dict
, name
); 
1701         if (value 
!= NULL
) { 
1702             return (PyObject
*)cp
; 
1704         n 
= PyTuple_Size(cp
->cl_bases
); 
1705         for (i 
= 0; i 
< n
; i
++) { 
1706             PyObject
* base 
= PyTuple_GetItem(cp
->cl_bases
, i
); 
1707             PyObject 
*v 
= PyFindClassWithAttr(base
, name
); 
1719 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
) 
1721     PyObject
* mgc 
= PyMethod_GET_CLASS(method
); 
1723 #if PYTHON_API_VERSION <= 1010    // prior to Python 2.2, the easy way 
1725 #else                             // 2.2 and after, the hard way... 
1727     PyObject
* nameo 
= PyString_FromString(name
); 
1728     PyObject
* klass 
= PyFindClassWithAttr(mgc
, nameo
); 
1736 bool wxPyCallbackHelper::findCallback(const char* name
) const { 
1737     wxPyCallbackHelper
* self 
= (wxPyCallbackHelper
*)this; // cast away const 
1738     self
->m_lastFound 
= NULL
; 
1740     // If the object (m_self) has an attibute of the given name... 
1741     if (m_self 
&& PyObject_HasAttrString(m_self
, (char*)name
)) { 
1742         PyObject 
*method
, *klass
; 
1743         method 
= PyObject_GetAttrString(m_self
, (char*)name
); 
1745         // ...and if that attribute is a method, and if that method's class is 
1746         // not from a base class... 
1747         if (PyMethod_Check(method
) && 
1748             (klass 
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL 
&& 
1749             ((klass 
== m_class
) || PyObject_IsSubclass(klass
, m_class
))) { 
1751             // ...then we'll save a pointer to the method so callCallback can call it. 
1752             self
->m_lastFound 
= method
; 
1758     return m_lastFound 
!= NULL
; 
1762 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const { 
1766     result 
= callCallbackObj(argTuple
); 
1767     if (result
) {                       // Assumes an integer return type... 
1768         retval 
= PyInt_AsLong(result
); 
1770         PyErr_Clear();                  // forget about it if it's not... 
1775 // Invoke the Python callable object, returning the raw PyObject return 
1776 // value.  Caller should DECREF the return value and also manage the GIL. 
1777 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const { 
1780     // Save a copy of the pointer in case the callback generates another 
1781     // callback.  In that case m_lastFound will have a different value when 
1782     // it gets back here... 
1783     PyObject
* method 
= m_lastFound
; 
1785     result 
= PyEval_CallObject(method
, argTuple
); 
1786     Py_DECREF(argTuple
); 
1795 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) { 
1796     cbh
.setSelf(self
, klass
, incref
); 
1799 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) { 
1800     return cbh
.findCallback(name
); 
1803 int  wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) { 
1804     return cbh
.callCallback(argTuple
); 
1807 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) { 
1808     return cbh
.callCallbackObj(argTuple
); 
1812 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) { 
1813     if (cbh
->m_incRef
) { 
1814         wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1815         Py_XDECREF(cbh
->m_self
); 
1816         Py_XDECREF(cbh
->m_class
); 
1817         wxPyEndBlockThreads(blocked
); 
1821 //--------------------------------------------------------------------------- 
1822 //--------------------------------------------------------------------------- 
1823 // These event classes can be derived from in Python and passed through the event 
1824 // system without losing anything.  They do this by keeping a reference to 
1825 // themselves and some special case handling in wxPyCallback::EventThunker. 
1828 wxPyEvtSelfRef::wxPyEvtSelfRef() { 
1829     //m_self = Py_None;         // **** We don't do normal ref counting to prevent 
1830     //Py_INCREF(m_self);        //      circular loops... 
1834 wxPyEvtSelfRef::~wxPyEvtSelfRef() { 
1835     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1838     wxPyEndBlockThreads(blocked
); 
1841 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) { 
1842     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1850     wxPyEndBlockThreads(blocked
); 
1853 PyObject
* wxPyEvtSelfRef::GetSelf() const { 
1859 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
); 
1860 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
); 
1863 wxPyEvent::wxPyEvent(int winid
, wxEventType commandType
) 
1864     : wxEvent(winid
, commandType
) { 
1868 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
) 
1871     SetSelf(evt
.m_self
, true); 
1875 wxPyEvent::~wxPyEvent() { 
1879 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
) 
1880     : wxCommandEvent(commandType
, id
) { 
1884 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
) 
1885     : wxCommandEvent(evt
) 
1887     SetSelf(evt
.m_self
, true); 
1891 wxPyCommandEvent::~wxPyCommandEvent() { 
1898 //--------------------------------------------------------------------------- 
1899 //--------------------------------------------------------------------------- 
1900 // Convert a wxList to a Python List, only works for lists of wxObjects 
1902 PyObject
* wxPy_ConvertList(wxListBase
* listbase
) { 
1903     wxList
*     list 
= (wxList
*)listbase
;  // this is probably bad... 
1907     wxNode
*     node 
= list
->GetFirst(); 
1909     wxPyBlock_t blocked 
= wxPyBeginBlockThreads(); 
1910     pyList 
= PyList_New(0); 
1912         wxObj 
= node
->GetData(); 
1913         pyObj 
= wxPyMake_wxObject(wxObj
,false); 
1914         PyList_Append(pyList
, pyObj
); 
1915         node 
= node
->GetNext(); 
1917     wxPyEndBlockThreads(blocked
); 
1921 //---------------------------------------------------------------------- 
1923 long wxPyGetWinHandle(wxWindow
* win
) { 
1926     return (long)win
->GetHandle(); 
1929 #if defined(__WXGTK__) || defined(__WXX11) 
1930     return (long)GetXWindow(win
); 
1934     //return (long)MAC_WXHWND(win->MacGetTopLevelWindowRef()); 
1935     return (long)win
->GetHandle(); 
1941 //---------------------------------------------------------------------- 
1942 // Some helper functions for typemaps in my_typemaps.i, so they won't be 
1943 // included in every file over and over again... 
1945 wxString
* wxString_in_helper(PyObject
* source
) { 
1946     wxString
* target 
= NULL
; 
1948     if (!PyString_Check(source
) && !PyUnicode_Check(source
)) { 
1949         PyErr_SetString(PyExc_TypeError
, "String or Unicode type required"); 
1953     PyObject
* uni 
= source
; 
1954     if (PyString_Check(source
)) { 
1955         uni 
= PyUnicode_FromEncodedObject(source
, wxPyDefaultEncoding
, "strict"); 
1956         if (PyErr_Occurred()) return NULL
; 
1958     target 
= new wxString(); 
1959     size_t len 
= PyUnicode_GET_SIZE(uni
); 
1961         PyUnicode_AsWideChar((PyUnicodeObject
*)uni
, target
->GetWriteBuf(len
), len
); 
1962         target
->UngetWriteBuf(len
); 
1965     if (PyString_Check(source
)) 
1968     // Convert to a string object if it isn't already, then to wxString 
1969     PyObject
* str 
= source
; 
1970     if (PyUnicode_Check(source
)) { 
1971         str 
= PyUnicode_AsEncodedString(source
, wxPyDefaultEncoding
, "strict"); 
1972         if (PyErr_Occurred()) return NULL
; 
1974     else if (!PyString_Check(source
)) { 
1975         str 
= PyObject_Str(source
); 
1976         if (PyErr_Occurred()) return NULL
; 
1978     char* tmpPtr
; int tmpSize
; 
1979     PyString_AsStringAndSize(str
, &tmpPtr
, &tmpSize
); 
1980     target 
= new wxString(tmpPtr
, tmpSize
); 
1982     if (!PyString_Check(source
)) 
1984 #endif // wxUSE_UNICODE 
1990 // Similar to above except doesn't use "new" and doesn't set an exception 
1991 wxString 
Py2wxString(PyObject
* source
) 
1996     // Convert to a unicode object, if not already, then to a wxString 
1997     PyObject
* uni 
= source
; 
1998     if (!PyUnicode_Check(source
)) { 
1999         uni 
= PyUnicode_FromEncodedObject(source
, wxPyDefaultEncoding
, "strict"); 
2000         if (PyErr_Occurred()) return wxEmptyString
;  // TODO:  should we PyErr_Clear? 
2002     size_t len 
= PyUnicode_GET_SIZE(uni
); 
2004         PyUnicode_AsWideChar((PyUnicodeObject
*)uni
, target
.GetWriteBuf(len
), len
); 
2005         target
.UngetWriteBuf(); 
2008     if (!PyUnicode_Check(source
)) 
2011     // Convert to a string object if it isn't already, then to wxString 
2012     PyObject
* str 
= source
; 
2013     if (PyUnicode_Check(source
)) { 
2014         str 
= PyUnicode_AsEncodedString(source
, wxPyDefaultEncoding
, "strict"); 
2015         if (PyErr_Occurred()) return wxEmptyString
;    // TODO:  should we PyErr_Clear? 
2017     else if (!PyString_Check(source
)) { 
2018         str 
= PyObject_Str(source
); 
2019         if (PyErr_Occurred()) return wxEmptyString
;    // TODO:  should we PyErr_Clear? 
2021     char* tmpPtr
; int tmpSize
; 
2022     PyString_AsStringAndSize(str
, &tmpPtr
, &tmpSize
); 
2023     target 
= wxString(tmpPtr
, tmpSize
); 
2025     if (!PyString_Check(source
)) 
2027 #endif // wxUSE_UNICODE 
2033 // Make either a Python String or Unicode object, depending on build mode 
2034 PyObject
* wx2PyString(const wxString
& src
) 
2038     str 
= PyUnicode_FromWideChar(src
.c_str(), src
.Len()); 
2040     str 
= PyString_FromStringAndSize(src
.c_str(), src
.Len()); 
2047 void wxSetDefaultPyEncoding(const char* encoding
) 
2049     strncpy(wxPyDefaultEncoding
, encoding
, DEFAULTENCODING_SIZE
); 
2052 const char* wxGetDefaultPyEncoding() 
2054     return wxPyDefaultEncoding
; 
2057 //---------------------------------------------------------------------- 
2060 byte
* byte_LIST_helper(PyObject
* source
) { 
2061     if (!PyList_Check(source
)) { 
2062         PyErr_SetString(PyExc_TypeError
, "Expected a list object."); 
2065     int count 
= PyList_Size(source
); 
2066     byte
* temp 
= new byte
[count
]; 
2068         PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array"); 
2071     for (int x
=0; x
<count
; x
++) { 
2072         PyObject
* o 
= PyList_GetItem(source
, x
); 
2073         if (! PyInt_Check(o
)) { 
2074             PyErr_SetString(PyExc_TypeError
, "Expected a list of integers."); 
2077         temp
[x
] = (byte
)PyInt_AsLong(o
); 
2083 int* int_LIST_helper(PyObject
* source
) { 
2084     if (!PyList_Check(source
)) { 
2085         PyErr_SetString(PyExc_TypeError
, "Expected a list object."); 
2088     int count 
= PyList_Size(source
); 
2089     int* temp 
= new int[count
]; 
2091         PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array"); 
2094     for (int x
=0; x
<count
; x
++) { 
2095         PyObject
* o 
= PyList_GetItem(source
, x
); 
2096         if (! PyInt_Check(o
)) { 
2097             PyErr_SetString(PyExc_TypeError
, "Expected a list of integers."); 
2100         temp
[x
] = PyInt_AsLong(o
); 
2106 long* long_LIST_helper(PyObject
* source
) { 
2107     if (!PyList_Check(source
)) { 
2108         PyErr_SetString(PyExc_TypeError
, "Expected a list object."); 
2111     int count 
= PyList_Size(source
); 
2112     long* temp 
= new long[count
]; 
2114         PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array"); 
2117     for (int x
=0; x
<count
; x
++) { 
2118         PyObject
* o 
= PyList_GetItem(source
, x
); 
2119         if (! PyInt_Check(o
)) { 
2120             PyErr_SetString(PyExc_TypeError
, "Expected a list of integers."); 
2123         temp
[x
] = PyInt_AsLong(o
); 
2129 char** string_LIST_helper(PyObject
* source
) { 
2130     if (!PyList_Check(source
)) { 
2131         PyErr_SetString(PyExc_TypeError
, "Expected a list object."); 
2134     int count 
= PyList_Size(source
); 
2135     char** temp 
= new char*[count
]; 
2137         PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array"); 
2140     for (int x
=0; x
<count
; x
++) { 
2141         PyObject
* o 
= PyList_GetItem(source
, x
); 
2142         if (! PyString_Check(o
)) { 
2143             PyErr_SetString(PyExc_TypeError
, "Expected a list of strings."); 
2146         temp
[x
] = PyString_AsString(o
); 
2151 //-------------------------------- 
2152 // Part of patch from Tim Hochberg 
2153 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) { 
2154     if (PyInt_Check(o1
) && PyInt_Check(o2
)) { 
2155         point
->x 
= PyInt_AS_LONG(o1
); 
2156         point
->y 
= PyInt_AS_LONG(o2
); 
2159     if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) { 
2160         point
->x 
= (int)PyFloat_AS_DOUBLE(o1
); 
2161         point
->y 
= (int)PyFloat_AS_DOUBLE(o2
); 
2164     if (wxPySwigInstance_Check(o1
) || wxPySwigInstance_Check(o2
)) {  // TODO: Why??? 
2165         // Disallow instances because they can cause havok 
2168     if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) { 
2169         // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2 
2170         point
->x 
= PyInt_AsLong(o1
); 
2171         point
->y 
= PyInt_AsLong(o2
); 
2178 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) { 
2179     // Putting all of the declarations here allows 
2180     // us to put the error handling all in one place. 
2183     PyObject 
*o
, *o1
, *o2
; 
2184     bool isFast 
= PyList_Check(source
) || PyTuple_Check(source
); 
2186     if (!PySequence_Check(source
)) { 
2190     // The length of the sequence is returned in count. 
2191     *count 
= PySequence_Length(source
); 
2196     temp 
= new wxPoint
[*count
]; 
2198         PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array"); 
2201     for (x
=0; x
<*count
; x
++) { 
2202         // Get an item: try fast way first. 
2204             o 
= PySequence_Fast_GET_ITEM(source
, x
); 
2207             o 
= PySequence_GetItem(source
, x
); 
2213         // Convert o to wxPoint. 
2214         if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) || 
2215             (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) { 
2216             o1 
= PySequence_Fast_GET_ITEM(o
, 0); 
2217             o2 
= PySequence_Fast_GET_ITEM(o
, 1); 
2218             if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) { 
2222         else if (wxPySwigInstance_Check(o
)) { 
2224             if (! wxPyConvertSwigPtr(o
, (void **)&pt
, wxT("wxPoint"))) { 
2229         else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) { 
2230             o1 
= PySequence_GetItem(o
, 0); 
2231             o2 
= PySequence_GetItem(o
, 1); 
2232             if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) { 
2256     PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints."); 
2260 //------------------------------ 
2263 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) { 
2264     if (!PyList_Check(source
)) { 
2265         PyErr_SetString(PyExc_TypeError
, "Expected a list object."); 
2268     int count 
= PyList_Size(source
); 
2269     wxBitmap
** temp 
= new wxBitmap
*[count
]; 
2271         PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array"); 
2274     for (int x
=0; x
<count
; x
++) { 
2275         PyObject
* o 
= PyList_GetItem(source
, x
); 
2276         if (wxPySwigInstance_Check(o
)) { 
2278             if (! wxPyConvertSwigPtr(o
, (void **) &pt
, wxT("wxBitmap"))) { 
2279                 PyErr_SetString(PyExc_TypeError
,"Expected wxBitmap."); 
2285             PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps."); 
2294 wxString
* wxString_LIST_helper(PyObject
* source
) { 
2295     if (!PyList_Check(source
)) { 
2296         PyErr_SetString(PyExc_TypeError
, "Expected a list object."); 
2299     int count 
= PyList_Size(source
); 
2300     wxString
* temp 
= new wxString
[count
]; 
2302         PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array"); 
2305     for (int x
=0; x
<count
; x
++) { 
2306         PyObject
* o 
= PyList_GetItem(source
, x
); 
2307 #if PYTHON_API_VERSION >= 1009 
2308         if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) { 
2309             PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects."); 
2313         if (! PyString_Check(o
)) { 
2314             PyErr_SetString(PyExc_TypeError
, "Expected a list of strings."); 
2319         wxString
* pStr 
= wxString_in_helper(o
); 
2327 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) { 
2328     if (!PyList_Check(source
)) { 
2329         PyErr_SetString(PyExc_TypeError
, "Expected a list object."); 
2332     int count                
= PyList_Size(source
); 
2333     wxAcceleratorEntry
* temp 
= new wxAcceleratorEntry
[count
]; 
2335         PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array"); 
2338     for (int x
=0; x
<count
; x
++) { 
2339         PyObject
* o 
= PyList_GetItem(source
, x
); 
2340         if (wxPySwigInstance_Check(o
)) { 
2341             wxAcceleratorEntry
* ae
; 
2342             if (! wxPyConvertSwigPtr(o
, (void **) &ae
, wxT("wxAcceleratorEntry"))) { 
2343                 PyErr_SetString(PyExc_TypeError
,"Expected wxAcceleratorEntry."); 
2348         else if (PyTuple_Check(o
)) { 
2349             PyObject
* o1 
= PyTuple_GetItem(o
, 0); 
2350             PyObject
* o2 
= PyTuple_GetItem(o
, 1); 
2351             PyObject
* o3 
= PyTuple_GetItem(o
, 2); 
2352             temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
)); 
2355             PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects."); 
2363 wxPen
** wxPen_LIST_helper(PyObject
* source
) { 
2364     if (!PyList_Check(source
)) { 
2365         PyErr_SetString(PyExc_TypeError
, "Expected a list object."); 
2368     int count 
= PyList_Size(source
); 
2369     wxPen
** temp 
= new wxPen
*[count
]; 
2371         PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array"); 
2374     for (int x
=0; x
<count
; x
++) { 
2375         PyObject
* o 
= PyList_GetItem(source
, x
); 
2376         if (wxPySwigInstance_Check(o
)) { 
2378             if (! wxPyConvertSwigPtr(o
, (void **)&pt
, wxT("wxPen"))) { 
2380                 PyErr_SetString(PyExc_TypeError
,"Expected wxPen."); 
2387             PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens."); 
2395 bool wxPy2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) { 
2396     bool isFast 
= PyList_Check(source
) || PyTuple_Check(source
); 
2399     if (!PySequence_Check(source
) || PySequence_Length(source
) != 2) 
2403         o1 
= PySequence_Fast_GET_ITEM(source
, 0); 
2404         o2 
= PySequence_Fast_GET_ITEM(source
, 1); 
2407         o1 
= PySequence_GetItem(source
, 0); 
2408         o2 
= PySequence_GetItem(source
, 1); 
2411     *i1 
= PyInt_AsLong(o1
); 
2412     *i2 
= PyInt_AsLong(o2
); 
2422 bool wxPy4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) { 
2423     bool isFast 
= PyList_Check(source
) || PyTuple_Check(source
); 
2424     PyObject 
*o1
, *o2
, *o3
, *o4
; 
2426     if (!PySequence_Check(source
) || PySequence_Length(source
) != 4) 
2430         o1 
= PySequence_Fast_GET_ITEM(source
, 0); 
2431         o2 
= PySequence_Fast_GET_ITEM(source
, 1); 
2432         o3 
= PySequence_Fast_GET_ITEM(source
, 2); 
2433         o4 
= PySequence_Fast_GET_ITEM(source
, 3); 
2436         o1 
= PySequence_GetItem(source
, 0); 
2437         o2 
= PySequence_GetItem(source
, 1); 
2438         o3 
= PySequence_GetItem(source
, 2); 
2439         o4 
= PySequence_GetItem(source
, 3); 
2442     *i1 
= PyInt_AsLong(o1
); 
2443     *i2 
= PyInt_AsLong(o2
); 
2444     *i3 
= PyInt_AsLong(o3
); 
2445     *i4 
= PyInt_AsLong(o4
); 
2457 //---------------------------------------------------------------------- 
2459 bool wxPySimple_typecheck(PyObject
* source
, const wxChar
* classname
, int seqLen
) 
2463     if (wxPySwigInstance_Check(source
) && 
2464         wxPyConvertSwigPtr(source
, (void **)&ptr
, classname
)) 
2468     if (PySequence_Check(source
) && PySequence_Length(source
) == seqLen
) 
2474 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) 
2476     if (source 
== Py_None
) { 
2477         **obj 
= wxSize(-1,-1); 
2480     return wxPyTwoIntItem_helper(source
, obj
, wxT("wxSize")); 
2484 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) 
2486     if (source 
== Py_None
) { 
2487         **obj 
= wxPoint(-1,-1); 
2490     return wxPyTwoIntItem_helper(source
, obj
, wxT("wxPoint")); 
2495 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) { 
2497     if (source 
== Py_None
) { 
2498         **obj 
= wxRealPoint(-1,-1); 
2502     // If source is an object instance then it may already be the right type 
2503     if (wxPySwigInstance_Check(source
)) { 
2505         if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxRealPoint"))) 
2510     // otherwise a 2-tuple of floats is expected 
2511     else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) { 
2512         PyObject
* o1 
= PySequence_GetItem(source
, 0); 
2513         PyObject
* o2 
= PySequence_GetItem(source
, 1); 
2514         if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) { 
2519         **obj 
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
)); 
2526     PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object."); 
2532 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) { 
2534     if (source 
== Py_None
) { 
2535         **obj 
= wxRect(-1,-1,-1,-1); 
2539     // If source is an object instance then it may already be the right type 
2540     if (wxPySwigInstance_Check(source
)) { 
2542         if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxRect"))) 
2547     // otherwise a 4-tuple of integers is expected 
2548     else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) { 
2549         PyObject
* o1 
= PySequence_GetItem(source
, 0); 
2550         PyObject
* o2 
= PySequence_GetItem(source
, 1); 
2551         PyObject
* o3 
= PySequence_GetItem(source
, 2); 
2552         PyObject
* o4 
= PySequence_GetItem(source
, 3); 
2553         if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) || 
2554             !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) { 
2561         **obj 
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
), 
2562                        PyInt_AsLong(o3
), PyInt_AsLong(o4
)); 
2571     PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object."); 
2577 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) { 
2579     if (source 
== Py_None
) { 
2580         **obj 
= wxNullColour
; 
2584     // If source is an object instance then it may already be the right type 
2585     if (wxPySwigInstance_Check(source
)) { 
2587         if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxColour"))) 
2592     // otherwise check for a string 
2593     else if (PyString_Check(source
) || PyUnicode_Check(source
)) { 
2594         wxString spec 
= Py2wxString(source
); 
2595         if (spec
.GetChar(0) == '#' && spec
.Length() == 7) {  // It's  #RRGGBB 
2596             long red
, green
, blue
; 
2597             red 
= green 
= blue 
= 0; 
2598             spec
.Mid(1,2).ToLong(&red
,   16); 
2599             spec
.Mid(3,2).ToLong(&green
, 16); 
2600             spec
.Mid(5,2).ToLong(&blue
,  16); 
2602             **obj 
= wxColour(red
, green
, blue
); 
2605         else {                                       // it's a colour name 
2606             **obj 
= wxColour(spec
); 
2610     // last chance: 3-tuple of integers is expected 
2611     else if (PySequence_Check(source
) && PyObject_Length(source
) == 3) { 
2612         PyObject
* o1 
= PySequence_GetItem(source
, 0); 
2613         PyObject
* o2 
= PySequence_GetItem(source
, 1); 
2614         PyObject
* o3 
= PySequence_GetItem(source
, 2); 
2615         if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) || !PyNumber_Check(o3
)) { 
2621         **obj 
= wxColour(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
)); 
2629     PyErr_SetString(PyExc_TypeError
, 
2630                     "Expected a wxColour object or a string containing a colour name or '#RRGGBB'."); 
2635 bool wxColour_typecheck(PyObject
* source
) { 
2637     if (wxPySimple_typecheck(source
, wxT("wxColour"), 3)) 
2640     if (PyString_Check(source
) || PyUnicode_Check(source
)) 
2648 bool wxPoint2D_helper(PyObject
* source
, wxPoint2D
** obj
) { 
2650     if (source 
== Py_None
) { 
2651         **obj 
= wxPoint2D(-1,-1); 
2655     // If source is an object instance then it may already be the right type 
2656     if (wxPySwigInstance_Check(source
)) { 
2658         if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxPoint2D"))) 
2663     // otherwise a length-2 sequence of floats is expected 
2664     if (PySequence_Check(source
) && PySequence_Length(source
) == 2) { 
2665         PyObject
* o1 
= PySequence_GetItem(source
, 0); 
2666         PyObject
* o2 
= PySequence_GetItem(source
, 1); 
2667         // This should really check for floats, not numbers -- but that would break code. 
2668         if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) { 
2673         **obj 
= wxPoint2D(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
)); 
2679     PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxPoint2D object."); 
2684 //---------------------------------------------------------------------- 
2686 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) { 
2688     PyObject
* list 
= PyList_New(0); 
2689     for (size_t i
=0; i 
< arr
.GetCount(); i
++) { 
2691         PyObject
* str 
= PyUnicode_FromWideChar(arr
[i
].c_str(), arr
[i
].Len()); 
2693         PyObject
* str 
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len()); 
2695         PyList_Append(list
, str
); 
2702 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) { 
2704     PyObject
* list 
= PyList_New(0); 
2705     for (size_t i
=0; i 
< arr
.GetCount(); i
++) { 
2706         PyObject
* number 
= PyInt_FromLong(arr
[i
]); 
2707         PyList_Append(list
, number
); 
2714 //---------------------------------------------------------------------- 
2715 //----------------------------------------------------------------------