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