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