]>
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 | { |
0d6f9504 RD |
124 | // Bail out if there is already windows created. This means that the |
125 | // toolkit has already been initialized, as in embedding wxPython in | |
126 | // a C++ wxWindows app. | |
127 | if (wxTopLevelWindows.Number() > 0) | |
128 | return; | |
7bf85405 | 129 | |
7bf85405 | 130 | #ifdef __WXMSW__ |
9c039d08 | 131 | wxApp::Initialize(); |
7bf85405 | 132 | #endif |
21f4bf45 | 133 | |
08127323 | 134 | #ifdef __WXGTK__ |
fb5e0af0 RD |
135 | PyObject* sysargv = PySys_GetObject("argv"); |
136 | int argc = PyList_Size(sysargv); | |
137 | char** argv = new char*[argc+1]; | |
138 | int x; | |
139 | for(x=0; x<argc; x++) | |
140 | argv[x] = PyString_AsString(PyList_GetItem(sysargv, x)); | |
141 | argv[argc] = NULL; | |
142 | ||
08127323 | 143 | gtk_set_locale(); |
8bf5d46e | 144 | if (!wxOKlibc()) wxConvCurrent = &wxConvLocal; |
0d6f9504 | 145 | gtk_init( &argc, &argv ); |
8bf5d46e | 146 | wxSetDetectableAutoRepeat( TRUE ); |
0d6f9504 | 147 | delete [] argv; |
7bf85405 | 148 | |
08127323 | 149 | wxApp::Initialize(); // may return FALSE. Should we check? |
7bf85405 RD |
150 | #endif |
151 | ||
0d6f9504 RD |
152 | } |
153 | ||
154 | ||
cf694132 | 155 | #ifdef WXP_WITH_THREAD |
bb0054cd | 156 | PyThreadState* wxPyEventThreadState = NULL; |
cf694132 | 157 | #endif |
0d6f9504 RD |
158 | static char* __nullArgv[1] = { 0 }; |
159 | ||
8bf5d46e RD |
160 | |
161 | ||
0d6f9504 RD |
162 | // Start the user application, user App's OnInit method is a parameter here |
163 | PyObject* __wxStart(PyObject* /* self */, PyObject* args) | |
164 | { | |
165 | PyObject* onInitFunc = NULL; | |
166 | PyObject* arglist; | |
167 | PyObject* result; | |
168 | long bResult; | |
169 | ||
cf694132 | 170 | #ifdef WXP_WITH_THREAD |
bb0054cd | 171 | wxPyEventThreadState = PyThreadState_Get(); |
cf694132 | 172 | #endif |
0d6f9504 RD |
173 | |
174 | if (!PyArg_ParseTuple(args, "O", &onInitFunc)) | |
175 | return NULL; | |
176 | ||
177 | if (wxTopLevelWindows.Number() > 0) { | |
178 | PyErr_SetString(PyExc_TypeError, "Only 1 wxApp per process!"); | |
179 | return NULL; | |
180 | } | |
181 | ||
182 | ||
183 | // This is the next part of the wxEntry functionality... | |
184 | wxPythonApp->argc = 0; | |
cf694132 | 185 | wxPythonApp->argv = NULL; |
0d6f9504 RD |
186 | wxPythonApp->OnInitGui(); |
187 | ||
7bf85405 RD |
188 | |
189 | // Call the Python App's OnInit function | |
190 | arglist = PyTuple_New(0); | |
191 | result = PyEval_CallObject(onInitFunc, arglist); | |
8bf5d46e RD |
192 | if (!result) { // an exception was raised. |
193 | return NULL; | |
7bf85405 RD |
194 | } |
195 | ||
196 | if (! PyInt_Check(result)) { | |
197 | PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value"); | |
198 | return NULL; | |
199 | } | |
200 | bResult = PyInt_AS_LONG(result); | |
201 | if (! bResult) { | |
7bf85405 RD |
202 | PyErr_SetString(PyExc_SystemExit, "OnInit returned false, exiting..."); |
203 | return NULL; | |
204 | } | |
205 | ||
13dfc243 | 206 | #ifdef __WXGTK__ |
fb5e0af0 | 207 | wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0); |
13dfc243 | 208 | #endif |
fb5e0af0 | 209 | |
7bf85405 RD |
210 | Py_INCREF(Py_None); |
211 | return Py_None; | |
212 | } | |
213 | ||
214 | ||
7bf85405 RD |
215 | |
216 | ||
217 | ||
218 | PyObject* wxPython_dict; | |
219 | PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args) | |
220 | { | |
221 | ||
222 | if (!PyArg_ParseTuple(args, "O", &wxPython_dict)) | |
223 | return NULL; | |
224 | ||
225 | if (!PyDict_Check(wxPython_dict)) { | |
226 | PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!"); | |
227 | return NULL; | |
228 | } | |
229 | #ifdef __WXMOTIF__ | |
21f4bf45 RD |
230 | #define wxPlatform "__WXMOTIF__" |
231 | #endif | |
232 | #ifdef __WXQT__ | |
233 | #define wxPlatform "__WXQT__" | |
7bf85405 RD |
234 | #endif |
235 | #ifdef __WXGTK__ | |
21f4bf45 | 236 | #define wxPlatform "__WXGTK__" |
7bf85405 RD |
237 | #endif |
238 | #if defined(__WIN32__) || defined(__WXMSW__) | |
fb5e0af0 | 239 | #define wxPlatform "__WXMSW__" |
7bf85405 RD |
240 | #endif |
241 | #ifdef __WXMAC__ | |
21f4bf45 | 242 | #define wxPlatform "__WXMAC__" |
7bf85405 RD |
243 | #endif |
244 | ||
245 | PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform)); | |
246 | ||
247 | Py_INCREF(Py_None); | |
248 | return Py_None; | |
249 | } | |
250 | ||
251 | ||
252 | //--------------------------------------------------------------------------- | |
253 | ||
d559219f | 254 | PyObject* wxPyConstructObject(void* ptr, char* className) { |
9c039d08 | 255 | char buff[64]; // should always be big enough... |
7bf85405 RD |
256 | char swigptr[64]; |
257 | ||
258 | sprintf(buff, "_%s_p", className); | |
259 | SWIG_MakePtr(swigptr, ptr, buff); | |
260 | ||
261 | sprintf(buff, "%sPtr", className); | |
262 | PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff); | |
263 | if (! classobj) { | |
264 | Py_INCREF(Py_None); | |
265 | return Py_None; | |
266 | } | |
267 | ||
268 | PyObject* arg = Py_BuildValue("(s)", swigptr); | |
269 | PyObject* obj = PyInstance_New(classobj, arg, NULL); | |
270 | Py_DECREF(arg); | |
271 | ||
272 | return obj; | |
273 | } | |
274 | ||
d559219f | 275 | //--------------------------------------------------------------------------- |
7bf85405 | 276 | |
d559219f RD |
277 | //static bool _wxPyInEvent = false; |
278 | static unsigned int _wxPyNestCount = 0; | |
cf694132 | 279 | |
d559219f RD |
280 | HELPEREXPORT bool wxPyRestoreThread() { |
281 | // #ifdef WXP_WITH_THREAD | |
282 | // //if (wxPyEventThreadState != PyThreadState_Get()) { | |
283 | // if (! _wxPyInEvent) { | |
284 | // PyEval_RestoreThread(wxPyEventThreadState); | |
285 | // _wxPyInEvent = true; | |
286 | // return TRUE; | |
287 | // } else | |
288 | // #endif | |
289 | // return FALSE; | |
290 | ||
291 | // NOTE: The Python API docs state that if a thread already has the | |
292 | // interpreter lock and calls PyEval_RestoreThread again a deadlock | |
293 | // occurs, so I put in the above code as a guard condition since there are | |
294 | // many possibilites for nested events and callbacks in wxPython. | |
295 | // | |
296 | // Unfortunately, it seems like somebody was lying (or I'm not | |
297 | // understanding...) because each of the nested calls to this function | |
298 | // MUST call PyEval_RestoreThread or Python pukes with a thread error (at | |
299 | // least on Win32.) | |
300 | // | |
301 | // until I know better, this is how I am doing it instead: | |
cf694132 | 302 | #ifdef WXP_WITH_THREAD |
d559219f RD |
303 | PyEval_RestoreThread(wxPyEventThreadState); |
304 | _wxPyNestCount += 1; | |
305 | if (_wxPyNestCount == 1) | |
306 | return TRUE; | |
307 | else | |
cf694132 | 308 | #endif |
d559219f RD |
309 | return FALSE; |
310 | } | |
cf694132 | 311 | |
cf694132 | 312 | |
d559219f | 313 | HELPEREXPORT void wxPySaveThread(bool doSave) { |
cf694132 | 314 | #ifdef WXP_WITH_THREAD |
d559219f | 315 | if (doSave) { |
bb0054cd | 316 | PyEval_SaveThread(); |
d559219f RD |
317 | //_wxPyInEvent = false; |
318 | } | |
319 | _wxPyNestCount -= 1; | |
cf694132 RD |
320 | #endif |
321 | } | |
322 | ||
d559219f RD |
323 | //--------------------------------------------------------------------------- |
324 | ||
325 | ||
2f90df85 RD |
326 | IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject); |
327 | ||
d559219f RD |
328 | wxPyCallback::wxPyCallback(PyObject* func) { |
329 | m_func = func; | |
330 | Py_INCREF(m_func); | |
331 | } | |
332 | ||
2f90df85 RD |
333 | wxPyCallback::wxPyCallback(const wxPyCallback& other) { |
334 | m_func = other.m_func; | |
335 | Py_INCREF(m_func); | |
336 | } | |
337 | ||
d559219f RD |
338 | wxPyCallback::~wxPyCallback() { |
339 | bool doSave = wxPyRestoreThread(); | |
340 | Py_DECREF(m_func); | |
341 | wxPySaveThread(doSave); | |
342 | } | |
343 | ||
cf694132 RD |
344 | |
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(); |
7bf85405 RD |
357 | arg = wxPyConstructObject((void*)&event, event.GetClassInfo()->GetClassName()); |
358 | ||
359 | tuple = PyTuple_New(1); | |
360 | PyTuple_SET_ITEM(tuple, 0, arg); | |
361 | result = PyEval_CallObject(func, tuple); | |
7bf85405 RD |
362 | Py_DECREF(tuple); |
363 | if (result) { | |
364 | Py_DECREF(result); | |
365 | PyErr_Clear(); | |
366 | } else { | |
367 | PyErr_Print(); | |
368 | } | |
d559219f | 369 | wxPySaveThread(doSave); |
7bf85405 RD |
370 | } |
371 | ||
372 | ||
d559219f | 373 | //---------------------------------------------------------------------- |
7bf85405 | 374 | |
d559219f RD |
375 | wxPyCallbackHelper::wxPyCallbackHelper() { |
376 | m_self = NULL; | |
377 | m_lastFound = NULL; | |
378 | } | |
8bf5d46e | 379 | |
8bf5d46e | 380 | |
d559219f RD |
381 | wxPyCallbackHelper::~wxPyCallbackHelper() { |
382 | bool doSave = wxPyRestoreThread(); | |
383 | Py_XDECREF(m_self); | |
384 | wxPySaveThread(doSave); | |
385 | } | |
8bf5d46e | 386 | |
2f90df85 RD |
387 | wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) { |
388 | m_lastFound = NULL; | |
389 | m_self = other.m_self; | |
390 | if (m_self) | |
391 | Py_INCREF(m_self); | |
392 | } | |
393 | ||
394 | ||
395 | void wxPyCallbackHelper::setSelf(PyObject* self, int incref) { | |
d559219f | 396 | m_self = self; |
2f90df85 RD |
397 | if (incref) |
398 | Py_INCREF(m_self); | |
d559219f | 399 | } |
8bf5d46e | 400 | |
8bf5d46e | 401 | |
d559219f RD |
402 | bool wxPyCallbackHelper::findCallback(const wxString& name) { |
403 | m_lastFound = NULL; | |
404 | if (m_self && PyObject_HasAttrString(m_self, (char*)name.c_str())) | |
405 | m_lastFound = PyObject_GetAttrString(m_self, (char*)name.c_str()); | |
8bf5d46e | 406 | |
d559219f RD |
407 | return m_lastFound != NULL; |
408 | } | |
8bf5d46e | 409 | |
d559219f RD |
410 | |
411 | int wxPyCallbackHelper::callCallback(PyObject* argTuple) { | |
412 | PyObject* result; | |
413 | int retval = FALSE; | |
414 | ||
415 | result = callCallbackObj(argTuple); | |
416 | if (result) { // Assumes an integer return type... | |
417 | retval = PyInt_AsLong(result); | |
418 | Py_DECREF(result); | |
419 | PyErr_Clear(); // forget about it if it's not... | |
420 | } | |
421 | return retval; | |
422 | } | |
423 | ||
424 | // Invoke the Python callable object, returning the raw PyObject return | |
425 | // value. Caller should DECREF the return value and also call PyEval_SaveThread. | |
426 | PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) { | |
427 | PyObject* result; | |
428 | ||
429 | result = PyEval_CallObject(m_lastFound, argTuple); | |
430 | Py_DECREF(argTuple); | |
431 | if (!result) { | |
432 | PyErr_Print(); | |
433 | } | |
434 | return result; | |
435 | } | |
714e6a9e | 436 | |
7bf85405 | 437 | |
d559219f RD |
438 | |
439 | //--------------------------------------------------------------------------- | |
7bf85405 RD |
440 | //--------------------------------------------------------------------------- |
441 | ||
d559219f | 442 | |
7bf85405 RD |
443 | wxPyTimer::wxPyTimer(PyObject* callback) { |
444 | func = callback; | |
445 | Py_INCREF(func); | |
446 | } | |
447 | ||
448 | wxPyTimer::~wxPyTimer() { | |
d559219f | 449 | bool doSave = wxPyRestoreThread(); |
7bf85405 | 450 | Py_DECREF(func); |
d559219f | 451 | wxPySaveThread(doSave); |
7bf85405 RD |
452 | } |
453 | ||
454 | void wxPyTimer::Notify() { | |
d559219f RD |
455 | bool doSave = wxPyRestoreThread(); |
456 | ||
7bf85405 RD |
457 | PyObject* result; |
458 | PyObject* args = Py_BuildValue("()"); | |
459 | ||
460 | result = PyEval_CallObject(func, args); | |
461 | Py_DECREF(args); | |
462 | if (result) { | |
463 | Py_DECREF(result); | |
464 | PyErr_Clear(); | |
465 | } else { | |
466 | PyErr_Print(); | |
467 | } | |
d559219f | 468 | wxPySaveThread(doSave); |
7bf85405 RD |
469 | } |
470 | ||
471 | ||
cf694132 | 472 | |
2f90df85 | 473 | //--------------------------------------------------------------------------- |
389c5527 RD |
474 | //--------------------------------------------------------------------------- |
475 | // Convert a wxList to a Python List | |
476 | ||
477 | PyObject* wxPy_ConvertList(wxListBase* list, char* className) { | |
478 | PyObject* pyList; | |
479 | PyObject* pyObj; | |
480 | wxObject* wxObj; | |
481 | wxNode* node = list->First(); | |
482 | ||
483 | bool doSave = wxPyRestoreThread(); | |
484 | pyList = PyList_New(0); | |
485 | while (node) { | |
486 | wxObj = node->Data(); | |
487 | pyObj = wxPyConstructObject(wxObj, className); | |
488 | PyList_Append(pyList, pyObj); | |
489 | node = node->Next(); | |
490 | } | |
491 | wxPySaveThread(doSave); | |
492 | return pyList; | |
493 | } | |
494 | ||
7bf85405 RD |
495 | //---------------------------------------------------------------------- |
496 | // Some helper functions for typemaps in my_typemaps.i, so they won't be | |
389c5527 | 497 | // included in every file... |
7bf85405 RD |
498 | |
499 | ||
2f90df85 | 500 | byte* byte_LIST_helper(PyObject* source) { |
b639c3c5 RD |
501 | if (!PyList_Check(source)) { |
502 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
503 | return NULL; | |
504 | } | |
505 | int count = PyList_Size(source); | |
506 | byte* temp = new byte[count]; | |
507 | if (! temp) { | |
508 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
509 | return NULL; | |
510 | } | |
511 | for (int x=0; x<count; x++) { | |
512 | PyObject* o = PyList_GetItem(source, x); | |
513 | if (! PyInt_Check(o)) { | |
514 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
515 | return NULL; | |
516 | } | |
517 | temp[x] = (byte)PyInt_AsLong(o); | |
518 | } | |
519 | return temp; | |
520 | } | |
521 | ||
522 | ||
2f90df85 | 523 | int* int_LIST_helper(PyObject* source) { |
7bf85405 RD |
524 | if (!PyList_Check(source)) { |
525 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
526 | return NULL; | |
527 | } | |
528 | int count = PyList_Size(source); | |
529 | int* temp = new int[count]; | |
530 | if (! temp) { | |
531 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
532 | return NULL; | |
533 | } | |
534 | for (int x=0; x<count; x++) { | |
535 | PyObject* o = PyList_GetItem(source, x); | |
536 | if (! PyInt_Check(o)) { | |
537 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
538 | return NULL; | |
539 | } | |
540 | temp[x] = PyInt_AsLong(o); | |
541 | } | |
542 | return temp; | |
543 | } | |
544 | ||
545 | ||
2f90df85 | 546 | long* long_LIST_helper(PyObject* source) { |
7bf85405 RD |
547 | if (!PyList_Check(source)) { |
548 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
549 | return NULL; | |
550 | } | |
551 | int count = PyList_Size(source); | |
552 | long* temp = new long[count]; | |
553 | if (! temp) { | |
554 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
555 | return NULL; | |
556 | } | |
557 | for (int x=0; x<count; x++) { | |
558 | PyObject* o = PyList_GetItem(source, x); | |
559 | if (! PyInt_Check(o)) { | |
560 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
561 | return NULL; | |
562 | } | |
563 | temp[x] = PyInt_AsLong(o); | |
564 | } | |
565 | return temp; | |
566 | } | |
567 | ||
568 | ||
2f90df85 | 569 | char** string_LIST_helper(PyObject* source) { |
7bf85405 RD |
570 | if (!PyList_Check(source)) { |
571 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
572 | return NULL; | |
573 | } | |
574 | int count = PyList_Size(source); | |
575 | char** temp = new char*[count]; | |
576 | if (! temp) { | |
577 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
578 | return NULL; | |
579 | } | |
580 | for (int x=0; x<count; x++) { | |
581 | PyObject* o = PyList_GetItem(source, x); | |
582 | if (! PyString_Check(o)) { | |
583 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
584 | return NULL; | |
585 | } | |
586 | temp[x] = PyString_AsString(o); | |
587 | } | |
588 | return temp; | |
589 | } | |
590 | ||
591 | ||
592 | ||
2f90df85 | 593 | wxPoint* wxPoint_LIST_helper(PyObject* source) { |
7bf85405 RD |
594 | if (!PyList_Check(source)) { |
595 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
596 | return NULL; | |
597 | } | |
598 | int count = PyList_Size(source); | |
599 | wxPoint* temp = new wxPoint[count]; | |
600 | if (! temp) { | |
601 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
602 | return NULL; | |
603 | } | |
604 | for (int x=0; x<count; x++) { | |
605 | PyObject* o = PyList_GetItem(source, x); | |
d559219f | 606 | if (PyTuple_Check(o)) { |
7bf85405 RD |
607 | PyObject* o1 = PyTuple_GetItem(o, 0); |
608 | PyObject* o2 = PyTuple_GetItem(o, 1); | |
609 | ||
610 | temp[x].x = PyInt_AsLong(o1); | |
611 | temp[x].y = PyInt_AsLong(o2); | |
612 | } | |
d559219f RD |
613 | else if (PyInstance_Check(o)) { |
614 | wxPoint* pt; | |
615 | if (SWIG_GetPtrObj(o,(void **) &pt,"_wxPoint_p")) { | |
616 | PyErr_SetString(PyExc_TypeError,"Expected _wxPoint_p."); | |
617 | return NULL; | |
618 | } | |
619 | temp[x] = *pt; | |
620 | } | |
7bf85405 RD |
621 | else { |
622 | PyErr_SetString(PyExc_TypeError, "Expected a list of 2-tuples or wxPoints."); | |
623 | return NULL; | |
624 | } | |
625 | } | |
626 | return temp; | |
627 | } | |
628 | ||
629 | ||
2f90df85 | 630 | wxBitmap** wxBitmap_LIST_helper(PyObject* source) { |
7bf85405 RD |
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 | wxBitmap** temp = new wxBitmap*[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); | |
e166644c | 643 | if (PyInstance_Check(o)) { |
7bf85405 | 644 | wxBitmap* pt; |
e166644c | 645 | if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) { |
7bf85405 RD |
646 | PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p."); |
647 | return NULL; | |
648 | } | |
649 | temp[x] = pt; | |
650 | } | |
651 | else { | |
652 | PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps."); | |
653 | return NULL; | |
654 | } | |
655 | } | |
656 | return temp; | |
657 | } | |
658 | ||
659 | ||
660 | ||
2f90df85 | 661 | wxString* wxString_LIST_helper(PyObject* source) { |
7bf85405 RD |
662 | if (!PyList_Check(source)) { |
663 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
664 | return NULL; | |
665 | } | |
666 | int count = PyList_Size(source); | |
667 | wxString* temp = new wxString[count]; | |
668 | if (! temp) { | |
669 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
670 | return NULL; | |
671 | } | |
672 | for (int x=0; x<count; x++) { | |
673 | PyObject* o = PyList_GetItem(source, x); | |
674 | if (! PyString_Check(o)) { | |
675 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
676 | return NULL; | |
677 | } | |
678 | temp[x] = PyString_AsString(o); | |
679 | } | |
680 | return temp; | |
681 | } | |
682 | ||
683 | ||
2f90df85 | 684 | wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) { |
7bf85405 RD |
685 | if (!PyList_Check(source)) { |
686 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
687 | return NULL; | |
688 | } | |
689 | int count = PyList_Size(source); | |
690 | wxAcceleratorEntry* temp = new wxAcceleratorEntry[count]; | |
691 | if (! temp) { | |
692 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
693 | return NULL; | |
694 | } | |
695 | for (int x=0; x<count; x++) { | |
696 | PyObject* o = PyList_GetItem(source, x); | |
e166644c | 697 | if (PyInstance_Check(o)) { |
7bf85405 | 698 | wxAcceleratorEntry* ae; |
e166644c | 699 | if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) { |
7bf85405 RD |
700 | PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p."); |
701 | return NULL; | |
702 | } | |
703 | temp[x] = *ae; | |
704 | } | |
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); | |
709 | ||
710 | temp[x].m_flags = PyInt_AsLong(o1); | |
711 | temp[x].m_keyCode = PyInt_AsLong(o2); | |
712 | temp[x].m_command = PyInt_AsLong(o3); | |
713 | } | |
714 | else { | |
715 | PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects."); | |
716 | return NULL; | |
717 | } | |
718 | } | |
719 | return temp; | |
720 | } | |
721 | ||
bb0054cd RD |
722 | |
723 | ||
724 | //---------------------------------------------------------------------- | |
bb0054cd | 725 | |
2f90df85 RD |
726 | bool wxSize_helper(PyObject* source, wxSize** obj) { |
727 | ||
728 | // If source is an object instance then it may already be the right type | |
729 | if (PyInstance_Check(source)) { | |
730 | wxSize* ptr; | |
731 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p")) | |
732 | goto error; | |
733 | *obj = ptr; | |
734 | return TRUE; | |
735 | } | |
736 | // otherwise a 2-tuple of integers is expected | |
737 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
738 | PyObject* o1 = PySequence_GetItem(source, 0); | |
739 | PyObject* o2 = PySequence_GetItem(source, 1); | |
740 | **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2)); | |
741 | return TRUE; | |
742 | } | |
743 | ||
744 | error: | |
745 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object."); | |
746 | return FALSE; | |
747 | } | |
748 | ||
749 | bool wxPoint_helper(PyObject* source, wxPoint** obj) { | |
750 | ||
751 | // If source is an object instance then it may already be the right type | |
752 | if (PyInstance_Check(source)) { | |
753 | wxPoint* ptr; | |
754 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p")) | |
755 | goto error; | |
756 | *obj = ptr; | |
757 | return TRUE; | |
758 | } | |
759 | // otherwise a 2-tuple of integers is expected | |
760 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
761 | PyObject* o1 = PySequence_GetItem(source, 0); | |
762 | PyObject* o2 = PySequence_GetItem(source, 1); | |
763 | **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2)); | |
764 | return TRUE; | |
765 | } | |
766 | ||
767 | error: | |
768 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object."); | |
769 | return FALSE; | |
770 | } | |
771 | ||
772 | ||
773 | ||
774 | bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) { | |
775 | ||
776 | // If source is an object instance then it may already be the right type | |
777 | if (PyInstance_Check(source)) { | |
778 | wxRealPoint* ptr; | |
779 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p")) | |
780 | goto error; | |
781 | *obj = ptr; | |
782 | return TRUE; | |
783 | } | |
784 | // otherwise a 2-tuple of floats is expected | |
785 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
786 | PyObject* o1 = PySequence_GetItem(source, 0); | |
787 | PyObject* o2 = PySequence_GetItem(source, 1); | |
788 | **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2)); | |
789 | return TRUE; | |
790 | } | |
791 | ||
792 | error: | |
793 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object."); | |
794 | return FALSE; | |
795 | } | |
796 | ||
797 | ||
798 | ||
799 | ||
800 | bool wxRect_helper(PyObject* source, wxRect** obj) { | |
801 | ||
802 | // If source is an object instance then it may already be the right type | |
803 | if (PyInstance_Check(source)) { | |
804 | wxRect* ptr; | |
805 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p")) | |
806 | goto error; | |
807 | *obj = ptr; | |
808 | return TRUE; | |
809 | } | |
810 | // otherwise a 4-tuple of integers is expected | |
811 | else if (PySequence_Check(source) && PyObject_Length(source) == 4) { | |
812 | PyObject* o1 = PySequence_GetItem(source, 0); | |
813 | PyObject* o2 = PySequence_GetItem(source, 1); | |
814 | PyObject* o3 = PySequence_GetItem(source, 2); | |
815 | PyObject* o4 = PySequence_GetItem(source, 3); | |
816 | **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2), | |
817 | PyInt_AsLong(o3), PyInt_AsLong(o4)); | |
818 | return TRUE; | |
819 | } | |
820 | ||
821 | error: | |
822 | PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object."); | |
823 | return FALSE; | |
824 | } | |
825 | ||
826 | ||
827 | ||
bb0054cd RD |
828 | //---------------------------------------------------------------------- |
829 | //---------------------------------------------------------------------- | |
7bf85405 | 830 | //---------------------------------------------------------------------- |
7bf85405 | 831 | |
7bf85405 | 832 | |
7bf85405 | 833 |