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