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