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