]> git.saurik.com Git - wxWidgets.git/blame - wxPython/src/helpers.cpp
Emptied patch.rsp
[wxWidgets.git] / 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>
78b57918 21#include <wx/msw/winundef.h>
af309447 22#endif
694759cf
RD
23
24#ifdef __WXGTK__
25#include <gtk/gtk.h>
54b96882
RD
26#include <gdk/gdkprivate.h>
27#include <wx/gtk/win_gtk.h>
694759cf 28#endif
7bf85405 29
cf694132 30
7bf85405
RD
31
32
21f4bf45 33#ifdef __WXMSW__ // If building for win32...
21f4bf45
RD
34//----------------------------------------------------------------------
35// This gets run when the DLL is loaded. We just need to save a handle.
36//----------------------------------------------------------------------
9c039d08 37
21f4bf45
RD
38BOOL WINAPI DllMain(
39 HINSTANCE hinstDLL, // handle to DLL module
40 DWORD fdwReason, // reason for calling function
41 LPVOID lpvReserved // reserved
42 )
43{
af309447 44 wxSetInstance(hinstDLL);
21f4bf45
RD
45 return 1;
46}
47#endif
48
7bf85405
RD
49//----------------------------------------------------------------------
50// Class for implementing the wxp main application shell.
51//----------------------------------------------------------------------
52
53wxPyApp *wxPythonApp = NULL; // Global instance of application object
54
55
cf694132
RD
56wxPyApp::wxPyApp() {
57// printf("**** ctor\n");
58}
59
60wxPyApp::~wxPyApp() {
61// printf("**** dtor\n");
62}
63
64
7bf85405
RD
65// This one isn't acutally called... See __wxStart()
66bool wxPyApp::OnInit(void) {
6999b0d8 67 return FALSE;
7bf85405
RD
68}
69
70int wxPyApp::MainLoop(void) {
7ff49f0c 71 int retval = 0;
af309447 72
7ff49f0c
RD
73 DeletePendingObjects();
74#ifdef __WXGTK__
75 m_initialized = wxTopLevelWindows.GetCount() != 0;
76#endif
7bf85405 77
7ff49f0c
RD
78 if (Initialized()) {
79 retval = wxApp::MainLoop();
80 wxPythonApp->OnExit();
81 }
82 return retval;
83}
7bf85405
RD
84
85
fb5e0af0 86//---------------------------------------------------------------------
7bf85405
RD
87//----------------------------------------------------------------------
88
de20db99
RD
89#ifdef __WXMSW__
90#include "wx/msw/msvcrt.h"
91#endif
92
93
7ece89c6
RD
94int WXDLLEXPORT wxEntryStart( int argc, char** argv );
95int WXDLLEXPORT wxEntryInitGui();
96void WXDLLEXPORT wxEntryCleanup();
7ff49f0c 97
7bf85405 98
f6bcfd97 99#ifdef WXP_WITH_THREAD
19a97bd6 100PyInterpreterState* wxPyInterpreter = NULL;
f6bcfd97 101#endif
f6bcfd97
BP
102
103
0d6f9504 104// This is where we pick up the first part of the wxEntry functionality...
f6bcfd97 105// The rest is in __wxStart and __wxCleanup. This function is called when
8bf5d46e 106// wxcmodule is imported. (Before there is a wxApp object.)
0d6f9504 107void __wxPreStart()
7bf85405 108{
de20db99
RD
109
110#ifdef __WXMSW__
111// wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
112#endif
113
9d8bd15f 114#ifdef WXP_WITH_THREAD
1112b0c6 115 PyEval_InitThreads();
19a97bd6 116 wxPyInterpreter = PyThreadState_Get()->interp;
9d8bd15f
RD
117#endif
118
0d6f9504
RD
119 // Bail out if there is already windows created. This means that the
120 // toolkit has already been initialized, as in embedding wxPython in
121 // a C++ wxWindows app.
122 if (wxTopLevelWindows.Number() > 0)
123 return;
7bf85405 124
7ff49f0c 125
9416aa89
RD
126 int argc = 0;
127 char** argv = NULL;
7ff49f0c 128 PyObject* sysargv = PySys_GetObject("argv");
9416aa89
RD
129 if (sysargv != NULL) {
130 argc = PyList_Size(sysargv);
131 argv = new char*[argc+1];
132 int x;
133 for(x=0; x<argc; x++)
134 argv[x] = copystring(PyString_AsString(PyList_GetItem(sysargv, x)));
135 argv[argc] = NULL;
136 }
7bf85405 137
7ff49f0c 138 wxEntryStart(argc, argv);
f6bcfd97 139 delete [] argv;
0d6f9504
RD
140}
141
142
7ff49f0c 143
0d6f9504
RD
144// Start the user application, user App's OnInit method is a parameter here
145PyObject* __wxStart(PyObject* /* self */, PyObject* args)
146{
147 PyObject* onInitFunc = NULL;
148 PyObject* arglist;
149 PyObject* result;
150 long bResult;
151
0d6f9504
RD
152 if (!PyArg_ParseTuple(args, "O", &onInitFunc))
153 return NULL;
154
83b18bab 155#if 0 // Try it out without this check, see how it does...
0d6f9504
RD
156 if (wxTopLevelWindows.Number() > 0) {
157 PyErr_SetString(PyExc_TypeError, "Only 1 wxApp per process!");
158 return NULL;
159 }
de20db99 160#endif
0d6f9504
RD
161
162 // This is the next part of the wxEntry functionality...
9416aa89
RD
163 int argc = 0;
164 char** argv = NULL;
f6bcfd97 165 PyObject* sysargv = PySys_GetObject("argv");
9416aa89
RD
166 if (sysargv != NULL) {
167 argc = PyList_Size(sysargv);
168 argv = new char*[argc+1];
169 int x;
170 for(x=0; x<argc; x++)
171 argv[x] = copystring(PyString_AsString(PyList_GetItem(sysargv, x)));
172 argv[argc] = NULL;
173 }
f6bcfd97
BP
174 wxPythonApp->argc = argc;
175 wxPythonApp->argv = argv;
0d6f9504 176
7ff49f0c 177 wxEntryInitGui();
7bf85405
RD
178
179 // Call the Python App's OnInit function
180 arglist = PyTuple_New(0);
181 result = PyEval_CallObject(onInitFunc, arglist);
8bf5d46e
RD
182 if (!result) { // an exception was raised.
183 return NULL;
7bf85405
RD
184 }
185
186 if (! PyInt_Check(result)) {
187 PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value");
188 return NULL;
189 }
190 bResult = PyInt_AS_LONG(result);
191 if (! bResult) {
194fa2ac 192 PyErr_SetString(PyExc_SystemExit, "OnInit returned FALSE, exiting...");
7bf85405
RD
193 return NULL;
194 }
195
13dfc243 196#ifdef __WXGTK__
7ff49f0c 197 wxTheApp->m_initialized = (wxTopLevelWindows.GetCount() > 0);
13dfc243 198#endif
fb5e0af0 199
7bf85405
RD
200 Py_INCREF(Py_None);
201 return Py_None;
202}
203
7ff49f0c
RD
204void __wxCleanup() {
205 wxEntryCleanup();
206}
7bf85405
RD
207
208
209
9416aa89
RD
210static PyObject* wxPython_dict = NULL;
211static PyObject* wxPyPtrTypeMap = NULL;
212
7bf85405
RD
213PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args)
214{
215
216 if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
217 return NULL;
218
219 if (!PyDict_Check(wxPython_dict)) {
220 PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!");
221 return NULL;
222 }
9416aa89
RD
223
224 if (! wxPyPtrTypeMap)
225 wxPyPtrTypeMap = PyDict_New();
226 PyDict_SetItemString(wxPython_dict, "__wxPyPtrTypeMap", wxPyPtrTypeMap);
227
228
7bf85405 229#ifdef __WXMOTIF__
21f4bf45
RD
230#define wxPlatform "__WXMOTIF__"
231#endif
232#ifdef __WXQT__
233#define wxPlatform "__WXQT__"
7bf85405
RD
234#endif
235#ifdef __WXGTK__
21f4bf45 236#define wxPlatform "__WXGTK__"
7bf85405
RD
237#endif
238#if defined(__WIN32__) || defined(__WXMSW__)
fb5e0af0 239#define wxPlatform "__WXMSW__"
7bf85405
RD
240#endif
241#ifdef __WXMAC__
21f4bf45 242#define wxPlatform "__WXMAC__"
7bf85405
RD
243#endif
244
245 PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform));
246
247 Py_INCREF(Py_None);
248 return Py_None;
249}
250
251
9416aa89
RD
252//---------------------------------------------------------------------------
253// Stuff used by OOR to find the right wxPython class type to return and to
254// build it.
255
256
257// The pointer type map is used when the "pointer" type name generated by SWIG
258// is not the same as the shadow class name, for example wxPyTreeCtrl
259// vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
260// so we'll just make it a Python dictionary in the wx module's namespace.
261void wxPyPtrTypeMap_Add(const char* commonName, const char* ptrName) {
262 if (! wxPyPtrTypeMap)
263 wxPyPtrTypeMap = PyDict_New();
264
265 PyDict_SetItemString(wxPyPtrTypeMap,
266 (char*)commonName,
267 PyString_FromString((char*)ptrName));
268}
269
270
271
272PyObject* wxPyClassExists(const char* className) {
273
274 if (!className)
275 return NULL;
276
277 char buff[64]; // should always be big enough...
278
279 sprintf(buff, "%sPtr", className);
280 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
281
282 return classobj; // returns NULL if not found
283}
284
285
2f4e9287 286PyObject* wxPyMake_wxObject(wxObject* source, bool checkEvtHandler) {
0122b7e3
RD
287 PyObject* target = NULL;
288 bool isEvtHandler = FALSE;
9416aa89
RD
289
290 if (source) {
2aab8f16 291 // If it's derived from wxEvtHandler then there may
2f4e9287
RD
292 // already be a pointer to a Python object that we can use
293 // in the OOR data.
294 if (checkEvtHandler && wxIsKindOf(source, wxEvtHandler)) {
295 isEvtHandler = TRUE;
0122b7e3
RD
296 wxEvtHandler* eh = (wxEvtHandler*)source;
297 wxPyClientData* data = (wxPyClientData*)eh->GetClientObject();
298 if (data) {
299 target = data->m_obj;
300 Py_INCREF(target);
301 }
9416aa89 302 }
0122b7e3
RD
303
304 if (! target) {
2aab8f16
RD
305 // Otherwise make it the old fashioned way by making a
306 // new shadow object and putting this pointer in it.
0122b7e3
RD
307 wxClassInfo* info = source->GetClassInfo();
308 wxChar* name = (wxChar*)info->GetClassName();
309 PyObject* klass = wxPyClassExists(name);
310 while (info && !klass) {
311 name = (wxChar*)info->GetBaseClassName1();
312 info = wxClassInfo::FindClass(name);
313 klass = wxPyClassExists(name);
314 }
315 if (info) {
316 target = wxPyConstructObject(source, name, klass, FALSE);
317 if (target && isEvtHandler)
318 ((wxEvtHandler*)source)->SetClientObject(new wxPyClientData(target));
319 } else {
320 wxString msg("wxPython class not found for ");
321 msg += source->GetClassInfo()->GetClassName();
322 PyErr_SetString(PyExc_NameError, msg.c_str());
323 target = NULL;
324 }
9416aa89
RD
325 }
326 } else { // source was NULL so return None.
327 Py_INCREF(Py_None); target = Py_None;
328 }
329 return target;
330}
331
0122b7e3 332
2f4e9287
RD
333PyObject* wxPyMake_wxSizer(wxSizer* source) {
334 PyObject* target = NULL;
335
336 if (source && wxIsKindOf(source, wxSizer)) {
337 // If it's derived from wxSizer then there may
338 // already be a pointer to a Python object that we can use
339 // in the OOR data.
340 wxSizer* sz = (wxSizer*)source;
341 wxPyClientData* data = (wxPyClientData*)sz->GetClientObject();
342 if (data) {
343 target = data->m_obj;
344 Py_INCREF(target);
345 }
346 }
347 if (! target) {
348 target = wxPyMake_wxObject(source, FALSE);
349 if (target != Py_None)
350 ((wxSizer*)source)->SetClientObject(new wxPyClientData(target));
351 }
352 return target;
353}
354
355
356
7bf85405
RD
357//---------------------------------------------------------------------------
358
c368d904 359PyObject* wxPyConstructObject(void* ptr,
1e7ecb7b 360 const char* className,
9416aa89 361 PyObject* klass,
1e7ecb7b 362 int setThisOwn) {
9416aa89 363
33510773
RD
364 PyObject* obj;
365 PyObject* arg;
9416aa89
RD
366 PyObject* item;
367 char swigptr[64]; // should always be big enough...
368 char buff[64];
369
370 if ((item = PyDict_GetItemString(wxPyPtrTypeMap, (char*)className)) != NULL) {
371 className = PyString_AsString(item);
372 }
373 sprintf(buff, "_%s_p", className);
374 SWIG_MakePtr(swigptr, ptr, buff);
375
376 arg = Py_BuildValue("(s)", swigptr);
377 obj = PyInstance_New(klass, arg, NULL);
378 Py_DECREF(arg);
379
380 if (setThisOwn) {
381 PyObject* one = PyInt_FromLong(1);
382 PyObject_SetAttrString(obj, "thisown", one);
383 Py_DECREF(one);
384 }
385
386 return obj;
387}
388
389
390PyObject* wxPyConstructObject(void* ptr,
391 const char* className,
392 int setThisOwn) {
393 PyObject* obj;
33510773
RD
394
395 if (!ptr) {
396 Py_INCREF(Py_None);
397 return Py_None;
398 }
399
9c039d08 400 char buff[64]; // should always be big enough...
7bf85405 401 sprintf(buff, "%sPtr", className);
c5943253 402
78b57918 403 wxASSERT_MSG(wxPython_dict, "wxPython_dict is not set yet!!");
c5943253 404
7bf85405
RD
405 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
406 if (! classobj) {
33510773
RD
407 char temp[128];
408 sprintf(temp,
409 "*** Unknown class name %s, tell Robin about it please ***",
410 buff);
411 obj = PyString_FromString(temp);
412 return obj;
7bf85405
RD
413 }
414
9416aa89 415 return wxPyConstructObject(ptr, className, classobj, setThisOwn);
7bf85405
RD
416}
417
d559219f 418//---------------------------------------------------------------------------
7bf85405 419
19a97bd6
RD
420
421wxPyTState* wxPyBeginBlockThreads() {
422 wxPyTState* state = NULL;
cf694132 423#ifdef WXP_WITH_THREAD
19a97bd6
RD
424 if (1) { // Can I check if I've already got the lock?
425 state = new wxPyTState;
426 PyEval_AcquireLock();
427 state->newState = PyThreadState_New(wxPyInterpreter);
428 state->prevState = PyThreadState_Swap(state->newState);
1112b0c6 429 }
cf694132 430#endif
19a97bd6 431 return state;
d559219f 432}
cf694132 433
cf694132 434
19a97bd6 435void wxPyEndBlockThreads(wxPyTState* state) {
cf694132 436#ifdef WXP_WITH_THREAD
19a97bd6
RD
437 if (state) {
438 PyThreadState_Swap(state->prevState);
439 PyThreadState_Clear(state->newState);
440 PyEval_ReleaseLock();
441 PyThreadState_Delete(state->newState);
442 delete state;
1112b0c6
RD
443 }
444#endif
cf694132
RD
445}
446
19a97bd6 447
d559219f
RD
448//---------------------------------------------------------------------------
449
2f90df85
RD
450IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject);
451
d559219f
RD
452wxPyCallback::wxPyCallback(PyObject* func) {
453 m_func = func;
454 Py_INCREF(m_func);
455}
456
2f90df85
RD
457wxPyCallback::wxPyCallback(const wxPyCallback& other) {
458 m_func = other.m_func;
459 Py_INCREF(m_func);
460}
461
d559219f 462wxPyCallback::~wxPyCallback() {
19a97bd6 463 wxPyTState* state = wxPyBeginBlockThreads();
d559219f 464 Py_DECREF(m_func);
19a97bd6 465 wxPyEndBlockThreads(state);
d559219f
RD
466}
467
cf694132
RD
468
469
7bf85405
RD
470// This function is used for all events destined for Python event handlers.
471void wxPyCallback::EventThunker(wxEvent& event) {
472 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
473 PyObject* func = cb->m_func;
474 PyObject* result;
475 PyObject* arg;
476 PyObject* tuple;
477
cf694132 478
19a97bd6 479 wxPyTState* state = wxPyBeginBlockThreads();
65dd82cb
RD
480 wxString className = event.GetClassInfo()->GetClassName();
481
482 if (className == "wxPyEvent")
483 arg = ((wxPyEvent*)&event)->GetSelf();
484 else if (className == "wxPyCommandEvent")
485 arg = ((wxPyCommandEvent*)&event)->GetSelf();
486 else
487 arg = wxPyConstructObject((void*)&event, className);
7bf85405
RD
488
489 tuple = PyTuple_New(1);
490 PyTuple_SET_ITEM(tuple, 0, arg);
491 result = PyEval_CallObject(func, tuple);
7bf85405
RD
492 Py_DECREF(tuple);
493 if (result) {
494 Py_DECREF(result);
ac346f50 495 PyErr_Clear(); // Just in case...
7bf85405
RD
496 } else {
497 PyErr_Print();
498 }
19a97bd6 499 wxPyEndBlockThreads(state);
7bf85405
RD
500}
501
502
d559219f 503//----------------------------------------------------------------------
7bf85405 504
2f90df85
RD
505wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) {
506 m_lastFound = NULL;
507 m_self = other.m_self;
f6bcfd97
BP
508 m_class = other.m_class;
509 if (m_self) {
2f90df85 510 Py_INCREF(m_self);
f6bcfd97
BP
511 Py_INCREF(m_class);
512 }
2f90df85
RD
513}
514
515
33510773 516void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) {
d559219f 517 m_self = self;
33510773 518 m_class = klass;
b7312675 519 m_incRef = incref;
f6bcfd97 520 if (incref) {
2f90df85 521 Py_INCREF(m_self);
f6bcfd97
BP
522 Py_INCREF(m_class);
523 }
d559219f 524}
8bf5d46e 525
8bf5d46e 526
78b57918
RD
527#if PYTHON_API_VERSION >= 1011
528
529// Prior to Python 2.2 PyMethod_GetClass returned the class object
530// in which the method was defined. Starting with 2.2 it returns
531// "class that asked for the method" which seems totally bogus to me
532// but apprently if fixes some obscure problem waiting to happen in
533// Python. Since the API was not documented Guido and the gang felt
534// safe in changing it. Needless to say that totally screwed up the
535// logic below in wxPyCallbackHelper::findCallback, hence this icky
536// code to find the class where the method is actuallt defined...
537
538static
539PyObject* PyFindClassWithAttr(PyObject *klass, PyObject *name)
540{
541 int i, n;
542
543 if (PyType_Check(klass)) { // new style classes
544 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
4a98c806 545 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
78b57918
RD
546 PyTypeObject* type = (PyTypeObject*)klass;
547 PyObject *mro, *res, *base, *dict;
548 /* Look in tp_dict of types in MRO */
549 mro = type->tp_mro;
550 assert(PyTuple_Check(mro));
551 n = PyTuple_GET_SIZE(mro);
552 for (i = 0; i < n; i++) {
553 base = PyTuple_GET_ITEM(mro, i);
554 if (PyClass_Check(base))
555 dict = ((PyClassObject *)base)->cl_dict;
556 else {
557 assert(PyType_Check(base));
558 dict = ((PyTypeObject *)base)->tp_dict;
559 }
560 assert(dict && PyDict_Check(dict));
561 res = PyDict_GetItem(dict, name);
562 if (res != NULL)
4a98c806 563 return base;
78b57918
RD
564 }
565 return NULL;
566 }
567
568 else if (PyClass_Check(klass)) { // old style classes
569 // This code is borrowed/adapted from class_lookup in classobject.c
570 PyClassObject* cp = (PyClassObject*)klass;
571 PyObject *value = PyDict_GetItem(cp->cl_dict, name);
572 if (value != NULL) {
573 return (PyObject*)cp;
574 }
575 n = PyTuple_Size(cp->cl_bases);
576 for (i = 0; i < n; i++) {
577 PyObject* base = PyTuple_GetItem(cp->cl_bases, i);
578 PyObject *v = PyFindClassWithAttr(base, name);
579 if (v != NULL)
580 return v;
581 }
582 return NULL;
583 }
584}
585#endif
586
587
588static
589PyObject* PyMethod_GetDefiningClass(PyObject* method, const char* name)
590{
591 PyObject* mgc = PyMethod_GET_CLASS(method);
592
593#if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
594 return mgc;
595#else // 2.2 and after, the hard way...
596
597 PyObject* nameo = PyString_FromString(name);
598 PyObject* klass = PyFindClassWithAttr(mgc, nameo);
599 Py_DECREF(nameo);
600 return klass;
601#endif
602}
603
604
605
de20db99 606bool wxPyCallbackHelper::findCallback(const char* name) const {
f6bcfd97
BP
607 wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const
608 self->m_lastFound = NULL;
78b57918
RD
609
610 // If the object (m_self) has an attibute of the given name...
de20db99 611 if (m_self && PyObject_HasAttrString(m_self, (char*)name)) {
78b57918 612 PyObject *method, *klass;
de20db99 613 method = PyObject_GetAttrString(m_self, (char*)name);
f6bcfd97 614
78b57918
RD
615 // ...and if that attribute is a method, and if that method's class is
616 // not from a base class...
f6bcfd97 617 if (PyMethod_Check(method) &&
78b57918
RD
618 (klass = PyMethod_GetDefiningClass(method, (char*)name)) != NULL &&
619 ((klass == m_class) || PyClass_IsSubclass(klass, m_class))) {
8bf5d46e 620
78b57918 621 // ...then we'll save a pointer to the method so callCallback can call it.
f6bcfd97
BP
622 self->m_lastFound = method;
623 }
de20db99
RD
624 else {
625 Py_DECREF(method);
626 }
f6bcfd97 627 }
d559219f
RD
628 return m_lastFound != NULL;
629}
8bf5d46e 630
d559219f 631
f6bcfd97 632int wxPyCallbackHelper::callCallback(PyObject* argTuple) const {
d559219f
RD
633 PyObject* result;
634 int retval = FALSE;
635
636 result = callCallbackObj(argTuple);
637 if (result) { // Assumes an integer return type...
638 retval = PyInt_AsLong(result);
639 Py_DECREF(result);
640 PyErr_Clear(); // forget about it if it's not...
641 }
642 return retval;
643}
644
645// Invoke the Python callable object, returning the raw PyObject return
646// value. Caller should DECREF the return value and also call PyEval_SaveThread.
f6bcfd97 647PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const {
de20db99 648 PyObject* result;
d559219f 649
de20db99
RD
650 // Save a copy of the pointer in case the callback generates another
651 // callback. In that case m_lastFound will have a different value when
652 // it gets back here...
653 PyObject* method = m_lastFound;
654
655 result = PyEval_CallObject(method, argTuple);
d559219f 656 Py_DECREF(argTuple);
de20db99 657 Py_DECREF(method);
d559219f
RD
658 if (!result) {
659 PyErr_Print();
660 }
661 return result;
662}
714e6a9e 663
7bf85405 664
0122b7e3 665void wxPyCBH_setCallbackInfo(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) {
1e7ecb7b
RD
666 cbh.setSelf(self, klass, incref);
667}
668
669bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) {
670 return cbh.findCallback(name);
671}
672
673int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
674 return cbh.callCallback(argTuple);
675}
676
677PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
678 return cbh.callCallbackObj(argTuple);
679}
680
681
682void wxPyCBH_delete(wxPyCallbackHelper* cbh) {
1e7ecb7b 683 if (cbh->m_incRef) {
19a97bd6 684 wxPyTState* state = wxPyBeginBlockThreads();
1e7ecb7b
RD
685 Py_XDECREF(cbh->m_self);
686 Py_XDECREF(cbh->m_class);
19a97bd6 687 wxPyEndBlockThreads(state);
1e7ecb7b 688 }
1e7ecb7b 689}
d559219f 690
65dd82cb
RD
691//---------------------------------------------------------------------------
692//---------------------------------------------------------------------------
2aab8f16 693// These event classes can be derived from in Python and passed through the event
c368d904 694// system without losing anything. They do this by keeping a reference to
65dd82cb
RD
695// themselves and some special case handling in wxPyCallback::EventThunker.
696
697
e19b7164 698wxPyEvtSelfRef::wxPyEvtSelfRef() {
65dd82cb
RD
699 //m_self = Py_None; // **** We don't do normal ref counting to prevent
700 //Py_INCREF(m_self); // circular loops...
194fa2ac 701 m_cloned = FALSE;
65dd82cb
RD
702}
703
e19b7164 704wxPyEvtSelfRef::~wxPyEvtSelfRef() {
19a97bd6 705 wxPyTState* state = wxPyBeginBlockThreads();
65dd82cb
RD
706 if (m_cloned)
707 Py_DECREF(m_self);
19a97bd6 708 wxPyEndBlockThreads(state);
65dd82cb
RD
709}
710
e19b7164 711void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) {
19a97bd6 712 wxPyTState* state = wxPyBeginBlockThreads();
65dd82cb
RD
713 if (m_cloned)
714 Py_DECREF(m_self);
715 m_self = self;
716 if (clone) {
717 Py_INCREF(m_self);
718 m_cloned = TRUE;
719 }
19a97bd6 720 wxPyEndBlockThreads(state);
65dd82cb
RD
721}
722
e19b7164 723PyObject* wxPyEvtSelfRef::GetSelf() const {
65dd82cb
RD
724 Py_INCREF(m_self);
725 return m_self;
726}
727
728
07b2e1cd
RD
729IMPLEMENT_ABSTRACT_CLASS(wxPyEvent, wxEvent);
730IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent, wxCommandEvent);
731
732
65dd82cb
RD
733wxPyEvent::wxPyEvent(int id)
734 : wxEvent(id) {
735}
736
65dd82cb 737
07b2e1cd
RD
738wxPyEvent::wxPyEvent(const wxPyEvent& evt)
739 : wxEvent(evt)
740{
741 SetSelf(evt.m_self, TRUE);
65dd82cb
RD
742}
743
744
07b2e1cd
RD
745wxPyEvent::~wxPyEvent() {
746}
65dd82cb
RD
747
748
749wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id)
750 : wxCommandEvent(commandType, id) {
751}
752
65dd82cb 753
07b2e1cd
RD
754wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent& evt)
755 : wxCommandEvent(evt)
756{
757 SetSelf(evt.m_self, TRUE);
65dd82cb
RD
758}
759
760
07b2e1cd
RD
761wxPyCommandEvent::~wxPyCommandEvent() {
762}
763
65dd82cb
RD
764
765
766
d559219f 767//---------------------------------------------------------------------------
7bf85405
RD
768//---------------------------------------------------------------------------
769
d559219f 770
7bf85405
RD
771wxPyTimer::wxPyTimer(PyObject* callback) {
772 func = callback;
773 Py_INCREF(func);
774}
775
776wxPyTimer::~wxPyTimer() {
19a97bd6 777 wxPyTState* state = wxPyBeginBlockThreads();
7bf85405 778 Py_DECREF(func);
19a97bd6 779 wxPyEndBlockThreads(state);
7bf85405
RD
780}
781
782void wxPyTimer::Notify() {
185d7c3e
RD
783 if (!func || func == Py_None) {
784 wxTimer::Notify();
785 }
786 else {
19a97bd6 787 wxPyTState* state = wxPyBeginBlockThreads();
185d7c3e
RD
788
789 PyObject* result;
790 PyObject* args = Py_BuildValue("()");
791
792 result = PyEval_CallObject(func, args);
793 Py_DECREF(args);
794 if (result) {
795 Py_DECREF(result);
796 PyErr_Clear();
797 } else {
798 PyErr_Print();
799 }
7bf85405 800
19a97bd6 801 wxPyEndBlockThreads(state);
7bf85405
RD
802 }
803}
804
805
cf694132 806
2f90df85 807//---------------------------------------------------------------------------
389c5527
RD
808//---------------------------------------------------------------------------
809// Convert a wxList to a Python List
810
65dd82cb 811PyObject* wxPy_ConvertList(wxListBase* list, const char* className) {
389c5527
RD
812 PyObject* pyList;
813 PyObject* pyObj;
814 wxObject* wxObj;
815 wxNode* node = list->First();
816
19a97bd6 817 wxPyTState* state = wxPyBeginBlockThreads();
389c5527
RD
818 pyList = PyList_New(0);
819 while (node) {
820 wxObj = node->Data();
9416aa89 821 pyObj = wxPyMake_wxObject(wxObj); //wxPyConstructObject(wxObj, className);
389c5527
RD
822 PyList_Append(pyList, pyObj);
823 node = node->Next();
824 }
19a97bd6 825 wxPyEndBlockThreads(state);
389c5527
RD
826 return pyList;
827}
828
54b96882
RD
829//----------------------------------------------------------------------
830
831long wxPyGetWinHandle(wxWindow* win) {
832#ifdef __WXMSW__
833 return (long)win->GetHandle();
834#endif
835
836 // Find and return the actual X-Window.
837#ifdef __WXGTK__
838 if (win->m_wxwindow) {
839 GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window;
840 if (bwin) {
841 return (long)bwin->xwindow;
842 }
843 }
844#endif
845 return 0;
846}
847
7bf85405
RD
848//----------------------------------------------------------------------
849// Some helper functions for typemaps in my_typemaps.i, so they won't be
389c5527 850// included in every file...
7bf85405
RD
851
852
2f90df85 853byte* byte_LIST_helper(PyObject* source) {
b639c3c5
RD
854 if (!PyList_Check(source)) {
855 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
856 return NULL;
857 }
858 int count = PyList_Size(source);
859 byte* temp = new byte[count];
860 if (! temp) {
861 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
862 return NULL;
863 }
864 for (int x=0; x<count; x++) {
865 PyObject* o = PyList_GetItem(source, x);
866 if (! PyInt_Check(o)) {
867 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
868 return NULL;
869 }
870 temp[x] = (byte)PyInt_AsLong(o);
871 }
872 return temp;
873}
874
875
2f90df85 876int* int_LIST_helper(PyObject* source) {
7bf85405
RD
877 if (!PyList_Check(source)) {
878 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
879 return NULL;
880 }
881 int count = PyList_Size(source);
882 int* temp = new int[count];
883 if (! temp) {
884 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
885 return NULL;
886 }
887 for (int x=0; x<count; x++) {
888 PyObject* o = PyList_GetItem(source, x);
889 if (! PyInt_Check(o)) {
890 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
891 return NULL;
892 }
893 temp[x] = PyInt_AsLong(o);
894 }
895 return temp;
896}
897
898
2f90df85 899long* long_LIST_helper(PyObject* source) {
7bf85405
RD
900 if (!PyList_Check(source)) {
901 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
902 return NULL;
903 }
904 int count = PyList_Size(source);
905 long* temp = new long[count];
906 if (! temp) {
907 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
908 return NULL;
909 }
910 for (int x=0; x<count; x++) {
911 PyObject* o = PyList_GetItem(source, x);
912 if (! PyInt_Check(o)) {
913 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
914 return NULL;
915 }
916 temp[x] = PyInt_AsLong(o);
917 }
918 return temp;
919}
920
921
2f90df85 922char** string_LIST_helper(PyObject* source) {
7bf85405
RD
923 if (!PyList_Check(source)) {
924 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
925 return NULL;
926 }
927 int count = PyList_Size(source);
928 char** temp = new char*[count];
929 if (! temp) {
930 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
931 return NULL;
932 }
933 for (int x=0; x<count; x++) {
934 PyObject* o = PyList_GetItem(source, x);
935 if (! PyString_Check(o)) {
936 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
937 return NULL;
938 }
939 temp[x] = PyString_AsString(o);
940 }
941 return temp;
942}
943
e0672e2f
RD
944//--------------------------------
945// Part of patch from Tim Hochberg
946static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) {
947 if (PyInt_Check(o1) && PyInt_Check(o2)) {
948 point->x = PyInt_AS_LONG(o1);
949 point->y = PyInt_AS_LONG(o2);
950 return true;
951 }
952 if (PyFloat_Check(o1) && PyFloat_Check(o2)) {
953 point->x = (int)PyFloat_AS_DOUBLE(o1);
954 point->y = (int)PyFloat_AS_DOUBLE(o2);
955 return true;
956 }
957 if (PyInstance_Check(o1) || PyInstance_Check(o2)) {
958 // Disallow instances because they can cause havok
959 return false;
960 }
961 if (PyNumber_Check(o1) && PyNumber_Check(o2)) {
962 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
963 point->x = PyInt_AsLong(o1);
964 point->y = PyInt_AsLong(o2);
965 return true;
966 }
967 return false;
968}
7bf85405
RD
969
970
e0672e2f
RD
971wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) {
972 // Putting all of the declarations here allows
973 // us to put the error handling all in one place.
974 int x;
975 wxPoint* temp;
976 PyObject *o, *o1, *o2;
9d37f964 977 bool isFast = PyList_Check(source) || PyTuple_Check(source);
e0672e2f 978
e0672e2f
RD
979 if (!PySequence_Check(source)) {
980 goto error0;
7bf85405 981 }
9d37f964
RD
982
983 // The length of the sequence is returned in count.
e0672e2f
RD
984 *count = PySequence_Length(source);
985 if (*count < 0) {
986 goto error0;
987 }
988
989 temp = new wxPoint[*count];
990 if (!temp) {
7bf85405
RD
991 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
992 return NULL;
993 }
e0672e2f
RD
994 for (x=0; x<*count; x++) {
995 // Get an item: try fast way first.
996 if (isFast) {
997 o = PySequence_Fast_GET_ITEM(source, x);
998 }
999 else {
1000 o = PySequence_GetItem(source, x);
1001 if (o == NULL) {
1002 goto error1;
1003 }
1004 }
7bf85405 1005
e0672e2f
RD
1006 // Convert o to wxPoint.
1007 if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) ||
1008 (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) {
1009 o1 = PySequence_Fast_GET_ITEM(o, 0);
1010 o2 = PySequence_Fast_GET_ITEM(o, 1);
1011 if (!wxPointFromObjects(o1, o2, &temp[x])) {
1012 goto error2;
1013 }
7bf85405 1014 }
d559219f
RD
1015 else if (PyInstance_Check(o)) {
1016 wxPoint* pt;
e0672e2f
RD
1017 if (SWIG_GetPtrObj(o, (void **)&pt, "_wxPoint_p")) {
1018 goto error2;
d559219f
RD
1019 }
1020 temp[x] = *pt;
1021 }
e0672e2f
RD
1022 else if (PySequence_Check(o) && PySequence_Length(o) == 2) {
1023 o1 = PySequence_GetItem(o, 0);
1024 o2 = PySequence_GetItem(o, 1);
1025 if (!wxPointFromObjects(o1, o2, &temp[x])) {
1026 goto error3;
1027 }
1028 Py_DECREF(o1);
1029 Py_DECREF(o2);
1030 }
7bf85405 1031 else {
e0672e2f 1032 goto error2;
7bf85405 1033 }
e0672e2f
RD
1034 // Clean up.
1035 if (!isFast)
1036 Py_DECREF(o);
7bf85405
RD
1037 }
1038 return temp;
e0672e2f
RD
1039
1040error3:
1041 Py_DECREF(o1);
1042 Py_DECREF(o2);
1043error2:
1044 if (!isFast)
1045 Py_DECREF(o);
1046error1:
1047 delete temp;
1048error0:
1049 PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints.");
1050 return NULL;
7bf85405 1051}
e0672e2f
RD
1052// end of patch
1053//------------------------------
7bf85405
RD
1054
1055
2f90df85 1056wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
7bf85405
RD
1057 if (!PyList_Check(source)) {
1058 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1059 return NULL;
1060 }
1061 int count = PyList_Size(source);
1062 wxBitmap** temp = new wxBitmap*[count];
1063 if (! temp) {
1064 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1065 return NULL;
1066 }
1067 for (int x=0; x<count; x++) {
1068 PyObject* o = PyList_GetItem(source, x);
e166644c 1069 if (PyInstance_Check(o)) {
7bf85405 1070 wxBitmap* pt;
e166644c 1071 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) {
7bf85405
RD
1072 PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p.");
1073 return NULL;
1074 }
1075 temp[x] = pt;
1076 }
1077 else {
1078 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
1079 return NULL;
1080 }
1081 }
1082 return temp;
1083}
1084
1085
1086
2f90df85 1087wxString* wxString_LIST_helper(PyObject* source) {
7bf85405
RD
1088 if (!PyList_Check(source)) {
1089 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1090 return NULL;
1091 }
1092 int count = PyList_Size(source);
1093 wxString* temp = new wxString[count];
1094 if (! temp) {
1095 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1096 return NULL;
1097 }
1098 for (int x=0; x<count; x++) {
1099 PyObject* o = PyList_GetItem(source, x);
ecc08ead
RD
1100#if PYTHON_API_VERSION >= 1009
1101 if (! PyString_Check(o) && ! PyUnicode_Check(o)) {
1102 PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects.");
1103 return NULL;
1104 }
1105
1106 char* buff;
1107 int length;
1108 if (PyString_AsStringAndSize(o, &buff, &length) == -1)
1109 return NULL;
1110 temp[x] = wxString(buff, length);
1111#else
7bf85405
RD
1112 if (! PyString_Check(o)) {
1113 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
1114 return NULL;
1115 }
1116 temp[x] = PyString_AsString(o);
ecc08ead 1117#endif
7bf85405
RD
1118 }
1119 return temp;
1120}
1121
1122
2f90df85 1123wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
7bf85405
RD
1124 if (!PyList_Check(source)) {
1125 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1126 return NULL;
1127 }
1128 int count = PyList_Size(source);
1129 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
1130 if (! temp) {
1131 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1132 return NULL;
1133 }
1134 for (int x=0; x<count; x++) {
1135 PyObject* o = PyList_GetItem(source, x);
e166644c 1136 if (PyInstance_Check(o)) {
7bf85405 1137 wxAcceleratorEntry* ae;
e166644c 1138 if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) {
7bf85405
RD
1139 PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p.");
1140 return NULL;
1141 }
1142 temp[x] = *ae;
1143 }
1144 else if (PyTuple_Check(o)) {
1145 PyObject* o1 = PyTuple_GetItem(o, 0);
1146 PyObject* o2 = PyTuple_GetItem(o, 1);
1147 PyObject* o3 = PyTuple_GetItem(o, 2);
0adbc166 1148 temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
7bf85405
RD
1149 }
1150 else {
1151 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1152 return NULL;
1153 }
1154 }
1155 return temp;
1156}
1157
bb0054cd 1158
9d37f964
RD
1159wxPen** wxPen_LIST_helper(PyObject* source) {
1160 if (!PyList_Check(source)) {
1161 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1162 return NULL;
1163 }
1164 int count = PyList_Size(source);
1165 wxPen** temp = new wxPen*[count];
1166 if (!temp) {
1167 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1168 return NULL;
1169 }
1170 for (int x=0; x<count; x++) {
1171 PyObject* o = PyList_GetItem(source, x);
1172 if (PyInstance_Check(o)) {
1173 wxPen* pt;
1174 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxPen_p")) {
1175 delete temp;
1176 PyErr_SetString(PyExc_TypeError,"Expected _wxPen_p.");
1177 return NULL;
1178 }
1179 temp[x] = pt;
1180 }
1181 else {
1182 delete temp;
1183 PyErr_SetString(PyExc_TypeError, "Expected a list of wxPens.");
1184 return NULL;
1185 }
1186 }
1187 return temp;
1188}
1189
1190
1191bool _2int_seq_helper(PyObject* source, int* i1, int* i2) {
1192 bool isFast = PyList_Check(source) || PyTuple_Check(source);
1193 PyObject *o1, *o2;
1194
1195 if (!PySequence_Check(source) || PySequence_Length(source) != 2)
1196 return FALSE;
1197
1198 if (isFast) {
1199 o1 = PySequence_Fast_GET_ITEM(source, 0);
1200 o2 = PySequence_Fast_GET_ITEM(source, 1);
1201 }
1202 else {
1203 o1 = PySequence_GetItem(source, 0);
1204 o2 = PySequence_GetItem(source, 1);
1205 }
1206
1207 *i1 = PyInt_AsLong(o1);
1208 *i2 = PyInt_AsLong(o2);
1209
1210 if (! isFast) {
1211 Py_DECREF(o1);
1212 Py_DECREF(o2);
1213 }
1214 return TRUE;
1215}
1216
1217
1218bool _4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4) {
1219 bool isFast = PyList_Check(source) || PyTuple_Check(source);
1220 PyObject *o1, *o2, *o3, *o4;
1221
1222 if (!PySequence_Check(source) || PySequence_Length(source) != 4)
1223 return FALSE;
1224
1225 if (isFast) {
1226 o1 = PySequence_Fast_GET_ITEM(source, 0);
1227 o2 = PySequence_Fast_GET_ITEM(source, 1);
1228 o3 = PySequence_Fast_GET_ITEM(source, 2);
1229 o4 = PySequence_Fast_GET_ITEM(source, 3);
1230 }
1231 else {
1232 o1 = PySequence_GetItem(source, 0);
1233 o2 = PySequence_GetItem(source, 1);
1234 o3 = PySequence_GetItem(source, 2);
1235 o4 = PySequence_GetItem(source, 3);
1236 }
1237
1238 *i1 = PyInt_AsLong(o1);
1239 *i2 = PyInt_AsLong(o2);
1240 *i3 = PyInt_AsLong(o3);
1241 *i4 = PyInt_AsLong(o4);
1242
1243 if (! isFast) {
1244 Py_DECREF(o1);
1245 Py_DECREF(o2);
1246 Py_DECREF(o3);
1247 Py_DECREF(o4);
1248 }
1249 return TRUE;
1250}
1251
bb0054cd
RD
1252
1253//----------------------------------------------------------------------
bb0054cd 1254
2f90df85
RD
1255bool wxSize_helper(PyObject* source, wxSize** obj) {
1256
1257 // If source is an object instance then it may already be the right type
1258 if (PyInstance_Check(source)) {
1259 wxSize* ptr;
1260 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p"))
1261 goto error;
1262 *obj = ptr;
1263 return TRUE;
1264 }
1265 // otherwise a 2-tuple of integers is expected
1266 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1267 PyObject* o1 = PySequence_GetItem(source, 0);
1268 PyObject* o2 = PySequence_GetItem(source, 1);
1269 **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2));
1270 return TRUE;
1271 }
1272
1273 error:
1274 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object.");
1275 return FALSE;
1276}
1277
1278bool wxPoint_helper(PyObject* source, wxPoint** obj) {
1279
1280 // If source is an object instance then it may already be the right type
1281 if (PyInstance_Check(source)) {
1282 wxPoint* ptr;
1283 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p"))
1284 goto error;
1285 *obj = ptr;
1286 return TRUE;
1287 }
e0672e2f
RD
1288 // otherwise a length-2 sequence of integers is expected
1289 if (PySequence_Check(source) && PySequence_Length(source) == 2) {
2f90df85
RD
1290 PyObject* o1 = PySequence_GetItem(source, 0);
1291 PyObject* o2 = PySequence_GetItem(source, 1);
78b57918
RD
1292 // This should really check for integers, not numbers -- but that would break code.
1293 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
1294 Py_DECREF(o1);
1295 Py_DECREF(o2);
1296 goto error;
1297 }
1298 **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2));
1299 Py_DECREF(o1);
1300 Py_DECREF(o2);
2f90df85
RD
1301 return TRUE;
1302 }
2f90df85
RD
1303 error:
1304 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object.");
1305 return FALSE;
1306}
1307
1308
1309
1310bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) {
1311
1312 // If source is an object instance then it may already be the right type
1313 if (PyInstance_Check(source)) {
1314 wxRealPoint* ptr;
1315 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p"))
1316 goto error;
1317 *obj = ptr;
1318 return TRUE;
1319 }
1320 // otherwise a 2-tuple of floats is expected
1321 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1322 PyObject* o1 = PySequence_GetItem(source, 0);
1323 PyObject* o2 = PySequence_GetItem(source, 1);
1324 **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
1325 return TRUE;
1326 }
1327
1328 error:
1329 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object.");
1330 return FALSE;
1331}
1332
1333
1334
1335
1336bool wxRect_helper(PyObject* source, wxRect** obj) {
1337
1338 // If source is an object instance then it may already be the right type
1339 if (PyInstance_Check(source)) {
1340 wxRect* ptr;
1341 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p"))
1342 goto error;
1343 *obj = ptr;
1344 return TRUE;
1345 }
1346 // otherwise a 4-tuple of integers is expected
1347 else if (PySequence_Check(source) && PyObject_Length(source) == 4) {
1348 PyObject* o1 = PySequence_GetItem(source, 0);
1349 PyObject* o2 = PySequence_GetItem(source, 1);
1350 PyObject* o3 = PySequence_GetItem(source, 2);
1351 PyObject* o4 = PySequence_GetItem(source, 3);
1352 **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2),
1353 PyInt_AsLong(o3), PyInt_AsLong(o4));
1354 return TRUE;
1355 }
1356
1357 error:
1358 PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object.");
1359 return FALSE;
1360}
1361
1362
1363
f6bcfd97
BP
1364bool wxColour_helper(PyObject* source, wxColour** obj) {
1365
1366 // If source is an object instance then it may already be the right type
1367 if (PyInstance_Check(source)) {
1368 wxColour* ptr;
1369 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p"))
1370 goto error;
1371 *obj = ptr;
1372 return TRUE;
1373 }
1374 // otherwise a string is expected
1375 else if (PyString_Check(source)) {
1376 wxString spec = PyString_AS_STRING(source);
1112b0c6 1377 if (spec[0U] == '#' && spec.Length() == 7) { // It's #RRGGBB
f6bcfd97
BP
1378 char* junk;
1379 int red = strtol(spec.Mid(1,2), &junk, 16);
1380 int green = strtol(spec.Mid(3,2), &junk, 16);
1381 int blue = strtol(spec.Mid(5,2), &junk, 16);
1382 **obj = wxColour(red, green, blue);
1383 return TRUE;
1384 }
1385 else { // it's a colour name
1386 **obj = wxColour(spec);
1387 return TRUE;
1388 }
1389 }
1390
1391 error:
de20db99 1392 PyErr_SetString(PyExc_TypeError, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
f6bcfd97
BP
1393 return FALSE;
1394}
1395
1396
bb0054cd 1397//----------------------------------------------------------------------
b37c7e1d
RD
1398
1399PyObject* wxArrayString2PyList_helper(const wxArrayString& arr) {
1400
1401 PyObject* list = PyList_New(0);
1402 for (size_t i=0; i < arr.GetCount(); i++) {
1403 PyObject* str = PyString_FromString(arr[i].c_str());
1404 PyList_Append(list, str);
8af26133 1405 Py_DECREF(str);
b37c7e1d
RD
1406 }
1407 return list;
1408}
1409
1410
bb0054cd 1411//----------------------------------------------------------------------
7bf85405 1412//----------------------------------------------------------------------
7bf85405 1413
7bf85405 1414
7bf85405 1415
de20db99 1416