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