]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/src/helpers.cpp
trying to add wxBase project file from Unix
[wxWidgets.git] / wxPython / src / helpers.cpp
... / ...
CommitLineData
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
42BOOL 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
57wxPyApp *wxPythonApp = NULL; // Global instance of application object
58
59
60wxPyApp::wxPyApp() {
61// printf("**** ctor\n");
62}
63
64wxPyApp::~wxPyApp() {
65// printf("**** dtor\n");
66}
67
68
69// This one isn't acutally called... See __wxStart()
70bool wxPyApp::OnInit(void) {
71 return FALSE;
72}
73
74int 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
98int WXDLLEXPORT wxEntryStart( int argc, char** argv );
99int WXDLLEXPORT wxEntryInitGui();
100void WXDLLEXPORT wxEntryCleanup();
101
102
103#ifdef WXP_WITH_THREAD
104//PyThreadState* wxPyEventThreadState = NULL;
105PyInterpreterState* wxPyInterpreter = NULL;
106#endif
107
108
109// This is where we pick up the first part of the wxEntry functionality...
110// The rest is in __wxStart and __wxCleanup. This function is called when
111// wxcmodule is imported. (Before there is a wxApp object.)
112void __wxPreStart()
113{
114
115#ifdef __WXMSW__
116// wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
117#endif
118
119#ifdef WXP_WITH_THREAD
120 PyEval_InitThreads();
121// wxPyEventThreadState = PyThreadState_Get(); // PyThreadState_New(PyThreadState_Get()->interp);
122 wxPyInterpreter = PyThreadState_Get()->interp;
123#endif
124
125 // Bail out if there is already windows created. This means that the
126 // toolkit has already been initialized, as in embedding wxPython in
127 // a C++ wxWindows app.
128 if (wxTopLevelWindows.Number() > 0)
129 return;
130
131
132 int argc = 0;
133 char** argv = NULL;
134 PyObject* sysargv = PySys_GetObject("argv");
135 if (sysargv != NULL) {
136 argc = PyList_Size(sysargv);
137 argv = new char*[argc+1];
138 int x;
139 for(x=0; x<argc; x++)
140 argv[x] = copystring(PyString_AsString(PyList_GetItem(sysargv, x)));
141 argv[argc] = NULL;
142 }
143
144 wxEntryStart(argc, argv);
145 delete [] argv;
146}
147
148
149
150// Start the user application, user App's OnInit method is a parameter here
151PyObject* __wxStart(PyObject* /* self */, PyObject* args)
152{
153 PyObject* onInitFunc = NULL;
154 PyObject* arglist;
155 PyObject* result;
156 long bResult;
157
158 if (!PyArg_ParseTuple(args, "O", &onInitFunc))
159 return NULL;
160
161#if 0 // Try it out without this check, see how it does...
162 if (wxTopLevelWindows.Number() > 0) {
163 PyErr_SetString(PyExc_TypeError, "Only 1 wxApp per process!");
164 return NULL;
165 }
166#endif
167
168 // This is the next part of the wxEntry functionality...
169 int argc = 0;
170 char** argv = NULL;
171 PyObject* sysargv = PySys_GetObject("argv");
172 if (sysargv != NULL) {
173 argc = PyList_Size(sysargv);
174 argv = new char*[argc+1];
175 int x;
176 for(x=0; x<argc; x++)
177 argv[x] = copystring(PyString_AsString(PyList_GetItem(sysargv, x)));
178 argv[argc] = NULL;
179 }
180 wxPythonApp->argc = argc;
181 wxPythonApp->argv = argv;
182
183 wxEntryInitGui();
184
185 // Call the Python App's OnInit function
186 arglist = PyTuple_New(0);
187 result = PyEval_CallObject(onInitFunc, arglist);
188 if (!result) { // an exception was raised.
189 return NULL;
190 }
191
192 if (! PyInt_Check(result)) {
193 PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value");
194 return NULL;
195 }
196 bResult = PyInt_AS_LONG(result);
197 if (! bResult) {
198 PyErr_SetString(PyExc_SystemExit, "OnInit returned FALSE, exiting...");
199 return NULL;
200 }
201
202#ifdef __WXGTK__
203 wxTheApp->m_initialized = (wxTopLevelWindows.GetCount() > 0);
204#endif
205
206 Py_INCREF(Py_None);
207 return Py_None;
208}
209
210void __wxCleanup() {
211 wxEntryCleanup();
212}
213
214
215
216static PyObject* wxPython_dict = NULL;
217static PyObject* wxPyPtrTypeMap = NULL;
218
219PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args)
220{
221
222 if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
223 return NULL;
224
225 if (!PyDict_Check(wxPython_dict)) {
226 PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!");
227 return NULL;
228 }
229
230 if (! wxPyPtrTypeMap)
231 wxPyPtrTypeMap = PyDict_New();
232 PyDict_SetItemString(wxPython_dict, "__wxPyPtrTypeMap", wxPyPtrTypeMap);
233
234
235#ifdef __WXMOTIF__
236#define wxPlatform "__WXMOTIF__"
237#endif
238#ifdef __WXQT__
239#define wxPlatform "__WXQT__"
240#endif
241#ifdef __WXGTK__
242#define wxPlatform "__WXGTK__"
243#endif
244#if defined(__WIN32__) || defined(__WXMSW__)
245#define wxPlatform "__WXMSW__"
246#endif
247#ifdef __WXMAC__
248#define wxPlatform "__WXMAC__"
249#endif
250
251 PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform));
252
253 Py_INCREF(Py_None);
254 return Py_None;
255}
256
257
258//---------------------------------------------------------------------------
259// Stuff used by OOR to find the right wxPython class type to return and to
260// build it.
261
262
263// The pointer type map is used when the "pointer" type name generated by SWIG
264// is not the same as the shadow class name, for example wxPyTreeCtrl
265// vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
266// so we'll just make it a Python dictionary in the wx module's namespace.
267void wxPyPtrTypeMap_Add(const char* commonName, const char* ptrName) {
268 if (! wxPyPtrTypeMap)
269 wxPyPtrTypeMap = PyDict_New();
270
271 PyDict_SetItemString(wxPyPtrTypeMap,
272 (char*)commonName,
273 PyString_FromString((char*)ptrName));
274}
275
276
277
278PyObject* wxPyClassExists(const char* className) {
279
280 if (!className)
281 return NULL;
282
283 char buff[64]; // should always be big enough...
284
285 sprintf(buff, "%sPtr", className);
286 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
287
288 return classobj; // returns NULL if not found
289}
290
291
292PyObject* wxPyMake_wxObject(wxObject* source) {
293 PyObject* target;
294
295 if (source) {
296 wxClassInfo* info = source->GetClassInfo();
297 wxChar* name = (wxChar*)info->GetClassName();
298 PyObject* klass = wxPyClassExists(name);
299 while (info && !klass) {
300 name = (wxChar*)info->GetBaseClassName1();
301 info = wxClassInfo::FindClass(name);
302 klass = wxPyClassExists(name);
303 }
304 if (info) {
305 target = wxPyConstructObject(source, name, klass, FALSE);
306 } else {
307 wxString msg("wxPython class not found for ");
308 msg += source->GetClassInfo()->GetClassName();
309 PyErr_SetString(PyExc_NameError, msg.c_str());
310 target = NULL;
311 }
312 } else { // source was NULL so return None.
313 Py_INCREF(Py_None); target = Py_None;
314 }
315 return target;
316}
317
318//---------------------------------------------------------------------------
319
320PyObject* wxPyConstructObject(void* ptr,
321 const char* className,
322 PyObject* klass,
323 int setThisOwn) {
324
325 PyObject* obj;
326 PyObject* arg;
327 PyObject* item;
328 char swigptr[64]; // should always be big enough...
329 char buff[64];
330
331 if ((item = PyDict_GetItemString(wxPyPtrTypeMap, (char*)className)) != NULL) {
332 className = PyString_AsString(item);
333 }
334 sprintf(buff, "_%s_p", className);
335 SWIG_MakePtr(swigptr, ptr, buff);
336
337 arg = Py_BuildValue("(s)", swigptr);
338 obj = PyInstance_New(klass, arg, NULL);
339 Py_DECREF(arg);
340
341 if (setThisOwn) {
342 PyObject* one = PyInt_FromLong(1);
343 PyObject_SetAttrString(obj, "thisown", one);
344 Py_DECREF(one);
345 }
346
347 return obj;
348}
349
350
351PyObject* wxPyConstructObject(void* ptr,
352 const char* className,
353 int setThisOwn) {
354 PyObject* obj;
355
356 if (!ptr) {
357 Py_INCREF(Py_None);
358 return Py_None;
359 }
360
361 char buff[64]; // should always be big enough...
362
363 sprintf(buff, "%sPtr", className);
364 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
365 if (! classobj) {
366 char temp[128];
367 sprintf(temp,
368 "*** Unknown class name %s, tell Robin about it please ***",
369 buff);
370 obj = PyString_FromString(temp);
371 return obj;
372 }
373
374 return wxPyConstructObject(ptr, className, classobj, setThisOwn);
375}
376
377//---------------------------------------------------------------------------
378
379// static PyThreadState* myPyThreadState_Get() {
380// PyThreadState* current;
381// current = PyThreadState_Swap(NULL);
382// PyThreadState_Swap(current);
383// return current;
384// }
385
386
387// bool wxPyRestoreThread() {
388// // NOTE: The Python API docs state that if a thread already has the
389// // interpreter lock and calls PyEval_RestoreThread again a deadlock
390// // occurs, so I put in this code as a guard condition since there are
391// // many possibilites for nested events and callbacks in wxPython. If
392// // The current thread is our thread, then we can assume that we
393// // already have the lock. (I hope!)
394// //
395// #ifdef WXP_WITH_THREAD
396// if (wxPyEventThreadState != myPyThreadState_Get()) {
397// PyEval_AcquireThread(wxPyEventThreadState);
398// return TRUE;
399// }
400// else
401// #endif
402// return FALSE;
403// }
404
405
406// void wxPySaveThread(bool doSave) {
407// #ifdef WXP_WITH_THREAD
408// if (doSave) {
409// PyEval_ReleaseThread(wxPyEventThreadState);
410// }
411// #endif
412// }
413
414
415
416wxPyTState* wxPyBeginBlockThreads() {
417 wxPyTState* state = NULL;
418#ifdef WXP_WITH_THREAD
419 if (1) { // Can I check if I've already got the lock?
420 state = new wxPyTState;
421 PyEval_AcquireLock();
422 state->newState = PyThreadState_New(wxPyInterpreter);
423 state->prevState = PyThreadState_Swap(state->newState);
424 }
425#endif
426 return state;
427}
428
429
430void wxPyEndBlockThreads(wxPyTState* state) {
431#ifdef WXP_WITH_THREAD
432 if (state) {
433 PyThreadState_Swap(state->prevState);
434 PyThreadState_Clear(state->newState);
435 PyEval_ReleaseLock();
436 PyThreadState_Delete(state->newState);
437 delete state;
438 }
439#endif
440}
441
442
443//---------------------------------------------------------------------------
444
445IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject);
446
447wxPyCallback::wxPyCallback(PyObject* func) {
448 m_func = func;
449 Py_INCREF(m_func);
450}
451
452wxPyCallback::wxPyCallback(const wxPyCallback& other) {
453 m_func = other.m_func;
454 Py_INCREF(m_func);
455}
456
457wxPyCallback::~wxPyCallback() {
458 wxPyTState* state = wxPyBeginBlockThreads();
459 Py_DECREF(m_func);
460 wxPyEndBlockThreads(state);
461}
462
463
464
465// This function is used for all events destined for Python event handlers.
466void wxPyCallback::EventThunker(wxEvent& event) {
467 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
468 PyObject* func = cb->m_func;
469 PyObject* result;
470 PyObject* arg;
471 PyObject* tuple;
472
473
474 wxPyTState* state = wxPyBeginBlockThreads();
475 wxString className = event.GetClassInfo()->GetClassName();
476
477 if (className == "wxPyEvent")
478 arg = ((wxPyEvent*)&event)->GetSelf();
479 else if (className == "wxPyCommandEvent")
480 arg = ((wxPyCommandEvent*)&event)->GetSelf();
481 else
482 arg = wxPyConstructObject((void*)&event, className);
483
484 tuple = PyTuple_New(1);
485 PyTuple_SET_ITEM(tuple, 0, arg);
486 result = PyEval_CallObject(func, tuple);
487 Py_DECREF(tuple);
488 if (result) {
489 Py_DECREF(result);
490 PyErr_Clear(); // Just in case...
491 } else {
492 PyErr_Print();
493 }
494 wxPyEndBlockThreads(state);
495}
496
497
498//----------------------------------------------------------------------
499
500wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) {
501 m_lastFound = NULL;
502 m_self = other.m_self;
503 m_class = other.m_class;
504 if (m_self) {
505 Py_INCREF(m_self);
506 Py_INCREF(m_class);
507 }
508}
509
510
511void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) {
512 m_self = self;
513 m_class = klass;
514 m_incRef = incref;
515 if (incref) {
516 Py_INCREF(m_self);
517 Py_INCREF(m_class);
518 }
519}
520
521
522// If the object (m_self) has an attibute of the given name, and if that
523// attribute is a method, and if that method's class is not from a base class,
524// then we'll save a pointer to the method so callCallback can call it.
525bool wxPyCallbackHelper::findCallback(const char* name) const {
526 wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const
527 self->m_lastFound = NULL;
528 if (m_self && PyObject_HasAttrString(m_self, (char*)name)) {
529 PyObject* method;
530 method = PyObject_GetAttrString(m_self, (char*)name);
531
532 if (PyMethod_Check(method) &&
533 ((PyMethod_GET_CLASS(method) == m_class) ||
534 PyClass_IsSubclass(PyMethod_GET_CLASS(method), m_class))) {
535
536 self->m_lastFound = method;
537 }
538 else {
539 Py_DECREF(method);
540 }
541 }
542 return m_lastFound != NULL;
543}
544
545
546int wxPyCallbackHelper::callCallback(PyObject* argTuple) const {
547 PyObject* result;
548 int retval = FALSE;
549
550 result = callCallbackObj(argTuple);
551 if (result) { // Assumes an integer return type...
552 retval = PyInt_AsLong(result);
553 Py_DECREF(result);
554 PyErr_Clear(); // forget about it if it's not...
555 }
556 return retval;
557}
558
559// Invoke the Python callable object, returning the raw PyObject return
560// value. Caller should DECREF the return value and also call PyEval_SaveThread.
561PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const {
562 PyObject* result;
563
564 // Save a copy of the pointer in case the callback generates another
565 // callback. In that case m_lastFound will have a different value when
566 // it gets back here...
567 PyObject* method = m_lastFound;
568
569 result = PyEval_CallObject(method, argTuple);
570 Py_DECREF(argTuple);
571 Py_DECREF(method);
572 if (!result) {
573 PyErr_Print();
574 }
575 return result;
576}
577
578
579void wxPyCBH_setSelf(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) {
580 cbh.setSelf(self, klass, incref);
581}
582
583bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) {
584 return cbh.findCallback(name);
585}
586
587int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
588 return cbh.callCallback(argTuple);
589}
590
591PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
592 return cbh.callCallbackObj(argTuple);
593}
594
595
596void wxPyCBH_delete(wxPyCallbackHelper* cbh) {
597 if (cbh->m_incRef) {
598 wxPyTState* state = wxPyBeginBlockThreads();
599 Py_XDECREF(cbh->m_self);
600 Py_XDECREF(cbh->m_class);
601 wxPyEndBlockThreads(state);
602 }
603}
604
605//---------------------------------------------------------------------------
606//---------------------------------------------------------------------------
607// These classes can be derived from in Python and passed through the event
608// system without losing anything. They do this by keeping a reference to
609// themselves and some special case handling in wxPyCallback::EventThunker.
610
611
612wxPyEvtSelfRef::wxPyEvtSelfRef() {
613 //m_self = Py_None; // **** We don't do normal ref counting to prevent
614 //Py_INCREF(m_self); // circular loops...
615 m_cloned = FALSE;
616}
617
618wxPyEvtSelfRef::~wxPyEvtSelfRef() {
619 wxPyTState* state = wxPyBeginBlockThreads();
620 if (m_cloned)
621 Py_DECREF(m_self);
622 wxPyEndBlockThreads(state);
623}
624
625void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) {
626 wxPyTState* state = wxPyBeginBlockThreads();
627 if (m_cloned)
628 Py_DECREF(m_self);
629 m_self = self;
630 if (clone) {
631 Py_INCREF(m_self);
632 m_cloned = TRUE;
633 }
634 wxPyEndBlockThreads(state);
635}
636
637PyObject* wxPyEvtSelfRef::GetSelf() const {
638 Py_INCREF(m_self);
639 return m_self;
640}
641
642
643wxPyEvent::wxPyEvent(int id)
644 : wxEvent(id) {
645}
646
647wxPyEvent::~wxPyEvent() {
648}
649
650// This one is so the event object can be Cloned...
651void wxPyEvent::CopyObject(wxObject& dest) const {
652 wxEvent::CopyObject(dest);
653 ((wxPyEvent*)&dest)->SetSelf(m_self, TRUE);
654}
655
656
657IMPLEMENT_DYNAMIC_CLASS(wxPyEvent, wxEvent);
658
659
660wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id)
661 : wxCommandEvent(commandType, id) {
662}
663
664wxPyCommandEvent::~wxPyCommandEvent() {
665}
666
667void wxPyCommandEvent::CopyObject(wxObject& dest) const {
668 wxCommandEvent::CopyObject(dest);
669 ((wxPyCommandEvent*)&dest)->SetSelf(m_self, TRUE);
670}
671
672
673IMPLEMENT_DYNAMIC_CLASS(wxPyCommandEvent, wxCommandEvent);
674
675
676
677//---------------------------------------------------------------------------
678//---------------------------------------------------------------------------
679
680
681wxPyTimer::wxPyTimer(PyObject* callback) {
682 func = callback;
683 Py_INCREF(func);
684}
685
686wxPyTimer::~wxPyTimer() {
687 wxPyTState* state = wxPyBeginBlockThreads();
688 Py_DECREF(func);
689 wxPyEndBlockThreads(state);
690}
691
692void wxPyTimer::Notify() {
693 if (!func || func == Py_None) {
694 wxTimer::Notify();
695 }
696 else {
697 wxPyTState* state = wxPyBeginBlockThreads();
698
699 PyObject* result;
700 PyObject* args = Py_BuildValue("()");
701
702 result = PyEval_CallObject(func, args);
703 Py_DECREF(args);
704 if (result) {
705 Py_DECREF(result);
706 PyErr_Clear();
707 } else {
708 PyErr_Print();
709 }
710
711 wxPyEndBlockThreads(state);
712 }
713}
714
715
716
717//---------------------------------------------------------------------------
718//---------------------------------------------------------------------------
719// Convert a wxList to a Python List
720
721PyObject* wxPy_ConvertList(wxListBase* list, const char* className) {
722 PyObject* pyList;
723 PyObject* pyObj;
724 wxObject* wxObj;
725 wxNode* node = list->First();
726
727 wxPyTState* state = wxPyBeginBlockThreads();
728 pyList = PyList_New(0);
729 while (node) {
730 wxObj = node->Data();
731 pyObj = wxPyMake_wxObject(wxObj); //wxPyConstructObject(wxObj, className);
732 PyList_Append(pyList, pyObj);
733 node = node->Next();
734 }
735 wxPyEndBlockThreads(state);
736 return pyList;
737}
738
739//----------------------------------------------------------------------
740
741long wxPyGetWinHandle(wxWindow* win) {
742#ifdef __WXMSW__
743 return (long)win->GetHandle();
744#endif
745
746 // Find and return the actual X-Window.
747#ifdef __WXGTK__
748 if (win->m_wxwindow) {
749 GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window;
750 if (bwin) {
751 return (long)bwin->xwindow;
752 }
753 }
754#endif
755 return 0;
756}
757
758//----------------------------------------------------------------------
759// Some helper functions for typemaps in my_typemaps.i, so they won't be
760// included in every file...
761
762
763byte* byte_LIST_helper(PyObject* source) {
764 if (!PyList_Check(source)) {
765 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
766 return NULL;
767 }
768 int count = PyList_Size(source);
769 byte* temp = new byte[count];
770 if (! temp) {
771 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
772 return NULL;
773 }
774 for (int x=0; x<count; x++) {
775 PyObject* o = PyList_GetItem(source, x);
776 if (! PyInt_Check(o)) {
777 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
778 return NULL;
779 }
780 temp[x] = (byte)PyInt_AsLong(o);
781 }
782 return temp;
783}
784
785
786int* int_LIST_helper(PyObject* source) {
787 if (!PyList_Check(source)) {
788 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
789 return NULL;
790 }
791 int count = PyList_Size(source);
792 int* temp = new int[count];
793 if (! temp) {
794 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
795 return NULL;
796 }
797 for (int x=0; x<count; x++) {
798 PyObject* o = PyList_GetItem(source, x);
799 if (! PyInt_Check(o)) {
800 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
801 return NULL;
802 }
803 temp[x] = PyInt_AsLong(o);
804 }
805 return temp;
806}
807
808
809long* long_LIST_helper(PyObject* source) {
810 if (!PyList_Check(source)) {
811 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
812 return NULL;
813 }
814 int count = PyList_Size(source);
815 long* temp = new long[count];
816 if (! temp) {
817 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
818 return NULL;
819 }
820 for (int x=0; x<count; x++) {
821 PyObject* o = PyList_GetItem(source, x);
822 if (! PyInt_Check(o)) {
823 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
824 return NULL;
825 }
826 temp[x] = PyInt_AsLong(o);
827 }
828 return temp;
829}
830
831
832char** string_LIST_helper(PyObject* source) {
833 if (!PyList_Check(source)) {
834 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
835 return NULL;
836 }
837 int count = PyList_Size(source);
838 char** temp = new char*[count];
839 if (! temp) {
840 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
841 return NULL;
842 }
843 for (int x=0; x<count; x++) {
844 PyObject* o = PyList_GetItem(source, x);
845 if (! PyString_Check(o)) {
846 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
847 return NULL;
848 }
849 temp[x] = PyString_AsString(o);
850 }
851 return temp;
852}
853
854//--------------------------------
855// Part of patch from Tim Hochberg
856static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) {
857 if (PyInt_Check(o1) && PyInt_Check(o2)) {
858 point->x = PyInt_AS_LONG(o1);
859 point->y = PyInt_AS_LONG(o2);
860 return true;
861 }
862 if (PyFloat_Check(o1) && PyFloat_Check(o2)) {
863 point->x = (int)PyFloat_AS_DOUBLE(o1);
864 point->y = (int)PyFloat_AS_DOUBLE(o2);
865 return true;
866 }
867 if (PyInstance_Check(o1) || PyInstance_Check(o2)) {
868 // Disallow instances because they can cause havok
869 return false;
870 }
871 if (PyNumber_Check(o1) && PyNumber_Check(o2)) {
872 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
873 point->x = PyInt_AsLong(o1);
874 point->y = PyInt_AsLong(o2);
875 return true;
876 }
877 return false;
878}
879
880
881#if PYTHON_API_VERSION < 1009
882#define PySequence_Fast_GET_ITEM(o, i)\
883 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
884#endif
885
886wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) {
887 // Putting all of the declarations here allows
888 // us to put the error handling all in one place.
889 int x;
890 wxPoint* temp;
891 PyObject *o, *o1, *o2;
892 int isFast = PyList_Check(source) || PyTuple_Check(source);
893
894 // The length of the sequence is returned in count.
895 if (!PySequence_Check(source)) {
896 goto error0;
897 }
898 *count = PySequence_Length(source);
899 if (*count < 0) {
900 goto error0;
901 }
902
903 temp = new wxPoint[*count];
904 if (!temp) {
905 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
906 return NULL;
907 }
908 for (x=0; x<*count; x++) {
909 // Get an item: try fast way first.
910 if (isFast) {
911 o = PySequence_Fast_GET_ITEM(source, x);
912 }
913 else {
914 o = PySequence_GetItem(source, x);
915 if (o == NULL) {
916 goto error1;
917 }
918 }
919
920 // Convert o to wxPoint.
921 if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) ||
922 (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) {
923 o1 = PySequence_Fast_GET_ITEM(o, 0);
924 o2 = PySequence_Fast_GET_ITEM(o, 1);
925 if (!wxPointFromObjects(o1, o2, &temp[x])) {
926 goto error2;
927 }
928 }
929 else if (PyInstance_Check(o)) {
930 wxPoint* pt;
931 if (SWIG_GetPtrObj(o, (void **)&pt, "_wxPoint_p")) {
932 goto error2;
933 }
934 temp[x] = *pt;
935 }
936 else if (PySequence_Check(o) && PySequence_Length(o) == 2) {
937 o1 = PySequence_GetItem(o, 0);
938 o2 = PySequence_GetItem(o, 1);
939 if (!wxPointFromObjects(o1, o2, &temp[x])) {
940 goto error3;
941 }
942 Py_DECREF(o1);
943 Py_DECREF(o2);
944 }
945 else {
946 goto error2;
947 }
948 // Clean up.
949 if (!isFast)
950 Py_DECREF(o);
951 }
952 return temp;
953
954error3:
955 Py_DECREF(o1);
956 Py_DECREF(o2);
957error2:
958 if (!isFast)
959 Py_DECREF(o);
960error1:
961 delete temp;
962error0:
963 PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints.");
964 return NULL;
965}
966// end of patch
967//------------------------------
968
969
970wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
971 if (!PyList_Check(source)) {
972 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
973 return NULL;
974 }
975 int count = PyList_Size(source);
976 wxBitmap** temp = new wxBitmap*[count];
977 if (! temp) {
978 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
979 return NULL;
980 }
981 for (int x=0; x<count; x++) {
982 PyObject* o = PyList_GetItem(source, x);
983 if (PyInstance_Check(o)) {
984 wxBitmap* pt;
985 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) {
986 PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p.");
987 return NULL;
988 }
989 temp[x] = pt;
990 }
991 else {
992 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
993 return NULL;
994 }
995 }
996 return temp;
997}
998
999
1000
1001wxString* wxString_LIST_helper(PyObject* source) {
1002 if (!PyList_Check(source)) {
1003 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1004 return NULL;
1005 }
1006 int count = PyList_Size(source);
1007 wxString* temp = new wxString[count];
1008 if (! temp) {
1009 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1010 return NULL;
1011 }
1012 for (int x=0; x<count; x++) {
1013 PyObject* o = PyList_GetItem(source, x);
1014#if PYTHON_API_VERSION >= 1009
1015 if (! PyString_Check(o) && ! PyUnicode_Check(o)) {
1016 PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects.");
1017 return NULL;
1018 }
1019
1020 char* buff;
1021 int length;
1022 if (PyString_AsStringAndSize(o, &buff, &length) == -1)
1023 return NULL;
1024 temp[x] = wxString(buff, length);
1025#else
1026 if (! PyString_Check(o)) {
1027 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
1028 return NULL;
1029 }
1030 temp[x] = PyString_AsString(o);
1031#endif
1032 }
1033 return temp;
1034}
1035
1036
1037wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
1038 if (!PyList_Check(source)) {
1039 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1040 return NULL;
1041 }
1042 int count = PyList_Size(source);
1043 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
1044 if (! temp) {
1045 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1046 return NULL;
1047 }
1048 for (int x=0; x<count; x++) {
1049 PyObject* o = PyList_GetItem(source, x);
1050 if (PyInstance_Check(o)) {
1051 wxAcceleratorEntry* ae;
1052 if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) {
1053 PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p.");
1054 return NULL;
1055 }
1056 temp[x] = *ae;
1057 }
1058 else if (PyTuple_Check(o)) {
1059 PyObject* o1 = PyTuple_GetItem(o, 0);
1060 PyObject* o2 = PyTuple_GetItem(o, 1);
1061 PyObject* o3 = PyTuple_GetItem(o, 2);
1062 temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
1063 }
1064 else {
1065 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1066 return NULL;
1067 }
1068 }
1069 return temp;
1070}
1071
1072
1073
1074//----------------------------------------------------------------------
1075
1076bool wxSize_helper(PyObject* source, wxSize** obj) {
1077
1078 // If source is an object instance then it may already be the right type
1079 if (PyInstance_Check(source)) {
1080 wxSize* ptr;
1081 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p"))
1082 goto error;
1083 *obj = ptr;
1084 return TRUE;
1085 }
1086 // otherwise a 2-tuple of integers is expected
1087 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1088 PyObject* o1 = PySequence_GetItem(source, 0);
1089 PyObject* o2 = PySequence_GetItem(source, 1);
1090 **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2));
1091 return TRUE;
1092 }
1093
1094 error:
1095 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object.");
1096 return FALSE;
1097}
1098
1099bool wxPoint_helper(PyObject* source, wxPoint** obj) {
1100
1101 // If source is an object instance then it may already be the right type
1102 if (PyInstance_Check(source)) {
1103 wxPoint* ptr;
1104 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p"))
1105 goto error;
1106 *obj = ptr;
1107 return TRUE;
1108 }
1109 // otherwise a length-2 sequence of integers is expected
1110 if (PySequence_Check(source) && PySequence_Length(source) == 2) {
1111 PyObject* o1 = PySequence_GetItem(source, 0);
1112 PyObject* o2 = PySequence_GetItem(source, 1);
1113 // This should really check for integers, not numbers -- but that would break code.
1114 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
1115 Py_DECREF(o1);
1116 Py_DECREF(o2);
1117 goto error;
1118 }
1119 **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2));
1120 Py_DECREF(o1);
1121 Py_DECREF(o2);
1122 return TRUE;
1123 }
1124 error:
1125 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object.");
1126 return FALSE;
1127}
1128
1129
1130
1131bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) {
1132
1133 // If source is an object instance then it may already be the right type
1134 if (PyInstance_Check(source)) {
1135 wxRealPoint* ptr;
1136 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p"))
1137 goto error;
1138 *obj = ptr;
1139 return TRUE;
1140 }
1141 // otherwise a 2-tuple of floats is expected
1142 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1143 PyObject* o1 = PySequence_GetItem(source, 0);
1144 PyObject* o2 = PySequence_GetItem(source, 1);
1145 **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
1146 return TRUE;
1147 }
1148
1149 error:
1150 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object.");
1151 return FALSE;
1152}
1153
1154
1155
1156
1157bool wxRect_helper(PyObject* source, wxRect** obj) {
1158
1159 // If source is an object instance then it may already be the right type
1160 if (PyInstance_Check(source)) {
1161 wxRect* ptr;
1162 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p"))
1163 goto error;
1164 *obj = ptr;
1165 return TRUE;
1166 }
1167 // otherwise a 4-tuple of integers is expected
1168 else if (PySequence_Check(source) && PyObject_Length(source) == 4) {
1169 PyObject* o1 = PySequence_GetItem(source, 0);
1170 PyObject* o2 = PySequence_GetItem(source, 1);
1171 PyObject* o3 = PySequence_GetItem(source, 2);
1172 PyObject* o4 = PySequence_GetItem(source, 3);
1173 **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2),
1174 PyInt_AsLong(o3), PyInt_AsLong(o4));
1175 return TRUE;
1176 }
1177
1178 error:
1179 PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object.");
1180 return FALSE;
1181}
1182
1183
1184
1185bool wxColour_helper(PyObject* source, wxColour** obj) {
1186
1187 // If source is an object instance then it may already be the right type
1188 if (PyInstance_Check(source)) {
1189 wxColour* ptr;
1190 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p"))
1191 goto error;
1192 *obj = ptr;
1193 return TRUE;
1194 }
1195 // otherwise a string is expected
1196 else if (PyString_Check(source)) {
1197 wxString spec = PyString_AS_STRING(source);
1198 if (spec[0U] == '#' && spec.Length() == 7) { // It's #RRGGBB
1199 char* junk;
1200 int red = strtol(spec.Mid(1,2), &junk, 16);
1201 int green = strtol(spec.Mid(3,2), &junk, 16);
1202 int blue = strtol(spec.Mid(5,2), &junk, 16);
1203 **obj = wxColour(red, green, blue);
1204 return TRUE;
1205 }
1206 else { // it's a colour name
1207 **obj = wxColour(spec);
1208 return TRUE;
1209 }
1210 }
1211
1212 error:
1213 PyErr_SetString(PyExc_TypeError, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1214 return FALSE;
1215}
1216
1217
1218//----------------------------------------------------------------------
1219//----------------------------------------------------------------------
1220//----------------------------------------------------------------------
1221
1222
1223
1224