]>
Commit | Line | Data |
---|---|---|
7bf85405 RD |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: helpers.cpp | |
03e9bead | 3 | // Purpose: Helper functions/classes for the wxPython extension module |
7bf85405 RD |
4 | // |
5 | // Author: Robin Dunn | |
6 | // | |
7 | // Created: 7/1/97 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 by Total Control Software | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
21f4bf45 | 13 | |
08127323 | 14 | #ifdef __WXGTK__ |
efc5f224 | 15 | #include <gtk/gtk.h> |
08127323 RD |
16 | #endif |
17 | ||
7bf85405 RD |
18 | #undef DEBUG |
19 | #include <Python.h> | |
20 | #include "helpers.h" | |
af309447 RD |
21 | #ifdef __WXMSW__ |
22 | #include <wx/msw/private.h> | |
23 | #undef FindWindow | |
24 | #undef GetCharWidth | |
25 | #undef LoadAccelerators | |
26 | #undef GetClassInfo | |
27 | #undef GetClassName | |
28 | #endif | |
dd9a3de8 | 29 | #include <wx/module.h> |
7bf85405 | 30 | |
cf694132 | 31 | |
7bf85405 RD |
32 | //--------------------------------------------------------------------------- |
33 | ||
34 | //wxHashTable* wxPyWindows = NULL; | |
35 | ||
36 | ||
37 | wxPoint wxPyDefaultPosition; //wxDefaultPosition); | |
38 | wxSize wxPyDefaultSize; //wxDefaultSize); | |
39 | wxString wxPyEmptyStr(""); | |
40 | ||
41 | ||
42 | ||
21f4bf45 | 43 | #ifdef __WXMSW__ // If building for win32... |
21f4bf45 RD |
44 | //---------------------------------------------------------------------- |
45 | // This gets run when the DLL is loaded. We just need to save a handle. | |
46 | //---------------------------------------------------------------------- | |
9c039d08 | 47 | |
21f4bf45 RD |
48 | BOOL WINAPI DllMain( |
49 | HINSTANCE hinstDLL, // handle to DLL module | |
50 | DWORD fdwReason, // reason for calling function | |
51 | LPVOID lpvReserved // reserved | |
52 | ) | |
53 | { | |
af309447 | 54 | wxSetInstance(hinstDLL); |
21f4bf45 RD |
55 | return 1; |
56 | } | |
57 | #endif | |
58 | ||
7bf85405 RD |
59 | //---------------------------------------------------------------------- |
60 | // Class for implementing the wxp main application shell. | |
61 | //---------------------------------------------------------------------- | |
62 | ||
63 | wxPyApp *wxPythonApp = NULL; // Global instance of application object | |
64 | ||
65 | ||
cf694132 RD |
66 | wxPyApp::wxPyApp() { |
67 | // printf("**** ctor\n"); | |
68 | } | |
69 | ||
70 | wxPyApp::~wxPyApp() { | |
71 | // printf("**** dtor\n"); | |
72 | } | |
73 | ||
74 | ||
7bf85405 RD |
75 | // This one isn't acutally called... See __wxStart() |
76 | bool wxPyApp::OnInit(void) { | |
77 | return false; | |
78 | } | |
79 | ||
80 | int wxPyApp::MainLoop(void) { | |
81 | int retval = wxApp::MainLoop(); | |
8bf5d46e RD |
82 | //# AfterMainLoop(); |
83 | wxPythonApp->OnExit(); //# | |
7bf85405 RD |
84 | return retval; |
85 | } | |
86 | ||
af309447 | 87 | |
8bf5d46e RD |
88 | //# void wxPyApp::AfterMainLoop(void) { |
89 | // // more stuff from wxEntry... | |
0d6f9504 | 90 | |
8bf5d46e RD |
91 | // if (wxPythonApp->GetTopWindow()) { |
92 | // // Forcibly delete the window. | |
93 | // if (wxPythonApp->GetTopWindow()->IsKindOf(CLASSINFO(wxFrame)) || | |
94 | // wxPythonApp->GetTopWindow()->IsKindOf(CLASSINFO(wxDialog))) { | |
7bf85405 | 95 | |
8bf5d46e RD |
96 | // wxPythonApp->GetTopWindow()->Close(TRUE); |
97 | // wxPythonApp->DeletePendingObjects(); | |
98 | // } | |
99 | // else { | |
100 | // delete wxPythonApp->GetTopWindow(); | |
101 | // wxPythonApp->SetTopWindow(NULL); | |
102 | // } | |
103 | // } | |
104 | // #ifdef __WXGTK__ | |
105 | // wxPythonApp->DeletePendingObjects(); | |
106 | // #endif | |
7bf85405 | 107 | |
8bf5d46e RD |
108 | // wxPythonApp->OnExit(); |
109 | // wxApp::CleanUp(); | |
110 | // // delete wxPythonApp; | |
111 | // } | |
7bf85405 RD |
112 | |
113 | ||
fb5e0af0 | 114 | //--------------------------------------------------------------------- |
7bf85405 RD |
115 | // a few native methods to add to the module |
116 | //---------------------------------------------------------------------- | |
117 | ||
118 | ||
0d6f9504 | 119 | // This is where we pick up the first part of the wxEntry functionality... |
4464bbee | 120 | // The rest is in __wxStart and AfterMainLoop. This function is called when |
8bf5d46e | 121 | // wxcmodule is imported. (Before there is a wxApp object.) |
0d6f9504 | 122 | void __wxPreStart() |
7bf85405 | 123 | { |
9d8bd15f RD |
124 | #ifdef WXP_WITH_THREAD |
125 | PyEval_InitThreads(); | |
126 | #endif | |
127 | ||
0d6f9504 RD |
128 | // Bail out if there is already windows created. This means that the |
129 | // toolkit has already been initialized, as in embedding wxPython in | |
130 | // a C++ wxWindows app. | |
131 | if (wxTopLevelWindows.Number() > 0) | |
132 | return; | |
7bf85405 | 133 | |
7bf85405 | 134 | #ifdef __WXMSW__ |
9c039d08 | 135 | wxApp::Initialize(); |
7bf85405 | 136 | #endif |
21f4bf45 | 137 | |
08127323 | 138 | #ifdef __WXGTK__ |
fb5e0af0 RD |
139 | PyObject* sysargv = PySys_GetObject("argv"); |
140 | int argc = PyList_Size(sysargv); | |
141 | char** argv = new char*[argc+1]; | |
142 | int x; | |
143 | for(x=0; x<argc; x++) | |
144 | argv[x] = PyString_AsString(PyList_GetItem(sysargv, x)); | |
145 | argv[argc] = NULL; | |
146 | ||
08127323 | 147 | gtk_set_locale(); |
8f17924e | 148 | #if wxUSE_WCHAR_T |
8bf5d46e | 149 | if (!wxOKlibc()) wxConvCurrent = &wxConvLocal; |
8f17924e RD |
150 | #else |
151 | if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL; | |
152 | #endif | |
0d6f9504 | 153 | gtk_init( &argc, &argv ); |
8bf5d46e | 154 | wxSetDetectableAutoRepeat( TRUE ); |
0d6f9504 | 155 | delete [] argv; |
7bf85405 | 156 | |
08127323 | 157 | wxApp::Initialize(); // may return FALSE. Should we check? |
7bf85405 RD |
158 | #endif |
159 | ||
0d6f9504 RD |
160 | } |
161 | ||
162 | ||
cf694132 | 163 | #ifdef WXP_WITH_THREAD |
bb0054cd | 164 | PyThreadState* wxPyEventThreadState = NULL; |
cf694132 | 165 | #endif |
0d6f9504 RD |
166 | static char* __nullArgv[1] = { 0 }; |
167 | ||
8bf5d46e RD |
168 | |
169 | ||
0d6f9504 RD |
170 | // Start the user application, user App's OnInit method is a parameter here |
171 | PyObject* __wxStart(PyObject* /* self */, PyObject* args) | |
172 | { | |
173 | PyObject* onInitFunc = NULL; | |
174 | PyObject* arglist; | |
175 | PyObject* result; | |
176 | long bResult; | |
177 | ||
cf694132 | 178 | #ifdef WXP_WITH_THREAD |
bb0054cd | 179 | wxPyEventThreadState = PyThreadState_Get(); |
cf694132 | 180 | #endif |
0d6f9504 RD |
181 | |
182 | if (!PyArg_ParseTuple(args, "O", &onInitFunc)) | |
183 | return NULL; | |
184 | ||
185 | if (wxTopLevelWindows.Number() > 0) { | |
186 | PyErr_SetString(PyExc_TypeError, "Only 1 wxApp per process!"); | |
187 | return NULL; | |
188 | } | |
189 | ||
190 | ||
191 | // This is the next part of the wxEntry functionality... | |
192 | wxPythonApp->argc = 0; | |
cf694132 | 193 | wxPythonApp->argv = NULL; |
0d6f9504 RD |
194 | wxPythonApp->OnInitGui(); |
195 | ||
7bf85405 RD |
196 | |
197 | // Call the Python App's OnInit function | |
198 | arglist = PyTuple_New(0); | |
199 | result = PyEval_CallObject(onInitFunc, arglist); | |
8bf5d46e RD |
200 | if (!result) { // an exception was raised. |
201 | return NULL; | |
7bf85405 RD |
202 | } |
203 | ||
204 | if (! PyInt_Check(result)) { | |
205 | PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value"); | |
206 | return NULL; | |
207 | } | |
208 | bResult = PyInt_AS_LONG(result); | |
209 | if (! bResult) { | |
7bf85405 RD |
210 | PyErr_SetString(PyExc_SystemExit, "OnInit returned false, exiting..."); |
211 | return NULL; | |
212 | } | |
213 | ||
13dfc243 | 214 | #ifdef __WXGTK__ |
fb5e0af0 | 215 | wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0); |
13dfc243 | 216 | #endif |
fb5e0af0 | 217 | |
7bf85405 RD |
218 | Py_INCREF(Py_None); |
219 | return Py_None; | |
220 | } | |
221 | ||
222 | ||
7bf85405 RD |
223 | |
224 | ||
225 | ||
226 | PyObject* wxPython_dict; | |
227 | PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args) | |
228 | { | |
229 | ||
230 | if (!PyArg_ParseTuple(args, "O", &wxPython_dict)) | |
231 | return NULL; | |
232 | ||
233 | if (!PyDict_Check(wxPython_dict)) { | |
234 | PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!"); | |
235 | return NULL; | |
236 | } | |
237 | #ifdef __WXMOTIF__ | |
21f4bf45 RD |
238 | #define wxPlatform "__WXMOTIF__" |
239 | #endif | |
240 | #ifdef __WXQT__ | |
241 | #define wxPlatform "__WXQT__" | |
7bf85405 RD |
242 | #endif |
243 | #ifdef __WXGTK__ | |
21f4bf45 | 244 | #define wxPlatform "__WXGTK__" |
7bf85405 RD |
245 | #endif |
246 | #if defined(__WIN32__) || defined(__WXMSW__) | |
fb5e0af0 | 247 | #define wxPlatform "__WXMSW__" |
7bf85405 RD |
248 | #endif |
249 | #ifdef __WXMAC__ | |
21f4bf45 | 250 | #define wxPlatform "__WXMAC__" |
7bf85405 RD |
251 | #endif |
252 | ||
253 | PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform)); | |
254 | ||
255 | Py_INCREF(Py_None); | |
256 | return Py_None; | |
257 | } | |
258 | ||
259 | ||
260 | //--------------------------------------------------------------------------- | |
261 | ||
65dd82cb | 262 | PyObject* wxPyConstructObject(void* ptr, const char* className) { |
9c039d08 | 263 | char buff[64]; // should always be big enough... |
7bf85405 RD |
264 | char swigptr[64]; |
265 | ||
266 | sprintf(buff, "_%s_p", className); | |
267 | SWIG_MakePtr(swigptr, ptr, buff); | |
268 | ||
269 | sprintf(buff, "%sPtr", className); | |
270 | PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff); | |
271 | if (! classobj) { | |
272 | Py_INCREF(Py_None); | |
273 | return Py_None; | |
274 | } | |
275 | ||
276 | PyObject* arg = Py_BuildValue("(s)", swigptr); | |
277 | PyObject* obj = PyInstance_New(classobj, arg, NULL); | |
278 | Py_DECREF(arg); | |
279 | ||
280 | return obj; | |
281 | } | |
282 | ||
d559219f | 283 | //--------------------------------------------------------------------------- |
7bf85405 | 284 | |
99a49d3e | 285 | static unsigned int _wxPyNestCount = 0; |
cf694132 | 286 | |
99a49d3e RD |
287 | static PyThreadState* myPyThreadState_Get() { |
288 | PyThreadState* current; | |
289 | current = PyThreadState_Swap(NULL); | |
290 | PyThreadState_Swap(current); | |
291 | return current; | |
292 | } | |
d559219f | 293 | |
99a49d3e RD |
294 | |
295 | HELPEREXPORT bool wxPyRestoreThread() { | |
d559219f RD |
296 | // NOTE: The Python API docs state that if a thread already has the |
297 | // interpreter lock and calls PyEval_RestoreThread again a deadlock | |
99a49d3e RD |
298 | // occurs, so I put in this code as a guard condition since there are |
299 | // many possibilites for nested events and callbacks in wxPython. If | |
300 | // The current thread is our thread, then we can assume that we | |
2abc0a0f | 301 | // already have the lock. (I hope!) |
d559219f | 302 | // |
cf694132 | 303 | #ifdef WXP_WITH_THREAD |
99a49d3e RD |
304 | _wxPyNestCount += 1; |
305 | if (wxPyEventThreadState != myPyThreadState_Get()) { | |
306 | PyEval_RestoreThread(wxPyEventThreadState); | |
307 | return TRUE; | |
308 | } | |
309 | else | |
cf694132 | 310 | #endif |
d559219f RD |
311 | return FALSE; |
312 | } | |
cf694132 | 313 | |
cf694132 | 314 | |
d559219f | 315 | HELPEREXPORT void wxPySaveThread(bool doSave) { |
cf694132 | 316 | #ifdef WXP_WITH_THREAD |
99a49d3e | 317 | if (doSave) { |
9d8bd15f | 318 | wxPyEventThreadState = PyEval_SaveThread(); |
99a49d3e RD |
319 | } |
320 | _wxPyNestCount -= 1; | |
cf694132 RD |
321 | #endif |
322 | } | |
323 | ||
d559219f RD |
324 | //--------------------------------------------------------------------------- |
325 | ||
326 | ||
2f90df85 RD |
327 | IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject); |
328 | ||
d559219f RD |
329 | wxPyCallback::wxPyCallback(PyObject* func) { |
330 | m_func = func; | |
331 | Py_INCREF(m_func); | |
332 | } | |
333 | ||
2f90df85 RD |
334 | wxPyCallback::wxPyCallback(const wxPyCallback& other) { |
335 | m_func = other.m_func; | |
336 | Py_INCREF(m_func); | |
337 | } | |
338 | ||
d559219f RD |
339 | wxPyCallback::~wxPyCallback() { |
340 | bool doSave = wxPyRestoreThread(); | |
341 | Py_DECREF(m_func); | |
342 | wxPySaveThread(doSave); | |
343 | } | |
344 | ||
cf694132 RD |
345 | |
346 | ||
7bf85405 RD |
347 | // This function is used for all events destined for Python event handlers. |
348 | void wxPyCallback::EventThunker(wxEvent& event) { | |
349 | wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData; | |
350 | PyObject* func = cb->m_func; | |
351 | PyObject* result; | |
352 | PyObject* arg; | |
353 | PyObject* tuple; | |
354 | ||
cf694132 | 355 | |
d559219f | 356 | bool doSave = wxPyRestoreThread(); |
65dd82cb RD |
357 | wxString className = event.GetClassInfo()->GetClassName(); |
358 | ||
359 | if (className == "wxPyEvent") | |
360 | arg = ((wxPyEvent*)&event)->GetSelf(); | |
361 | else if (className == "wxPyCommandEvent") | |
362 | arg = ((wxPyCommandEvent*)&event)->GetSelf(); | |
363 | else | |
364 | arg = wxPyConstructObject((void*)&event, className); | |
7bf85405 RD |
365 | |
366 | tuple = PyTuple_New(1); | |
367 | PyTuple_SET_ITEM(tuple, 0, arg); | |
368 | result = PyEval_CallObject(func, tuple); | |
7bf85405 RD |
369 | Py_DECREF(tuple); |
370 | if (result) { | |
371 | Py_DECREF(result); | |
372 | PyErr_Clear(); | |
373 | } else { | |
374 | PyErr_Print(); | |
375 | } | |
d559219f | 376 | wxPySaveThread(doSave); |
7bf85405 RD |
377 | } |
378 | ||
379 | ||
d559219f | 380 | //---------------------------------------------------------------------- |
7bf85405 | 381 | |
d559219f RD |
382 | wxPyCallbackHelper::wxPyCallbackHelper() { |
383 | m_self = NULL; | |
384 | m_lastFound = NULL; | |
b7312675 | 385 | m_incRef = FALSE; |
d559219f | 386 | } |
8bf5d46e | 387 | |
8bf5d46e | 388 | |
d559219f RD |
389 | wxPyCallbackHelper::~wxPyCallbackHelper() { |
390 | bool doSave = wxPyRestoreThread(); | |
b7312675 RD |
391 | if (m_incRef) |
392 | Py_XDECREF(m_self); | |
d559219f RD |
393 | wxPySaveThread(doSave); |
394 | } | |
8bf5d46e | 395 | |
2f90df85 RD |
396 | wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) { |
397 | m_lastFound = NULL; | |
398 | m_self = other.m_self; | |
399 | if (m_self) | |
400 | Py_INCREF(m_self); | |
401 | } | |
402 | ||
403 | ||
404 | void wxPyCallbackHelper::setSelf(PyObject* self, int incref) { | |
d559219f | 405 | m_self = self; |
b7312675 | 406 | m_incRef = incref; |
2f90df85 RD |
407 | if (incref) |
408 | Py_INCREF(m_self); | |
d559219f | 409 | } |
8bf5d46e | 410 | |
8bf5d46e | 411 | |
d559219f RD |
412 | bool wxPyCallbackHelper::findCallback(const wxString& name) { |
413 | m_lastFound = NULL; | |
414 | if (m_self && PyObject_HasAttrString(m_self, (char*)name.c_str())) | |
415 | m_lastFound = PyObject_GetAttrString(m_self, (char*)name.c_str()); | |
8bf5d46e | 416 | |
d559219f RD |
417 | return m_lastFound != NULL; |
418 | } | |
8bf5d46e | 419 | |
d559219f RD |
420 | |
421 | int wxPyCallbackHelper::callCallback(PyObject* argTuple) { | |
422 | PyObject* result; | |
423 | int retval = FALSE; | |
424 | ||
425 | result = callCallbackObj(argTuple); | |
426 | if (result) { // Assumes an integer return type... | |
427 | retval = PyInt_AsLong(result); | |
428 | Py_DECREF(result); | |
429 | PyErr_Clear(); // forget about it if it's not... | |
430 | } | |
431 | return retval; | |
432 | } | |
433 | ||
434 | // Invoke the Python callable object, returning the raw PyObject return | |
435 | // value. Caller should DECREF the return value and also call PyEval_SaveThread. | |
436 | PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) { | |
437 | PyObject* result; | |
438 | ||
439 | result = PyEval_CallObject(m_lastFound, argTuple); | |
440 | Py_DECREF(argTuple); | |
441 | if (!result) { | |
442 | PyErr_Print(); | |
443 | } | |
444 | return result; | |
445 | } | |
714e6a9e | 446 | |
7bf85405 | 447 | |
d559219f | 448 | |
65dd82cb RD |
449 | //--------------------------------------------------------------------------- |
450 | //--------------------------------------------------------------------------- | |
451 | // These classes can be derived from in Python and passed through the event | |
452 | // system without loosing anything. They do this by keeping a reference to | |
453 | // themselves and some special case handling in wxPyCallback::EventThunker. | |
454 | ||
455 | ||
e19b7164 | 456 | wxPyEvtSelfRef::wxPyEvtSelfRef() { |
65dd82cb RD |
457 | //m_self = Py_None; // **** We don't do normal ref counting to prevent |
458 | //Py_INCREF(m_self); // circular loops... | |
459 | m_cloned = false; | |
460 | } | |
461 | ||
e19b7164 | 462 | wxPyEvtSelfRef::~wxPyEvtSelfRef() { |
65dd82cb RD |
463 | bool doSave = wxPyRestoreThread(); |
464 | if (m_cloned) | |
465 | Py_DECREF(m_self); | |
466 | wxPySaveThread(doSave); | |
467 | } | |
468 | ||
e19b7164 | 469 | void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) { |
65dd82cb RD |
470 | bool doSave = wxPyRestoreThread(); |
471 | if (m_cloned) | |
472 | Py_DECREF(m_self); | |
473 | m_self = self; | |
474 | if (clone) { | |
475 | Py_INCREF(m_self); | |
476 | m_cloned = TRUE; | |
477 | } | |
478 | wxPySaveThread(doSave); | |
479 | } | |
480 | ||
e19b7164 | 481 | PyObject* wxPyEvtSelfRef::GetSelf() const { |
65dd82cb RD |
482 | Py_INCREF(m_self); |
483 | return m_self; | |
484 | } | |
485 | ||
486 | ||
487 | wxPyEvent::wxPyEvent(int id) | |
488 | : wxEvent(id) { | |
489 | } | |
490 | ||
491 | wxPyEvent::~wxPyEvent() { | |
492 | } | |
493 | ||
494 | // This one is so the event object can be Cloned... | |
495 | void wxPyEvent::CopyObject(wxObject& dest) const { | |
496 | wxEvent::CopyObject(dest); | |
497 | ((wxPyEvent*)&dest)->SetSelf(m_self, TRUE); | |
498 | } | |
499 | ||
500 | ||
501 | IMPLEMENT_DYNAMIC_CLASS(wxPyEvent, wxEvent); | |
502 | ||
503 | ||
504 | wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id) | |
505 | : wxCommandEvent(commandType, id) { | |
506 | } | |
507 | ||
508 | wxPyCommandEvent::~wxPyCommandEvent() { | |
509 | } | |
510 | ||
511 | void wxPyCommandEvent::CopyObject(wxObject& dest) const { | |
512 | wxCommandEvent::CopyObject(dest); | |
513 | ((wxPyCommandEvent*)&dest)->SetSelf(m_self, TRUE); | |
514 | } | |
515 | ||
516 | ||
517 | IMPLEMENT_DYNAMIC_CLASS(wxPyCommandEvent, wxCommandEvent); | |
518 | ||
519 | ||
520 | ||
d559219f | 521 | //--------------------------------------------------------------------------- |
7bf85405 RD |
522 | //--------------------------------------------------------------------------- |
523 | ||
d559219f | 524 | |
7bf85405 RD |
525 | wxPyTimer::wxPyTimer(PyObject* callback) { |
526 | func = callback; | |
527 | Py_INCREF(func); | |
528 | } | |
529 | ||
530 | wxPyTimer::~wxPyTimer() { | |
d559219f | 531 | bool doSave = wxPyRestoreThread(); |
7bf85405 | 532 | Py_DECREF(func); |
d559219f | 533 | wxPySaveThread(doSave); |
7bf85405 RD |
534 | } |
535 | ||
536 | void wxPyTimer::Notify() { | |
d559219f RD |
537 | bool doSave = wxPyRestoreThread(); |
538 | ||
7bf85405 RD |
539 | PyObject* result; |
540 | PyObject* args = Py_BuildValue("()"); | |
541 | ||
542 | result = PyEval_CallObject(func, args); | |
543 | Py_DECREF(args); | |
544 | if (result) { | |
545 | Py_DECREF(result); | |
546 | PyErr_Clear(); | |
547 | } else { | |
548 | PyErr_Print(); | |
549 | } | |
d559219f | 550 | wxPySaveThread(doSave); |
7bf85405 RD |
551 | } |
552 | ||
553 | ||
cf694132 | 554 | |
2f90df85 | 555 | //--------------------------------------------------------------------------- |
389c5527 RD |
556 | //--------------------------------------------------------------------------- |
557 | // Convert a wxList to a Python List | |
558 | ||
65dd82cb | 559 | PyObject* wxPy_ConvertList(wxListBase* list, const char* className) { |
389c5527 RD |
560 | PyObject* pyList; |
561 | PyObject* pyObj; | |
562 | wxObject* wxObj; | |
563 | wxNode* node = list->First(); | |
564 | ||
565 | bool doSave = wxPyRestoreThread(); | |
566 | pyList = PyList_New(0); | |
567 | while (node) { | |
568 | wxObj = node->Data(); | |
569 | pyObj = wxPyConstructObject(wxObj, className); | |
570 | PyList_Append(pyList, pyObj); | |
571 | node = node->Next(); | |
572 | } | |
573 | wxPySaveThread(doSave); | |
574 | return pyList; | |
575 | } | |
576 | ||
7bf85405 RD |
577 | //---------------------------------------------------------------------- |
578 | // Some helper functions for typemaps in my_typemaps.i, so they won't be | |
389c5527 | 579 | // included in every file... |
7bf85405 RD |
580 | |
581 | ||
2f90df85 | 582 | byte* byte_LIST_helper(PyObject* source) { |
b639c3c5 RD |
583 | if (!PyList_Check(source)) { |
584 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
585 | return NULL; | |
586 | } | |
587 | int count = PyList_Size(source); | |
588 | byte* temp = new byte[count]; | |
589 | if (! temp) { | |
590 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
591 | return NULL; | |
592 | } | |
593 | for (int x=0; x<count; x++) { | |
594 | PyObject* o = PyList_GetItem(source, x); | |
595 | if (! PyInt_Check(o)) { | |
596 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
597 | return NULL; | |
598 | } | |
599 | temp[x] = (byte)PyInt_AsLong(o); | |
600 | } | |
601 | return temp; | |
602 | } | |
603 | ||
604 | ||
2f90df85 | 605 | int* int_LIST_helper(PyObject* source) { |
7bf85405 RD |
606 | if (!PyList_Check(source)) { |
607 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
608 | return NULL; | |
609 | } | |
610 | int count = PyList_Size(source); | |
611 | int* temp = new int[count]; | |
612 | if (! temp) { | |
613 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
614 | return NULL; | |
615 | } | |
616 | for (int x=0; x<count; x++) { | |
617 | PyObject* o = PyList_GetItem(source, x); | |
618 | if (! PyInt_Check(o)) { | |
619 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
620 | return NULL; | |
621 | } | |
622 | temp[x] = PyInt_AsLong(o); | |
623 | } | |
624 | return temp; | |
625 | } | |
626 | ||
627 | ||
2f90df85 | 628 | long* long_LIST_helper(PyObject* source) { |
7bf85405 RD |
629 | if (!PyList_Check(source)) { |
630 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
631 | return NULL; | |
632 | } | |
633 | int count = PyList_Size(source); | |
634 | long* temp = new long[count]; | |
635 | if (! temp) { | |
636 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
637 | return NULL; | |
638 | } | |
639 | for (int x=0; x<count; x++) { | |
640 | PyObject* o = PyList_GetItem(source, x); | |
641 | if (! PyInt_Check(o)) { | |
642 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
643 | return NULL; | |
644 | } | |
645 | temp[x] = PyInt_AsLong(o); | |
646 | } | |
647 | return temp; | |
648 | } | |
649 | ||
650 | ||
2f90df85 | 651 | char** string_LIST_helper(PyObject* source) { |
7bf85405 RD |
652 | if (!PyList_Check(source)) { |
653 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
654 | return NULL; | |
655 | } | |
656 | int count = PyList_Size(source); | |
657 | char** temp = new char*[count]; | |
658 | if (! temp) { | |
659 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
660 | return NULL; | |
661 | } | |
662 | for (int x=0; x<count; x++) { | |
663 | PyObject* o = PyList_GetItem(source, x); | |
664 | if (! PyString_Check(o)) { | |
665 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
666 | return NULL; | |
667 | } | |
668 | temp[x] = PyString_AsString(o); | |
669 | } | |
670 | return temp; | |
671 | } | |
672 | ||
673 | ||
674 | ||
2f90df85 | 675 | wxPoint* wxPoint_LIST_helper(PyObject* source) { |
7bf85405 RD |
676 | if (!PyList_Check(source)) { |
677 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
678 | return NULL; | |
679 | } | |
680 | int count = PyList_Size(source); | |
681 | wxPoint* temp = new wxPoint[count]; | |
682 | if (! temp) { | |
683 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
684 | return NULL; | |
685 | } | |
686 | for (int x=0; x<count; x++) { | |
687 | PyObject* o = PyList_GetItem(source, x); | |
d559219f | 688 | if (PyTuple_Check(o)) { |
7bf85405 RD |
689 | PyObject* o1 = PyTuple_GetItem(o, 0); |
690 | PyObject* o2 = PyTuple_GetItem(o, 1); | |
691 | ||
692 | temp[x].x = PyInt_AsLong(o1); | |
693 | temp[x].y = PyInt_AsLong(o2); | |
694 | } | |
d559219f RD |
695 | else if (PyInstance_Check(o)) { |
696 | wxPoint* pt; | |
697 | if (SWIG_GetPtrObj(o,(void **) &pt,"_wxPoint_p")) { | |
698 | PyErr_SetString(PyExc_TypeError,"Expected _wxPoint_p."); | |
699 | return NULL; | |
700 | } | |
701 | temp[x] = *pt; | |
702 | } | |
7bf85405 RD |
703 | else { |
704 | PyErr_SetString(PyExc_TypeError, "Expected a list of 2-tuples or wxPoints."); | |
705 | return NULL; | |
706 | } | |
707 | } | |
708 | return temp; | |
709 | } | |
710 | ||
711 | ||
2f90df85 | 712 | wxBitmap** wxBitmap_LIST_helper(PyObject* source) { |
7bf85405 RD |
713 | if (!PyList_Check(source)) { |
714 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
715 | return NULL; | |
716 | } | |
717 | int count = PyList_Size(source); | |
718 | wxBitmap** temp = new wxBitmap*[count]; | |
719 | if (! temp) { | |
720 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
721 | return NULL; | |
722 | } | |
723 | for (int x=0; x<count; x++) { | |
724 | PyObject* o = PyList_GetItem(source, x); | |
e166644c | 725 | if (PyInstance_Check(o)) { |
7bf85405 | 726 | wxBitmap* pt; |
e166644c | 727 | if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) { |
7bf85405 RD |
728 | PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p."); |
729 | return NULL; | |
730 | } | |
731 | temp[x] = pt; | |
732 | } | |
733 | else { | |
734 | PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps."); | |
735 | return NULL; | |
736 | } | |
737 | } | |
738 | return temp; | |
739 | } | |
740 | ||
741 | ||
742 | ||
2f90df85 | 743 | wxString* wxString_LIST_helper(PyObject* source) { |
7bf85405 RD |
744 | if (!PyList_Check(source)) { |
745 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
746 | return NULL; | |
747 | } | |
748 | int count = PyList_Size(source); | |
749 | wxString* temp = new wxString[count]; | |
750 | if (! temp) { | |
751 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
752 | return NULL; | |
753 | } | |
754 | for (int x=0; x<count; x++) { | |
755 | PyObject* o = PyList_GetItem(source, x); | |
756 | if (! PyString_Check(o)) { | |
757 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
758 | return NULL; | |
759 | } | |
760 | temp[x] = PyString_AsString(o); | |
761 | } | |
762 | return temp; | |
763 | } | |
764 | ||
765 | ||
2f90df85 | 766 | wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) { |
7bf85405 RD |
767 | if (!PyList_Check(source)) { |
768 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
769 | return NULL; | |
770 | } | |
771 | int count = PyList_Size(source); | |
772 | wxAcceleratorEntry* temp = new wxAcceleratorEntry[count]; | |
773 | if (! temp) { | |
774 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
775 | return NULL; | |
776 | } | |
777 | for (int x=0; x<count; x++) { | |
778 | PyObject* o = PyList_GetItem(source, x); | |
e166644c | 779 | if (PyInstance_Check(o)) { |
7bf85405 | 780 | wxAcceleratorEntry* ae; |
e166644c | 781 | if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) { |
7bf85405 RD |
782 | PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p."); |
783 | return NULL; | |
784 | } | |
785 | temp[x] = *ae; | |
786 | } | |
787 | else if (PyTuple_Check(o)) { | |
788 | PyObject* o1 = PyTuple_GetItem(o, 0); | |
789 | PyObject* o2 = PyTuple_GetItem(o, 1); | |
790 | PyObject* o3 = PyTuple_GetItem(o, 2); | |
791 | ||
792 | temp[x].m_flags = PyInt_AsLong(o1); | |
793 | temp[x].m_keyCode = PyInt_AsLong(o2); | |
794 | temp[x].m_command = PyInt_AsLong(o3); | |
795 | } | |
796 | else { | |
797 | PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects."); | |
798 | return NULL; | |
799 | } | |
800 | } | |
801 | return temp; | |
802 | } | |
803 | ||
bb0054cd RD |
804 | |
805 | ||
806 | //---------------------------------------------------------------------- | |
bb0054cd | 807 | |
2f90df85 RD |
808 | bool wxSize_helper(PyObject* source, wxSize** obj) { |
809 | ||
810 | // If source is an object instance then it may already be the right type | |
811 | if (PyInstance_Check(source)) { | |
812 | wxSize* ptr; | |
813 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p")) | |
814 | goto error; | |
815 | *obj = ptr; | |
816 | return TRUE; | |
817 | } | |
818 | // otherwise a 2-tuple of integers is expected | |
819 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
820 | PyObject* o1 = PySequence_GetItem(source, 0); | |
821 | PyObject* o2 = PySequence_GetItem(source, 1); | |
822 | **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2)); | |
823 | return TRUE; | |
824 | } | |
825 | ||
826 | error: | |
827 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object."); | |
828 | return FALSE; | |
829 | } | |
830 | ||
831 | bool wxPoint_helper(PyObject* source, wxPoint** obj) { | |
832 | ||
833 | // If source is an object instance then it may already be the right type | |
834 | if (PyInstance_Check(source)) { | |
835 | wxPoint* ptr; | |
836 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p")) | |
837 | goto error; | |
838 | *obj = ptr; | |
839 | return TRUE; | |
840 | } | |
841 | // otherwise a 2-tuple of integers is expected | |
842 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
843 | PyObject* o1 = PySequence_GetItem(source, 0); | |
844 | PyObject* o2 = PySequence_GetItem(source, 1); | |
845 | **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2)); | |
846 | return TRUE; | |
847 | } | |
848 | ||
849 | error: | |
850 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object."); | |
851 | return FALSE; | |
852 | } | |
853 | ||
854 | ||
855 | ||
856 | bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) { | |
857 | ||
858 | // If source is an object instance then it may already be the right type | |
859 | if (PyInstance_Check(source)) { | |
860 | wxRealPoint* ptr; | |
861 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p")) | |
862 | goto error; | |
863 | *obj = ptr; | |
864 | return TRUE; | |
865 | } | |
866 | // otherwise a 2-tuple of floats is expected | |
867 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
868 | PyObject* o1 = PySequence_GetItem(source, 0); | |
869 | PyObject* o2 = PySequence_GetItem(source, 1); | |
870 | **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2)); | |
871 | return TRUE; | |
872 | } | |
873 | ||
874 | error: | |
875 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object."); | |
876 | return FALSE; | |
877 | } | |
878 | ||
879 | ||
880 | ||
881 | ||
882 | bool wxRect_helper(PyObject* source, wxRect** obj) { | |
883 | ||
884 | // If source is an object instance then it may already be the right type | |
885 | if (PyInstance_Check(source)) { | |
886 | wxRect* ptr; | |
887 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p")) | |
888 | goto error; | |
889 | *obj = ptr; | |
890 | return TRUE; | |
891 | } | |
892 | // otherwise a 4-tuple of integers is expected | |
893 | else if (PySequence_Check(source) && PyObject_Length(source) == 4) { | |
894 | PyObject* o1 = PySequence_GetItem(source, 0); | |
895 | PyObject* o2 = PySequence_GetItem(source, 1); | |
896 | PyObject* o3 = PySequence_GetItem(source, 2); | |
897 | PyObject* o4 = PySequence_GetItem(source, 3); | |
898 | **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2), | |
899 | PyInt_AsLong(o3), PyInt_AsLong(o4)); | |
900 | return TRUE; | |
901 | } | |
902 | ||
903 | error: | |
904 | PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object."); | |
905 | return FALSE; | |
906 | } | |
907 | ||
908 | ||
909 | ||
bb0054cd RD |
910 | //---------------------------------------------------------------------- |
911 | //---------------------------------------------------------------------- | |
7bf85405 | 912 | //---------------------------------------------------------------------- |
7bf85405 | 913 | |
7bf85405 | 914 | |
7bf85405 | 915 |