]> git.saurik.com Git - wxWidgets.git/blame - wxPython/src/helpers.cpp
Bug fix
[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
08127323 13
7bf85405
RD
14#undef DEBUG
15#include <Python.h>
16#include "helpers.h"
cbf60e09 17#include "pyistream.h"
694759cf 18
af309447
RD
19#ifdef __WXMSW__
20#include <wx/msw/private.h>
78b57918 21#include <wx/msw/winundef.h>
4268f798 22#include <wx/msw/msvcrt.h>
af309447 23#endif
694759cf
RD
24
25#ifdef __WXGTK__
26#include <gtk/gtk.h>
54b96882
RD
27#include <gdk/gdkprivate.h>
28#include <wx/gtk/win_gtk.h>
694759cf 29#endif
7bf85405 30
c8bc7bb8
RD
31//----------------------------------------------------------------------
32
33#if PYTHON_API_VERSION <= 1007 && wxUSE_UNICODE
34#error Python must support Unicode to use wxWindows Unicode
35#endif
36
4268f798
RD
37//----------------------------------------------------------------------
38
1893b029
RD
39#ifdef __WXGTK__
40int WXDLLEXPORT wxEntryStart( int& argc, char** argv );
41#else
4268f798 42int WXDLLEXPORT wxEntryStart( int argc, char** argv );
1893b029 43#endif
4268f798
RD
44int WXDLLEXPORT wxEntryInitGui();
45void WXDLLEXPORT wxEntryCleanup();
46
47wxPyApp* wxPythonApp = NULL; // Global instance of application object
e3dbf593 48bool wxPyDoCleanup = FALSE;
4268f798
RD
49
50
51#ifdef WXP_WITH_THREAD
52struct wxPyThreadState {
53 unsigned long tid;
54 PyThreadState* tstate;
55
56 wxPyThreadState(unsigned long _tid=0, PyThreadState* _tstate=NULL)
57 : tid(_tid), tstate(_tstate) {}
58};
59
60#include <wx/dynarray.h>
61WX_DECLARE_OBJARRAY(wxPyThreadState, wxPyThreadStateArray);
62#include <wx/arrimpl.cpp>
63WX_DEFINE_OBJARRAY(wxPyThreadStateArray);
64
65wxPyThreadStateArray* wxPyTStates = NULL;
66wxMutex* wxPyTMutex = NULL;
67#endif
7bf85405
RD
68
69
21f4bf45 70#ifdef __WXMSW__ // If building for win32...
21f4bf45
RD
71//----------------------------------------------------------------------
72// This gets run when the DLL is loaded. We just need to save a handle.
73//----------------------------------------------------------------------
9c039d08 74
21f4bf45
RD
75BOOL WINAPI DllMain(
76 HINSTANCE hinstDLL, // handle to DLL module
77 DWORD fdwReason, // reason for calling function
78 LPVOID lpvReserved // reserved
79 )
80{
a2426843
RD
81 // If wxPython is embedded in another wxWindows app then
82 // the inatance has already been set.
83 if (! wxGetInstance())
84 wxSetInstance(hinstDLL);
85 return TRUE;
21f4bf45
RD
86}
87#endif
88
7bf85405 89//----------------------------------------------------------------------
4268f798 90// Classes for implementing the wxp main application shell.
7bf85405
RD
91//----------------------------------------------------------------------
92
7bf85405 93
cf694132 94wxPyApp::wxPyApp() {
bc5f2236 95 SetUseBestVisual(TRUE);
cf694132
RD
96}
97
98wxPyApp::~wxPyApp() {
cf694132
RD
99}
100
101
7bf85405 102// This one isn't acutally called... See __wxStart()
4268f798 103bool wxPyApp::OnInit() {
6999b0d8 104 return FALSE;
7bf85405
RD
105}
106
4268f798
RD
107
108int wxPyApp::MainLoop() {
7ff49f0c 109 int retval = 0;
af309447 110
7ff49f0c 111 DeletePendingObjects();
4268f798 112 bool initialized = wxTopLevelWindows.GetCount() != 0;
7ff49f0c 113#ifdef __WXGTK__
4268f798 114 m_initialized = initialized;
7ff49f0c 115#endif
7bf85405 116
4268f798 117 if (initialized) {
7ff49f0c 118 retval = wxApp::MainLoop();
4268f798 119 OnExit();
7ff49f0c
RD
120 }
121 return retval;
122}
7bf85405
RD
123
124
4268f798 125
fb5e0af0 126//---------------------------------------------------------------------
7bf85405
RD
127//----------------------------------------------------------------------
128
a541c325
RD
129
130static char* wxPyCopyCString(const wxChar* src)
131{
132 wxWX2MBbuf buff = (wxWX2MBbuf)wxConvCurrent->cWX2MB(src);
133 size_t len = strlen(buff);
134 char* dest = new char[len+1];
135 strcpy(dest, buff);
136 return dest;
137}
138
c8bc7bb8 139#if wxUSE_UNICODE
a541c325 140static char* wxPyCopyCString(const char* src) // we need a char version too
c8bc7bb8 141{
a541c325
RD
142 size_t len = strlen(src);
143 char* dest = new char[len+1];
144 strcpy(dest, src);
145 return dest;
c8bc7bb8 146}
a541c325 147#endif
c8bc7bb8 148
a541c325 149static wxChar* wxPyCopyWString(const char *src)
c8bc7bb8 150{
a541c325
RD
151 //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
152 wxString str(src, *wxConvCurrent);
153 return copystring(str);
c8bc7bb8
RD
154}
155
a541c325
RD
156#if wxUSE_UNICODE
157static wxChar* wxPyCopyWString(const wxChar *src)
c8bc7bb8 158{
a541c325 159 return copystring(src);
c8bc7bb8
RD
160}
161#endif
f6bcfd97 162
a541c325
RD
163
164//----------------------------------------------------------------------
165
0d6f9504 166// This is where we pick up the first part of the wxEntry functionality...
f6bcfd97 167// The rest is in __wxStart and __wxCleanup. This function is called when
8bf5d46e 168// wxcmodule is imported. (Before there is a wxApp object.)
0d6f9504 169void __wxPreStart()
7bf85405 170{
de20db99
RD
171
172#ifdef __WXMSW__
44f68505 173// wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
de20db99
RD
174#endif
175
9d8bd15f 176#ifdef WXP_WITH_THREAD
1112b0c6 177 PyEval_InitThreads();
4268f798
RD
178 wxPyTStates = new wxPyThreadStateArray;
179 wxPyTMutex = new wxMutex;
9d8bd15f
RD
180#endif
181
0b85cc38
RD
182 wxApp::CheckBuildOptions(wxBuildOptions());
183
a2426843 184 // Bail out if there is already a wxApp created. This means that the
0d6f9504 185 // toolkit has already been initialized, as in embedding wxPython in
a2426843
RD
186 // a C++ wxWindows app, so we don't need to call wxEntryStart.
187 if (wxTheApp != NULL) {
0d6f9504 188 return;
a2426843 189 }
e3dbf593 190 wxPyDoCleanup = TRUE;
7ff49f0c 191
9416aa89
RD
192 int argc = 0;
193 char** argv = NULL;
7ff49f0c 194 PyObject* sysargv = PySys_GetObject("argv");
9416aa89
RD
195 if (sysargv != NULL) {
196 argc = PyList_Size(sysargv);
197 argv = new char*[argc+1];
198 int x;
c8bc7bb8
RD
199 for(x=0; x<argc; x++) {
200 PyObject *item = PyList_GetItem(sysargv, x);
201#if wxUSE_UNICODE
202 if (PyUnicode_Check(item))
a541c325
RD
203 argv[x] = wxPyCopyCString(PyUnicode_AS_UNICODE(item));
204 else
c8bc7bb8 205#endif
a541c325 206 argv[x] = wxPyCopyCString(PyString_AsString(item));
c8bc7bb8 207 }
9416aa89
RD
208 argv[argc] = NULL;
209 }
7bf85405 210
7ff49f0c 211 wxEntryStart(argc, argv);
f6bcfd97 212 delete [] argv;
0d6f9504
RD
213}
214
215
7ff49f0c 216
0d6f9504
RD
217// Start the user application, user App's OnInit method is a parameter here
218PyObject* __wxStart(PyObject* /* self */, PyObject* args)
219{
220 PyObject* onInitFunc = NULL;
221 PyObject* arglist;
222 PyObject* result;
223 long bResult;
224
0d6f9504
RD
225 if (!PyArg_ParseTuple(args, "O", &onInitFunc))
226 return NULL;
227
0d6f9504 228 // This is the next part of the wxEntry functionality...
9416aa89 229 int argc = 0;
c8bc7bb8 230 wxChar** argv = NULL;
f6bcfd97 231 PyObject* sysargv = PySys_GetObject("argv");
9416aa89
RD
232 if (sysargv != NULL) {
233 argc = PyList_Size(sysargv);
c8bc7bb8 234 argv = new wxChar*[argc+1];
9416aa89 235 int x;
c8bc7bb8
RD
236 for(x=0; x<argc; x++) {
237 PyObject *pyArg = PyList_GetItem(sysargv, x);
238#if wxUSE_UNICODE
a541c325
RD
239 if (PyUnicode_Check(pyArg))
240 argv[x] = wxPyCopyWString(PyUnicode_AS_UNICODE(pyArg));
241 else
c8bc7bb8 242#endif
a541c325 243 argv[x] = wxPyCopyWString(PyString_AsString(pyArg));
c8bc7bb8 244 }
9416aa89
RD
245 argv[argc] = NULL;
246 }
c8bc7bb8 247
f6bcfd97
BP
248 wxPythonApp->argc = argc;
249 wxPythonApp->argv = argv;
0d6f9504 250
7ff49f0c 251 wxEntryInitGui();
7bf85405
RD
252
253 // Call the Python App's OnInit function
254 arglist = PyTuple_New(0);
255 result = PyEval_CallObject(onInitFunc, arglist);
8bf5d46e
RD
256 if (!result) { // an exception was raised.
257 return NULL;
7bf85405
RD
258 }
259
260 if (! PyInt_Check(result)) {
261 PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value");
262 return NULL;
263 }
264 bResult = PyInt_AS_LONG(result);
265 if (! bResult) {
194fa2ac 266 PyErr_SetString(PyExc_SystemExit, "OnInit returned FALSE, exiting...");
7bf85405
RD
267 return NULL;
268 }
269
13dfc243 270#ifdef __WXGTK__
7ff49f0c 271 wxTheApp->m_initialized = (wxTopLevelWindows.GetCount() > 0);
13dfc243 272#endif
fb5e0af0 273
7bf85405
RD
274 Py_INCREF(Py_None);
275 return Py_None;
276}
277
4268f798 278
7ff49f0c 279void __wxCleanup() {
e3dbf593
RD
280 if (wxPyDoCleanup)
281 wxEntryCleanup();
7faa9591 282#ifdef WXP_WITH_THREAD
4268f798
RD
283 delete wxPyTMutex;
284 wxPyTMutex = NULL;
285 wxPyTStates->Empty();
286 delete wxPyTStates;
287 wxPyTStates = NULL;
7faa9591 288#endif
7ff49f0c 289}
7bf85405
RD
290
291
292
9416aa89
RD
293static PyObject* wxPython_dict = NULL;
294static PyObject* wxPyPtrTypeMap = NULL;
295
4acff284 296
7bf85405
RD
297PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args)
298{
299
300 if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
301 return NULL;
302
303 if (!PyDict_Check(wxPython_dict)) {
304 PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!");
305 return NULL;
306 }
9416aa89
RD
307
308 if (! wxPyPtrTypeMap)
309 wxPyPtrTypeMap = PyDict_New();
310 PyDict_SetItemString(wxPython_dict, "__wxPyPtrTypeMap", wxPyPtrTypeMap);
311
312
7bf85405 313#ifdef __WXMOTIF__
21f4bf45
RD
314#define wxPlatform "__WXMOTIF__"
315#endif
be43cc44
RD
316#ifdef __WXX11__
317#define wxPlatform "__WXX11__"
7bf85405
RD
318#endif
319#ifdef __WXGTK__
21f4bf45 320#define wxPlatform "__WXGTK__"
7bf85405
RD
321#endif
322#if defined(__WIN32__) || defined(__WXMSW__)
fb5e0af0 323#define wxPlatform "__WXMSW__"
7bf85405
RD
324#endif
325#ifdef __WXMAC__
21f4bf45 326#define wxPlatform "__WXMAC__"
7bf85405
RD
327#endif
328
249a57f4
RD
329#ifdef __WXDEBUG__
330 int wxdebug = 1;
331#else
332 int wxdebug = 0;
333#endif
334
7bf85405 335 PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform));
c8bc7bb8 336 PyDict_SetItemString(wxPython_dict, "wxUSE_UNICODE", PyInt_FromLong(wxUSE_UNICODE));
249a57f4 337 PyDict_SetItemString(wxPython_dict, "__WXDEBUG__", PyInt_FromLong(wxdebug));
c8bc7bb8 338
7bf85405
RD
339 Py_INCREF(Py_None);
340 return Py_None;
341}
342
4acff284
RD
343//---------------------------------------------------------------------------
344
345void wxPyClientData_dtor(wxPyClientData* self) {
346 wxPyBeginBlockThreads();
347 Py_DECREF(self->m_obj);
348 wxPyEndBlockThreads();
349}
350
351void wxPyUserData_dtor(wxPyUserData* self) {
352 wxPyBeginBlockThreads();
353 Py_DECREF(self->m_obj);
354 wxPyEndBlockThreads();
355}
356
357
358// This is called when an OOR controled object is being destroyed. Although
359// the C++ object is going away there is no way to force the Python object
360// (and all references to it) to die too. This causes problems (crashes) in
361// wxPython when a python shadow object attempts to call a C++ method using
362// the now bogus pointer... So to try and prevent this we'll do a little black
363// magic and change the class of the python instance to a class that will
364// raise an exception for any attempt to call methods with it. See
365// _wxPyDeadObject in _extras.py for the implementation of this class.
366void wxPyOORClientData_dtor(wxPyOORClientData* self) {
367
368 static PyObject* deadObjectClass = NULL;
369
370 wxPyBeginBlockThreads();
371 if (deadObjectClass == NULL) {
372 deadObjectClass = PyDict_GetItemString(wxPython_dict, "_wxPyDeadObject");
373 wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!"));
374 Py_INCREF(deadObjectClass);
375 }
376
377 // Clear the instance's dictionary, put the name of the old class into the
378 // instance, and then reset the class to be the dead class.
379 if (self->m_obj->ob_refcnt > 1) { // but only if there is more than one reference
380 wxASSERT_MSG(PyInstance_Check(self->m_obj), wxT("m_obj not an instance!?!?!"));
381 PyInstanceObject* inst = (PyInstanceObject*)self->m_obj;
382 PyDict_Clear(inst->in_dict);
383 PyDict_SetItemString(inst->in_dict, "_name", inst->in_class->cl_name);
384 inst->in_class = (PyClassObject*)deadObjectClass;
385 Py_INCREF(deadObjectClass);
386 }
387 wxPyEndBlockThreads();
388}
7bf85405 389
9416aa89
RD
390//---------------------------------------------------------------------------
391// Stuff used by OOR to find the right wxPython class type to return and to
392// build it.
393
394
395// The pointer type map is used when the "pointer" type name generated by SWIG
396// is not the same as the shadow class name, for example wxPyTreeCtrl
397// vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
398// so we'll just make it a Python dictionary in the wx module's namespace.
4acff284 399// (See __wxSetDictionary)
9416aa89
RD
400void wxPyPtrTypeMap_Add(const char* commonName, const char* ptrName) {
401 if (! wxPyPtrTypeMap)
402 wxPyPtrTypeMap = PyDict_New();
9416aa89
RD
403 PyDict_SetItemString(wxPyPtrTypeMap,
404 (char*)commonName,
405 PyString_FromString((char*)ptrName));
406}
407
408
409
a541c325 410PyObject* wxPyClassExists(const wxString& className) {
9416aa89
RD
411
412 if (!className)
413 return NULL;
414
415 char buff[64]; // should always be big enough...
416
a541c325 417 sprintf(buff, "%sPtr", className.mbc_str());
9416aa89
RD
418 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
419
420 return classobj; // returns NULL if not found
421}
422
423
2f4e9287 424PyObject* wxPyMake_wxObject(wxObject* source, bool checkEvtHandler) {
0122b7e3
RD
425 PyObject* target = NULL;
426 bool isEvtHandler = FALSE;
9416aa89
RD
427
428 if (source) {
2aab8f16 429 // If it's derived from wxEvtHandler then there may
2f4e9287
RD
430 // already be a pointer to a Python object that we can use
431 // in the OOR data.
432 if (checkEvtHandler && wxIsKindOf(source, wxEvtHandler)) {
433 isEvtHandler = TRUE;
0122b7e3 434 wxEvtHandler* eh = (wxEvtHandler*)source;
4acff284 435 wxPyOORClientData* data = (wxPyOORClientData*)eh->GetClientObject();
0122b7e3
RD
436 if (data) {
437 target = data->m_obj;
438 Py_INCREF(target);
439 }
9416aa89 440 }
0122b7e3
RD
441
442 if (! target) {
2aab8f16
RD
443 // Otherwise make it the old fashioned way by making a
444 // new shadow object and putting this pointer in it.
0122b7e3
RD
445 wxClassInfo* info = source->GetClassInfo();
446 wxChar* name = (wxChar*)info->GetClassName();
447 PyObject* klass = wxPyClassExists(name);
448 while (info && !klass) {
449 name = (wxChar*)info->GetBaseClassName1();
450 info = wxClassInfo::FindClass(name);
451 klass = wxPyClassExists(name);
452 }
453 if (info) {
454 target = wxPyConstructObject(source, name, klass, FALSE);
455 if (target && isEvtHandler)
4acff284 456 ((wxEvtHandler*)source)->SetClientObject(new wxPyOORClientData(target));
0122b7e3 457 } else {
4acff284 458 wxString msg(wxT("wxPython class not found for "));
0122b7e3 459 msg += source->GetClassInfo()->GetClassName();
a541c325 460 PyErr_SetString(PyExc_NameError, msg.mbc_str());
0122b7e3
RD
461 target = NULL;
462 }
9416aa89
RD
463 }
464 } else { // source was NULL so return None.
465 Py_INCREF(Py_None); target = Py_None;
466 }
467 return target;
468}
469
0122b7e3 470
2f4e9287
RD
471PyObject* wxPyMake_wxSizer(wxSizer* source) {
472 PyObject* target = NULL;
473
474 if (source && wxIsKindOf(source, wxSizer)) {
475 // If it's derived from wxSizer then there may
476 // already be a pointer to a Python object that we can use
477 // in the OOR data.
478 wxSizer* sz = (wxSizer*)source;
4acff284 479 wxPyOORClientData* data = (wxPyOORClientData*)sz->GetClientObject();
2f4e9287
RD
480 if (data) {
481 target = data->m_obj;
482 Py_INCREF(target);
483 }
484 }
485 if (! target) {
486 target = wxPyMake_wxObject(source, FALSE);
487 if (target != Py_None)
4acff284 488 ((wxSizer*)source)->SetClientObject(new wxPyOORClientData(target));
2f4e9287
RD
489 }
490 return target;
491}
492
493
494
7bf85405
RD
495//---------------------------------------------------------------------------
496
c368d904 497PyObject* wxPyConstructObject(void* ptr,
a541c325 498 const wxString& className,
9416aa89 499 PyObject* klass,
1e7ecb7b 500 int setThisOwn) {
9416aa89 501
33510773
RD
502 PyObject* obj;
503 PyObject* arg;
9416aa89 504 PyObject* item;
a541c325 505 wxString name(className);
9416aa89
RD
506 char swigptr[64]; // should always be big enough...
507 char buff[64];
508
a541c325
RD
509 if ((item = PyDict_GetItemString(wxPyPtrTypeMap, (char*)(const char*)name.mbc_str())) != NULL) {
510 name = wxString(PyString_AsString(item), *wxConvCurrent);
9416aa89 511 }
a541c325 512 sprintf(buff, "_%s_p", (const char*)name.mbc_str());
9416aa89
RD
513 SWIG_MakePtr(swigptr, ptr, buff);
514
515 arg = Py_BuildValue("(s)", swigptr);
516 obj = PyInstance_New(klass, arg, NULL);
517 Py_DECREF(arg);
518
519 if (setThisOwn) {
520 PyObject* one = PyInt_FromLong(1);
521 PyObject_SetAttrString(obj, "thisown", one);
522 Py_DECREF(one);
523 }
524
525 return obj;
526}
527
528
529PyObject* wxPyConstructObject(void* ptr,
a541c325 530 const wxString& className,
9416aa89 531 int setThisOwn) {
33510773
RD
532 if (!ptr) {
533 Py_INCREF(Py_None);
534 return Py_None;
535 }
536
9c039d08 537 char buff[64]; // should always be big enough...
a541c325 538 sprintf(buff, "%sPtr", (const char*)className.mbc_str());
c5943253 539
a541c325 540 wxASSERT_MSG(wxPython_dict, wxT("wxPython_dict is not set yet!!"));
c5943253 541
7bf85405
RD
542 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
543 if (! classobj) {
e3dbf593
RD
544 wxString msg(wxT("wxPython class not found for "));
545 msg += className;
546 PyErr_SetString(PyExc_NameError, msg.mbc_str());
547 return NULL;
7bf85405
RD
548 }
549
9416aa89 550 return wxPyConstructObject(ptr, className, classobj, setThisOwn);
7bf85405
RD
551}
552
a541c325 553
d559219f 554//---------------------------------------------------------------------------
7bf85405 555
4958ea8f 556
cf694132 557#ifdef WXP_WITH_THREAD
4268f798
RD
558inline
559unsigned long wxPyGetCurrentThreadId() {
4958ea8f 560 return wxThread::GetCurrentId();
4268f798
RD
561}
562
be43cc44 563static PyThreadState* gs_shutdownTState;
4268f798
RD
564static
565PyThreadState* wxPyGetThreadState() {
be43cc44
RD
566 if (wxPyTMutex == NULL) // Python is shutting down...
567 return gs_shutdownTState;
568
4268f798
RD
569 unsigned long ctid = wxPyGetCurrentThreadId();
570 PyThreadState* tstate = NULL;
571
572 wxPyTMutex->Lock();
573 for(size_t i=0; i < wxPyTStates->GetCount(); i++) {
574 wxPyThreadState& info = wxPyTStates->Item(i);
575 if (info.tid == ctid) {
576 tstate = info.tstate;
577 break;
578 }
579 }
580 wxPyTMutex->Unlock();
a541c325 581 wxASSERT_MSG(tstate, wxT("PyThreadState should not be NULL!"));
4268f798
RD
582 return tstate;
583}
584
585static
586void wxPySaveThreadState(PyThreadState* tstate) {
be43cc44
RD
587 if (wxPyTMutex == NULL) { // Python is shutting down, assume a single thread...
588 gs_shutdownTState = tstate;
589 return;
590 }
4268f798
RD
591 unsigned long ctid = wxPyGetCurrentThreadId();
592 wxPyTMutex->Lock();
593 for(size_t i=0; i < wxPyTStates->GetCount(); i++) {
594 wxPyThreadState& info = wxPyTStates->Item(i);
595 if (info.tid == ctid) {
763d71e4
RD
596#if 0
597 if (info.tstate != tstate)
598 wxLogMessage("*** tstate mismatch!???");
599#endif
46e10fde 600 // info.tstate = tstate; *** DO NOT update existing ones???
763d71e4
RD
601 // Normally it will never change, but apparently COM callbacks
602 // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient
46e10fde 603 // tstate which will then be garbage the next time we try to use
763d71e4 604 // it...
4268f798
RD
605 wxPyTMutex->Unlock();
606 return;
607 }
1112b0c6 608 }
4268f798
RD
609 // not found, so add it...
610 wxPyTStates->Add(new wxPyThreadState(ctid, tstate));
611 wxPyTMutex->Unlock();
612}
613
614#endif
615
616
617// Calls from Python to wxWindows code are wrapped in calls to these
618// functions:
619
620PyThreadState* wxPyBeginAllowThreads() {
621#ifdef WXP_WITH_THREAD
622 PyThreadState* saved = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
623 wxPySaveThreadState(saved);
624 return saved;
625#else
626 return NULL;
627#endif
628}
629
630void wxPyEndAllowThreads(PyThreadState* saved) {
631#ifdef WXP_WITH_THREAD
632 PyEval_RestoreThread(saved); // Py_END_ALLOW_THREADS;
cf694132 633#endif
d559219f 634}
cf694132 635
cf694132 636
4268f798
RD
637
638// Calls from wxWindows back to Python code, or even any PyObject
639// manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
640
641void wxPyBeginBlockThreads() {
cf694132 642#ifdef WXP_WITH_THREAD
4268f798
RD
643 PyThreadState* tstate = wxPyGetThreadState();
644 PyEval_RestoreThread(tstate);
645#endif
646}
647
648
649void wxPyEndBlockThreads() {
650#ifdef WXP_WITH_THREAD
4268f798 651 // Is there any need to save it again?
c4770edd
RD
652 // PyThreadState* tstate =
653 PyEval_SaveThread();
1112b0c6 654#endif
cf694132
RD
655}
656
19a97bd6 657
d559219f 658//---------------------------------------------------------------------------
f74ff5ef
RD
659// wxPyInputStream and wxPyCBInputStream methods
660
f74ff5ef
RD
661
662void wxPyInputStream::close() {
a541c325 663 /* do nothing for now */
f74ff5ef
RD
664}
665
666void wxPyInputStream::flush() {
a541c325 667 /* do nothing for now */
f74ff5ef
RD
668}
669
670bool wxPyInputStream::eof() {
671 if (m_wxis)
672 return m_wxis->Eof();
673 else
674 return TRUE;
675}
676
677wxPyInputStream::~wxPyInputStream() {
678 /* do nothing */
679}
680
a541c325
RD
681
682
683
684PyObject* wxPyInputStream::read(int size) {
685 PyObject* obj = NULL;
686 wxMemoryBuffer buf;
f74ff5ef
RD
687 const int BUFSIZE = 1024;
688
689 // check if we have a real wxInputStream to work with
690 if (!m_wxis) {
691 PyErr_SetString(PyExc_IOError, "no valid C-wxInputStream");
692 return NULL;
693 }
694
695 if (size < 0) {
f74ff5ef
RD
696 // read until EOF
697 while (! m_wxis->Eof()) {
a541c325
RD
698 m_wxis->Read(buf.GetAppendBuf(BUFSIZE), BUFSIZE);
699 buf.UngetAppendBuf(m_wxis->LastRead());
f74ff5ef
RD
700 }
701
702 } else { // Read only size number of characters
a541c325
RD
703 m_wxis->Read(buf.GetWriteBuf(size), size);
704 buf.UngetWriteBuf(m_wxis->LastRead());
705 }
f74ff5ef 706
a541c325
RD
707 // error check
708 if (m_wxis->LastError() == wxSTREAM_READ_ERROR) {
709 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
710 }
711 else {
712 // We use only strings for the streams, not unicode
713 obj = PyString_FromStringAndSize(buf, buf.GetDataLen());
f74ff5ef 714 }
a541c325 715 return obj;
f74ff5ef
RD
716}
717
718
a541c325
RD
719PyObject* wxPyInputStream::readline(int size) {
720 PyObject* obj = NULL;
721 wxMemoryBuffer buf;
722 int i;
723 char ch;
724
f74ff5ef
RD
725 // check if we have a real wxInputStream to work with
726 if (!m_wxis) {
727 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream");
728 return NULL;
729 }
730
f74ff5ef
RD
731 // read until \n or byte limit reached
732 for (i=ch=0; (ch != '\n') && (!m_wxis->Eof()) && ((size < 0) || (i < size)); i++) {
a541c325
RD
733 ch = m_wxis->GetC();
734 buf.AppendByte(ch);
f74ff5ef
RD
735 }
736
737 // errorcheck
738 if (m_wxis->LastError() == wxSTREAM_READ_ERROR) {
f74ff5ef 739 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
f74ff5ef 740 }
a541c325
RD
741 else {
742 // We use only strings for the streams, not unicode
743 obj = PyString_FromStringAndSize((char*)buf.GetData(), buf.GetDataLen());
744 }
745 return obj;
f74ff5ef
RD
746}
747
748
a541c325
RD
749PyObject* wxPyInputStream::readlines(int sizehint) {
750 PyObject* pylist;
751
f74ff5ef
RD
752 // check if we have a real wxInputStream to work with
753 if (!m_wxis) {
754 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
755 return NULL;
756 }
757
758 // init list
a541c325
RD
759 pylist = PyList_New(0);
760 if (!pylist) {
f74ff5ef
RD
761 PyErr_NoMemory();
762 return NULL;
763 }
764
765 // read sizehint bytes or until EOF
766 int i;
767 for (i=0; (!m_wxis->Eof()) && ((sizehint < 0) || (i < sizehint));) {
a541c325 768 PyObject* s = this->readline();
f74ff5ef 769 if (s == NULL) {
a541c325 770 Py_DECREF(pylist);
f74ff5ef
RD
771 return NULL;
772 }
a541c325
RD
773 PyList_Append(pylist, s);
774 i += PyString_Size(s);
f74ff5ef
RD
775 }
776
777 // error check
778 if (m_wxis->LastError() == wxSTREAM_READ_ERROR) {
a541c325 779 Py_DECREF(pylist);
f74ff5ef
RD
780 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
781 return NULL;
782 }
a541c325
RD
783
784 return pylist;
f74ff5ef
RD
785}
786
787
788void wxPyInputStream::seek(int offset, int whence) {
789 if (m_wxis)
790 m_wxis->SeekI(offset, wxSeekMode(whence));
791}
792
793int wxPyInputStream::tell(){
794 if (m_wxis)
795 return m_wxis->TellI();
796 else return 0;
797}
798
799
800
801
802wxPyCBInputStream::wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block)
803 : wxInputStream(), m_read(r), m_seek(s), m_tell(t), m_block(block)
804{}
805
806
807wxPyCBInputStream::~wxPyCBInputStream() {
808 if (m_block) wxPyBeginBlockThreads();
809 Py_XDECREF(m_read);
810 Py_XDECREF(m_seek);
811 Py_XDECREF(m_tell);
812 if (m_block) wxPyEndBlockThreads();
813}
814
815
816wxPyCBInputStream* wxPyCBInputStream::create(PyObject *py, bool block) {
817 if (block) wxPyBeginBlockThreads();
818
819 PyObject* read = getMethod(py, "read");
820 PyObject* seek = getMethod(py, "seek");
821 PyObject* tell = getMethod(py, "tell");
822
823 if (!read) {
824 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
825 Py_XDECREF(read);
826 Py_XDECREF(seek);
827 Py_XDECREF(tell);
828 if (block) wxPyEndBlockThreads();
829 return NULL;
830 }
831
832 if (block) wxPyEndBlockThreads();
833 return new wxPyCBInputStream(read, seek, tell, block);
834}
835
249a57f4
RD
836
837wxPyCBInputStream* wxPyCBInputStream_create(PyObject *py, bool block) {
838 return wxPyCBInputStream::create(py, block);
839}
840
f74ff5ef
RD
841PyObject* wxPyCBInputStream::getMethod(PyObject* py, char* name) {
842 if (!PyObject_HasAttrString(py, name))
843 return NULL;
844 PyObject* o = PyObject_GetAttrString(py, name);
845 if (!PyMethod_Check(o) && !PyCFunction_Check(o)) {
846 Py_DECREF(o);
847 return NULL;
848 }
849 return o;
850}
851
852
853size_t wxPyCBInputStream::GetSize() const {
854 wxPyCBInputStream* self = (wxPyCBInputStream*)this; // cast off const
855 if (m_seek && m_tell) {
856 off_t temp = self->OnSysTell();
857 off_t ret = self->OnSysSeek(0, wxFromEnd);
858 self->OnSysSeek(temp, wxFromStart);
859 return ret;
860 }
861 else
862 return 0;
863}
864
865
866size_t wxPyCBInputStream::OnSysRead(void *buffer, size_t bufsize) {
867 if (bufsize == 0)
868 return 0;
869
870 wxPyBeginBlockThreads();
871 PyObject* arglist = Py_BuildValue("(i)", bufsize);
872 PyObject* result = PyEval_CallObject(m_read, arglist);
873 Py_DECREF(arglist);
874
875 size_t o = 0;
a541c325 876 if ((result != NULL) && PyString_Check(result)) {
f74ff5ef
RD
877 o = PyString_Size(result);
878 if (o == 0)
879 m_lasterror = wxSTREAM_EOF;
880 if (o > bufsize)
881 o = bufsize;
a541c325 882 memcpy((char*)buffer, PyString_AsString(result), o); // strings only, not unicode...
f74ff5ef
RD
883 Py_DECREF(result);
884
885 }
886 else
887 m_lasterror = wxSTREAM_READ_ERROR;
888 wxPyEndBlockThreads();
889 m_lastcount = o;
890 return o;
891}
892
893size_t wxPyCBInputStream::OnSysWrite(const void *buffer, size_t bufsize) {
894 m_lasterror = wxSTREAM_WRITE_ERROR;
895 return 0;
896}
897
898off_t wxPyCBInputStream::OnSysSeek(off_t off, wxSeekMode mode) {
899 wxPyBeginBlockThreads();
900 PyObject* arglist = Py_BuildValue("(ii)", off, mode);
901 PyObject* result = PyEval_CallObject(m_seek, arglist);
902 Py_DECREF(arglist);
903 Py_XDECREF(result);
904 wxPyEndBlockThreads();
905 return OnSysTell();
906}
907
908off_t wxPyCBInputStream::OnSysTell() const {
909 wxPyBeginBlockThreads();
910 PyObject* arglist = Py_BuildValue("()");
911 PyObject* result = PyEval_CallObject(m_tell, arglist);
912 Py_DECREF(arglist);
913 off_t o = 0;
914 if (result != NULL) {
915 o = PyInt_AsLong(result);
916 Py_DECREF(result);
917 };
918 wxPyEndBlockThreads();
919 return o;
920}
921
922//----------------------------------------------------------------------
d559219f 923
2f90df85
RD
924IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject);
925
d559219f
RD
926wxPyCallback::wxPyCallback(PyObject* func) {
927 m_func = func;
928 Py_INCREF(m_func);
929}
930
2f90df85
RD
931wxPyCallback::wxPyCallback(const wxPyCallback& other) {
932 m_func = other.m_func;
933 Py_INCREF(m_func);
934}
935
d559219f 936wxPyCallback::~wxPyCallback() {
4268f798 937 wxPyBeginBlockThreads();
d559219f 938 Py_DECREF(m_func);
4268f798 939 wxPyEndBlockThreads();
d559219f
RD
940}
941
cf694132
RD
942
943
7bf85405
RD
944// This function is used for all events destined for Python event handlers.
945void wxPyCallback::EventThunker(wxEvent& event) {
946 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
947 PyObject* func = cb->m_func;
948 PyObject* result;
949 PyObject* arg;
950 PyObject* tuple;
951
cf694132 952
4268f798 953 wxPyBeginBlockThreads();
65dd82cb
RD
954 wxString className = event.GetClassInfo()->GetClassName();
955
956 if (className == "wxPyEvent")
957 arg = ((wxPyEvent*)&event)->GetSelf();
958 else if (className == "wxPyCommandEvent")
959 arg = ((wxPyCommandEvent*)&event)->GetSelf();
c8bc7bb8 960 else {
a541c325 961 arg = wxPyConstructObject((void*)&event, className);
c8bc7bb8 962 }
7bf85405
RD
963
964 tuple = PyTuple_New(1);
965 PyTuple_SET_ITEM(tuple, 0, arg);
966 result = PyEval_CallObject(func, tuple);
7bf85405
RD
967 Py_DECREF(tuple);
968 if (result) {
969 Py_DECREF(result);
ac346f50 970 PyErr_Clear(); // Just in case...
7bf85405
RD
971 } else {
972 PyErr_Print();
973 }
4268f798 974 wxPyEndBlockThreads();
7bf85405
RD
975}
976
977
d559219f 978//----------------------------------------------------------------------
7bf85405 979
2f90df85
RD
980wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) {
981 m_lastFound = NULL;
982 m_self = other.m_self;
f6bcfd97
BP
983 m_class = other.m_class;
984 if (m_self) {
2f90df85 985 Py_INCREF(m_self);
f6bcfd97
BP
986 Py_INCREF(m_class);
987 }
2f90df85
RD
988}
989
990
33510773 991void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) {
d559219f 992 m_self = self;
33510773 993 m_class = klass;
b7312675 994 m_incRef = incref;
f6bcfd97 995 if (incref) {
2f90df85 996 Py_INCREF(m_self);
f6bcfd97
BP
997 Py_INCREF(m_class);
998 }
d559219f 999}
8bf5d46e 1000
8bf5d46e 1001
78b57918
RD
1002#if PYTHON_API_VERSION >= 1011
1003
1004// Prior to Python 2.2 PyMethod_GetClass returned the class object
1005// in which the method was defined. Starting with 2.2 it returns
1006// "class that asked for the method" which seems totally bogus to me
4152e8b9 1007// but apprently it fixes some obscure problem waiting to happen in
78b57918
RD
1008// Python. Since the API was not documented Guido and the gang felt
1009// safe in changing it. Needless to say that totally screwed up the
1010// logic below in wxPyCallbackHelper::findCallback, hence this icky
4152e8b9 1011// code to find the class where the method is actually defined...
78b57918
RD
1012
1013static
1014PyObject* PyFindClassWithAttr(PyObject *klass, PyObject *name)
1015{
1016 int i, n;
1017
1018 if (PyType_Check(klass)) { // new style classes
1019 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
4a98c806 1020 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
78b57918
RD
1021 PyTypeObject* type = (PyTypeObject*)klass;
1022 PyObject *mro, *res, *base, *dict;
1023 /* Look in tp_dict of types in MRO */
1024 mro = type->tp_mro;
1025 assert(PyTuple_Check(mro));
1026 n = PyTuple_GET_SIZE(mro);
1027 for (i = 0; i < n; i++) {
1028 base = PyTuple_GET_ITEM(mro, i);
1029 if (PyClass_Check(base))
1030 dict = ((PyClassObject *)base)->cl_dict;
1031 else {
1032 assert(PyType_Check(base));
1033 dict = ((PyTypeObject *)base)->tp_dict;
1034 }
1035 assert(dict && PyDict_Check(dict));
1036 res = PyDict_GetItem(dict, name);
1037 if (res != NULL)
4a98c806 1038 return base;
78b57918
RD
1039 }
1040 return NULL;
1041 }
1042
1043 else if (PyClass_Check(klass)) { // old style classes
1044 // This code is borrowed/adapted from class_lookup in classobject.c
1045 PyClassObject* cp = (PyClassObject*)klass;
1046 PyObject *value = PyDict_GetItem(cp->cl_dict, name);
1047 if (value != NULL) {
1048 return (PyObject*)cp;
1049 }
1050 n = PyTuple_Size(cp->cl_bases);
1051 for (i = 0; i < n; i++) {
1052 PyObject* base = PyTuple_GetItem(cp->cl_bases, i);
1053 PyObject *v = PyFindClassWithAttr(base, name);
1054 if (v != NULL)
1055 return v;
1056 }
1057 return NULL;
1058 }
4152e8b9 1059 return NULL;
78b57918
RD
1060}
1061#endif
1062
1063
1064static
1065PyObject* PyMethod_GetDefiningClass(PyObject* method, const char* name)
1066{
1067 PyObject* mgc = PyMethod_GET_CLASS(method);
1068
1069#if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1070 return mgc;
1071#else // 2.2 and after, the hard way...
1072
1073 PyObject* nameo = PyString_FromString(name);
1074 PyObject* klass = PyFindClassWithAttr(mgc, nameo);
1075 Py_DECREF(nameo);
1076 return klass;
1077#endif
1078}
1079
1080
1081
de20db99 1082bool wxPyCallbackHelper::findCallback(const char* name) const {
f6bcfd97
BP
1083 wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const
1084 self->m_lastFound = NULL;
78b57918
RD
1085
1086 // If the object (m_self) has an attibute of the given name...
de20db99 1087 if (m_self && PyObject_HasAttrString(m_self, (char*)name)) {
78b57918 1088 PyObject *method, *klass;
de20db99 1089 method = PyObject_GetAttrString(m_self, (char*)name);
f6bcfd97 1090
78b57918
RD
1091 // ...and if that attribute is a method, and if that method's class is
1092 // not from a base class...
f6bcfd97 1093 if (PyMethod_Check(method) &&
78b57918
RD
1094 (klass = PyMethod_GetDefiningClass(method, (char*)name)) != NULL &&
1095 ((klass == m_class) || PyClass_IsSubclass(klass, m_class))) {
8bf5d46e 1096
78b57918 1097 // ...then we'll save a pointer to the method so callCallback can call it.
f6bcfd97
BP
1098 self->m_lastFound = method;
1099 }
de20db99
RD
1100 else {
1101 Py_DECREF(method);
1102 }
f6bcfd97 1103 }
d559219f
RD
1104 return m_lastFound != NULL;
1105}
8bf5d46e 1106
d559219f 1107
f6bcfd97 1108int wxPyCallbackHelper::callCallback(PyObject* argTuple) const {
d559219f
RD
1109 PyObject* result;
1110 int retval = FALSE;
1111
1112 result = callCallbackObj(argTuple);
1113 if (result) { // Assumes an integer return type...
1114 retval = PyInt_AsLong(result);
1115 Py_DECREF(result);
1116 PyErr_Clear(); // forget about it if it's not...
1117 }
1118 return retval;
1119}
1120
1121// Invoke the Python callable object, returning the raw PyObject return
1122// value. Caller should DECREF the return value and also call PyEval_SaveThread.
f6bcfd97 1123PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const {
de20db99 1124 PyObject* result;
d559219f 1125
de20db99
RD
1126 // Save a copy of the pointer in case the callback generates another
1127 // callback. In that case m_lastFound will have a different value when
1128 // it gets back here...
1129 PyObject* method = m_lastFound;
1130
1131 result = PyEval_CallObject(method, argTuple);
d559219f 1132 Py_DECREF(argTuple);
de20db99 1133 Py_DECREF(method);
d559219f
RD
1134 if (!result) {
1135 PyErr_Print();
1136 }
1137 return result;
1138}
714e6a9e 1139
7bf85405 1140
0122b7e3 1141void wxPyCBH_setCallbackInfo(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) {
1e7ecb7b
RD
1142 cbh.setSelf(self, klass, incref);
1143}
1144
1145bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) {
1146 return cbh.findCallback(name);
1147}
1148
1149int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
1150 return cbh.callCallback(argTuple);
1151}
1152
1153PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
1154 return cbh.callCallbackObj(argTuple);
1155}
1156
1157
1158void wxPyCBH_delete(wxPyCallbackHelper* cbh) {
1e7ecb7b 1159 if (cbh->m_incRef) {
4268f798 1160 wxPyBeginBlockThreads();
1e7ecb7b
RD
1161 Py_XDECREF(cbh->m_self);
1162 Py_XDECREF(cbh->m_class);
4268f798 1163 wxPyEndBlockThreads();
1e7ecb7b 1164 }
1e7ecb7b 1165}
d559219f 1166
65dd82cb
RD
1167//---------------------------------------------------------------------------
1168//---------------------------------------------------------------------------
2aab8f16 1169// These event classes can be derived from in Python and passed through the event
c368d904 1170// system without losing anything. They do this by keeping a reference to
65dd82cb
RD
1171// themselves and some special case handling in wxPyCallback::EventThunker.
1172
1173
e19b7164 1174wxPyEvtSelfRef::wxPyEvtSelfRef() {
65dd82cb
RD
1175 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1176 //Py_INCREF(m_self); // circular loops...
194fa2ac 1177 m_cloned = FALSE;
65dd82cb
RD
1178}
1179
e19b7164 1180wxPyEvtSelfRef::~wxPyEvtSelfRef() {
4268f798 1181 wxPyBeginBlockThreads();
65dd82cb
RD
1182 if (m_cloned)
1183 Py_DECREF(m_self);
4268f798 1184 wxPyEndBlockThreads();
65dd82cb
RD
1185}
1186
e19b7164 1187void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) {
4268f798 1188 wxPyBeginBlockThreads();
65dd82cb
RD
1189 if (m_cloned)
1190 Py_DECREF(m_self);
1191 m_self = self;
1192 if (clone) {
1193 Py_INCREF(m_self);
1194 m_cloned = TRUE;
1195 }
4268f798 1196 wxPyEndBlockThreads();
65dd82cb
RD
1197}
1198
e19b7164 1199PyObject* wxPyEvtSelfRef::GetSelf() const {
65dd82cb
RD
1200 Py_INCREF(m_self);
1201 return m_self;
1202}
1203
1204
07b2e1cd
RD
1205IMPLEMENT_ABSTRACT_CLASS(wxPyEvent, wxEvent);
1206IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent, wxCommandEvent);
1207
1208
65dd82cb
RD
1209wxPyEvent::wxPyEvent(int id)
1210 : wxEvent(id) {
1211}
1212
65dd82cb 1213
07b2e1cd
RD
1214wxPyEvent::wxPyEvent(const wxPyEvent& evt)
1215 : wxEvent(evt)
1216{
1217 SetSelf(evt.m_self, TRUE);
65dd82cb
RD
1218}
1219
1220
07b2e1cd
RD
1221wxPyEvent::~wxPyEvent() {
1222}
65dd82cb
RD
1223
1224
1225wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id)
1226 : wxCommandEvent(commandType, id) {
1227}
1228
65dd82cb 1229
07b2e1cd
RD
1230wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent& evt)
1231 : wxCommandEvent(evt)
1232{
1233 SetSelf(evt.m_self, TRUE);
65dd82cb
RD
1234}
1235
1236
07b2e1cd
RD
1237wxPyCommandEvent::~wxPyCommandEvent() {
1238}
1239
65dd82cb
RD
1240
1241
1242
d559219f 1243//---------------------------------------------------------------------------
7bf85405
RD
1244//---------------------------------------------------------------------------
1245
d559219f 1246
7bf85405
RD
1247wxPyTimer::wxPyTimer(PyObject* callback) {
1248 func = callback;
1249 Py_INCREF(func);
1250}
1251
1252wxPyTimer::~wxPyTimer() {
4268f798 1253 wxPyBeginBlockThreads();
7bf85405 1254 Py_DECREF(func);
4268f798 1255 wxPyEndBlockThreads();
7bf85405
RD
1256}
1257
1258void wxPyTimer::Notify() {
185d7c3e
RD
1259 if (!func || func == Py_None) {
1260 wxTimer::Notify();
1261 }
1262 else {
4268f798 1263 wxPyBeginBlockThreads();
185d7c3e
RD
1264
1265 PyObject* result;
1266 PyObject* args = Py_BuildValue("()");
1267
1268 result = PyEval_CallObject(func, args);
1269 Py_DECREF(args);
1270 if (result) {
1271 Py_DECREF(result);
1272 PyErr_Clear();
1273 } else {
1274 PyErr_Print();
1275 }
7bf85405 1276
4268f798 1277 wxPyEndBlockThreads();
7bf85405
RD
1278 }
1279}
1280
1281
cf694132 1282
2f90df85 1283//---------------------------------------------------------------------------
389c5527
RD
1284//---------------------------------------------------------------------------
1285// Convert a wxList to a Python List
1286
65dd82cb 1287PyObject* wxPy_ConvertList(wxListBase* list, const char* className) {
389c5527
RD
1288 PyObject* pyList;
1289 PyObject* pyObj;
1290 wxObject* wxObj;
1291 wxNode* node = list->First();
1292
4268f798 1293 wxPyBeginBlockThreads();
389c5527
RD
1294 pyList = PyList_New(0);
1295 while (node) {
1296 wxObj = node->Data();
9416aa89 1297 pyObj = wxPyMake_wxObject(wxObj); //wxPyConstructObject(wxObj, className);
389c5527
RD
1298 PyList_Append(pyList, pyObj);
1299 node = node->Next();
1300 }
4268f798 1301 wxPyEndBlockThreads();
389c5527
RD
1302 return pyList;
1303}
1304
54b96882
RD
1305//----------------------------------------------------------------------
1306
1307long wxPyGetWinHandle(wxWindow* win) {
1308#ifdef __WXMSW__
1309 return (long)win->GetHandle();
1310#endif
1311
1312 // Find and return the actual X-Window.
1313#ifdef __WXGTK__
1314 if (win->m_wxwindow) {
1315 GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window;
1316 if (bwin) {
1317 return (long)bwin->xwindow;
1318 }
1319 }
1320#endif
1321 return 0;
1322}
1323
7bf85405
RD
1324//----------------------------------------------------------------------
1325// Some helper functions for typemaps in my_typemaps.i, so they won't be
c8bc7bb8
RD
1326// included in every file over and over again...
1327
1328#if PYTHON_API_VERSION >= 1009
1329 static char* wxStringErrorMsg = "String or Unicode type required";
1330#else
1331 static char* wxStringErrorMsg = "String type required";
1332#endif
1333
1334
1335wxString* wxString_in_helper(PyObject* source) {
1336 wxString* target;
1337#if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1338 if (!PyString_Check(source) && !PyUnicode_Check(source)) {
1339 PyErr_SetString(PyExc_TypeError, wxStringErrorMsg);
1340 return NULL;
1341 }
1342#if wxUSE_UNICODE
1343 if (PyUnicode_Check(source)) {
1344 target = new wxString(PyUnicode_AS_UNICODE(source));
1345 } else {
a541c325
RD
1346 // It is a string, get pointers to it and transform to unicode
1347 char* tmpPtr; int tmpSize;
1348 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1349 target = new wxString(tmpPtr, *wxConvCurrent, tmpSize);
c8bc7bb8
RD
1350 }
1351#else
1352 char* tmpPtr; int tmpSize;
1353 if (PyString_AsStringAndSize(source, &tmpPtr, &tmpSize) == -1) {
1354 PyErr_SetString(PyExc_TypeError, "Unable to convert string");
1355 return NULL;
1356 }
1357 target = new wxString(tmpPtr, tmpSize);
1358#endif // wxUSE_UNICODE
1359
1360#else // No Python unicode API (1.5.2)
1361 if (!PyString_Check(source)) {
1362 PyErr_SetString(PyExc_TypeError, wxStringErrorMsg);
1363 return NULL;
1364 }
1365 target = new wxString(PyString_AS_STRING(source), PyString_GET_SIZE(source));
1366#endif
1367 return target;
1368}
1369
7bf85405 1370
a541c325
RD
1371// Similar to above except doesn't use "new" and doesn't set an exception
1372wxString Py2wxString(PyObject* source)
1373{
1374 wxString target;
1375 bool doDecRef = FALSE;
1376
1377#if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1378 if (!PyString_Check(source) && !PyUnicode_Check(source)) {
1379 // Convert to String if not one already... (TODO: Unicode too?)
1380 source = PyObject_Str(source);
1381 doDecRef = TRUE;
1382 }
1383
1384#if wxUSE_UNICODE
1385 if (PyUnicode_Check(source)) {
1386 target = PyUnicode_AS_UNICODE(source);
1387 } else {
1388 // It is a string, get pointers to it and transform to unicode
1389 char* tmpPtr; int tmpSize;
1390 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1391 target = wxString(tmpPtr, *wxConvCurrent, tmpSize);
1392 }
1393#else
1394 char* tmpPtr; int tmpSize;
1395 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1396 target = wxString(tmpPtr, tmpSize);
1397#endif // wxUSE_UNICODE
1398
1399#else // No Python unicode API (1.5.2)
1400 if (!PyString_Check(source)) {
1401 // Convert to String if not one already...
1402 source = PyObject_Str(source);
1403 doDecRef = TRUE;
1404 }
1405 target = wxString(PyString_AS_STRING(source), PyString_GET_SIZE(source));
1406#endif
1407
1408 if (doDecRef)
1409 Py_DECREF(source);
1410 return target;
1411}
1412
1413
1414// Make either a Python String or Unicode object, depending on build mode
1415PyObject* wx2PyString(const wxString& src)
1416{
1417 PyObject* str;
1418#if wxUSE_UNICODE
1419 str = PyUnicode_FromUnicode(src.c_str(), src.Len());
1420#else
1421 str = PyString_FromStringAndSize(src.c_str(), src.Len());
1422#endif
1423 return str;
1424}
1425
1426
1427//----------------------------------------------------------------------
1428
7bf85405 1429
2f90df85 1430byte* byte_LIST_helper(PyObject* source) {
b639c3c5
RD
1431 if (!PyList_Check(source)) {
1432 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1433 return NULL;
1434 }
1435 int count = PyList_Size(source);
1436 byte* temp = new byte[count];
1437 if (! temp) {
1438 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1439 return NULL;
1440 }
1441 for (int x=0; x<count; x++) {
1442 PyObject* o = PyList_GetItem(source, x);
1443 if (! PyInt_Check(o)) {
1444 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1445 return NULL;
1446 }
1447 temp[x] = (byte)PyInt_AsLong(o);
1448 }
1449 return temp;
1450}
1451
1452
2f90df85 1453int* int_LIST_helper(PyObject* source) {
7bf85405
RD
1454 if (!PyList_Check(source)) {
1455 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1456 return NULL;
1457 }
1458 int count = PyList_Size(source);
1459 int* temp = new int[count];
1460 if (! temp) {
1461 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1462 return NULL;
1463 }
1464 for (int x=0; x<count; x++) {
1465 PyObject* o = PyList_GetItem(source, x);
1466 if (! PyInt_Check(o)) {
1467 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1468 return NULL;
1469 }
1470 temp[x] = PyInt_AsLong(o);
1471 }
1472 return temp;
1473}
1474
1475
2f90df85 1476long* long_LIST_helper(PyObject* source) {
7bf85405
RD
1477 if (!PyList_Check(source)) {
1478 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1479 return NULL;
1480 }
1481 int count = PyList_Size(source);
1482 long* temp = new long[count];
1483 if (! temp) {
1484 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1485 return NULL;
1486 }
1487 for (int x=0; x<count; x++) {
1488 PyObject* o = PyList_GetItem(source, x);
1489 if (! PyInt_Check(o)) {
1490 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1491 return NULL;
1492 }
1493 temp[x] = PyInt_AsLong(o);
1494 }
1495 return temp;
1496}
1497
1498
2f90df85 1499char** string_LIST_helper(PyObject* source) {
7bf85405
RD
1500 if (!PyList_Check(source)) {
1501 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1502 return NULL;
1503 }
1504 int count = PyList_Size(source);
1505 char** temp = new char*[count];
1506 if (! temp) {
1507 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1508 return NULL;
1509 }
1510 for (int x=0; x<count; x++) {
1511 PyObject* o = PyList_GetItem(source, x);
1512 if (! PyString_Check(o)) {
1513 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
1514 return NULL;
1515 }
1516 temp[x] = PyString_AsString(o);
1517 }
1518 return temp;
1519}
1520
e0672e2f
RD
1521//--------------------------------
1522// Part of patch from Tim Hochberg
1523static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) {
1524 if (PyInt_Check(o1) && PyInt_Check(o2)) {
1525 point->x = PyInt_AS_LONG(o1);
1526 point->y = PyInt_AS_LONG(o2);
1527 return true;
1528 }
1529 if (PyFloat_Check(o1) && PyFloat_Check(o2)) {
1530 point->x = (int)PyFloat_AS_DOUBLE(o1);
1531 point->y = (int)PyFloat_AS_DOUBLE(o2);
1532 return true;
1533 }
1534 if (PyInstance_Check(o1) || PyInstance_Check(o2)) {
1535 // Disallow instances because they can cause havok
1536 return false;
1537 }
1538 if (PyNumber_Check(o1) && PyNumber_Check(o2)) {
1539 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1540 point->x = PyInt_AsLong(o1);
1541 point->y = PyInt_AsLong(o2);
1542 return true;
1543 }
1544 return false;
1545}
7bf85405
RD
1546
1547
e0672e2f
RD
1548wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) {
1549 // Putting all of the declarations here allows
1550 // us to put the error handling all in one place.
1551 int x;
1552 wxPoint* temp;
1553 PyObject *o, *o1, *o2;
9d37f964 1554 bool isFast = PyList_Check(source) || PyTuple_Check(source);
e0672e2f 1555
e0672e2f
RD
1556 if (!PySequence_Check(source)) {
1557 goto error0;
7bf85405 1558 }
9d37f964
RD
1559
1560 // The length of the sequence is returned in count.
e0672e2f
RD
1561 *count = PySequence_Length(source);
1562 if (*count < 0) {
1563 goto error0;
1564 }
1565
1566 temp = new wxPoint[*count];
1567 if (!temp) {
7bf85405
RD
1568 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1569 return NULL;
1570 }
e0672e2f
RD
1571 for (x=0; x<*count; x++) {
1572 // Get an item: try fast way first.
1573 if (isFast) {
1574 o = PySequence_Fast_GET_ITEM(source, x);
1575 }
1576 else {
1577 o = PySequence_GetItem(source, x);
1578 if (o == NULL) {
1579 goto error1;
1580 }
1581 }
7bf85405 1582
e0672e2f
RD
1583 // Convert o to wxPoint.
1584 if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) ||
1585 (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) {
1586 o1 = PySequence_Fast_GET_ITEM(o, 0);
1587 o2 = PySequence_Fast_GET_ITEM(o, 1);
1588 if (!wxPointFromObjects(o1, o2, &temp[x])) {
1589 goto error2;
1590 }
7bf85405 1591 }
d559219f
RD
1592 else if (PyInstance_Check(o)) {
1593 wxPoint* pt;
e0672e2f
RD
1594 if (SWIG_GetPtrObj(o, (void **)&pt, "_wxPoint_p")) {
1595 goto error2;
d559219f
RD
1596 }
1597 temp[x] = *pt;
1598 }
e0672e2f
RD
1599 else if (PySequence_Check(o) && PySequence_Length(o) == 2) {
1600 o1 = PySequence_GetItem(o, 0);
1601 o2 = PySequence_GetItem(o, 1);
1602 if (!wxPointFromObjects(o1, o2, &temp[x])) {
1603 goto error3;
1604 }
1605 Py_DECREF(o1);
1606 Py_DECREF(o2);
1607 }
7bf85405 1608 else {
e0672e2f 1609 goto error2;
7bf85405 1610 }
e0672e2f
RD
1611 // Clean up.
1612 if (!isFast)
1613 Py_DECREF(o);
7bf85405
RD
1614 }
1615 return temp;
e0672e2f
RD
1616
1617error3:
1618 Py_DECREF(o1);
1619 Py_DECREF(o2);
1620error2:
1621 if (!isFast)
1622 Py_DECREF(o);
1623error1:
1624 delete temp;
1625error0:
1626 PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints.");
1627 return NULL;
7bf85405 1628}
e0672e2f
RD
1629// end of patch
1630//------------------------------
7bf85405
RD
1631
1632
2f90df85 1633wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
7bf85405
RD
1634 if (!PyList_Check(source)) {
1635 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1636 return NULL;
1637 }
1638 int count = PyList_Size(source);
1639 wxBitmap** temp = new wxBitmap*[count];
1640 if (! temp) {
1641 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1642 return NULL;
1643 }
1644 for (int x=0; x<count; x++) {
1645 PyObject* o = PyList_GetItem(source, x);
e166644c 1646 if (PyInstance_Check(o)) {
7bf85405 1647 wxBitmap* pt;
e166644c 1648 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) {
7bf85405
RD
1649 PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p.");
1650 return NULL;
1651 }
1652 temp[x] = pt;
1653 }
1654 else {
1655 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
1656 return NULL;
1657 }
1658 }
1659 return temp;
1660}
1661
1662
1663
2f90df85 1664wxString* wxString_LIST_helper(PyObject* source) {
7bf85405
RD
1665 if (!PyList_Check(source)) {
1666 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1667 return NULL;
1668 }
1669 int count = PyList_Size(source);
1670 wxString* temp = new wxString[count];
1671 if (! temp) {
1672 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1673 return NULL;
1674 }
1675 for (int x=0; x<count; x++) {
1676 PyObject* o = PyList_GetItem(source, x);
ecc08ead
RD
1677#if PYTHON_API_VERSION >= 1009
1678 if (! PyString_Check(o) && ! PyUnicode_Check(o)) {
1679 PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects.");
1680 return NULL;
1681 }
ecc08ead 1682#else
7bf85405
RD
1683 if (! PyString_Check(o)) {
1684 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
1685 return NULL;
1686 }
ecc08ead 1687#endif
a541c325
RD
1688
1689 wxString* pStr = wxString_in_helper(o);
1690 temp[x] = *pStr;
1691 delete pStr;
7bf85405
RD
1692 }
1693 return temp;
1694}
1695
1696
2f90df85 1697wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
7bf85405
RD
1698 if (!PyList_Check(source)) {
1699 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1700 return NULL;
1701 }
1702 int count = PyList_Size(source);
1703 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
1704 if (! temp) {
1705 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1706 return NULL;
1707 }
1708 for (int x=0; x<count; x++) {
1709 PyObject* o = PyList_GetItem(source, x);
e166644c 1710 if (PyInstance_Check(o)) {
7bf85405 1711 wxAcceleratorEntry* ae;
e166644c 1712 if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) {
7bf85405
RD
1713 PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p.");
1714 return NULL;
1715 }
1716 temp[x] = *ae;
1717 }
1718 else if (PyTuple_Check(o)) {
1719 PyObject* o1 = PyTuple_GetItem(o, 0);
1720 PyObject* o2 = PyTuple_GetItem(o, 1);
1721 PyObject* o3 = PyTuple_GetItem(o, 2);
0adbc166 1722 temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
7bf85405
RD
1723 }
1724 else {
1725 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1726 return NULL;
1727 }
1728 }
1729 return temp;
1730}
1731
bb0054cd 1732
9d37f964
RD
1733wxPen** wxPen_LIST_helper(PyObject* source) {
1734 if (!PyList_Check(source)) {
1735 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1736 return NULL;
1737 }
1738 int count = PyList_Size(source);
1739 wxPen** temp = new wxPen*[count];
1740 if (!temp) {
1741 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1742 return NULL;
1743 }
1744 for (int x=0; x<count; x++) {
1745 PyObject* o = PyList_GetItem(source, x);
1746 if (PyInstance_Check(o)) {
1747 wxPen* pt;
1748 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxPen_p")) {
1749 delete temp;
1750 PyErr_SetString(PyExc_TypeError,"Expected _wxPen_p.");
1751 return NULL;
1752 }
1753 temp[x] = pt;
1754 }
1755 else {
1756 delete temp;
1757 PyErr_SetString(PyExc_TypeError, "Expected a list of wxPens.");
1758 return NULL;
1759 }
1760 }
1761 return temp;
1762}
1763
1764
1765bool _2int_seq_helper(PyObject* source, int* i1, int* i2) {
1766 bool isFast = PyList_Check(source) || PyTuple_Check(source);
1767 PyObject *o1, *o2;
1768
1769 if (!PySequence_Check(source) || PySequence_Length(source) != 2)
1770 return FALSE;
1771
1772 if (isFast) {
1773 o1 = PySequence_Fast_GET_ITEM(source, 0);
1774 o2 = PySequence_Fast_GET_ITEM(source, 1);
1775 }
1776 else {
1777 o1 = PySequence_GetItem(source, 0);
1778 o2 = PySequence_GetItem(source, 1);
1779 }
1780
1781 *i1 = PyInt_AsLong(o1);
1782 *i2 = PyInt_AsLong(o2);
1783
1784 if (! isFast) {
1785 Py_DECREF(o1);
1786 Py_DECREF(o2);
1787 }
1788 return TRUE;
1789}
1790
1791
1792bool _4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4) {
1793 bool isFast = PyList_Check(source) || PyTuple_Check(source);
1794 PyObject *o1, *o2, *o3, *o4;
1795
1796 if (!PySequence_Check(source) || PySequence_Length(source) != 4)
1797 return FALSE;
1798
1799 if (isFast) {
1800 o1 = PySequence_Fast_GET_ITEM(source, 0);
1801 o2 = PySequence_Fast_GET_ITEM(source, 1);
1802 o3 = PySequence_Fast_GET_ITEM(source, 2);
1803 o4 = PySequence_Fast_GET_ITEM(source, 3);
1804 }
1805 else {
1806 o1 = PySequence_GetItem(source, 0);
1807 o2 = PySequence_GetItem(source, 1);
1808 o3 = PySequence_GetItem(source, 2);
1809 o4 = PySequence_GetItem(source, 3);
1810 }
1811
1812 *i1 = PyInt_AsLong(o1);
1813 *i2 = PyInt_AsLong(o2);
1814 *i3 = PyInt_AsLong(o3);
1815 *i4 = PyInt_AsLong(o4);
1816
1817 if (! isFast) {
1818 Py_DECREF(o1);
1819 Py_DECREF(o2);
1820 Py_DECREF(o3);
1821 Py_DECREF(o4);
1822 }
1823 return TRUE;
1824}
1825
bb0054cd
RD
1826
1827//----------------------------------------------------------------------
bb0054cd 1828
2f90df85
RD
1829bool wxSize_helper(PyObject* source, wxSize** obj) {
1830
1831 // If source is an object instance then it may already be the right type
1832 if (PyInstance_Check(source)) {
1833 wxSize* ptr;
1834 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p"))
1835 goto error;
1836 *obj = ptr;
1837 return TRUE;
1838 }
1839 // otherwise a 2-tuple of integers is expected
1840 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1841 PyObject* o1 = PySequence_GetItem(source, 0);
1842 PyObject* o2 = PySequence_GetItem(source, 1);
db0ff83e
RD
1843 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
1844 Py_DECREF(o1);
1845 Py_DECREF(o2);
1846 goto error;
1847 }
2f90df85 1848 **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2));
db0ff83e
RD
1849 Py_DECREF(o1);
1850 Py_DECREF(o2);
2f90df85
RD
1851 return TRUE;
1852 }
1853
1854 error:
1855 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object.");
1856 return FALSE;
1857}
1858
1859bool wxPoint_helper(PyObject* source, wxPoint** obj) {
1860
1861 // If source is an object instance then it may already be the right type
1862 if (PyInstance_Check(source)) {
1863 wxPoint* ptr;
1864 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p"))
1865 goto error;
1866 *obj = ptr;
1867 return TRUE;
1868 }
e0672e2f
RD
1869 // otherwise a length-2 sequence of integers is expected
1870 if (PySequence_Check(source) && PySequence_Length(source) == 2) {
2f90df85
RD
1871 PyObject* o1 = PySequence_GetItem(source, 0);
1872 PyObject* o2 = PySequence_GetItem(source, 1);
db0ff83e
RD
1873 // This should really check for integers, not numbers -- but that would break code.
1874 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
1875 Py_DECREF(o1);
1876 Py_DECREF(o2);
1877 goto error;
1878 }
1879 **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2));
1880 Py_DECREF(o1);
1881 Py_DECREF(o2);
2f90df85
RD
1882 return TRUE;
1883 }
2f90df85
RD
1884 error:
1885 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object.");
1886 return FALSE;
1887}
1888
1889
1890
1891bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) {
1892
1893 // If source is an object instance then it may already be the right type
1894 if (PyInstance_Check(source)) {
1895 wxRealPoint* ptr;
1896 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p"))
1897 goto error;
1898 *obj = ptr;
1899 return TRUE;
1900 }
1901 // otherwise a 2-tuple of floats is expected
1902 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1903 PyObject* o1 = PySequence_GetItem(source, 0);
1904 PyObject* o2 = PySequence_GetItem(source, 1);
db0ff83e
RD
1905 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
1906 Py_DECREF(o1);
1907 Py_DECREF(o2);
1908 goto error;
1909 }
2f90df85 1910 **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
db0ff83e
RD
1911 Py_DECREF(o1);
1912 Py_DECREF(o2);
2f90df85
RD
1913 return TRUE;
1914 }
1915
1916 error:
1917 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object.");
1918 return FALSE;
1919}
1920
1921
1922
1923
1924bool wxRect_helper(PyObject* source, wxRect** obj) {
1925
1926 // If source is an object instance then it may already be the right type
1927 if (PyInstance_Check(source)) {
1928 wxRect* ptr;
1929 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p"))
1930 goto error;
1931 *obj = ptr;
1932 return TRUE;
1933 }
1934 // otherwise a 4-tuple of integers is expected
1935 else if (PySequence_Check(source) && PyObject_Length(source) == 4) {
1936 PyObject* o1 = PySequence_GetItem(source, 0);
1937 PyObject* o2 = PySequence_GetItem(source, 1);
1938 PyObject* o3 = PySequence_GetItem(source, 2);
1939 PyObject* o4 = PySequence_GetItem(source, 3);
db0ff83e
RD
1940 if (!PyNumber_Check(o1) || !PyNumber_Check(o2) ||
1941 !PyNumber_Check(o3) || !PyNumber_Check(o4)) {
1942 Py_DECREF(o1);
1943 Py_DECREF(o2);
1944 Py_DECREF(o3);
1945 Py_DECREF(o4);
1946 goto error;
1947 }
2f90df85 1948 **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2),
db0ff83e
RD
1949 PyInt_AsLong(o3), PyInt_AsLong(o4));
1950 Py_DECREF(o1);
1951 Py_DECREF(o2);
1952 Py_DECREF(o3);
1953 Py_DECREF(o4);
2f90df85
RD
1954 return TRUE;
1955 }
1956
1957 error:
1958 PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object.");
1959 return FALSE;
1960}
1961
1962
1963
f6bcfd97
BP
1964bool wxColour_helper(PyObject* source, wxColour** obj) {
1965
1966 // If source is an object instance then it may already be the right type
1967 if (PyInstance_Check(source)) {
1968 wxColour* ptr;
1969 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p"))
1970 goto error;
1971 *obj = ptr;
1972 return TRUE;
1973 }
1974 // otherwise a string is expected
1975 else if (PyString_Check(source)) {
a541c325
RD
1976 wxString spec(PyString_AS_STRING(source), *wxConvCurrent);
1977 if (spec.GetChar(0) == '#' && spec.Length() == 7) { // It's #RRGGBB
1978 long red, green, blue;
1979 red = green = blue = 0;
a541c325
RD
1980 spec.Mid(1,2).ToLong(&red, 16);
1981 spec.Mid(3,2).ToLong(&green, 16);
1982 spec.Mid(5,2).ToLong(&blue, 16);
1983
f6bcfd97
BP
1984 **obj = wxColour(red, green, blue);
1985 return TRUE;
1986 }
1987 else { // it's a colour name
1988 **obj = wxColour(spec);
1989 return TRUE;
1990 }
1991 }
1992
1993 error:
a541c325
RD
1994 PyErr_SetString(PyExc_TypeError,
1995 "Expected a wxColour object or a string containing a colour "
1996 "name or '#RRGGBB'.");
f6bcfd97
BP
1997 return FALSE;
1998}
1999
2000
bb0054cd 2001//----------------------------------------------------------------------
b37c7e1d
RD
2002
2003PyObject* wxArrayString2PyList_helper(const wxArrayString& arr) {
2004
2005 PyObject* list = PyList_New(0);
2006 for (size_t i=0; i < arr.GetCount(); i++) {
c8bc7bb8
RD
2007#if wxUSE_UNICODE
2008 PyObject* str = PyUnicode_FromUnicode(arr[i].c_str(), arr[i].Len());
2009#else
2010 PyObject* str = PyString_FromStringAndSize(arr[i].c_str(), arr[i].Len());
2011#endif
b37c7e1d 2012 PyList_Append(list, str);
8af26133 2013 Py_DECREF(str);
b37c7e1d
RD
2014 }
2015 return list;
2016}
2017
2018
293a0a86
RD
2019PyObject* wxArrayInt2PyList_helper(const wxArrayInt& arr) {
2020
2021 PyObject* list = PyList_New(0);
2022 for (size_t i=0; i < arr.GetCount(); i++) {
2023 PyObject* number = PyInt_FromLong(arr[i]);
2024 PyList_Append(list, number);
2025 Py_DECREF(number);
2026 }
2027 return list;
2028}
2029
2030
bb0054cd 2031//----------------------------------------------------------------------
7bf85405 2032//----------------------------------------------------------------------
7bf85405 2033
7bf85405 2034
7bf85405 2035
de20db99 2036