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