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