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