]> git.saurik.com Git - wxWidgets.git/blob - wxPython/src/helpers.cpp
wxDefaultPosition and wxDefaultSize are marked read-only.
[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 //#include <gdk/gdk.h>
33 //#include <gdk/gdkx.h>
34 //#include <gtk/gtkwindow.h>
35
36 //extern GtkWidget *wxRootWindow;
37
38 #endif
39
40
41
42
43 #ifdef __WXMSW__ // If building for win32...
44 //----------------------------------------------------------------------
45 // This gets run when the DLL is loaded. We just need to save a handle.
46 //----------------------------------------------------------------------
47
48 BOOL WINAPI DllMain(
49 HINSTANCE hinstDLL, // handle to DLL module
50 DWORD fdwReason, // reason for calling function
51 LPVOID lpvReserved // reserved
52 )
53 {
54 wxSetInstance(hinstDLL);
55 return 1;
56 }
57 #endif
58
59 //----------------------------------------------------------------------
60 // Class for implementing the wxp main application shell.
61 //----------------------------------------------------------------------
62
63 wxPyApp *wxPythonApp = NULL; // Global instance of application object
64
65
66 wxPyApp::wxPyApp() {
67 // printf("**** ctor\n");
68 }
69
70 wxPyApp::~wxPyApp() {
71 // printf("**** dtor\n");
72 }
73
74
75 // This one isn't acutally called... See __wxStart()
76 bool wxPyApp::OnInit(void) {
77 return FALSE;
78 }
79
80 int wxPyApp::MainLoop(void) {
81 int retval = 0;
82
83 DeletePendingObjects();
84 #ifdef __WXGTK__
85 m_initialized = wxTopLevelWindows.GetCount() != 0;
86 #endif
87
88 if (Initialized()) {
89 retval = wxApp::MainLoop();
90 wxPythonApp->OnExit();
91 }
92 return retval;
93 }
94
95
96 //---------------------------------------------------------------------
97 //----------------------------------------------------------------------
98
99 #ifdef __WXMSW__
100 #include "wx/msw/msvcrt.h"
101 #endif
102
103
104 int WXDLLEXPORT wxEntryStart( int argc, char** argv );
105 int WXDLLEXPORT wxEntryInitGui();
106 void WXDLLEXPORT wxEntryCleanup();
107
108
109 #ifdef WXP_WITH_THREAD
110 PyThreadState* wxPyEventThreadState = NULL;
111 #endif
112 static char* __nullArgv[1] = { 0 };
113
114
115 // This is where we pick up the first part of the wxEntry functionality...
116 // The rest is in __wxStart and __wxCleanup. This function is called when
117 // wxcmodule is imported. (Before there is a wxApp object.)
118 void __wxPreStart()
119 {
120
121 #ifdef __WXMSW__
122 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
123 #endif
124
125 #ifdef WXP_WITH_THREAD
126 PyEval_InitThreads();
127 wxPyEventThreadState = PyThreadState_Get();
128 #endif
129
130 // Bail out if there is already windows created. This means that the
131 // toolkit has already been initialized, as in embedding wxPython in
132 // a C++ wxWindows app.
133 if (wxTopLevelWindows.Number() > 0)
134 return;
135
136
137 PyObject* sysargv = PySys_GetObject("argv");
138 int argc = PyList_Size(sysargv);
139 char** argv = new char*[argc+1];
140 int x;
141 for(x=0; x<argc; x++)
142 argv[x] = PyString_AsString(PyList_GetItem(sysargv, x));
143 argv[argc] = NULL;
144
145 wxEntryStart(argc, argv);
146 delete [] argv;
147 }
148
149
150
151 // Start the user application, user App's OnInit method is a parameter here
152 PyObject* __wxStart(PyObject* /* self */, PyObject* args)
153 {
154 PyObject* onInitFunc = NULL;
155 PyObject* arglist;
156 PyObject* result;
157 long bResult;
158
159 if (!PyArg_ParseTuple(args, "O", &onInitFunc))
160 return NULL;
161
162 #if 0 // Try it out without this check, soo how it does...
163 if (wxTopLevelWindows.Number() > 0) {
164 PyErr_SetString(PyExc_TypeError, "Only 1 wxApp per process!");
165 return NULL;
166 }
167 #endif
168
169 // This is the next part of the wxEntry functionality...
170 PyObject* sysargv = PySys_GetObject("argv");
171 int argc = PyList_Size(sysargv);
172 char** 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 PyObject* wxPython_dict;
215 PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args)
216 {
217
218 if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
219 return NULL;
220
221 if (!PyDict_Check(wxPython_dict)) {
222 PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!");
223 return NULL;
224 }
225 #ifdef __WXMOTIF__
226 #define wxPlatform "__WXMOTIF__"
227 #endif
228 #ifdef __WXQT__
229 #define wxPlatform "__WXQT__"
230 #endif
231 #ifdef __WXGTK__
232 #define wxPlatform "__WXGTK__"
233 #endif
234 #if defined(__WIN32__) || defined(__WXMSW__)
235 #define wxPlatform "__WXMSW__"
236 #endif
237 #ifdef __WXMAC__
238 #define wxPlatform "__WXMAC__"
239 #endif
240
241 PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform));
242
243 Py_INCREF(Py_None);
244 return Py_None;
245 }
246
247
248 //---------------------------------------------------------------------------
249
250 PyObject* wxPyConstructObject(void* ptr,
251 const char* className,
252 int setThisOwn) {
253 PyObject* obj;
254 PyObject* arg;
255
256 if (!ptr) {
257 Py_INCREF(Py_None);
258 return Py_None;
259 }
260
261 char buff[64]; // should always be big enough...
262 char swigptr[64];
263
264 sprintf(buff, "_%s_p", className);
265 SWIG_MakePtr(swigptr, ptr, buff);
266
267 sprintf(buff, "%sPtr", className);
268 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
269 if (! classobj) {
270 //Py_INCREF(Py_None);
271 //return Py_None;
272 char temp[128];
273 sprintf(temp,
274 "*** Unknown class name %s, tell Robin about it please ***",
275 buff);
276 obj = PyString_FromString(temp);
277 return obj;
278 }
279
280 arg = Py_BuildValue("(s)", swigptr);
281 obj = PyInstance_New(classobj, arg, NULL);
282 Py_DECREF(arg);
283
284 if (setThisOwn) {
285 PyObject* one = PyInt_FromLong(1);
286 PyObject_SetAttrString(obj, "thisown", one);
287 Py_DECREF(one);
288 }
289
290 return obj;
291 }
292
293 //---------------------------------------------------------------------------
294
295 static PyThreadState* myPyThreadState_Get() {
296 PyThreadState* current;
297 current = PyThreadState_Swap(NULL);
298 PyThreadState_Swap(current);
299 return current;
300 }
301
302
303 bool wxPyRestoreThread() {
304 // NOTE: The Python API docs state that if a thread already has the
305 // interpreter lock and calls PyEval_RestoreThread again a deadlock
306 // occurs, so I put in this code as a guard condition since there are
307 // many possibilites for nested events and callbacks in wxPython. If
308 // The current thread is our thread, then we can assume that we
309 // already have the lock. (I hope!)
310 //
311 #ifdef WXP_WITH_THREAD
312 if (wxPyEventThreadState != myPyThreadState_Get()) {
313 PyEval_RestoreThread(wxPyEventThreadState);
314 return TRUE;
315 }
316 else
317 #endif
318 return FALSE;
319 }
320
321
322 void wxPySaveThread(bool doSave) {
323 #ifdef WXP_WITH_THREAD
324 if (doSave) {
325 wxPyEventThreadState = PyEval_SaveThread();
326 }
327 #endif
328 }
329
330 //---------------------------------------------------------------------------
331
332
333 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject);
334
335 wxPyCallback::wxPyCallback(PyObject* func) {
336 m_func = func;
337 Py_INCREF(m_func);
338 }
339
340 wxPyCallback::wxPyCallback(const wxPyCallback& other) {
341 m_func = other.m_func;
342 Py_INCREF(m_func);
343 }
344
345 wxPyCallback::~wxPyCallback() {
346 bool doSave = wxPyRestoreThread();
347 Py_DECREF(m_func);
348 wxPySaveThread(doSave);
349 }
350
351
352
353 // This function is used for all events destined for Python event handlers.
354 void wxPyCallback::EventThunker(wxEvent& event) {
355 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
356 PyObject* func = cb->m_func;
357 PyObject* result;
358 PyObject* arg;
359 PyObject* tuple;
360
361
362 bool doSave = wxPyRestoreThread();
363 wxString className = event.GetClassInfo()->GetClassName();
364
365 if (className == "wxPyEvent")
366 arg = ((wxPyEvent*)&event)->GetSelf();
367 else if (className == "wxPyCommandEvent")
368 arg = ((wxPyCommandEvent*)&event)->GetSelf();
369 else
370 arg = wxPyConstructObject((void*)&event, className);
371
372 tuple = PyTuple_New(1);
373 PyTuple_SET_ITEM(tuple, 0, arg);
374 result = PyEval_CallObject(func, tuple);
375 Py_DECREF(tuple);
376 if (result) {
377 Py_DECREF(result);
378 PyErr_Clear();
379 } else {
380 PyErr_Print();
381 }
382 wxPySaveThread(doSave);
383 }
384
385
386 //----------------------------------------------------------------------
387
388 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) {
389 m_lastFound = NULL;
390 m_self = other.m_self;
391 m_class = other.m_class;
392 if (m_self) {
393 Py_INCREF(m_self);
394 Py_INCREF(m_class);
395 }
396 }
397
398
399 void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) {
400 m_self = self;
401 m_class = klass;
402 m_incRef = incref;
403 if (incref) {
404 Py_INCREF(m_self);
405 Py_INCREF(m_class);
406 }
407 }
408
409
410 // If the object (m_self) has an attibute of the given name, and if that
411 // attribute is a method, and if that method's class is not from a base class,
412 // then we'll save a pointer to the method so callCallback can call it.
413 bool wxPyCallbackHelper::findCallback(const char* name) const {
414 wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const
415 self->m_lastFound = NULL;
416 if (m_self && PyObject_HasAttrString(m_self, (char*)name)) {
417 PyObject* method;
418 method = PyObject_GetAttrString(m_self, (char*)name);
419
420 if (PyMethod_Check(method) &&
421 ((PyMethod_GET_CLASS(method) == m_class) ||
422 PyClass_IsSubclass(PyMethod_GET_CLASS(method), m_class))) {
423
424 self->m_lastFound = method;
425 }
426 else {
427 Py_DECREF(method);
428 }
429 }
430 return m_lastFound != NULL;
431 }
432
433
434 int wxPyCallbackHelper::callCallback(PyObject* argTuple) const {
435 PyObject* result;
436 int retval = FALSE;
437
438 result = callCallbackObj(argTuple);
439 if (result) { // Assumes an integer return type...
440 retval = PyInt_AsLong(result);
441 Py_DECREF(result);
442 PyErr_Clear(); // forget about it if it's not...
443 }
444 return retval;
445 }
446
447 // Invoke the Python callable object, returning the raw PyObject return
448 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
449 PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const {
450 wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const
451 PyObject* result;
452
453 // Save a copy of the pointer in case the callback generates another
454 // callback. In that case m_lastFound will have a different value when
455 // it gets back here...
456 PyObject* method = m_lastFound;
457
458 result = PyEval_CallObject(method, argTuple);
459 Py_DECREF(argTuple);
460 Py_DECREF(method);
461 if (!result) {
462 PyErr_Print();
463 }
464 return result;
465 }
466
467
468 void wxPyCBH_setSelf(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) {
469 cbh.setSelf(self, klass, incref);
470 }
471
472 bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) {
473 return cbh.findCallback(name);
474 }
475
476 int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
477 return cbh.callCallback(argTuple);
478 }
479
480 PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
481 return cbh.callCallbackObj(argTuple);
482 }
483
484
485 void wxPyCBH_delete(wxPyCallbackHelper* cbh) {
486 bool doSave = wxPyRestoreThread();
487 if (cbh->m_incRef) {
488 Py_XDECREF(cbh->m_self);
489 Py_XDECREF(cbh->m_class);
490 }
491 wxPySaveThread(doSave);
492 }
493
494 //---------------------------------------------------------------------------
495 //---------------------------------------------------------------------------
496 // These classes can be derived from in Python and passed through the event
497 // system without losing anything. They do this by keeping a reference to
498 // themselves and some special case handling in wxPyCallback::EventThunker.
499
500
501 wxPyEvtSelfRef::wxPyEvtSelfRef() {
502 //m_self = Py_None; // **** We don't do normal ref counting to prevent
503 //Py_INCREF(m_self); // circular loops...
504 m_cloned = FALSE;
505 }
506
507 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
508 bool doSave = wxPyRestoreThread();
509 if (m_cloned)
510 Py_DECREF(m_self);
511 wxPySaveThread(doSave);
512 }
513
514 void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) {
515 bool doSave = wxPyRestoreThread();
516 if (m_cloned)
517 Py_DECREF(m_self);
518 m_self = self;
519 if (clone) {
520 Py_INCREF(m_self);
521 m_cloned = TRUE;
522 }
523 wxPySaveThread(doSave);
524 }
525
526 PyObject* wxPyEvtSelfRef::GetSelf() const {
527 Py_INCREF(m_self);
528 return m_self;
529 }
530
531
532 wxPyEvent::wxPyEvent(int id)
533 : wxEvent(id) {
534 }
535
536 wxPyEvent::~wxPyEvent() {
537 }
538
539 // This one is so the event object can be Cloned...
540 void wxPyEvent::CopyObject(wxObject& dest) const {
541 wxEvent::CopyObject(dest);
542 ((wxPyEvent*)&dest)->SetSelf(m_self, TRUE);
543 }
544
545
546 IMPLEMENT_DYNAMIC_CLASS(wxPyEvent, wxEvent);
547
548
549 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id)
550 : wxCommandEvent(commandType, id) {
551 }
552
553 wxPyCommandEvent::~wxPyCommandEvent() {
554 }
555
556 void wxPyCommandEvent::CopyObject(wxObject& dest) const {
557 wxCommandEvent::CopyObject(dest);
558 ((wxPyCommandEvent*)&dest)->SetSelf(m_self, TRUE);
559 }
560
561
562 IMPLEMENT_DYNAMIC_CLASS(wxPyCommandEvent, wxCommandEvent);
563
564
565
566 //---------------------------------------------------------------------------
567 //---------------------------------------------------------------------------
568
569
570 wxPyTimer::wxPyTimer(PyObject* callback) {
571 func = callback;
572 Py_INCREF(func);
573 }
574
575 wxPyTimer::~wxPyTimer() {
576 bool doSave = wxPyRestoreThread();
577 Py_DECREF(func);
578 wxPySaveThread(doSave);
579 }
580
581 void wxPyTimer::Notify() {
582 if (!func || func == Py_None) {
583 wxTimer::Notify();
584 }
585 else {
586 bool doSave = wxPyRestoreThread();
587
588 PyObject* result;
589 PyObject* args = Py_BuildValue("()");
590
591 result = PyEval_CallObject(func, args);
592 Py_DECREF(args);
593 if (result) {
594 Py_DECREF(result);
595 PyErr_Clear();
596 } else {
597 PyErr_Print();
598 }
599
600 wxPySaveThread(doSave);
601 }
602 }
603
604
605
606 //---------------------------------------------------------------------------
607 //---------------------------------------------------------------------------
608 // Convert a wxList to a Python List
609
610 PyObject* wxPy_ConvertList(wxListBase* list, const char* className) {
611 PyObject* pyList;
612 PyObject* pyObj;
613 wxObject* wxObj;
614 wxNode* node = list->First();
615
616 bool doSave = wxPyRestoreThread();
617 pyList = PyList_New(0);
618 while (node) {
619 wxObj = node->Data();
620 pyObj = wxPyConstructObject(wxObj, className);
621 PyList_Append(pyList, pyObj);
622 node = node->Next();
623 }
624 wxPySaveThread(doSave);
625 return pyList;
626 }
627
628 //----------------------------------------------------------------------
629
630 long wxPyGetWinHandle(wxWindow* win) {
631 #ifdef __WXMSW__
632 return (long)win->GetHandle();
633 #endif
634
635 // Find and return the actual X-Window.
636 #ifdef __WXGTK__
637 if (win->m_wxwindow) {
638 GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window;
639 if (bwin) {
640 return (long)bwin->xwindow;
641 }
642 }
643 #endif
644 return 0;
645 }
646
647 //----------------------------------------------------------------------
648 // Some helper functions for typemaps in my_typemaps.i, so they won't be
649 // included in every file...
650
651
652 byte* byte_LIST_helper(PyObject* source) {
653 if (!PyList_Check(source)) {
654 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
655 return NULL;
656 }
657 int count = PyList_Size(source);
658 byte* temp = new byte[count];
659 if (! temp) {
660 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
661 return NULL;
662 }
663 for (int x=0; x<count; x++) {
664 PyObject* o = PyList_GetItem(source, x);
665 if (! PyInt_Check(o)) {
666 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
667 return NULL;
668 }
669 temp[x] = (byte)PyInt_AsLong(o);
670 }
671 return temp;
672 }
673
674
675 int* int_LIST_helper(PyObject* source) {
676 if (!PyList_Check(source)) {
677 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
678 return NULL;
679 }
680 int count = PyList_Size(source);
681 int* temp = new int[count];
682 if (! temp) {
683 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
684 return NULL;
685 }
686 for (int x=0; x<count; x++) {
687 PyObject* o = PyList_GetItem(source, x);
688 if (! PyInt_Check(o)) {
689 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
690 return NULL;
691 }
692 temp[x] = PyInt_AsLong(o);
693 }
694 return temp;
695 }
696
697
698 long* long_LIST_helper(PyObject* source) {
699 if (!PyList_Check(source)) {
700 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
701 return NULL;
702 }
703 int count = PyList_Size(source);
704 long* temp = new long[count];
705 if (! temp) {
706 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
707 return NULL;
708 }
709 for (int x=0; x<count; x++) {
710 PyObject* o = PyList_GetItem(source, x);
711 if (! PyInt_Check(o)) {
712 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
713 return NULL;
714 }
715 temp[x] = PyInt_AsLong(o);
716 }
717 return temp;
718 }
719
720
721 char** string_LIST_helper(PyObject* source) {
722 if (!PyList_Check(source)) {
723 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
724 return NULL;
725 }
726 int count = PyList_Size(source);
727 char** temp = new char*[count];
728 if (! temp) {
729 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
730 return NULL;
731 }
732 for (int x=0; x<count; x++) {
733 PyObject* o = PyList_GetItem(source, x);
734 if (! PyString_Check(o)) {
735 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
736 return NULL;
737 }
738 temp[x] = PyString_AsString(o);
739 }
740 return temp;
741 }
742
743
744
745 wxPoint* wxPoint_LIST_helper(PyObject* source) {
746 if (!PyList_Check(source)) {
747 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
748 return NULL;
749 }
750 int count = PyList_Size(source);
751 wxPoint* temp = new wxPoint[count];
752 if (! temp) {
753 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
754 return NULL;
755 }
756 for (int x=0; x<count; x++) {
757 PyObject* o = PyList_GetItem(source, x);
758 if (PyTuple_Check(o)) {
759 PyObject* o1 = PyTuple_GetItem(o, 0);
760 PyObject* o2 = PyTuple_GetItem(o, 1);
761
762 temp[x].x = PyInt_AsLong(o1);
763 temp[x].y = PyInt_AsLong(o2);
764 }
765 else if (PyInstance_Check(o)) {
766 wxPoint* pt;
767 if (SWIG_GetPtrObj(o,(void **) &pt,"_wxPoint_p")) {
768 PyErr_SetString(PyExc_TypeError,"Expected _wxPoint_p.");
769 return NULL;
770 }
771 temp[x] = *pt;
772 }
773 else {
774 PyErr_SetString(PyExc_TypeError, "Expected a list of 2-tuples or wxPoints.");
775 return NULL;
776 }
777 }
778 return temp;
779 }
780
781
782 wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
783 if (!PyList_Check(source)) {
784 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
785 return NULL;
786 }
787 int count = PyList_Size(source);
788 wxBitmap** temp = new wxBitmap*[count];
789 if (! temp) {
790 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
791 return NULL;
792 }
793 for (int x=0; x<count; x++) {
794 PyObject* o = PyList_GetItem(source, x);
795 if (PyInstance_Check(o)) {
796 wxBitmap* pt;
797 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) {
798 PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p.");
799 return NULL;
800 }
801 temp[x] = pt;
802 }
803 else {
804 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
805 return NULL;
806 }
807 }
808 return temp;
809 }
810
811
812
813 wxString* wxString_LIST_helper(PyObject* source) {
814 if (!PyList_Check(source)) {
815 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
816 return NULL;
817 }
818 int count = PyList_Size(source);
819 wxString* temp = new wxString[count];
820 if (! temp) {
821 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
822 return NULL;
823 }
824 for (int x=0; x<count; x++) {
825 PyObject* o = PyList_GetItem(source, x);
826 if (! PyString_Check(o)) {
827 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
828 return NULL;
829 }
830 temp[x] = PyString_AsString(o);
831 }
832 return temp;
833 }
834
835
836 wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
837 if (!PyList_Check(source)) {
838 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
839 return NULL;
840 }
841 int count = PyList_Size(source);
842 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
843 if (! temp) {
844 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
845 return NULL;
846 }
847 for (int x=0; x<count; x++) {
848 PyObject* o = PyList_GetItem(source, x);
849 if (PyInstance_Check(o)) {
850 wxAcceleratorEntry* ae;
851 if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) {
852 PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p.");
853 return NULL;
854 }
855 temp[x] = *ae;
856 }
857 else if (PyTuple_Check(o)) {
858 PyObject* o1 = PyTuple_GetItem(o, 0);
859 PyObject* o2 = PyTuple_GetItem(o, 1);
860 PyObject* o3 = PyTuple_GetItem(o, 2);
861
862 temp[x].m_flags = PyInt_AsLong(o1);
863 temp[x].m_keyCode = PyInt_AsLong(o2);
864 temp[x].m_command = PyInt_AsLong(o3);
865 }
866 else {
867 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
868 return NULL;
869 }
870 }
871 return temp;
872 }
873
874
875
876 //----------------------------------------------------------------------
877
878 bool wxSize_helper(PyObject* source, wxSize** obj) {
879
880 // If source is an object instance then it may already be the right type
881 if (PyInstance_Check(source)) {
882 wxSize* ptr;
883 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p"))
884 goto error;
885 *obj = ptr;
886 return TRUE;
887 }
888 // otherwise a 2-tuple of integers is expected
889 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
890 PyObject* o1 = PySequence_GetItem(source, 0);
891 PyObject* o2 = PySequence_GetItem(source, 1);
892 **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2));
893 return TRUE;
894 }
895
896 error:
897 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object.");
898 return FALSE;
899 }
900
901 bool wxPoint_helper(PyObject* source, wxPoint** obj) {
902
903 // If source is an object instance then it may already be the right type
904 if (PyInstance_Check(source)) {
905 wxPoint* ptr;
906 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p"))
907 goto error;
908 *obj = ptr;
909 return TRUE;
910 }
911 // otherwise a 2-tuple of integers is expected
912 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
913 PyObject* o1 = PySequence_GetItem(source, 0);
914 PyObject* o2 = PySequence_GetItem(source, 1);
915 **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2));
916 return TRUE;
917 }
918
919 error:
920 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object.");
921 return FALSE;
922 }
923
924
925
926 bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) {
927
928 // If source is an object instance then it may already be the right type
929 if (PyInstance_Check(source)) {
930 wxRealPoint* ptr;
931 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p"))
932 goto error;
933 *obj = ptr;
934 return TRUE;
935 }
936 // otherwise a 2-tuple of floats is expected
937 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
938 PyObject* o1 = PySequence_GetItem(source, 0);
939 PyObject* o2 = PySequence_GetItem(source, 1);
940 **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
941 return TRUE;
942 }
943
944 error:
945 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object.");
946 return FALSE;
947 }
948
949
950
951
952 bool wxRect_helper(PyObject* source, wxRect** obj) {
953
954 // If source is an object instance then it may already be the right type
955 if (PyInstance_Check(source)) {
956 wxRect* ptr;
957 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p"))
958 goto error;
959 *obj = ptr;
960 return TRUE;
961 }
962 // otherwise a 4-tuple of integers is expected
963 else if (PySequence_Check(source) && PyObject_Length(source) == 4) {
964 PyObject* o1 = PySequence_GetItem(source, 0);
965 PyObject* o2 = PySequence_GetItem(source, 1);
966 PyObject* o3 = PySequence_GetItem(source, 2);
967 PyObject* o4 = PySequence_GetItem(source, 3);
968 **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2),
969 PyInt_AsLong(o3), PyInt_AsLong(o4));
970 return TRUE;
971 }
972
973 error:
974 PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object.");
975 return FALSE;
976 }
977
978
979
980 bool wxColour_helper(PyObject* source, wxColour** obj) {
981
982 // If source is an object instance then it may already be the right type
983 if (PyInstance_Check(source)) {
984 wxColour* ptr;
985 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p"))
986 goto error;
987 *obj = ptr;
988 return TRUE;
989 }
990 // otherwise a string is expected
991 else if (PyString_Check(source)) {
992 wxString spec = PyString_AS_STRING(source);
993 if (spec[0] == '#' && spec.Length() == 7) { // It's #RRGGBB
994 char* junk;
995 int red = strtol(spec.Mid(1,2), &junk, 16);
996 int green = strtol(spec.Mid(3,2), &junk, 16);
997 int blue = strtol(spec.Mid(5,2), &junk, 16);
998 **obj = wxColour(red, green, blue);
999 return TRUE;
1000 }
1001 else { // it's a colour name
1002 **obj = wxColour(spec);
1003 return TRUE;
1004 }
1005 }
1006
1007 error:
1008 PyErr_SetString(PyExc_TypeError, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1009 return FALSE;
1010 }
1011
1012
1013 //----------------------------------------------------------------------
1014 //----------------------------------------------------------------------
1015 //----------------------------------------------------------------------
1016
1017
1018
1019