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