]>
Commit | Line | Data |
---|---|---|
7bf85405 RD |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: helpers.cpp | |
03e9bead | 3 | // Purpose: Helper functions/classes for the wxPython extension module |
7bf85405 RD |
4 | // |
5 | // Author: Robin Dunn | |
6 | // | |
7 | // Created: 7/1/97 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 by Total Control Software | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
694759cf | 13 | #include <stdio.h> // get the correct definition of NULL |
08127323 | 14 | |
7bf85405 RD |
15 | #undef DEBUG |
16 | #include <Python.h> | |
17 | #include "helpers.h" | |
694759cf | 18 | |
af309447 RD |
19 | #ifdef __WXMSW__ |
20 | #include <wx/msw/private.h> | |
21 | #undef FindWindow | |
22 | #undef GetCharWidth | |
23 | #undef LoadAccelerators | |
24 | #undef GetClassInfo | |
25 | #undef GetClassName | |
26 | #endif | |
694759cf RD |
27 | |
28 | #ifdef __WXGTK__ | |
29 | #include <gtk/gtk.h> | |
54b96882 RD |
30 | #include <gdk/gdkprivate.h> |
31 | #include <wx/gtk/win_gtk.h> | |
32 | //#include <gdk/gdk.h> | |
33 | //#include <gdk/gdkx.h> | |
34 | //#include <gtk/gtkwindow.h> | |
7ff49f0c | 35 | |
f6bcfd97 | 36 | //extern GtkWidget *wxRootWindow; |
7ff49f0c | 37 | |
694759cf | 38 | #endif |
7bf85405 | 39 | |
cf694132 | 40 | |
7bf85405 RD |
41 | //--------------------------------------------------------------------------- |
42 | ||
43 | //wxHashTable* wxPyWindows = NULL; | |
44 | ||
45 | ||
46 | wxPoint wxPyDefaultPosition; //wxDefaultPosition); | |
47 | wxSize wxPyDefaultSize; //wxDefaultSize); | |
48 | wxString wxPyEmptyStr(""); | |
49 | ||
50 | ||
51 | ||
21f4bf45 | 52 | #ifdef __WXMSW__ // If building for win32... |
21f4bf45 RD |
53 | //---------------------------------------------------------------------- |
54 | // This gets run when the DLL is loaded. We just need to save a handle. | |
55 | //---------------------------------------------------------------------- | |
9c039d08 | 56 | |
21f4bf45 RD |
57 | BOOL WINAPI DllMain( |
58 | HINSTANCE hinstDLL, // handle to DLL module | |
59 | DWORD fdwReason, // reason for calling function | |
60 | LPVOID lpvReserved // reserved | |
61 | ) | |
62 | { | |
af309447 | 63 | wxSetInstance(hinstDLL); |
21f4bf45 RD |
64 | return 1; |
65 | } | |
66 | #endif | |
67 | ||
7bf85405 RD |
68 | //---------------------------------------------------------------------- |
69 | // Class for implementing the wxp main application shell. | |
70 | //---------------------------------------------------------------------- | |
71 | ||
72 | wxPyApp *wxPythonApp = NULL; // Global instance of application object | |
73 | ||
74 | ||
cf694132 RD |
75 | wxPyApp::wxPyApp() { |
76 | // printf("**** ctor\n"); | |
77 | } | |
78 | ||
79 | wxPyApp::~wxPyApp() { | |
80 | // printf("**** dtor\n"); | |
81 | } | |
82 | ||
83 | ||
7bf85405 RD |
84 | // This one isn't acutally called... See __wxStart() |
85 | bool wxPyApp::OnInit(void) { | |
6999b0d8 | 86 | return FALSE; |
7bf85405 RD |
87 | } |
88 | ||
89 | int wxPyApp::MainLoop(void) { | |
7ff49f0c | 90 | int retval = 0; |
af309447 | 91 | |
7ff49f0c RD |
92 | DeletePendingObjects(); |
93 | #ifdef __WXGTK__ | |
94 | m_initialized = wxTopLevelWindows.GetCount() != 0; | |
95 | #endif | |
7bf85405 | 96 | |
7ff49f0c RD |
97 | if (Initialized()) { |
98 | retval = wxApp::MainLoop(); | |
99 | wxPythonApp->OnExit(); | |
100 | } | |
101 | return retval; | |
102 | } | |
7bf85405 RD |
103 | |
104 | ||
fb5e0af0 | 105 | //--------------------------------------------------------------------- |
7bf85405 RD |
106 | //---------------------------------------------------------------------- |
107 | ||
7ece89c6 RD |
108 | int WXDLLEXPORT wxEntryStart( int argc, char** argv ); |
109 | int WXDLLEXPORT wxEntryInitGui(); | |
110 | void WXDLLEXPORT wxEntryCleanup(); | |
7ff49f0c | 111 | |
7bf85405 | 112 | |
f6bcfd97 BP |
113 | #ifdef WXP_WITH_THREAD |
114 | PyThreadState* wxPyEventThreadState = NULL; | |
115 | #endif | |
116 | static char* __nullArgv[1] = { 0 }; | |
117 | ||
118 | ||
0d6f9504 | 119 | // This is where we pick up the first part of the wxEntry functionality... |
f6bcfd97 | 120 | // The rest is in __wxStart and __wxCleanup. This function is called when |
8bf5d46e | 121 | // wxcmodule is imported. (Before there is a wxApp object.) |
0d6f9504 | 122 | void __wxPreStart() |
7bf85405 | 123 | { |
9d8bd15f RD |
124 | #ifdef WXP_WITH_THREAD |
125 | PyEval_InitThreads(); | |
f6bcfd97 | 126 | wxPyEventThreadState = PyThreadState_Get(); |
9d8bd15f RD |
127 | #endif |
128 | ||
0d6f9504 RD |
129 | // Bail out if there is already windows created. This means that the |
130 | // toolkit has already been initialized, as in embedding wxPython in | |
131 | // a C++ wxWindows app. | |
132 | if (wxTopLevelWindows.Number() > 0) | |
133 | return; | |
7bf85405 | 134 | |
7ff49f0c RD |
135 | |
136 | PyObject* sysargv = PySys_GetObject("argv"); | |
137 | int argc = PyList_Size(sysargv); | |
138 | char** argv = new char*[argc+1]; | |
139 | int x; | |
140 | for(x=0; x<argc; x++) | |
141 | argv[x] = PyString_AsString(PyList_GetItem(sysargv, x)); | |
142 | argv[argc] = NULL; | |
7bf85405 | 143 | |
7ff49f0c | 144 | wxEntryStart(argc, argv); |
f6bcfd97 | 145 | delete [] argv; |
0d6f9504 RD |
146 | } |
147 | ||
148 | ||
7ff49f0c | 149 | |
0d6f9504 RD |
150 | // Start the user application, user App's OnInit method is a parameter here |
151 | PyObject* __wxStart(PyObject* /* self */, PyObject* args) | |
152 | { | |
153 | PyObject* onInitFunc = NULL; | |
154 | PyObject* arglist; | |
155 | PyObject* result; | |
156 | long bResult; | |
157 | ||
0d6f9504 RD |
158 | if (!PyArg_ParseTuple(args, "O", &onInitFunc)) |
159 | return NULL; | |
160 | ||
161 | if (wxTopLevelWindows.Number() > 0) { | |
162 | PyErr_SetString(PyExc_TypeError, "Only 1 wxApp per process!"); | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | ||
167 | // This is the next part of the wxEntry functionality... | |
f6bcfd97 BP |
168 | PyObject* sysargv = PySys_GetObject("argv"); |
169 | int argc = PyList_Size(sysargv); | |
170 | char** argv = new char*[argc+1]; | |
171 | int x; | |
172 | for(x=0; x<argc; x++) | |
173 | argv[x] = PyString_AsString(PyList_GetItem(sysargv, x)); | |
174 | argv[argc] = NULL; | |
175 | ||
176 | wxPythonApp->argc = argc; | |
177 | wxPythonApp->argv = argv; | |
0d6f9504 | 178 | |
7ff49f0c | 179 | wxEntryInitGui(); |
7bf85405 RD |
180 | |
181 | // Call the Python App's OnInit function | |
182 | arglist = PyTuple_New(0); | |
183 | result = PyEval_CallObject(onInitFunc, arglist); | |
8bf5d46e RD |
184 | if (!result) { // an exception was raised. |
185 | return NULL; | |
7bf85405 RD |
186 | } |
187 | ||
188 | if (! PyInt_Check(result)) { | |
189 | PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value"); | |
190 | return NULL; | |
191 | } | |
192 | bResult = PyInt_AS_LONG(result); | |
193 | if (! bResult) { | |
194fa2ac | 194 | PyErr_SetString(PyExc_SystemExit, "OnInit returned FALSE, exiting..."); |
7bf85405 RD |
195 | return NULL; |
196 | } | |
197 | ||
13dfc243 | 198 | #ifdef __WXGTK__ |
7ff49f0c | 199 | wxTheApp->m_initialized = (wxTopLevelWindows.GetCount() > 0); |
13dfc243 | 200 | #endif |
fb5e0af0 | 201 | |
7bf85405 RD |
202 | Py_INCREF(Py_None); |
203 | return Py_None; | |
204 | } | |
205 | ||
7ff49f0c RD |
206 | void __wxCleanup() { |
207 | wxEntryCleanup(); | |
208 | } | |
7bf85405 RD |
209 | |
210 | ||
211 | ||
212 | PyObject* wxPython_dict; | |
213 | PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args) | |
214 | { | |
215 | ||
216 | if (!PyArg_ParseTuple(args, "O", &wxPython_dict)) | |
217 | return NULL; | |
218 | ||
219 | if (!PyDict_Check(wxPython_dict)) { | |
220 | PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!"); | |
221 | return NULL; | |
222 | } | |
223 | #ifdef __WXMOTIF__ | |
21f4bf45 RD |
224 | #define wxPlatform "__WXMOTIF__" |
225 | #endif | |
226 | #ifdef __WXQT__ | |
227 | #define wxPlatform "__WXQT__" | |
7bf85405 RD |
228 | #endif |
229 | #ifdef __WXGTK__ | |
21f4bf45 | 230 | #define wxPlatform "__WXGTK__" |
7bf85405 RD |
231 | #endif |
232 | #if defined(__WIN32__) || defined(__WXMSW__) | |
fb5e0af0 | 233 | #define wxPlatform "__WXMSW__" |
7bf85405 RD |
234 | #endif |
235 | #ifdef __WXMAC__ | |
21f4bf45 | 236 | #define wxPlatform "__WXMAC__" |
7bf85405 RD |
237 | #endif |
238 | ||
239 | PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform)); | |
240 | ||
241 | Py_INCREF(Py_None); | |
242 | return Py_None; | |
243 | } | |
244 | ||
245 | ||
246 | //--------------------------------------------------------------------------- | |
247 | ||
65dd82cb | 248 | PyObject* wxPyConstructObject(void* ptr, const char* className) { |
9c039d08 | 249 | char buff[64]; // should always be big enough... |
7bf85405 RD |
250 | char swigptr[64]; |
251 | ||
252 | sprintf(buff, "_%s_p", className); | |
253 | SWIG_MakePtr(swigptr, ptr, buff); | |
254 | ||
255 | sprintf(buff, "%sPtr", className); | |
256 | PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff); | |
257 | if (! classobj) { | |
258 | Py_INCREF(Py_None); | |
259 | return Py_None; | |
260 | } | |
261 | ||
262 | PyObject* arg = Py_BuildValue("(s)", swigptr); | |
263 | PyObject* obj = PyInstance_New(classobj, arg, NULL); | |
264 | Py_DECREF(arg); | |
265 | ||
266 | return obj; | |
267 | } | |
268 | ||
d559219f | 269 | //--------------------------------------------------------------------------- |
7bf85405 | 270 | |
99a49d3e | 271 | static unsigned int _wxPyNestCount = 0; |
cf694132 | 272 | |
99a49d3e RD |
273 | static PyThreadState* myPyThreadState_Get() { |
274 | PyThreadState* current; | |
275 | current = PyThreadState_Swap(NULL); | |
276 | PyThreadState_Swap(current); | |
277 | return current; | |
278 | } | |
d559219f | 279 | |
99a49d3e RD |
280 | |
281 | HELPEREXPORT bool wxPyRestoreThread() { | |
d559219f RD |
282 | // NOTE: The Python API docs state that if a thread already has the |
283 | // interpreter lock and calls PyEval_RestoreThread again a deadlock | |
99a49d3e RD |
284 | // occurs, so I put in this code as a guard condition since there are |
285 | // many possibilites for nested events and callbacks in wxPython. If | |
286 | // The current thread is our thread, then we can assume that we | |
2abc0a0f | 287 | // already have the lock. (I hope!) |
d559219f | 288 | // |
cf694132 | 289 | #ifdef WXP_WITH_THREAD |
99a49d3e RD |
290 | _wxPyNestCount += 1; |
291 | if (wxPyEventThreadState != myPyThreadState_Get()) { | |
292 | PyEval_RestoreThread(wxPyEventThreadState); | |
293 | return TRUE; | |
294 | } | |
295 | else | |
cf694132 | 296 | #endif |
d559219f RD |
297 | return FALSE; |
298 | } | |
cf694132 | 299 | |
cf694132 | 300 | |
d559219f | 301 | HELPEREXPORT void wxPySaveThread(bool doSave) { |
cf694132 | 302 | #ifdef WXP_WITH_THREAD |
99a49d3e | 303 | if (doSave) { |
9d8bd15f | 304 | wxPyEventThreadState = PyEval_SaveThread(); |
99a49d3e RD |
305 | } |
306 | _wxPyNestCount -= 1; | |
cf694132 RD |
307 | #endif |
308 | } | |
309 | ||
d559219f RD |
310 | //--------------------------------------------------------------------------- |
311 | ||
312 | ||
2f90df85 RD |
313 | IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject); |
314 | ||
d559219f RD |
315 | wxPyCallback::wxPyCallback(PyObject* func) { |
316 | m_func = func; | |
317 | Py_INCREF(m_func); | |
318 | } | |
319 | ||
2f90df85 RD |
320 | wxPyCallback::wxPyCallback(const wxPyCallback& other) { |
321 | m_func = other.m_func; | |
322 | Py_INCREF(m_func); | |
323 | } | |
324 | ||
d559219f RD |
325 | wxPyCallback::~wxPyCallback() { |
326 | bool doSave = wxPyRestoreThread(); | |
327 | Py_DECREF(m_func); | |
328 | wxPySaveThread(doSave); | |
329 | } | |
330 | ||
cf694132 RD |
331 | |
332 | ||
7bf85405 RD |
333 | // This function is used for all events destined for Python event handlers. |
334 | void wxPyCallback::EventThunker(wxEvent& event) { | |
335 | wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData; | |
336 | PyObject* func = cb->m_func; | |
337 | PyObject* result; | |
338 | PyObject* arg; | |
339 | PyObject* tuple; | |
340 | ||
cf694132 | 341 | |
d559219f | 342 | bool doSave = wxPyRestoreThread(); |
65dd82cb RD |
343 | wxString className = event.GetClassInfo()->GetClassName(); |
344 | ||
345 | if (className == "wxPyEvent") | |
346 | arg = ((wxPyEvent*)&event)->GetSelf(); | |
347 | else if (className == "wxPyCommandEvent") | |
348 | arg = ((wxPyCommandEvent*)&event)->GetSelf(); | |
349 | else | |
350 | arg = wxPyConstructObject((void*)&event, className); | |
7bf85405 RD |
351 | |
352 | tuple = PyTuple_New(1); | |
353 | PyTuple_SET_ITEM(tuple, 0, arg); | |
354 | result = PyEval_CallObject(func, tuple); | |
7bf85405 RD |
355 | Py_DECREF(tuple); |
356 | if (result) { | |
357 | Py_DECREF(result); | |
358 | PyErr_Clear(); | |
359 | } else { | |
360 | PyErr_Print(); | |
361 | } | |
d559219f | 362 | wxPySaveThread(doSave); |
7bf85405 RD |
363 | } |
364 | ||
365 | ||
d559219f | 366 | //---------------------------------------------------------------------- |
7bf85405 | 367 | |
d559219f | 368 | wxPyCallbackHelper::wxPyCallbackHelper() { |
f6bcfd97 | 369 | m_class = NULL; |
d559219f RD |
370 | m_self = NULL; |
371 | m_lastFound = NULL; | |
b7312675 | 372 | m_incRef = FALSE; |
d559219f | 373 | } |
8bf5d46e | 374 | |
8bf5d46e | 375 | |
d559219f RD |
376 | wxPyCallbackHelper::~wxPyCallbackHelper() { |
377 | bool doSave = wxPyRestoreThread(); | |
f6bcfd97 | 378 | if (m_incRef) { |
b7312675 | 379 | Py_XDECREF(m_self); |
f6bcfd97 BP |
380 | Py_XDECREF(m_class); |
381 | } | |
d559219f RD |
382 | wxPySaveThread(doSave); |
383 | } | |
8bf5d46e | 384 | |
2f90df85 RD |
385 | wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) { |
386 | m_lastFound = NULL; | |
387 | m_self = other.m_self; | |
f6bcfd97 BP |
388 | m_class = other.m_class; |
389 | if (m_self) { | |
2f90df85 | 390 | Py_INCREF(m_self); |
f6bcfd97 BP |
391 | Py_INCREF(m_class); |
392 | } | |
2f90df85 RD |
393 | } |
394 | ||
395 | ||
f6bcfd97 | 396 | void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* _class, int incref) { |
d559219f | 397 | m_self = self; |
f6bcfd97 | 398 | m_class = _class; |
b7312675 | 399 | m_incRef = incref; |
f6bcfd97 | 400 | if (incref) { |
2f90df85 | 401 | Py_INCREF(m_self); |
f6bcfd97 BP |
402 | Py_INCREF(m_class); |
403 | } | |
d559219f | 404 | } |
8bf5d46e | 405 | |
8bf5d46e | 406 | |
f6bcfd97 BP |
407 | // If the object (m_self) has an attibute of the given name, and if that |
408 | // attribute is a method, and if that method's class is not from a base class, | |
409 | // then we'll save a pointer to the method so callCallback can call it. | |
410 | bool wxPyCallbackHelper::findCallback(const wxString& name) const { | |
411 | wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const | |
412 | self->m_lastFound = NULL; | |
413 | if (m_self && PyObject_HasAttrString(m_self, (char*)name.c_str())) { | |
414 | PyObject* method; | |
415 | method = PyObject_GetAttrString(m_self, (char*)name.c_str()); | |
416 | ||
417 | if (PyMethod_Check(method) && | |
418 | ((PyMethod_GET_CLASS(method) == m_class) || | |
419 | PyClass_IsSubclass(PyMethod_GET_CLASS(method), m_class))) { | |
8bf5d46e | 420 | |
f6bcfd97 BP |
421 | self->m_lastFound = method; |
422 | } | |
423 | } | |
d559219f RD |
424 | return m_lastFound != NULL; |
425 | } | |
8bf5d46e | 426 | |
d559219f | 427 | |
f6bcfd97 | 428 | int wxPyCallbackHelper::callCallback(PyObject* argTuple) const { |
d559219f RD |
429 | PyObject* result; |
430 | int retval = FALSE; | |
431 | ||
432 | result = callCallbackObj(argTuple); | |
433 | if (result) { // Assumes an integer return type... | |
434 | retval = PyInt_AsLong(result); | |
435 | Py_DECREF(result); | |
436 | PyErr_Clear(); // forget about it if it's not... | |
437 | } | |
438 | return retval; | |
439 | } | |
440 | ||
441 | // Invoke the Python callable object, returning the raw PyObject return | |
442 | // value. Caller should DECREF the return value and also call PyEval_SaveThread. | |
f6bcfd97 | 443 | PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const { |
d559219f RD |
444 | PyObject* result; |
445 | ||
446 | result = PyEval_CallObject(m_lastFound, argTuple); | |
447 | Py_DECREF(argTuple); | |
448 | if (!result) { | |
449 | PyErr_Print(); | |
450 | } | |
451 | return result; | |
452 | } | |
714e6a9e | 453 | |
7bf85405 | 454 | |
d559219f | 455 | |
65dd82cb RD |
456 | //--------------------------------------------------------------------------- |
457 | //--------------------------------------------------------------------------- | |
458 | // These classes can be derived from in Python and passed through the event | |
459 | // system without loosing anything. They do this by keeping a reference to | |
460 | // themselves and some special case handling in wxPyCallback::EventThunker. | |
461 | ||
462 | ||
e19b7164 | 463 | wxPyEvtSelfRef::wxPyEvtSelfRef() { |
65dd82cb RD |
464 | //m_self = Py_None; // **** We don't do normal ref counting to prevent |
465 | //Py_INCREF(m_self); // circular loops... | |
194fa2ac | 466 | m_cloned = FALSE; |
65dd82cb RD |
467 | } |
468 | ||
e19b7164 | 469 | wxPyEvtSelfRef::~wxPyEvtSelfRef() { |
65dd82cb RD |
470 | bool doSave = wxPyRestoreThread(); |
471 | if (m_cloned) | |
472 | Py_DECREF(m_self); | |
473 | wxPySaveThread(doSave); | |
474 | } | |
475 | ||
e19b7164 | 476 | void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) { |
65dd82cb RD |
477 | bool doSave = wxPyRestoreThread(); |
478 | if (m_cloned) | |
479 | Py_DECREF(m_self); | |
480 | m_self = self; | |
481 | if (clone) { | |
482 | Py_INCREF(m_self); | |
483 | m_cloned = TRUE; | |
484 | } | |
485 | wxPySaveThread(doSave); | |
486 | } | |
487 | ||
e19b7164 | 488 | PyObject* wxPyEvtSelfRef::GetSelf() const { |
65dd82cb RD |
489 | Py_INCREF(m_self); |
490 | return m_self; | |
491 | } | |
492 | ||
493 | ||
494 | wxPyEvent::wxPyEvent(int id) | |
495 | : wxEvent(id) { | |
496 | } | |
497 | ||
498 | wxPyEvent::~wxPyEvent() { | |
499 | } | |
500 | ||
501 | // This one is so the event object can be Cloned... | |
502 | void wxPyEvent::CopyObject(wxObject& dest) const { | |
503 | wxEvent::CopyObject(dest); | |
504 | ((wxPyEvent*)&dest)->SetSelf(m_self, TRUE); | |
505 | } | |
506 | ||
507 | ||
508 | IMPLEMENT_DYNAMIC_CLASS(wxPyEvent, wxEvent); | |
509 | ||
510 | ||
511 | wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id) | |
512 | : wxCommandEvent(commandType, id) { | |
513 | } | |
514 | ||
515 | wxPyCommandEvent::~wxPyCommandEvent() { | |
516 | } | |
517 | ||
518 | void wxPyCommandEvent::CopyObject(wxObject& dest) const { | |
519 | wxCommandEvent::CopyObject(dest); | |
520 | ((wxPyCommandEvent*)&dest)->SetSelf(m_self, TRUE); | |
521 | } | |
522 | ||
523 | ||
524 | IMPLEMENT_DYNAMIC_CLASS(wxPyCommandEvent, wxCommandEvent); | |
525 | ||
526 | ||
527 | ||
d559219f | 528 | //--------------------------------------------------------------------------- |
7bf85405 RD |
529 | //--------------------------------------------------------------------------- |
530 | ||
d559219f | 531 | |
7bf85405 RD |
532 | wxPyTimer::wxPyTimer(PyObject* callback) { |
533 | func = callback; | |
534 | Py_INCREF(func); | |
535 | } | |
536 | ||
537 | wxPyTimer::~wxPyTimer() { | |
d559219f | 538 | bool doSave = wxPyRestoreThread(); |
7bf85405 | 539 | Py_DECREF(func); |
d559219f | 540 | wxPySaveThread(doSave); |
7bf85405 RD |
541 | } |
542 | ||
543 | void wxPyTimer::Notify() { | |
d559219f RD |
544 | bool doSave = wxPyRestoreThread(); |
545 | ||
7bf85405 RD |
546 | PyObject* result; |
547 | PyObject* args = Py_BuildValue("()"); | |
548 | ||
549 | result = PyEval_CallObject(func, args); | |
550 | Py_DECREF(args); | |
551 | if (result) { | |
552 | Py_DECREF(result); | |
553 | PyErr_Clear(); | |
554 | } else { | |
555 | PyErr_Print(); | |
556 | } | |
d559219f | 557 | wxPySaveThread(doSave); |
7bf85405 RD |
558 | } |
559 | ||
560 | ||
cf694132 | 561 | |
2f90df85 | 562 | //--------------------------------------------------------------------------- |
389c5527 RD |
563 | //--------------------------------------------------------------------------- |
564 | // Convert a wxList to a Python List | |
565 | ||
65dd82cb | 566 | PyObject* wxPy_ConvertList(wxListBase* list, const char* className) { |
389c5527 RD |
567 | PyObject* pyList; |
568 | PyObject* pyObj; | |
569 | wxObject* wxObj; | |
570 | wxNode* node = list->First(); | |
571 | ||
572 | bool doSave = wxPyRestoreThread(); | |
573 | pyList = PyList_New(0); | |
574 | while (node) { | |
575 | wxObj = node->Data(); | |
576 | pyObj = wxPyConstructObject(wxObj, className); | |
577 | PyList_Append(pyList, pyObj); | |
578 | node = node->Next(); | |
579 | } | |
580 | wxPySaveThread(doSave); | |
581 | return pyList; | |
582 | } | |
583 | ||
54b96882 RD |
584 | //---------------------------------------------------------------------- |
585 | ||
586 | long wxPyGetWinHandle(wxWindow* win) { | |
587 | #ifdef __WXMSW__ | |
588 | return (long)win->GetHandle(); | |
589 | #endif | |
590 | ||
591 | // Find and return the actual X-Window. | |
592 | #ifdef __WXGTK__ | |
593 | if (win->m_wxwindow) { | |
594 | GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window; | |
595 | if (bwin) { | |
596 | return (long)bwin->xwindow; | |
597 | } | |
598 | } | |
599 | #endif | |
600 | return 0; | |
601 | } | |
602 | ||
7bf85405 RD |
603 | //---------------------------------------------------------------------- |
604 | // Some helper functions for typemaps in my_typemaps.i, so they won't be | |
389c5527 | 605 | // included in every file... |
7bf85405 RD |
606 | |
607 | ||
2f90df85 | 608 | byte* byte_LIST_helper(PyObject* source) { |
b639c3c5 RD |
609 | if (!PyList_Check(source)) { |
610 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
611 | return NULL; | |
612 | } | |
613 | int count = PyList_Size(source); | |
614 | byte* temp = new byte[count]; | |
615 | if (! temp) { | |
616 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
617 | return NULL; | |
618 | } | |
619 | for (int x=0; x<count; x++) { | |
620 | PyObject* o = PyList_GetItem(source, x); | |
621 | if (! PyInt_Check(o)) { | |
622 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
623 | return NULL; | |
624 | } | |
625 | temp[x] = (byte)PyInt_AsLong(o); | |
626 | } | |
627 | return temp; | |
628 | } | |
629 | ||
630 | ||
2f90df85 | 631 | int* int_LIST_helper(PyObject* source) { |
7bf85405 RD |
632 | if (!PyList_Check(source)) { |
633 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
634 | return NULL; | |
635 | } | |
636 | int count = PyList_Size(source); | |
637 | int* temp = new int[count]; | |
638 | if (! temp) { | |
639 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
640 | return NULL; | |
641 | } | |
642 | for (int x=0; x<count; x++) { | |
643 | PyObject* o = PyList_GetItem(source, x); | |
644 | if (! PyInt_Check(o)) { | |
645 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
646 | return NULL; | |
647 | } | |
648 | temp[x] = PyInt_AsLong(o); | |
649 | } | |
650 | return temp; | |
651 | } | |
652 | ||
653 | ||
2f90df85 | 654 | long* long_LIST_helper(PyObject* source) { |
7bf85405 RD |
655 | if (!PyList_Check(source)) { |
656 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
657 | return NULL; | |
658 | } | |
659 | int count = PyList_Size(source); | |
660 | long* temp = new long[count]; | |
661 | if (! temp) { | |
662 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
663 | return NULL; | |
664 | } | |
665 | for (int x=0; x<count; x++) { | |
666 | PyObject* o = PyList_GetItem(source, x); | |
667 | if (! PyInt_Check(o)) { | |
668 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
669 | return NULL; | |
670 | } | |
671 | temp[x] = PyInt_AsLong(o); | |
672 | } | |
673 | return temp; | |
674 | } | |
675 | ||
676 | ||
2f90df85 | 677 | char** string_LIST_helper(PyObject* source) { |
7bf85405 RD |
678 | if (!PyList_Check(source)) { |
679 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
680 | return NULL; | |
681 | } | |
682 | int count = PyList_Size(source); | |
683 | char** temp = new char*[count]; | |
684 | if (! temp) { | |
685 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
686 | return NULL; | |
687 | } | |
688 | for (int x=0; x<count; x++) { | |
689 | PyObject* o = PyList_GetItem(source, x); | |
690 | if (! PyString_Check(o)) { | |
691 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
692 | return NULL; | |
693 | } | |
694 | temp[x] = PyString_AsString(o); | |
695 | } | |
696 | return temp; | |
697 | } | |
698 | ||
699 | ||
700 | ||
2f90df85 | 701 | wxPoint* wxPoint_LIST_helper(PyObject* source) { |
7bf85405 RD |
702 | if (!PyList_Check(source)) { |
703 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
704 | return NULL; | |
705 | } | |
706 | int count = PyList_Size(source); | |
707 | wxPoint* temp = new wxPoint[count]; | |
708 | if (! temp) { | |
709 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
710 | return NULL; | |
711 | } | |
712 | for (int x=0; x<count; x++) { | |
713 | PyObject* o = PyList_GetItem(source, x); | |
d559219f | 714 | if (PyTuple_Check(o)) { |
7bf85405 RD |
715 | PyObject* o1 = PyTuple_GetItem(o, 0); |
716 | PyObject* o2 = PyTuple_GetItem(o, 1); | |
717 | ||
718 | temp[x].x = PyInt_AsLong(o1); | |
719 | temp[x].y = PyInt_AsLong(o2); | |
720 | } | |
d559219f RD |
721 | else if (PyInstance_Check(o)) { |
722 | wxPoint* pt; | |
723 | if (SWIG_GetPtrObj(o,(void **) &pt,"_wxPoint_p")) { | |
724 | PyErr_SetString(PyExc_TypeError,"Expected _wxPoint_p."); | |
725 | return NULL; | |
726 | } | |
727 | temp[x] = *pt; | |
728 | } | |
7bf85405 RD |
729 | else { |
730 | PyErr_SetString(PyExc_TypeError, "Expected a list of 2-tuples or wxPoints."); | |
731 | return NULL; | |
732 | } | |
733 | } | |
734 | return temp; | |
735 | } | |
736 | ||
737 | ||
2f90df85 | 738 | wxBitmap** wxBitmap_LIST_helper(PyObject* source) { |
7bf85405 RD |
739 | if (!PyList_Check(source)) { |
740 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
741 | return NULL; | |
742 | } | |
743 | int count = PyList_Size(source); | |
744 | wxBitmap** temp = new wxBitmap*[count]; | |
745 | if (! temp) { | |
746 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
747 | return NULL; | |
748 | } | |
749 | for (int x=0; x<count; x++) { | |
750 | PyObject* o = PyList_GetItem(source, x); | |
e166644c | 751 | if (PyInstance_Check(o)) { |
7bf85405 | 752 | wxBitmap* pt; |
e166644c | 753 | if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) { |
7bf85405 RD |
754 | PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p."); |
755 | return NULL; | |
756 | } | |
757 | temp[x] = pt; | |
758 | } | |
759 | else { | |
760 | PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps."); | |
761 | return NULL; | |
762 | } | |
763 | } | |
764 | return temp; | |
765 | } | |
766 | ||
767 | ||
768 | ||
2f90df85 | 769 | wxString* wxString_LIST_helper(PyObject* source) { |
7bf85405 RD |
770 | if (!PyList_Check(source)) { |
771 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
772 | return NULL; | |
773 | } | |
774 | int count = PyList_Size(source); | |
775 | wxString* temp = new wxString[count]; | |
776 | if (! temp) { | |
777 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
778 | return NULL; | |
779 | } | |
780 | for (int x=0; x<count; x++) { | |
781 | PyObject* o = PyList_GetItem(source, x); | |
782 | if (! PyString_Check(o)) { | |
783 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
784 | return NULL; | |
785 | } | |
786 | temp[x] = PyString_AsString(o); | |
787 | } | |
788 | return temp; | |
789 | } | |
790 | ||
791 | ||
2f90df85 | 792 | wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) { |
7bf85405 RD |
793 | if (!PyList_Check(source)) { |
794 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
795 | return NULL; | |
796 | } | |
797 | int count = PyList_Size(source); | |
798 | wxAcceleratorEntry* temp = new wxAcceleratorEntry[count]; | |
799 | if (! temp) { | |
800 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
801 | return NULL; | |
802 | } | |
803 | for (int x=0; x<count; x++) { | |
804 | PyObject* o = PyList_GetItem(source, x); | |
e166644c | 805 | if (PyInstance_Check(o)) { |
7bf85405 | 806 | wxAcceleratorEntry* ae; |
e166644c | 807 | if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) { |
7bf85405 RD |
808 | PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p."); |
809 | return NULL; | |
810 | } | |
811 | temp[x] = *ae; | |
812 | } | |
813 | else if (PyTuple_Check(o)) { | |
814 | PyObject* o1 = PyTuple_GetItem(o, 0); | |
815 | PyObject* o2 = PyTuple_GetItem(o, 1); | |
816 | PyObject* o3 = PyTuple_GetItem(o, 2); | |
817 | ||
818 | temp[x].m_flags = PyInt_AsLong(o1); | |
819 | temp[x].m_keyCode = PyInt_AsLong(o2); | |
820 | temp[x].m_command = PyInt_AsLong(o3); | |
821 | } | |
822 | else { | |
823 | PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects."); | |
824 | return NULL; | |
825 | } | |
826 | } | |
827 | return temp; | |
828 | } | |
829 | ||
bb0054cd RD |
830 | |
831 | ||
832 | //---------------------------------------------------------------------- | |
bb0054cd | 833 | |
2f90df85 RD |
834 | bool wxSize_helper(PyObject* source, wxSize** obj) { |
835 | ||
836 | // If source is an object instance then it may already be the right type | |
837 | if (PyInstance_Check(source)) { | |
838 | wxSize* ptr; | |
839 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p")) | |
840 | goto error; | |
841 | *obj = ptr; | |
842 | return TRUE; | |
843 | } | |
844 | // otherwise a 2-tuple of integers is expected | |
845 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
846 | PyObject* o1 = PySequence_GetItem(source, 0); | |
847 | PyObject* o2 = PySequence_GetItem(source, 1); | |
848 | **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2)); | |
849 | return TRUE; | |
850 | } | |
851 | ||
852 | error: | |
853 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object."); | |
854 | return FALSE; | |
855 | } | |
856 | ||
857 | bool wxPoint_helper(PyObject* source, wxPoint** obj) { | |
858 | ||
859 | // If source is an object instance then it may already be the right type | |
860 | if (PyInstance_Check(source)) { | |
861 | wxPoint* ptr; | |
862 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p")) | |
863 | goto error; | |
864 | *obj = ptr; | |
865 | return TRUE; | |
866 | } | |
867 | // otherwise a 2-tuple of integers is expected | |
868 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
869 | PyObject* o1 = PySequence_GetItem(source, 0); | |
870 | PyObject* o2 = PySequence_GetItem(source, 1); | |
871 | **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2)); | |
872 | return TRUE; | |
873 | } | |
874 | ||
875 | error: | |
876 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object."); | |
877 | return FALSE; | |
878 | } | |
879 | ||
880 | ||
881 | ||
882 | bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) { | |
883 | ||
884 | // If source is an object instance then it may already be the right type | |
885 | if (PyInstance_Check(source)) { | |
886 | wxRealPoint* ptr; | |
887 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p")) | |
888 | goto error; | |
889 | *obj = ptr; | |
890 | return TRUE; | |
891 | } | |
892 | // otherwise a 2-tuple of floats is expected | |
893 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
894 | PyObject* o1 = PySequence_GetItem(source, 0); | |
895 | PyObject* o2 = PySequence_GetItem(source, 1); | |
896 | **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2)); | |
897 | return TRUE; | |
898 | } | |
899 | ||
900 | error: | |
901 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object."); | |
902 | return FALSE; | |
903 | } | |
904 | ||
905 | ||
906 | ||
907 | ||
908 | bool wxRect_helper(PyObject* source, wxRect** obj) { | |
909 | ||
910 | // If source is an object instance then it may already be the right type | |
911 | if (PyInstance_Check(source)) { | |
912 | wxRect* ptr; | |
913 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p")) | |
914 | goto error; | |
915 | *obj = ptr; | |
916 | return TRUE; | |
917 | } | |
918 | // otherwise a 4-tuple of integers is expected | |
919 | else if (PySequence_Check(source) && PyObject_Length(source) == 4) { | |
920 | PyObject* o1 = PySequence_GetItem(source, 0); | |
921 | PyObject* o2 = PySequence_GetItem(source, 1); | |
922 | PyObject* o3 = PySequence_GetItem(source, 2); | |
923 | PyObject* o4 = PySequence_GetItem(source, 3); | |
924 | **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2), | |
925 | PyInt_AsLong(o3), PyInt_AsLong(o4)); | |
926 | return TRUE; | |
927 | } | |
928 | ||
929 | error: | |
930 | PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object."); | |
931 | return FALSE; | |
932 | } | |
933 | ||
934 | ||
935 | ||
f6bcfd97 BP |
936 | bool wxColour_helper(PyObject* source, wxColour** obj) { |
937 | ||
938 | // If source is an object instance then it may already be the right type | |
939 | if (PyInstance_Check(source)) { | |
940 | wxColour* ptr; | |
941 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p")) | |
942 | goto error; | |
943 | *obj = ptr; | |
944 | return TRUE; | |
945 | } | |
946 | // otherwise a string is expected | |
947 | else if (PyString_Check(source)) { | |
948 | wxString spec = PyString_AS_STRING(source); | |
949 | if (spec[0] == '#' && spec.Length() == 7) { // It's #RRGGBB | |
950 | char* junk; | |
951 | int red = strtol(spec.Mid(1,2), &junk, 16); | |
952 | int green = strtol(spec.Mid(3,2), &junk, 16); | |
953 | int blue = strtol(spec.Mid(5,2), &junk, 16); | |
954 | **obj = wxColour(red, green, blue); | |
955 | return TRUE; | |
956 | } | |
957 | else { // it's a colour name | |
958 | **obj = wxColour(spec); | |
959 | return TRUE; | |
960 | } | |
961 | } | |
962 | ||
963 | error: | |
964 | PyErr_SetString(PyExc_TypeError, "Expected a wxColour object or a string containing a colour name or #RRGGBB."); | |
965 | return FALSE; | |
966 | } | |
967 | ||
968 | ||
bb0054cd RD |
969 | //---------------------------------------------------------------------- |
970 | //---------------------------------------------------------------------- | |
7bf85405 | 971 | //---------------------------------------------------------------------- |
7bf85405 | 972 | |
7bf85405 | 973 | |
7bf85405 | 974 |