]>
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 | #include <wx/msw/winundef.h> | |
22 | #endif | |
23 | ||
24 | #ifdef __WXGTK__ | |
25 | #include <gtk/gtk.h> | |
26 | #include <gdk/gdkprivate.h> | |
27 | #include <wx/gtk/win_gtk.h> | |
28 | #endif | |
29 | ||
30 | ||
31 | ||
32 | ||
33 | #ifdef __WXMSW__ // If building for win32... | |
34 | //---------------------------------------------------------------------- | |
35 | // This gets run when the DLL is loaded. We just need to save a handle. | |
36 | //---------------------------------------------------------------------- | |
37 | ||
38 | BOOL WINAPI DllMain( | |
39 | HINSTANCE hinstDLL, // handle to DLL module | |
40 | DWORD fdwReason, // reason for calling function | |
41 | LPVOID lpvReserved // reserved | |
42 | ) | |
43 | { | |
44 | wxSetInstance(hinstDLL); | |
45 | return 1; | |
46 | } | |
47 | #endif | |
48 | ||
49 | //---------------------------------------------------------------------- | |
50 | // Class for implementing the wxp main application shell. | |
51 | //---------------------------------------------------------------------- | |
52 | ||
53 | wxPyApp *wxPythonApp = NULL; // Global instance of application object | |
54 | ||
55 | ||
56 | wxPyApp::wxPyApp() { | |
57 | // printf("**** ctor\n"); | |
58 | } | |
59 | ||
60 | wxPyApp::~wxPyApp() { | |
61 | // printf("**** dtor\n"); | |
62 | } | |
63 | ||
64 | ||
65 | // This one isn't acutally called... See __wxStart() | |
66 | bool wxPyApp::OnInit(void) { | |
67 | return FALSE; | |
68 | } | |
69 | ||
70 | int wxPyApp::MainLoop(void) { | |
71 | int retval = 0; | |
72 | ||
73 | DeletePendingObjects(); | |
74 | #ifdef __WXGTK__ | |
75 | m_initialized = wxTopLevelWindows.GetCount() != 0; | |
76 | #endif | |
77 | ||
78 | if (Initialized()) { | |
79 | retval = wxApp::MainLoop(); | |
80 | wxPythonApp->OnExit(); | |
81 | } | |
82 | return retval; | |
83 | } | |
84 | ||
85 | ||
86 | //--------------------------------------------------------------------- | |
87 | //---------------------------------------------------------------------- | |
88 | ||
89 | #ifdef __WXMSW__ | |
90 | #include "wx/msw/msvcrt.h" | |
91 | #endif | |
92 | ||
93 | ||
94 | int WXDLLEXPORT wxEntryStart( int argc, char** argv ); | |
95 | int WXDLLEXPORT wxEntryInitGui(); | |
96 | void WXDLLEXPORT wxEntryCleanup(); | |
97 | ||
98 | ||
99 | #ifdef WXP_WITH_THREAD | |
100 | PyInterpreterState* wxPyInterpreter = NULL; | |
101 | #endif | |
102 | ||
103 | ||
104 | // This is where we pick up the first part of the wxEntry functionality... | |
105 | // The rest is in __wxStart and __wxCleanup. This function is called when | |
106 | // wxcmodule is imported. (Before there is a wxApp object.) | |
107 | void __wxPreStart() | |
108 | { | |
109 | ||
110 | #ifdef __WXMSW__ | |
111 | // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); | |
112 | #endif | |
113 | ||
114 | #ifdef WXP_WITH_THREAD | |
115 | PyEval_InitThreads(); | |
116 | wxPyInterpreter = PyThreadState_Get()->interp; | |
117 | #endif | |
118 | ||
119 | // Bail out if there is already windows created. This means that the | |
120 | // toolkit has already been initialized, as in embedding wxPython in | |
121 | // a C++ wxWindows app. | |
122 | if (wxTopLevelWindows.Number() > 0) | |
123 | return; | |
124 | ||
125 | ||
126 | int argc = 0; | |
127 | char** argv = NULL; | |
128 | PyObject* sysargv = PySys_GetObject("argv"); | |
129 | if (sysargv != NULL) { | |
130 | argc = PyList_Size(sysargv); | |
131 | argv = new char*[argc+1]; | |
132 | int x; | |
133 | for(x=0; x<argc; x++) | |
134 | argv[x] = copystring(PyString_AsString(PyList_GetItem(sysargv, x))); | |
135 | argv[argc] = NULL; | |
136 | } | |
137 | ||
138 | wxEntryStart(argc, argv); | |
139 | delete [] argv; | |
140 | } | |
141 | ||
142 | ||
143 | ||
144 | // Start the user application, user App's OnInit method is a parameter here | |
145 | PyObject* __wxStart(PyObject* /* self */, PyObject* args) | |
146 | { | |
147 | PyObject* onInitFunc = NULL; | |
148 | PyObject* arglist; | |
149 | PyObject* result; | |
150 | long bResult; | |
151 | ||
152 | if (!PyArg_ParseTuple(args, "O", &onInitFunc)) | |
153 | return NULL; | |
154 | ||
155 | #if 0 // Try it out without this check, see how it does... | |
156 | if (wxTopLevelWindows.Number() > 0) { | |
157 | PyErr_SetString(PyExc_TypeError, "Only 1 wxApp per process!"); | |
158 | return NULL; | |
159 | } | |
160 | #endif | |
161 | ||
162 | // This is the next part of the wxEntry functionality... | |
163 | int argc = 0; | |
164 | char** argv = NULL; | |
165 | PyObject* sysargv = PySys_GetObject("argv"); | |
166 | if (sysargv != NULL) { | |
167 | argc = PyList_Size(sysargv); | |
168 | argv = new char*[argc+1]; | |
169 | int x; | |
170 | for(x=0; x<argc; x++) | |
171 | argv[x] = copystring(PyString_AsString(PyList_GetItem(sysargv, x))); | |
172 | argv[argc] = NULL; | |
173 | } | |
174 | wxPythonApp->argc = argc; | |
175 | wxPythonApp->argv = argv; | |
176 | ||
177 | wxEntryInitGui(); | |
178 | ||
179 | // Call the Python App's OnInit function | |
180 | arglist = PyTuple_New(0); | |
181 | result = PyEval_CallObject(onInitFunc, arglist); | |
182 | if (!result) { // an exception was raised. | |
183 | return NULL; | |
184 | } | |
185 | ||
186 | if (! PyInt_Check(result)) { | |
187 | PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value"); | |
188 | return NULL; | |
189 | } | |
190 | bResult = PyInt_AS_LONG(result); | |
191 | if (! bResult) { | |
192 | PyErr_SetString(PyExc_SystemExit, "OnInit returned FALSE, exiting..."); | |
193 | return NULL; | |
194 | } | |
195 | ||
196 | #ifdef __WXGTK__ | |
197 | wxTheApp->m_initialized = (wxTopLevelWindows.GetCount() > 0); | |
198 | #endif | |
199 | ||
200 | Py_INCREF(Py_None); | |
201 | return Py_None; | |
202 | } | |
203 | ||
204 | void __wxCleanup() { | |
205 | wxEntryCleanup(); | |
206 | } | |
207 | ||
208 | ||
209 | ||
210 | static PyObject* wxPython_dict = NULL; | |
211 | static PyObject* wxPyPtrTypeMap = NULL; | |
212 | ||
213 | PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args) | |
214 | { | |
215 | ||
216 | if (!PyArg_ParseTuple(args, "O", &wxPython_dict)) | |
217 | return NULL; | |
218 | ||
219 | if (!PyDict_Check(wxPython_dict)) { | |
220 | PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!"); | |
221 | return NULL; | |
222 | } | |
223 | ||
224 | if (! wxPyPtrTypeMap) | |
225 | wxPyPtrTypeMap = PyDict_New(); | |
226 | PyDict_SetItemString(wxPython_dict, "__wxPyPtrTypeMap", wxPyPtrTypeMap); | |
227 | ||
228 | ||
229 | #ifdef __WXMOTIF__ | |
230 | #define wxPlatform "__WXMOTIF__" | |
231 | #endif | |
232 | #ifdef __WXQT__ | |
233 | #define wxPlatform "__WXQT__" | |
234 | #endif | |
235 | #ifdef __WXGTK__ | |
236 | #define wxPlatform "__WXGTK__" | |
237 | #endif | |
238 | #if defined(__WIN32__) || defined(__WXMSW__) | |
239 | #define wxPlatform "__WXMSW__" | |
240 | #endif | |
241 | #ifdef __WXMAC__ | |
242 | #define wxPlatform "__WXMAC__" | |
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 | // Stuff used by OOR to find the right wxPython class type to return and to | |
254 | // build it. | |
255 | ||
256 | ||
257 | // The pointer type map is used when the "pointer" type name generated by SWIG | |
258 | // is not the same as the shadow class name, for example wxPyTreeCtrl | |
259 | // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++, | |
260 | // so we'll just make it a Python dictionary in the wx module's namespace. | |
261 | void wxPyPtrTypeMap_Add(const char* commonName, const char* ptrName) { | |
262 | if (! wxPyPtrTypeMap) | |
263 | wxPyPtrTypeMap = PyDict_New(); | |
264 | ||
265 | PyDict_SetItemString(wxPyPtrTypeMap, | |
266 | (char*)commonName, | |
267 | PyString_FromString((char*)ptrName)); | |
268 | } | |
269 | ||
270 | ||
271 | ||
272 | PyObject* wxPyClassExists(const char* className) { | |
273 | ||
274 | if (!className) | |
275 | return NULL; | |
276 | ||
277 | char buff[64]; // should always be big enough... | |
278 | ||
279 | sprintf(buff, "%sPtr", className); | |
280 | PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff); | |
281 | ||
282 | return classobj; // returns NULL if not found | |
283 | } | |
284 | ||
285 | ||
286 | PyObject* wxPyMake_wxObject(wxObject* source, bool checkEvtHandler) { | |
287 | PyObject* target = NULL; | |
288 | bool isEvtHandler = FALSE; | |
289 | ||
290 | if (source) { | |
291 | // If it's derived from wxEvtHandler then there may | |
292 | // already be a pointer to a Python object that we can use | |
293 | // in the OOR data. | |
294 | if (checkEvtHandler && wxIsKindOf(source, wxEvtHandler)) { | |
295 | isEvtHandler = TRUE; | |
296 | wxEvtHandler* eh = (wxEvtHandler*)source; | |
297 | wxPyClientData* data = (wxPyClientData*)eh->GetClientObject(); | |
298 | if (data) { | |
299 | target = data->m_obj; | |
300 | Py_INCREF(target); | |
301 | } | |
302 | } | |
303 | ||
304 | if (! target) { | |
305 | // Otherwise make it the old fashioned way by making a | |
306 | // new shadow object and putting this pointer in it. | |
307 | wxClassInfo* info = source->GetClassInfo(); | |
308 | wxChar* name = (wxChar*)info->GetClassName(); | |
309 | PyObject* klass = wxPyClassExists(name); | |
310 | while (info && !klass) { | |
311 | name = (wxChar*)info->GetBaseClassName1(); | |
312 | info = wxClassInfo::FindClass(name); | |
313 | klass = wxPyClassExists(name); | |
314 | } | |
315 | if (info) { | |
316 | target = wxPyConstructObject(source, name, klass, FALSE); | |
317 | if (target && isEvtHandler) | |
318 | ((wxEvtHandler*)source)->SetClientObject(new wxPyClientData(target)); | |
319 | } else { | |
320 | wxString msg("wxPython class not found for "); | |
321 | msg += source->GetClassInfo()->GetClassName(); | |
322 | PyErr_SetString(PyExc_NameError, msg.c_str()); | |
323 | target = NULL; | |
324 | } | |
325 | } | |
326 | } else { // source was NULL so return None. | |
327 | Py_INCREF(Py_None); target = Py_None; | |
328 | } | |
329 | return target; | |
330 | } | |
331 | ||
332 | ||
333 | PyObject* wxPyMake_wxSizer(wxSizer* source) { | |
334 | PyObject* target = NULL; | |
335 | ||
336 | if (source && wxIsKindOf(source, wxSizer)) { | |
337 | // If it's derived from wxSizer then there may | |
338 | // already be a pointer to a Python object that we can use | |
339 | // in the OOR data. | |
340 | wxSizer* sz = (wxSizer*)source; | |
341 | wxPyClientData* data = (wxPyClientData*)sz->GetClientObject(); | |
342 | if (data) { | |
343 | target = data->m_obj; | |
344 | Py_INCREF(target); | |
345 | } | |
346 | } | |
347 | if (! target) { | |
348 | target = wxPyMake_wxObject(source, FALSE); | |
349 | if (target != Py_None) | |
350 | ((wxSizer*)source)->SetClientObject(new wxPyClientData(target)); | |
351 | } | |
352 | return target; | |
353 | } | |
354 | ||
355 | ||
356 | ||
357 | //--------------------------------------------------------------------------- | |
358 | ||
359 | PyObject* wxPyConstructObject(void* ptr, | |
360 | const char* className, | |
361 | PyObject* klass, | |
362 | int setThisOwn) { | |
363 | ||
364 | PyObject* obj; | |
365 | PyObject* arg; | |
366 | PyObject* item; | |
367 | char swigptr[64]; // should always be big enough... | |
368 | char buff[64]; | |
369 | ||
370 | if ((item = PyDict_GetItemString(wxPyPtrTypeMap, (char*)className)) != NULL) { | |
371 | className = PyString_AsString(item); | |
372 | } | |
373 | sprintf(buff, "_%s_p", className); | |
374 | SWIG_MakePtr(swigptr, ptr, buff); | |
375 | ||
376 | arg = Py_BuildValue("(s)", swigptr); | |
377 | obj = PyInstance_New(klass, arg, NULL); | |
378 | Py_DECREF(arg); | |
379 | ||
380 | if (setThisOwn) { | |
381 | PyObject* one = PyInt_FromLong(1); | |
382 | PyObject_SetAttrString(obj, "thisown", one); | |
383 | Py_DECREF(one); | |
384 | } | |
385 | ||
386 | return obj; | |
387 | } | |
388 | ||
389 | ||
390 | PyObject* wxPyConstructObject(void* ptr, | |
391 | const char* className, | |
392 | int setThisOwn) { | |
393 | PyObject* obj; | |
394 | ||
395 | if (!ptr) { | |
396 | Py_INCREF(Py_None); | |
397 | return Py_None; | |
398 | } | |
399 | ||
400 | char buff[64]; // should always be big enough... | |
401 | sprintf(buff, "%sPtr", className); | |
402 | ||
403 | wxASSERT_MSG(wxPython_dict, "wxPython_dict is not set yet!!"); | |
404 | ||
405 | PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff); | |
406 | if (! classobj) { | |
407 | char temp[128]; | |
408 | sprintf(temp, | |
409 | "*** Unknown class name %s, tell Robin about it please ***", | |
410 | buff); | |
411 | obj = PyString_FromString(temp); | |
412 | return obj; | |
413 | } | |
414 | ||
415 | return wxPyConstructObject(ptr, className, classobj, setThisOwn); | |
416 | } | |
417 | ||
418 | //--------------------------------------------------------------------------- | |
419 | ||
420 | ||
421 | wxPyTState* wxPyBeginBlockThreads() { | |
422 | wxPyTState* state = NULL; | |
423 | #ifdef WXP_WITH_THREAD | |
424 | if (1) { // Can I check if I've already got the lock? | |
425 | state = new wxPyTState; | |
426 | PyEval_AcquireLock(); | |
427 | state->newState = PyThreadState_New(wxPyInterpreter); | |
428 | state->prevState = PyThreadState_Swap(state->newState); | |
429 | } | |
430 | #endif | |
431 | return state; | |
432 | } | |
433 | ||
434 | ||
435 | void wxPyEndBlockThreads(wxPyTState* state) { | |
436 | #ifdef WXP_WITH_THREAD | |
437 | if (state) { | |
438 | PyThreadState_Swap(state->prevState); | |
439 | PyThreadState_Clear(state->newState); | |
440 | PyEval_ReleaseLock(); | |
441 | PyThreadState_Delete(state->newState); | |
442 | delete state; | |
443 | } | |
444 | #endif | |
445 | } | |
446 | ||
447 | ||
448 | //--------------------------------------------------------------------------- | |
449 | ||
450 | IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject); | |
451 | ||
452 | wxPyCallback::wxPyCallback(PyObject* func) { | |
453 | m_func = func; | |
454 | Py_INCREF(m_func); | |
455 | } | |
456 | ||
457 | wxPyCallback::wxPyCallback(const wxPyCallback& other) { | |
458 | m_func = other.m_func; | |
459 | Py_INCREF(m_func); | |
460 | } | |
461 | ||
462 | wxPyCallback::~wxPyCallback() { | |
463 | wxPyTState* state = wxPyBeginBlockThreads(); | |
464 | Py_DECREF(m_func); | |
465 | wxPyEndBlockThreads(state); | |
466 | } | |
467 | ||
468 | ||
469 | ||
470 | // This function is used for all events destined for Python event handlers. | |
471 | void wxPyCallback::EventThunker(wxEvent& event) { | |
472 | wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData; | |
473 | PyObject* func = cb->m_func; | |
474 | PyObject* result; | |
475 | PyObject* arg; | |
476 | PyObject* tuple; | |
477 | ||
478 | ||
479 | wxPyTState* state = wxPyBeginBlockThreads(); | |
480 | wxString className = event.GetClassInfo()->GetClassName(); | |
481 | ||
482 | if (className == "wxPyEvent") | |
483 | arg = ((wxPyEvent*)&event)->GetSelf(); | |
484 | else if (className == "wxPyCommandEvent") | |
485 | arg = ((wxPyCommandEvent*)&event)->GetSelf(); | |
486 | else | |
487 | arg = wxPyConstructObject((void*)&event, className); | |
488 | ||
489 | tuple = PyTuple_New(1); | |
490 | PyTuple_SET_ITEM(tuple, 0, arg); | |
491 | result = PyEval_CallObject(func, tuple); | |
492 | Py_DECREF(tuple); | |
493 | if (result) { | |
494 | Py_DECREF(result); | |
495 | PyErr_Clear(); // Just in case... | |
496 | } else { | |
497 | PyErr_Print(); | |
498 | } | |
499 | wxPyEndBlockThreads(state); | |
500 | } | |
501 | ||
502 | ||
503 | //---------------------------------------------------------------------- | |
504 | ||
505 | wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) { | |
506 | m_lastFound = NULL; | |
507 | m_self = other.m_self; | |
508 | m_class = other.m_class; | |
509 | if (m_self) { | |
510 | Py_INCREF(m_self); | |
511 | Py_INCREF(m_class); | |
512 | } | |
513 | } | |
514 | ||
515 | ||
516 | void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) { | |
517 | m_self = self; | |
518 | m_class = klass; | |
519 | m_incRef = incref; | |
520 | if (incref) { | |
521 | Py_INCREF(m_self); | |
522 | Py_INCREF(m_class); | |
523 | } | |
524 | } | |
525 | ||
526 | ||
527 | #if PYTHON_API_VERSION >= 1011 | |
528 | ||
529 | // Prior to Python 2.2 PyMethod_GetClass returned the class object | |
530 | // in which the method was defined. Starting with 2.2 it returns | |
531 | // "class that asked for the method" which seems totally bogus to me | |
532 | // but apprently if fixes some obscure problem waiting to happen in | |
533 | // Python. Since the API was not documented Guido and the gang felt | |
534 | // safe in changing it. Needless to say that totally screwed up the | |
535 | // logic below in wxPyCallbackHelper::findCallback, hence this icky | |
536 | // code to find the class where the method is actuallt defined... | |
537 | ||
538 | static | |
539 | PyObject* PyFindClassWithAttr(PyObject *klass, PyObject *name) | |
540 | { | |
541 | int i, n; | |
542 | ||
543 | if (PyType_Check(klass)) { // new style classes | |
544 | // This code is borrowed/adapted from _PyType_Lookup in typeobject.c | |
545 | // (TODO: This part is not tested yet, so I'm not sure it is correct...) | |
546 | PyTypeObject* type = (PyTypeObject*)klass; | |
547 | PyObject *mro, *res, *base, *dict; | |
548 | /* Look in tp_dict of types in MRO */ | |
549 | mro = type->tp_mro; | |
550 | assert(PyTuple_Check(mro)); | |
551 | n = PyTuple_GET_SIZE(mro); | |
552 | for (i = 0; i < n; i++) { | |
553 | base = PyTuple_GET_ITEM(mro, i); | |
554 | if (PyClass_Check(base)) | |
555 | dict = ((PyClassObject *)base)->cl_dict; | |
556 | else { | |
557 | assert(PyType_Check(base)); | |
558 | dict = ((PyTypeObject *)base)->tp_dict; | |
559 | } | |
560 | assert(dict && PyDict_Check(dict)); | |
561 | res = PyDict_GetItem(dict, name); | |
562 | if (res != NULL) | |
563 | return base; | |
564 | } | |
565 | return NULL; | |
566 | } | |
567 | ||
568 | else if (PyClass_Check(klass)) { // old style classes | |
569 | // This code is borrowed/adapted from class_lookup in classobject.c | |
570 | PyClassObject* cp = (PyClassObject*)klass; | |
571 | PyObject *value = PyDict_GetItem(cp->cl_dict, name); | |
572 | if (value != NULL) { | |
573 | return (PyObject*)cp; | |
574 | } | |
575 | n = PyTuple_Size(cp->cl_bases); | |
576 | for (i = 0; i < n; i++) { | |
577 | PyObject* base = PyTuple_GetItem(cp->cl_bases, i); | |
578 | PyObject *v = PyFindClassWithAttr(base, name); | |
579 | if (v != NULL) | |
580 | return v; | |
581 | } | |
582 | return NULL; | |
583 | } | |
584 | } | |
585 | #endif | |
586 | ||
587 | ||
588 | static | |
589 | PyObject* PyMethod_GetDefiningClass(PyObject* method, const char* name) | |
590 | { | |
591 | PyObject* mgc = PyMethod_GET_CLASS(method); | |
592 | ||
593 | #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way | |
594 | return mgc; | |
595 | #else // 2.2 and after, the hard way... | |
596 | ||
597 | PyObject* nameo = PyString_FromString(name); | |
598 | PyObject* klass = PyFindClassWithAttr(mgc, nameo); | |
599 | Py_DECREF(nameo); | |
600 | return klass; | |
601 | #endif | |
602 | } | |
603 | ||
604 | ||
605 | ||
606 | bool wxPyCallbackHelper::findCallback(const char* name) const { | |
607 | wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const | |
608 | self->m_lastFound = NULL; | |
609 | ||
610 | // If the object (m_self) has an attibute of the given name... | |
611 | if (m_self && PyObject_HasAttrString(m_self, (char*)name)) { | |
612 | PyObject *method, *klass; | |
613 | method = PyObject_GetAttrString(m_self, (char*)name); | |
614 | ||
615 | // ...and if that attribute is a method, and if that method's class is | |
616 | // not from a base class... | |
617 | if (PyMethod_Check(method) && | |
618 | (klass = PyMethod_GetDefiningClass(method, (char*)name)) != NULL && | |
619 | ((klass == m_class) || PyClass_IsSubclass(klass, m_class))) { | |
620 | ||
621 | // ...then we'll save a pointer to the method so callCallback can call it. | |
622 | self->m_lastFound = method; | |
623 | } | |
624 | else { | |
625 | Py_DECREF(method); | |
626 | } | |
627 | } | |
628 | return m_lastFound != NULL; | |
629 | } | |
630 | ||
631 | ||
632 | int wxPyCallbackHelper::callCallback(PyObject* argTuple) const { | |
633 | PyObject* result; | |
634 | int retval = FALSE; | |
635 | ||
636 | result = callCallbackObj(argTuple); | |
637 | if (result) { // Assumes an integer return type... | |
638 | retval = PyInt_AsLong(result); | |
639 | Py_DECREF(result); | |
640 | PyErr_Clear(); // forget about it if it's not... | |
641 | } | |
642 | return retval; | |
643 | } | |
644 | ||
645 | // Invoke the Python callable object, returning the raw PyObject return | |
646 | // value. Caller should DECREF the return value and also call PyEval_SaveThread. | |
647 | PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const { | |
648 | PyObject* result; | |
649 | ||
650 | // Save a copy of the pointer in case the callback generates another | |
651 | // callback. In that case m_lastFound will have a different value when | |
652 | // it gets back here... | |
653 | PyObject* method = m_lastFound; | |
654 | ||
655 | result = PyEval_CallObject(method, argTuple); | |
656 | Py_DECREF(argTuple); | |
657 | Py_DECREF(method); | |
658 | if (!result) { | |
659 | PyErr_Print(); | |
660 | } | |
661 | return result; | |
662 | } | |
663 | ||
664 | ||
665 | void wxPyCBH_setCallbackInfo(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) { | |
666 | cbh.setSelf(self, klass, incref); | |
667 | } | |
668 | ||
669 | bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) { | |
670 | return cbh.findCallback(name); | |
671 | } | |
672 | ||
673 | int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) { | |
674 | return cbh.callCallback(argTuple); | |
675 | } | |
676 | ||
677 | PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) { | |
678 | return cbh.callCallbackObj(argTuple); | |
679 | } | |
680 | ||
681 | ||
682 | void wxPyCBH_delete(wxPyCallbackHelper* cbh) { | |
683 | if (cbh->m_incRef) { | |
684 | wxPyTState* state = wxPyBeginBlockThreads(); | |
685 | Py_XDECREF(cbh->m_self); | |
686 | Py_XDECREF(cbh->m_class); | |
687 | wxPyEndBlockThreads(state); | |
688 | } | |
689 | } | |
690 | ||
691 | //--------------------------------------------------------------------------- | |
692 | //--------------------------------------------------------------------------- | |
693 | // These event classes can be derived from in Python and passed through the event | |
694 | // system without losing anything. They do this by keeping a reference to | |
695 | // themselves and some special case handling in wxPyCallback::EventThunker. | |
696 | ||
697 | ||
698 | wxPyEvtSelfRef::wxPyEvtSelfRef() { | |
699 | //m_self = Py_None; // **** We don't do normal ref counting to prevent | |
700 | //Py_INCREF(m_self); // circular loops... | |
701 | m_cloned = FALSE; | |
702 | } | |
703 | ||
704 | wxPyEvtSelfRef::~wxPyEvtSelfRef() { | |
705 | wxPyTState* state = wxPyBeginBlockThreads(); | |
706 | if (m_cloned) | |
707 | Py_DECREF(m_self); | |
708 | wxPyEndBlockThreads(state); | |
709 | } | |
710 | ||
711 | void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) { | |
712 | wxPyTState* state = wxPyBeginBlockThreads(); | |
713 | if (m_cloned) | |
714 | Py_DECREF(m_self); | |
715 | m_self = self; | |
716 | if (clone) { | |
717 | Py_INCREF(m_self); | |
718 | m_cloned = TRUE; | |
719 | } | |
720 | wxPyEndBlockThreads(state); | |
721 | } | |
722 | ||
723 | PyObject* wxPyEvtSelfRef::GetSelf() const { | |
724 | Py_INCREF(m_self); | |
725 | return m_self; | |
726 | } | |
727 | ||
728 | ||
729 | IMPLEMENT_ABSTRACT_CLASS(wxPyEvent, wxEvent); | |
730 | IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent, wxCommandEvent); | |
731 | ||
732 | ||
733 | wxPyEvent::wxPyEvent(int id) | |
734 | : wxEvent(id) { | |
735 | } | |
736 | ||
737 | ||
738 | wxPyEvent::wxPyEvent(const wxPyEvent& evt) | |
739 | : wxEvent(evt) | |
740 | { | |
741 | SetSelf(evt.m_self, TRUE); | |
742 | } | |
743 | ||
744 | ||
745 | wxPyEvent::~wxPyEvent() { | |
746 | } | |
747 | ||
748 | ||
749 | wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id) | |
750 | : wxCommandEvent(commandType, id) { | |
751 | } | |
752 | ||
753 | ||
754 | wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent& evt) | |
755 | : wxCommandEvent(evt) | |
756 | { | |
757 | SetSelf(evt.m_self, TRUE); | |
758 | } | |
759 | ||
760 | ||
761 | wxPyCommandEvent::~wxPyCommandEvent() { | |
762 | } | |
763 | ||
764 | ||
765 | ||
766 | ||
767 | //--------------------------------------------------------------------------- | |
768 | //--------------------------------------------------------------------------- | |
769 | ||
770 | ||
771 | wxPyTimer::wxPyTimer(PyObject* callback) { | |
772 | func = callback; | |
773 | Py_INCREF(func); | |
774 | } | |
775 | ||
776 | wxPyTimer::~wxPyTimer() { | |
777 | wxPyTState* state = wxPyBeginBlockThreads(); | |
778 | Py_DECREF(func); | |
779 | wxPyEndBlockThreads(state); | |
780 | } | |
781 | ||
782 | void wxPyTimer::Notify() { | |
783 | if (!func || func == Py_None) { | |
784 | wxTimer::Notify(); | |
785 | } | |
786 | else { | |
787 | wxPyTState* state = wxPyBeginBlockThreads(); | |
788 | ||
789 | PyObject* result; | |
790 | PyObject* args = Py_BuildValue("()"); | |
791 | ||
792 | result = PyEval_CallObject(func, args); | |
793 | Py_DECREF(args); | |
794 | if (result) { | |
795 | Py_DECREF(result); | |
796 | PyErr_Clear(); | |
797 | } else { | |
798 | PyErr_Print(); | |
799 | } | |
800 | ||
801 | wxPyEndBlockThreads(state); | |
802 | } | |
803 | } | |
804 | ||
805 | ||
806 | ||
807 | //--------------------------------------------------------------------------- | |
808 | //--------------------------------------------------------------------------- | |
809 | // Convert a wxList to a Python List | |
810 | ||
811 | PyObject* wxPy_ConvertList(wxListBase* list, const char* className) { | |
812 | PyObject* pyList; | |
813 | PyObject* pyObj; | |
814 | wxObject* wxObj; | |
815 | wxNode* node = list->First(); | |
816 | ||
817 | wxPyTState* state = wxPyBeginBlockThreads(); | |
818 | pyList = PyList_New(0); | |
819 | while (node) { | |
820 | wxObj = node->Data(); | |
821 | pyObj = wxPyMake_wxObject(wxObj); //wxPyConstructObject(wxObj, className); | |
822 | PyList_Append(pyList, pyObj); | |
823 | node = node->Next(); | |
824 | } | |
825 | wxPyEndBlockThreads(state); | |
826 | return pyList; | |
827 | } | |
828 | ||
829 | //---------------------------------------------------------------------- | |
830 | ||
831 | long wxPyGetWinHandle(wxWindow* win) { | |
832 | #ifdef __WXMSW__ | |
833 | return (long)win->GetHandle(); | |
834 | #endif | |
835 | ||
836 | // Find and return the actual X-Window. | |
837 | #ifdef __WXGTK__ | |
838 | if (win->m_wxwindow) { | |
839 | GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window; | |
840 | if (bwin) { | |
841 | return (long)bwin->xwindow; | |
842 | } | |
843 | } | |
844 | #endif | |
845 | return 0; | |
846 | } | |
847 | ||
848 | //---------------------------------------------------------------------- | |
849 | // Some helper functions for typemaps in my_typemaps.i, so they won't be | |
850 | // included in every file... | |
851 | ||
852 | ||
853 | byte* byte_LIST_helper(PyObject* source) { | |
854 | if (!PyList_Check(source)) { | |
855 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
856 | return NULL; | |
857 | } | |
858 | int count = PyList_Size(source); | |
859 | byte* temp = new byte[count]; | |
860 | if (! temp) { | |
861 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
862 | return NULL; | |
863 | } | |
864 | for (int x=0; x<count; x++) { | |
865 | PyObject* o = PyList_GetItem(source, x); | |
866 | if (! PyInt_Check(o)) { | |
867 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
868 | return NULL; | |
869 | } | |
870 | temp[x] = (byte)PyInt_AsLong(o); | |
871 | } | |
872 | return temp; | |
873 | } | |
874 | ||
875 | ||
876 | int* int_LIST_helper(PyObject* source) { | |
877 | if (!PyList_Check(source)) { | |
878 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
879 | return NULL; | |
880 | } | |
881 | int count = PyList_Size(source); | |
882 | int* temp = new int[count]; | |
883 | if (! temp) { | |
884 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
885 | return NULL; | |
886 | } | |
887 | for (int x=0; x<count; x++) { | |
888 | PyObject* o = PyList_GetItem(source, x); | |
889 | if (! PyInt_Check(o)) { | |
890 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
891 | return NULL; | |
892 | } | |
893 | temp[x] = PyInt_AsLong(o); | |
894 | } | |
895 | return temp; | |
896 | } | |
897 | ||
898 | ||
899 | long* long_LIST_helper(PyObject* source) { | |
900 | if (!PyList_Check(source)) { | |
901 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
902 | return NULL; | |
903 | } | |
904 | int count = PyList_Size(source); | |
905 | long* temp = new long[count]; | |
906 | if (! temp) { | |
907 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
908 | return NULL; | |
909 | } | |
910 | for (int x=0; x<count; x++) { | |
911 | PyObject* o = PyList_GetItem(source, x); | |
912 | if (! PyInt_Check(o)) { | |
913 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
914 | return NULL; | |
915 | } | |
916 | temp[x] = PyInt_AsLong(o); | |
917 | } | |
918 | return temp; | |
919 | } | |
920 | ||
921 | ||
922 | char** string_LIST_helper(PyObject* source) { | |
923 | if (!PyList_Check(source)) { | |
924 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
925 | return NULL; | |
926 | } | |
927 | int count = PyList_Size(source); | |
928 | char** temp = new char*[count]; | |
929 | if (! temp) { | |
930 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
931 | return NULL; | |
932 | } | |
933 | for (int x=0; x<count; x++) { | |
934 | PyObject* o = PyList_GetItem(source, x); | |
935 | if (! PyString_Check(o)) { | |
936 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
937 | return NULL; | |
938 | } | |
939 | temp[x] = PyString_AsString(o); | |
940 | } | |
941 | return temp; | |
942 | } | |
943 | ||
944 | //-------------------------------- | |
945 | // Part of patch from Tim Hochberg | |
946 | static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) { | |
947 | if (PyInt_Check(o1) && PyInt_Check(o2)) { | |
948 | point->x = PyInt_AS_LONG(o1); | |
949 | point->y = PyInt_AS_LONG(o2); | |
950 | return true; | |
951 | } | |
952 | if (PyFloat_Check(o1) && PyFloat_Check(o2)) { | |
953 | point->x = (int)PyFloat_AS_DOUBLE(o1); | |
954 | point->y = (int)PyFloat_AS_DOUBLE(o2); | |
955 | return true; | |
956 | } | |
957 | if (PyInstance_Check(o1) || PyInstance_Check(o2)) { | |
958 | // Disallow instances because they can cause havok | |
959 | return false; | |
960 | } | |
961 | if (PyNumber_Check(o1) && PyNumber_Check(o2)) { | |
962 | // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2 | |
963 | point->x = PyInt_AsLong(o1); | |
964 | point->y = PyInt_AsLong(o2); | |
965 | return true; | |
966 | } | |
967 | return false; | |
968 | } | |
969 | ||
970 | ||
971 | wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) { | |
972 | // Putting all of the declarations here allows | |
973 | // us to put the error handling all in one place. | |
974 | int x; | |
975 | wxPoint* temp; | |
976 | PyObject *o, *o1, *o2; | |
977 | bool isFast = PyList_Check(source) || PyTuple_Check(source); | |
978 | ||
979 | if (!PySequence_Check(source)) { | |
980 | goto error0; | |
981 | } | |
982 | ||
983 | // The length of the sequence is returned in count. | |
984 | *count = PySequence_Length(source); | |
985 | if (*count < 0) { | |
986 | goto error0; | |
987 | } | |
988 | ||
989 | temp = new wxPoint[*count]; | |
990 | if (!temp) { | |
991 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
992 | return NULL; | |
993 | } | |
994 | for (x=0; x<*count; x++) { | |
995 | // Get an item: try fast way first. | |
996 | if (isFast) { | |
997 | o = PySequence_Fast_GET_ITEM(source, x); | |
998 | } | |
999 | else { | |
1000 | o = PySequence_GetItem(source, x); | |
1001 | if (o == NULL) { | |
1002 | goto error1; | |
1003 | } | |
1004 | } | |
1005 | ||
1006 | // Convert o to wxPoint. | |
1007 | if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) || | |
1008 | (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) { | |
1009 | o1 = PySequence_Fast_GET_ITEM(o, 0); | |
1010 | o2 = PySequence_Fast_GET_ITEM(o, 1); | |
1011 | if (!wxPointFromObjects(o1, o2, &temp[x])) { | |
1012 | goto error2; | |
1013 | } | |
1014 | } | |
1015 | else if (PyInstance_Check(o)) { | |
1016 | wxPoint* pt; | |
1017 | if (SWIG_GetPtrObj(o, (void **)&pt, "_wxPoint_p")) { | |
1018 | goto error2; | |
1019 | } | |
1020 | temp[x] = *pt; | |
1021 | } | |
1022 | else if (PySequence_Check(o) && PySequence_Length(o) == 2) { | |
1023 | o1 = PySequence_GetItem(o, 0); | |
1024 | o2 = PySequence_GetItem(o, 1); | |
1025 | if (!wxPointFromObjects(o1, o2, &temp[x])) { | |
1026 | goto error3; | |
1027 | } | |
1028 | Py_DECREF(o1); | |
1029 | Py_DECREF(o2); | |
1030 | } | |
1031 | else { | |
1032 | goto error2; | |
1033 | } | |
1034 | // Clean up. | |
1035 | if (!isFast) | |
1036 | Py_DECREF(o); | |
1037 | } | |
1038 | return temp; | |
1039 | ||
1040 | error3: | |
1041 | Py_DECREF(o1); | |
1042 | Py_DECREF(o2); | |
1043 | error2: | |
1044 | if (!isFast) | |
1045 | Py_DECREF(o); | |
1046 | error1: | |
1047 | delete temp; | |
1048 | error0: | |
1049 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints."); | |
1050 | return NULL; | |
1051 | } | |
1052 | // end of patch | |
1053 | //------------------------------ | |
1054 | ||
1055 | ||
1056 | wxBitmap** wxBitmap_LIST_helper(PyObject* source) { | |
1057 | if (!PyList_Check(source)) { | |
1058 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1059 | return NULL; | |
1060 | } | |
1061 | int count = PyList_Size(source); | |
1062 | wxBitmap** temp = new wxBitmap*[count]; | |
1063 | if (! temp) { | |
1064 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1065 | return NULL; | |
1066 | } | |
1067 | for (int x=0; x<count; x++) { | |
1068 | PyObject* o = PyList_GetItem(source, x); | |
1069 | if (PyInstance_Check(o)) { | |
1070 | wxBitmap* pt; | |
1071 | if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) { | |
1072 | PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p."); | |
1073 | return NULL; | |
1074 | } | |
1075 | temp[x] = pt; | |
1076 | } | |
1077 | else { | |
1078 | PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps."); | |
1079 | return NULL; | |
1080 | } | |
1081 | } | |
1082 | return temp; | |
1083 | } | |
1084 | ||
1085 | ||
1086 | ||
1087 | wxString* wxString_LIST_helper(PyObject* source) { | |
1088 | if (!PyList_Check(source)) { | |
1089 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1090 | return NULL; | |
1091 | } | |
1092 | int count = PyList_Size(source); | |
1093 | wxString* temp = new wxString[count]; | |
1094 | if (! temp) { | |
1095 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1096 | return NULL; | |
1097 | } | |
1098 | for (int x=0; x<count; x++) { | |
1099 | PyObject* o = PyList_GetItem(source, x); | |
1100 | #if PYTHON_API_VERSION >= 1009 | |
1101 | if (! PyString_Check(o) && ! PyUnicode_Check(o)) { | |
1102 | PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects."); | |
1103 | return NULL; | |
1104 | } | |
1105 | ||
1106 | char* buff; | |
1107 | int length; | |
1108 | if (PyString_AsStringAndSize(o, &buff, &length) == -1) | |
1109 | return NULL; | |
1110 | temp[x] = wxString(buff, length); | |
1111 | #else | |
1112 | if (! PyString_Check(o)) { | |
1113 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
1114 | return NULL; | |
1115 | } | |
1116 | temp[x] = PyString_AsString(o); | |
1117 | #endif | |
1118 | } | |
1119 | return temp; | |
1120 | } | |
1121 | ||
1122 | ||
1123 | wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) { | |
1124 | if (!PyList_Check(source)) { | |
1125 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1126 | return NULL; | |
1127 | } | |
1128 | int count = PyList_Size(source); | |
1129 | wxAcceleratorEntry* temp = new wxAcceleratorEntry[count]; | |
1130 | if (! temp) { | |
1131 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1132 | return NULL; | |
1133 | } | |
1134 | for (int x=0; x<count; x++) { | |
1135 | PyObject* o = PyList_GetItem(source, x); | |
1136 | if (PyInstance_Check(o)) { | |
1137 | wxAcceleratorEntry* ae; | |
1138 | if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) { | |
1139 | PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p."); | |
1140 | return NULL; | |
1141 | } | |
1142 | temp[x] = *ae; | |
1143 | } | |
1144 | else if (PyTuple_Check(o)) { | |
1145 | PyObject* o1 = PyTuple_GetItem(o, 0); | |
1146 | PyObject* o2 = PyTuple_GetItem(o, 1); | |
1147 | PyObject* o3 = PyTuple_GetItem(o, 2); | |
1148 | temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3)); | |
1149 | } | |
1150 | else { | |
1151 | PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects."); | |
1152 | return NULL; | |
1153 | } | |
1154 | } | |
1155 | return temp; | |
1156 | } | |
1157 | ||
1158 | ||
1159 | wxPen** wxPen_LIST_helper(PyObject* source) { | |
1160 | if (!PyList_Check(source)) { | |
1161 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1162 | return NULL; | |
1163 | } | |
1164 | int count = PyList_Size(source); | |
1165 | wxPen** temp = new wxPen*[count]; | |
1166 | if (!temp) { | |
1167 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1168 | return NULL; | |
1169 | } | |
1170 | for (int x=0; x<count; x++) { | |
1171 | PyObject* o = PyList_GetItem(source, x); | |
1172 | if (PyInstance_Check(o)) { | |
1173 | wxPen* pt; | |
1174 | if (SWIG_GetPtrObj(o, (void **) &pt,"_wxPen_p")) { | |
1175 | delete temp; | |
1176 | PyErr_SetString(PyExc_TypeError,"Expected _wxPen_p."); | |
1177 | return NULL; | |
1178 | } | |
1179 | temp[x] = pt; | |
1180 | } | |
1181 | else { | |
1182 | delete temp; | |
1183 | PyErr_SetString(PyExc_TypeError, "Expected a list of wxPens."); | |
1184 | return NULL; | |
1185 | } | |
1186 | } | |
1187 | return temp; | |
1188 | } | |
1189 | ||
1190 | ||
1191 | bool _2int_seq_helper(PyObject* source, int* i1, int* i2) { | |
1192 | bool isFast = PyList_Check(source) || PyTuple_Check(source); | |
1193 | PyObject *o1, *o2; | |
1194 | ||
1195 | if (!PySequence_Check(source) || PySequence_Length(source) != 2) | |
1196 | return FALSE; | |
1197 | ||
1198 | if (isFast) { | |
1199 | o1 = PySequence_Fast_GET_ITEM(source, 0); | |
1200 | o2 = PySequence_Fast_GET_ITEM(source, 1); | |
1201 | } | |
1202 | else { | |
1203 | o1 = PySequence_GetItem(source, 0); | |
1204 | o2 = PySequence_GetItem(source, 1); | |
1205 | } | |
1206 | ||
1207 | *i1 = PyInt_AsLong(o1); | |
1208 | *i2 = PyInt_AsLong(o2); | |
1209 | ||
1210 | if (! isFast) { | |
1211 | Py_DECREF(o1); | |
1212 | Py_DECREF(o2); | |
1213 | } | |
1214 | return TRUE; | |
1215 | } | |
1216 | ||
1217 | ||
1218 | bool _4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4) { | |
1219 | bool isFast = PyList_Check(source) || PyTuple_Check(source); | |
1220 | PyObject *o1, *o2, *o3, *o4; | |
1221 | ||
1222 | if (!PySequence_Check(source) || PySequence_Length(source) != 4) | |
1223 | return FALSE; | |
1224 | ||
1225 | if (isFast) { | |
1226 | o1 = PySequence_Fast_GET_ITEM(source, 0); | |
1227 | o2 = PySequence_Fast_GET_ITEM(source, 1); | |
1228 | o3 = PySequence_Fast_GET_ITEM(source, 2); | |
1229 | o4 = PySequence_Fast_GET_ITEM(source, 3); | |
1230 | } | |
1231 | else { | |
1232 | o1 = PySequence_GetItem(source, 0); | |
1233 | o2 = PySequence_GetItem(source, 1); | |
1234 | o3 = PySequence_GetItem(source, 2); | |
1235 | o4 = PySequence_GetItem(source, 3); | |
1236 | } | |
1237 | ||
1238 | *i1 = PyInt_AsLong(o1); | |
1239 | *i2 = PyInt_AsLong(o2); | |
1240 | *i3 = PyInt_AsLong(o3); | |
1241 | *i4 = PyInt_AsLong(o4); | |
1242 | ||
1243 | if (! isFast) { | |
1244 | Py_DECREF(o1); | |
1245 | Py_DECREF(o2); | |
1246 | Py_DECREF(o3); | |
1247 | Py_DECREF(o4); | |
1248 | } | |
1249 | return TRUE; | |
1250 | } | |
1251 | ||
1252 | ||
1253 | //---------------------------------------------------------------------- | |
1254 | ||
1255 | bool wxSize_helper(PyObject* source, wxSize** obj) { | |
1256 | ||
1257 | // If source is an object instance then it may already be the right type | |
1258 | if (PyInstance_Check(source)) { | |
1259 | wxSize* ptr; | |
1260 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p")) | |
1261 | goto error; | |
1262 | *obj = ptr; | |
1263 | return TRUE; | |
1264 | } | |
1265 | // otherwise a 2-tuple of integers is expected | |
1266 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
1267 | PyObject* o1 = PySequence_GetItem(source, 0); | |
1268 | PyObject* o2 = PySequence_GetItem(source, 1); | |
1269 | **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2)); | |
1270 | return TRUE; | |
1271 | } | |
1272 | ||
1273 | error: | |
1274 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object."); | |
1275 | return FALSE; | |
1276 | } | |
1277 | ||
1278 | bool wxPoint_helper(PyObject* source, wxPoint** obj) { | |
1279 | ||
1280 | // If source is an object instance then it may already be the right type | |
1281 | if (PyInstance_Check(source)) { | |
1282 | wxPoint* ptr; | |
1283 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p")) | |
1284 | goto error; | |
1285 | *obj = ptr; | |
1286 | return TRUE; | |
1287 | } | |
1288 | // otherwise a length-2 sequence of integers is expected | |
1289 | if (PySequence_Check(source) && PySequence_Length(source) == 2) { | |
1290 | PyObject* o1 = PySequence_GetItem(source, 0); | |
1291 | PyObject* o2 = PySequence_GetItem(source, 1); | |
1292 | // This should really check for integers, not numbers -- but that would break code. | |
1293 | if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) { | |
1294 | Py_DECREF(o1); | |
1295 | Py_DECREF(o2); | |
1296 | goto error; | |
1297 | } | |
1298 | **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2)); | |
1299 | Py_DECREF(o1); | |
1300 | Py_DECREF(o2); | |
1301 | return TRUE; | |
1302 | } | |
1303 | error: | |
1304 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object."); | |
1305 | return FALSE; | |
1306 | } | |
1307 | ||
1308 | ||
1309 | ||
1310 | bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) { | |
1311 | ||
1312 | // If source is an object instance then it may already be the right type | |
1313 | if (PyInstance_Check(source)) { | |
1314 | wxRealPoint* ptr; | |
1315 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p")) | |
1316 | goto error; | |
1317 | *obj = ptr; | |
1318 | return TRUE; | |
1319 | } | |
1320 | // otherwise a 2-tuple of floats is expected | |
1321 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
1322 | PyObject* o1 = PySequence_GetItem(source, 0); | |
1323 | PyObject* o2 = PySequence_GetItem(source, 1); | |
1324 | **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2)); | |
1325 | return TRUE; | |
1326 | } | |
1327 | ||
1328 | error: | |
1329 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object."); | |
1330 | return FALSE; | |
1331 | } | |
1332 | ||
1333 | ||
1334 | ||
1335 | ||
1336 | bool wxRect_helper(PyObject* source, wxRect** obj) { | |
1337 | ||
1338 | // If source is an object instance then it may already be the right type | |
1339 | if (PyInstance_Check(source)) { | |
1340 | wxRect* ptr; | |
1341 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p")) | |
1342 | goto error; | |
1343 | *obj = ptr; | |
1344 | return TRUE; | |
1345 | } | |
1346 | // otherwise a 4-tuple of integers is expected | |
1347 | else if (PySequence_Check(source) && PyObject_Length(source) == 4) { | |
1348 | PyObject* o1 = PySequence_GetItem(source, 0); | |
1349 | PyObject* o2 = PySequence_GetItem(source, 1); | |
1350 | PyObject* o3 = PySequence_GetItem(source, 2); | |
1351 | PyObject* o4 = PySequence_GetItem(source, 3); | |
1352 | **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2), | |
1353 | PyInt_AsLong(o3), PyInt_AsLong(o4)); | |
1354 | return TRUE; | |
1355 | } | |
1356 | ||
1357 | error: | |
1358 | PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object."); | |
1359 | return FALSE; | |
1360 | } | |
1361 | ||
1362 | ||
1363 | ||
1364 | bool wxColour_helper(PyObject* source, wxColour** obj) { | |
1365 | ||
1366 | // If source is an object instance then it may already be the right type | |
1367 | if (PyInstance_Check(source)) { | |
1368 | wxColour* ptr; | |
1369 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p")) | |
1370 | goto error; | |
1371 | *obj = ptr; | |
1372 | return TRUE; | |
1373 | } | |
1374 | // otherwise a string is expected | |
1375 | else if (PyString_Check(source)) { | |
1376 | wxString spec = PyString_AS_STRING(source); | |
1377 | if (spec[0U] == '#' && spec.Length() == 7) { // It's #RRGGBB | |
1378 | char* junk; | |
1379 | int red = strtol(spec.Mid(1,2), &junk, 16); | |
1380 | int green = strtol(spec.Mid(3,2), &junk, 16); | |
1381 | int blue = strtol(spec.Mid(5,2), &junk, 16); | |
1382 | **obj = wxColour(red, green, blue); | |
1383 | return TRUE; | |
1384 | } | |
1385 | else { // it's a colour name | |
1386 | **obj = wxColour(spec); | |
1387 | return TRUE; | |
1388 | } | |
1389 | } | |
1390 | ||
1391 | error: | |
1392 | PyErr_SetString(PyExc_TypeError, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'."); | |
1393 | return FALSE; | |
1394 | } | |
1395 | ||
1396 | ||
1397 | //---------------------------------------------------------------------- | |
1398 | ||
1399 | PyObject* wxArrayString2PyList_helper(const wxArrayString& arr) { | |
1400 | ||
1401 | PyObject* list = PyList_New(0); | |
1402 | for (size_t i=0; i < arr.GetCount(); i++) { | |
1403 | PyObject* str = PyString_FromString(arr[i].c_str()); | |
1404 | PyList_Append(list, str); | |
1405 | Py_DECREF(str); | |
1406 | } | |
1407 | return list; | |
1408 | } | |
1409 | ||
1410 | ||
1411 | //---------------------------------------------------------------------- | |
1412 | //---------------------------------------------------------------------- | |
1413 | ||
1414 | ||
1415 | ||
1416 |