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