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