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