]> git.saurik.com Git - wxWidgets.git/blame - wxPython/src/helpers.cpp
fixed crash in OnSize() handler
[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) {
293 PyObject* target;
294
295 if (source) {
296 wxClassInfo* info = source->GetClassInfo();
297 wxChar* name = (wxChar*)info->GetClassName();
298 PyObject* klass = wxPyClassExists(name);
299 while (info && !klass) {
300 name = (wxChar*)info->GetBaseClassName1();
301 info = wxClassInfo::FindClass(name);
302 klass = wxPyClassExists(name);
303 }
304 if (info) {
305 target = wxPyConstructObject(source, name, klass, FALSE);
306 } else {
307 wxString msg("wxPython class not found for ");
308 msg += source->GetClassInfo()->GetClassName();
309 PyErr_SetString(PyExc_NameError, msg.c_str());
493f1553 310 target = NULL;
9416aa89
RD
311 }
312 } else { // source was NULL so return None.
313 Py_INCREF(Py_None); target = Py_None;
314 }
315 return target;
316}
317
7bf85405
RD
318//---------------------------------------------------------------------------
319
c368d904 320PyObject* wxPyConstructObject(void* ptr,
1e7ecb7b 321 const char* className,
9416aa89 322 PyObject* klass,
1e7ecb7b 323 int setThisOwn) {
9416aa89 324
33510773
RD
325 PyObject* obj;
326 PyObject* arg;
9416aa89
RD
327 PyObject* item;
328 char swigptr[64]; // should always be big enough...
329 char buff[64];
330
331 if ((item = PyDict_GetItemString(wxPyPtrTypeMap, (char*)className)) != NULL) {
332 className = PyString_AsString(item);
333 }
334 sprintf(buff, "_%s_p", className);
335 SWIG_MakePtr(swigptr, ptr, buff);
336
337 arg = Py_BuildValue("(s)", swigptr);
338 obj = PyInstance_New(klass, arg, NULL);
339 Py_DECREF(arg);
340
341 if (setThisOwn) {
342 PyObject* one = PyInt_FromLong(1);
343 PyObject_SetAttrString(obj, "thisown", one);
344 Py_DECREF(one);
345 }
346
347 return obj;
348}
349
350
351PyObject* wxPyConstructObject(void* ptr,
352 const char* className,
353 int setThisOwn) {
354 PyObject* obj;
33510773
RD
355
356 if (!ptr) {
357 Py_INCREF(Py_None);
358 return Py_None;
359 }
360
9c039d08 361 char buff[64]; // should always be big enough...
7bf85405
RD
362
363 sprintf(buff, "%sPtr", className);
364 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
365 if (! classobj) {
33510773
RD
366 char temp[128];
367 sprintf(temp,
368 "*** Unknown class name %s, tell Robin about it please ***",
369 buff);
370 obj = PyString_FromString(temp);
371 return obj;
7bf85405
RD
372 }
373
9416aa89 374 return wxPyConstructObject(ptr, className, classobj, setThisOwn);
7bf85405
RD
375}
376
d559219f 377//---------------------------------------------------------------------------
7bf85405 378
19a97bd6
RD
379// static PyThreadState* myPyThreadState_Get() {
380// PyThreadState* current;
381// current = PyThreadState_Swap(NULL);
382// PyThreadState_Swap(current);
383// return current;
384// }
385
386
387// bool wxPyRestoreThread() {
388// // NOTE: The Python API docs state that if a thread already has the
389// // interpreter lock and calls PyEval_RestoreThread again a deadlock
390// // occurs, so I put in this code as a guard condition since there are
391// // many possibilites for nested events and callbacks in wxPython. If
392// // The current thread is our thread, then we can assume that we
393// // already have the lock. (I hope!)
394// //
395// #ifdef WXP_WITH_THREAD
396// if (wxPyEventThreadState != myPyThreadState_Get()) {
397// PyEval_AcquireThread(wxPyEventThreadState);
398// return TRUE;
399// }
400// else
401// #endif
402// return FALSE;
403// }
404
405
406// void wxPySaveThread(bool doSave) {
407// #ifdef WXP_WITH_THREAD
408// if (doSave) {
409// PyEval_ReleaseThread(wxPyEventThreadState);
410// }
411// #endif
412// }
413
414
415
416wxPyTState* wxPyBeginBlockThreads() {
417 wxPyTState* state = NULL;
cf694132 418#ifdef WXP_WITH_THREAD
19a97bd6
RD
419 if (1) { // Can I check if I've already got the lock?
420 state = new wxPyTState;
421 PyEval_AcquireLock();
422 state->newState = PyThreadState_New(wxPyInterpreter);
423 state->prevState = PyThreadState_Swap(state->newState);
1112b0c6 424 }
cf694132 425#endif
19a97bd6 426 return state;
d559219f 427}
cf694132 428
cf694132 429
19a97bd6 430void wxPyEndBlockThreads(wxPyTState* state) {
cf694132 431#ifdef WXP_WITH_THREAD
19a97bd6
RD
432 if (state) {
433 PyThreadState_Swap(state->prevState);
434 PyThreadState_Clear(state->newState);
435 PyEval_ReleaseLock();
436 PyThreadState_Delete(state->newState);
437 delete state;
1112b0c6
RD
438 }
439#endif
cf694132
RD
440}
441
19a97bd6 442
d559219f
RD
443//---------------------------------------------------------------------------
444
2f90df85
RD
445IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject);
446
d559219f
RD
447wxPyCallback::wxPyCallback(PyObject* func) {
448 m_func = func;
449 Py_INCREF(m_func);
450}
451
2f90df85
RD
452wxPyCallback::wxPyCallback(const wxPyCallback& other) {
453 m_func = other.m_func;
454 Py_INCREF(m_func);
455}
456
d559219f 457wxPyCallback::~wxPyCallback() {
19a97bd6 458 wxPyTState* state = wxPyBeginBlockThreads();
d559219f 459 Py_DECREF(m_func);
19a97bd6 460 wxPyEndBlockThreads(state);
d559219f
RD
461}
462
cf694132
RD
463
464
7bf85405
RD
465// This function is used for all events destined for Python event handlers.
466void wxPyCallback::EventThunker(wxEvent& event) {
467 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
468 PyObject* func = cb->m_func;
469 PyObject* result;
470 PyObject* arg;
471 PyObject* tuple;
472
cf694132 473
19a97bd6 474 wxPyTState* state = wxPyBeginBlockThreads();
65dd82cb
RD
475 wxString className = event.GetClassInfo()->GetClassName();
476
477 if (className == "wxPyEvent")
478 arg = ((wxPyEvent*)&event)->GetSelf();
479 else if (className == "wxPyCommandEvent")
480 arg = ((wxPyCommandEvent*)&event)->GetSelf();
481 else
482 arg = wxPyConstructObject((void*)&event, className);
7bf85405
RD
483
484 tuple = PyTuple_New(1);
485 PyTuple_SET_ITEM(tuple, 0, arg);
486 result = PyEval_CallObject(func, tuple);
7bf85405
RD
487 Py_DECREF(tuple);
488 if (result) {
489 Py_DECREF(result);
ac346f50 490 PyErr_Clear(); // Just in case...
7bf85405
RD
491 } else {
492 PyErr_Print();
493 }
19a97bd6 494 wxPyEndBlockThreads(state);
7bf85405
RD
495}
496
497
d559219f 498//----------------------------------------------------------------------
7bf85405 499
2f90df85
RD
500wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) {
501 m_lastFound = NULL;
502 m_self = other.m_self;
f6bcfd97
BP
503 m_class = other.m_class;
504 if (m_self) {
2f90df85 505 Py_INCREF(m_self);
f6bcfd97
BP
506 Py_INCREF(m_class);
507 }
2f90df85
RD
508}
509
510
33510773 511void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) {
d559219f 512 m_self = self;
33510773 513 m_class = klass;
b7312675 514 m_incRef = incref;
f6bcfd97 515 if (incref) {
2f90df85 516 Py_INCREF(m_self);
f6bcfd97
BP
517 Py_INCREF(m_class);
518 }
d559219f 519}
8bf5d46e 520
8bf5d46e 521
f6bcfd97
BP
522// If the object (m_self) has an attibute of the given name, and if that
523// attribute is a method, and if that method's class is not from a base class,
524// then we'll save a pointer to the method so callCallback can call it.
de20db99 525bool wxPyCallbackHelper::findCallback(const char* name) const {
f6bcfd97
BP
526 wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const
527 self->m_lastFound = NULL;
de20db99 528 if (m_self && PyObject_HasAttrString(m_self, (char*)name)) {
f6bcfd97 529 PyObject* method;
de20db99 530 method = PyObject_GetAttrString(m_self, (char*)name);
f6bcfd97
BP
531
532 if (PyMethod_Check(method) &&
533 ((PyMethod_GET_CLASS(method) == m_class) ||
534 PyClass_IsSubclass(PyMethod_GET_CLASS(method), m_class))) {
8bf5d46e 535
f6bcfd97
BP
536 self->m_lastFound = method;
537 }
de20db99
RD
538 else {
539 Py_DECREF(method);
540 }
f6bcfd97 541 }
d559219f
RD
542 return m_lastFound != NULL;
543}
8bf5d46e 544
d559219f 545
f6bcfd97 546int wxPyCallbackHelper::callCallback(PyObject* argTuple) const {
d559219f
RD
547 PyObject* result;
548 int retval = FALSE;
549
550 result = callCallbackObj(argTuple);
551 if (result) { // Assumes an integer return type...
552 retval = PyInt_AsLong(result);
553 Py_DECREF(result);
554 PyErr_Clear(); // forget about it if it's not...
555 }
556 return retval;
557}
558
559// Invoke the Python callable object, returning the raw PyObject return
560// value. Caller should DECREF the return value and also call PyEval_SaveThread.
f6bcfd97 561PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const {
de20db99 562 PyObject* result;
d559219f 563
de20db99
RD
564 // Save a copy of the pointer in case the callback generates another
565 // callback. In that case m_lastFound will have a different value when
566 // it gets back here...
567 PyObject* method = m_lastFound;
568
569 result = PyEval_CallObject(method, argTuple);
d559219f 570 Py_DECREF(argTuple);
de20db99 571 Py_DECREF(method);
d559219f
RD
572 if (!result) {
573 PyErr_Print();
574 }
575 return result;
576}
714e6a9e 577
7bf85405 578
1e7ecb7b
RD
579void wxPyCBH_setSelf(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) {
580 cbh.setSelf(self, klass, incref);
581}
582
583bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) {
584 return cbh.findCallback(name);
585}
586
587int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
588 return cbh.callCallback(argTuple);
589}
590
591PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) {
592 return cbh.callCallbackObj(argTuple);
593}
594
595
596void wxPyCBH_delete(wxPyCallbackHelper* cbh) {
1e7ecb7b 597 if (cbh->m_incRef) {
19a97bd6 598 wxPyTState* state = wxPyBeginBlockThreads();
1e7ecb7b
RD
599 Py_XDECREF(cbh->m_self);
600 Py_XDECREF(cbh->m_class);
19a97bd6 601 wxPyEndBlockThreads(state);
1e7ecb7b 602 }
1e7ecb7b 603}
d559219f 604
65dd82cb
RD
605//---------------------------------------------------------------------------
606//---------------------------------------------------------------------------
607// These classes can be derived from in Python and passed through the event
c368d904 608// system without losing anything. They do this by keeping a reference to
65dd82cb
RD
609// themselves and some special case handling in wxPyCallback::EventThunker.
610
611
e19b7164 612wxPyEvtSelfRef::wxPyEvtSelfRef() {
65dd82cb
RD
613 //m_self = Py_None; // **** We don't do normal ref counting to prevent
614 //Py_INCREF(m_self); // circular loops...
194fa2ac 615 m_cloned = FALSE;
65dd82cb
RD
616}
617
e19b7164 618wxPyEvtSelfRef::~wxPyEvtSelfRef() {
19a97bd6 619 wxPyTState* state = wxPyBeginBlockThreads();
65dd82cb
RD
620 if (m_cloned)
621 Py_DECREF(m_self);
19a97bd6 622 wxPyEndBlockThreads(state);
65dd82cb
RD
623}
624
e19b7164 625void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) {
19a97bd6 626 wxPyTState* state = wxPyBeginBlockThreads();
65dd82cb
RD
627 if (m_cloned)
628 Py_DECREF(m_self);
629 m_self = self;
630 if (clone) {
631 Py_INCREF(m_self);
632 m_cloned = TRUE;
633 }
19a97bd6 634 wxPyEndBlockThreads(state);
65dd82cb
RD
635}
636
e19b7164 637PyObject* wxPyEvtSelfRef::GetSelf() const {
65dd82cb
RD
638 Py_INCREF(m_self);
639 return m_self;
640}
641
642
643wxPyEvent::wxPyEvent(int id)
644 : wxEvent(id) {
645}
646
647wxPyEvent::~wxPyEvent() {
648}
649
650// This one is so the event object can be Cloned...
651void wxPyEvent::CopyObject(wxObject& dest) const {
652 wxEvent::CopyObject(dest);
653 ((wxPyEvent*)&dest)->SetSelf(m_self, TRUE);
654}
655
656
657IMPLEMENT_DYNAMIC_CLASS(wxPyEvent, wxEvent);
658
659
660wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id)
661 : wxCommandEvent(commandType, id) {
662}
663
664wxPyCommandEvent::~wxPyCommandEvent() {
665}
666
667void wxPyCommandEvent::CopyObject(wxObject& dest) const {
668 wxCommandEvent::CopyObject(dest);
669 ((wxPyCommandEvent*)&dest)->SetSelf(m_self, TRUE);
670}
671
672
673IMPLEMENT_DYNAMIC_CLASS(wxPyCommandEvent, wxCommandEvent);
674
675
676
d559219f 677//---------------------------------------------------------------------------
7bf85405
RD
678//---------------------------------------------------------------------------
679
d559219f 680
7bf85405
RD
681wxPyTimer::wxPyTimer(PyObject* callback) {
682 func = callback;
683 Py_INCREF(func);
684}
685
686wxPyTimer::~wxPyTimer() {
19a97bd6 687 wxPyTState* state = wxPyBeginBlockThreads();
7bf85405 688 Py_DECREF(func);
19a97bd6 689 wxPyEndBlockThreads(state);
7bf85405
RD
690}
691
692void wxPyTimer::Notify() {
185d7c3e
RD
693 if (!func || func == Py_None) {
694 wxTimer::Notify();
695 }
696 else {
19a97bd6 697 wxPyTState* state = wxPyBeginBlockThreads();
185d7c3e
RD
698
699 PyObject* result;
700 PyObject* args = Py_BuildValue("()");
701
702 result = PyEval_CallObject(func, args);
703 Py_DECREF(args);
704 if (result) {
705 Py_DECREF(result);
706 PyErr_Clear();
707 } else {
708 PyErr_Print();
709 }
7bf85405 710
19a97bd6 711 wxPyEndBlockThreads(state);
7bf85405
RD
712 }
713}
714
715
cf694132 716
2f90df85 717//---------------------------------------------------------------------------
389c5527
RD
718//---------------------------------------------------------------------------
719// Convert a wxList to a Python List
720
65dd82cb 721PyObject* wxPy_ConvertList(wxListBase* list, const char* className) {
389c5527
RD
722 PyObject* pyList;
723 PyObject* pyObj;
724 wxObject* wxObj;
725 wxNode* node = list->First();
726
19a97bd6 727 wxPyTState* state = wxPyBeginBlockThreads();
389c5527
RD
728 pyList = PyList_New(0);
729 while (node) {
730 wxObj = node->Data();
9416aa89 731 pyObj = wxPyMake_wxObject(wxObj); //wxPyConstructObject(wxObj, className);
389c5527
RD
732 PyList_Append(pyList, pyObj);
733 node = node->Next();
734 }
19a97bd6 735 wxPyEndBlockThreads(state);
389c5527
RD
736 return pyList;
737}
738
54b96882
RD
739//----------------------------------------------------------------------
740
741long wxPyGetWinHandle(wxWindow* win) {
742#ifdef __WXMSW__
743 return (long)win->GetHandle();
744#endif
745
746 // Find and return the actual X-Window.
747#ifdef __WXGTK__
748 if (win->m_wxwindow) {
749 GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window;
750 if (bwin) {
751 return (long)bwin->xwindow;
752 }
753 }
754#endif
755 return 0;
756}
757
7bf85405
RD
758//----------------------------------------------------------------------
759// Some helper functions for typemaps in my_typemaps.i, so they won't be
389c5527 760// included in every file...
7bf85405
RD
761
762
2f90df85 763byte* byte_LIST_helper(PyObject* source) {
b639c3c5
RD
764 if (!PyList_Check(source)) {
765 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
766 return NULL;
767 }
768 int count = PyList_Size(source);
769 byte* temp = new byte[count];
770 if (! temp) {
771 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
772 return NULL;
773 }
774 for (int x=0; x<count; x++) {
775 PyObject* o = PyList_GetItem(source, x);
776 if (! PyInt_Check(o)) {
777 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
778 return NULL;
779 }
780 temp[x] = (byte)PyInt_AsLong(o);
781 }
782 return temp;
783}
784
785
2f90df85 786int* int_LIST_helper(PyObject* source) {
7bf85405
RD
787 if (!PyList_Check(source)) {
788 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
789 return NULL;
790 }
791 int count = PyList_Size(source);
792 int* temp = new int[count];
793 if (! temp) {
794 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
795 return NULL;
796 }
797 for (int x=0; x<count; x++) {
798 PyObject* o = PyList_GetItem(source, x);
799 if (! PyInt_Check(o)) {
800 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
801 return NULL;
802 }
803 temp[x] = PyInt_AsLong(o);
804 }
805 return temp;
806}
807
808
2f90df85 809long* long_LIST_helper(PyObject* source) {
7bf85405
RD
810 if (!PyList_Check(source)) {
811 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
812 return NULL;
813 }
814 int count = PyList_Size(source);
815 long* temp = new long[count];
816 if (! temp) {
817 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
818 return NULL;
819 }
820 for (int x=0; x<count; x++) {
821 PyObject* o = PyList_GetItem(source, x);
822 if (! PyInt_Check(o)) {
823 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
824 return NULL;
825 }
826 temp[x] = PyInt_AsLong(o);
827 }
828 return temp;
829}
830
831
2f90df85 832char** string_LIST_helper(PyObject* source) {
7bf85405
RD
833 if (!PyList_Check(source)) {
834 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
835 return NULL;
836 }
837 int count = PyList_Size(source);
838 char** temp = new char*[count];
839 if (! temp) {
840 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
841 return NULL;
842 }
843 for (int x=0; x<count; x++) {
844 PyObject* o = PyList_GetItem(source, x);
845 if (! PyString_Check(o)) {
846 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
847 return NULL;
848 }
849 temp[x] = PyString_AsString(o);
850 }
851 return temp;
852}
853
e0672e2f
RD
854//--------------------------------
855// Part of patch from Tim Hochberg
856static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) {
857 if (PyInt_Check(o1) && PyInt_Check(o2)) {
858 point->x = PyInt_AS_LONG(o1);
859 point->y = PyInt_AS_LONG(o2);
860 return true;
861 }
862 if (PyFloat_Check(o1) && PyFloat_Check(o2)) {
863 point->x = (int)PyFloat_AS_DOUBLE(o1);
864 point->y = (int)PyFloat_AS_DOUBLE(o2);
865 return true;
866 }
867 if (PyInstance_Check(o1) || PyInstance_Check(o2)) {
868 // Disallow instances because they can cause havok
869 return false;
870 }
871 if (PyNumber_Check(o1) && PyNumber_Check(o2)) {
872 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
873 point->x = PyInt_AsLong(o1);
874 point->y = PyInt_AsLong(o2);
875 return true;
876 }
877 return false;
878}
7bf85405
RD
879
880
a7fa65b9
RD
881#if PYTHON_API_VERSION < 1009
882#define PySequence_Fast_GET_ITEM(o, i)\
883 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
884#endif
885
e0672e2f
RD
886wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) {
887 // Putting all of the declarations here allows
888 // us to put the error handling all in one place.
889 int x;
890 wxPoint* temp;
891 PyObject *o, *o1, *o2;
892 int isFast = PyList_Check(source) || PyTuple_Check(source);
893
894 // The length of the sequence is returned in count.
895 if (!PySequence_Check(source)) {
896 goto error0;
7bf85405 897 }
e0672e2f
RD
898 *count = PySequence_Length(source);
899 if (*count < 0) {
900 goto error0;
901 }
902
903 temp = new wxPoint[*count];
904 if (!temp) {
7bf85405
RD
905 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
906 return NULL;
907 }
e0672e2f
RD
908 for (x=0; x<*count; x++) {
909 // Get an item: try fast way first.
910 if (isFast) {
911 o = PySequence_Fast_GET_ITEM(source, x);
912 }
913 else {
914 o = PySequence_GetItem(source, x);
915 if (o == NULL) {
916 goto error1;
917 }
918 }
7bf85405 919
e0672e2f
RD
920 // Convert o to wxPoint.
921 if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) ||
922 (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) {
923 o1 = PySequence_Fast_GET_ITEM(o, 0);
924 o2 = PySequence_Fast_GET_ITEM(o, 1);
925 if (!wxPointFromObjects(o1, o2, &temp[x])) {
926 goto error2;
927 }
7bf85405 928 }
d559219f
RD
929 else if (PyInstance_Check(o)) {
930 wxPoint* pt;
e0672e2f
RD
931 if (SWIG_GetPtrObj(o, (void **)&pt, "_wxPoint_p")) {
932 goto error2;
d559219f
RD
933 }
934 temp[x] = *pt;
935 }
e0672e2f
RD
936 else if (PySequence_Check(o) && PySequence_Length(o) == 2) {
937 o1 = PySequence_GetItem(o, 0);
938 o2 = PySequence_GetItem(o, 1);
939 if (!wxPointFromObjects(o1, o2, &temp[x])) {
940 goto error3;
941 }
942 Py_DECREF(o1);
943 Py_DECREF(o2);
944 }
7bf85405 945 else {
e0672e2f 946 goto error2;
7bf85405 947 }
e0672e2f
RD
948 // Clean up.
949 if (!isFast)
950 Py_DECREF(o);
7bf85405
RD
951 }
952 return temp;
e0672e2f
RD
953
954error3:
955 Py_DECREF(o1);
956 Py_DECREF(o2);
957error2:
958 if (!isFast)
959 Py_DECREF(o);
960error1:
961 delete temp;
962error0:
963 PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints.");
964 return NULL;
7bf85405 965}
e0672e2f
RD
966// end of patch
967//------------------------------
7bf85405
RD
968
969
2f90df85 970wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
7bf85405
RD
971 if (!PyList_Check(source)) {
972 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
973 return NULL;
974 }
975 int count = PyList_Size(source);
976 wxBitmap** temp = new wxBitmap*[count];
977 if (! temp) {
978 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
979 return NULL;
980 }
981 for (int x=0; x<count; x++) {
982 PyObject* o = PyList_GetItem(source, x);
e166644c 983 if (PyInstance_Check(o)) {
7bf85405 984 wxBitmap* pt;
e166644c 985 if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) {
7bf85405
RD
986 PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p.");
987 return NULL;
988 }
989 temp[x] = pt;
990 }
991 else {
992 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
993 return NULL;
994 }
995 }
996 return temp;
997}
998
999
1000
2f90df85 1001wxString* wxString_LIST_helper(PyObject* source) {
7bf85405
RD
1002 if (!PyList_Check(source)) {
1003 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1004 return NULL;
1005 }
1006 int count = PyList_Size(source);
1007 wxString* temp = new wxString[count];
1008 if (! temp) {
1009 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1010 return NULL;
1011 }
1012 for (int x=0; x<count; x++) {
1013 PyObject* o = PyList_GetItem(source, x);
ecc08ead
RD
1014#if PYTHON_API_VERSION >= 1009
1015 if (! PyString_Check(o) && ! PyUnicode_Check(o)) {
1016 PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects.");
1017 return NULL;
1018 }
1019
1020 char* buff;
1021 int length;
1022 if (PyString_AsStringAndSize(o, &buff, &length) == -1)
1023 return NULL;
1024 temp[x] = wxString(buff, length);
1025#else
7bf85405
RD
1026 if (! PyString_Check(o)) {
1027 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
1028 return NULL;
1029 }
1030 temp[x] = PyString_AsString(o);
ecc08ead 1031#endif
7bf85405
RD
1032 }
1033 return temp;
1034}
1035
1036
2f90df85 1037wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
7bf85405
RD
1038 if (!PyList_Check(source)) {
1039 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
1040 return NULL;
1041 }
1042 int count = PyList_Size(source);
1043 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
1044 if (! temp) {
1045 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
1046 return NULL;
1047 }
1048 for (int x=0; x<count; x++) {
1049 PyObject* o = PyList_GetItem(source, x);
e166644c 1050 if (PyInstance_Check(o)) {
7bf85405 1051 wxAcceleratorEntry* ae;
e166644c 1052 if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) {
7bf85405
RD
1053 PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p.");
1054 return NULL;
1055 }
1056 temp[x] = *ae;
1057 }
1058 else if (PyTuple_Check(o)) {
1059 PyObject* o1 = PyTuple_GetItem(o, 0);
1060 PyObject* o2 = PyTuple_GetItem(o, 1);
1061 PyObject* o3 = PyTuple_GetItem(o, 2);
0adbc166 1062 temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3));
7bf85405
RD
1063 }
1064 else {
1065 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1066 return NULL;
1067 }
1068 }
1069 return temp;
1070}
1071
bb0054cd
RD
1072
1073
1074//----------------------------------------------------------------------
bb0054cd 1075
2f90df85
RD
1076bool wxSize_helper(PyObject* source, wxSize** obj) {
1077
1078 // If source is an object instance then it may already be the right type
1079 if (PyInstance_Check(source)) {
1080 wxSize* ptr;
1081 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p"))
1082 goto error;
1083 *obj = ptr;
1084 return TRUE;
1085 }
1086 // otherwise a 2-tuple of integers is expected
1087 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1088 PyObject* o1 = PySequence_GetItem(source, 0);
1089 PyObject* o2 = PySequence_GetItem(source, 1);
1090 **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2));
1091 return TRUE;
1092 }
1093
1094 error:
1095 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object.");
1096 return FALSE;
1097}
1098
1099bool wxPoint_helper(PyObject* source, wxPoint** obj) {
1100
1101 // If source is an object instance then it may already be the right type
1102 if (PyInstance_Check(source)) {
1103 wxPoint* ptr;
1104 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p"))
1105 goto error;
1106 *obj = ptr;
1107 return TRUE;
1108 }
e0672e2f
RD
1109 // otherwise a length-2 sequence of integers is expected
1110 if (PySequence_Check(source) && PySequence_Length(source) == 2) {
2f90df85
RD
1111 PyObject* o1 = PySequence_GetItem(source, 0);
1112 PyObject* o2 = PySequence_GetItem(source, 1);
e0672e2f
RD
1113 // This should really check for integers, not numbers -- but that would break code.
1114 if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
1115 Py_DECREF(o1);
1116 Py_DECREF(o2);
1117 goto error;
1118 }
1119 **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2));
1120 Py_DECREF(o1);
1121 Py_DECREF(o2);
2f90df85
RD
1122 return TRUE;
1123 }
2f90df85
RD
1124 error:
1125 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object.");
1126 return FALSE;
1127}
1128
1129
1130
1131bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) {
1132
1133 // If source is an object instance then it may already be the right type
1134 if (PyInstance_Check(source)) {
1135 wxRealPoint* ptr;
1136 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p"))
1137 goto error;
1138 *obj = ptr;
1139 return TRUE;
1140 }
1141 // otherwise a 2-tuple of floats is expected
1142 else if (PySequence_Check(source) && PyObject_Length(source) == 2) {
1143 PyObject* o1 = PySequence_GetItem(source, 0);
1144 PyObject* o2 = PySequence_GetItem(source, 1);
1145 **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
1146 return TRUE;
1147 }
1148
1149 error:
1150 PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object.");
1151 return FALSE;
1152}
1153
1154
1155
1156
1157bool wxRect_helper(PyObject* source, wxRect** obj) {
1158
1159 // If source is an object instance then it may already be the right type
1160 if (PyInstance_Check(source)) {
1161 wxRect* ptr;
1162 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p"))
1163 goto error;
1164 *obj = ptr;
1165 return TRUE;
1166 }
1167 // otherwise a 4-tuple of integers is expected
1168 else if (PySequence_Check(source) && PyObject_Length(source) == 4) {
1169 PyObject* o1 = PySequence_GetItem(source, 0);
1170 PyObject* o2 = PySequence_GetItem(source, 1);
1171 PyObject* o3 = PySequence_GetItem(source, 2);
1172 PyObject* o4 = PySequence_GetItem(source, 3);
1173 **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2),
1174 PyInt_AsLong(o3), PyInt_AsLong(o4));
1175 return TRUE;
1176 }
1177
1178 error:
1179 PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object.");
1180 return FALSE;
1181}
1182
1183
1184
f6bcfd97
BP
1185bool wxColour_helper(PyObject* source, wxColour** obj) {
1186
1187 // If source is an object instance then it may already be the right type
1188 if (PyInstance_Check(source)) {
1189 wxColour* ptr;
1190 if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p"))
1191 goto error;
1192 *obj = ptr;
1193 return TRUE;
1194 }
1195 // otherwise a string is expected
1196 else if (PyString_Check(source)) {
1197 wxString spec = PyString_AS_STRING(source);
1112b0c6 1198 if (spec[0U] == '#' && spec.Length() == 7) { // It's #RRGGBB
f6bcfd97
BP
1199 char* junk;
1200 int red = strtol(spec.Mid(1,2), &junk, 16);
1201 int green = strtol(spec.Mid(3,2), &junk, 16);
1202 int blue = strtol(spec.Mid(5,2), &junk, 16);
1203 **obj = wxColour(red, green, blue);
1204 return TRUE;
1205 }
1206 else { // it's a colour name
1207 **obj = wxColour(spec);
1208 return TRUE;
1209 }
1210 }
1211
1212 error:
de20db99 1213 PyErr_SetString(PyExc_TypeError, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
f6bcfd97
BP
1214 return FALSE;
1215}
1216
1217
bb0054cd
RD
1218//----------------------------------------------------------------------
1219//----------------------------------------------------------------------
7bf85405 1220//----------------------------------------------------------------------
7bf85405 1221
7bf85405 1222
7bf85405 1223
de20db99 1224