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