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