]>
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 | 405 | sprintf(buff, "%sPtr", className); |
c5943253 RD |
406 | |
407 | wxASSERT_MSG(wxPython_dict, "wxPython_dict is not set yet!!"); | |
408 | ||
7bf85405 RD |
409 | PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff); |
410 | if (! classobj) { | |
33510773 RD |
411 | char temp[128]; |
412 | sprintf(temp, | |
413 | "*** Unknown class name %s, tell Robin about it please ***", | |
414 | buff); | |
415 | obj = PyString_FromString(temp); | |
416 | return obj; | |
7bf85405 RD |
417 | } |
418 | ||
9416aa89 | 419 | return wxPyConstructObject(ptr, className, classobj, setThisOwn); |
7bf85405 RD |
420 | } |
421 | ||
d559219f | 422 | //--------------------------------------------------------------------------- |
7bf85405 | 423 | |
19a97bd6 RD |
424 | |
425 | wxPyTState* wxPyBeginBlockThreads() { | |
426 | wxPyTState* state = NULL; | |
cf694132 | 427 | #ifdef WXP_WITH_THREAD |
19a97bd6 RD |
428 | if (1) { // Can I check if I've already got the lock? |
429 | state = new wxPyTState; | |
430 | PyEval_AcquireLock(); | |
431 | state->newState = PyThreadState_New(wxPyInterpreter); | |
432 | state->prevState = PyThreadState_Swap(state->newState); | |
1112b0c6 | 433 | } |
cf694132 | 434 | #endif |
19a97bd6 | 435 | return state; |
d559219f | 436 | } |
cf694132 | 437 | |
cf694132 | 438 | |
19a97bd6 | 439 | void wxPyEndBlockThreads(wxPyTState* state) { |
cf694132 | 440 | #ifdef WXP_WITH_THREAD |
19a97bd6 RD |
441 | if (state) { |
442 | PyThreadState_Swap(state->prevState); | |
443 | PyThreadState_Clear(state->newState); | |
444 | PyEval_ReleaseLock(); | |
445 | PyThreadState_Delete(state->newState); | |
446 | delete state; | |
1112b0c6 RD |
447 | } |
448 | #endif | |
cf694132 RD |
449 | } |
450 | ||
19a97bd6 | 451 | |
d559219f RD |
452 | //--------------------------------------------------------------------------- |
453 | ||
2f90df85 RD |
454 | IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject); |
455 | ||
d559219f RD |
456 | wxPyCallback::wxPyCallback(PyObject* func) { |
457 | m_func = func; | |
458 | Py_INCREF(m_func); | |
459 | } | |
460 | ||
2f90df85 RD |
461 | wxPyCallback::wxPyCallback(const wxPyCallback& other) { |
462 | m_func = other.m_func; | |
463 | Py_INCREF(m_func); | |
464 | } | |
465 | ||
d559219f | 466 | wxPyCallback::~wxPyCallback() { |
19a97bd6 | 467 | wxPyTState* state = wxPyBeginBlockThreads(); |
d559219f | 468 | Py_DECREF(m_func); |
19a97bd6 | 469 | wxPyEndBlockThreads(state); |
d559219f RD |
470 | } |
471 | ||
cf694132 RD |
472 | |
473 | ||
7bf85405 RD |
474 | // This function is used for all events destined for Python event handlers. |
475 | void wxPyCallback::EventThunker(wxEvent& event) { | |
476 | wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData; | |
477 | PyObject* func = cb->m_func; | |
478 | PyObject* result; | |
479 | PyObject* arg; | |
480 | PyObject* tuple; | |
481 | ||
cf694132 | 482 | |
19a97bd6 | 483 | wxPyTState* state = wxPyBeginBlockThreads(); |
65dd82cb RD |
484 | wxString className = event.GetClassInfo()->GetClassName(); |
485 | ||
486 | if (className == "wxPyEvent") | |
487 | arg = ((wxPyEvent*)&event)->GetSelf(); | |
488 | else if (className == "wxPyCommandEvent") | |
489 | arg = ((wxPyCommandEvent*)&event)->GetSelf(); | |
490 | else | |
491 | arg = wxPyConstructObject((void*)&event, className); | |
7bf85405 RD |
492 | |
493 | tuple = PyTuple_New(1); | |
494 | PyTuple_SET_ITEM(tuple, 0, arg); | |
495 | result = PyEval_CallObject(func, tuple); | |
7bf85405 RD |
496 | Py_DECREF(tuple); |
497 | if (result) { | |
498 | Py_DECREF(result); | |
ac346f50 | 499 | PyErr_Clear(); // Just in case... |
7bf85405 RD |
500 | } else { |
501 | PyErr_Print(); | |
502 | } | |
19a97bd6 | 503 | wxPyEndBlockThreads(state); |
7bf85405 RD |
504 | } |
505 | ||
506 | ||
d559219f | 507 | //---------------------------------------------------------------------- |
7bf85405 | 508 | |
2f90df85 RD |
509 | wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) { |
510 | m_lastFound = NULL; | |
511 | m_self = other.m_self; | |
f6bcfd97 BP |
512 | m_class = other.m_class; |
513 | if (m_self) { | |
2f90df85 | 514 | Py_INCREF(m_self); |
f6bcfd97 BP |
515 | Py_INCREF(m_class); |
516 | } | |
2f90df85 RD |
517 | } |
518 | ||
519 | ||
33510773 | 520 | void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) { |
d559219f | 521 | m_self = self; |
33510773 | 522 | m_class = klass; |
b7312675 | 523 | m_incRef = incref; |
f6bcfd97 | 524 | if (incref) { |
2f90df85 | 525 | Py_INCREF(m_self); |
f6bcfd97 BP |
526 | Py_INCREF(m_class); |
527 | } | |
d559219f | 528 | } |
8bf5d46e | 529 | |
8bf5d46e | 530 | |
f6bcfd97 BP |
531 | // If the object (m_self) has an attibute of the given name, and if that |
532 | // attribute is a method, and if that method's class is not from a base class, | |
533 | // then we'll save a pointer to the method so callCallback can call it. | |
de20db99 | 534 | bool wxPyCallbackHelper::findCallback(const char* name) const { |
f6bcfd97 BP |
535 | wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const |
536 | self->m_lastFound = NULL; | |
de20db99 | 537 | if (m_self && PyObject_HasAttrString(m_self, (char*)name)) { |
f6bcfd97 | 538 | PyObject* method; |
de20db99 | 539 | method = PyObject_GetAttrString(m_self, (char*)name); |
f6bcfd97 BP |
540 | |
541 | if (PyMethod_Check(method) && | |
542 | ((PyMethod_GET_CLASS(method) == m_class) || | |
543 | PyClass_IsSubclass(PyMethod_GET_CLASS(method), m_class))) { | |
8bf5d46e | 544 | |
f6bcfd97 BP |
545 | self->m_lastFound = method; |
546 | } | |
de20db99 RD |
547 | else { |
548 | Py_DECREF(method); | |
549 | } | |
f6bcfd97 | 550 | } |
d559219f RD |
551 | return m_lastFound != NULL; |
552 | } | |
8bf5d46e | 553 | |
d559219f | 554 | |
f6bcfd97 | 555 | int wxPyCallbackHelper::callCallback(PyObject* argTuple) const { |
d559219f RD |
556 | PyObject* result; |
557 | int retval = FALSE; | |
558 | ||
559 | result = callCallbackObj(argTuple); | |
560 | if (result) { // Assumes an integer return type... | |
561 | retval = PyInt_AsLong(result); | |
562 | Py_DECREF(result); | |
563 | PyErr_Clear(); // forget about it if it's not... | |
564 | } | |
565 | return retval; | |
566 | } | |
567 | ||
568 | // Invoke the Python callable object, returning the raw PyObject return | |
569 | // value. Caller should DECREF the return value and also call PyEval_SaveThread. | |
f6bcfd97 | 570 | PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const { |
de20db99 | 571 | PyObject* result; |
d559219f | 572 | |
de20db99 RD |
573 | // Save a copy of the pointer in case the callback generates another |
574 | // callback. In that case m_lastFound will have a different value when | |
575 | // it gets back here... | |
576 | PyObject* method = m_lastFound; | |
577 | ||
578 | result = PyEval_CallObject(method, argTuple); | |
d559219f | 579 | Py_DECREF(argTuple); |
de20db99 | 580 | Py_DECREF(method); |
d559219f RD |
581 | if (!result) { |
582 | PyErr_Print(); | |
583 | } | |
584 | return result; | |
585 | } | |
714e6a9e | 586 | |
7bf85405 | 587 | |
0122b7e3 | 588 | void wxPyCBH_setCallbackInfo(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) { |
1e7ecb7b RD |
589 | cbh.setSelf(self, klass, incref); |
590 | } | |
591 | ||
592 | bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) { | |
593 | return cbh.findCallback(name); | |
594 | } | |
595 | ||
596 | int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) { | |
597 | return cbh.callCallback(argTuple); | |
598 | } | |
599 | ||
600 | PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) { | |
601 | return cbh.callCallbackObj(argTuple); | |
602 | } | |
603 | ||
604 | ||
605 | void wxPyCBH_delete(wxPyCallbackHelper* cbh) { | |
1e7ecb7b | 606 | if (cbh->m_incRef) { |
19a97bd6 | 607 | wxPyTState* state = wxPyBeginBlockThreads(); |
1e7ecb7b RD |
608 | Py_XDECREF(cbh->m_self); |
609 | Py_XDECREF(cbh->m_class); | |
19a97bd6 | 610 | wxPyEndBlockThreads(state); |
1e7ecb7b | 611 | } |
1e7ecb7b | 612 | } |
d559219f | 613 | |
65dd82cb RD |
614 | //--------------------------------------------------------------------------- |
615 | //--------------------------------------------------------------------------- | |
2aab8f16 | 616 | // These event classes can be derived from in Python and passed through the event |
c368d904 | 617 | // system without losing anything. They do this by keeping a reference to |
65dd82cb RD |
618 | // themselves and some special case handling in wxPyCallback::EventThunker. |
619 | ||
620 | ||
e19b7164 | 621 | wxPyEvtSelfRef::wxPyEvtSelfRef() { |
65dd82cb RD |
622 | //m_self = Py_None; // **** We don't do normal ref counting to prevent |
623 | //Py_INCREF(m_self); // circular loops... | |
194fa2ac | 624 | m_cloned = FALSE; |
65dd82cb RD |
625 | } |
626 | ||
e19b7164 | 627 | wxPyEvtSelfRef::~wxPyEvtSelfRef() { |
19a97bd6 | 628 | wxPyTState* state = wxPyBeginBlockThreads(); |
65dd82cb RD |
629 | if (m_cloned) |
630 | Py_DECREF(m_self); | |
19a97bd6 | 631 | wxPyEndBlockThreads(state); |
65dd82cb RD |
632 | } |
633 | ||
e19b7164 | 634 | void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) { |
19a97bd6 | 635 | wxPyTState* state = wxPyBeginBlockThreads(); |
65dd82cb RD |
636 | if (m_cloned) |
637 | Py_DECREF(m_self); | |
638 | m_self = self; | |
639 | if (clone) { | |
640 | Py_INCREF(m_self); | |
641 | m_cloned = TRUE; | |
642 | } | |
19a97bd6 | 643 | wxPyEndBlockThreads(state); |
65dd82cb RD |
644 | } |
645 | ||
e19b7164 | 646 | PyObject* wxPyEvtSelfRef::GetSelf() const { |
65dd82cb RD |
647 | Py_INCREF(m_self); |
648 | return m_self; | |
649 | } | |
650 | ||
651 | ||
07b2e1cd RD |
652 | IMPLEMENT_ABSTRACT_CLASS(wxPyEvent, wxEvent); |
653 | IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent, wxCommandEvent); | |
654 | ||
655 | ||
65dd82cb RD |
656 | wxPyEvent::wxPyEvent(int id) |
657 | : wxEvent(id) { | |
658 | } | |
659 | ||
65dd82cb | 660 | |
07b2e1cd RD |
661 | wxPyEvent::wxPyEvent(const wxPyEvent& evt) |
662 | : wxEvent(evt) | |
663 | { | |
664 | SetSelf(evt.m_self, TRUE); | |
65dd82cb RD |
665 | } |
666 | ||
667 | ||
07b2e1cd RD |
668 | wxPyEvent::~wxPyEvent() { |
669 | } | |
65dd82cb RD |
670 | |
671 | ||
672 | wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id) | |
673 | : wxCommandEvent(commandType, id) { | |
674 | } | |
675 | ||
65dd82cb | 676 | |
07b2e1cd RD |
677 | wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent& evt) |
678 | : wxCommandEvent(evt) | |
679 | { | |
680 | SetSelf(evt.m_self, TRUE); | |
65dd82cb RD |
681 | } |
682 | ||
683 | ||
07b2e1cd RD |
684 | wxPyCommandEvent::~wxPyCommandEvent() { |
685 | } | |
686 | ||
65dd82cb RD |
687 | |
688 | ||
689 | ||
d559219f | 690 | //--------------------------------------------------------------------------- |
7bf85405 RD |
691 | //--------------------------------------------------------------------------- |
692 | ||
d559219f | 693 | |
7bf85405 RD |
694 | wxPyTimer::wxPyTimer(PyObject* callback) { |
695 | func = callback; | |
696 | Py_INCREF(func); | |
697 | } | |
698 | ||
699 | wxPyTimer::~wxPyTimer() { | |
19a97bd6 | 700 | wxPyTState* state = wxPyBeginBlockThreads(); |
7bf85405 | 701 | Py_DECREF(func); |
19a97bd6 | 702 | wxPyEndBlockThreads(state); |
7bf85405 RD |
703 | } |
704 | ||
705 | void wxPyTimer::Notify() { | |
185d7c3e RD |
706 | if (!func || func == Py_None) { |
707 | wxTimer::Notify(); | |
708 | } | |
709 | else { | |
19a97bd6 | 710 | wxPyTState* state = wxPyBeginBlockThreads(); |
185d7c3e RD |
711 | |
712 | PyObject* result; | |
713 | PyObject* args = Py_BuildValue("()"); | |
714 | ||
715 | result = PyEval_CallObject(func, args); | |
716 | Py_DECREF(args); | |
717 | if (result) { | |
718 | Py_DECREF(result); | |
719 | PyErr_Clear(); | |
720 | } else { | |
721 | PyErr_Print(); | |
722 | } | |
7bf85405 | 723 | |
19a97bd6 | 724 | wxPyEndBlockThreads(state); |
7bf85405 RD |
725 | } |
726 | } | |
727 | ||
728 | ||
cf694132 | 729 | |
2f90df85 | 730 | //--------------------------------------------------------------------------- |
389c5527 RD |
731 | //--------------------------------------------------------------------------- |
732 | // Convert a wxList to a Python List | |
733 | ||
65dd82cb | 734 | PyObject* wxPy_ConvertList(wxListBase* list, const char* className) { |
389c5527 RD |
735 | PyObject* pyList; |
736 | PyObject* pyObj; | |
737 | wxObject* wxObj; | |
738 | wxNode* node = list->First(); | |
739 | ||
19a97bd6 | 740 | wxPyTState* state = wxPyBeginBlockThreads(); |
389c5527 RD |
741 | pyList = PyList_New(0); |
742 | while (node) { | |
743 | wxObj = node->Data(); | |
9416aa89 | 744 | pyObj = wxPyMake_wxObject(wxObj); //wxPyConstructObject(wxObj, className); |
389c5527 RD |
745 | PyList_Append(pyList, pyObj); |
746 | node = node->Next(); | |
747 | } | |
19a97bd6 | 748 | wxPyEndBlockThreads(state); |
389c5527 RD |
749 | return pyList; |
750 | } | |
751 | ||
54b96882 RD |
752 | //---------------------------------------------------------------------- |
753 | ||
754 | long wxPyGetWinHandle(wxWindow* win) { | |
755 | #ifdef __WXMSW__ | |
756 | return (long)win->GetHandle(); | |
757 | #endif | |
758 | ||
759 | // Find and return the actual X-Window. | |
760 | #ifdef __WXGTK__ | |
761 | if (win->m_wxwindow) { | |
762 | GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window; | |
763 | if (bwin) { | |
764 | return (long)bwin->xwindow; | |
765 | } | |
766 | } | |
767 | #endif | |
768 | return 0; | |
769 | } | |
770 | ||
7bf85405 RD |
771 | //---------------------------------------------------------------------- |
772 | // Some helper functions for typemaps in my_typemaps.i, so they won't be | |
389c5527 | 773 | // included in every file... |
7bf85405 RD |
774 | |
775 | ||
2f90df85 | 776 | byte* byte_LIST_helper(PyObject* source) { |
b639c3c5 RD |
777 | if (!PyList_Check(source)) { |
778 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
779 | return NULL; | |
780 | } | |
781 | int count = PyList_Size(source); | |
782 | byte* temp = new byte[count]; | |
783 | if (! temp) { | |
784 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
785 | return NULL; | |
786 | } | |
787 | for (int x=0; x<count; x++) { | |
788 | PyObject* o = PyList_GetItem(source, x); | |
789 | if (! PyInt_Check(o)) { | |
790 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
791 | return NULL; | |
792 | } | |
793 | temp[x] = (byte)PyInt_AsLong(o); | |
794 | } | |
795 | return temp; | |
796 | } | |
797 | ||
798 | ||
2f90df85 | 799 | int* int_LIST_helper(PyObject* source) { |
7bf85405 RD |
800 | if (!PyList_Check(source)) { |
801 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
802 | return NULL; | |
803 | } | |
804 | int count = PyList_Size(source); | |
805 | int* temp = new int[count]; | |
806 | if (! temp) { | |
807 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
808 | return NULL; | |
809 | } | |
810 | for (int x=0; x<count; x++) { | |
811 | PyObject* o = PyList_GetItem(source, x); | |
812 | if (! PyInt_Check(o)) { | |
813 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
814 | return NULL; | |
815 | } | |
816 | temp[x] = PyInt_AsLong(o); | |
817 | } | |
818 | return temp; | |
819 | } | |
820 | ||
821 | ||
2f90df85 | 822 | long* long_LIST_helper(PyObject* source) { |
7bf85405 RD |
823 | if (!PyList_Check(source)) { |
824 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
825 | return NULL; | |
826 | } | |
827 | int count = PyList_Size(source); | |
828 | long* temp = new long[count]; | |
829 | if (! temp) { | |
830 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
831 | return NULL; | |
832 | } | |
833 | for (int x=0; x<count; x++) { | |
834 | PyObject* o = PyList_GetItem(source, x); | |
835 | if (! PyInt_Check(o)) { | |
836 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
837 | return NULL; | |
838 | } | |
839 | temp[x] = PyInt_AsLong(o); | |
840 | } | |
841 | return temp; | |
842 | } | |
843 | ||
844 | ||
2f90df85 | 845 | char** string_LIST_helper(PyObject* source) { |
7bf85405 RD |
846 | if (!PyList_Check(source)) { |
847 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
848 | return NULL; | |
849 | } | |
850 | int count = PyList_Size(source); | |
851 | char** temp = new char*[count]; | |
852 | if (! temp) { | |
853 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
854 | return NULL; | |
855 | } | |
856 | for (int x=0; x<count; x++) { | |
857 | PyObject* o = PyList_GetItem(source, x); | |
858 | if (! PyString_Check(o)) { | |
859 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
860 | return NULL; | |
861 | } | |
862 | temp[x] = PyString_AsString(o); | |
863 | } | |
864 | return temp; | |
865 | } | |
866 | ||
e0672e2f RD |
867 | //-------------------------------- |
868 | // Part of patch from Tim Hochberg | |
869 | static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) { | |
870 | if (PyInt_Check(o1) && PyInt_Check(o2)) { | |
871 | point->x = PyInt_AS_LONG(o1); | |
872 | point->y = PyInt_AS_LONG(o2); | |
873 | return true; | |
874 | } | |
875 | if (PyFloat_Check(o1) && PyFloat_Check(o2)) { | |
876 | point->x = (int)PyFloat_AS_DOUBLE(o1); | |
877 | point->y = (int)PyFloat_AS_DOUBLE(o2); | |
878 | return true; | |
879 | } | |
880 | if (PyInstance_Check(o1) || PyInstance_Check(o2)) { | |
881 | // Disallow instances because they can cause havok | |
882 | return false; | |
883 | } | |
884 | if (PyNumber_Check(o1) && PyNumber_Check(o2)) { | |
885 | // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2 | |
886 | point->x = PyInt_AsLong(o1); | |
887 | point->y = PyInt_AsLong(o2); | |
888 | return true; | |
889 | } | |
890 | return false; | |
891 | } | |
7bf85405 RD |
892 | |
893 | ||
e0672e2f RD |
894 | wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) { |
895 | // Putting all of the declarations here allows | |
896 | // us to put the error handling all in one place. | |
897 | int x; | |
898 | wxPoint* temp; | |
899 | PyObject *o, *o1, *o2; | |
9d37f964 | 900 | bool isFast = PyList_Check(source) || PyTuple_Check(source); |
e0672e2f | 901 | |
e0672e2f RD |
902 | if (!PySequence_Check(source)) { |
903 | goto error0; | |
7bf85405 | 904 | } |
9d37f964 RD |
905 | |
906 | // The length of the sequence is returned in count. | |
e0672e2f RD |
907 | *count = PySequence_Length(source); |
908 | if (*count < 0) { | |
909 | goto error0; | |
910 | } | |
911 | ||
912 | temp = new wxPoint[*count]; | |
913 | if (!temp) { | |
7bf85405 RD |
914 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); |
915 | return NULL; | |
916 | } | |
e0672e2f RD |
917 | for (x=0; x<*count; x++) { |
918 | // Get an item: try fast way first. | |
919 | if (isFast) { | |
920 | o = PySequence_Fast_GET_ITEM(source, x); | |
921 | } | |
922 | else { | |
923 | o = PySequence_GetItem(source, x); | |
924 | if (o == NULL) { | |
925 | goto error1; | |
926 | } | |
927 | } | |
7bf85405 | 928 | |
e0672e2f RD |
929 | // Convert o to wxPoint. |
930 | if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) || | |
931 | (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) { | |
932 | o1 = PySequence_Fast_GET_ITEM(o, 0); | |
933 | o2 = PySequence_Fast_GET_ITEM(o, 1); | |
934 | if (!wxPointFromObjects(o1, o2, &temp[x])) { | |
935 | goto error2; | |
936 | } | |
7bf85405 | 937 | } |
d559219f RD |
938 | else if (PyInstance_Check(o)) { |
939 | wxPoint* pt; | |
e0672e2f RD |
940 | if (SWIG_GetPtrObj(o, (void **)&pt, "_wxPoint_p")) { |
941 | goto error2; | |
d559219f RD |
942 | } |
943 | temp[x] = *pt; | |
944 | } | |
e0672e2f RD |
945 | else if (PySequence_Check(o) && PySequence_Length(o) == 2) { |
946 | o1 = PySequence_GetItem(o, 0); | |
947 | o2 = PySequence_GetItem(o, 1); | |
948 | if (!wxPointFromObjects(o1, o2, &temp[x])) { | |
949 | goto error3; | |
950 | } | |
951 | Py_DECREF(o1); | |
952 | Py_DECREF(o2); | |
953 | } | |
7bf85405 | 954 | else { |
e0672e2f | 955 | goto error2; |
7bf85405 | 956 | } |
e0672e2f RD |
957 | // Clean up. |
958 | if (!isFast) | |
959 | Py_DECREF(o); | |
7bf85405 RD |
960 | } |
961 | return temp; | |
e0672e2f RD |
962 | |
963 | error3: | |
964 | Py_DECREF(o1); | |
965 | Py_DECREF(o2); | |
966 | error2: | |
967 | if (!isFast) | |
968 | Py_DECREF(o); | |
969 | error1: | |
970 | delete temp; | |
971 | error0: | |
972 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints."); | |
973 | return NULL; | |
7bf85405 | 974 | } |
e0672e2f RD |
975 | // end of patch |
976 | //------------------------------ | |
7bf85405 RD |
977 | |
978 | ||
2f90df85 | 979 | wxBitmap** wxBitmap_LIST_helper(PyObject* source) { |
7bf85405 RD |
980 | if (!PyList_Check(source)) { |
981 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
982 | return NULL; | |
983 | } | |
984 | int count = PyList_Size(source); | |
985 | wxBitmap** temp = new wxBitmap*[count]; | |
986 | if (! temp) { | |
987 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
988 | return NULL; | |
989 | } | |
990 | for (int x=0; x<count; x++) { | |
991 | PyObject* o = PyList_GetItem(source, x); | |
e166644c | 992 | if (PyInstance_Check(o)) { |
7bf85405 | 993 | wxBitmap* pt; |
e166644c | 994 | if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) { |
7bf85405 RD |
995 | PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p."); |
996 | return NULL; | |
997 | } | |
998 | temp[x] = pt; | |
999 | } | |
1000 | else { | |
1001 | PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps."); | |
1002 | return NULL; | |
1003 | } | |
1004 | } | |
1005 | return temp; | |
1006 | } | |
1007 | ||
1008 | ||
1009 | ||
2f90df85 | 1010 | wxString* wxString_LIST_helper(PyObject* source) { |
7bf85405 RD |
1011 | if (!PyList_Check(source)) { |
1012 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1013 | return NULL; | |
1014 | } | |
1015 | int count = PyList_Size(source); | |
1016 | wxString* temp = new wxString[count]; | |
1017 | if (! temp) { | |
1018 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1019 | return NULL; | |
1020 | } | |
1021 | for (int x=0; x<count; x++) { | |
1022 | PyObject* o = PyList_GetItem(source, x); | |
ecc08ead RD |
1023 | #if PYTHON_API_VERSION >= 1009 |
1024 | if (! PyString_Check(o) && ! PyUnicode_Check(o)) { | |
1025 | PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects."); | |
1026 | return NULL; | |
1027 | } | |
1028 | ||
1029 | char* buff; | |
1030 | int length; | |
1031 | if (PyString_AsStringAndSize(o, &buff, &length) == -1) | |
1032 | return NULL; | |
1033 | temp[x] = wxString(buff, length); | |
1034 | #else | |
7bf85405 RD |
1035 | if (! PyString_Check(o)) { |
1036 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
1037 | return NULL; | |
1038 | } | |
1039 | temp[x] = PyString_AsString(o); | |
ecc08ead | 1040 | #endif |
7bf85405 RD |
1041 | } |
1042 | return temp; | |
1043 | } | |
1044 | ||
1045 | ||
2f90df85 | 1046 | wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) { |
7bf85405 RD |
1047 | if (!PyList_Check(source)) { |
1048 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1049 | return NULL; | |
1050 | } | |
1051 | int count = PyList_Size(source); | |
1052 | wxAcceleratorEntry* temp = new wxAcceleratorEntry[count]; | |
1053 | if (! temp) { | |
1054 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1055 | return NULL; | |
1056 | } | |
1057 | for (int x=0; x<count; x++) { | |
1058 | PyObject* o = PyList_GetItem(source, x); | |
e166644c | 1059 | if (PyInstance_Check(o)) { |
7bf85405 | 1060 | wxAcceleratorEntry* ae; |
e166644c | 1061 | if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) { |
7bf85405 RD |
1062 | PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p."); |
1063 | return NULL; | |
1064 | } | |
1065 | temp[x] = *ae; | |
1066 | } | |
1067 | else if (PyTuple_Check(o)) { | |
1068 | PyObject* o1 = PyTuple_GetItem(o, 0); | |
1069 | PyObject* o2 = PyTuple_GetItem(o, 1); | |
1070 | PyObject* o3 = PyTuple_GetItem(o, 2); | |
0adbc166 | 1071 | temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3)); |
7bf85405 RD |
1072 | } |
1073 | else { | |
1074 | PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects."); | |
1075 | return NULL; | |
1076 | } | |
1077 | } | |
1078 | return temp; | |
1079 | } | |
1080 | ||
bb0054cd | 1081 | |
9d37f964 RD |
1082 | wxPen** wxPen_LIST_helper(PyObject* source) { |
1083 | if (!PyList_Check(source)) { | |
1084 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1085 | return NULL; | |
1086 | } | |
1087 | int count = PyList_Size(source); | |
1088 | wxPen** temp = new wxPen*[count]; | |
1089 | if (!temp) { | |
1090 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1091 | return NULL; | |
1092 | } | |
1093 | for (int x=0; x<count; x++) { | |
1094 | PyObject* o = PyList_GetItem(source, x); | |
1095 | if (PyInstance_Check(o)) { | |
1096 | wxPen* pt; | |
1097 | if (SWIG_GetPtrObj(o, (void **) &pt,"_wxPen_p")) { | |
1098 | delete temp; | |
1099 | PyErr_SetString(PyExc_TypeError,"Expected _wxPen_p."); | |
1100 | return NULL; | |
1101 | } | |
1102 | temp[x] = pt; | |
1103 | } | |
1104 | else { | |
1105 | delete temp; | |
1106 | PyErr_SetString(PyExc_TypeError, "Expected a list of wxPens."); | |
1107 | return NULL; | |
1108 | } | |
1109 | } | |
1110 | return temp; | |
1111 | } | |
1112 | ||
1113 | ||
1114 | bool _2int_seq_helper(PyObject* source, int* i1, int* i2) { | |
1115 | bool isFast = PyList_Check(source) || PyTuple_Check(source); | |
1116 | PyObject *o1, *o2; | |
1117 | ||
1118 | if (!PySequence_Check(source) || PySequence_Length(source) != 2) | |
1119 | return FALSE; | |
1120 | ||
1121 | if (isFast) { | |
1122 | o1 = PySequence_Fast_GET_ITEM(source, 0); | |
1123 | o2 = PySequence_Fast_GET_ITEM(source, 1); | |
1124 | } | |
1125 | else { | |
1126 | o1 = PySequence_GetItem(source, 0); | |
1127 | o2 = PySequence_GetItem(source, 1); | |
1128 | } | |
1129 | ||
1130 | *i1 = PyInt_AsLong(o1); | |
1131 | *i2 = PyInt_AsLong(o2); | |
1132 | ||
1133 | if (! isFast) { | |
1134 | Py_DECREF(o1); | |
1135 | Py_DECREF(o2); | |
1136 | } | |
1137 | return TRUE; | |
1138 | } | |
1139 | ||
1140 | ||
1141 | bool _4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4) { | |
1142 | bool isFast = PyList_Check(source) || PyTuple_Check(source); | |
1143 | PyObject *o1, *o2, *o3, *o4; | |
1144 | ||
1145 | if (!PySequence_Check(source) || PySequence_Length(source) != 4) | |
1146 | return FALSE; | |
1147 | ||
1148 | if (isFast) { | |
1149 | o1 = PySequence_Fast_GET_ITEM(source, 0); | |
1150 | o2 = PySequence_Fast_GET_ITEM(source, 1); | |
1151 | o3 = PySequence_Fast_GET_ITEM(source, 2); | |
1152 | o4 = PySequence_Fast_GET_ITEM(source, 3); | |
1153 | } | |
1154 | else { | |
1155 | o1 = PySequence_GetItem(source, 0); | |
1156 | o2 = PySequence_GetItem(source, 1); | |
1157 | o3 = PySequence_GetItem(source, 2); | |
1158 | o4 = PySequence_GetItem(source, 3); | |
1159 | } | |
1160 | ||
1161 | *i1 = PyInt_AsLong(o1); | |
1162 | *i2 = PyInt_AsLong(o2); | |
1163 | *i3 = PyInt_AsLong(o3); | |
1164 | *i4 = PyInt_AsLong(o4); | |
1165 | ||
1166 | if (! isFast) { | |
1167 | Py_DECREF(o1); | |
1168 | Py_DECREF(o2); | |
1169 | Py_DECREF(o3); | |
1170 | Py_DECREF(o4); | |
1171 | } | |
1172 | return TRUE; | |
1173 | } | |
1174 | ||
bb0054cd RD |
1175 | |
1176 | //---------------------------------------------------------------------- | |
bb0054cd | 1177 | |
2f90df85 RD |
1178 | bool wxSize_helper(PyObject* source, wxSize** obj) { |
1179 | ||
1180 | // If source is an object instance then it may already be the right type | |
1181 | if (PyInstance_Check(source)) { | |
1182 | wxSize* ptr; | |
1183 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p")) | |
1184 | goto error; | |
1185 | *obj = ptr; | |
1186 | return TRUE; | |
1187 | } | |
1188 | // otherwise a 2-tuple of integers is expected | |
1189 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
1190 | PyObject* o1 = PySequence_GetItem(source, 0); | |
1191 | PyObject* o2 = PySequence_GetItem(source, 1); | |
1192 | **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2)); | |
1193 | return TRUE; | |
1194 | } | |
1195 | ||
1196 | error: | |
1197 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object."); | |
1198 | return FALSE; | |
1199 | } | |
1200 | ||
1201 | bool wxPoint_helper(PyObject* source, wxPoint** obj) { | |
1202 | ||
1203 | // If source is an object instance then it may already be the right type | |
1204 | if (PyInstance_Check(source)) { | |
1205 | wxPoint* ptr; | |
1206 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p")) | |
1207 | goto error; | |
1208 | *obj = ptr; | |
1209 | return TRUE; | |
1210 | } | |
e0672e2f RD |
1211 | // otherwise a length-2 sequence of integers is expected |
1212 | if (PySequence_Check(source) && PySequence_Length(source) == 2) { | |
2f90df85 RD |
1213 | PyObject* o1 = PySequence_GetItem(source, 0); |
1214 | PyObject* o2 = PySequence_GetItem(source, 1); | |
e0672e2f RD |
1215 | // This should really check for integers, not numbers -- but that would break code. |
1216 | if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) { | |
1217 | Py_DECREF(o1); | |
1218 | Py_DECREF(o2); | |
1219 | goto error; | |
1220 | } | |
1221 | **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2)); | |
1222 | Py_DECREF(o1); | |
1223 | Py_DECREF(o2); | |
2f90df85 RD |
1224 | return TRUE; |
1225 | } | |
2f90df85 RD |
1226 | error: |
1227 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object."); | |
1228 | return FALSE; | |
1229 | } | |
1230 | ||
1231 | ||
1232 | ||
1233 | bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) { | |
1234 | ||
1235 | // If source is an object instance then it may already be the right type | |
1236 | if (PyInstance_Check(source)) { | |
1237 | wxRealPoint* ptr; | |
1238 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p")) | |
1239 | goto error; | |
1240 | *obj = ptr; | |
1241 | return TRUE; | |
1242 | } | |
1243 | // otherwise a 2-tuple of floats is expected | |
1244 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
1245 | PyObject* o1 = PySequence_GetItem(source, 0); | |
1246 | PyObject* o2 = PySequence_GetItem(source, 1); | |
1247 | **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2)); | |
1248 | return TRUE; | |
1249 | } | |
1250 | ||
1251 | error: | |
1252 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object."); | |
1253 | return FALSE; | |
1254 | } | |
1255 | ||
1256 | ||
1257 | ||
1258 | ||
1259 | bool wxRect_helper(PyObject* source, wxRect** obj) { | |
1260 | ||
1261 | // If source is an object instance then it may already be the right type | |
1262 | if (PyInstance_Check(source)) { | |
1263 | wxRect* ptr; | |
1264 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p")) | |
1265 | goto error; | |
1266 | *obj = ptr; | |
1267 | return TRUE; | |
1268 | } | |
1269 | // otherwise a 4-tuple of integers is expected | |
1270 | else if (PySequence_Check(source) && PyObject_Length(source) == 4) { | |
1271 | PyObject* o1 = PySequence_GetItem(source, 0); | |
1272 | PyObject* o2 = PySequence_GetItem(source, 1); | |
1273 | PyObject* o3 = PySequence_GetItem(source, 2); | |
1274 | PyObject* o4 = PySequence_GetItem(source, 3); | |
1275 | **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2), | |
1276 | PyInt_AsLong(o3), PyInt_AsLong(o4)); | |
1277 | return TRUE; | |
1278 | } | |
1279 | ||
1280 | error: | |
1281 | PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object."); | |
1282 | return FALSE; | |
1283 | } | |
1284 | ||
1285 | ||
1286 | ||
f6bcfd97 BP |
1287 | bool wxColour_helper(PyObject* source, wxColour** obj) { |
1288 | ||
1289 | // If source is an object instance then it may already be the right type | |
1290 | if (PyInstance_Check(source)) { | |
1291 | wxColour* ptr; | |
1292 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p")) | |
1293 | goto error; | |
1294 | *obj = ptr; | |
1295 | return TRUE; | |
1296 | } | |
1297 | // otherwise a string is expected | |
1298 | else if (PyString_Check(source)) { | |
1299 | wxString spec = PyString_AS_STRING(source); | |
1112b0c6 | 1300 | if (spec[0U] == '#' && spec.Length() == 7) { // It's #RRGGBB |
f6bcfd97 BP |
1301 | char* junk; |
1302 | int red = strtol(spec.Mid(1,2), &junk, 16); | |
1303 | int green = strtol(spec.Mid(3,2), &junk, 16); | |
1304 | int blue = strtol(spec.Mid(5,2), &junk, 16); | |
1305 | **obj = wxColour(red, green, blue); | |
1306 | return TRUE; | |
1307 | } | |
1308 | else { // it's a colour name | |
1309 | **obj = wxColour(spec); | |
1310 | return TRUE; | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | error: | |
de20db99 | 1315 | PyErr_SetString(PyExc_TypeError, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'."); |
f6bcfd97 BP |
1316 | return FALSE; |
1317 | } | |
1318 | ||
1319 | ||
bb0054cd | 1320 | //---------------------------------------------------------------------- |
b37c7e1d RD |
1321 | |
1322 | PyObject* wxArrayString2PyList_helper(const wxArrayString& arr) { | |
1323 | ||
1324 | PyObject* list = PyList_New(0); | |
1325 | for (size_t i=0; i < arr.GetCount(); i++) { | |
1326 | PyObject* str = PyString_FromString(arr[i].c_str()); | |
1327 | PyList_Append(list, str); | |
8af26133 | 1328 | Py_DECREF(str); |
b37c7e1d RD |
1329 | } |
1330 | return list; | |
1331 | } | |
1332 | ||
1333 | ||
bb0054cd | 1334 | //---------------------------------------------------------------------- |
7bf85405 | 1335 | //---------------------------------------------------------------------- |
7bf85405 | 1336 | |
7bf85405 | 1337 | |
7bf85405 | 1338 | |
de20db99 | 1339 |