1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Helper functions/classes for the wxPython extension module
9 // Copyright: (c) 1998 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
22 #include <wx/msw/private.h>
25 #undef LoadAccelerators
29 #include <wx/module.h>
32 //---------------------------------------------------------------------------
34 //wxHashTable* wxPyWindows = NULL;
37 wxPoint wxPyDefaultPosition
; //wxDefaultPosition);
38 wxSize wxPyDefaultSize
; //wxDefaultSize);
39 wxString
wxPyEmptyStr("");
43 #ifdef __WXMSW__ // If building for win32...
44 //----------------------------------------------------------------------
45 // This gets run when the DLL is loaded. We just need to save a handle.
46 //----------------------------------------------------------------------
49 HINSTANCE hinstDLL
, // handle to DLL module
50 DWORD fdwReason
, // reason for calling function
51 LPVOID lpvReserved
// reserved
54 wxSetInstance(hinstDLL
);
59 //----------------------------------------------------------------------
60 // Class for implementing the wxp main application shell.
61 //----------------------------------------------------------------------
63 wxPyApp
*wxPythonApp
= NULL
; // Global instance of application object
67 // printf("**** ctor\n");
71 // printf("**** dtor\n");
75 // This one isn't acutally called... See __wxStart()
76 bool wxPyApp::OnInit(void) {
80 int wxPyApp::MainLoop(void) {
81 int retval
= wxApp::MainLoop();
87 void wxPyApp::AfterMainLoop(void) {
88 // more stuff from wxEntry...
90 if (wxPythonApp
->GetTopWindow()) {
91 // Forcibly delete the window.
92 if (wxPythonApp
->GetTopWindow()->IsKindOf(CLASSINFO(wxFrame
)) ||
93 wxPythonApp
->GetTopWindow()->IsKindOf(CLASSINFO(wxDialog
))) {
95 wxPythonApp
->GetTopWindow()->Close(TRUE
);
96 wxPythonApp
->DeletePendingObjects();
99 delete wxPythonApp
->GetTopWindow();
100 wxPythonApp
->SetTopWindow(NULL
);
104 wxPythonApp
->DeletePendingObjects();
107 wxPythonApp
->OnExit();
109 // delete wxPythonApp;
113 //---------------------------------------------------------------------
114 // a few native methods to add to the module
115 //----------------------------------------------------------------------
118 // This is where we pick up the first part of the wxEntry functionality...
119 // The rest is in __wxStart and AfterMainLoop. This function is called when
120 // wxpc is imported. (Before there is a wxApp object.)
123 // Bail out if there is already windows created. This means that the
124 // toolkit has already been initialized, as in embedding wxPython in
125 // a C++ wxWindows app.
126 if (wxTopLevelWindows
.Number() > 0)
134 PyObject
* sysargv
= PySys_GetObject("argv");
135 int argc
= PyList_Size(sysargv
);
136 char** argv
= new char*[argc
+1];
138 for(x
=0; x
<argc
; x
++)
139 argv
[x
] = PyString_AsString(PyList_GetItem(sysargv
, x
));
143 gtk_init( &argc
, &argv
);
146 wxApp::Initialize(); // may return FALSE. Should we check?
152 #ifdef WXP_WITH_THREAD
153 PyThreadState
* wxPyEventThreadState
= NULL
;
154 bool wxPyInEvent
= false;
156 static char* __nullArgv
[1] = { 0 };
158 // Start the user application, user App's OnInit method is a parameter here
159 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
161 PyObject
* onInitFunc
= NULL
;
166 #ifdef WXP_WITH_THREAD
167 wxPyEventThreadState
= PyThreadState_Get();
170 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
173 if (wxTopLevelWindows
.Number() > 0) {
174 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
179 // This is the next part of the wxEntry functionality...
180 wxPythonApp
->argc
= 0;
181 wxPythonApp
->argv
= NULL
;
182 wxPythonApp
->OnInitGui();
185 // Call the Python App's OnInit function
186 arglist
= PyTuple_New(0);
188 // Py_END_ALLOW_THREADS; **** __wxStart was called from Python,
189 // should already have the lock
190 result
= PyEval_CallObject(onInitFunc
, arglist
);
191 // Py_BEGIN_ALLOW_THREADS;
198 if (! PyInt_Check(result
)) {
199 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
202 bResult
= PyInt_AS_LONG(result
);
204 wxPythonApp
->DeletePendingObjects();
205 wxPythonApp
->OnExit();
207 PyErr_SetString(PyExc_SystemExit
, "OnInit returned false, exiting...");
212 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
223 PyObject
* wxPython_dict
;
224 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
227 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
230 if (!PyDict_Check(wxPython_dict
)) {
231 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
235 #define wxPlatform "__WXMOTIF__"
238 #define wxPlatform "__WXQT__"
241 #define wxPlatform "__WXGTK__"
243 #if defined(__WIN32__) || defined(__WXMSW__)
244 #define wxPlatform "__WXMSW__"
247 #define wxPlatform "__WXMAC__"
250 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
257 //---------------------------------------------------------------------------
261 PyObject
* wxPyConstructObject(void* ptr
, char* className
)
263 char buff
[64]; // should always be big enough...
266 sprintf(buff
, "_%s_p", className
);
267 SWIG_MakePtr(swigptr
, ptr
, buff
);
269 sprintf(buff
, "%sPtr", className
);
270 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
276 PyObject
* arg
= Py_BuildValue("(s)", swigptr
);
277 PyObject
* obj
= PyInstance_New(classobj
, arg
, NULL
);
285 wxPyCallback::wxPyCallback(PyObject
* func
) {
290 wxPyCallback::~wxPyCallback() {
291 #ifdef WXP_WITH_THREAD
293 PyEval_RestoreThread(wxPyEventThreadState
);
298 #ifdef WXP_WITH_THREAD
307 // This function is used for all events destined for Python event handlers.
308 void wxPyCallback::EventThunker(wxEvent
& event
) {
309 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
310 PyObject
* func
= cb
->m_func
;
316 #ifdef WXP_WITH_THREAD
317 PyEval_RestoreThread(wxPyEventThreadState
);
320 arg
= wxPyConstructObject((void*)&event
, event
.GetClassInfo()->GetClassName());
322 tuple
= PyTuple_New(1);
323 PyTuple_SET_ITEM(tuple
, 0, arg
);
324 result
= PyEval_CallObject(func
, tuple
);
332 #ifdef WXP_WITH_THREAD
339 //---------------------------------------------------------------------------
341 wxPyMenu::wxPyMenu(const wxString
& title
, PyObject
* _func
)
342 : wxMenu(title
, (wxFunction
)(func
? MenuCallback
: NULL
)), func(0) {
350 wxPyMenu::~wxPyMenu() {
351 #ifdef WXP_WITH_THREAD
353 PyEval_RestoreThread(wxPyEventThreadState
);
359 #ifdef WXP_WITH_THREAD
366 void wxPyMenu::MenuCallback(wxMenu
& menu
, wxCommandEvent
& evt
) {
373 #ifdef WXP_WITH_THREAD
374 PyEval_RestoreThread(wxPyEventThreadState
);
377 evtobj
= wxPyConstructObject((void*)&evt
, "wxCommandEvent");
378 menuobj
= wxPyConstructObject((void*)&menu
, "wxMenu");
379 if (PyErr_Occurred()) {
380 // bail out if a problem
384 // Now call the callback...
385 func
= ((wxPyMenu
*)&menu
)->func
;
386 args
= PyTuple_New(2);
387 PyTuple_SET_ITEM(args
, 0, menuobj
);
388 PyTuple_SET_ITEM(args
, 1, evtobj
);
389 res
= PyEval_CallObject(func
, args
);
391 Py_XDECREF(res
); /* In case res is a NULL pointer */
393 #ifdef WXP_WITH_THREAD
401 //---------------------------------------------------------------------------
403 wxPyTimer::wxPyTimer(PyObject
* callback
) {
408 wxPyTimer::~wxPyTimer() {
409 #ifdef WXP_WITH_THREAD
411 PyEval_RestoreThread(wxPyEventThreadState
);
416 #ifdef WXP_WITH_THREAD
422 void wxPyTimer::Notify() {
423 #ifdef WXP_WITH_THREAD
424 PyEval_RestoreThread(wxPyEventThreadState
);
428 PyObject
* args
= Py_BuildValue("()");
430 result
= PyEval_CallObject(func
, args
);
438 #ifdef WXP_WITH_THREAD
445 //----------------------------------------------------------------------
447 IMPLEMENT_DYNAMIC_CLASS(wxPyEvent
, wxCommandEvent
)
449 wxPyEvent::wxPyEvent(wxEventType commandType
, PyObject
* userData
)
450 : wxCommandEvent(commandType
), m_userData(Py_None
)
452 m_userData
= userData
;
453 if (m_userData
!= Py_None
) {
454 Py_INCREF(m_userData
);
459 wxPyEvent::~wxPyEvent() {
460 #ifdef WXP_WITH_THREAD
462 PyEval_RestoreThread(wxPyEventThreadState
);
464 if (m_userData
!= Py_None
) {
465 Py_DECREF(m_userData
);
466 m_userData
= Py_None
;
468 #ifdef WXP_WITH_THREAD
475 void wxPyEvent::SetUserData(PyObject
* userData
) {
476 if (m_userData
!= Py_None
) {
477 Py_DECREF(m_userData
);
478 m_userData
= Py_None
;
480 m_userData
= userData
;
481 if (m_userData
!= Py_None
) {
482 Py_INCREF(m_userData
);
487 PyObject
* wxPyEvent::GetUserData() {
491 //----------------------------------------------------------------------
492 //----------------------------------------------------------------------
493 // Some helper functions for typemaps in my_typemaps.i, so they won't be
494 // imcluded in every file...
497 byte
* byte_LIST_helper(PyObject
* source
) {
498 if (!PyList_Check(source
)) {
499 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
502 int count
= PyList_Size(source
);
503 byte
* temp
= new byte
[count
];
505 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
508 for (int x
=0; x
<count
; x
++) {
509 PyObject
* o
= PyList_GetItem(source
, x
);
510 if (! PyInt_Check(o
)) {
511 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
514 temp
[x
] = (byte
)PyInt_AsLong(o
);
520 int* int_LIST_helper(PyObject
* source
) {
521 if (!PyList_Check(source
)) {
522 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
525 int count
= PyList_Size(source
);
526 int* temp
= new int[count
];
528 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
531 for (int x
=0; x
<count
; x
++) {
532 PyObject
* o
= PyList_GetItem(source
, x
);
533 if (! PyInt_Check(o
)) {
534 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
537 temp
[x
] = PyInt_AsLong(o
);
543 long* long_LIST_helper(PyObject
* source
) {
544 if (!PyList_Check(source
)) {
545 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
548 int count
= PyList_Size(source
);
549 long* temp
= new long[count
];
551 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
554 for (int x
=0; x
<count
; x
++) {
555 PyObject
* o
= PyList_GetItem(source
, x
);
556 if (! PyInt_Check(o
)) {
557 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
560 temp
[x
] = PyInt_AsLong(o
);
566 char** string_LIST_helper(PyObject
* source
) {
567 if (!PyList_Check(source
)) {
568 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
571 int count
= PyList_Size(source
);
572 char** temp
= new char*[count
];
574 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
577 for (int x
=0; x
<count
; x
++) {
578 PyObject
* o
= PyList_GetItem(source
, x
);
579 if (! PyString_Check(o
)) {
580 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
583 temp
[x
] = PyString_AsString(o
);
590 wxPoint
* wxPoint_LIST_helper(PyObject
* source
) {
591 if (!PyList_Check(source
)) {
592 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
595 int count
= PyList_Size(source
);
596 wxPoint
* temp
= new wxPoint
[count
];
598 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
601 for (int x
=0; x
<count
; x
++) {
602 PyObject
* o
= PyList_GetItem(source
, x
);
603 if (PyString_Check(o
)) {
604 char* st
= PyString_AsString(o
);
606 if (SWIG_GetPtr(st
,(void **) &pt
,"_wxPoint_p")) {
607 PyErr_SetString(PyExc_TypeError
,"Expected _wxPoint_p.");
612 else if (PyTuple_Check(o
)) {
613 PyObject
* o1
= PyTuple_GetItem(o
, 0);
614 PyObject
* o2
= PyTuple_GetItem(o
, 1);
616 temp
[x
].x
= PyInt_AsLong(o1
);
617 temp
[x
].y
= PyInt_AsLong(o2
);
620 PyErr_SetString(PyExc_TypeError
, "Expected a list of 2-tuples or wxPoints.");
628 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
629 if (!PyList_Check(source
)) {
630 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
633 int count
= PyList_Size(source
);
634 wxBitmap
** temp
= new wxBitmap
*[count
];
636 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
639 for (int x
=0; x
<count
; x
++) {
640 PyObject
* o
= PyList_GetItem(source
, x
);
641 if (PyString_Check(o
)) {
642 char* st
= PyString_AsString(o
);
644 if (SWIG_GetPtr(st
,(void **) &pt
,"_wxBitmap_p")) {
645 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
651 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
660 wxString
* wxString_LIST_helper(PyObject
* source
) {
661 if (!PyList_Check(source
)) {
662 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
665 int count
= PyList_Size(source
);
666 wxString
* temp
= new wxString
[count
];
668 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
671 for (int x
=0; x
<count
; x
++) {
672 PyObject
* o
= PyList_GetItem(source
, x
);
673 if (! PyString_Check(o
)) {
674 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
677 temp
[x
] = PyString_AsString(o
);
683 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
684 if (!PyList_Check(source
)) {
685 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
688 int count
= PyList_Size(source
);
689 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
691 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
694 for (int x
=0; x
<count
; x
++) {
695 PyObject
* o
= PyList_GetItem(source
, x
);
696 if (PyString_Check(o
)) {
697 char* st
= PyString_AsString(o
);
698 wxAcceleratorEntry
* ae
;
699 if (SWIG_GetPtr(st
,(void **) &ae
,"_wxAcceleratorEntry_p")) {
700 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
705 else if (PyTuple_Check(o
)) {
706 PyObject
* o1
= PyTuple_GetItem(o
, 0);
707 PyObject
* o2
= PyTuple_GetItem(o
, 1);
708 PyObject
* o3
= PyTuple_GetItem(o
, 2);
710 temp
[x
].m_flags
= PyInt_AsLong(o1
);
711 temp
[x
].m_keyCode
= PyInt_AsLong(o2
);
712 temp
[x
].m_command
= PyInt_AsLong(o3
);
715 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
724 //----------------------------------------------------------------------
725 //----------------------------------------------------------------------
727 wxPyCallbackHelper::wxPyCallbackHelper() {
733 wxPyCallbackHelper::~wxPyCallbackHelper() {
734 #ifdef WXP_WITH_THREAD
735 PyEval_RestoreThread(wxPyEventThreadState
);
740 #ifdef WXP_WITH_THREAD
745 void wxPyCallbackHelper::setSelf(PyObject
* self
) {
752 bool wxPyCallbackHelper::findCallback(const wxString
& name
) {
754 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
.c_str()))
755 m_lastFound
= PyObject_GetAttrString(m_self
, (char*)name
.c_str());
757 return m_lastFound
!= NULL
;
761 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) {
765 result
= callCallbackObj(argTuple
);
766 if (result
) { // Assumes an integer return type...
767 retval
= PyInt_AsLong(result
);
769 PyErr_Clear(); // forget about it if it's not...
771 #ifdef WXP_WITH_THREAD
777 // Invoke the Python callable object, returning the raw PyObject return
778 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
779 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) {
780 #ifdef WXP_WITH_THREAD
781 PyEval_RestoreThread(wxPyEventThreadState
);
785 result
= PyEval_CallObject(m_lastFound
, argTuple
);
796 //----------------------------------------------------------------------
797 //----------------------------------------------------------------------
798 //----------------------------------------------------------------------