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