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