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