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