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