]> git.saurik.com Git - wxWidgets.git/blob - wxPython/src/helpers.cpp
An incorrect patch to SWIG caused incorrect generated code. Fixed and regenerated.
[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 wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) {
897 // Putting all of the declarations here allows
898 // us to put the error handling all in one place.
899 int x;
900 wxPoint* temp;
901 PyObject *o, *o1, *o2;
902 bool isFast = PyList_Check(source) || PyTuple_Check(source);
903
904 if (!PySequence_Check(source)) {
905 goto error0;
906 }
907
908 // The length of the sequence is returned in count.
909 *count = PySequence_Length(source);
910 if (*count < 0) {
911 goto error0;
912 }
913
914 temp = new wxPoint[*count];
915 if (!temp) {
916 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
917 return NULL;
918 }
919 for (x=0; x<*count; x++) {
920 // Get an item: try fast way first.
921 if (isFast) {
922 o = PySequence_Fast_GET_ITEM(source, x);
923 }
924 else {
925 o = PySequence_GetItem(source, x);
926 if (o == NULL) {
927 goto error1;
928 }
929 }
930
931 // Convert o to wxPoint.
932 if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) ||
933 (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) {
934 o1 = PySequence_Fast_GET_ITEM(o, 0);
935 o2 = PySequence_Fast_GET_ITEM(o, 1);
936 if (!wxPointFromObjects(o1, o2, &temp[x])) {
937 goto error2;
938 }
939 }
940 else if (PyInstance_Check(o)) {
941 wxPoint* pt;
942 if (SWIG_GetPtrObj(o, (void **)&pt, "_wxPoint_p")) {
943 goto error2;
944 }
945 temp[x] = *pt;
946 }
947 else if (PySequence_Check(o) && PySequence_Length(o) == 2) {
948 o1 = PySequence_GetItem(o, 0);
949 o2 = PySequence_GetItem(o, 1);
950 if (!wxPointFromObjects(o1, o2, &temp[x])) {
951 goto error3;
952 }
953 Py_DECREF(o1);
954 Py_DECREF(o2);
955 }
956 else {
957 goto error2;
958 }
959 // Clean up.
960 if (!isFast)
961 Py_DECREF(o);
962 }
963 return temp;
964
965 error3:
966 Py_DECREF(o1);
967 Py_DECREF(o2);
968 error2:
969 if (!isFast)
970 Py_DECREF(o);
971 error1:
972 delete temp;
973 error0:
974 PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints.");
975 return NULL;
976 }
977 // end of patch
978 //------------------------------
979
980
981 wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
982 if (!PyList_Check(source)) {
983 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
984 return NULL;
985 }
986 int count = PyList_Size(source);
987 wxBitmap** temp = new wxBitmap*[count];
988 if (! temp) {
989 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
990 return NULL;
991 }
992 for (int x=0; x<count; x++) {
993 PyObject* o = PyList_GetItem(source, x);
994 if (PyInstance_Check(o)) {
995 wxBitmap* pt;
996 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) {
997 PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p.");
998 return NULL;
999 }
1000 temp[x] = pt;
1001 }
1002 else {
1003 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
1004 return NULL;
1005 }
1006 }
1007 return temp;
1008 }
1009
1010
1011
1012 wxString* wxString_LIST_helper(PyObject* source) {
1013 if (!PyList_Check(source)) {
1014 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1015 return NULL;
1016 }
1017 int count = PyList_Size(source);
1018 wxString* temp = new wxString[count];
1019 if (! temp) {
1020 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1021 return NULL;
1022 }
1023 for (int x=0; x<count; x++) {
1024 PyObject* o = PyList_GetItem(source, x);
1025 #if PYTHON_API_VERSION >= 1009
1026 if (! PyString_Check(o) && ! PyUnicode_Check(o)) {
1027 PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects.");
1028 return NULL;
1029 }
1030
1031 char* buff;
1032 int length;
1033 if (PyString_AsStringAndSize(o, &buff, &length) == -1)
1034 return NULL;
1035 temp[x] = wxString(buff, length);
1036 #else
1037 if (! PyString_Check(o)) {
1038 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
1039 return NULL;
1040 }
1041 temp[x] = PyString_AsString(o);
1042 #endif
1043 }
1044 return temp;
1045 }
1046
1047
1048 wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
1049 if (!PyList_Check(source)) {
1050 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1051 return NULL;
1052 }
1053 int count = PyList_Size(source);
1054 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
1055 if (! temp) {
1056 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1057 return NULL;
1058 }
1059 for (int x=0; x<count; x++) {
1060 PyObject* o = PyList_GetItem(source, x);
1061 if (PyInstance_Check(o)) {
1062 wxAcceleratorEntry* ae;
1063 if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) {
1064 PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p.");
1065 return NULL;
1066 }
1067 temp[x] = *ae;
1068 }
1069 else if (PyTuple_Check(o)) {
1070 PyObject* o1 = PyTuple_GetItem(o, 0);
1071 PyObject* o2 = PyTuple_GetItem(o, 1);
1072 PyObject* o3 = PyTuple_GetItem(o, 2);
1073 temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
1074 }
1075 else {
1076 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1077 return NULL;
1078 }
1079 }
1080 return temp;
1081 }
1082
1083
1084 wxPen** wxPen_LIST_helper(PyObject* source) {
1085 if (!PyList_Check(source)) {
1086 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1087 return NULL;
1088 }
1089 int count = PyList_Size(source);
1090 wxPen** temp = new wxPen*[count];
1091 if (!temp) {
1092 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1093 return NULL;
1094 }
1095 for (int x=0; x<count; x++) {
1096 PyObject* o = PyList_GetItem(source, x);
1097 if (PyInstance_Check(o)) {
1098 wxPen* pt;
1099 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxPen_p")) {
1100 delete temp;
1101 PyErr_SetString(PyExc_TypeError,"Expected _wxPen_p.");
1102 return NULL;
1103 }
1104 temp[x] = pt;
1105 }
1106 else {
1107 delete temp;
1108 PyErr_SetString(PyExc_TypeError, "Expected a list of wxPens.");
1109 return NULL;
1110 }
1111 }
1112 return temp;
1113 }
1114
1115
1116 bool _2int_seq_helper(PyObject* source, int* i1, int* i2) {
1117 bool isFast = PyList_Check(source) || PyTuple_Check(source);
1118 PyObject *o1, *o2;
1119
1120 if (!PySequence_Check(source) || PySequence_Length(source) != 2)
1121 return FALSE;
1122
1123 if (isFast) {
1124 o1 = PySequence_Fast_GET_ITEM(source, 0);
1125 o2 = PySequence_Fast_GET_ITEM(source, 1);
1126 }
1127 else {
1128 o1 = PySequence_GetItem(source, 0);
1129 o2 = PySequence_GetItem(source, 1);
1130 }
1131
1132 *i1 = PyInt_AsLong(o1);
1133 *i2 = PyInt_AsLong(o2);
1134
1135 if (! isFast) {
1136 Py_DECREF(o1);
1137 Py_DECREF(o2);
1138 }
1139 return TRUE;
1140 }
1141
1142
1143 bool _4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4) {
1144 bool isFast = PyList_Check(source) || PyTuple_Check(source);
1145 PyObject *o1, *o2, *o3, *o4;
1146
1147 if (!PySequence_Check(source) || PySequence_Length(source) != 4)
1148 return FALSE;
1149
1150 if (isFast) {
1151 o1 = PySequence_Fast_GET_ITEM(source, 0);
1152 o2 = PySequence_Fast_GET_ITEM(source, 1);
1153 o3 = PySequence_Fast_GET_ITEM(source, 2);
1154 o4 = PySequence_Fast_GET_ITEM(source, 3);
1155 }
1156 else {
1157 o1 = PySequence_GetItem(source, 0);
1158 o2 = PySequence_GetItem(source, 1);
1159 o3 = PySequence_GetItem(source, 2);
1160 o4 = PySequence_GetItem(source, 3);
1161 }
1162
1163 *i1 = PyInt_AsLong(o1);
1164 *i2 = PyInt_AsLong(o2);
1165 *i3 = PyInt_AsLong(o3);
1166 *i4 = PyInt_AsLong(o4);
1167
1168 if (! isFast) {
1169 Py_DECREF(o1);
1170 Py_DECREF(o2);
1171 Py_DECREF(o3);
1172 Py_DECREF(o4);
1173 }
1174 return TRUE;
1175 }
1176
1177
1178 //----------------------------------------------------------------------
1179
1180 bool wxSize_helper(PyObject* source, wxSize** obj) {
1181
1182 // If source is an object instance then it may already be the right type
1183 if (PyInstance_Check(source)) {
1184 wxSize* ptr;
1185 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p"))
1186 goto error;
1187 *obj = ptr;
1188 return TRUE;
1189 }
1190 // otherwise a 2-tuple of integers is expected
1191 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1192 PyObject* o1 = PySequence_GetItem(source, 0);
1193 PyObject* o2 = PySequence_GetItem(source, 1);
1194 **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2));
1195 return TRUE;
1196 }
1197
1198 error:
1199 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object.");
1200 return FALSE;
1201 }
1202
1203 bool wxPoint_helper(PyObject* source, wxPoint** obj) {
1204
1205 // If source is an object instance then it may already be the right type
1206 if (PyInstance_Check(source)) {
1207 wxPoint* ptr;
1208 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p"))
1209 goto error;
1210 *obj = ptr;
1211 return TRUE;
1212 }
1213 // otherwise a length-2 sequence of integers is expected
1214 if (PySequence_Check(source) && PySequence_Length(source) == 2) {
1215 PyObject* o1 = PySequence_GetItem(source, 0);
1216 PyObject* o2 = PySequence_GetItem(source, 1);
1217 // This should really check for integers, not numbers -- but that would break code.
1218 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
1219 Py_DECREF(o1);
1220 Py_DECREF(o2);
1221 goto error;
1222 }
1223 **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2));
1224 Py_DECREF(o1);
1225 Py_DECREF(o2);
1226 return TRUE;
1227 }
1228 error:
1229 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object.");
1230 return FALSE;
1231 }
1232
1233
1234
1235 bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) {
1236
1237 // If source is an object instance then it may already be the right type
1238 if (PyInstance_Check(source)) {
1239 wxRealPoint* ptr;
1240 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p"))
1241 goto error;
1242 *obj = ptr;
1243 return TRUE;
1244 }
1245 // otherwise a 2-tuple of floats is expected
1246 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1247 PyObject* o1 = PySequence_GetItem(source, 0);
1248 PyObject* o2 = PySequence_GetItem(source, 1);
1249 **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
1250 return TRUE;
1251 }
1252
1253 error:
1254 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object.");
1255 return FALSE;
1256 }
1257
1258
1259
1260
1261 bool wxRect_helper(PyObject* source, wxRect** obj) {
1262
1263 // If source is an object instance then it may already be the right type
1264 if (PyInstance_Check(source)) {
1265 wxRect* ptr;
1266 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p"))
1267 goto error;
1268 *obj = ptr;
1269 return TRUE;
1270 }
1271 // otherwise a 4-tuple of integers is expected
1272 else if (PySequence_Check(source) && PyObject_Length(source) == 4) {
1273 PyObject* o1 = PySequence_GetItem(source, 0);
1274 PyObject* o2 = PySequence_GetItem(source, 1);
1275 PyObject* o3 = PySequence_GetItem(source, 2);
1276 PyObject* o4 = PySequence_GetItem(source, 3);
1277 **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2),
1278 PyInt_AsLong(o3), PyInt_AsLong(o4));
1279 return TRUE;
1280 }
1281
1282 error:
1283 PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object.");
1284 return FALSE;
1285 }
1286
1287
1288
1289 bool wxColour_helper(PyObject* source, wxColour** obj) {
1290
1291 // If source is an object instance then it may already be the right type
1292 if (PyInstance_Check(source)) {
1293 wxColour* ptr;
1294 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p"))
1295 goto error;
1296 *obj = ptr;
1297 return TRUE;
1298 }
1299 // otherwise a string is expected
1300 else if (PyString_Check(source)) {
1301 wxString spec = PyString_AS_STRING(source);
1302 if (spec[0U] == '#' && spec.Length() == 7) { // It's #RRGGBB
1303 char* junk;
1304 int red = strtol(spec.Mid(1,2), &junk, 16);
1305 int green = strtol(spec.Mid(3,2), &junk, 16);
1306 int blue = strtol(spec.Mid(5,2), &junk, 16);
1307 **obj = wxColour(red, green, blue);
1308 return TRUE;
1309 }
1310 else { // it's a colour name
1311 **obj = wxColour(spec);
1312 return TRUE;
1313 }
1314 }
1315
1316 error:
1317 PyErr_SetString(PyExc_TypeError, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1318 return FALSE;
1319 }
1320
1321
1322 //----------------------------------------------------------------------
1323 //----------------------------------------------------------------------
1324 //----------------------------------------------------------------------
1325
1326
1327
1328