]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/src/helpers.cpp
Fixed a deadlock problem when using threads
[wxWidgets.git] / utils / wxPython / src / helpers.cpp
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
14 #ifdef __WXGTK__
15 #include <gtk/gtk.h>
16 #endif
17
18 #undef DEBUG
19 #include <Python.h>
20 #include "helpers.h"
21 #ifdef __WXMSW__
22 #include <wx/msw/private.h>
23 #undef FindWindow
24 #undef GetCharWidth
25 #undef LoadAccelerators
26 #undef GetClassInfo
27 #undef GetClassName
28 #endif
29 #include <wx/module.h>
30
31
32 //---------------------------------------------------------------------------
33
34 //wxHashTable* wxPyWindows = NULL;
35
36
37 wxPoint wxPyDefaultPosition; //wxDefaultPosition);
38 wxSize wxPyDefaultSize; //wxDefaultSize);
39 wxString wxPyEmptyStr("");
40
41
42
43 #ifdef __WXMSW__ // If building for win32...
44 //----------------------------------------------------------------------
45 // This gets run when the DLL is loaded. We just need to save a handle.
46 //----------------------------------------------------------------------
47
48 BOOL WINAPI DllMain(
49 HINSTANCE hinstDLL, // handle to DLL module
50 DWORD fdwReason, // reason for calling function
51 LPVOID lpvReserved // reserved
52 )
53 {
54 wxSetInstance(hinstDLL);
55 return 1;
56 }
57 #endif
58
59 //----------------------------------------------------------------------
60 // Class for implementing the wxp main application shell.
61 //----------------------------------------------------------------------
62
63 wxPyApp *wxPythonApp = NULL; // Global instance of application object
64
65
66 wxPyApp::wxPyApp() {
67 // printf("**** ctor\n");
68 }
69
70 wxPyApp::~wxPyApp() {
71 // printf("**** dtor\n");
72 }
73
74
75 // This one isn't acutally called... See __wxStart()
76 bool wxPyApp::OnInit(void) {
77 return false;
78 }
79
80 int wxPyApp::MainLoop(void) {
81 int retval = wxApp::MainLoop();
82 //# AfterMainLoop();
83 wxPythonApp->OnExit(); //#
84 return retval;
85 }
86
87
88 //# void wxPyApp::AfterMainLoop(void) {
89 // // more stuff from wxEntry...
90
91 // if (wxPythonApp->GetTopWindow()) {
92 // // Forcibly delete the window.
93 // if (wxPythonApp->GetTopWindow()->IsKindOf(CLASSINFO(wxFrame)) ||
94 // wxPythonApp->GetTopWindow()->IsKindOf(CLASSINFO(wxDialog))) {
95
96 // wxPythonApp->GetTopWindow()->Close(TRUE);
97 // wxPythonApp->DeletePendingObjects();
98 // }
99 // else {
100 // delete wxPythonApp->GetTopWindow();
101 // wxPythonApp->SetTopWindow(NULL);
102 // }
103 // }
104 // #ifdef __WXGTK__
105 // wxPythonApp->DeletePendingObjects();
106 // #endif
107
108 // wxPythonApp->OnExit();
109 // wxApp::CleanUp();
110 // // delete wxPythonApp;
111 // }
112
113
114 //---------------------------------------------------------------------
115 // a few native methods to add to the module
116 //----------------------------------------------------------------------
117
118
119 // This is where we pick up the first part of the wxEntry functionality...
120 // The rest is in __wxStart and AfterMainLoop. This function is called when
121 // wxcmodule is imported. (Before there is a wxApp object.)
122 void __wxPreStart()
123 {
124 #ifdef WXP_WITH_THREAD
125 PyEval_InitThreads();
126 #endif
127
128 // Bail out if there is already windows created. This means that the
129 // toolkit has already been initialized, as in embedding wxPython in
130 // a C++ wxWindows app.
131 if (wxTopLevelWindows.Number() > 0)
132 return;
133
134 #ifdef __WXMSW__
135 wxApp::Initialize();
136 #endif
137
138 #ifdef __WXGTK__
139 PyObject* sysargv = PySys_GetObject("argv");
140 int argc = PyList_Size(sysargv);
141 char** argv = new char*[argc+1];
142 int x;
143 for(x=0; x<argc; x++)
144 argv[x] = PyString_AsString(PyList_GetItem(sysargv, x));
145 argv[argc] = NULL;
146
147 gtk_set_locale();
148 #if wxUSE_WCHAR_T
149 if (!wxOKlibc()) wxConvCurrent = &wxConvLocal;
150 #else
151 if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL;
152 #endif
153 gtk_init( &argc, &argv );
154 wxSetDetectableAutoRepeat( TRUE );
155 delete [] argv;
156
157 wxApp::Initialize(); // may return FALSE. Should we check?
158 #endif
159
160 }
161
162
163 #ifdef WXP_WITH_THREAD
164 PyThreadState* wxPyEventThreadState = NULL;
165 #endif
166 static char* __nullArgv[1] = { 0 };
167
168
169
170 // Start the user application, user App's OnInit method is a parameter here
171 PyObject* __wxStart(PyObject* /* self */, PyObject* args)
172 {
173 PyObject* onInitFunc = NULL;
174 PyObject* arglist;
175 PyObject* result;
176 long bResult;
177
178 #ifdef WXP_WITH_THREAD
179 wxPyEventThreadState = PyThreadState_Get();
180 #endif
181
182 if (!PyArg_ParseTuple(args, "O", &onInitFunc))
183 return NULL;
184
185 if (wxTopLevelWindows.Number() > 0) {
186 PyErr_SetString(PyExc_TypeError, "Only 1 wxApp per process!");
187 return NULL;
188 }
189
190
191 // This is the next part of the wxEntry functionality...
192 wxPythonApp->argc = 0;
193 wxPythonApp->argv = NULL;
194 wxPythonApp->OnInitGui();
195
196
197 // Call the Python App's OnInit function
198 arglist = PyTuple_New(0);
199 result = PyEval_CallObject(onInitFunc, arglist);
200 if (!result) { // an exception was raised.
201 return NULL;
202 }
203
204 if (! PyInt_Check(result)) {
205 PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value");
206 return NULL;
207 }
208 bResult = PyInt_AS_LONG(result);
209 if (! bResult) {
210 PyErr_SetString(PyExc_SystemExit, "OnInit returned false, exiting...");
211 return NULL;
212 }
213
214 #ifdef __WXGTK__
215 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
216 #endif
217
218 Py_INCREF(Py_None);
219 return Py_None;
220 }
221
222
223
224
225
226 PyObject* wxPython_dict;
227 PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args)
228 {
229
230 if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
231 return NULL;
232
233 if (!PyDict_Check(wxPython_dict)) {
234 PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!");
235 return NULL;
236 }
237 #ifdef __WXMOTIF__
238 #define wxPlatform "__WXMOTIF__"
239 #endif
240 #ifdef __WXQT__
241 #define wxPlatform "__WXQT__"
242 #endif
243 #ifdef __WXGTK__
244 #define wxPlatform "__WXGTK__"
245 #endif
246 #if defined(__WIN32__) || defined(__WXMSW__)
247 #define wxPlatform "__WXMSW__"
248 #endif
249 #ifdef __WXMAC__
250 #define wxPlatform "__WXMAC__"
251 #endif
252
253 PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform));
254
255 Py_INCREF(Py_None);
256 return Py_None;
257 }
258
259
260 //---------------------------------------------------------------------------
261
262 PyObject* wxPyConstructObject(void* ptr, char* className) {
263 char buff[64]; // should always be big enough...
264 char swigptr[64];
265
266 sprintf(buff, "_%s_p", className);
267 SWIG_MakePtr(swigptr, ptr, buff);
268
269 sprintf(buff, "%sPtr", className);
270 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
271 if (! classobj) {
272 Py_INCREF(Py_None);
273 return Py_None;
274 }
275
276 PyObject* arg = Py_BuildValue("(s)", swigptr);
277 PyObject* obj = PyInstance_New(classobj, arg, NULL);
278 Py_DECREF(arg);
279
280 return obj;
281 }
282
283 //---------------------------------------------------------------------------
284
285 //static bool _wxPyInEvent = false;
286 //static unsigned int _wxPyNestCount = 0;
287
288 HELPEREXPORT bool wxPyRestoreThread() {
289 // #ifdef WXP_WITH_THREAD
290 // //if (wxPyEventThreadState != PyThreadState_Get()) {
291 // if (! _wxPyInEvent) {
292 // PyEval_RestoreThread(wxPyEventThreadState);
293 // _wxPyInEvent = true;
294 // return TRUE;
295 // } else
296 // #endif
297 // return FALSE;
298
299 // NOTE: The Python API docs state that if a thread already has the
300 // interpreter lock and calls PyEval_RestoreThread again a deadlock
301 // occurs, so I put in the above code as a guard condition since there are
302 // many possibilites for nested events and callbacks in wxPython.
303 //
304 // Unfortunately, it seems like somebody was lying (or I'm not
305 // understanding...) because each of the nested calls to this function
306 // MUST call PyEval_RestoreThread or Python pukes with a thread error (at
307 // least on Win32.)
308 //
309 // until I know better, this is how I am doing it instead:
310 #ifdef WXP_WITH_THREAD
311 PyEval_RestoreThread(wxPyEventThreadState);
312 // _wxPyNestCount += 1;
313 // if (_wxPyNestCount == 1)
314 // return TRUE;
315 // else
316 #endif
317 return FALSE;
318 }
319
320
321 HELPEREXPORT void wxPySaveThread(bool doSave) {
322 #ifdef WXP_WITH_THREAD
323 // if (doSave) {
324 wxPyEventThreadState = PyEval_SaveThread();
325 // _wxPyInEvent = false;
326 // }
327 // _wxPyNestCount -= 1;
328 #endif
329 }
330
331 //---------------------------------------------------------------------------
332
333
334 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject);
335
336 wxPyCallback::wxPyCallback(PyObject* func) {
337 m_func = func;
338 Py_INCREF(m_func);
339 }
340
341 wxPyCallback::wxPyCallback(const wxPyCallback& other) {
342 m_func = other.m_func;
343 Py_INCREF(m_func);
344 }
345
346 wxPyCallback::~wxPyCallback() {
347 bool doSave = wxPyRestoreThread();
348 Py_DECREF(m_func);
349 wxPySaveThread(doSave);
350 }
351
352
353
354
355 // This function is used for all events destined for Python event handlers.
356 void wxPyCallback::EventThunker(wxEvent& event) {
357 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
358 PyObject* func = cb->m_func;
359 PyObject* result;
360 PyObject* arg;
361 PyObject* tuple;
362
363
364 bool doSave = wxPyRestoreThread();
365 arg = wxPyConstructObject((void*)&event, event.GetClassInfo()->GetClassName());
366
367 tuple = PyTuple_New(1);
368 PyTuple_SET_ITEM(tuple, 0, arg);
369 result = PyEval_CallObject(func, tuple);
370 Py_DECREF(tuple);
371 if (result) {
372 Py_DECREF(result);
373 PyErr_Clear();
374 } else {
375 PyErr_Print();
376 }
377 wxPySaveThread(doSave);
378 }
379
380
381 //----------------------------------------------------------------------
382
383 wxPyCallbackHelper::wxPyCallbackHelper() {
384 m_self = NULL;
385 m_lastFound = NULL;
386 }
387
388
389 wxPyCallbackHelper::~wxPyCallbackHelper() {
390 bool doSave = wxPyRestoreThread();
391 Py_XDECREF(m_self);
392 wxPySaveThread(doSave);
393 }
394
395 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) {
396 m_lastFound = NULL;
397 m_self = other.m_self;
398 if (m_self)
399 Py_INCREF(m_self);
400 }
401
402
403 void wxPyCallbackHelper::setSelf(PyObject* self, int incref) {
404 m_self = self;
405 if (incref)
406 Py_INCREF(m_self);
407 }
408
409
410 bool wxPyCallbackHelper::findCallback(const wxString& name) {
411 m_lastFound = NULL;
412 if (m_self && PyObject_HasAttrString(m_self, (char*)name.c_str()))
413 m_lastFound = PyObject_GetAttrString(m_self, (char*)name.c_str());
414
415 return m_lastFound != NULL;
416 }
417
418
419 int wxPyCallbackHelper::callCallback(PyObject* argTuple) {
420 PyObject* result;
421 int retval = FALSE;
422
423 result = callCallbackObj(argTuple);
424 if (result) { // Assumes an integer return type...
425 retval = PyInt_AsLong(result);
426 Py_DECREF(result);
427 PyErr_Clear(); // forget about it if it's not...
428 }
429 return retval;
430 }
431
432 // Invoke the Python callable object, returning the raw PyObject return
433 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
434 PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) {
435 PyObject* result;
436
437 result = PyEval_CallObject(m_lastFound, argTuple);
438 Py_DECREF(argTuple);
439 if (!result) {
440 PyErr_Print();
441 }
442 return result;
443 }
444
445
446
447 //---------------------------------------------------------------------------
448 //---------------------------------------------------------------------------
449
450
451 wxPyTimer::wxPyTimer(PyObject* callback) {
452 func = callback;
453 Py_INCREF(func);
454 }
455
456 wxPyTimer::~wxPyTimer() {
457 bool doSave = wxPyRestoreThread();
458 Py_DECREF(func);
459 wxPySaveThread(doSave);
460 }
461
462 void wxPyTimer::Notify() {
463 bool doSave = wxPyRestoreThread();
464
465 PyObject* result;
466 PyObject* args = Py_BuildValue("()");
467
468 result = PyEval_CallObject(func, args);
469 Py_DECREF(args);
470 if (result) {
471 Py_DECREF(result);
472 PyErr_Clear();
473 } else {
474 PyErr_Print();
475 }
476 wxPySaveThread(doSave);
477 }
478
479
480
481 //---------------------------------------------------------------------------
482 //---------------------------------------------------------------------------
483 // Convert a wxList to a Python List
484
485 PyObject* wxPy_ConvertList(wxListBase* list, char* className) {
486 PyObject* pyList;
487 PyObject* pyObj;
488 wxObject* wxObj;
489 wxNode* node = list->First();
490
491 bool doSave = wxPyRestoreThread();
492 pyList = PyList_New(0);
493 while (node) {
494 wxObj = node->Data();
495 pyObj = wxPyConstructObject(wxObj, className);
496 PyList_Append(pyList, pyObj);
497 node = node->Next();
498 }
499 wxPySaveThread(doSave);
500 return pyList;
501 }
502
503 //----------------------------------------------------------------------
504 // Some helper functions for typemaps in my_typemaps.i, so they won't be
505 // included in every file...
506
507
508 byte* byte_LIST_helper(PyObject* source) {
509 if (!PyList_Check(source)) {
510 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
511 return NULL;
512 }
513 int count = PyList_Size(source);
514 byte* temp = new byte[count];
515 if (! temp) {
516 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
517 return NULL;
518 }
519 for (int x=0; x<count; x++) {
520 PyObject* o = PyList_GetItem(source, x);
521 if (! PyInt_Check(o)) {
522 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
523 return NULL;
524 }
525 temp[x] = (byte)PyInt_AsLong(o);
526 }
527 return temp;
528 }
529
530
531 int* int_LIST_helper(PyObject* source) {
532 if (!PyList_Check(source)) {
533 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
534 return NULL;
535 }
536 int count = PyList_Size(source);
537 int* temp = new int[count];
538 if (! temp) {
539 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
540 return NULL;
541 }
542 for (int x=0; x<count; x++) {
543 PyObject* o = PyList_GetItem(source, x);
544 if (! PyInt_Check(o)) {
545 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
546 return NULL;
547 }
548 temp[x] = PyInt_AsLong(o);
549 }
550 return temp;
551 }
552
553
554 long* long_LIST_helper(PyObject* source) {
555 if (!PyList_Check(source)) {
556 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
557 return NULL;
558 }
559 int count = PyList_Size(source);
560 long* temp = new long[count];
561 if (! temp) {
562 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
563 return NULL;
564 }
565 for (int x=0; x<count; x++) {
566 PyObject* o = PyList_GetItem(source, x);
567 if (! PyInt_Check(o)) {
568 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
569 return NULL;
570 }
571 temp[x] = PyInt_AsLong(o);
572 }
573 return temp;
574 }
575
576
577 char** string_LIST_helper(PyObject* source) {
578 if (!PyList_Check(source)) {
579 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
580 return NULL;
581 }
582 int count = PyList_Size(source);
583 char** temp = new char*[count];
584 if (! temp) {
585 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
586 return NULL;
587 }
588 for (int x=0; x<count; x++) {
589 PyObject* o = PyList_GetItem(source, x);
590 if (! PyString_Check(o)) {
591 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
592 return NULL;
593 }
594 temp[x] = PyString_AsString(o);
595 }
596 return temp;
597 }
598
599
600
601 wxPoint* wxPoint_LIST_helper(PyObject* source) {
602 if (!PyList_Check(source)) {
603 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
604 return NULL;
605 }
606 int count = PyList_Size(source);
607 wxPoint* temp = new wxPoint[count];
608 if (! temp) {
609 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
610 return NULL;
611 }
612 for (int x=0; x<count; x++) {
613 PyObject* o = PyList_GetItem(source, x);
614 if (PyTuple_Check(o)) {
615 PyObject* o1 = PyTuple_GetItem(o, 0);
616 PyObject* o2 = PyTuple_GetItem(o, 1);
617
618 temp[x].x = PyInt_AsLong(o1);
619 temp[x].y = PyInt_AsLong(o2);
620 }
621 else if (PyInstance_Check(o)) {
622 wxPoint* pt;
623 if (SWIG_GetPtrObj(o,(void **) &pt,"_wxPoint_p")) {
624 PyErr_SetString(PyExc_TypeError,"Expected _wxPoint_p.");
625 return NULL;
626 }
627 temp[x] = *pt;
628 }
629 else {
630 PyErr_SetString(PyExc_TypeError, "Expected a list of 2-tuples or wxPoints.");
631 return NULL;
632 }
633 }
634 return temp;
635 }
636
637
638 wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
639 if (!PyList_Check(source)) {
640 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
641 return NULL;
642 }
643 int count = PyList_Size(source);
644 wxBitmap** temp = new wxBitmap*[count];
645 if (! temp) {
646 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
647 return NULL;
648 }
649 for (int x=0; x<count; x++) {
650 PyObject* o = PyList_GetItem(source, x);
651 if (PyInstance_Check(o)) {
652 wxBitmap* pt;
653 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) {
654 PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p.");
655 return NULL;
656 }
657 temp[x] = pt;
658 }
659 else {
660 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
661 return NULL;
662 }
663 }
664 return temp;
665 }
666
667
668
669 wxString* wxString_LIST_helper(PyObject* source) {
670 if (!PyList_Check(source)) {
671 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
672 return NULL;
673 }
674 int count = PyList_Size(source);
675 wxString* temp = new wxString[count];
676 if (! temp) {
677 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
678 return NULL;
679 }
680 for (int x=0; x<count; x++) {
681 PyObject* o = PyList_GetItem(source, x);
682 if (! PyString_Check(o)) {
683 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
684 return NULL;
685 }
686 temp[x] = PyString_AsString(o);
687 }
688 return temp;
689 }
690
691
692 wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
693 if (!PyList_Check(source)) {
694 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
695 return NULL;
696 }
697 int count = PyList_Size(source);
698 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
699 if (! temp) {
700 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
701 return NULL;
702 }
703 for (int x=0; x<count; x++) {
704 PyObject* o = PyList_GetItem(source, x);
705 if (PyInstance_Check(o)) {
706 wxAcceleratorEntry* ae;
707 if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) {
708 PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p.");
709 return NULL;
710 }
711 temp[x] = *ae;
712 }
713 else if (PyTuple_Check(o)) {
714 PyObject* o1 = PyTuple_GetItem(o, 0);
715 PyObject* o2 = PyTuple_GetItem(o, 1);
716 PyObject* o3 = PyTuple_GetItem(o, 2);
717
718 temp[x].m_flags = PyInt_AsLong(o1);
719 temp[x].m_keyCode = PyInt_AsLong(o2);
720 temp[x].m_command = PyInt_AsLong(o3);
721 }
722 else {
723 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
724 return NULL;
725 }
726 }
727 return temp;
728 }
729
730
731
732 //----------------------------------------------------------------------
733
734 bool wxSize_helper(PyObject* source, wxSize** obj) {
735
736 // If source is an object instance then it may already be the right type
737 if (PyInstance_Check(source)) {
738 wxSize* ptr;
739 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p"))
740 goto error;
741 *obj = ptr;
742 return TRUE;
743 }
744 // otherwise a 2-tuple of integers is expected
745 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
746 PyObject* o1 = PySequence_GetItem(source, 0);
747 PyObject* o2 = PySequence_GetItem(source, 1);
748 **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2));
749 return TRUE;
750 }
751
752 error:
753 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object.");
754 return FALSE;
755 }
756
757 bool wxPoint_helper(PyObject* source, wxPoint** obj) {
758
759 // If source is an object instance then it may already be the right type
760 if (PyInstance_Check(source)) {
761 wxPoint* ptr;
762 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p"))
763 goto error;
764 *obj = ptr;
765 return TRUE;
766 }
767 // otherwise a 2-tuple of integers is expected
768 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
769 PyObject* o1 = PySequence_GetItem(source, 0);
770 PyObject* o2 = PySequence_GetItem(source, 1);
771 **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2));
772 return TRUE;
773 }
774
775 error:
776 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object.");
777 return FALSE;
778 }
779
780
781
782 bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) {
783
784 // If source is an object instance then it may already be the right type
785 if (PyInstance_Check(source)) {
786 wxRealPoint* ptr;
787 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p"))
788 goto error;
789 *obj = ptr;
790 return TRUE;
791 }
792 // otherwise a 2-tuple of floats is expected
793 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
794 PyObject* o1 = PySequence_GetItem(source, 0);
795 PyObject* o2 = PySequence_GetItem(source, 1);
796 **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
797 return TRUE;
798 }
799
800 error:
801 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object.");
802 return FALSE;
803 }
804
805
806
807
808 bool wxRect_helper(PyObject* source, wxRect** obj) {
809
810 // If source is an object instance then it may already be the right type
811 if (PyInstance_Check(source)) {
812 wxRect* ptr;
813 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p"))
814 goto error;
815 *obj = ptr;
816 return TRUE;
817 }
818 // otherwise a 4-tuple of integers is expected
819 else if (PySequence_Check(source) && PyObject_Length(source) == 4) {
820 PyObject* o1 = PySequence_GetItem(source, 0);
821 PyObject* o2 = PySequence_GetItem(source, 1);
822 PyObject* o3 = PySequence_GetItem(source, 2);
823 PyObject* o4 = PySequence_GetItem(source, 3);
824 **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2),
825 PyInt_AsLong(o3), PyInt_AsLong(o4));
826 return TRUE;
827 }
828
829 error:
830 PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object.");
831 return FALSE;
832 }
833
834
835
836 //----------------------------------------------------------------------
837 //----------------------------------------------------------------------
838 //----------------------------------------------------------------------
839
840
841