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