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