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