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