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