]> git.saurik.com Git - wxWidgets.git/blame - wxPython/src/helpers.cpp
Second phase of OOR completed. (Original python object return for
[wxWidgets.git] / wxPython / src / helpers.cpp
CommitLineData
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>
694759cf 32#endif
7bf85405 33
cf694132 34
7bf85405
RD
35
36
21f4bf45 37#ifdef __WXMSW__ // If building for win32...
21f4bf45
RD
38//----------------------------------------------------------------------
39// This gets run when the DLL is loaded. We just need to save a handle.
40//----------------------------------------------------------------------
9c039d08 41
21f4bf45
RD
42BOOL WINAPI DllMain(
43 HINSTANCE hinstDLL, // handle to DLL module
44 DWORD fdwReason, // reason for calling function
45 LPVOID lpvReserved // reserved
46 )
47{
af309447 48 wxSetInstance(hinstDLL);
21f4bf45
RD
49 return 1;
50}
51#endif
52
7bf85405
RD
53//----------------------------------------------------------------------
54// Class for implementing the wxp main application shell.
55//----------------------------------------------------------------------
56
57wxPyApp *wxPythonApp = NULL; // Global instance of application object
58
59
cf694132
RD
60wxPyApp::wxPyApp() {
61// printf("**** ctor\n");
62}
63
64wxPyApp::~wxPyApp() {
65// printf("**** dtor\n");
66}
67
68
7bf85405
RD
69// This one isn't acutally called... See __wxStart()
70bool wxPyApp::OnInit(void) {
6999b0d8 71 return FALSE;
7bf85405
RD
72}
73
74int wxPyApp::MainLoop(void) {
7ff49f0c 75 int retval = 0;
af309447 76
7ff49f0c
RD
77 DeletePendingObjects();
78#ifdef __WXGTK__
79 m_initialized = wxTopLevelWindows.GetCount() != 0;
80#endif
7bf85405 81
7ff49f0c
RD
82 if (Initialized()) {
83 retval = wxApp::MainLoop();
84 wxPythonApp->OnExit();
85 }
86 return retval;
87}
7bf85405
RD
88
89
fb5e0af0 90//---------------------------------------------------------------------
7bf85405
RD
91//----------------------------------------------------------------------
92
de20db99
RD
93#ifdef __WXMSW__
94#include "wx/msw/msvcrt.h"
95#endif
96
97
7ece89c6
RD
98int WXDLLEXPORT wxEntryStart( int argc, char** argv );
99int WXDLLEXPORT wxEntryInitGui();
100void WXDLLEXPORT wxEntryCleanup();
7ff49f0c 101
7bf85405 102
f6bcfd97 103#ifdef WXP_WITH_THREAD
19a97bd6
RD
104//PyThreadState* wxPyEventThreadState = NULL;
105PyInterpreterState* wxPyInterpreter = NULL;
f6bcfd97 106#endif
f6bcfd97
BP
107
108
0d6f9504 109// This is where we pick up the first part of the wxEntry functionality...
f6bcfd97 110// The rest is in __wxStart and __wxCleanup. This function is called when
8bf5d46e 111// wxcmodule is imported. (Before there is a wxApp object.)
0d6f9504 112void __wxPreStart()
7bf85405 113{
de20db99
RD
114
115#ifdef __WXMSW__
116// wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
117#endif
118
9d8bd15f 119#ifdef WXP_WITH_THREAD
1112b0c6 120 PyEval_InitThreads();
19a97bd6
RD
121// wxPyEventThreadState = PyThreadState_Get(); // PyThreadState_New(PyThreadState_Get()->interp);
122 wxPyInterpreter = PyThreadState_Get()->interp;
9d8bd15f
RD
123#endif
124
0d6f9504
RD
125 // Bail out if there is already windows created. This means that the
126 // toolkit has already been initialized, as in embedding wxPython in
127 // a C++ wxWindows app.
128 if (wxTopLevelWindows.Number() > 0)
129 return;
7bf85405 130
7ff49f0c 131
9416aa89
RD
132 int argc = 0;
133 char** argv = NULL;
7ff49f0c 134 PyObject* sysargv = PySys_GetObject("argv");
9416aa89
RD
135 if (sysargv != NULL) {
136 argc = PyList_Size(sysargv);
137 argv = new char*[argc+1];
138 int x;
139 for(x=0; x<argc; x++)
140 argv[x] = copystring(PyString_AsString(PyList_GetItem(sysargv, x)));
141 argv[argc] = NULL;
142 }
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
151PyObject* __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
83b18bab 161#if 0 // Try it out without this check, see 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...
9416aa89
RD
169 int argc = 0;
170 char** argv = NULL;
f6bcfd97 171 PyObject* sysargv = PySys_GetObject("argv");
9416aa89
RD
172 if (sysargv != NULL) {
173 argc = PyList_Size(sysargv);
174 argv = new char*[argc+1];
175 int x;
176 for(x=0; x<argc; x++)
177 argv[x] = copystring(PyString_AsString(PyList_GetItem(sysargv, x)));
178 argv[argc] = NULL;
179 }
f6bcfd97
BP
180 wxPythonApp->argc = argc;
181 wxPythonApp->argv = argv;
0d6f9504 182
7ff49f0c 183 wxEntryInitGui();
7bf85405
RD
184
185 // Call the Python App's OnInit function
186 arglist = PyTuple_New(0);
187 result = PyEval_CallObject(onInitFunc, arglist);
8bf5d46e
RD
188 if (!result) { // an exception was raised.
189 return NULL;
7bf85405
RD
190 }
191
192 if (! PyInt_Check(result)) {
193 PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value");
194 return NULL;
195 }
196 bResult = PyInt_AS_LONG(result);
197 if (! bResult) {
194fa2ac 198 PyErr_SetString(PyExc_SystemExit, "OnInit returned FALSE, exiting...");
7bf85405
RD
199 return NULL;
200 }
201
13dfc243 202#ifdef __WXGTK__
7ff49f0c 203 wxTheApp->m_initialized = (wxTopLevelWindows.GetCount() > 0);
13dfc243 204#endif
fb5e0af0 205
7bf85405
RD
206 Py_INCREF(Py_None);
207 return Py_None;
208}
209
7ff49f0c
RD
210void __wxCleanup() {
211 wxEntryCleanup();
212}
7bf85405
RD
213
214
215
9416aa89
RD
216static PyObject* wxPython_dict = NULL;
217static PyObject* wxPyPtrTypeMap = NULL;
218
7bf85405
RD
219PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args)
220{
221
222 if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
223 return NULL;
224
225 if (!PyDict_Check(wxPython_dict)) {
226 PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!");
227 return NULL;
228 }
9416aa89
RD
229
230 if (! wxPyPtrTypeMap)
231 wxPyPtrTypeMap = PyDict_New();
232 PyDict_SetItemString(wxPython_dict, "__wxPyPtrTypeMap", wxPyPtrTypeMap);
233
234
7bf85405 235#ifdef __WXMOTIF__
21f4bf45
RD
236#define wxPlatform "__WXMOTIF__"
237#endif
238#ifdef __WXQT__
239#define wxPlatform "__WXQT__"
7bf85405
RD
240#endif
241#ifdef __WXGTK__
21f4bf45 242#define wxPlatform "__WXGTK__"
7bf85405
RD
243#endif
244#if defined(__WIN32__) || defined(__WXMSW__)
fb5e0af0 245#define wxPlatform "__WXMSW__"
7bf85405
RD
246#endif
247#ifdef __WXMAC__
21f4bf45 248#define wxPlatform "__WXMAC__"
7bf85405
RD
249#endif
250
251 PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform));
252
253 Py_INCREF(Py_None);
254 return Py_None;
255}
256
257
9416aa89
RD
258//---------------------------------------------------------------------------
259// Stuff used by OOR to find the right wxPython class type to return and to
260// build it.
261
262
263// The pointer type map is used when the "pointer" type name generated by SWIG
264// is not the same as the shadow class name, for example wxPyTreeCtrl
265// vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
266// so we'll just make it a Python dictionary in the wx module's namespace.
267void wxPyPtrTypeMap_Add(const char* commonName, const char* ptrName) {
268 if (! wxPyPtrTypeMap)
269 wxPyPtrTypeMap = PyDict_New();
270
271 PyDict_SetItemString(wxPyPtrTypeMap,
272 (char*)commonName,
273 PyString_FromString((char*)ptrName));
274}
275
276
277
278PyObject* wxPyClassExists(const char* className) {
279
280 if (!className)
281 return NULL;
282
283 char buff[64]; // should always be big enough...
284
285 sprintf(buff, "%sPtr", className);
286 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
287
288 return classobj; // returns NULL if not found
289}
290
291
292PyObject* wxPyMake_wxObject(wxObject* source) {
0122b7e3
RD
293 PyObject* target = NULL;
294 bool isEvtHandler = FALSE;
9416aa89
RD
295
296 if (source) {
0122b7e3
RD
297 if (wxIsKindOf(source, wxEvtHandler)) {
298 wxEvtHandler* eh = (wxEvtHandler*)source;
299 wxPyClientData* data = (wxPyClientData*)eh->GetClientObject();
300 if (data) {
301 target = data->m_obj;
302 Py_INCREF(target);
303 }
9416aa89 304 }
0122b7e3
RD
305
306 if (! target) {
307 wxClassInfo* info = source->GetClassInfo();
308 wxChar* name = (wxChar*)info->GetClassName();
309 PyObject* klass = wxPyClassExists(name);
310 while (info && !klass) {
311 name = (wxChar*)info->GetBaseClassName1();
312 info = wxClassInfo::FindClass(name);
313 klass = wxPyClassExists(name);
314 }
315 if (info) {
316 target = wxPyConstructObject(source, name, klass, FALSE);
317 if (target && isEvtHandler)
318 ((wxEvtHandler*)source)->SetClientObject(new wxPyClientData(target));
319 } else {
320 wxString msg("wxPython class not found for ");
321 msg += source->GetClassInfo()->GetClassName();
322 PyErr_SetString(PyExc_NameError, msg.c_str());
323 target = NULL;
324 }
9416aa89
RD
325 }
326 } else { // source was NULL so return None.
327 Py_INCREF(Py_None); target = Py_None;
328 }
329 return target;
330}
331
0122b7e3 332
7bf85405
RD
333//---------------------------------------------------------------------------
334
c368d904 335PyObject* wxPyConstructObject(void* ptr,
1e7ecb7b 336 const char* className,
9416aa89 337 PyObject* klass,
1e7ecb7b 338 int setThisOwn) {
9416aa89 339
33510773
RD
340 PyObject* obj;
341 PyObject* arg;
9416aa89
RD
342 PyObject* item;
343 char swigptr[64]; // should always be big enough...
344 char buff[64];
345
346 if ((item = PyDict_GetItemString(wxPyPtrTypeMap, (char*)className)) != NULL) {
347 className = PyString_AsString(item);
348 }
349 sprintf(buff, "_%s_p", className);
350 SWIG_MakePtr(swigptr, ptr, buff);
351
352 arg = Py_BuildValue("(s)", swigptr);
353 obj = PyInstance_New(klass, arg, NULL);
354 Py_DECREF(arg);
355
356 if (setThisOwn) {
357 PyObject* one = PyInt_FromLong(1);
358 PyObject_SetAttrString(obj, "thisown", one);
359 Py_DECREF(one);
360 }
361
362 return obj;
363}
364
365
366PyObject* wxPyConstructObject(void* ptr,
367 const char* className,
368 int setThisOwn) {
369 PyObject* obj;
33510773
RD
370
371 if (!ptr) {
372 Py_INCREF(Py_None);
373 return Py_None;
374 }
375
9c039d08 376 char buff[64]; // should always be big enough...
7bf85405
RD
377
378 sprintf(buff, "%sPtr", className);
379 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
380 if (! classobj) {
33510773
RD
381 char temp[128];
382 sprintf(temp,
383 "*** Unknown class name %s, tell Robin about it please ***",
384 buff);
385 obj = PyString_FromString(temp);
386 return obj;
7bf85405
RD
387 }
388
9416aa89 389 return wxPyConstructObject(ptr, className, classobj, setThisOwn);
7bf85405
RD
390}
391
d559219f 392//---------------------------------------------------------------------------
7bf85405 393
19a97bd6
RD
394// static PyThreadState* myPyThreadState_Get() {
395// PyThreadState* current;
396// current = PyThreadState_Swap(NULL);
397// PyThreadState_Swap(current);
398// return current;
399// }
400
401
402// bool wxPyRestoreThread() {
403// // NOTE: The Python API docs state that if a thread already has the
404// // interpreter lock and calls PyEval_RestoreThread again a deadlock
405// // occurs, so I put in this code as a guard condition since there are
406// // many possibilites for nested events and callbacks in wxPython. If
407// // The current thread is our thread, then we can assume that we
408// // already have the lock. (I hope!)
409// //
410// #ifdef WXP_WITH_THREAD
411// if (wxPyEventThreadState != myPyThreadState_Get()) {
412// PyEval_AcquireThread(wxPyEventThreadState);
413// return TRUE;
414// }
415// else
416// #endif
417// return FALSE;
418// }
419
420
421// void wxPySaveThread(bool doSave) {
422// #ifdef WXP_WITH_THREAD
423// if (doSave) {
424// PyEval_ReleaseThread(wxPyEventThreadState);
425// }
426// #endif
427// }
428
429
430
431wxPyTState* wxPyBeginBlockThreads() {
432 wxPyTState* state = NULL;
cf694132 433#ifdef WXP_WITH_THREAD
19a97bd6
RD
434 if (1) { // Can I check if I've already got the lock?
435 state = new wxPyTState;
436 PyEval_AcquireLock();
437 state->newState = PyThreadState_New(wxPyInterpreter);
438 state->prevState = PyThreadState_Swap(state->newState);
1112b0c6 439 }
cf694132 440#endif
19a97bd6 441 return state;
d559219f 442}
cf694132 443
cf694132 444
19a97bd6 445void wxPyEndBlockThreads(wxPyTState* state) {
cf694132 446#ifdef WXP_WITH_THREAD
19a97bd6
RD
447 if (state) {
448 PyThreadState_Swap(state->prevState);
449 PyThreadState_Clear(state->newState);
450 PyEval_ReleaseLock();
451 PyThreadState_Delete(state->newState);
452 delete state;
1112b0c6
RD
453 }
454#endif
cf694132
RD
455}
456
19a97bd6 457
d559219f
RD
458//---------------------------------------------------------------------------
459
2f90df85
RD
460IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject);
461
d559219f
RD
462wxPyCallback::wxPyCallback(PyObject* func) {
463 m_func = func;
464 Py_INCREF(m_func);
465}
466
2f90df85
RD
467wxPyCallback::wxPyCallback(const wxPyCallback& other) {
468 m_func = other.m_func;
469 Py_INCREF(m_func);
470}
471
d559219f 472wxPyCallback::~wxPyCallback() {
19a97bd6 473 wxPyTState* state = wxPyBeginBlockThreads();
d559219f 474 Py_DECREF(m_func);
19a97bd6 475 wxPyEndBlockThreads(state);
d559219f
RD
476}
477
cf694132
RD
478
479
7bf85405
RD
480// This function is used for all events destined for Python event handlers.
481void wxPyCallback::EventThunker(wxEvent& event) {
482 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
483 PyObject* func = cb->m_func;
484 PyObject* result;
485 PyObject* arg;
486 PyObject* tuple;
487
cf694132 488
19a97bd6 489 wxPyTState* state = wxPyBeginBlockThreads();
65dd82cb
RD
490 wxString className = event.GetClassInfo()->GetClassName();
491
492 if (className == "wxPyEvent")
493 arg = ((wxPyEvent*)&event)->GetSelf();
494 else if (className == "wxPyCommandEvent")
495 arg = ((wxPyCommandEvent*)&event)->GetSelf();
496 else
497 arg = wxPyConstructObject((void*)&event, className);
7bf85405
RD
498
499 tuple = PyTuple_New(1);
500 PyTuple_SET_ITEM(tuple, 0, arg);
501 result = PyEval_CallObject(func, tuple);
7bf85405
RD
502 Py_DECREF(tuple);
503 if (result) {
504 Py_DECREF(result);
ac346f50 505 PyErr_Clear(); // Just in case...
7bf85405
RD
506 } else {
507 PyErr_Print();
508 }
19a97bd6 509 wxPyEndBlockThreads(state);
7bf85405
RD
510}
511
512
d559219f 513//----------------------------------------------------------------------
7bf85405 514
2f90df85
RD
515wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) {
516 m_lastFound = NULL;
517 m_self = other.m_self;
f6bcfd97
BP
518 m_class = other.m_class;
519 if (m_self) {
2f90df85 520 Py_INCREF(m_self);
f6bcfd97
BP
521 Py_INCREF(m_class);
522 }
2f90df85
RD
523}
524
525
33510773 526void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) {
d559219f 527 m_self = self;
33510773 528 m_class = klass;
b7312675 529 m_incRef = incref;
f6bcfd97 530 if (incref) {
2f90df85 531 Py_INCREF(m_self);
f6bcfd97
BP
532 Py_INCREF(m_class);
533 }
d559219f 534}
8bf5d46e 535
8bf5d46e 536
f6bcfd97
BP
537// If the object (m_self) has an attibute of the given name, and if that
538// attribute is a method, and if that method's class is not from a base class,
539// then we'll save a pointer to the method so callCallback can call it.
de20db99 540bool wxPyCallbackHelper::findCallback(const char* name) const {
f6bcfd97
BP
541 wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const
542 self->m_lastFound = NULL;
de20db99 543 if (m_self && PyObject_HasAttrString(m_self, (char*)name)) {
f6bcfd97 544 PyObject* method;
de20db99 545 method = PyObject_GetAttrString(m_self, (char*)name);
f6bcfd97
BP
546
547 if (PyMethod_Check(method) &&
548 ((PyMethod_GET_CLASS(method) == m_class) ||
549 PyClass_IsSubclass(PyMethod_GET_CLASS(method), m_class))) {
8bf5d46e 550
f6bcfd97
BP
551 self->m_lastFound = method;
552 }
de20db99
RD
553 else {
554 Py_DECREF(method);
555 }
f6bcfd97 556 }
d559219f
RD
557 return m_lastFound != NULL;
558}
8bf5d46e 559
d559219f 560
f6bcfd97 561int wxPyCallbackHelper::callCallback(PyObject* argTuple) const {
d559219f
RD
562 PyObject* result;
563 int retval = FALSE;
564
565 result = callCallbackObj(argTuple);
566 if (result) { // Assumes an integer return type...
567 retval = PyInt_AsLong(result);
568 Py_DECREF(result);
569 PyErr_Clear(); // forget about it if it's not...
570 }
571 return retval;
572}
573
574// Invoke the Python callable object, returning the raw PyObject return
575// value. Caller should DECREF the return value and also call PyEval_SaveThread.
f6bcfd97 576PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const {
de20db99 577 PyObject* result;
d559219f 578
de20db99
RD
579 // Save a copy of the pointer in case the callback generates another
580 // callback. In that case m_lastFound will have a different value when
581 // it gets back here...
582 PyObject* method = m_lastFound;
583
584 result = PyEval_CallObject(method, argTuple);
d559219f 585 Py_DECREF(argTuple);
de20db99 586 Py_DECREF(method);
d559219f
RD
587 if (!result) {
588 PyErr_Print();
589 }
590 return result;
591}
714e6a9e 592
7bf85405 593
0122b7e3 594void wxPyCBH_setCallbackInfo(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) {
1e7ecb7b
RD
595 cbh.setSelf(self, klass, incref);
596}
597
598bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) {
599 return cbh.findCallback(name);
600}
601
602int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
603 return cbh.callCallback(argTuple);
604}
605
606PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
607 return cbh.callCallbackObj(argTuple);
608}
609
610
611void wxPyCBH_delete(wxPyCallbackHelper* cbh) {
1e7ecb7b 612 if (cbh->m_incRef) {
19a97bd6 613 wxPyTState* state = wxPyBeginBlockThreads();
1e7ecb7b
RD
614 Py_XDECREF(cbh->m_self);
615 Py_XDECREF(cbh->m_class);
19a97bd6 616 wxPyEndBlockThreads(state);
1e7ecb7b 617 }
1e7ecb7b 618}
d559219f 619
65dd82cb
RD
620//---------------------------------------------------------------------------
621//---------------------------------------------------------------------------
622// These classes can be derived from in Python and passed through the event
c368d904 623// system without losing anything. They do this by keeping a reference to
65dd82cb
RD
624// themselves and some special case handling in wxPyCallback::EventThunker.
625
626
e19b7164 627wxPyEvtSelfRef::wxPyEvtSelfRef() {
65dd82cb
RD
628 //m_self = Py_None; // **** We don't do normal ref counting to prevent
629 //Py_INCREF(m_self); // circular loops...
194fa2ac 630 m_cloned = FALSE;
65dd82cb
RD
631}
632
e19b7164 633wxPyEvtSelfRef::~wxPyEvtSelfRef() {
19a97bd6 634 wxPyTState* state = wxPyBeginBlockThreads();
65dd82cb
RD
635 if (m_cloned)
636 Py_DECREF(m_self);
19a97bd6 637 wxPyEndBlockThreads(state);
65dd82cb
RD
638}
639
e19b7164 640void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) {
19a97bd6 641 wxPyTState* state = wxPyBeginBlockThreads();
65dd82cb
RD
642 if (m_cloned)
643 Py_DECREF(m_self);
644 m_self = self;
645 if (clone) {
646 Py_INCREF(m_self);
647 m_cloned = TRUE;
648 }
19a97bd6 649 wxPyEndBlockThreads(state);
65dd82cb
RD
650}
651
e19b7164 652PyObject* wxPyEvtSelfRef::GetSelf() const {
65dd82cb
RD
653 Py_INCREF(m_self);
654 return m_self;
655}
656
657
658wxPyEvent::wxPyEvent(int id)
659 : wxEvent(id) {
660}
661
662wxPyEvent::~wxPyEvent() {
663}
664
665// This one is so the event object can be Cloned...
666void wxPyEvent::CopyObject(wxObject& dest) const {
667 wxEvent::CopyObject(dest);
668 ((wxPyEvent*)&dest)->SetSelf(m_self, TRUE);
669}
670
671
672IMPLEMENT_DYNAMIC_CLASS(wxPyEvent, wxEvent);
673
674
675wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id)
676 : wxCommandEvent(commandType, id) {
677}
678
679wxPyCommandEvent::~wxPyCommandEvent() {
680}
681
682void wxPyCommandEvent::CopyObject(wxObject& dest) const {
683 wxCommandEvent::CopyObject(dest);
684 ((wxPyCommandEvent*)&dest)->SetSelf(m_self, TRUE);
685}
686
687
688IMPLEMENT_DYNAMIC_CLASS(wxPyCommandEvent, wxCommandEvent);
689
690
691
d559219f 692//---------------------------------------------------------------------------
7bf85405
RD
693//---------------------------------------------------------------------------
694
d559219f 695
7bf85405
RD
696wxPyTimer::wxPyTimer(PyObject* callback) {
697 func = callback;
698 Py_INCREF(func);
699}
700
701wxPyTimer::~wxPyTimer() {
19a97bd6 702 wxPyTState* state = wxPyBeginBlockThreads();
7bf85405 703 Py_DECREF(func);
19a97bd6 704 wxPyEndBlockThreads(state);
7bf85405
RD
705}
706
707void wxPyTimer::Notify() {
185d7c3e
RD
708 if (!func || func == Py_None) {
709 wxTimer::Notify();
710 }
711 else {
19a97bd6 712 wxPyTState* state = wxPyBeginBlockThreads();
185d7c3e
RD
713
714 PyObject* result;
715 PyObject* args = Py_BuildValue("()");
716
717 result = PyEval_CallObject(func, args);
718 Py_DECREF(args);
719 if (result) {
720 Py_DECREF(result);
721 PyErr_Clear();
722 } else {
723 PyErr_Print();
724 }
7bf85405 725
19a97bd6 726 wxPyEndBlockThreads(state);
7bf85405
RD
727 }
728}
729
730
cf694132 731
2f90df85 732//---------------------------------------------------------------------------
389c5527
RD
733//---------------------------------------------------------------------------
734// Convert a wxList to a Python List
735
65dd82cb 736PyObject* wxPy_ConvertList(wxListBase* list, const char* className) {
389c5527
RD
737 PyObject* pyList;
738 PyObject* pyObj;
739 wxObject* wxObj;
740 wxNode* node = list->First();
741
19a97bd6 742 wxPyTState* state = wxPyBeginBlockThreads();
389c5527
RD
743 pyList = PyList_New(0);
744 while (node) {
745 wxObj = node->Data();
9416aa89 746 pyObj = wxPyMake_wxObject(wxObj); //wxPyConstructObject(wxObj, className);
389c5527
RD
747 PyList_Append(pyList, pyObj);
748 node = node->Next();
749 }
19a97bd6 750 wxPyEndBlockThreads(state);
389c5527
RD
751 return pyList;
752}
753
54b96882
RD
754//----------------------------------------------------------------------
755
756long wxPyGetWinHandle(wxWindow* win) {
757#ifdef __WXMSW__
758 return (long)win->GetHandle();
759#endif
760
761 // Find and return the actual X-Window.
762#ifdef __WXGTK__
763 if (win->m_wxwindow) {
764 GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window;
765 if (bwin) {
766 return (long)bwin->xwindow;
767 }
768 }
769#endif
770 return 0;
771}
772
7bf85405
RD
773//----------------------------------------------------------------------
774// Some helper functions for typemaps in my_typemaps.i, so they won't be
389c5527 775// included in every file...
7bf85405
RD
776
777
2f90df85 778byte* byte_LIST_helper(PyObject* source) {
b639c3c5
RD
779 if (!PyList_Check(source)) {
780 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
781 return NULL;
782 }
783 int count = PyList_Size(source);
784 byte* temp = new byte[count];
785 if (! temp) {
786 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
787 return NULL;
788 }
789 for (int x=0; x<count; x++) {
790 PyObject* o = PyList_GetItem(source, x);
791 if (! PyInt_Check(o)) {
792 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
793 return NULL;
794 }
795 temp[x] = (byte)PyInt_AsLong(o);
796 }
797 return temp;
798}
799
800
2f90df85 801int* int_LIST_helper(PyObject* source) {
7bf85405
RD
802 if (!PyList_Check(source)) {
803 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
804 return NULL;
805 }
806 int count = PyList_Size(source);
807 int* temp = new int[count];
808 if (! temp) {
809 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
810 return NULL;
811 }
812 for (int x=0; x<count; x++) {
813 PyObject* o = PyList_GetItem(source, x);
814 if (! PyInt_Check(o)) {
815 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
816 return NULL;
817 }
818 temp[x] = PyInt_AsLong(o);
819 }
820 return temp;
821}
822
823
2f90df85 824long* long_LIST_helper(PyObject* source) {
7bf85405
RD
825 if (!PyList_Check(source)) {
826 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
827 return NULL;
828 }
829 int count = PyList_Size(source);
830 long* temp = new long[count];
831 if (! temp) {
832 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
833 return NULL;
834 }
835 for (int x=0; x<count; x++) {
836 PyObject* o = PyList_GetItem(source, x);
837 if (! PyInt_Check(o)) {
838 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
839 return NULL;
840 }
841 temp[x] = PyInt_AsLong(o);
842 }
843 return temp;
844}
845
846
2f90df85 847char** string_LIST_helper(PyObject* source) {
7bf85405
RD
848 if (!PyList_Check(source)) {
849 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
850 return NULL;
851 }
852 int count = PyList_Size(source);
853 char** temp = new char*[count];
854 if (! temp) {
855 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
856 return NULL;
857 }
858 for (int x=0; x<count; x++) {
859 PyObject* o = PyList_GetItem(source, x);
860 if (! PyString_Check(o)) {
861 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
862 return NULL;
863 }
864 temp[x] = PyString_AsString(o);
865 }
866 return temp;
867}
868
e0672e2f
RD
869//--------------------------------
870// Part of patch from Tim Hochberg
871static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) {
872 if (PyInt_Check(o1) && PyInt_Check(o2)) {
873 point->x = PyInt_AS_LONG(o1);
874 point->y = PyInt_AS_LONG(o2);
875 return true;
876 }
877 if (PyFloat_Check(o1) && PyFloat_Check(o2)) {
878 point->x = (int)PyFloat_AS_DOUBLE(o1);
879 point->y = (int)PyFloat_AS_DOUBLE(o2);
880 return true;
881 }
882 if (PyInstance_Check(o1) || PyInstance_Check(o2)) {
883 // Disallow instances because they can cause havok
884 return false;
885 }
886 if (PyNumber_Check(o1) && PyNumber_Check(o2)) {
887 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
888 point->x = PyInt_AsLong(o1);
889 point->y = PyInt_AsLong(o2);
890 return true;
891 }
892 return false;
893}
7bf85405
RD
894
895
a7fa65b9
RD
896#if PYTHON_API_VERSION < 1009
897#define PySequence_Fast_GET_ITEM(o, i)\
898 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
899#endif
900
e0672e2f
RD
901wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) {
902 // Putting all of the declarations here allows
903 // us to put the error handling all in one place.
904 int x;
905 wxPoint* temp;
906 PyObject *o, *o1, *o2;
907 int isFast = PyList_Check(source) || PyTuple_Check(source);
908
909 // The length of the sequence is returned in count.
910 if (!PySequence_Check(source)) {
911 goto error0;
7bf85405 912 }
e0672e2f
RD
913 *count = PySequence_Length(source);
914 if (*count < 0) {
915 goto error0;
916 }
917
918 temp = new wxPoint[*count];
919 if (!temp) {
7bf85405
RD
920 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
921 return NULL;
922 }
e0672e2f
RD
923 for (x=0; x<*count; x++) {
924 // Get an item: try fast way first.
925 if (isFast) {
926 o = PySequence_Fast_GET_ITEM(source, x);
927 }
928 else {
929 o = PySequence_GetItem(source, x);
930 if (o == NULL) {
931 goto error1;
932 }
933 }
7bf85405 934
e0672e2f
RD
935 // Convert o to wxPoint.
936 if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) ||
937 (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) {
938 o1 = PySequence_Fast_GET_ITEM(o, 0);
939 o2 = PySequence_Fast_GET_ITEM(o, 1);
940 if (!wxPointFromObjects(o1, o2, &temp[x])) {
941 goto error2;
942 }
7bf85405 943 }
d559219f
RD
944 else if (PyInstance_Check(o)) {
945 wxPoint* pt;
e0672e2f
RD
946 if (SWIG_GetPtrObj(o, (void **)&pt, "_wxPoint_p")) {
947 goto error2;
d559219f
RD
948 }
949 temp[x] = *pt;
950 }
e0672e2f
RD
951 else if (PySequence_Check(o) && PySequence_Length(o) == 2) {
952 o1 = PySequence_GetItem(o, 0);
953 o2 = PySequence_GetItem(o, 1);
954 if (!wxPointFromObjects(o1, o2, &temp[x])) {
955 goto error3;
956 }
957 Py_DECREF(o1);
958 Py_DECREF(o2);
959 }
7bf85405 960 else {
e0672e2f 961 goto error2;
7bf85405 962 }
e0672e2f
RD
963 // Clean up.
964 if (!isFast)
965 Py_DECREF(o);
7bf85405
RD
966 }
967 return temp;
e0672e2f
RD
968
969error3:
970 Py_DECREF(o1);
971 Py_DECREF(o2);
972error2:
973 if (!isFast)
974 Py_DECREF(o);
975error1:
976 delete temp;
977error0:
978 PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints.");
979 return NULL;
7bf85405 980}
e0672e2f
RD
981// end of patch
982//------------------------------
7bf85405
RD
983
984
2f90df85 985wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
7bf85405
RD
986 if (!PyList_Check(source)) {
987 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
988 return NULL;
989 }
990 int count = PyList_Size(source);
991 wxBitmap** temp = new wxBitmap*[count];
992 if (! temp) {
993 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
994 return NULL;
995 }
996 for (int x=0; x<count; x++) {
997 PyObject* o = PyList_GetItem(source, x);
e166644c 998 if (PyInstance_Check(o)) {
7bf85405 999 wxBitmap* pt;
e166644c 1000 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) {
7bf85405
RD
1001 PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p.");
1002 return NULL;
1003 }
1004 temp[x] = pt;
1005 }
1006 else {
1007 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
1008 return NULL;
1009 }
1010 }
1011 return temp;
1012}
1013
1014
1015
2f90df85 1016wxString* wxString_LIST_helper(PyObject* source) {
7bf85405
RD
1017 if (!PyList_Check(source)) {
1018 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1019 return NULL;
1020 }
1021 int count = PyList_Size(source);
1022 wxString* temp = new wxString[count];
1023 if (! temp) {
1024 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1025 return NULL;
1026 }
1027 for (int x=0; x<count; x++) {
1028 PyObject* o = PyList_GetItem(source, x);
ecc08ead
RD
1029#if PYTHON_API_VERSION >= 1009
1030 if (! PyString_Check(o) && ! PyUnicode_Check(o)) {
1031 PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects.");
1032 return NULL;
1033 }
1034
1035 char* buff;
1036 int length;
1037 if (PyString_AsStringAndSize(o, &buff, &length) == -1)
1038 return NULL;
1039 temp[x] = wxString(buff, length);
1040#else
7bf85405
RD
1041 if (! PyString_Check(o)) {
1042 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
1043 return NULL;
1044 }
1045 temp[x] = PyString_AsString(o);
ecc08ead 1046#endif
7bf85405
RD
1047 }
1048 return temp;
1049}
1050
1051
2f90df85 1052wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
7bf85405
RD
1053 if (!PyList_Check(source)) {
1054 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1055 return NULL;
1056 }
1057 int count = PyList_Size(source);
1058 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
1059 if (! temp) {
1060 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1061 return NULL;
1062 }
1063 for (int x=0; x<count; x++) {
1064 PyObject* o = PyList_GetItem(source, x);
e166644c 1065 if (PyInstance_Check(o)) {
7bf85405 1066 wxAcceleratorEntry* ae;
e166644c 1067 if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) {
7bf85405
RD
1068 PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p.");
1069 return NULL;
1070 }
1071 temp[x] = *ae;
1072 }
1073 else if (PyTuple_Check(o)) {
1074 PyObject* o1 = PyTuple_GetItem(o, 0);
1075 PyObject* o2 = PyTuple_GetItem(o, 1);
1076 PyObject* o3 = PyTuple_GetItem(o, 2);
0adbc166 1077 temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
7bf85405
RD
1078 }
1079 else {
1080 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1081 return NULL;
1082 }
1083 }
1084 return temp;
1085}
1086
bb0054cd
RD
1087
1088
1089//----------------------------------------------------------------------
bb0054cd 1090
2f90df85
RD
1091bool wxSize_helper(PyObject* source, wxSize** obj) {
1092
1093 // If source is an object instance then it may already be the right type
1094 if (PyInstance_Check(source)) {
1095 wxSize* ptr;
1096 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p"))
1097 goto error;
1098 *obj = ptr;
1099 return TRUE;
1100 }
1101 // otherwise a 2-tuple of integers is expected
1102 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1103 PyObject* o1 = PySequence_GetItem(source, 0);
1104 PyObject* o2 = PySequence_GetItem(source, 1);
1105 **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2));
1106 return TRUE;
1107 }
1108
1109 error:
1110 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object.");
1111 return FALSE;
1112}
1113
1114bool wxPoint_helper(PyObject* source, wxPoint** obj) {
1115
1116 // If source is an object instance then it may already be the right type
1117 if (PyInstance_Check(source)) {
1118 wxPoint* ptr;
1119 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p"))
1120 goto error;
1121 *obj = ptr;
1122 return TRUE;
1123 }
e0672e2f
RD
1124 // otherwise a length-2 sequence of integers is expected
1125 if (PySequence_Check(source) && PySequence_Length(source) == 2) {
2f90df85
RD
1126 PyObject* o1 = PySequence_GetItem(source, 0);
1127 PyObject* o2 = PySequence_GetItem(source, 1);
e0672e2f
RD
1128 // This should really check for integers, not numbers -- but that would break code.
1129 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
1130 Py_DECREF(o1);
1131 Py_DECREF(o2);
1132 goto error;
1133 }
1134 **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2));
1135 Py_DECREF(o1);
1136 Py_DECREF(o2);
2f90df85
RD
1137 return TRUE;
1138 }
2f90df85
RD
1139 error:
1140 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object.");
1141 return FALSE;
1142}
1143
1144
1145
1146bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) {
1147
1148 // If source is an object instance then it may already be the right type
1149 if (PyInstance_Check(source)) {
1150 wxRealPoint* ptr;
1151 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p"))
1152 goto error;
1153 *obj = ptr;
1154 return TRUE;
1155 }
1156 // otherwise a 2-tuple of floats is expected
1157 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1158 PyObject* o1 = PySequence_GetItem(source, 0);
1159 PyObject* o2 = PySequence_GetItem(source, 1);
1160 **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
1161 return TRUE;
1162 }
1163
1164 error:
1165 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object.");
1166 return FALSE;
1167}
1168
1169
1170
1171
1172bool wxRect_helper(PyObject* source, wxRect** obj) {
1173
1174 // If source is an object instance then it may already be the right type
1175 if (PyInstance_Check(source)) {
1176 wxRect* ptr;
1177 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p"))
1178 goto error;
1179 *obj = ptr;
1180 return TRUE;
1181 }
1182 // otherwise a 4-tuple of integers is expected
1183 else if (PySequence_Check(source) && PyObject_Length(source) == 4) {
1184 PyObject* o1 = PySequence_GetItem(source, 0);
1185 PyObject* o2 = PySequence_GetItem(source, 1);
1186 PyObject* o3 = PySequence_GetItem(source, 2);
1187 PyObject* o4 = PySequence_GetItem(source, 3);
1188 **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2),
1189 PyInt_AsLong(o3), PyInt_AsLong(o4));
1190 return TRUE;
1191 }
1192
1193 error:
1194 PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object.");
1195 return FALSE;
1196}
1197
1198
1199
f6bcfd97
BP
1200bool wxColour_helper(PyObject* source, wxColour** obj) {
1201
1202 // If source is an object instance then it may already be the right type
1203 if (PyInstance_Check(source)) {
1204 wxColour* ptr;
1205 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p"))
1206 goto error;
1207 *obj = ptr;
1208 return TRUE;
1209 }
1210 // otherwise a string is expected
1211 else if (PyString_Check(source)) {
1212 wxString spec = PyString_AS_STRING(source);
1112b0c6 1213 if (spec[0U] == '#' && spec.Length() == 7) { // It's #RRGGBB
f6bcfd97
BP
1214 char* junk;
1215 int red = strtol(spec.Mid(1,2), &junk, 16);
1216 int green = strtol(spec.Mid(3,2), &junk, 16);
1217 int blue = strtol(spec.Mid(5,2), &junk, 16);
1218 **obj = wxColour(red, green, blue);
1219 return TRUE;
1220 }
1221 else { // it's a colour name
1222 **obj = wxColour(spec);
1223 return TRUE;
1224 }
1225 }
1226
1227 error:
de20db99 1228 PyErr_SetString(PyExc_TypeError, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
f6bcfd97
BP
1229 return FALSE;
1230}
1231
1232
bb0054cd
RD
1233//----------------------------------------------------------------------
1234//----------------------------------------------------------------------
7bf85405 1235//----------------------------------------------------------------------
7bf85405 1236
7bf85405 1237
7bf85405 1238
de20db99 1239