]> git.saurik.com Git - wxWidgets.git/blame - wxPython/src/helpers.cpp
Applied [ 880011 ] Several fixes to DbBrowse demo
[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//
d14a1e28 7// Created: 1-July-1997
7bf85405
RD
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>
d14a1e28
RD
16#include "wx/wxPython/wxPython_int.h"
17#include "wx/wxPython/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
dd116e73
RD
31#include <wx/clipbrd.h>
32#include <wx/mimetype.h>
d84a9306 33#include <wx/image.h>
dd116e73 34
c8bc7bb8
RD
35//----------------------------------------------------------------------
36
37#if PYTHON_API_VERSION <= 1007 && wxUSE_UNICODE
38#error Python must support Unicode to use wxWindows Unicode
39#endif
40
4268f798
RD
41//----------------------------------------------------------------------
42
4268f798 43wxPyApp* wxPythonApp = NULL; // Global instance of application object
dd9f7fea
RD
44bool wxPyDoCleanup = False;
45bool wxPyDoingCleanup = False;
4268f798
RD
46
47
48#ifdef WXP_WITH_THREAD
49struct wxPyThreadState {
50 unsigned long tid;
51 PyThreadState* tstate;
b856768b 52 int blocked;
4268f798
RD
53
54 wxPyThreadState(unsigned long _tid=0, PyThreadState* _tstate=NULL)
b856768b 55 : tid(_tid), tstate(_tstate), blocked(1) {}
4268f798
RD
56};
57
58#include <wx/dynarray.h>
59WX_DECLARE_OBJARRAY(wxPyThreadState, wxPyThreadStateArray);
60#include <wx/arrimpl.cpp>
61WX_DEFINE_OBJARRAY(wxPyThreadStateArray);
62
63wxPyThreadStateArray* wxPyTStates = NULL;
64wxMutex* wxPyTMutex = NULL;
65#endif
7bf85405
RD
66
67
1e4a197e 68static PyObject* wxPython_dict = NULL;
1e4a197e
RD
69static PyObject* wxPyAssertionError = NULL;
70
d14a1e28
RD
71PyObject* wxPyPtrTypeMap = NULL;
72
1e4a197e 73
21f4bf45 74#ifdef __WXMSW__ // If building for win32...
21f4bf45
RD
75//----------------------------------------------------------------------
76// This gets run when the DLL is loaded. We just need to save a handle.
77//----------------------------------------------------------------------
9c039d08 78
21f4bf45
RD
79BOOL WINAPI DllMain(
80 HINSTANCE hinstDLL, // handle to DLL module
81 DWORD fdwReason, // reason for calling function
82 LPVOID lpvReserved // reserved
83 )
84{
a2426843
RD
85 // If wxPython is embedded in another wxWindows app then
86 // the inatance has already been set.
87 if (! wxGetInstance())
88 wxSetInstance(hinstDLL);
dd9f7fea 89 return True;
21f4bf45
RD
90}
91#endif
92
7bf85405 93//----------------------------------------------------------------------
4268f798 94// Classes for implementing the wxp main application shell.
7bf85405
RD
95//----------------------------------------------------------------------
96
1e4a197e
RD
97IMPLEMENT_ABSTRACT_CLASS(wxPyApp, wxApp);
98
7bf85405 99
cf694132 100wxPyApp::wxPyApp() {
1e4a197e 101 m_assertMode = wxPYAPP_ASSERT_EXCEPTION;
dd9f7fea 102 m_startupComplete = False;
cf694132
RD
103}
104
1e4a197e 105
cf694132 106wxPyApp::~wxPyApp() {
cf694132
RD
107}
108
109
d14a1e28 110// This one isn't acutally called... We fake it with _BootstrapApp
4268f798 111bool wxPyApp::OnInit() {
dd9f7fea 112 return False;
7bf85405
RD
113}
114
4268f798
RD
115
116int wxPyApp::MainLoop() {
7ff49f0c 117 int retval = 0;
af309447 118
7ff49f0c 119 DeletePendingObjects();
4268f798 120 bool initialized = wxTopLevelWindows.GetCount() != 0;
4268f798 121 if (initialized) {
098be78d
RD
122 if ( m_exitOnFrameDelete == Later ) {
123 m_exitOnFrameDelete = Yes;
124 }
125
7ff49f0c 126 retval = wxApp::MainLoop();
4268f798 127 OnExit();
7ff49f0c
RD
128 }
129 return retval;
130}
7bf85405
RD
131
132
1e4a197e 133bool wxPyApp::OnInitGui() {
dd9f7fea 134 bool rval=True;
1e4a197e 135 wxApp::OnInitGui(); // in this case always call the base class version
d14a1e28 136 wxPyBeginBlockThreads();
1e4a197e
RD
137 if (wxPyCBH_findCallback(m_myInst, "OnInitGui"))
138 rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
d14a1e28 139 wxPyEndBlockThreads();
1e4a197e
RD
140 return rval;
141}
142
143
144int wxPyApp::OnExit() {
145 int rval=0;
146 wxPyBeginBlockThreads();
147 if (wxPyCBH_findCallback(m_myInst, "OnExit"))
148 rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
149 wxPyEndBlockThreads();
150 wxApp::OnExit(); // in this case always call the base class version
151 return rval;
152}
153
154
155#ifdef __WXDEBUG__
156void wxPyApp::OnAssert(const wxChar *file,
157 int line,
158 const wxChar *cond,
159 const wxChar *msg) {
160
d14a1e28
RD
161 // if we're not fully initialized then just log the error
162 if (! m_startupComplete) {
163 wxString buf;
164 buf.Alloc(4096);
165 buf.Printf(wxT("%s(%d): assert \"%s\" failed"),
166 file, line, cond);
167 if (msg != NULL) {
168 buf += wxT(": ");
169 buf += msg;
170 }
171 wxLogDebug(buf);
172 return;
173 }
174
1e4a197e
RD
175 // If the OnAssert is overloaded in the Python class then call it...
176 bool found;
177 wxPyBeginBlockThreads();
178 if ((found = wxPyCBH_findCallback(m_myInst, "OnAssert"))) {
179 PyObject* fso = wx2PyString(file);
180 PyObject* cso = wx2PyString(file);
181 PyObject* mso;
182 if (msg != NULL)
183 mso = wx2PyString(file);
184 else {
185 mso = Py_None; Py_INCREF(Py_None);
186 }
187 wxPyCBH_callCallback(m_myInst, Py_BuildValue("(OiOO)", fso, line, cso, mso));
188 Py_DECREF(fso);
189 Py_DECREF(cso);
190 Py_DECREF(mso);
191 }
192 wxPyEndBlockThreads();
193
194 // ...otherwise do our own thing with it
195 if (! found) {
196 // ignore it?
197 if (m_assertMode & wxPYAPP_ASSERT_SUPPRESS)
198 return;
199
200 // turn it into a Python exception?
201 if (m_assertMode & wxPYAPP_ASSERT_EXCEPTION) {
202 wxString buf;
203 buf.Alloc(4096);
204 buf.Printf(wxT("C++ assertion \"%s\" failed in %s(%d)"), cond, file, line);
205 if (msg != NULL) {
206 buf += wxT(": ");
207 buf += msg;
208 }
209
210 // set the exception
211 wxPyBeginBlockThreads();
212 PyObject* s = wx2PyString(buf);
213 PyErr_SetObject(wxPyAssertionError, s);
214 Py_DECREF(s);
215 wxPyEndBlockThreads();
216
217 // Now when control returns to whatever API wrapper was called from
218 // Python it should detect that an exception is set and will return
219 // NULL, signalling the exception to Python.
220 }
221
222 // Send it to the normal log destination, but only if
223 // not _DIALOG because it will call this too
224 if ( (m_assertMode & wxPYAPP_ASSERT_LOG) && !(m_assertMode & wxPYAPP_ASSERT_DIALOG)) {
225 wxString buf;
226 buf.Alloc(4096);
227 buf.Printf(wxT("%s(%d): assert \"%s\" failed"),
228 file, line, cond);
229 if (msg != NULL) {
230 buf += wxT(": ");
231 buf += msg;
232 }
233 wxLogDebug(buf);
234 }
235
236 // do the normal wx assert dialog?
237 if (m_assertMode & wxPYAPP_ASSERT_DIALOG)
238 wxApp::OnAssert(file, line, cond, msg);
239 }
240}
241#endif
242
243
d14a1e28 244
1e4a197e
RD
245/*static*/
246bool wxPyApp::GetMacSupportPCMenuShortcuts() {
247#ifdef __WXMAC__
248 return s_macSupportPCMenuShortcuts;
249#else
250 return 0;
251#endif
252}
253
254/*static*/
255long wxPyApp::GetMacAboutMenuItemId() {
256#ifdef __WXMAC__
257 return s_macAboutMenuItemId;
258#else
259 return 0;
260#endif
261}
262
263/*static*/
264long wxPyApp::GetMacPreferencesMenuItemId() {
265#ifdef __WXMAC__
266 return s_macPreferencesMenuItemId;
267#else
268 return 0;
269#endif
270}
271
272/*static*/
273long wxPyApp::GetMacExitMenuItemId() {
274#ifdef __WXMAC__
275 return s_macExitMenuItemId;
276#else
277 return 0;
278#endif
279}
280
281/*static*/
282wxString wxPyApp::GetMacHelpMenuTitleName() {
283#ifdef __WXMAC__
284 return s_macHelpMenuTitleName;
285#else
286 return wxEmptyString;
287#endif
288}
289
1e4a197e
RD
290/*static*/
291void wxPyApp::SetMacSupportPCMenuShortcuts(bool val) {
292#ifdef __WXMAC__
293 s_macSupportPCMenuShortcuts = val;
294#endif
295}
296
297/*static*/
298void wxPyApp::SetMacAboutMenuItemId(long val) {
299#ifdef __WXMAC__
300 s_macAboutMenuItemId = val;
301#endif
302}
303
304/*static*/
305void wxPyApp::SetMacPreferencesMenuItemId(long val) {
306#ifdef __WXMAC__
307 s_macPreferencesMenuItemId = val;
308#endif
309}
310
311/*static*/
312void wxPyApp::SetMacExitMenuItemId(long val) {
313#ifdef __WXMAC__
314 s_macExitMenuItemId = val;
315#endif
316}
317
318/*static*/
319void wxPyApp::SetMacHelpMenuTitleName(const wxString& val) {
320#ifdef __WXMAC__
321 s_macHelpMenuTitleName = val;
322#endif
323}
324
325
d14a1e28
RD
326// This finishes the initialization of wxWindows and then calls the OnInit
327// that should be present in the derived (Python) class.
328void wxPyApp::_BootstrapApp()
329{
330 bool result;
331 PyObject* retval = NULL;
332 PyObject* pyint = NULL;
333
334
335 // Get any command-line args passed to this program from the sys module
336 int argc = 0;
337 char** argv = NULL;
338 wxPyBeginBlockThreads();
339 PyObject* sysargv = PySys_GetObject("argv");
340 if (sysargv != NULL) {
341 argc = PyList_Size(sysargv);
342 argv = new char*[argc+1];
343 int x;
344 for(x=0; x<argc; x++) {
345 PyObject *pyArg = PyList_GetItem(sysargv, x);
346 argv[x] = PyString_AsString(pyArg);
347 }
348 argv[argc] = NULL;
349 }
350 wxPyEndBlockThreads();
351
352 result = wxEntryStart(argc, argv);
353 delete [] argv;
354
355 wxPyBeginBlockThreads();
356 if (! result) {
357 PyErr_SetString(PyExc_SystemError, "wxEntryStart failed!");
358 goto error;
359 }
360
361 // The stock objects were all NULL when they were loaded into
362 // SWIG generated proxies, so re-init those now...
9e58eb56 363 wxPy_ReinitStockObjects(3);
d14a1e28
RD
364
365 // It's now ok to generate exceptions for assertion errors.
dd9f7fea 366 wxPythonApp->SetStartupComplete(True);
d14a1e28
RD
367
368 // Call the Python wxApp's OnInit function
369 if (wxPyCBH_findCallback(m_myInst, "OnInit")) {
f048f829
RD
370
371 PyObject* method = m_myInst.GetLastFound();
372 PyObject* argTuple = PyTuple_New(0);
373 retval = PyEval_CallObject(method, argTuple);
374 Py_DECREF(argTuple);
375 Py_DECREF(method);
376 if (retval == NULL)
377 goto error;
378
379 pyint = PyNumber_Int(retval);
d14a1e28
RD
380 if (! pyint) {
381 PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value");
382 goto error;
383 }
384 result = PyInt_AS_LONG(pyint);
385 }
386 else {
387 // Is it okay if there is no OnInit? Probably so...
dd9f7fea 388 result = True;
d14a1e28
RD
389 }
390
391
392 if (! result) {
393 PyErr_SetString(PyExc_SystemExit, "OnInit returned False, exiting...");
394 }
395
396 error:
397 Py_XDECREF(retval);
398 Py_XDECREF(pyint);
399
400 wxPyEndBlockThreads();
401};
4268f798 402
fb5e0af0 403//---------------------------------------------------------------------
7bf85405
RD
404//----------------------------------------------------------------------
405
a541c325 406
dd116e73 407#if 0
a541c325
RD
408static char* wxPyCopyCString(const wxChar* src)
409{
410 wxWX2MBbuf buff = (wxWX2MBbuf)wxConvCurrent->cWX2MB(src);
411 size_t len = strlen(buff);
412 char* dest = new char[len+1];
413 strcpy(dest, buff);
414 return dest;
415}
416
c8bc7bb8 417#if wxUSE_UNICODE
a541c325 418static char* wxPyCopyCString(const char* src) // we need a char version too
c8bc7bb8 419{
a541c325
RD
420 size_t len = strlen(src);
421 char* dest = new char[len+1];
422 strcpy(dest, src);
423 return dest;
c8bc7bb8 424}
a541c325 425#endif
c8bc7bb8 426
a541c325 427static wxChar* wxPyCopyWString(const char *src)
c8bc7bb8 428{
a541c325
RD
429 //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
430 wxString str(src, *wxConvCurrent);
431 return copystring(str);
c8bc7bb8
RD
432}
433
a541c325
RD
434#if wxUSE_UNICODE
435static wxChar* wxPyCopyWString(const wxChar *src)
c8bc7bb8 436{
a541c325 437 return copystring(src);
c8bc7bb8
RD
438}
439#endif
dd116e73 440#endif
f6bcfd97 441
a541c325 442
d14a1e28
RD
443inline const char* dropwx(const char* name) {
444 if (name[0] == 'w' && name[1] == 'x')
445 return name+2;
446 else
447 return name;
448}
449
a541c325
RD
450//----------------------------------------------------------------------
451
dd116e73 452// This function is called when the wxc module is imported to do some initial
d14a1e28
RD
453// setup. (Before there is a wxApp object.) The rest happens in
454// wxPyApp::_BootstrapApp
455void __wxPyPreStart(PyObject* moduleDict)
7bf85405 456{
de20db99
RD
457
458#ifdef __WXMSW__
44f68505 459// wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
de20db99
RD
460#endif
461
9d8bd15f 462#ifdef WXP_WITH_THREAD
1112b0c6 463 PyEval_InitThreads();
4268f798
RD
464 wxPyTStates = new wxPyThreadStateArray;
465 wxPyTMutex = new wxMutex;
098d1f0c
RD
466
467 // Save the current (main) thread state in our array
468 PyThreadState* tstate = wxPyBeginAllowThreads();
469 wxPyEndAllowThreads(tstate);
9d8bd15f
RD
470#endif
471
dd116e73 472 // Ensure that the build options in the DLL (or whatever) match this build
3628e088 473 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "wxPython");
0b85cc38 474
d14a1e28 475 // Init the stock objects to a non-NULL value so SWIG doesn't create them as None
9e58eb56 476 wxPy_ReinitStockObjects(1);
0d6f9504
RD
477}
478
479
dd116e73 480
d14a1e28 481void __wxPyCleanup() {
dd9f7fea 482 wxPyDoingCleanup = True;
d14a1e28 483 if (wxPyDoCleanup) {
dd9f7fea 484 wxPyDoCleanup = False;
e3dbf593 485 wxEntryCleanup();
d14a1e28 486 }
7faa9591 487#ifdef WXP_WITH_THREAD
4268f798
RD
488 delete wxPyTMutex;
489 wxPyTMutex = NULL;
490 wxPyTStates->Empty();
491 delete wxPyTStates;
492 wxPyTStates = NULL;
7faa9591 493#endif
7ff49f0c 494}
7bf85405
RD
495
496
d14a1e28
RD
497// Save a reference to the dictionary of the wx.core module, and inject
498// a few more things into it.
499PyObject* __wxPySetDictionary(PyObject* /* self */, PyObject* args)
7bf85405
RD
500{
501
502 if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
503 return NULL;
504
505 if (!PyDict_Check(wxPython_dict)) {
d14a1e28 506 PyErr_SetString(PyExc_TypeError, "_wxPySetDictionary must have dictionary object!");
7bf85405
RD
507 return NULL;
508 }
9416aa89
RD
509
510 if (! wxPyPtrTypeMap)
511 wxPyPtrTypeMap = PyDict_New();
512 PyDict_SetItemString(wxPython_dict, "__wxPyPtrTypeMap", wxPyPtrTypeMap);
513
d14a1e28
RD
514 // Create an exception object to use for wxASSERTions
515 wxPyAssertionError = PyErr_NewException("wx.core.PyAssertionError",
516 PyExc_AssertionError, NULL);
517 PyDict_SetItemString(wxPython_dict, "PyAssertionError", wxPyAssertionError);
518
9416aa89 519
7bf85405 520#ifdef __WXMOTIF__
21f4bf45
RD
521#define wxPlatform "__WXMOTIF__"
522#endif
be43cc44
RD
523#ifdef __WXX11__
524#define wxPlatform "__WXX11__"
7bf85405
RD
525#endif
526#ifdef __WXGTK__
21f4bf45 527#define wxPlatform "__WXGTK__"
7bf85405
RD
528#endif
529#if defined(__WIN32__) || defined(__WXMSW__)
fb5e0af0 530#define wxPlatform "__WXMSW__"
7bf85405
RD
531#endif
532#ifdef __WXMAC__
21f4bf45 533#define wxPlatform "__WXMAC__"
7bf85405
RD
534#endif
535
249a57f4
RD
536#ifdef __WXDEBUG__
537 int wxdebug = 1;
538#else
539 int wxdebug = 0;
540#endif
541
d14a1e28
RD
542 PyDict_SetItemString(wxPython_dict, "Platform", PyString_FromString(wxPlatform));
543 PyDict_SetItemString(wxPython_dict, "USE_UNICODE", PyInt_FromLong(wxUSE_UNICODE));
249a57f4 544 PyDict_SetItemString(wxPython_dict, "__WXDEBUG__", PyInt_FromLong(wxdebug));
c8bc7bb8 545
d14a1e28 546 RETURN_NONE();
7bf85405
RD
547}
548
dd116e73 549
d14a1e28
RD
550//---------------------------------------------------------------------------
551
dd9f7fea 552// Python's PyInstance_Check does not return True for instances of new-style
d14a1e28
RD
553// classes. This should get close enough for both new and old classes but I
554// should re-evaluate the need for doing instance checks...
555bool wxPyInstance_Check(PyObject* obj) {
556 return PyObject_HasAttrString(obj, "__class__") != 0;
557}
558
559
560// This one checks if the object is an instance of a SWIG proxy class (it has
561// a .this attribute)
562bool wxPySwigInstance_Check(PyObject* obj) {
563 return PyObject_HasAttrString(obj, "this") != 0;
564}
565
dd116e73
RD
566//---------------------------------------------------------------------------
567
9e58eb56
RD
568// The stock objects are no longer created when the wxc module is imported,
569// but only after the app object has been created. The
570// wxPy_ReinitStockObjects function will be called 3 times to pass the stock
571// objects though various stages of evolution:
572//
573// pass 1: Set all the pointers to a non-NULL value so the Python proxy
574// object will be created (otherwise it will just use None.)
575//
576// pass 2: After the module has been imported and the python proxys have
577// been created, then set the __class__ to be _wxPyUnbornObject so
578// it will catch any access to the object and will raise an exception.
579//
580// pass 3: Finally, from OnInit patch things up so the stock objects can
581// be used.
582
583
584PyObject* __wxPyFixStockObjects(PyObject* /* self */, PyObject* args)
585{
586 wxPy_ReinitStockObjects(2);
587 RETURN_NONE();
588}
589
dd116e73 590
9e58eb56 591static void rsoPass2(const char* name)
dd116e73 592{
9e58eb56 593 static PyObject* unbornObjectClass = NULL;
dd116e73 594 PyObject* obj;
9e58eb56
RD
595
596 if (unbornObjectClass == NULL) {
597 unbornObjectClass = PyDict_GetItemString(wxPython_dict, "_wxPyUnbornObject");
598 Py_INCREF(unbornObjectClass);
599 }
600
601 // Find the object instance
e4f4d653 602 obj = PyDict_GetItemString(wxPython_dict, (char*)dropwx(name));
9e58eb56
RD
603 wxCHECK_RET(obj != NULL, wxT("Unable to find stock object"));
604 wxCHECK_RET(wxPySwigInstance_Check(obj), wxT("Not a swig instance"));
605
606 // Change its class
607 PyObject_SetAttrString(obj, "__class__", unbornObjectClass);
608
609}
610
611static void rsoPass3(const char* name, const char* classname, void* ptr)
612{
613 PyObject* obj;
614 PyObject* classobj;
dd116e73
RD
615 PyObject* ptrobj;
616
9e58eb56 617 // Find the object instance
e4f4d653 618 obj = PyDict_GetItemString(wxPython_dict, (char*)dropwx(name));
9e58eb56
RD
619 wxCHECK_RET(obj != NULL, wxT("Unable to find stock object"));
620 wxCHECK_RET(wxPySwigInstance_Check(obj), wxT("Not a swig instance"));
621
622 // Find the class object and put it back in the instance
e4f4d653 623 classobj = PyDict_GetItemString(wxPython_dict, (char*)dropwx(classname));
9e58eb56
RD
624 wxCHECK_RET(classobj != NULL, wxT("Unable to find stock class object"));
625 PyObject_SetAttrString(obj, "__class__", classobj);
626
627 // Rebuild the .this swigified pointer with the new value of the C++ pointer
628 ptrobj = wxPyMakeSwigPtr(ptr, wxString(classname, *wxConvCurrent));
629 PyObject_SetAttrString(obj, "this", ptrobj);
630 Py_DECREF(ptrobj);
631}
632
633
634
635void wxPy_ReinitStockObjects(int pass)
636{
dd116e73 637
d14a1e28 638#define REINITOBJ(name, classname) \
9e58eb56
RD
639 if (pass == 1) { name = (classname*)0xC0C0C0C0; } \
640 else if (pass == 2) { rsoPass2(#name); } \
641 else if (pass == 3) { rsoPass3(#name, #classname, (void*)name); }
642
d14a1e28
RD
643
644#define REINITOBJ2(name, classname) \
9e58eb56
RD
645 if (pass == 1) { } \
646 else if (pass == 2) { rsoPass2(#name); } \
647 else if (pass == 3) { rsoPass3(#name, #classname, (void*)&name); }
dd116e73
RD
648
649
650 REINITOBJ(wxNORMAL_FONT, wxFont);
651 REINITOBJ(wxSMALL_FONT, wxFont);
652 REINITOBJ(wxITALIC_FONT, wxFont);
653 REINITOBJ(wxSWISS_FONT, wxFont);
654
655 REINITOBJ(wxRED_PEN, wxPen);
656 REINITOBJ(wxCYAN_PEN, wxPen);
657 REINITOBJ(wxGREEN_PEN, wxPen);
658 REINITOBJ(wxBLACK_PEN, wxPen);
659 REINITOBJ(wxWHITE_PEN, wxPen);
660 REINITOBJ(wxTRANSPARENT_PEN, wxPen);
661 REINITOBJ(wxBLACK_DASHED_PEN, wxPen);
662 REINITOBJ(wxGREY_PEN, wxPen);
663 REINITOBJ(wxMEDIUM_GREY_PEN, wxPen);
664 REINITOBJ(wxLIGHT_GREY_PEN, wxPen);
665
666 REINITOBJ(wxBLUE_BRUSH, wxBrush);
667 REINITOBJ(wxGREEN_BRUSH, wxBrush);
668 REINITOBJ(wxWHITE_BRUSH, wxBrush);
669 REINITOBJ(wxBLACK_BRUSH, wxBrush);
670 REINITOBJ(wxTRANSPARENT_BRUSH, wxBrush);
671 REINITOBJ(wxCYAN_BRUSH, wxBrush);
672 REINITOBJ(wxRED_BRUSH, wxBrush);
673 REINITOBJ(wxGREY_BRUSH, wxBrush);
674 REINITOBJ(wxMEDIUM_GREY_BRUSH, wxBrush);
675 REINITOBJ(wxLIGHT_GREY_BRUSH, wxBrush);
676
677 REINITOBJ(wxBLACK, wxColour);
678 REINITOBJ(wxWHITE, wxColour);
679 REINITOBJ(wxRED, wxColour);
680 REINITOBJ(wxBLUE, wxColour);
681 REINITOBJ(wxGREEN, wxColour);
682 REINITOBJ(wxCYAN, wxColour);
683 REINITOBJ(wxLIGHT_GREY, wxColour);
684
685 REINITOBJ(wxSTANDARD_CURSOR, wxCursor);
686 REINITOBJ(wxHOURGLASS_CURSOR, wxCursor);
687 REINITOBJ(wxCROSS_CURSOR, wxCursor);
688
689 REINITOBJ2(wxNullBitmap, wxBitmap);
690 REINITOBJ2(wxNullIcon, wxIcon);
691 REINITOBJ2(wxNullCursor, wxCursor);
692 REINITOBJ2(wxNullPen, wxPen);
693 REINITOBJ2(wxNullBrush, wxBrush);
694 REINITOBJ2(wxNullPalette, wxPalette);
695 REINITOBJ2(wxNullFont, wxFont);
696 REINITOBJ2(wxNullColour, wxColour);
697
698 REINITOBJ(wxTheFontList, wxFontList);
699 REINITOBJ(wxThePenList, wxPenList);
700 REINITOBJ(wxTheBrushList, wxBrushList);
701 REINITOBJ(wxTheColourDatabase, wxColourDatabase);
702
703
704 REINITOBJ(wxTheClipboard, wxClipboard);
dd116e73
RD
705 REINITOBJ2(wxDefaultValidator, wxValidator);
706 REINITOBJ2(wxNullImage, wxImage);
707 REINITOBJ2(wxNullAcceleratorTable, wxAcceleratorTable);
708
709#undef REINITOBJ
710#undef REINITOBJ2
711}
712
4acff284
RD
713//---------------------------------------------------------------------------
714
715void wxPyClientData_dtor(wxPyClientData* self) {
1e4a197e
RD
716 if (! wxPyDoingCleanup) { // Don't do it during cleanup as Python
717 // may have already garbage collected the object...
718 wxPyBeginBlockThreads();
719 Py_DECREF(self->m_obj);
d14a1e28 720 self->m_obj = NULL;
1e4a197e
RD
721 wxPyEndBlockThreads();
722 }
4acff284
RD
723}
724
725void wxPyUserData_dtor(wxPyUserData* self) {
1e4a197e
RD
726 if (! wxPyDoingCleanup) {
727 wxPyBeginBlockThreads();
728 Py_DECREF(self->m_obj);
d14a1e28 729 self->m_obj = NULL;
1e4a197e
RD
730 wxPyEndBlockThreads();
731 }
4acff284
RD
732}
733
734
735// This is called when an OOR controled object is being destroyed. Although
736// the C++ object is going away there is no way to force the Python object
737// (and all references to it) to die too. This causes problems (crashes) in
738// wxPython when a python shadow object attempts to call a C++ method using
739// the now bogus pointer... So to try and prevent this we'll do a little black
740// magic and change the class of the python instance to a class that will
741// raise an exception for any attempt to call methods with it. See
d14a1e28 742// _wxPyDeadObject in _core_ex.py for the implementation of this class.
4acff284
RD
743void wxPyOORClientData_dtor(wxPyOORClientData* self) {
744
745 static PyObject* deadObjectClass = NULL;
746
747 wxPyBeginBlockThreads();
748 if (deadObjectClass == NULL) {
749 deadObjectClass = PyDict_GetItemString(wxPython_dict, "_wxPyDeadObject");
d14a1e28
RD
750 // TODO: Can not wxASSERT here because inside a wxPyBeginBlock Threads,
751 // will lead to a deadlock when it tries to aquire the GIL again.
752 //wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!"));
4acff284
RD
753 Py_INCREF(deadObjectClass);
754 }
755
1e4a197e 756
1fded56b
RD
757 // Only if there is more than one reference to the object
758 if ( !wxPyDoingCleanup && self->m_obj->ob_refcnt > 1 ) {
d14a1e28
RD
759 // bool isInstance = wxPyInstance_Check(self->m_obj);
760 // TODO same here
761 //wxASSERT_MSG(isInstance, wxT("m_obj not an instance!?!?!"));
1fded56b
RD
762
763 // Call __del__, if there is one.
764 PyObject* func = PyObject_GetAttrString(self->m_obj, "__del__");
765 if (func) {
766 PyObject* rv = PyObject_CallMethod(self->m_obj, "__del__", NULL);
767 Py_XDECREF(rv);
768 Py_DECREF(func);
769 }
770 if (PyErr_Occurred())
771 PyErr_Clear(); // just ignore it for now
772
d14a1e28
RD
773
774 PyObject* dict = PyObject_GetAttrString(self->m_obj, "__dict__");
775 if (dict) {
776 // Clear the instance's dictionary
777 PyDict_Clear(dict);
778
779 // put the name of the old class into the instance, and then reset the
780 // class to be the dead class.
781 PyObject* klass = PyObject_GetAttrString(self->m_obj, "__class__");
782 PyObject* name = PyObject_GetAttrString(klass, "__name__");
783 PyDict_SetItemString(dict, "_name", name);
784 PyObject_SetAttrString(self->m_obj, "__class__", deadObjectClass);
785 //Py_INCREF(deadObjectClass);
786 Py_DECREF(klass);
787 Py_DECREF(name);
788 }
4acff284 789 }
1fded56b 790
d14a1e28 791 // m_obj is DECREF'd in the base class dtor...
4acff284
RD
792 wxPyEndBlockThreads();
793}
7bf85405 794
1fded56b 795
9416aa89
RD
796//---------------------------------------------------------------------------
797// Stuff used by OOR to find the right wxPython class type to return and to
798// build it.
799
800
801// The pointer type map is used when the "pointer" type name generated by SWIG
802// is not the same as the shadow class name, for example wxPyTreeCtrl
803// vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
804// so we'll just make it a Python dictionary in the wx module's namespace.
4acff284 805// (See __wxSetDictionary)
9416aa89
RD
806void wxPyPtrTypeMap_Add(const char* commonName, const char* ptrName) {
807 if (! wxPyPtrTypeMap)
808 wxPyPtrTypeMap = PyDict_New();
9416aa89
RD
809 PyDict_SetItemString(wxPyPtrTypeMap,
810 (char*)commonName,
811 PyString_FromString((char*)ptrName));
812}
813
814
815
9416aa89 816
2f4e9287 817PyObject* wxPyMake_wxObject(wxObject* source, bool checkEvtHandler) {
0122b7e3 818 PyObject* target = NULL;
dd9f7fea 819 bool isEvtHandler = False;
9416aa89
RD
820
821 if (source) {
2aab8f16 822 // If it's derived from wxEvtHandler then there may
2f4e9287
RD
823 // already be a pointer to a Python object that we can use
824 // in the OOR data.
825 if (checkEvtHandler && wxIsKindOf(source, wxEvtHandler)) {
dd9f7fea 826 isEvtHandler = True;
0122b7e3 827 wxEvtHandler* eh = (wxEvtHandler*)source;
4acff284 828 wxPyOORClientData* data = (wxPyOORClientData*)eh->GetClientObject();
0122b7e3
RD
829 if (data) {
830 target = data->m_obj;
d14a1e28
RD
831 if (target)
832 Py_INCREF(target);
0122b7e3 833 }
9416aa89 834 }
0122b7e3
RD
835
836 if (! target) {
d14a1e28
RD
837 // Otherwise make it the old fashioned way by making a new shadow
838 // object and putting this pointer in it. Look up the class
839 // heirarchy until we find a class name that is located in the
840 // python module.
841 const wxClassInfo* info = source->GetClassInfo();
842 wxString name = info->GetClassName();
843 bool exists = wxPyCheckSwigType(name);
844 while (info && !exists) {
845 info = info->GetBaseClass1();
846 name = info->GetClassName();
847 exists = wxPyCheckSwigType(name);
0122b7e3
RD
848 }
849 if (info) {
dd9f7fea 850 target = wxPyConstructObject((void*)source, name, False);
0122b7e3 851 if (target && isEvtHandler)
4acff284 852 ((wxEvtHandler*)source)->SetClientObject(new wxPyOORClientData(target));
0122b7e3 853 } else {
4acff284 854 wxString msg(wxT("wxPython class not found for "));
0122b7e3 855 msg += source->GetClassInfo()->GetClassName();
a541c325 856 PyErr_SetString(PyExc_NameError, msg.mbc_str());
0122b7e3
RD
857 target = NULL;
858 }
9416aa89
RD
859 }
860 } else { // source was NULL so return None.
861 Py_INCREF(Py_None); target = Py_None;
862 }
863 return target;
864}
865
0122b7e3 866
2f4e9287
RD
867PyObject* wxPyMake_wxSizer(wxSizer* source) {
868 PyObject* target = NULL;
869
870 if (source && wxIsKindOf(source, wxSizer)) {
d14a1e28
RD
871 // If it's derived from wxSizer then there may already be a pointer to
872 // a Python object that we can use in the OOR data.
2f4e9287 873 wxSizer* sz = (wxSizer*)source;
4acff284 874 wxPyOORClientData* data = (wxPyOORClientData*)sz->GetClientObject();
2f4e9287
RD
875 if (data) {
876 target = data->m_obj;
d14a1e28
RD
877 if (target)
878 Py_INCREF(target);
2f4e9287
RD
879 }
880 }
881 if (! target) {
dd9f7fea 882 target = wxPyMake_wxObject(source, False);
2f4e9287 883 if (target != Py_None)
4acff284 884 ((wxSizer*)source)->SetClientObject(new wxPyOORClientData(target));
2f4e9287
RD
885 }
886 return target;
887}
888
889
d559219f 890//---------------------------------------------------------------------------
7bf85405 891
4958ea8f 892
cf694132 893#ifdef WXP_WITH_THREAD
4268f798
RD
894inline
895unsigned long wxPyGetCurrentThreadId() {
4958ea8f 896 return wxThread::GetCurrentId();
4268f798
RD
897}
898
b856768b 899static wxPyThreadState gs_shutdownTState;
d14a1e28 900
4268f798 901static
b856768b 902wxPyThreadState* wxPyGetThreadState() {
be43cc44 903 if (wxPyTMutex == NULL) // Python is shutting down...
b856768b 904 return &gs_shutdownTState;
be43cc44 905
4268f798 906 unsigned long ctid = wxPyGetCurrentThreadId();
b856768b 907 wxPyThreadState* tstate = NULL;
4268f798
RD
908
909 wxPyTMutex->Lock();
910 for(size_t i=0; i < wxPyTStates->GetCount(); i++) {
911 wxPyThreadState& info = wxPyTStates->Item(i);
912 if (info.tid == ctid) {
b856768b 913 tstate = &info;
4268f798
RD
914 break;
915 }
916 }
917 wxPyTMutex->Unlock();
a541c325 918 wxASSERT_MSG(tstate, wxT("PyThreadState should not be NULL!"));
4268f798
RD
919 return tstate;
920}
921
b856768b 922
4268f798
RD
923static
924void wxPySaveThreadState(PyThreadState* tstate) {
be43cc44 925 if (wxPyTMutex == NULL) { // Python is shutting down, assume a single thread...
b856768b 926 gs_shutdownTState.tstate = tstate;
be43cc44
RD
927 return;
928 }
4268f798
RD
929 unsigned long ctid = wxPyGetCurrentThreadId();
930 wxPyTMutex->Lock();
931 for(size_t i=0; i < wxPyTStates->GetCount(); i++) {
932 wxPyThreadState& info = wxPyTStates->Item(i);
933 if (info.tid == ctid) {
763d71e4
RD
934#if 0
935 if (info.tstate != tstate)
936 wxLogMessage("*** tstate mismatch!???");
937#endif
46e10fde 938 // info.tstate = tstate; *** DO NOT update existing ones???
763d71e4
RD
939 // Normally it will never change, but apparently COM callbacks
940 // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient
46e10fde 941 // tstate which will then be garbage the next time we try to use
763d71e4 942 // it...
4268f798
RD
943 wxPyTMutex->Unlock();
944 return;
945 }
1112b0c6 946 }
4268f798
RD
947 // not found, so add it...
948 wxPyTStates->Add(new wxPyThreadState(ctid, tstate));
949 wxPyTMutex->Unlock();
950}
951
952#endif
953
954
b856768b 955
4268f798
RD
956// Calls from Python to wxWindows code are wrapped in calls to these
957// functions:
958
959PyThreadState* wxPyBeginAllowThreads() {
960#ifdef WXP_WITH_THREAD
961 PyThreadState* saved = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
962 wxPySaveThreadState(saved);
b856768b 963 wxPyGetThreadState()->blocked -= 1;
4268f798
RD
964 return saved;
965#else
966 return NULL;
967#endif
968}
969
970void wxPyEndAllowThreads(PyThreadState* saved) {
971#ifdef WXP_WITH_THREAD
b856768b 972 wxPyGetThreadState()->blocked += 1;
4268f798 973 PyEval_RestoreThread(saved); // Py_END_ALLOW_THREADS;
cf694132 974#endif
d559219f 975}
cf694132 976
cf694132 977
4268f798
RD
978
979// Calls from wxWindows back to Python code, or even any PyObject
980// manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
981
982void wxPyBeginBlockThreads() {
cf694132 983#ifdef WXP_WITH_THREAD
b856768b
RD
984 wxPyThreadState* tstate = wxPyGetThreadState();
985 if (tstate->blocked++ == 0) { // if nested calls then do nothing
986 PyEval_RestoreThread(tstate->tstate);
987 }
4268f798
RD
988#endif
989}
990
991
992void wxPyEndBlockThreads() {
993#ifdef WXP_WITH_THREAD
b856768b
RD
994 wxPyThreadState* tstate = wxPyGetThreadState();
995 tstate->blocked -= 1;
996 if ( tstate->blocked == 0) { // if nested calls then do nothing
997 PyEval_SaveThread();
998 }
1112b0c6 999#endif
cf694132
RD
1000}
1001
19a97bd6 1002
d559219f 1003//---------------------------------------------------------------------------
f74ff5ef
RD
1004// wxPyInputStream and wxPyCBInputStream methods
1005
f74ff5ef
RD
1006
1007void wxPyInputStream::close() {
a541c325 1008 /* do nothing for now */
f74ff5ef
RD
1009}
1010
1011void wxPyInputStream::flush() {
a541c325 1012 /* do nothing for now */
f74ff5ef
RD
1013}
1014
1015bool wxPyInputStream::eof() {
1016 if (m_wxis)
1017 return m_wxis->Eof();
1018 else
dd9f7fea 1019 return True;
f74ff5ef
RD
1020}
1021
1022wxPyInputStream::~wxPyInputStream() {
1023 /* do nothing */
1024}
1025
a541c325
RD
1026
1027
1028
1029PyObject* wxPyInputStream::read(int size) {
1030 PyObject* obj = NULL;
1031 wxMemoryBuffer buf;
f74ff5ef
RD
1032 const int BUFSIZE = 1024;
1033
1034 // check if we have a real wxInputStream to work with
1035 if (!m_wxis) {
1e4a197e 1036 wxPyBeginBlockThreads();
f74ff5ef 1037 PyErr_SetString(PyExc_IOError, "no valid C-wxInputStream");
1e4a197e 1038 wxPyEndBlockThreads();
f74ff5ef
RD
1039 return NULL;
1040 }
1041
1042 if (size < 0) {
1e4a197e
RD
1043 // read while bytes are available on the stream
1044 while ( m_wxis->CanRead() ) {
a541c325
RD
1045 m_wxis->Read(buf.GetAppendBuf(BUFSIZE), BUFSIZE);
1046 buf.UngetAppendBuf(m_wxis->LastRead());
f74ff5ef
RD
1047 }
1048
1049 } else { // Read only size number of characters
a541c325
RD
1050 m_wxis->Read(buf.GetWriteBuf(size), size);
1051 buf.UngetWriteBuf(m_wxis->LastRead());
1052 }
f74ff5ef 1053
a541c325 1054 // error check
1e4a197e
RD
1055 wxPyBeginBlockThreads();
1056 wxStreamError err = m_wxis->GetLastError();
1057 if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) {
a541c325
RD
1058 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
1059 }
1060 else {
1061 // We use only strings for the streams, not unicode
1062 obj = PyString_FromStringAndSize(buf, buf.GetDataLen());
f74ff5ef 1063 }
1e4a197e 1064 wxPyEndBlockThreads();
a541c325 1065 return obj;
f74ff5ef
RD
1066}
1067
1068
a541c325
RD
1069PyObject* wxPyInputStream::readline(int size) {
1070 PyObject* obj = NULL;
1071 wxMemoryBuffer buf;
1072 int i;
1073 char ch;
1074
f74ff5ef
RD
1075 // check if we have a real wxInputStream to work with
1076 if (!m_wxis) {
1e4a197e 1077 wxPyBeginBlockThreads();
f74ff5ef 1078 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream");
1e4a197e 1079 wxPyEndBlockThreads();
f74ff5ef
RD
1080 return NULL;
1081 }
1082
f74ff5ef 1083 // read until \n or byte limit reached
1e4a197e 1084 for (i=ch=0; (ch != '\n') && (m_wxis->CanRead()) && ((size < 0) || (i < size)); i++) {
a541c325
RD
1085 ch = m_wxis->GetC();
1086 buf.AppendByte(ch);
f74ff5ef
RD
1087 }
1088
1089 // errorcheck
1e4a197e
RD
1090 wxPyBeginBlockThreads();
1091 wxStreamError err = m_wxis->GetLastError();
1092 if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) {
f74ff5ef 1093 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
f74ff5ef 1094 }
a541c325
RD
1095 else {
1096 // We use only strings for the streams, not unicode
1097 obj = PyString_FromStringAndSize((char*)buf.GetData(), buf.GetDataLen());
1098 }
1e4a197e 1099 wxPyEndBlockThreads();
a541c325 1100 return obj;
f74ff5ef
RD
1101}
1102
1103
a541c325
RD
1104PyObject* wxPyInputStream::readlines(int sizehint) {
1105 PyObject* pylist;
1106
f74ff5ef
RD
1107 // check if we have a real wxInputStream to work with
1108 if (!m_wxis) {
1e4a197e
RD
1109 wxPyBeginBlockThreads();
1110 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream");
1111 wxPyEndBlockThreads();
f74ff5ef
RD
1112 return NULL;
1113 }
1114
1115 // init list
1e4a197e 1116 wxPyBeginBlockThreads();
a541c325
RD
1117 pylist = PyList_New(0);
1118 if (!pylist) {
1e4a197e 1119 wxPyBeginBlockThreads();
f74ff5ef 1120 PyErr_NoMemory();
1e4a197e 1121 wxPyEndBlockThreads();
f74ff5ef
RD
1122 return NULL;
1123 }
1124
1125 // read sizehint bytes or until EOF
1126 int i;
1e4a197e 1127 for (i=0; (m_wxis->CanRead()) && ((sizehint < 0) || (i < sizehint));) {
a541c325 1128 PyObject* s = this->readline();
f74ff5ef 1129 if (s == NULL) {
1e4a197e 1130 wxPyBeginBlockThreads();
a541c325 1131 Py_DECREF(pylist);
1e4a197e 1132 wxPyEndBlockThreads();
f74ff5ef
RD
1133 return NULL;
1134 }
1e4a197e 1135 wxPyBeginBlockThreads();
a541c325
RD
1136 PyList_Append(pylist, s);
1137 i += PyString_Size(s);
1e4a197e 1138 wxPyEndBlockThreads();
f74ff5ef
RD
1139 }
1140
1141 // error check
1e4a197e
RD
1142 wxStreamError err = m_wxis->GetLastError();
1143 if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) {
1144 wxPyBeginBlockThreads();
a541c325 1145 Py_DECREF(pylist);
f74ff5ef 1146 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
1e4a197e 1147 wxPyEndBlockThreads();
f74ff5ef
RD
1148 return NULL;
1149 }
a541c325
RD
1150
1151 return pylist;
f74ff5ef
RD
1152}
1153
1154
1155void wxPyInputStream::seek(int offset, int whence) {
1156 if (m_wxis)
1157 m_wxis->SeekI(offset, wxSeekMode(whence));
1158}
1159
1160int wxPyInputStream::tell(){
1161 if (m_wxis)
1162 return m_wxis->TellI();
1163 else return 0;
1164}
1165
1166
1167
1168
1169wxPyCBInputStream::wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block)
1170 : wxInputStream(), m_read(r), m_seek(s), m_tell(t), m_block(block)
1171{}
1172
1173
1174wxPyCBInputStream::~wxPyCBInputStream() {
1175 if (m_block) wxPyBeginBlockThreads();
1176 Py_XDECREF(m_read);
1177 Py_XDECREF(m_seek);
1178 Py_XDECREF(m_tell);
1179 if (m_block) wxPyEndBlockThreads();
1180}
1181
1182
1183wxPyCBInputStream* wxPyCBInputStream::create(PyObject *py, bool block) {
1184 if (block) wxPyBeginBlockThreads();
1185
1186 PyObject* read = getMethod(py, "read");
1187 PyObject* seek = getMethod(py, "seek");
1188 PyObject* tell = getMethod(py, "tell");
1189
1190 if (!read) {
1191 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
1192 Py_XDECREF(read);
1193 Py_XDECREF(seek);
1194 Py_XDECREF(tell);
1195 if (block) wxPyEndBlockThreads();
1196 return NULL;
1197 }
1198
1199 if (block) wxPyEndBlockThreads();
1200 return new wxPyCBInputStream(read, seek, tell, block);
1201}
1202
249a57f4
RD
1203
1204wxPyCBInputStream* wxPyCBInputStream_create(PyObject *py, bool block) {
1205 return wxPyCBInputStream::create(py, block);
1206}
1207
f74ff5ef
RD
1208PyObject* wxPyCBInputStream::getMethod(PyObject* py, char* name) {
1209 if (!PyObject_HasAttrString(py, name))
1210 return NULL;
1211 PyObject* o = PyObject_GetAttrString(py, name);
1212 if (!PyMethod_Check(o) && !PyCFunction_Check(o)) {
1213 Py_DECREF(o);
1214 return NULL;
1215 }
1216 return o;
1217}
1218
1219
1220size_t wxPyCBInputStream::GetSize() const {
1221 wxPyCBInputStream* self = (wxPyCBInputStream*)this; // cast off const
1222 if (m_seek && m_tell) {
1223 off_t temp = self->OnSysTell();
1224 off_t ret = self->OnSysSeek(0, wxFromEnd);
1225 self->OnSysSeek(temp, wxFromStart);
1226 return ret;
1227 }
1228 else
1229 return 0;
1230}
1231
1232
1233size_t wxPyCBInputStream::OnSysRead(void *buffer, size_t bufsize) {
1234 if (bufsize == 0)
1235 return 0;
1236
1237 wxPyBeginBlockThreads();
1238 PyObject* arglist = Py_BuildValue("(i)", bufsize);
1239 PyObject* result = PyEval_CallObject(m_read, arglist);
1240 Py_DECREF(arglist);
1241
1242 size_t o = 0;
a541c325 1243 if ((result != NULL) && PyString_Check(result)) {
f74ff5ef
RD
1244 o = PyString_Size(result);
1245 if (o == 0)
1246 m_lasterror = wxSTREAM_EOF;
1247 if (o > bufsize)
1248 o = bufsize;
a541c325 1249 memcpy((char*)buffer, PyString_AsString(result), o); // strings only, not unicode...
f74ff5ef
RD
1250 Py_DECREF(result);
1251
1252 }
1253 else
1254 m_lasterror = wxSTREAM_READ_ERROR;
1255 wxPyEndBlockThreads();
f74ff5ef
RD
1256 return o;
1257}
1258
1259size_t wxPyCBInputStream::OnSysWrite(const void *buffer, size_t bufsize) {
1260 m_lasterror = wxSTREAM_WRITE_ERROR;
1261 return 0;
1262}
1263
1264off_t wxPyCBInputStream::OnSysSeek(off_t off, wxSeekMode mode) {
1265 wxPyBeginBlockThreads();
1fded56b
RD
1266#ifdef _LARGE_FILES
1267 // off_t is a 64-bit value...
1268 PyObject* arglist = Py_BuildValue("(Li)", off, mode);
1269#else
f74ff5ef 1270 PyObject* arglist = Py_BuildValue("(ii)", off, mode);
1fded56b 1271#endif
f74ff5ef
RD
1272 PyObject* result = PyEval_CallObject(m_seek, arglist);
1273 Py_DECREF(arglist);
1274 Py_XDECREF(result);
1275 wxPyEndBlockThreads();
1276 return OnSysTell();
1277}
1278
1fded56b 1279
f74ff5ef
RD
1280off_t wxPyCBInputStream::OnSysTell() const {
1281 wxPyBeginBlockThreads();
1282 PyObject* arglist = Py_BuildValue("()");
1283 PyObject* result = PyEval_CallObject(m_tell, arglist);
1284 Py_DECREF(arglist);
1285 off_t o = 0;
1286 if (result != NULL) {
1fded56b
RD
1287#ifdef _LARGE_FILES
1288 if (PyLong_Check(result))
1289 o = PyLong_AsLongLong(result);
1290 else
1fded56b 1291#endif
93b663b8 1292 o = PyInt_AsLong(result);
f74ff5ef
RD
1293 Py_DECREF(result);
1294 };
1295 wxPyEndBlockThreads();
1296 return o;
1297}
1298
1299//----------------------------------------------------------------------
d559219f 1300
2f90df85
RD
1301IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject);
1302
d559219f
RD
1303wxPyCallback::wxPyCallback(PyObject* func) {
1304 m_func = func;
1305 Py_INCREF(m_func);
1306}
1307
2f90df85
RD
1308wxPyCallback::wxPyCallback(const wxPyCallback& other) {
1309 m_func = other.m_func;
1310 Py_INCREF(m_func);
1311}
1312
d559219f 1313wxPyCallback::~wxPyCallback() {
4268f798 1314 wxPyBeginBlockThreads();
d559219f 1315 Py_DECREF(m_func);
4268f798 1316 wxPyEndBlockThreads();
d559219f
RD
1317}
1318
cf694132
RD
1319
1320
7bf85405
RD
1321// This function is used for all events destined for Python event handlers.
1322void wxPyCallback::EventThunker(wxEvent& event) {
1323 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
1324 PyObject* func = cb->m_func;
1325 PyObject* result;
1326 PyObject* arg;
1327 PyObject* tuple;
dd9f7fea 1328 bool checkSkip = False;
cf694132 1329
4268f798 1330 wxPyBeginBlockThreads();
65dd82cb
RD
1331 wxString className = event.GetClassInfo()->GetClassName();
1332
3734a866
RD
1333 // If the event is one of these types then pass the original
1334 // event object instead of the one passed to us.
1335 if ( className == wxT("wxPyEvent") ) {
1336 arg = ((wxPyEvent*)&event)->GetSelf();
1337 checkSkip = ((wxPyEvent*)&event)->GetCloned();
1338 }
1339 else if ( className == wxT("wxPyCommandEvent") ) {
1340 arg = ((wxPyCommandEvent*)&event)->GetSelf();
1341 checkSkip = ((wxPyCommandEvent*)&event)->GetCloned();
1342 }
c8bc7bb8 1343 else {
a541c325 1344 arg = wxPyConstructObject((void*)&event, className);
c8bc7bb8 1345 }
7bf85405 1346
b856768b 1347 if (!arg) {
7bf85405 1348 PyErr_Print();
b856768b
RD
1349 } else {
1350 // Call the event handler, passing the event object
1351 tuple = PyTuple_New(1);
1352 PyTuple_SET_ITEM(tuple, 0, arg); // steals ref to arg
1353 result = PyEval_CallObject(func, tuple);
3734a866 1354 if ( result ) {
b856768b
RD
1355 Py_DECREF(result); // result is ignored, but we still need to decref it
1356 PyErr_Clear(); // Just in case...
3734a866
RD
1357 } else {
1358 PyErr_Print();
1359 }
3734a866 1360
b856768b
RD
1361 if ( checkSkip ) {
1362 // if the event object was one of our special types and
1363 // it had been cloned, then we need to extract the Skipped
1364 // value from the original and set it in the clone.
1365 result = PyObject_CallMethod(arg, "GetSkipped", "");
1366 if ( result ) {
1367 event.Skip(PyInt_AsLong(result));
1368 Py_DECREF(result);
1369 } else {
1370 PyErr_Print();
1371 }
1372 }
1373 Py_DECREF(tuple);
1374 }
4268f798 1375 wxPyEndBlockThreads();
7bf85405
RD
1376}
1377
1378
d559219f 1379//----------------------------------------------------------------------
7bf85405 1380
2f90df85
RD
1381wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) {
1382 m_lastFound = NULL;
1383 m_self = other.m_self;
f6bcfd97
BP
1384 m_class = other.m_class;
1385 if (m_self) {
2f90df85 1386 Py_INCREF(m_self);
f6bcfd97
BP
1387 Py_INCREF(m_class);
1388 }
2f90df85
RD
1389}
1390
1391
33510773 1392void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) {
d559219f 1393 m_self = self;
33510773 1394 m_class = klass;
b7312675 1395 m_incRef = incref;
f6bcfd97 1396 if (incref) {
2f90df85 1397 Py_INCREF(m_self);
f6bcfd97
BP
1398 Py_INCREF(m_class);
1399 }
d559219f 1400}
8bf5d46e 1401
8bf5d46e 1402
78b57918
RD
1403#if PYTHON_API_VERSION >= 1011
1404
1405// Prior to Python 2.2 PyMethod_GetClass returned the class object
1406// in which the method was defined. Starting with 2.2 it returns
1407// "class that asked for the method" which seems totally bogus to me
4152e8b9 1408// but apprently it fixes some obscure problem waiting to happen in
78b57918
RD
1409// Python. Since the API was not documented Guido and the gang felt
1410// safe in changing it. Needless to say that totally screwed up the
1411// logic below in wxPyCallbackHelper::findCallback, hence this icky
4152e8b9 1412// code to find the class where the method is actually defined...
78b57918
RD
1413
1414static
1415PyObject* PyFindClassWithAttr(PyObject *klass, PyObject *name)
1416{
1417 int i, n;
1418
1419 if (PyType_Check(klass)) { // new style classes
1420 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1421 PyTypeObject* type = (PyTypeObject*)klass;
1422 PyObject *mro, *res, *base, *dict;
1423 /* Look in tp_dict of types in MRO */
1424 mro = type->tp_mro;
1425 assert(PyTuple_Check(mro));
1426 n = PyTuple_GET_SIZE(mro);
1427 for (i = 0; i < n; i++) {
1428 base = PyTuple_GET_ITEM(mro, i);
1429 if (PyClass_Check(base))
1430 dict = ((PyClassObject *)base)->cl_dict;
1431 else {
1432 assert(PyType_Check(base));
1433 dict = ((PyTypeObject *)base)->tp_dict;
1434 }
1435 assert(dict && PyDict_Check(dict));
1436 res = PyDict_GetItem(dict, name);
1437 if (res != NULL)
4a98c806 1438 return base;
78b57918
RD
1439 }
1440 return NULL;
1441 }
1442
1443 else if (PyClass_Check(klass)) { // old style classes
1444 // This code is borrowed/adapted from class_lookup in classobject.c
1445 PyClassObject* cp = (PyClassObject*)klass;
1446 PyObject *value = PyDict_GetItem(cp->cl_dict, name);
1447 if (value != NULL) {
1448 return (PyObject*)cp;
1449 }
1450 n = PyTuple_Size(cp->cl_bases);
1451 for (i = 0; i < n; i++) {
1452 PyObject* base = PyTuple_GetItem(cp->cl_bases, i);
1453 PyObject *v = PyFindClassWithAttr(base, name);
1454 if (v != NULL)
1455 return v;
1456 }
1457 return NULL;
1458 }
4152e8b9 1459 return NULL;
78b57918
RD
1460}
1461#endif
1462
1463
1464static
1465PyObject* PyMethod_GetDefiningClass(PyObject* method, const char* name)
1466{
1467 PyObject* mgc = PyMethod_GET_CLASS(method);
1468
1469#if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1470 return mgc;
1471#else // 2.2 and after, the hard way...
1472
1473 PyObject* nameo = PyString_FromString(name);
1474 PyObject* klass = PyFindClassWithAttr(mgc, nameo);
1475 Py_DECREF(nameo);
1476 return klass;
1477#endif
1478}
1479
1480
1481
de20db99 1482bool wxPyCallbackHelper::findCallback(const char* name) const {
f6bcfd97
BP
1483 wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const
1484 self->m_lastFound = NULL;
78b57918
RD
1485
1486 // If the object (m_self) has an attibute of the given name...
de20db99 1487 if (m_self && PyObject_HasAttrString(m_self, (char*)name)) {
78b57918 1488 PyObject *method, *klass;
de20db99 1489 method = PyObject_GetAttrString(m_self, (char*)name);
f6bcfd97 1490
78b57918
RD
1491 // ...and if that attribute is a method, and if that method's class is
1492 // not from a base class...
f6bcfd97 1493 if (PyMethod_Check(method) &&
78b57918 1494 (klass = PyMethod_GetDefiningClass(method, (char*)name)) != NULL &&
d14a1e28 1495 ((klass == m_class) || PyObject_IsSubclass(klass, m_class))) {
8bf5d46e 1496
78b57918 1497 // ...then we'll save a pointer to the method so callCallback can call it.
f6bcfd97
BP
1498 self->m_lastFound = method;
1499 }
de20db99
RD
1500 else {
1501 Py_DECREF(method);
1502 }
f6bcfd97 1503 }
d559219f
RD
1504 return m_lastFound != NULL;
1505}
8bf5d46e 1506
d559219f 1507
f6bcfd97 1508int wxPyCallbackHelper::callCallback(PyObject* argTuple) const {
d559219f 1509 PyObject* result;
dd9f7fea 1510 int retval = False;
d559219f
RD
1511
1512 result = callCallbackObj(argTuple);
1513 if (result) { // Assumes an integer return type...
1514 retval = PyInt_AsLong(result);
1515 Py_DECREF(result);
1516 PyErr_Clear(); // forget about it if it's not...
1517 }
1518 return retval;
1519}
1520
1521// Invoke the Python callable object, returning the raw PyObject return
f048f829 1522// value. Caller should DECREF the return value and also manage the GIL.
f6bcfd97 1523PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const {
de20db99 1524 PyObject* result;
d559219f 1525
de20db99
RD
1526 // Save a copy of the pointer in case the callback generates another
1527 // callback. In that case m_lastFound will have a different value when
1528 // it gets back here...
1529 PyObject* method = m_lastFound;
1530
1531 result = PyEval_CallObject(method, argTuple);
d559219f 1532 Py_DECREF(argTuple);
de20db99 1533 Py_DECREF(method);
d559219f
RD
1534 if (!result) {
1535 PyErr_Print();
1536 }
1537 return result;
1538}
714e6a9e 1539
7bf85405 1540
0122b7e3 1541void wxPyCBH_setCallbackInfo(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) {
1e7ecb7b
RD
1542 cbh.setSelf(self, klass, incref);
1543}
1544
1545bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) {
1546 return cbh.findCallback(name);
1547}
1548
1549int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
1550 return cbh.callCallback(argTuple);
1551}
1552
1553PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
1554 return cbh.callCallbackObj(argTuple);
1555}
1556
1557
1558void wxPyCBH_delete(wxPyCallbackHelper* cbh) {
1e7ecb7b 1559 if (cbh->m_incRef) {
4268f798 1560 wxPyBeginBlockThreads();
1e7ecb7b
RD
1561 Py_XDECREF(cbh->m_self);
1562 Py_XDECREF(cbh->m_class);
4268f798 1563 wxPyEndBlockThreads();
1e7ecb7b 1564 }
1e7ecb7b 1565}
d559219f 1566
65dd82cb
RD
1567//---------------------------------------------------------------------------
1568//---------------------------------------------------------------------------
2aab8f16 1569// These event classes can be derived from in Python and passed through the event
c368d904 1570// system without losing anything. They do this by keeping a reference to
65dd82cb
RD
1571// themselves and some special case handling in wxPyCallback::EventThunker.
1572
1573
e19b7164 1574wxPyEvtSelfRef::wxPyEvtSelfRef() {
65dd82cb
RD
1575 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1576 //Py_INCREF(m_self); // circular loops...
dd9f7fea 1577 m_cloned = False;
65dd82cb
RD
1578}
1579
e19b7164 1580wxPyEvtSelfRef::~wxPyEvtSelfRef() {
4268f798 1581 wxPyBeginBlockThreads();
65dd82cb
RD
1582 if (m_cloned)
1583 Py_DECREF(m_self);
4268f798 1584 wxPyEndBlockThreads();
65dd82cb
RD
1585}
1586
e19b7164 1587void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) {
4268f798 1588 wxPyBeginBlockThreads();
65dd82cb
RD
1589 if (m_cloned)
1590 Py_DECREF(m_self);
1591 m_self = self;
1592 if (clone) {
1593 Py_INCREF(m_self);
dd9f7fea 1594 m_cloned = True;
65dd82cb 1595 }
4268f798 1596 wxPyEndBlockThreads();
65dd82cb
RD
1597}
1598
e19b7164 1599PyObject* wxPyEvtSelfRef::GetSelf() const {
65dd82cb
RD
1600 Py_INCREF(m_self);
1601 return m_self;
1602}
1603
1604
07b2e1cd
RD
1605IMPLEMENT_ABSTRACT_CLASS(wxPyEvent, wxEvent);
1606IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent, wxCommandEvent);
1607
1608
3ef86e32
RD
1609wxPyEvent::wxPyEvent(int winid, wxEventType commandType)
1610 : wxEvent(winid, commandType) {
65dd82cb
RD
1611}
1612
65dd82cb 1613
07b2e1cd
RD
1614wxPyEvent::wxPyEvent(const wxPyEvent& evt)
1615 : wxEvent(evt)
1616{
dd9f7fea 1617 SetSelf(evt.m_self, True);
65dd82cb
RD
1618}
1619
1620
07b2e1cd
RD
1621wxPyEvent::~wxPyEvent() {
1622}
65dd82cb
RD
1623
1624
1625wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id)
1626 : wxCommandEvent(commandType, id) {
1627}
1628
65dd82cb 1629
07b2e1cd
RD
1630wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent& evt)
1631 : wxCommandEvent(evt)
1632{
dd9f7fea 1633 SetSelf(evt.m_self, True);
65dd82cb
RD
1634}
1635
1636
07b2e1cd
RD
1637wxPyCommandEvent::~wxPyCommandEvent() {
1638}
1639
65dd82cb
RD
1640
1641
1642
cf694132 1643
2f90df85 1644//---------------------------------------------------------------------------
389c5527 1645//---------------------------------------------------------------------------
d14a1e28 1646// Convert a wxList to a Python List, only works for lists of wxObjects
389c5527 1647
d14a1e28 1648PyObject* wxPy_ConvertList(wxListBase* listbase) {
1e4a197e 1649 wxList* list = (wxList*)listbase; // this is probably bad...
389c5527
RD
1650 PyObject* pyList;
1651 PyObject* pyObj;
1652 wxObject* wxObj;
1e4a197e 1653 wxNode* node = list->GetFirst();
389c5527 1654
4268f798 1655 wxPyBeginBlockThreads();
389c5527
RD
1656 pyList = PyList_New(0);
1657 while (node) {
1e4a197e 1658 wxObj = node->GetData();
d14a1e28 1659 pyObj = wxPyMake_wxObject(wxObj);
389c5527 1660 PyList_Append(pyList, pyObj);
1e4a197e 1661 node = node->GetNext();
389c5527 1662 }
4268f798 1663 wxPyEndBlockThreads();
389c5527
RD
1664 return pyList;
1665}
1666
54b96882
RD
1667//----------------------------------------------------------------------
1668
1669long wxPyGetWinHandle(wxWindow* win) {
1670#ifdef __WXMSW__
1671 return (long)win->GetHandle();
1672#endif
1673
1674 // Find and return the actual X-Window.
1675#ifdef __WXGTK__
1676 if (win->m_wxwindow) {
1e4a197e
RD
1677#ifdef __WXGTK20__
1678 return (long) GDK_WINDOW_XWINDOW(GTK_PIZZA(win->m_wxwindow)->bin_window);
1679#else
54b96882
RD
1680 GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window;
1681 if (bwin) {
1682 return (long)bwin->xwindow;
1683 }
1e4a197e 1684#endif
54b96882
RD
1685 }
1686#endif
1687 return 0;
1688}
1689
7bf85405
RD
1690//----------------------------------------------------------------------
1691// Some helper functions for typemaps in my_typemaps.i, so they won't be
c8bc7bb8
RD
1692// included in every file over and over again...
1693
1694#if PYTHON_API_VERSION >= 1009
1695 static char* wxStringErrorMsg = "String or Unicode type required";
1696#else
1697 static char* wxStringErrorMsg = "String type required";
1698#endif
1699
1700
1701wxString* wxString_in_helper(PyObject* source) {
1702 wxString* target;
1703#if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1704 if (!PyString_Check(source) && !PyUnicode_Check(source)) {
1705 PyErr_SetString(PyExc_TypeError, wxStringErrorMsg);
1706 return NULL;
1707 }
1708#if wxUSE_UNICODE
1709 if (PyUnicode_Check(source)) {
1e4a197e
RD
1710 target = new wxString();
1711 size_t len = PyUnicode_GET_SIZE(source);
1712 if (len) {
1713 PyUnicode_AsWideChar((PyUnicodeObject*)source, target->GetWriteBuf(len), len);
1714 target->UngetWriteBuf();
1715 }
c8bc7bb8 1716 } else {
a541c325
RD
1717 // It is a string, get pointers to it and transform to unicode
1718 char* tmpPtr; int tmpSize;
1719 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1720 target = new wxString(tmpPtr, *wxConvCurrent, tmpSize);
c8bc7bb8
RD
1721 }
1722#else
1723 char* tmpPtr; int tmpSize;
1724 if (PyString_AsStringAndSize(source, &tmpPtr, &tmpSize) == -1) {
1725 PyErr_SetString(PyExc_TypeError, "Unable to convert string");
1726 return NULL;
1727 }
1728 target = new wxString(tmpPtr, tmpSize);
1729#endif // wxUSE_UNICODE
1730
1731#else // No Python unicode API (1.5.2)
1732 if (!PyString_Check(source)) {
1733 PyErr_SetString(PyExc_TypeError, wxStringErrorMsg);
1734 return NULL;
1735 }
1736 target = new wxString(PyString_AS_STRING(source), PyString_GET_SIZE(source));
1737#endif
1738 return target;
1739}
1740
7bf85405 1741
a541c325
RD
1742// Similar to above except doesn't use "new" and doesn't set an exception
1743wxString Py2wxString(PyObject* source)
1744{
1745 wxString target;
dd9f7fea 1746 bool doDecRef = False;
a541c325
RD
1747
1748#if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1749 if (!PyString_Check(source) && !PyUnicode_Check(source)) {
1750 // Convert to String if not one already... (TODO: Unicode too?)
1751 source = PyObject_Str(source);
dd9f7fea 1752 doDecRef = True;
a541c325
RD
1753 }
1754
1755#if wxUSE_UNICODE
1756 if (PyUnicode_Check(source)) {
1e4a197e
RD
1757 size_t len = PyUnicode_GET_SIZE(source);
1758 if (len) {
1759 PyUnicode_AsWideChar((PyUnicodeObject*)source, target.GetWriteBuf(len), len);
1760 target.UngetWriteBuf();
1761 }
a541c325
RD
1762 } else {
1763 // It is a string, get pointers to it and transform to unicode
1764 char* tmpPtr; int tmpSize;
1765 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1766 target = wxString(tmpPtr, *wxConvCurrent, tmpSize);
1767 }
1768#else
1769 char* tmpPtr; int tmpSize;
1770 PyString_AsStringAndSize(source, &tmpPtr, &tmpSize);
1771 target = wxString(tmpPtr, tmpSize);
1772#endif // wxUSE_UNICODE
1773
1774#else // No Python unicode API (1.5.2)
1775 if (!PyString_Check(source)) {
1776 // Convert to String if not one already...
1777 source = PyObject_Str(source);
dd9f7fea 1778 doDecRef = True;
a541c325
RD
1779 }
1780 target = wxString(PyString_AS_STRING(source), PyString_GET_SIZE(source));
1781#endif
1782
1783 if (doDecRef)
1784 Py_DECREF(source);
1785 return target;
1786}
1787
1788
1789// Make either a Python String or Unicode object, depending on build mode
1790PyObject* wx2PyString(const wxString& src)
1791{
1792 PyObject* str;
1793#if wxUSE_UNICODE
1e4a197e 1794 str = PyUnicode_FromWideChar(src.c_str(), src.Len());
a541c325
RD
1795#else
1796 str = PyString_FromStringAndSize(src.c_str(), src.Len());
1797#endif
1798 return str;
1799}
1800
1801
1802//----------------------------------------------------------------------
1803
7bf85405 1804
2f90df85 1805byte* byte_LIST_helper(PyObject* source) {
b639c3c5
RD
1806 if (!PyList_Check(source)) {
1807 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1808 return NULL;
1809 }
1810 int count = PyList_Size(source);
1811 byte* temp = new byte[count];
1812 if (! temp) {
1813 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1814 return NULL;
1815 }
1816 for (int x=0; x<count; x++) {
1817 PyObject* o = PyList_GetItem(source, x);
1818 if (! PyInt_Check(o)) {
1819 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1820 return NULL;
1821 }
1822 temp[x] = (byte)PyInt_AsLong(o);
1823 }
1824 return temp;
1825}
1826
1827
2f90df85 1828int* int_LIST_helper(PyObject* source) {
7bf85405
RD
1829 if (!PyList_Check(source)) {
1830 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1831 return NULL;
1832 }
1833 int count = PyList_Size(source);
1834 int* temp = new int[count];
1835 if (! temp) {
1836 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1837 return NULL;
1838 }
1839 for (int x=0; x<count; x++) {
1840 PyObject* o = PyList_GetItem(source, x);
1841 if (! PyInt_Check(o)) {
1842 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1843 return NULL;
1844 }
1845 temp[x] = PyInt_AsLong(o);
1846 }
1847 return temp;
1848}
1849
1850
2f90df85 1851long* long_LIST_helper(PyObject* source) {
7bf85405
RD
1852 if (!PyList_Check(source)) {
1853 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1854 return NULL;
1855 }
1856 int count = PyList_Size(source);
1857 long* temp = new long[count];
1858 if (! temp) {
1859 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1860 return NULL;
1861 }
1862 for (int x=0; x<count; x++) {
1863 PyObject* o = PyList_GetItem(source, x);
1864 if (! PyInt_Check(o)) {
1865 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
1866 return NULL;
1867 }
1868 temp[x] = PyInt_AsLong(o);
1869 }
1870 return temp;
1871}
1872
1873
2f90df85 1874char** string_LIST_helper(PyObject* source) {
7bf85405
RD
1875 if (!PyList_Check(source)) {
1876 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1877 return NULL;
1878 }
1879 int count = PyList_Size(source);
1880 char** temp = new char*[count];
1881 if (! temp) {
1882 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1883 return NULL;
1884 }
1885 for (int x=0; x<count; x++) {
1886 PyObject* o = PyList_GetItem(source, x);
1887 if (! PyString_Check(o)) {
1888 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
1889 return NULL;
1890 }
1891 temp[x] = PyString_AsString(o);
1892 }
1893 return temp;
1894}
1895
e0672e2f
RD
1896//--------------------------------
1897// Part of patch from Tim Hochberg
1898static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) {
1899 if (PyInt_Check(o1) && PyInt_Check(o2)) {
1900 point->x = PyInt_AS_LONG(o1);
1901 point->y = PyInt_AS_LONG(o2);
dd9f7fea 1902 return True;
e0672e2f
RD
1903 }
1904 if (PyFloat_Check(o1) && PyFloat_Check(o2)) {
1905 point->x = (int)PyFloat_AS_DOUBLE(o1);
1906 point->y = (int)PyFloat_AS_DOUBLE(o2);
dd9f7fea 1907 return True;
e0672e2f 1908 }
d14a1e28 1909 if (wxPySwigInstance_Check(o1) || wxPySwigInstance_Check(o2)) { // TODO: Why???
e0672e2f 1910 // Disallow instances because they can cause havok
dd9f7fea 1911 return False;
e0672e2f
RD
1912 }
1913 if (PyNumber_Check(o1) && PyNumber_Check(o2)) {
1914 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1915 point->x = PyInt_AsLong(o1);
1916 point->y = PyInt_AsLong(o2);
dd9f7fea 1917 return True;
e0672e2f 1918 }
dd9f7fea 1919 return False;
e0672e2f 1920}
7bf85405
RD
1921
1922
e0672e2f
RD
1923wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) {
1924 // Putting all of the declarations here allows
1925 // us to put the error handling all in one place.
1926 int x;
1927 wxPoint* temp;
1928 PyObject *o, *o1, *o2;
9d37f964 1929 bool isFast = PyList_Check(source) || PyTuple_Check(source);
e0672e2f 1930
e0672e2f
RD
1931 if (!PySequence_Check(source)) {
1932 goto error0;
7bf85405 1933 }
9d37f964
RD
1934
1935 // The length of the sequence is returned in count.
e0672e2f
RD
1936 *count = PySequence_Length(source);
1937 if (*count < 0) {
1938 goto error0;
1939 }
1940
1941 temp = new wxPoint[*count];
1942 if (!temp) {
7bf85405
RD
1943 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1944 return NULL;
1945 }
e0672e2f
RD
1946 for (x=0; x<*count; x++) {
1947 // Get an item: try fast way first.
1948 if (isFast) {
1949 o = PySequence_Fast_GET_ITEM(source, x);
1950 }
1951 else {
1952 o = PySequence_GetItem(source, x);
1953 if (o == NULL) {
1954 goto error1;
1955 }
1956 }
7bf85405 1957
e0672e2f
RD
1958 // Convert o to wxPoint.
1959 if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) ||
1960 (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) {
1961 o1 = PySequence_Fast_GET_ITEM(o, 0);
1962 o2 = PySequence_Fast_GET_ITEM(o, 1);
1963 if (!wxPointFromObjects(o1, o2, &temp[x])) {
1964 goto error2;
1965 }
7bf85405 1966 }
d14a1e28 1967 else if (wxPySwigInstance_Check(o)) {
d559219f 1968 wxPoint* pt;
d14a1e28 1969 if (! wxPyConvertSwigPtr(o, (void **)&pt, wxT("wxPoint"))) {
e0672e2f 1970 goto error2;
d559219f
RD
1971 }
1972 temp[x] = *pt;
1973 }
e0672e2f
RD
1974 else if (PySequence_Check(o) && PySequence_Length(o) == 2) {
1975 o1 = PySequence_GetItem(o, 0);
1976 o2 = PySequence_GetItem(o, 1);
1977 if (!wxPointFromObjects(o1, o2, &temp[x])) {
1978 goto error3;
1979 }
1980 Py_DECREF(o1);
1981 Py_DECREF(o2);
1982 }
7bf85405 1983 else {
e0672e2f 1984 goto error2;
7bf85405 1985 }
e0672e2f
RD
1986 // Clean up.
1987 if (!isFast)
1988 Py_DECREF(o);
7bf85405
RD
1989 }
1990 return temp;
e0672e2f
RD
1991
1992error3:
1993 Py_DECREF(o1);
1994 Py_DECREF(o2);
1995error2:
1996 if (!isFast)
1997 Py_DECREF(o);
1998error1:
1e4a197e 1999 delete [] temp;
e0672e2f
RD
2000error0:
2001 PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints.");
2002 return NULL;
7bf85405 2003}
e0672e2f
RD
2004// end of patch
2005//------------------------------
7bf85405
RD
2006
2007
2f90df85 2008wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
7bf85405
RD
2009 if (!PyList_Check(source)) {
2010 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2011 return NULL;
2012 }
2013 int count = PyList_Size(source);
2014 wxBitmap** temp = new wxBitmap*[count];
2015 if (! temp) {
2016 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2017 return NULL;
2018 }
2019 for (int x=0; x<count; x++) {
2020 PyObject* o = PyList_GetItem(source, x);
d14a1e28 2021 if (wxPySwigInstance_Check(o)) {
7bf85405 2022 wxBitmap* pt;
d14a1e28
RD
2023 if (! wxPyConvertSwigPtr(o, (void **) &pt, wxT("wxBitmap"))) {
2024 PyErr_SetString(PyExc_TypeError,"Expected wxBitmap.");
7bf85405
RD
2025 return NULL;
2026 }
2027 temp[x] = pt;
2028 }
2029 else {
2030 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
2031 return NULL;
2032 }
2033 }
2034 return temp;
2035}
2036
2037
2038
2f90df85 2039wxString* wxString_LIST_helper(PyObject* source) {
7bf85405
RD
2040 if (!PyList_Check(source)) {
2041 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2042 return NULL;
2043 }
2044 int count = PyList_Size(source);
2045 wxString* temp = new wxString[count];
2046 if (! temp) {
2047 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2048 return NULL;
2049 }
2050 for (int x=0; x<count; x++) {
2051 PyObject* o = PyList_GetItem(source, x);
ecc08ead
RD
2052#if PYTHON_API_VERSION >= 1009
2053 if (! PyString_Check(o) && ! PyUnicode_Check(o)) {
2054 PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects.");
2055 return NULL;
2056 }
ecc08ead 2057#else
7bf85405
RD
2058 if (! PyString_Check(o)) {
2059 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
2060 return NULL;
2061 }
ecc08ead 2062#endif
a541c325
RD
2063
2064 wxString* pStr = wxString_in_helper(o);
2065 temp[x] = *pStr;
2066 delete pStr;
7bf85405
RD
2067 }
2068 return temp;
2069}
2070
2071
2f90df85 2072wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
7bf85405
RD
2073 if (!PyList_Check(source)) {
2074 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2075 return NULL;
2076 }
2077 int count = PyList_Size(source);
2078 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
2079 if (! temp) {
2080 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2081 return NULL;
2082 }
2083 for (int x=0; x<count; x++) {
2084 PyObject* o = PyList_GetItem(source, x);
d14a1e28 2085 if (wxPySwigInstance_Check(o)) {
7bf85405 2086 wxAcceleratorEntry* ae;
d14a1e28
RD
2087 if (! wxPyConvertSwigPtr(o, (void **) &ae, wxT("wxAcceleratorEntry"))) {
2088 PyErr_SetString(PyExc_TypeError,"Expected wxAcceleratorEntry.");
7bf85405
RD
2089 return NULL;
2090 }
2091 temp[x] = *ae;
2092 }
2093 else if (PyTuple_Check(o)) {
2094 PyObject* o1 = PyTuple_GetItem(o, 0);
2095 PyObject* o2 = PyTuple_GetItem(o, 1);
2096 PyObject* o3 = PyTuple_GetItem(o, 2);
0adbc166 2097 temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
7bf85405
RD
2098 }
2099 else {
2100 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
2101 return NULL;
2102 }
2103 }
2104 return temp;
2105}
2106
bb0054cd 2107
9d37f964
RD
2108wxPen** wxPen_LIST_helper(PyObject* source) {
2109 if (!PyList_Check(source)) {
2110 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
2111 return NULL;
2112 }
2113 int count = PyList_Size(source);
2114 wxPen** temp = new wxPen*[count];
2115 if (!temp) {
2116 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
2117 return NULL;
2118 }
2119 for (int x=0; x<count; x++) {
2120 PyObject* o = PyList_GetItem(source, x);
d14a1e28 2121 if (wxPySwigInstance_Check(o)) {
9d37f964 2122 wxPen* pt;
d14a1e28 2123 if (! wxPyConvertSwigPtr(o, (void **)&pt, wxT("wxPen"))) {
9d37f964 2124 delete temp;
d14a1e28 2125 PyErr_SetString(PyExc_TypeError,"Expected wxPen.");
9d37f964
RD
2126 return NULL;
2127 }
2128 temp[x] = pt;
2129 }
2130 else {
2131 delete temp;
2132 PyErr_SetString(PyExc_TypeError, "Expected a list of wxPens.");
2133 return NULL;
2134 }
2135 }
2136 return temp;
2137}
2138
2139
1e4a197e 2140bool wxPy2int_seq_helper(PyObject* source, int* i1, int* i2) {
9d37f964
RD
2141 bool isFast = PyList_Check(source) || PyTuple_Check(source);
2142 PyObject *o1, *o2;
2143
2144 if (!PySequence_Check(source) || PySequence_Length(source) != 2)
dd9f7fea 2145 return False;
9d37f964
RD
2146
2147 if (isFast) {
2148 o1 = PySequence_Fast_GET_ITEM(source, 0);
2149 o2 = PySequence_Fast_GET_ITEM(source, 1);
2150 }
2151 else {
2152 o1 = PySequence_GetItem(source, 0);
2153 o2 = PySequence_GetItem(source, 1);
2154 }
2155
2156 *i1 = PyInt_AsLong(o1);
2157 *i2 = PyInt_AsLong(o2);
2158
2159 if (! isFast) {
2160 Py_DECREF(o1);
2161 Py_DECREF(o2);
2162 }
dd9f7fea 2163 return True;
9d37f964
RD
2164}
2165
2166
1e4a197e 2167bool wxPy4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4) {
9d37f964
RD
2168 bool isFast = PyList_Check(source) || PyTuple_Check(source);
2169 PyObject *o1, *o2, *o3, *o4;
2170
2171 if (!PySequence_Check(source) || PySequence_Length(source) != 4)
dd9f7fea 2172 return False;
9d37f964
RD
2173
2174 if (isFast) {
2175 o1 = PySequence_Fast_GET_ITEM(source, 0);
2176 o2 = PySequence_Fast_GET_ITEM(source, 1);
2177 o3 = PySequence_Fast_GET_ITEM(source, 2);
2178 o4 = PySequence_Fast_GET_ITEM(source, 3);
2179 }
2180 else {
2181 o1 = PySequence_GetItem(source, 0);
2182 o2 = PySequence_GetItem(source, 1);
2183 o3 = PySequence_GetItem(source, 2);
2184 o4 = PySequence_GetItem(source, 3);
2185 }
2186
2187 *i1 = PyInt_AsLong(o1);
2188 *i2 = PyInt_AsLong(o2);
2189 *i3 = PyInt_AsLong(o3);
2190 *i4 = PyInt_AsLong(o4);
2191
2192 if (! isFast) {
2193 Py_DECREF(o1);
2194 Py_DECREF(o2);
2195 Py_DECREF(o3);
2196 Py_DECREF(o4);
2197 }
dd9f7fea 2198 return True;
9d37f964
RD
2199}
2200
bb0054cd
RD
2201
2202//----------------------------------------------------------------------
bb0054cd 2203
d14a1e28
RD
2204bool wxPySimple_typecheck(PyObject* source, const wxChar* classname, int seqLen)
2205{
2206 void* ptr;
2207
2208 if (wxPySwigInstance_Check(source) &&
2209 wxPyConvertSwigPtr(source, (void **)&ptr, classname))
dd9f7fea 2210 return True;
2f90df85 2211
d14a1e28
RD
2212 PyErr_Clear();
2213 if (PySequence_Check(source) && PySequence_Length(source) == seqLen)
dd9f7fea 2214 return True;
d14a1e28 2215
dd9f7fea 2216 return False;
d14a1e28 2217}
2f90df85 2218
d14a1e28
RD
2219bool wxSize_helper(PyObject* source, wxSize** obj)
2220{
2221 return wxPyTwoIntItem_helper(source, obj, wxT("wxSize"));
2f90df85
RD
2222}
2223
1e4a197e 2224
d14a1e28
RD
2225bool wxPoint_helper(PyObject* source, wxPoint** obj)
2226{
2227 return wxPyTwoIntItem_helper(source, obj, wxT("wxPoint"));
2f90df85
RD
2228}
2229
2230
2231
2232bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) {
2233
2234 // If source is an object instance then it may already be the right type
d14a1e28 2235 if (wxPySwigInstance_Check(source)) {
2f90df85 2236 wxRealPoint* ptr;
d14a1e28 2237 if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxRealPoint")))
2f90df85
RD
2238 goto error;
2239 *obj = ptr;
dd9f7fea 2240 return True;
2f90df85
RD
2241 }
2242 // otherwise a 2-tuple of floats is expected
2243 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
2244 PyObject* o1 = PySequence_GetItem(source, 0);
2245 PyObject* o2 = PySequence_GetItem(source, 1);
db0ff83e
RD
2246 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
2247 Py_DECREF(o1);
2248 Py_DECREF(o2);
2249 goto error;
2250 }
2f90df85 2251 **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
db0ff83e
RD
2252 Py_DECREF(o1);
2253 Py_DECREF(o2);
dd9f7fea 2254 return True;
2f90df85
RD
2255 }
2256
2257 error:
2258 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object.");
dd9f7fea 2259 return False;
2f90df85
RD
2260}
2261
2262
2263
2f90df85
RD
2264bool wxRect_helper(PyObject* source, wxRect** obj) {
2265
2266 // If source is an object instance then it may already be the right type
d14a1e28 2267 if (wxPySwigInstance_Check(source)) {
2f90df85 2268 wxRect* ptr;
d14a1e28 2269 if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxRect")))
2f90df85
RD
2270 goto error;
2271 *obj = ptr;
dd9f7fea 2272 return True;
2f90df85
RD
2273 }
2274 // otherwise a 4-tuple of integers is expected
2275 else if (PySequence_Check(source) && PyObject_Length(source) == 4) {
2276 PyObject* o1 = PySequence_GetItem(source, 0);
2277 PyObject* o2 = PySequence_GetItem(source, 1);
2278 PyObject* o3 = PySequence_GetItem(source, 2);
2279 PyObject* o4 = PySequence_GetItem(source, 3);
db0ff83e
RD
2280 if (!PyNumber_Check(o1) || !PyNumber_Check(o2) ||
2281 !PyNumber_Check(o3) || !PyNumber_Check(o4)) {
2282 Py_DECREF(o1);
2283 Py_DECREF(o2);
2284 Py_DECREF(o3);
2285 Py_DECREF(o4);
2286 goto error;
2287 }
2f90df85 2288 **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2),
db0ff83e
RD
2289 PyInt_AsLong(o3), PyInt_AsLong(o4));
2290 Py_DECREF(o1);
2291 Py_DECREF(o2);
2292 Py_DECREF(o3);
2293 Py_DECREF(o4);
dd9f7fea 2294 return True;
2f90df85
RD
2295 }
2296
2297 error:
2298 PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object.");
dd9f7fea 2299 return False;
2f90df85
RD
2300}
2301
2302
2303
f6bcfd97
BP
2304bool wxColour_helper(PyObject* source, wxColour** obj) {
2305
2306 // If source is an object instance then it may already be the right type
d14a1e28 2307 if (wxPySwigInstance_Check(source)) {
f6bcfd97 2308 wxColour* ptr;
d14a1e28 2309 if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxColour")))
f6bcfd97
BP
2310 goto error;
2311 *obj = ptr;
dd9f7fea 2312 return True;
f6bcfd97 2313 }
1e4a197e
RD
2314 // otherwise check for a string
2315 else if (PyString_Check(source) || PyUnicode_Check(source)) {
2316 wxString spec = Py2wxString(source);
a541c325
RD
2317 if (spec.GetChar(0) == '#' && spec.Length() == 7) { // It's #RRGGBB
2318 long red, green, blue;
2319 red = green = blue = 0;
a541c325
RD
2320 spec.Mid(1,2).ToLong(&red, 16);
2321 spec.Mid(3,2).ToLong(&green, 16);
2322 spec.Mid(5,2).ToLong(&blue, 16);
2323
f6bcfd97 2324 **obj = wxColour(red, green, blue);
dd9f7fea 2325 return True;
f6bcfd97
BP
2326 }
2327 else { // it's a colour name
2328 **obj = wxColour(spec);
dd9f7fea 2329 return True;
f6bcfd97
BP
2330 }
2331 }
1e4a197e
RD
2332 // last chance: 3-tuple of integers is expected
2333 else if (PySequence_Check(source) && PyObject_Length(source) == 3) {
2334 PyObject* o1 = PySequence_GetItem(source, 0);
2335 PyObject* o2 = PySequence_GetItem(source, 1);
2336 PyObject* o3 = PySequence_GetItem(source, 2);
2337 if (!PyNumber_Check(o1) || !PyNumber_Check(o2) || !PyNumber_Check(o3)) {
2338 Py_DECREF(o1);
2339 Py_DECREF(o2);
2340 Py_DECREF(o3);
2341 goto error;
2342 }
2343 **obj = wxColour(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
2344 Py_DECREF(o1);
2345 Py_DECREF(o2);
2346 Py_DECREF(o3);
dd9f7fea 2347 return True;
1e4a197e 2348 }
f6bcfd97
BP
2349
2350 error:
a541c325 2351 PyErr_SetString(PyExc_TypeError,
1e4a197e 2352 "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
dd9f7fea 2353 return False;
1e4a197e
RD
2354}
2355
2356
d14a1e28
RD
2357bool wxColour_typecheck(PyObject* source) {
2358
2359 if (wxPySimple_typecheck(source, wxT("wxColour"), 3))
dd9f7fea 2360 return True;
d14a1e28
RD
2361
2362 if (PyString_Check(source) || PyUnicode_Check(source))
dd9f7fea 2363 return True;
d14a1e28 2364
dd9f7fea 2365 return False;
d14a1e28
RD
2366}
2367
2368
1e4a197e 2369
d14a1e28 2370bool wxPoint2D_helper(PyObject* source, wxPoint2D** obj) {
1e4a197e 2371 // If source is an object instance then it may already be the right type
d14a1e28
RD
2372 if (wxPySwigInstance_Check(source)) {
2373 wxPoint2D* ptr;
2374 if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxPoint2D")))
1e4a197e
RD
2375 goto error;
2376 *obj = ptr;
dd9f7fea 2377 return True;
1e4a197e
RD
2378 }
2379 // otherwise a length-2 sequence of floats is expected
2380 if (PySequence_Check(source) && PySequence_Length(source) == 2) {
2381 PyObject* o1 = PySequence_GetItem(source, 0);
2382 PyObject* o2 = PySequence_GetItem(source, 1);
d14a1e28 2383 // This should really check for floats, not numbers -- but that would break code.
1e4a197e
RD
2384 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
2385 Py_DECREF(o1);
2386 Py_DECREF(o2);
2387 goto error;
2388 }
d14a1e28 2389 **obj = wxPoint2D(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
1e4a197e
RD
2390 Py_DECREF(o1);
2391 Py_DECREF(o2);
dd9f7fea 2392 return True;
1e4a197e
RD
2393 }
2394 error:
d14a1e28 2395 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxPoint2D object.");
dd9f7fea 2396 return False;
f6bcfd97
BP
2397}
2398
2399
bb0054cd 2400//----------------------------------------------------------------------
b37c7e1d
RD
2401
2402PyObject* wxArrayString2PyList_helper(const wxArrayString& arr) {
2403
2404 PyObject* list = PyList_New(0);
2405 for (size_t i=0; i < arr.GetCount(); i++) {
c8bc7bb8 2406#if wxUSE_UNICODE
1e4a197e 2407 PyObject* str = PyUnicode_FromWideChar(arr[i].c_str(), arr[i].Len());
c8bc7bb8
RD
2408#else
2409 PyObject* str = PyString_FromStringAndSize(arr[i].c_str(), arr[i].Len());
2410#endif
b37c7e1d 2411 PyList_Append(list, str);
8af26133 2412 Py_DECREF(str);
b37c7e1d
RD
2413 }
2414 return list;
2415}
2416
2417
293a0a86
RD
2418PyObject* wxArrayInt2PyList_helper(const wxArrayInt& arr) {
2419
2420 PyObject* list = PyList_New(0);
2421 for (size_t i=0; i < arr.GetCount(); i++) {
2422 PyObject* number = PyInt_FromLong(arr[i]);
2423 PyList_Append(list, number);
2424 Py_DECREF(number);
2425 }
2426 return list;
2427}
2428
2429
bb0054cd 2430//----------------------------------------------------------------------
7bf85405 2431//----------------------------------------------------------------------
7bf85405 2432
7bf85405 2433
7bf85405 2434
de20db99 2435