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